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
28 * flvmux muxes different streams into an FLV file.
30 * ## Example launch line
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
60 #define DEFAULT_STREAMABLE FALSE
61 #define MAX_INDEX_ENTRIES 128
62 #define DEFAULT_METADATACREATOR "GStreamer " PACKAGE_VERSION " FLV muxer"
64 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
67 GST_STATIC_CAPS ("video/x-flv")
70 static GstStaticPadTemplate videosink_templ = GST_STATIC_PAD_TEMPLATE ("video",
73 GST_STATIC_CAPS ("video/x-flash-video; "
74 "video/x-flash-screen; "
75 "video/x-vp6-flash; " "video/x-vp6-alpha; "
76 "video/x-h264, stream-format=avc;")
79 static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
83 ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
84 "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }, parsed = (boolean) TRUE; "
85 "audio/mpeg, mpegversion = (int) { 4, 2 }, stream-format = (string) raw; "
86 "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
87 "audio/x-raw, format = (string) { U8, S16LE}, layout = (string) interleaved, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
88 "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) 8000; "
89 "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) 8000; "
90 "audio/x-speex, channels = (int) 1, rate = (int) 16000;")
93 G_DEFINE_TYPE (GstFlvMuxPad, gst_flv_mux_pad, GST_TYPE_AGGREGATOR_PAD);
95 #define gst_flv_mux_parent_class parent_class
96 G_DEFINE_TYPE_WITH_CODE (GstFlvMux, gst_flv_mux, GST_TYPE_AGGREGATOR,
97 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
100 gst_flv_mux_aggregate (GstAggregator * aggregator, gboolean timeout);
102 gst_flv_mux_sink_event (GstAggregator * aggregator, GstAggregatorPad * pad,
105 static GstAggregatorPad *gst_flv_mux_create_new_pad (GstAggregator * agg,
106 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps);
107 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
109 static gboolean gst_flv_mux_video_pad_setcaps (GstFlvMuxPad * pad,
111 static gboolean gst_flv_mux_audio_pad_setcaps (GstFlvMuxPad * pad,
114 static void gst_flv_mux_get_property (GObject * object,
115 guint prop_id, GValue * value, GParamSpec * pspec);
116 static void gst_flv_mux_set_property (GObject * object,
117 guint prop_id, const GValue * value, GParamSpec * pspec);
118 static void gst_flv_mux_finalize (GObject * object);
120 static void gst_flv_mux_reset (GstElement * element);
121 static void gst_flv_mux_reset_pad (GstFlvMuxPad * pad);
123 static void gst_flv_mux_pad_finalize (GObject * object);
125 static gboolean gst_flv_mux_start (GstAggregator * aggregator);
126 static GstFlowReturn gst_flv_mux_flush (GstAggregator * aggregator);
127 static GstClockTime gst_flv_mux_get_next_time (GstAggregator * aggregator);
128 static GstFlowReturn gst_flv_mux_write_eos (GstFlvMux * mux);
129 static GstFlowReturn gst_flv_mux_write_header (GstFlvMux * mux);
130 static GstFlowReturn gst_flv_mux_rewrite_header (GstFlvMux * mux);
131 static gboolean gst_flv_mux_are_all_pads_eos (GstFlvMux * mux);
132 static GstFlowReturn gst_flv_mux_update_src_caps (GstAggregator * aggregator,
133 GstCaps * caps, GstCaps ** ret);
134 static guint64 gst_flv_mux_query_upstream_duration (GstFlvMux * mux);
137 gst_flv_mux_pad_flush (GstAggregatorPad * pad, GstAggregator * aggregator)
139 GstFlvMuxPad *flvpad = GST_FLV_MUX_PAD (pad);
141 flvpad->last_timestamp = 0;
142 flvpad->pts = GST_CLOCK_STIME_NONE;
143 flvpad->dts = GST_CLOCK_STIME_NONE;
149 gst_flv_mux_pad_class_init (GstFlvMuxPadClass * klass)
151 GstAggregatorPadClass *aggregatorpad_class = (GstAggregatorPadClass *) klass;
152 GObjectClass *gobject_class = (GObjectClass *) klass;
154 gobject_class->finalize = gst_flv_mux_pad_finalize;
156 aggregatorpad_class->flush = GST_DEBUG_FUNCPTR (gst_flv_mux_pad_flush);
160 gst_flv_mux_pad_init (GstFlvMuxPad * pad)
162 gst_flv_mux_reset_pad (pad);
169 } GstFlvMuxIndexEntry;
172 gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
174 g_slice_free (GstFlvMuxIndexEntry, entry);
178 _gst_buffer_new_wrapped (gpointer mem, gsize size, GFreeFunc free_func)
182 buf = gst_buffer_new ();
183 gst_buffer_append_memory (buf,
184 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
185 mem, size, 0, size, mem, free_func));
191 _gst_buffer_new_and_alloc (gsize size, GstBuffer ** buffer, guint8 ** data)
193 g_return_if_fail (data != NULL);
194 g_return_if_fail (buffer != NULL);
196 *data = g_malloc (size);
197 *buffer = _gst_buffer_new_wrapped (*data, size, g_free);
201 gst_flv_mux_class_init (GstFlvMuxClass * klass)
203 GObjectClass *gobject_class;
204 GstElementClass *gstelement_class;
205 GstAggregatorClass *gstaggregator_class;
207 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
209 gobject_class = (GObjectClass *) klass;
210 gstelement_class = (GstElementClass *) klass;
211 gstaggregator_class = (GstAggregatorClass *) klass;
213 gobject_class->get_property = gst_flv_mux_get_property;
214 gobject_class->set_property = gst_flv_mux_set_property;
215 gobject_class->finalize = gst_flv_mux_finalize;
217 /* FIXME: ideally the right mode of operation should be detected
218 * automatically using queries when parameter not specified. */
220 * GstFlvMux:streamable
222 * If True, the output will be streaming friendly. (ie without indexes and
225 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
226 g_param_spec_boolean ("streamable", "streamable",
227 "If set to true, the output should be as if it is to be streamed "
228 "and hence no indexes written or duration written.",
229 DEFAULT_STREAMABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_METADATACREATOR,
231 g_param_spec_string ("metadatacreator", "metadatacreator",
232 "The value of metadatacreator in the meta packet.",
233 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_ENCODER,
236 g_param_spec_string ("encoder", "encoder",
237 "The value of encoder in the meta packet.",
238 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 gstaggregator_class->create_new_pad =
241 GST_DEBUG_FUNCPTR (gst_flv_mux_create_new_pad);
242 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
244 gstaggregator_class->start = GST_DEBUG_FUNCPTR (gst_flv_mux_start);
245 gstaggregator_class->aggregate = GST_DEBUG_FUNCPTR (gst_flv_mux_aggregate);
246 gstaggregator_class->sink_event = GST_DEBUG_FUNCPTR (gst_flv_mux_sink_event);
247 gstaggregator_class->flush = GST_DEBUG_FUNCPTR (gst_flv_mux_flush);
248 gstaggregator_class->get_next_time =
249 GST_DEBUG_FUNCPTR (gst_flv_mux_get_next_time);
250 gstaggregator_class->update_src_caps =
251 GST_DEBUG_FUNCPTR (gst_flv_mux_update_src_caps);
253 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
254 &videosink_templ, GST_TYPE_FLV_MUX_PAD);
255 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
256 &audiosink_templ, GST_TYPE_FLV_MUX_PAD);
257 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
258 &src_templ, GST_TYPE_AGGREGATOR_PAD);
259 gst_element_class_set_static_metadata (gstelement_class, "FLV muxer",
261 "Muxes video/audio streams into a FLV stream",
262 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
264 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
268 gst_flv_mux_init (GstFlvMux * mux)
270 mux->srcpad = GST_AGGREGATOR_CAST (mux)->srcpad;
273 mux->streamable = DEFAULT_STREAMABLE;
274 mux->metadatacreator = g_strdup (DEFAULT_METADATACREATOR);
275 mux->encoder = g_strdup (DEFAULT_METADATACREATOR);
277 mux->new_tags = FALSE;
279 gst_flv_mux_reset (GST_ELEMENT (mux));
283 gst_flv_mux_finalize (GObject * object)
285 GstFlvMux *mux = GST_FLV_MUX (object);
287 gst_flv_mux_reset (GST_ELEMENT (object));
288 g_free (mux->metadatacreator);
289 g_free (mux->encoder);
291 G_OBJECT_CLASS (gst_flv_mux_parent_class)->finalize (object);
295 gst_flv_mux_pad_finalize (GObject * object)
297 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (object);
299 gst_flv_mux_reset_pad (pad);
301 G_OBJECT_CLASS (gst_flv_mux_pad_parent_class)->finalize (object);
305 gst_flv_mux_flush (GstAggregator * aggregator)
307 /* TODO: What is the right behaviour on flush? Should we just ignore it ?
308 * This still needs to be defined. */
310 gst_flv_mux_reset (GST_ELEMENT (aggregator));
315 gst_flv_mux_start (GstAggregator * aggregator)
317 gst_flv_mux_reset (GST_ELEMENT (aggregator));
322 gst_flv_mux_reset (GstElement * element)
324 GstFlvMux *mux = GST_FLV_MUX (element);
326 g_list_foreach (mux->index, (GFunc) gst_flv_mux_index_entry_free, NULL);
327 g_list_free (mux->index);
331 mux->duration = GST_CLOCK_TIME_NONE;
332 mux->new_tags = FALSE;
333 mux->first_timestamp = GST_CLOCK_STIME_NONE;
336 mux->state = GST_FLV_MUX_STATE_HEADER;
337 mux->sent_header = FALSE;
340 gst_tag_setter_reset_tags (GST_TAG_SETTER (mux));
343 /* Extract per-codec relevant tags for
344 * insertion into the metadata later - ie bitrate,
345 * but maybe others in the future */
347 gst_flv_mux_store_codec_tags (GstFlvMux * mux,
348 GstFlvMuxPad * flvpad, GstTagList * list)
350 /* Look for a bitrate as either nominal or actual bitrate tag */
351 if (gst_tag_list_get_uint (list, GST_TAG_NOMINAL_BITRATE, &flvpad->bitrate)
352 || gst_tag_list_get_uint (list, GST_TAG_BITRATE, &flvpad->bitrate)) {
353 GST_DEBUG_OBJECT (mux, "Stored bitrate for pad %" GST_PTR_FORMAT " = %u",
354 flvpad, flvpad->bitrate);
359 gst_flv_mux_sink_event (GstAggregator * aggregator, GstAggregatorPad * pad,
362 GstFlvMux *mux = GST_FLV_MUX (aggregator);
363 GstFlvMuxPad *flvpad = (GstFlvMuxPad *) pad;
366 switch (GST_EVENT_TYPE (event)) {
371 gst_event_parse_caps (event, &caps);
373 if (mux->video_pad == flvpad) {
374 ret = gst_flv_mux_video_pad_setcaps (flvpad, caps);
375 } else if (mux->audio_pad == flvpad) {
376 ret = gst_flv_mux_audio_pad_setcaps (flvpad, caps);
378 g_assert_not_reached ();
384 GstTagSetter *setter = GST_TAG_SETTER (mux);
385 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
387 gst_event_parse_tag (event, &list);
388 gst_tag_setter_merge_tags (setter, list, mode);
389 gst_flv_mux_store_codec_tags (mux, flvpad, list);
390 mux->new_tags = TRUE;
401 return GST_AGGREGATOR_CLASS (parent_class)->sink_event (aggregator, pad,
406 gst_flv_mux_video_pad_setcaps (GstFlvMuxPad * pad, GstCaps * caps)
408 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
412 GstBuffer *old_codec_data = NULL;
414 old_codec = pad->codec;
416 old_codec_data = gst_buffer_ref (pad->codec_data);
418 s = gst_caps_get_structure (caps, 0);
420 if (strcmp (gst_structure_get_name (s), "video/x-flash-video") == 0) {
422 } else if (strcmp (gst_structure_get_name (s), "video/x-flash-screen") == 0) {
424 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-flash") == 0) {
426 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-alpha") == 0) {
428 } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
434 if (ret && gst_structure_has_field (s, "codec_data")) {
435 const GValue *val = gst_structure_get_value (s, "codec_data");
438 gst_buffer_replace (&pad->codec_data, gst_value_get_buffer (val));
439 else if (!val && pad->codec_data)
440 gst_buffer_unref (pad->codec_data);
443 if (ret && mux->streamable && mux->state != GST_FLV_MUX_STATE_HEADER) {
444 if (old_codec != pad->codec) {
445 pad->info_changed = TRUE;
448 if (old_codec_data && pad->codec_data) {
451 gst_buffer_map (old_codec_data, &map, GST_MAP_READ);
452 if (map.size != gst_buffer_get_size (pad->codec_data) ||
453 gst_buffer_memcmp (pad->codec_data, 0, map.data, map.size))
454 pad->info_changed = TRUE;
456 gst_buffer_unmap (old_codec_data, &map);
457 } else if (!old_codec_data && pad->codec_data) {
458 pad->info_changed = TRUE;
461 if (pad->info_changed)
462 mux->state = GST_FLV_MUX_STATE_HEADER;
466 gst_buffer_unref (old_codec_data);
468 gst_object_unref (mux);
474 gst_flv_mux_audio_pad_setcaps (GstFlvMuxPad * pad, GstCaps * caps)
476 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
479 guint old_codec, old_rate, old_width, old_channels;
480 GstBuffer *old_codec_data = NULL;
482 old_codec = pad->codec;
483 old_rate = pad->rate;
484 old_width = pad->width;
485 old_channels = pad->channels;
487 old_codec_data = gst_buffer_ref (pad->codec_data);
489 s = gst_caps_get_structure (caps, 0);
491 if (strcmp (gst_structure_get_name (s), "audio/x-adpcm") == 0) {
492 const gchar *layout = gst_structure_get_string (s, "layout");
493 if (layout && strcmp (layout, "swf") == 0) {
498 } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
501 if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
502 if (mpegversion == 1) {
505 if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
508 if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
515 } else if (mpegversion == 4 || mpegversion == 2) {
523 } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
526 if (gst_structure_get_int (s, "rate", &rate)
527 && gst_structure_get_int (s, "channels", &channels)) {
528 if (channels == 1 && rate == 16000)
530 else if (channels == 1 && rate == 8000)
537 } else if (strcmp (gst_structure_get_name (s), "audio/x-raw") == 0) {
540 if (gst_audio_info_from_caps (&info, caps)) {
543 if (GST_AUDIO_INFO_WIDTH (&info) == 8)
545 else if (GST_AUDIO_INFO_WIDTH (&info) == 16)
551 } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
553 } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
555 } else if (strcmp (gst_structure_get_name (s), "audio/x-speex") == 0) {
564 if (gst_structure_get_int (s, "rate", &rate)) {
565 if (pad->codec == 10)
567 else if (rate == 5512)
569 else if (rate == 11025)
571 else if (rate == 22050)
573 else if (rate == 44100)
575 else if (rate == 8000 && (pad->codec == 5 || pad->codec == 14
576 || pad->codec == 7 || pad->codec == 8))
578 else if (rate == 16000 && (pad->codec == 4 || pad->codec == 11))
582 } else if (pad->codec == 10) {
588 if (gst_structure_get_int (s, "channels", &channels)) {
589 if (pad->codec == 4 || pad->codec == 5
590 || pad->codec == 6 || pad->codec == 11)
592 else if (pad->codec == 10)
594 else if (channels == 1)
596 else if (channels == 2)
600 } else if (pad->codec == 4 || pad->codec == 5 || pad->codec == 6) {
602 } else if (pad->codec == 10) {
612 if (ret && gst_structure_has_field (s, "codec_data")) {
613 const GValue *val = gst_structure_get_value (s, "codec_data");
616 gst_buffer_replace (&pad->codec_data, gst_value_get_buffer (val));
617 else if (!val && pad->codec_data)
618 gst_buffer_unref (pad->codec_data);
621 if (ret && mux->streamable && mux->state != GST_FLV_MUX_STATE_HEADER) {
622 if (old_codec != pad->codec || old_rate != pad->rate ||
623 old_width != pad->width || old_channels != pad->channels) {
624 pad->info_changed = TRUE;
627 if (old_codec_data && pad->codec_data) {
630 gst_buffer_map (old_codec_data, &map, GST_MAP_READ);
631 if (map.size != gst_buffer_get_size (pad->codec_data) ||
632 gst_buffer_memcmp (pad->codec_data, 0, map.data, map.size))
633 pad->info_changed = TRUE;
635 gst_buffer_unmap (old_codec_data, &map);
636 } else if (!old_codec_data && pad->codec_data) {
637 pad->info_changed = TRUE;
640 if (pad->info_changed)
641 mux->state = GST_FLV_MUX_STATE_HEADER;
645 gst_buffer_unref (old_codec_data);
647 gst_object_unref (mux);
653 gst_flv_mux_reset_pad (GstFlvMuxPad * pad)
655 GST_DEBUG_OBJECT (pad, "resetting pad");
658 gst_buffer_unref (pad->codec_data);
659 pad->codec_data = NULL;
660 pad->codec = G_MAXUINT;
661 pad->rate = G_MAXUINT;
662 pad->width = G_MAXUINT;
663 pad->channels = G_MAXUINT;
664 pad->info_changed = FALSE;
666 gst_flv_mux_pad_flush (GST_AGGREGATOR_PAD_CAST (pad), NULL);
669 static GstAggregatorPad *
670 gst_flv_mux_create_new_pad (GstAggregator * agg,
671 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
673 GstElementClass *klass = GST_ELEMENT_GET_CLASS (agg);
674 GstAggregatorPad *aggpad;
675 GstFlvMux *mux = GST_FLV_MUX (agg);
676 GstFlvMuxPad *pad = NULL;
677 const gchar *name = NULL;
680 if (mux->state != GST_FLV_MUX_STATE_HEADER && !mux->streamable) {
681 GST_ELEMENT_WARNING (mux, STREAM, MUX,
682 ("Requested a late stream in a non-streamable file"),
683 ("Stream added after file started and therefore won't be playable"));
687 if (templ == gst_element_class_get_pad_template (klass, "audio")) {
688 if (mux->audio_pad) {
689 GST_WARNING_OBJECT (mux, "Already have an audio pad");
694 } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
695 if (mux->video_pad) {
696 GST_WARNING_OBJECT (mux, "Already have a video pad");
702 GST_WARNING_OBJECT (mux, "Invalid template");
707 GST_AGGREGATOR_CLASS (gst_flv_mux_parent_class)->create_new_pad (agg,
712 pad = GST_FLV_MUX_PAD (aggpad);
714 gst_flv_mux_reset_pad (pad);
717 mux->video_pad = pad;
719 mux->audio_pad = pad;
725 gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
727 GstFlvMux *mux = GST_FLV_MUX (element);
728 GstFlvMuxPad *flvpad = GST_FLV_MUX_PAD (pad);
730 gst_pad_set_active (pad, FALSE);
731 gst_flv_mux_reset_pad (flvpad);
733 if (flvpad == mux->video_pad) {
734 mux->video_pad = NULL;
735 } else if (flvpad == mux->audio_pad) {
736 mux->audio_pad = NULL;
738 GST_WARNING_OBJECT (pad, "Pad is not known audio or video pad");
741 gst_element_remove_pad (element, pad);
745 gst_flv_mux_push (GstFlvMux * mux, GstBuffer * buffer)
747 GstAggregator *agg = GST_AGGREGATOR (mux);
748 GstAggregatorPad *srcpad = GST_AGGREGATOR_PAD (agg->srcpad);
750 if (GST_BUFFER_PTS_IS_VALID (buffer))
751 srcpad->segment.position = GST_BUFFER_PTS (buffer);
753 /* pushing the buffer that rewrites the header will make it no longer be the
754 * total output size in bytes, but it doesn't matter at that point */
755 mux->byte_count += gst_buffer_get_size (buffer);
757 return gst_aggregator_finish_buffer (GST_AGGREGATOR_CAST (mux), buffer);
761 gst_flv_mux_create_header (GstFlvMux * mux)
768 _gst_buffer_new_and_alloc (9 + 4, &header, &data);
773 data[3] = 0x01; /* Version */
775 have_audio = (mux->audio_pad && mux->audio_pad->codec != G_MAXUINT);
776 have_video = (mux->video_pad && mux->video_pad->codec != G_MAXUINT);
778 data[4] = (have_audio << 2) | have_video; /* flags */
779 GST_WRITE_UINT32_BE (data + 5, 9); /* data offset */
780 GST_WRITE_UINT32_BE (data + 9, 0); /* previous tag size */
786 gst_flv_mux_preallocate_index (GstFlvMux * mux)
790 gint preallocate_size;
792 /* preallocate index of size:
793 * - 'keyframes' ECMA array key: 2 + 9 = 11 bytes
794 * - nested ECMA array header, length and end marker: 8 bytes
795 * - 'times' and 'filepositions' keys: 22 bytes
796 * - two strict arrays headers and lengths: 10 bytes
797 * - each index entry: 18 bytes
799 preallocate_size = 11 + 8 + 22 + 10 + MAX_INDEX_ENTRIES * 18;
800 GST_DEBUG_OBJECT (mux, "preallocating %d bytes for the index",
803 _gst_buffer_new_and_alloc (preallocate_size, &tmp, &data);
805 /* prefill the space with a gstfiller: <spaces> script tag variable */
806 GST_WRITE_UINT16_BE (data, 9); /* 9 characters */
807 memcpy (data + 2, "gstfiller", 9);
808 GST_WRITE_UINT8 (data + 11, AMF0_STRING_MARKER); /* a string value */
809 GST_WRITE_UINT16_BE (data + 12, preallocate_size - 14);
810 memset (data + 14, ' ', preallocate_size - 14); /* the rest is spaces */
815 gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
819 gsize len = strlen (name);
821 _gst_buffer_new_and_alloc (2 + len + 1 + 8, &tmp, &data);
823 GST_WRITE_UINT16_BE (data, len);
824 data += 2; /* name length */
825 memcpy (data, name, len);
827 *data++ = AMF0_NUMBER_MARKER; /* double type */
828 GST_WRITE_DOUBLE_BE (data, value);
834 gst_flv_mux_create_metadata (GstFlvMux * mux)
836 const GstTagList *tags;
837 GstBuffer *script_tag, *tmp;
841 gint i, n_tags, tags_written = 0;
843 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (mux));
847 /* Timestamp must start at zero */
848 if (GST_CLOCK_STIME_IS_VALID (mux->first_timestamp)) {
849 dts -= mux->first_timestamp / GST_MSECOND;
852 GST_DEBUG_OBJECT (mux,
853 "Creating metadata, dts %" G_GUINT64_FORMAT ", tags = %" GST_PTR_FORMAT,
856 if (dts > G_MAXUINT32) {
858 "Detected rollover, timestamp will be truncated (previous:%"
859 G_GUINT64_FORMAT ", new:%u)", dts, (guint32) dts);
862 /* FIXME perhaps some bytewriter'ing here ... */
864 _gst_buffer_new_and_alloc (11, &script_tag, &data);
868 /* Data size, unknown for now */
874 GST_WRITE_UINT24_BE (data + 4, dts);
875 data[7] = (((guint) dts) >> 24) & 0xff;
878 data[8] = data[9] = data[10] = 0;
880 _gst_buffer_new_and_alloc (13, &tmp, &data);
881 data[0] = AMF0_STRING_MARKER; /* string */
883 data[2] = 10; /* length 10 */
884 memcpy (&data[3], "onMetaData", 10);
886 script_tag = gst_buffer_append (script_tag, tmp);
888 n_tags = (tags) ? gst_tag_list_n_tags (tags) : 0;
889 _gst_buffer_new_and_alloc (5, &tmp, &data);
890 data[0] = 8; /* ECMA array */
891 GST_WRITE_UINT32_BE (data + 1, n_tags);
892 script_tag = gst_buffer_append (script_tag, tmp);
894 /* Some players expect the 'duration' to be always set. Fill it out later,
895 after querying the pads or after getting EOS */
896 if (!mux->streamable) {
897 tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
898 script_tag = gst_buffer_append (script_tag, tmp);
901 /* Sometimes the information about the total file size is useful for the
902 player. It will be filled later, after getting EOS */
903 tmp = gst_flv_mux_create_number_script_value ("filesize", 0);
904 script_tag = gst_buffer_append (script_tag, tmp);
907 /* Preallocate space for the index to be written at EOS */
908 tmp = gst_flv_mux_preallocate_index (mux);
909 script_tag = gst_buffer_append (script_tag, tmp);
911 GST_DEBUG_OBJECT (mux, "not preallocating index, streamable mode");
914 for (i = 0; tags && i < n_tags; i++) {
915 const gchar *tag_name = gst_tag_list_nth_tag_name (tags, i);
916 if (!strcmp (tag_name, GST_TAG_DURATION)) {
919 if (!gst_tag_list_get_uint64 (tags, GST_TAG_DURATION, &dur))
922 } else if (!strcmp (tag_name, GST_TAG_ARTIST) ||
923 !strcmp (tag_name, GST_TAG_TITLE)) {
925 const gchar *t = NULL;
927 if (!strcmp (tag_name, GST_TAG_ARTIST))
929 else if (!strcmp (tag_name, GST_TAG_TITLE))
932 if (!gst_tag_list_get_string (tags, tag_name, &s))
935 _gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s),
937 data[0] = 0; /* tag name length */
938 data[1] = strlen (t);
939 memcpy (&data[2], t, strlen (t));
940 data[2 + strlen (t)] = 2; /* string */
941 data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
942 data[4 + strlen (t)] = (strlen (s)) & 0xff;
943 memcpy (&data[5 + strlen (t)], s, strlen (s));
944 script_tag = gst_buffer_append (script_tag, tmp);
951 if (!mux->streamable && mux->duration == GST_CLOCK_TIME_NONE) {
952 mux->duration = gst_flv_mux_query_upstream_duration (mux);
955 if (!mux->streamable && mux->duration != GST_CLOCK_TIME_NONE) {
959 d = gst_guint64_to_gdouble (mux->duration);
960 d /= (gdouble) GST_SECOND;
962 GST_DEBUG_OBJECT (mux, "determined the duration to be %f", d);
963 gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
964 GST_WRITE_DOUBLE_BE (map.data + 29 + 2 + 8 + 1, d);
965 gst_buffer_unmap (script_tag, &map);
968 if (mux->video_pad && mux->video_pad->codec != G_MAXUINT) {
969 GstCaps *caps = NULL;
972 caps = gst_pad_get_current_caps (GST_PAD (mux->video_pad));
979 GST_DEBUG_OBJECT (mux, "putting videocodecid %d in the metadata",
980 mux->video_pad->codec);
982 tmp = gst_flv_mux_create_number_script_value ("videocodecid",
983 mux->video_pad->codec);
984 script_tag = gst_buffer_append (script_tag, tmp);
987 s = gst_caps_get_structure (caps, 0);
988 gst_caps_unref (caps);
990 if (gst_structure_get_int (s, "width", &size)) {
991 GST_DEBUG_OBJECT (mux, "putting width %d in the metadata", size);
993 tmp = gst_flv_mux_create_number_script_value ("width", size);
994 script_tag = gst_buffer_append (script_tag, tmp);
998 if (gst_structure_get_int (s, "height", &size)) {
999 GST_DEBUG_OBJECT (mux, "putting height %d in the metadata", size);
1001 tmp = gst_flv_mux_create_number_script_value ("height", size);
1002 script_tag = gst_buffer_append (script_tag, tmp);
1006 if (gst_structure_get_fraction (s, "pixel-aspect-ratio", &num, &den)) {
1010 GST_DEBUG_OBJECT (mux, "putting AspectRatioX %f in the metadata", d);
1012 tmp = gst_flv_mux_create_number_script_value ("AspectRatioX", d);
1013 script_tag = gst_buffer_append (script_tag, tmp);
1017 GST_DEBUG_OBJECT (mux, "putting AspectRatioY %f in the metadata", d);
1019 tmp = gst_flv_mux_create_number_script_value ("AspectRatioY", d);
1020 script_tag = gst_buffer_append (script_tag, tmp);
1024 if (gst_structure_get_fraction (s, "framerate", &num, &den)) {
1027 gst_util_fraction_to_double (num, den, &d);
1028 GST_DEBUG_OBJECT (mux, "putting framerate %f in the metadata", d);
1030 tmp = gst_flv_mux_create_number_script_value ("framerate", d);
1031 script_tag = gst_buffer_append (script_tag, tmp);
1035 GST_DEBUG_OBJECT (mux, "putting videodatarate %u KB/s in the metadata",
1036 mux->video_pad->bitrate / 1024);
1037 tmp = gst_flv_mux_create_number_script_value ("videodatarate",
1038 mux->video_pad->bitrate / 1024);
1039 script_tag = gst_buffer_append (script_tag, tmp);
1044 if (mux->audio_pad && mux->audio_pad->codec != G_MAXUINT) {
1045 GST_DEBUG_OBJECT (mux, "putting audiocodecid %d in the metadata",
1046 mux->audio_pad->codec);
1048 tmp = gst_flv_mux_create_number_script_value ("audiocodecid",
1049 mux->audio_pad->codec);
1050 script_tag = gst_buffer_append (script_tag, tmp);
1053 GST_DEBUG_OBJECT (mux, "putting audiodatarate %u KB/s in the metadata",
1054 mux->audio_pad->bitrate / 1024);
1055 tmp = gst_flv_mux_create_number_script_value ("audiodatarate",
1056 mux->audio_pad->bitrate / 1024);
1057 script_tag = gst_buffer_append (script_tag, tmp);
1061 _gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (mux->metadatacreator),
1063 data[0] = 0; /* 15 bytes name */
1065 memcpy (&data[2], "metadatacreator", 15);
1066 data[17] = 2; /* string */
1067 data[18] = (strlen (mux->metadatacreator) >> 8) & 0xff;
1068 data[19] = (strlen (mux->metadatacreator)) & 0xff;
1069 memcpy (&data[20], mux->metadatacreator, strlen (mux->metadatacreator));
1070 script_tag = gst_buffer_append (script_tag, tmp);
1073 _gst_buffer_new_and_alloc (2 + 7 + 1 + 2 + strlen (mux->encoder),
1075 data[0] = 0; /* 7 bytes name */
1077 memcpy (&data[2], "encoder", 7);
1078 data[9] = 2; /* string */
1079 data[10] = (strlen (mux->encoder) >> 8) & 0xff;
1080 data[11] = (strlen (mux->encoder)) & 0xff;
1081 memcpy (&data[12], mux->encoder, strlen (mux->encoder));
1082 script_tag = gst_buffer_append (script_tag, tmp);
1089 static const gchar *weekdays[] = {
1090 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1092 static const gchar *months[] = {
1093 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
1094 "Aug", "Sep", "Oct", "Nov", "Dec"
1097 secs = g_get_real_time () / G_USEC_PER_SEC;
1098 #ifdef HAVE_GMTIME_R
1099 gmtime_r (&secs, &tm);
1101 tm = *gmtime (&secs);
1104 s = g_strdup_printf ("%s %s %d %02d:%02d:%02d %d", weekdays[tm.tm_wday],
1105 months[tm.tm_mon], tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1108 _gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s), &tmp, &data);
1109 data[0] = 0; /* 12 bytes name */
1111 memcpy (&data[2], "creationdate", 12);
1112 data[14] = 2; /* string */
1113 data[15] = (strlen (s) >> 8) & 0xff;
1114 data[16] = (strlen (s)) & 0xff;
1115 memcpy (&data[17], s, strlen (s));
1116 script_tag = gst_buffer_append (script_tag, tmp);
1122 if (!tags_written) {
1123 gst_buffer_unref (script_tag);
1128 _gst_buffer_new_and_alloc (2 + 0 + 1, &tmp, &data);
1129 data[0] = 0; /* 0 byte size */
1131 data[2] = 9; /* end marker */
1132 script_tag = gst_buffer_append (script_tag, tmp);
1135 _gst_buffer_new_and_alloc (4, &tmp, &data);
1136 GST_WRITE_UINT32_BE (data, gst_buffer_get_size (script_tag));
1137 script_tag = gst_buffer_append (script_tag, tmp);
1139 gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
1140 map.data[1] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 16) & 0xff;
1141 map.data[2] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 8) & 0xff;
1142 map.data[3] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 0) & 0xff;
1144 GST_WRITE_UINT32_BE (map.data + 11 + 13 + 1, tags_written);
1145 gst_buffer_unmap (script_tag, &map);
1152 gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
1153 GstFlvMuxPad * pad, gboolean is_codec_data)
1158 guint64 pts, dts, cts;
1159 guint8 *data, *bdata = NULL;
1162 if (!GST_CLOCK_STIME_IS_VALID (pad->dts)) {
1163 pts = dts = pad->last_timestamp / GST_MSECOND;
1165 pts = pad->pts / GST_MSECOND;
1166 dts = pad->dts / GST_MSECOND;
1169 /* We prevent backwards timestamps because they confuse librtmp,
1170 * it expects timestamps to go forward not only inside one stream, but
1171 * also between the audio & video streams.
1173 if (dts < mux->last_dts) {
1174 GST_WARNING_OBJECT (pad, "Got backwards dts! (%" GST_TIME_FORMAT
1175 " < %" GST_TIME_FORMAT ")", GST_TIME_ARGS (dts * GST_MSECOND),
1176 GST_TIME_ARGS (mux->last_dts * GST_MSECOND));
1177 dts = mux->last_dts;
1179 mux->last_dts = dts;
1182 /* Be safe in case TS are buggy */
1188 /* Timestamp must start at zero */
1189 if (GST_CLOCK_STIME_IS_VALID (mux->first_timestamp)) {
1190 dts -= mux->first_timestamp / GST_MSECOND;
1194 GST_LOG_OBJECT (mux,
1195 "got pts %" G_GUINT64_FORMAT " dts %" G_GUINT64_FORMAT " cts %"
1196 G_GUINT64_FORMAT, pts, dts, cts);
1198 if (dts > G_MAXUINT32) {
1199 GST_LOG_OBJECT (mux,
1200 "Detected rollover, timestamp will be truncated (previous:%"
1201 G_GUINT64_FORMAT ", new:%u)", dts, (guint32) dts);
1204 if (buffer != NULL) {
1205 gst_buffer_map (buffer, &map, GST_MAP_READ);
1211 if (mux->video_pad == pad) {
1213 if (pad->codec == 7)
1219 if (pad->codec == 10)
1226 _gst_buffer_new_and_alloc (size, &tag, &data);
1227 memset (data, 0, size);
1229 data[0] = (mux->video_pad == pad) ? 9 : 8;
1231 data[1] = ((size - 11 - 4) >> 16) & 0xff;
1232 data[2] = ((size - 11 - 4) >> 8) & 0xff;
1233 data[3] = ((size - 11 - 4) >> 0) & 0xff;
1236 GST_WRITE_UINT24_BE (data + 4, dts);
1237 data[7] = (((guint) dts) >> 24) & 0xff;
1239 data[8] = data[9] = data[10] = 0;
1241 if (mux->video_pad == pad) {
1242 if (buffer && GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1247 data[11] |= pad->codec & 0x0f;
1249 if (pad->codec == 7) {
1250 if (is_codec_data) {
1252 GST_WRITE_UINT24_BE (data + 13, 0);
1253 } else if (bsize == 0) {
1254 /* AVC end of sequence */
1256 GST_WRITE_UINT24_BE (data + 13, 0);
1260 GST_WRITE_UINT24_BE (data + 13, cts);
1262 memcpy (data + 11 + 1 + 4, bdata, bsize);
1264 memcpy (data + 11 + 1, bdata, bsize);
1267 data[11] |= (pad->codec << 4) & 0xf0;
1268 data[11] |= (pad->rate << 2) & 0x0c;
1269 data[11] |= (pad->width << 1) & 0x02;
1270 data[11] |= (pad->channels << 0) & 0x01;
1272 GST_DEBUG_OBJECT (mux, "Creating byte %02x with "
1273 "codec:%d, rate:%d, width:%d, channels:%d",
1274 data[11], pad->codec, pad->rate, pad->width, pad->channels);
1276 if (pad->codec == 10) {
1277 data[12] = is_codec_data ? 0 : 1;
1279 memcpy (data + 11 + 1 + 1, bdata, bsize);
1281 memcpy (data + 11 + 1, bdata, bsize);
1286 gst_buffer_unmap (buffer, &map);
1288 GST_WRITE_UINT32_BE (data + size - 4, size - 4);
1290 GST_BUFFER_PTS (tag) = GST_CLOCK_TIME_NONE;
1291 GST_BUFFER_DTS (tag) = GST_CLOCK_TIME_NONE;
1292 GST_BUFFER_DURATION (tag) = GST_CLOCK_TIME_NONE;
1295 /* if we are streamable we copy over timestamps and offsets,
1296 if not just copy the offsets */
1297 if (mux->streamable) {
1298 GstClockTime timestamp = GST_CLOCK_TIME_NONE;
1300 if (gst_segment_to_running_time_full (&GST_AGGREGATOR_PAD (pad)->segment,
1301 GST_FORMAT_TIME, GST_BUFFER_DTS_OR_PTS (buffer),
1303 GST_BUFFER_PTS (tag) = timestamp;
1304 GST_BUFFER_DURATION (tag) = GST_BUFFER_DURATION (buffer);
1306 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_NONE;
1307 GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_NONE;
1309 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET (buffer);
1310 GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_END (buffer);
1313 /* mark the buffer if it's an audio buffer and there's also video being muxed
1314 * or it's a video interframe */
1315 if (mux->video_pad == pad &&
1316 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1317 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1319 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1320 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
1321 GST_BUFFER_OFFSET_NONE;
1327 static inline GstBuffer *
1328 gst_flv_mux_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1331 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, pad, FALSE);
1334 static inline GstBuffer *
1335 gst_flv_mux_codec_data_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1338 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, pad, TRUE);
1341 static inline GstBuffer *
1342 gst_flv_mux_eos_to_tag (GstFlvMux * mux, GstFlvMuxPad * pad)
1344 return gst_flv_mux_buffer_to_tag_internal (mux, NULL, pad, FALSE);
1348 gst_flv_mux_put_buffer_in_streamheader (GValue * streamheader,
1351 GValue value = { 0 };
1354 g_value_init (&value, GST_TYPE_BUFFER);
1355 buf = gst_buffer_copy (buffer);
1356 gst_value_set_buffer (&value, buf);
1357 gst_buffer_unref (buf);
1358 gst_value_array_append_value (streamheader, &value);
1359 g_value_unset (&value);
1363 gst_flv_mux_prepare_src_caps (GstFlvMux * mux, GstBuffer ** header_buf,
1364 GstBuffer ** metadata_buf, GstBuffer ** video_codec_data_buf,
1365 GstBuffer ** audio_codec_data_buf)
1367 GstBuffer *header, *metadata;
1368 GstBuffer *video_codec_data, *audio_codec_data;
1370 GstStructure *structure;
1371 GValue streamheader = { 0 };
1374 header = gst_flv_mux_create_header (mux);
1375 metadata = gst_flv_mux_create_metadata (mux);
1376 video_codec_data = NULL;
1377 audio_codec_data = NULL;
1379 GST_OBJECT_LOCK (mux);
1380 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l != NULL; l = l->next) {
1381 GstFlvMuxPad *pad = l->data;
1383 /* Get H.264 and AAC codec data, if present */
1384 if (pad && mux->video_pad == pad && pad->codec == 7) {
1385 if (pad->codec_data == NULL)
1386 GST_WARNING_OBJECT (mux, "Codec data for video stream not found, "
1387 "output might not be playable");
1390 gst_flv_mux_codec_data_buffer_to_tag (mux, pad->codec_data, pad);
1391 } else if (pad && mux->audio_pad == pad && pad->codec == 10) {
1392 if (pad->codec_data == NULL)
1393 GST_WARNING_OBJECT (mux, "Codec data for audio stream not found, "
1394 "output might not be playable");
1397 gst_flv_mux_codec_data_buffer_to_tag (mux, pad->codec_data, pad);
1400 GST_OBJECT_UNLOCK (mux);
1402 /* mark buffers that will go in the streamheader */
1403 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_HEADER);
1404 GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_HEADER);
1405 if (video_codec_data != NULL) {
1406 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_HEADER);
1407 /* mark as a delta unit, so downstream will not try to synchronize on that
1408 * buffer - to actually start playback you need a real video keyframe */
1409 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_DELTA_UNIT);
1411 if (audio_codec_data != NULL) {
1412 GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_HEADER);
1415 /* put buffers in streamheader */
1416 g_value_init (&streamheader, GST_TYPE_ARRAY);
1417 gst_flv_mux_put_buffer_in_streamheader (&streamheader, header);
1418 gst_flv_mux_put_buffer_in_streamheader (&streamheader, metadata);
1419 if (video_codec_data != NULL)
1420 gst_flv_mux_put_buffer_in_streamheader (&streamheader, video_codec_data);
1421 if (audio_codec_data != NULL)
1422 gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);
1424 /* create the caps and put the streamheader in them */
1425 caps = gst_caps_new_empty_simple ("video/x-flv");
1426 structure = gst_caps_get_structure (caps, 0);
1427 gst_structure_set_value (structure, "streamheader", &streamheader);
1428 g_value_unset (&streamheader);
1431 *header_buf = header;
1433 gst_buffer_unref (header);
1437 *metadata_buf = metadata;
1439 gst_buffer_unref (metadata);
1442 if (video_codec_data_buf) {
1443 *video_codec_data_buf = video_codec_data;
1444 } else if (video_codec_data) {
1445 gst_buffer_unref (video_codec_data);
1448 if (audio_codec_data_buf) {
1449 *audio_codec_data_buf = audio_codec_data;
1450 } else if (audio_codec_data) {
1451 gst_buffer_unref (audio_codec_data);
1457 static GstFlowReturn
1458 gst_flv_mux_write_header (GstFlvMux * mux)
1460 GstBuffer *header, *metadata;
1461 GstBuffer *video_codec_data, *audio_codec_data;
1465 header = metadata = video_codec_data = audio_codec_data = NULL;
1467 /* if not streaming, check if downstream is seekable */
1468 if (!mux->streamable) {
1472 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1473 if (gst_pad_peer_query (mux->srcpad, query)) {
1474 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1475 GST_INFO_OBJECT (mux, "downstream is %sseekable", seekable ? "" : "not ");
1477 /* have to assume seeking is supported if query not handled downstream */
1478 GST_WARNING_OBJECT (mux, "downstream did not handle seeking query");
1482 mux->streamable = TRUE;
1483 g_object_notify (G_OBJECT (mux), "streamable");
1484 GST_WARNING_OBJECT (mux, "downstream is not seekable, but "
1485 "streamable=false. Will ignore that and create streamable output "
1488 gst_query_unref (query);
1491 if (mux->streamable && mux->sent_header) {
1492 GstBuffer **video_codec_data_p = NULL, **audio_codec_data_p = NULL;
1494 if (mux->video_pad && mux->video_pad->info_changed)
1495 video_codec_data_p = &video_codec_data;
1496 if (mux->audio_pad && mux->audio_pad->info_changed)
1497 audio_codec_data_p = &audio_codec_data;
1499 caps = gst_flv_mux_prepare_src_caps (mux,
1500 NULL, NULL, video_codec_data_p, audio_codec_data_p);
1502 caps = gst_flv_mux_prepare_src_caps (mux,
1503 &header, &metadata, &video_codec_data, &audio_codec_data);
1506 gst_aggregator_set_src_caps (GST_AGGREGATOR_CAST (mux), caps);
1508 gst_caps_unref (caps);
1510 /* push the header buffer, the metadata and the codec info, if any */
1511 if (header != NULL) {
1512 ret = gst_flv_mux_push (mux, header);
1513 if (ret != GST_FLOW_OK)
1514 goto failure_header;
1515 mux->sent_header = TRUE;
1517 if (metadata != NULL) {
1518 ret = gst_flv_mux_push (mux, metadata);
1519 if (ret != GST_FLOW_OK)
1520 goto failure_metadata;
1521 mux->new_tags = FALSE;
1523 if (video_codec_data != NULL) {
1524 ret = gst_flv_mux_push (mux, video_codec_data);
1525 if (ret != GST_FLOW_OK)
1526 goto failure_video_codec_data;
1527 mux->video_pad->info_changed = FALSE;
1529 if (audio_codec_data != NULL) {
1530 ret = gst_flv_mux_push (mux, audio_codec_data);
1531 if (ret != GST_FLOW_OK)
1532 goto failure_audio_codec_data;
1533 mux->audio_pad->info_changed = FALSE;
1538 gst_buffer_unref (metadata);
1541 if (video_codec_data != NULL)
1542 gst_buffer_unref (video_codec_data);
1544 failure_video_codec_data:
1545 if (audio_codec_data != NULL)
1546 gst_buffer_unref (audio_codec_data);
1548 failure_audio_codec_data:
1553 gst_flv_mux_segment_to_running_time (const GstSegment * segment, GstClockTime t)
1555 /* we can get a dts before the segment, if dts < pts and pts is inside
1556 * the segment, so we consider early times as 0 */
1557 if (t < segment->start)
1559 return gst_segment_to_running_time (segment, GST_FORMAT_TIME, t);
1563 gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer,
1567 * Add the tag byte offset and to the index if it's a valid seek point, which
1568 * means it's either a video keyframe or if there is no video pad (in that
1569 * case every FLV tag is a valid seek point)
1571 if (mux->video_pad == pad &&
1572 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1575 if (GST_BUFFER_PTS_IS_VALID (buffer)) {
1576 GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
1578 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1579 (pad)->segment, GST_BUFFER_PTS (buffer));
1580 entry->position = mux->byte_count;
1581 entry->time = gst_guint64_to_gdouble (pts) / GST_SECOND;
1582 mux->index = g_list_prepend (mux->index, entry);
1586 static GstFlowReturn
1587 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvMuxPad * pad,
1593 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD (pad)->segment,
1594 GST_BUFFER_DTS (buffer));
1596 /* clipping function arranged for running_time */
1598 if (!mux->streamable)
1599 gst_flv_mux_update_index (mux, buffer, pad);
1601 tag = gst_flv_mux_buffer_to_tag (mux, buffer, pad);
1603 gst_buffer_unref (buffer);
1605 ret = gst_flv_mux_push (mux, tag);
1607 if (ret == GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (dts))
1608 pad->last_timestamp = dts;
1615 gst_flv_mux_determine_duration (GstFlvMux * mux)
1618 GstClockTime duration = GST_CLOCK_TIME_NONE;
1620 GST_DEBUG_OBJECT (mux, "trying to determine the duration "
1621 "from pad timestamps");
1623 GST_OBJECT_LOCK (mux);
1624 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l != NULL; l = l->next) {
1625 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
1627 if (pad && (pad->last_timestamp != GST_CLOCK_TIME_NONE)) {
1628 if (duration == GST_CLOCK_TIME_NONE)
1629 duration = pad->last_timestamp;
1631 duration = MAX (duration, pad->last_timestamp);
1634 GST_OBJECT_UNLOCK (mux);
1641 GstClockTime duration;
1645 duration_query_cb (GstElement * element, GstPad * pad,
1646 struct DurationData *data)
1650 if (gst_pad_peer_query_duration (GST_PAD (pad), GST_FORMAT_TIME,
1651 (gint64 *) & dur) && dur != GST_CLOCK_TIME_NONE) {
1652 if (data->duration == GST_CLOCK_TIME_NONE)
1653 data->duration = dur;
1655 data->duration = MAX (dur, data->duration);
1662 gst_flv_mux_query_upstream_duration (GstFlvMux * mux)
1664 struct DurationData cb_data = { GST_CLOCK_TIME_NONE };
1666 gst_element_foreach_sink_pad (GST_ELEMENT (mux),
1667 (GstElementForeachPadFunc) (duration_query_cb), &cb_data);
1669 return cb_data.duration;
1674 gst_flv_mux_are_all_pads_eos (GstFlvMux * mux)
1678 GST_OBJECT_LOCK (mux);
1679 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l; l = l->next) {
1680 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
1682 if (!gst_aggregator_pad_is_eos (GST_AGGREGATOR_PAD (pad))) {
1683 GST_OBJECT_UNLOCK (mux);
1687 GST_OBJECT_UNLOCK (mux);
1691 static GstFlowReturn
1692 gst_flv_mux_write_eos (GstFlvMux * mux)
1696 if (mux->video_pad == NULL)
1699 tag = gst_flv_mux_eos_to_tag (mux, mux->video_pad);
1701 return gst_flv_mux_push (mux, tag);
1704 static GstFlowReturn
1705 gst_flv_mux_rewrite_header (GstFlvMux * mux)
1707 GstBuffer *rewrite, *index, *tmp;
1712 guint32 index_len, allocate_size;
1713 guint32 i, index_skip;
1717 if (mux->streamable)
1720 /* seek back to the preallocated index space */
1721 gst_segment_init (&segment, GST_FORMAT_BYTES);
1722 segment.start = segment.time = 13 + 29;
1723 event = gst_event_new_segment (&segment);
1724 if (!gst_pad_push_event (mux->srcpad, event)) {
1725 GST_WARNING_OBJECT (mux, "Seek to rewrite header failed");
1729 /* determine duration now based on our own timestamping,
1730 * so that it is likely many times better and consistent
1731 * than whatever obtained by some query */
1732 dur = gst_flv_mux_determine_duration (mux);
1733 if (dur != GST_CLOCK_TIME_NONE)
1734 mux->duration = dur;
1736 /* rewrite the duration tag */
1737 d = gst_guint64_to_gdouble (mux->duration);
1738 d /= (gdouble) GST_SECOND;
1740 GST_DEBUG_OBJECT (mux, "determined the final duration to be %f", d);
1742 rewrite = gst_flv_mux_create_number_script_value ("duration", d);
1744 /* rewrite the filesize tag */
1745 d = gst_guint64_to_gdouble (mux->byte_count);
1747 GST_DEBUG_OBJECT (mux, "putting total filesize %f in the metadata", d);
1749 tmp = gst_flv_mux_create_number_script_value ("filesize", d);
1750 rewrite = gst_buffer_append (rewrite, tmp);
1753 /* no index, so push buffer and return */
1754 return gst_flv_mux_push (mux, rewrite);
1757 /* rewrite the index */
1758 mux->index = g_list_reverse (mux->index);
1759 index_len = g_list_length (mux->index);
1761 /* We write at most MAX_INDEX_ENTRIES elements */
1762 if (index_len > MAX_INDEX_ENTRIES) {
1763 index_skip = 1 + index_len / MAX_INDEX_ENTRIES;
1764 index_len = (index_len + index_skip - 1) / index_skip;
1769 GST_DEBUG_OBJECT (mux, "Index length is %d", index_len);
1770 /* see size calculation in gst_flv_mux_preallocate_index */
1771 allocate_size = 11 + 8 + 22 + 10 + index_len * 18;
1772 GST_DEBUG_OBJECT (mux, "Allocating %d bytes for index", allocate_size);
1773 _gst_buffer_new_and_alloc (allocate_size, &index, &data);
1775 GST_WRITE_UINT16_BE (data, 9); /* the 'keyframes' key */
1776 memcpy (data + 2, "keyframes", 9);
1777 GST_WRITE_UINT8 (data + 11, 8); /* nested ECMA array */
1778 GST_WRITE_UINT32_BE (data + 12, 2); /* two elements */
1779 GST_WRITE_UINT16_BE (data + 16, 5); /* first string key: 'times' */
1780 memcpy (data + 18, "times", 5);
1781 GST_WRITE_UINT8 (data + 23, 10); /* strict array */
1782 GST_WRITE_UINT32_BE (data + 24, index_len);
1785 /* the keyframes' times */
1786 for (i = 0, l = mux->index; l; l = l->next, i++) {
1787 GstFlvMuxIndexEntry *entry = l->data;
1789 if (i % index_skip != 0)
1791 GST_WRITE_UINT8 (data, 0); /* numeric (aka double) */
1792 GST_WRITE_DOUBLE_BE (data + 1, entry->time);
1796 GST_WRITE_UINT16_BE (data, 13); /* second string key: 'filepositions' */
1797 memcpy (data + 2, "filepositions", 13);
1798 GST_WRITE_UINT8 (data + 15, 10); /* strict array */
1799 GST_WRITE_UINT32_BE (data + 16, index_len);
1802 /* the keyframes' file positions */
1803 for (i = 0, l = mux->index; l; l = l->next, i++) {
1804 GstFlvMuxIndexEntry *entry = l->data;
1806 if (i % index_skip != 0)
1808 GST_WRITE_UINT8 (data, 0);
1809 GST_WRITE_DOUBLE_BE (data + 1, entry->position);
1813 GST_WRITE_UINT24_BE (data, 9); /* finish the ECMA array */
1815 /* If there is space left in the prefilled area, reinsert the filler.
1816 There is at least 18 bytes free, so it will always fit. */
1817 if (index_len < MAX_INDEX_ENTRIES) {
1820 guint32 remaining_filler_size;
1822 _gst_buffer_new_and_alloc (14, &tmp, &data);
1823 GST_WRITE_UINT16_BE (data, 9);
1824 memcpy (data + 2, "gstfiller", 9);
1825 GST_WRITE_UINT8 (data + 11, 2); /* string */
1827 /* There is 18 bytes per remaining index entry minus what is used for
1828 * the'gstfiller' key. The rest is already filled with spaces, so just need
1829 * to update length. */
1830 remaining_filler_size = (MAX_INDEX_ENTRIES - index_len) * 18 - 14;
1831 GST_DEBUG_OBJECT (mux, "Remaining filler size is %d bytes",
1832 remaining_filler_size);
1833 GST_WRITE_UINT16_BE (data + 12, remaining_filler_size);
1834 index = gst_buffer_append (index, tmp);
1837 rewrite = gst_buffer_append (rewrite, index);
1839 return gst_flv_mux_push (mux, rewrite);
1842 /* Returns NULL, or a reference to the pad with the
1843 * buffer with lowest running time */
1844 static GstFlvMuxPad *
1845 gst_flv_mux_find_best_pad (GstAggregator * aggregator, GstClockTime * ts)
1847 GstFlvMuxPad *best = NULL;
1849 GstClockTime best_ts = GST_CLOCK_TIME_NONE;
1851 GValue padptr = { 0, };
1852 gboolean done = FALSE;
1854 pads = gst_element_iterate_sink_pads (GST_ELEMENT (aggregator));
1857 switch (gst_iterator_next (pads, &padptr)) {
1858 case GST_ITERATOR_OK:{
1860 GstAggregatorPad *apad = g_value_get_object (&padptr);
1861 buffer = gst_aggregator_pad_peek_buffer (apad);
1864 if (best_ts == GST_CLOCK_TIME_NONE) {
1865 gst_object_replace ((GstObject **) & best, GST_OBJECT (apad));
1866 best_ts = gst_flv_mux_segment_to_running_time (&apad->segment,
1867 GST_BUFFER_DTS_OR_PTS (buffer));
1868 } else if (GST_BUFFER_DTS_OR_PTS (buffer) != GST_CLOCK_TIME_NONE) {
1869 gint64 t = gst_flv_mux_segment_to_running_time (&apad->segment,
1870 GST_BUFFER_DTS_OR_PTS (buffer));
1872 gst_object_replace ((GstObject **) & best, GST_OBJECT (apad));
1876 gst_buffer_unref (buffer);
1877 g_value_reset (&padptr);
1880 case GST_ITERATOR_DONE:
1883 case GST_ITERATOR_RESYNC:
1884 gst_iterator_resync (pads);
1885 /* Clear the best pad and start again. It might have disappeared */
1886 gst_object_replace ((GstObject **) best, NULL);
1887 best_ts = GST_CLOCK_TIME_NONE;
1889 case GST_ITERATOR_ERROR:
1890 /* This can't happen if the parameters to gst_iterator_next() are valid */
1891 g_assert_not_reached ();
1895 g_value_unset (&padptr);
1896 gst_iterator_free (pads);
1898 GST_DEBUG_OBJECT (aggregator,
1899 "Best pad found with %" GST_TIME_FORMAT ": %" GST_PTR_FORMAT,
1900 GST_TIME_ARGS (best_ts), best);
1906 static GstFlowReturn
1907 gst_flv_mux_aggregate (GstAggregator * aggregator, gboolean timeout)
1909 GstFlvMux *mux = GST_FLV_MUX (aggregator);
1911 gint64 best_time = GST_CLOCK_STIME_NONE;
1914 GstBuffer *buffer = NULL;
1916 if (mux->state == GST_FLV_MUX_STATE_HEADER) {
1917 if (GST_ELEMENT_CAST (mux)->sinkpads == NULL) {
1918 GST_ELEMENT_ERROR (mux, STREAM, MUX, (NULL),
1919 ("No input streams configured"));
1920 return GST_FLOW_ERROR;
1923 ret = gst_flv_mux_write_header (mux);
1924 if (ret != GST_FLOW_OK)
1926 mux->state = GST_FLV_MUX_STATE_DATA;
1928 best = gst_flv_mux_find_best_pad (aggregator, &ts);
1930 if (!mux->streamable || mux->first_timestamp == GST_CLOCK_STIME_NONE) {
1931 if (best && GST_CLOCK_STIME_IS_VALID (ts))
1932 mux->first_timestamp = ts;
1934 mux->first_timestamp = 0;
1937 best = gst_flv_mux_find_best_pad (aggregator, &ts);
1940 if (mux->new_tags && mux->streamable) {
1941 GstBuffer *buf = gst_flv_mux_create_metadata (mux);
1943 gst_flv_mux_push (mux, buf);
1944 mux->new_tags = FALSE;
1948 buffer = gst_aggregator_pad_pop_buffer (GST_AGGREGATOR_PAD (best));
1951 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1952 (best)->segment, GST_BUFFER_DTS_OR_PTS (buffer));
1954 if (GST_CLOCK_STIME_IS_VALID (best->dts))
1955 best_time = best->dts - mux->first_timestamp;
1957 if (GST_BUFFER_PTS_IS_VALID (buffer))
1959 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1960 (best)->segment, GST_BUFFER_PTS (buffer));
1962 best->pts = best->dts;
1964 GST_LOG_OBJECT (best, "got buffer PTS %" GST_TIME_FORMAT " DTS %"
1965 GST_STIME_FORMAT, GST_TIME_ARGS (best->pts),
1966 GST_STIME_ARGS (best->dts));
1968 best_time = GST_CLOCK_STIME_NONE;
1971 /* The FLV timestamp is an int32 field. For non-live streams error out if a
1972 bigger timestamp is seen, for live the timestamp will get wrapped in
1973 gst_flv_mux_buffer_to_tag */
1974 if (!mux->streamable && (GST_CLOCK_STIME_IS_VALID (best_time))
1975 && best_time / GST_MSECOND > G_MAXINT32) {
1976 GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
1978 gst_buffer_unref (buffer);
1981 gst_object_unref (best);
1986 GstFlowReturn ret = gst_flv_mux_write_buffer (mux, best, buffer);
1987 gst_object_unref (best);
1990 if (gst_flv_mux_are_all_pads_eos (mux)) {
1991 gst_flv_mux_write_eos (mux);
1992 gst_flv_mux_rewrite_header (mux);
1993 return GST_FLOW_EOS;
2000 gst_flv_mux_get_property (GObject * object,
2001 guint prop_id, GValue * value, GParamSpec * pspec)
2003 GstFlvMux *mux = GST_FLV_MUX (object);
2006 case PROP_STREAMABLE:
2007 g_value_set_boolean (value, mux->streamable);
2009 case PROP_METADATACREATOR:
2010 g_value_set_string (value, mux->metadatacreator);
2013 g_value_set_string (value, mux->encoder);
2016 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2022 gst_flv_mux_set_property (GObject * object,
2023 guint prop_id, const GValue * value, GParamSpec * pspec)
2025 GstFlvMux *mux = GST_FLV_MUX (object);
2028 case PROP_STREAMABLE:
2029 mux->streamable = g_value_get_boolean (value);
2030 if (mux->streamable)
2031 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
2032 GST_TAG_MERGE_REPLACE);
2034 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
2035 GST_TAG_MERGE_KEEP);
2037 case PROP_METADATACREATOR:
2038 g_free (mux->metadatacreator);
2039 if (!g_value_get_string (value)) {
2040 GST_WARNING_OBJECT (mux, "metadatacreator property can not be NULL");
2041 mux->metadatacreator = g_strdup (DEFAULT_METADATACREATOR);
2043 mux->metadatacreator = g_value_dup_string (value);
2047 g_free (mux->encoder);
2048 if (!g_value_get_string (value)) {
2049 GST_WARNING_OBJECT (mux, "encoder property can not be NULL");
2050 mux->encoder = g_strdup (DEFAULT_METADATACREATOR);
2052 mux->encoder = g_value_dup_string (value);
2056 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2062 gst_flv_mux_get_next_time (GstAggregator * aggregator)
2064 GstFlvMux *mux = GST_FLV_MUX (aggregator);
2065 GstAggregatorPad *agg_audio_pad = GST_AGGREGATOR_PAD_CAST (mux->audio_pad);
2066 GstAggregatorPad *agg_video_pad = GST_AGGREGATOR_PAD_CAST (mux->video_pad);
2068 GST_OBJECT_LOCK (aggregator);
2069 if (mux->state == GST_FLV_MUX_STATE_HEADER &&
2070 ((mux->audio_pad && mux->audio_pad->codec == G_MAXUINT) ||
2071 (mux->video_pad && mux->video_pad->codec == G_MAXUINT)))
2074 if (!((agg_audio_pad && gst_aggregator_pad_has_buffer (agg_audio_pad)) ||
2075 (agg_video_pad && gst_aggregator_pad_has_buffer (agg_video_pad))))
2077 GST_OBJECT_UNLOCK (aggregator);
2079 return gst_aggregator_simple_get_next_time (aggregator);
2082 GST_OBJECT_UNLOCK (aggregator);
2083 return GST_CLOCK_TIME_NONE;
2086 static GstFlowReturn
2087 gst_flv_mux_update_src_caps (GstAggregator * aggregator,
2088 GstCaps * caps, GstCaps ** ret)
2090 GstFlvMux *mux = GST_FLV_MUX (aggregator);
2092 *ret = gst_flv_mux_prepare_src_caps (mux, NULL, NULL, NULL, NULL);