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/video/video.h>
121 #include <gst/tag/xmpwriter.h>
123 #include <sys/types.h>
125 #include <io.h> /* lseek, open, close, read */
127 #define lseek _lseeki64
129 #define off_t guint64
133 #define ftruncate g_win32_ftruncate
140 #include "gstqtmux.h"
142 GST_DEBUG_CATEGORY_STATIC (gst_qt_mux_debug);
143 #define GST_CAT_DEFAULT gst_qt_mux_debug
153 gst_qt_mux_dts_method_get_type (void)
155 static GType gst_qt_mux_dts_method = 0;
157 if (!gst_qt_mux_dts_method) {
158 static const GEnumValue dts_methods[] = {
159 {DTS_METHOD_DD, "delta/duration", "dd"},
160 {DTS_METHOD_REORDER, "reorder", "reorder"},
161 {DTS_METHOD_ASC, "ascending", "asc"},
165 gst_qt_mux_dts_method =
166 g_enum_register_static ("GstQTMuxDtsMethods", dts_methods);
169 return gst_qt_mux_dts_method;
172 #define GST_TYPE_QT_MUX_DTS_METHOD \
173 (gst_qt_mux_dts_method_get_type ())
175 /* QTMux signals and args */
185 PROP_MOVIE_TIMESCALE,
188 PROP_FAST_START_TEMP_FILE,
189 PROP_MOOV_RECOV_FILE,
190 PROP_FRAGMENT_DURATION,
196 /* some spare for header size as well */
197 #define MDAT_LARGE_FILE_LIMIT ((guint64) 1024 * 1024 * 1024 * 2)
198 #define MAX_TOLERATED_LATENESS (GST_SECOND / 10)
200 #define DEFAULT_MOVIE_TIMESCALE 1000
201 #define DEFAULT_TRAK_TIMESCALE 0
202 #define DEFAULT_DO_CTTS TRUE
203 #define DEFAULT_FAST_START FALSE
204 #define DEFAULT_FAST_START_TEMP_FILE NULL
205 #define DEFAULT_MOOV_RECOV_FILE NULL
206 #define DEFAULT_FRAGMENT_DURATION 0
207 #define DEFAULT_STREAMABLE FALSE
208 #define DEFAULT_DTS_METHOD DTS_METHOD_REORDER
211 static void gst_qt_mux_finalize (GObject * object);
213 static GstStateChangeReturn gst_qt_mux_change_state (GstElement * element,
214 GstStateChange transition);
216 /* property functions */
217 static void gst_qt_mux_set_property (GObject * object,
218 guint prop_id, const GValue * value, GParamSpec * pspec);
219 static void gst_qt_mux_get_property (GObject * object,
220 guint prop_id, GValue * value, GParamSpec * pspec);
223 static GstPad *gst_qt_mux_request_new_pad (GstElement * element,
224 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
225 static void gst_qt_mux_release_pad (GstElement * element, GstPad * pad);
228 static gboolean gst_qt_mux_sink_event (GstPad * pad, GstEvent * event);
230 static GstFlowReturn gst_qt_mux_collected (GstCollectPads * pads,
232 static GstFlowReturn gst_qt_mux_add_buffer (GstQTMux * qtmux, GstQTPad * pad,
235 static GstElementClass *parent_class = NULL;
238 gst_qt_mux_base_init (gpointer g_class)
240 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
241 GstQTMuxClass *klass = (GstQTMuxClass *) g_class;
242 GstQTMuxClassParams *params;
243 GstPadTemplate *videosinktempl, *audiosinktempl, *srctempl;
244 gchar *longname, *description;
247 (GstQTMuxClassParams *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (g_class),
248 GST_QT_MUX_PARAMS_QDATA);
249 g_assert (params != NULL);
251 /* construct the element details struct */
252 longname = g_strdup_printf ("%s Muxer", params->prop->long_name);
253 description = g_strdup_printf ("Multiplex audio and video into a %s file%s",
254 params->prop->long_name,
255 (params->prop->rank == GST_RANK_NONE) ? " (deprecated)" : "");
256 gst_element_class_set_details_simple (element_class, longname,
257 "Codec/Muxer", description,
258 "Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>");
260 g_free (description);
263 srctempl = gst_pad_template_new ("src", GST_PAD_SRC,
264 GST_PAD_ALWAYS, params->src_caps);
265 gst_element_class_add_pad_template (element_class, srctempl);
267 if (params->audio_sink_caps) {
268 audiosinktempl = gst_pad_template_new ("audio_%d",
269 GST_PAD_SINK, GST_PAD_REQUEST, params->audio_sink_caps);
270 gst_element_class_add_pad_template (element_class, audiosinktempl);
273 if (params->video_sink_caps) {
274 videosinktempl = gst_pad_template_new ("video_%d",
275 GST_PAD_SINK, GST_PAD_REQUEST, params->video_sink_caps);
276 gst_element_class_add_pad_template (element_class, videosinktempl);
279 klass->format = params->prop->format;
283 gst_qt_mux_class_init (GstQTMuxClass * klass)
285 GObjectClass *gobject_class;
286 GstElementClass *gstelement_class;
288 gobject_class = (GObjectClass *) klass;
289 gstelement_class = (GstElementClass *) klass;
291 parent_class = g_type_class_peek_parent (klass);
293 gobject_class->finalize = gst_qt_mux_finalize;
294 gobject_class->get_property = gst_qt_mux_get_property;
295 gobject_class->set_property = gst_qt_mux_set_property;
297 g_object_class_install_property (gobject_class, PROP_MOVIE_TIMESCALE,
298 g_param_spec_uint ("movie-timescale", "Movie timescale",
299 "Timescale to use in the movie (units per second)",
300 1, G_MAXUINT32, DEFAULT_MOVIE_TIMESCALE,
301 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_TRAK_TIMESCALE,
303 g_param_spec_uint ("trak-timescale", "Track timescale",
304 "Timescale to use for the tracks (units per second, 0 is automatic)",
305 0, G_MAXUINT32, DEFAULT_TRAK_TIMESCALE,
306 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_DO_CTTS,
308 g_param_spec_boolean ("presentation-time",
309 "Include presentation-time info",
310 "Calculate and include presentation/composition time "
311 "(in addition to decoding time)", DEFAULT_DO_CTTS,
312 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_DTS_METHOD,
314 g_param_spec_enum ("dts-method", "dts-method",
315 "Method to determine DTS time",
316 GST_TYPE_QT_MUX_DTS_METHOD, DEFAULT_DTS_METHOD,
317 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_FAST_START,
319 g_param_spec_boolean ("faststart", "Format file to faststart",
320 "If the file should be formatted for faststart (headers first)",
321 DEFAULT_FAST_START, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_FAST_START_TEMP_FILE,
323 g_param_spec_string ("faststart-file", "File to use for storing buffers",
324 "File that will be used temporarily to store data from the stream "
325 "when creating a faststart file. If null a filepath will be "
326 "created automatically", DEFAULT_FAST_START_TEMP_FILE,
327 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_MOOV_RECOV_FILE,
329 g_param_spec_string ("moov-recovery-file",
330 "File to store data for posterior moov atom recovery",
331 "File to be used to store "
332 "data for moov atom making movie file recovery possible in case "
333 "of a crash during muxing. Null for disabled. (Experimental)",
334 DEFAULT_MOOV_RECOV_FILE,
335 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
336 g_object_class_install_property (gobject_class, PROP_FRAGMENT_DURATION,
337 g_param_spec_uint ("fragment-duration", "Fragment duration",
338 "Fragment durations in ms (produce a fragmented file if > 0)",
339 0, G_MAXUINT32, klass->format == GST_QT_MUX_FORMAT_ISML ?
340 2000 : DEFAULT_FRAGMENT_DURATION,
341 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
343 g_param_spec_boolean ("streamable", "Streamable",
344 "If set to true, the output should be as if it is to be streamed "
345 "and hence no indexes written or duration written.",
347 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
349 gstelement_class->request_new_pad =
350 GST_DEBUG_FUNCPTR (gst_qt_mux_request_new_pad);
351 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_qt_mux_change_state);
352 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_qt_mux_release_pad);
356 gst_qt_mux_pad_reset (GstQTPad * qtpad)
361 qtpad->is_out_of_order = FALSE;
362 qtpad->have_dts = FALSE;
363 qtpad->sample_size = 0;
366 qtpad->first_ts = GST_CLOCK_TIME_NONE;
367 qtpad->prepare_buf_func = NULL;
368 qtpad->avg_bitrate = 0;
369 qtpad->max_bitrate = 0;
370 qtpad->ts_n_entries = 0;
374 for (i = 0; i < G_N_ELEMENTS (qtpad->buf_entries); i++) {
375 if (qtpad->buf_entries[i]) {
376 gst_buffer_unref (qtpad->buf_entries[i]);
377 qtpad->buf_entries[i] = NULL;
382 gst_buffer_replace (&qtpad->last_buf, NULL);
384 /* reference owned elsewhere */
388 atom_traf_free (qtpad->traf);
391 atom_array_clear (&qtpad->fragment_buffers);
393 /* reference owned elsewhere */
398 * Takes GstQTMux back to its initial state
401 gst_qt_mux_reset (GstQTMux * qtmux, gboolean alloc)
405 qtmux->state = GST_QT_MUX_STATE_NONE;
406 qtmux->header_size = 0;
407 qtmux->mdat_size = 0;
409 qtmux->longest_chunk = GST_CLOCK_TIME_NONE;
410 qtmux->video_pads = 0;
411 qtmux->audio_pads = 0;
412 qtmux->fragment_sequence = 0;
415 atom_ftyp_free (qtmux->ftyp);
419 atom_moov_free (qtmux->moov);
423 atom_mfra_free (qtmux->mfra);
426 if (qtmux->fast_start_file) {
427 fclose (qtmux->fast_start_file);
428 g_remove (qtmux->fast_start_file_path);
429 qtmux->fast_start_file = NULL;
431 if (qtmux->moov_recov_file) {
432 fclose (qtmux->moov_recov_file);
433 qtmux->moov_recov_file = NULL;
435 for (walk = qtmux->extra_atoms; walk; walk = g_slist_next (walk)) {
436 AtomInfo *ainfo = (AtomInfo *) walk->data;
437 ainfo->free_func (ainfo->atom);
440 g_slist_free (qtmux->extra_atoms);
441 qtmux->extra_atoms = NULL;
443 GST_OBJECT_LOCK (qtmux);
444 gst_tag_setter_reset_tags (GST_TAG_SETTER (qtmux));
445 GST_OBJECT_UNLOCK (qtmux);
448 for (walk = qtmux->sinkpads; walk; walk = g_slist_next (walk)) {
449 GstQTPad *qtpad = (GstQTPad *) walk->data;
450 gst_qt_mux_pad_reset (qtpad);
452 /* hm, moov_free above yanked the traks away from us,
453 * so do not free, but do clear */
458 qtmux->moov = atom_moov_new (qtmux->context);
459 /* ensure all is as nice and fresh as request_new_pad would provide it */
460 for (walk = qtmux->sinkpads; walk; walk = g_slist_next (walk)) {
461 GstQTPad *qtpad = (GstQTPad *) walk->data;
463 qtpad->trak = atom_trak_new (qtmux->context);
464 atom_moov_add_trak (qtmux->moov, qtpad->trak);
470 gst_qt_mux_init (GstQTMux * qtmux, GstQTMuxClass * qtmux_klass)
472 GstElementClass *klass = GST_ELEMENT_CLASS (qtmux_klass);
473 GstPadTemplate *templ;
475 templ = gst_element_class_get_pad_template (klass, "src");
476 qtmux->srcpad = gst_pad_new_from_template (templ, "src");
477 gst_pad_use_fixed_caps (qtmux->srcpad);
478 gst_element_add_pad (GST_ELEMENT (qtmux), qtmux->srcpad);
480 qtmux->sinkpads = NULL;
481 qtmux->collect = gst_collect_pads_new ();
482 gst_collect_pads_set_function (qtmux->collect,
483 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_qt_mux_collected), qtmux);
485 /* properties set to default upon construction */
487 /* always need this */
489 atoms_context_new (gst_qt_mux_map_format_to_flavor (qtmux_klass->format));
491 /* internals to initial state */
492 gst_qt_mux_reset (qtmux, TRUE);
497 gst_qt_mux_finalize (GObject * object)
499 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
501 gst_qt_mux_reset (qtmux, FALSE);
503 g_free (qtmux->fast_start_file_path);
504 g_free (qtmux->moov_recov_file_path);
506 atoms_context_free (qtmux->context);
507 gst_object_unref (qtmux->collect);
509 g_slist_free (qtmux->sinkpads);
511 G_OBJECT_CLASS (parent_class)->finalize (object);
515 gst_qt_mux_prepare_jpc_buffer (GstQTPad * qtpad, GstBuffer * buf,
522 GST_LOG_OBJECT (qtmux, "Preparing jpc buffer");
527 size = gst_buffer_get_size (buf);
528 newbuf = gst_buffer_new_and_alloc (size + 8);
529 gst_buffer_copy_into (newbuf, buf, GST_BUFFER_COPY_ALL, 8, size);
531 data = gst_buffer_map (newbuf, &size, NULL, GST_MAP_WRITE);
532 GST_WRITE_UINT32_BE (data, size);
533 GST_WRITE_UINT32_LE (data + 4, FOURCC_jp2c);
535 gst_buffer_unmap (buf, data, size);
536 gst_buffer_unref (buf);
542 gst_qt_mux_add_mp4_tag (GstQTMux * qtmux, const GstTagList * list,
543 const char *tag, const char *tag2, guint32 fourcc)
545 switch (gst_tag_get_type (tag)) {
551 if (!gst_tag_list_get_string (list, tag, &str) || !str)
553 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
554 GST_FOURCC_ARGS (fourcc), str);
555 atom_moov_add_str_tag (qtmux->moov, fourcc, str);
564 if (!gst_tag_list_get_double (list, tag, &value))
566 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u",
567 GST_FOURCC_ARGS (fourcc), (gint) value);
568 atom_moov_add_uint_tag (qtmux->moov, fourcc, 21, (gint) value);
575 /* paired unsigned integers */
578 if (!(gst_tag_list_get_uint (list, tag, &value) ||
579 gst_tag_list_get_uint (list, tag2, &count)))
581 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u/%u",
582 GST_FOURCC_ARGS (fourcc), value, count);
583 atom_moov_add_uint_tag (qtmux->moov, fourcc, 0,
584 value << 16 | (count & 0xFFFF));
586 /* unpaired unsigned integers */
587 if (!gst_tag_list_get_uint (list, tag, &value))
589 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u",
590 GST_FOURCC_ARGS (fourcc), value);
591 atom_moov_add_uint_tag (qtmux->moov, fourcc, 1, value);
596 g_assert_not_reached ();
602 gst_qt_mux_add_mp4_date (GstQTMux * qtmux, const GstTagList * list,
603 const char *tag, const char *tag2, guint32 fourcc)
611 g_return_if_fail (gst_tag_get_type (tag) == GST_TYPE_DATE);
613 if (!gst_tag_list_get_date (list, tag, &date) || !date)
616 year = g_date_get_year (date);
617 month = g_date_get_month (date);
618 day = g_date_get_day (date);
622 if (year == G_DATE_BAD_YEAR && month == G_DATE_BAD_MONTH &&
623 day == G_DATE_BAD_DAY) {
624 GST_WARNING_OBJECT (qtmux, "invalid date in tag");
628 str = g_strdup_printf ("%u-%u-%u", year, month, day);
629 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
630 GST_FOURCC_ARGS (fourcc), str);
631 atom_moov_add_str_tag (qtmux->moov, fourcc, str);
636 gst_qt_mux_add_mp4_cover (GstQTMux * qtmux, const GstTagList * list,
637 const char *tag, const char *tag2, guint32 fourcc)
639 GValue value = { 0, };
642 GstStructure *structure;
647 g_return_if_fail (gst_tag_get_type (tag) == GST_TYPE_BUFFER);
649 if (!gst_tag_list_copy_value (&value, list, tag))
652 buf = gst_value_get_buffer (&value);
656 /* FIXME-0.11 caps metadata ? */
657 /* caps = gst_buffer_get_caps (buf); */
660 GST_WARNING_OBJECT (qtmux, "preview image without caps");
664 GST_DEBUG_OBJECT (qtmux, "preview image caps %" GST_PTR_FORMAT, caps);
666 structure = gst_caps_get_structure (caps, 0);
667 if (gst_structure_has_name (structure, "image/jpeg"))
669 else if (gst_structure_has_name (structure, "image/png"))
671 gst_caps_unref (caps);
674 GST_WARNING_OBJECT (qtmux, "preview image format not supported");
678 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
679 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT
680 " -> image size %d", GST_FOURCC_ARGS (fourcc), size);
681 atom_moov_add_tag (qtmux->moov, fourcc, flags, data, size);
682 gst_buffer_unmap (buf, data, size);
684 g_value_unset (&value);
688 gst_qt_mux_add_3gp_str (GstQTMux * qtmux, const GstTagList * list,
689 const char *tag, const char *tag2, guint32 fourcc)
694 g_return_if_fail (gst_tag_get_type (tag) == G_TYPE_STRING);
695 g_return_if_fail (!tag2 || gst_tag_get_type (tag2) == G_TYPE_UINT);
697 if (!gst_tag_list_get_string (list, tag, &str) || !str)
701 if (!gst_tag_list_get_uint (list, tag2, &number))
705 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
706 GST_FOURCC_ARGS (fourcc), str);
707 atom_moov_add_3gp_str_tag (qtmux->moov, fourcc, str);
709 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s/%d",
710 GST_FOURCC_ARGS (fourcc), str, number);
711 atom_moov_add_3gp_str_int_tag (qtmux->moov, fourcc, str, number);
718 gst_qt_mux_add_3gp_date (GstQTMux * qtmux, const GstTagList * list,
719 const char *tag, const char *tag2, guint32 fourcc)
724 g_return_if_fail (gst_tag_get_type (tag) == GST_TYPE_DATE);
726 if (!gst_tag_list_get_date (list, tag, &date) || !date)
729 year = g_date_get_year (date);
731 if (year == G_DATE_BAD_YEAR) {
732 GST_WARNING_OBJECT (qtmux, "invalid date in tag");
736 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %d",
737 GST_FOURCC_ARGS (fourcc), year);
738 atom_moov_add_3gp_uint_tag (qtmux->moov, fourcc, year);
742 gst_qt_mux_add_3gp_location (GstQTMux * qtmux, const GstTagList * list,
743 const char *tag, const char *tag2, guint32 fourcc)
745 gdouble latitude = -360, longitude = -360, altitude = 0;
746 gchar *location = NULL;
747 guint8 *data, *ddata;
748 gint size = 0, len = 0;
749 gboolean ret = FALSE;
751 g_return_if_fail (strcmp (tag, GST_TAG_GEO_LOCATION_NAME) == 0);
753 ret = gst_tag_list_get_string (list, tag, &location);
754 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_LONGITUDE,
756 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_LATITUDE,
758 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_ELEVATION,
765 len = strlen (location);
768 /* role + (long, lat, alt) + body + notes */
769 size += 1 + 3 * 4 + 1 + 1;
771 data = ddata = g_malloc (size);
774 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
777 memcpy (data + 2, location, len);
778 GST_WRITE_UINT8 (data + 2 + len, 0);
781 GST_WRITE_UINT8 (data, 0);
783 #define QT_WRITE_SFP32(data, fp) GST_WRITE_UINT32_BE(data, (guint32) ((gint) (fp * 65536.0)))
784 QT_WRITE_SFP32 (data + 1, longitude);
785 QT_WRITE_SFP32 (data + 5, latitude);
786 QT_WRITE_SFP32 (data + 9, altitude);
787 /* neither astronomical body nor notes */
788 GST_WRITE_UINT16_BE (data + 13, 0);
790 GST_DEBUG_OBJECT (qtmux, "Adding tag 'loci'");
791 atom_moov_add_3gp_tag (qtmux->moov, fourcc, ddata, size);
796 gst_qt_mux_add_3gp_keywords (GstQTMux * qtmux, const GstTagList * list,
797 const char *tag, const char *tag2, guint32 fourcc)
799 gchar *keywords = NULL;
800 guint8 *data, *ddata;
804 g_return_if_fail (strcmp (tag, GST_TAG_KEYWORDS) == 0);
806 if (!gst_tag_list_get_string (list, tag, &keywords) || !keywords)
809 kwds = g_strsplit (keywords, ",", 0);
813 for (i = 0; kwds[i]; i++) {
814 /* size byte + null-terminator */
815 size += strlen (kwds[i]) + 1 + 1;
818 /* language tag + count + keywords */
821 data = ddata = g_malloc (size);
824 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
826 GST_WRITE_UINT8 (data + 2, i);
829 for (i = 0; kwds[i]; ++i) {
830 gint len = strlen (kwds[i]);
832 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
833 GST_FOURCC_ARGS (fourcc), kwds[i]);
835 GST_WRITE_UINT8 (data, len + 1);
836 memcpy (data + 1, kwds[i], len + 1);
842 atom_moov_add_3gp_tag (qtmux->moov, fourcc, ddata, size);
847 gst_qt_mux_parse_classification_string (GstQTMux * qtmux, const gchar * input,
848 guint32 * p_fourcc, guint16 * p_table, gchar ** p_content)
856 size = strlen (input);
858 if (size < 4 + 3 + 1 + 1 + 1) {
859 /* at least the minimum xxxx://y/z */
860 GST_WARNING_OBJECT (qtmux, "Classification tag input (%s) too short, "
865 /* read the fourcc */
866 memcpy (&fourcc, data, 4);
870 if (strncmp (data, "://", 3) != 0) {
876 /* read the table number */
877 if (sscanf (data, "%d", &table) != 1) {
881 GST_WARNING_OBJECT (qtmux, "Invalid table number in classification tag (%d)"
882 ", table numbers should be positive, ignoring tag", table);
886 /* find the next / */
887 while (size > 0 && data[0] != '/') {
894 g_assert (data[0] == '/');
903 /* read up the rest of the string */
904 *p_content = g_strdup (data);
905 *p_table = (guint16) table;
911 GST_WARNING_OBJECT (qtmux, "Ignoring classification tag as "
912 "input (%s) didn't match the expected entitycode://table/content",
919 gst_qt_mux_add_3gp_classification (GstQTMux * qtmux, const GstTagList * list,
920 const char *tag, const char *tag2, guint32 fourcc)
922 gchar *clsf_data = NULL;
926 gchar *content = NULL;
929 g_return_if_fail (strcmp (tag, GST_TAG_3GP_CLASSIFICATION) == 0);
931 if (!gst_tag_list_get_string (list, tag, &clsf_data) || !clsf_data)
934 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
935 GST_FOURCC_ARGS (fourcc), clsf_data);
937 /* parse the string, format is:
938 * entityfourcc://table/content
940 gst_qt_mux_parse_classification_string (qtmux, clsf_data, &entity, &table,
944 size = strlen (content) + 1;
946 /* now we have everything, build the atom
947 * atom description is at 3GPP TS 26.244 V8.2.0 (2009-09) */
948 data = g_malloc (4 + 2 + 2 + size);
949 GST_WRITE_UINT32_LE (data, entity);
950 GST_WRITE_UINT16_BE (data + 4, (guint16) table);
951 GST_WRITE_UINT16_BE (data + 6, 0);
952 memcpy (data + 8, content, size);
955 atom_moov_add_3gp_tag (qtmux->moov, fourcc, data, 4 + 2 + 2 + size);
959 typedef void (*GstQTMuxAddTagFunc) (GstQTMux * mux, const GstTagList * list,
960 const char *tag, const char *tag2, guint32 fourcc);
963 * Struct to record mappings from gstreamer tags to fourcc codes
965 typedef struct _GstTagToFourcc
969 const gchar *gsttag2;
970 const GstQTMuxAddTagFunc func;
973 /* tag list tags to fourcc matching */
974 static const GstTagToFourcc tag_matches_mp4[] = {
975 {FOURCC__alb, GST_TAG_ALBUM, NULL, gst_qt_mux_add_mp4_tag},
976 {FOURCC_soal, GST_TAG_ALBUM_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
977 {FOURCC__ART, GST_TAG_ARTIST, NULL, gst_qt_mux_add_mp4_tag},
978 {FOURCC_soar, GST_TAG_ARTIST_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
979 {FOURCC_aART, GST_TAG_ALBUM_ARTIST, NULL, gst_qt_mux_add_mp4_tag},
980 {FOURCC_soaa, GST_TAG_ALBUM_ARTIST_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
981 {FOURCC__cmt, GST_TAG_COMMENT, NULL, gst_qt_mux_add_mp4_tag},
982 {FOURCC__wrt, GST_TAG_COMPOSER, NULL, gst_qt_mux_add_mp4_tag},
983 {FOURCC_soco, GST_TAG_COMPOSER_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
984 {FOURCC_tvsh, GST_TAG_SHOW_NAME, NULL, gst_qt_mux_add_mp4_tag},
985 {FOURCC_sosn, GST_TAG_SHOW_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
986 {FOURCC_tvsn, GST_TAG_SHOW_SEASON_NUMBER, NULL, gst_qt_mux_add_mp4_tag},
987 {FOURCC_tves, GST_TAG_SHOW_EPISODE_NUMBER, NULL, gst_qt_mux_add_mp4_tag},
988 {FOURCC__gen, GST_TAG_GENRE, NULL, gst_qt_mux_add_mp4_tag},
989 {FOURCC__nam, GST_TAG_TITLE, NULL, gst_qt_mux_add_mp4_tag},
990 {FOURCC_sonm, GST_TAG_TITLE_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
991 {FOURCC_perf, GST_TAG_PERFORMER, NULL, gst_qt_mux_add_mp4_tag},
992 {FOURCC__grp, GST_TAG_GROUPING, NULL, gst_qt_mux_add_mp4_tag},
993 {FOURCC__des, GST_TAG_DESCRIPTION, NULL, gst_qt_mux_add_mp4_tag},
994 {FOURCC__lyr, GST_TAG_LYRICS, NULL, gst_qt_mux_add_mp4_tag},
995 {FOURCC__too, GST_TAG_ENCODER, NULL, gst_qt_mux_add_mp4_tag},
996 {FOURCC_cprt, GST_TAG_COPYRIGHT, NULL, gst_qt_mux_add_mp4_tag},
997 {FOURCC_keyw, GST_TAG_KEYWORDS, NULL, gst_qt_mux_add_mp4_tag},
998 {FOURCC__day, GST_TAG_DATE, NULL, gst_qt_mux_add_mp4_date},
999 {FOURCC_tmpo, GST_TAG_BEATS_PER_MINUTE, NULL, gst_qt_mux_add_mp4_tag},
1000 {FOURCC_trkn, GST_TAG_TRACK_NUMBER, GST_TAG_TRACK_COUNT,
1001 gst_qt_mux_add_mp4_tag},
1002 {FOURCC_disk, GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_ALBUM_VOLUME_COUNT,
1003 gst_qt_mux_add_mp4_tag},
1004 {FOURCC_covr, GST_TAG_PREVIEW_IMAGE, NULL, gst_qt_mux_add_mp4_cover},
1005 {FOURCC_covr, GST_TAG_IMAGE, NULL, gst_qt_mux_add_mp4_cover},
1009 static const GstTagToFourcc tag_matches_3gp[] = {
1010 {FOURCC_titl, GST_TAG_TITLE, NULL, gst_qt_mux_add_3gp_str},
1011 {FOURCC_dscp, GST_TAG_DESCRIPTION, NULL, gst_qt_mux_add_3gp_str},
1012 {FOURCC_cprt, GST_TAG_COPYRIGHT, NULL, gst_qt_mux_add_3gp_str},
1013 {FOURCC_perf, GST_TAG_ARTIST, NULL, gst_qt_mux_add_3gp_str},
1014 {FOURCC_auth, GST_TAG_COMPOSER, NULL, gst_qt_mux_add_3gp_str},
1015 {FOURCC_gnre, GST_TAG_GENRE, NULL, gst_qt_mux_add_3gp_str},
1016 {FOURCC_kywd, GST_TAG_KEYWORDS, NULL, gst_qt_mux_add_3gp_keywords},
1017 {FOURCC_yrrc, GST_TAG_DATE, NULL, gst_qt_mux_add_3gp_date},
1018 {FOURCC_albm, GST_TAG_ALBUM, GST_TAG_TRACK_NUMBER, gst_qt_mux_add_3gp_str},
1019 {FOURCC_loci, GST_TAG_GEO_LOCATION_NAME, NULL, gst_qt_mux_add_3gp_location},
1020 {FOURCC_clsf, GST_TAG_3GP_CLASSIFICATION, NULL,
1021 gst_qt_mux_add_3gp_classification},
1025 /* qtdemux produces these for atoms it cannot parse */
1026 #define GST_QT_DEMUX_PRIVATE_TAG "private-qt-tag"
1029 gst_qt_mux_add_xmp_tags (GstQTMux * qtmux, const GstTagList * list)
1031 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
1032 GstBuffer *xmp = NULL;
1034 /* adobe specs only have 'quicktime' and 'mp4',
1035 * but I guess we can extrapolate to gpp.
1036 * Keep mj2 out for now as we don't add any tags for it yet.
1037 * If you have further info about xmp on these formats, please share */
1038 if (qtmux_klass->format == GST_QT_MUX_FORMAT_MJ2)
1041 GST_DEBUG_OBJECT (qtmux, "Adding xmp tags");
1043 if (qtmux_klass->format == GST_QT_MUX_FORMAT_QT) {
1044 xmp = gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER (qtmux),
1047 atom_moov_add_xmp_tags (qtmux->moov, xmp);
1050 /* for isom/mp4, it is a top level uuid atom */
1051 xmp = gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER (qtmux),
1054 ainfo = build_uuid_xmp_atom (xmp);
1056 qtmux->extra_atoms = g_slist_prepend (qtmux->extra_atoms, ainfo);
1061 gst_buffer_unref (xmp);
1065 gst_qt_mux_add_metadata_tags (GstQTMux * qtmux, const GstTagList * list)
1067 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
1070 const gchar *tag, *tag2;
1071 const GstTagToFourcc *tag_matches;
1073 switch (qtmux_klass->format) {
1074 case GST_QT_MUX_FORMAT_3GP:
1075 tag_matches = tag_matches_3gp;
1077 case GST_QT_MUX_FORMAT_MJ2:
1081 /* sort of iTunes style for mp4 and QT (?) */
1082 tag_matches = tag_matches_mp4;
1089 for (i = 0; tag_matches[i].fourcc; i++) {
1090 fourcc = tag_matches[i].fourcc;
1091 tag = tag_matches[i].gsttag;
1092 tag2 = tag_matches[i].gsttag2;
1094 g_assert (tag_matches[i].func);
1095 tag_matches[i].func (qtmux, list, tag, tag2, fourcc);
1098 /* add unparsed blobs if present */
1099 if (gst_tag_exists (GST_QT_DEMUX_PRIVATE_TAG)) {
1102 num_tags = gst_tag_list_get_tag_size (list, GST_QT_DEMUX_PRIVATE_TAG);
1103 for (i = 0; i < num_tags; ++i) {
1106 GstCaps *caps = NULL;
1108 val = gst_tag_list_get_value_index (list, GST_QT_DEMUX_PRIVATE_TAG, i);
1109 buf = (GstBuffer *) gst_value_get_buffer (val);
1112 if (buf && (caps = NULL /*gst_buffer_get_caps (buf) */ )) {
1114 const gchar *style = NULL;
1118 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1119 GST_DEBUG_OBJECT (qtmux, "Found private tag %d/%d; size %d, caps %"
1120 GST_PTR_FORMAT, i, num_tags, size, caps);
1121 s = gst_caps_get_structure (caps, 0);
1122 if (s && (style = gst_structure_get_string (s, "style"))) {
1123 /* try to prevent some style tag ending up into another variant
1124 * (todo: make into a list if more cases) */
1125 if ((strcmp (style, "itunes") == 0 &&
1126 qtmux_klass->format == GST_QT_MUX_FORMAT_MP4) ||
1127 (strcmp (style, "iso") == 0 &&
1128 qtmux_klass->format == GST_QT_MUX_FORMAT_3GP)) {
1129 GST_DEBUG_OBJECT (qtmux, "Adding private tag");
1130 atom_moov_add_blob_tag (qtmux->moov, data, size);
1133 gst_buffer_unmap (buf, data, size);
1134 gst_caps_unref (caps);
1143 * Gets the tagsetter iface taglist and puts the known tags
1144 * into the output stream
1147 gst_qt_mux_setup_metadata (GstQTMux * qtmux)
1149 const GstTagList *tags;
1151 GST_OBJECT_LOCK (qtmux);
1152 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (qtmux));
1153 GST_OBJECT_UNLOCK (qtmux);
1155 GST_LOG_OBJECT (qtmux, "tags: %" GST_PTR_FORMAT, tags);
1157 if (tags && !gst_tag_list_is_empty (tags)) {
1158 GstTagList *copy = gst_tag_list_copy (tags);
1160 GST_DEBUG_OBJECT (qtmux, "Removing bogus tags");
1161 gst_tag_list_remove_tag (copy, GST_TAG_VIDEO_CODEC);
1162 gst_tag_list_remove_tag (copy, GST_TAG_AUDIO_CODEC);
1163 gst_tag_list_remove_tag (copy, GST_TAG_CONTAINER_FORMAT);
1165 GST_DEBUG_OBJECT (qtmux, "Formatting tags");
1166 gst_qt_mux_add_metadata_tags (qtmux, copy);
1167 gst_qt_mux_add_xmp_tags (qtmux, copy);
1168 gst_tag_list_free (copy);
1170 GST_DEBUG_OBJECT (qtmux, "No tags received");
1174 static inline GstBuffer *
1175 _gst_buffer_new_take_data (guint8 * data, guint size)
1179 buf = gst_buffer_new ();
1180 gst_buffer_take_memory (buf, -1,
1181 gst_memory_new_wrapped (0, data, g_free, size, 0, size));
1186 static GstFlowReturn
1187 gst_qt_mux_send_buffer (GstQTMux * qtmux, GstBuffer * buf, guint64 * offset,
1193 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
1195 size = gst_buffer_get_size (buf);
1196 GST_LOG_OBJECT (qtmux, "sending buffer size %d", size);
1198 if (mind_fast && qtmux->fast_start_file) {
1202 GST_LOG_OBJECT (qtmux, "to temporary file");
1203 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1204 ret = fwrite (data, sizeof (guint8), size, qtmux->fast_start_file);
1205 gst_buffer_unmap (buf, data, size);
1206 gst_buffer_unref (buf);
1212 GST_LOG_OBJECT (qtmux, "downstream");
1213 res = gst_pad_push (qtmux->srcpad, buf);
1216 if (G_LIKELY (offset))
1224 GST_ELEMENT_ERROR (qtmux, RESOURCE, WRITE,
1225 ("Failed to write to temporary file"), GST_ERROR_SYSTEM);
1226 return GST_FLOW_ERROR;
1231 gst_qt_mux_seek_to_beginning (FILE * f)
1234 if (fseeko (f, (off_t) 0, SEEK_SET) != 0)
1236 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1237 if (lseek (fileno (f), (off_t) 0, SEEK_SET) == (off_t) - 1)
1240 if (fseek (f, (long) 0, SEEK_SET) != 0)
1246 static GstFlowReturn
1247 gst_qt_mux_send_buffered_data (GstQTMux * qtmux, guint64 * offset)
1249 GstFlowReturn ret = GST_FLOW_OK;
1250 GstBuffer *buf = NULL;
1252 if (fflush (qtmux->fast_start_file))
1255 if (!gst_qt_mux_seek_to_beginning (qtmux->fast_start_file))
1258 /* hm, this could all take a really really long time,
1259 * but there may not be another way to get moov atom first
1260 * (somehow optimize copy?) */
1261 GST_DEBUG_OBJECT (qtmux, "Sending buffered data");
1262 while (ret == GST_FLOW_OK) {
1263 const int bufsize = 4096;
1267 buf = gst_buffer_new_and_alloc (bufsize);
1268 data = gst_buffer_map (buf, &size, NULL, GST_MAP_WRITE);
1269 size = fread (data, sizeof (guint8), bufsize, qtmux->fast_start_file);
1271 gst_buffer_unmap (buf, data, -1);
1274 gst_buffer_unmap (buf, data, size);
1275 GST_LOG_OBJECT (qtmux, "Pushing buffered buffer of size %d", (gint) size);
1276 ret = gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1280 gst_buffer_unref (buf);
1282 if (ftruncate (fileno (qtmux->fast_start_file), 0))
1284 if (!gst_qt_mux_seek_to_beginning (qtmux->fast_start_file))
1292 GST_ELEMENT_ERROR (qtmux, RESOURCE, WRITE,
1293 ("Failed to flush temporary file"), GST_ERROR_SYSTEM);
1294 ret = GST_FLOW_ERROR;
1299 GST_ELEMENT_ERROR (qtmux, RESOURCE, SEEK,
1300 ("Failed to seek temporary file"), GST_ERROR_SYSTEM);
1301 ret = GST_FLOW_ERROR;
1306 /* clear descriptor so we don't remove temp file later on,
1307 * might be possible to recover */
1308 fclose (qtmux->fast_start_file);
1309 qtmux->fast_start_file = NULL;
1315 * Sends the initial mdat atom fields (size fields and fourcc type),
1316 * the subsequent buffers are considered part of it's data.
1317 * As we can't predict the amount of data that we are going to place in mdat
1318 * we need to record the position of the size field in the stream so we can
1319 * seek back to it later and update when the streams have finished.
1321 static GstFlowReturn
1322 gst_qt_mux_send_mdat_header (GstQTMux * qtmux, guint64 * off, guint64 size,
1327 guint8 *data = NULL;
1330 GST_DEBUG_OBJECT (qtmux, "Sending mdat's atom header, "
1331 "size %" G_GUINT64_FORMAT, size);
1333 node_header = g_malloc0 (sizeof (Atom));
1334 node_header->type = FOURCC_mdat;
1336 /* use extended size */
1337 node_header->size = 1;
1338 node_header->extended_size = 0;
1340 node_header->extended_size = size + 16;
1342 node_header->size = size + 8;
1346 if (atom_copy_data (node_header, &data, &size, &offset) == 0)
1347 goto serialize_error;
1349 buf = _gst_buffer_new_take_data (data, offset);
1350 g_free (node_header);
1352 GST_LOG_OBJECT (qtmux, "Pushing mdat start");
1353 return gst_qt_mux_send_buffer (qtmux, buf, off, FALSE);
1358 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
1359 ("Failed to serialize mdat"));
1360 return GST_FLOW_ERROR;
1365 * We get the position of the mdat size field, seek back to it
1366 * and overwrite with the real value
1368 static GstFlowReturn
1369 gst_qt_mux_update_mdat_size (GstQTMux * qtmux, guint64 mdat_pos,
1370 guint64 mdat_size, guint64 * offset)
1373 gboolean large_file;
1378 large_file = (mdat_size > MDAT_LARGE_FILE_LIMIT);
1383 /* seek and rewrite the header */
1384 gst_segment_init (&segment, GST_FORMAT_BYTES);
1385 segment.start = mdat_pos;
1386 gst_pad_push_event (qtmux->srcpad, gst_event_new_segment (&segment));
1389 buf = gst_buffer_new_and_alloc (sizeof (guint64));
1390 data = gst_buffer_map (buf, &size, NULL, GST_MAP_WRITE);
1391 GST_WRITE_UINT64_BE (data, mdat_size + 16);
1393 buf = gst_buffer_new_and_alloc (16);
1394 data = gst_buffer_map (buf, &size, NULL, GST_MAP_WRITE);
1395 GST_WRITE_UINT32_BE (data, 8);
1396 GST_WRITE_UINT32_LE (data + 4, FOURCC_free);
1397 GST_WRITE_UINT32_BE (data + 8, mdat_size + 8);
1398 GST_WRITE_UINT32_LE (data + 12, FOURCC_mdat);
1400 gst_buffer_unmap (buf, data, size);
1402 return gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1405 static GstFlowReturn
1406 gst_qt_mux_send_ftyp (GstQTMux * qtmux, guint64 * off)
1409 guint64 size = 0, offset = 0;
1410 guint8 *data = NULL;
1412 GST_DEBUG_OBJECT (qtmux, "Sending ftyp atom");
1414 if (!atom_ftyp_copy_data (qtmux->ftyp, &data, &size, &offset))
1415 goto serialize_error;
1417 buf = _gst_buffer_new_take_data (data, offset);
1419 GST_LOG_OBJECT (qtmux, "Pushing ftyp");
1420 return gst_qt_mux_send_buffer (qtmux, buf, off, FALSE);
1425 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
1426 ("Failed to serialize ftyp"));
1427 return GST_FLOW_ERROR;
1432 gst_qt_mux_prepare_ftyp (GstQTMux * qtmux, AtomFTYP ** p_ftyp,
1433 GstBuffer ** p_prefix)
1435 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
1436 guint32 major, version;
1438 GstBuffer *prefix = NULL;
1439 AtomFTYP *ftyp = NULL;
1441 GST_DEBUG_OBJECT (qtmux, "Preparing ftyp and possible prefix atom");
1443 /* init and send context and ftyp based on current property state */
1444 gst_qt_mux_map_format_to_header (qtmux_klass->format, &prefix, &major,
1445 &version, &comp, qtmux->moov, qtmux->longest_chunk,
1446 qtmux->fast_start_file != NULL);
1447 ftyp = atom_ftyp_new (qtmux->context, major, version, comp);
1454 gst_buffer_unref (prefix);
1459 static GstFlowReturn
1460 gst_qt_mux_prepare_and_send_ftyp (GstQTMux * qtmux)
1462 GstFlowReturn ret = GST_FLOW_OK;
1463 GstBuffer *prefix = NULL;
1465 GST_DEBUG_OBJECT (qtmux, "Preparing to send ftyp atom");
1467 /* init and send context and ftyp based on current property state */
1469 atom_ftyp_free (qtmux->ftyp);
1472 gst_qt_mux_prepare_ftyp (qtmux, &qtmux->ftyp, &prefix);
1474 ret = gst_qt_mux_send_buffer (qtmux, prefix, &qtmux->header_size, FALSE);
1475 if (ret != GST_FLOW_OK)
1478 return gst_qt_mux_send_ftyp (qtmux, &qtmux->header_size);
1482 gst_qt_mux_set_header_on_caps (GstQTMux * mux, GstBuffer * buf)
1484 GstStructure *structure;
1485 GValue array = { 0 };
1486 GValue value = { 0 };
1487 GstCaps *caps, *tcaps;
1489 tcaps = gst_pad_get_current_caps (mux->srcpad);
1490 caps = gst_caps_copy (tcaps);
1491 gst_caps_unref (tcaps);
1493 structure = gst_caps_get_structure (caps, 0);
1495 g_value_init (&array, GST_TYPE_ARRAY);
1497 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
1498 g_value_init (&value, GST_TYPE_BUFFER);
1499 gst_value_take_buffer (&value, gst_buffer_ref (buf));
1500 gst_value_array_append_value (&array, &value);
1501 g_value_unset (&value);
1503 gst_structure_set_value (structure, "streamheader", &array);
1504 g_value_unset (&array);
1505 gst_pad_set_caps (mux->srcpad, caps);
1506 gst_caps_unref (caps);
1510 gst_qt_mux_configure_moov (GstQTMux * qtmux, guint32 * _timescale)
1512 gboolean fragmented;
1515 GST_OBJECT_LOCK (qtmux);
1516 timescale = qtmux->timescale;
1517 fragmented = qtmux->fragment_sequence > 0;
1518 GST_OBJECT_UNLOCK (qtmux);
1520 /* inform lower layers of our property wishes, and determine duration.
1521 * Let moov take care of this using its list of traks;
1522 * so that released pads are also included */
1523 GST_DEBUG_OBJECT (qtmux, "Updating timescale to %" G_GUINT32_FORMAT,
1525 atom_moov_update_timescale (qtmux->moov, timescale);
1526 atom_moov_set_fragmented (qtmux->moov, fragmented);
1528 atom_moov_update_duration (qtmux->moov);
1531 *_timescale = timescale;
1534 static GstFlowReturn
1535 gst_qt_mux_send_moov (GstQTMux * qtmux, guint64 * _offset, gboolean mind_fast)
1537 guint64 offset = 0, size = 0;
1540 GstFlowReturn ret = GST_FLOW_OK;
1542 /* serialize moov */
1545 GST_LOG_OBJECT (qtmux, "Copying movie header into buffer");
1546 if (!atom_moov_copy_data (qtmux->moov, &data, &size, &offset))
1547 goto serialize_error;
1549 buf = _gst_buffer_new_take_data (data, offset);
1550 GST_DEBUG_OBJECT (qtmux, "Pushing moov atoms");
1551 gst_qt_mux_set_header_on_caps (qtmux, buf);
1552 ret = gst_qt_mux_send_buffer (qtmux, buf, _offset, mind_fast);
1559 return GST_FLOW_ERROR;
1563 /* either calculates size of extra atoms or pushes them */
1564 static GstFlowReturn
1565 gst_qt_mux_send_extra_atoms (GstQTMux * qtmux, gboolean send, guint64 * offset,
1569 guint64 loffset = 0, size = 0;
1571 GstFlowReturn ret = GST_FLOW_OK;
1573 for (walk = qtmux->extra_atoms; walk; walk = g_slist_next (walk)) {
1574 AtomInfo *ainfo = (AtomInfo *) walk->data;
1578 if (!ainfo->copy_data_func (ainfo->atom,
1579 send ? &data : NULL, &size, &loffset))
1580 goto serialize_error;
1585 GST_DEBUG_OBJECT (qtmux,
1586 "Pushing extra top-level atom %" GST_FOURCC_FORMAT,
1587 GST_FOURCC_ARGS (ainfo->atom->type));
1588 buf = _gst_buffer_new_take_data (data, loffset);
1589 ret = gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1590 if (ret != GST_FLOW_OK)
1603 return GST_FLOW_ERROR;
1607 static GstFlowReturn
1608 gst_qt_mux_start_file (GstQTMux * qtmux)
1610 GstFlowReturn ret = GST_FLOW_OK;
1614 GST_DEBUG_OBJECT (qtmux, "starting file");
1616 caps = gst_caps_copy (gst_pad_get_pad_template_caps (qtmux->srcpad));
1617 /* qtmux has structure with and without variant, remove all but the first */
1618 while (gst_caps_get_size (caps) > 1)
1619 gst_caps_remove_structure (caps, 1);
1620 gst_pad_set_caps (qtmux->srcpad, caps);
1621 gst_caps_unref (caps);
1623 /* let downstream know we think in BYTES and expect to do seeking later on */
1624 gst_segment_init (&segment, GST_FORMAT_BYTES);
1625 gst_pad_push_event (qtmux->srcpad, gst_event_new_segment (&segment));
1627 /* initialize our moov recovery file */
1628 GST_OBJECT_LOCK (qtmux);
1629 if (qtmux->moov_recov_file_path) {
1630 GST_DEBUG_OBJECT (qtmux, "Openning moov recovery file: %s",
1631 qtmux->moov_recov_file_path);
1632 qtmux->moov_recov_file = g_fopen (qtmux->moov_recov_file_path, "wb+");
1633 if (qtmux->moov_recov_file == NULL) {
1634 GST_WARNING_OBJECT (qtmux, "Failed to open moov recovery file in %s",
1635 qtmux->moov_recov_file_path);
1638 gboolean fail = FALSE;
1639 AtomFTYP *ftyp = NULL;
1640 GstBuffer *prefix = NULL;
1642 gst_qt_mux_prepare_ftyp (qtmux, &ftyp, &prefix);
1644 if (!atoms_recov_write_headers (qtmux->moov_recov_file, ftyp, prefix,
1645 qtmux->moov, qtmux->timescale,
1646 g_slist_length (qtmux->sinkpads))) {
1647 GST_WARNING_OBJECT (qtmux, "Failed to write moov recovery file "
1652 atom_ftyp_free (ftyp);
1654 gst_buffer_unref (prefix);
1656 for (walk = qtmux->sinkpads; walk && !fail; walk = g_slist_next (walk)) {
1657 GstCollectData *cdata = (GstCollectData *) walk->data;
1658 GstQTPad *qpad = (GstQTPad *) cdata;
1659 /* write info for each stream */
1660 fail = atoms_recov_write_trak_info (qtmux->moov_recov_file, qpad->trak);
1662 GST_WARNING_OBJECT (qtmux, "Failed to write trak info to recovery "
1668 fclose (qtmux->moov_recov_file);
1669 qtmux->moov_recov_file = NULL;
1670 GST_WARNING_OBJECT (qtmux, "An error was detected while writing to "
1671 "recover file, moov recovery won't work");
1675 GST_OBJECT_UNLOCK (qtmux);
1678 * send mdat header if already needed, and mark position for later update.
1679 * We don't send ftyp now if we are on fast start mode, because we can
1680 * better fine tune using the information we gather to create the whole moov
1683 if (qtmux->fast_start) {
1684 GST_OBJECT_LOCK (qtmux);
1685 qtmux->fast_start_file = g_fopen (qtmux->fast_start_file_path, "wb+");
1686 if (!qtmux->fast_start_file)
1688 GST_OBJECT_UNLOCK (qtmux);
1690 /* send a dummy buffer for preroll */
1691 ret = gst_qt_mux_send_buffer (qtmux, gst_buffer_new (), NULL, FALSE);
1692 if (ret != GST_FLOW_OK)
1696 ret = gst_qt_mux_prepare_and_send_ftyp (qtmux);
1697 if (ret != GST_FLOW_OK) {
1701 /* well, it's moov pos if fragmented ... */
1702 qtmux->mdat_pos = qtmux->header_size;
1704 if (qtmux->fragment_duration) {
1705 GST_DEBUG_OBJECT (qtmux, "fragment duration %d ms, writing headers",
1706 qtmux->fragment_duration);
1707 /* also used as snapshot marker to indicate fragmented file */
1708 qtmux->fragment_sequence = 1;
1709 /* prepare moov and/or tags */
1710 gst_qt_mux_configure_moov (qtmux, NULL);
1711 gst_qt_mux_setup_metadata (qtmux);
1712 ret = gst_qt_mux_send_moov (qtmux, &qtmux->header_size, FALSE);
1713 if (ret != GST_FLOW_OK)
1717 gst_qt_mux_send_extra_atoms (qtmux, TRUE, &qtmux->header_size, FALSE);
1718 if (ret != GST_FLOW_OK)
1721 if (!qtmux->streamable)
1722 qtmux->mfra = atom_mfra_new (qtmux->context);
1724 /* extended to ensure some spare space */
1725 ret = gst_qt_mux_send_mdat_header (qtmux, &qtmux->header_size, 0, TRUE);
1735 GST_ELEMENT_ERROR (qtmux, RESOURCE, OPEN_READ_WRITE,
1736 (("Could not open temporary file \"%s\""), qtmux->fast_start_file_path),
1738 GST_OBJECT_UNLOCK (qtmux);
1739 return GST_FLOW_ERROR;
1743 static GstFlowReturn
1744 gst_qt_mux_stop_file (GstQTMux * qtmux)
1746 gboolean ret = GST_FLOW_OK;
1747 guint64 offset = 0, size = 0;
1749 gboolean large_file;
1751 GstClockTime first_ts = GST_CLOCK_TIME_NONE;
1753 GST_DEBUG_OBJECT (qtmux, "Updating remaining values and sending last data");
1755 /* pushing last buffers for each pad */
1756 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
1757 GstCollectData *cdata = (GstCollectData *) walk->data;
1758 GstQTPad *qtpad = (GstQTPad *) cdata;
1760 /* send last buffer */
1761 GST_DEBUG_OBJECT (qtmux, "Sending the last buffer for pad %s",
1762 GST_PAD_NAME (qtpad->collect.pad));
1763 ret = gst_qt_mux_add_buffer (qtmux, qtpad, NULL);
1764 if (ret != GST_FLOW_OK)
1765 GST_WARNING_OBJECT (qtmux, "Failed to send last buffer for %s, "
1766 "flow return: %s", GST_PAD_NAME (qtpad->collect.pad),
1767 gst_flow_get_name (ret));
1769 if (!GST_CLOCK_TIME_IS_VALID (qtpad->first_ts)) {
1770 GST_DEBUG_OBJECT (qtmux, "Pad %s has no buffers",
1771 GST_PAD_NAME (qtpad->collect.pad));
1775 /* determine max stream duration */
1776 if (!GST_CLOCK_TIME_IS_VALID (first_ts) ||
1777 (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
1778 qtpad->last_dts > first_ts)) {
1779 first_ts = qtpad->last_dts;
1783 if (qtmux->fragment_sequence) {
1787 guint8 *data = NULL;
1791 GST_DEBUG_OBJECT (qtmux, "adding mfra");
1792 if (!atom_mfra_copy_data (qtmux->mfra, &data, &size, &offset))
1793 goto serialize_error;
1794 buf = _gst_buffer_new_take_data (data, offset);
1795 ret = gst_qt_mux_send_buffer (qtmux, buf, NULL, FALSE);
1796 if (ret != GST_FLOW_OK)
1799 /* must have been streamable; no need to write duration */
1800 GST_DEBUG_OBJECT (qtmux, "streamable file; nothing to stop");
1804 timescale = qtmux->timescale;
1805 /* only mvex duration is updated,
1806 * mvhd should be consistent with empty moov
1807 * (but TODO maybe some clients do not handle that well ?) */
1808 qtmux->moov->mvex.mehd.fragment_duration =
1809 gst_util_uint64_scale (first_ts, timescale, GST_SECOND);
1810 GST_DEBUG_OBJECT (qtmux, "rewriting moov with mvex duration %"
1811 GST_TIME_FORMAT, GST_TIME_ARGS (first_ts));
1812 /* seek and rewrite the header */
1813 gst_segment_init (&segment, GST_FORMAT_BYTES);
1814 segment.start = qtmux->mdat_pos;
1815 gst_pad_push_event (qtmux->srcpad, gst_event_new_segment (&segment));
1816 /* no need to seek back */
1817 return gst_qt_mux_send_moov (qtmux, NULL, FALSE);
1820 gst_qt_mux_configure_moov (qtmux, ×cale);
1822 /* check for late streams */
1823 first_ts = GST_CLOCK_TIME_NONE;
1824 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
1825 GstCollectData *cdata = (GstCollectData *) walk->data;
1826 GstQTPad *qtpad = (GstQTPad *) cdata;
1828 if (!GST_CLOCK_TIME_IS_VALID (first_ts) ||
1829 (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
1830 qtpad->first_ts < first_ts)) {
1831 first_ts = qtpad->first_ts;
1834 GST_DEBUG_OBJECT (qtmux, "Media first ts selected: %" GST_TIME_FORMAT,
1835 GST_TIME_ARGS (first_ts));
1836 /* add EDTSs for late streams */
1837 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
1838 GstCollectData *cdata = (GstCollectData *) walk->data;
1839 GstQTPad *qtpad = (GstQTPad *) cdata;
1843 if (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
1844 qtpad->first_ts > first_ts + MAX_TOLERATED_LATENESS) {
1845 GST_DEBUG_OBJECT (qtmux, "Pad %s is a late stream by %" GST_TIME_FORMAT,
1846 GST_PAD_NAME (qtpad->collect.pad),
1847 GST_TIME_ARGS (qtpad->first_ts - first_ts));
1848 lateness = gst_util_uint64_scale_round (qtpad->first_ts - first_ts,
1849 timescale, GST_SECOND);
1850 duration = qtpad->trak->tkhd.duration;
1851 atom_trak_add_elst_entry (qtpad->trak, lateness, (guint32) - 1,
1852 (guint32) (1 * 65536.0));
1853 atom_trak_add_elst_entry (qtpad->trak, duration, 0,
1854 (guint32) (1 * 65536.0));
1856 /* need to add the empty time to the trak duration */
1857 qtpad->trak->tkhd.duration += lateness;
1861 /* tags into file metadata */
1862 gst_qt_mux_setup_metadata (qtmux);
1864 large_file = (qtmux->mdat_size > MDAT_LARGE_FILE_LIMIT);
1865 /* if faststart, update the offset of the atoms in the movie with the offset
1866 * that the movie headers before mdat will cause.
1867 * Also, send the ftyp */
1868 if (qtmux->fast_start_file) {
1869 GstFlowReturn flow_ret;
1872 flow_ret = gst_qt_mux_prepare_and_send_ftyp (qtmux);
1873 if (flow_ret != GST_FLOW_OK) {
1876 /* copy into NULL to obtain size */
1877 if (!atom_moov_copy_data (qtmux->moov, NULL, &size, &offset))
1878 goto serialize_error;
1879 GST_DEBUG_OBJECT (qtmux, "calculated moov atom size %" G_GUINT64_FORMAT,
1881 offset += qtmux->header_size + (large_file ? 16 : 8);
1883 /* sum up with the extra atoms size */
1884 ret = gst_qt_mux_send_extra_atoms (qtmux, FALSE, &offset, FALSE);
1885 if (ret != GST_FLOW_OK)
1888 offset = qtmux->header_size;
1890 atom_moov_chunks_add_offset (qtmux->moov, offset);
1893 /* note: as of this point, we no longer care about tracking written data size,
1894 * since there is no more use for it anyway */
1895 ret = gst_qt_mux_send_moov (qtmux, NULL, FALSE);
1896 if (ret != GST_FLOW_OK)
1900 ret = gst_qt_mux_send_extra_atoms (qtmux, TRUE, NULL, FALSE);
1901 if (ret != GST_FLOW_OK)
1904 /* if needed, send mdat atom and move buffered data into it */
1905 if (qtmux->fast_start_file) {
1906 /* mdat_size = accumulated (buffered data) */
1907 ret = gst_qt_mux_send_mdat_header (qtmux, NULL, qtmux->mdat_size,
1909 if (ret != GST_FLOW_OK)
1911 ret = gst_qt_mux_send_buffered_data (qtmux, NULL);
1912 if (ret != GST_FLOW_OK)
1915 /* mdat needs update iff not using faststart */
1916 GST_DEBUG_OBJECT (qtmux, "updating mdat size");
1917 ret = gst_qt_mux_update_mdat_size (qtmux, qtmux->mdat_pos,
1918 qtmux->mdat_size, NULL);
1919 /* note; no seeking back to the end of file is done,
1920 * since we no longer write anything anyway */
1928 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
1929 ("Failed to serialize moov"));
1930 return GST_FLOW_ERROR;
1934 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL), ("Failed to send ftyp"));
1935 return GST_FLOW_ERROR;
1939 static GstFlowReturn
1940 gst_qt_mux_pad_fragment_add_buffer (GstQTMux * qtmux, GstQTPad * pad,
1941 GstBuffer * buf, gboolean force, guint32 nsamples, gint64 dts,
1942 guint32 delta, guint32 size, gboolean sync, gint64 pts_offset)
1944 GstFlowReturn ret = GST_FLOW_OK;
1946 /* setup if needed */
1947 if (G_UNLIKELY (!pad->traf || force))
1951 /* flush pad fragment if threshold reached,
1952 * or at new keyframe if we should be minding those in the first place */
1953 if (G_UNLIKELY (force || (sync && pad->sync) ||
1954 pad->fragment_duration < (gint64) delta)) {
1956 guint64 size = 0, offset = 0;
1957 guint8 *data = NULL;
1959 guint i, total_size;
1961 /* now we know where moof ends up, update offset in tfra */
1963 atom_tfra_update_offset (pad->tfra, qtmux->header_size);
1965 moof = atom_moof_new (qtmux->context, qtmux->fragment_sequence);
1966 /* takes ownership */
1967 atom_moof_add_traf (moof, pad->traf);
1969 atom_moof_copy_data (moof, &data, &size, &offset);
1970 buffer = _gst_buffer_new_take_data (data, offset);
1971 GST_LOG_OBJECT (qtmux, "writing moof size %d",
1972 gst_buffer_get_size (buffer));
1973 ret = gst_qt_mux_send_buffer (qtmux, buffer, &qtmux->header_size, FALSE);
1975 /* and actual data */
1977 for (i = 0; i < atom_array_get_len (&pad->fragment_buffers); i++) {
1979 gst_buffer_get_size (atom_array_index (&pad->fragment_buffers, i));
1982 GST_LOG_OBJECT (qtmux, "writing %d buffers, total_size %d",
1983 atom_array_get_len (&pad->fragment_buffers), total_size);
1984 if (ret == GST_FLOW_OK)
1985 ret = gst_qt_mux_send_mdat_header (qtmux, &qtmux->header_size, total_size,
1987 for (i = 0; i < atom_array_get_len (&pad->fragment_buffers); i++) {
1988 if (G_LIKELY (ret == GST_FLOW_OK))
1989 ret = gst_qt_mux_send_buffer (qtmux,
1990 atom_array_index (&pad->fragment_buffers, i), &qtmux->header_size,
1993 gst_buffer_unref (atom_array_index (&pad->fragment_buffers, i));
1996 atom_array_clear (&pad->fragment_buffers);
1997 atom_moof_free (moof);
1998 qtmux->fragment_sequence++;
2003 if (G_UNLIKELY (!pad->traf)) {
2004 GST_LOG_OBJECT (qtmux, "setting up new fragment");
2005 pad->traf = atom_traf_new (qtmux->context, atom_trak_get_id (pad->trak));
2006 atom_array_init (&pad->fragment_buffers, 512);
2007 pad->fragment_duration = gst_util_uint64_scale (qtmux->fragment_duration,
2008 atom_trak_get_timescale (pad->trak), 1000);
2010 if (G_UNLIKELY (qtmux->mfra && !pad->tfra)) {
2011 pad->tfra = atom_tfra_new (qtmux->context, atom_trak_get_id (pad->trak));
2012 atom_mfra_add_tfra (qtmux->mfra, pad->tfra);
2016 /* add buffer and metadata */
2017 atom_traf_add_samples (pad->traf, delta, size, sync, pts_offset,
2019 atom_array_append (&pad->fragment_buffers, buf, 256);
2020 pad->fragment_duration -= delta;
2023 guint32 sn = atom_traf_get_sample_num (pad->traf);
2025 if ((sync && pad->sync) || (sn == 1 && !pad->sync))
2026 atom_tfra_add_entry (pad->tfra, dts, sn);
2029 if (G_UNLIKELY (force))
2035 /* sigh, tiny list helpers to re-order stuff */
2037 gst_qt_mux_push_ts (GstQTMux * qtmux, GstQTPad * pad, GstClockTime ts)
2041 for (i = 0; (i < QTMUX_NO_OF_TS) && (i < pad->ts_n_entries); i++) {
2042 if (ts > pad->ts_entries[i])
2045 memmove (&pad->ts_entries[i + 1], &pad->ts_entries[i],
2046 sizeof (GstClockTime) * (pad->ts_n_entries - i));
2047 pad->ts_entries[i] = ts;
2048 pad->ts_n_entries++;
2051 /* takes ownership of @buf */
2053 gst_qt_mux_get_asc_buffer_ts (GstQTMux * qtmux, GstQTPad * pad, GstBuffer * buf)
2055 const gint wrap = G_N_ELEMENTS (pad->buf_entries);
2058 /* store buffer and ts, latter ordered */
2060 pad->buf_entries[pad->buf_tail++] = buf;
2061 pad->buf_tail %= wrap;
2062 gst_qt_mux_push_ts (qtmux, pad, GST_BUFFER_TIMESTAMP (buf));
2065 if (pad->ts_n_entries && (!buf || pad->ts_n_entries >= QTMUX_NO_OF_TS)) {
2066 ts = pad->ts_entries[--pad->ts_n_entries];
2067 buf = pad->buf_entries[pad->buf_head];
2068 pad->buf_entries[pad->buf_head++] = NULL;
2069 pad->buf_head %= wrap;
2070 buf = gst_buffer_make_writable (buf);
2071 /* track original ts (= pts ?) for later */
2072 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_TIMESTAMP (buf);
2073 GST_BUFFER_TIMESTAMP (buf) = ts;
2074 GST_DEBUG_OBJECT (qtmux, "next buffer uses reordered ts %" GST_TIME_FORMAT,
2075 GST_TIME_ARGS (ts));
2084 * Here we push the buffer and update the tables in the track atoms
2086 static GstFlowReturn
2087 gst_qt_mux_add_buffer (GstQTMux * qtmux, GstQTPad * pad, GstBuffer * buf)
2089 GstBuffer *last_buf = NULL;
2090 GstClockTime duration;
2091 guint nsamples, sample_size;
2092 guint64 chunk_offset;
2093 gint64 last_dts, scaled_duration;
2094 gint64 pts_offset = 0;
2095 gboolean sync = FALSE, do_pts = FALSE;
2096 gboolean drain = (buf == NULL);
2097 GstFlowReturn ret = GST_FLOW_OK;
2100 goto not_negotiated;
2102 /* if this pad has a prepare function, call it */
2103 if (pad->prepare_buf_func != NULL) {
2104 buf = pad->prepare_buf_func (pad, buf, qtmux);
2108 last_buf = pad->last_buf;
2109 if (G_UNLIKELY (qtmux->dts_method == DTS_METHOD_REORDER)) {
2110 buf = gst_qt_mux_get_asc_buffer_ts (qtmux, pad, buf);
2111 if (!buf && !last_buf) {
2112 GST_DEBUG_OBJECT (qtmux, "no reordered buffer");
2117 if (last_buf == NULL) {
2118 #ifndef GST_DISABLE_GST_DEBUG
2120 GST_DEBUG_OBJECT (qtmux, "Pad %s has no previous buffer stored and "
2121 "received NULL buffer, doing nothing",
2122 GST_PAD_NAME (pad->collect.pad));
2124 GST_LOG_OBJECT (qtmux,
2125 "Pad %s has no previous buffer stored, storing now",
2126 GST_PAD_NAME (pad->collect.pad));
2129 pad->last_buf = buf;
2132 gst_buffer_ref (last_buf);
2134 /* nasty heuristic mess to guestimate dealing with DTS/PTS,
2135 * while also trying to stay close to input ts to preserve sync,
2136 * so in DTS_METHOD_DD:
2137 * - prefer using input ts where possible
2138 * - if those detected out-of-order (*), mark as out-of-order
2139 * - if in out-of-order, then
2140 * - if duration available, use that as delta
2141 * Also mind to preserve sync between streams, and adding
2142 * durations might drift, so try to resync when we expect
2143 * input ts == (sum of durations), which is at some keyframe input frame.
2144 * - if no duration available, we are actually in serious trouble and need
2145 * to hack around that, so we fail.
2146 * To remedy failure, alternatively, in DTS_METHOD_REORDER:
2147 * - collect some buffers and re-order timestamp,
2148 * then process the oldest buffer with smallest timestamps.
2149 * This should typically compensate for some codec's handywork with ts.
2150 * ... but in case this makes ts end up where not expected, in DTS_METHOD_ASC:
2151 * - keep each ts with its buffer and still keep a list of most recent X ts,
2152 * use the (ascending) minimum of those as DTS (and the difference as ts delta),
2153 * and use this DTS as a basis to obtain a (positive) CTS offset.
2154 * This should yield exact PTS == buffer ts, but it seems not all players
2155 * out there are aware of ctts pts ...
2157 * 0.11 Phew, can we (pretty) please please sort out DTS/PTS on buffers ...
2159 if (G_LIKELY (buf) && !pad->is_out_of_order) {
2160 if (G_LIKELY (GST_BUFFER_TIMESTAMP_IS_VALID (last_buf) &&
2161 GST_BUFFER_TIMESTAMP_IS_VALID (buf))) {
2162 if ((GST_BUFFER_TIMESTAMP (buf) < GST_BUFFER_TIMESTAMP (last_buf))) {
2163 GST_DEBUG_OBJECT (qtmux, "detected out-of-order input");
2164 pad->is_out_of_order = TRUE;
2167 /* this is pretty bad */
2168 GST_WARNING_OBJECT (qtmux, "missing input timestamp");
2169 /* fall back to durations */
2170 pad->is_out_of_order = TRUE;
2174 /* would have to be some unusual input, but not impossible */
2175 if (G_UNLIKELY (qtmux->dts_method == DTS_METHOD_REORDER &&
2176 pad->is_out_of_order)) {
2180 /* fall back to duration if last buffer or
2181 * out-of-order (determined previously), otherwise use input ts */
2183 (pad->is_out_of_order && qtmux->dts_method == DTS_METHOD_DD)) {
2184 if (!GST_BUFFER_DURATION_IS_VALID (last_buf)) {
2185 /* be forgiving for some possibly last upstream flushed buffer */
2188 GST_WARNING_OBJECT (qtmux, "no duration for last buffer");
2189 /* iso spec recommends some small value, try 0 */
2192 duration = GST_BUFFER_DURATION (last_buf);
2193 /* avoid drift in sum timestamps,
2194 * so use input timestamp for suitable keyframe */
2195 if (buf && !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) &&
2196 GST_BUFFER_TIMESTAMP (buf) >= pad->last_dts) {
2197 GST_DEBUG_OBJECT (qtmux, "resyncing out-of-order input to ts; "
2198 "replacing %" GST_TIME_FORMAT " by %" GST_TIME_FORMAT,
2199 GST_TIME_ARGS (pad->last_dts + duration),
2200 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2201 duration = GST_BUFFER_TIMESTAMP (buf) - pad->last_dts;
2204 } else if (qtmux->dts_method != DTS_METHOD_ASC) {
2205 duration = GST_BUFFER_TIMESTAMP (buf) - GST_BUFFER_TIMESTAMP (last_buf);
2209 g_assert (qtmux->dts_method == DTS_METHOD_ASC);
2210 if (!qtmux->guess_pts)
2213 /* add timestamp to queue; keeps in descending order */
2214 gst_qt_mux_push_ts (qtmux, pad, GST_BUFFER_TIMESTAMP (last_buf));
2215 /* chuck out smallest/last one if we have enough */
2216 if (G_LIKELY (pad->ts_n_entries > QTMUX_NO_OF_TS))
2217 pad->ts_n_entries--;
2218 /* peek the now smallest timestamp */
2219 ts = pad->ts_entries[pad->ts_n_entries - 1];
2220 /* these tails are expected to be (strictly) ascending with
2221 * large enough history */
2222 GST_DEBUG_OBJECT (qtmux, "ASC method; base timestamp %" GST_TIME_FORMAT,
2223 GST_TIME_ARGS (ts));
2224 if (ts >= pad->last_dts) {
2225 duration = ts - pad->last_dts;
2227 /* fallback to previous value, negative ct offset might handle */
2228 GST_WARNING_OBJECT (qtmux, "unexpected decrease in timestamp");
2231 /* arrange for small non-zero duration/delta << expected frame time */
2232 ts = gst_util_uint64_scale (10, GST_SECOND,
2233 atom_trak_get_timescale (pad->trak));
2234 duration = MAX (duration, ts);
2237 gst_buffer_replace (&pad->last_buf, buf);
2239 last_dts = gst_util_uint64_scale_round (pad->last_dts,
2240 atom_trak_get_timescale (pad->trak), GST_SECOND);
2242 /* fragments only deal with 1 buffer == 1 chunk (== 1 sample) */
2243 if (pad->sample_size && !qtmux->fragment_sequence) {
2244 /* Constant size packets: usually raw audio (with many samples per
2245 buffer (= chunk)), but can also be fixed-packet-size codecs like ADPCM
2247 sample_size = pad->sample_size;
2248 if (gst_buffer_get_size (last_buf) % sample_size != 0)
2249 goto fragmented_sample;
2250 /* note: qt raw audio storage warps it implicitly into a timewise
2251 * perfect stream, discarding buffer times */
2252 if (GST_BUFFER_DURATION (last_buf) != GST_CLOCK_TIME_NONE) {
2253 nsamples = gst_util_uint64_scale_round (GST_BUFFER_DURATION (last_buf),
2254 atom_trak_get_timescale (pad->trak), GST_SECOND);
2256 nsamples = gst_buffer_get_size (last_buf) / sample_size;
2258 duration = GST_BUFFER_DURATION (last_buf) / nsamples;
2260 /* timescale = samplerate */
2261 scaled_duration = 1;
2262 pad->last_dts += duration * nsamples;
2265 sample_size = gst_buffer_get_size (last_buf);
2266 if (pad->have_dts) {
2268 pad->last_dts = GST_BUFFER_OFFSET_END (last_buf);
2269 if ((gint64) (pad->last_dts) < 0) {
2270 scaled_dts = -gst_util_uint64_scale_round (-pad->last_dts,
2271 atom_trak_get_timescale (pad->trak), GST_SECOND);
2273 scaled_dts = gst_util_uint64_scale_round (pad->last_dts,
2274 atom_trak_get_timescale (pad->trak), GST_SECOND);
2276 scaled_duration = scaled_dts - last_dts;
2277 last_dts = scaled_dts;
2279 /* first convert intended timestamp (in GstClockTime resolution) to
2280 * trak timescale, then derive delta;
2281 * this ensures sums of (scale)delta add up to converted timestamp,
2282 * which only deviates at most 1/scale from timestamp itself */
2283 scaled_duration = gst_util_uint64_scale_round (pad->last_dts + duration,
2284 atom_trak_get_timescale (pad->trak), GST_SECOND) - last_dts;
2285 pad->last_dts += duration;
2288 chunk_offset = qtmux->mdat_size;
2290 GST_LOG_OBJECT (qtmux,
2291 "Pad (%s) dts updated to %" GST_TIME_FORMAT,
2292 GST_PAD_NAME (pad->collect.pad), GST_TIME_ARGS (pad->last_dts));
2293 GST_LOG_OBJECT (qtmux,
2294 "Adding %d samples to track, duration: %" G_GUINT64_FORMAT
2295 " size: %" G_GUINT32_FORMAT " chunk offset: %" G_GUINT64_FORMAT,
2296 nsamples, scaled_duration, sample_size, chunk_offset);
2298 /* might be a sync sample */
2300 !GST_BUFFER_FLAG_IS_SET (last_buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
2301 GST_LOG_OBJECT (qtmux, "Adding new sync sample entry for track of pad %s",
2302 GST_PAD_NAME (pad->collect.pad));
2306 /* optionally calculate ctts entry values
2307 * (if composition-time expected different from decoding-time) */
2308 /* really not recommended:
2309 * - decoder typically takes care of dts/pts issues
2310 * - in case of out-of-order, dts may only be determined as above
2311 * (e.g. sum of duration), which may be totally different from
2312 * buffer timestamps in case of multiple segment, non-perfect streams
2313 * (and just perhaps maybe with some luck segment_to_running_time
2314 * or segment_to_media_time might get near to it) */
2315 if ((pad->have_dts || qtmux->guess_pts)) {
2318 pts = qtmux->dts_method == DTS_METHOD_REORDER ?
2319 GST_BUFFER_OFFSET_END (last_buf) : GST_BUFFER_TIMESTAMP (last_buf);
2320 pts = gst_util_uint64_scale_round (pts,
2321 atom_trak_get_timescale (pad->trak), GST_SECOND);
2322 pts_offset = (gint64) (pts - last_dts);
2324 GST_LOG_OBJECT (qtmux, "Adding ctts entry for pad %s: %" G_GINT64_FORMAT,
2325 GST_PAD_NAME (pad->collect.pad), pts_offset);
2329 * Each buffer starts a new chunk, so we can assume the buffer
2330 * duration is the chunk duration
2332 if (GST_CLOCK_TIME_IS_VALID (duration) && (duration > qtmux->longest_chunk ||
2333 !GST_CLOCK_TIME_IS_VALID (qtmux->longest_chunk))) {
2334 GST_DEBUG_OBJECT (qtmux, "New longest chunk found: %" GST_TIME_FORMAT
2335 ", pad %s", GST_TIME_ARGS (duration), GST_PAD_NAME (pad->collect.pad));
2336 qtmux->longest_chunk = duration;
2339 /* if this is the first buffer, store the timestamp */
2340 if (G_UNLIKELY (pad->first_ts == GST_CLOCK_TIME_NONE) && last_buf) {
2341 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (last_buf))) {
2342 pad->first_ts = GST_BUFFER_TIMESTAMP (last_buf);
2344 GST_DEBUG_OBJECT (qtmux, "First buffer for pad %s has no timestamp, "
2345 "using 0 as first timestamp", GST_PAD_NAME (pad->collect.pad));
2348 GST_DEBUG_OBJECT (qtmux, "Stored first timestamp for pad %s %"
2349 GST_TIME_FORMAT, GST_PAD_NAME (pad->collect.pad),
2350 GST_TIME_ARGS (pad->first_ts));
2353 /* now we go and register this buffer/sample all over */
2354 /* note that a new chunk is started each time (not fancy but works) */
2355 if (qtmux->moov_recov_file) {
2356 if (!atoms_recov_write_trak_samples (qtmux->moov_recov_file, pad->trak,
2357 nsamples, (gint32) scaled_duration, sample_size, chunk_offset, sync,
2358 do_pts, pts_offset)) {
2359 GST_WARNING_OBJECT (qtmux, "Failed to write sample information to "
2360 "recovery file, disabling recovery");
2361 fclose (qtmux->moov_recov_file);
2362 qtmux->moov_recov_file = NULL;
2367 gst_buffer_unref (buf);
2369 if (qtmux->fragment_sequence) {
2370 /* ensure that always sync samples are marked as such */
2371 ret = gst_qt_mux_pad_fragment_add_buffer (qtmux, pad, last_buf,
2372 buf == NULL, nsamples, last_dts, (gint32) scaled_duration, sample_size,
2373 !pad->sync || sync, pts_offset);
2375 atom_trak_add_samples (pad->trak, nsamples, (gint32) scaled_duration,
2376 sample_size, chunk_offset, sync, pts_offset);
2377 ret = gst_qt_mux_send_buffer (qtmux, last_buf, &qtmux->mdat_size, TRUE);
2381 if (G_UNLIKELY (drain && qtmux->dts_method == DTS_METHOD_REORDER &&
2382 ret == GST_FLOW_OK)) {
2393 gst_buffer_unref (buf);
2394 gst_buffer_unref (last_buf);
2395 return GST_FLOW_ERROR;
2399 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2400 ("Received buffer without timestamp/duration. "
2401 "Using e.g. dts-method=reorder might help."));
2406 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2407 ("DTS method failed to re-order timestamps."));
2412 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2413 ("Selected DTS method also needs PTS enabled."));
2418 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2419 ("Audio buffer contains fragmented sample."));
2424 GST_ELEMENT_ERROR (qtmux, CORE, NEGOTIATION, (NULL),
2425 ("format wasn't negotiated before buffer flow on pad %s",
2426 GST_PAD_NAME (pad->collect.pad)));
2428 gst_buffer_unref (buf);
2429 return GST_FLOW_NOT_NEGOTIATED;
2433 static GstFlowReturn
2434 gst_qt_mux_collected (GstCollectPads * pads, gpointer user_data)
2436 GstFlowReturn ret = GST_FLOW_OK;
2437 GstQTMux *qtmux = GST_QT_MUX_CAST (user_data);
2439 GstQTPad *best_pad = NULL;
2440 GstClockTime time, best_time = GST_CLOCK_TIME_NONE;
2443 if (G_UNLIKELY (qtmux->state == GST_QT_MUX_STATE_STARTED)) {
2444 if ((ret = gst_qt_mux_start_file (qtmux)) != GST_FLOW_OK)
2447 qtmux->state = GST_QT_MUX_STATE_DATA;
2450 if (G_UNLIKELY (qtmux->state == GST_QT_MUX_STATE_EOS))
2451 return GST_FLOW_UNEXPECTED;
2453 /* select the best buffer */
2454 walk = qtmux->collect->data;
2457 GstCollectData *data;
2459 data = (GstCollectData *) walk->data;
2460 pad = (GstQTPad *) data;
2462 walk = g_slist_next (walk);
2464 buf = gst_collect_pads_peek (pads, data);
2466 GST_LOG_OBJECT (qtmux, "Pad %s has no buffers",
2467 GST_PAD_NAME (pad->collect.pad));
2470 time = GST_BUFFER_TIMESTAMP (buf);
2471 gst_buffer_unref (buf);
2473 /* invalid should pass */
2474 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
2476 gst_segment_to_running_time (&data->segment, GST_FORMAT_TIME, time);
2477 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
2478 GST_DEBUG_OBJECT (qtmux, "clipping buffer on pad %s outside segment",
2479 GST_PAD_NAME (data->pad));
2480 buf = gst_collect_pads_pop (pads, data);
2481 gst_buffer_unref (buf);
2486 if (best_pad == NULL || !GST_CLOCK_TIME_IS_VALID (time) ||
2487 (GST_CLOCK_TIME_IS_VALID (best_time) && time < best_time)) {
2493 if (best_pad != NULL) {
2494 GST_LOG_OBJECT (qtmux, "selected pad %s with time %" GST_TIME_FORMAT,
2495 GST_PAD_NAME (best_pad->collect.pad), GST_TIME_ARGS (best_time));
2496 buf = gst_collect_pads_pop (pads, &best_pad->collect);
2497 buf = gst_buffer_make_writable (buf);
2498 GST_BUFFER_TIMESTAMP (buf) = best_time;
2499 ret = gst_qt_mux_add_buffer (qtmux, best_pad, buf);
2501 ret = gst_qt_mux_stop_file (qtmux);
2502 if (ret == GST_FLOW_OK) {
2503 GST_DEBUG_OBJECT (qtmux, "Pushing eos");
2504 gst_pad_push_event (qtmux->srcpad, gst_event_new_eos ());
2505 ret = GST_FLOW_UNEXPECTED;
2507 GST_WARNING_OBJECT (qtmux, "Failed to stop file: %s",
2508 gst_flow_get_name (ret));
2510 qtmux->state = GST_QT_MUX_STATE_EOS;
2517 check_field (GQuark field_id, const GValue * value, gpointer user_data)
2519 GstStructure *structure = (GstStructure *) user_data;
2520 const GValue *other = gst_structure_id_get_value (structure, field_id);
2523 return gst_value_compare (value, other) == GST_VALUE_EQUAL;
2527 gst_qtmux_caps_is_subset_full (GstQTMux * qtmux, GstCaps * subset,
2530 GstStructure *sub_s = gst_caps_get_structure (subset, 0);
2531 GstStructure *sup_s = gst_caps_get_structure (superset, 0);
2533 return gst_structure_foreach (sub_s, check_field, sup_s);
2537 gst_qt_mux_audio_sink_set_caps (GstPad * pad, GstCaps * caps)
2539 GstQTMux *qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
2540 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
2541 GstQTPad *qtpad = NULL;
2542 GstStructure *structure;
2543 const gchar *mimetype;
2544 gint rate, channels;
2545 const GValue *value = NULL;
2546 const GstBuffer *codec_data = NULL;
2547 GstQTMuxFormat format;
2548 AudioSampleEntry entry = { 0, };
2549 AtomInfo *ext_atom = NULL;
2550 gint constant_size = 0;
2551 const gchar *stream_format;
2553 /* find stream data */
2554 qtpad = (GstQTPad *) gst_pad_get_element_private (pad);
2557 qtpad->prepare_buf_func = NULL;
2559 /* does not go well to renegotiate stream mid-way, unless
2560 * the old caps are a subset of the new one (this means upstream
2561 * added more info to the caps, as both should be 'fixed' caps) */
2562 if (qtpad->fourcc) {
2563 GstCaps *current_caps;
2565 current_caps = gst_pad_get_current_caps (pad);
2566 g_assert (caps != NULL);
2568 if (!gst_qtmux_caps_is_subset_full (qtmux, current_caps, caps)) {
2569 gst_caps_unref (current_caps);
2570 goto refuse_renegotiation;
2572 GST_DEBUG_OBJECT (qtmux,
2573 "pad %s accepted renegotiation to %" GST_PTR_FORMAT " from %"
2574 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, current_caps);
2575 gst_caps_unref (current_caps);
2578 GST_DEBUG_OBJECT (qtmux, "%s:%s, caps=%" GST_PTR_FORMAT,
2579 GST_DEBUG_PAD_NAME (pad), caps);
2581 format = qtmux_klass->format;
2582 structure = gst_caps_get_structure (caps, 0);
2583 mimetype = gst_structure_get_name (structure);
2586 if (!gst_structure_get_int (structure, "channels", &channels) ||
2587 !gst_structure_get_int (structure, "rate", &rate)) {
2592 value = gst_structure_get_value (structure, "codec_data");
2594 codec_data = gst_value_get_buffer (value);
2596 qtpad->is_out_of_order = FALSE;
2597 qtpad->have_dts = FALSE;
2599 /* set common properties */
2600 entry.sample_rate = rate;
2601 entry.channels = channels;
2603 entry.sample_size = 16;
2604 /* this is the typical compressed case */
2605 if (format == GST_QT_MUX_FORMAT_QT) {
2607 entry.compression_id = -2;
2610 /* now map onto a fourcc, and some extra properties */
2611 if (strcmp (mimetype, "audio/mpeg") == 0) {
2612 gint mpegversion = 0;
2615 gst_structure_get_int (structure, "mpegversion", &mpegversion);
2616 switch (mpegversion) {
2618 gst_structure_get_int (structure, "layer", &layer);
2622 /* note: QuickTime player does not like mp3 either way in iso/mp4 */
2623 if (format == GST_QT_MUX_FORMAT_QT)
2624 entry.fourcc = FOURCC__mp3;
2626 entry.fourcc = FOURCC_mp4a;
2628 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG1_P3,
2629 ESDS_STREAM_TYPE_AUDIO, codec_data, qtpad->avg_bitrate,
2630 qtpad->max_bitrate);
2632 entry.samples_per_packet = 1152;
2633 entry.bytes_per_sample = 2;
2639 /* check stream-format */
2640 stream_format = gst_structure_get_string (structure, "stream-format");
2641 if (stream_format) {
2642 if (strcmp (stream_format, "raw") != 0) {
2643 GST_WARNING_OBJECT (qtmux, "Unsupported AAC stream-format %s, "
2644 "please use 'raw'", stream_format);
2648 GST_WARNING_OBJECT (qtmux, "No stream-format present in caps, "
2652 if (!codec_data || gst_buffer_get_size ((GstBuffer *) codec_data) < 2)
2653 GST_WARNING_OBJECT (qtmux, "no (valid) codec_data for AAC audio");
2657 gst_buffer_extract ((GstBuffer *) codec_data, 0, &profile, 1);
2658 /* warn if not Low Complexity profile */
2661 GST_WARNING_OBJECT (qtmux,
2662 "non-LC AAC may not run well on (Apple) QuickTime/iTunes");
2666 entry.fourcc = FOURCC_mp4a;
2668 if (format == GST_QT_MUX_FORMAT_QT)
2669 ext_atom = build_mov_aac_extension (qtpad->trak, codec_data,
2670 qtpad->avg_bitrate, qtpad->max_bitrate);
2673 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG4_P3,
2674 ESDS_STREAM_TYPE_AUDIO, codec_data, qtpad->avg_bitrate,
2675 qtpad->max_bitrate);
2680 } else if (strcmp (mimetype, "audio/AMR") == 0) {
2681 entry.fourcc = FOURCC_samr;
2682 entry.sample_size = 16;
2683 entry.samples_per_packet = 160;
2684 entry.bytes_per_sample = 2;
2685 ext_atom = build_amr_extension ();
2686 } else if (strcmp (mimetype, "audio/AMR-WB") == 0) {
2687 entry.fourcc = FOURCC_sawb;
2688 entry.sample_size = 16;
2689 entry.samples_per_packet = 320;
2690 entry.bytes_per_sample = 2;
2691 ext_atom = build_amr_extension ();
2692 } else if (strcmp (mimetype, "audio/x-raw-int") == 0) {
2698 if (!gst_structure_get_int (structure, "width", &width) ||
2699 !gst_structure_get_int (structure, "depth", &depth) ||
2700 !gst_structure_get_boolean (structure, "signed", &sign)) {
2701 GST_DEBUG_OBJECT (qtmux, "broken caps, width/depth/signed field missing");
2706 endianness = G_BYTE_ORDER;
2707 } else if (!gst_structure_get_int (structure, "endianness", &endianness)) {
2708 GST_DEBUG_OBJECT (qtmux, "broken caps, endianness field missing");
2712 /* spec has no place for a distinction in these */
2713 if (width != depth) {
2714 GST_DEBUG_OBJECT (qtmux, "width must be same as depth!");
2719 if (endianness == G_LITTLE_ENDIAN)
2720 entry.fourcc = FOURCC_sowt;
2721 else if (endianness == G_BIG_ENDIAN)
2722 entry.fourcc = FOURCC_twos;
2723 /* maximum backward compatibility; only new version for > 16 bit */
2726 /* not compressed in any case */
2727 entry.compression_id = 0;
2728 /* QT spec says: max at 16 bit even if sample size were actually larger,
2729 * however, most players (e.g. QuickTime!) seem to disagree, so ... */
2730 entry.sample_size = depth;
2731 entry.bytes_per_sample = depth / 8;
2732 entry.samples_per_packet = 1;
2733 entry.bytes_per_packet = depth / 8;
2734 entry.bytes_per_frame = entry.bytes_per_packet * channels;
2736 if (width == 8 && depth == 8) {
2737 /* fall back to old 8-bit version */
2738 entry.fourcc = FOURCC_raw_;
2740 entry.compression_id = 0;
2741 entry.sample_size = 8;
2743 GST_DEBUG_OBJECT (qtmux, "non 8-bit PCM must be signed");
2747 constant_size = (depth / 8) * channels;
2748 } else if (strcmp (mimetype, "audio/x-alaw") == 0) {
2749 entry.fourcc = FOURCC_alaw;
2750 entry.samples_per_packet = 1023;
2751 entry.bytes_per_sample = 2;
2752 } else if (strcmp (mimetype, "audio/x-mulaw") == 0) {
2753 entry.fourcc = FOURCC_ulaw;
2754 entry.samples_per_packet = 1023;
2755 entry.bytes_per_sample = 2;
2756 } else if (strcmp (mimetype, "audio/x-adpcm") == 0) {
2758 if (!gst_structure_get_int (structure, "block_align", &blocksize)) {
2759 GST_DEBUG_OBJECT (qtmux, "broken caps, block_align missing");
2762 /* Currently only supports WAV-style IMA ADPCM, for which the codec id is
2764 entry.fourcc = MS_WAVE_FOURCC (0x11);
2765 /* 4 byte header per channel (including one sample). 2 samples per byte
2766 remaining. Simplifying gives the following (samples per block per
2768 entry.samples_per_packet = 2 * blocksize / channels - 7;
2769 entry.bytes_per_sample = 2;
2771 entry.bytes_per_frame = blocksize;
2772 entry.bytes_per_packet = blocksize / channels;
2773 /* ADPCM has constant size packets */
2775 /* TODO: I don't really understand why this helps, but it does! Constant
2776 * size and compression_id of -2 seem to be incompatible, and other files
2777 * in the wild use this too. */
2778 entry.compression_id = -1;
2780 ext_atom = build_ima_adpcm_extension (channels, rate, blocksize);
2781 } else if (strcmp (mimetype, "audio/x-alac") == 0) {
2782 GstBuffer *codec_config;
2787 entry.fourcc = FOURCC_alac;
2788 data = gst_buffer_map ((GstBuffer *) codec_data, &size, NULL, GST_MAP_READ);
2789 /* let's check if codec data already comes with 'alac' atom prefix */
2790 if (!codec_data || (len = size) < 28) {
2791 GST_DEBUG_OBJECT (qtmux, "broken caps, codec data missing");
2792 gst_buffer_unmap ((GstBuffer *) codec_data, data, size);
2795 if (GST_READ_UINT32_LE (data + 4) == FOURCC_alac) {
2798 gst_buffer_copy_region ((GstBuffer *) codec_data, 0, 8, len);
2800 codec_config = gst_buffer_ref ((GstBuffer *) codec_data);
2802 gst_buffer_unmap ((GstBuffer *) codec_data, data, size);
2804 /* does not look good, but perhaps some trailing unneeded stuff */
2805 GST_WARNING_OBJECT (qtmux, "unexpected codec-data size, possibly broken");
2807 if (format == GST_QT_MUX_FORMAT_QT)
2808 ext_atom = build_mov_alac_extension (qtpad->trak, codec_config);
2810 ext_atom = build_codec_data_extension (FOURCC_alac, codec_config);
2811 /* set some more info */
2812 data = gst_buffer_map (codec_config, &size, NULL, GST_MAP_READ);
2813 entry.bytes_per_sample = 2;
2814 entry.samples_per_packet = GST_READ_UINT32_BE (data + 4);
2815 gst_buffer_unmap (codec_config, data, size);
2816 gst_buffer_unref (codec_config);
2822 /* ok, set the pad info accordingly */
2823 qtpad->fourcc = entry.fourcc;
2824 qtpad->sample_size = constant_size;
2825 atom_trak_set_audio_type (qtpad->trak, qtmux->context, &entry,
2826 qtmux->trak_timescale ? qtmux->trak_timescale : entry.sample_rate,
2827 ext_atom, constant_size);
2829 gst_object_unref (qtmux);
2835 GST_WARNING_OBJECT (qtmux, "pad %s refused caps %" GST_PTR_FORMAT,
2836 GST_PAD_NAME (pad), caps);
2837 gst_object_unref (qtmux);
2840 refuse_renegotiation:
2842 GST_WARNING_OBJECT (qtmux,
2843 "pad %s refused renegotiation to %" GST_PTR_FORMAT,
2844 GST_PAD_NAME (pad), caps);
2845 gst_object_unref (qtmux);
2850 /* scale rate up or down by factor of 10 to fit into [1000,10000] interval */
2852 adjust_rate (guint64 rate)
2857 while (rate >= 10000)
2863 return (guint32) rate;
2867 gst_qt_mux_video_sink_set_caps (GstPad * pad, GstCaps * caps)
2869 GstQTMux *qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
2870 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
2871 GstQTPad *qtpad = NULL;
2872 GstStructure *structure;
2873 const gchar *mimetype;
2874 gint width, height, depth = -1;
2875 gint framerate_num, framerate_den;
2877 const GValue *value = NULL;
2878 const GstBuffer *codec_data = NULL;
2879 VisualSampleEntry entry = { 0, };
2880 GstQTMuxFormat format;
2881 AtomInfo *ext_atom = NULL;
2882 GList *ext_atom_list = NULL;
2883 gboolean sync = FALSE;
2884 int par_num, par_den;
2886 /* find stream data */
2887 qtpad = (GstQTPad *) gst_pad_get_element_private (pad);
2890 qtpad->prepare_buf_func = NULL;
2892 /* does not go well to renegotiate stream mid-way, unless
2893 * the old caps are a subset of the new one (this means upstream
2894 * added more info to the caps, as both should be 'fixed' caps) */
2895 if (qtpad->fourcc) {
2896 GstCaps *current_caps;
2898 current_caps = gst_pad_get_current_caps (pad);
2899 g_assert (caps != NULL);
2901 if (!gst_qtmux_caps_is_subset_full (qtmux, current_caps, caps)) {
2902 gst_caps_unref (current_caps);
2903 goto refuse_renegotiation;
2905 GST_DEBUG_OBJECT (qtmux,
2906 "pad %s accepted renegotiation to %" GST_PTR_FORMAT " from %"
2907 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, current_caps);
2908 gst_caps_unref (current_caps);
2911 GST_DEBUG_OBJECT (qtmux, "%s:%s, caps=%" GST_PTR_FORMAT,
2912 GST_DEBUG_PAD_NAME (pad), caps);
2914 format = qtmux_klass->format;
2915 structure = gst_caps_get_structure (caps, 0);
2916 mimetype = gst_structure_get_name (structure);
2918 /* required parts */
2919 if (!gst_structure_get_int (structure, "width", &width) ||
2920 !gst_structure_get_int (structure, "height", &height))
2925 /* works as a default timebase */
2926 framerate_num = 10000;
2928 gst_structure_get_fraction (structure, "framerate", &framerate_num,
2930 gst_structure_get_int (structure, "depth", &depth);
2931 value = gst_structure_get_value (structure, "codec_data");
2933 codec_data = gst_value_get_buffer (value);
2937 gst_structure_get_fraction (structure, "pixel-aspect-ratio", &par_num,
2940 qtpad->is_out_of_order = FALSE;
2942 /* bring frame numerator into a range that ensures both reasonable resolution
2943 * as well as a fair duration */
2944 rate = qtmux->trak_timescale ?
2945 qtmux->trak_timescale : adjust_rate (framerate_num);
2946 GST_DEBUG_OBJECT (qtmux, "Rate of video track selected: %" G_GUINT32_FORMAT,
2949 /* set common properties */
2950 entry.width = width;
2951 entry.height = height;
2952 entry.par_n = par_num;
2953 entry.par_d = par_den;
2954 /* should be OK according to qt and iso spec, override if really needed */
2955 entry.color_table_id = -1;
2956 entry.frame_count = 1;
2959 /* sync entries by default */
2962 /* now map onto a fourcc, and some extra properties */
2963 if (strcmp (mimetype, "video/x-raw") == 0) {
2964 const gchar *format;
2966 const GstVideoFormatInfo *vinfo;
2968 format = gst_structure_get_string (structure, "format");
2969 fmt = gst_video_format_from_string (format);
2970 vinfo = gst_video_format_get_info (fmt);
2973 case GST_VIDEO_FORMAT_UYVY:
2976 entry.fourcc = FOURCC_2vuy;
2977 entry.depth = depth;
2981 if (GST_VIDEO_FORMAT_INFO_FLAGS (vinfo) & GST_VIDEO_FORMAT_FLAG_RGB) {
2982 entry.fourcc = FOURCC_raw_;
2983 entry.depth = GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, 0) * 8;
2988 } else if (strcmp (mimetype, "video/x-h263") == 0) {
2990 if (format == GST_QT_MUX_FORMAT_QT)
2991 entry.fourcc = FOURCC_h263;
2993 entry.fourcc = FOURCC_s263;
2994 ext_atom = build_h263_extension ();
2995 if (ext_atom != NULL)
2996 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
2997 } else if (strcmp (mimetype, "video/x-divx") == 0 ||
2998 strcmp (mimetype, "video/mpeg") == 0) {
3001 if (strcmp (mimetype, "video/x-divx") == 0) {
3002 gst_structure_get_int (structure, "divxversion", &version);
3003 version = version == 5 ? 1 : 0;
3005 gst_structure_get_int (structure, "mpegversion", &version);
3006 version = version == 4 ? 1 : 0;
3009 entry.fourcc = FOURCC_mp4v;
3011 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG4_P2,
3012 ESDS_STREAM_TYPE_VISUAL, codec_data, qtpad->avg_bitrate,
3013 qtpad->max_bitrate);
3014 if (ext_atom != NULL)
3015 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3017 GST_WARNING_OBJECT (qtmux, "no codec_data for MPEG4 video; "
3018 "output might not play in Apple QuickTime (try global-headers?)");
3020 } else if (strcmp (mimetype, "video/x-h264") == 0) {
3021 /* check if we accept these caps */
3022 if (gst_structure_has_field (structure, "stream-format")) {
3023 const gchar *format;
3024 const gchar *alignment;
3026 format = gst_structure_get_string (structure, "stream-format");
3027 alignment = gst_structure_get_string (structure, "alignment");
3029 if (strcmp (format, "avc") != 0 || alignment == NULL ||
3030 strcmp (alignment, "au") != 0) {
3031 GST_WARNING_OBJECT (qtmux, "Rejecting h264 caps, qtmux only accepts "
3032 "avc format with AU aligned samples");
3036 GST_WARNING_OBJECT (qtmux, "no stream-format field in h264 caps");
3041 GST_WARNING_OBJECT (qtmux, "no codec_data in h264 caps");
3045 entry.fourcc = FOURCC_avc1;
3046 if (qtpad->avg_bitrate == 0) {
3047 gint avg_bitrate = 0;
3048 gst_structure_get_int (structure, "bitrate", &avg_bitrate);
3049 qtpad->avg_bitrate = avg_bitrate;
3051 ext_atom = build_btrt_extension (0, qtpad->avg_bitrate, qtpad->max_bitrate);
3052 if (ext_atom != NULL)
3053 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3054 ext_atom = build_codec_data_extension (FOURCC_avcC, codec_data);
3055 if (ext_atom != NULL)
3056 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3057 } else if (strcmp (mimetype, "video/x-svq") == 0) {
3059 const GstBuffer *seqh = NULL;
3060 const GValue *seqh_value;
3063 gst_structure_get_int (structure, "svqversion", &version);
3065 entry.fourcc = FOURCC_SVQ3;
3069 seqh_value = gst_structure_get_value (structure, "seqh");
3071 seqh = gst_value_get_buffer (seqh_value);
3072 ext_atom = build_SMI_atom (seqh);
3074 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3077 /* we need to add the gamma anyway because quicktime might crash
3078 * when it doesn't find it */
3079 if (!gst_structure_get_double (structure, "applied-gamma", &gamma)) {
3080 /* it seems that using 0 here makes it ignored */
3083 ext_atom = build_gama_atom (gamma);
3085 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3087 GST_WARNING_OBJECT (qtmux, "SVQ version %d not supported. Please file "
3088 "a bug at http://bugzilla.gnome.org", version);
3090 } else if (strcmp (mimetype, "video/x-dv") == 0) {
3092 gboolean pal = TRUE;
3095 if (framerate_num != 25 || framerate_den != 1)
3097 gst_structure_get_int (structure, "dvversion", &version);
3098 /* fall back to typical one */
3104 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', 'c', 'p');
3106 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', 'c', ' ');
3110 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', '5', 'p');
3112 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', '5', 'n');
3115 GST_WARNING_OBJECT (qtmux, "unrecognized dv version");
3118 } else if (strcmp (mimetype, "image/jpeg") == 0) {
3119 entry.fourcc = FOURCC_jpeg;
3121 } else if (strcmp (mimetype, "image/x-j2c") == 0 ||
3122 strcmp (mimetype, "image/x-jpc") == 0) {
3123 const gchar *colorspace;
3124 const GValue *cmap_array;
3125 const GValue *cdef_array;
3129 if (strcmp (mimetype, "image/x-jpc") == 0) {
3130 qtpad->prepare_buf_func = gst_qt_mux_prepare_jpc_buffer;
3133 gst_structure_get_int (structure, "num-components", &ncomp);
3134 gst_structure_get_int (structure, "fields", &fields);
3135 cmap_array = gst_structure_get_value (structure, "component-map");
3136 cdef_array = gst_structure_get_value (structure, "channel-definitions");
3139 entry.fourcc = FOURCC_mjp2;
3142 colorspace = gst_structure_get_string (structure, "colorspace");
3145 build_jp2h_extension (qtpad->trak, width, height, colorspace, ncomp,
3146 cmap_array, cdef_array)) != NULL) {
3147 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3149 ext_atom = build_fiel_extension (fields);
3151 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3153 ext_atom = build_jp2x_extension (codec_data);
3155 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3157 GST_DEBUG_OBJECT (qtmux, "missing or invalid fourcc in jp2 caps");
3160 } else if (strcmp (mimetype, "video/x-vp8") == 0) {
3161 entry.fourcc = FOURCC_VP80;
3163 } else if (strcmp (mimetype, "video/x-dirac") == 0) {
3164 entry.fourcc = FOURCC_drac;
3165 qtpad->have_dts = TRUE;
3166 } else if (strcmp (mimetype, "video/x-qt-part") == 0) {
3169 gst_structure_get_uint (structure, "format", &fourcc);
3170 entry.fourcc = fourcc;
3171 qtpad->have_dts = TRUE;
3172 } else if (strcmp (mimetype, "video/x-mp4-part") == 0) {
3175 gst_structure_get_uint (structure, "format", &fourcc);
3176 entry.fourcc = fourcc;
3177 qtpad->have_dts = TRUE;
3183 /* ok, set the pad info accordingly */
3184 qtpad->fourcc = entry.fourcc;
3186 atom_trak_set_video_type (qtpad->trak, qtmux->context, &entry, rate,
3189 gst_object_unref (qtmux);
3195 GST_WARNING_OBJECT (qtmux, "pad %s refused caps %" GST_PTR_FORMAT,
3196 GST_PAD_NAME (pad), caps);
3197 gst_object_unref (qtmux);
3200 refuse_renegotiation:
3202 GST_WARNING_OBJECT (qtmux,
3203 "pad %s refused renegotiation to %" GST_PTR_FORMAT, GST_PAD_NAME (pad),
3205 gst_object_unref (qtmux);
3211 gst_qt_mux_sink_event (GstPad * pad, GstEvent * event)
3215 guint32 avg_bitrate = 0, max_bitrate = 0;
3217 qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
3219 switch (GST_EVENT_TYPE (event)) {
3220 case GST_EVENT_CAPS:
3223 GstQTPad *collect_pad;
3225 gst_event_parse_caps (event, &caps);
3227 /* find stream data */
3228 collect_pad = (GstQTPad *) gst_pad_get_element_private (pad);
3229 g_assert (collect_pad);
3230 g_assert (collect_pad->set_caps);
3232 collect_pad->set_caps (pad, caps);
3235 case GST_EVENT_TAG:{
3237 GstTagSetter *setter = GST_TAG_SETTER (qtmux);
3238 GstTagMergeMode mode;
3240 GST_OBJECT_LOCK (qtmux);
3241 mode = gst_tag_setter_get_tag_merge_mode (setter);
3243 GST_DEBUG_OBJECT (qtmux, "received tag event");
3244 gst_event_parse_tag (event, &list);
3246 gst_tag_setter_merge_tags (setter, list, mode);
3247 GST_OBJECT_UNLOCK (qtmux);
3249 if (gst_tag_list_get_uint (list, GST_TAG_BITRATE, &avg_bitrate) |
3250 gst_tag_list_get_uint (list, GST_TAG_MAXIMUM_BITRATE, &max_bitrate)) {
3251 GstQTPad *qtpad = gst_pad_get_element_private (pad);
3254 if (avg_bitrate > 0 && avg_bitrate < G_MAXUINT32)
3255 qtpad->avg_bitrate = avg_bitrate;
3256 if (max_bitrate > 0 && max_bitrate < G_MAXUINT32)
3257 qtpad->max_bitrate = max_bitrate;
3266 ret = qtmux->collect_event (pad, event);
3267 gst_object_unref (qtmux);
3273 gst_qt_mux_release_pad (GstElement * element, GstPad * pad)
3275 GstQTMux *mux = GST_QT_MUX_CAST (element);
3278 GST_DEBUG_OBJECT (element, "Releasing %s:%s", GST_DEBUG_PAD_NAME (pad));
3280 for (walk = mux->sinkpads; walk; walk = g_slist_next (walk)) {
3281 GstQTPad *qtpad = (GstQTPad *) walk->data;
3282 GST_DEBUG ("Checking %s:%s", GST_DEBUG_PAD_NAME (qtpad->collect.pad));
3283 if (qtpad->collect.pad == pad) {
3284 /* this is it, remove */
3285 mux->sinkpads = g_slist_delete_link (mux->sinkpads, walk);
3286 gst_element_remove_pad (element, pad);
3291 gst_collect_pads_remove_pad (mux->collect, pad);
3295 gst_qt_mux_request_new_pad (GstElement * element,
3296 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
3298 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
3299 GstQTMux *qtmux = GST_QT_MUX_CAST (element);
3300 GstQTPad *collect_pad;
3306 if (templ->direction != GST_PAD_SINK)
3307 goto wrong_direction;
3309 if (qtmux->state > GST_QT_MUX_STATE_STARTED)
3312 if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
3314 if (req_name != NULL && sscanf (req_name, "audio_%02d", &pad_id) == 1) {
3315 name = g_strdup (req_name);
3317 name = g_strdup_printf ("audio_%02d", qtmux->audio_pads++);
3319 } else if (templ == gst_element_class_get_pad_template (klass, "video_%d")) {
3321 if (req_name != NULL && sscanf (req_name, "video_%02d", &pad_id) == 1) {
3322 name = g_strdup (req_name);
3324 name = g_strdup_printf ("video_%02d", qtmux->video_pads++);
3327 goto wrong_template;
3329 GST_DEBUG_OBJECT (qtmux, "Requested pad: %s", name);
3331 /* create pad and add to collections */
3332 newpad = gst_pad_new_from_template (templ, name);
3334 collect_pad = (GstQTPad *)
3335 gst_collect_pads_add_pad_full (qtmux->collect, newpad, sizeof (GstQTPad),
3336 (GstCollectDataDestroyNotify) (gst_qt_mux_pad_reset));
3338 gst_qt_mux_pad_reset (collect_pad);
3339 collect_pad->trak = atom_trak_new (qtmux->context);
3340 atom_moov_add_trak (qtmux->moov, collect_pad->trak);
3342 qtmux->sinkpads = g_slist_append (qtmux->sinkpads, collect_pad);
3344 /* set up pad functions */
3346 collect_pad->set_caps = GST_DEBUG_FUNCPTR (gst_qt_mux_audio_sink_set_caps);
3348 collect_pad->set_caps = GST_DEBUG_FUNCPTR (gst_qt_mux_video_sink_set_caps);
3350 /* FIXME: hacked way to override/extend the event function of
3351 * GstCollectPads; because it sets its own event function giving the
3352 * element no access to events.
3354 qtmux->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
3355 gst_pad_set_event_function (newpad,
3356 GST_DEBUG_FUNCPTR (gst_qt_mux_sink_event));
3358 gst_pad_set_active (newpad, TRUE);
3359 gst_element_add_pad (element, newpad);
3366 GST_WARNING_OBJECT (qtmux, "Request pad that is not a SINK pad.");
3371 GST_WARNING_OBJECT (qtmux, "Not providing request pad after stream start.");
3376 GST_WARNING_OBJECT (qtmux, "This is not our template!");
3382 gst_qt_mux_get_property (GObject * object,
3383 guint prop_id, GValue * value, GParamSpec * pspec)
3385 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
3387 GST_OBJECT_LOCK (qtmux);
3389 case PROP_MOVIE_TIMESCALE:
3390 g_value_set_uint (value, qtmux->timescale);
3392 case PROP_TRAK_TIMESCALE:
3393 g_value_set_uint (value, qtmux->trak_timescale);
3396 g_value_set_boolean (value, qtmux->guess_pts);
3398 case PROP_DTS_METHOD:
3399 g_value_set_enum (value, qtmux->dts_method);
3401 case PROP_FAST_START:
3402 g_value_set_boolean (value, qtmux->fast_start);
3404 case PROP_FAST_START_TEMP_FILE:
3405 g_value_set_string (value, qtmux->fast_start_file_path);
3407 case PROP_MOOV_RECOV_FILE:
3408 g_value_set_string (value, qtmux->moov_recov_file_path);
3410 case PROP_FRAGMENT_DURATION:
3411 g_value_set_uint (value, qtmux->fragment_duration);
3413 case PROP_STREAMABLE:
3414 g_value_set_boolean (value, qtmux->streamable);
3417 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3420 GST_OBJECT_UNLOCK (qtmux);
3424 gst_qt_mux_generate_fast_start_file_path (GstQTMux * qtmux)
3428 g_free (qtmux->fast_start_file_path);
3429 qtmux->fast_start_file_path = NULL;
3431 tmp = g_strdup_printf ("%s%d", "qtmux", g_random_int ());
3432 qtmux->fast_start_file_path = g_build_filename (g_get_tmp_dir (), tmp, NULL);
3437 gst_qt_mux_set_property (GObject * object,
3438 guint prop_id, const GValue * value, GParamSpec * pspec)
3440 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
3442 GST_OBJECT_LOCK (qtmux);
3444 case PROP_MOVIE_TIMESCALE:
3445 qtmux->timescale = g_value_get_uint (value);
3447 case PROP_TRAK_TIMESCALE:
3448 qtmux->trak_timescale = g_value_get_uint (value);
3451 qtmux->guess_pts = g_value_get_boolean (value);
3453 case PROP_DTS_METHOD:
3454 qtmux->dts_method = g_value_get_enum (value);
3456 case PROP_FAST_START:
3457 qtmux->fast_start = g_value_get_boolean (value);
3459 case PROP_FAST_START_TEMP_FILE:
3460 g_free (qtmux->fast_start_file_path);
3461 qtmux->fast_start_file_path = g_value_dup_string (value);
3462 /* NULL means to generate a random one */
3463 if (!qtmux->fast_start_file_path) {
3464 gst_qt_mux_generate_fast_start_file_path (qtmux);
3467 case PROP_MOOV_RECOV_FILE:
3468 g_free (qtmux->moov_recov_file_path);
3469 qtmux->moov_recov_file_path = g_value_dup_string (value);
3471 case PROP_FRAGMENT_DURATION:
3472 qtmux->fragment_duration = g_value_get_uint (value);
3474 case PROP_STREAMABLE:
3475 qtmux->streamable = g_value_get_boolean (value);
3478 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3481 GST_OBJECT_UNLOCK (qtmux);
3484 static GstStateChangeReturn
3485 gst_qt_mux_change_state (GstElement * element, GstStateChange transition)
3487 GstStateChangeReturn ret;
3488 GstQTMux *qtmux = GST_QT_MUX_CAST (element);
3490 switch (transition) {
3491 case GST_STATE_CHANGE_NULL_TO_READY:
3493 case GST_STATE_CHANGE_READY_TO_PAUSED:
3494 gst_collect_pads_start (qtmux->collect);
3495 qtmux->state = GST_QT_MUX_STATE_STARTED;
3497 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3499 case GST_STATE_CHANGE_PAUSED_TO_READY:
3500 gst_collect_pads_stop (qtmux->collect);
3506 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3508 switch (transition) {
3509 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3511 case GST_STATE_CHANGE_PAUSED_TO_READY:
3512 gst_qt_mux_reset (qtmux, TRUE);
3514 case GST_STATE_CHANGE_READY_TO_NULL:
3524 gst_qt_mux_register (GstPlugin * plugin)
3526 GTypeInfo typeinfo = {
3527 sizeof (GstQTMuxClass),
3528 (GBaseInitFunc) gst_qt_mux_base_init,
3530 (GClassInitFunc) gst_qt_mux_class_init,
3535 (GInstanceInitFunc) gst_qt_mux_init,
3537 static const GInterfaceInfo tag_setter_info = {
3540 static const GInterfaceInfo tag_xmp_writer_info = {
3544 GstQTMuxFormat format;
3545 GstQTMuxClassParams *params;
3548 GST_DEBUG_CATEGORY_INIT (gst_qt_mux_debug, "qtmux", 0, "QT Muxer");
3550 GST_LOG ("Registering muxers");
3553 GstQTMuxFormatProp *prop;
3555 prop = &gst_qt_mux_format_list[i];
3556 format = prop->format;
3557 if (format == GST_QT_MUX_FORMAT_NONE)
3560 /* create a cache for these properties */
3561 params = g_new0 (GstQTMuxClassParams, 1);
3562 params->prop = prop;
3563 params->src_caps = gst_static_caps_get (&prop->src_caps);
3564 params->video_sink_caps = gst_static_caps_get (&prop->video_sink_caps);
3565 params->audio_sink_caps = gst_static_caps_get (&prop->audio_sink_caps);
3567 /* create the type now */
3568 type = g_type_register_static (GST_TYPE_ELEMENT, prop->type_name, &typeinfo,
3570 g_type_set_qdata (type, GST_QT_MUX_PARAMS_QDATA, (gpointer) params);
3571 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
3572 g_type_add_interface_static (type, GST_TYPE_TAG_XMP_WRITER,
3573 &tag_xmp_writer_info);
3575 if (!gst_element_register (plugin, prop->name, prop->rank, type))
3581 GST_LOG ("Finished registering muxers");
3583 /* FIXME: ideally classification tag should be added and
3584 registered in gstreamer core gsttaglist
3587 GST_LOG ("Registering tags");
3589 gst_tag_register (GST_TAG_3GP_CLASSIFICATION, GST_TAG_FLAG_META,
3590 G_TYPE_STRING, GST_TAG_3GP_CLASSIFICATION, "content classification",
3591 gst_tag_merge_use_first);
3593 GST_LOG ("Finished registering tags");