3 * Copyright (c) 2008,2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * Copyright (c) 2008-2017 Collabora Ltd
5 * @author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * @author: Vincent Penquerc'h <vincent.penquerch@collabora.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:element-flvmux
27 * flvmux muxes different streams into an FLV file.
30 * <title>Example launch line</title>
32 * gst-launch-1.0 -v flvmux name=mux ! filesink location=test.flv audiotestsrc samplesperbuffer=44100 num-buffers=10 ! faac ! mux. videotestsrc num-buffers=250 ! video/x-raw,framerate=25/1 ! x264enc ! mux.
33 * ]| This pipeline encodes a test audio and video stream and muxes both into an FLV file.
44 #include <gst/audio/audio.h>
46 #include "gstflvmux.h"
49 GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
50 #define GST_CAT_DEFAULT flvmux_debug
59 #define DEFAULT_STREAMABLE FALSE
60 #define MAX_INDEX_ENTRIES 128
61 #define DEFAULT_METADATACREATOR "GStreamer " PACKAGE_VERSION " FLV muxer"
63 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
66 GST_STATIC_CAPS ("video/x-flv")
69 static GstStaticPadTemplate videosink_templ = GST_STATIC_PAD_TEMPLATE ("video",
72 GST_STATIC_CAPS ("video/x-flash-video; "
73 "video/x-flash-screen; "
74 "video/x-vp6-flash; " "video/x-vp6-alpha; "
75 "video/x-h264, stream-format=avc;")
78 static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
82 ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
83 "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }, parsed = (boolean) TRUE; "
84 "audio/mpeg, mpegversion = (int) { 4, 2 }, stream-format = (string) raw; "
85 "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
86 "audio/x-raw, format = (string) { U8, S16LE}, layout = (string) interleaved, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
87 "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
88 "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
89 "audio/x-speex, channels = (int) 1, rate = (int) 16000;")
92 G_DEFINE_TYPE (GstFlvMuxPad, gst_flv_mux_pad, GST_TYPE_AGGREGATOR_PAD);
94 #define gst_flv_mux_parent_class parent_class
95 G_DEFINE_TYPE_WITH_CODE (GstFlvMux, gst_flv_mux, GST_TYPE_AGGREGATOR,
96 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
99 gst_flv_mux_aggregate (GstAggregator * aggregator, gboolean timeout);
101 gst_flv_mux_sink_event (GstAggregator * aggregator, GstAggregatorPad * pad,
104 static GstAggregatorPad *gst_flv_mux_create_new_pad (GstAggregator * agg,
105 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps);
106 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
108 static gboolean gst_flv_mux_video_pad_setcaps (GstFlvMuxPad * pad,
110 static gboolean gst_flv_mux_audio_pad_setcaps (GstFlvMuxPad * pad,
113 static void gst_flv_mux_get_property (GObject * object,
114 guint prop_id, GValue * value, GParamSpec * pspec);
115 static void gst_flv_mux_set_property (GObject * object,
116 guint prop_id, const GValue * value, GParamSpec * pspec);
117 static void gst_flv_mux_finalize (GObject * object);
119 static void gst_flv_mux_reset (GstElement * element);
120 static void gst_flv_mux_reset_pad (GstFlvMuxPad * pad);
122 static void gst_flv_mux_pad_finalize (GObject * object);
124 static gboolean gst_flv_mux_start (GstAggregator * aggregator);
125 static GstFlowReturn gst_flv_mux_flush (GstAggregator * aggregator);
126 static GstClockTime gst_flv_mux_get_next_time (GstAggregator * aggregator);
127 static GstFlowReturn gst_flv_mux_write_eos (GstFlvMux * mux);
128 static GstFlowReturn gst_flv_mux_write_header (GstFlvMux * mux);
129 static GstFlowReturn gst_flv_mux_rewrite_header (GstFlvMux * mux);
130 static gboolean gst_flv_mux_are_all_pads_eos (GstFlvMux * mux);
134 gst_flv_mux_pad_flush (GstAggregatorPad * pad, GstAggregator * aggregator)
136 GstFlvMuxPad *flvpad = GST_FLV_MUX_PAD (pad);
138 flvpad->last_timestamp = 0;
139 flvpad->pts = GST_CLOCK_STIME_NONE;
140 flvpad->dts = GST_CLOCK_STIME_NONE;
146 gst_flv_mux_pad_class_init (GstFlvMuxPadClass * klass)
148 GstAggregatorPadClass *aggregatorpad_class = (GstAggregatorPadClass *) klass;
149 GObjectClass *gobject_class = (GObjectClass *) klass;
151 gobject_class->finalize = gst_flv_mux_pad_finalize;
153 aggregatorpad_class->flush = GST_DEBUG_FUNCPTR (gst_flv_mux_pad_flush);
157 gst_flv_mux_pad_init (GstFlvMuxPad * pad)
159 gst_flv_mux_reset_pad (pad);
166 } GstFlvMuxIndexEntry;
169 gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
171 g_slice_free (GstFlvMuxIndexEntry, entry);
175 _gst_buffer_new_wrapped (gpointer mem, gsize size, GFreeFunc free_func)
179 buf = gst_buffer_new ();
180 gst_buffer_append_memory (buf,
181 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
182 mem, size, 0, size, mem, free_func));
188 _gst_buffer_new_and_alloc (gsize size, GstBuffer ** buffer, guint8 ** data)
190 g_return_if_fail (data != NULL);
191 g_return_if_fail (buffer != NULL);
193 *data = g_malloc (size);
194 *buffer = _gst_buffer_new_wrapped (*data, size, g_free);
198 gst_flv_mux_class_init (GstFlvMuxClass * klass)
200 GObjectClass *gobject_class;
201 GstElementClass *gstelement_class;
202 GstAggregatorClass *gstaggregator_class;
204 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
206 gobject_class = (GObjectClass *) klass;
207 gstelement_class = (GstElementClass *) klass;
208 gstaggregator_class = (GstAggregatorClass *) klass;
210 gobject_class->get_property = gst_flv_mux_get_property;
211 gobject_class->set_property = gst_flv_mux_set_property;
212 gobject_class->finalize = gst_flv_mux_finalize;
214 /* FIXME: ideally the right mode of operation should be detected
215 * automatically using queries when parameter not specified. */
217 * GstFlvMux:streamable
219 * If True, the output will be streaming friendly. (ie without indexes and
222 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
223 g_param_spec_boolean ("streamable", "streamable",
224 "If set to true, the output should be as if it is to be streamed "
225 "and hence no indexes written or duration written.",
226 DEFAULT_STREAMABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_METADATACREATOR,
228 g_param_spec_string ("metadatacreator", "metadatacreator",
229 "The value of metadatacreator in the meta packet.",
230 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 gstaggregator_class->create_new_pad =
233 GST_DEBUG_FUNCPTR (gst_flv_mux_create_new_pad);
234 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
236 gstaggregator_class->start = GST_DEBUG_FUNCPTR (gst_flv_mux_start);
237 gstaggregator_class->aggregate = GST_DEBUG_FUNCPTR (gst_flv_mux_aggregate);
238 gstaggregator_class->sink_event = GST_DEBUG_FUNCPTR (gst_flv_mux_sink_event);
239 gstaggregator_class->flush = GST_DEBUG_FUNCPTR (gst_flv_mux_flush);
240 gstaggregator_class->get_next_time =
241 GST_DEBUG_FUNCPTR (gst_flv_mux_get_next_time);
243 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
244 &videosink_templ, GST_TYPE_FLV_MUX_PAD);
245 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
246 &audiosink_templ, GST_TYPE_FLV_MUX_PAD);
247 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
248 &src_templ, GST_TYPE_AGGREGATOR_PAD);
249 gst_element_class_set_static_metadata (gstelement_class, "FLV muxer",
251 "Muxes video/audio streams into a FLV stream",
252 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
254 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
258 gst_flv_mux_init (GstFlvMux * mux)
260 mux->srcpad = GST_AGGREGATOR_CAST (mux)->srcpad;
263 mux->streamable = DEFAULT_STREAMABLE;
264 mux->metadatacreator = g_strdup (DEFAULT_METADATACREATOR);
266 mux->new_tags = FALSE;
268 gst_flv_mux_reset (GST_ELEMENT (mux));
272 gst_flv_mux_finalize (GObject * object)
274 GstFlvMux *mux = GST_FLV_MUX (object);
276 gst_flv_mux_reset (GST_ELEMENT (object));
277 g_free (mux->metadatacreator);
279 G_OBJECT_CLASS (gst_flv_mux_parent_class)->finalize (object);
283 gst_flv_mux_pad_finalize (GObject * object)
285 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (object);
287 gst_flv_mux_reset_pad (pad);
289 G_OBJECT_CLASS (gst_flv_mux_pad_parent_class)->finalize (object);
293 gst_flv_mux_flush (GstAggregator * aggregator)
295 /* TODO: What is the right behaviour on flush? Should we just ignore it ?
296 * This still needs to be defined. */
298 gst_flv_mux_reset (GST_ELEMENT (aggregator));
303 gst_flv_mux_start (GstAggregator * aggregator)
305 gst_flv_mux_reset (GST_ELEMENT (aggregator));
310 gst_flv_mux_reset (GstElement * element)
312 GstFlvMux *mux = GST_FLV_MUX (element);
314 g_list_foreach (mux->index, (GFunc) gst_flv_mux_index_entry_free, NULL);
315 g_list_free (mux->index);
319 mux->duration = GST_CLOCK_TIME_NONE;
320 mux->new_tags = FALSE;
321 mux->first_timestamp = GST_CLOCK_STIME_NONE;
323 mux->state = GST_FLV_MUX_STATE_HEADER;
326 gst_tag_setter_reset_tags (GST_TAG_SETTER (mux));
329 /* Extract per-codec relevant tags for
330 * insertion into the metadata later - ie bitrate,
331 * but maybe others in the future */
333 gst_flv_mux_store_codec_tags (GstFlvMux * mux,
334 GstFlvMuxPad * flvpad, GstTagList * list)
336 /* Look for a bitrate as either nominal or actual bitrate tag */
337 if (gst_tag_list_get_uint (list, GST_TAG_NOMINAL_BITRATE, &flvpad->bitrate)
338 || gst_tag_list_get_uint (list, GST_TAG_BITRATE, &flvpad->bitrate)) {
339 GST_DEBUG_OBJECT (mux, "Stored bitrate for pad %" GST_PTR_FORMAT " = %u",
340 flvpad, flvpad->bitrate);
345 gst_flv_mux_sink_event (GstAggregator * aggregator, GstAggregatorPad * pad,
348 GstFlvMux *mux = GST_FLV_MUX (aggregator);
349 GstFlvMuxPad *flvpad = (GstFlvMuxPad *) pad;
352 switch (GST_EVENT_TYPE (event)) {
357 gst_event_parse_caps (event, &caps);
359 if (mux->video_pad == flvpad) {
360 ret = gst_flv_mux_video_pad_setcaps (flvpad, caps);
361 } else if (mux->audio_pad == flvpad) {
362 ret = gst_flv_mux_audio_pad_setcaps (flvpad, caps);
364 g_assert_not_reached ();
370 GstTagSetter *setter = GST_TAG_SETTER (mux);
371 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
373 gst_event_parse_tag (event, &list);
374 gst_tag_setter_merge_tags (setter, list, mode);
375 gst_flv_mux_store_codec_tags (mux, flvpad, list);
376 mux->new_tags = TRUE;
387 return GST_AGGREGATOR_CLASS (parent_class)->sink_event (aggregator, pad,
392 gst_flv_mux_video_pad_setcaps (GstFlvMuxPad * pad, GstCaps * caps)
394 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
398 s = gst_caps_get_structure (caps, 0);
400 if (strcmp (gst_structure_get_name (s), "video/x-flash-video") == 0) {
402 } else if (strcmp (gst_structure_get_name (s), "video/x-flash-screen") == 0) {
404 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-flash") == 0) {
406 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-alpha") == 0) {
408 } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
414 if (ret && gst_structure_has_field (s, "codec_data")) {
415 const GValue *val = gst_structure_get_value (s, "codec_data");
418 pad->codec_data = gst_buffer_ref (gst_value_get_buffer (val));
421 gst_object_unref (mux);
427 gst_flv_mux_audio_pad_setcaps (GstFlvMuxPad * pad, GstCaps * caps)
429 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
433 s = gst_caps_get_structure (caps, 0);
435 if (strcmp (gst_structure_get_name (s), "audio/x-adpcm") == 0) {
436 const gchar *layout = gst_structure_get_string (s, "layout");
437 if (layout && strcmp (layout, "swf") == 0) {
442 } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
445 if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
446 if (mpegversion == 1) {
449 if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
452 if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
459 } else if (mpegversion == 4 || mpegversion == 2) {
467 } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
470 if (gst_structure_get_int (s, "rate", &rate)
471 && gst_structure_get_int (s, "channels", &channels)) {
472 if (channels == 1 && rate == 16000)
474 else if (channels == 1 && rate == 8000)
481 } else if (strcmp (gst_structure_get_name (s), "audio/x-raw") == 0) {
484 if (gst_audio_info_from_caps (&info, caps)) {
487 if (GST_AUDIO_INFO_WIDTH (&info) == 8)
489 else if (GST_AUDIO_INFO_WIDTH (&info) == 16)
495 } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
497 } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
499 } else if (strcmp (gst_structure_get_name (s), "audio/x-speex") == 0) {
508 if (gst_structure_get_int (s, "rate", &rate)) {
509 if (pad->codec == 10)
511 else if (rate == 5512)
513 else if (rate == 11025)
515 else if (rate == 22050)
517 else if (rate == 44100)
519 else if (rate == 8000 && (pad->codec == 5 || pad->codec == 14))
521 else if (rate == 16000 && (pad->codec == 4 || pad->codec == 11))
525 } else if (pad->codec == 10) {
531 if (gst_structure_get_int (s, "channels", &channels)) {
532 if (pad->codec == 4 || pad->codec == 5
533 || pad->codec == 6 || pad->codec == 11)
535 else if (pad->codec == 10)
537 else if (channels == 1)
539 else if (channels == 2)
543 } else if (pad->codec == 4 || pad->codec == 5 || pad->codec == 6) {
545 } else if (pad->codec == 10) {
555 if (ret && gst_structure_has_field (s, "codec_data")) {
556 const GValue *val = gst_structure_get_value (s, "codec_data");
559 pad->codec_data = gst_buffer_ref (gst_value_get_buffer (val));
562 gst_object_unref (mux);
568 gst_flv_mux_reset_pad (GstFlvMuxPad * pad)
570 GST_DEBUG_OBJECT (pad, "resetting pad");
573 gst_buffer_unref (pad->codec_data);
574 pad->codec_data = NULL;
575 pad->codec = G_MAXUINT;
576 pad->rate = G_MAXUINT;
577 pad->width = G_MAXUINT;
578 pad->channels = G_MAXUINT;
580 gst_flv_mux_pad_flush (GST_AGGREGATOR_PAD_CAST (pad), NULL);
583 static GstAggregatorPad *
584 gst_flv_mux_create_new_pad (GstAggregator * agg,
585 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
587 GstElementClass *klass = GST_ELEMENT_GET_CLASS (agg);
588 GstAggregatorPad *aggpad;
589 GstFlvMux *mux = GST_FLV_MUX (agg);
590 GstFlvMuxPad *pad = NULL;
591 const gchar *name = NULL;
594 if (mux->state != GST_FLV_MUX_STATE_HEADER) {
595 GST_WARNING_OBJECT (mux, "Can't request pads after writing header");
599 if (templ == gst_element_class_get_pad_template (klass, "audio")) {
600 if (mux->audio_pad) {
601 GST_WARNING_OBJECT (mux, "Already have an audio pad");
606 } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
607 if (mux->video_pad) {
608 GST_WARNING_OBJECT (mux, "Already have a video pad");
614 GST_WARNING_OBJECT (mux, "Invalid template");
619 GST_AGGREGATOR_CLASS (gst_flv_mux_parent_class)->create_new_pad (agg,
624 pad = GST_FLV_MUX_PAD (aggpad);
626 gst_flv_mux_reset_pad (pad);
629 mux->video_pad = pad;
631 mux->audio_pad = pad;
637 gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
639 GstFlvMux *mux = GST_FLV_MUX (element);
640 GstFlvMuxPad *flvpad = GST_FLV_MUX_PAD (pad);
642 gst_pad_set_active (pad, FALSE);
643 gst_flv_mux_reset_pad (flvpad);
645 if (flvpad == mux->video_pad) {
646 mux->video_pad = NULL;
647 } else if (flvpad == mux->audio_pad) {
648 mux->audio_pad = NULL;
650 GST_WARNING_OBJECT (pad, "Pad is not known audio or video pad");
653 gst_element_remove_pad (element, pad);
657 gst_flv_mux_push (GstFlvMux * mux, GstBuffer * buffer)
659 /* pushing the buffer that rewrites the header will make it no longer be the
660 * total output size in bytes, but it doesn't matter at that point */
661 mux->byte_count += gst_buffer_get_size (buffer);
663 return gst_aggregator_finish_buffer (GST_AGGREGATOR_CAST (mux), buffer);
667 gst_flv_mux_create_header (GstFlvMux * mux)
674 _gst_buffer_new_and_alloc (9 + 4, &header, &data);
679 data[3] = 0x01; /* Version */
681 have_audio = (mux->audio_pad && mux->audio_pad->codec != G_MAXUINT);
682 have_video = (mux->video_pad && mux->video_pad->codec != G_MAXUINT);
684 data[4] = (have_audio << 2) | have_video; /* flags */
685 GST_WRITE_UINT32_BE (data + 5, 9); /* data offset */
686 GST_WRITE_UINT32_BE (data + 9, 0); /* previous tag size */
692 gst_flv_mux_preallocate_index (GstFlvMux * mux)
696 gint preallocate_size;
698 /* preallocate index of size:
699 * - 'keyframes' ECMA array key: 2 + 9 = 11 bytes
700 * - nested ECMA array header, length and end marker: 8 bytes
701 * - 'times' and 'filepositions' keys: 22 bytes
702 * - two strict arrays headers and lengths: 10 bytes
703 * - each index entry: 18 bytes
705 preallocate_size = 11 + 8 + 22 + 10 + MAX_INDEX_ENTRIES * 18;
706 GST_DEBUG_OBJECT (mux, "preallocating %d bytes for the index",
709 _gst_buffer_new_and_alloc (preallocate_size, &tmp, &data);
711 /* prefill the space with a gstfiller: <spaces> script tag variable */
712 GST_WRITE_UINT16_BE (data, 9); /* 9 characters */
713 memcpy (data + 2, "gstfiller", 9);
714 GST_WRITE_UINT8 (data + 11, AMF0_STRING_MARKER); /* a string value */
715 GST_WRITE_UINT16_BE (data + 12, preallocate_size - 14);
716 memset (data + 14, ' ', preallocate_size - 14); /* the rest is spaces */
721 gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
725 gsize len = strlen (name);
727 _gst_buffer_new_and_alloc (2 + len + 1 + 8, &tmp, &data);
729 GST_WRITE_UINT16_BE (data, len);
730 data += 2; /* name length */
731 memcpy (data, name, len);
733 *data++ = AMF0_NUMBER_MARKER; /* double type */
734 GST_WRITE_DOUBLE_BE (data, value);
740 gst_flv_mux_create_metadata (GstFlvMux * mux, gboolean full)
742 const GstTagList *tags;
743 GstBuffer *script_tag, *tmp;
746 gint i, n_tags, tags_written = 0;
748 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (mux));
750 GST_DEBUG_OBJECT (mux, "tags = %" GST_PTR_FORMAT, tags);
752 /* FIXME perhaps some bytewriter'ing here ... */
754 _gst_buffer_new_and_alloc (11, &script_tag, &data);
758 /* Data size, unknown for now */
764 data[4] = data[5] = data[6] = data[7] = 0;
767 data[8] = data[9] = data[10] = 0;
769 _gst_buffer_new_and_alloc (13, &tmp, &data);
770 data[0] = AMF0_STRING_MARKER; /* string */
772 data[2] = 10; /* length 10 */
773 memcpy (&data[3], "onMetaData", 10);
775 script_tag = gst_buffer_append (script_tag, tmp);
777 n_tags = (tags) ? gst_tag_list_n_tags (tags) : 0;
778 _gst_buffer_new_and_alloc (5, &tmp, &data);
779 data[0] = 8; /* ECMA array */
780 GST_WRITE_UINT32_BE (data + 1, n_tags);
781 script_tag = gst_buffer_append (script_tag, tmp);
786 /* Some players expect the 'duration' to be always set. Fill it out later,
787 after querying the pads or after getting EOS */
788 if (!mux->streamable) {
789 tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
790 script_tag = gst_buffer_append (script_tag, tmp);
793 /* Sometimes the information about the total file size is useful for the
794 player. It will be filled later, after getting EOS */
795 tmp = gst_flv_mux_create_number_script_value ("filesize", 0);
796 script_tag = gst_buffer_append (script_tag, tmp);
799 /* Preallocate space for the index to be written at EOS */
800 tmp = gst_flv_mux_preallocate_index (mux);
801 script_tag = gst_buffer_append (script_tag, tmp);
803 GST_DEBUG_OBJECT (mux, "not preallocating index, streamable mode");
807 for (i = 0; tags && i < n_tags; i++) {
808 const gchar *tag_name = gst_tag_list_nth_tag_name (tags, i);
809 if (!strcmp (tag_name, GST_TAG_DURATION)) {
812 if (!gst_tag_list_get_uint64 (tags, GST_TAG_DURATION, &dur))
815 } else if (!strcmp (tag_name, GST_TAG_ARTIST) ||
816 !strcmp (tag_name, GST_TAG_TITLE)) {
818 const gchar *t = NULL;
820 if (!strcmp (tag_name, GST_TAG_ARTIST))
822 else if (!strcmp (tag_name, GST_TAG_TITLE))
825 if (!gst_tag_list_get_string (tags, tag_name, &s))
828 _gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s),
830 data[0] = 0; /* tag name length */
831 data[1] = strlen (t);
832 memcpy (&data[2], t, strlen (t));
833 data[2 + strlen (t)] = 2; /* string */
834 data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
835 data[4 + strlen (t)] = (strlen (s)) & 0xff;
836 memcpy (&data[5 + strlen (t)], s, strlen (s));
837 script_tag = gst_buffer_append (script_tag, tmp);
847 if (mux->duration == GST_CLOCK_TIME_NONE) {
851 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l; l = l->next) {
852 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
854 if (gst_pad_peer_query_duration (GST_PAD (pad), GST_FORMAT_TIME,
855 (gint64 *) & dur) && dur != GST_CLOCK_TIME_NONE) {
856 if (mux->duration == GST_CLOCK_TIME_NONE)
859 mux->duration = MAX (dur, mux->duration);
864 if (!mux->streamable && mux->duration != GST_CLOCK_TIME_NONE) {
868 d = gst_guint64_to_gdouble (mux->duration);
869 d /= (gdouble) GST_SECOND;
871 GST_DEBUG_OBJECT (mux, "determined the duration to be %f", d);
872 gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
873 GST_WRITE_DOUBLE_BE (map.data + 29 + 2 + 8 + 1, d);
874 gst_buffer_unmap (script_tag, &map);
877 if (mux->video_pad && mux->video_pad->codec != G_MAXUINT) {
878 GstCaps *caps = NULL;
881 caps = gst_pad_get_current_caps (GST_PAD (mux->video_pad));
888 GST_DEBUG_OBJECT (mux, "putting videocodecid %d in the metadata",
889 mux->video_pad->codec);
891 tmp = gst_flv_mux_create_number_script_value ("videocodecid",
892 mux->video_pad->codec);
893 script_tag = gst_buffer_append (script_tag, tmp);
896 s = gst_caps_get_structure (caps, 0);
897 gst_caps_unref (caps);
899 if (gst_structure_get_int (s, "width", &size)) {
900 GST_DEBUG_OBJECT (mux, "putting width %d in the metadata", size);
902 tmp = gst_flv_mux_create_number_script_value ("width", size);
903 script_tag = gst_buffer_append (script_tag, tmp);
907 if (gst_structure_get_int (s, "height", &size)) {
908 GST_DEBUG_OBJECT (mux, "putting height %d in the metadata", size);
910 tmp = gst_flv_mux_create_number_script_value ("height", size);
911 script_tag = gst_buffer_append (script_tag, tmp);
915 if (gst_structure_get_fraction (s, "pixel-aspect-ratio", &num, &den)) {
919 GST_DEBUG_OBJECT (mux, "putting AspectRatioX %f in the metadata", d);
921 tmp = gst_flv_mux_create_number_script_value ("AspectRatioX", d);
922 script_tag = gst_buffer_append (script_tag, tmp);
926 GST_DEBUG_OBJECT (mux, "putting AspectRatioY %f in the metadata", d);
928 tmp = gst_flv_mux_create_number_script_value ("AspectRatioY", d);
929 script_tag = gst_buffer_append (script_tag, tmp);
933 if (gst_structure_get_fraction (s, "framerate", &num, &den)) {
936 gst_util_fraction_to_double (num, den, &d);
937 GST_DEBUG_OBJECT (mux, "putting framerate %f in the metadata", d);
939 tmp = gst_flv_mux_create_number_script_value ("framerate", d);
940 script_tag = gst_buffer_append (script_tag, tmp);
944 GST_DEBUG_OBJECT (mux, "putting videodatarate %u KB/s in the metadata",
945 mux->video_pad->bitrate / 1024);
946 tmp = gst_flv_mux_create_number_script_value ("videodatarate",
947 mux->video_pad->bitrate / 1024);
948 script_tag = gst_buffer_append (script_tag, tmp);
953 if (mux->audio_pad && mux->audio_pad->codec != G_MAXUINT) {
954 GST_DEBUG_OBJECT (mux, "putting audiocodecid %d in the metadata",
955 mux->audio_pad->codec);
957 tmp = gst_flv_mux_create_number_script_value ("audiocodecid",
958 mux->audio_pad->codec);
959 script_tag = gst_buffer_append (script_tag, tmp);
962 GST_DEBUG_OBJECT (mux, "putting audiodatarate %u KB/s in the metadata",
963 mux->audio_pad->bitrate / 1024);
964 tmp = gst_flv_mux_create_number_script_value ("audiodatarate",
965 mux->audio_pad->bitrate / 1024);
966 script_tag = gst_buffer_append (script_tag, tmp);
970 _gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (mux->metadatacreator),
972 data[0] = 0; /* 15 bytes name */
974 memcpy (&data[2], "metadatacreator", 15);
975 data[17] = 2; /* string */
976 data[18] = (strlen (mux->metadatacreator) >> 8) & 0xff;
977 data[19] = (strlen (mux->metadatacreator)) & 0xff;
978 memcpy (&data[20], mux->metadatacreator, strlen (mux->metadatacreator));
979 script_tag = gst_buffer_append (script_tag, tmp);
984 GTimeVal tv = { 0, };
988 static const gchar *weekdays[] = {
989 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
991 static const gchar *months[] = {
992 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
993 "Aug", "Sep", "Oct", "Nov", "Dec"
996 g_get_current_time (&tv);
1000 s = g_strdup_printf ("%s %s %d %02d:%02d:%02d %d", weekdays[tm->tm_wday],
1001 months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
1002 tm->tm_year + 1900);
1004 _gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s), &tmp, &data);
1005 data[0] = 0; /* 12 bytes name */
1007 memcpy (&data[2], "creationdate", 12);
1008 data[14] = 2; /* string */
1009 data[15] = (strlen (s) >> 8) & 0xff;
1010 data[16] = (strlen (s)) & 0xff;
1011 memcpy (&data[17], s, strlen (s));
1012 script_tag = gst_buffer_append (script_tag, tmp);
1020 if (!tags_written) {
1021 gst_buffer_unref (script_tag);
1026 _gst_buffer_new_and_alloc (2 + 0 + 1, &tmp, &data);
1027 data[0] = 0; /* 0 byte size */
1029 data[2] = 9; /* end marker */
1030 script_tag = gst_buffer_append (script_tag, tmp);
1033 _gst_buffer_new_and_alloc (4, &tmp, &data);
1034 GST_WRITE_UINT32_BE (data, gst_buffer_get_size (script_tag));
1035 script_tag = gst_buffer_append (script_tag, tmp);
1037 gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
1038 map.data[1] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 16) & 0xff;
1039 map.data[2] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 8) & 0xff;
1040 map.data[3] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 0) & 0xff;
1042 GST_WRITE_UINT32_BE (map.data + 11 + 13 + 1, tags_written);
1043 gst_buffer_unmap (script_tag, &map);
1050 gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
1051 GstFlvMuxPad * pad, gboolean is_codec_data)
1056 guint32 pts, dts, cts;
1057 guint8 *data, *bdata = NULL;
1060 if (!GST_CLOCK_STIME_IS_VALID (pad->dts)) {
1061 pts = dts = pad->last_timestamp / GST_MSECOND;
1063 pts = pad->pts / GST_MSECOND;
1064 dts = pad->dts / GST_MSECOND;
1067 /* Be safe in case TS are buggy */
1073 /* Timestamp must start at zero */
1074 if (GST_CLOCK_STIME_IS_VALID (mux->first_timestamp)) {
1075 dts -= mux->first_timestamp / GST_MSECOND;
1079 GST_LOG_OBJECT (mux, "got pts %i dts %i cts %i", pts, dts, cts);
1081 if (buffer != NULL) {
1082 gst_buffer_map (buffer, &map, GST_MAP_READ);
1088 if (mux->video_pad == pad) {
1090 if (pad->codec == 7)
1096 if (pad->codec == 10)
1103 _gst_buffer_new_and_alloc (size, &tag, &data);
1104 memset (data, 0, size);
1106 data[0] = (mux->video_pad == pad) ? 9 : 8;
1108 data[1] = ((size - 11 - 4) >> 16) & 0xff;
1109 data[2] = ((size - 11 - 4) >> 8) & 0xff;
1110 data[3] = ((size - 11 - 4) >> 0) & 0xff;
1112 GST_WRITE_UINT24_BE (data + 4, dts);
1113 data[7] = (((guint) dts) >> 24) & 0xff;
1115 data[8] = data[9] = data[10] = 0;
1117 if (mux->video_pad == pad) {
1118 if (buffer && GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1123 data[11] |= pad->codec & 0x0f;
1125 if (pad->codec == 7) {
1126 if (is_codec_data) {
1128 GST_WRITE_UINT24_BE (data + 13, 0);
1129 } else if (bsize == 0) {
1130 /* AVC end of sequence */
1132 GST_WRITE_UINT24_BE (data + 13, 0);
1136 GST_WRITE_UINT24_BE (data + 13, cts);
1138 memcpy (data + 11 + 1 + 4, bdata, bsize);
1140 memcpy (data + 11 + 1, bdata, bsize);
1143 data[11] |= (pad->codec << 4) & 0xf0;
1144 data[11] |= (pad->rate << 2) & 0x0c;
1145 data[11] |= (pad->width << 1) & 0x02;
1146 data[11] |= (pad->channels << 0) & 0x01;
1148 GST_DEBUG_OBJECT (mux, "Creating byte %02x with "
1149 "codec:%d, rate:%d, width:%d, channels:%d",
1150 data[11], pad->codec, pad->rate, pad->width, pad->channels);
1152 if (pad->codec == 10) {
1153 data[12] = is_codec_data ? 0 : 1;
1155 memcpy (data + 11 + 1 + 1, bdata, bsize);
1157 memcpy (data + 11 + 1, bdata, bsize);
1162 gst_buffer_unmap (buffer, &map);
1164 GST_WRITE_UINT32_BE (data + size - 4, size - 4);
1166 GST_BUFFER_PTS (tag) = GST_CLOCK_TIME_NONE;
1167 GST_BUFFER_DTS (tag) = GST_CLOCK_TIME_NONE;
1168 GST_BUFFER_DURATION (tag) = GST_CLOCK_TIME_NONE;
1171 /* if we are streamable we copy over timestamps and offsets,
1172 if not just copy the offsets */
1173 if (mux->streamable) {
1174 GstClockTime timestamp = GST_CLOCK_TIME_NONE;
1176 if (gst_segment_to_running_time_full (&GST_AGGREGATOR_PAD (pad)->segment,
1177 GST_FORMAT_TIME, GST_BUFFER_DTS_OR_PTS (buffer),
1179 GST_BUFFER_PTS (tag) = timestamp;
1180 GST_BUFFER_DURATION (tag) = GST_BUFFER_DURATION (buffer);
1182 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_NONE;
1183 GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_NONE;
1185 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET (buffer);
1186 GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_END (buffer);
1189 /* mark the buffer if it's an audio buffer and there's also video being muxed
1190 * or it's a video interframe */
1191 if (mux->video_pad == pad &&
1192 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1193 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1195 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1196 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
1197 GST_BUFFER_OFFSET_NONE;
1203 static inline GstBuffer *
1204 gst_flv_mux_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1207 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, pad, FALSE);
1210 static inline GstBuffer *
1211 gst_flv_mux_codec_data_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1214 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, pad, TRUE);
1217 static inline GstBuffer *
1218 gst_flv_mux_eos_to_tag (GstFlvMux * mux, GstFlvMuxPad * pad)
1220 return gst_flv_mux_buffer_to_tag_internal (mux, NULL, pad, FALSE);
1224 gst_flv_mux_put_buffer_in_streamheader (GValue * streamheader,
1227 GValue value = { 0 };
1230 g_value_init (&value, GST_TYPE_BUFFER);
1231 buf = gst_buffer_copy (buffer);
1232 gst_value_set_buffer (&value, buf);
1233 gst_buffer_unref (buf);
1234 gst_value_array_append_value (streamheader, &value);
1235 g_value_unset (&value);
1238 static GstFlowReturn
1239 gst_flv_mux_write_header (GstFlvMux * mux)
1241 GstBuffer *header, *metadata;
1242 GstBuffer *video_codec_data, *audio_codec_data;
1244 GstStructure *structure;
1245 GValue streamheader = { 0 };
1249 /* if not streaming, check if downstream is seekable */
1250 if (!mux->streamable) {
1254 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1255 if (gst_pad_peer_query (mux->srcpad, query)) {
1256 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1257 GST_INFO_OBJECT (mux, "downstream is %sseekable", seekable ? "" : "not ");
1259 /* have to assume seeking is supported if query not handled downstream */
1260 GST_WARNING_OBJECT (mux, "downstream did not handle seeking query");
1264 mux->streamable = TRUE;
1265 g_object_notify (G_OBJECT (mux), "streamable");
1266 GST_WARNING_OBJECT (mux, "downstream is not seekable, but "
1267 "streamable=false. Will ignore that and create streamable output "
1270 gst_query_unref (query);
1273 header = gst_flv_mux_create_header (mux);
1274 metadata = gst_flv_mux_create_metadata (mux, TRUE);
1275 video_codec_data = NULL;
1276 audio_codec_data = NULL;
1278 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l != NULL; l = l->next) {
1279 GstFlvMuxPad *pad = l->data;
1281 /* Get H.264 and AAC codec data, if present */
1282 if (pad && mux->video_pad == pad && pad->codec == 7) {
1283 if (pad->codec_data == NULL)
1284 GST_WARNING_OBJECT (mux, "Codec data for video stream not found, "
1285 "output might not be playable");
1288 gst_flv_mux_codec_data_buffer_to_tag (mux, pad->codec_data, pad);
1289 } else if (pad && mux->audio_pad == pad && pad->codec == 10) {
1290 if (pad->codec_data == NULL)
1291 GST_WARNING_OBJECT (mux, "Codec data for audio stream not found, "
1292 "output might not be playable");
1295 gst_flv_mux_codec_data_buffer_to_tag (mux, pad->codec_data, pad);
1299 /* mark buffers that will go in the streamheader */
1300 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_HEADER);
1301 GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_HEADER);
1302 if (video_codec_data != NULL) {
1303 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_HEADER);
1304 /* mark as a delta unit, so downstream will not try to synchronize on that
1305 * buffer - to actually start playback you need a real video keyframe */
1306 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_DELTA_UNIT);
1308 if (audio_codec_data != NULL) {
1309 GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_HEADER);
1312 /* put buffers in streamheader */
1313 g_value_init (&streamheader, GST_TYPE_ARRAY);
1314 gst_flv_mux_put_buffer_in_streamheader (&streamheader, header);
1315 gst_flv_mux_put_buffer_in_streamheader (&streamheader, metadata);
1316 if (video_codec_data != NULL)
1317 gst_flv_mux_put_buffer_in_streamheader (&streamheader, video_codec_data);
1318 if (audio_codec_data != NULL)
1319 gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);
1321 /* create the caps and put the streamheader in them */
1322 caps = gst_caps_new_empty_simple ("video/x-flv");
1323 structure = gst_caps_get_structure (caps, 0);
1324 gst_structure_set_value (structure, "streamheader", &streamheader);
1325 g_value_unset (&streamheader);
1327 gst_aggregator_set_src_caps (GST_AGGREGATOR_CAST (mux), caps);
1329 gst_caps_unref (caps);
1331 /* push the header buffer, the metadata and the codec info, if any */
1332 ret = gst_flv_mux_push (mux, header);
1333 if (ret != GST_FLOW_OK)
1334 goto failure_header;
1335 ret = gst_flv_mux_push (mux, metadata);
1336 if (ret != GST_FLOW_OK)
1337 goto failure_metadata;
1338 if (video_codec_data != NULL) {
1339 ret = gst_flv_mux_push (mux, video_codec_data);
1340 if (ret != GST_FLOW_OK)
1341 goto failure_video_codec_data;
1343 if (audio_codec_data != NULL) {
1344 ret = gst_flv_mux_push (mux, audio_codec_data);
1345 if (ret != GST_FLOW_OK)
1346 goto failure_audio_codec_data;
1351 gst_buffer_unref (metadata);
1354 if (video_codec_data != NULL)
1355 gst_buffer_unref (video_codec_data);
1357 failure_video_codec_data:
1358 if (audio_codec_data != NULL)
1359 gst_buffer_unref (audio_codec_data);
1361 failure_audio_codec_data:
1366 gst_flv_mux_segment_to_running_time (const GstSegment * segment, GstClockTime t)
1368 /* we can get a dts before the segment, if dts < pts and pts is inside
1369 * the segment, so we consider early times as 0 */
1370 if (t < segment->start)
1372 return gst_segment_to_running_time (segment, GST_FORMAT_TIME, t);
1376 gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer,
1380 * Add the tag byte offset and to the index if it's a valid seek point, which
1381 * means it's either a video keyframe or if there is no video pad (in that
1382 * case every FLV tag is a valid seek point)
1384 if (mux->video_pad == pad &&
1385 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1388 if (GST_BUFFER_PTS_IS_VALID (buffer)) {
1389 GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
1391 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1392 (pad)->segment, GST_BUFFER_PTS (buffer));
1393 entry->position = mux->byte_count;
1394 entry->time = gst_guint64_to_gdouble (pts) / GST_SECOND;
1395 mux->index = g_list_prepend (mux->index, entry);
1399 static GstFlowReturn
1400 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvMuxPad * pad,
1406 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD (pad)->segment,
1407 GST_BUFFER_DTS (buffer));
1409 /* clipping function arranged for running_time */
1411 if (!mux->streamable)
1412 gst_flv_mux_update_index (mux, buffer, pad);
1414 tag = gst_flv_mux_buffer_to_tag (mux, buffer, pad);
1416 gst_buffer_unref (buffer);
1418 ret = gst_flv_mux_push (mux, tag);
1420 if (ret == GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (dts))
1421 pad->last_timestamp = dts;
1428 gst_flv_mux_determine_duration (GstFlvMux * mux)
1431 GstClockTime duration = GST_CLOCK_TIME_NONE;
1433 GST_DEBUG_OBJECT (mux, "trying to determine the duration "
1434 "from pad timestamps");
1436 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l != NULL; l = l->next) {
1437 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
1439 if (pad && (pad->last_timestamp != GST_CLOCK_TIME_NONE)) {
1440 if (duration == GST_CLOCK_TIME_NONE)
1441 duration = pad->last_timestamp;
1443 duration = MAX (duration, pad->last_timestamp);
1451 gst_flv_mux_are_all_pads_eos (GstFlvMux * mux)
1455 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l; l = l->next) {
1456 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
1458 if (!gst_aggregator_pad_is_eos (GST_AGGREGATOR_PAD (pad)))
1464 static GstFlowReturn
1465 gst_flv_mux_write_eos (GstFlvMux * mux)
1469 if (mux->video_pad == NULL)
1472 tag = gst_flv_mux_eos_to_tag (mux, mux->video_pad);
1474 return gst_flv_mux_push (mux, tag);
1477 static GstFlowReturn
1478 gst_flv_mux_rewrite_header (GstFlvMux * mux)
1480 GstBuffer *rewrite, *index, *tmp;
1485 guint32 index_len, allocate_size;
1486 guint32 i, index_skip;
1490 if (mux->streamable)
1493 /* seek back to the preallocated index space */
1494 gst_segment_init (&segment, GST_FORMAT_BYTES);
1495 segment.start = segment.time = 13 + 29;
1496 event = gst_event_new_segment (&segment);
1497 if (!gst_pad_push_event (mux->srcpad, event)) {
1498 GST_WARNING_OBJECT (mux, "Seek to rewrite header failed");
1502 /* determine duration now based on our own timestamping,
1503 * so that it is likely many times better and consistent
1504 * than whatever obtained by some query */
1505 dur = gst_flv_mux_determine_duration (mux);
1506 if (dur != GST_CLOCK_TIME_NONE)
1507 mux->duration = dur;
1509 /* rewrite the duration tag */
1510 d = gst_guint64_to_gdouble (mux->duration);
1511 d /= (gdouble) GST_SECOND;
1513 GST_DEBUG_OBJECT (mux, "determined the final duration to be %f", d);
1515 rewrite = gst_flv_mux_create_number_script_value ("duration", d);
1517 /* rewrite the filesize tag */
1518 d = gst_guint64_to_gdouble (mux->byte_count);
1520 GST_DEBUG_OBJECT (mux, "putting total filesize %f in the metadata", d);
1522 tmp = gst_flv_mux_create_number_script_value ("filesize", d);
1523 rewrite = gst_buffer_append (rewrite, tmp);
1526 /* no index, so push buffer and return */
1527 return gst_flv_mux_push (mux, rewrite);
1530 /* rewrite the index */
1531 mux->index = g_list_reverse (mux->index);
1532 index_len = g_list_length (mux->index);
1534 /* We write at most MAX_INDEX_ENTRIES elements */
1535 if (index_len > MAX_INDEX_ENTRIES) {
1536 index_skip = 1 + index_len / MAX_INDEX_ENTRIES;
1537 index_len = (index_len + index_skip - 1) / index_skip;
1542 GST_DEBUG_OBJECT (mux, "Index length is %d", index_len);
1543 /* see size calculation in gst_flv_mux_preallocate_index */
1544 allocate_size = 11 + 8 + 22 + 10 + index_len * 18;
1545 GST_DEBUG_OBJECT (mux, "Allocating %d bytes for index", allocate_size);
1546 _gst_buffer_new_and_alloc (allocate_size, &index, &data);
1548 GST_WRITE_UINT16_BE (data, 9); /* the 'keyframes' key */
1549 memcpy (data + 2, "keyframes", 9);
1550 GST_WRITE_UINT8 (data + 11, 8); /* nested ECMA array */
1551 GST_WRITE_UINT32_BE (data + 12, 2); /* two elements */
1552 GST_WRITE_UINT16_BE (data + 16, 5); /* first string key: 'times' */
1553 memcpy (data + 18, "times", 5);
1554 GST_WRITE_UINT8 (data + 23, 10); /* strict array */
1555 GST_WRITE_UINT32_BE (data + 24, index_len);
1558 /* the keyframes' times */
1559 for (i = 0, l = mux->index; l; l = l->next, i++) {
1560 GstFlvMuxIndexEntry *entry = l->data;
1562 if (i % index_skip != 0)
1564 GST_WRITE_UINT8 (data, 0); /* numeric (aka double) */
1565 GST_WRITE_DOUBLE_BE (data + 1, entry->time);
1569 GST_WRITE_UINT16_BE (data, 13); /* second string key: 'filepositions' */
1570 memcpy (data + 2, "filepositions", 13);
1571 GST_WRITE_UINT8 (data + 15, 10); /* strict array */
1572 GST_WRITE_UINT32_BE (data + 16, index_len);
1575 /* the keyframes' file positions */
1576 for (i = 0, l = mux->index; l; l = l->next, i++) {
1577 GstFlvMuxIndexEntry *entry = l->data;
1579 if (i % index_skip != 0)
1581 GST_WRITE_UINT8 (data, 0);
1582 GST_WRITE_DOUBLE_BE (data + 1, entry->position);
1586 GST_WRITE_UINT24_BE (data, 9); /* finish the ECMA array */
1588 /* If there is space left in the prefilled area, reinsert the filler.
1589 There is at least 18 bytes free, so it will always fit. */
1590 if (index_len < MAX_INDEX_ENTRIES) {
1593 guint32 remaining_filler_size;
1595 _gst_buffer_new_and_alloc (14, &tmp, &data);
1596 GST_WRITE_UINT16_BE (data, 9);
1597 memcpy (data + 2, "gstfiller", 9);
1598 GST_WRITE_UINT8 (data + 11, 2); /* string */
1600 /* There is 18 bytes per remaining index entry minus what is used for
1601 * the'gstfiller' key. The rest is already filled with spaces, so just need
1602 * to update length. */
1603 remaining_filler_size = (MAX_INDEX_ENTRIES - index_len) * 18 - 14;
1604 GST_DEBUG_OBJECT (mux, "Remaining filler size is %d bytes",
1605 remaining_filler_size);
1606 GST_WRITE_UINT16_BE (data + 12, remaining_filler_size);
1607 index = gst_buffer_append (index, tmp);
1610 rewrite = gst_buffer_append (rewrite, index);
1612 return gst_flv_mux_push (mux, rewrite);
1615 static GstFlvMuxPad *
1616 gst_flv_mux_find_best_pad (GstAggregator * aggregator, GstClockTime * ts)
1618 GstAggregatorPad *apad;
1619 GstFlvMuxPad *pad, *best = NULL;
1622 GstClockTime best_ts = GST_CLOCK_TIME_NONE;
1624 for (l = GST_ELEMENT_CAST (aggregator)->sinkpads; l; l = l->next) {
1625 apad = GST_AGGREGATOR_PAD (l->data);
1626 pad = GST_FLV_MUX_PAD (l->data);
1627 buffer = gst_aggregator_pad_peek_buffer (GST_AGGREGATOR_PAD (pad));
1630 if (best_ts == GST_CLOCK_TIME_NONE) {
1632 best_ts = gst_flv_mux_segment_to_running_time (&apad->segment,
1633 GST_BUFFER_DTS_OR_PTS (buffer));
1634 } else if (GST_BUFFER_DTS_OR_PTS (buffer) != GST_CLOCK_TIME_NONE) {
1635 gint64 t = gst_flv_mux_segment_to_running_time (&apad->segment,
1636 GST_BUFFER_DTS_OR_PTS (buffer));
1642 gst_buffer_unref (buffer);
1644 GST_DEBUG_OBJECT (aggregator,
1645 "Best pad found with %" GST_TIME_FORMAT ": %" GST_PTR_FORMAT,
1646 GST_TIME_ARGS (best_ts), best);
1652 static GstFlowReturn
1653 gst_flv_mux_aggregate (GstAggregator * aggregator, gboolean timeout)
1655 GstFlvMux *mux = GST_FLV_MUX (aggregator);
1657 gint64 best_time = GST_CLOCK_STIME_NONE;
1660 GstBuffer *buffer = NULL;
1662 if (mux->state == GST_FLV_MUX_STATE_HEADER) {
1663 if (GST_ELEMENT_CAST (mux)->sinkpads == NULL) {
1664 GST_ELEMENT_ERROR (mux, STREAM, MUX, (NULL),
1665 ("No input streams configured"));
1666 return GST_FLOW_ERROR;
1669 ret = gst_flv_mux_write_header (mux);
1670 if (ret != GST_FLOW_OK)
1672 mux->state = GST_FLV_MUX_STATE_DATA;
1674 best = gst_flv_mux_find_best_pad (aggregator, &ts);
1675 if (best && GST_CLOCK_STIME_IS_VALID (ts))
1676 mux->first_timestamp = ts;
1678 mux->first_timestamp = 0;
1680 best = gst_flv_mux_find_best_pad (aggregator, &ts);
1683 if (mux->new_tags) {
1684 GstBuffer *buf = gst_flv_mux_create_metadata (mux, FALSE);
1686 gst_flv_mux_push (mux, buf);
1687 mux->new_tags = FALSE;
1691 buffer = gst_aggregator_pad_pop_buffer (GST_AGGREGATOR_PAD (best));
1694 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1695 (best)->segment, GST_BUFFER_DTS_OR_PTS (buffer));
1697 if (GST_CLOCK_STIME_IS_VALID (best->dts))
1698 best_time = best->dts - mux->first_timestamp;
1700 if (GST_BUFFER_PTS_IS_VALID (buffer))
1702 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1703 (best)->segment, GST_BUFFER_PTS (buffer));
1705 best->pts = best->dts;
1707 GST_LOG_OBJECT (best, "got buffer PTS %" GST_TIME_FORMAT " DTS %"
1708 GST_STIME_FORMAT, GST_TIME_ARGS (best->pts),
1709 GST_STIME_ARGS (best->dts));
1711 best_time = GST_CLOCK_STIME_NONE;
1714 /* The FLV timestamp is an int32 field. For non-live streams error out if a
1715 bigger timestamp is seen, for live the timestamp will get wrapped in
1716 gst_flv_mux_buffer_to_tag */
1717 if (!mux->streamable && (GST_CLOCK_STIME_IS_VALID (best_time))
1718 && best_time / GST_MSECOND > G_MAXINT32) {
1719 GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
1721 gst_buffer_unref (buffer);
1728 return gst_flv_mux_write_buffer (mux, best, buffer);
1730 if (gst_flv_mux_are_all_pads_eos (mux)) {
1731 gst_flv_mux_write_eos (mux);
1732 gst_flv_mux_rewrite_header (mux);
1733 return GST_FLOW_EOS;
1740 gst_flv_mux_get_property (GObject * object,
1741 guint prop_id, GValue * value, GParamSpec * pspec)
1743 GstFlvMux *mux = GST_FLV_MUX (object);
1746 case PROP_STREAMABLE:
1747 g_value_set_boolean (value, mux->streamable);
1749 case PROP_METADATACREATOR:
1750 g_value_set_string (value, mux->metadatacreator);
1753 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1759 gst_flv_mux_set_property (GObject * object,
1760 guint prop_id, const GValue * value, GParamSpec * pspec)
1762 GstFlvMux *mux = GST_FLV_MUX (object);
1765 case PROP_STREAMABLE:
1766 mux->streamable = g_value_get_boolean (value);
1767 if (mux->streamable)
1768 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1769 GST_TAG_MERGE_REPLACE);
1771 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1772 GST_TAG_MERGE_KEEP);
1774 case PROP_METADATACREATOR:
1775 g_free (mux->metadatacreator);
1776 if (!g_value_get_string (value)) {
1777 GST_WARNING_OBJECT (mux, "metadatacreator property can not be NULL");
1778 mux->metadatacreator = g_strdup (DEFAULT_METADATACREATOR);
1780 mux->metadatacreator = g_value_dup_string (value);
1784 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1790 gst_flv_mux_get_next_time_for_segment (GstAggregator * aggregator,
1791 const GstSegment * segment)
1793 GstClockTime next_time;
1795 GST_OBJECT_LOCK (aggregator);
1796 if (segment->position == -1 || segment->position < segment->start)
1797 next_time = segment->start;
1799 next_time = segment->position;
1801 if (segment->stop != -1 && next_time > segment->stop)
1802 next_time = segment->stop;
1804 next_time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, next_time);
1805 GST_OBJECT_UNLOCK (aggregator);
1807 GST_DEBUG_OBJECT (aggregator, "next_time: %" GST_TIME_FORMAT,
1808 GST_TIME_ARGS (next_time));
1813 gst_flv_mux_get_next_time (GstAggregator * aggregator)
1815 return gst_flv_mux_get_next_time_for_segment (aggregator,
1816 &GST_AGGREGATOR_PAD (aggregator->srcpad)->segment);