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"
43 GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
44 #define GST_CAT_DEFAULT flvmux_debug
46 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("video/x-flv")
52 static GstStaticPadTemplate videosink_templ = GST_STATIC_PAD_TEMPLATE ("video",
55 GST_STATIC_CAPS ("video/x-flash-video; "
56 "video/x-flash-screen; "
57 "video/x-vp6-flash; " "video/x-vp6-alpha; " "video/x-h264;")
60 static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
64 ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
65 "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }, parsed = (boolean) TRUE; "
66 "audio/mpeg, mpegversion = (int) 4, framed = (boolean) TRUE; "
67 "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
68 "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; "
69 "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; "
70 "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
71 "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
72 "audio/x-speex, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
75 #define _do_init(type) \
77 static const GInterfaceInfo tag_setter_info = { \
82 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, \
86 GST_BOILERPLATE_FULL (GstFlvMux, gst_flv_mux, GstElement, GST_TYPE_ELEMENT,
89 static void gst_flv_mux_finalize (GObject * object);
91 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data);
93 static gboolean gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event);
94 static GstPad *gst_flv_mux_request_new_pad (GstElement * element,
95 GstPadTemplate * templ, const gchar * name);
96 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
98 static GstStateChangeReturn
99 gst_flv_mux_change_state (GstElement * element, GstStateChange transition);
101 static void gst_flv_mux_reset (GstElement * element);
107 } GstFlvMuxIndexEntry;
110 gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
112 g_slice_free (GstFlvMuxIndexEntry, entry);
116 gst_flv_mux_base_init (gpointer g_class)
118 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
120 gst_element_class_add_pad_template (element_class,
121 gst_static_pad_template_get (&videosink_templ));
122 gst_element_class_add_pad_template (element_class,
123 gst_static_pad_template_get (&audiosink_templ));
124 gst_element_class_add_pad_template (element_class,
125 gst_static_pad_template_get (&src_templ));
126 gst_element_class_set_details_simple (element_class, "FLV muxer",
128 "Muxes video/audio streams into a FLV stream",
129 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
131 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
135 gst_flv_mux_class_init (GstFlvMuxClass * klass)
137 GObjectClass *gobject_class;
138 GstElementClass *gstelement_class;
140 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
142 gobject_class = (GObjectClass *) klass;
143 gstelement_class = (GstElementClass *) klass;
145 gobject_class->finalize = gst_flv_mux_finalize;
147 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_flv_mux_change_state);
148 gstelement_class->request_new_pad =
149 GST_DEBUG_FUNCPTR (gst_flv_mux_request_new_pad);
150 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
154 gst_flv_mux_init (GstFlvMux * mux, GstFlvMuxClass * g_class)
156 mux->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
157 gst_pad_set_event_function (mux->srcpad, gst_flv_mux_handle_src_event);
158 gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
160 mux->collect = gst_collect_pads_new ();
161 gst_collect_pads_set_function (mux->collect,
162 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_flv_mux_collected), mux);
164 gst_flv_mux_reset (GST_ELEMENT (mux));
168 gst_flv_mux_finalize (GObject * object)
170 GstFlvMux *mux = GST_FLV_MUX (object);
172 gst_object_unref (mux->collect);
174 G_OBJECT_CLASS (parent_class)->finalize (object);
178 gst_flv_mux_reset (GstElement * element)
180 GstFlvMux *mux = GST_FLV_MUX (element);
183 while ((sl = mux->collect->data) != NULL) {
184 GstFlvPad *cpad = (GstFlvPad *) sl->data;
186 gst_element_release_request_pad (element, cpad->collect.pad);
189 g_list_foreach (mux->index, (GFunc) gst_flv_mux_index_entry_free, NULL);
190 g_list_free (mux->index);
194 mux->have_audio = mux->have_video = FALSE;
196 mux->state = GST_FLV_MUX_STATE_HEADER;
199 gst_tag_setter_reset_tags (GST_TAG_SETTER (mux));
203 gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event)
207 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
211 /* disable seeking for now */
217 return gst_pad_event_default (pad, event);
221 gst_flv_mux_handle_sink_event (GstPad * pad, GstEvent * event)
223 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
226 switch (GST_EVENT_TYPE (event)) {
229 GstTagSetter *setter = GST_TAG_SETTER (mux);
230 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
232 gst_event_parse_tag (event, &list);
233 gst_tag_setter_merge_tags (setter, list, mode);
236 case GST_EVENT_NEWSEGMENT:
237 /* We don't support NEWSEGMENT events */
239 gst_event_unref (event);
245 /* now GstCollectPads can take care of the rest, e.g. EOS */
247 ret = mux->collect_event (pad, event);
248 gst_object_unref (mux);
254 gst_flv_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps)
256 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
257 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
261 s = gst_caps_get_structure (caps, 0);
263 if (strcmp (gst_structure_get_name (s), "video/x-flash-video") == 0) {
264 cpad->video_codec = 2;
265 } else if (strcmp (gst_structure_get_name (s), "video/x-flash-screen") == 0) {
266 cpad->video_codec = 3;
267 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-flash") == 0) {
268 cpad->video_codec = 4;
269 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-alpha") == 0) {
270 cpad->video_codec = 5;
271 } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
272 cpad->video_codec = 7;
277 if (ret && gst_structure_has_field (s, "codec_data")) {
278 const GValue *val = gst_structure_get_value (s, "codec_data");
281 cpad->video_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
282 cpad->sent_codec_data = FALSE;
284 cpad->sent_codec_data = TRUE;
287 cpad->sent_codec_data = TRUE;
290 gst_object_unref (mux);
296 gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
298 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
299 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
303 s = gst_caps_get_structure (caps, 0);
305 if (strcmp (gst_structure_get_name (s), "audio/x-adpcm") == 0) {
306 const gchar *layout = gst_structure_get_string (s, "layout");
307 if (layout && strcmp (layout, "swf") == 0) {
308 cpad->audio_codec = 1;
312 } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
315 if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
316 if (mpegversion == 1) {
319 if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
322 if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
323 cpad->audio_codec = 14;
325 cpad->audio_codec = 2;
329 } else if (mpegversion == 4) {
330 cpad->audio_codec = 10;
337 } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
340 if (gst_structure_get_int (s, "rate", &rate)
341 && gst_structure_get_int (s, "channels", &channels)) {
342 if (channels == 1 && rate == 16000)
343 cpad->audio_codec = 4;
344 else if (channels == 1 && rate == 8000)
345 cpad->audio_codec = 5;
347 cpad->audio_codec = 6;
349 } else if (strcmp (gst_structure_get_name (s), "audio/x-raw-int") == 0) {
352 if (gst_structure_get_int (s, "endianness", &endianness)
353 && endianness == G_LITTLE_ENDIAN)
354 cpad->audio_codec = 3;
357 } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
358 cpad->audio_codec = 7;
359 } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
360 cpad->audio_codec = 8;
361 } else if (strcmp (gst_structure_get_name (s), "audio/x-speex") == 0) {
362 cpad->audio_codec = 11;
368 gint rate, channels, width;
370 if (gst_structure_get_int (s, "rate", &rate)) {
371 if (cpad->audio_codec == 10)
373 else if (rate == 5512)
375 else if (rate == 11025)
377 else if (rate == 22050)
379 else if (rate == 44100)
381 else if (rate == 8000 && (cpad->audio_codec == 5
382 || cpad->audio_codec == 14))
384 else if (rate == 16000 && cpad->audio_codec == 4)
388 } else if (cpad->audio_codec == 10) {
394 if (gst_structure_get_int (s, "channels", &channels)) {
395 if (cpad->audio_codec == 4 || cpad->audio_codec == 5
396 || cpad->audio_codec == 6)
398 else if (cpad->audio_codec == 10)
400 else if (channels == 1)
402 else if (channels == 2)
406 } else if (cpad->audio_codec == 4 || cpad->audio_codec == 5
407 || cpad->audio_codec == 6) {
409 } else if (cpad->audio_codec == 10) {
415 if (gst_structure_get_int (s, "width", &width)) {
416 if (cpad->audio_codec != 3)
420 else if (width == 16)
424 } else if (cpad->audio_codec != 3) {
431 if (ret && gst_structure_has_field (s, "codec_data")) {
432 const GValue *val = gst_structure_get_value (s, "codec_data");
435 cpad->audio_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
436 cpad->sent_codec_data = FALSE;
438 cpad->sent_codec_data = TRUE;
441 cpad->sent_codec_data = TRUE;
444 gst_object_unref (mux);
450 gst_flv_mux_request_new_pad (GstElement * element,
451 GstPadTemplate * templ, const gchar * pad_name)
453 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
454 GstFlvMux *mux = GST_FLV_MUX (element);
457 const gchar *name = NULL;
458 GstPadSetCapsFunction setcapsfunc = NULL;
461 if (mux->state != GST_FLV_MUX_STATE_HEADER) {
462 GST_WARNING_OBJECT (mux, "Can't request pads after writing header");
466 if (templ == gst_element_class_get_pad_template (klass, "audio")) {
467 if (mux->have_audio) {
468 GST_WARNING_OBJECT (mux, "Already have an audio pad");
471 mux->have_audio = TRUE;
474 setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_audio_pad_setcaps);
475 } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
476 if (mux->have_video) {
477 GST_WARNING_OBJECT (mux, "Already have a video pad");
480 mux->have_video = TRUE;
483 setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_video_pad_setcaps);
485 GST_WARNING_OBJECT (mux, "Invalid template");
489 pad = gst_pad_new_from_template (templ, name);
491 gst_collect_pads_add_pad (mux->collect, pad, sizeof (GstFlvPad));
495 cpad->audio_codec = G_MAXUINT;
496 cpad->rate = G_MAXUINT;
497 cpad->width = G_MAXUINT;
498 cpad->channels = G_MAXUINT;
499 cpad->audio_codec_data = NULL;
501 cpad->video_codec = G_MAXUINT;
502 cpad->video_codec_data = NULL;
504 cpad->sent_codec_data = FALSE;
506 cpad->last_timestamp = 0;
508 /* FIXME: hacked way to override/extend the event function of
509 * GstCollectPads; because it sets its own event function giving the
510 * element no access to events.
512 mux->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (pad);
513 gst_pad_set_event_function (pad,
514 GST_DEBUG_FUNCPTR (gst_flv_mux_handle_sink_event));
516 gst_pad_set_setcaps_function (pad, setcapsfunc);
517 gst_pad_set_active (pad, TRUE);
518 gst_element_add_pad (element, pad);
524 gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
526 GstFlvMux *mux = GST_FLV_MUX (GST_PAD_PARENT (pad));
527 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
529 if (cpad && cpad->audio_codec_data)
530 gst_buffer_unref (cpad->audio_codec_data);
531 if (cpad && cpad->video_codec_data)
532 gst_buffer_unref (cpad->video_codec_data);
534 gst_collect_pads_remove_pad (mux->collect, pad);
535 gst_element_remove_pad (element, pad);
539 gst_flv_mux_push (GstFlvMux * mux, GstBuffer * buffer)
541 mux->byte_count += GST_BUFFER_SIZE (buffer);
543 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
544 GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
545 entry->position = mux->byte_count;
547 gst_guint64_to_gdouble (GST_BUFFER_TIMESTAMP (buffer)) / GST_MSECOND;
548 mux->index = g_list_prepend (mux->index, entry);
551 return gst_pad_push (mux->srcpad, buffer);
555 gst_flv_mux_write_metadata (GstFlvMux * mux)
557 const GstTagList *tags;
558 GstFlowReturn ret = GST_FLOW_OK;
559 GstBuffer *script_tag, *tmp;
561 gint i, n_tags, tags_written = 0;
562 GstClockTime duration = GST_CLOCK_TIME_NONE;
564 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (mux));
566 GST_DEBUG_OBJECT (mux, "tags = %" GST_PTR_FORMAT, tags);
568 script_tag = gst_buffer_new_and_alloc (11);
569 data = GST_BUFFER_DATA (script_tag);
573 /* Data size, unknown for now */
579 data[4] = data[5] = data[6] = data[7] = 0;
582 data[8] = data[9] = data[10] = 0;
584 tmp = gst_buffer_new_and_alloc (13);
585 data = GST_BUFFER_DATA (tmp);
586 data[0] = 2; /* string */
588 data[2] = 10; /* length 10 */
589 memcpy (&data[3], "onMetaData", 10);
591 script_tag = gst_buffer_join (script_tag, tmp);
593 n_tags = (tags) ? gst_structure_n_fields ((GstStructure *) tags) : 0;
594 tmp = gst_buffer_new_and_alloc (5);
595 data = GST_BUFFER_DATA (tmp);
596 data[0] = 8; /* ECMA array */
597 GST_WRITE_UINT32_BE (data + 1, n_tags);
598 script_tag = gst_buffer_join (script_tag, tmp);
600 for (i = 0; tags && i < n_tags; i++) {
601 const gchar *tag_name =
602 gst_structure_nth_field_name ((const GstStructure *) tags, i);
603 if (!strcmp (tag_name, GST_TAG_DURATION)) {
606 if (!gst_tag_list_get_uint64 (tags, GST_TAG_DURATION, &dur))
609 } else if (!strcmp (tag_name, GST_TAG_ARTIST) ||
610 !strcmp (tag_name, GST_TAG_TITLE)) {
612 const gchar *t = NULL;
614 if (!strcmp (tag_name, GST_TAG_ARTIST))
616 else if (!strcmp (tag_name, GST_TAG_TITLE))
619 if (!gst_tag_list_get_string (tags, tag_name, &s))
622 tmp = gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s));
623 data = GST_BUFFER_DATA (tmp);
624 data[0] = 0; /* tag name length */
625 data[1] = strlen (t);
626 memcpy (&data[2], t, strlen (t));
627 data[2 + strlen (t)] = 2; /* string */
628 data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
629 data[4 + strlen (t)] = (strlen (s)) & 0xff;
630 memcpy (&data[5 + strlen (t)], s, strlen (s));
631 script_tag = gst_buffer_join (script_tag, tmp);
639 if (duration == GST_CLOCK_TIME_NONE) {
642 GstFormat fmt = GST_FORMAT_TIME;
645 for (l = mux->collect->data; l; l = l->next) {
646 GstCollectData *cdata = l->data;
648 fmt = GST_FORMAT_TIME;
650 if (gst_pad_query_peer_duration (cdata->pad, &fmt, (gint64 *) & dur) &&
651 fmt == GST_FORMAT_TIME && dur != GST_CLOCK_TIME_NONE) {
652 if (duration == GST_CLOCK_TIME_NONE)
655 duration = MAX (dur, duration);
660 if (duration != GST_CLOCK_TIME_NONE) {
662 d = gst_guint64_to_gdouble (duration);
663 d /= (gdouble) GST_SECOND;
665 tmp = gst_buffer_new_and_alloc (2 + 8 + 1 + 8);
666 data = GST_BUFFER_DATA (tmp);
667 data[0] = 0; /* 8 bytes name */
669 memcpy (&data[2], "duration", 8);
670 data[10] = 0; /* double */
671 GST_WRITE_DOUBLE_BE (data + 11, d);
672 script_tag = gst_buffer_join (script_tag, tmp);
676 if (mux->have_video) {
677 GstPad *video_pad = NULL;
678 GSList *l = mux->collect->data;
680 for (; l; l = l->next) {
681 GstFlvPad *cpad = l->data;
682 if (cpad && cpad->video) {
683 video_pad = cpad->collect.pad;
688 if (video_pad && GST_PAD_CAPS (video_pad)) {
689 GstStructure *s = gst_caps_get_structure (GST_PAD_CAPS (video_pad), 0);
692 if (gst_structure_get_fraction (s, "pixel-aspect-ratio", &par_x, &par_y)) {
696 tmp = gst_buffer_new_and_alloc (2 + 12 + 1 + 8);
697 data = GST_BUFFER_DATA (tmp);
698 data[0] = 0; /* 12 bytes name */
700 memcpy (&data[2], "AspectRatioX", 12);
701 data[14] = 0; /* double */
702 GST_WRITE_DOUBLE_BE (data + 15, d);
703 script_tag = gst_buffer_join (script_tag, tmp);
707 tmp = gst_buffer_new_and_alloc (2 + 12 + 1 + 8);
708 data = GST_BUFFER_DATA (tmp);
709 data[0] = 0; /* 12 bytes name */
711 memcpy (&data[2], "AspectRatioY", 12);
712 data[14] = 0; /* double */
713 GST_WRITE_DOUBLE_BE (data + 15, d);
714 script_tag = gst_buffer_join (script_tag, tmp);
721 const gchar *s = "GStreamer FLV muxer";
723 tmp = gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (s));
724 data = GST_BUFFER_DATA (tmp);
725 data[0] = 0; /* 15 bytes name */
727 memcpy (&data[2], "metadatacreator", 15);
728 data[17] = 2; /* string */
729 data[18] = (strlen (s) >> 8) & 0xff;
730 data[19] = (strlen (s)) & 0xff;
731 memcpy (&data[20], s, strlen (s));
732 script_tag = gst_buffer_join (script_tag, tmp);
738 GTimeVal tv = { 0, };
742 static const gchar *weekdays[] = {
743 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
745 static const gchar *months[] = {
746 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
747 "Aug", "Sep", "Oct", "Nov", "Dec"
750 g_get_current_time (&tv);
754 s = g_strdup_printf ("%s %s %d %d:%d:%d %d", weekdays[tm->tm_wday],
755 months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
758 tmp = gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s));
759 data = GST_BUFFER_DATA (tmp);
760 data[0] = 0; /* 12 bytes name */
762 memcpy (&data[2], "creationdate", 12);
763 data[14] = 2; /* string */
764 data[15] = (strlen (s) >> 8) & 0xff;
765 data[16] = (strlen (s)) & 0xff;
766 memcpy (&data[17], s, strlen (s));
767 script_tag = gst_buffer_join (script_tag, tmp);
773 tmp = gst_buffer_new_and_alloc (2 + 0 + 1);
774 data = GST_BUFFER_DATA (tmp);
775 data[0] = 0; /* 0 byte size */
777 data[2] = 9; /* end marker */
778 script_tag = gst_buffer_join (script_tag, tmp);
782 tmp = gst_buffer_new_and_alloc (4);
783 data = GST_BUFFER_DATA (tmp);
784 GST_WRITE_UINT32_BE (data, GST_BUFFER_SIZE (script_tag));
785 script_tag = gst_buffer_join (script_tag, tmp);
787 data = GST_BUFFER_DATA (script_tag);
788 data[1] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 16) & 0xff;
789 data[2] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 8) & 0xff;
790 data[3] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 0) & 0xff;
792 GST_WRITE_UINT32_BE (data + 11 + 13 + 1, tags_written);
794 gst_buffer_set_caps (script_tag, GST_PAD_CAPS (mux->srcpad));
795 ret = gst_flv_mux_push (mux, script_tag);
801 gst_flv_mux_write_header (GstFlvMux * mux)
803 GstBuffer *header = gst_buffer_new_and_alloc (9 + 4);
804 guint8 *data = GST_BUFFER_DATA (header);
807 if (GST_PAD_CAPS (mux->srcpad) == NULL) {
808 GstCaps *caps = gst_caps_new_simple ("video/x-flv", NULL);
810 gst_pad_set_caps (mux->srcpad, caps);
811 gst_caps_unref (caps);
813 gst_buffer_set_caps (header, GST_PAD_CAPS (mux->srcpad));
818 data[3] = 0x01; /* Version */
820 data[4] = (mux->have_audio << 2) | mux->have_video; /* flags */
821 GST_WRITE_UINT32_BE (data + 5, 9); /* data offset */
823 GST_WRITE_UINT32_BE (data + 9, 0); /* previous tag size */
825 ret = gst_flv_mux_push (mux, header);
826 if (ret != GST_FLOW_OK)
829 return gst_flv_mux_write_metadata (mux);
833 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad)
839 gst_collect_pads_pop (mux->collect, (GstCollectData *) cpad);
841 (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) ? GST_BUFFER_TIMESTAMP (buffer) /
842 GST_MSECOND : cpad->last_timestamp / GST_MSECOND;
843 gboolean second_run = FALSE;
850 if (cpad->video_codec == 7 && !cpad->sent_codec_data)
851 size += 4 + GST_BUFFER_SIZE (cpad->video_codec_data);
852 else if (cpad->video_codec == 7)
853 size += 4 + GST_BUFFER_SIZE (buffer);
855 size += GST_BUFFER_SIZE (buffer);
858 if (cpad->audio_codec == 10 && !cpad->sent_codec_data)
859 size += 1 + GST_BUFFER_SIZE (cpad->audio_codec_data);
860 else if (cpad->audio_codec == 10)
861 size += 1 + GST_BUFFER_SIZE (buffer);
863 size += GST_BUFFER_SIZE (buffer);
867 tag = gst_buffer_new_and_alloc (size);
868 GST_BUFFER_TIMESTAMP (tag) = timestamp * GST_MSECOND;
869 data = GST_BUFFER_DATA (tag);
870 memset (data, 0, size);
872 data[0] = (cpad->video) ? 9 : 8;
874 data[1] = ((size - 11 - 4) >> 16) & 0xff;
875 data[2] = ((size - 11 - 4) >> 8) & 0xff;
876 data[3] = ((size - 11 - 4) >> 0) & 0xff;
878 data[4] = (timestamp >> 16) & 0xff;
879 data[5] = (timestamp >> 8) & 0xff;
880 data[6] = (timestamp >> 0) & 0xff;
881 data[7] = (timestamp >> 24) & 0xff;
883 data[8] = data[9] = data[10] = 0;
886 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
891 data[11] |= cpad->video_codec & 0x0f;
893 if (cpad->video_codec == 7 && !cpad->sent_codec_data) {
895 data[13] = data[14] = data[15] = 0;
897 memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (cpad->video_codec_data),
898 GST_BUFFER_SIZE (cpad->video_codec_data));
900 } else if (cpad->video_codec == 7) {
903 /* FIXME: what to do about composition time */
904 data[13] = data[14] = data[15] = 0;
906 memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (buffer),
907 GST_BUFFER_SIZE (buffer));
909 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
910 GST_BUFFER_SIZE (buffer));
913 data[11] |= (cpad->audio_codec << 4) & 0xf0;
914 data[11] |= (cpad->rate << 2) & 0x0c;
915 data[11] |= (cpad->width << 1) & 0x02;
916 data[11] |= (cpad->channels << 0) & 0x01;
918 if (cpad->audio_codec == 10 && !cpad->sent_codec_data) {
921 memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (cpad->audio_codec_data),
922 GST_BUFFER_SIZE (cpad->audio_codec_data));
924 } else if (cpad->audio_codec == 10) {
927 memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (buffer),
928 GST_BUFFER_SIZE (buffer));
930 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
931 GST_BUFFER_SIZE (buffer));
935 GST_WRITE_UINT32_BE (data + size - 4, size - 4);
937 gst_buffer_set_caps (tag, GST_PAD_CAPS (mux->srcpad));
941 cpad->sent_codec_data = TRUE;
943 ret = gst_flv_mux_push (mux, tag);
944 if (ret != GST_FLOW_OK) {
945 gst_buffer_unref (buffer);
949 cpad->last_timestamp = timestamp;
955 gst_buffer_copy_metadata (tag, buffer, GST_BUFFER_COPY_TIMESTAMPS);
956 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
957 GST_BUFFER_OFFSET_NONE;
959 gst_buffer_unref (buffer);
961 ret = gst_flv_mux_push (mux, tag);
963 if (ret == GST_FLOW_OK)
964 cpad->last_timestamp = timestamp;
970 gst_flv_mux_write_index (GstFlvMux * mux)
972 GstFlowReturn ret = GST_FLOW_OK;
973 GstBuffer *script_tag, *tmp;
977 guint32 i, index_skip;
982 script_tag = gst_buffer_new_and_alloc (11);
983 data = GST_BUFFER_DATA (script_tag);
987 /* Data size, unknown for now */
993 data[4] = data[5] = data[6] = data[7] = 0;
996 data[8] = data[9] = data[10] = 0;
998 tmp = gst_buffer_new_and_alloc (13);
999 data = GST_BUFFER_DATA (tmp);
1000 data[0] = 2; /* string */
1002 data[2] = 0x0a; /* length 10 */
1003 memcpy (&data[3], "onMetaData", 10);
1005 script_tag = gst_buffer_join (script_tag, tmp);
1007 tmp = gst_buffer_new_and_alloc (5);
1008 data = GST_BUFFER_DATA (tmp);
1009 data[0] = 8; /* ECMA array */
1010 GST_WRITE_UINT32_BE (data + 1, 2);
1011 script_tag = gst_buffer_join (script_tag, tmp);
1013 mux->index = g_list_reverse (mux->index);
1014 index_len = g_list_length (mux->index);
1016 /* We write at most 128 elements */
1017 index_skip = (index_len > 128) ? 1 + index_len / 128 : 1;
1020 128) ? index_len : (index_len + index_skip - 1) / index_skip;
1022 tmp = gst_buffer_new_and_alloc (2 + 5 + 1 + 4 + index_len * (1 + 8));
1023 data = GST_BUFFER_DATA (tmp);
1024 data[0] = 0; /* 5 bytes name */
1026 memcpy (&data[2], "times", 5);
1027 data[7] = 10; /* array */
1028 GST_WRITE_UINT32_BE (&data[8], index_len);
1031 for (i = 0, l = mux->index; l; l = l->next, i++) {
1032 GstFlvMuxIndexEntry *entry = l->data;
1034 if (i % index_skip != 0)
1038 GST_WRITE_DOUBLE_BE (&data[1], entry->time);
1041 script_tag = gst_buffer_join (script_tag, tmp);
1043 tmp = gst_buffer_new_and_alloc (2 + 13 + 1 + 4 + index_len * (1 + 8));
1044 data = GST_BUFFER_DATA (tmp);
1045 data[0] = 0; /* 13 bytes name */
1047 memcpy (&data[2], "filepositions", 13);
1048 data[15] = 10; /* array */
1049 GST_WRITE_UINT32_BE (&data[16], index_len);
1052 for (i = 0, l = mux->index; l; l = l->next, i++) {
1053 GstFlvMuxIndexEntry *entry = l->data;
1055 if (i % index_skip != 0)
1058 GST_WRITE_DOUBLE_BE (&data[1], entry->position);
1061 script_tag = gst_buffer_join (script_tag, tmp);
1063 tmp = gst_buffer_new_and_alloc (4);
1064 data = GST_BUFFER_DATA (tmp);
1065 GST_WRITE_UINT32_BE (data, GST_BUFFER_SIZE (script_tag));
1066 script_tag = gst_buffer_join (script_tag, tmp);
1068 data = GST_BUFFER_DATA (script_tag);
1069 data[1] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 16) & 0xff;
1070 data[2] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 8) & 0xff;
1071 data[3] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 0) & 0xff;
1073 gst_buffer_set_caps (script_tag, GST_PAD_CAPS (mux->srcpad));
1074 ret = gst_flv_mux_push (mux, script_tag);
1079 static GstFlowReturn
1080 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data)
1082 GstFlvMux *mux = GST_FLV_MUX (user_data);
1084 GstClockTime best_time;
1087 gboolean eos = TRUE;
1089 if (mux->state == GST_FLV_MUX_STATE_HEADER) {
1090 if (mux->collect->data == NULL) {
1091 GST_ELEMENT_ERROR (mux, STREAM, MUX, (NULL),
1092 ("No input streams configured"));
1093 return GST_FLOW_ERROR;
1096 if (gst_pad_push_event (mux->srcpad,
1097 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0)))
1098 ret = gst_flv_mux_write_header (mux);
1100 ret = GST_FLOW_ERROR;
1102 if (ret != GST_FLOW_OK)
1104 mux->state = GST_FLV_MUX_STATE_DATA;
1108 best_time = GST_CLOCK_TIME_NONE;
1109 for (sl = mux->collect->data; sl; sl = sl->next) {
1110 GstFlvPad *cpad = sl->data;
1111 GstBuffer *buffer = gst_collect_pads_peek (pads, (GstCollectData *) cpad);
1119 time = GST_BUFFER_TIMESTAMP (buffer);
1120 gst_buffer_unref (buffer);
1122 /* Use buffers without valid timestamp first */
1123 if (!GST_CLOCK_TIME_IS_VALID (time)) {
1124 GST_WARNING_OBJECT (pads, "Buffer without valid timestamp");
1126 best_time = cpad->last_timestamp;
1132 if (best == NULL || (GST_CLOCK_TIME_IS_VALID (best_time)
1133 && time < best_time)) {
1139 if (GST_CLOCK_TIME_IS_VALID (best_time)
1140 && best_time / GST_MSECOND > G_MAXUINT32) {
1141 GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
1146 return gst_flv_mux_write_buffer (mux, best);
1148 gst_flv_mux_write_index (mux);
1149 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
1150 return GST_FLOW_UNEXPECTED;
1156 static GstStateChangeReturn
1157 gst_flv_mux_change_state (GstElement * element, GstStateChange transition)
1159 GstStateChangeReturn ret;
1160 GstFlvMux *mux = GST_FLV_MUX (element);
1162 switch (transition) {
1163 case GST_STATE_CHANGE_NULL_TO_READY:
1165 case GST_STATE_CHANGE_READY_TO_PAUSED:
1166 gst_collect_pads_start (mux->collect);
1168 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1170 case GST_STATE_CHANGE_PAUSED_TO_READY:
1171 gst_collect_pads_stop (mux->collect);
1177 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1179 switch (transition) {
1180 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1182 case GST_STATE_CHANGE_PAUSED_TO_READY:
1183 gst_flv_mux_reset (GST_ELEMENT (mux));
1185 case GST_STATE_CHANGE_READY_TO_NULL: