3 * Copyright (c) 2008,2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-flvmux
24 * flvmux muxes different streams into an FLV file.
27 * <title>Example launch line</title>
29 * gst-launch -v filesrc location=/path/to/audio ! decodebin2 ! queue ! flvmux name=m ! filesink location=file.flv filesrc location=/path/to/video ! decodebin2 ! queue ! m.
30 * ]| This pipeline muxes an audio and video file into a single FLV file.
41 #include "gstflvmux.h"
44 GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
45 #define GST_CAT_DEFAULT flvmux_debug
53 #define DEFAULT_STREAMABLE FALSE
54 #define MAX_INDEX_ENTRIES 128
56 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
59 GST_STATIC_CAPS ("video/x-flv")
62 static GstStaticPadTemplate videosink_templ = GST_STATIC_PAD_TEMPLATE ("video",
65 GST_STATIC_CAPS ("video/x-flash-video; "
66 "video/x-flash-screen; "
67 "video/x-vp6-flash; " "video/x-vp6-alpha; "
68 "video/x-h264, stream-format=avc;")
71 static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
75 ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
76 "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }, parsed = (boolean) TRUE; "
77 "audio/mpeg, mpegversion = (int) 2, framed = (boolean) TRUE; "
78 "audio/mpeg, mpegversion = (int) 4, stream-format = (string) raw, framed = (boolean) TRUE; "
79 "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
80 "audio/x-raw-int, endianness = (int) LITTLE_ENDIAN, channels = (int) { 1, 2 }, width = (int) 8, depth = (int) 8, rate = (int) { 5512, 11025, 22050, 44100 }, signed = (boolean) FALSE; "
81 "audio/x-raw-int, endianness = (int) LITTLE_ENDIAN, channels = (int) { 1, 2 }, width = (int) 16, depth = (int) 16, rate = (int) { 5512, 11025, 22050, 44100 }, signed = (boolean) TRUE; "
82 "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
83 "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
84 "audio/x-speex, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
87 #define _do_init(type) \
89 static const GInterfaceInfo tag_setter_info = { \
94 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, \
98 GST_BOILERPLATE_FULL (GstFlvMux, gst_flv_mux, GstElement, GST_TYPE_ELEMENT,
101 static void gst_flv_mux_finalize (GObject * object);
103 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data);
105 static gboolean gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event);
106 static GstPad *gst_flv_mux_request_new_pad (GstElement * element,
107 GstPadTemplate * templ, const gchar * name);
108 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
110 static void gst_flv_mux_get_property (GObject * object,
111 guint prop_id, GValue * value, GParamSpec * pspec);
112 static void gst_flv_mux_set_property (GObject * object,
113 guint prop_id, const GValue * value, GParamSpec * pspec);
115 static GstStateChangeReturn
116 gst_flv_mux_change_state (GstElement * element, GstStateChange transition);
118 static void gst_flv_mux_reset (GstElement * element);
119 static void gst_flv_mux_reset_pad (GstFlvMux * mux, GstFlvPad * pad,
126 } GstFlvMuxIndexEntry;
129 gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
131 g_slice_free (GstFlvMuxIndexEntry, entry);
135 gst_flv_mux_base_init (gpointer g_class)
137 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
139 gst_element_class_add_static_pad_template (element_class,
141 gst_element_class_add_static_pad_template (element_class,
143 gst_element_class_add_static_pad_template (element_class, &src_templ);
144 gst_element_class_set_details_simple (element_class, "FLV muxer",
146 "Muxes video/audio streams into a FLV stream",
147 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
149 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
153 gst_flv_mux_class_init (GstFlvMuxClass * klass)
155 GObjectClass *gobject_class;
156 GstElementClass *gstelement_class;
158 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
160 gobject_class = (GObjectClass *) klass;
161 gstelement_class = (GstElementClass *) klass;
163 gobject_class->get_property = gst_flv_mux_get_property;
164 gobject_class->set_property = gst_flv_mux_set_property;
165 gobject_class->finalize = gst_flv_mux_finalize;
167 /* FIXME: ideally the right mode of operation should be detected
168 * automatically using queries when parameter not specified. */
170 * GstFlvMux:streamable
172 * If True, the output will be streaming friendly. (ie without indexes and
177 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
178 g_param_spec_boolean ("streamable", "streamable",
179 "If set to true, the output should be as if it is to be streamed "
180 "and hence no indexes written or duration written.",
181 DEFAULT_STREAMABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_flv_mux_change_state);
184 gstelement_class->request_new_pad =
185 GST_DEBUG_FUNCPTR (gst_flv_mux_request_new_pad);
186 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
190 gst_flv_mux_init (GstFlvMux * mux, GstFlvMuxClass * g_class)
192 mux->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
193 gst_pad_set_event_function (mux->srcpad, gst_flv_mux_handle_src_event);
194 gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
197 mux->streamable = DEFAULT_STREAMABLE;
199 mux->new_tags = FALSE;
201 mux->collect = gst_collect_pads_new ();
202 gst_collect_pads_set_function (mux->collect,
203 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_flv_mux_collected), mux);
205 gst_flv_mux_reset (GST_ELEMENT (mux));
209 gst_flv_mux_finalize (GObject * object)
211 GstFlvMux *mux = GST_FLV_MUX (object);
213 gst_object_unref (mux->collect);
215 G_OBJECT_CLASS (parent_class)->finalize (object);
219 gst_flv_mux_reset (GstElement * element)
221 GstFlvMux *mux = GST_FLV_MUX (element);
224 for (sl = mux->collect->data; sl != NULL; sl = g_slist_next (sl)) {
225 GstFlvPad *cpad = (GstFlvPad *) sl->data;
227 gst_flv_mux_reset_pad (mux, cpad, cpad->video);
230 g_list_foreach (mux->index, (GFunc) gst_flv_mux_index_entry_free, NULL);
231 g_list_free (mux->index);
235 mux->have_audio = mux->have_video = FALSE;
236 mux->duration = GST_CLOCK_TIME_NONE;
237 mux->new_tags = FALSE;
239 mux->state = GST_FLV_MUX_STATE_HEADER;
242 gst_tag_setter_reset_tags (GST_TAG_SETTER (mux));
246 gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event)
250 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
254 /* disable seeking for now */
260 return gst_pad_event_default (pad, event);
264 gst_flv_mux_handle_sink_event (GstPad * pad, GstEvent * event)
266 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
269 switch (GST_EVENT_TYPE (event)) {
272 GstTagSetter *setter = GST_TAG_SETTER (mux);
273 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
275 gst_event_parse_tag (event, &list);
276 gst_tag_setter_merge_tags (setter, list, mode);
277 mux->new_tags = TRUE;
284 /* now GstCollectPads can take care of the rest, e.g. EOS */
286 ret = mux->collect_event (pad, event);
287 gst_object_unref (mux);
293 gst_flv_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps)
295 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
296 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
300 s = gst_caps_get_structure (caps, 0);
302 if (strcmp (gst_structure_get_name (s), "video/x-flash-video") == 0) {
303 cpad->video_codec = 2;
304 } else if (strcmp (gst_structure_get_name (s), "video/x-flash-screen") == 0) {
305 cpad->video_codec = 3;
306 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-flash") == 0) {
307 cpad->video_codec = 4;
308 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-alpha") == 0) {
309 cpad->video_codec = 5;
310 } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
311 cpad->video_codec = 7;
316 if (ret && gst_structure_has_field (s, "codec_data")) {
317 const GValue *val = gst_structure_get_value (s, "codec_data");
320 cpad->video_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
323 gst_object_unref (mux);
329 gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
331 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
332 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
336 s = gst_caps_get_structure (caps, 0);
338 if (strcmp (gst_structure_get_name (s), "audio/x-adpcm") == 0) {
339 const gchar *layout = gst_structure_get_string (s, "layout");
340 if (layout && strcmp (layout, "swf") == 0) {
341 cpad->audio_codec = 1;
345 } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
348 if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
349 if (mpegversion == 1) {
352 if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
355 if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
356 cpad->audio_codec = 14;
358 cpad->audio_codec = 2;
362 } else if (mpegversion == 4 || mpegversion == 2) {
363 cpad->audio_codec = 10;
370 } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
373 if (gst_structure_get_int (s, "rate", &rate)
374 && gst_structure_get_int (s, "channels", &channels)) {
375 if (channels == 1 && rate == 16000)
376 cpad->audio_codec = 4;
377 else if (channels == 1 && rate == 8000)
378 cpad->audio_codec = 5;
380 cpad->audio_codec = 6;
382 cpad->audio_codec = 6;
384 } else if (strcmp (gst_structure_get_name (s), "audio/x-raw-int") == 0) {
387 if (gst_structure_get_int (s, "endianness", &endianness)
388 && endianness == G_LITTLE_ENDIAN)
389 cpad->audio_codec = 3;
392 } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
393 cpad->audio_codec = 7;
394 } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
395 cpad->audio_codec = 8;
396 } else if (strcmp (gst_structure_get_name (s), "audio/x-speex") == 0) {
397 cpad->audio_codec = 11;
403 gint rate, channels, width;
405 if (gst_structure_get_int (s, "rate", &rate)) {
406 if (cpad->audio_codec == 10)
408 else if (rate == 5512)
410 else if (rate == 11025)
412 else if (rate == 22050)
414 else if (rate == 44100)
416 else if (rate == 8000 && (cpad->audio_codec == 5
417 || cpad->audio_codec == 14))
419 else if (rate == 16000 && cpad->audio_codec == 4)
423 } else if (cpad->audio_codec == 10) {
429 if (gst_structure_get_int (s, "channels", &channels)) {
430 if (cpad->audio_codec == 4 || cpad->audio_codec == 5
431 || cpad->audio_codec == 6)
433 else if (cpad->audio_codec == 10)
435 else if (channels == 1)
437 else if (channels == 2)
441 } else if (cpad->audio_codec == 4 || cpad->audio_codec == 5
442 || cpad->audio_codec == 6) {
444 } else if (cpad->audio_codec == 10) {
450 if (gst_structure_get_int (s, "width", &width)) {
451 if (cpad->audio_codec != 3)
455 else if (width == 16)
459 } else if (cpad->audio_codec != 3) {
466 if (ret && gst_structure_has_field (s, "codec_data")) {
467 const GValue *val = gst_structure_get_value (s, "codec_data");
470 cpad->audio_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
473 gst_object_unref (mux);
479 gst_flv_mux_reset_pad (GstFlvMux * mux, GstFlvPad * cpad, gboolean video)
483 if (cpad->audio_codec_data)
484 gst_buffer_unref (cpad->audio_codec_data);
485 cpad->audio_codec_data = NULL;
486 cpad->audio_codec = G_MAXUINT;
487 cpad->rate = G_MAXUINT;
488 cpad->width = G_MAXUINT;
489 cpad->channels = G_MAXUINT;
491 if (cpad->video_codec_data)
492 gst_buffer_unref (cpad->video_codec_data);
493 cpad->video_codec_data = NULL;
494 cpad->video_codec = G_MAXUINT;
495 cpad->last_timestamp = 0;
499 gst_flv_mux_request_new_pad (GstElement * element,
500 GstPadTemplate * templ, const gchar * pad_name)
502 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
503 GstFlvMux *mux = GST_FLV_MUX (element);
506 const gchar *name = NULL;
507 GstPadSetCapsFunction setcapsfunc = NULL;
510 if (mux->state != GST_FLV_MUX_STATE_HEADER) {
511 GST_WARNING_OBJECT (mux, "Can't request pads after writing header");
515 if (templ == gst_element_class_get_pad_template (klass, "audio")) {
516 if (mux->have_audio) {
517 GST_WARNING_OBJECT (mux, "Already have an audio pad");
520 mux->have_audio = TRUE;
523 setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_audio_pad_setcaps);
524 } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
525 if (mux->have_video) {
526 GST_WARNING_OBJECT (mux, "Already have a video pad");
529 mux->have_video = TRUE;
532 setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_video_pad_setcaps);
534 GST_WARNING_OBJECT (mux, "Invalid template");
538 pad = gst_pad_new_from_template (templ, name);
540 gst_collect_pads_add_pad (mux->collect, pad, sizeof (GstFlvPad));
542 cpad->audio_codec_data = NULL;
543 cpad->video_codec_data = NULL;
544 gst_flv_mux_reset_pad (mux, cpad, video);
546 /* FIXME: hacked way to override/extend the event function of
547 * GstCollectPads; because it sets its own event function giving the
548 * element no access to events.
550 mux->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (pad);
551 gst_pad_set_event_function (pad,
552 GST_DEBUG_FUNCPTR (gst_flv_mux_handle_sink_event));
554 gst_pad_set_setcaps_function (pad, setcapsfunc);
555 gst_pad_set_active (pad, TRUE);
556 gst_element_add_pad (element, pad);
562 gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
564 GstFlvMux *mux = GST_FLV_MUX (GST_PAD_PARENT (pad));
565 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
567 gst_flv_mux_reset_pad (mux, cpad, cpad->video);
568 gst_collect_pads_remove_pad (mux->collect, pad);
569 gst_element_remove_pad (element, pad);
573 gst_flv_mux_push (GstFlvMux * mux, GstBuffer * buffer)
575 buffer = gst_buffer_make_metadata_writable (buffer);
576 gst_buffer_set_caps (buffer, GST_PAD_CAPS (mux->srcpad));
577 /* pushing the buffer that rewrites the header will make it no longer be the
578 * total output size in bytes, but it doesn't matter at that point */
579 mux->byte_count += GST_BUFFER_SIZE (buffer);
581 return gst_pad_push (mux->srcpad, buffer);
585 gst_flv_mux_create_header (GstFlvMux * mux)
590 header = gst_buffer_new_and_alloc (9 + 4);
591 data = GST_BUFFER_DATA (header);
596 data[3] = 0x01; /* Version */
598 data[4] = (mux->have_audio << 2) | mux->have_video; /* flags */
599 GST_WRITE_UINT32_BE (data + 5, 9); /* data offset */
600 GST_WRITE_UINT32_BE (data + 9, 0); /* previous tag size */
606 gst_flv_mux_preallocate_index (GstFlvMux * mux)
610 gint preallocate_size;
612 /* preallocate index of size:
613 * - 'keyframes' ECMA array key: 2 + 9 = 11 bytes
614 * - nested ECMA array header, length and end marker: 8 bytes
615 * - 'times' and 'filepositions' keys: 22 bytes
616 * - two strict arrays headers and lengths: 10 bytes
617 * - each index entry: 18 bytes
619 preallocate_size = 11 + 8 + 22 + 10 + MAX_INDEX_ENTRIES * 18;
620 GST_DEBUG_OBJECT (mux, "preallocating %d bytes for the index",
623 tmp = gst_buffer_new_and_alloc (preallocate_size);
624 data = GST_BUFFER_DATA (tmp);
626 /* prefill the space with a gstfiller: <spaces> script tag variable */
627 GST_WRITE_UINT16_BE (data, 9); /* 9 characters */
628 memcpy (data + 2, "gstfiller", 9);
629 GST_WRITE_UINT8 (data + 11, AMF0_STRING_MARKER); /* a string value */
630 GST_WRITE_UINT16_BE (data + 12, preallocate_size - 14);
631 memset (data + 14, ' ', preallocate_size - 14); /* the rest is spaces */
636 gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
638 GstBuffer *tmp = gst_buffer_new_and_alloc (2 + strlen (name) + 1 + 8);
639 guint8 *data = GST_BUFFER_DATA (tmp);
640 gsize len = strlen (name);
642 GST_WRITE_UINT16_BE (data, len);
643 data += 2; /* name length */
644 memcpy (data, name, len);
646 *data++ = AMF0_NUMBER_MARKER; /* double type */
647 GST_WRITE_DOUBLE_BE (data, value);
653 gst_flv_mux_create_metadata (GstFlvMux * mux, gboolean full)
655 const GstTagList *tags;
656 GstBuffer *script_tag, *tmp;
658 gint i, n_tags, tags_written = 0;
660 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (mux));
662 GST_DEBUG_OBJECT (mux, "tags = %" GST_PTR_FORMAT, tags);
664 script_tag = gst_buffer_new_and_alloc (11);
665 data = GST_BUFFER_DATA (script_tag);
669 /* Data size, unknown for now */
675 data[4] = data[5] = data[6] = data[7] = 0;
678 data[8] = data[9] = data[10] = 0;
680 tmp = gst_buffer_new_and_alloc (13);
681 data = GST_BUFFER_DATA (tmp);
682 data[0] = AMF0_STRING_MARKER; /* string */
684 data[2] = 10; /* length 10 */
685 memcpy (&data[3], "onMetaData", 10);
687 script_tag = gst_buffer_join (script_tag, tmp);
689 n_tags = (tags) ? gst_structure_n_fields ((GstStructure *) tags) : 0;
690 tmp = gst_buffer_new_and_alloc (5);
691 data = GST_BUFFER_DATA (tmp);
692 data[0] = 8; /* ECMA array */
693 GST_WRITE_UINT32_BE (data + 1, n_tags);
694 script_tag = gst_buffer_join (script_tag, tmp);
699 /* Some players expect the 'duration' to be always set. Fill it out later,
700 after querying the pads or after getting EOS */
701 if (!mux->streamable) {
702 tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
703 script_tag = gst_buffer_join (script_tag, tmp);
706 /* Sometimes the information about the total file size is useful for the
707 player. It will be filled later, after getting EOS */
708 tmp = gst_flv_mux_create_number_script_value ("filesize", 0);
709 script_tag = gst_buffer_join (script_tag, tmp);
712 /* Preallocate space for the index to be written at EOS */
713 tmp = gst_flv_mux_preallocate_index (mux);
714 script_tag = gst_buffer_join (script_tag, tmp);
716 GST_DEBUG_OBJECT (mux, "not preallocating index, streamable mode");
720 for (i = 0; tags && i < n_tags; i++) {
721 const gchar *tag_name =
722 gst_structure_nth_field_name ((const GstStructure *) tags, i);
723 if (!strcmp (tag_name, GST_TAG_DURATION)) {
726 if (!gst_tag_list_get_uint64 (tags, GST_TAG_DURATION, &dur))
729 } else if (!strcmp (tag_name, GST_TAG_ARTIST) ||
730 !strcmp (tag_name, GST_TAG_TITLE)) {
732 const gchar *t = NULL;
734 if (!strcmp (tag_name, GST_TAG_ARTIST))
736 else if (!strcmp (tag_name, GST_TAG_TITLE))
739 if (!gst_tag_list_get_string (tags, tag_name, &s))
742 tmp = gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s));
743 data = GST_BUFFER_DATA (tmp);
744 data[0] = 0; /* tag name length */
745 data[1] = strlen (t);
746 memcpy (&data[2], t, strlen (t));
747 data[2 + strlen (t)] = 2; /* string */
748 data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
749 data[4 + strlen (t)] = (strlen (s)) & 0xff;
750 memcpy (&data[5 + strlen (t)], s, strlen (s));
751 script_tag = gst_buffer_join (script_tag, tmp);
761 if (mux->duration == GST_CLOCK_TIME_NONE) {
764 GstFormat fmt = GST_FORMAT_TIME;
767 for (l = mux->collect->data; l; l = l->next) {
768 GstCollectData *cdata = l->data;
770 fmt = GST_FORMAT_TIME;
772 if (gst_pad_query_peer_duration (cdata->pad, &fmt, (gint64 *) & dur) &&
773 fmt == GST_FORMAT_TIME && dur != GST_CLOCK_TIME_NONE) {
774 if (mux->duration == GST_CLOCK_TIME_NONE)
777 mux->duration = MAX (dur, mux->duration);
782 if (!mux->streamable && mux->duration != GST_CLOCK_TIME_NONE) {
784 d = gst_guint64_to_gdouble (mux->duration);
785 d /= (gdouble) GST_SECOND;
787 GST_DEBUG_OBJECT (mux, "determined the duration to be %f", d);
788 data = GST_BUFFER_DATA (script_tag);
789 GST_WRITE_DOUBLE_BE (data + 29 + 2 + 8 + 1, d);
792 if (mux->have_video) {
793 GstPad *video_pad = NULL;
795 GSList *l = mux->collect->data;
797 for (; l; l = l->next) {
799 if (cpad && cpad->video) {
800 video_pad = cpad->collect.pad;
805 if (video_pad && GST_PAD_CAPS (video_pad)) {
806 GstStructure *s = gst_caps_get_structure (GST_PAD_CAPS (video_pad), 0);
810 GST_DEBUG_OBJECT (mux, "putting videocodecid %d in the metadata",
813 tmp = gst_flv_mux_create_number_script_value ("videocodecid",
815 script_tag = gst_buffer_join (script_tag, tmp);
818 if (gst_structure_get_int (s, "width", &size)) {
819 GST_DEBUG_OBJECT (mux, "putting width %d in the metadata", size);
821 tmp = gst_flv_mux_create_number_script_value ("width", size);
822 script_tag = gst_buffer_join (script_tag, tmp);
826 if (gst_structure_get_int (s, "height", &size)) {
827 GST_DEBUG_OBJECT (mux, "putting height %d in the metadata", size);
829 tmp = gst_flv_mux_create_number_script_value ("height", size);
830 script_tag = gst_buffer_join (script_tag, tmp);
834 if (gst_structure_get_fraction (s, "pixel-aspect-ratio", &num, &den)) {
838 GST_DEBUG_OBJECT (mux, "putting AspectRatioX %f in the metadata", d);
840 tmp = gst_flv_mux_create_number_script_value ("AspectRatioX", d);
841 script_tag = gst_buffer_join (script_tag, tmp);
845 GST_DEBUG_OBJECT (mux, "putting AspectRatioY %f in the metadata", d);
847 tmp = gst_flv_mux_create_number_script_value ("AspectRatioY", d);
848 script_tag = gst_buffer_join (script_tag, tmp);
852 if (gst_structure_get_fraction (s, "framerate", &num, &den)) {
855 gst_util_fraction_to_double (num, den, &d);
856 GST_DEBUG_OBJECT (mux, "putting framerate %f in the metadata", d);
858 tmp = gst_flv_mux_create_number_script_value ("framerate", d);
859 script_tag = gst_buffer_join (script_tag, tmp);
865 if (mux->have_audio) {
866 GstPad *audio_pad = NULL;
868 GSList *l = mux->collect->data;
870 for (; l; l = l->next) {
872 if (cpad && !cpad->video) {
873 audio_pad = cpad->collect.pad;
879 GST_DEBUG_OBJECT (mux, "putting audiocodecid %d in the metadata",
882 tmp = gst_flv_mux_create_number_script_value ("audiocodecid",
884 script_tag = gst_buffer_join (script_tag, tmp);
890 const gchar *s = "GStreamer FLV muxer";
892 tmp = gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (s));
893 data = GST_BUFFER_DATA (tmp);
894 data[0] = 0; /* 15 bytes name */
896 memcpy (&data[2], "metadatacreator", 15);
897 data[17] = 2; /* string */
898 data[18] = (strlen (s) >> 8) & 0xff;
899 data[19] = (strlen (s)) & 0xff;
900 memcpy (&data[20], s, strlen (s));
901 script_tag = gst_buffer_join (script_tag, tmp);
907 GTimeVal tv = { 0, };
911 static const gchar *weekdays[] = {
912 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
914 static const gchar *months[] = {
915 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
916 "Aug", "Sep", "Oct", "Nov", "Dec"
919 g_get_current_time (&tv);
923 s = g_strdup_printf ("%s %s %d %d:%d:%d %d", weekdays[tm->tm_wday],
924 months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
927 tmp = gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s));
928 data = GST_BUFFER_DATA (tmp);
929 data[0] = 0; /* 12 bytes name */
931 memcpy (&data[2], "creationdate", 12);
932 data[14] = 2; /* string */
933 data[15] = (strlen (s) >> 8) & 0xff;
934 data[16] = (strlen (s)) & 0xff;
935 memcpy (&data[17], s, strlen (s));
936 script_tag = gst_buffer_join (script_tag, tmp);
945 gst_buffer_unref (script_tag);
950 tmp = gst_buffer_new_and_alloc (2 + 0 + 1);
951 data = GST_BUFFER_DATA (tmp);
952 data[0] = 0; /* 0 byte size */
954 data[2] = 9; /* end marker */
955 script_tag = gst_buffer_join (script_tag, tmp);
958 tmp = gst_buffer_new_and_alloc (4);
959 data = GST_BUFFER_DATA (tmp);
960 GST_WRITE_UINT32_BE (data, GST_BUFFER_SIZE (script_tag));
961 script_tag = gst_buffer_join (script_tag, tmp);
963 data = GST_BUFFER_DATA (script_tag);
964 data[1] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 16) & 0xff;
965 data[2] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 8) & 0xff;
966 data[3] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 0) & 0xff;
968 GST_WRITE_UINT32_BE (data + 11 + 13 + 1, tags_written);
975 gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
976 GstFlvPad * cpad, gboolean is_codec_data)
982 (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) ? GST_BUFFER_TIMESTAMP (buffer) /
983 GST_MSECOND : cpad->last_timestamp / GST_MSECOND;
988 if (cpad->video_codec == 7)
989 size += 4 + GST_BUFFER_SIZE (buffer);
991 size += GST_BUFFER_SIZE (buffer);
994 if (cpad->audio_codec == 10)
995 size += 1 + GST_BUFFER_SIZE (buffer);
997 size += GST_BUFFER_SIZE (buffer);
1001 tag = gst_buffer_new_and_alloc (size);
1002 GST_BUFFER_TIMESTAMP (tag) = timestamp * GST_MSECOND;
1003 data = GST_BUFFER_DATA (tag);
1004 memset (data, 0, size);
1006 data[0] = (cpad->video) ? 9 : 8;
1008 data[1] = ((size - 11 - 4) >> 16) & 0xff;
1009 data[2] = ((size - 11 - 4) >> 8) & 0xff;
1010 data[3] = ((size - 11 - 4) >> 0) & 0xff;
1012 /* wrap the timestamp every G_MAXINT32 miliseconds */
1013 timestamp &= 0x7fffffff;
1014 data[4] = (timestamp >> 16) & 0xff;
1015 data[5] = (timestamp >> 8) & 0xff;
1016 data[6] = (timestamp >> 0) & 0xff;
1017 data[7] = (timestamp >> 24) & 0xff;
1019 data[8] = data[9] = data[10] = 0;
1022 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1027 data[11] |= cpad->video_codec & 0x0f;
1029 if (cpad->video_codec == 7) {
1030 data[12] = is_codec_data ? 0 : 1;
1032 /* FIXME: what to do about composition time */
1033 data[13] = data[14] = data[15] = 0;
1035 memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (buffer),
1036 GST_BUFFER_SIZE (buffer));
1038 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
1039 GST_BUFFER_SIZE (buffer));
1042 data[11] |= (cpad->audio_codec << 4) & 0xf0;
1043 data[11] |= (cpad->rate << 2) & 0x0c;
1044 data[11] |= (cpad->width << 1) & 0x02;
1045 data[11] |= (cpad->channels << 0) & 0x01;
1047 if (cpad->audio_codec == 10) {
1048 data[12] = is_codec_data ? 0 : 1;
1050 memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (buffer),
1051 GST_BUFFER_SIZE (buffer));
1053 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
1054 GST_BUFFER_SIZE (buffer));
1058 GST_WRITE_UINT32_BE (data + size - 4, size - 4);
1060 gst_buffer_copy_metadata (tag, buffer, GST_BUFFER_COPY_TIMESTAMPS);
1061 /* mark the buffer if it's an audio buffer and there's also video being muxed
1062 * or it's a video interframe */
1063 if ((mux->have_video && !cpad->video) ||
1064 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1065 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1067 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
1068 GST_BUFFER_OFFSET_NONE;
1073 static inline GstBuffer *
1074 gst_flv_mux_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1077 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, cpad, FALSE);
1080 static inline GstBuffer *
1081 gst_flv_mux_codec_data_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1084 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, cpad, TRUE);
1088 gst_flv_mux_put_buffer_in_streamheader (GValue * streamheader,
1091 GValue value = { 0 };
1094 g_value_init (&value, GST_TYPE_BUFFER);
1095 buf = gst_buffer_copy (buffer);
1096 gst_value_set_buffer (&value, buf);
1097 gst_buffer_unref (buf);
1098 gst_value_array_append_value (streamheader, &value);
1099 g_value_unset (&value);
1102 static GstFlowReturn
1103 gst_flv_mux_write_header (GstFlvMux * mux)
1105 GstBuffer *header, *metadata;
1106 GstBuffer *video_codec_data, *audio_codec_data;
1108 GstStructure *structure;
1109 GValue streamheader = { 0 };
1113 header = gst_flv_mux_create_header (mux);
1114 metadata = gst_flv_mux_create_metadata (mux, TRUE);
1115 video_codec_data = NULL;
1116 audio_codec_data = NULL;
1118 for (l = mux->collect->data; l != NULL; l = l->next) {
1119 GstFlvPad *cpad = l->data;
1121 /* Get H.264 and AAC codec data, if present */
1122 if (cpad && cpad->video && cpad->video_codec == 7) {
1123 if (cpad->video_codec_data == NULL)
1124 GST_WARNING_OBJECT (mux, "Codec data for video stream not found, "
1125 "output might not be playable");
1128 gst_flv_mux_codec_data_buffer_to_tag (mux, cpad->video_codec_data,
1130 } else if (cpad && !cpad->video && cpad->audio_codec == 10) {
1131 if (cpad->audio_codec_data == NULL)
1132 GST_WARNING_OBJECT (mux, "Codec data for audio stream not found, "
1133 "output might not be playable");
1136 gst_flv_mux_codec_data_buffer_to_tag (mux, cpad->audio_codec_data,
1141 /* mark buffers that will go in the streamheader */
1142 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_IN_CAPS);
1143 GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_IN_CAPS);
1144 if (video_codec_data != NULL) {
1145 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_IN_CAPS);
1146 /* mark as a delta unit, so downstream will not try to synchronize on that
1147 * buffer - to actually start playback you need a real video keyframe */
1148 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_DELTA_UNIT);
1150 if (audio_codec_data != NULL) {
1151 GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_IN_CAPS);
1154 /* put buffers in streamheader */
1155 g_value_init (&streamheader, GST_TYPE_ARRAY);
1156 gst_flv_mux_put_buffer_in_streamheader (&streamheader, header);
1157 gst_flv_mux_put_buffer_in_streamheader (&streamheader, metadata);
1158 if (video_codec_data != NULL)
1159 gst_flv_mux_put_buffer_in_streamheader (&streamheader, video_codec_data);
1160 if (audio_codec_data != NULL)
1161 gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);
1163 /* create the caps and put the streamheader in them */
1164 caps = gst_caps_new_simple ("video/x-flv", NULL);
1165 caps = gst_caps_make_writable (caps);
1166 structure = gst_caps_get_structure (caps, 0);
1167 gst_structure_set_value (structure, "streamheader", &streamheader);
1168 g_value_unset (&streamheader);
1170 if (GST_PAD_CAPS (mux->srcpad) == NULL)
1171 gst_pad_set_caps (mux->srcpad, caps);
1173 gst_caps_unref (caps);
1175 /* push the header buffer, the metadata and the codec info, if any */
1176 ret = gst_flv_mux_push (mux, header);
1177 if (ret != GST_FLOW_OK)
1179 ret = gst_flv_mux_push (mux, metadata);
1180 if (ret != GST_FLOW_OK)
1182 if (video_codec_data != NULL) {
1183 ret = gst_flv_mux_push (mux, video_codec_data);
1184 if (ret != GST_FLOW_OK)
1187 if (audio_codec_data != NULL) {
1188 ret = gst_flv_mux_push (mux, audio_codec_data);
1189 if (ret != GST_FLOW_OK)
1196 gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer, GstFlvPad * cpad)
1199 * Add the tag byte offset and to the index if it's a valid seek point, which
1200 * means it's either a video keyframe or if there is no video pad (in that
1201 * case every FLV tag is a valid seek point)
1203 if (mux->have_video &&
1205 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)))
1208 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
1209 GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
1210 entry->position = mux->byte_count;
1212 gst_guint64_to_gdouble (GST_BUFFER_TIMESTAMP (buffer)) / GST_SECOND;
1213 mux->index = g_list_prepend (mux->index, entry);
1217 static GstFlowReturn
1218 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad)
1222 gst_collect_pads_pop (mux->collect, (GstCollectData *) cpad);
1225 /* arrange downstream running time */
1226 buffer = gst_buffer_make_metadata_writable (buffer);
1227 GST_BUFFER_TIMESTAMP (buffer) =
1228 gst_segment_to_running_time (&cpad->collect.segment,
1229 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buffer));
1231 if (!mux->streamable)
1232 gst_flv_mux_update_index (mux, buffer, cpad);
1234 tag = gst_flv_mux_buffer_to_tag (mux, buffer, cpad);
1236 gst_buffer_unref (buffer);
1238 ret = gst_flv_mux_push (mux, tag);
1240 if (ret == GST_FLOW_OK && GST_BUFFER_TIMESTAMP_IS_VALID (tag))
1241 cpad->last_timestamp = GST_BUFFER_TIMESTAMP (tag);
1247 gst_flv_mux_determine_duration (GstFlvMux * mux)
1250 GstClockTime duration = GST_CLOCK_TIME_NONE;
1252 GST_DEBUG_OBJECT (mux, "trying to determine the duration "
1253 "from pad timestamps");
1255 for (l = mux->collect->data; l != NULL; l = l->next) {
1256 GstFlvPad *cpad = l->data;
1258 if (cpad && (cpad->last_timestamp != GST_CLOCK_TIME_NONE)) {
1259 if (duration == GST_CLOCK_TIME_NONE)
1260 duration = cpad->last_timestamp;
1262 duration = MAX (duration, cpad->last_timestamp);
1269 static GstFlowReturn
1270 gst_flv_mux_rewrite_header (GstFlvMux * mux)
1272 GstBuffer *rewrite, *index, *tmp;
1277 guint32 index_len, allocate_size;
1278 guint32 i, index_skip;
1281 if (mux->streamable)
1284 /* seek back to the preallocated index space */
1285 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1286 13 + 29, GST_CLOCK_TIME_NONE, 13 + 29);
1287 if (!gst_pad_push_event (mux->srcpad, event)) {
1288 GST_WARNING_OBJECT (mux, "Seek to rewrite header failed");
1292 /* determine duration now based on our own timestamping,
1293 * so that it is likely many times better and consistent
1294 * than whatever obtained by some query */
1295 dur = gst_flv_mux_determine_duration (mux);
1296 if (dur != GST_CLOCK_TIME_NONE)
1297 mux->duration = dur;
1299 /* rewrite the duration tag */
1300 d = gst_guint64_to_gdouble (mux->duration);
1301 d /= (gdouble) GST_SECOND;
1303 GST_DEBUG_OBJECT (mux, "determined the final duration to be %f", d);
1305 rewrite = gst_flv_mux_create_number_script_value ("duration", d);
1307 /* rewrite the filesize tag */
1308 d = gst_guint64_to_gdouble (mux->byte_count);
1310 GST_DEBUG_OBJECT (mux, "putting total filesize %f in the metadata", d);
1312 tmp = gst_flv_mux_create_number_script_value ("filesize", d);
1313 rewrite = gst_buffer_join (rewrite, tmp);
1316 /* no index, so push buffer and return */
1317 return gst_flv_mux_push (mux, rewrite);
1320 /* rewrite the index */
1321 mux->index = g_list_reverse (mux->index);
1322 index_len = g_list_length (mux->index);
1324 /* We write at most MAX_INDEX_ENTRIES elements */
1325 if (index_len > MAX_INDEX_ENTRIES) {
1326 index_skip = 1 + index_len / MAX_INDEX_ENTRIES;
1327 index_len = (index_len + index_skip - 1) / index_skip;
1332 GST_DEBUG_OBJECT (mux, "Index length is %d", index_len);
1333 /* see size calculation in gst_flv_mux_preallocate_index */
1334 allocate_size = 11 + 8 + 22 + 10 + index_len * 18;
1335 GST_DEBUG_OBJECT (mux, "Allocating %d bytes for index", allocate_size);
1336 index = gst_buffer_new_and_alloc (allocate_size);
1337 data = GST_BUFFER_DATA (index);
1339 GST_WRITE_UINT16_BE (data, 9); /* the 'keyframes' key */
1340 memcpy (data + 2, "keyframes", 9);
1341 GST_WRITE_UINT8 (data + 11, 8); /* nested ECMA array */
1342 GST_WRITE_UINT32_BE (data + 12, 2); /* two elements */
1343 GST_WRITE_UINT16_BE (data + 16, 5); /* first string key: 'times' */
1344 memcpy (data + 18, "times", 5);
1345 GST_WRITE_UINT8 (data + 23, 10); /* strict array */
1346 GST_WRITE_UINT32_BE (data + 24, index_len);
1349 /* the keyframes' times */
1350 for (i = 0, l = mux->index; l; l = l->next, i++) {
1351 GstFlvMuxIndexEntry *entry = l->data;
1353 if (i % index_skip != 0)
1355 GST_WRITE_UINT8 (data, 0); /* numeric (aka double) */
1356 GST_WRITE_DOUBLE_BE (data + 1, entry->time);
1360 GST_WRITE_UINT16_BE (data, 13); /* second string key: 'filepositions' */
1361 memcpy (data + 2, "filepositions", 13);
1362 GST_WRITE_UINT8 (data + 15, 10); /* strict array */
1363 GST_WRITE_UINT32_BE (data + 16, index_len);
1366 /* the keyframes' file positions */
1367 for (i = 0, l = mux->index; l; l = l->next, i++) {
1368 GstFlvMuxIndexEntry *entry = l->data;
1370 if (i % index_skip != 0)
1372 GST_WRITE_UINT8 (data, 0);
1373 GST_WRITE_DOUBLE_BE (data + 1, entry->position);
1377 GST_WRITE_UINT24_BE (data, 9); /* finish the ECMA array */
1379 /* If there is space left in the prefilled area, reinsert the filler.
1380 There is at least 18 bytes free, so it will always fit. */
1381 if (index_len < MAX_INDEX_ENTRIES) {
1384 guint32 remaining_filler_size;
1386 tmp = gst_buffer_new_and_alloc (14);
1387 data = GST_BUFFER_DATA (tmp);
1388 GST_WRITE_UINT16_BE (data, 9);
1389 memcpy (data + 2, "gstfiller", 9);
1390 GST_WRITE_UINT8 (data + 11, 2); /* string */
1392 /* There is 18 bytes per remaining index entry minus what is used for
1393 * the'gstfiller' key. The rest is already filled with spaces, so just need
1394 * to update length. */
1395 remaining_filler_size = (MAX_INDEX_ENTRIES - index_len) * 18 - 14;
1396 GST_DEBUG_OBJECT (mux, "Remaining filler size is %d bytes",
1397 remaining_filler_size);
1398 GST_WRITE_UINT16_BE (data + 12, remaining_filler_size);
1399 index = gst_buffer_join (index, tmp);
1402 rewrite = gst_buffer_join (rewrite, index);
1404 gst_buffer_set_caps (rewrite, GST_PAD_CAPS (mux->srcpad));
1405 return gst_flv_mux_push (mux, rewrite);
1408 static GstFlowReturn
1409 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data)
1411 GstFlvMux *mux = GST_FLV_MUX (user_data);
1413 GstClockTime best_time;
1416 gboolean eos = TRUE;
1418 if (mux->state == GST_FLV_MUX_STATE_HEADER) {
1419 if (mux->collect->data == NULL) {
1420 GST_ELEMENT_ERROR (mux, STREAM, MUX, (NULL),
1421 ("No input streams configured"));
1422 return GST_FLOW_ERROR;
1425 if (gst_pad_push_event (mux->srcpad,
1426 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0)))
1427 ret = gst_flv_mux_write_header (mux);
1429 ret = GST_FLOW_ERROR;
1431 if (ret != GST_FLOW_OK)
1433 mux->state = GST_FLV_MUX_STATE_DATA;
1436 if (mux->new_tags) {
1437 GstBuffer *buf = gst_flv_mux_create_metadata (mux, FALSE);
1439 gst_flv_mux_push (mux, buf);
1440 mux->new_tags = FALSE;
1445 best_time = GST_CLOCK_TIME_NONE;
1446 for (sl = mux->collect->data; sl; sl = sl->next) {
1447 GstFlvPad *cpad = sl->data;
1448 GstBuffer *buffer = gst_collect_pads_peek (pads, (GstCollectData *) cpad);
1456 time = GST_BUFFER_TIMESTAMP (buffer);
1457 gst_buffer_unref (buffer);
1459 /* Use buffers without valid timestamp first */
1460 if (!GST_CLOCK_TIME_IS_VALID (time)) {
1461 GST_WARNING_OBJECT (pads, "Buffer without valid timestamp");
1463 best_time = cpad->last_timestamp;
1468 time = gst_segment_to_running_time (&cpad->collect.segment,
1469 GST_FORMAT_TIME, time);
1470 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
1471 GST_DEBUG_OBJECT (mux, "clipping buffer on pad %s outside segment",
1472 GST_PAD_NAME (cpad->collect.pad));
1473 buffer = gst_collect_pads_pop (pads, (GstCollectData *) cpad);
1474 gst_buffer_unref (buffer);
1478 if (best == NULL || (GST_CLOCK_TIME_IS_VALID (best_time)
1479 && time < best_time)) {
1485 /* The FLV timestamp is an int32 field. For non-live streams error out if a
1486 bigger timestamp is seen, for live the timestamp will get wrapped in
1487 gst_flv_mux_buffer_to_tag */
1488 if (!mux->streamable && GST_CLOCK_TIME_IS_VALID (best_time)
1489 && best_time / GST_MSECOND > G_MAXINT32) {
1490 GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
1495 return gst_flv_mux_write_buffer (mux, best);
1497 gst_flv_mux_rewrite_header (mux);
1498 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
1499 return GST_FLOW_UNEXPECTED;
1506 gst_flv_mux_get_property (GObject * object,
1507 guint prop_id, GValue * value, GParamSpec * pspec)
1509 GstFlvMux *mux = GST_FLV_MUX (object);
1512 case PROP_STREAMABLE:
1513 g_value_set_boolean (value, mux->streamable);
1516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1522 gst_flv_mux_set_property (GObject * object,
1523 guint prop_id, const GValue * value, GParamSpec * pspec)
1525 GstFlvMux *mux = GST_FLV_MUX (object);
1528 case PROP_STREAMABLE:
1529 mux->streamable = g_value_get_boolean (value);
1530 if (mux->streamable)
1531 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1532 GST_TAG_MERGE_REPLACE);
1534 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1535 GST_TAG_MERGE_KEEP);
1538 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1543 static GstStateChangeReturn
1544 gst_flv_mux_change_state (GstElement * element, GstStateChange transition)
1546 GstStateChangeReturn ret;
1547 GstFlvMux *mux = GST_FLV_MUX (element);
1549 switch (transition) {
1550 case GST_STATE_CHANGE_NULL_TO_READY:
1552 case GST_STATE_CHANGE_READY_TO_PAUSED:
1553 gst_collect_pads_start (mux->collect);
1555 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1557 case GST_STATE_CHANGE_PAUSED_TO_READY:
1558 gst_collect_pads_stop (mux->collect);
1564 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1566 switch (transition) {
1567 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1569 case GST_STATE_CHANGE_PAUSED_TO_READY:
1570 gst_flv_mux_reset (GST_ELEMENT (mux));
1572 case GST_STATE_CHANGE_READY_TO_NULL: