3 * Copyright (c) 2008 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 * - Write metadata for the file, see FLV spec page 13
32 #include "gstflvmux.h"
34 GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
35 #define GST_CAT_DEFAULT flvmux_debug
37 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
40 GST_STATIC_CAPS ("video/x-flv")
43 static GstStaticPadTemplate videosink_templ = GST_STATIC_PAD_TEMPLATE ("video",
46 GST_STATIC_CAPS ("video/x-flash-video; "
47 "video/x-flash-screen; "
48 "video/x-vp6-flash; " "video/x-vp6-alpha; " "video/x-h264;")
51 static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
55 ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
56 "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }; "
57 "audio/mpeg, mpegversion = (int) 4; "
58 "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
59 "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; "
60 "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; "
61 "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
62 "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
65 GST_BOILERPLATE (GstFlvMux, gst_flv_mux, GstElement, GST_TYPE_ELEMENT);
67 static void gst_flv_mux_finalize (GObject * object);
69 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data);
71 static gboolean gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event);
72 static GstPad *gst_flv_mux_request_new_pad (GstElement * element,
73 GstPadTemplate * templ, const gchar * name);
74 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
76 static GstStateChangeReturn
77 gst_flv_mux_change_state (GstElement * element, GstStateChange transition);
79 static void gst_flv_mux_reset (GstElement * element);
82 gst_flv_mux_base_init (gpointer g_class)
84 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
86 gst_element_class_add_pad_template (element_class,
87 gst_static_pad_template_get (&videosink_templ));
88 gst_element_class_add_pad_template (element_class,
89 gst_static_pad_template_get (&audiosink_templ));
90 gst_element_class_add_pad_template (element_class,
91 gst_static_pad_template_get (&src_templ));
92 gst_element_class_set_details_simple (element_class, "FLV muxer",
94 "Muxes video/audio streams into a FLV stream",
95 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
97 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
101 gst_flv_mux_class_init (GstFlvMuxClass * klass)
103 GObjectClass *gobject_class;
104 GstElementClass *gstelement_class;
106 GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
111 gobject_class->finalize = gst_flv_mux_finalize;
113 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_flv_mux_change_state);
114 gstelement_class->request_new_pad =
115 GST_DEBUG_FUNCPTR (gst_flv_mux_request_new_pad);
116 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
120 gst_flv_mux_init (GstFlvMux * mux, GstFlvMuxClass * g_class)
122 mux->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
123 gst_pad_set_event_function (mux->srcpad, gst_flv_mux_handle_src_event);
124 gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
126 mux->collect = gst_collect_pads_new ();
127 gst_collect_pads_set_function (mux->collect,
128 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_flv_mux_collected), mux);
130 gst_flv_mux_reset (GST_ELEMENT (mux));
134 gst_flv_mux_finalize (GObject * object)
136 GstFlvMux *mux = GST_FLV_MUX (object);
138 gst_object_unref (mux->collect);
140 G_OBJECT_CLASS (parent_class)->finalize (object);
144 gst_flv_mux_reset (GstElement * element)
146 GstFlvMux *mux = GST_FLV_MUX (element);
149 while ((sl = mux->collect->data) != NULL) {
150 GstFlvPad *cpad = (GstFlvPad *) sl->data;
152 if (cpad->audio_codec_data)
153 gst_buffer_unref (cpad->audio_codec_data);
154 if (cpad->video_codec_data)
155 gst_buffer_unref (cpad->video_codec_data);
157 gst_collect_pads_remove_pad (mux->collect, cpad->collect.pad);
160 mux->state = GST_FLV_MUX_STATE_HEADER;
164 gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event)
168 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
172 /* disable seeking for now */
178 return gst_pad_event_default (pad, event);
182 gst_flv_mux_handle_sink_event (GstPad * pad, GstEvent * event)
184 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
187 switch (GST_EVENT_TYPE (event)) {
189 /* TODO do something with the tags */
191 case GST_EVENT_NEWSEGMENT:
192 /* We don't support NEWSEGMENT events */
194 gst_event_unref (event);
200 /* now GstCollectPads can take care of the rest, e.g. EOS */
202 ret = mux->collect_event (pad, event);
203 gst_object_unref (mux);
209 gst_flv_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps)
211 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
212 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
216 s = gst_caps_get_structure (caps, 0);
218 if (strcmp (gst_structure_get_name (s), "video/x-flash-video") == 0) {
219 cpad->video_codec = 2;
220 } else if (strcmp (gst_structure_get_name (s), "video/x-flash-screen") == 0) {
221 cpad->video_codec = 3;
222 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-flash") == 0) {
223 cpad->video_codec = 4;
224 } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-alpha") == 0) {
225 cpad->video_codec = 5;
226 } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
227 cpad->video_codec = 7;
232 if (ret && gst_structure_has_field (s, "codec_data")) {
233 const GValue *val = gst_structure_get_value (s, "codec_data");
236 cpad->video_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
237 cpad->sent_codec_data = FALSE;
239 cpad->sent_codec_data = TRUE;
242 cpad->sent_codec_data = TRUE;
245 gst_object_unref (mux);
251 gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
253 GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
254 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
258 s = gst_caps_get_structure (caps, 0);
260 if (strcmp (gst_structure_get_name (s), "audio/x-adpcm") == 0) {
261 const gchar *layout = gst_structure_get_string (s, "layout");
262 if (layout && strcmp (layout, "swf") == 0) {
263 cpad->audio_codec = 1;
267 } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
270 if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
271 if (mpegversion == 1) {
274 if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
277 if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
278 cpad->audio_codec = 14;
280 cpad->audio_codec = 2;
284 } else if (mpegversion == 4) {
285 cpad->audio_codec = 10;
292 } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
295 if (gst_structure_get_int (s, "rate", &rate)
296 && gst_structure_get_int (s, "channels", &channels)) {
297 if (channels == 1 && rate == 16000)
298 cpad->audio_codec = 4;
299 else if (channels == 1 && rate == 8000)
300 cpad->audio_codec = 5;
302 cpad->audio_codec = 6;
304 } else if (strcmp (gst_structure_get_name (s), "audio/x-raw-int") == 0) {
307 if (gst_structure_get_int (s, "endianness", &endianness)
308 && endianness == G_LITTLE_ENDIAN)
309 cpad->audio_codec = 3;
312 } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
313 cpad->video_codec = 7;
314 } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
315 cpad->video_codec = 8;
321 gint rate, channels, width;
323 if (gst_structure_get_int (s, "rate", &rate)) {
324 if (cpad->audio_codec == 10)
326 else if (rate == 5512)
328 else if (rate == 11025)
330 else if (rate == 22050)
332 else if (rate == 44100)
334 else if (rate == 8000 && (cpad->audio_codec == 5
335 || cpad->audio_codec == 14))
337 else if (rate == 16000 && cpad->audio_codec == 4)
341 } else if (cpad->audio_codec == 10) {
347 if (gst_structure_get_int (s, "channels", &channels)) {
348 if (cpad->audio_codec == 4 || cpad->audio_codec == 5
349 || cpad->audio_codec == 6)
351 else if (cpad->audio_codec == 10)
353 else if (channels == 1)
355 else if (channels == 2)
359 } else if (cpad->audio_codec == 4 || cpad->audio_codec == 5
360 || cpad->audio_codec == 6) {
362 } else if (cpad->audio_codec == 10) {
368 if (gst_structure_get_int (s, "width", &width)) {
369 if (cpad->audio_codec != 3)
373 else if (width == 16)
377 } else if (cpad->audio_codec != 3) {
384 if (ret && gst_structure_has_field (s, "codec_data")) {
385 const GValue *val = gst_structure_get_value (s, "codec_data");
388 cpad->audio_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
389 cpad->sent_codec_data = FALSE;
391 cpad->sent_codec_data = TRUE;
394 cpad->sent_codec_data = TRUE;
397 gst_object_unref (mux);
403 gst_flv_mux_request_new_pad (GstElement * element,
404 GstPadTemplate * templ, const gchar * pad_name)
406 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
407 GstFlvMux *mux = GST_FLV_MUX (element);
410 const gchar *name = NULL;
411 GstPadSetCapsFunction setcapsfunc = NULL;
414 if (mux->state != GST_FLV_MUX_STATE_HEADER) {
415 GST_WARNING_OBJECT (mux, "Can't request pads after writing header");
419 if (templ == gst_element_class_get_pad_template (klass, "audio")) {
420 if (mux->have_audio) {
421 GST_WARNING_OBJECT (mux, "Already have an audio pad");
424 mux->have_audio = TRUE;
427 setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_audio_pad_setcaps);
428 } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
429 if (mux->have_video) {
430 GST_WARNING_OBJECT (mux, "Already have a video pad");
433 mux->have_video = TRUE;
436 setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_video_pad_setcaps);
438 GST_WARNING_OBJECT (mux, "Invalid template");
442 pad = gst_pad_new_from_template (templ, name);
444 gst_collect_pads_add_pad (mux->collect, pad, sizeof (GstFlvPad));
448 cpad->audio_codec = G_MAXUINT;
449 cpad->rate = G_MAXUINT;
450 cpad->width = G_MAXUINT;
451 cpad->channels = G_MAXUINT;
452 cpad->audio_codec_data = NULL;
454 cpad->video_codec = G_MAXUINT;
455 cpad->video_codec_data = NULL;
457 cpad->sent_codec_data = FALSE;
459 /* FIXME: hacked way to override/extend the event function of
460 * GstCollectPads; because it sets its own event function giving the
461 * element no access to events.
463 mux->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (pad);
464 gst_pad_set_event_function (pad,
465 GST_DEBUG_FUNCPTR (gst_flv_mux_handle_sink_event));
467 gst_pad_set_setcaps_function (pad, setcapsfunc);
468 gst_pad_set_active (pad, TRUE);
469 gst_element_add_pad (element, pad);
475 gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
477 GstFlvMux *mux = GST_FLV_MUX (GST_PAD_PARENT (pad));
478 GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
480 if (cpad && cpad->audio_codec_data)
481 gst_buffer_unref (cpad->audio_codec_data);
482 if (cpad && cpad->video_codec_data)
483 gst_buffer_unref (cpad->video_codec_data);
485 gst_collect_pads_remove_pad (mux->collect, pad);
486 gst_element_remove_pad (element, pad);
490 gst_flv_mux_write_header (GstFlvMux * mux)
492 GstBuffer *header = gst_buffer_new_and_alloc (9 + 4);
493 guint8 *data = GST_BUFFER_DATA (header);
495 if (GST_PAD_CAPS (mux->srcpad) == NULL) {
496 GstCaps *caps = gst_caps_new_simple ("video/x-flv", NULL);
498 gst_pad_set_caps (mux->srcpad, caps);
499 gst_caps_unref (caps);
501 gst_buffer_set_caps (header, GST_PAD_CAPS (mux->srcpad));
506 data[3] = 0x01; /* Version */
508 data[4] = (mux->have_audio << 2) | mux->have_video; /* flags */
509 GST_WRITE_UINT32_BE (data + 5, 9); /* data offset */
511 GST_WRITE_UINT32_BE (data + 9, 0); /* previous tag size */
513 return gst_pad_push (mux->srcpad, header);
517 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad)
523 gst_collect_pads_pop (mux->collect, (GstCollectData *) cpad);
524 guint32 timestamp = GST_BUFFER_TIMESTAMP (buffer) / GST_MSECOND;
525 gboolean second_run = FALSE;
531 if (cpad->video_codec == 7 && !cpad->sent_codec_data)
532 size += 4 + GST_BUFFER_SIZE (cpad->video_codec_data);
533 else if (cpad->video_codec == 7)
534 size += 4 + GST_BUFFER_SIZE (buffer);
536 size += GST_BUFFER_SIZE (buffer);
539 if (cpad->video_codec == 10 && !cpad->sent_codec_data)
540 size += 1 + GST_BUFFER_SIZE (cpad->audio_codec_data);
541 else if (cpad->audio_codec == 10)
542 size += 1 + GST_BUFFER_SIZE (buffer);
544 size += GST_BUFFER_SIZE (buffer);
548 tag = gst_buffer_new_and_alloc (size);
549 data = GST_BUFFER_DATA (tag);
550 memset (data, 0, size);
552 data[0] = (cpad->video) ? 9 : 8;
554 data[1] = ((size - 11 - 4) >> 16) & 0xff;
555 data[2] = ((size - 11 - 4) >> 8) & 0xff;
556 data[3] = ((size - 11 - 4) >> 0) & 0xff;
558 data[4] = (timestamp >> 16) & 0xff;
559 data[5] = (timestamp >> 8) & 0xff;
560 data[6] = (timestamp >> 0) & 0xff;
561 data[7] = (timestamp >> 24) & 0xff;
563 data[8] = data[9] = data[10] = 0;
566 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
571 data[11] |= cpad->video_codec & 0x0f;
573 if (cpad->video_codec == 7 && !cpad->sent_codec_data) {
575 data[13] = data[14] = data[15] = 0;
577 memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (cpad->video_codec_data),
578 GST_BUFFER_SIZE (cpad->video_codec_data));
580 } else if (cpad->video_codec == 7) {
583 /* FIXME: what to do about composition time */
584 data[13] = data[14] = data[15] = 0;
586 memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (buffer),
587 GST_BUFFER_SIZE (buffer));
589 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
590 GST_BUFFER_SIZE (buffer));
593 data[11] |= (cpad->audio_codec << 4) & 0xf0;
594 data[11] |= (cpad->rate << 2) & 0x0c;
595 data[11] |= (cpad->width << 1) & 0x02;
596 data[11] |= (cpad->channels << 0) & 0x01;
598 if (cpad->video_codec == 10 && !cpad->sent_codec_data) {
601 memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (cpad->audio_codec_data),
602 GST_BUFFER_SIZE (cpad->audio_codec_data));
604 } else if (cpad->audio_codec == 10) {
607 memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (buffer),
608 GST_BUFFER_SIZE (buffer));
610 memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
611 GST_BUFFER_SIZE (buffer));
615 GST_WRITE_UINT32_BE (data + size - 4, size - 4);
617 gst_buffer_set_caps (tag, GST_PAD_CAPS (mux->srcpad));
623 cpad->sent_codec_data = TRUE;
625 ret = gst_pad_push (mux->srcpad, tag);
626 if (ret != GST_FLOW_OK) {
627 gst_buffer_unref (buffer);
635 gst_buffer_copy_metadata (tag, buffer, GST_BUFFER_COPY_TIMESTAMPS);
636 GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
637 GST_BUFFER_OFFSET_NONE;
639 gst_buffer_unref (buffer);
641 return gst_pad_push (mux->srcpad, tag);
645 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data)
647 GstFlvMux *mux = GST_FLV_MUX (user_data);
649 GstClockTime best_time;
654 if (mux->state == GST_FLV_MUX_STATE_HEADER) {
655 if (mux->collect->data == NULL) {
656 GST_ELEMENT_ERROR (mux, STREAM, MUX, (NULL),
657 ("No input streams configured"));
658 return GST_FLOW_ERROR;
661 if (gst_pad_push_event (mux->srcpad,
662 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0)))
663 ret = gst_flv_mux_write_header (mux);
665 ret = GST_FLOW_ERROR;
667 if (ret != GST_FLOW_OK)
669 mux->state = GST_FLV_MUX_STATE_DATA;
673 best_time = GST_CLOCK_TIME_NONE;
674 for (sl = mux->collect->data; sl; sl = sl->next) {
675 GstFlvPad *cpad = sl->data;
676 GstBuffer *buffer = gst_collect_pads_peek (pads, (GstCollectData *) cpad);
684 time = GST_BUFFER_TIMESTAMP (buffer);
685 gst_buffer_unref (buffer);
687 if (!GST_CLOCK_TIME_IS_VALID (time)) {
688 GST_WARNING_OBJECT (pads, "Buffer without valid timestamp, dropping");
690 gst_buffer_unref (gst_collect_pads_pop (pads, (GstCollectData *) cpad));
695 if (best == NULL || (GST_CLOCK_TIME_IS_VALID (best_time)
696 && time < best_time)) {
702 if (GST_CLOCK_TIME_IS_VALID (best_time)
703 && best_time / GST_MSECOND > G_MAXUINT32) {
704 GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
709 return gst_flv_mux_write_buffer (mux, best);
711 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
712 return GST_FLOW_UNEXPECTED;
718 static GstStateChangeReturn
719 gst_flv_mux_change_state (GstElement * element, GstStateChange transition)
721 GstStateChangeReturn ret;
722 GstFlvMux *mux = GST_FLV_MUX (element);
724 switch (transition) {
725 case GST_STATE_CHANGE_NULL_TO_READY:
727 case GST_STATE_CHANGE_READY_TO_PAUSED:
728 gst_collect_pads_start (mux->collect);
730 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
732 case GST_STATE_CHANGE_PAUSED_TO_READY:
733 gst_collect_pads_stop (mux->collect);
739 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
741 switch (transition) {
742 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
744 case GST_STATE_CHANGE_PAUSED_TO_READY:
745 gst_flv_mux_reset (GST_ELEMENT (mux));
747 case GST_STATE_CHANGE_READY_TO_NULL: