blob: 80b22ebcb9fc47eca990b1c5a5fd5cb568b363a1 [file] [log] [blame]
Thiago Santosb692f9f2009-12-12 16:07:15 -03001/* 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
Tim-Philipp Müller230cf412012-11-04 00:07:18 +000016 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
Thiago Santosb692f9f2009-12-12 16:07:15 -030018 */
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/**
Tim-Philipp Müller3346a532011-04-21 23:30:26 +010045 * SECTION:element-qtmoovrecover
Thiago Santosb692f9f2009-12-12 16:07:15 -030046 * @short_description: Utility element for recovering unfinished quicktime files
47 *
48 * <refsect2>
49 * <para>
Tim-Philipp Müller3346a532011-04-21 23:30:26 +010050 * This element recovers quicktime files created with qtmux using the moov
51 * recovery feature.
Thiago Santosb692f9f2009-12-12 16:07:15 -030052 * </para>
53 * <title>Example pipelines</title>
54 * <para>
55 * <programlisting>
56 * TODO
57 * </programlisting>
Tim-Philipp Müller3346a532011-04-21 23:30:26 +010058 * </para>
Thiago Santosb692f9f2009-12-12 16:07:15 -030059 * </refsect2>
Thiago Santosb692f9f2009-12-12 16:07:15 -030060 */
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
71GST_DEBUG_CATEGORY_STATIC (gst_qt_moov_recover_debug);
72#define GST_CAT_DEFAULT gst_qt_moov_recover_debug
73
Thiago Santosb692f9f2009-12-12 16:07:15 -030074/* QTMoovRecover signals and args */
75enum
76{
77 /* FILL ME */
78 LAST_SIGNAL
79};
80
81enum
82{
83 PROP_0,
84 PROP_RECOVERY_INPUT,
85 PROP_BROKEN_INPUT,
86 PROP_FIXED_OUTPUT,
87 PROP_FAST_START_MODE
88};
89
Mark Nauwelaertsf8866812011-06-29 12:46:20 +020090#define gst_qt_moov_recover_parent_class parent_class
91G_DEFINE_TYPE (GstQTMoovRecover, gst_qt_moov_recover, GST_TYPE_PIPELINE);
Thiago Santosb692f9f2009-12-12 16:07:15 -030092
93/* property functions */
94static void gst_qt_moov_recover_set_property (GObject * object,
95 guint prop_id, const GValue * value, GParamSpec * pspec);
96static void gst_qt_moov_recover_get_property (GObject * object,
97 guint prop_id, GValue * value, GParamSpec * pspec);
98
99static GstStateChangeReturn gst_qt_moov_recover_change_state (GstElement *
100 element, GstStateChange transition);
101
102static void gst_qt_moov_recover_finalize (GObject * object);
103
104static void
Thiago Santosb692f9f2009-12-12 16:07:15 -0300105gst_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)",
Stefan Kost1d1ffe22010-10-19 13:43:14 +0300125 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300126 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 "
Stefan Kost1d1ffe22010-10-19 13:43:14 +0300130 "file is the faststart file)", NULL,
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300132 g_object_class_install_property (gobject_class, PROP_RECOVERY_INPUT,
133 g_param_spec_string ("recovery-input",
134 "Path to recovery file",
Stefan Kost1d1ffe22010-10-19 13:43:14 +0300135 "Path to recovery file (used as input)", NULL,
136 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300137 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",
Stefan Kost1d1ffe22010-10-19 13:43:14 +0300141 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300142
143 GST_DEBUG_CATEGORY_INIT (gst_qt_moov_recover_debug, "qtmoovrecover", 0,
144 "QT Moovie Recover");
Mark Nauwelaertsf8866812011-06-29 12:46:20 +0200145
Tim-Philipp Müllere09ae572012-04-10 00:51:41 +0100146 gst_element_class_set_static_metadata (gstelement_class, "QT Moov Recover",
Mark Nauwelaertsf8866812011-06-29 12:46:20 +0200147 "Util", "Recovers unfinished qtmux files",
148 "Thiago Santos <thiago.sousa.santos@collabora.co.uk>");
Thiago Santosb692f9f2009-12-12 16:07:15 -0300149}
150
151static void
Mark Nauwelaertsf8866812011-06-29 12:46:20 +0200152gst_qt_moov_recover_init (GstQTMoovRecover * qtmr)
Thiago Santosb692f9f2009-12-12 16:07:15 -0300153{
154}
155
156static void
157gst_qt_moov_recover_finalize (GObject * object)
158{
159 G_OBJECT_CLASS (parent_class)->finalize (object);
160}
161
162static void
163gst_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
173 GST_LOG_OBJECT (qtmr, "Starting task");
174
175 GST_DEBUG_OBJECT (qtmr, "Validating properties");
176 GST_OBJECT_LOCK (qtmr);
177 /* validate properties */
178 if (qtmr->broken_input == NULL) {
179 GST_OBJECT_UNLOCK (qtmr);
180 GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000181 ("Please set broken-input property"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300182 goto end;
183 }
184 if (qtmr->recovery_input == NULL) {
185 GST_OBJECT_UNLOCK (qtmr);
186 GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000187 ("Please set recovery-input property"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300188 goto end;
189 }
190 if (qtmr->fixed_output == NULL) {
191 GST_OBJECT_UNLOCK (qtmr);
192 GST_ELEMENT_ERROR (qtmr, RESOURCE, SETTINGS,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000193 ("Please set fixed-output property"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300194 goto end;
195 }
196
197 GST_DEBUG_OBJECT (qtmr, "Opening input/output files");
198 /* open files */
199 moovrec = g_fopen (qtmr->recovery_input, "rb");
200 if (moovrec == NULL) {
201 GST_OBJECT_UNLOCK (qtmr);
202 GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000203 ("Failed to open recovery-input file"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300204 goto end;
205 }
206
207 mdatinput = g_fopen (qtmr->broken_input, "rb");
208 if (mdatinput == NULL) {
209 GST_OBJECT_UNLOCK (qtmr);
210 GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000211 ("Failed to open broken-input file"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300212 goto end;
213 }
214 output = g_fopen (qtmr->fixed_output, "wb+");
215 if (output == NULL) {
216 GST_OBJECT_UNLOCK (qtmr);
217 GST_ELEMENT_ERROR (qtmr, RESOURCE, OPEN_READ_WRITE,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000218 ("Failed to open fixed-output file"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300219 goto end;
220 }
221 GST_OBJECT_UNLOCK (qtmr);
222
223 GST_DEBUG_OBJECT (qtmr, "Parsing input files");
224 /* now create our structures */
225 mdat_recov = mdat_recov_file_create (mdatinput, qtmr->faststart_mode, &err);
226 mdatinput = NULL;
227 if (mdat_recov == NULL) {
228 GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000229 ("Broken file could not be parsed correctly"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300230 goto end;
231 }
232 moov_recov = moov_recov_file_create (moovrec, &err);
233 moovrec = NULL;
234 if (moov_recov == NULL) {
235 GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED,
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000236 ("Recovery file could not be parsed correctly"), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300237 goto end;
238 }
239
240 /* now parse the buffers data from moovrec */
241 if (!moov_recov_parse_buffers (moov_recov, mdat_recov, &err)) {
242 goto end;
243 }
244
245 GST_DEBUG_OBJECT (qtmr, "Writing fixed file to output");
246 if (!moov_recov_write_file (moov_recov, mdat_recov, output, &err)) {
247 goto end;
248 }
249
250 /* here means success */
251 GST_DEBUG_OBJECT (qtmr, "Finished successfully, posting EOS");
252 gst_element_post_message (GST_ELEMENT_CAST (qtmr),
253 gst_message_new_eos (GST_OBJECT_CAST (qtmr)));
254
255end:
256 GST_LOG_OBJECT (qtmr, "Finalizing task");
257 if (err) {
Tim-Philipp Müllerf9bba012010-03-11 17:17:15 +0000258 GST_ELEMENT_ERROR (qtmr, RESOURCE, FAILED, ("%s", err->message), (NULL));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300259 g_error_free (err);
260 }
261
262 if (moov_recov)
263 moov_recov_file_free (moov_recov);
264 if (moovrec)
265 fclose (moovrec);
266
267 if (mdat_recov)
268 mdat_recov_file_free (mdat_recov);
269 if (mdatinput)
270 fclose (mdatinput);
271
272 if (output)
273 fclose (output);
274 GST_LOG_OBJECT (qtmr, "Leaving task");
275 gst_task_stop (qtmr->task);
276}
277
278static void
279gst_qt_moov_recover_get_property (GObject * object,
280 guint prop_id, GValue * value, GParamSpec * pspec)
281{
282 GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (object);
283
284 GST_OBJECT_LOCK (qtmr);
285 switch (prop_id) {
286 case PROP_FAST_START_MODE:
287 g_value_set_boolean (value, qtmr->faststart_mode);
288 break;
289 case PROP_BROKEN_INPUT:
290 g_value_set_string (value, qtmr->broken_input);
291 break;
292 case PROP_RECOVERY_INPUT:
293 g_value_set_string (value, qtmr->recovery_input);
294 break;
295 case PROP_FIXED_OUTPUT:
296 g_value_set_string (value, qtmr->fixed_output);
297 break;
298 default:
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 break;
301 }
302 GST_OBJECT_UNLOCK (qtmr);
303}
304
305static void
306gst_qt_moov_recover_set_property (GObject * object,
307 guint prop_id, const GValue * value, GParamSpec * pspec)
308{
309 GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (object);
310
311 GST_OBJECT_LOCK (qtmr);
312 switch (prop_id) {
313 case PROP_FAST_START_MODE:
314 qtmr->faststart_mode = g_value_get_boolean (value);
315 break;
316 case PROP_BROKEN_INPUT:
317 g_free (qtmr->broken_input);
318 qtmr->broken_input = g_value_dup_string (value);
319 break;
320 case PROP_RECOVERY_INPUT:
321 g_free (qtmr->recovery_input);
322 qtmr->recovery_input = g_value_dup_string (value);
323 break;
324 case PROP_FIXED_OUTPUT:
325 g_free (qtmr->fixed_output);
326 qtmr->fixed_output = g_value_dup_string (value);
327 break;
328 default:
329 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 break;
331 }
332 GST_OBJECT_UNLOCK (qtmr);
333}
334
335static GstStateChangeReturn
336gst_qt_moov_recover_change_state (GstElement * element,
337 GstStateChange transition)
338{
339 GstStateChangeReturn ret;
340 GstQTMoovRecover *qtmr = GST_QT_MOOV_RECOVER_CAST (element);
341
342 switch (transition) {
343 case GST_STATE_CHANGE_NULL_TO_READY:
Wim Taymans30d3dfe2012-06-20 10:33:42 +0200344 qtmr->task = gst_task_new (gst_qt_moov_recover_run, qtmr, NULL);
Wim Taymans15848062012-01-19 11:33:53 +0100345 g_rec_mutex_init (&qtmr->task_mutex);
Sebastian Drögebf046e02011-04-14 13:43:06 +0200346 gst_task_set_lock (qtmr->task, &qtmr->task_mutex);
Thiago Santosb692f9f2009-12-12 16:07:15 -0300347 break;
348 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
349 gst_task_start (qtmr->task);
350 break;
351 default:
352 break;
353 }
354
355 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
356
357 switch (transition) {
358 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
359 gst_task_stop (qtmr->task);
360 gst_task_join (qtmr->task);
361 break;
362 case GST_STATE_CHANGE_READY_TO_NULL:
Tim-Philipp Müller4de8bd02012-08-08 10:56:51 +0100363 if (gst_task_get_state (qtmr->task) != GST_TASK_STOPPED)
364 GST_ERROR ("task %p should be stopped by now", qtmr->task);
Thiago Santosb692f9f2009-12-12 16:07:15 -0300365 gst_object_unref (qtmr->task);
366 qtmr->task = NULL;
Wim Taymans15848062012-01-19 11:33:53 +0100367 g_rec_mutex_clear (&qtmr->task_mutex);
Thiago Santosb692f9f2009-12-12 16:07:15 -0300368 break;
369 default:
370 break;
371 }
372 return ret;
373}
374
375
376gboolean
377gst_qt_moov_recover_register (GstPlugin * plugin)
378{
379 return gst_element_register (plugin, "qtmoovrecover", GST_RANK_NONE,
380 GST_TYPE_QT_MOOV_RECOVER);
381}