[MOVED FROM BAD 31/57] gst/flv/gstflvmux.c: Don't set video_codec to the value that...
[platform/upstream/gstreamer.git] / gst / flv / gstflvmux.c
1 /* GStreamer
2  *
3  * Copyright (c) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /* TODO:
22  *   - Write metadata for the file, see FLV spec page 13
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <math.h>
30 #include <string.h>
31
32 #include "gstflvmux.h"
33
34 GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
35 #define GST_CAT_DEFAULT flvmux_debug
36
37 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
38     GST_PAD_SRC,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("video/x-flv")
41     );
42
43 static GstStaticPadTemplate videosink_templ = GST_STATIC_PAD_TEMPLATE ("video",
44     GST_PAD_SINK,
45     GST_PAD_REQUEST,
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;")
49     );
50
51 static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
52     GST_PAD_SINK,
53     GST_PAD_REQUEST,
54     GST_STATIC_CAPS
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 };")
63     );
64
65 GST_BOILERPLATE (GstFlvMux, gst_flv_mux, GstElement, GST_TYPE_ELEMENT);
66
67 static void gst_flv_mux_finalize (GObject * object);
68 static GstFlowReturn
69 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data);
70
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);
75
76 static GstStateChangeReturn
77 gst_flv_mux_change_state (GstElement * element, GstStateChange transition);
78
79 static void gst_flv_mux_reset (GstElement * element);
80
81 static void
82 gst_flv_mux_base_init (gpointer g_class)
83 {
84   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
85
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",
93       "Codec/Muxer",
94       "Muxes video/audio streams into a FLV stream",
95       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
96
97   GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
98 }
99
100 static void
101 gst_flv_mux_class_init (GstFlvMuxClass * klass)
102 {
103   GObjectClass *gobject_class;
104   GstElementClass *gstelement_class;
105
106   GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
107
108   gobject_class = (GObjectClass *) klass;
109   gstelement_class = (GstElementClass *) klass;
110
111   gobject_class->finalize = gst_flv_mux_finalize;
112
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);
117 }
118
119 static void
120 gst_flv_mux_init (GstFlvMux * mux, GstFlvMuxClass * g_class)
121 {
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);
125
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);
129
130   gst_flv_mux_reset (GST_ELEMENT (mux));
131 }
132
133 static void
134 gst_flv_mux_finalize (GObject * object)
135 {
136   GstFlvMux *mux = GST_FLV_MUX (object);
137
138   gst_object_unref (mux->collect);
139
140   G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 static void
144 gst_flv_mux_reset (GstElement * element)
145 {
146   GstFlvMux *mux = GST_FLV_MUX (element);
147   GSList *sl;
148
149   while ((sl = mux->collect->data) != NULL) {
150     GstFlvPad *cpad = (GstFlvPad *) sl->data;
151
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);
156
157     gst_collect_pads_remove_pad (mux->collect, cpad->collect.pad);
158   }
159
160   mux->state = GST_FLV_MUX_STATE_HEADER;
161 }
162
163 static gboolean
164 gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event)
165 {
166   GstEventType type;
167
168   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
169
170   switch (type) {
171     case GST_EVENT_SEEK:
172       /* disable seeking for now */
173       return FALSE;
174     default:
175       break;
176   }
177
178   return gst_pad_event_default (pad, event);
179 }
180
181 static gboolean
182 gst_flv_mux_handle_sink_event (GstPad * pad, GstEvent * event)
183 {
184   GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
185   gboolean ret = TRUE;
186
187   switch (GST_EVENT_TYPE (event)) {
188     case GST_EVENT_TAG:
189       /* TODO do something with the tags */
190       break;
191     case GST_EVENT_NEWSEGMENT:
192       /* We don't support NEWSEGMENT events */
193       ret = FALSE;
194       gst_event_unref (event);
195       break;
196     default:
197       break;
198   }
199
200   /* now GstCollectPads can take care of the rest, e.g. EOS */
201   if (ret)
202     ret = mux->collect_event (pad, event);
203   gst_object_unref (mux);
204
205   return ret;
206 }
207
208 static gboolean
209 gst_flv_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps)
210 {
211   GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
212   GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
213   gboolean ret = TRUE;
214   GstStructure *s;
215
216   s = gst_caps_get_structure (caps, 0);
217
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;
228   } else {
229     ret = FALSE;
230   }
231
232   if (ret && gst_structure_has_field (s, "codec_data")) {
233     const GValue *val = gst_structure_get_value (s, "codec_data");
234
235     if (val) {
236       cpad->video_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
237       cpad->sent_codec_data = FALSE;
238     } else {
239       cpad->sent_codec_data = TRUE;
240     }
241   } else {
242     cpad->sent_codec_data = TRUE;
243   }
244
245   gst_object_unref (mux);
246
247   return ret;
248 }
249
250 static gboolean
251 gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
252 {
253   GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
254   GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
255   gboolean ret = TRUE;
256   GstStructure *s;
257
258   s = gst_caps_get_structure (caps, 0);
259
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;
264     } else {
265       ret = FALSE;
266     }
267   } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
268     gint mpegversion;
269
270     if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
271       if (mpegversion == 1) {
272         gint layer;
273
274         if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
275           gint rate;
276
277           if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
278             cpad->audio_codec = 14;
279           else
280             cpad->audio_codec = 2;
281         } else {
282           ret = FALSE;
283         }
284       } else if (mpegversion == 4) {
285         cpad->audio_codec = 10;
286       } else {
287         ret = FALSE;
288       }
289     } else {
290       ret = FALSE;
291     }
292   } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
293     gint rate, channels;
294
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;
301     } else {
302       cpad->audio_codec = 6;
303     }
304   } else if (strcmp (gst_structure_get_name (s), "audio/x-raw-int") == 0) {
305     gint endianness;
306
307     if (gst_structure_get_int (s, "endianness", &endianness)
308         && endianness == G_LITTLE_ENDIAN)
309       cpad->audio_codec = 3;
310     else
311       ret = FALSE;
312   } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
313     cpad->audio_codec = 7;
314   } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
315     cpad->audio_codec = 8;
316   } else {
317     ret = FALSE;
318   }
319
320   if (ret) {
321     gint rate, channels, width;
322
323     if (gst_structure_get_int (s, "rate", &rate)) {
324       if (cpad->audio_codec == 10)
325         cpad->rate = 3;
326       else if (rate == 5512)
327         cpad->rate = 0;
328       else if (rate == 11025)
329         cpad->rate = 1;
330       else if (rate == 22050)
331         cpad->rate = 2;
332       else if (rate == 44100)
333         cpad->rate = 3;
334       else if (rate == 8000 && (cpad->audio_codec == 5
335               || cpad->audio_codec == 14))
336         cpad->rate = 0;
337       else if (rate == 16000 && cpad->audio_codec == 4)
338         cpad->rate = 0;
339       else
340         ret = FALSE;
341     } else if (cpad->audio_codec == 10) {
342       cpad->rate = 3;
343     } else {
344       ret = FALSE;
345     }
346
347     if (gst_structure_get_int (s, "channels", &channels)) {
348       if (cpad->audio_codec == 4 || cpad->audio_codec == 5
349           || cpad->audio_codec == 6)
350         cpad->channels = 0;
351       else if (cpad->audio_codec == 10)
352         cpad->channels = 1;
353       else if (channels == 1)
354         cpad->channels = 0;
355       else if (channels == 2)
356         cpad->channels = 1;
357       else
358         ret = FALSE;
359     } else if (cpad->audio_codec == 4 || cpad->audio_codec == 5
360         || cpad->audio_codec == 6) {
361       cpad->channels = 0;
362     } else if (cpad->audio_codec == 10) {
363       cpad->channels = 1;
364     } else {
365       ret = FALSE;
366     }
367
368     if (gst_structure_get_int (s, "width", &width)) {
369       if (cpad->audio_codec != 3)
370         cpad->width = 1;
371       else if (width == 8)
372         cpad->width = 0;
373       else if (width == 16)
374         cpad->width = 1;
375       else
376         ret = FALSE;
377     } else if (cpad->audio_codec != 3) {
378       cpad->width = 1;
379     } else {
380       ret = FALSE;
381     }
382   }
383
384   if (ret && gst_structure_has_field (s, "codec_data")) {
385     const GValue *val = gst_structure_get_value (s, "codec_data");
386
387     if (val) {
388       cpad->audio_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
389       cpad->sent_codec_data = FALSE;
390     } else {
391       cpad->sent_codec_data = TRUE;
392     }
393   } else {
394     cpad->sent_codec_data = TRUE;
395   }
396
397   gst_object_unref (mux);
398
399   return ret;
400 }
401
402 static GstPad *
403 gst_flv_mux_request_new_pad (GstElement * element,
404     GstPadTemplate * templ, const gchar * pad_name)
405 {
406   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
407   GstFlvMux *mux = GST_FLV_MUX (element);
408   GstFlvPad *cpad;
409   GstPad *pad = NULL;
410   const gchar *name = NULL;
411   GstPadSetCapsFunction setcapsfunc = NULL;
412   gboolean video;
413
414   if (mux->state != GST_FLV_MUX_STATE_HEADER) {
415     GST_WARNING_OBJECT (mux, "Can't request pads after writing header");
416     return NULL;
417   }
418
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");
422       return NULL;
423     }
424     mux->have_audio = TRUE;
425     name = "audio";
426     video = FALSE;
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");
431       return NULL;
432     }
433     mux->have_video = TRUE;
434     name = "video";
435     video = TRUE;
436     setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_video_pad_setcaps);
437   } else {
438     GST_WARNING_OBJECT (mux, "Invalid template");
439     return NULL;
440   }
441
442   pad = gst_pad_new_from_template (templ, name);
443   cpad = (GstFlvPad *)
444       gst_collect_pads_add_pad (mux->collect, pad, sizeof (GstFlvPad));
445
446   cpad->video = video;
447
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;
453
454   cpad->video_codec = G_MAXUINT;
455   cpad->video_codec_data = NULL;
456
457   cpad->sent_codec_data = FALSE;
458
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.
462    */
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));
466
467   gst_pad_set_setcaps_function (pad, setcapsfunc);
468   gst_pad_set_active (pad, TRUE);
469   gst_element_add_pad (element, pad);
470
471   return pad;
472 }
473
474 static void
475 gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
476 {
477   GstFlvMux *mux = GST_FLV_MUX (GST_PAD_PARENT (pad));
478   GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
479
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);
484
485   gst_collect_pads_remove_pad (mux->collect, pad);
486   gst_element_remove_pad (element, pad);
487 }
488
489 static GstFlowReturn
490 gst_flv_mux_write_header (GstFlvMux * mux)
491 {
492   GstBuffer *header = gst_buffer_new_and_alloc (9 + 4);
493   guint8 *data = GST_BUFFER_DATA (header);
494
495   if (GST_PAD_CAPS (mux->srcpad) == NULL) {
496     GstCaps *caps = gst_caps_new_simple ("video/x-flv", NULL);
497
498     gst_pad_set_caps (mux->srcpad, caps);
499     gst_caps_unref (caps);
500   }
501   gst_buffer_set_caps (header, GST_PAD_CAPS (mux->srcpad));
502
503   data[0] = 'F';
504   data[1] = 'L';
505   data[2] = 'V';
506   data[3] = 0x01;               /* Version */
507
508   data[4] = (mux->have_audio << 2) | mux->have_video;   /* flags */
509   GST_WRITE_UINT32_BE (data + 5, 9);    /* data offset */
510
511   GST_WRITE_UINT32_BE (data + 9, 0);    /* previous tag size */
512
513   return gst_pad_push (mux->srcpad, header);
514 }
515
516 static GstFlowReturn
517 gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad)
518 {
519   GstBuffer *tag;
520   guint8 *data;
521   guint size;
522   GstBuffer *buffer =
523       gst_collect_pads_pop (mux->collect, (GstCollectData *) cpad);
524   guint32 timestamp = GST_BUFFER_TIMESTAMP (buffer) / GST_MSECOND;
525   gboolean second_run = FALSE;
526
527 next:
528   size = 11;
529   if (cpad->video) {
530     size += 1;
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);
535     else
536       size += GST_BUFFER_SIZE (buffer);
537   } else {
538     size += 1;
539     if (cpad->audio_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);
543     else
544       size += GST_BUFFER_SIZE (buffer);
545   }
546   size += 4;
547
548   tag = gst_buffer_new_and_alloc (size);
549   data = GST_BUFFER_DATA (tag);
550   memset (data, 0, size);
551
552   data[0] = (cpad->video) ? 9 : 8;
553
554   data[1] = ((size - 11 - 4) >> 16) & 0xff;
555   data[2] = ((size - 11 - 4) >> 8) & 0xff;
556   data[3] = ((size - 11 - 4) >> 0) & 0xff;
557
558   data[4] = (timestamp >> 16) & 0xff;
559   data[5] = (timestamp >> 8) & 0xff;
560   data[6] = (timestamp >> 0) & 0xff;
561   data[7] = (timestamp >> 24) & 0xff;
562
563   data[8] = data[9] = data[10] = 0;
564
565   if (cpad->video) {
566     if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
567       data[11] |= 2 << 4;
568     else
569       data[11] |= 1 << 4;
570
571     data[11] |= cpad->video_codec & 0x0f;
572
573     if (cpad->video_codec == 7 && !cpad->sent_codec_data) {
574       data[12] = 0;
575       data[13] = data[14] = data[15] = 0;
576
577       memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (cpad->video_codec_data),
578           GST_BUFFER_SIZE (cpad->video_codec_data));
579       second_run = TRUE;
580     } else if (cpad->video_codec == 7) {
581       data[12] = 1;
582
583       /* FIXME: what to do about composition time */
584       data[13] = data[14] = data[15] = 0;
585
586       memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (buffer),
587           GST_BUFFER_SIZE (buffer));
588     } else {
589       memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
590           GST_BUFFER_SIZE (buffer));
591     }
592   } else {
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;
597
598     if (cpad->audio_codec == 10 && !cpad->sent_codec_data) {
599       data[12] = 0;
600
601       memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (cpad->audio_codec_data),
602           GST_BUFFER_SIZE (cpad->audio_codec_data));
603       second_run = TRUE;
604     } else if (cpad->audio_codec == 10) {
605       data[12] = 1;
606
607       memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (buffer),
608           GST_BUFFER_SIZE (buffer));
609     } else {
610       memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
611           GST_BUFFER_SIZE (buffer));
612     }
613   }
614
615   GST_WRITE_UINT32_BE (data + size - 4, size - 4);
616
617   gst_buffer_set_caps (tag, GST_PAD_CAPS (mux->srcpad));
618
619   if (second_run) {
620     GstFlowReturn ret;
621
622     second_run = FALSE;
623     cpad->sent_codec_data = TRUE;
624
625     ret = gst_pad_push (mux->srcpad, tag);
626     if (ret != GST_FLOW_OK) {
627       gst_buffer_unref (buffer);
628       return ret;
629     }
630
631     tag = NULL;
632     goto next;
633   }
634
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;
638
639   gst_buffer_unref (buffer);
640
641   return gst_pad_push (mux->srcpad, tag);
642 }
643
644 static GstFlowReturn
645 gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data)
646 {
647   GstFlvMux *mux = GST_FLV_MUX (user_data);
648   GstFlvPad *best;
649   GstClockTime best_time;
650   GstFlowReturn ret;
651   GSList *sl;
652   gboolean eos = TRUE;
653
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;
659     }
660
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);
664     else
665       ret = GST_FLOW_ERROR;
666
667     if (ret != GST_FLOW_OK)
668       return ret;
669     mux->state = GST_FLV_MUX_STATE_DATA;
670   }
671
672   best = NULL;
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);
677     GstClockTime time;
678
679     if (!buffer)
680       continue;
681
682     eos = FALSE;
683
684     time = GST_BUFFER_TIMESTAMP (buffer);
685     gst_buffer_unref (buffer);
686
687     if (!GST_CLOCK_TIME_IS_VALID (time)) {
688       GST_WARNING_OBJECT (pads, "Buffer without valid timestamp, dropping");
689
690       gst_buffer_unref (gst_collect_pads_pop (pads, (GstCollectData *) cpad));
691       continue;
692     }
693
694
695     if (best == NULL || (GST_CLOCK_TIME_IS_VALID (best_time)
696             && time < best_time)) {
697       best = cpad;
698       best_time = time;
699     }
700   }
701
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");
705     eos = TRUE;
706   }
707
708   if (!eos && best) {
709     return gst_flv_mux_write_buffer (mux, best);
710   } else if (eos) {
711     gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
712     return GST_FLOW_UNEXPECTED;
713   } else {
714     return GST_FLOW_OK;
715   }
716 }
717
718 static GstStateChangeReturn
719 gst_flv_mux_change_state (GstElement * element, GstStateChange transition)
720 {
721   GstStateChangeReturn ret;
722   GstFlvMux *mux = GST_FLV_MUX (element);
723
724   switch (transition) {
725     case GST_STATE_CHANGE_NULL_TO_READY:
726       break;
727     case GST_STATE_CHANGE_READY_TO_PAUSED:
728       gst_collect_pads_start (mux->collect);
729       break;
730     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
731       break;
732     case GST_STATE_CHANGE_PAUSED_TO_READY:
733       gst_collect_pads_stop (mux->collect);
734       break;
735     default:
736       break;
737   }
738
739   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
740
741   switch (transition) {
742     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
743       break;
744     case GST_STATE_CHANGE_PAUSED_TO_READY:
745       gst_flv_mux_reset (GST_ELEMENT (mux));
746       break;
747     case GST_STATE_CHANGE_READY_TO_NULL:
748       break;
749     default:
750       break;
751   }
752
753   return ret;
754 }