2 * Copyright (C) <2007> Julien Moutte <julien@moutte.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-flvdemux
23 * flvdemux demuxes an FLV file into the different contained streams.
26 * <title>Example launch line</title>
28 * gst-launch -v filesrc location=/path/to/flv ! flvdemux ! audioconvert ! autoaudiosink
29 * ]| This pipeline demuxes an FLV file and outputs the contained raw audio streams.
37 #include "gstflvdemux.h"
38 #include "gstflvmux.h"
41 #include <gst/base/gstbytereader.h>
42 #include <gst/pbutils/descriptions.h>
43 #include <gst/pbutils/pbutils.h>
45 static GstStaticPadTemplate flv_sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("video/x-flv")
51 static GstStaticPadTemplate audio_src_template =
52 GST_STATIC_PAD_TEMPLATE ("audio",
56 ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
57 "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }, parsed = (boolean) TRUE; "
58 "audio/mpeg, mpegversion = (int) 4, framed = (boolean) TRUE; "
59 "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
60 "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; "
61 "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; "
62 "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
63 "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
64 "audio/x-speex, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
67 static GstStaticPadTemplate video_src_template =
68 GST_STATIC_PAD_TEMPLATE ("video",
71 GST_STATIC_CAPS ("video/x-flash-video; "
72 "video/x-flash-screen; "
73 "video/x-vp6-flash; " "video/x-vp6-alpha; "
74 "video/x-h264, stream-format=avc;")
77 GST_DEBUG_CATEGORY_STATIC (flvdemux_debug);
78 #define GST_CAT_DEFAULT flvdemux_debug
80 #define gst_flv_demux_parent_class parent_class
81 G_DEFINE_TYPE (GstFlvDemux, gst_flv_demux, GST_TYPE_ELEMENT);
83 /* 9 bytes of header + 4 bytes of first previous tag size */
84 #define FLV_HEADER_SIZE 13
85 /* 1 byte of tag type + 3 bytes of tag data size */
86 #define FLV_TAG_TYPE_SIZE 4
88 static gboolean flv_demux_handle_seek_push (GstFlvDemux * demux,
90 static gboolean gst_flv_demux_handle_seek_pull (GstFlvDemux * demux,
91 GstEvent * event, gboolean seeking);
93 static gboolean gst_flv_demux_query (GstPad * pad, GstQuery * query);
94 static gboolean gst_flv_demux_src_event (GstPad * pad, GstEvent * event);
98 gst_flv_demux_parse_and_add_index_entry (GstFlvDemux * demux, GstClockTime ts,
99 guint64 pos, gboolean keyframe)
101 static GstIndexAssociation associations[2];
102 static GstIndexEntry *entry;
104 GST_LOG_OBJECT (demux,
105 "adding key=%d association %" GST_TIME_FORMAT "-> %" G_GUINT64_FORMAT,
106 keyframe, GST_TIME_ARGS (ts), pos);
108 /* if upstream is not seekable there is no point in building an index */
109 if (!demux->upstream_seekable)
112 /* entry may already have been added before, avoid adding indefinitely */
113 entry = gst_index_get_assoc_entry (demux->index, demux->index_id,
114 GST_INDEX_LOOKUP_EXACT, GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, pos);
117 #ifndef GST_DISABLE_GST_DEBUG
121 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
122 key = ! !(GST_INDEX_ASSOC_FLAGS (entry) & GST_ASSOCIATION_FLAG_KEY_UNIT);
123 GST_LOG_OBJECT (demux, "position already mapped to time %" GST_TIME_FORMAT
124 ", keyframe %d", GST_TIME_ARGS (time), key);
125 /* there is not really a way to delete the existing one */
126 if (time != ts || key != ! !keyframe)
127 GST_DEBUG_OBJECT (demux, "metadata mismatch");
132 associations[0].format = GST_FORMAT_TIME;
133 associations[0].value = ts;
134 associations[1].format = GST_FORMAT_BYTES;
135 associations[1].value = pos;
137 gst_index_add_associationv (demux->index, demux->index_id,
138 (keyframe) ? GST_ASSOCIATION_FLAG_KEY_UNIT :
139 GST_ASSOCIATION_FLAG_DELTA_UNIT, 2,
140 (const GstIndexAssociation *) &associations);
142 if (pos > demux->index_max_pos)
143 demux->index_max_pos = pos;
144 if (ts > demux->index_max_time)
145 demux->index_max_time = ts;
149 FLV_GET_STRING (GstByteReader * reader)
151 guint16 string_size = 0;
152 gchar *string = NULL;
153 const guint8 *str = NULL;
155 g_return_val_if_fail (reader != NULL, NULL);
157 if (G_UNLIKELY (!gst_byte_reader_get_uint16_be (reader, &string_size)))
160 if (G_UNLIKELY (string_size > gst_byte_reader_get_remaining (reader)))
163 string = g_try_malloc0 (string_size + 1);
164 if (G_UNLIKELY (!string)) {
168 if (G_UNLIKELY (!gst_byte_reader_get_data (reader, string_size, &str))) {
173 memcpy (string, str, string_size);
174 if (!g_utf8_validate (string, string_size, NULL)) {
183 gst_flv_demux_check_seekability (GstFlvDemux * demux)
186 gint64 start = -1, stop = -1;
188 demux->upstream_seekable = FALSE;
190 query = gst_query_new_seeking (GST_FORMAT_BYTES);
191 if (!gst_pad_peer_query (demux->sinkpad, query)) {
192 GST_DEBUG_OBJECT (demux, "seeking query failed");
193 gst_query_unref (query);
197 gst_query_parse_seeking (query, NULL, &demux->upstream_seekable,
200 gst_query_unref (query);
202 /* try harder to query upstream size if we didn't get it the first time */
203 if (demux->upstream_seekable && stop == -1) {
204 GST_DEBUG_OBJECT (demux, "doing duration query to fix up unset stop");
205 gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES, &stop);
208 /* if upstream doesn't know the size, it's likely that it's not seekable in
209 * practice even if it technically may be seekable */
210 if (demux->upstream_seekable && (start != 0 || stop <= start)) {
211 GST_DEBUG_OBJECT (demux, "seekable but unknown start/stop -> disable");
212 demux->upstream_seekable = FALSE;
215 GST_DEBUG_OBJECT (demux, "upstream seekable: %d", demux->upstream_seekable);
219 parse_flv_demux_parse_date_string (GDate * date, const gchar * s)
221 g_date_set_parse (date, s);
222 if (g_date_valid (date))
225 /* "Fri Oct 15 15:13:16 2004" needs to be parsed */
227 static const gchar *months[] = {
228 "Jan", "Feb", "Mar", "Apr",
229 "May", "Jun", "Jul", "Aug",
230 "Sep", "Oct", "Nov", "Dec"
232 gchar **tokens = g_strsplit (s, " ", -1);
237 if (g_strv_length (tokens) != 5)
240 if (strlen (tokens[1]) != 3)
242 for (i = 0; i < 12; i++) {
243 if (!strcmp (tokens[1], months[i])) {
249 g_date_set_month (date, i + 1);
251 d = g_ascii_strtoull (tokens[2], &endptr, 10);
252 if (d == 0 && *endptr != '\0')
255 g_date_set_day (date, d);
257 d = g_ascii_strtoull (tokens[4], &endptr, 10);
258 if (d == 0 && *endptr != '\0')
261 g_date_set_year (date, d);
270 gst_flv_demux_parse_metadata_item (GstFlvDemux * demux, GstByteReader * reader,
271 gboolean * end_marker)
273 gchar *tag_name = NULL;
276 /* Initialize the end_marker flag to FALSE */
279 /* Name of the tag */
280 tag_name = FLV_GET_STRING (reader);
281 if (G_UNLIKELY (!tag_name)) {
282 GST_WARNING_OBJECT (demux, "failed reading tag name");
286 /* What kind of object is that */
287 if (!gst_byte_reader_get_uint8 (reader, &tag_type))
290 GST_DEBUG_OBJECT (demux, "tag name %s, tag type %d", tag_name, tag_type);
294 { /* Use a union to read the uint64 and then as a double */
297 if (!gst_byte_reader_get_float64_be (reader, &d))
300 GST_DEBUG_OBJECT (demux, "%s => (double) %f", tag_name, d);
302 if (!strcmp (tag_name, "duration")) {
303 demux->duration = d * GST_SECOND;
305 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
306 GST_TAG_DURATION, demux->duration, NULL);
307 } else if (!strcmp (tag_name, "AspectRatioX")) {
309 demux->got_par = TRUE;
310 } else if (!strcmp (tag_name, "AspectRatioY")) {
312 demux->got_par = TRUE;
313 } else if (!strcmp (tag_name, "width")) {
315 } else if (!strcmp (tag_name, "height")) {
317 } else if (!strcmp (tag_name, "framerate")) {
318 demux->framerate = d;
320 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
329 if (!gst_byte_reader_get_uint8 (reader, &b))
332 GST_DEBUG_OBJECT (demux, "%s => (boolean) %d", tag_name, b);
334 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
342 s = FLV_GET_STRING (reader);
346 GST_DEBUG_OBJECT (demux, "%s => (string) %s", tag_name, s);
348 if (!strcmp (tag_name, "creationdate")) {
349 GDate *date = g_date_new ();
351 parse_flv_demux_parse_date_string (date, s);
352 if (!g_date_valid (date)) {
353 GST_DEBUG_OBJECT (demux, "Failed to parse string as date");
355 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
356 GST_TAG_DATE, date, NULL);
359 } else if (!strcmp (tag_name, "creator")) {
360 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
361 GST_TAG_ARTIST, s, NULL);
362 } else if (!strcmp (tag_name, "title")) {
363 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
364 GST_TAG_TITLE, s, NULL);
365 } else if (!strcmp (tag_name, "metadatacreator")) {
366 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
367 GST_TAG_ENCODER, s, NULL);
369 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
378 gboolean end_of_object_marker = FALSE;
380 while (!end_of_object_marker) {
381 gboolean ok = gst_flv_demux_parse_metadata_item (demux, reader,
382 &end_of_object_marker);
384 if (G_UNLIKELY (!ok)) {
385 GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
392 case 8: // ECMA array
394 guint32 nb_elems = 0;
395 gboolean end_of_object_marker = FALSE;
397 if (!gst_byte_reader_get_uint32_be (reader, &nb_elems))
400 GST_DEBUG_OBJECT (demux, "there are approx. %d elements in the array",
403 while (!end_of_object_marker) {
404 gboolean ok = gst_flv_demux_parse_metadata_item (demux, reader,
405 &end_of_object_marker);
407 if (G_UNLIKELY (!ok)) {
408 GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
415 case 9: // End marker
417 GST_DEBUG_OBJECT (demux, "end marker ?");
418 if (tag_name[0] == '\0') {
420 GST_DEBUG_OBJECT (demux, "end marker detected");
429 guint32 nb_elems = 0;
431 if (!gst_byte_reader_get_uint32_be (reader, &nb_elems))
434 GST_DEBUG_OBJECT (demux, "array has %d elements", nb_elems);
436 if (!strcmp (tag_name, "times")) {
438 g_array_free (demux->times, TRUE);
440 demux->times = g_array_new (FALSE, TRUE, sizeof (gdouble));
441 } else if (!strcmp (tag_name, "filepositions")) {
442 if (demux->filepositions) {
443 g_array_free (demux->filepositions, TRUE);
445 demux->filepositions = g_array_new (FALSE, TRUE, sizeof (gdouble));
449 guint8 elem_type = 0;
451 if (!gst_byte_reader_get_uint8 (reader, &elem_type))
459 if (!gst_byte_reader_get_float64_be (reader, &d))
462 GST_DEBUG_OBJECT (demux, "element is a double %f", d);
464 if (!strcmp (tag_name, "times") && demux->times) {
465 g_array_append_val (demux->times, d);
466 } else if (!strcmp (tag_name, "filepositions") &&
467 demux->filepositions) {
468 g_array_append_val (demux->filepositions, d);
473 GST_WARNING_OBJECT (demux, "unsupported array element type %d",
485 if (!gst_byte_reader_get_float64_be (reader, &d))
488 if (!gst_byte_reader_get_int16_be (reader, &i))
491 GST_DEBUG_OBJECT (demux,
492 "%s => (date as a double) %f, timezone offset %d", tag_name, d, i);
494 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
499 GST_WARNING_OBJECT (demux, "unsupported tag type %d", tag_type);
513 gst_flv_demux_parse_tag_script (GstFlvDemux * demux, GstBuffer * buffer)
515 GstFlowReturn ret = GST_FLOW_OK;
516 GstByteReader reader;
521 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 7, GST_FLOW_ERROR);
523 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
524 gst_byte_reader_init (&reader, data, size);
526 gst_byte_reader_skip (&reader, 7);
528 GST_LOG_OBJECT (demux, "parsing a script tag");
530 if (!gst_byte_reader_get_uint8 (&reader, &type))
535 gchar *function_name;
538 function_name = FLV_GET_STRING (&reader);
540 GST_LOG_OBJECT (demux, "function name is %s", GST_STR_NULL (function_name));
542 if (function_name != NULL && strcmp (function_name, "onMetaData") == 0) {
543 gboolean end_marker = FALSE;
544 GST_DEBUG_OBJECT (demux, "we have a metadata script object");
546 if (!gst_byte_reader_get_uint8 (&reader, &type)) {
547 g_free (function_name);
554 guint32 nb_elems = 0;
557 if (!gst_byte_reader_get_uint32_be (&reader, &nb_elems)) {
558 g_free (function_name);
562 /* The number of elements is just a hint, some files have
563 nb_elements == 0 and actually contain items. */
564 GST_DEBUG_OBJECT (demux, "there are approx. %d elements in the array",
567 /* fallthrough to read data */
571 while (!end_marker) {
573 gst_flv_demux_parse_metadata_item (demux, &reader, &end_marker);
575 if (G_UNLIKELY (!ok)) {
576 GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
583 GST_DEBUG_OBJECT (demux, "Unhandled script data type : %d", type);
584 g_free (function_name);
588 demux->push_tags = TRUE;
591 g_free (function_name);
593 if (demux->index && demux->times && demux->filepositions) {
596 /* If an index was found, insert associations */
597 num = MIN (demux->times->len, demux->filepositions->len);
598 for (i = 0; i < num; i++) {
599 guint64 time, fileposition;
601 time = g_array_index (demux->times, gdouble, i) * GST_SECOND;
602 fileposition = g_array_index (demux->filepositions, gdouble, i);
603 gst_flv_demux_parse_and_add_index_entry (demux, time, fileposition,
606 demux->indexed = TRUE;
611 gst_buffer_unmap (buffer, data, -1);
617 gst_flv_demux_audio_negotiate (GstFlvDemux * demux, guint32 codec_tag,
618 guint32 rate, guint32 channels, guint32 width)
620 GstCaps *caps = NULL;
621 gchar *codec_name = NULL;
622 gboolean ret = FALSE;
623 guint adjusted_rate = rate;
627 caps = gst_caps_new_simple ("audio/x-adpcm", "layout", G_TYPE_STRING,
632 caps = gst_caps_new_simple ("audio/mpeg",
633 "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3,
634 "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
638 /* Assuming little endian for 0 (aka endianness of the
639 * system on which the file was created) as most people
640 * are probably using little endian machines */
641 caps = gst_caps_new_simple ("audio/x-raw-int",
642 "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
643 "signed", G_TYPE_BOOLEAN, (width == 8) ? FALSE : TRUE,
644 "width", G_TYPE_INT, width, "depth", G_TYPE_INT, width, NULL);
649 caps = gst_caps_new_empty_simple ("audio/x-nellymoser");
656 if (demux->audio_codec_data)
657 data = gst_buffer_map (demux->audio_codec_data, &size, NULL,
659 /* use codec-data to extract and verify samplerate */
660 if (demux->audio_codec_data && size >= 2) {
663 freq_index = GST_READ_UINT16_BE (data);
664 freq_index = (freq_index & 0x0780) >> 7;
666 gst_codec_utils_aac_get_sample_rate_from_index (freq_index);
668 if (adjusted_rate && (rate != adjusted_rate)) {
669 GST_LOG_OBJECT (demux, "Ajusting AAC sample rate %d -> %d", rate,
672 adjusted_rate = rate;
676 gst_buffer_unmap (demux->audio_codec_data, data, -1);
677 caps = gst_caps_new_simple ("audio/mpeg",
678 "mpegversion", G_TYPE_INT, 4, "framed", G_TYPE_BOOLEAN, TRUE,
679 "stream-format", G_TYPE_STRING, "raw", NULL);
683 caps = gst_caps_new_empty_simple ("audio/x-alaw");
686 caps = gst_caps_new_empty_simple ("audio/x-mulaw");
689 caps = gst_caps_new_empty_simple ("audio/x-speex");
692 GST_WARNING_OBJECT (demux, "unsupported audio codec tag %u", codec_tag);
695 if (G_UNLIKELY (!caps)) {
696 GST_WARNING_OBJECT (demux, "failed creating caps for audio pad");
700 gst_caps_set_simple (caps, "rate", G_TYPE_INT, adjusted_rate,
701 "channels", G_TYPE_INT, channels, NULL);
703 if (demux->audio_codec_data) {
704 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER,
705 demux->audio_codec_data, NULL);
708 ret = gst_pad_set_caps (demux->audio_pad, caps);
710 if (G_LIKELY (ret)) {
711 /* Store the caps we got from tags */
712 demux->audio_codec_tag = codec_tag;
714 demux->channels = channels;
715 demux->width = width;
717 codec_name = gst_pb_utils_get_codec_description (caps);
720 if (demux->taglist == NULL)
721 demux->taglist = gst_tag_list_new_empty ();
722 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
723 GST_TAG_AUDIO_CODEC, codec_name, NULL);
727 GST_DEBUG_OBJECT (demux->audio_pad, "successfully negotiated caps %"
728 GST_PTR_FORMAT, caps);
730 GST_WARNING_OBJECT (demux->audio_pad, "failed negotiating caps %"
731 GST_PTR_FORMAT, caps);
734 gst_caps_unref (caps);
741 gst_flv_demux_push_src_event (GstFlvDemux * demux, GstEvent * event)
745 if (demux->audio_pad)
746 ret |= gst_pad_push_event (demux->audio_pad, gst_event_ref (event));
748 if (demux->video_pad)
749 ret |= gst_pad_push_event (demux->video_pad, gst_event_ref (event));
751 gst_event_unref (event);
757 gst_flv_demux_push_tags (GstFlvDemux * demux)
759 if (demux->has_audio && !demux->audio_pad) {
760 GST_DEBUG_OBJECT (demux,
761 "Waiting for audio stream pad to come up before we can push tags");
764 if (demux->has_video && !demux->video_pad) {
765 GST_DEBUG_OBJECT (demux,
766 "Waiting for video stream pad to come up before we can push tags");
769 if (demux->taglist) {
770 GST_DEBUG_OBJECT (demux, "pushing tags out %" GST_PTR_FORMAT,
772 gst_flv_demux_push_src_event (demux, gst_event_new_tag (demux->taglist));
773 demux->taglist = gst_tag_list_new_empty ();
774 demux->push_tags = FALSE;
779 gst_flv_demux_parse_tag_audio (GstFlvDemux * demux, GstBuffer * buffer)
781 GstFlowReturn ret = GST_FLOW_OK;
782 guint32 pts = 0, codec_tag = 0, rate = 5512, width = 8, channels = 1;
783 guint32 codec_data = 0, pts_ext = 0;
789 GST_LOG_OBJECT (demux, "parsing an audio tag");
791 if (demux->no_more_pads && !demux->audio_pad) {
792 GST_WARNING_OBJECT (demux,
793 "Signaled no-more-pads already but had no audio pad -- ignoring");
797 g_return_val_if_fail (gst_buffer_get_size (buffer) == demux->tag_size,
800 /* Error out on tags with too small headers */
801 if (gst_buffer_get_size (buffer) < 11) {
802 GST_ERROR_OBJECT (demux, "Too small tag size (%d)",
803 gst_buffer_get_size (buffer));
804 return GST_FLOW_ERROR;
807 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
809 /* Grab information about audio tag */
810 pts = GST_READ_UINT24_BE (data);
811 /* read the pts extension to 32 bits integer */
812 pts_ext = GST_READ_UINT8 (data + 3);
814 pts |= pts_ext << 24;
816 GST_LOG_OBJECT (demux, "pts bytes %02X %02X %02X %02X (%d)", data[0], data[1],
817 data[2], data[3], pts);
819 /* Skip the stream id and go directly to the flags */
820 flags = GST_READ_UINT8 (data + 7);
822 /* Silently skip buffers with no data */
835 if ((flags & 0x0C) == 0x0C) {
837 } else if ((flags & 0x0C) == 0x08) {
839 } else if ((flags & 0x0C) == 0x04) {
843 codec_tag = flags >> 4;
844 if (codec_tag == 10) { /* AAC has an extra byte for packet type */
850 /* codec tags with special rates */
851 if (codec_tag == 5 || codec_tag == 14)
853 else if (codec_tag == 4)
856 GST_LOG_OBJECT (demux, "audio tag with %d channels, %dHz sampling rate, "
857 "%d bits width, codec tag %u (flags %02X)", channels, rate, width,
860 /* If we don't have our audio pad created, then create it. */
861 if (G_UNLIKELY (!demux->audio_pad)) {
864 gst_pad_new_from_template (gst_element_class_get_pad_template
865 (GST_ELEMENT_GET_CLASS (demux), "audio"), "audio");
866 if (G_UNLIKELY (!demux->audio_pad)) {
867 GST_WARNING_OBJECT (demux, "failed creating audio pad");
868 ret = GST_FLOW_ERROR;
873 if (!gst_flv_demux_audio_negotiate (demux, codec_tag, rate, channels,
875 gst_object_unref (demux->audio_pad);
876 demux->audio_pad = NULL;
877 ret = GST_FLOW_ERROR;
880 #ifndef GST_DISABLE_GST_DEBUG
884 caps = gst_pad_get_current_caps (demux->audio_pad);
885 GST_DEBUG_OBJECT (demux, "created audio pad with caps %" GST_PTR_FORMAT,
888 gst_caps_unref (caps);
892 /* Set functions on the pad */
893 gst_pad_set_query_function (demux->audio_pad,
894 GST_DEBUG_FUNCPTR (gst_flv_demux_query));
895 gst_pad_set_event_function (demux->audio_pad,
896 GST_DEBUG_FUNCPTR (gst_flv_demux_src_event));
898 gst_pad_use_fixed_caps (demux->audio_pad);
901 gst_pad_set_active (demux->audio_pad, TRUE);
903 /* We need to set caps before adding */
904 gst_element_add_pad (GST_ELEMENT (demux),
905 gst_object_ref (demux->audio_pad));
907 /* We only emit no more pads when we have audio and video. Indeed we can
908 * not trust the FLV header to tell us if there will be only audio or
909 * only video and we would just break discovery of some files */
910 if (demux->audio_pad && demux->video_pad) {
911 GST_DEBUG_OBJECT (demux, "emitting no more pads");
912 gst_element_no_more_pads (GST_ELEMENT (demux));
913 demux->no_more_pads = TRUE;
914 demux->push_tags = TRUE;
918 /* Check if caps have changed */
919 if (G_UNLIKELY (rate != demux->rate || channels != demux->channels ||
920 codec_tag != demux->audio_codec_tag || width != demux->width)) {
921 GST_DEBUG_OBJECT (demux, "audio settings have changed, changing caps");
924 if (!gst_flv_demux_audio_negotiate (demux, codec_tag, rate, channels,
926 ret = GST_FLOW_ERROR;
931 /* Push taglist if present */
932 if (G_UNLIKELY (demux->push_tags))
933 gst_flv_demux_push_tags (demux);
935 /* Check if we have anything to push */
936 if (demux->tag_data_size <= codec_data) {
937 GST_LOG_OBJECT (demux, "Nothing left in this tag, returning");
941 /* Create buffer from pad */
942 outbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY,
943 7 + codec_data, demux->tag_data_size - codec_data);
945 if (demux->audio_codec_tag == 10) {
946 guint8 aac_packet_type = GST_READ_UINT8 (data + 8);
948 switch (aac_packet_type) {
951 /* AudioSpecificConfig data */
952 GST_LOG_OBJECT (demux, "got an AAC codec data packet");
953 if (demux->audio_codec_data) {
954 gst_buffer_unref (demux->audio_codec_data);
956 demux->audio_codec_data = outbuf;
957 /* Use that buffer data in the caps */
958 gst_flv_demux_audio_negotiate (demux, codec_tag, rate, channels, width);
964 GST_LOG_OBJECT (demux, "got a raw AAC audio packet");
967 GST_WARNING_OBJECT (demux, "invalid AAC packet type %u",
972 /* Fill buffer with data */
973 GST_BUFFER_TIMESTAMP (outbuf) = pts * GST_MSECOND;
974 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
975 GST_BUFFER_OFFSET (outbuf) = demux->audio_offset++;
976 GST_BUFFER_OFFSET_END (outbuf) = demux->audio_offset;
978 if (demux->duration == GST_CLOCK_TIME_NONE ||
979 demux->duration < GST_BUFFER_TIMESTAMP (outbuf))
980 demux->duration = GST_BUFFER_TIMESTAMP (outbuf);
982 /* Only add audio frames to the index if we have no video,
983 * and if the index is not yet complete */
984 if (!demux->has_video && demux->index && !demux->indexed) {
985 gst_flv_demux_parse_and_add_index_entry (demux,
986 GST_BUFFER_TIMESTAMP (outbuf), demux->cur_tag_offset, TRUE);
989 if (G_UNLIKELY (demux->audio_need_discont)) {
990 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
991 demux->audio_need_discont = FALSE;
994 demux->segment.position = GST_BUFFER_TIMESTAMP (outbuf);
996 /* Do we need a newsegment event ? */
997 if (G_UNLIKELY (demux->audio_need_segment)) {
998 /* FIXME need one segment sent for all stream to maintain a/v sync */
999 if (!demux->new_seg_event) {
1000 GST_DEBUG_OBJECT (demux, "pushing newsegment from %"
1001 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
1002 GST_TIME_ARGS (demux->segment.position),
1003 GST_TIME_ARGS (demux->segment.stop));
1004 demux->segment.start = demux->segment.time = demux->segment.position;
1005 demux->new_seg_event = gst_event_new_segment (&demux->segment);
1007 GST_DEBUG_OBJECT (demux, "pushing pre-generated newsegment event");
1010 gst_pad_push_event (demux->audio_pad, gst_event_ref (demux->new_seg_event));
1012 demux->audio_need_segment = FALSE;
1015 GST_LOG_OBJECT (demux, "pushing %d bytes buffer at pts %" GST_TIME_FORMAT
1016 " with duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT,
1017 gst_buffer_get_size (outbuf),
1018 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1019 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf));
1021 if (!GST_CLOCK_TIME_IS_VALID (demux->audio_start)) {
1022 demux->audio_start = GST_BUFFER_TIMESTAMP (outbuf);
1024 if (!GST_CLOCK_TIME_IS_VALID (demux->audio_first_ts)) {
1025 demux->audio_first_ts = GST_BUFFER_TIMESTAMP (outbuf);
1028 if (G_UNLIKELY (!demux->no_more_pads
1029 && (GST_CLOCK_DIFF (demux->audio_start,
1030 GST_BUFFER_TIMESTAMP (outbuf)) > 6 * GST_SECOND))) {
1031 GST_DEBUG_OBJECT (demux,
1032 "Signalling no-more-pads because no video stream was found"
1033 " after 6 seconds of audio");
1034 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
1035 demux->no_more_pads = TRUE;
1036 demux->push_tags = TRUE;
1039 /* Push downstream */
1040 ret = gst_pad_push (demux->audio_pad, outbuf);
1041 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1042 if (demux->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
1043 demux->segment.position > demux->segment.stop) {
1044 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
1045 * we are at the end of the segment, so we just need to jump
1046 * back to the previous section. */
1047 GST_DEBUG_OBJECT (demux, "downstream has reached end of segment");
1048 demux->audio_done = TRUE;
1051 GST_WARNING_OBJECT (demux, "failed pushing a %" G_GUINT64_FORMAT
1052 " bytes audio buffer: %s", demux->tag_data_size,
1053 gst_flow_get_name (ret));
1054 if (ret == GST_FLOW_NOT_LINKED) {
1055 demux->audio_linked = FALSE;
1061 demux->audio_linked = TRUE;
1064 gst_buffer_unmap (buffer, data, -1);
1070 gst_flv_demux_video_negotiate (GstFlvDemux * demux, guint32 codec_tag)
1072 gboolean ret = FALSE;
1073 GstCaps *caps = NULL;
1074 gchar *codec_name = NULL;
1076 /* Generate caps for that pad */
1077 switch (codec_tag) {
1079 caps = gst_caps_new_empty_simple ("video/x-flash-video");
1082 caps = gst_caps_new_empty_simple ("video/x-flash-screen");
1085 caps = gst_caps_new_empty_simple ("video/x-vp6-flash");
1088 caps = gst_caps_new_empty_simple ("video/x-vp6-alpha");
1092 gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
1096 GST_WARNING_OBJECT (demux, "unsupported video codec tag %u", codec_tag);
1099 if (G_UNLIKELY (!caps)) {
1100 GST_WARNING_OBJECT (demux, "failed creating caps for video pad");
1104 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1105 demux->par_x, demux->par_y, NULL);
1107 if (G_LIKELY (demux->w)) {
1108 gst_caps_set_simple (caps, "width", G_TYPE_INT, demux->w, NULL);
1111 if (G_LIKELY (demux->h)) {
1112 gst_caps_set_simple (caps, "height", G_TYPE_INT, demux->h, NULL);
1115 if (G_LIKELY (demux->framerate)) {
1116 gint num = 0, den = 0;
1118 gst_util_double_to_fraction (demux->framerate, &num, &den);
1119 GST_DEBUG_OBJECT (demux->video_pad,
1120 "fps to be used on caps %f (as a fraction = %d/%d)", demux->framerate,
1123 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, num, den, NULL);
1126 if (demux->video_codec_data) {
1127 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER,
1128 demux->video_codec_data, NULL);
1131 ret = gst_pad_set_caps (demux->video_pad, caps);
1133 if (G_LIKELY (ret)) {
1134 /* Store the caps we have set */
1135 demux->video_codec_tag = codec_tag;
1137 codec_name = gst_pb_utils_get_codec_description (caps);
1140 if (demux->taglist == NULL)
1141 demux->taglist = gst_tag_list_new_empty ();
1142 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
1143 GST_TAG_VIDEO_CODEC, codec_name, NULL);
1144 g_free (codec_name);
1147 GST_DEBUG_OBJECT (demux->video_pad, "successfully negotiated caps %"
1148 GST_PTR_FORMAT, caps);
1150 GST_WARNING_OBJECT (demux->video_pad, "failed negotiating caps %"
1151 GST_PTR_FORMAT, caps);
1154 gst_caps_unref (caps);
1160 static GstFlowReturn
1161 gst_flv_demux_parse_tag_video (GstFlvDemux * demux, GstBuffer * buffer)
1163 GstFlowReturn ret = GST_FLOW_OK;
1164 guint32 pts = 0, codec_data = 1, pts_ext = 0;
1165 gboolean keyframe = FALSE;
1166 guint8 flags = 0, codec_tag = 0;
1171 g_return_val_if_fail (gst_buffer_get_size (buffer) == demux->tag_size,
1174 GST_LOG_OBJECT (demux, "parsing a video tag");
1176 if (demux->no_more_pads && !demux->video_pad) {
1177 GST_WARNING_OBJECT (demux,
1178 "Signaled no-more-pads already but had no audio pad -- ignoring");
1182 if (gst_buffer_get_size (buffer) < 12) {
1183 GST_ERROR_OBJECT (demux, "Too small tag size");
1184 return GST_FLOW_ERROR;
1187 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1189 /* Grab information about video tag */
1190 pts = GST_READ_UINT24_BE (data);
1191 /* read the pts extension to 32 bits integer */
1192 pts_ext = GST_READ_UINT8 (data + 3);
1194 pts |= pts_ext << 24;
1196 GST_LOG_OBJECT (demux, "pts bytes %02X %02X %02X %02X (%d)", data[0], data[1],
1197 data[2], data[3], pts);
1199 /* Skip the stream id and go directly to the flags */
1200 flags = GST_READ_UINT8 (data + 7);
1203 if ((flags >> 4) == 1) {
1207 codec_tag = flags & 0x0F;
1208 if (codec_tag == 4 || codec_tag == 5) {
1210 } else if (codec_tag == 7) {
1215 cts = GST_READ_UINT24_BE (data + 9);
1216 cts = (cts + 0xff800000) ^ 0xff800000;
1218 GST_LOG_OBJECT (demux, "got cts %d", cts);
1220 /* avoid negative overflow */
1221 if (cts >= 0 || pts >= -cts)
1225 GST_LOG_OBJECT (demux, "video tag with codec tag %u, keyframe (%d) "
1226 "(flags %02X)", codec_tag, keyframe, flags);
1228 /* If we don't have our video pad created, then create it. */
1229 if (G_UNLIKELY (!demux->video_pad)) {
1231 gst_pad_new_from_template (gst_element_class_get_pad_template
1232 (GST_ELEMENT_GET_CLASS (demux), "video"), "video");
1233 if (G_UNLIKELY (!demux->video_pad)) {
1234 GST_WARNING_OBJECT (demux, "failed creating video pad");
1235 ret = GST_FLOW_ERROR;
1239 if (!gst_flv_demux_video_negotiate (demux, codec_tag)) {
1240 gst_object_unref (demux->video_pad);
1241 demux->video_pad = NULL;
1242 ret = GST_FLOW_ERROR;
1246 /* When we ve set pixel-aspect-ratio we use that boolean to detect a
1247 * metadata tag that would come later and trigger a caps change */
1248 demux->got_par = FALSE;
1250 #ifndef GST_DISABLE_GST_DEBUG
1254 caps = gst_pad_get_current_caps (demux->video_pad);
1255 GST_DEBUG_OBJECT (demux, "created video pad with caps %" GST_PTR_FORMAT,
1258 gst_caps_unref (caps);
1262 /* Set functions on the pad */
1263 gst_pad_set_query_function (demux->video_pad,
1264 GST_DEBUG_FUNCPTR (gst_flv_demux_query));
1265 gst_pad_set_event_function (demux->video_pad,
1266 GST_DEBUG_FUNCPTR (gst_flv_demux_src_event));
1268 gst_pad_use_fixed_caps (demux->video_pad);
1270 /* Make it active */
1271 gst_pad_set_active (demux->video_pad, TRUE);
1273 /* We need to set caps before adding */
1274 gst_element_add_pad (GST_ELEMENT (demux),
1275 gst_object_ref (demux->video_pad));
1277 /* We only emit no more pads when we have audio and video. Indeed we can
1278 * not trust the FLV header to tell us if there will be only audio or
1279 * only video and we would just break discovery of some files */
1280 if (demux->audio_pad && demux->video_pad) {
1281 GST_DEBUG_OBJECT (demux, "emitting no more pads");
1282 gst_element_no_more_pads (GST_ELEMENT (demux));
1283 demux->no_more_pads = TRUE;
1284 demux->push_tags = TRUE;
1288 /* Check if caps have changed */
1289 if (G_UNLIKELY (codec_tag != demux->video_codec_tag || demux->got_par)) {
1291 GST_DEBUG_OBJECT (demux, "video settings have changed, changing caps");
1293 if (!gst_flv_demux_video_negotiate (demux, codec_tag)) {
1294 ret = GST_FLOW_ERROR;
1298 /* When we ve set pixel-aspect-ratio we use that boolean to detect a
1299 * metadata tag that would come later and trigger a caps change */
1300 demux->got_par = FALSE;
1303 /* Push taglist if present */
1304 if (G_UNLIKELY (demux->push_tags))
1305 gst_flv_demux_push_tags (demux);
1307 /* Check if we have anything to push */
1308 if (demux->tag_data_size <= codec_data) {
1309 GST_LOG_OBJECT (demux, "Nothing left in this tag, returning");
1313 /* Create buffer from pad */
1314 outbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY,
1315 7 + codec_data, demux->tag_data_size - codec_data);
1317 if (demux->video_codec_tag == 7) {
1318 guint8 avc_packet_type = GST_READ_UINT8 (data + 8);
1320 switch (avc_packet_type) {
1323 /* AVCDecoderConfigurationRecord data */
1324 GST_LOG_OBJECT (demux, "got an H.264 codec data packet");
1325 if (demux->video_codec_data) {
1326 gst_buffer_unref (demux->video_codec_data);
1328 demux->video_codec_data = outbuf;
1329 /* Use that buffer data in the caps */
1330 gst_flv_demux_video_negotiate (demux, codec_tag);
1335 /* H.264 NALU packet */
1336 GST_LOG_OBJECT (demux, "got a H.264 NALU video packet");
1339 GST_WARNING_OBJECT (demux, "invalid video packet type %u",
1344 /* Fill buffer with data */
1345 GST_BUFFER_TIMESTAMP (outbuf) = pts * GST_MSECOND;
1346 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
1347 GST_BUFFER_OFFSET (outbuf) = demux->video_offset++;
1348 GST_BUFFER_OFFSET_END (outbuf) = demux->video_offset;
1350 if (demux->duration == GST_CLOCK_TIME_NONE ||
1351 demux->duration < GST_BUFFER_TIMESTAMP (outbuf))
1352 demux->duration = GST_BUFFER_TIMESTAMP (outbuf);
1355 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1357 if (!demux->indexed && demux->index) {
1358 gst_flv_demux_parse_and_add_index_entry (demux,
1359 GST_BUFFER_TIMESTAMP (outbuf), demux->cur_tag_offset, keyframe);
1362 if (G_UNLIKELY (demux->video_need_discont)) {
1363 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1364 demux->video_need_discont = FALSE;
1367 demux->segment.position = GST_BUFFER_TIMESTAMP (outbuf);
1369 /* Do we need a newsegment event ? */
1370 if (G_UNLIKELY (demux->video_need_segment)) {
1371 /* FIXME need one segment sent for all stream to maintain a/v sync */
1372 if (!demux->new_seg_event) {
1373 GST_DEBUG_OBJECT (demux, "pushing newsegment from %"
1374 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
1375 GST_TIME_ARGS (demux->segment.position),
1376 GST_TIME_ARGS (demux->segment.stop));
1377 demux->segment.start = demux->segment.time = demux->segment.position;
1378 demux->new_seg_event = gst_event_new_segment (&demux->segment);
1380 GST_DEBUG_OBJECT (demux, "pushing pre-generated newsegment event");
1383 gst_pad_push_event (demux->video_pad, gst_event_ref (demux->new_seg_event));
1385 demux->video_need_segment = FALSE;
1388 GST_LOG_OBJECT (demux, "pushing %d bytes buffer at pts %" GST_TIME_FORMAT
1389 " with duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
1390 ", keyframe (%d)", gst_buffer_get_size (outbuf),
1391 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1392 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
1395 if (!GST_CLOCK_TIME_IS_VALID (demux->video_start)) {
1396 demux->video_start = GST_BUFFER_TIMESTAMP (outbuf);
1398 if (!GST_CLOCK_TIME_IS_VALID (demux->audio_first_ts)) {
1399 demux->video_first_ts = GST_BUFFER_TIMESTAMP (outbuf);
1402 if (G_UNLIKELY (!demux->no_more_pads
1403 && (GST_CLOCK_DIFF (demux->video_start,
1404 GST_BUFFER_TIMESTAMP (outbuf)) > 6 * GST_SECOND))) {
1405 GST_DEBUG_OBJECT (demux,
1406 "Signalling no-more-pads because no audio stream was found"
1407 " after 6 seconds of video");
1408 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
1409 demux->no_more_pads = TRUE;
1410 demux->push_tags = TRUE;
1413 /* Push downstream */
1414 ret = gst_pad_push (demux->video_pad, outbuf);
1416 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1417 if (demux->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
1418 demux->segment.position > demux->segment.stop) {
1419 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
1420 * we are at the end of the segment, so we just need to jump
1421 * back to the previous section. */
1422 GST_DEBUG_OBJECT (demux, "downstream has reached end of segment");
1423 demux->video_done = TRUE;
1426 GST_WARNING_OBJECT (demux, "failed pushing a %" G_GUINT64_FORMAT
1427 " bytes video buffer: %s", demux->tag_data_size,
1428 gst_flow_get_name (ret));
1429 if (ret == GST_FLOW_NOT_LINKED) {
1430 demux->video_linked = FALSE;
1436 demux->video_linked = TRUE;
1439 gst_buffer_unmap (buffer, data, -1);
1444 gst_flv_demux_parse_tag_timestamp (GstFlvDemux * demux, gboolean index,
1445 GstBuffer * buffer, size_t * tag_size)
1447 guint32 pts = 0, pts_ext = 0;
1448 guint32 tag_data_size;
1450 gboolean keyframe = TRUE;
1451 GstClockTime ret = GST_CLOCK_TIME_NONE;
1452 guint8 *data, *bdata;
1455 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 12,
1456 GST_CLOCK_TIME_NONE);
1458 data = bdata = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1462 if (type != 9 && type != 8 && type != 18) {
1463 GST_WARNING_OBJECT (demux, "Unsupported tag type %u", data[0]);
1468 demux->has_video = TRUE;
1470 demux->has_audio = TRUE;
1472 tag_data_size = GST_READ_UINT24_BE (data + 1);
1474 if (size >= tag_data_size + 11 + 4) {
1475 if (GST_READ_UINT32_BE (data + tag_data_size + 11) != tag_data_size + 11) {
1476 GST_WARNING_OBJECT (demux, "Invalid tag size");
1482 *tag_size = tag_data_size + 11 + 4;
1486 GST_LOG_OBJECT (demux, "pts bytes %02X %02X %02X %02X", data[0], data[1],
1489 /* Grab timestamp of tag tag */
1490 pts = GST_READ_UINT24_BE (data);
1491 /* read the pts extension to 32 bits integer */
1492 pts_ext = GST_READ_UINT8 (data + 3);
1494 pts |= pts_ext << 24;
1499 keyframe = ((data[0] >> 4) == 1);
1502 ret = pts * GST_MSECOND;
1503 GST_LOG_OBJECT (demux, "pts: %" GST_TIME_FORMAT, GST_TIME_ARGS (ret));
1505 if (index && demux->index && !demux->indexed && (type == 9 || (type == 8
1506 && !demux->has_video))) {
1507 gst_flv_demux_parse_and_add_index_entry (demux, ret, demux->offset,
1511 if (demux->duration == GST_CLOCK_TIME_NONE || demux->duration < ret)
1512 demux->duration = ret;
1515 gst_buffer_unmap (buffer, bdata, -1);
1519 static GstFlowReturn
1520 gst_flv_demux_parse_tag_type (GstFlvDemux * demux, GstBuffer * buffer)
1522 GstFlowReturn ret = GST_FLOW_OK;
1523 guint8 tag_type = 0;
1526 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 4, GST_FLOW_ERROR);
1528 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
1534 demux->state = FLV_STATE_TAG_VIDEO;
1535 demux->has_video = TRUE;
1538 demux->state = FLV_STATE_TAG_AUDIO;
1539 demux->has_audio = TRUE;
1542 demux->state = FLV_STATE_TAG_SCRIPT;
1545 GST_WARNING_OBJECT (demux, "unsupported tag type %u", tag_type);
1548 /* Tag size is 1 byte of type + 3 bytes of size + 7 bytes + tag data size +
1549 * 4 bytes of previous tag size */
1550 demux->tag_data_size = GST_READ_UINT24_BE (data + 1);
1551 demux->tag_size = demux->tag_data_size + 11;
1553 GST_LOG_OBJECT (demux, "tag data size is %" G_GUINT64_FORMAT,
1554 demux->tag_data_size);
1556 gst_buffer_unmap (buffer, data, -1);
1561 static GstFlowReturn
1562 gst_flv_demux_parse_header (GstFlvDemux * demux, GstBuffer * buffer)
1564 GstFlowReturn ret = GST_FLOW_OK;
1567 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 9, GST_FLOW_ERROR);
1569 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
1571 /* Check for the FLV tag */
1572 if (data[0] == 'F' && data[1] == 'L' && data[2] == 'V') {
1573 GST_DEBUG_OBJECT (demux, "FLV header detected");
1575 if (G_UNLIKELY (demux->strict)) {
1576 GST_WARNING_OBJECT (demux, "invalid header tag detected");
1577 ret = GST_FLOW_UNEXPECTED;
1582 /* Now look at audio/video flags */
1584 guint8 flags = data[4];
1586 demux->has_video = demux->has_audio = FALSE;
1589 GST_DEBUG_OBJECT (demux, "there is a video stream");
1590 demux->has_video = TRUE;
1593 GST_DEBUG_OBJECT (demux, "there is an audio stream");
1594 demux->has_audio = TRUE;
1598 /* do a one-time seekability check */
1599 gst_flv_demux_check_seekability (demux);
1601 /* We don't care about the rest */
1602 demux->need_header = FALSE;
1605 gst_buffer_unmap (buffer, data, -1);
1611 gst_flv_demux_flush (GstFlvDemux * demux, gboolean discont)
1613 GST_DEBUG_OBJECT (demux, "flushing queued data in the FLV demuxer");
1615 gst_adapter_clear (demux->adapter);
1617 demux->audio_need_discont = TRUE;
1618 demux->video_need_discont = TRUE;
1620 demux->flushing = FALSE;
1622 /* Only in push mode and if we're not during a seek */
1623 if (!demux->random_access && demux->state != FLV_STATE_SEEK) {
1624 /* After a flush we expect a tag_type */
1625 demux->state = FLV_STATE_TAG_TYPE;
1626 /* We reset the offset and will get one from first push */
1632 gst_flv_demux_cleanup (GstFlvDemux * demux)
1634 GST_DEBUG_OBJECT (demux, "cleaning up FLV demuxer");
1636 demux->state = FLV_STATE_HEADER;
1638 demux->flushing = FALSE;
1639 demux->need_header = TRUE;
1640 demux->audio_need_segment = TRUE;
1641 demux->video_need_segment = TRUE;
1642 demux->audio_need_discont = TRUE;
1643 demux->video_need_discont = TRUE;
1645 /* By default we consider them as linked */
1646 demux->audio_linked = TRUE;
1647 demux->video_linked = TRUE;
1649 demux->has_audio = FALSE;
1650 demux->has_video = FALSE;
1651 demux->push_tags = FALSE;
1652 demux->got_par = FALSE;
1654 demux->indexed = FALSE;
1655 demux->upstream_seekable = FALSE;
1656 demux->file_size = 0;
1658 demux->index_max_pos = 0;
1659 demux->index_max_time = 0;
1661 demux->audio_start = demux->video_start = GST_CLOCK_TIME_NONE;
1663 demux->no_more_pads = FALSE;
1665 gst_segment_init (&demux->segment, GST_FORMAT_TIME);
1667 demux->w = demux->h = 0;
1668 demux->framerate = 0.0;
1669 demux->par_x = demux->par_y = 1;
1670 demux->video_offset = 0;
1671 demux->audio_offset = 0;
1672 demux->offset = demux->cur_tag_offset = 0;
1673 demux->tag_size = demux->tag_data_size = 0;
1674 demux->duration = GST_CLOCK_TIME_NONE;
1676 if (demux->new_seg_event) {
1677 gst_event_unref (demux->new_seg_event);
1678 demux->new_seg_event = NULL;
1681 gst_adapter_clear (demux->adapter);
1683 if (demux->audio_codec_data) {
1684 gst_buffer_unref (demux->audio_codec_data);
1685 demux->audio_codec_data = NULL;
1688 if (demux->video_codec_data) {
1689 gst_buffer_unref (demux->video_codec_data);
1690 demux->video_codec_data = NULL;
1693 if (demux->audio_pad) {
1694 gst_element_remove_pad (GST_ELEMENT (demux), demux->audio_pad);
1695 gst_object_unref (demux->audio_pad);
1696 demux->audio_pad = NULL;
1699 if (demux->video_pad) {
1700 gst_element_remove_pad (GST_ELEMENT (demux), demux->video_pad);
1701 gst_object_unref (demux->video_pad);
1702 demux->video_pad = NULL;
1706 g_array_free (demux->times, TRUE);
1707 demux->times = NULL;
1710 if (demux->filepositions) {
1711 g_array_free (demux->filepositions, TRUE);
1712 demux->filepositions = NULL;
1717 * Create and push a flushing seek event upstream
1720 flv_demux_seek_to_offset (GstFlvDemux * demux, guint64 offset)
1725 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1728 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1729 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1730 GST_SEEK_TYPE_NONE, -1);
1732 res = gst_pad_push_event (demux->sinkpad, event);
1735 demux->offset = offset;
1739 static GstFlowReturn
1740 gst_flv_demux_chain (GstPad * pad, GstBuffer * buffer)
1742 GstFlowReturn ret = GST_FLOW_OK;
1743 GstFlvDemux *demux = NULL;
1745 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
1747 GST_LOG_OBJECT (demux, "received buffer of %d bytes at offset %"
1748 G_GUINT64_FORMAT, gst_buffer_get_size (buffer),
1749 GST_BUFFER_OFFSET (buffer));
1751 if (G_UNLIKELY (GST_BUFFER_OFFSET (buffer) == 0)) {
1752 GST_DEBUG_OBJECT (demux, "beginning of file, expect header");
1753 demux->state = FLV_STATE_HEADER;
1757 if (G_UNLIKELY (demux->offset == 0 && GST_BUFFER_OFFSET (buffer) != 0)) {
1758 GST_DEBUG_OBJECT (demux, "offset was zero, synchronizing with buffer's");
1759 demux->offset = GST_BUFFER_OFFSET (buffer);
1762 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
1763 GST_DEBUG_OBJECT (demux, "Discontinuity");
1764 gst_adapter_clear (demux->adapter);
1767 gst_adapter_push (demux->adapter, buffer);
1769 if (demux->seeking) {
1770 demux->state = FLV_STATE_SEEK;
1771 GST_OBJECT_LOCK (demux);
1772 demux->seeking = FALSE;
1773 GST_OBJECT_UNLOCK (demux);
1777 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1778 if (ret == GST_FLOW_NOT_LINKED && (demux->audio_linked
1779 || demux->video_linked)) {
1782 GST_DEBUG_OBJECT (demux, "got flow return %s", gst_flow_get_name (ret));
1787 if (G_UNLIKELY (demux->flushing)) {
1788 GST_DEBUG_OBJECT (demux, "we are now flushing, exiting parser loop");
1789 ret = GST_FLOW_WRONG_STATE;
1793 switch (demux->state) {
1794 case FLV_STATE_HEADER:
1796 if (gst_adapter_available (demux->adapter) >= FLV_HEADER_SIZE) {
1799 buffer = gst_adapter_take_buffer (demux->adapter, FLV_HEADER_SIZE);
1801 ret = gst_flv_demux_parse_header (demux, buffer);
1803 gst_buffer_unref (buffer);
1804 demux->offset += FLV_HEADER_SIZE;
1806 demux->state = FLV_STATE_TAG_TYPE;
1812 case FLV_STATE_TAG_TYPE:
1814 if (gst_adapter_available (demux->adapter) >= FLV_TAG_TYPE_SIZE) {
1817 /* Remember the tag offset in bytes */
1818 demux->cur_tag_offset = demux->offset;
1820 buffer = gst_adapter_take_buffer (demux->adapter, FLV_TAG_TYPE_SIZE);
1822 ret = gst_flv_demux_parse_tag_type (demux, buffer);
1824 gst_buffer_unref (buffer);
1825 demux->offset += FLV_TAG_TYPE_SIZE;
1827 /* last tag is not an index => no index/don't know where the index is
1828 * seek back to the beginning */
1829 if (demux->seek_event && demux->state != FLV_STATE_TAG_SCRIPT)
1837 case FLV_STATE_TAG_VIDEO:
1839 if (gst_adapter_available (demux->adapter) >= demux->tag_size) {
1842 buffer = gst_adapter_take_buffer (demux->adapter, demux->tag_size);
1844 ret = gst_flv_demux_parse_tag_video (demux, buffer);
1846 gst_buffer_unref (buffer);
1847 demux->offset += demux->tag_size;
1849 demux->state = FLV_STATE_TAG_TYPE;
1855 case FLV_STATE_TAG_AUDIO:
1857 if (gst_adapter_available (demux->adapter) >= demux->tag_size) {
1860 buffer = gst_adapter_take_buffer (demux->adapter, demux->tag_size);
1862 ret = gst_flv_demux_parse_tag_audio (demux, buffer);
1864 gst_buffer_unref (buffer);
1865 demux->offset += demux->tag_size;
1867 demux->state = FLV_STATE_TAG_TYPE;
1873 case FLV_STATE_TAG_SCRIPT:
1875 if (gst_adapter_available (demux->adapter) >= demux->tag_size) {
1878 buffer = gst_adapter_take_buffer (demux->adapter, demux->tag_size);
1880 ret = gst_flv_demux_parse_tag_script (demux, buffer);
1882 gst_buffer_unref (buffer);
1883 demux->offset += demux->tag_size;
1885 demux->state = FLV_STATE_TAG_TYPE;
1887 /* if there's a seek event we're here for the index so if we don't have it
1888 * we seek back to the beginning */
1889 if (demux->seek_event) {
1891 demux->state = FLV_STATE_SEEK;
1901 case FLV_STATE_SEEK:
1907 if (!demux->indexed) {
1908 if (demux->offset == demux->file_size - sizeof (guint32)) {
1909 guint64 seek_offset;
1912 data = gst_adapter_take (demux->adapter, 4);
1916 seek_offset = demux->file_size - sizeof (guint32) -
1917 GST_READ_UINT32_BE (data);
1920 GST_INFO_OBJECT (demux,
1921 "Seeking to beginning of last tag at %" G_GUINT64_FORMAT,
1923 demux->state = FLV_STATE_TAG_TYPE;
1924 flv_demux_seek_to_offset (demux, seek_offset);
1930 GST_OBJECT_LOCK (demux);
1931 event = demux->seek_event;
1932 demux->seek_event = NULL;
1933 GST_OBJECT_UNLOCK (demux);
1935 /* calculate and perform seek */
1936 if (!flv_demux_handle_seek_push (demux, event))
1939 gst_event_unref (event);
1940 demux->state = FLV_STATE_TAG_TYPE;
1944 GST_DEBUG_OBJECT (demux, "unexpected demuxer state");
1948 if (G_UNLIKELY (ret == GST_FLOW_NOT_LINKED)) {
1949 /* If either audio or video is linked we return GST_FLOW_OK */
1950 if (demux->audio_linked || demux->video_linked) {
1955 gst_object_unref (demux);
1962 GST_OBJECT_LOCK (demux);
1963 demux->seeking = FALSE;
1964 gst_event_unref (demux->seek_event);
1965 demux->seek_event = NULL;
1966 GST_OBJECT_UNLOCK (demux);
1967 GST_WARNING_OBJECT (demux,
1968 "failed to find an index, seeking back to beginning");
1969 flv_demux_seek_to_offset (demux, 0);
1974 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("seek failed"));
1975 return GST_FLOW_ERROR;
1980 static GstFlowReturn
1981 gst_flv_demux_pull_range (GstFlvDemux * demux, GstPad * pad, guint64 offset,
1982 guint size, GstBuffer ** buffer)
1986 ret = gst_pad_pull_range (pad, offset, size, buffer);
1987 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1988 GST_WARNING_OBJECT (demux,
1989 "failed when pulling %d bytes from offset %" G_GUINT64_FORMAT ": %s",
1990 size, offset, gst_flow_get_name (ret));
1995 if (G_UNLIKELY (*buffer && gst_buffer_get_size (*buffer) != size)) {
1996 GST_WARNING_OBJECT (demux,
1997 "partial pull got %d when expecting %d from offset %" G_GUINT64_FORMAT,
1998 gst_buffer_get_size (*buffer), size, offset);
1999 gst_buffer_unref (*buffer);
2000 ret = GST_FLOW_UNEXPECTED;
2008 static GstFlowReturn
2009 gst_flv_demux_pull_tag (GstPad * pad, GstFlvDemux * demux)
2011 GstBuffer *buffer = NULL;
2012 GstFlowReturn ret = GST_FLOW_OK;
2014 /* Store tag offset */
2015 demux->cur_tag_offset = demux->offset;
2017 /* Get the first 4 bytes to identify tag type and size */
2018 if (G_UNLIKELY ((ret = gst_flv_demux_pull_range (demux, pad, demux->offset,
2019 FLV_TAG_TYPE_SIZE, &buffer)) != GST_FLOW_OK))
2022 /* Identify tag type */
2023 ret = gst_flv_demux_parse_tag_type (demux, buffer);
2025 gst_buffer_unref (buffer);
2027 if (G_UNLIKELY (ret != GST_FLOW_OK))
2030 /* Jump over tag type + size */
2031 demux->offset += FLV_TAG_TYPE_SIZE;
2033 /* Pull the whole tag */
2034 if (G_UNLIKELY ((ret = gst_flv_demux_pull_range (demux, pad, demux->offset,
2035 demux->tag_size, &buffer)) != GST_FLOW_OK))
2038 switch (demux->state) {
2039 case FLV_STATE_TAG_VIDEO:
2040 ret = gst_flv_demux_parse_tag_video (demux, buffer);
2042 case FLV_STATE_TAG_AUDIO:
2043 ret = gst_flv_demux_parse_tag_audio (demux, buffer);
2045 case FLV_STATE_TAG_SCRIPT:
2046 ret = gst_flv_demux_parse_tag_script (demux, buffer);
2049 GST_WARNING_OBJECT (demux, "unexpected state %d", demux->state);
2052 gst_buffer_unref (buffer);
2054 /* Jump over that part we've just parsed */
2055 demux->offset += demux->tag_size;
2057 /* Make sure we reinitialize the tag size */
2058 demux->tag_size = 0;
2060 /* Ready for the next tag */
2061 demux->state = FLV_STATE_TAG_TYPE;
2063 if (G_UNLIKELY (ret == GST_FLOW_NOT_LINKED)) {
2064 /* If either audio or video is linked we return GST_FLOW_OK */
2065 if (demux->audio_linked || demux->video_linked) {
2068 GST_WARNING_OBJECT (demux, "parsing this tag returned not-linked and "
2069 "neither video nor audio are linked");
2077 static GstFlowReturn
2078 gst_flv_demux_pull_header (GstPad * pad, GstFlvDemux * demux)
2080 GstBuffer *buffer = NULL;
2081 GstFlowReturn ret = GST_FLOW_OK;
2083 /* Get the first 9 bytes */
2084 if (G_UNLIKELY ((ret = gst_flv_demux_pull_range (demux, pad, demux->offset,
2085 FLV_HEADER_SIZE, &buffer)) != GST_FLOW_OK))
2088 ret = gst_flv_demux_parse_header (demux, buffer);
2090 gst_buffer_unref (buffer);
2092 /* Jump over the header now */
2093 demux->offset += FLV_HEADER_SIZE;
2094 demux->state = FLV_STATE_TAG_TYPE;
2101 gst_flv_demux_move_to_offset (GstFlvDemux * demux, gint64 offset,
2104 demux->offset = offset;
2106 /* Tell all the stream we moved to a different position (discont) */
2107 demux->audio_need_discont = TRUE;
2108 demux->video_need_discont = TRUE;
2110 /* next section setup */
2111 demux->from_offset = -1;
2112 demux->audio_done = demux->video_done = FALSE;
2113 demux->audio_first_ts = demux->video_first_ts = GST_CLOCK_TIME_NONE;
2116 demux->from_offset = -1;
2117 demux->to_offset = G_MAXINT64;
2120 /* If we seeked at the beginning of the file parse the header again */
2121 if (G_UNLIKELY (!demux->offset)) {
2122 demux->state = FLV_STATE_HEADER;
2123 } else { /* or parse a tag */
2124 demux->state = FLV_STATE_TAG_TYPE;
2128 static GstFlowReturn
2129 gst_flv_demux_seek_to_prev_keyframe (GstFlvDemux * demux)
2131 GstFlowReturn ret = GST_FLOW_UNEXPECTED;
2132 GstIndexEntry *entry = NULL;
2134 GST_DEBUG_OBJECT (demux,
2135 "terminated section started at offset %" G_GINT64_FORMAT,
2136 demux->from_offset);
2138 /* we are done if we got all audio and video */
2139 if ((!GST_CLOCK_TIME_IS_VALID (demux->audio_first_ts) ||
2140 demux->audio_first_ts < demux->segment.start) &&
2141 (!GST_CLOCK_TIME_IS_VALID (demux->video_first_ts) ||
2142 demux->video_first_ts < demux->segment.start))
2145 if (demux->from_offset <= 0)
2148 GST_DEBUG_OBJECT (demux, "locating previous position");
2150 /* locate index entry before previous start position */
2152 entry = gst_index_get_assoc_entry (demux->index, demux->index_id,
2153 GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT,
2154 GST_FORMAT_BYTES, demux->from_offset - 1);
2159 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
2160 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
2162 GST_DEBUG_OBJECT (demux, "found index entry for %" G_GINT64_FORMAT
2163 " at %" GST_TIME_FORMAT ", seeking to %" G_GINT64_FORMAT,
2164 demux->offset - 1, GST_TIME_ARGS (time), bytes);
2166 /* setup for next section */
2167 demux->to_offset = demux->from_offset;
2168 gst_flv_demux_move_to_offset (demux, bytes, FALSE);
2176 static GstFlowReturn
2177 gst_flv_demux_create_index (GstFlvDemux * demux, gint64 pos, GstClockTime ts)
2183 GstClockTime tag_time;
2184 GstFlowReturn ret = GST_FLOW_OK;
2186 if (!gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES, &size))
2189 GST_DEBUG_OBJECT (demux, "building index at %" G_GINT64_FORMAT
2190 " looking for time %" GST_TIME_FORMAT, pos, GST_TIME_ARGS (ts));
2192 old_offset = demux->offset;
2193 demux->offset = pos;
2195 while ((ret = gst_flv_demux_pull_range (demux, demux->sinkpad, demux->offset,
2196 12, &buffer)) == GST_FLOW_OK) {
2198 gst_flv_demux_parse_tag_timestamp (demux, TRUE, buffer, &tag_size);
2200 gst_buffer_unref (buffer);
2202 if (G_UNLIKELY (tag_time == GST_CLOCK_TIME_NONE || tag_time > ts))
2205 demux->offset += tag_size;
2208 if (ret == GST_FLOW_UNEXPECTED) {
2209 /* file ran out, so mark we have complete index */
2210 demux->indexed = TRUE;
2215 demux->offset = old_offset;
2221 gst_flv_demux_get_metadata (GstFlvDemux * demux)
2223 gint64 ret = 0, offset;
2224 size_t tag_size, size;
2225 GstBuffer *buffer = NULL;
2228 if (!gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES, &offset))
2232 GST_DEBUG_OBJECT (demux, "upstream size: %" G_GINT64_FORMAT, offset);
2233 if (G_UNLIKELY (offset < 4))
2237 if (GST_FLOW_OK != gst_flv_demux_pull_range (demux, demux->sinkpad, offset,
2241 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
2242 tag_size = GST_READ_UINT32_BE (data);
2243 gst_buffer_unmap (buffer, data, -1);
2244 GST_DEBUG_OBJECT (demux, "last tag size: %" G_GSIZE_FORMAT, tag_size);
2245 gst_buffer_unref (buffer);
2249 if (GST_FLOW_OK != gst_flv_demux_pull_range (demux, demux->sinkpad, offset,
2253 /* a consistency check */
2254 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
2255 size = GST_READ_UINT24_BE (data + 1);
2256 if (size != tag_size - 11) {
2257 gst_buffer_unmap (buffer, data, -1);
2258 GST_DEBUG_OBJECT (demux,
2259 "tag size %" G_GSIZE_FORMAT ", expected %" G_GSIZE_FORMAT
2260 ", corrupt or truncated file", size, tag_size - 11);
2264 /* try to update duration with timestamp in any case */
2265 gst_flv_demux_parse_tag_timestamp (demux, FALSE, buffer, &size);
2267 /* maybe get some more metadata */
2268 if (data[0] == 18) {
2269 gst_buffer_unmap (buffer, data, -1);
2270 gst_buffer_unref (buffer);
2272 GST_DEBUG_OBJECT (demux, "script tag, pulling it to parse");
2274 if (GST_FLOW_OK == gst_flv_demux_pull_range (demux, demux->sinkpad, offset,
2276 gst_flv_demux_parse_tag_script (demux, buffer);
2278 gst_buffer_unmap (buffer, data, -1);
2283 gst_buffer_unref (buffer);
2289 gst_flv_demux_loop (GstPad * pad)
2291 GstFlvDemux *demux = NULL;
2292 GstFlowReturn ret = GST_FLOW_OK;
2294 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2297 switch (demux->state) {
2298 case FLV_STATE_TAG_TYPE:
2299 if (demux->from_offset == -1)
2300 demux->from_offset = demux->offset;
2301 ret = gst_flv_demux_pull_tag (pad, demux);
2302 /* if we have seen real data, we probably passed a possible metadata
2303 * header located at start. So if we do not yet have an index,
2304 * try to pick up metadata (index, duration) at the end */
2305 if (G_UNLIKELY (!demux->file_size && !demux->indexed &&
2306 (demux->has_video || demux->has_audio)))
2307 demux->file_size = gst_flv_demux_get_metadata (demux);
2309 case FLV_STATE_DONE:
2310 ret = GST_FLOW_UNEXPECTED;
2312 case FLV_STATE_SEEK:
2313 /* seek issued with insufficient index;
2314 * scan for index in task thread from current maximum offset to
2315 * desired time and then perform seek */
2316 /* TODO maybe some buffering message or so to indicate scan progress */
2317 ret = gst_flv_demux_create_index (demux, demux->index_max_pos,
2319 if (ret != GST_FLOW_OK)
2321 /* position and state arranged by seek,
2322 * also unrefs event */
2323 gst_flv_demux_handle_seek_pull (demux, demux->seek_event, FALSE);
2324 demux->seek_event = NULL;
2327 ret = gst_flv_demux_pull_header (pad, demux);
2328 /* index scans start after header */
2329 demux->index_max_pos = demux->offset;
2333 if (demux->segment.rate < 0.0) {
2334 /* check end of section */
2335 if ((gint64) demux->offset >= demux->to_offset ||
2336 demux->segment.position >= demux->segment.stop + 2 * GST_SECOND ||
2337 (demux->audio_done && demux->video_done))
2338 ret = gst_flv_demux_seek_to_prev_keyframe (demux);
2340 /* check EOS condition */
2341 if ((demux->segment.stop != -1) &&
2342 (demux->segment.position >= demux->segment.stop)) {
2343 ret = GST_FLOW_UNEXPECTED;
2347 /* pause if something went wrong or at end */
2348 if (G_UNLIKELY (ret != GST_FLOW_OK))
2351 gst_object_unref (demux);
2357 const gchar *reason = gst_flow_get_name (ret);
2359 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
2360 gst_pad_pause_task (pad);
2362 if (ret == GST_FLOW_UNEXPECTED) {
2363 /* handle end-of-stream/segment */
2364 /* so align our position with the end of it, if there is one
2365 * this ensures a subsequent will arrive at correct base/acc time */
2366 if (demux->segment.rate > 0.0 &&
2367 GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
2368 demux->segment.position = demux->segment.stop;
2369 else if (demux->segment.rate < 0.0)
2370 demux->segment.position = demux->segment.start;
2372 /* perform EOS logic */
2373 if (!demux->no_more_pads) {
2374 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2375 demux->no_more_pads = TRUE;
2378 if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2381 /* for segment playback we need to post when (in stream time)
2382 * we stopped, this is either stop (when set) or the duration. */
2383 if ((stop = demux->segment.stop) == -1)
2384 stop = demux->segment.duration;
2386 if (demux->segment.rate >= 0) {
2387 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
2388 gst_element_post_message (GST_ELEMENT_CAST (demux),
2389 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2390 GST_FORMAT_TIME, stop));
2391 } else { /* Reverse playback */
2392 GST_LOG_OBJECT (demux, "Sending segment done, at beginning of "
2394 gst_element_post_message (GST_ELEMENT_CAST (demux),
2395 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2396 GST_FORMAT_TIME, demux->segment.start));
2399 /* normal playback, send EOS to all linked pads */
2400 if (!demux->no_more_pads) {
2401 gst_element_no_more_pads (GST_ELEMENT (demux));
2402 demux->no_more_pads = TRUE;
2405 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
2406 if (!gst_flv_demux_push_src_event (demux, gst_event_new_eos ()))
2407 GST_WARNING_OBJECT (demux, "failed pushing EOS on streams");
2409 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2410 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2411 ("Internal data stream error."),
2412 ("stream stopped, reason %s", reason));
2413 gst_flv_demux_push_src_event (demux, gst_event_new_eos ());
2415 gst_object_unref (demux);
2421 gst_flv_demux_find_offset (GstFlvDemux * demux, GstSegment * segment)
2425 GstIndexEntry *entry;
2427 g_return_val_if_fail (segment != NULL, 0);
2429 time = segment->position;
2432 /* Let's check if we have an index entry for that seek time */
2433 entry = gst_index_get_assoc_entry (demux->index, demux->index_id,
2434 GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT,
2435 GST_FORMAT_TIME, time);
2438 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
2439 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
2441 GST_DEBUG_OBJECT (demux, "found index entry for %" GST_TIME_FORMAT
2442 " at %" GST_TIME_FORMAT ", seeking to %" G_GINT64_FORMAT,
2443 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (time), bytes);
2445 /* Key frame seeking */
2446 if (segment->flags & GST_SEEK_FLAG_KEY_UNIT) {
2447 /* Adjust the segment so that the keyframe fits in */
2448 if (time < segment->start) {
2449 segment->start = segment->time = time;
2451 segment->position = time;
2454 GST_DEBUG_OBJECT (demux, "no index entry found for %" GST_TIME_FORMAT,
2455 GST_TIME_ARGS (segment->start));
2463 flv_demux_handle_seek_push (GstFlvDemux * demux, GstEvent * event)
2467 GstSeekType start_type, stop_type;
2470 gboolean update, flush, ret;
2471 GstSegment seeksegment;
2473 gst_event_parse_seek (event, &rate, &format, &flags,
2474 &start_type, &start, &stop_type, &stop);
2476 if (format != GST_FORMAT_TIME)
2479 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
2480 /* FIXME : the keyframe flag is never used ! */
2482 /* Work on a copy until we are sure the seek succeeded. */
2483 memcpy (&seeksegment, &demux->segment, sizeof (GstSegment));
2485 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
2488 /* Apply the seek to our segment */
2489 gst_segment_do_seek (&seeksegment, rate, format, flags,
2490 start_type, start, stop_type, stop, &update);
2492 GST_DEBUG_OBJECT (demux, "segment configured %" GST_SEGMENT_FORMAT,
2495 if (flush || seeksegment.position != demux->segment.position) {
2496 /* Do the actual seeking */
2497 guint64 offset = gst_flv_demux_find_offset (demux, &seeksegment);
2499 GST_DEBUG_OBJECT (demux, "generating an upstream seek at position %"
2500 G_GUINT64_FORMAT, offset);
2501 ret = gst_pad_push_event (demux->sinkpad,
2502 gst_event_new_seek (seeksegment.rate, GST_FORMAT_BYTES,
2503 seeksegment.flags | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET,
2504 offset, GST_SEEK_TYPE_NONE, 0));
2505 if (G_UNLIKELY (!ret)) {
2506 GST_WARNING_OBJECT (demux, "upstream seek failed");
2509 /* Tell all the stream we moved to a different position (discont) */
2510 demux->audio_need_discont = TRUE;
2511 demux->video_need_discont = TRUE;
2517 /* Ok seek succeeded, take the newly configured segment */
2518 memcpy (&demux->segment, &seeksegment, sizeof (GstSegment));
2520 /* Tell all the stream a new segment is needed */
2521 demux->audio_need_segment = TRUE;
2522 demux->video_need_segment = TRUE;
2523 /* Clean any potential newsegment event kept for the streams. The first
2524 * stream needing a new segment will create a new one. */
2525 if (G_UNLIKELY (demux->new_seg_event)) {
2526 gst_event_unref (demux->new_seg_event);
2527 demux->new_seg_event = NULL;
2529 gst_event_unref (event);
2531 ret = gst_pad_push_event (demux->sinkpad, event);
2539 GST_WARNING_OBJECT (demux, "we only support seeking in TIME format");
2540 gst_event_unref (event);
2546 gst_flv_demux_handle_seek_push (GstFlvDemux * demux, GstEvent * event)
2550 gst_event_parse_seek (event, NULL, &format, NULL, NULL, NULL, NULL, NULL);
2552 if (format != GST_FORMAT_TIME) {
2553 GST_WARNING_OBJECT (demux, "we only support seeking in TIME format");
2554 gst_event_unref (event);
2558 /* First try upstream */
2559 if (gst_pad_push_event (demux->sinkpad, gst_event_ref (event))) {
2560 GST_DEBUG_OBJECT (demux, "Upstream successfully seeked");
2561 gst_event_unref (event);
2565 if (!demux->indexed) {
2566 guint64 seek_offset = 0;
2567 gboolean building_index;
2569 GST_OBJECT_LOCK (demux);
2570 /* handle the seek in the chain function */
2571 demux->seeking = TRUE;
2572 demux->state = FLV_STATE_SEEK;
2574 /* copy the event */
2575 if (demux->seek_event)
2576 gst_event_unref (demux->seek_event);
2577 demux->seek_event = gst_event_ref (event);
2579 /* set the building_index flag so that only one thread can setup the
2580 * structures for index seeking. */
2581 building_index = demux->building_index;
2582 if (!building_index) {
2583 demux->building_index = TRUE;
2584 if (!demux->file_size
2585 && !gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES,
2586 &demux->file_size)) {
2587 GST_WARNING_OBJECT (demux, "Failed to query upstream file size");
2588 GST_OBJECT_UNLOCK (demux);
2592 /* we hope the last tag is a scriptdataobject containing an index
2593 * the size of the last tag is given in the last guint32 bits
2594 * then we seek to the beginning of the tag, parse it and hopefully obtain an index */
2595 seek_offset = demux->file_size - sizeof (guint32);
2596 GST_DEBUG_OBJECT (demux,
2597 "File size obtained, seeking to %" G_GUINT64_FORMAT, seek_offset);
2599 GST_OBJECT_UNLOCK (demux);
2601 if (!building_index) {
2602 GST_INFO_OBJECT (demux, "Seeking to last 4 bytes at %" G_GUINT64_FORMAT,
2604 return flv_demux_seek_to_offset (demux, seek_offset);
2607 /* FIXME: we have to always return true so that we don't block the seek
2609 * Note: maybe it is OK to return true if we're still building the index */
2613 return flv_demux_handle_seek_push (demux, event);
2617 gst_flv_demux_handle_seek_pull (GstFlvDemux * demux, GstEvent * event,
2622 GstSeekType start_type, stop_type;
2625 gboolean update, flush, ret = FALSE;
2626 GstSegment seeksegment;
2628 gst_event_parse_seek (event, &rate, &format, &flags,
2629 &start_type, &start, &stop_type, &stop);
2631 if (format != GST_FORMAT_TIME)
2634 /* mark seeking thread entering flushing/pausing */
2635 GST_OBJECT_LOCK (demux);
2637 demux->seeking = seeking;
2638 GST_OBJECT_UNLOCK (demux);
2640 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
2641 /* FIXME : the keyframe flag is never used */
2644 /* Flush start up and downstream to make sure data flow and loops are
2646 gst_flv_demux_push_src_event (demux, gst_event_new_flush_start ());
2647 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
2649 /* Pause the pulling task */
2650 gst_pad_pause_task (demux->sinkpad);
2653 /* Take the stream lock */
2654 GST_PAD_STREAM_LOCK (demux->sinkpad);
2657 /* Stop flushing upstream we need to pull */
2658 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop (TRUE));
2661 /* Work on a copy until we are sure the seek succeeded. */
2662 memcpy (&seeksegment, &demux->segment, sizeof (GstSegment));
2664 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
2667 /* Apply the seek to our segment */
2668 gst_segment_do_seek (&seeksegment, rate, format, flags,
2669 start_type, start, stop_type, stop, &update);
2671 GST_DEBUG_OBJECT (demux, "segment configured %" GST_SEGMENT_FORMAT,
2674 if (flush || seeksegment.position != demux->segment.position) {
2675 /* Do the actual seeking */
2676 /* index is reliable if it is complete or we do not go to far ahead */
2677 if (seeking && !demux->indexed &&
2678 seeksegment.position > demux->index_max_time + 10 * GST_SECOND) {
2679 GST_DEBUG_OBJECT (demux, "delaying seek to post-scan; "
2680 " index only up to %" GST_TIME_FORMAT,
2681 GST_TIME_ARGS (demux->index_max_time));
2682 /* stop flushing for now */
2684 gst_flv_demux_push_src_event (demux, gst_event_new_flush_stop (TRUE));
2685 /* delegate scanning and index building to task thread to avoid
2686 * occupying main (UI) loop */
2687 if (demux->seek_event)
2688 gst_event_unref (demux->seek_event);
2689 demux->seek_event = gst_event_ref (event);
2690 demux->seek_time = seeksegment.position;
2691 demux->state = FLV_STATE_SEEK;
2692 /* do not know about succes yet, but we did care and handled it */
2696 /* now index should be as reliable as it can be for current purpose */
2697 gst_flv_demux_move_to_offset (demux,
2698 gst_flv_demux_find_offset (demux, &seeksegment), TRUE);
2705 /* Stop flushing, the sinks are at time 0 now */
2706 gst_flv_demux_push_src_event (demux, gst_event_new_flush_stop (TRUE));
2710 /* Ok seek succeeded, take the newly configured segment */
2711 memcpy (&demux->segment, &seeksegment, sizeof (GstSegment));
2713 /* Notify about the start of a new segment */
2714 if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2715 gst_element_post_message (GST_ELEMENT (demux),
2716 gst_message_new_segment_start (GST_OBJECT (demux),
2717 demux->segment.format, demux->segment.position));
2720 /* Tell all the stream a new segment is needed */
2721 demux->audio_need_segment = TRUE;
2722 demux->video_need_segment = TRUE;
2723 /* Clean any potential newsegment event kept for the streams. The first
2724 * stream needing a new segment will create a new one. */
2725 if (G_UNLIKELY (demux->new_seg_event)) {
2726 gst_event_unref (demux->new_seg_event);
2727 demux->new_seg_event = NULL;
2729 if (demux->segment.rate < 0.0) {
2730 /* we can't generate a segment by locking on
2731 * to the first timestamp we see */
2732 GST_DEBUG_OBJECT (demux, "preparing newsegment from %"
2733 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
2734 GST_TIME_ARGS (demux->segment.start),
2735 GST_TIME_ARGS (demux->segment.stop));
2736 demux->new_seg_event = gst_event_new_segment (&demux->segment);
2741 GST_OBJECT_LOCK (demux);
2742 seeking = demux->seeking && !seeking;
2743 demux->seeking = FALSE;
2744 GST_OBJECT_UNLOCK (demux);
2746 /* if we detect an external seek having started (and possibly already having
2747 * flushed), do not restart task to give it a chance.
2748 * Otherwise external one's flushing will take care to pause task */
2750 gst_pad_pause_task (demux->sinkpad);
2752 gst_pad_start_task (demux->sinkpad,
2753 (GstTaskFunction) gst_flv_demux_loop, demux->sinkpad);
2756 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
2758 gst_event_unref (event);
2764 GST_WARNING_OBJECT (demux, "we only support seeking in TIME format");
2765 gst_event_unref (event);
2770 /* If we can pull that's prefered */
2772 gst_flv_demux_sink_activate (GstPad * sinkpad)
2777 query = gst_query_new_scheduling ();
2779 if (!gst_pad_peer_query (sinkpad, query)) {
2780 gst_query_unref (query);
2784 gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
2785 gst_query_unref (query);
2790 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2791 return gst_pad_activate_pull (sinkpad, TRUE);
2795 GST_DEBUG_OBJECT (sinkpad, "activating push");
2796 return gst_pad_activate_push (sinkpad, TRUE);
2800 /* This function gets called when we activate ourselves in push mode.
2801 * We cannot seek (ourselves) in the stream */
2803 gst_flv_demux_sink_activate_push (GstPad * sinkpad, gboolean active)
2807 demux = GST_FLV_DEMUX (gst_pad_get_parent (sinkpad));
2809 demux->random_access = FALSE;
2811 gst_object_unref (demux);
2816 /* this function gets called when we activate ourselves in pull mode.
2817 * We can perform random access to the resource and we start a task
2818 * to start reading */
2820 gst_flv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
2824 demux = GST_FLV_DEMUX (gst_pad_get_parent (sinkpad));
2827 demux->random_access = TRUE;
2828 gst_object_unref (demux);
2829 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_flv_demux_loop,
2832 demux->random_access = FALSE;
2833 gst_object_unref (demux);
2834 return gst_pad_stop_task (sinkpad);
2839 gst_flv_demux_sink_event (GstPad * pad, GstEvent * event)
2842 gboolean ret = FALSE;
2844 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2846 GST_DEBUG_OBJECT (demux, "handling event %s", GST_EVENT_TYPE_NAME (event));
2848 switch (GST_EVENT_TYPE (event)) {
2849 case GST_EVENT_FLUSH_START:
2850 GST_DEBUG_OBJECT (demux, "trying to force chain function to exit");
2851 demux->flushing = TRUE;
2852 ret = gst_flv_demux_push_src_event (demux, event);
2854 case GST_EVENT_FLUSH_STOP:
2855 GST_DEBUG_OBJECT (demux, "flushing FLV demuxer");
2856 gst_flv_demux_flush (demux, TRUE);
2857 ret = gst_flv_demux_push_src_event (demux, event);
2860 GST_DEBUG_OBJECT (demux, "received EOS");
2862 GST_DEBUG_OBJECT (demux, "committing index");
2863 gst_index_commit (demux->index, demux->index_id);
2865 if (!demux->no_more_pads) {
2866 gst_element_no_more_pads (GST_ELEMENT (demux));
2867 demux->no_more_pads = TRUE;
2870 if (!gst_flv_demux_push_src_event (demux, event))
2871 GST_WARNING_OBJECT (demux, "failed pushing EOS on streams");
2874 case GST_EVENT_SEGMENT:
2876 GstSegment in_segment;
2878 GST_DEBUG_OBJECT (demux, "received new segment");
2880 gst_event_copy_segment (event, &in_segment);
2882 if (in_segment.format == GST_FORMAT_TIME) {
2883 /* time segment, this is perfect, copy over the values. */
2884 memcpy (&demux->segment, &in_segment, sizeof (in_segment));
2886 GST_DEBUG_OBJECT (demux, "NEWSEGMENT: %" GST_SEGMENT_FORMAT,
2890 ret = gst_flv_demux_push_src_event (demux, event);
2892 /* non-time format */
2893 demux->audio_need_segment = TRUE;
2894 demux->video_need_segment = TRUE;
2896 gst_event_unref (event);
2901 ret = gst_flv_demux_push_src_event (demux, event);
2905 gst_object_unref (demux);
2911 gst_flv_demux_src_event (GstPad * pad, GstEvent * event)
2914 gboolean ret = FALSE;
2916 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2918 GST_DEBUG_OBJECT (demux, "handling event %s", GST_EVENT_TYPE_NAME (event));
2920 switch (GST_EVENT_TYPE (event)) {
2921 case GST_EVENT_SEEK:
2922 if (demux->random_access) {
2923 ret = gst_flv_demux_handle_seek_pull (demux, event, TRUE);
2925 ret = gst_flv_demux_handle_seek_push (demux, event);
2929 ret = gst_pad_push_event (demux->sinkpad, event);
2933 gst_object_unref (demux);
2939 gst_flv_demux_query (GstPad * pad, GstQuery * query)
2941 gboolean res = TRUE;
2944 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2946 switch (GST_QUERY_TYPE (query)) {
2947 case GST_QUERY_DURATION:
2951 gst_query_parse_duration (query, &format, NULL);
2953 /* duration is time only */
2954 if (format != GST_FORMAT_TIME) {
2955 GST_DEBUG_OBJECT (demux, "duration query only supported for time "
2961 GST_DEBUG_OBJECT (pad, "duration query, replying %" GST_TIME_FORMAT,
2962 GST_TIME_ARGS (demux->duration));
2964 gst_query_set_duration (query, GST_FORMAT_TIME, demux->duration);
2968 case GST_QUERY_POSITION:
2972 gst_query_parse_position (query, &format, NULL);
2974 /* position is time only */
2975 if (format != GST_FORMAT_TIME) {
2976 GST_DEBUG_OBJECT (demux, "position query only supported for time "
2982 GST_DEBUG_OBJECT (pad, "position query, replying %" GST_TIME_FORMAT,
2983 GST_TIME_ARGS (demux->segment.position));
2985 gst_query_set_position (query, GST_FORMAT_TIME, demux->segment.position);
2990 case GST_QUERY_SEEKING:{
2993 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2995 /* First ask upstream */
2996 if (fmt == GST_FORMAT_TIME && gst_pad_peer_query (demux->sinkpad, query)) {
2999 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
3006 if (fmt != GST_FORMAT_TIME || !demux->index) {
3007 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
3008 } else if (demux->random_access) {
3009 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
3012 GstQuery *peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
3013 gboolean seekable = gst_pad_peer_query (demux->sinkpad, peerquery);
3016 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
3017 gst_query_unref (peerquery);
3020 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
3023 gst_query_set_seeking (query, GST_FORMAT_TIME, FALSE, -1, -1);
3027 case GST_QUERY_LATENCY:
3032 if ((peer = gst_pad_get_peer (demux->sinkpad))) {
3033 /* query latency on peer pad */
3034 res = gst_pad_query (peer, query);
3035 gst_object_unref (peer);
3037 /* no peer, we don't know */
3045 gst_object_unref (demux);
3050 static GstStateChangeReturn
3051 gst_flv_demux_change_state (GstElement * element, GstStateChange transition)
3054 GstStateChangeReturn ret;
3056 demux = GST_FLV_DEMUX (element);
3058 switch (transition) {
3059 case GST_STATE_CHANGE_READY_TO_PAUSED:
3060 /* If this is our own index destroy it as the
3061 * old entries might be wrong for the new stream */
3062 if (demux->own_index) {
3063 gst_object_unref (demux->index);
3064 demux->index = NULL;
3065 demux->own_index = FALSE;
3068 /* If no index was created, generate one */
3069 if (G_UNLIKELY (!demux->index)) {
3070 GST_DEBUG_OBJECT (demux, "no index provided creating our own");
3072 demux->index = gst_index_factory_make ("memindex");
3074 gst_index_get_writer_id (demux->index, GST_OBJECT (demux),
3076 demux->own_index = TRUE;
3078 gst_flv_demux_cleanup (demux);
3084 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3085 if (ret == GST_STATE_CHANGE_FAILURE)
3088 switch (transition) {
3089 case GST_STATE_CHANGE_PAUSED_TO_READY:
3090 gst_flv_demux_cleanup (demux);
3100 gst_flv_demux_set_index (GstElement * element, GstIndex * index)
3102 GstFlvDemux *demux = GST_FLV_DEMUX (element);
3104 GST_OBJECT_LOCK (demux);
3106 gst_object_unref (demux->index);
3108 demux->index = gst_object_ref (index);
3109 demux->own_index = FALSE;
3111 demux->index = NULL;
3113 GST_OBJECT_UNLOCK (demux);
3114 /* object lock might be taken again */
3116 gst_index_get_writer_id (index, GST_OBJECT (element), &demux->index_id);
3117 GST_DEBUG_OBJECT (demux, "Set index %" GST_PTR_FORMAT, demux->index);
3122 gst_flv_demux_get_index (GstElement * element)
3124 GstIndex *result = NULL;
3126 GstFlvDemux *demux = GST_FLV_DEMUX (element);
3128 GST_OBJECT_LOCK (demux);
3130 result = gst_object_ref (demux->index);
3131 GST_OBJECT_UNLOCK (demux);
3137 gst_flv_demux_dispose (GObject * object)
3139 GstFlvDemux *demux = GST_FLV_DEMUX (object);
3141 GST_DEBUG_OBJECT (demux, "disposing FLV demuxer");
3143 if (demux->adapter) {
3144 gst_adapter_clear (demux->adapter);
3145 g_object_unref (demux->adapter);
3146 demux->adapter = NULL;
3149 if (demux->taglist) {
3150 gst_tag_list_free (demux->taglist);
3151 demux->taglist = NULL;
3154 if (demux->new_seg_event) {
3155 gst_event_unref (demux->new_seg_event);
3156 demux->new_seg_event = NULL;
3159 if (demux->audio_codec_data) {
3160 gst_buffer_unref (demux->audio_codec_data);
3161 demux->audio_codec_data = NULL;
3164 if (demux->video_codec_data) {
3165 gst_buffer_unref (demux->video_codec_data);
3166 demux->video_codec_data = NULL;
3169 if (demux->audio_pad) {
3170 gst_object_unref (demux->audio_pad);
3171 demux->audio_pad = NULL;
3174 if (demux->video_pad) {
3175 gst_object_unref (demux->video_pad);
3176 demux->video_pad = NULL;
3180 gst_object_unref (demux->index);
3181 demux->index = NULL;
3185 g_array_free (demux->times, TRUE);
3186 demux->times = NULL;
3189 if (demux->filepositions) {
3190 g_array_free (demux->filepositions, TRUE);
3191 demux->filepositions = NULL;
3194 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
3198 gst_flv_demux_class_init (GstFlvDemuxClass * klass)
3200 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
3201 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
3203 gobject_class->dispose = gst_flv_demux_dispose;
3205 gstelement_class->change_state =
3206 GST_DEBUG_FUNCPTR (gst_flv_demux_change_state);
3207 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_flv_demux_set_index);
3208 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_flv_demux_get_index);
3210 gst_element_class_add_pad_template (gstelement_class,
3211 gst_static_pad_template_get (&flv_sink_template));
3212 gst_element_class_add_pad_template (gstelement_class,
3213 gst_static_pad_template_get (&audio_src_template));
3214 gst_element_class_add_pad_template (gstelement_class,
3215 gst_static_pad_template_get (&video_src_template));
3216 gst_element_class_set_details_simple (gstelement_class, "FLV Demuxer",
3218 "Demux FLV feeds into digital streams",
3219 "Julien Moutte <julien@moutte.net>");
3223 gst_flv_demux_init (GstFlvDemux * demux)
3226 gst_pad_new_from_static_template (&flv_sink_template, "sink");
3228 gst_pad_set_event_function (demux->sinkpad,
3229 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_event));
3230 gst_pad_set_chain_function (demux->sinkpad,
3231 GST_DEBUG_FUNCPTR (gst_flv_demux_chain));
3232 gst_pad_set_activate_function (demux->sinkpad,
3233 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_activate));
3234 gst_pad_set_activatepull_function (demux->sinkpad,
3235 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_activate_pull));
3236 gst_pad_set_activatepush_function (demux->sinkpad,
3237 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_activate_push));
3239 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
3241 demux->adapter = gst_adapter_new ();
3242 demux->taglist = gst_tag_list_new_empty ();
3243 gst_segment_init (&demux->segment, GST_FORMAT_TIME);
3245 demux->own_index = FALSE;
3247 gst_flv_demux_cleanup (demux);
3251 plugin_init (GstPlugin * plugin)
3253 GST_DEBUG_CATEGORY_INIT (flvdemux_debug, "flvdemux", 0, "FLV demuxer");
3255 if (!gst_element_register (plugin, "flvdemux", GST_RANK_PRIMARY,
3256 gst_flv_demux_get_type ()) ||
3257 !gst_element_register (plugin, "flvmux", GST_RANK_PRIMARY,
3258 gst_flv_mux_get_type ()))
3264 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
3265 "flv", "FLV muxing and demuxing plugin",
3266 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)