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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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:element-qtmoovrecover
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
51  * recovery feature.
52  * </para>
53  * <title>Example pipelines</title>
54  * <para>
55  * <programlisting>
56  * TODO
57  * </programlisting>
58  * </para>
59  * </refsect2>
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 #define gst_qt_moov_recover_parent_class parent_class
91 G_DEFINE_TYPE (GstQTMoovRecover, gst_qt_moov_recover, 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
gst_qt_moov_recover_class_init(GstQTMoovRecoverClass * klass)105 gst_qt_moov_recover_class_init (GstQTMoovRecoverClass * klass)
106 {
107   GObjectClass *gobject_class;
108   GstElementClass *gstelement_class;
109 
110   gobject_class = (GObjectClass *) klass;
111   gstelement_class = (GstElementClass *) klass;
112 
113   parent_class = g_type_class_peek_parent (klass);
114 
115   gobject_class->finalize = gst_qt_moov_recover_finalize;
116   gobject_class->get_property = gst_qt_moov_recover_get_property;
117   gobject_class->set_property = gst_qt_moov_recover_set_property;
118 
119   gstelement_class->change_state = gst_qt_moov_recover_change_state;
120 
121   g_object_class_install_property (gobject_class, PROP_FIXED_OUTPUT,
122       g_param_spec_string ("fixed-output",
123           "Path to write the fixed file",
124           "Path to write the fixed file to (used as output)",
125           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126   g_object_class_install_property (gobject_class, PROP_BROKEN_INPUT,
127       g_param_spec_string ("broken-input",
128           "Path to broken input file",
129           "Path to broken input file. (If qtmux was on faststart mode, this "
130           "file is the faststart file)", NULL,
131           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132   g_object_class_install_property (gobject_class, PROP_RECOVERY_INPUT,
133       g_param_spec_string ("recovery-input",
134           "Path to recovery file",
135           "Path to recovery file (used as input)", NULL,
136           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
137   g_object_class_install_property (gobject_class, PROP_FAST_START_MODE,
138       g_param_spec_boolean ("faststart-mode",
139           "If the broken input is from faststart mode",
140           "If the broken input is from faststart mode",
141           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
142 
143   GST_DEBUG_CATEGORY_INIT (gst_qt_moov_recover_debug, "qtmoovrecover", 0,
144       "QT Moovie Recover");
145 
146   gst_element_class_set_static_metadata (gstelement_class, "QT Moov Recover",
147       "Util", "Recovers unfinished qtmux files",
148       "Thiago Santos <thiago.sousa.santos@collabora.co.uk>");
149 }
150 
151 static void
gst_qt_moov_recover_init(GstQTMoovRecover * qtmr)152 gst_qt_moov_recover_init (GstQTMoovRecover * qtmr)
153 {
154 }
155 
156 static void
gst_qt_moov_recover_finalize(GObject * object)157 gst_qt_moov_recover_finalize (GObject * object)
158 {
159   G_OBJECT_CLASS (parent_class)->finalize (object);
160 }
161 
162 static void
gst_qt_moov_recover_run(void * data)163 gst_qt_moov_recover_run (void *data)
164 {
165   FILE *moovrec = NULL;
166   FILE *mdatinput = NULL;
167   FILE *output = NULL;
168   MdatRecovFile *mdat_recov = NULL;
169   MoovRecovFile *moov_recov = NULL;
170   GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (data);
171   GError *err = NULL;
172   GError *warn = NULL;
173 
174   GST_LOG_OBJECT (qtmr, "Starting task");
175 
176   GST_DEBUG_OBJECT (qtmr, "Validating properties");
177   GST_OBJECT_LOCK (qtmr);
178   /* validate properties */
179   if (qtmr->broken_input == NULL) {
180     GST_OBJECT_UNLOCK (qtmr);
181     GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
182         ("Please set broken-input property"), (NULL));
183     goto end;
184   }
185   if (qtmr->recovery_input == NULL) {
186     GST_OBJECT_UNLOCK (qtmr);
187     GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
188         ("Please set recovery-input property"), (NULL));
189     goto end;
190   }
191   if (qtmr->fixed_output == NULL) {
192     GST_OBJECT_UNLOCK (qtmr);
193     GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
194         ("Please set fixed-output property"), (NULL));
195     goto end;
196   }
197 
198   GST_DEBUG_OBJECT (qtmr, "Opening input/output files");
199   /* open files */
200   moovrec = g_fopen (qtmr->recovery_input, "rb");
201   if (moovrec == NULL) {
202     GST_OBJECT_UNLOCK (qtmr);
203     GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ,
204         ("Failed to open recovery-input file"), (NULL));
205     goto end;
206   }
207 
208   mdatinput = g_fopen (qtmr->broken_input, "rb");
209   if (mdatinput == NULL) {
210     GST_OBJECT_UNLOCK (qtmr);
211     GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ,
212         ("Failed to open broken-input file"), (NULL));
213     goto end;
214   }
215   output = g_fopen (qtmr->fixed_output, "wb+");
216   if (output == NULL) {
217     GST_OBJECT_UNLOCK (qtmr);
218     GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ_WRITE,
219         ("Failed to open fixed-output file"), (NULL));
220     goto end;
221   }
222   GST_OBJECT_UNLOCK (qtmr);
223 
224   GST_DEBUG_OBJECT (qtmr, "Parsing input files");
225   /* now create our structures */
226   mdat_recov = mdat_recov_file_create (mdatinput, qtmr->faststart_mode, &err);
227   mdatinput = NULL;
228   if (mdat_recov == NULL) {
229     GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED,
230         ("Broken file could not be parsed correctly"), (NULL));
231     goto end;
232   }
233   moov_recov = moov_recov_file_create (moovrec, &err);
234   moovrec = NULL;
235   if (moov_recov == NULL) {
236     GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED,
237         ("Recovery file could not be parsed correctly"), (NULL));
238     goto end;
239   }
240 
241   /* now parse the buffers data from moovrec */
242   if (!moov_recov_parse_buffers (moov_recov, mdat_recov, &err)) {
243     goto end;
244   }
245 
246   GST_DEBUG_OBJECT (qtmr, "Writing fixed file to output");
247   if (!moov_recov_write_file (moov_recov, mdat_recov, output, &err, &warn)) {
248     goto end;
249   }
250 
251   if (warn) {
252     GST_ELEMENT_WARNING (qtmr, RESOURCE, FAILED, ("%s", warn->message), (NULL));
253     g_error_free (warn);
254   }
255 
256   /* here means success */
257   GST_DEBUG_OBJECT (qtmr, "Finished successfully, posting EOS");
258   gst_element_post_message (GST_ELEMENT_CAST (qtmr),
259       gst_message_new_eos (GST_OBJECT_CAST (qtmr)));
260 
261 end:
262   GST_LOG_OBJECT (qtmr, "Finalizing task");
263   if (err) {
264     GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED, ("%s", err->message), (NULL));
265     g_error_free (err);
266   }
267 
268   if (moov_recov)
269     moov_recov_file_free (moov_recov);
270   if (moovrec)
271     fclose (moovrec);
272 
273   if (mdat_recov)
274     mdat_recov_file_free (mdat_recov);
275   if (mdatinput)
276     fclose (mdatinput);
277 
278   if (output)
279     fclose (output);
280   GST_LOG_OBJECT (qtmr, "Leaving task");
281   gst_task_stop (qtmr->task);
282 }
283 
284 static void
gst_qt_moov_recover_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)285 gst_qt_moov_recover_get_property (GObject * object,
286     guint prop_id, GValue * value, GParamSpec * pspec)
287 {
288   GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (object);
289 
290   GST_OBJECT_LOCK (qtmr);
291   switch (prop_id) {
292     case PROP_FAST_START_MODE:
293       g_value_set_boolean (value, qtmr->faststart_mode);
294       break;
295     case PROP_BROKEN_INPUT:
296       g_value_set_string (value, qtmr->broken_input);
297       break;
298     case PROP_RECOVERY_INPUT:
299       g_value_set_string (value, qtmr->recovery_input);
300       break;
301     case PROP_FIXED_OUTPUT:
302       g_value_set_string (value, qtmr->fixed_output);
303       break;
304     default:
305       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306       break;
307   }
308   GST_OBJECT_UNLOCK (qtmr);
309 }
310 
311 static void
gst_qt_moov_recover_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)312 gst_qt_moov_recover_set_property (GObject * object,
313     guint prop_id, const GValue * value, GParamSpec * pspec)
314 {
315   GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (object);
316 
317   GST_OBJECT_LOCK (qtmr);
318   switch (prop_id) {
319     case PROP_FAST_START_MODE:
320       qtmr->faststart_mode = g_value_get_boolean (value);
321       break;
322     case PROP_BROKEN_INPUT:
323       g_free (qtmr->broken_input);
324       qtmr->broken_input = g_value_dup_string (value);
325       break;
326     case PROP_RECOVERY_INPUT:
327       g_free (qtmr->recovery_input);
328       qtmr->recovery_input = g_value_dup_string (value);
329       break;
330     case PROP_FIXED_OUTPUT:
331       g_free (qtmr->fixed_output);
332       qtmr->fixed_output = g_value_dup_string (value);
333       break;
334     default:
335       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336       break;
337   }
338   GST_OBJECT_UNLOCK (qtmr);
339 }
340 
341 static GstStateChangeReturn
gst_qt_moov_recover_change_state(GstElement * element,GstStateChange transition)342 gst_qt_moov_recover_change_state (GstElement * element,
343     GstStateChange transition)
344 {
345   GstStateChangeReturn ret;
346   GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (element);
347 
348   switch (transition) {
349     case GST_STATE_CHANGE_NULL_TO_READY:
350       qtmr->task = gst_task_new (gst_qt_moov_recover_run, qtmr, NULL);
351       g_rec_mutex_init (&qtmr->task_mutex);
352       gst_task_set_lock (qtmr->task, &qtmr->task_mutex);
353       break;
354     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
355       gst_task_start (qtmr->task);
356       break;
357     default:
358       break;
359   }
360 
361   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
362 
363   switch (transition) {
364     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
365       gst_task_stop (qtmr->task);
366       gst_task_join (qtmr->task);
367       break;
368     case GST_STATE_CHANGE_READY_TO_NULL:
369       if (gst_task_get_state (qtmr->task) != GST_TASK_STOPPED)
370         GST_ERROR ("task %p should be stopped by now", qtmr->task);
371       gst_object_unref (qtmr->task);
372       qtmr->task = NULL;
373       g_rec_mutex_clear (&qtmr->task_mutex);
374       break;
375     default:
376       break;
377   }
378   return ret;
379 }
380 
381 
382 gboolean
gst_qt_moov_recover_register(GstPlugin * plugin)383 gst_qt_moov_recover_register (GstPlugin * plugin)
384 {
385   return gst_element_register (plugin, "qtmoovrecover", GST_RANK_NONE,
386       GST_TYPE_QT_MOOV_RECOVER);
387 }
388