Branch data Line data Source code
1 : : /* Quicktime muxer plugin for GStreamer
2 : : * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
3 : : *
4 : : * This library is free software; you can redistribute it and/or
5 : : * modify it under the terms of the GNU Library General Public
6 : : * License as published by the Free Software Foundation; either
7 : : * version 2 of the License, or (at your option) any later version.
8 : : *
9 : : * This library is distributed in the hope that it will be useful,
10 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : : * Library General Public License for more details.
13 : : *
14 : : * You should have received a copy of the GNU Library General Public
15 : : * License along with this library; if not, write to the
16 : : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : : * Boston, MA 02111-1307, USA.
18 : : */
19 : : /*
20 : : * Unless otherwise indicated, Source Code is licensed under MIT license.
21 : : * See further explanation attached in License Statement (distributed in the file
22 : : * LICENSE).
23 : : *
24 : : * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 : : * this software and associated documentation files (the "Software"), to deal in
26 : : * the Software without restriction, including without limitation the rights to
27 : : * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 : : * of the Software, and to permit persons to whom the Software is furnished to do
29 : : * so, subject to the following conditions:
30 : : *
31 : : * The above copyright notice and this permission notice shall be included in all
32 : : * copies or substantial portions of the Software.
33 : : *
34 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 : : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 : : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 : : * SOFTWARE.
41 : : */
42 : :
43 : :
44 : : /**
45 : : * SECTION:gstqtmoovrecover
46 : : * @short_description: Utility element for recovering unfinished quicktime files
47 : : *
48 : : * <refsect2>
49 : : * <para>
50 : : * This element recovers quicktime files created with qtmux using the moov recovery feature.
51 : : * </para>
52 : : * <title>Example pipelines</title>
53 : : * <para>
54 : : * <programlisting>
55 : : * TODO
56 : : * </programlisting>
57 : : * </refsect2>
58 : : *
59 : : * Last reviewed on 2010-02-01
60 : : */
61 : :
62 : : #ifdef HAVE_CONFIG_H
63 : : #include "config.h"
64 : : #endif
65 : :
66 : : #include <glib/gstdio.h>
67 : : #include <gst/gst.h>
68 : :
69 : : #include "gstqtmoovrecover.h"
70 : :
71 : : GST_DEBUG_CATEGORY_STATIC (gst_qt_moov_recover_debug);
72 : : #define GST_CAT_DEFAULT gst_qt_moov_recover_debug
73 : :
74 : : /* QTMoovRecover signals and args */
75 : : enum
76 : : {
77 : : /* FILL ME */
78 : : LAST_SIGNAL
79 : : };
80 : :
81 : : enum
82 : : {
83 : : PROP_0,
84 : : PROP_RECOVERY_INPUT,
85 : : PROP_BROKEN_INPUT,
86 : : PROP_FIXED_OUTPUT,
87 : : PROP_FAST_START_MODE
88 : : };
89 : :
90 [ + - ]: 27 : GST_BOILERPLATE (GstQTMoovRecover, gst_qt_moov_recover, GstPipeline,
91 : 27 : GST_TYPE_PIPELINE);
92 : :
93 : : /* property functions */
94 : : static void gst_qt_moov_recover_set_property (GObject * object,
95 : : guint prop_id, const GValue * value, GParamSpec * pspec);
96 : : static void gst_qt_moov_recover_get_property (GObject * object,
97 : : guint prop_id, GValue * value, GParamSpec * pspec);
98 : :
99 : : static GstStateChangeReturn gst_qt_moov_recover_change_state (GstElement *
100 : : element, GstStateChange transition);
101 : :
102 : : static void gst_qt_moov_recover_finalize (GObject * object);
103 : :
104 : : static void
105 : 6 : gst_qt_moov_recover_base_init (gpointer g_class)
106 : : {
107 : 6 : GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108 : : #if 0
109 : : GstQTMoovRecoverClass *klass = (GstQTMoovRecoverClass *) g_class;
110 : : #endif
111 : 6 : gst_element_class_set_details_simple (element_class, "QT Moov Recover",
112 : : "Util", "Recovers unfinished qtmux files",
113 : : "Thiago Santos <thiago.sousa.santos@collabora.co.uk>");
114 : 6 : }
115 : :
116 : : static void
117 : 6 : gst_qt_moov_recover_class_init (GstQTMoovRecoverClass * klass)
118 : : {
119 : : GObjectClass *gobject_class;
120 : : GstElementClass *gstelement_class;
121 : :
122 : 6 : gobject_class = (GObjectClass *) klass;
123 : 6 : gstelement_class = (GstElementClass *) klass;
124 : :
125 : 6 : parent_class = g_type_class_peek_parent (klass);
126 : :
127 : 6 : gobject_class->finalize = gst_qt_moov_recover_finalize;
128 : 6 : gobject_class->get_property = gst_qt_moov_recover_get_property;
129 : 6 : gobject_class->set_property = gst_qt_moov_recover_set_property;
130 : :
131 : 6 : gstelement_class->change_state = gst_qt_moov_recover_change_state;
132 : :
133 : 6 : g_object_class_install_property (gobject_class, PROP_FIXED_OUTPUT,
134 : : g_param_spec_string ("fixed-output",
135 : : "Path to write the fixed file",
136 : : "Path to write the fixed file to (used as output)",
137 : : NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 : 6 : g_object_class_install_property (gobject_class, PROP_BROKEN_INPUT,
139 : : g_param_spec_string ("broken-input",
140 : : "Path to broken input file",
141 : : "Path to broken input file. (If qtmux was on faststart mode, this "
142 : : "file is the faststart file)", NULL,
143 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 : 6 : g_object_class_install_property (gobject_class, PROP_RECOVERY_INPUT,
145 : : g_param_spec_string ("recovery-input",
146 : : "Path to recovery file",
147 : : "Path to recovery file (used as input)", NULL,
148 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 : 6 : g_object_class_install_property (gobject_class, PROP_FAST_START_MODE,
150 : : g_param_spec_boolean ("faststart-mode",
151 : : "If the broken input is from faststart mode",
152 : : "If the broken input is from faststart mode",
153 : : FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 : :
155 [ + - ]: 6 : GST_DEBUG_CATEGORY_INIT (gst_qt_moov_recover_debug, "qtmoovrecover", 0,
156 : : "QT Moovie Recover");
157 : 6 : }
158 : :
159 : : static void
160 : 4 : gst_qt_moov_recover_init (GstQTMoovRecover * qtmr,
161 : : GstQTMoovRecoverClass * qtmr_klass)
162 : : {
163 : 4 : }
164 : :
165 : : static void
166 : 4 : gst_qt_moov_recover_finalize (GObject * object)
167 : : {
168 : 4 : G_OBJECT_CLASS (parent_class)->finalize (object);
169 : 4 : }
170 : :
171 : : static void
172 : 6 : gst_qt_moov_recover_run (void *data)
173 : : {
174 : 6 : FILE *moovrec = NULL;
175 : 6 : FILE *mdatinput = NULL;
176 : 6 : FILE *output = NULL;
177 : 6 : MdatRecovFile *mdat_recov = NULL;
178 : 6 : MoovRecovFile *moov_recov = NULL;
179 : 6 : GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (data);
180 : 6 : GError *err = NULL;
181 : :
182 [ - + ]: 6 : GST_LOG_OBJECT (qtmr, "Starting task");
183 : :
184 [ - + ]: 6 : GST_DEBUG_OBJECT (qtmr, "Validating properties");
185 : 6 : GST_OBJECT_LOCK (qtmr);
186 : : /* validate properties */
187 [ + - ]: 6 : if (qtmr->broken_input == NULL) {
188 : 6 : GST_OBJECT_UNLOCK (qtmr);
189 [ + - ][ - + ]: 6 : GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
[ - + ][ # # ]
190 : : ("Please set broken-input property"), (NULL));
191 : 6 : goto end;
192 : : }
193 [ # # ]: 0 : if (qtmr->recovery_input == NULL) {
194 : 0 : GST_OBJECT_UNLOCK (qtmr);
195 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
[ # # ][ # # ]
196 : : ("Please set recovery-input property"), (NULL));
197 : 0 : goto end;
198 : : }
199 [ # # ]: 0 : if (qtmr->fixed_output == NULL) {
200 : 0 : GST_OBJECT_UNLOCK (qtmr);
201 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
[ # # ][ # # ]
202 : : ("Please set fixed-output property"), (NULL));
203 : 0 : goto end;
204 : : }
205 : :
206 [ # # ]: 0 : GST_DEBUG_OBJECT (qtmr, "Opening input/output files");
207 : : /* open files */
208 : 0 : moovrec = g_fopen (qtmr->recovery_input, "rb");
209 [ # # ]: 0 : if (moovrec == NULL) {
210 : 0 : GST_OBJECT_UNLOCK (qtmr);
211 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ,
[ # # ][ # # ]
212 : : ("Failed to open recovery-input file"), (NULL));
213 : 0 : goto end;
214 : : }
215 : :
216 : 0 : mdatinput = g_fopen (qtmr->broken_input, "rb");
217 [ # # ]: 0 : if (mdatinput == NULL) {
218 : 0 : GST_OBJECT_UNLOCK (qtmr);
219 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ,
[ # # ][ # # ]
220 : : ("Failed to open broken-input file"), (NULL));
221 : 0 : goto end;
222 : : }
223 : 0 : output = g_fopen (qtmr->fixed_output, "wb+");
224 [ # # ]: 0 : if (output == NULL) {
225 : 0 : GST_OBJECT_UNLOCK (qtmr);
226 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ_WRITE,
[ # # ][ # # ]
227 : : ("Failed to open fixed-output file"), (NULL));
228 : 0 : goto end;
229 : : }
230 : 0 : GST_OBJECT_UNLOCK (qtmr);
231 : :
232 [ # # ]: 0 : GST_DEBUG_OBJECT (qtmr, "Parsing input files");
233 : : /* now create our structures */
234 : 0 : mdat_recov = mdat_recov_file_create (mdatinput, qtmr->faststart_mode, &err);
235 : 0 : mdatinput = NULL;
236 [ # # ]: 0 : if (mdat_recov == NULL) {
237 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED,
[ # # ][ # # ]
238 : : ("Broken file could not be parsed correctly"), (NULL));
239 : 0 : goto end;
240 : : }
241 : 0 : moov_recov = moov_recov_file_create (moovrec, &err);
242 : 0 : moovrec = NULL;
243 [ # # ]: 0 : if (moov_recov == NULL) {
244 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED,
[ # # ][ # # ]
245 : : ("Recovery file could not be parsed correctly"), (NULL));
246 : 0 : goto end;
247 : : }
248 : :
249 : : /* now parse the buffers data from moovrec */
250 [ # # ]: 0 : if (!moov_recov_parse_buffers (moov_recov, mdat_recov, &err)) {
251 : 0 : goto end;
252 : : }
253 : :
254 [ # # ]: 0 : GST_DEBUG_OBJECT (qtmr, "Writing fixed file to output");
255 [ # # ]: 0 : if (!moov_recov_write_file (moov_recov, mdat_recov, output, &err)) {
256 : 0 : goto end;
257 : : }
258 : :
259 : : /* here means success */
260 [ # # ]: 0 : GST_DEBUG_OBJECT (qtmr, "Finished successfully, posting EOS");
261 : 0 : gst_element_post_message (GST_ELEMENT_CAST (qtmr),
262 : : gst_message_new_eos (GST_OBJECT_CAST (qtmr)));
263 : :
264 : : end:
265 [ - + ]: 6 : GST_LOG_OBJECT (qtmr, "Finalizing task");
266 [ - + ]: 6 : if (err) {
267 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED, ("%s", err->message), (NULL));
[ # # ][ # # ]
268 : 0 : g_error_free (err);
269 : : }
270 : :
271 [ - + ]: 6 : if (moov_recov)
272 : 0 : moov_recov_file_free (moov_recov);
273 [ - + ]: 6 : if (moovrec)
274 : 0 : fclose (moovrec);
275 : :
276 [ - + ]: 6 : if (mdat_recov)
277 : 0 : mdat_recov_file_free (mdat_recov);
278 [ - + ]: 6 : if (mdatinput)
279 : 0 : fclose (mdatinput);
280 : :
281 [ - + ]: 6 : if (output)
282 : 0 : fclose (output);
283 [ - + ]: 6 : GST_LOG_OBJECT (qtmr, "Leaving task");
284 : 6 : gst_task_stop (qtmr->task);
285 : 6 : }
286 : :
287 : : static void
288 : 4 : gst_qt_moov_recover_get_property (GObject * object,
289 : : guint prop_id, GValue * value, GParamSpec * pspec)
290 : : {
291 : 4 : GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (object);
292 : :
293 : 4 : GST_OBJECT_LOCK (qtmr);
294 [ + + + + : 4 : switch (prop_id) {
- ]
295 : : case PROP_FAST_START_MODE:
296 : 1 : g_value_set_boolean (value, qtmr->faststart_mode);
297 : 1 : break;
298 : : case PROP_BROKEN_INPUT:
299 : 1 : g_value_set_string (value, qtmr->broken_input);
300 : 1 : break;
301 : : case PROP_RECOVERY_INPUT:
302 : 1 : g_value_set_string (value, qtmr->recovery_input);
303 : 1 : break;
304 : : case PROP_FIXED_OUTPUT:
305 : 1 : g_value_set_string (value, qtmr->fixed_output);
306 : 1 : break;
307 : : default:
308 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
309 : 0 : break;
310 : : }
311 : 4 : GST_OBJECT_UNLOCK (qtmr);
312 : 4 : }
313 : :
314 : : static void
315 : 0 : gst_qt_moov_recover_set_property (GObject * object,
316 : : guint prop_id, const GValue * value, GParamSpec * pspec)
317 : : {
318 : 0 : GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (object);
319 : :
320 : 0 : GST_OBJECT_LOCK (qtmr);
321 [ # # # # : 0 : switch (prop_id) {
# ]
322 : : case PROP_FAST_START_MODE:
323 : 0 : qtmr->faststart_mode = g_value_get_boolean (value);
324 : 0 : break;
325 : : case PROP_BROKEN_INPUT:
326 : 0 : g_free (qtmr->broken_input);
327 : 0 : qtmr->broken_input = g_value_dup_string (value);
328 : 0 : break;
329 : : case PROP_RECOVERY_INPUT:
330 : 0 : g_free (qtmr->recovery_input);
331 : 0 : qtmr->recovery_input = g_value_dup_string (value);
332 : 0 : break;
333 : : case PROP_FIXED_OUTPUT:
334 : 0 : g_free (qtmr->fixed_output);
335 : 0 : qtmr->fixed_output = g_value_dup_string (value);
336 : 0 : break;
337 : : default:
338 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
339 : 0 : break;
340 : : }
341 : 0 : GST_OBJECT_UNLOCK (qtmr);
342 : 0 : }
343 : :
344 : : static GstStateChangeReturn
345 : 34 : gst_qt_moov_recover_change_state (GstElement * element,
346 : : GstStateChange transition)
347 : : {
348 : : GstStateChangeReturn ret;
349 : 34 : GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (element);
350 : :
351 [ + + + ]: 34 : switch (transition) {
352 : : case GST_STATE_CHANGE_NULL_TO_READY:
353 : 4 : qtmr->task = gst_task_create (gst_qt_moov_recover_run, qtmr);
354 : 4 : qtmr->task_mutex = g_new (GStaticRecMutex, 1);
355 : 4 : g_static_rec_mutex_init (qtmr->task_mutex);
356 : 4 : gst_task_set_lock (qtmr->task, qtmr->task_mutex);
357 : 4 : break;
358 : : case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
359 : 6 : gst_task_start (qtmr->task);
360 : 6 : break;
361 : : default:
362 : 24 : break;
363 : : }
364 : :
365 : 34 : ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
366 : :
367 [ + + + ]: 34 : switch (transition) {
368 : : case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
369 : 6 : gst_task_stop (qtmr->task);
370 : 6 : gst_task_join (qtmr->task);
371 : 6 : break;
372 : : case GST_STATE_CHANGE_READY_TO_NULL:
373 [ - + ]: 4 : g_assert (gst_task_get_state (qtmr->task) == GST_TASK_STOPPED);
374 : 4 : gst_object_unref (qtmr->task);
375 : 4 : qtmr->task = NULL;
376 : 4 : g_static_rec_mutex_free (qtmr->task_mutex);
377 : 4 : break;
378 : : default:
379 : 24 : break;
380 : : }
381 : 34 : return ret;
382 : : }
383 : :
384 : :
385 : : gboolean
386 : 21 : gst_qt_moov_recover_register (GstPlugin * plugin)
387 : : {
388 : 21 : return gst_element_register (plugin, "qtmoovrecover", GST_RANK_NONE,
389 : : GST_TYPE_QT_MOOV_RECOVER);
390 : : }
|