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, 4 }, framed = (boolean) TRUE; "
78 "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
79 "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; "
80 "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; "
81 "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
82 "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
83 "audio/x-speex, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
86 #define _do_init(type) \
88 static const GInterfaceInfo tag_setter_info = { \
93 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, \
97 GST_BOILERPLATE_FULL (GstFlvMux, gst_flv_mux, GstElement, GST_TYPE_ELEMENT,
100 static void gst_flv_mux_finalize (GObject * object);
102 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data);
104 static gboolean gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event);
105 static GstPad *gst_flv_mux_request_new_pad (GstElement * element,
106 GstPadTemplate * templ, const gchar * name);
107 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
109 static void gst_flv_mux_get_property (GObject * object,
110 guint prop_id, GValue * value, GParamSpec * pspec);
111 static void gst_flv_mux_set_property (GObject * object,
112 guint prop_id, const GValue * value, GParamSpec * pspec);
114 static GstStateChangeReturn
115 gst_flv_mux_change_state (GstElement * element, GstStateChange transition);
117 static void gst_flv_mux_reset (GstElement * element);
118 static void gst_flv_mux_reset_pad (GstFlvMux * mux, GstFlvPad * pad,
125 } GstFlvMuxIndexEntry;
128 gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
130 g_slice_free (GstFlvMuxIndexEntry, entry);
134 gst_flv_mux_base_init (gpointer g_class)
136 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
138 gst_element_class_add_pad_template (element_class,
139 gst_static_pad_template_get (&videosink_templ));
140 gst_element_class_add_pad_template (element_class,
141 gst_static_pad_template_get (&audiosink_templ));
142 gst_element_class_add_pad_template (element_class,
143 gst_static_pad_template_get (&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 gst_buffer_set_caps (buffer, GST_PAD_CAPS (mux->srcpad));
576 /* pushing the buffer that rewrites the header will make it no longer be the
577 * total output size in bytes, but it doesn't matter at that point */
578 mux->byte_count += GST_BUFFER_SIZE (buffer);
580 return gst_pad_push (mux->srcpad, buffer);
584 gst_flv_mux_create_header (GstFlvMux * mux)
589 header = gst_buffer_new_and_alloc (9 + 4);
590 data = GST_BUFFER_DATA (header);
595 data[3] = 0x01; /* Version */
597 data[4] = (mux->have_audio << 2) | mux->have_video; /* flags */
598 GST_WRITE_UINT32_BE (data + 5, 9); /* data offset */
599 GST_WRITE_UINT32_BE (data + 9, 0); /* previous tag size */
605 gst_flv_mux_preallocate_index (GstFlvMux * mux)
609 gint preallocate_size;
611 /* preallocate index of size:
612 * - 'keyframes' ECMA array key: 2 + 9 = 11 bytes
613 * - nested ECMA array header, length and end marker: 8 bytes
614 * - 'times' and 'filepositions' keys: 22 bytes
615 * - two strict arrays headers and lengths: 10 bytes
616 * - each index entry: 18 bytes
618 preallocate_size = 11 + 8 + 22 + 10 + MAX_INDEX_ENTRIES * 18;
619 GST_DEBUG_OBJECT (mux, "preallocating %d bytes for the index",
622 tmp = gst_buffer_new_and_alloc (preallocate_size);
623 data = GST_BUFFER_DATA (tmp);
625 /* prefill the space with a gstfiller: <spaces> script tag variable */
626 GST_WRITE_UINT16_BE (data, 9); /* 9 characters */
627 memcpy (data + 2, "gstfiller", 9);
628 GST_WRITE_UINT8 (data + 11, AMF0_STRING_MARKER); /* a string value */
629 GST_WRITE_UINT16_BE (data + 12, preallocate_size - 14);
630 memset (data + 14, ' ', preallocate_size - 14); /* the rest is spaces */
635 gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
637 GstBuffer *tmp = gst_buffer_new_and_alloc (2 + strlen (name) + 1 + 8);
638 guint8 *data = GST_BUFFER_DATA (tmp);
639 gsize len = strlen (name);
641 GST_WRITE_UINT16_BE (data, len);
642 data += 2; /* name length */
643 memcpy (data, name, len);
645 *data++ = AMF0_NUMBER_MARKER; /* double type */
646 GST_WRITE_DOUBLE_BE (data, value);
652 gst_flv_mux_create_metadata (GstFlvMux * mux)
654 const GstTagList *tags;
655 GstBuffer *script_tag, *tmp;
657 gint i, n_tags, tags_written = 0;
659 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (mux));
661 GST_DEBUG_OBJECT (mux, "tags = %" GST_PTR_FORMAT, tags);
663 script_tag = gst_buffer_new_and_alloc (11);
664 data = GST_BUFFER_DATA (script_tag);
668 /* Data size, unknown for now */
674 data[4] = data[5] = data[6] = data[7] = 0;
677 data[8] = data[9] = data[10] = 0;
679 tmp = gst_buffer_new_and_alloc (13);
680 data = GST_BUFFER_DATA (tmp);
681 data[0] = AMF0_STRING_MARKER; /* string */
683 data[2] = 10; /* length 10 */
684 memcpy (&data[3], "onMetaData", 10);
686 script_tag = gst_buffer_join (script_tag, tmp);
688 n_tags = (tags) ? gst_structure_n_fields ((GstStructure *) tags) : 0;
689 tmp = gst_buffer_new_and_alloc (5);
690 data = GST_BUFFER_DATA (tmp);
691 data[0] = 8; /* ECMA array */
692 GST_WRITE_UINT32_BE (data + 1, n_tags);
693 script_tag = gst_buffer_join (script_tag, tmp);
695 /* Some players expect the 'duration' to be always set. Fill it out later,
696 after querying the pads or after getting EOS */
697 if (!mux->streamable) {
698 tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
699 script_tag = gst_buffer_join (script_tag, tmp);
702 /* Sometimes the information about the total file size is useful for the
703 player. It will be filled later, after getting EOS */
704 tmp = gst_flv_mux_create_number_script_value ("filesize", 0);
705 script_tag = gst_buffer_join (script_tag, tmp);
708 /* Preallocate space for the index to be written at EOS */
709 tmp = gst_flv_mux_preallocate_index (mux);
710 script_tag = gst_buffer_join (script_tag, tmp);
712 GST_DEBUG_OBJECT (mux, "not preallocating index, streamable mode");
715 for (i = 0; tags && i < n_tags; i++) {
716 const gchar *tag_name =
717 gst_structure_nth_field_name ((const GstStructure *) tags, i);
718 if (!strcmp (tag_name, GST_TAG_DURATION)) {
721 if (!gst_tag_list_get_uint64 (tags, GST_TAG_DURATION, &dur))
724 } else if (!strcmp (tag_name, GST_TAG_ARTIST) ||
725 !strcmp (tag_name, GST_TAG_TITLE)) {
727 const gchar *t = NULL;
729 if (!strcmp (tag_name, GST_TAG_ARTIST))
731 else if (!strcmp (tag_name, GST_TAG_TITLE))
734 if (!gst_tag_list_get_string (tags, tag_name, &s))
737 tmp = gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s));
738 data = GST_BUFFER_DATA (tmp);
739 data[0] = 0; /* tag name length */
740 data[1] = strlen (t);
741 memcpy (&data[2], t, strlen (t));
742 data[2 + strlen (t)] = 2; /* string */
743 data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
744 data[4 + strlen (t)] = (strlen (s)) & 0xff;
745 memcpy (&data[5 + strlen (t)], s, strlen (s));
746 script_tag = gst_buffer_join (script_tag, tmp);
753 if (mux->duration == GST_CLOCK_TIME_NONE) {
756 GstFormat fmt = GST_FORMAT_TIME;
759 for (l = mux->collect->data; l; l = l->next) {
760 GstCollectData *cdata = l->data;
762 fmt = GST_FORMAT_TIME;
764 if (gst_pad_query_peer_duration (cdata->pad, &fmt, (gint64 *) & dur) &&
765 fmt == GST_FORMAT_TIME && dur != GST_CLOCK_TIME_NONE) {
766 if (mux->duration == GST_CLOCK_TIME_NONE)
769 mux->duration = MAX (dur, mux->duration);
774 if (!mux->streamable && mux->duration != GST_CLOCK_TIME_NONE) {
776 d = gst_guint64_to_gdouble (mux->duration);
777 d /= (gdouble) GST_SECOND;
779 GST_DEBUG_OBJECT (mux, "determined the duration to be %f", d);
780 data = GST_BUFFER_DATA (script_tag);
781 GST_WRITE_DOUBLE_BE (data + 29 + 2 + 8 + 1, d);
784 if (mux->have_video) {
785 GstPad *video_pad = NULL;
787 GSList *l = mux->collect->data;
789 for (; l; l = l->next) {
791 if (cpad && cpad->video) {
792 video_pad = cpad->collect.pad;
797 if (video_pad && GST_PAD_CAPS (video_pad)) {
798 GstStructure *s = gst_caps_get_structure (GST_PAD_CAPS (video_pad), 0);
802 GST_DEBUG_OBJECT (mux, "putting videocodecid %d in the metadata",
805 tmp = gst_flv_mux_create_number_script_value ("videocodecid",
807 script_tag = gst_buffer_join (script_tag, tmp);
810 if (gst_structure_get_int (s, "width", &size)) {
811 GST_DEBUG_OBJECT (mux, "putting width %d in the metadata", size);
813 tmp = gst_flv_mux_create_number_script_value ("width", size);
814 script_tag = gst_buffer_join (script_tag, tmp);
818 if (gst_structure_get_int (s, "height", &size)) {
819 GST_DEBUG_OBJECT (mux, "putting height %d in the metadata", size);
821 tmp = gst_flv_mux_create_number_script_value ("height", size);
822 script_tag = gst_buffer_join (script_tag, tmp);
826 if (gst_structure_get_fraction (s, "pixel-aspect-ratio", &num, &den)) {
830 GST_DEBUG_OBJECT (mux, "putting AspectRatioX %f in the metadata", d);
832 tmp = gst_flv_mux_create_number_script_value ("AspectRatioX", d);
833 script_tag = gst_buffer_join (script_tag, tmp);
837 GST_DEBUG_OBJECT (mux, "putting AspectRatioY %f in the metadata", d);
839 tmp = gst_flv_mux_create_number_script_value ("AspectRatioY", d);
840 script_tag = gst_buffer_join (script_tag, tmp);
844 if (gst_structure_get_fraction (s, "framerate", &num, &den)) {
847 gst_util_fraction_to_double (num, den, &d);
848 GST_DEBUG_OBJECT (mux, "putting framerate %f in the metadata", d);
850 tmp = gst_flv_mux_create_number_script_value ("framerate", d);
851 script_tag = gst_buffer_join (script_tag, tmp);
857 if (mux->have_audio) {
858 GstPad *audio_pad = NULL;
860 GSList *l = mux->collect->data;
862 for (; l; l = l->next) {
864 if (cpad && !cpad->video) {
865 audio_pad = cpad->collect.pad;
871 GST_DEBUG_OBJECT (mux, "putting audiocodecid %d in the metadata",
874 tmp = gst_flv_mux_create_number_script_value ("audiocodecid",
876 script_tag = gst_buffer_join (script_tag, tmp);
882 const gchar *s = "GStreamer FLV muxer";
884 tmp = gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (s));
885 data = GST_BUFFER_DATA (tmp);
886 data[0] = 0; /* 15 bytes name */
888 memcpy (&data[2], "metadatacreator", 15);
889 data[17] = 2; /* string */
890 data[18] = (strlen (s) >> 8) & 0xff;
891 data[19] = (strlen (s)) & 0xff;
892 memcpy (&data[20], s, strlen (s));
893 script_tag = gst_buffer_join (script_tag, tmp);
899 GTimeVal tv = { 0, };
903 static const gchar *weekdays[] = {
904 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
906 static const gchar *months[] = {
907 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
908 "Aug", "Sep", "Oct", "Nov", "Dec"
911 g_get_current_time (&tv);
915 s = g_strdup_printf ("%s %s %d %d:%d:%d %d", weekdays[tm->tm_wday],
916 months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
919 tmp = gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s));
920 data = GST_BUFFER_DATA (tmp);
921 data[0] = 0; /* 12 bytes name */
923 memcpy (&data[2], "creationdate", 12);
924 data[14] = 2; /* string */
925 data[15] = (strlen (s) >> 8) & 0xff;
926 data[16] = (strlen (s)) & 0xff;
927 memcpy (&data[17], s, strlen (s));
928 script_tag = gst_buffer_join (script_tag, tmp);
934 tmp = gst_buffer_new_and_alloc (2 + 0 + 1);
935 data = GST_BUFFER_DATA (tmp);
936 data[0] = 0; /* 0 byte size */
938 data[2] = 9; /* end marker */
939 script_tag = gst_buffer_join (script_tag, tmp);
942 tmp = gst_buffer_new_and_alloc (4);
943 data = GST_BUFFER_DATA (tmp);
944 GST_WRITE_UINT32_BE (data, GST_BUFFER_SIZE (script_tag));
945 script_tag = gst_buffer_join (script_tag, tmp);
947 data = GST_BUFFER_DATA (script_tag);
948 data[1] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 16) & 0xff;
949 data[2] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 8) & 0xff;
950 data[3] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 0) & 0xff;
952 GST_WRITE_UINT32_BE (data + 11 + 13 + 1, tags_written);
958 gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
959 GstFlvPad * cpad, gboolean is_codec_data)
965 (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) ? GST_BUFFER_TIMESTAMP (buffer) /
966 GST_MSECOND : cpad->last_timestamp / GST_MSECOND;
971 if (cpad->video_codec == 7)
972 size += 4 + GST_BUFFER_SIZE (buffer);
974 size += GST_BUFFER_SIZE (buffer);
977 if (cpad->audio_codec == 10)
978 size += 1 + GST_BUFFER_SIZE (buffer);
980 size += GST_BUFFER_SIZE (buffer);
984 tag = gst_buffer_new_and_alloc (size);
985 GST_BUFFER_TIMESTAMP (tag) = timestamp * GST_MSECOND;
986 data = GST_BUFFER_DATA (tag);
987 memset (data, 0, size);
989 data[0] = (cpad->video) ? 9 : 8;
991 data[1] = ((size - 11 - 4) >> 16) & 0xff;
992 data[2] = ((size - 11 - 4) >> 8) & 0xff;
993 data[3] = ((size - 11 - 4) >> 0) & 0xff;
995 /* wrap the timestamp every G_MAXINT32 miliseconds */
996 timestamp &= 0x7fffffff;
997 data[4] = (timestamp >> 16) & 0xff;
998 data[5] = (timestamp >> 8) & 0xff;
999 data[6] = (timestamp >> 0) & 0xff;
1000 data[7] = (timestamp >> 24) & 0xff;
1002 data[8] = data[9] = data[10] = 0;
1005 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1010 data[11] |= cpad->video_codec & 0x0f;
1012 if (cpad->video_codec == 7) {
1013 data[12] = is_codec_data ? 0 : 1;
1015 /* FIXME: what to do about composition time */
1016 data[13] = data[14] = data[15] = 0;
1018 memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (buffer),
1019 GST_BUFFER_SIZE (buffer));
1021 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
1022 GST_BUFFER_SIZE (buffer));
1025 data[11] |= (cpad->audio_codec << 4) & 0xf0;
1026 data[11] |= (cpad->rate << 2) & 0x0c;
1027 data[11] |= (cpad->width << 1) & 0x02;
1028 data[11] |= (cpad->channels << 0) & 0x01;
1030 if (cpad->audio_codec == 10) {
1031 data[12] = is_codec_data ? 0 : 1;
1033 memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (buffer),
1034 GST_BUFFER_SIZE (buffer));
1036 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
1037 GST_BUFFER_SIZE (buffer));
1041 GST_WRITE_UINT32_BE (data + size - 4, size - 4);
1043 gst_buffer_copy_metadata (tag, buffer, GST_BUFFER_COPY_TIMESTAMPS);
1044 /* mark the buffer if it's an audio buffer and there's also video being muxed
1045 * or it's a video interframe */
1046 if ((mux->have_video && !cpad->video) ||
1047 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
1048 GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);
1050 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
1051 GST_BUFFER_OFFSET_NONE;
1056 static inline GstBuffer *
1057 gst_flv_mux_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1060 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, cpad, FALSE);
1063 static inline GstBuffer *
1064 gst_flv_mux_codec_data_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
1067 return gst_flv_mux_buffer_to_tag_internal (mux, buffer, cpad, TRUE);
1071 gst_flv_mux_put_buffer_in_streamheader (GValue * streamheader,
1074 GValue value = { 0 };
1077 g_value_init (&value, GST_TYPE_BUFFER);
1078 buf = gst_buffer_copy (buffer);
1079 gst_value_set_buffer (&value, buf);
1080 gst_buffer_unref (buf);
1081 gst_value_array_append_value (streamheader, &value);
1082 g_value_unset (&value);
1085 static GstFlowReturn
1086 gst_flv_mux_write_header (GstFlvMux * mux)
1088 GstBuffer *header, *metadata;
1089 GstBuffer *video_codec_data, *audio_codec_data;
1091 GstStructure *structure;
1092 GValue streamheader = { 0 };
1096 header = gst_flv_mux_create_header (mux);
1097 metadata = gst_flv_mux_create_metadata (mux);
1098 video_codec_data = NULL;
1099 audio_codec_data = NULL;
1101 for (l = mux->collect->data; l != NULL; l = l->next) {
1102 GstFlvPad *cpad = l->data;
1104 /* Get H.264 and AAC codec data, if present */
1105 if (cpad && cpad->video && cpad->video_codec == 7) {
1106 if (cpad->video_codec_data == NULL)
1107 GST_WARNING_OBJECT (mux, "Codec data for video stream not found, "
1108 "output might not be playable");
1111 gst_flv_mux_codec_data_buffer_to_tag (mux, cpad->video_codec_data,
1113 } else if (cpad && !cpad->video && cpad->audio_codec == 10) {
1114 if (cpad->audio_codec_data == NULL)
1115 GST_WARNING_OBJECT (mux, "Codec data for audio stream not found, "
1116 "output might not be playable");
1119 gst_flv_mux_codec_data_buffer_to_tag (mux, cpad->audio_codec_data,
1124 /* mark buffers that will go in the streamheader */
1125 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_IN_CAPS);
1126 GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_IN_CAPS);
1127 if (video_codec_data != NULL) {
1128 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_IN_CAPS);
1129 /* mark as a delta unit, so downstream will not try to synchronize on that
1130 * buffer - to actually start playback you need a real video keyframe */
1131 GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_DELTA_UNIT);
1133 if (audio_codec_data != NULL) {
1134 GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_IN_CAPS);
1137 /* put buffers in streamheader */
1138 g_value_init (&streamheader, GST_TYPE_ARRAY);
1139 gst_flv_mux_put_buffer_in_streamheader (&streamheader, header);
1140 gst_flv_mux_put_buffer_in_streamheader (&streamheader, metadata);
1141 if (video_codec_data != NULL)
1142 gst_flv_mux_put_buffer_in_streamheader (&streamheader, video_codec_data);
1143 if (audio_codec_data != NULL)
1144 gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);
1146 /* create the caps and put the streamheader in them */
1147 caps = gst_caps_new_simple ("video/x-flv", NULL);
1148 caps = gst_caps_make_writable (caps);
1149 structure = gst_caps_get_structure (caps, 0);
1150 gst_structure_set_value (structure, "streamheader", &streamheader);
1151 g_value_unset (&streamheader);
1153 if (GST_PAD_CAPS (mux->srcpad) == NULL)
1154 gst_pad_set_caps (mux->srcpad, caps);
1156 gst_caps_unref (caps);
1158 /* push the header buffer, the metadata and the codec info, if any */
1159 ret = gst_flv_mux_push (mux, header);
1160 if (ret != GST_FLOW_OK)
1162 ret = gst_flv_mux_push (mux, metadata);
1163 if (ret != GST_FLOW_OK)
1165 if (video_codec_data != NULL) {
1166 ret = gst_flv_mux_push (mux, video_codec_data);
1167 if (ret != GST_FLOW_OK)
1170 if (audio_codec_data != NULL) {
1171 ret = gst_flv_mux_push (mux, audio_codec_data);
1172 if (ret != GST_FLOW_OK)
1179 gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer, GstFlvPad * cpad)
1182 * Add the tag byte offset and to the index if it's a valid seek point, which
1183 * means it's either a video keyframe or if there is no video pad (in that
1184 * case every FLV tag is a valid seek point)
1186 if (mux->have_video &&
1188 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)))
1191 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
1192 GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
1193 entry->position = mux->byte_count;
1195 gst_guint64_to_gdouble (GST_BUFFER_TIMESTAMP (buffer)) / GST_SECOND;
1196 mux->index = g_list_prepend (mux->index, entry);
1200 static GstFlowReturn
1201 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad)
1205 gst_collect_pads_pop (mux->collect, (GstCollectData *) cpad);
1208 /* arrange downstream running time */
1209 buffer = gst_buffer_make_metadata_writable (buffer);
1210 GST_BUFFER_TIMESTAMP (buffer) =
1211 gst_segment_to_running_time (&cpad->collect.segment,
1212 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buffer));
1214 if (!mux->streamable)
1215 gst_flv_mux_update_index (mux, buffer, cpad);
1217 tag = gst_flv_mux_buffer_to_tag (mux, buffer, cpad);
1219 gst_buffer_unref (buffer);
1221 ret = gst_flv_mux_push (mux, tag);
1223 if (ret == GST_FLOW_OK && GST_BUFFER_TIMESTAMP_IS_VALID (tag))
1224 cpad->last_timestamp = GST_BUFFER_TIMESTAMP (tag);
1230 gst_flv_mux_determine_duration (GstFlvMux * mux)
1233 GstClockTime duration = GST_CLOCK_TIME_NONE;
1235 GST_DEBUG_OBJECT (mux, "trying to determine the duration "
1236 "from pad timestamps");
1238 for (l = mux->collect->data; l != NULL; l = l->next) {
1239 GstFlvPad *cpad = l->data;
1241 if (cpad && (cpad->last_timestamp != GST_CLOCK_TIME_NONE)) {
1242 if (duration == GST_CLOCK_TIME_NONE)
1243 duration = cpad->last_timestamp;
1245 duration = MAX (duration, cpad->last_timestamp);
1249 if (duration == GST_CLOCK_TIME_NONE) {
1250 GST_DEBUG_OBJECT (mux, "not able to determine duration "
1251 "from pad timestamps, assuming 0");
1258 static GstFlowReturn
1259 gst_flv_mux_rewrite_header (GstFlvMux * mux)
1261 GstBuffer *rewrite, *index, *tmp;
1266 guint32 index_len, allocate_size;
1267 guint32 i, index_skip;
1269 if (mux->streamable)
1272 /* seek back to the preallocated index space */
1273 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1274 13 + 29, GST_CLOCK_TIME_NONE, 13 + 29);
1275 if (!gst_pad_push_event (mux->srcpad, event)) {
1276 GST_WARNING_OBJECT (mux, "Seek to rewrite header failed");
1280 /* if we were not able to determine the duration before, set it now */
1281 if (mux->duration == GST_CLOCK_TIME_NONE)
1282 mux->duration = gst_flv_mux_determine_duration (mux);
1284 /* rewrite the duration tag */
1285 d = gst_guint64_to_gdouble (mux->duration);
1286 d /= (gdouble) GST_SECOND;
1288 GST_DEBUG_OBJECT (mux, "determined the final duration to be %f", d);
1290 rewrite = gst_flv_mux_create_number_script_value ("duration", d);
1292 /* rewrite the filesize tag */
1293 d = gst_guint64_to_gdouble (mux->byte_count);
1295 GST_DEBUG_OBJECT (mux, "putting total filesize %f in the metadata", d);
1297 tmp = gst_flv_mux_create_number_script_value ("filesize", d);
1298 rewrite = gst_buffer_join (rewrite, tmp);
1301 /* no index, so push buffer and return */
1302 return gst_flv_mux_push (mux, rewrite);
1305 /* rewrite the index */
1306 mux->index = g_list_reverse (mux->index);
1307 index_len = g_list_length (mux->index);
1309 /* We write at most MAX_INDEX_ENTRIES elements */
1310 if (index_len > MAX_INDEX_ENTRIES) {
1311 index_skip = 1 + index_len / MAX_INDEX_ENTRIES;
1312 index_len = (index_len + index_skip - 1) / index_skip;
1317 GST_DEBUG_OBJECT (mux, "Index length is %d", index_len);
1318 /* see size calculation in gst_flv_mux_preallocate_index */
1319 allocate_size = 11 + 8 + 22 + 10 + index_len * 18;
1320 GST_DEBUG_OBJECT (mux, "Allocating %d bytes for index", allocate_size);
1321 index = gst_buffer_new_and_alloc (allocate_size);
1322 data = GST_BUFFER_DATA (index);
1324 GST_WRITE_UINT16_BE (data, 9); /* the 'keyframes' key */
1325 memcpy (data + 2, "keyframes", 9);
1326 GST_WRITE_UINT8 (data + 11, 8); /* nested ECMA array */
1327 GST_WRITE_UINT32_BE (data + 12, 2); /* two elements */
1328 GST_WRITE_UINT16_BE (data + 16, 5); /* first string key: 'times' */
1329 memcpy (data + 18, "times", 5);
1330 GST_WRITE_UINT8 (data + 23, 10); /* strict array */
1331 GST_WRITE_UINT32_BE (data + 24, index_len);
1334 /* the keyframes' times */
1335 for (i = 0, l = mux->index; l; l = l->next, i++) {
1336 GstFlvMuxIndexEntry *entry = l->data;
1338 if (i % index_skip != 0)
1340 GST_WRITE_UINT8 (data, 0); /* numeric (aka double) */
1341 GST_WRITE_DOUBLE_BE (data + 1, entry->time);
1345 GST_WRITE_UINT16_BE (data, 13); /* second string key: 'filepositions' */
1346 memcpy (data + 2, "filepositions", 13);
1347 GST_WRITE_UINT8 (data + 15, 10); /* strict array */
1348 GST_WRITE_UINT32_BE (data + 16, index_len);
1351 /* the keyframes' file positions */
1352 for (i = 0, l = mux->index; l; l = l->next, i++) {
1353 GstFlvMuxIndexEntry *entry = l->data;
1355 if (i % index_skip != 0)
1357 GST_WRITE_UINT8 (data, 0);
1358 GST_WRITE_DOUBLE_BE (data + 1, entry->position);
1362 GST_WRITE_UINT24_BE (data, 9); /* finish the ECMA array */
1364 /* If there is space left in the prefilled area, reinsert the filler.
1365 There is at least 18 bytes free, so it will always fit. */
1366 if (index_len < MAX_INDEX_ENTRIES) {
1369 guint32 remaining_filler_size;
1371 tmp = gst_buffer_new_and_alloc (14);
1372 data = GST_BUFFER_DATA (tmp);
1373 GST_WRITE_UINT16_BE (data, 9);
1374 memcpy (data + 2, "gstfiller", 9);
1375 GST_WRITE_UINT8 (data + 11, 2); /* string */
1377 /* There is 18 bytes per remaining index entry minus what is used for
1378 * the'gstfiller' key. The rest is already filled with spaces, so just need
1379 * to update length. */
1380 remaining_filler_size = (MAX_INDEX_ENTRIES - index_len) * 18 - 14;
1381 GST_DEBUG_OBJECT (mux, "Remaining filler size is %d bytes",
1382 remaining_filler_size);
1383 GST_WRITE_UINT16_BE (data + 12, remaining_filler_size);
1384 index = gst_buffer_join (index, tmp);
1387 rewrite = gst_buffer_join (rewrite, index);
1389 gst_buffer_set_caps (rewrite, GST_PAD_CAPS (mux->srcpad));
1390 return gst_flv_mux_push (mux, rewrite);
1393 static GstFlowReturn
1394 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data)
1396 GstFlvMux *mux = GST_FLV_MUX (user_data);
1398 GstClockTime best_time;
1401 gboolean eos = TRUE;
1403 if (mux->state == GST_FLV_MUX_STATE_HEADER) {
1404 if (mux->collect->data == NULL) {
1405 GST_ELEMENT_ERROR (mux, STREAM, MUX, (NULL),
1406 ("No input streams configured"));
1407 return GST_FLOW_ERROR;
1410 if (gst_pad_push_event (mux->srcpad,
1411 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0)))
1412 ret = gst_flv_mux_write_header (mux);
1414 ret = GST_FLOW_ERROR;
1416 if (ret != GST_FLOW_OK)
1418 mux->state = GST_FLV_MUX_STATE_DATA;
1421 if (mux->new_tags) {
1422 GstBuffer *buf = gst_flv_mux_create_metadata (mux);
1423 gst_flv_mux_push (mux, buf);
1424 mux->new_tags = FALSE;
1429 best_time = GST_CLOCK_TIME_NONE;
1430 for (sl = mux->collect->data; sl; sl = sl->next) {
1431 GstFlvPad *cpad = sl->data;
1432 GstBuffer *buffer = gst_collect_pads_peek (pads, (GstCollectData *) cpad);
1440 time = GST_BUFFER_TIMESTAMP (buffer);
1441 gst_buffer_unref (buffer);
1443 /* Use buffers without valid timestamp first */
1444 if (!GST_CLOCK_TIME_IS_VALID (time)) {
1445 GST_WARNING_OBJECT (pads, "Buffer without valid timestamp");
1447 best_time = cpad->last_timestamp;
1452 time = gst_segment_to_running_time (&cpad->collect.segment,
1453 GST_FORMAT_TIME, time);
1454 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
1455 GST_DEBUG_OBJECT (mux, "clipping buffer on pad %s outside segment",
1456 GST_PAD_NAME (cpad->collect.pad));
1457 buffer = gst_collect_pads_pop (pads, (GstCollectData *) cpad);
1458 gst_buffer_unref (buffer);
1462 if (best == NULL || (GST_CLOCK_TIME_IS_VALID (best_time)
1463 && time < best_time)) {
1469 /* The FLV timestamp is an int32 field. For non-live streams error out if a
1470 bigger timestamp is seen, for live the timestamp will get wrapped in
1471 gst_flv_mux_buffer_to_tag */
1472 if (!mux->streamable && GST_CLOCK_TIME_IS_VALID (best_time)
1473 && best_time / GST_MSECOND > G_MAXINT32) {
1474 GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
1479 return gst_flv_mux_write_buffer (mux, best);
1481 gst_flv_mux_rewrite_header (mux);
1482 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
1483 return GST_FLOW_UNEXPECTED;
1490 gst_flv_mux_get_property (GObject * object,
1491 guint prop_id, GValue * value, GParamSpec * pspec)
1493 GstFlvMux *mux = GST_FLV_MUX (object);
1496 case PROP_STREAMABLE:
1497 g_value_set_boolean (value, mux->streamable);
1500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1506 gst_flv_mux_set_property (GObject * object,
1507 guint prop_id, const GValue * value, GParamSpec * pspec)
1509 GstFlvMux *mux = GST_FLV_MUX (object);
1512 case PROP_STREAMABLE:
1513 mux->streamable = g_value_get_boolean (value);
1514 if (mux->streamable)
1515 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1516 GST_TAG_MERGE_REPLACE);
1518 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (mux),
1519 GST_TAG_MERGE_KEEP);
1522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1527 static GstStateChangeReturn
1528 gst_flv_mux_change_state (GstElement * element, GstStateChange transition)
1530 GstStateChangeReturn ret;
1531 GstFlvMux *mux = GST_FLV_MUX (element);
1533 switch (transition) {
1534 case GST_STATE_CHANGE_NULL_TO_READY:
1536 case GST_STATE_CHANGE_READY_TO_PAUSED:
1537 gst_collect_pads_start (mux->collect);
1539 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1541 case GST_STATE_CHANGE_PAUSED_TO_READY:
1542 gst_collect_pads_stop (mux->collect);
1548 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1550 switch (transition) {
1551 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1553 case GST_STATE_CHANGE_PAUSED_TO_READY:
1554 gst_flv_mux_reset (GST_ELEMENT (mux));
1556 case GST_STATE_CHANGE_READY_TO_NULL: