1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
3 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 * Unless otherwise indicated, Source Code is licensed under MIT license.
24 * See further explanation attached in License Statement (distributed in the file
27 * Permission is hereby granted, free of charge, to any person obtaining a copy of
28 * this software and associated documentation files (the "Software"), to deal in
29 * the Software without restriction, including without limitation the rights to
30 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
31 * of the Software, and to permit persons to whom the Software is furnished to do
32 * so, subject to the following conditions:
34 * The above copyright notice and this permission notice shall be included in all
35 * copies or substantial portions of the Software.
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
48 * SECTION:element-qtmux
49 * @short_description: Muxer for quicktime(.mov) files
51 * This element merges streams (audio and video) into QuickTime(.mov) files.
53 * The following background intends to explain why various similar muxers
54 * are present in this plugin.
56 * The <ulink url="http://www.apple.com/quicktime/resources/qtfileformat.pdf">
57 * QuickTime file format specification</ulink> served as basis for the MP4 file
58 * format specification (mp4mux), and as such the QuickTime file structure is
59 * nearly identical to the so-called ISO Base Media file format defined in
60 * ISO 14496-12 (except for some media specific parts).
61 * In turn, the latter ISO Base Media format was further specialized as a
62 * Motion JPEG-2000 file format in ISO 15444-3 (mj2mux)
63 * and in various 3GPP(2) specs (gppmux).
64 * The fragmented file features defined (only) in ISO Base Media are used by
65 * ISMV files making up (a.o.) Smooth Streaming (ismlmux).
67 * A few properties (<link linkend="GstQTMux--movie-timescale">movie-timescale</link>,
68 * <link linkend="GstQTMux--trak-timescale">trak-timescale</link>) allow adjusting
69 * some technical parameters, which might be useful in (rare) cases to resolve
70 * compatibility issues in some situations.
72 * Some other properties influence the result more fundamentally.
73 * A typical mov/mp4 file's metadata (aka moov) is located at the end of the file,
74 * somewhat contrary to this usually being called "the header".
75 * However, a <link linkend="GstQTMux--faststart">faststart</link> file will
76 * (with some effort) arrange this to be located near start of the file,
77 * which then allows it e.g. to be played while downloading.
78 * Alternatively, rather than having one chunk of metadata at start (or end),
79 * there can be some metadata at start and most of the other data can be spread
80 * out into fragments of <link linkend="GstQTMux--fragment-duration">fragment-duration</link>.
81 * If such fragmented layout is intended for streaming purposes, then
82 * <link linkend="GstQTMux--streamable">streamable</link> allows foregoing to add
83 * index metadata (at the end of file).
85 * <link linkend="GstQTMux--dts-method">dts-method</link> allows selecting a
86 * method for managing input timestamps (stay tuned for 0.11 to have this
87 * automagically settled). The default delta/duration method should handle nice
88 * (aka perfect streams) just fine, but may experience problems otherwise
89 * (e.g. input stream with re-ordered B-frames and/or with frame dropping).
90 * The re-ordering approach re-assigns incoming timestamps in ascending order
91 * to incoming buffers and offers an alternative in such cases. In cases where
92 * that might fail, the remaining method can be tried, which is exact and
93 * according to specs, but might experience playback on not so spec-wise players.
94 * Note that this latter approach also requires one to enable
95 * <link linkend="GstQTMux--presentation-timestamp">presentation-timestamp</link>.
98 * <title>Example pipelines</title>
100 * gst-launch v4l2src num-buffers=500 ! video/x-raw-yuv,width=320,height=240 ! ffmpegcolorspace ! qtmux ! filesink location=video.mov
102 * Records a video stream captured from a v4l2 device and muxes it into a qt file.
105 * Last reviewed on 2010-12-03
116 #include <glib/gstdio.h>
119 #include <gst/base/gstcollectpads.h>
120 #include <gst/tag/xmpwriter.h>
122 #include <sys/types.h>
124 #include <io.h> /* lseek, open, close, read */
126 #define lseek _lseeki64
128 #define off_t guint64
132 #define ftruncate g_win32_ftruncate
139 #include "gstqtmux.h"
141 GST_DEBUG_CATEGORY_STATIC (gst_qt_mux_debug);
142 #define GST_CAT_DEFAULT gst_qt_mux_debug
152 gst_qt_mux_dts_method_get_type (void)
154 static GType gst_qt_mux_dts_method = 0;
156 if (!gst_qt_mux_dts_method) {
157 static const GEnumValue dts_methods[] = {
158 {DTS_METHOD_DD, "delta/duration", "dd"},
159 {DTS_METHOD_REORDER, "reorder", "reorder"},
160 {DTS_METHOD_ASC, "ascending", "asc"},
164 gst_qt_mux_dts_method =
165 g_enum_register_static ("GstQTMuxDtsMethods", dts_methods);
168 return gst_qt_mux_dts_method;
171 #define GST_TYPE_QT_MUX_DTS_METHOD \
172 (gst_qt_mux_dts_method_get_type ())
174 /* QTMux signals and args */
184 PROP_MOVIE_TIMESCALE,
187 PROP_FAST_START_TEMP_FILE,
188 PROP_MOOV_RECOV_FILE,
189 PROP_FRAGMENT_DURATION,
195 /* some spare for header size as well */
196 #define MDAT_LARGE_FILE_LIMIT ((guint64) 1024 * 1024 * 1024 * 2)
197 #define MAX_TOLERATED_LATENESS (GST_SECOND / 10)
199 #define DEFAULT_MOVIE_TIMESCALE 1000
200 #define DEFAULT_TRAK_TIMESCALE 0
201 #define DEFAULT_DO_CTTS TRUE
202 #define DEFAULT_FAST_START FALSE
203 #define DEFAULT_FAST_START_TEMP_FILE NULL
204 #define DEFAULT_MOOV_RECOV_FILE NULL
205 #define DEFAULT_FRAGMENT_DURATION 0
206 #define DEFAULT_STREAMABLE FALSE
207 #define DEFAULT_DTS_METHOD DTS_METHOD_REORDER
210 static void gst_qt_mux_finalize (GObject * object);
212 static GstStateChangeReturn gst_qt_mux_change_state (GstElement * element,
213 GstStateChange transition);
215 /* property functions */
216 static void gst_qt_mux_set_property (GObject * object,
217 guint prop_id, const GValue * value, GParamSpec * pspec);
218 static void gst_qt_mux_get_property (GObject * object,
219 guint prop_id, GValue * value, GParamSpec * pspec);
222 static GstPad *gst_qt_mux_request_new_pad (GstElement * element,
223 GstPadTemplate * templ, const gchar * name);
224 static void gst_qt_mux_release_pad (GstElement * element, GstPad * pad);
227 static gboolean gst_qt_mux_sink_event (GstPad * pad, GstEvent * event);
229 static GstFlowReturn gst_qt_mux_collected (GstCollectPads * pads,
231 static GstFlowReturn gst_qt_mux_add_buffer (GstQTMux * qtmux, GstQTPad * pad,
234 static GstElementClass *parent_class = NULL;
237 gst_qt_mux_base_init (gpointer g_class)
239 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
240 GstQTMuxClass *klass = (GstQTMuxClass *) g_class;
241 GstQTMuxClassParams *params;
242 GstPadTemplate *videosinktempl, *audiosinktempl, *srctempl;
243 gchar *longname, *description;
246 (GstQTMuxClassParams *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (g_class),
247 GST_QT_MUX_PARAMS_QDATA);
248 g_assert (params != NULL);
250 /* construct the element details struct */
251 longname = g_strdup_printf ("%s Muxer", params->prop->long_name);
252 description = g_strdup_printf ("Multiplex audio and video into a %s file%s",
253 params->prop->long_name,
254 (params->prop->rank == GST_RANK_NONE) ? " (deprecated)" : "");
255 gst_element_class_set_details_simple (element_class, longname,
256 "Codec/Muxer", description,
257 "Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>");
259 g_free (description);
262 srctempl = gst_pad_template_new ("src", GST_PAD_SRC,
263 GST_PAD_ALWAYS, params->src_caps);
264 gst_element_class_add_pad_template (element_class, srctempl);
266 if (params->audio_sink_caps) {
267 audiosinktempl = gst_pad_template_new ("audio_%d",
268 GST_PAD_SINK, GST_PAD_REQUEST, params->audio_sink_caps);
269 gst_element_class_add_pad_template (element_class, audiosinktempl);
272 if (params->video_sink_caps) {
273 videosinktempl = gst_pad_template_new ("video_%d",
274 GST_PAD_SINK, GST_PAD_REQUEST, params->video_sink_caps);
275 gst_element_class_add_pad_template (element_class, videosinktempl);
278 klass->format = params->prop->format;
282 gst_qt_mux_class_init (GstQTMuxClass * klass)
284 GObjectClass *gobject_class;
285 GstElementClass *gstelement_class;
287 gobject_class = (GObjectClass *) klass;
288 gstelement_class = (GstElementClass *) klass;
290 parent_class = g_type_class_peek_parent (klass);
292 gobject_class->finalize = gst_qt_mux_finalize;
293 gobject_class->get_property = gst_qt_mux_get_property;
294 gobject_class->set_property = gst_qt_mux_set_property;
296 g_object_class_install_property (gobject_class, PROP_MOVIE_TIMESCALE,
297 g_param_spec_uint ("movie-timescale", "Movie timescale",
298 "Timescale to use in the movie (units per second)",
299 1, G_MAXUINT32, DEFAULT_MOVIE_TIMESCALE,
300 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (gobject_class, PROP_TRAK_TIMESCALE,
302 g_param_spec_uint ("trak-timescale", "Track timescale",
303 "Timescale to use for the tracks (units per second, 0 is automatic)",
304 0, G_MAXUINT32, DEFAULT_TRAK_TIMESCALE,
305 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_DO_CTTS,
307 g_param_spec_boolean ("presentation-time",
308 "Include presentation-time info",
309 "Calculate and include presentation/composition time "
310 "(in addition to decoding time)", DEFAULT_DO_CTTS,
311 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_DTS_METHOD,
313 g_param_spec_enum ("dts-method", "dts-method",
314 "Method to determine DTS time",
315 GST_TYPE_QT_MUX_DTS_METHOD, DEFAULT_DTS_METHOD,
316 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_FAST_START,
318 g_param_spec_boolean ("faststart", "Format file to faststart",
319 "If the file should be formatted for faststart (headers first)",
320 DEFAULT_FAST_START, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_FAST_START_TEMP_FILE,
322 g_param_spec_string ("faststart-file", "File to use for storing buffers",
323 "File that will be used temporarily to store data from the stream "
324 "when creating a faststart file. If null a filepath will be "
325 "created automatically", DEFAULT_FAST_START_TEMP_FILE,
326 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_MOOV_RECOV_FILE,
328 g_param_spec_string ("moov-recovery-file",
329 "File to store data for posterior moov atom recovery",
330 "File to be used to store "
331 "data for moov atom making movie file recovery possible in case "
332 "of a crash during muxing. Null for disabled. (Experimental)",
333 DEFAULT_MOOV_RECOV_FILE,
334 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_FRAGMENT_DURATION,
336 g_param_spec_uint ("fragment-duration", "Fragment duration",
337 "Fragment durations in ms (produce a fragmented file if > 0)",
338 0, G_MAXUINT32, klass->format == GST_QT_MUX_FORMAT_ISML ?
339 2000 : DEFAULT_FRAGMENT_DURATION,
340 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
342 g_param_spec_boolean ("streamable", "Streamable",
343 "If set to true, the output should be as if it is to be streamed "
344 "and hence no indexes written or duration written.",
346 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
348 gstelement_class->request_new_pad =
349 GST_DEBUG_FUNCPTR (gst_qt_mux_request_new_pad);
350 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_qt_mux_change_state);
351 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_qt_mux_release_pad);
355 gst_qt_mux_pad_reset (GstQTPad * qtpad)
360 qtpad->is_out_of_order = FALSE;
361 qtpad->have_dts = FALSE;
362 qtpad->sample_size = 0;
365 qtpad->first_ts = GST_CLOCK_TIME_NONE;
366 qtpad->prepare_buf_func = NULL;
367 qtpad->avg_bitrate = 0;
368 qtpad->max_bitrate = 0;
369 qtpad->ts_n_entries = 0;
373 for (i = 0; i < G_N_ELEMENTS (qtpad->buf_entries); i++) {
374 if (qtpad->buf_entries[i]) {
375 gst_buffer_unref (qtpad->buf_entries[i]);
376 qtpad->buf_entries[i] = NULL;
381 gst_buffer_replace (&qtpad->last_buf, NULL);
383 /* reference owned elsewhere */
387 atom_traf_free (qtpad->traf);
390 atom_array_clear (&qtpad->fragment_buffers);
392 /* reference owned elsewhere */
397 * Takes GstQTMux back to its initial state
400 gst_qt_mux_reset (GstQTMux * qtmux, gboolean alloc)
404 qtmux->state = GST_QT_MUX_STATE_NONE;
405 qtmux->header_size = 0;
406 qtmux->mdat_size = 0;
408 qtmux->longest_chunk = GST_CLOCK_TIME_NONE;
409 qtmux->video_pads = 0;
410 qtmux->audio_pads = 0;
411 qtmux->fragment_sequence = 0;
414 atom_ftyp_free (qtmux->ftyp);
418 atom_moov_free (qtmux->moov);
422 atom_mfra_free (qtmux->mfra);
425 if (qtmux->fast_start_file) {
426 fclose (qtmux->fast_start_file);
427 g_remove (qtmux->fast_start_file_path);
428 qtmux->fast_start_file = NULL;
430 if (qtmux->moov_recov_file) {
431 fclose (qtmux->moov_recov_file);
432 qtmux->moov_recov_file = NULL;
434 for (walk = qtmux->extra_atoms; walk; walk = g_slist_next (walk)) {
435 AtomInfo *ainfo = (AtomInfo *) walk->data;
436 ainfo->free_func (ainfo->atom);
439 g_slist_free (qtmux->extra_atoms);
440 qtmux->extra_atoms = NULL;
442 GST_OBJECT_LOCK (qtmux);
443 gst_tag_setter_reset_tags (GST_TAG_SETTER (qtmux));
444 GST_OBJECT_UNLOCK (qtmux);
447 for (walk = qtmux->sinkpads; walk; walk = g_slist_next (walk)) {
448 GstQTPad *qtpad = (GstQTPad *) walk->data;
449 gst_qt_mux_pad_reset (qtpad);
451 /* hm, moov_free above yanked the traks away from us,
452 * so do not free, but do clear */
457 qtmux->moov = atom_moov_new (qtmux->context);
458 /* ensure all is as nice and fresh as request_new_pad would provide it */
459 for (walk = qtmux->sinkpads; walk; walk = g_slist_next (walk)) {
460 GstQTPad *qtpad = (GstQTPad *) walk->data;
462 qtpad->trak = atom_trak_new (qtmux->context);
463 atom_moov_add_trak (qtmux->moov, qtpad->trak);
469 gst_qt_mux_init (GstQTMux * qtmux, GstQTMuxClass * qtmux_klass)
471 GstElementClass *klass = GST_ELEMENT_CLASS (qtmux_klass);
472 GstPadTemplate *templ;
474 templ = gst_element_class_get_pad_template (klass, "src");
475 qtmux->srcpad = gst_pad_new_from_template (templ, "src");
476 gst_pad_use_fixed_caps (qtmux->srcpad);
477 gst_element_add_pad (GST_ELEMENT (qtmux), qtmux->srcpad);
479 qtmux->sinkpads = NULL;
480 qtmux->collect = gst_collect_pads_new ();
481 gst_collect_pads_set_function (qtmux->collect,
482 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_qt_mux_collected), qtmux);
484 /* properties set to default upon construction */
486 /* always need this */
488 atoms_context_new (gst_qt_mux_map_format_to_flavor (qtmux_klass->format));
490 /* internals to initial state */
491 gst_qt_mux_reset (qtmux, TRUE);
496 gst_qt_mux_finalize (GObject * object)
498 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
500 gst_qt_mux_reset (qtmux, FALSE);
502 g_free (qtmux->fast_start_file_path);
503 g_free (qtmux->moov_recov_file_path);
505 atoms_context_free (qtmux->context);
506 gst_object_unref (qtmux->collect);
508 g_slist_free (qtmux->sinkpads);
510 G_OBJECT_CLASS (parent_class)->finalize (object);
514 gst_qt_mux_prepare_jpc_buffer (GstQTPad * qtpad, GstBuffer * buf,
519 GST_LOG_OBJECT (qtmux, "Preparing jpc buffer");
524 newbuf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buf) + 8);
525 gst_buffer_copy_metadata (newbuf, buf, GST_BUFFER_COPY_ALL);
527 GST_WRITE_UINT32_BE (GST_BUFFER_DATA (newbuf), GST_BUFFER_SIZE (newbuf));
528 GST_WRITE_UINT32_LE (GST_BUFFER_DATA (newbuf) + 4, FOURCC_jp2c);
530 memcpy (GST_BUFFER_DATA (newbuf) + 8, GST_BUFFER_DATA (buf),
531 GST_BUFFER_SIZE (buf));
532 gst_buffer_unref (buf);
538 gst_qt_mux_add_mp4_tag (GstQTMux * qtmux, const GstTagList * list,
539 const char *tag, const char *tag2, guint32 fourcc)
541 switch (gst_tag_get_type (tag)) {
547 if (!gst_tag_list_get_string (list, tag, &str) || !str)
549 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
550 GST_FOURCC_ARGS (fourcc), str);
551 atom_moov_add_str_tag (qtmux->moov, fourcc, str);
560 if (!gst_tag_list_get_double (list, tag, &value))
562 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u",
563 GST_FOURCC_ARGS (fourcc), (gint) value);
564 atom_moov_add_uint_tag (qtmux->moov, fourcc, 21, (gint) value);
571 /* paired unsigned integers */
574 if (!(gst_tag_list_get_uint (list, tag, &value) ||
575 gst_tag_list_get_uint (list, tag2, &count)))
577 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u/%u",
578 GST_FOURCC_ARGS (fourcc), value, count);
579 atom_moov_add_uint_tag (qtmux->moov, fourcc, 0,
580 value << 16 | (count & 0xFFFF));
582 /* unpaired unsigned integers */
583 if (!gst_tag_list_get_uint (list, tag, &value))
585 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u",
586 GST_FOURCC_ARGS (fourcc), value);
587 atom_moov_add_uint_tag (qtmux->moov, fourcc, 1, value);
592 g_assert_not_reached ();
598 gst_qt_mux_add_mp4_date (GstQTMux * qtmux, const GstTagList * list,
599 const char *tag, const char *tag2, guint32 fourcc)
607 g_return_if_fail (gst_tag_get_type (tag) == GST_TYPE_DATE);
609 if (!gst_tag_list_get_date (list, tag, &date) || !date)
612 year = g_date_get_year (date);
613 month = g_date_get_month (date);
614 day = g_date_get_day (date);
616 if (year == G_DATE_BAD_YEAR && month == G_DATE_BAD_MONTH &&
617 day == G_DATE_BAD_DAY) {
618 GST_WARNING_OBJECT (qtmux, "invalid date in tag");
622 str = g_strdup_printf ("%u-%u-%u", year, month, day);
623 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
624 GST_FOURCC_ARGS (fourcc), str);
625 atom_moov_add_str_tag (qtmux->moov, fourcc, str);
630 gst_qt_mux_add_mp4_cover (GstQTMux * qtmux, const GstTagList * list,
631 const char *tag, const char *tag2, guint32 fourcc)
633 GValue value = { 0, };
636 GstStructure *structure;
639 g_return_if_fail (gst_tag_get_type (tag) == GST_TYPE_BUFFER);
641 if (!gst_tag_list_copy_value (&value, list, tag))
644 buf = gst_value_get_buffer (&value);
648 caps = gst_buffer_get_caps (buf);
650 GST_WARNING_OBJECT (qtmux, "preview image without caps");
654 GST_DEBUG_OBJECT (qtmux, "preview image caps %" GST_PTR_FORMAT, caps);
656 structure = gst_caps_get_structure (caps, 0);
657 if (gst_structure_has_name (structure, "image/jpeg"))
659 else if (gst_structure_has_name (structure, "image/png"))
661 gst_caps_unref (caps);
664 GST_WARNING_OBJECT (qtmux, "preview image format not supported");
668 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT
669 " -> image size %d", GST_FOURCC_ARGS (fourcc), GST_BUFFER_SIZE (buf));
670 atom_moov_add_tag (qtmux->moov, fourcc, flags, GST_BUFFER_DATA (buf),
671 GST_BUFFER_SIZE (buf));
673 g_value_unset (&value);
677 gst_qt_mux_add_3gp_str (GstQTMux * qtmux, const GstTagList * list,
678 const char *tag, const char *tag2, guint32 fourcc)
683 g_return_if_fail (gst_tag_get_type (tag) == G_TYPE_STRING);
684 g_return_if_fail (!tag2 || gst_tag_get_type (tag2) == G_TYPE_UINT);
686 if (!gst_tag_list_get_string (list, tag, &str) || !str)
690 if (!gst_tag_list_get_uint (list, tag2, &number))
694 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
695 GST_FOURCC_ARGS (fourcc), str);
696 atom_moov_add_3gp_str_tag (qtmux->moov, fourcc, str);
698 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s/%d",
699 GST_FOURCC_ARGS (fourcc), str, number);
700 atom_moov_add_3gp_str_int_tag (qtmux->moov, fourcc, str, number);
707 gst_qt_mux_add_3gp_date (GstQTMux * qtmux, const GstTagList * list,
708 const char *tag, const char *tag2, guint32 fourcc)
713 g_return_if_fail (gst_tag_get_type (tag) == GST_TYPE_DATE);
715 if (!gst_tag_list_get_date (list, tag, &date) || !date)
718 year = g_date_get_year (date);
720 if (year == G_DATE_BAD_YEAR) {
721 GST_WARNING_OBJECT (qtmux, "invalid date in tag");
725 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %d",
726 GST_FOURCC_ARGS (fourcc), year);
727 atom_moov_add_3gp_uint_tag (qtmux->moov, fourcc, year);
731 gst_qt_mux_add_3gp_location (GstQTMux * qtmux, const GstTagList * list,
732 const char *tag, const char *tag2, guint32 fourcc)
734 gdouble latitude = -360, longitude = -360, altitude = 0;
735 gchar *location = NULL;
736 guint8 *data, *ddata;
737 gint size = 0, len = 0;
738 gboolean ret = FALSE;
740 g_return_if_fail (strcmp (tag, GST_TAG_GEO_LOCATION_NAME) == 0);
742 ret = gst_tag_list_get_string (list, tag, &location);
743 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_LONGITUDE,
745 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_LATITUDE,
747 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_ELEVATION,
754 len = strlen (location);
757 /* role + (long, lat, alt) + body + notes */
758 size += 1 + 3 * 4 + 1 + 1;
760 data = ddata = g_malloc (size);
763 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
766 memcpy (data + 2, location, len);
767 GST_WRITE_UINT8 (data + 2 + len, 0);
770 GST_WRITE_UINT8 (data, 0);
772 #define QT_WRITE_SFP32(data, fp) GST_WRITE_UINT32_BE(data, (guint32) ((gint) (fp * 65536.0)))
773 QT_WRITE_SFP32 (data + 1, longitude);
774 QT_WRITE_SFP32 (data + 5, latitude);
775 QT_WRITE_SFP32 (data + 9, altitude);
776 /* neither astronomical body nor notes */
777 GST_WRITE_UINT16_BE (data + 13, 0);
779 GST_DEBUG_OBJECT (qtmux, "Adding tag 'loci'");
780 atom_moov_add_3gp_tag (qtmux->moov, fourcc, ddata, size);
785 gst_qt_mux_add_3gp_keywords (GstQTMux * qtmux, const GstTagList * list,
786 const char *tag, const char *tag2, guint32 fourcc)
788 gchar *keywords = NULL;
789 guint8 *data, *ddata;
793 g_return_if_fail (strcmp (tag, GST_TAG_KEYWORDS) == 0);
795 if (!gst_tag_list_get_string (list, tag, &keywords) || !keywords)
798 kwds = g_strsplit (keywords, ",", 0);
802 for (i = 0; kwds[i]; i++) {
803 /* size byte + null-terminator */
804 size += strlen (kwds[i]) + 1 + 1;
807 /* language tag + count + keywords */
810 data = ddata = g_malloc (size);
813 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
815 GST_WRITE_UINT8 (data + 2, i);
818 for (i = 0; kwds[i]; ++i) {
819 gint len = strlen (kwds[i]);
821 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
822 GST_FOURCC_ARGS (fourcc), kwds[i]);
824 GST_WRITE_UINT8 (data, len + 1);
825 memcpy (data + 1, kwds[i], len + 1);
831 atom_moov_add_3gp_tag (qtmux->moov, fourcc, ddata, size);
836 gst_qt_mux_parse_classification_string (GstQTMux * qtmux, const gchar * input,
837 guint32 * p_fourcc, guint16 * p_table, gchar ** p_content)
845 size = strlen (input);
847 if (size < 4 + 3 + 1 + 1 + 1) {
848 /* at least the minimum xxxx://y/z */
849 GST_WARNING_OBJECT (qtmux, "Classification tag input (%s) too short, "
854 /* read the fourcc */
855 memcpy (&fourcc, data, 4);
859 if (strncmp (data, "://", 3) != 0) {
865 /* read the table number */
866 if (sscanf (data, "%d", &table) != 1) {
870 GST_WARNING_OBJECT (qtmux, "Invalid table number in classification tag (%d)"
871 ", table numbers should be positive, ignoring tag", table);
875 /* find the next / */
876 while (size > 0 && data[0] != '/') {
883 g_assert (data[0] == '/');
892 /* read up the rest of the string */
893 *p_content = g_strdup (data);
894 *p_table = (guint16) table;
900 GST_WARNING_OBJECT (qtmux, "Ignoring classification tag as "
901 "input (%s) didn't match the expected entitycode://table/content",
908 gst_qt_mux_add_3gp_classification (GstQTMux * qtmux, const GstTagList * list,
909 const char *tag, const char *tag2, guint32 fourcc)
911 gchar *clsf_data = NULL;
915 gchar *content = NULL;
918 g_return_if_fail (strcmp (tag, GST_TAG_3GP_CLASSIFICATION) == 0);
920 if (!gst_tag_list_get_string (list, tag, &clsf_data) || !clsf_data)
923 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
924 GST_FOURCC_ARGS (fourcc), clsf_data);
926 /* parse the string, format is:
927 * entityfourcc://table/content
929 gst_qt_mux_parse_classification_string (qtmux, clsf_data, &entity, &table,
933 size = strlen (content) + 1;
935 /* now we have everything, build the atom
936 * atom description is at 3GPP TS 26.244 V8.2.0 (2009-09) */
937 data = g_malloc (4 + 2 + 2 + size);
938 GST_WRITE_UINT32_LE (data, entity);
939 GST_WRITE_UINT16_BE (data + 4, (guint16) table);
940 GST_WRITE_UINT16_BE (data + 6, 0);
941 memcpy (data + 8, content, size);
944 atom_moov_add_3gp_tag (qtmux->moov, fourcc, data, 4 + 2 + 2 + size);
948 typedef void (*GstQTMuxAddTagFunc) (GstQTMux * mux, const GstTagList * list,
949 const char *tag, const char *tag2, guint32 fourcc);
952 * Struct to record mappings from gstreamer tags to fourcc codes
954 typedef struct _GstTagToFourcc
958 const gchar *gsttag2;
959 const GstQTMuxAddTagFunc func;
962 /* tag list tags to fourcc matching */
963 static const GstTagToFourcc tag_matches_mp4[] = {
964 {FOURCC__alb, GST_TAG_ALBUM, NULL, gst_qt_mux_add_mp4_tag},
965 {FOURCC_soal, GST_TAG_ALBUM_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
966 {FOURCC__ART, GST_TAG_ARTIST, NULL, gst_qt_mux_add_mp4_tag},
967 {FOURCC_soar, GST_TAG_ARTIST_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
968 {FOURCC_aART, GST_TAG_ALBUM_ARTIST, NULL, gst_qt_mux_add_mp4_tag},
969 {FOURCC_soaa, GST_TAG_ALBUM_ARTIST_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
970 {FOURCC__cmt, GST_TAG_COMMENT, NULL, gst_qt_mux_add_mp4_tag},
971 {FOURCC__wrt, GST_TAG_COMPOSER, NULL, gst_qt_mux_add_mp4_tag},
972 {FOURCC_soco, GST_TAG_COMPOSER_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
973 {FOURCC_tvsh, GST_TAG_SHOW_NAME, NULL, gst_qt_mux_add_mp4_tag},
974 {FOURCC_sosn, GST_TAG_SHOW_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
975 {FOURCC_tvsn, GST_TAG_SHOW_SEASON_NUMBER, NULL, gst_qt_mux_add_mp4_tag},
976 {FOURCC_tves, GST_TAG_SHOW_EPISODE_NUMBER, NULL, gst_qt_mux_add_mp4_tag},
977 {FOURCC__gen, GST_TAG_GENRE, NULL, gst_qt_mux_add_mp4_tag},
978 {FOURCC__nam, GST_TAG_TITLE, NULL, gst_qt_mux_add_mp4_tag},
979 {FOURCC_sonm, GST_TAG_TITLE_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
980 {FOURCC_perf, GST_TAG_PERFORMER, NULL, gst_qt_mux_add_mp4_tag},
981 {FOURCC__grp, GST_TAG_GROUPING, NULL, gst_qt_mux_add_mp4_tag},
982 {FOURCC__des, GST_TAG_DESCRIPTION, NULL, gst_qt_mux_add_mp4_tag},
983 {FOURCC__lyr, GST_TAG_LYRICS, NULL, gst_qt_mux_add_mp4_tag},
984 {FOURCC__too, GST_TAG_ENCODER, NULL, gst_qt_mux_add_mp4_tag},
985 {FOURCC_cprt, GST_TAG_COPYRIGHT, NULL, gst_qt_mux_add_mp4_tag},
986 {FOURCC_keyw, GST_TAG_KEYWORDS, NULL, gst_qt_mux_add_mp4_tag},
987 {FOURCC__day, GST_TAG_DATE, NULL, gst_qt_mux_add_mp4_date},
988 {FOURCC_tmpo, GST_TAG_BEATS_PER_MINUTE, NULL, gst_qt_mux_add_mp4_tag},
989 {FOURCC_trkn, GST_TAG_TRACK_NUMBER, GST_TAG_TRACK_COUNT,
990 gst_qt_mux_add_mp4_tag},
991 {FOURCC_disk, GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_ALBUM_VOLUME_COUNT,
992 gst_qt_mux_add_mp4_tag},
993 {FOURCC_covr, GST_TAG_PREVIEW_IMAGE, NULL, gst_qt_mux_add_mp4_cover},
997 static const GstTagToFourcc tag_matches_3gp[] = {
998 {FOURCC_titl, GST_TAG_TITLE, NULL, gst_qt_mux_add_3gp_str},
999 {FOURCC_dscp, GST_TAG_DESCRIPTION, NULL, gst_qt_mux_add_3gp_str},
1000 {FOURCC_cprt, GST_TAG_COPYRIGHT, NULL, gst_qt_mux_add_3gp_str},
1001 {FOURCC_perf, GST_TAG_ARTIST, NULL, gst_qt_mux_add_3gp_str},
1002 {FOURCC_auth, GST_TAG_COMPOSER, NULL, gst_qt_mux_add_3gp_str},
1003 {FOURCC_gnre, GST_TAG_GENRE, NULL, gst_qt_mux_add_3gp_str},
1004 {FOURCC_kywd, GST_TAG_KEYWORDS, NULL, gst_qt_mux_add_3gp_keywords},
1005 {FOURCC_yrrc, GST_TAG_DATE, NULL, gst_qt_mux_add_3gp_date},
1006 {FOURCC_albm, GST_TAG_ALBUM, GST_TAG_TRACK_NUMBER, gst_qt_mux_add_3gp_str},
1007 {FOURCC_loci, GST_TAG_GEO_LOCATION_NAME, NULL, gst_qt_mux_add_3gp_location},
1008 {FOURCC_clsf, GST_TAG_3GP_CLASSIFICATION, NULL,
1009 gst_qt_mux_add_3gp_classification},
1013 /* qtdemux produces these for atoms it cannot parse */
1014 #define GST_QT_DEMUX_PRIVATE_TAG "private-qt-tag"
1017 gst_qt_mux_add_xmp_tags (GstQTMux * qtmux, const GstTagList * list)
1019 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
1020 GstBuffer *xmp = NULL;
1022 /* adobe specs only have 'quicktime' and 'mp4',
1023 * but I guess we can extrapolate to gpp.
1024 * Keep mj2 out for now as we don't add any tags for it yet.
1025 * If you have further info about xmp on these formats, please share */
1026 if (qtmux_klass->format == GST_QT_MUX_FORMAT_MJ2)
1029 GST_DEBUG_OBJECT (qtmux, "Adding xmp tags");
1031 if (qtmux_klass->format == GST_QT_MUX_FORMAT_QT) {
1032 xmp = gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER (qtmux),
1035 atom_moov_add_xmp_tags (qtmux->moov, xmp);
1038 /* for isom/mp4, it is a top level uuid atom */
1039 xmp = gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER (qtmux),
1042 ainfo = build_uuid_xmp_atom (xmp);
1044 qtmux->extra_atoms = g_slist_prepend (qtmux->extra_atoms, ainfo);
1049 gst_buffer_unref (xmp);
1053 gst_qt_mux_add_metadata_tags (GstQTMux * qtmux, const GstTagList * list)
1055 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
1058 const gchar *tag, *tag2;
1059 const GstTagToFourcc *tag_matches;
1061 switch (qtmux_klass->format) {
1062 case GST_QT_MUX_FORMAT_3GP:
1063 tag_matches = tag_matches_3gp;
1065 case GST_QT_MUX_FORMAT_MJ2:
1069 /* sort of iTunes style for mp4 and QT (?) */
1070 tag_matches = tag_matches_mp4;
1077 for (i = 0; tag_matches[i].fourcc; i++) {
1078 fourcc = tag_matches[i].fourcc;
1079 tag = tag_matches[i].gsttag;
1080 tag2 = tag_matches[i].gsttag2;
1082 g_assert (tag_matches[i].func);
1083 tag_matches[i].func (qtmux, list, tag, tag2, fourcc);
1086 /* add unparsed blobs if present */
1087 if (gst_tag_exists (GST_QT_DEMUX_PRIVATE_TAG)) {
1090 num_tags = gst_tag_list_get_tag_size (list, GST_QT_DEMUX_PRIVATE_TAG);
1091 for (i = 0; i < num_tags; ++i) {
1094 GstCaps *caps = NULL;
1096 val = gst_tag_list_get_value_index (list, GST_QT_DEMUX_PRIVATE_TAG, i);
1097 buf = (GstBuffer *) gst_value_get_mini_object (val);
1099 if (buf && (caps = gst_buffer_get_caps (buf))) {
1101 const gchar *style = NULL;
1103 GST_DEBUG_OBJECT (qtmux, "Found private tag %d/%d; size %d, caps %"
1104 GST_PTR_FORMAT, i, num_tags, GST_BUFFER_SIZE (buf), caps);
1105 s = gst_caps_get_structure (caps, 0);
1106 if (s && (style = gst_structure_get_string (s, "style"))) {
1107 /* try to prevent some style tag ending up into another variant
1108 * (todo: make into a list if more cases) */
1109 if ((strcmp (style, "itunes") == 0 &&
1110 qtmux_klass->format == GST_QT_MUX_FORMAT_MP4) ||
1111 (strcmp (style, "iso") == 0 &&
1112 qtmux_klass->format == GST_QT_MUX_FORMAT_3GP)) {
1113 GST_DEBUG_OBJECT (qtmux, "Adding private tag");
1114 atom_moov_add_blob_tag (qtmux->moov, GST_BUFFER_DATA (buf),
1115 GST_BUFFER_SIZE (buf));
1118 gst_caps_unref (caps);
1127 * Gets the tagsetter iface taglist and puts the known tags
1128 * into the output stream
1131 gst_qt_mux_setup_metadata (GstQTMux * qtmux)
1133 const GstTagList *tags;
1135 GST_OBJECT_LOCK (qtmux);
1136 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (qtmux));
1137 GST_OBJECT_UNLOCK (qtmux);
1139 GST_LOG_OBJECT (qtmux, "tags: %" GST_PTR_FORMAT, tags);
1141 if (tags && !gst_tag_list_is_empty (tags)) {
1142 GstTagList *copy = gst_tag_list_copy (tags);
1144 GST_DEBUG_OBJECT (qtmux, "Removing bogus tags");
1145 gst_tag_list_remove_tag (copy, GST_TAG_VIDEO_CODEC);
1146 gst_tag_list_remove_tag (copy, GST_TAG_AUDIO_CODEC);
1147 gst_tag_list_remove_tag (copy, GST_TAG_CONTAINER_FORMAT);
1149 GST_DEBUG_OBJECT (qtmux, "Formatting tags");
1150 gst_qt_mux_add_metadata_tags (qtmux, copy);
1151 gst_qt_mux_add_xmp_tags (qtmux, copy);
1152 gst_tag_list_free (copy);
1154 GST_DEBUG_OBJECT (qtmux, "No tags received");
1158 static inline GstBuffer *
1159 _gst_buffer_new_take_data (guint8 * data, guint size)
1163 buf = gst_buffer_new ();
1164 GST_BUFFER_DATA (buf) = GST_BUFFER_MALLOCDATA (buf) = data;
1165 GST_BUFFER_SIZE (buf) = size;
1170 static GstFlowReturn
1171 gst_qt_mux_send_buffer (GstQTMux * qtmux, GstBuffer * buf, guint64 * offset,
1178 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
1180 data = GST_BUFFER_DATA (buf);
1181 size = GST_BUFFER_SIZE (buf);
1183 GST_LOG_OBJECT (qtmux, "sending buffer size %d", size);
1185 if (mind_fast && qtmux->fast_start_file) {
1188 GST_LOG_OBJECT (qtmux, "to temporary file");
1189 ret = fwrite (data, sizeof (guint8), size, qtmux->fast_start_file);
1190 gst_buffer_unref (buf);
1196 GST_LOG_OBJECT (qtmux, "downstream");
1198 buf = gst_buffer_make_metadata_writable (buf);
1199 gst_buffer_set_caps (buf, GST_PAD_CAPS (qtmux->srcpad));
1200 res = gst_pad_push (qtmux->srcpad, buf);
1203 if (G_LIKELY (offset))
1211 GST_ELEMENT_ERROR (qtmux, RESOURCE, WRITE,
1212 ("Failed to write to temporary file"), GST_ERROR_SYSTEM);
1213 return GST_FLOW_ERROR;
1218 gst_qt_mux_seek_to_beginning (FILE * f)
1221 if (fseeko (f, (off_t) 0, SEEK_SET) != 0)
1223 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1224 if (lseek (fileno (f), (off_t) 0, SEEK_SET) == (off_t) - 1)
1227 if (fseek (f, (long) 0, SEEK_SET) != 0)
1233 static GstFlowReturn
1234 gst_qt_mux_send_buffered_data (GstQTMux * qtmux, guint64 * offset)
1236 GstFlowReturn ret = GST_FLOW_OK;
1237 GstBuffer *buf = NULL;
1239 if (fflush (qtmux->fast_start_file))
1242 if (!gst_qt_mux_seek_to_beginning (qtmux->fast_start_file))
1245 /* hm, this could all take a really really long time,
1246 * but there may not be another way to get moov atom first
1247 * (somehow optimize copy?) */
1248 GST_DEBUG_OBJECT (qtmux, "Sending buffered data");
1249 while (ret == GST_FLOW_OK) {
1251 const int bufsize = 4096;
1253 buf = gst_buffer_new_and_alloc (bufsize);
1254 r = fread (GST_BUFFER_DATA (buf), sizeof (guint8), bufsize,
1255 qtmux->fast_start_file);
1258 GST_BUFFER_SIZE (buf) = r;
1259 GST_LOG_OBJECT (qtmux, "Pushing buffered buffer of size %d", r);
1260 ret = gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1264 gst_buffer_unref (buf);
1266 if (ftruncate (fileno (qtmux->fast_start_file), 0))
1268 if (!gst_qt_mux_seek_to_beginning (qtmux->fast_start_file))
1276 GST_ELEMENT_ERROR (qtmux, RESOURCE, WRITE,
1277 ("Failed to flush temporary file"), GST_ERROR_SYSTEM);
1278 ret = GST_FLOW_ERROR;
1283 GST_ELEMENT_ERROR (qtmux, RESOURCE, SEEK,
1284 ("Failed to seek temporary file"), GST_ERROR_SYSTEM);
1285 ret = GST_FLOW_ERROR;
1290 /* clear descriptor so we don't remove temp file later on,
1291 * might be possible to recover */
1292 fclose (qtmux->fast_start_file);
1293 qtmux->fast_start_file = NULL;
1299 * Sends the initial mdat atom fields (size fields and fourcc type),
1300 * the subsequent buffers are considered part of it's data.
1301 * As we can't predict the amount of data that we are going to place in mdat
1302 * we need to record the position of the size field in the stream so we can
1303 * seek back to it later and update when the streams have finished.
1305 static GstFlowReturn
1306 gst_qt_mux_send_mdat_header (GstQTMux * qtmux, guint64 * off, guint64 size,
1311 guint8 *data = NULL;
1314 GST_DEBUG_OBJECT (qtmux, "Sending mdat's atom header, "
1315 "size %" G_GUINT64_FORMAT, size);
1317 node_header = g_malloc0 (sizeof (Atom));
1318 node_header->type = FOURCC_mdat;
1320 /* use extended size */
1321 node_header->size = 1;
1322 node_header->extended_size = 0;
1324 node_header->extended_size = size + 16;
1326 node_header->size = size + 8;
1330 if (atom_copy_data (node_header, &data, &size, &offset) == 0)
1331 goto serialize_error;
1333 buf = _gst_buffer_new_take_data (data, offset);
1334 g_free (node_header);
1336 GST_LOG_OBJECT (qtmux, "Pushing mdat start");
1337 return gst_qt_mux_send_buffer (qtmux, buf, off, FALSE);
1342 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
1343 ("Failed to serialize mdat"));
1344 return GST_FLOW_ERROR;
1349 * We get the position of the mdat size field, seek back to it
1350 * and overwrite with the real value
1352 static GstFlowReturn
1353 gst_qt_mux_update_mdat_size (GstQTMux * qtmux, guint64 mdat_pos,
1354 guint64 mdat_size, guint64 * offset)
1358 gboolean large_file;
1360 large_file = (mdat_size > MDAT_LARGE_FILE_LIMIT);
1365 /* seek and rewrite the header */
1366 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1367 mdat_pos, GST_CLOCK_TIME_NONE, 0);
1368 gst_pad_push_event (qtmux->srcpad, event);
1371 buf = gst_buffer_new_and_alloc (sizeof (guint64));
1372 GST_WRITE_UINT64_BE (GST_BUFFER_DATA (buf), mdat_size + 16);
1376 buf = gst_buffer_new_and_alloc (16);
1377 data = GST_BUFFER_DATA (buf);
1378 GST_WRITE_UINT32_BE (data, 8);
1379 GST_WRITE_UINT32_LE (data + 4, FOURCC_free);
1380 GST_WRITE_UINT32_BE (data + 8, mdat_size + 8);
1381 GST_WRITE_UINT32_LE (data + 12, FOURCC_mdat);
1384 return gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1387 static GstFlowReturn
1388 gst_qt_mux_send_ftyp (GstQTMux * qtmux, guint64 * off)
1391 guint64 size = 0, offset = 0;
1392 guint8 *data = NULL;
1394 GST_DEBUG_OBJECT (qtmux, "Sending ftyp atom");
1396 if (!atom_ftyp_copy_data (qtmux->ftyp, &data, &size, &offset))
1397 goto serialize_error;
1399 buf = _gst_buffer_new_take_data (data, offset);
1401 GST_LOG_OBJECT (qtmux, "Pushing ftyp");
1402 return gst_qt_mux_send_buffer (qtmux, buf, off, FALSE);
1407 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
1408 ("Failed to serialize ftyp"));
1409 return GST_FLOW_ERROR;
1414 gst_qt_mux_prepare_ftyp (GstQTMux * qtmux, AtomFTYP ** p_ftyp,
1415 GstBuffer ** p_prefix)
1417 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
1418 guint32 major, version;
1420 GstBuffer *prefix = NULL;
1421 AtomFTYP *ftyp = NULL;
1423 GST_DEBUG_OBJECT (qtmux, "Preparing ftyp and possible prefix atom");
1425 /* init and send context and ftyp based on current property state */
1426 gst_qt_mux_map_format_to_header (qtmux_klass->format, &prefix, &major,
1427 &version, &comp, qtmux->moov, qtmux->longest_chunk,
1428 qtmux->fast_start_file != NULL);
1429 ftyp = atom_ftyp_new (qtmux->context, major, version, comp);
1436 gst_buffer_unref (prefix);
1441 static GstFlowReturn
1442 gst_qt_mux_prepare_and_send_ftyp (GstQTMux * qtmux)
1444 GstFlowReturn ret = GST_FLOW_OK;
1445 GstBuffer *prefix = NULL;
1447 GST_DEBUG_OBJECT (qtmux, "Preparing to send ftyp atom");
1449 /* init and send context and ftyp based on current property state */
1451 atom_ftyp_free (qtmux->ftyp);
1454 gst_qt_mux_prepare_ftyp (qtmux, &qtmux->ftyp, &prefix);
1456 ret = gst_qt_mux_send_buffer (qtmux, prefix, &qtmux->header_size, FALSE);
1457 if (ret != GST_FLOW_OK)
1460 return gst_qt_mux_send_ftyp (qtmux, &qtmux->header_size);
1464 gst_qt_mux_set_header_on_caps (GstQTMux * mux, GstBuffer * buf)
1466 GstStructure *structure;
1467 GValue array = { 0 };
1468 GValue value = { 0 };
1469 GstCaps *caps = GST_PAD_CAPS (mux->srcpad);
1471 caps = gst_caps_copy (GST_PAD_CAPS (mux->srcpad));
1472 structure = gst_caps_get_structure (caps, 0);
1474 g_value_init (&array, GST_TYPE_ARRAY);
1476 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
1477 g_value_init (&value, GST_TYPE_BUFFER);
1478 gst_value_take_buffer (&value, gst_buffer_ref (buf));
1479 gst_value_array_append_value (&array, &value);
1480 g_value_unset (&value);
1482 gst_structure_set_value (structure, "streamheader", &array);
1483 g_value_unset (&array);
1484 gst_pad_set_caps (mux->srcpad, caps);
1485 gst_caps_unref (caps);
1489 gst_qt_mux_configure_moov (GstQTMux * qtmux, guint32 * _timescale)
1491 gboolean fragmented;
1494 GST_OBJECT_LOCK (qtmux);
1495 timescale = qtmux->timescale;
1496 fragmented = qtmux->fragment_sequence > 0;
1497 GST_OBJECT_UNLOCK (qtmux);
1499 /* inform lower layers of our property wishes, and determine duration.
1500 * Let moov take care of this using its list of traks;
1501 * so that released pads are also included */
1502 GST_DEBUG_OBJECT (qtmux, "Updating timescale to %" G_GUINT32_FORMAT,
1504 atom_moov_update_timescale (qtmux->moov, timescale);
1505 atom_moov_set_fragmented (qtmux->moov, fragmented);
1507 atom_moov_update_duration (qtmux->moov);
1510 *_timescale = timescale;
1513 static GstFlowReturn
1514 gst_qt_mux_send_moov (GstQTMux * qtmux, guint64 * _offset, gboolean mind_fast)
1516 guint64 offset = 0, size = 0;
1519 GstFlowReturn ret = GST_FLOW_OK;
1521 /* serialize moov */
1524 GST_LOG_OBJECT (qtmux, "Copying movie header into buffer");
1525 if (!atom_moov_copy_data (qtmux->moov, &data, &size, &offset))
1526 goto serialize_error;
1528 buf = _gst_buffer_new_take_data (data, offset);
1529 GST_DEBUG_OBJECT (qtmux, "Pushing moov atoms");
1530 gst_qt_mux_set_header_on_caps (qtmux, buf);
1531 ret = gst_qt_mux_send_buffer (qtmux, buf, _offset, mind_fast);
1538 return GST_FLOW_ERROR;
1542 /* either calculates size of extra atoms or pushes them */
1543 static GstFlowReturn
1544 gst_qt_mux_send_extra_atoms (GstQTMux * qtmux, gboolean send, guint64 * offset,
1548 guint64 loffset = 0, size = 0;
1550 GstFlowReturn ret = GST_FLOW_OK;
1552 for (walk = qtmux->extra_atoms; walk; walk = g_slist_next (walk)) {
1553 AtomInfo *ainfo = (AtomInfo *) walk->data;
1557 if (!ainfo->copy_data_func (ainfo->atom,
1558 send ? &data : NULL, &size, &loffset))
1559 goto serialize_error;
1564 GST_DEBUG_OBJECT (qtmux,
1565 "Pushing extra top-level atom %" GST_FOURCC_FORMAT,
1566 GST_FOURCC_ARGS (ainfo->atom->type));
1567 buf = _gst_buffer_new_take_data (data, loffset);
1568 ret = gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1569 if (ret != GST_FLOW_OK)
1582 return GST_FLOW_ERROR;
1586 static GstFlowReturn
1587 gst_qt_mux_start_file (GstQTMux * qtmux)
1589 GstFlowReturn ret = GST_FLOW_OK;
1592 GST_DEBUG_OBJECT (qtmux, "starting file");
1594 caps = gst_caps_copy (gst_pad_get_pad_template_caps (qtmux->srcpad));
1595 /* qtmux has structure with and without variant, remove all but the first */
1596 while (gst_caps_get_size (caps) > 1)
1597 gst_caps_remove_structure (caps, 1);
1598 gst_pad_set_caps (qtmux->srcpad, caps);
1599 gst_caps_unref (caps);
1601 /* let downstream know we think in BYTES and expect to do seeking later on */
1602 gst_pad_push_event (qtmux->srcpad,
1603 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0));
1605 /* initialize our moov recovery file */
1606 GST_OBJECT_LOCK (qtmux);
1607 if (qtmux->moov_recov_file_path) {
1608 GST_DEBUG_OBJECT (qtmux, "Openning moov recovery file: %s",
1609 qtmux->moov_recov_file_path);
1610 qtmux->moov_recov_file = g_fopen (qtmux->moov_recov_file_path, "wb+");
1611 if (qtmux->moov_recov_file == NULL) {
1612 GST_WARNING_OBJECT (qtmux, "Failed to open moov recovery file in %s",
1613 qtmux->moov_recov_file_path);
1616 gboolean fail = FALSE;
1617 AtomFTYP *ftyp = NULL;
1618 GstBuffer *prefix = NULL;
1620 gst_qt_mux_prepare_ftyp (qtmux, &ftyp, &prefix);
1622 if (!atoms_recov_write_headers (qtmux->moov_recov_file, ftyp, prefix,
1623 qtmux->moov, qtmux->timescale,
1624 g_slist_length (qtmux->sinkpads))) {
1625 GST_WARNING_OBJECT (qtmux, "Failed to write moov recovery file "
1630 atom_ftyp_free (ftyp);
1632 gst_buffer_unref (prefix);
1634 for (walk = qtmux->sinkpads; walk && !fail; walk = g_slist_next (walk)) {
1635 GstCollectData *cdata = (GstCollectData *) walk->data;
1636 GstQTPad *qpad = (GstQTPad *) cdata;
1637 /* write info for each stream */
1638 fail = atoms_recov_write_trak_info (qtmux->moov_recov_file, qpad->trak);
1640 GST_WARNING_OBJECT (qtmux, "Failed to write trak info to recovery "
1646 fclose (qtmux->moov_recov_file);
1647 qtmux->moov_recov_file = NULL;
1648 GST_WARNING_OBJECT (qtmux, "An error was detected while writing to "
1649 "recover file, moov recovery won't work");
1653 GST_OBJECT_UNLOCK (qtmux);
1656 * send mdat header if already needed, and mark position for later update.
1657 * We don't send ftyp now if we are on fast start mode, because we can
1658 * better fine tune using the information we gather to create the whole moov
1661 if (qtmux->fast_start) {
1662 GST_OBJECT_LOCK (qtmux);
1663 qtmux->fast_start_file = g_fopen (qtmux->fast_start_file_path, "wb+");
1664 if (!qtmux->fast_start_file)
1666 GST_OBJECT_UNLOCK (qtmux);
1668 /* send a dummy buffer for preroll */
1669 ret = gst_qt_mux_send_buffer (qtmux, gst_buffer_new (), NULL, FALSE);
1670 if (ret != GST_FLOW_OK)
1674 ret = gst_qt_mux_prepare_and_send_ftyp (qtmux);
1675 if (ret != GST_FLOW_OK) {
1679 /* well, it's moov pos if fragmented ... */
1680 qtmux->mdat_pos = qtmux->header_size;
1682 if (qtmux->fragment_duration) {
1683 GST_DEBUG_OBJECT (qtmux, "fragment duration %d ms, writing headers",
1684 qtmux->fragment_duration);
1685 /* also used as snapshot marker to indicate fragmented file */
1686 qtmux->fragment_sequence = 1;
1687 /* prepare moov and/or tags */
1688 gst_qt_mux_configure_moov (qtmux, NULL);
1689 gst_qt_mux_setup_metadata (qtmux);
1690 ret = gst_qt_mux_send_moov (qtmux, &qtmux->header_size, FALSE);
1691 if (ret != GST_FLOW_OK)
1695 gst_qt_mux_send_extra_atoms (qtmux, TRUE, &qtmux->header_size, FALSE);
1696 if (ret != GST_FLOW_OK)
1699 if (!qtmux->streamable)
1700 qtmux->mfra = atom_mfra_new (qtmux->context);
1702 /* extended to ensure some spare space */
1703 ret = gst_qt_mux_send_mdat_header (qtmux, &qtmux->header_size, 0, TRUE);
1713 GST_ELEMENT_ERROR (qtmux, RESOURCE, OPEN_READ_WRITE,
1714 (("Could not open temporary file \"%s\""), qtmux->fast_start_file_path),
1716 GST_OBJECT_UNLOCK (qtmux);
1717 return GST_FLOW_ERROR;
1721 static GstFlowReturn
1722 gst_qt_mux_stop_file (GstQTMux * qtmux)
1724 gboolean ret = GST_FLOW_OK;
1725 guint64 offset = 0, size = 0;
1727 gboolean large_file;
1729 GstClockTime first_ts = GST_CLOCK_TIME_NONE;
1731 GST_DEBUG_OBJECT (qtmux, "Updating remaining values and sending last data");
1733 /* pushing last buffers for each pad */
1734 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
1735 GstCollectData *cdata = (GstCollectData *) walk->data;
1736 GstQTPad *qtpad = (GstQTPad *) cdata;
1738 /* send last buffer */
1739 GST_DEBUG_OBJECT (qtmux, "Sending the last buffer for pad %s",
1740 GST_PAD_NAME (qtpad->collect.pad));
1741 ret = gst_qt_mux_add_buffer (qtmux, qtpad, NULL);
1742 if (ret != GST_FLOW_OK)
1743 GST_WARNING_OBJECT (qtmux, "Failed to send last buffer for %s, "
1744 "flow return: %s", GST_PAD_NAME (qtpad->collect.pad),
1745 gst_flow_get_name (ret));
1747 if (!GST_CLOCK_TIME_IS_VALID (qtpad->first_ts)) {
1748 GST_DEBUG_OBJECT (qtmux, "Pad %s has no buffers",
1749 GST_PAD_NAME (qtpad->collect.pad));
1753 /* determine max stream duration */
1754 if (!GST_CLOCK_TIME_IS_VALID (first_ts) ||
1755 (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
1756 qtpad->last_dts > first_ts)) {
1757 first_ts = qtpad->last_dts;
1761 if (qtmux->fragment_sequence) {
1765 guint8 *data = NULL;
1769 GST_DEBUG_OBJECT (qtmux, "adding mfra");
1770 if (!atom_mfra_copy_data (qtmux->mfra, &data, &size, &offset))
1771 goto serialize_error;
1772 buf = _gst_buffer_new_take_data (data, offset);
1773 ret = gst_qt_mux_send_buffer (qtmux, buf, NULL, FALSE);
1774 if (ret != GST_FLOW_OK)
1777 /* must have been streamable; no need to write duration */
1778 GST_DEBUG_OBJECT (qtmux, "streamable file; nothing to stop");
1783 timescale = qtmux->timescale;
1784 /* only mvex duration is updated,
1785 * mvhd should be consistent with empty moov
1786 * (but TODO maybe some clients do not handle that well ?) */
1787 qtmux->moov->mvex.mehd.fragment_duration =
1788 gst_util_uint64_scale (first_ts, timescale, GST_SECOND);
1789 GST_DEBUG_OBJECT (qtmux, "rewriting moov with mvex duration %"
1790 GST_TIME_FORMAT, GST_TIME_ARGS (first_ts));
1791 /* seek and rewrite the header */
1792 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1793 qtmux->mdat_pos, GST_CLOCK_TIME_NONE, 0);
1794 gst_pad_push_event (qtmux->srcpad, event);
1795 /* no need to seek back */
1796 return gst_qt_mux_send_moov (qtmux, NULL, FALSE);
1799 gst_qt_mux_configure_moov (qtmux, ×cale);
1801 /* check for late streams */
1802 first_ts = GST_CLOCK_TIME_NONE;
1803 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
1804 GstCollectData *cdata = (GstCollectData *) walk->data;
1805 GstQTPad *qtpad = (GstQTPad *) cdata;
1807 if (!GST_CLOCK_TIME_IS_VALID (first_ts) ||
1808 (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
1809 qtpad->first_ts < first_ts)) {
1810 first_ts = qtpad->first_ts;
1813 GST_DEBUG_OBJECT (qtmux, "Media first ts selected: %" GST_TIME_FORMAT,
1814 GST_TIME_ARGS (first_ts));
1815 /* add EDTSs for late streams */
1816 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
1817 GstCollectData *cdata = (GstCollectData *) walk->data;
1818 GstQTPad *qtpad = (GstQTPad *) cdata;
1822 if (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
1823 qtpad->first_ts > first_ts + MAX_TOLERATED_LATENESS) {
1824 GST_DEBUG_OBJECT (qtmux, "Pad %s is a late stream by %" GST_TIME_FORMAT,
1825 GST_PAD_NAME (qtpad->collect.pad),
1826 GST_TIME_ARGS (qtpad->first_ts - first_ts));
1827 lateness = gst_util_uint64_scale_round (qtpad->first_ts - first_ts,
1828 timescale, GST_SECOND);
1829 duration = qtpad->trak->tkhd.duration;
1830 atom_trak_add_elst_entry (qtpad->trak, lateness, (guint32) - 1,
1831 (guint32) (1 * 65536.0));
1832 atom_trak_add_elst_entry (qtpad->trak, duration, 0,
1833 (guint32) (1 * 65536.0));
1835 /* need to add the empty time to the trak duration */
1836 qtpad->trak->tkhd.duration += lateness;
1840 /* tags into file metadata */
1841 gst_qt_mux_setup_metadata (qtmux);
1843 large_file = (qtmux->mdat_size > MDAT_LARGE_FILE_LIMIT);
1844 /* if faststart, update the offset of the atoms in the movie with the offset
1845 * that the movie headers before mdat will cause.
1846 * Also, send the ftyp */
1847 if (qtmux->fast_start_file) {
1848 GstFlowReturn flow_ret;
1851 flow_ret = gst_qt_mux_prepare_and_send_ftyp (qtmux);
1852 if (flow_ret != GST_FLOW_OK) {
1855 /* copy into NULL to obtain size */
1856 if (!atom_moov_copy_data (qtmux->moov, NULL, &size, &offset))
1857 goto serialize_error;
1858 GST_DEBUG_OBJECT (qtmux, "calculated moov atom size %" G_GUINT64_FORMAT,
1860 offset += qtmux->header_size + (large_file ? 16 : 8);
1862 /* sum up with the extra atoms size */
1863 ret = gst_qt_mux_send_extra_atoms (qtmux, FALSE, &offset, FALSE);
1864 if (ret != GST_FLOW_OK)
1867 offset = qtmux->header_size;
1869 atom_moov_chunks_add_offset (qtmux->moov, offset);
1872 /* note: as of this point, we no longer care about tracking written data size,
1873 * since there is no more use for it anyway */
1874 ret = gst_qt_mux_send_moov (qtmux, NULL, FALSE);
1875 if (ret != GST_FLOW_OK)
1879 ret = gst_qt_mux_send_extra_atoms (qtmux, TRUE, NULL, FALSE);
1880 if (ret != GST_FLOW_OK)
1883 /* if needed, send mdat atom and move buffered data into it */
1884 if (qtmux->fast_start_file) {
1885 /* mdat_size = accumulated (buffered data) */
1886 ret = gst_qt_mux_send_mdat_header (qtmux, NULL, qtmux->mdat_size,
1888 if (ret != GST_FLOW_OK)
1890 ret = gst_qt_mux_send_buffered_data (qtmux, NULL);
1891 if (ret != GST_FLOW_OK)
1894 /* mdat needs update iff not using faststart */
1895 GST_DEBUG_OBJECT (qtmux, "updating mdat size");
1896 ret = gst_qt_mux_update_mdat_size (qtmux, qtmux->mdat_pos,
1897 qtmux->mdat_size, NULL);
1898 /* note; no seeking back to the end of file is done,
1899 * since we no longer write anything anyway */
1907 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
1908 ("Failed to serialize moov"));
1909 return GST_FLOW_ERROR;
1913 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL), ("Failed to send ftyp"));
1914 return GST_FLOW_ERROR;
1918 static GstFlowReturn
1919 gst_qt_mux_pad_fragment_add_buffer (GstQTMux * qtmux, GstQTPad * pad,
1920 GstBuffer * buf, gboolean force, guint32 nsamples, gint64 dts,
1921 guint32 delta, guint32 size, gboolean sync, gint64 pts_offset)
1923 GstFlowReturn ret = GST_FLOW_OK;
1925 /* setup if needed */
1926 if (G_UNLIKELY (!pad->traf || force))
1930 /* flush pad fragment if threshold reached,
1931 * or at new keyframe if we should be minding those in the first place */
1932 if (G_UNLIKELY (force || (sync && pad->sync) ||
1933 pad->fragment_duration < (gint64) delta)) {
1935 guint64 size = 0, offset = 0;
1936 guint8 *data = NULL;
1938 guint i, total_size;
1940 /* now we know where moof ends up, update offset in tfra */
1942 atom_tfra_update_offset (pad->tfra, qtmux->header_size);
1944 moof = atom_moof_new (qtmux->context, qtmux->fragment_sequence);
1945 /* takes ownership */
1946 atom_moof_add_traf (moof, pad->traf);
1948 atom_moof_copy_data (moof, &data, &size, &offset);
1949 buffer = _gst_buffer_new_take_data (data, offset);
1950 GST_LOG_OBJECT (qtmux, "writing moof size %d", GST_BUFFER_SIZE (buffer));
1951 ret = gst_qt_mux_send_buffer (qtmux, buffer, &qtmux->header_size, FALSE);
1953 /* and actual data */
1955 for (i = 0; i < atom_array_get_len (&pad->fragment_buffers); i++) {
1957 GST_BUFFER_SIZE (atom_array_index (&pad->fragment_buffers, i));
1960 GST_LOG_OBJECT (qtmux, "writing %d buffers, total_size %d",
1961 atom_array_get_len (&pad->fragment_buffers), total_size);
1962 if (ret == GST_FLOW_OK)
1963 ret = gst_qt_mux_send_mdat_header (qtmux, &qtmux->header_size, total_size,
1965 for (i = 0; i < atom_array_get_len (&pad->fragment_buffers); i++) {
1966 if (G_LIKELY (ret == GST_FLOW_OK))
1967 ret = gst_qt_mux_send_buffer (qtmux,
1968 atom_array_index (&pad->fragment_buffers, i), &qtmux->header_size,
1971 gst_buffer_unref (atom_array_index (&pad->fragment_buffers, i));
1974 atom_array_clear (&pad->fragment_buffers);
1975 atom_moof_free (moof);
1976 qtmux->fragment_sequence++;
1981 if (G_UNLIKELY (!pad->traf)) {
1982 GST_LOG_OBJECT (qtmux, "setting up new fragment");
1983 pad->traf = atom_traf_new (qtmux->context, atom_trak_get_id (pad->trak));
1984 atom_array_init (&pad->fragment_buffers, 512);
1985 pad->fragment_duration = gst_util_uint64_scale (qtmux->fragment_duration,
1986 atom_trak_get_timescale (pad->trak), 1000);
1988 if (G_UNLIKELY (qtmux->mfra && !pad->tfra)) {
1989 pad->tfra = atom_tfra_new (qtmux->context, atom_trak_get_id (pad->trak));
1990 atom_mfra_add_tfra (qtmux->mfra, pad->tfra);
1994 /* add buffer and metadata */
1995 atom_traf_add_samples (pad->traf, delta, size, sync, pts_offset,
1997 atom_array_append (&pad->fragment_buffers, buf, 256);
1998 pad->fragment_duration -= delta;
2001 guint32 sn = atom_traf_get_sample_num (pad->traf);
2003 if ((sync && pad->sync) || (sn == 1 && !pad->sync))
2004 atom_tfra_add_entry (pad->tfra, dts, sn);
2007 if (G_UNLIKELY (force))
2013 /* sigh, tiny list helpers to re-order stuff */
2015 gst_qt_mux_push_ts (GstQTMux * qtmux, GstQTPad * pad, GstClockTime ts)
2019 for (i = 0; (i < QTMUX_NO_OF_TS) && (i < pad->ts_n_entries); i++) {
2020 if (ts > pad->ts_entries[i])
2023 memmove (&pad->ts_entries[i + 1], &pad->ts_entries[i],
2024 sizeof (GstClockTime) * (pad->ts_n_entries - i));
2025 pad->ts_entries[i] = ts;
2026 pad->ts_n_entries++;
2029 /* takes ownership of @buf */
2031 gst_qt_mux_get_asc_buffer_ts (GstQTMux * qtmux, GstQTPad * pad, GstBuffer * buf)
2033 const gint wrap = G_N_ELEMENTS (pad->buf_entries);
2036 /* store buffer and ts, latter ordered */
2038 pad->buf_entries[pad->buf_tail++] = buf;
2039 pad->buf_tail %= wrap;
2040 gst_qt_mux_push_ts (qtmux, pad, GST_BUFFER_TIMESTAMP (buf));
2043 if (pad->ts_n_entries && (!buf || pad->ts_n_entries >= QTMUX_NO_OF_TS)) {
2044 ts = pad->ts_entries[--pad->ts_n_entries];
2045 buf = pad->buf_entries[pad->buf_head];
2046 pad->buf_entries[pad->buf_head++] = NULL;
2047 pad->buf_head %= wrap;
2048 buf = gst_buffer_make_metadata_writable (buf);
2049 /* track original ts (= pts ?) for later */
2050 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_TIMESTAMP (buf);
2051 GST_BUFFER_TIMESTAMP (buf) = ts;
2052 GST_DEBUG_OBJECT (qtmux, "next buffer uses reordered ts %" GST_TIME_FORMAT,
2053 GST_TIME_ARGS (ts));
2062 * Here we push the buffer and update the tables in the track atoms
2064 static GstFlowReturn
2065 gst_qt_mux_add_buffer (GstQTMux * qtmux, GstQTPad * pad, GstBuffer * buf)
2067 GstBuffer *last_buf = NULL;
2068 GstClockTime duration;
2069 guint nsamples, sample_size;
2070 guint64 chunk_offset;
2071 gint64 last_dts, scaled_duration;
2072 gint64 pts_offset = 0;
2073 gboolean sync = FALSE, do_pts = FALSE;
2074 gboolean drain = (buf == NULL);
2075 GstFlowReturn ret = GST_FLOW_OK;
2078 goto not_negotiated;
2080 /* if this pad has a prepare function, call it */
2081 if (pad->prepare_buf_func != NULL) {
2082 buf = pad->prepare_buf_func (pad, buf, qtmux);
2086 last_buf = pad->last_buf;
2087 if (G_UNLIKELY (qtmux->dts_method == DTS_METHOD_REORDER)) {
2088 buf = gst_qt_mux_get_asc_buffer_ts (qtmux, pad, buf);
2089 if (!buf && !last_buf) {
2090 GST_DEBUG_OBJECT (qtmux, "no reordered buffer");
2095 if (last_buf == NULL) {
2096 #ifndef GST_DISABLE_GST_DEBUG
2098 GST_DEBUG_OBJECT (qtmux, "Pad %s has no previous buffer stored and "
2099 "received NULL buffer, doing nothing",
2100 GST_PAD_NAME (pad->collect.pad));
2102 GST_LOG_OBJECT (qtmux,
2103 "Pad %s has no previous buffer stored, storing now",
2104 GST_PAD_NAME (pad->collect.pad));
2107 pad->last_buf = buf;
2110 gst_buffer_ref (last_buf);
2112 /* nasty heuristic mess to guestimate dealing with DTS/PTS,
2113 * while also trying to stay close to input ts to preserve sync,
2114 * so in DTS_METHOD_DD:
2115 * - prefer using input ts where possible
2116 * - if those detected out-of-order (*), mark as out-of-order
2117 * - if in out-of-order, then
2118 * - if duration available, use that as delta
2119 * Also mind to preserve sync between streams, and adding
2120 * durations might drift, so try to resync when we expect
2121 * input ts == (sum of durations), which is at some keyframe input frame.
2122 * - if no duration available, we are actually in serious trouble and need
2123 * to hack around that, so we fail.
2124 * To remedy failure, alternatively, in DTS_METHOD_REORDER:
2125 * - collect some buffers and re-order timestamp,
2126 * then process the oldest buffer with smallest timestamps.
2127 * This should typically compensate for some codec's handywork with ts.
2128 * ... but in case this makes ts end up where not expected, in DTS_METHOD_ASC:
2129 * - keep each ts with its buffer and still keep a list of most recent X ts,
2130 * use the (ascending) minimum of those as DTS (and the difference as ts delta),
2131 * and use this DTS as a basis to obtain a (positive) CTS offset.
2132 * This should yield exact PTS == buffer ts, but it seems not all players
2133 * out there are aware of ctts pts ...
2135 * 0.11 Phew, can we (pretty) please please sort out DTS/PTS on buffers ...
2137 if (G_LIKELY (buf) && !pad->is_out_of_order) {
2138 if (G_LIKELY (GST_BUFFER_TIMESTAMP_IS_VALID (last_buf) &&
2139 GST_BUFFER_TIMESTAMP_IS_VALID (buf))) {
2140 if ((GST_BUFFER_TIMESTAMP (buf) < GST_BUFFER_TIMESTAMP (last_buf))) {
2141 GST_DEBUG_OBJECT (qtmux, "detected out-of-order input");
2142 pad->is_out_of_order = TRUE;
2145 /* this is pretty bad */
2146 GST_WARNING_OBJECT (qtmux, "missing input timestamp");
2147 /* fall back to durations */
2148 pad->is_out_of_order = TRUE;
2152 /* would have to be some unusual input, but not impossible */
2153 if (G_UNLIKELY (qtmux->dts_method == DTS_METHOD_REORDER &&
2154 pad->is_out_of_order)) {
2158 /* fall back to duration if last buffer or
2159 * out-of-order (determined previously), otherwise use input ts */
2161 (pad->is_out_of_order && qtmux->dts_method == DTS_METHOD_DD)) {
2162 if (!GST_BUFFER_DURATION_IS_VALID (last_buf)) {
2163 /* be forgiving for some possibly last upstream flushed buffer */
2166 GST_WARNING_OBJECT (qtmux, "no duration for last buffer");
2167 /* iso spec recommends some small value, try 0 */
2170 duration = GST_BUFFER_DURATION (last_buf);
2171 /* avoid drift in sum timestamps,
2172 * so use input timestamp for suitable keyframe */
2173 if (buf && !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) &&
2174 GST_BUFFER_TIMESTAMP (buf) >= pad->last_dts) {
2175 GST_DEBUG_OBJECT (qtmux, "resyncing out-of-order input to ts; "
2176 "replacing %" GST_TIME_FORMAT " by %" GST_TIME_FORMAT,
2177 GST_TIME_ARGS (pad->last_dts + duration),
2178 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2179 duration = GST_BUFFER_TIMESTAMP (buf) - pad->last_dts;
2182 } else if (qtmux->dts_method != DTS_METHOD_ASC) {
2183 duration = GST_BUFFER_TIMESTAMP (buf) - GST_BUFFER_TIMESTAMP (last_buf);
2187 g_assert (qtmux->dts_method == DTS_METHOD_ASC);
2188 if (!qtmux->guess_pts)
2191 /* add timestamp to queue; keeps in descending order */
2192 gst_qt_mux_push_ts (qtmux, pad, GST_BUFFER_TIMESTAMP (last_buf));
2193 /* chuck out smallest/last one if we have enough */
2194 if (G_LIKELY (pad->ts_n_entries > QTMUX_NO_OF_TS))
2195 pad->ts_n_entries--;
2196 /* peek the now smallest timestamp */
2197 ts = pad->ts_entries[pad->ts_n_entries - 1];
2198 /* these tails are expected to be (strictly) ascending with
2199 * large enough history */
2200 GST_DEBUG_OBJECT (qtmux, "ASC method; base timestamp %" GST_TIME_FORMAT,
2201 GST_TIME_ARGS (ts));
2202 if (ts >= pad->last_dts) {
2203 duration = ts - pad->last_dts;
2205 /* fallback to previous value, negative ct offset might handle */
2206 GST_WARNING_OBJECT (qtmux, "unexpected decrease in timestamp");
2209 /* arrange for small non-zero duration/delta << expected frame time */
2210 ts = gst_util_uint64_scale (10, GST_SECOND,
2211 atom_trak_get_timescale (pad->trak));
2212 duration = MAX (duration, ts);
2215 gst_buffer_replace (&pad->last_buf, buf);
2217 last_dts = gst_util_uint64_scale_round (pad->last_dts,
2218 atom_trak_get_timescale (pad->trak), GST_SECOND);
2220 /* fragments only deal with 1 buffer == 1 chunk (== 1 sample) */
2221 if (pad->sample_size && !qtmux->fragment_sequence) {
2222 /* Constant size packets: usually raw audio (with many samples per
2223 buffer (= chunk)), but can also be fixed-packet-size codecs like ADPCM
2225 sample_size = pad->sample_size;
2226 if (GST_BUFFER_SIZE (last_buf) % sample_size != 0)
2227 goto fragmented_sample;
2228 /* note: qt raw audio storage warps it implicitly into a timewise
2229 * perfect stream, discarding buffer times */
2230 if (GST_BUFFER_DURATION (last_buf) != GST_CLOCK_TIME_NONE) {
2231 nsamples = gst_util_uint64_scale_round (GST_BUFFER_DURATION (last_buf),
2232 atom_trak_get_timescale (pad->trak), GST_SECOND);
2234 nsamples = GST_BUFFER_SIZE (last_buf) / sample_size;
2236 duration = GST_BUFFER_DURATION (last_buf) / nsamples;
2238 /* timescale = samplerate */
2239 scaled_duration = 1;
2240 pad->last_dts += duration * nsamples;
2243 sample_size = GST_BUFFER_SIZE (last_buf);
2244 if (pad->have_dts) {
2246 pad->last_dts = GST_BUFFER_OFFSET_END (last_buf);
2247 if ((gint64) (pad->last_dts) < 0) {
2248 scaled_dts = -gst_util_uint64_scale_round (-pad->last_dts,
2249 atom_trak_get_timescale (pad->trak), GST_SECOND);
2251 scaled_dts = gst_util_uint64_scale_round (pad->last_dts,
2252 atom_trak_get_timescale (pad->trak), GST_SECOND);
2254 scaled_duration = scaled_dts - last_dts;
2255 last_dts = scaled_dts;
2257 /* first convert intended timestamp (in GstClockTime resolution) to
2258 * trak timescale, then derive delta;
2259 * this ensures sums of (scale)delta add up to converted timestamp,
2260 * which only deviates at most 1/scale from timestamp itself */
2261 scaled_duration = gst_util_uint64_scale_round (pad->last_dts + duration,
2262 atom_trak_get_timescale (pad->trak), GST_SECOND) - last_dts;
2263 pad->last_dts += duration;
2266 chunk_offset = qtmux->mdat_size;
2268 GST_LOG_OBJECT (qtmux,
2269 "Pad (%s) dts updated to %" GST_TIME_FORMAT,
2270 GST_PAD_NAME (pad->collect.pad), GST_TIME_ARGS (pad->last_dts));
2271 GST_LOG_OBJECT (qtmux,
2272 "Adding %d samples to track, duration: %" G_GUINT64_FORMAT
2273 " size: %" G_GUINT32_FORMAT " chunk offset: %" G_GUINT64_FORMAT,
2274 nsamples, scaled_duration, sample_size, chunk_offset);
2276 /* might be a sync sample */
2278 !GST_BUFFER_FLAG_IS_SET (last_buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
2279 GST_LOG_OBJECT (qtmux, "Adding new sync sample entry for track of pad %s",
2280 GST_PAD_NAME (pad->collect.pad));
2284 /* optionally calculate ctts entry values
2285 * (if composition-time expected different from decoding-time) */
2286 /* really not recommended:
2287 * - decoder typically takes care of dts/pts issues
2288 * - in case of out-of-order, dts may only be determined as above
2289 * (e.g. sum of duration), which may be totally different from
2290 * buffer timestamps in case of multiple segment, non-perfect streams
2291 * (and just perhaps maybe with some luck segment_to_running_time
2292 * or segment_to_media_time might get near to it) */
2293 if ((pad->have_dts || qtmux->guess_pts)) {
2296 pts = qtmux->dts_method == DTS_METHOD_REORDER ?
2297 GST_BUFFER_OFFSET_END (last_buf) : GST_BUFFER_TIMESTAMP (last_buf);
2298 pts = gst_util_uint64_scale_round (pts,
2299 atom_trak_get_timescale (pad->trak), GST_SECOND);
2300 pts_offset = (gint64) (pts - last_dts);
2302 GST_LOG_OBJECT (qtmux, "Adding ctts entry for pad %s: %" G_GINT64_FORMAT,
2303 GST_PAD_NAME (pad->collect.pad), pts_offset);
2307 * Each buffer starts a new chunk, so we can assume the buffer
2308 * duration is the chunk duration
2310 if (GST_CLOCK_TIME_IS_VALID (duration) && (duration > qtmux->longest_chunk ||
2311 !GST_CLOCK_TIME_IS_VALID (qtmux->longest_chunk))) {
2312 GST_DEBUG_OBJECT (qtmux, "New longest chunk found: %" GST_TIME_FORMAT
2313 ", pad %s", GST_TIME_ARGS (duration), GST_PAD_NAME (pad->collect.pad));
2314 qtmux->longest_chunk = duration;
2317 /* if this is the first buffer, store the timestamp */
2318 if (G_UNLIKELY (pad->first_ts == GST_CLOCK_TIME_NONE) && last_buf) {
2319 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (last_buf))) {
2320 pad->first_ts = GST_BUFFER_TIMESTAMP (last_buf);
2322 GST_DEBUG_OBJECT (qtmux, "First buffer for pad %s has no timestamp, "
2323 "using 0 as first timestamp", GST_PAD_NAME (pad->collect.pad));
2326 GST_DEBUG_OBJECT (qtmux, "Stored first timestamp for pad %s %"
2327 GST_TIME_FORMAT, GST_PAD_NAME (pad->collect.pad),
2328 GST_TIME_ARGS (pad->first_ts));
2331 /* now we go and register this buffer/sample all over */
2332 /* note that a new chunk is started each time (not fancy but works) */
2333 if (qtmux->moov_recov_file) {
2334 if (!atoms_recov_write_trak_samples (qtmux->moov_recov_file, pad->trak,
2335 nsamples, (gint32) scaled_duration, sample_size, chunk_offset, sync,
2336 do_pts, pts_offset)) {
2337 GST_WARNING_OBJECT (qtmux, "Failed to write sample information to "
2338 "recovery file, disabling recovery");
2339 fclose (qtmux->moov_recov_file);
2340 qtmux->moov_recov_file = NULL;
2345 gst_buffer_unref (buf);
2347 if (qtmux->fragment_sequence) {
2348 /* ensure that always sync samples are marked as such */
2349 ret = gst_qt_mux_pad_fragment_add_buffer (qtmux, pad, last_buf,
2350 buf == NULL, nsamples, last_dts, (gint32) scaled_duration, sample_size,
2351 !pad->sync || sync, pts_offset);
2353 atom_trak_add_samples (pad->trak, nsamples, (gint32) scaled_duration,
2354 sample_size, chunk_offset, sync, pts_offset);
2355 ret = gst_qt_mux_send_buffer (qtmux, last_buf, &qtmux->mdat_size, TRUE);
2359 if (G_UNLIKELY (drain && qtmux->dts_method == DTS_METHOD_REORDER &&
2360 ret == GST_FLOW_OK)) {
2371 gst_buffer_unref (buf);
2372 gst_buffer_unref (last_buf);
2373 return GST_FLOW_ERROR;
2377 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2378 ("Received buffer without timestamp/duration. "
2379 "Using e.g. dts-method=reorder might help."));
2384 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2385 ("DTS method failed to re-order timestamps."));
2390 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2391 ("Selected DTS method also needs PTS enabled."));
2396 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2397 ("Audio buffer contains fragmented sample."));
2402 GST_ELEMENT_ERROR (qtmux, CORE, NEGOTIATION, (NULL),
2403 ("format wasn't negotiated before buffer flow on pad %s",
2404 GST_PAD_NAME (pad->collect.pad)));
2406 gst_buffer_unref (buf);
2407 return GST_FLOW_NOT_NEGOTIATED;
2411 static GstFlowReturn
2412 gst_qt_mux_collected (GstCollectPads * pads, gpointer user_data)
2414 GstFlowReturn ret = GST_FLOW_OK;
2415 GstQTMux *qtmux = GST_QT_MUX_CAST (user_data);
2417 GstQTPad *best_pad = NULL;
2418 GstClockTime time, best_time = GST_CLOCK_TIME_NONE;
2421 if (G_UNLIKELY (qtmux->state == GST_QT_MUX_STATE_STARTED)) {
2422 if ((ret = gst_qt_mux_start_file (qtmux)) != GST_FLOW_OK)
2425 qtmux->state = GST_QT_MUX_STATE_DATA;
2428 if (G_UNLIKELY (qtmux->state == GST_QT_MUX_STATE_EOS))
2429 return GST_FLOW_UNEXPECTED;
2431 /* select the best buffer */
2432 walk = qtmux->collect->data;
2435 GstCollectData *data;
2437 data = (GstCollectData *) walk->data;
2438 pad = (GstQTPad *) data;
2440 walk = g_slist_next (walk);
2442 buf = gst_collect_pads_peek (pads, data);
2444 GST_LOG_OBJECT (qtmux, "Pad %s has no buffers",
2445 GST_PAD_NAME (pad->collect.pad));
2448 time = GST_BUFFER_TIMESTAMP (buf);
2449 gst_buffer_unref (buf);
2451 /* invalid should pass */
2452 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
2454 gst_segment_to_running_time (&data->segment, GST_FORMAT_TIME, time);
2455 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
2456 GST_DEBUG_OBJECT (qtmux, "clipping buffer on pad %s outside segment",
2457 GST_PAD_NAME (data->pad));
2458 buf = gst_collect_pads_pop (pads, data);
2459 gst_buffer_unref (buf);
2464 if (best_pad == NULL || !GST_CLOCK_TIME_IS_VALID (time) ||
2465 (GST_CLOCK_TIME_IS_VALID (best_time) && time < best_time)) {
2471 if (best_pad != NULL) {
2472 GST_LOG_OBJECT (qtmux, "selected pad %s with time %" GST_TIME_FORMAT,
2473 GST_PAD_NAME (best_pad->collect.pad), GST_TIME_ARGS (best_time));
2474 buf = gst_collect_pads_pop (pads, &best_pad->collect);
2475 buf = gst_buffer_make_metadata_writable (buf);
2476 GST_BUFFER_TIMESTAMP (buf) = best_time;
2477 ret = gst_qt_mux_add_buffer (qtmux, best_pad, buf);
2479 ret = gst_qt_mux_stop_file (qtmux);
2480 if (ret == GST_FLOW_OK) {
2481 GST_DEBUG_OBJECT (qtmux, "Pushing eos");
2482 gst_pad_push_event (qtmux->srcpad, gst_event_new_eos ());
2483 ret = GST_FLOW_UNEXPECTED;
2485 GST_WARNING_OBJECT (qtmux, "Failed to stop file: %s",
2486 gst_flow_get_name (ret));
2488 qtmux->state = GST_QT_MUX_STATE_EOS;
2495 check_field (GQuark field_id, const GValue * value, gpointer user_data)
2497 GstStructure *structure = (GstStructure *) user_data;
2498 const GValue *other = gst_structure_id_get_value (structure, field_id);
2501 return gst_value_compare (value, other) == GST_VALUE_EQUAL;
2505 gst_qtmux_caps_is_subset_full (GstQTMux * qtmux, GstCaps * subset,
2508 GstStructure *sub_s = gst_caps_get_structure (subset, 0);
2509 GstStructure *sup_s = gst_caps_get_structure (superset, 0);
2511 return gst_structure_foreach (sub_s, check_field, sup_s);
2515 gst_qt_mux_audio_sink_set_caps (GstPad * pad, GstCaps * caps)
2517 GstQTMux *qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
2518 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
2519 GstQTPad *qtpad = NULL;
2520 GstStructure *structure;
2521 const gchar *mimetype;
2522 gint rate, channels;
2523 const GValue *value = NULL;
2524 const GstBuffer *codec_data = NULL;
2525 GstQTMuxFormat format;
2526 AudioSampleEntry entry = { 0, };
2527 AtomInfo *ext_atom = NULL;
2528 gint constant_size = 0;
2529 const gchar *stream_format;
2530 GstCaps *current_caps = NULL;
2532 /* find stream data */
2533 qtpad = (GstQTPad *) gst_pad_get_element_private (pad);
2536 qtpad->prepare_buf_func = NULL;
2538 /* does not go well to renegotiate stream mid-way, unless
2539 * the old caps are a subset of the new one (this means upstream
2540 * added more info to the caps, as both should be 'fixed' caps) */
2541 if (qtpad->fourcc) {
2542 g_object_get (pad, "caps", ¤t_caps, NULL);
2543 g_assert (caps != NULL);
2545 if (!gst_qtmux_caps_is_subset_full (qtmux, current_caps, caps)) {
2546 goto refuse_renegotiation;
2548 GST_DEBUG_OBJECT (qtmux,
2549 "pad %s accepted renegotiation to %" GST_PTR_FORMAT " from %"
2550 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, GST_PAD_CAPS (pad));
2553 GST_DEBUG_OBJECT (qtmux, "%s:%s, caps=%" GST_PTR_FORMAT,
2554 GST_DEBUG_PAD_NAME (pad), caps);
2556 format = qtmux_klass->format;
2557 structure = gst_caps_get_structure (caps, 0);
2558 mimetype = gst_structure_get_name (structure);
2561 if (!gst_structure_get_int (structure, "channels", &channels) ||
2562 !gst_structure_get_int (structure, "rate", &rate)) {
2567 value = gst_structure_get_value (structure, "codec_data");
2569 codec_data = gst_value_get_buffer (value);
2571 qtpad->is_out_of_order = FALSE;
2572 qtpad->have_dts = FALSE;
2574 /* set common properties */
2575 entry.sample_rate = rate;
2576 entry.channels = channels;
2578 entry.sample_size = 16;
2579 /* this is the typical compressed case */
2580 if (format == GST_QT_MUX_FORMAT_QT) {
2582 entry.compression_id = -2;
2585 /* now map onto a fourcc, and some extra properties */
2586 if (strcmp (mimetype, "audio/mpeg") == 0) {
2587 gint mpegversion = 0;
2590 gst_structure_get_int (structure, "mpegversion", &mpegversion);
2591 switch (mpegversion) {
2593 gst_structure_get_int (structure, "layer", &layer);
2597 /* note: QuickTime player does not like mp3 either way in iso/mp4 */
2598 if (format == GST_QT_MUX_FORMAT_QT)
2599 entry.fourcc = FOURCC__mp3;
2601 entry.fourcc = FOURCC_mp4a;
2603 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG1_P3,
2604 ESDS_STREAM_TYPE_AUDIO, codec_data, qtpad->avg_bitrate,
2605 qtpad->max_bitrate);
2607 entry.samples_per_packet = 1152;
2608 entry.bytes_per_sample = 2;
2614 /* check stream-format */
2615 stream_format = gst_structure_get_string (structure, "stream-format");
2616 if (stream_format) {
2617 if (strcmp (stream_format, "raw") != 0) {
2618 GST_WARNING_OBJECT (qtmux, "Unsupported AAC stream-format %s, "
2619 "please use 'raw'", stream_format);
2623 GST_WARNING_OBJECT (qtmux, "No stream-format present in caps, "
2627 if (!codec_data || GST_BUFFER_SIZE (codec_data) < 2)
2628 GST_WARNING_OBJECT (qtmux, "no (valid) codec_data for AAC audio");
2630 guint8 profile = GST_READ_UINT8 (GST_BUFFER_DATA (codec_data));
2632 /* warn if not Low Complexity profile */
2635 GST_WARNING_OBJECT (qtmux,
2636 "non-LC AAC may not run well on (Apple) QuickTime/iTunes");
2640 entry.fourcc = FOURCC_mp4a;
2642 if (format == GST_QT_MUX_FORMAT_QT)
2643 ext_atom = build_mov_aac_extension (qtpad->trak, codec_data,
2644 qtpad->avg_bitrate, qtpad->max_bitrate);
2647 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG4_P3,
2648 ESDS_STREAM_TYPE_AUDIO, codec_data, qtpad->avg_bitrate,
2649 qtpad->max_bitrate);
2654 } else if (strcmp (mimetype, "audio/AMR") == 0) {
2655 entry.fourcc = FOURCC_samr;
2656 entry.sample_size = 16;
2657 entry.samples_per_packet = 160;
2658 entry.bytes_per_sample = 2;
2659 ext_atom = build_amr_extension ();
2660 } else if (strcmp (mimetype, "audio/AMR-WB") == 0) {
2661 entry.fourcc = FOURCC_sawb;
2662 entry.sample_size = 16;
2663 entry.samples_per_packet = 320;
2664 entry.bytes_per_sample = 2;
2665 ext_atom = build_amr_extension ();
2666 } else if (strcmp (mimetype, "audio/x-raw-int") == 0) {
2672 if (!gst_structure_get_int (structure, "width", &width) ||
2673 !gst_structure_get_int (structure, "depth", &depth) ||
2674 !gst_structure_get_boolean (structure, "signed", &sign)) {
2675 GST_DEBUG_OBJECT (qtmux, "broken caps, width/depth/signed field missing");
2680 endianness = G_BYTE_ORDER;
2681 } else if (!gst_structure_get_int (structure, "endianness", &endianness)) {
2682 GST_DEBUG_OBJECT (qtmux, "broken caps, endianness field missing");
2686 /* spec has no place for a distinction in these */
2687 if (width != depth) {
2688 GST_DEBUG_OBJECT (qtmux, "width must be same as depth!");
2693 if (endianness == G_LITTLE_ENDIAN)
2694 entry.fourcc = FOURCC_sowt;
2695 else if (endianness == G_BIG_ENDIAN)
2696 entry.fourcc = FOURCC_twos;
2697 /* maximum backward compatibility; only new version for > 16 bit */
2700 /* not compressed in any case */
2701 entry.compression_id = 0;
2702 /* QT spec says: max at 16 bit even if sample size were actually larger,
2703 * however, most players (e.g. QuickTime!) seem to disagree, so ... */
2704 entry.sample_size = depth;
2705 entry.bytes_per_sample = depth / 8;
2706 entry.samples_per_packet = 1;
2707 entry.bytes_per_packet = depth / 8;
2708 entry.bytes_per_frame = entry.bytes_per_packet * channels;
2710 if (width == 8 && depth == 8) {
2711 /* fall back to old 8-bit version */
2712 entry.fourcc = FOURCC_raw_;
2714 entry.compression_id = 0;
2715 entry.sample_size = 8;
2717 GST_DEBUG_OBJECT (qtmux, "non 8-bit PCM must be signed");
2721 constant_size = (depth / 8) * channels;
2722 } else if (strcmp (mimetype, "audio/x-alaw") == 0) {
2723 entry.fourcc = FOURCC_alaw;
2724 entry.samples_per_packet = 1023;
2725 entry.bytes_per_sample = 2;
2726 } else if (strcmp (mimetype, "audio/x-mulaw") == 0) {
2727 entry.fourcc = FOURCC_ulaw;
2728 entry.samples_per_packet = 1023;
2729 entry.bytes_per_sample = 2;
2730 } else if (strcmp (mimetype, "audio/x-adpcm") == 0) {
2732 if (!gst_structure_get_int (structure, "block_align", &blocksize)) {
2733 GST_DEBUG_OBJECT (qtmux, "broken caps, block_align missing");
2736 /* Currently only supports WAV-style IMA ADPCM, for which the codec id is
2738 entry.fourcc = MS_WAVE_FOURCC (0x11);
2739 /* 4 byte header per channel (including one sample). 2 samples per byte
2740 remaining. Simplifying gives the following (samples per block per
2742 entry.samples_per_packet = 2 * blocksize / channels - 7;
2743 entry.bytes_per_sample = 2;
2745 entry.bytes_per_frame = blocksize;
2746 entry.bytes_per_packet = blocksize / channels;
2747 /* ADPCM has constant size packets */
2749 /* TODO: I don't really understand why this helps, but it does! Constant
2750 * size and compression_id of -2 seem to be incompatible, and other files
2751 * in the wild use this too. */
2752 entry.compression_id = -1;
2754 ext_atom = build_ima_adpcm_extension (channels, rate, blocksize);
2755 } else if (strcmp (mimetype, "audio/x-alac") == 0) {
2756 GstBuffer *codec_config;
2759 entry.fourcc = FOURCC_alac;
2760 /* let's check if codec data already comes with 'alac' atom prefix */
2761 if (!codec_data || (len = GST_BUFFER_SIZE (codec_data)) < 28) {
2762 GST_DEBUG_OBJECT (qtmux, "broken caps, codec data missing");
2765 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (codec_data) + 4) == FOURCC_alac) {
2767 codec_config = gst_buffer_create_sub ((GstBuffer *) codec_data, 8, len);
2769 codec_config = gst_buffer_ref ((GstBuffer *) codec_data);
2772 /* does not look good, but perhaps some trailing unneeded stuff */
2773 GST_WARNING_OBJECT (qtmux, "unexpected codec-data size, possibly broken");
2775 if (format == GST_QT_MUX_FORMAT_QT)
2776 ext_atom = build_mov_alac_extension (qtpad->trak, codec_config);
2778 ext_atom = build_codec_data_extension (FOURCC_alac, codec_config);
2779 /* set some more info */
2780 entry.bytes_per_sample = 2;
2781 entry.samples_per_packet =
2782 GST_READ_UINT32_BE (GST_BUFFER_DATA (codec_config) + 4);
2783 gst_buffer_unref (codec_config);
2789 /* ok, set the pad info accordingly */
2790 qtpad->fourcc = entry.fourcc;
2791 qtpad->sample_size = constant_size;
2792 atom_trak_set_audio_type (qtpad->trak, qtmux->context, &entry,
2793 qtmux->trak_timescale ? qtmux->trak_timescale : entry.sample_rate,
2794 ext_atom, constant_size);
2796 gst_object_unref (qtmux);
2802 GST_WARNING_OBJECT (qtmux, "pad %s refused caps %" GST_PTR_FORMAT,
2803 GST_PAD_NAME (pad), caps);
2804 gst_object_unref (qtmux);
2807 refuse_renegotiation:
2809 GST_WARNING_OBJECT (qtmux,
2810 "pad %s refused renegotiation to %" GST_PTR_FORMAT,
2811 GST_PAD_NAME (pad), caps);
2812 gst_object_unref (qtmux);
2817 /* scale rate up or down by factor of 10 to fit into [1000,10000] interval */
2819 adjust_rate (guint64 rate)
2824 while (rate >= 10000)
2830 return (guint32) rate;
2834 gst_qt_mux_video_sink_set_caps (GstPad * pad, GstCaps * caps)
2836 GstQTMux *qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
2837 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
2838 GstQTPad *qtpad = NULL;
2839 GstStructure *structure;
2840 const gchar *mimetype;
2841 gint width, height, depth = -1;
2842 gint framerate_num, framerate_den;
2844 const GValue *value = NULL;
2845 const GstBuffer *codec_data = NULL;
2846 VisualSampleEntry entry = { 0, };
2847 GstQTMuxFormat format;
2848 AtomInfo *ext_atom = NULL;
2849 GList *ext_atom_list = NULL;
2850 gboolean sync = FALSE;
2851 int par_num, par_den;
2852 GstCaps *current_caps = NULL;
2854 /* find stream data */
2855 qtpad = (GstQTPad *) gst_pad_get_element_private (pad);
2858 qtpad->prepare_buf_func = NULL;
2860 /* does not go well to renegotiate stream mid-way, unless
2861 * the old caps are a subset of the new one (this means upstream
2862 * added more info to the caps, as both should be 'fixed' caps) */
2863 if (qtpad->fourcc) {
2864 g_object_get (pad, "caps", ¤t_caps, NULL);
2865 g_assert (caps != NULL);
2867 if (!gst_qtmux_caps_is_subset_full (qtmux, current_caps, caps)) {
2868 goto refuse_renegotiation;
2870 GST_DEBUG_OBJECT (qtmux,
2871 "pad %s accepted renegotiation to %" GST_PTR_FORMAT " from %"
2872 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, GST_PAD_CAPS (pad));
2875 GST_DEBUG_OBJECT (qtmux, "%s:%s, caps=%" GST_PTR_FORMAT,
2876 GST_DEBUG_PAD_NAME (pad), caps);
2878 format = qtmux_klass->format;
2879 structure = gst_caps_get_structure (caps, 0);
2880 mimetype = gst_structure_get_name (structure);
2882 /* required parts */
2883 if (!gst_structure_get_int (structure, "width", &width) ||
2884 !gst_structure_get_int (structure, "height", &height))
2889 /* works as a default timebase */
2890 framerate_num = 10000;
2892 gst_structure_get_fraction (structure, "framerate", &framerate_num,
2894 gst_structure_get_int (structure, "depth", &depth);
2895 value = gst_structure_get_value (structure, "codec_data");
2897 codec_data = gst_value_get_buffer (value);
2901 gst_structure_get_fraction (structure, "pixel-aspect-ratio", &par_num,
2904 qtpad->is_out_of_order = FALSE;
2906 /* bring frame numerator into a range that ensures both reasonable resolution
2907 * as well as a fair duration */
2908 rate = qtmux->trak_timescale ?
2909 qtmux->trak_timescale : adjust_rate (framerate_num);
2910 GST_DEBUG_OBJECT (qtmux, "Rate of video track selected: %" G_GUINT32_FORMAT,
2913 /* set common properties */
2914 entry.width = width;
2915 entry.height = height;
2916 entry.par_n = par_num;
2917 entry.par_d = par_den;
2918 /* should be OK according to qt and iso spec, override if really needed */
2919 entry.color_table_id = -1;
2920 entry.frame_count = 1;
2923 /* sync entries by default */
2926 /* now map onto a fourcc, and some extra properties */
2927 if (strcmp (mimetype, "video/x-raw-rgb") == 0) {
2930 entry.fourcc = FOURCC_raw_;
2931 gst_structure_get_int (structure, "bpp", &bpp);
2934 } else if (strcmp (mimetype, "video/x-raw-yuv") == 0) {
2938 gst_structure_get_fourcc (structure, "format", &format);
2940 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
2943 entry.fourcc = FOURCC_2vuy;
2944 entry.depth = depth;
2947 } else if (strcmp (mimetype, "video/x-h263") == 0) {
2949 if (format == GST_QT_MUX_FORMAT_QT)
2950 entry.fourcc = FOURCC_h263;
2952 entry.fourcc = FOURCC_s263;
2953 ext_atom = build_h263_extension ();
2954 if (ext_atom != NULL)
2955 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
2956 } else if (strcmp (mimetype, "video/x-divx") == 0 ||
2957 strcmp (mimetype, "video/mpeg") == 0) {
2960 if (strcmp (mimetype, "video/x-divx") == 0) {
2961 gst_structure_get_int (structure, "divxversion", &version);
2962 version = version == 5 ? 1 : 0;
2964 gst_structure_get_int (structure, "mpegversion", &version);
2965 version = version == 4 ? 1 : 0;
2968 entry.fourcc = FOURCC_mp4v;
2970 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG4_P2,
2971 ESDS_STREAM_TYPE_VISUAL, codec_data, qtpad->avg_bitrate,
2972 qtpad->max_bitrate);
2973 if (ext_atom != NULL)
2974 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
2976 GST_WARNING_OBJECT (qtmux, "no codec_data for MPEG4 video; "
2977 "output might not play in Apple QuickTime (try global-headers?)");
2979 } else if (strcmp (mimetype, "video/x-h264") == 0) {
2980 /* check if we accept these caps */
2981 if (gst_structure_has_field (structure, "stream-format")) {
2982 const gchar *format;
2983 const gchar *alignment;
2985 format = gst_structure_get_string (structure, "stream-format");
2986 alignment = gst_structure_get_string (structure, "alignment");
2988 if (strcmp (format, "avc") != 0 || alignment == NULL ||
2989 strcmp (alignment, "au") != 0) {
2990 GST_WARNING_OBJECT (qtmux, "Rejecting h264 caps, qtmux only accepts "
2991 "avc format with AU aligned samples");
2995 GST_WARNING_OBJECT (qtmux, "no stream-format field in h264 caps");
3000 GST_WARNING_OBJECT (qtmux, "no codec_data in h264 caps");
3004 entry.fourcc = FOURCC_avc1;
3005 if (qtpad->avg_bitrate == 0) {
3006 gint avg_bitrate = 0;
3007 gst_structure_get_int (structure, "bitrate", &avg_bitrate);
3008 qtpad->avg_bitrate = avg_bitrate;
3010 ext_atom = build_btrt_extension (0, qtpad->avg_bitrate, qtpad->max_bitrate);
3011 if (ext_atom != NULL)
3012 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3013 ext_atom = build_codec_data_extension (FOURCC_avcC, codec_data);
3014 if (ext_atom != NULL)
3015 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3016 } else if (strcmp (mimetype, "video/x-svq") == 0) {
3018 const GstBuffer *seqh = NULL;
3019 const GValue *seqh_value;
3022 gst_structure_get_int (structure, "svqversion", &version);
3024 entry.fourcc = FOURCC_SVQ3;
3028 seqh_value = gst_structure_get_value (structure, "seqh");
3030 seqh = gst_value_get_buffer (seqh_value);
3031 ext_atom = build_SMI_atom (seqh);
3033 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3036 /* we need to add the gamma anyway because quicktime might crash
3037 * when it doesn't find it */
3038 if (!gst_structure_get_double (structure, "applied-gamma", &gamma)) {
3039 /* it seems that using 0 here makes it ignored */
3042 ext_atom = build_gama_atom (gamma);
3044 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3046 GST_WARNING_OBJECT (qtmux, "SVQ version %d not supported. Please file "
3047 "a bug at http://bugzilla.gnome.org", version);
3049 } else if (strcmp (mimetype, "video/x-dv") == 0) {
3051 gboolean pal = TRUE;
3054 if (framerate_num != 25 || framerate_den != 1)
3056 gst_structure_get_int (structure, "dvversion", &version);
3057 /* fall back to typical one */
3063 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', 'c', 'p');
3065 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', 'c', ' ');
3069 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', '5', 'p');
3071 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', '5', 'n');
3074 GST_WARNING_OBJECT (qtmux, "unrecognized dv version");
3077 } else if (strcmp (mimetype, "image/jpeg") == 0) {
3078 entry.fourcc = FOURCC_jpeg;
3080 } else if (strcmp (mimetype, "image/x-j2c") == 0 ||
3081 strcmp (mimetype, "image/x-jpc") == 0) {
3083 const GValue *cmap_array;
3084 const GValue *cdef_array;
3088 if (strcmp (mimetype, "image/x-jpc") == 0) {
3089 qtpad->prepare_buf_func = gst_qt_mux_prepare_jpc_buffer;
3092 gst_structure_get_int (structure, "num-components", &ncomp);
3093 gst_structure_get_int (structure, "fields", &fields);
3094 cmap_array = gst_structure_get_value (structure, "component-map");
3095 cdef_array = gst_structure_get_value (structure, "channel-definitions");
3098 entry.fourcc = FOURCC_mjp2;
3100 if (gst_structure_get_fourcc (structure, "fourcc", &fourcc) &&
3102 build_jp2h_extension (qtpad->trak, width, height, fourcc, ncomp,
3103 cmap_array, cdef_array)) != NULL) {
3104 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3106 ext_atom = build_fiel_extension (fields);
3108 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3110 ext_atom = build_jp2x_extension (codec_data);
3112 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3114 GST_DEBUG_OBJECT (qtmux, "missing or invalid fourcc in jp2 caps");
3117 } else if (strcmp (mimetype, "video/x-vp8") == 0) {
3118 entry.fourcc = FOURCC_VP80;
3120 } else if (strcmp (mimetype, "video/x-qt-part") == 0) {
3123 gst_structure_get_fourcc (structure, "format", &fourcc);
3124 entry.fourcc = fourcc;
3125 qtpad->have_dts = TRUE;
3126 } else if (strcmp (mimetype, "video/x-mp4-part") == 0) {
3129 gst_structure_get_fourcc (structure, "format", &fourcc);
3130 entry.fourcc = fourcc;
3131 qtpad->have_dts = TRUE;
3137 /* ok, set the pad info accordingly */
3138 qtpad->fourcc = entry.fourcc;
3140 atom_trak_set_video_type (qtpad->trak, qtmux->context, &entry, rate,
3143 gst_object_unref (qtmux);
3149 GST_WARNING_OBJECT (qtmux, "pad %s refused caps %" GST_PTR_FORMAT,
3150 GST_PAD_NAME (pad), caps);
3151 gst_object_unref (qtmux);
3154 refuse_renegotiation:
3156 GST_WARNING_OBJECT (qtmux,
3157 "pad %s refused renegotiation to %" GST_PTR_FORMAT " from %"
3158 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, GST_PAD_CAPS (pad));
3159 gst_object_unref (qtmux);
3165 gst_qt_mux_sink_event (GstPad * pad, GstEvent * event)
3169 guint32 avg_bitrate = 0, max_bitrate = 0;
3171 qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
3172 switch (GST_EVENT_TYPE (event)) {
3173 case GST_EVENT_TAG:{
3175 GstTagSetter *setter = GST_TAG_SETTER (qtmux);
3176 GstTagMergeMode mode;
3178 GST_OBJECT_LOCK (qtmux);
3179 mode = gst_tag_setter_get_tag_merge_mode (setter);
3181 GST_DEBUG_OBJECT (qtmux, "received tag event");
3182 gst_event_parse_tag (event, &list);
3184 gst_tag_setter_merge_tags (setter, list, mode);
3185 GST_OBJECT_UNLOCK (qtmux);
3187 if (gst_tag_list_get_uint (list, GST_TAG_BITRATE, &avg_bitrate) |
3188 gst_tag_list_get_uint (list, GST_TAG_MAXIMUM_BITRATE, &max_bitrate)) {
3189 GstQTPad *qtpad = gst_pad_get_element_private (pad);
3192 if (avg_bitrate > 0 && avg_bitrate < G_MAXUINT32)
3193 qtpad->avg_bitrate = avg_bitrate;
3194 if (max_bitrate > 0 && max_bitrate < G_MAXUINT32)
3195 qtpad->max_bitrate = max_bitrate;
3204 ret = qtmux->collect_event (pad, event);
3205 gst_object_unref (qtmux);
3211 gst_qt_mux_release_pad (GstElement * element, GstPad * pad)
3213 GstQTMux *mux = GST_QT_MUX_CAST (element);
3216 GST_DEBUG_OBJECT (element, "Releasing %s:%s", GST_DEBUG_PAD_NAME (pad));
3218 for (walk = mux->sinkpads; walk; walk = g_slist_next (walk)) {
3219 GstQTPad *qtpad = (GstQTPad *) walk->data;
3220 GST_DEBUG ("Checking %s:%s", GST_DEBUG_PAD_NAME (qtpad->collect.pad));
3221 if (qtpad->collect.pad == pad) {
3222 /* this is it, remove */
3223 mux->sinkpads = g_slist_delete_link (mux->sinkpads, walk);
3224 gst_element_remove_pad (element, pad);
3229 gst_collect_pads_remove_pad (mux->collect, pad);
3233 gst_qt_mux_request_new_pad (GstElement * element,
3234 GstPadTemplate * templ, const gchar * req_name)
3236 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
3237 GstQTMux *qtmux = GST_QT_MUX_CAST (element);
3238 GstQTPad *collect_pad;
3243 if (templ->direction != GST_PAD_SINK)
3244 goto wrong_direction;
3246 if (qtmux->state > GST_QT_MUX_STATE_STARTED)
3249 if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
3251 name = g_strdup_printf ("audio_%02d", qtmux->audio_pads++);
3252 } else if (templ == gst_element_class_get_pad_template (klass, "video_%d")) {
3254 name = g_strdup_printf ("video_%02d", qtmux->video_pads++);
3256 goto wrong_template;
3258 GST_DEBUG_OBJECT (qtmux, "Requested pad: %s", name);
3260 /* create pad and add to collections */
3261 newpad = gst_pad_new_from_template (templ, name);
3263 collect_pad = (GstQTPad *)
3264 gst_collect_pads_add_pad_full (qtmux->collect, newpad, sizeof (GstQTPad),
3265 (GstCollectDataDestroyNotify) (gst_qt_mux_pad_reset));
3267 gst_qt_mux_pad_reset (collect_pad);
3268 collect_pad->trak = atom_trak_new (qtmux->context);
3269 atom_moov_add_trak (qtmux->moov, collect_pad->trak);
3271 qtmux->sinkpads = g_slist_append (qtmux->sinkpads, collect_pad);
3273 /* set up pad functions */
3275 gst_pad_set_setcaps_function (newpad,
3276 GST_DEBUG_FUNCPTR (gst_qt_mux_audio_sink_set_caps));
3278 gst_pad_set_setcaps_function (newpad,
3279 GST_DEBUG_FUNCPTR (gst_qt_mux_video_sink_set_caps));
3281 /* FIXME: hacked way to override/extend the event function of
3282 * GstCollectPads; because it sets its own event function giving the
3283 * element no access to events.
3285 qtmux->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
3286 gst_pad_set_event_function (newpad,
3287 GST_DEBUG_FUNCPTR (gst_qt_mux_sink_event));
3289 gst_pad_set_active (newpad, TRUE);
3290 gst_element_add_pad (element, newpad);
3297 GST_WARNING_OBJECT (qtmux, "Request pad that is not a SINK pad.");
3302 GST_WARNING_OBJECT (qtmux, "Not providing request pad after stream start.");
3307 GST_WARNING_OBJECT (qtmux, "This is not our template!");
3313 gst_qt_mux_get_property (GObject * object,
3314 guint prop_id, GValue * value, GParamSpec * pspec)
3316 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
3318 GST_OBJECT_LOCK (qtmux);
3320 case PROP_MOVIE_TIMESCALE:
3321 g_value_set_uint (value, qtmux->timescale);
3323 case PROP_TRAK_TIMESCALE:
3324 g_value_set_uint (value, qtmux->trak_timescale);
3327 g_value_set_boolean (value, qtmux->guess_pts);
3329 case PROP_DTS_METHOD:
3330 g_value_set_enum (value, qtmux->dts_method);
3332 case PROP_FAST_START:
3333 g_value_set_boolean (value, qtmux->fast_start);
3335 case PROP_FAST_START_TEMP_FILE:
3336 g_value_set_string (value, qtmux->fast_start_file_path);
3338 case PROP_MOOV_RECOV_FILE:
3339 g_value_set_string (value, qtmux->moov_recov_file_path);
3341 case PROP_FRAGMENT_DURATION:
3342 g_value_set_uint (value, qtmux->fragment_duration);
3344 case PROP_STREAMABLE:
3345 g_value_set_boolean (value, qtmux->streamable);
3348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3351 GST_OBJECT_UNLOCK (qtmux);
3355 gst_qt_mux_generate_fast_start_file_path (GstQTMux * qtmux)
3359 g_free (qtmux->fast_start_file_path);
3360 qtmux->fast_start_file_path = NULL;
3362 tmp = g_strdup_printf ("%s%d", "qtmux", g_random_int ());
3363 qtmux->fast_start_file_path = g_build_filename (g_get_tmp_dir (), tmp, NULL);
3368 gst_qt_mux_set_property (GObject * object,
3369 guint prop_id, const GValue * value, GParamSpec * pspec)
3371 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
3373 GST_OBJECT_LOCK (qtmux);
3375 case PROP_MOVIE_TIMESCALE:
3376 qtmux->timescale = g_value_get_uint (value);
3378 case PROP_TRAK_TIMESCALE:
3379 qtmux->trak_timescale = g_value_get_uint (value);
3382 qtmux->guess_pts = g_value_get_boolean (value);
3384 case PROP_DTS_METHOD:
3385 qtmux->dts_method = g_value_get_enum (value);
3387 case PROP_FAST_START:
3388 qtmux->fast_start = g_value_get_boolean (value);
3390 case PROP_FAST_START_TEMP_FILE:
3391 g_free (qtmux->fast_start_file_path);
3392 qtmux->fast_start_file_path = g_value_dup_string (value);
3393 /* NULL means to generate a random one */
3394 if (!qtmux->fast_start_file_path) {
3395 gst_qt_mux_generate_fast_start_file_path (qtmux);
3398 case PROP_MOOV_RECOV_FILE:
3399 g_free (qtmux->moov_recov_file_path);
3400 qtmux->moov_recov_file_path = g_value_dup_string (value);
3402 case PROP_FRAGMENT_DURATION:
3403 qtmux->fragment_duration = g_value_get_uint (value);
3405 case PROP_STREAMABLE:
3406 qtmux->streamable = g_value_get_boolean (value);
3409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3412 GST_OBJECT_UNLOCK (qtmux);
3415 static GstStateChangeReturn
3416 gst_qt_mux_change_state (GstElement * element, GstStateChange transition)
3418 GstStateChangeReturn ret;
3419 GstQTMux *qtmux = GST_QT_MUX_CAST (element);
3421 switch (transition) {
3422 case GST_STATE_CHANGE_NULL_TO_READY:
3424 case GST_STATE_CHANGE_READY_TO_PAUSED:
3425 gst_collect_pads_start (qtmux->collect);
3426 qtmux->state = GST_QT_MUX_STATE_STARTED;
3428 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3430 case GST_STATE_CHANGE_PAUSED_TO_READY:
3431 gst_collect_pads_stop (qtmux->collect);
3437 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3439 switch (transition) {
3440 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3442 case GST_STATE_CHANGE_PAUSED_TO_READY:
3443 gst_qt_mux_reset (qtmux, TRUE);
3445 case GST_STATE_CHANGE_READY_TO_NULL:
3455 gst_qt_mux_register (GstPlugin * plugin)
3457 GTypeInfo typeinfo = {
3458 sizeof (GstQTMuxClass),
3459 (GBaseInitFunc) gst_qt_mux_base_init,
3461 (GClassInitFunc) gst_qt_mux_class_init,
3466 (GInstanceInitFunc) gst_qt_mux_init,
3468 static const GInterfaceInfo tag_setter_info = {
3471 static const GInterfaceInfo tag_xmp_writer_info = {
3475 GstQTMuxFormat format;
3476 GstQTMuxClassParams *params;
3479 GST_DEBUG_CATEGORY_INIT (gst_qt_mux_debug, "qtmux", 0, "QT Muxer");
3481 GST_LOG ("Registering muxers");
3484 GstQTMuxFormatProp *prop;
3486 prop = &gst_qt_mux_format_list[i];
3487 format = prop->format;
3488 if (format == GST_QT_MUX_FORMAT_NONE)
3491 /* create a cache for these properties */
3492 params = g_new0 (GstQTMuxClassParams, 1);
3493 params->prop = prop;
3494 params->src_caps = gst_static_caps_get (&prop->src_caps);
3495 params->video_sink_caps = gst_static_caps_get (&prop->video_sink_caps);
3496 params->audio_sink_caps = gst_static_caps_get (&prop->audio_sink_caps);
3498 /* create the type now */
3499 type = g_type_register_static (GST_TYPE_ELEMENT, prop->type_name, &typeinfo,
3501 g_type_set_qdata (type, GST_QT_MUX_PARAMS_QDATA, (gpointer) params);
3502 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
3503 g_type_add_interface_static (type, GST_TYPE_TAG_XMP_WRITER,
3504 &tag_xmp_writer_info);
3506 if (!gst_element_register (plugin, prop->name, prop->rank, type))
3512 GST_LOG ("Finished registering muxers");
3514 /* FIXME: ideally classification tag should be added and
3515 registered in gstreamer core gsttaglist
3518 GST_LOG ("Registering tags");
3520 gst_tag_register (GST_TAG_3GP_CLASSIFICATION, GST_TAG_FLAG_META,
3521 G_TYPE_STRING, GST_TAG_3GP_CLASSIFICATION, "content classification",
3522 gst_tag_merge_use_first);
3524 GST_LOG ("Finished registering tags");