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);
136 gst_flv_mux_pad_flush (GstAggregatorPad * pad, GstAggregator * aggregator)
138 GstFlvMuxPad *flvpad = GST_FLV_MUX_PAD (pad);
140 flvpad->last_timestamp = 0;
141 flvpad->pts = GST_CLOCK_STIME_NONE;
142 flvpad->dts = GST_CLOCK_STIME_NONE;
148 gst_flv_mux_pad_class_init (GstFlvMuxPadClass * klass)
150 GstAggregatorPadClass *aggregatorpad_class = (GstAggregatorPadClass *) klass;
151 GObjectClass *gobject_class = (GObjectClass *) klass;
153 gobject_class->finalize = gst_flv_mux_pad_finalize;
155 aggregatorpad_class->flush = GST_DEBUG_FUNCPTR (gst_flv_mux_pad_flush);
159 gst_flv_mux_pad_init (GstFlvMuxPad * pad)
161 gst_flv_mux_reset_pad (pad);
168 } GstFlvMuxIndexEntry;
171 gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
173 g_slice_free (GstFlvMuxIndexEntry, entry);
177 _gst_buffer_new_wrapped (gpointer mem, gsize size, GFreeFunc free_func)
181 buf = gst_buffer_new ();
182 gst_buffer_append_memory (buf,
183 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
184 mem, size, 0, size, mem, free_func));
190 _gst_buffer_new_and_alloc (gsize size, GstBuffer ** buffer, guint8 ** data)
192 g_return_if_fail (data != NULL);
193 g_return_if_fail (buffer != NULL);
195 *data = g_malloc (size);
196 *buffer = _gst_buffer_new_wrapped (*data, size, g_free);
200 gst_flv_mux_class_init (GstFlvMuxClass * klass)
202 GObjectClass *gobject_class;
203 GstElementClass *gstelement_class;
204 GstAggregatorClass *gstaggregator_class;
206 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
208 gobject_class = (GObjectClass *) klass;
209 gstelement_class = (GstElementClass *) klass;
210 gstaggregator_class = (GstAggregatorClass *) klass;
212 gobject_class->get_property = gst_flv_mux_get_property;
213 gobject_class->set_property = gst_flv_mux_set_property;
214 gobject_class->finalize = gst_flv_mux_finalize;
216 /* FIXME: ideally the right mode of operation should be detected
217 * automatically using queries when parameter not specified. */
219 * GstFlvMux:streamable
221 * If True, the output will be streaming friendly. (ie without indexes and
224 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
225 g_param_spec_boolean ("streamable", "streamable",
226 "If set to true, the output should be as if it is to be streamed "
227 "and hence no indexes written or duration written.",
228 DEFAULT_STREAMABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class, PROP_METADATACREATOR,
230 g_param_spec_string ("metadatacreator", "metadatacreator",
231 "The value of metadatacreator in the meta packet.",
232 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_ENCODER,
235 g_param_spec_string ("encoder", "encoder",
236 "The value of encoder in the meta packet.",
237 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 gstaggregator_class->create_new_pad =
240 GST_DEBUG_FUNCPTR (gst_flv_mux_create_new_pad);
241 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
243 gstaggregator_class->start = GST_DEBUG_FUNCPTR (gst_flv_mux_start);
244 gstaggregator_class->aggregate = GST_DEBUG_FUNCPTR (gst_flv_mux_aggregate);
245 gstaggregator_class->sink_event = GST_DEBUG_FUNCPTR (gst_flv_mux_sink_event);
246 gstaggregator_class->flush = GST_DEBUG_FUNCPTR (gst_flv_mux_flush);
247 gstaggregator_class->get_next_time =
248 GST_DEBUG_FUNCPTR (gst_flv_mux_get_next_time);
249 gstaggregator_class->update_src_caps =
250 GST_DEBUG_FUNCPTR (gst_flv_mux_update_src_caps);
252 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
253 &videosink_templ, GST_TYPE_FLV_MUX_PAD);
254 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
255 &audiosink_templ, GST_TYPE_FLV_MUX_PAD);
256 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
257 &src_templ, GST_TYPE_AGGREGATOR_PAD);
258 gst_element_class_set_static_metadata (gstelement_class, "FLV muxer",
260 "Muxes video/audio streams into a FLV stream",
261 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
263 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
267 gst_flv_mux_init (GstFlvMux * mux)
269 mux->srcpad = GST_AGGREGATOR_CAST (mux)->srcpad;
272 mux->streamable = DEFAULT_STREAMABLE;
273 mux->metadatacreator = g_strdup (DEFAULT_METADATACREATOR);
274 mux->encoder = g_strdup (DEFAULT_METADATACREATOR);
276 mux->new_tags = FALSE;
278 gst_flv_mux_reset (GST_ELEMENT (mux));
282 gst_flv_mux_finalize (GObject * object)
284 GstFlvMux *mux = GST_FLV_MUX (object);
286 gst_flv_mux_reset (GST_ELEMENT (object));
287 g_free (mux->metadatacreator);
288 g_free (mux->encoder);
290 G_OBJECT_CLASS (gst_flv_mux_parent_class)->finalize (object);
294 gst_flv_mux_pad_finalize (GObject * object)
296 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (object);
298 gst_flv_mux_reset_pad (pad);
300 G_OBJECT_CLASS (gst_flv_mux_pad_parent_class)->finalize (object);
304 gst_flv_mux_flush (GstAggregator * aggregator)
306 /* TODO: What is the right behaviour on flush? Should we just ignore it ?
307 * This still needs to be defined. */
309 gst_flv_mux_reset (GST_ELEMENT (aggregator));
314 gst_flv_mux_start (GstAggregator * aggregator)
316 gst_flv_mux_reset (GST_ELEMENT (aggregator));
321 gst_flv_mux_reset (GstElement * element)
323 GstFlvMux *mux = GST_FLV_MUX (element);
325 g_list_foreach (mux->index, (GFunc) gst_flv_mux_index_entry_free, NULL);
326 g_list_free (mux->index);
330 mux->duration = GST_CLOCK_TIME_NONE;
331 mux->new_tags = FALSE;
332 mux->first_timestamp = GST_CLOCK_STIME_NONE;
335 mux->state = GST_FLV_MUX_STATE_HEADER;
336 mux->sent_header = FALSE;
339 gst_tag_setter_reset_tags (GST_TAG_SETTER (mux));
342 /* Extract per-codec relevant tags for
343 * insertion into the metadata later - ie bitrate,
344 * but maybe others in the future */
346 gst_flv_mux_store_codec_tags (GstFlvMux * mux,
347 GstFlvMuxPad * flvpad, GstTagList * list)
349 /* Look for a bitrate as either nominal or actual bitrate tag */
350 if (gst_tag_list_get_uint (list, GST_TAG_NOMINAL_BITRATE, &flvpad->bitrate)
351 || gst_tag_list_get_uint (list, GST_TAG_BITRATE, &flvpad->bitrate)) {
352 GST_DEBUG_OBJECT (mux, "Stored bitrate for pad %" GST_PTR_FORMAT " = %u",
353 flvpad, flvpad->bitrate);
358 gst_flv_mux_sink_event (GstAggregator * aggregator, GstAggregatorPad * pad,
361 GstFlvMux *mux = GST_FLV_MUX (aggregator);
362 GstFlvMuxPad *flvpad = (GstFlvMuxPad *) pad;
365 switch (GST_EVENT_TYPE (event)) {
370 gst_event_parse_caps (event, &caps);
372 if (mux->video_pad == flvpad) {
373 ret = gst_flv_mux_video_pad_setcaps (flvpad, caps);
374 } else if (mux->audio_pad == flvpad) {
375 ret = gst_flv_mux_audio_pad_setcaps (flvpad, caps);
377 g_assert_not_reached ();
383 GstTagSetter *setter = GST_TAG_SETTER (mux);
384 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
386 gst_event_parse_tag (event, &list);
387 gst_tag_setter_merge_tags (setter, list, mode);
388 gst_flv_mux_store_codec_tags (mux, flvpad, list);
389 mux->new_tags = TRUE;
400 return GST_AGGREGATOR_CLASS (parent_class)->sink_event (aggregator, pad,
405 gst_flv_mux_video_pad_setcaps (GstFlvMuxPad * pad, GstCaps * caps)
407 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
411 GstBuffer *old_codec_data = NULL;
413 old_codec = pad->codec;
415 old_codec_data = gst_buffer_ref (pad->codec_data);
417 s = gst_caps_get_structure (caps, 0);
419 if (strcmp (gst_structure_get_name (s), "video/x-flash-video") == 0) {
421 } else if (strcmp (gst_structure_get_name (s), "video/x-flash-screen") == 0) {
423 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-flash") == 0) {
425 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-alpha") == 0) {
427 } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
433 if (ret && gst_structure_has_field (s, "codec_data")) {
434 const GValue *val = gst_structure_get_value (s, "codec_data");
437 gst_buffer_replace (&pad->codec_data, gst_value_get_buffer (val));
438 else if (!val && pad->codec_data)
439 gst_buffer_unref (pad->codec_data);
442 if (ret && mux->streamable && mux->state != GST_FLV_MUX_STATE_HEADER) {
443 if (old_codec != pad->codec) {
444 pad->info_changed = TRUE;
447 if (old_codec_data && pad->codec_data) {
450 gst_buffer_map (old_codec_data, &map, GST_MAP_READ);
451 if (map.size != gst_buffer_get_size (pad->codec_data) ||
452 gst_buffer_memcmp (pad->codec_data, 0, map.data, map.size))
453 pad->info_changed = TRUE;
455 gst_buffer_unmap (old_codec_data, &map);
456 } else if (!old_codec_data && pad->codec_data) {
457 pad->info_changed = TRUE;
460 if (pad->info_changed)
461 mux->state = GST_FLV_MUX_STATE_HEADER;
465 gst_buffer_unref (old_codec_data);
467 gst_object_unref (mux);
473 gst_flv_mux_audio_pad_setcaps (GstFlvMuxPad * pad, GstCaps * caps)
475 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
478 guint old_codec, old_rate, old_width, old_channels;
479 GstBuffer *old_codec_data = NULL;
481 old_codec = pad->codec;
482 old_rate = pad->rate;
483 old_width = pad->width;
484 old_channels = pad->channels;
486 old_codec_data = gst_buffer_ref (pad->codec_data);
488 s = gst_caps_get_structure (caps, 0);
490 if (strcmp (gst_structure_get_name (s), "audio/x-adpcm") == 0) {
491 const gchar *layout = gst_structure_get_string (s, "layout");
492 if (layout && strcmp (layout, "swf") == 0) {
497 } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
500 if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
501 if (mpegversion == 1) {
504 if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
507 if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
514 } else if (mpegversion == 4 || mpegversion == 2) {
522 } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
525 if (gst_structure_get_int (s, "rate", &rate)
526 && gst_structure_get_int (s, "channels", &channels)) {
527 if (channels == 1 && rate == 16000)
529 else if (channels == 1 && rate == 8000)
536 } else if (strcmp (gst_structure_get_name (s), "audio/x-raw") == 0) {
539 if (gst_audio_info_from_caps (&info, caps)) {
542 if (GST_AUDIO_INFO_WIDTH (&info) == 8)
544 else if (GST_AUDIO_INFO_WIDTH (&info) == 16)
550 } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
552 } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
554 } else if (strcmp (gst_structure_get_name (s), "audio/x-speex") == 0) {
563 if (gst_structure_get_int (s, "rate", &rate)) {
564 if (pad->codec == 10)
566 else if (rate == 5512)
568 else if (rate == 11025)
570 else if (rate == 22050)
572 else if (rate == 44100)
574 else if (rate == 8000 && (pad->codec == 5 || pad->codec == 14
575 || pad->codec == 7 || pad->codec == 8))
577 else if (rate == 16000 && (pad->codec == 4 || pad->codec == 11))
581 } else if (pad->codec == 10) {
587 if (gst_structure_get_int (s, "channels", &channels)) {
588 if (pad->codec == 4 || pad->codec == 5
589 || pad->codec == 6 || pad->codec == 11)
591 else if (pad->codec == 10)
593 else if (channels == 1)
595 else if (channels == 2)
599 } else if (pad->codec == 4 || pad->codec == 5 || pad->codec == 6) {
601 } else if (pad->codec == 10) {
611 if (ret && gst_structure_has_field (s, "codec_data")) {
612 const GValue *val = gst_structure_get_value (s, "codec_data");
615 gst_buffer_replace (&pad->codec_data, gst_value_get_buffer (val));
616 else if (!val && pad->codec_data)
617 gst_buffer_unref (pad->codec_data);
620 if (ret && mux->streamable && mux->state != GST_FLV_MUX_STATE_HEADER) {
621 if (old_codec != pad->codec || old_rate != pad->rate ||
622 old_width != pad->width || old_channels != pad->channels) {
623 pad->info_changed = TRUE;
626 if (old_codec_data && pad->codec_data) {
629 gst_buffer_map (old_codec_data, &map, GST_MAP_READ);
630 if (map.size != gst_buffer_get_size (pad->codec_data) ||
631 gst_buffer_memcmp (pad->codec_data, 0, map.data, map.size))
632 pad->info_changed = TRUE;
634 gst_buffer_unmap (old_codec_data, &map);
635 } else if (!old_codec_data && pad->codec_data) {
636 pad->info_changed = TRUE;
639 if (pad->info_changed)
640 mux->state = GST_FLV_MUX_STATE_HEADER;
644 gst_buffer_unref (old_codec_data);
646 gst_object_unref (mux);
652 gst_flv_mux_reset_pad (GstFlvMuxPad * pad)
654 GST_DEBUG_OBJECT (pad, "resetting pad");
657 gst_buffer_unref (pad->codec_data);
658 pad->codec_data = NULL;
659 pad->codec = G_MAXUINT;
660 pad->rate = G_MAXUINT;
661 pad->width = G_MAXUINT;
662 pad->channels = G_MAXUINT;
663 pad->info_changed = FALSE;
665 gst_flv_mux_pad_flush (GST_AGGREGATOR_PAD_CAST (pad), NULL);
668 static GstAggregatorPad *
669 gst_flv_mux_create_new_pad (GstAggregator * agg,
670 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
672 GstElementClass *klass = GST_ELEMENT_GET_CLASS (agg);
673 GstAggregatorPad *aggpad;
674 GstFlvMux *mux = GST_FLV_MUX (agg);
675 GstFlvMuxPad *pad = NULL;
676 const gchar *name = NULL;
679 if (mux->state != GST_FLV_MUX_STATE_HEADER) {
680 GST_WARNING_OBJECT (mux, "Can't request pads after writing header");
684 if (templ == gst_element_class_get_pad_template (klass, "audio")) {
685 if (mux->audio_pad) {
686 GST_WARNING_OBJECT (mux, "Already have an audio pad");
691 } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
692 if (mux->video_pad) {
693 GST_WARNING_OBJECT (mux, "Already have a video pad");
699 GST_WARNING_OBJECT (mux, "Invalid template");
704 GST_AGGREGATOR_CLASS (gst_flv_mux_parent_class)->create_new_pad (agg,
709 pad = GST_FLV_MUX_PAD (aggpad);
711 gst_flv_mux_reset_pad (pad);
714 mux->video_pad = pad;
716 mux->audio_pad = pad;
722 gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
724 GstFlvMux *mux = GST_FLV_MUX (element);
725 GstFlvMuxPad *flvpad = GST_FLV_MUX_PAD (pad);
727 gst_pad_set_active (pad, FALSE);
728 gst_flv_mux_reset_pad (flvpad);
730 if (flvpad == mux->video_pad) {
731 mux->video_pad = NULL;
732 } else if (flvpad == mux->audio_pad) {
733 mux->audio_pad = NULL;
735 GST_WARNING_OBJECT (pad, "Pad is not known audio or video pad");
738 gst_element_remove_pad (element, pad);
742 gst_flv_mux_push (GstFlvMux * mux, GstBuffer * buffer)
744 GstAggregator *agg = GST_AGGREGATOR (mux);
745 GstAggregatorPad *srcpad = GST_AGGREGATOR_PAD (agg->srcpad);
747 if (GST_BUFFER_PTS_IS_VALID (buffer))
748 srcpad->segment.position = GST_BUFFER_PTS (buffer);
750 /* pushing the buffer that rewrites the header will make it no longer be the
751 * total output size in bytes, but it doesn't matter at that point */
752 mux->byte_count += gst_buffer_get_size (buffer);
754 return gst_aggregator_finish_buffer (GST_AGGREGATOR_CAST (mux), buffer);
758 gst_flv_mux_create_header (GstFlvMux * mux)
765 _gst_buffer_new_and_alloc (9 + 4, &header, &data);
770 data[3] = 0x01; /* Version */
772 have_audio = (mux->audio_pad && mux->audio_pad->codec != G_MAXUINT);
773 have_video = (mux->video_pad && mux->video_pad->codec != G_MAXUINT);
775 data[4] = (have_audio << 2) | have_video; /* flags */
776 GST_WRITE_UINT32_BE (data + 5, 9); /* data offset */
777 GST_WRITE_UINT32_BE (data + 9, 0); /* previous tag size */
783 gst_flv_mux_preallocate_index (GstFlvMux * mux)
787 gint preallocate_size;
789 /* preallocate index of size:
790 * - 'keyframes' ECMA array key: 2 + 9 = 11 bytes
791 * - nested ECMA array header, length and end marker: 8 bytes
792 * - 'times' and 'filepositions' keys: 22 bytes
793 * - two strict arrays headers and lengths: 10 bytes
794 * - each index entry: 18 bytes
796 preallocate_size = 11 + 8 + 22 + 10 + MAX_INDEX_ENTRIES * 18;
797 GST_DEBUG_OBJECT (mux, "preallocating %d bytes for the index",
800 _gst_buffer_new_and_alloc (preallocate_size, &tmp, &data);
802 /* prefill the space with a gstfiller: <spaces> script tag variable */
803 GST_WRITE_UINT16_BE (data, 9); /* 9 characters */
804 memcpy (data + 2, "gstfiller", 9);
805 GST_WRITE_UINT8 (data + 11, AMF0_STRING_MARKER); /* a string value */
806 GST_WRITE_UINT16_BE (data + 12, preallocate_size - 14);
807 memset (data + 14, ' ', preallocate_size - 14); /* the rest is spaces */
812 gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
816 gsize len = strlen (name);
818 _gst_buffer_new_and_alloc (2 + len + 1 + 8, &tmp, &data);
820 GST_WRITE_UINT16_BE (data, len);
821 data += 2; /* name length */
822 memcpy (data, name, len);
824 *data++ = AMF0_NUMBER_MARKER; /* double type */
825 GST_WRITE_DOUBLE_BE (data, value);
831 gst_flv_mux_create_metadata (GstFlvMux * mux, gboolean full)
833 const GstTagList *tags;
834 GstBuffer *script_tag, *tmp;
837 gint i, n_tags, tags_written = 0;
839 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (mux));
841 GST_DEBUG_OBJECT (mux, "tags = %" GST_PTR_FORMAT, tags);
843 /* FIXME perhaps some bytewriter'ing here ... */
845 _gst_buffer_new_and_alloc (11, &script_tag, &data);
849 /* Data size, unknown for now */
855 data[4] = data[5] = data[6] = data[7] = 0;
858 data[8] = data[9] = data[10] = 0;
860 _gst_buffer_new_and_alloc (13, &tmp, &data);
861 data[0] = AMF0_STRING_MARKER; /* string */
863 data[2] = 10; /* length 10 */
864 memcpy (&data[3], "onMetaData", 10);
866 script_tag = gst_buffer_append (script_tag, tmp);
868 n_tags = (tags) ? gst_tag_list_n_tags (tags) : 0;
869 _gst_buffer_new_and_alloc (5, &tmp, &data);
870 data[0] = 8; /* ECMA array */
871 GST_WRITE_UINT32_BE (data + 1, n_tags);
872 script_tag = gst_buffer_append (script_tag, tmp);
877 /* Some players expect the 'duration' to be always set. Fill it out later,
878 after querying the pads or after getting EOS */
879 if (!mux->streamable) {
880 tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
881 script_tag = gst_buffer_append (script_tag, tmp);
884 /* Sometimes the information about the total file size is useful for the
885 player. It will be filled later, after getting EOS */
886 tmp = gst_flv_mux_create_number_script_value ("filesize", 0);
887 script_tag = gst_buffer_append (script_tag, tmp);
890 /* Preallocate space for the index to be written at EOS */
891 tmp = gst_flv_mux_preallocate_index (mux);
892 script_tag = gst_buffer_append (script_tag, tmp);
894 GST_DEBUG_OBJECT (mux, "not preallocating index, streamable mode");
898 for (i = 0; tags && i < n_tags; i++) {
899 const gchar *tag_name = gst_tag_list_nth_tag_name (tags, i);
900 if (!strcmp (tag_name, GST_TAG_DURATION)) {
903 if (!gst_tag_list_get_uint64 (tags, GST_TAG_DURATION, &dur))
906 } else if (!strcmp (tag_name, GST_TAG_ARTIST) ||
907 !strcmp (tag_name, GST_TAG_TITLE)) {
909 const gchar *t = NULL;
911 if (!strcmp (tag_name, GST_TAG_ARTIST))
913 else if (!strcmp (tag_name, GST_TAG_TITLE))
916 if (!gst_tag_list_get_string (tags, tag_name, &s))
919 _gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s),
921 data[0] = 0; /* tag name length */
922 data[1] = strlen (t);
923 memcpy (&data[2], t, strlen (t));
924 data[2 + strlen (t)] = 2; /* string */
925 data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
926 data[4 + strlen (t)] = (strlen (s)) & 0xff;
927 memcpy (&data[5 + strlen (t)], s, strlen (s));
928 script_tag = gst_buffer_append (script_tag, tmp);
938 if (!mux->streamable && mux->duration == GST_CLOCK_TIME_NONE) {
942 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l; l = l->next) {
943 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
945 if (gst_pad_peer_query_duration (GST_PAD (pad), GST_FORMAT_TIME,
946 (gint64 *) & dur) && dur != GST_CLOCK_TIME_NONE) {
947 if (mux->duration == GST_CLOCK_TIME_NONE)
950 mux->duration = MAX (dur, mux->duration);
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 tm = gmtime (&secs);
1100 s = g_strdup_printf ("%s %s %d %02d:%02d:%02d %d", weekdays[tm->tm_wday],
1101 months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
1102 tm->tm_year + 1900);
1104 _gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s), &tmp, &data);
1105 data[0] = 0; /* 12 bytes name */
1107 memcpy (&data[2], "creationdate", 12);
1108 data[14] = 2; /* string */
1109 data[15] = (strlen (s) >> 8) & 0xff;
1110 data[16] = (strlen (s)) & 0xff;
1111 memcpy (&data[17], s, strlen (s));
1112 script_tag = gst_buffer_append (script_tag, tmp);
1120 if (!tags_written) {
1121 gst_buffer_unref (script_tag);
1126 _gst_buffer_new_and_alloc (2 + 0 + 1, &tmp, &data);
1127 data[0] = 0; /* 0 byte size */
1129 data[2] = 9; /* end marker */
1130 script_tag = gst_buffer_append (script_tag, tmp);
1133 _gst_buffer_new_and_alloc (4, &tmp, &data);
1134 GST_WRITE_UINT32_BE (data, gst_buffer_get_size (script_tag));
1135 script_tag = gst_buffer_append (script_tag, tmp);
1137 gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
1138 map.data[1] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 16) & 0xff;
1139 map.data[2] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 8) & 0xff;
1140 map.data[3] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 0) & 0xff;
1142 GST_WRITE_UINT32_BE (map.data + 11 + 13 + 1, tags_written);
1143 gst_buffer_unmap (script_tag, &map);
1150 gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
1151 GstFlvMuxPad * pad, gboolean is_codec_data)
1156 guint32 pts, dts, cts;
1157 guint8 *data, *bdata = NULL;
1160 if (!GST_CLOCK_STIME_IS_VALID (pad->dts)) {
1161 pts = dts = pad->last_timestamp / GST_MSECOND;
1163 pts = pad->pts / GST_MSECOND;
1164 dts = pad->dts / GST_MSECOND;
1167 /* We prevent backwards timestamps because they confuse librtmp,
1168 * it expects timestamps to go forward not only inside one stream, but
1169 * also between the audio & video streams.
1171 if (dts < mux->last_dts) {
1172 GST_WARNING_OBJECT (pad, "Got backwards dts! (%" GST_TIME_FORMAT
1173 " < %" GST_TIME_FORMAT ")", GST_TIME_ARGS (dts * GST_MSECOND),
1174 GST_TIME_ARGS (mux->last_dts * GST_MSECOND));
1175 dts = mux->last_dts;
1177 mux->last_dts = dts;
1180 /* Be safe in case TS are buggy */
1186 /* Timestamp must start at zero */
1187 if (GST_CLOCK_STIME_IS_VALID (mux->first_timestamp)) {
1188 dts -= mux->first_timestamp / GST_MSECOND;
1192 GST_LOG_OBJECT (mux, "got pts %i dts %i cts %i", pts, dts, cts);
1194 if (buffer != NULL) {
1195 gst_buffer_map (buffer, &map, GST_MAP_READ);
1201 if (mux->video_pad == pad) {
1203 if (pad->codec == 7)
1209 if (pad->codec == 10)
1216 _gst_buffer_new_and_alloc (size, &tag, &data);
1217 memset (data, 0, size);
1219 data[0] = (mux->video_pad == pad) ? 9 : 8;
1221 data[1] = ((size - 11 - 4) >> 16) & 0xff;
1222 data[2] = ((size - 11 - 4) >> 8) & 0xff;
1223 data[3] = ((size - 11 - 4) >> 0) & 0xff;
1226 GST_WRITE_UINT24_BE (data + 4, dts);
1227 data[7] = (((guint) dts) >> 24) & 0xff;
1229 data[8] = data[9] = data[10] = 0;
1231 if (mux->video_pad == pad) {
1232 if (buffer && GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1237 data[11] |= pad->codec & 0x0f;
1239 if (pad->codec == 7) {
1240 if (is_codec_data) {
1242 GST_WRITE_UINT24_BE (data + 13, 0);
1243 } else if (bsize == 0) {
1244 /* AVC end of sequence */
1246 GST_WRITE_UINT24_BE (data + 13, 0);
1250 GST_WRITE_UINT24_BE (data + 13, cts);
1252 memcpy (data + 11 + 1 + 4, bdata, bsize);
1254 memcpy (data + 11 + 1, bdata, bsize);
1257 data[11] |= (pad->codec << 4) & 0xf0;
1258 data[11] |= (pad->rate << 2) & 0x0c;
1259 data[11] |= (pad->width << 1) & 0x02;
1260 data[11] |= (pad->channels << 0) & 0x01;
1262 GST_DEBUG_OBJECT (mux, "Creating byte %02x with "
1263 "codec:%d, rate:%d, width:%d, channels:%d",
1264 data[11], pad->codec, pad->rate, pad->width, pad->channels);
1266 if (pad->codec == 10) {
1267 data[12] = is_codec_data ? 0 : 1;
1269 memcpy (data + 11 + 1 + 1, bdata, bsize);
1271 memcpy (data + 11 + 1, bdata, bsize);
1276 gst_buffer_unmap (buffer, &map);
1278 GST_WRITE_UINT32_BE (data + size - 4, size - 4);
1280 GST_BUFFER_PTS (tag) = GST_CLOCK_TIME_NONE;
1281 GST_BUFFER_DTS (tag) = GST_CLOCK_TIME_NONE;
1282 GST_BUFFER_DURATION (tag) = GST_CLOCK_TIME_NONE;
1285 /* if we are streamable we copy over timestamps and offsets,
1286 if not just copy the offsets */
1287 if (mux->streamable) {
1288 GstClockTime timestamp = GST_CLOCK_TIME_NONE;
1290 if (gst_segment_to_running_time_full (&GST_AGGREGATOR_PAD (pad)->segment,
1291 GST_FORMAT_TIME, GST_BUFFER_DTS_OR_PTS (buffer),
1293 GST_BUFFER_PTS (tag) = timestamp;
1294 GST_BUFFER_DURATION (tag) = GST_BUFFER_DURATION (buffer);
1296 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_NONE;
1297 GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_NONE;
1299 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET (buffer);
1300 GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_END (buffer);
1303 /* mark the buffer if it's an audio buffer and there's also video being muxed
1304 * or it's a video interframe */
1305 if (mux->video_pad == pad &&
1306 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1307 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1309 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1310 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
1311 GST_BUFFER_OFFSET_NONE;
1317 static inline GstBuffer *
1318 gst_flv_mux_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1321 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, pad, FALSE);
1324 static inline GstBuffer *
1325 gst_flv_mux_codec_data_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1328 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, pad, TRUE);
1331 static inline GstBuffer *
1332 gst_flv_mux_eos_to_tag (GstFlvMux * mux, GstFlvMuxPad * pad)
1334 return gst_flv_mux_buffer_to_tag_internal (mux, NULL, pad, FALSE);
1338 gst_flv_mux_put_buffer_in_streamheader (GValue * streamheader,
1341 GValue value = { 0 };
1344 g_value_init (&value, GST_TYPE_BUFFER);
1345 buf = gst_buffer_copy (buffer);
1346 gst_value_set_buffer (&value, buf);
1347 gst_buffer_unref (buf);
1348 gst_value_array_append_value (streamheader, &value);
1349 g_value_unset (&value);
1353 gst_flv_mux_prepare_src_caps (GstFlvMux * mux, GstBuffer ** header_buf,
1354 GstBuffer ** metadata_buf, GstBuffer ** video_codec_data_buf,
1355 GstBuffer ** audio_codec_data_buf)
1357 GstBuffer *header, *metadata;
1358 GstBuffer *video_codec_data, *audio_codec_data;
1360 GstStructure *structure;
1361 GValue streamheader = { 0 };
1364 header = gst_flv_mux_create_header (mux);
1365 metadata = gst_flv_mux_create_metadata (mux, TRUE);
1366 video_codec_data = NULL;
1367 audio_codec_data = NULL;
1369 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l != NULL; l = l->next) {
1370 GstFlvMuxPad *pad = l->data;
1372 /* Get H.264 and AAC codec data, if present */
1373 if (pad && mux->video_pad == pad && pad->codec == 7) {
1374 if (pad->codec_data == NULL)
1375 GST_WARNING_OBJECT (mux, "Codec data for video stream not found, "
1376 "output might not be playable");
1379 gst_flv_mux_codec_data_buffer_to_tag (mux, pad->codec_data, pad);
1380 } else if (pad && mux->audio_pad == pad && pad->codec == 10) {
1381 if (pad->codec_data == NULL)
1382 GST_WARNING_OBJECT (mux, "Codec data for audio stream not found, "
1383 "output might not be playable");
1386 gst_flv_mux_codec_data_buffer_to_tag (mux, pad->codec_data, pad);
1390 /* mark buffers that will go in the streamheader */
1391 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_HEADER);
1392 GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_HEADER);
1393 if (video_codec_data != NULL) {
1394 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_HEADER);
1395 /* mark as a delta unit, so downstream will not try to synchronize on that
1396 * buffer - to actually start playback you need a real video keyframe */
1397 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_DELTA_UNIT);
1399 if (audio_codec_data != NULL) {
1400 GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_HEADER);
1403 /* put buffers in streamheader */
1404 g_value_init (&streamheader, GST_TYPE_ARRAY);
1405 gst_flv_mux_put_buffer_in_streamheader (&streamheader, header);
1406 gst_flv_mux_put_buffer_in_streamheader (&streamheader, metadata);
1407 if (video_codec_data != NULL)
1408 gst_flv_mux_put_buffer_in_streamheader (&streamheader, video_codec_data);
1409 if (audio_codec_data != NULL)
1410 gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);
1412 /* create the caps and put the streamheader in them */
1413 caps = gst_caps_new_empty_simple ("video/x-flv");
1414 structure = gst_caps_get_structure (caps, 0);
1415 gst_structure_set_value (structure, "streamheader", &streamheader);
1416 g_value_unset (&streamheader);
1419 *header_buf = header;
1421 gst_buffer_unref (header);
1425 *metadata_buf = metadata;
1427 gst_buffer_unref (metadata);
1430 if (video_codec_data_buf) {
1431 *video_codec_data_buf = video_codec_data;
1432 } else if (video_codec_data) {
1433 gst_buffer_unref (video_codec_data);
1436 if (audio_codec_data_buf) {
1437 *audio_codec_data_buf = audio_codec_data;
1438 } else if (audio_codec_data) {
1439 gst_buffer_unref (audio_codec_data);
1445 static GstFlowReturn
1446 gst_flv_mux_write_header (GstFlvMux * mux)
1448 GstBuffer *header, *metadata;
1449 GstBuffer *video_codec_data, *audio_codec_data;
1453 header = metadata = video_codec_data = audio_codec_data = NULL;
1455 /* if not streaming, check if downstream is seekable */
1456 if (!mux->streamable) {
1460 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1461 if (gst_pad_peer_query (mux->srcpad, query)) {
1462 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1463 GST_INFO_OBJECT (mux, "downstream is %sseekable", seekable ? "" : "not ");
1465 /* have to assume seeking is supported if query not handled downstream */
1466 GST_WARNING_OBJECT (mux, "downstream did not handle seeking query");
1470 mux->streamable = TRUE;
1471 g_object_notify (G_OBJECT (mux), "streamable");
1472 GST_WARNING_OBJECT (mux, "downstream is not seekable, but "
1473 "streamable=false. Will ignore that and create streamable output "
1476 gst_query_unref (query);
1479 if (mux->streamable && mux->sent_header) {
1480 GstBuffer **video_codec_data_p = NULL, **audio_codec_data_p = NULL;
1482 if (mux->video_pad && mux->video_pad->info_changed)
1483 video_codec_data_p = &video_codec_data;
1484 if (mux->audio_pad && mux->audio_pad->info_changed)
1485 audio_codec_data_p = &audio_codec_data;
1487 caps = gst_flv_mux_prepare_src_caps (mux,
1488 NULL, NULL, video_codec_data_p, audio_codec_data_p);
1490 caps = gst_flv_mux_prepare_src_caps (mux,
1491 &header, &metadata, &video_codec_data, &audio_codec_data);
1494 gst_aggregator_set_src_caps (GST_AGGREGATOR_CAST (mux), caps);
1496 gst_caps_unref (caps);
1498 /* push the header buffer, the metadata and the codec info, if any */
1499 if (header != NULL) {
1500 ret = gst_flv_mux_push (mux, header);
1501 if (ret != GST_FLOW_OK)
1502 goto failure_header;
1503 mux->sent_header = TRUE;
1505 if (metadata != NULL) {
1506 ret = gst_flv_mux_push (mux, metadata);
1507 if (ret != GST_FLOW_OK)
1508 goto failure_metadata;
1509 mux->new_tags = FALSE;
1511 if (video_codec_data != NULL) {
1512 ret = gst_flv_mux_push (mux, video_codec_data);
1513 if (ret != GST_FLOW_OK)
1514 goto failure_video_codec_data;
1515 mux->video_pad->info_changed = FALSE;
1517 if (audio_codec_data != NULL) {
1518 ret = gst_flv_mux_push (mux, audio_codec_data);
1519 if (ret != GST_FLOW_OK)
1520 goto failure_audio_codec_data;
1521 mux->audio_pad->info_changed = FALSE;
1526 gst_buffer_unref (metadata);
1529 if (video_codec_data != NULL)
1530 gst_buffer_unref (video_codec_data);
1532 failure_video_codec_data:
1533 if (audio_codec_data != NULL)
1534 gst_buffer_unref (audio_codec_data);
1536 failure_audio_codec_data:
1541 gst_flv_mux_segment_to_running_time (const GstSegment * segment, GstClockTime t)
1543 /* we can get a dts before the segment, if dts < pts and pts is inside
1544 * the segment, so we consider early times as 0 */
1545 if (t < segment->start)
1547 return gst_segment_to_running_time (segment, GST_FORMAT_TIME, t);
1551 gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer,
1555 * Add the tag byte offset and to the index if it's a valid seek point, which
1556 * means it's either a video keyframe or if there is no video pad (in that
1557 * case every FLV tag is a valid seek point)
1559 if (mux->video_pad == pad &&
1560 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1563 if (GST_BUFFER_PTS_IS_VALID (buffer)) {
1564 GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
1566 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1567 (pad)->segment, GST_BUFFER_PTS (buffer));
1568 entry->position = mux->byte_count;
1569 entry->time = gst_guint64_to_gdouble (pts) / GST_SECOND;
1570 mux->index = g_list_prepend (mux->index, entry);
1574 static GstFlowReturn
1575 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvMuxPad * pad,
1581 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD (pad)->segment,
1582 GST_BUFFER_DTS (buffer));
1584 /* clipping function arranged for running_time */
1586 if (!mux->streamable)
1587 gst_flv_mux_update_index (mux, buffer, pad);
1589 tag = gst_flv_mux_buffer_to_tag (mux, buffer, pad);
1591 gst_buffer_unref (buffer);
1593 ret = gst_flv_mux_push (mux, tag);
1595 if (ret == GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (dts))
1596 pad->last_timestamp = dts;
1603 gst_flv_mux_determine_duration (GstFlvMux * mux)
1606 GstClockTime duration = GST_CLOCK_TIME_NONE;
1608 GST_DEBUG_OBJECT (mux, "trying to determine the duration "
1609 "from pad timestamps");
1611 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l != NULL; l = l->next) {
1612 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
1614 if (pad && (pad->last_timestamp != GST_CLOCK_TIME_NONE)) {
1615 if (duration == GST_CLOCK_TIME_NONE)
1616 duration = pad->last_timestamp;
1618 duration = MAX (duration, pad->last_timestamp);
1626 gst_flv_mux_are_all_pads_eos (GstFlvMux * mux)
1630 for (l = GST_ELEMENT_CAST (mux)->sinkpads; l; l = l->next) {
1631 GstFlvMuxPad *pad = GST_FLV_MUX_PAD (l->data);
1633 if (!gst_aggregator_pad_is_eos (GST_AGGREGATOR_PAD (pad)))
1639 static GstFlowReturn
1640 gst_flv_mux_write_eos (GstFlvMux * mux)
1644 if (mux->video_pad == NULL)
1647 tag = gst_flv_mux_eos_to_tag (mux, mux->video_pad);
1649 return gst_flv_mux_push (mux, tag);
1652 static GstFlowReturn
1653 gst_flv_mux_rewrite_header (GstFlvMux * mux)
1655 GstBuffer *rewrite, *index, *tmp;
1660 guint32 index_len, allocate_size;
1661 guint32 i, index_skip;
1665 if (mux->streamable)
1668 /* seek back to the preallocated index space */
1669 gst_segment_init (&segment, GST_FORMAT_BYTES);
1670 segment.start = segment.time = 13 + 29;
1671 event = gst_event_new_segment (&segment);
1672 if (!gst_pad_push_event (mux->srcpad, event)) {
1673 GST_WARNING_OBJECT (mux, "Seek to rewrite header failed");
1677 /* determine duration now based on our own timestamping,
1678 * so that it is likely many times better and consistent
1679 * than whatever obtained by some query */
1680 dur = gst_flv_mux_determine_duration (mux);
1681 if (dur != GST_CLOCK_TIME_NONE)
1682 mux->duration = dur;
1684 /* rewrite the duration tag */
1685 d = gst_guint64_to_gdouble (mux->duration);
1686 d /= (gdouble) GST_SECOND;
1688 GST_DEBUG_OBJECT (mux, "determined the final duration to be %f", d);
1690 rewrite = gst_flv_mux_create_number_script_value ("duration", d);
1692 /* rewrite the filesize tag */
1693 d = gst_guint64_to_gdouble (mux->byte_count);
1695 GST_DEBUG_OBJECT (mux, "putting total filesize %f in the metadata", d);
1697 tmp = gst_flv_mux_create_number_script_value ("filesize", d);
1698 rewrite = gst_buffer_append (rewrite, tmp);
1701 /* no index, so push buffer and return */
1702 return gst_flv_mux_push (mux, rewrite);
1705 /* rewrite the index */
1706 mux->index = g_list_reverse (mux->index);
1707 index_len = g_list_length (mux->index);
1709 /* We write at most MAX_INDEX_ENTRIES elements */
1710 if (index_len > MAX_INDEX_ENTRIES) {
1711 index_skip = 1 + index_len / MAX_INDEX_ENTRIES;
1712 index_len = (index_len + index_skip - 1) / index_skip;
1717 GST_DEBUG_OBJECT (mux, "Index length is %d", index_len);
1718 /* see size calculation in gst_flv_mux_preallocate_index */
1719 allocate_size = 11 + 8 + 22 + 10 + index_len * 18;
1720 GST_DEBUG_OBJECT (mux, "Allocating %d bytes for index", allocate_size);
1721 _gst_buffer_new_and_alloc (allocate_size, &index, &data);
1723 GST_WRITE_UINT16_BE (data, 9); /* the 'keyframes' key */
1724 memcpy (data + 2, "keyframes", 9);
1725 GST_WRITE_UINT8 (data + 11, 8); /* nested ECMA array */
1726 GST_WRITE_UINT32_BE (data + 12, 2); /* two elements */
1727 GST_WRITE_UINT16_BE (data + 16, 5); /* first string key: 'times' */
1728 memcpy (data + 18, "times", 5);
1729 GST_WRITE_UINT8 (data + 23, 10); /* strict array */
1730 GST_WRITE_UINT32_BE (data + 24, index_len);
1733 /* the keyframes' times */
1734 for (i = 0, l = mux->index; l; l = l->next, i++) {
1735 GstFlvMuxIndexEntry *entry = l->data;
1737 if (i % index_skip != 0)
1739 GST_WRITE_UINT8 (data, 0); /* numeric (aka double) */
1740 GST_WRITE_DOUBLE_BE (data + 1, entry->time);
1744 GST_WRITE_UINT16_BE (data, 13); /* second string key: 'filepositions' */
1745 memcpy (data + 2, "filepositions", 13);
1746 GST_WRITE_UINT8 (data + 15, 10); /* strict array */
1747 GST_WRITE_UINT32_BE (data + 16, index_len);
1750 /* the keyframes' file positions */
1751 for (i = 0, l = mux->index; l; l = l->next, i++) {
1752 GstFlvMuxIndexEntry *entry = l->data;
1754 if (i % index_skip != 0)
1756 GST_WRITE_UINT8 (data, 0);
1757 GST_WRITE_DOUBLE_BE (data + 1, entry->position);
1761 GST_WRITE_UINT24_BE (data, 9); /* finish the ECMA array */
1763 /* If there is space left in the prefilled area, reinsert the filler.
1764 There is at least 18 bytes free, so it will always fit. */
1765 if (index_len < MAX_INDEX_ENTRIES) {
1768 guint32 remaining_filler_size;
1770 _gst_buffer_new_and_alloc (14, &tmp, &data);
1771 GST_WRITE_UINT16_BE (data, 9);
1772 memcpy (data + 2, "gstfiller", 9);
1773 GST_WRITE_UINT8 (data + 11, 2); /* string */
1775 /* There is 18 bytes per remaining index entry minus what is used for
1776 * the'gstfiller' key. The rest is already filled with spaces, so just need
1777 * to update length. */
1778 remaining_filler_size = (MAX_INDEX_ENTRIES - index_len) * 18 - 14;
1779 GST_DEBUG_OBJECT (mux, "Remaining filler size is %d bytes",
1780 remaining_filler_size);
1781 GST_WRITE_UINT16_BE (data + 12, remaining_filler_size);
1782 index = gst_buffer_append (index, tmp);
1785 rewrite = gst_buffer_append (rewrite, index);
1787 return gst_flv_mux_push (mux, rewrite);
1790 static GstFlvMuxPad *
1791 gst_flv_mux_find_best_pad (GstAggregator * aggregator, GstClockTime * ts)
1793 GstAggregatorPad *apad;
1794 GstFlvMuxPad *pad, *best = NULL;
1797 GstClockTime best_ts = GST_CLOCK_TIME_NONE;
1799 for (l = GST_ELEMENT_CAST (aggregator)->sinkpads; l; l = l->next) {
1800 apad = GST_AGGREGATOR_PAD (l->data);
1801 pad = GST_FLV_MUX_PAD (l->data);
1802 buffer = gst_aggregator_pad_peek_buffer (GST_AGGREGATOR_PAD (pad));
1805 if (best_ts == GST_CLOCK_TIME_NONE) {
1807 best_ts = gst_flv_mux_segment_to_running_time (&apad->segment,
1808 GST_BUFFER_DTS_OR_PTS (buffer));
1809 } else if (GST_BUFFER_DTS_OR_PTS (buffer) != GST_CLOCK_TIME_NONE) {
1810 gint64 t = gst_flv_mux_segment_to_running_time (&apad->segment,
1811 GST_BUFFER_DTS_OR_PTS (buffer));
1817 gst_buffer_unref (buffer);
1819 GST_DEBUG_OBJECT (aggregator,
1820 "Best pad found with %" GST_TIME_FORMAT ": %" GST_PTR_FORMAT,
1821 GST_TIME_ARGS (best_ts), best);
1827 static GstFlowReturn
1828 gst_flv_mux_aggregate (GstAggregator * aggregator, gboolean timeout)
1830 GstFlvMux *mux = GST_FLV_MUX (aggregator);
1832 gint64 best_time = GST_CLOCK_STIME_NONE;
1835 GstBuffer *buffer = NULL;
1837 if (mux->state == GST_FLV_MUX_STATE_HEADER) {
1838 if (GST_ELEMENT_CAST (mux)->sinkpads == NULL) {
1839 GST_ELEMENT_ERROR (mux, STREAM, MUX, (NULL),
1840 ("No input streams configured"));
1841 return GST_FLOW_ERROR;
1844 ret = gst_flv_mux_write_header (mux);
1845 if (ret != GST_FLOW_OK)
1847 mux->state = GST_FLV_MUX_STATE_DATA;
1849 best = gst_flv_mux_find_best_pad (aggregator, &ts);
1851 if (!mux->streamable || mux->first_timestamp == GST_CLOCK_STIME_NONE) {
1852 if (best && GST_CLOCK_STIME_IS_VALID (ts))
1853 mux->first_timestamp = ts;
1855 mux->first_timestamp = 0;
1858 best = gst_flv_mux_find_best_pad (aggregator, &ts);
1861 if (mux->new_tags) {
1862 GstBuffer *buf = gst_flv_mux_create_metadata (mux, FALSE);
1864 gst_flv_mux_push (mux, buf);
1865 mux->new_tags = FALSE;
1869 buffer = gst_aggregator_pad_pop_buffer (GST_AGGREGATOR_PAD (best));
1872 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1873 (best)->segment, GST_BUFFER_DTS_OR_PTS (buffer));
1875 if (GST_CLOCK_STIME_IS_VALID (best->dts))
1876 best_time = best->dts - mux->first_timestamp;
1878 if (GST_BUFFER_PTS_IS_VALID (buffer))
1880 gst_flv_mux_segment_to_running_time (&GST_AGGREGATOR_PAD
1881 (best)->segment, GST_BUFFER_PTS (buffer));
1883 best->pts = best->dts;
1885 GST_LOG_OBJECT (best, "got buffer PTS %" GST_TIME_FORMAT " DTS %"
1886 GST_STIME_FORMAT, GST_TIME_ARGS (best->pts),
1887 GST_STIME_ARGS (best->dts));
1889 best_time = GST_CLOCK_STIME_NONE;
1892 /* The FLV timestamp is an int32 field. For non-live streams error out if a
1893 bigger timestamp is seen, for live the timestamp will get wrapped in
1894 gst_flv_mux_buffer_to_tag */
1895 if (!mux->streamable && (GST_CLOCK_STIME_IS_VALID (best_time))
1896 && best_time / GST_MSECOND > G_MAXINT32) {
1897 GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
1899 gst_buffer_unref (buffer);
1906 return gst_flv_mux_write_buffer (mux, best, buffer);
1908 if (gst_flv_mux_are_all_pads_eos (mux)) {
1909 gst_flv_mux_write_eos (mux);
1910 gst_flv_mux_rewrite_header (mux);
1911 return GST_FLOW_EOS;
1918 gst_flv_mux_get_property (GObject * object,
1919 guint prop_id, GValue * value, GParamSpec * pspec)
1921 GstFlvMux *mux = GST_FLV_MUX (object);
1924 case PROP_STREAMABLE:
1925 g_value_set_boolean (value, mux->streamable);
1927 case PROP_METADATACREATOR:
1928 g_value_set_string (value, mux->metadatacreator);
1931 g_value_set_string (value, mux->encoder);
1934 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1940 gst_flv_mux_set_property (GObject * object,
1941 guint prop_id, const GValue * value, GParamSpec * pspec)
1943 GstFlvMux *mux = GST_FLV_MUX (object);
1946 case PROP_STREAMABLE:
1947 mux->streamable = g_value_get_boolean (value);
1948 if (mux->streamable)
1949 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1950 GST_TAG_MERGE_REPLACE);
1952 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1953 GST_TAG_MERGE_KEEP);
1955 case PROP_METADATACREATOR:
1956 g_free (mux->metadatacreator);
1957 if (!g_value_get_string (value)) {
1958 GST_WARNING_OBJECT (mux, "metadatacreator property can not be NULL");
1959 mux->metadatacreator = g_strdup (DEFAULT_METADATACREATOR);
1961 mux->metadatacreator = g_value_dup_string (value);
1965 g_free (mux->encoder);
1966 if (!g_value_get_string (value)) {
1967 GST_WARNING_OBJECT (mux, "encoder property can not be NULL");
1968 mux->encoder = g_strdup (DEFAULT_METADATACREATOR);
1970 mux->encoder = g_value_dup_string (value);
1974 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1980 gst_flv_mux_get_next_time (GstAggregator * aggregator)
1982 GstFlvMux *mux = GST_FLV_MUX (aggregator);
1983 GstAggregatorPad *agg_audio_pad = GST_AGGREGATOR_PAD_CAST (mux->audio_pad);
1984 GstAggregatorPad *agg_video_pad = GST_AGGREGATOR_PAD_CAST (mux->video_pad);
1986 GST_OBJECT_LOCK (aggregator);
1987 if (mux->state == GST_FLV_MUX_STATE_HEADER &&
1988 ((mux->audio_pad && mux->audio_pad->codec == G_MAXUINT) ||
1989 (mux->video_pad && mux->video_pad->codec == G_MAXUINT)))
1992 if (!((agg_audio_pad && gst_aggregator_pad_has_buffer (agg_audio_pad)) ||
1993 (agg_video_pad && gst_aggregator_pad_has_buffer (agg_video_pad))))
1995 GST_OBJECT_UNLOCK (aggregator);
1997 return gst_aggregator_simple_get_next_time (aggregator);
2000 GST_OBJECT_UNLOCK (aggregator);
2001 return GST_CLOCK_TIME_NONE;
2004 static GstFlowReturn
2005 gst_flv_mux_update_src_caps (GstAggregator * aggregator,
2006 GstCaps * caps, GstCaps ** ret)
2008 GstFlvMux *mux = GST_FLV_MUX (aggregator);
2010 *ret = gst_flv_mux_prepare_src_caps (mux, NULL, NULL, NULL, NULL);