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)) {
182 static const GstQueryType *
183 gst_flv_demux_query_types (GstPad * pad)
185 static const GstQueryType query_types[] = {
196 gst_flv_demux_check_seekability (GstFlvDemux * demux)
199 gint64 start = -1, stop = -1;
201 demux->upstream_seekable = FALSE;
203 query = gst_query_new_seeking (GST_FORMAT_BYTES);
204 if (!gst_pad_peer_query (demux->sinkpad, query)) {
205 GST_DEBUG_OBJECT (demux, "seeking query failed");
206 gst_query_unref (query);
210 gst_query_parse_seeking (query, NULL, &demux->upstream_seekable,
213 gst_query_unref (query);
215 /* try harder to query upstream size if we didn't get it the first time */
216 if (demux->upstream_seekable && stop == -1) {
217 GST_DEBUG_OBJECT (demux, "doing duration query to fix up unset stop");
218 gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES, &stop);
221 /* if upstream doesn't know the size, it's likely that it's not seekable in
222 * practice even if it technically may be seekable */
223 if (demux->upstream_seekable && (start != 0 || stop <= start)) {
224 GST_DEBUG_OBJECT (demux, "seekable but unknown start/stop -> disable");
225 demux->upstream_seekable = FALSE;
228 GST_DEBUG_OBJECT (demux, "upstream seekable: %d", demux->upstream_seekable);
232 parse_flv_demux_parse_date_string (GDate * date, const gchar * s)
234 g_date_set_parse (date, s);
235 if (g_date_valid (date))
238 /* "Fri Oct 15 15:13:16 2004" needs to be parsed */
240 static const gchar *months[] = {
241 "Jan", "Feb", "Mar", "Apr",
242 "May", "Jun", "Jul", "Aug",
243 "Sep", "Oct", "Nov", "Dec"
245 gchar **tokens = g_strsplit (s, " ", -1);
250 if (g_strv_length (tokens) != 5)
253 if (strlen (tokens[1]) != 3)
255 for (i = 0; i < 12; i++) {
256 if (!strcmp (tokens[1], months[i])) {
262 g_date_set_month (date, i + 1);
264 d = g_ascii_strtoull (tokens[2], &endptr, 10);
265 if (d == 0 && *endptr != '\0')
268 g_date_set_day (date, d);
270 d = g_ascii_strtoull (tokens[4], &endptr, 10);
271 if (d == 0 && *endptr != '\0')
274 g_date_set_year (date, d);
283 gst_flv_demux_parse_metadata_item (GstFlvDemux * demux, GstByteReader * reader,
284 gboolean * end_marker)
286 gchar *tag_name = NULL;
289 /* Initialize the end_marker flag to FALSE */
292 /* Name of the tag */
293 tag_name = FLV_GET_STRING (reader);
294 if (G_UNLIKELY (!tag_name)) {
295 GST_WARNING_OBJECT (demux, "failed reading tag name");
299 /* What kind of object is that */
300 if (!gst_byte_reader_get_uint8 (reader, &tag_type))
303 GST_DEBUG_OBJECT (demux, "tag name %s, tag type %d", tag_name, tag_type);
307 { /* Use a union to read the uint64 and then as a double */
310 if (!gst_byte_reader_get_float64_be (reader, &d))
313 GST_DEBUG_OBJECT (demux, "%s => (double) %f", tag_name, d);
315 if (!strcmp (tag_name, "duration")) {
316 demux->duration = d * GST_SECOND;
318 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
319 GST_TAG_DURATION, demux->duration, NULL);
320 } else if (!strcmp (tag_name, "AspectRatioX")) {
322 demux->got_par = TRUE;
323 } else if (!strcmp (tag_name, "AspectRatioY")) {
325 demux->got_par = TRUE;
326 } else if (!strcmp (tag_name, "width")) {
328 } else if (!strcmp (tag_name, "height")) {
330 } else if (!strcmp (tag_name, "framerate")) {
331 demux->framerate = d;
333 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
342 if (!gst_byte_reader_get_uint8 (reader, &b))
345 GST_DEBUG_OBJECT (demux, "%s => (boolean) %d", tag_name, b);
347 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
355 s = FLV_GET_STRING (reader);
359 GST_DEBUG_OBJECT (demux, "%s => (string) %s", tag_name, s);
361 if (!strcmp (tag_name, "creationdate")) {
362 GDate *date = g_date_new ();
364 parse_flv_demux_parse_date_string (date, s);
365 if (!g_date_valid (date)) {
366 GST_DEBUG_OBJECT (demux, "Failed to parse string as date");
368 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
369 GST_TAG_DATE, date, NULL);
372 } else if (!strcmp (tag_name, "creator")) {
373 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
374 GST_TAG_ARTIST, s, NULL);
375 } else if (!strcmp (tag_name, "title")) {
376 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
377 GST_TAG_TITLE, s, NULL);
378 } else if (!strcmp (tag_name, "metadatacreator")) {
379 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
380 GST_TAG_ENCODER, s, NULL);
382 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
391 gboolean end_of_object_marker = FALSE;
393 while (!end_of_object_marker) {
394 gboolean ok = gst_flv_demux_parse_metadata_item (demux, reader,
395 &end_of_object_marker);
397 if (G_UNLIKELY (!ok)) {
398 GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
405 case 8: // ECMA array
407 guint32 nb_elems = 0;
408 gboolean end_of_object_marker = FALSE;
410 if (!gst_byte_reader_get_uint32_be (reader, &nb_elems))
413 GST_DEBUG_OBJECT (demux, "there are approx. %d elements in the array",
416 while (!end_of_object_marker) {
417 gboolean ok = gst_flv_demux_parse_metadata_item (demux, reader,
418 &end_of_object_marker);
420 if (G_UNLIKELY (!ok)) {
421 GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
428 case 9: // End marker
430 GST_DEBUG_OBJECT (demux, "end marker ?");
431 if (tag_name[0] == '\0') {
433 GST_DEBUG_OBJECT (demux, "end marker detected");
442 guint32 nb_elems = 0;
444 if (!gst_byte_reader_get_uint32_be (reader, &nb_elems))
447 GST_DEBUG_OBJECT (demux, "array has %d elements", nb_elems);
449 if (!strcmp (tag_name, "times")) {
451 g_array_free (demux->times, TRUE);
453 demux->times = g_array_new (FALSE, TRUE, sizeof (gdouble));
454 } else if (!strcmp (tag_name, "filepositions")) {
455 if (demux->filepositions) {
456 g_array_free (demux->filepositions, TRUE);
458 demux->filepositions = g_array_new (FALSE, TRUE, sizeof (gdouble));
462 guint8 elem_type = 0;
464 if (!gst_byte_reader_get_uint8 (reader, &elem_type))
472 if (!gst_byte_reader_get_float64_be (reader, &d))
475 GST_DEBUG_OBJECT (demux, "element is a double %f", d);
477 if (!strcmp (tag_name, "times") && demux->times) {
478 g_array_append_val (demux->times, d);
479 } else if (!strcmp (tag_name, "filepositions") &&
480 demux->filepositions) {
481 g_array_append_val (demux->filepositions, d);
486 GST_WARNING_OBJECT (demux, "unsupported array element type %d",
498 if (!gst_byte_reader_get_float64_be (reader, &d))
501 if (!gst_byte_reader_get_int16_be (reader, &i))
504 GST_DEBUG_OBJECT (demux,
505 "%s => (date as a double) %f, timezone offset %d", tag_name, d, i);
507 GST_INFO_OBJECT (demux, "Tag \'%s\' not handled", tag_name);
512 GST_WARNING_OBJECT (demux, "unsupported tag type %d", tag_type);
526 gst_flv_demux_parse_tag_script (GstFlvDemux * demux, GstBuffer * buffer)
528 GstFlowReturn ret = GST_FLOW_OK;
529 GstByteReader reader;
534 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 7, GST_FLOW_ERROR);
536 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
537 gst_byte_reader_init (&reader, data, size);
539 gst_byte_reader_skip (&reader, 7);
541 GST_LOG_OBJECT (demux, "parsing a script tag");
543 if (!gst_byte_reader_get_uint8 (&reader, &type))
548 gchar *function_name;
551 function_name = FLV_GET_STRING (&reader);
553 GST_LOG_OBJECT (demux, "function name is %s", GST_STR_NULL (function_name));
555 if (function_name != NULL && strcmp (function_name, "onMetaData") == 0) {
556 gboolean end_marker = FALSE;
557 GST_DEBUG_OBJECT (demux, "we have a metadata script object");
559 if (!gst_byte_reader_get_uint8 (&reader, &type)) {
560 g_free (function_name);
567 guint32 nb_elems = 0;
570 if (!gst_byte_reader_get_uint32_be (&reader, &nb_elems)) {
571 g_free (function_name);
575 /* The number of elements is just a hint, some files have
576 nb_elements == 0 and actually contain items. */
577 GST_DEBUG_OBJECT (demux, "there are approx. %d elements in the array",
580 /* fallthrough to read data */
584 while (!end_marker) {
586 gst_flv_demux_parse_metadata_item (demux, &reader, &end_marker);
588 if (G_UNLIKELY (!ok)) {
589 GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
596 GST_DEBUG_OBJECT (demux, "Unhandled script data type : %d", type);
597 g_free (function_name);
601 demux->push_tags = TRUE;
604 g_free (function_name);
606 if (demux->index && demux->times && demux->filepositions) {
609 /* If an index was found, insert associations */
610 num = MIN (demux->times->len, demux->filepositions->len);
611 for (i = 0; i < num; i++) {
612 guint64 time, fileposition;
614 time = g_array_index (demux->times, gdouble, i) * GST_SECOND;
615 fileposition = g_array_index (demux->filepositions, gdouble, i);
616 gst_flv_demux_parse_and_add_index_entry (demux, time, fileposition,
619 demux->indexed = TRUE;
624 gst_buffer_unmap (buffer, data, -1);
630 gst_flv_demux_audio_negotiate (GstFlvDemux * demux, guint32 codec_tag,
631 guint32 rate, guint32 channels, guint32 width)
633 GstCaps *caps = NULL;
634 gchar *codec_name = NULL;
635 gboolean ret = FALSE;
636 guint adjusted_rate = rate;
640 caps = gst_caps_new_simple ("audio/x-adpcm", "layout", G_TYPE_STRING,
645 caps = gst_caps_new_simple ("audio/mpeg",
646 "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3,
647 "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
651 /* Assuming little endian for 0 (aka endianness of the
652 * system on which the file was created) as most people
653 * are probably using little endian machines */
654 caps = gst_caps_new_simple ("audio/x-raw-int",
655 "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
656 "signed", G_TYPE_BOOLEAN, (width == 8) ? FALSE : TRUE,
657 "width", G_TYPE_INT, width, "depth", G_TYPE_INT, width, NULL);
662 caps = gst_caps_new_empty_simple ("audio/x-nellymoser");
669 if (demux->audio_codec_data)
670 data = gst_buffer_map (demux->audio_codec_data, &size, NULL,
672 /* use codec-data to extract and verify samplerate */
673 if (demux->audio_codec_data && size >= 2) {
676 freq_index = GST_READ_UINT16_BE (data);
677 freq_index = (freq_index & 0x0780) >> 7;
679 gst_codec_utils_aac_get_sample_rate_from_index (freq_index);
681 if (adjusted_rate && (rate != adjusted_rate)) {
682 GST_LOG_OBJECT (demux, "Ajusting AAC sample rate %d -> %d", rate,
685 adjusted_rate = rate;
689 gst_buffer_unmap (demux->audio_codec_data, data, -1);
690 caps = gst_caps_new_simple ("audio/mpeg",
691 "mpegversion", G_TYPE_INT, 4, "framed", G_TYPE_BOOLEAN, TRUE,
692 "stream-format", G_TYPE_STRING, "raw", NULL);
696 caps = gst_caps_new_empty_simple ("audio/x-alaw");
699 caps = gst_caps_new_empty_simple ("audio/x-mulaw");
702 caps = gst_caps_new_empty_simple ("audio/x-speex");
705 GST_WARNING_OBJECT (demux, "unsupported audio codec tag %u", codec_tag);
708 if (G_UNLIKELY (!caps)) {
709 GST_WARNING_OBJECT (demux, "failed creating caps for audio pad");
713 gst_caps_set_simple (caps, "rate", G_TYPE_INT, adjusted_rate,
714 "channels", G_TYPE_INT, channels, NULL);
716 if (demux->audio_codec_data) {
717 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER,
718 demux->audio_codec_data, NULL);
721 ret = gst_pad_set_caps (demux->audio_pad, caps);
723 if (G_LIKELY (ret)) {
724 /* Store the caps we got from tags */
725 demux->audio_codec_tag = codec_tag;
727 demux->channels = channels;
728 demux->width = width;
730 codec_name = gst_pb_utils_get_codec_description (caps);
733 if (demux->taglist == NULL)
734 demux->taglist = gst_tag_list_new_empty ();
735 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
736 GST_TAG_AUDIO_CODEC, codec_name, NULL);
740 GST_DEBUG_OBJECT (demux->audio_pad, "successfully negotiated caps %"
741 GST_PTR_FORMAT, caps);
743 GST_WARNING_OBJECT (demux->audio_pad, "failed negotiating caps %"
744 GST_PTR_FORMAT, caps);
747 gst_caps_unref (caps);
754 gst_flv_demux_push_src_event (GstFlvDemux * demux, GstEvent * event)
758 if (demux->audio_pad)
759 ret |= gst_pad_push_event (demux->audio_pad, gst_event_ref (event));
761 if (demux->video_pad)
762 ret |= gst_pad_push_event (demux->video_pad, gst_event_ref (event));
764 gst_event_unref (event);
770 gst_flv_demux_push_tags (GstFlvDemux * demux)
772 if (demux->has_audio && !demux->audio_pad) {
773 GST_DEBUG_OBJECT (demux,
774 "Waiting for audio stream pad to come up before we can push tags");
777 if (demux->has_video && !demux->video_pad) {
778 GST_DEBUG_OBJECT (demux,
779 "Waiting for video stream pad to come up before we can push tags");
782 if (demux->taglist) {
783 GST_DEBUG_OBJECT (demux, "pushing tags out %" GST_PTR_FORMAT,
785 gst_flv_demux_push_src_event (demux, gst_event_new_tag (demux->taglist));
786 demux->taglist = gst_tag_list_new_empty ();
787 demux->push_tags = FALSE;
792 gst_flv_demux_parse_tag_audio (GstFlvDemux * demux, GstBuffer * buffer)
794 GstFlowReturn ret = GST_FLOW_OK;
795 guint32 pts = 0, codec_tag = 0, rate = 5512, width = 8, channels = 1;
796 guint32 codec_data = 0, pts_ext = 0;
802 GST_LOG_OBJECT (demux, "parsing an audio tag");
804 if (demux->no_more_pads && !demux->audio_pad) {
805 GST_WARNING_OBJECT (demux,
806 "Signaled no-more-pads already but had no audio pad -- ignoring");
810 g_return_val_if_fail (gst_buffer_get_size (buffer) == demux->tag_size,
813 /* Error out on tags with too small headers */
814 if (gst_buffer_get_size (buffer) < 11) {
815 GST_ERROR_OBJECT (demux, "Too small tag size (%d)",
816 gst_buffer_get_size (buffer));
817 return GST_FLOW_ERROR;
820 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
822 /* Grab information about audio tag */
823 pts = GST_READ_UINT24_BE (data);
824 /* read the pts extension to 32 bits integer */
825 pts_ext = GST_READ_UINT8 (data + 3);
827 pts |= pts_ext << 24;
829 GST_LOG_OBJECT (demux, "pts bytes %02X %02X %02X %02X (%d)", data[0], data[1],
830 data[2], data[3], pts);
832 /* Skip the stream id and go directly to the flags */
833 flags = GST_READ_UINT8 (data + 7);
835 /* Silently skip buffers with no data */
848 if ((flags & 0x0C) == 0x0C) {
850 } else if ((flags & 0x0C) == 0x08) {
852 } else if ((flags & 0x0C) == 0x04) {
856 codec_tag = flags >> 4;
857 if (codec_tag == 10) { /* AAC has an extra byte for packet type */
863 /* codec tags with special rates */
864 if (codec_tag == 5 || codec_tag == 14)
866 else if (codec_tag == 4)
869 GST_LOG_OBJECT (demux, "audio tag with %d channels, %dHz sampling rate, "
870 "%d bits width, codec tag %u (flags %02X)", channels, rate, width,
873 /* If we don't have our audio pad created, then create it. */
874 if (G_UNLIKELY (!demux->audio_pad)) {
877 gst_pad_new_from_template (gst_element_class_get_pad_template
878 (GST_ELEMENT_GET_CLASS (demux), "audio"), "audio");
879 if (G_UNLIKELY (!demux->audio_pad)) {
880 GST_WARNING_OBJECT (demux, "failed creating audio pad");
881 ret = GST_FLOW_ERROR;
886 if (!gst_flv_demux_audio_negotiate (demux, codec_tag, rate, channels,
888 gst_object_unref (demux->audio_pad);
889 demux->audio_pad = NULL;
890 ret = GST_FLOW_ERROR;
893 #ifndef GST_DISABLE_GST_DEBUG
897 caps = gst_pad_get_current_caps (demux->audio_pad);
898 GST_DEBUG_OBJECT (demux, "created audio pad with caps %" GST_PTR_FORMAT,
901 gst_caps_unref (caps);
905 /* Set functions on the pad */
906 gst_pad_set_query_type_function (demux->audio_pad,
907 GST_DEBUG_FUNCPTR (gst_flv_demux_query_types));
908 gst_pad_set_query_function (demux->audio_pad,
909 GST_DEBUG_FUNCPTR (gst_flv_demux_query));
910 gst_pad_set_event_function (demux->audio_pad,
911 GST_DEBUG_FUNCPTR (gst_flv_demux_src_event));
913 gst_pad_use_fixed_caps (demux->audio_pad);
916 gst_pad_set_active (demux->audio_pad, TRUE);
918 /* We need to set caps before adding */
919 gst_element_add_pad (GST_ELEMENT (demux),
920 gst_object_ref (demux->audio_pad));
922 /* We only emit no more pads when we have audio and video. Indeed we can
923 * not trust the FLV header to tell us if there will be only audio or
924 * only video and we would just break discovery of some files */
925 if (demux->audio_pad && demux->video_pad) {
926 GST_DEBUG_OBJECT (demux, "emitting no more pads");
927 gst_element_no_more_pads (GST_ELEMENT (demux));
928 demux->no_more_pads = TRUE;
929 demux->push_tags = TRUE;
933 /* Check if caps have changed */
934 if (G_UNLIKELY (rate != demux->rate || channels != demux->channels ||
935 codec_tag != demux->audio_codec_tag || width != demux->width)) {
936 GST_DEBUG_OBJECT (demux, "audio settings have changed, changing caps");
939 if (!gst_flv_demux_audio_negotiate (demux, codec_tag, rate, channels,
941 ret = GST_FLOW_ERROR;
946 /* Push taglist if present */
947 if (G_UNLIKELY (demux->push_tags))
948 gst_flv_demux_push_tags (demux);
950 /* Check if we have anything to push */
951 if (demux->tag_data_size <= codec_data) {
952 GST_LOG_OBJECT (demux, "Nothing left in this tag, returning");
956 /* Create buffer from pad */
957 outbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY,
958 7 + codec_data, demux->tag_data_size - codec_data);
960 if (demux->audio_codec_tag == 10) {
961 guint8 aac_packet_type = GST_READ_UINT8 (data + 8);
963 switch (aac_packet_type) {
966 /* AudioSpecificConfig data */
967 GST_LOG_OBJECT (demux, "got an AAC codec data packet");
968 if (demux->audio_codec_data) {
969 gst_buffer_unref (demux->audio_codec_data);
971 demux->audio_codec_data = outbuf;
972 /* Use that buffer data in the caps */
973 gst_flv_demux_audio_negotiate (demux, codec_tag, rate, channels, width);
979 GST_LOG_OBJECT (demux, "got a raw AAC audio packet");
982 GST_WARNING_OBJECT (demux, "invalid AAC packet type %u",
987 /* Fill buffer with data */
988 GST_BUFFER_TIMESTAMP (outbuf) = pts * GST_MSECOND;
989 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
990 GST_BUFFER_OFFSET (outbuf) = demux->audio_offset++;
991 GST_BUFFER_OFFSET_END (outbuf) = demux->audio_offset;
993 if (demux->duration == GST_CLOCK_TIME_NONE ||
994 demux->duration < GST_BUFFER_TIMESTAMP (outbuf))
995 demux->duration = GST_BUFFER_TIMESTAMP (outbuf);
997 /* Only add audio frames to the index if we have no video,
998 * and if the index is not yet complete */
999 if (!demux->has_video && demux->index && !demux->indexed) {
1000 gst_flv_demux_parse_and_add_index_entry (demux,
1001 GST_BUFFER_TIMESTAMP (outbuf), demux->cur_tag_offset, TRUE);
1004 if (G_UNLIKELY (demux->audio_need_discont)) {
1005 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1006 demux->audio_need_discont = FALSE;
1009 demux->segment.position = GST_BUFFER_TIMESTAMP (outbuf);
1011 /* Do we need a newsegment event ? */
1012 if (G_UNLIKELY (demux->audio_need_segment)) {
1013 /* FIXME need one segment sent for all stream to maintain a/v sync */
1014 if (!demux->new_seg_event) {
1015 GST_DEBUG_OBJECT (demux, "pushing newsegment from %"
1016 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
1017 GST_TIME_ARGS (demux->segment.position),
1018 GST_TIME_ARGS (demux->segment.stop));
1019 demux->segment.start = demux->segment.time = demux->segment.position;
1020 demux->new_seg_event = gst_event_new_segment (&demux->segment);
1022 GST_DEBUG_OBJECT (demux, "pushing pre-generated newsegment event");
1025 gst_pad_push_event (demux->audio_pad, gst_event_ref (demux->new_seg_event));
1027 demux->audio_need_segment = FALSE;
1030 GST_LOG_OBJECT (demux, "pushing %d bytes buffer at pts %" GST_TIME_FORMAT
1031 " with duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT,
1032 gst_buffer_get_size (outbuf),
1033 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1034 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf));
1036 if (!GST_CLOCK_TIME_IS_VALID (demux->audio_start)) {
1037 demux->audio_start = GST_BUFFER_TIMESTAMP (outbuf);
1039 if (!GST_CLOCK_TIME_IS_VALID (demux->audio_first_ts)) {
1040 demux->audio_first_ts = GST_BUFFER_TIMESTAMP (outbuf);
1043 if (G_UNLIKELY (!demux->no_more_pads
1044 && (GST_CLOCK_DIFF (demux->audio_start,
1045 GST_BUFFER_TIMESTAMP (outbuf)) > 6 * GST_SECOND))) {
1046 GST_DEBUG_OBJECT (demux,
1047 "Signalling no-more-pads because no video stream was found"
1048 " after 6 seconds of audio");
1049 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
1050 demux->no_more_pads = TRUE;
1051 demux->push_tags = TRUE;
1054 /* Push downstream */
1055 ret = gst_pad_push (demux->audio_pad, outbuf);
1056 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1057 if (demux->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
1058 demux->segment.position > demux->segment.stop) {
1059 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
1060 * we are at the end of the segment, so we just need to jump
1061 * back to the previous section. */
1062 GST_DEBUG_OBJECT (demux, "downstream has reached end of segment");
1063 demux->audio_done = TRUE;
1066 GST_WARNING_OBJECT (demux, "failed pushing a %" G_GUINT64_FORMAT
1067 " bytes audio buffer: %s", demux->tag_data_size,
1068 gst_flow_get_name (ret));
1069 if (ret == GST_FLOW_NOT_LINKED) {
1070 demux->audio_linked = FALSE;
1076 demux->audio_linked = TRUE;
1079 gst_buffer_unmap (buffer, data, -1);
1085 gst_flv_demux_video_negotiate (GstFlvDemux * demux, guint32 codec_tag)
1087 gboolean ret = FALSE;
1088 GstCaps *caps = NULL;
1089 gchar *codec_name = NULL;
1091 /* Generate caps for that pad */
1092 switch (codec_tag) {
1094 caps = gst_caps_new_empty_simple ("video/x-flash-video");
1097 caps = gst_caps_new_empty_simple ("video/x-flash-screen");
1100 caps = gst_caps_new_empty_simple ("video/x-vp6-flash");
1103 caps = gst_caps_new_empty_simple ("video/x-vp6-alpha");
1107 gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
1111 GST_WARNING_OBJECT (demux, "unsupported video codec tag %u", codec_tag);
1114 if (G_UNLIKELY (!caps)) {
1115 GST_WARNING_OBJECT (demux, "failed creating caps for video pad");
1119 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1120 demux->par_x, demux->par_y, NULL);
1122 if (G_LIKELY (demux->w)) {
1123 gst_caps_set_simple (caps, "width", G_TYPE_INT, demux->w, NULL);
1126 if (G_LIKELY (demux->h)) {
1127 gst_caps_set_simple (caps, "height", G_TYPE_INT, demux->h, NULL);
1130 if (G_LIKELY (demux->framerate)) {
1131 gint num = 0, den = 0;
1133 gst_util_double_to_fraction (demux->framerate, &num, &den);
1134 GST_DEBUG_OBJECT (demux->video_pad,
1135 "fps to be used on caps %f (as a fraction = %d/%d)", demux->framerate,
1138 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, num, den, NULL);
1141 if (demux->video_codec_data) {
1142 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER,
1143 demux->video_codec_data, NULL);
1146 ret = gst_pad_set_caps (demux->video_pad, caps);
1148 if (G_LIKELY (ret)) {
1149 /* Store the caps we have set */
1150 demux->video_codec_tag = codec_tag;
1152 codec_name = gst_pb_utils_get_codec_description (caps);
1155 if (demux->taglist == NULL)
1156 demux->taglist = gst_tag_list_new_empty ();
1157 gst_tag_list_add (demux->taglist, GST_TAG_MERGE_REPLACE,
1158 GST_TAG_VIDEO_CODEC, codec_name, NULL);
1159 g_free (codec_name);
1162 GST_DEBUG_OBJECT (demux->video_pad, "successfully negotiated caps %"
1163 GST_PTR_FORMAT, caps);
1165 GST_WARNING_OBJECT (demux->video_pad, "failed negotiating caps %"
1166 GST_PTR_FORMAT, caps);
1169 gst_caps_unref (caps);
1175 static GstFlowReturn
1176 gst_flv_demux_parse_tag_video (GstFlvDemux * demux, GstBuffer * buffer)
1178 GstFlowReturn ret = GST_FLOW_OK;
1179 guint32 pts = 0, codec_data = 1, pts_ext = 0;
1180 gboolean keyframe = FALSE;
1181 guint8 flags = 0, codec_tag = 0;
1186 g_return_val_if_fail (gst_buffer_get_size (buffer) == demux->tag_size,
1189 GST_LOG_OBJECT (demux, "parsing a video tag");
1191 if (demux->no_more_pads && !demux->video_pad) {
1192 GST_WARNING_OBJECT (demux,
1193 "Signaled no-more-pads already but had no audio pad -- ignoring");
1197 if (gst_buffer_get_size (buffer) < 12) {
1198 GST_ERROR_OBJECT (demux, "Too small tag size");
1199 return GST_FLOW_ERROR;
1202 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1204 /* Grab information about video tag */
1205 pts = GST_READ_UINT24_BE (data);
1206 /* read the pts extension to 32 bits integer */
1207 pts_ext = GST_READ_UINT8 (data + 3);
1209 pts |= pts_ext << 24;
1211 GST_LOG_OBJECT (demux, "pts bytes %02X %02X %02X %02X (%d)", data[0], data[1],
1212 data[2], data[3], pts);
1214 /* Skip the stream id and go directly to the flags */
1215 flags = GST_READ_UINT8 (data + 7);
1218 if ((flags >> 4) == 1) {
1222 codec_tag = flags & 0x0F;
1223 if (codec_tag == 4 || codec_tag == 5) {
1225 } else if (codec_tag == 7) {
1230 cts = GST_READ_UINT24_BE (data + 9);
1231 cts = (cts + 0xff800000) ^ 0xff800000;
1233 GST_LOG_OBJECT (demux, "got cts %d", cts);
1235 /* avoid negative overflow */
1236 if (cts >= 0 || pts >= -cts)
1240 GST_LOG_OBJECT (demux, "video tag with codec tag %u, keyframe (%d) "
1241 "(flags %02X)", codec_tag, keyframe, flags);
1243 /* If we don't have our video pad created, then create it. */
1244 if (G_UNLIKELY (!demux->video_pad)) {
1246 gst_pad_new_from_template (gst_element_class_get_pad_template
1247 (GST_ELEMENT_GET_CLASS (demux), "video"), "video");
1248 if (G_UNLIKELY (!demux->video_pad)) {
1249 GST_WARNING_OBJECT (demux, "failed creating video pad");
1250 ret = GST_FLOW_ERROR;
1254 if (!gst_flv_demux_video_negotiate (demux, codec_tag)) {
1255 gst_object_unref (demux->video_pad);
1256 demux->video_pad = NULL;
1257 ret = GST_FLOW_ERROR;
1261 /* When we ve set pixel-aspect-ratio we use that boolean to detect a
1262 * metadata tag that would come later and trigger a caps change */
1263 demux->got_par = FALSE;
1265 #ifndef GST_DISABLE_GST_DEBUG
1269 caps = gst_pad_get_current_caps (demux->video_pad);
1270 GST_DEBUG_OBJECT (demux, "created video pad with caps %" GST_PTR_FORMAT,
1273 gst_caps_unref (caps);
1277 /* Set functions on the pad */
1278 gst_pad_set_query_type_function (demux->video_pad,
1279 GST_DEBUG_FUNCPTR (gst_flv_demux_query_types));
1280 gst_pad_set_query_function (demux->video_pad,
1281 GST_DEBUG_FUNCPTR (gst_flv_demux_query));
1282 gst_pad_set_event_function (demux->video_pad,
1283 GST_DEBUG_FUNCPTR (gst_flv_demux_src_event));
1285 gst_pad_use_fixed_caps (demux->video_pad);
1287 /* Make it active */
1288 gst_pad_set_active (demux->video_pad, TRUE);
1290 /* We need to set caps before adding */
1291 gst_element_add_pad (GST_ELEMENT (demux),
1292 gst_object_ref (demux->video_pad));
1294 /* We only emit no more pads when we have audio and video. Indeed we can
1295 * not trust the FLV header to tell us if there will be only audio or
1296 * only video and we would just break discovery of some files */
1297 if (demux->audio_pad && demux->video_pad) {
1298 GST_DEBUG_OBJECT (demux, "emitting no more pads");
1299 gst_element_no_more_pads (GST_ELEMENT (demux));
1300 demux->no_more_pads = TRUE;
1301 demux->push_tags = TRUE;
1305 /* Check if caps have changed */
1306 if (G_UNLIKELY (codec_tag != demux->video_codec_tag || demux->got_par)) {
1308 GST_DEBUG_OBJECT (demux, "video settings have changed, changing caps");
1310 if (!gst_flv_demux_video_negotiate (demux, codec_tag)) {
1311 ret = GST_FLOW_ERROR;
1315 /* When we ve set pixel-aspect-ratio we use that boolean to detect a
1316 * metadata tag that would come later and trigger a caps change */
1317 demux->got_par = FALSE;
1320 /* Push taglist if present */
1321 if (G_UNLIKELY (demux->push_tags))
1322 gst_flv_demux_push_tags (demux);
1324 /* Check if we have anything to push */
1325 if (demux->tag_data_size <= codec_data) {
1326 GST_LOG_OBJECT (demux, "Nothing left in this tag, returning");
1330 /* Create buffer from pad */
1331 outbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY,
1332 7 + codec_data, demux->tag_data_size - codec_data);
1334 if (demux->video_codec_tag == 7) {
1335 guint8 avc_packet_type = GST_READ_UINT8 (data + 8);
1337 switch (avc_packet_type) {
1340 /* AVCDecoderConfigurationRecord data */
1341 GST_LOG_OBJECT (demux, "got an H.264 codec data packet");
1342 if (demux->video_codec_data) {
1343 gst_buffer_unref (demux->video_codec_data);
1345 demux->video_codec_data = outbuf;
1346 /* Use that buffer data in the caps */
1347 gst_flv_demux_video_negotiate (demux, codec_tag);
1352 /* H.264 NALU packet */
1353 GST_LOG_OBJECT (demux, "got a H.264 NALU video packet");
1356 GST_WARNING_OBJECT (demux, "invalid video packet type %u",
1361 /* Fill buffer with data */
1362 GST_BUFFER_TIMESTAMP (outbuf) = pts * GST_MSECOND;
1363 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
1364 GST_BUFFER_OFFSET (outbuf) = demux->video_offset++;
1365 GST_BUFFER_OFFSET_END (outbuf) = demux->video_offset;
1367 if (demux->duration == GST_CLOCK_TIME_NONE ||
1368 demux->duration < GST_BUFFER_TIMESTAMP (outbuf))
1369 demux->duration = GST_BUFFER_TIMESTAMP (outbuf);
1372 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1374 if (!demux->indexed && demux->index) {
1375 gst_flv_demux_parse_and_add_index_entry (demux,
1376 GST_BUFFER_TIMESTAMP (outbuf), demux->cur_tag_offset, keyframe);
1379 if (G_UNLIKELY (demux->video_need_discont)) {
1380 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1381 demux->video_need_discont = FALSE;
1384 demux->segment.position = GST_BUFFER_TIMESTAMP (outbuf);
1386 /* Do we need a newsegment event ? */
1387 if (G_UNLIKELY (demux->video_need_segment)) {
1388 /* FIXME need one segment sent for all stream to maintain a/v sync */
1389 if (!demux->new_seg_event) {
1390 GST_DEBUG_OBJECT (demux, "pushing newsegment from %"
1391 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
1392 GST_TIME_ARGS (demux->segment.position),
1393 GST_TIME_ARGS (demux->segment.stop));
1394 demux->segment.start = demux->segment.time = demux->segment.position;
1395 demux->new_seg_event = gst_event_new_segment (&demux->segment);
1397 GST_DEBUG_OBJECT (demux, "pushing pre-generated newsegment event");
1400 gst_pad_push_event (demux->video_pad, gst_event_ref (demux->new_seg_event));
1402 demux->video_need_segment = FALSE;
1405 GST_LOG_OBJECT (demux, "pushing %d bytes buffer at pts %" GST_TIME_FORMAT
1406 " with duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
1407 ", keyframe (%d)", gst_buffer_get_size (outbuf),
1408 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1409 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
1412 if (!GST_CLOCK_TIME_IS_VALID (demux->video_start)) {
1413 demux->video_start = GST_BUFFER_TIMESTAMP (outbuf);
1415 if (!GST_CLOCK_TIME_IS_VALID (demux->audio_first_ts)) {
1416 demux->video_first_ts = GST_BUFFER_TIMESTAMP (outbuf);
1419 if (G_UNLIKELY (!demux->no_more_pads
1420 && (GST_CLOCK_DIFF (demux->video_start,
1421 GST_BUFFER_TIMESTAMP (outbuf)) > 6 * GST_SECOND))) {
1422 GST_DEBUG_OBJECT (demux,
1423 "Signalling no-more-pads because no audio stream was found"
1424 " after 6 seconds of video");
1425 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
1426 demux->no_more_pads = TRUE;
1427 demux->push_tags = TRUE;
1430 /* Push downstream */
1431 ret = gst_pad_push (demux->video_pad, outbuf);
1433 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1434 if (demux->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
1435 demux->segment.position > demux->segment.stop) {
1436 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
1437 * we are at the end of the segment, so we just need to jump
1438 * back to the previous section. */
1439 GST_DEBUG_OBJECT (demux, "downstream has reached end of segment");
1440 demux->video_done = TRUE;
1443 GST_WARNING_OBJECT (demux, "failed pushing a %" G_GUINT64_FORMAT
1444 " bytes video buffer: %s", demux->tag_data_size,
1445 gst_flow_get_name (ret));
1446 if (ret == GST_FLOW_NOT_LINKED) {
1447 demux->video_linked = FALSE;
1453 demux->video_linked = TRUE;
1456 gst_buffer_unmap (buffer, data, -1);
1461 gst_flv_demux_parse_tag_timestamp (GstFlvDemux * demux, gboolean index,
1462 GstBuffer * buffer, size_t * tag_size)
1464 guint32 pts = 0, pts_ext = 0;
1465 guint32 tag_data_size;
1467 gboolean keyframe = TRUE;
1468 GstClockTime ret = GST_CLOCK_TIME_NONE;
1469 guint8 *data, *bdata;
1472 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 12,
1473 GST_CLOCK_TIME_NONE);
1475 data = bdata = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1479 if (type != 9 && type != 8 && type != 18) {
1480 GST_WARNING_OBJECT (demux, "Unsupported tag type %u", data[0]);
1485 demux->has_video = TRUE;
1487 demux->has_audio = TRUE;
1489 tag_data_size = GST_READ_UINT24_BE (data + 1);
1491 if (size >= tag_data_size + 11 + 4) {
1492 if (GST_READ_UINT32_BE (data + tag_data_size + 11) != tag_data_size + 11) {
1493 GST_WARNING_OBJECT (demux, "Invalid tag size");
1499 *tag_size = tag_data_size + 11 + 4;
1503 GST_LOG_OBJECT (demux, "pts bytes %02X %02X %02X %02X", data[0], data[1],
1506 /* Grab timestamp of tag tag */
1507 pts = GST_READ_UINT24_BE (data);
1508 /* read the pts extension to 32 bits integer */
1509 pts_ext = GST_READ_UINT8 (data + 3);
1511 pts |= pts_ext << 24;
1516 keyframe = ((data[0] >> 4) == 1);
1519 ret = pts * GST_MSECOND;
1520 GST_LOG_OBJECT (demux, "pts: %" GST_TIME_FORMAT, GST_TIME_ARGS (ret));
1522 if (index && demux->index && !demux->indexed && (type == 9 || (type == 8
1523 && !demux->has_video))) {
1524 gst_flv_demux_parse_and_add_index_entry (demux, ret, demux->offset,
1528 if (demux->duration == GST_CLOCK_TIME_NONE || demux->duration < ret)
1529 demux->duration = ret;
1532 gst_buffer_unmap (buffer, bdata, -1);
1536 static GstFlowReturn
1537 gst_flv_demux_parse_tag_type (GstFlvDemux * demux, GstBuffer * buffer)
1539 GstFlowReturn ret = GST_FLOW_OK;
1540 guint8 tag_type = 0;
1543 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 4, GST_FLOW_ERROR);
1545 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
1551 demux->state = FLV_STATE_TAG_VIDEO;
1552 demux->has_video = TRUE;
1555 demux->state = FLV_STATE_TAG_AUDIO;
1556 demux->has_audio = TRUE;
1559 demux->state = FLV_STATE_TAG_SCRIPT;
1562 GST_WARNING_OBJECT (demux, "unsupported tag type %u", tag_type);
1565 /* Tag size is 1 byte of type + 3 bytes of size + 7 bytes + tag data size +
1566 * 4 bytes of previous tag size */
1567 demux->tag_data_size = GST_READ_UINT24_BE (data + 1);
1568 demux->tag_size = demux->tag_data_size + 11;
1570 GST_LOG_OBJECT (demux, "tag data size is %" G_GUINT64_FORMAT,
1571 demux->tag_data_size);
1573 gst_buffer_unmap (buffer, data, -1);
1578 static GstFlowReturn
1579 gst_flv_demux_parse_header (GstFlvDemux * demux, GstBuffer * buffer)
1581 GstFlowReturn ret = GST_FLOW_OK;
1584 g_return_val_if_fail (gst_buffer_get_size (buffer) >= 9, GST_FLOW_ERROR);
1586 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
1588 /* Check for the FLV tag */
1589 if (data[0] == 'F' && data[1] == 'L' && data[2] == 'V') {
1590 GST_DEBUG_OBJECT (demux, "FLV header detected");
1592 if (G_UNLIKELY (demux->strict)) {
1593 GST_WARNING_OBJECT (demux, "invalid header tag detected");
1594 ret = GST_FLOW_UNEXPECTED;
1599 /* Now look at audio/video flags */
1601 guint8 flags = data[4];
1603 demux->has_video = demux->has_audio = FALSE;
1606 GST_DEBUG_OBJECT (demux, "there is a video stream");
1607 demux->has_video = TRUE;
1610 GST_DEBUG_OBJECT (demux, "there is an audio stream");
1611 demux->has_audio = TRUE;
1615 /* do a one-time seekability check */
1616 gst_flv_demux_check_seekability (demux);
1618 /* We don't care about the rest */
1619 demux->need_header = FALSE;
1622 gst_buffer_unmap (buffer, data, -1);
1628 gst_flv_demux_flush (GstFlvDemux * demux, gboolean discont)
1630 GST_DEBUG_OBJECT (demux, "flushing queued data in the FLV demuxer");
1632 gst_adapter_clear (demux->adapter);
1634 demux->audio_need_discont = TRUE;
1635 demux->video_need_discont = TRUE;
1637 demux->flushing = FALSE;
1639 /* Only in push mode and if we're not during a seek */
1640 if (!demux->random_access && demux->state != FLV_STATE_SEEK) {
1641 /* After a flush we expect a tag_type */
1642 demux->state = FLV_STATE_TAG_TYPE;
1643 /* We reset the offset and will get one from first push */
1649 gst_flv_demux_cleanup (GstFlvDemux * demux)
1651 GST_DEBUG_OBJECT (demux, "cleaning up FLV demuxer");
1653 demux->state = FLV_STATE_HEADER;
1655 demux->flushing = FALSE;
1656 demux->need_header = TRUE;
1657 demux->audio_need_segment = TRUE;
1658 demux->video_need_segment = TRUE;
1659 demux->audio_need_discont = TRUE;
1660 demux->video_need_discont = TRUE;
1662 /* By default we consider them as linked */
1663 demux->audio_linked = TRUE;
1664 demux->video_linked = TRUE;
1666 demux->has_audio = FALSE;
1667 demux->has_video = FALSE;
1668 demux->push_tags = FALSE;
1669 demux->got_par = FALSE;
1671 demux->indexed = FALSE;
1672 demux->upstream_seekable = FALSE;
1673 demux->file_size = 0;
1675 demux->index_max_pos = 0;
1676 demux->index_max_time = 0;
1678 demux->audio_start = demux->video_start = GST_CLOCK_TIME_NONE;
1680 demux->no_more_pads = FALSE;
1682 gst_segment_init (&demux->segment, GST_FORMAT_TIME);
1684 demux->w = demux->h = 0;
1685 demux->framerate = 0.0;
1686 demux->par_x = demux->par_y = 1;
1687 demux->video_offset = 0;
1688 demux->audio_offset = 0;
1689 demux->offset = demux->cur_tag_offset = 0;
1690 demux->tag_size = demux->tag_data_size = 0;
1691 demux->duration = GST_CLOCK_TIME_NONE;
1693 if (demux->new_seg_event) {
1694 gst_event_unref (demux->new_seg_event);
1695 demux->new_seg_event = NULL;
1698 gst_adapter_clear (demux->adapter);
1700 if (demux->audio_codec_data) {
1701 gst_buffer_unref (demux->audio_codec_data);
1702 demux->audio_codec_data = NULL;
1705 if (demux->video_codec_data) {
1706 gst_buffer_unref (demux->video_codec_data);
1707 demux->video_codec_data = NULL;
1710 if (demux->audio_pad) {
1711 gst_element_remove_pad (GST_ELEMENT (demux), demux->audio_pad);
1712 gst_object_unref (demux->audio_pad);
1713 demux->audio_pad = NULL;
1716 if (demux->video_pad) {
1717 gst_element_remove_pad (GST_ELEMENT (demux), demux->video_pad);
1718 gst_object_unref (demux->video_pad);
1719 demux->video_pad = NULL;
1723 g_array_free (demux->times, TRUE);
1724 demux->times = NULL;
1727 if (demux->filepositions) {
1728 g_array_free (demux->filepositions, TRUE);
1729 demux->filepositions = NULL;
1734 * Create and push a flushing seek event upstream
1737 flv_demux_seek_to_offset (GstFlvDemux * demux, guint64 offset)
1742 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1745 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1746 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1747 GST_SEEK_TYPE_NONE, -1);
1749 res = gst_pad_push_event (demux->sinkpad, event);
1752 demux->offset = offset;
1756 static GstFlowReturn
1757 gst_flv_demux_chain (GstPad * pad, GstBuffer * buffer)
1759 GstFlowReturn ret = GST_FLOW_OK;
1760 GstFlvDemux *demux = NULL;
1762 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
1764 GST_LOG_OBJECT (demux, "received buffer of %d bytes at offset %"
1765 G_GUINT64_FORMAT, gst_buffer_get_size (buffer),
1766 GST_BUFFER_OFFSET (buffer));
1768 if (G_UNLIKELY (GST_BUFFER_OFFSET (buffer) == 0)) {
1769 GST_DEBUG_OBJECT (demux, "beginning of file, expect header");
1770 demux->state = FLV_STATE_HEADER;
1774 if (G_UNLIKELY (demux->offset == 0 && GST_BUFFER_OFFSET (buffer) != 0)) {
1775 GST_DEBUG_OBJECT (demux, "offset was zero, synchronizing with buffer's");
1776 demux->offset = GST_BUFFER_OFFSET (buffer);
1779 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
1780 GST_DEBUG_OBJECT (demux, "Discontinuity");
1781 gst_adapter_clear (demux->adapter);
1784 gst_adapter_push (demux->adapter, buffer);
1786 if (demux->seeking) {
1787 demux->state = FLV_STATE_SEEK;
1788 GST_OBJECT_LOCK (demux);
1789 demux->seeking = FALSE;
1790 GST_OBJECT_UNLOCK (demux);
1794 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1795 if (ret == GST_FLOW_NOT_LINKED && (demux->audio_linked
1796 || demux->video_linked)) {
1799 GST_DEBUG_OBJECT (demux, "got flow return %s", gst_flow_get_name (ret));
1804 if (G_UNLIKELY (demux->flushing)) {
1805 GST_DEBUG_OBJECT (demux, "we are now flushing, exiting parser loop");
1806 ret = GST_FLOW_WRONG_STATE;
1810 switch (demux->state) {
1811 case FLV_STATE_HEADER:
1813 if (gst_adapter_available (demux->adapter) >= FLV_HEADER_SIZE) {
1816 buffer = gst_adapter_take_buffer (demux->adapter, FLV_HEADER_SIZE);
1818 ret = gst_flv_demux_parse_header (demux, buffer);
1820 gst_buffer_unref (buffer);
1821 demux->offset += FLV_HEADER_SIZE;
1823 demux->state = FLV_STATE_TAG_TYPE;
1829 case FLV_STATE_TAG_TYPE:
1831 if (gst_adapter_available (demux->adapter) >= FLV_TAG_TYPE_SIZE) {
1834 /* Remember the tag offset in bytes */
1835 demux->cur_tag_offset = demux->offset;
1837 buffer = gst_adapter_take_buffer (demux->adapter, FLV_TAG_TYPE_SIZE);
1839 ret = gst_flv_demux_parse_tag_type (demux, buffer);
1841 gst_buffer_unref (buffer);
1842 demux->offset += FLV_TAG_TYPE_SIZE;
1844 /* last tag is not an index => no index/don't know where the index is
1845 * seek back to the beginning */
1846 if (demux->seek_event && demux->state != FLV_STATE_TAG_SCRIPT)
1854 case FLV_STATE_TAG_VIDEO:
1856 if (gst_adapter_available (demux->adapter) >= demux->tag_size) {
1859 buffer = gst_adapter_take_buffer (demux->adapter, demux->tag_size);
1861 ret = gst_flv_demux_parse_tag_video (demux, buffer);
1863 gst_buffer_unref (buffer);
1864 demux->offset += demux->tag_size;
1866 demux->state = FLV_STATE_TAG_TYPE;
1872 case FLV_STATE_TAG_AUDIO:
1874 if (gst_adapter_available (demux->adapter) >= demux->tag_size) {
1877 buffer = gst_adapter_take_buffer (demux->adapter, demux->tag_size);
1879 ret = gst_flv_demux_parse_tag_audio (demux, buffer);
1881 gst_buffer_unref (buffer);
1882 demux->offset += demux->tag_size;
1884 demux->state = FLV_STATE_TAG_TYPE;
1890 case FLV_STATE_TAG_SCRIPT:
1892 if (gst_adapter_available (demux->adapter) >= demux->tag_size) {
1895 buffer = gst_adapter_take_buffer (demux->adapter, demux->tag_size);
1897 ret = gst_flv_demux_parse_tag_script (demux, buffer);
1899 gst_buffer_unref (buffer);
1900 demux->offset += demux->tag_size;
1902 demux->state = FLV_STATE_TAG_TYPE;
1904 /* if there's a seek event we're here for the index so if we don't have it
1905 * we seek back to the beginning */
1906 if (demux->seek_event) {
1908 demux->state = FLV_STATE_SEEK;
1918 case FLV_STATE_SEEK:
1924 if (!demux->indexed) {
1925 if (demux->offset == demux->file_size - sizeof (guint32)) {
1926 guint64 seek_offset;
1929 data = gst_adapter_take (demux->adapter, 4);
1933 seek_offset = demux->file_size - sizeof (guint32) -
1934 GST_READ_UINT32_BE (data);
1937 GST_INFO_OBJECT (demux,
1938 "Seeking to beginning of last tag at %" G_GUINT64_FORMAT,
1940 demux->state = FLV_STATE_TAG_TYPE;
1941 flv_demux_seek_to_offset (demux, seek_offset);
1947 GST_OBJECT_LOCK (demux);
1948 event = demux->seek_event;
1949 demux->seek_event = NULL;
1950 GST_OBJECT_UNLOCK (demux);
1952 /* calculate and perform seek */
1953 if (!flv_demux_handle_seek_push (demux, event))
1956 gst_event_unref (event);
1957 demux->state = FLV_STATE_TAG_TYPE;
1961 GST_DEBUG_OBJECT (demux, "unexpected demuxer state");
1965 if (G_UNLIKELY (ret == GST_FLOW_NOT_LINKED)) {
1966 /* If either audio or video is linked we return GST_FLOW_OK */
1967 if (demux->audio_linked || demux->video_linked) {
1972 gst_object_unref (demux);
1979 GST_OBJECT_LOCK (demux);
1980 demux->seeking = FALSE;
1981 gst_event_unref (demux->seek_event);
1982 demux->seek_event = NULL;
1983 GST_OBJECT_UNLOCK (demux);
1984 GST_WARNING_OBJECT (demux,
1985 "failed to find an index, seeking back to beginning");
1986 flv_demux_seek_to_offset (demux, 0);
1991 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("seek failed"));
1992 return GST_FLOW_ERROR;
1997 static GstFlowReturn
1998 gst_flv_demux_pull_range (GstFlvDemux * demux, GstPad * pad, guint64 offset,
1999 guint size, GstBuffer ** buffer)
2003 ret = gst_pad_pull_range (pad, offset, size, buffer);
2004 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2005 GST_WARNING_OBJECT (demux,
2006 "failed when pulling %d bytes from offset %" G_GUINT64_FORMAT ": %s",
2007 size, offset, gst_flow_get_name (ret));
2012 if (G_UNLIKELY (*buffer && gst_buffer_get_size (*buffer) != size)) {
2013 GST_WARNING_OBJECT (demux,
2014 "partial pull got %d when expecting %d from offset %" G_GUINT64_FORMAT,
2015 gst_buffer_get_size (*buffer), size, offset);
2016 gst_buffer_unref (*buffer);
2017 ret = GST_FLOW_UNEXPECTED;
2025 static GstFlowReturn
2026 gst_flv_demux_pull_tag (GstPad * pad, GstFlvDemux * demux)
2028 GstBuffer *buffer = NULL;
2029 GstFlowReturn ret = GST_FLOW_OK;
2031 /* Store tag offset */
2032 demux->cur_tag_offset = demux->offset;
2034 /* Get the first 4 bytes to identify tag type and size */
2035 if (G_UNLIKELY ((ret = gst_flv_demux_pull_range (demux, pad, demux->offset,
2036 FLV_TAG_TYPE_SIZE, &buffer)) != GST_FLOW_OK))
2039 /* Identify tag type */
2040 ret = gst_flv_demux_parse_tag_type (demux, buffer);
2042 gst_buffer_unref (buffer);
2044 if (G_UNLIKELY (ret != GST_FLOW_OK))
2047 /* Jump over tag type + size */
2048 demux->offset += FLV_TAG_TYPE_SIZE;
2050 /* Pull the whole tag */
2051 if (G_UNLIKELY ((ret = gst_flv_demux_pull_range (demux, pad, demux->offset,
2052 demux->tag_size, &buffer)) != GST_FLOW_OK))
2055 switch (demux->state) {
2056 case FLV_STATE_TAG_VIDEO:
2057 ret = gst_flv_demux_parse_tag_video (demux, buffer);
2059 case FLV_STATE_TAG_AUDIO:
2060 ret = gst_flv_demux_parse_tag_audio (demux, buffer);
2062 case FLV_STATE_TAG_SCRIPT:
2063 ret = gst_flv_demux_parse_tag_script (demux, buffer);
2066 GST_WARNING_OBJECT (demux, "unexpected state %d", demux->state);
2069 gst_buffer_unref (buffer);
2071 /* Jump over that part we've just parsed */
2072 demux->offset += demux->tag_size;
2074 /* Make sure we reinitialize the tag size */
2075 demux->tag_size = 0;
2077 /* Ready for the next tag */
2078 demux->state = FLV_STATE_TAG_TYPE;
2080 if (G_UNLIKELY (ret == GST_FLOW_NOT_LINKED)) {
2081 /* If either audio or video is linked we return GST_FLOW_OK */
2082 if (demux->audio_linked || demux->video_linked) {
2085 GST_WARNING_OBJECT (demux, "parsing this tag returned not-linked and "
2086 "neither video nor audio are linked");
2094 static GstFlowReturn
2095 gst_flv_demux_pull_header (GstPad * pad, GstFlvDemux * demux)
2097 GstBuffer *buffer = NULL;
2098 GstFlowReturn ret = GST_FLOW_OK;
2100 /* Get the first 9 bytes */
2101 if (G_UNLIKELY ((ret = gst_flv_demux_pull_range (demux, pad, demux->offset,
2102 FLV_HEADER_SIZE, &buffer)) != GST_FLOW_OK))
2105 ret = gst_flv_demux_parse_header (demux, buffer);
2107 gst_buffer_unref (buffer);
2109 /* Jump over the header now */
2110 demux->offset += FLV_HEADER_SIZE;
2111 demux->state = FLV_STATE_TAG_TYPE;
2118 gst_flv_demux_move_to_offset (GstFlvDemux * demux, gint64 offset,
2121 demux->offset = offset;
2123 /* Tell all the stream we moved to a different position (discont) */
2124 demux->audio_need_discont = TRUE;
2125 demux->video_need_discont = TRUE;
2127 /* next section setup */
2128 demux->from_offset = -1;
2129 demux->audio_done = demux->video_done = FALSE;
2130 demux->audio_first_ts = demux->video_first_ts = GST_CLOCK_TIME_NONE;
2133 demux->from_offset = -1;
2134 demux->to_offset = G_MAXINT64;
2137 /* If we seeked at the beginning of the file parse the header again */
2138 if (G_UNLIKELY (!demux->offset)) {
2139 demux->state = FLV_STATE_HEADER;
2140 } else { /* or parse a tag */
2141 demux->state = FLV_STATE_TAG_TYPE;
2145 static GstFlowReturn
2146 gst_flv_demux_seek_to_prev_keyframe (GstFlvDemux * demux)
2148 GstFlowReturn ret = GST_FLOW_UNEXPECTED;
2149 GstIndexEntry *entry = NULL;
2151 GST_DEBUG_OBJECT (demux,
2152 "terminated section started at offset %" G_GINT64_FORMAT,
2153 demux->from_offset);
2155 /* we are done if we got all audio and video */
2156 if ((!GST_CLOCK_TIME_IS_VALID (demux->audio_first_ts) ||
2157 demux->audio_first_ts < demux->segment.start) &&
2158 (!GST_CLOCK_TIME_IS_VALID (demux->video_first_ts) ||
2159 demux->video_first_ts < demux->segment.start))
2162 if (demux->from_offset <= 0)
2165 GST_DEBUG_OBJECT (demux, "locating previous position");
2167 /* locate index entry before previous start position */
2169 entry = gst_index_get_assoc_entry (demux->index, demux->index_id,
2170 GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT,
2171 GST_FORMAT_BYTES, demux->from_offset - 1);
2176 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
2177 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
2179 GST_DEBUG_OBJECT (demux, "found index entry for %" G_GINT64_FORMAT
2180 " at %" GST_TIME_FORMAT ", seeking to %" G_GINT64_FORMAT,
2181 demux->offset - 1, GST_TIME_ARGS (time), bytes);
2183 /* setup for next section */
2184 demux->to_offset = demux->from_offset;
2185 gst_flv_demux_move_to_offset (demux, bytes, FALSE);
2193 static GstFlowReturn
2194 gst_flv_demux_create_index (GstFlvDemux * demux, gint64 pos, GstClockTime ts)
2200 GstClockTime tag_time;
2201 GstFlowReturn ret = GST_FLOW_OK;
2203 if (!gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES, &size))
2206 GST_DEBUG_OBJECT (demux, "building index at %" G_GINT64_FORMAT
2207 " looking for time %" GST_TIME_FORMAT, pos, GST_TIME_ARGS (ts));
2209 old_offset = demux->offset;
2210 demux->offset = pos;
2212 while ((ret = gst_flv_demux_pull_range (demux, demux->sinkpad, demux->offset,
2213 12, &buffer)) == GST_FLOW_OK) {
2215 gst_flv_demux_parse_tag_timestamp (demux, TRUE, buffer, &tag_size);
2217 gst_buffer_unref (buffer);
2219 if (G_UNLIKELY (tag_time == GST_CLOCK_TIME_NONE || tag_time > ts))
2222 demux->offset += tag_size;
2225 if (ret == GST_FLOW_UNEXPECTED) {
2226 /* file ran out, so mark we have complete index */
2227 demux->indexed = TRUE;
2232 demux->offset = old_offset;
2238 gst_flv_demux_get_metadata (GstFlvDemux * demux)
2240 gint64 ret = 0, offset;
2241 size_t tag_size, size;
2242 GstBuffer *buffer = NULL;
2245 if (!gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES, &offset))
2249 GST_DEBUG_OBJECT (demux, "upstream size: %" G_GINT64_FORMAT, offset);
2250 if (G_UNLIKELY (offset < 4))
2254 if (GST_FLOW_OK != gst_flv_demux_pull_range (demux, demux->sinkpad, offset,
2258 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
2259 tag_size = GST_READ_UINT32_BE (data);
2260 gst_buffer_unmap (buffer, data, -1);
2261 GST_DEBUG_OBJECT (demux, "last tag size: %" G_GSIZE_FORMAT, tag_size);
2262 gst_buffer_unref (buffer);
2266 if (GST_FLOW_OK != gst_flv_demux_pull_range (demux, demux->sinkpad, offset,
2270 /* a consistency check */
2271 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
2272 size = GST_READ_UINT24_BE (data + 1);
2273 if (size != tag_size - 11) {
2274 gst_buffer_unmap (buffer, data, -1);
2275 GST_DEBUG_OBJECT (demux,
2276 "tag size %" G_GSIZE_FORMAT ", expected %" G_GSIZE_FORMAT
2277 ", corrupt or truncated file", size, tag_size - 11);
2281 /* try to update duration with timestamp in any case */
2282 gst_flv_demux_parse_tag_timestamp (demux, FALSE, buffer, &size);
2284 /* maybe get some more metadata */
2285 if (data[0] == 18) {
2286 gst_buffer_unmap (buffer, data, -1);
2287 gst_buffer_unref (buffer);
2289 GST_DEBUG_OBJECT (demux, "script tag, pulling it to parse");
2291 if (GST_FLOW_OK == gst_flv_demux_pull_range (demux, demux->sinkpad, offset,
2293 gst_flv_demux_parse_tag_script (demux, buffer);
2295 gst_buffer_unmap (buffer, data, -1);
2300 gst_buffer_unref (buffer);
2306 gst_flv_demux_loop (GstPad * pad)
2308 GstFlvDemux *demux = NULL;
2309 GstFlowReturn ret = GST_FLOW_OK;
2311 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2314 switch (demux->state) {
2315 case FLV_STATE_TAG_TYPE:
2316 if (demux->from_offset == -1)
2317 demux->from_offset = demux->offset;
2318 ret = gst_flv_demux_pull_tag (pad, demux);
2319 /* if we have seen real data, we probably passed a possible metadata
2320 * header located at start. So if we do not yet have an index,
2321 * try to pick up metadata (index, duration) at the end */
2322 if (G_UNLIKELY (!demux->file_size && !demux->indexed &&
2323 (demux->has_video || demux->has_audio)))
2324 demux->file_size = gst_flv_demux_get_metadata (demux);
2326 case FLV_STATE_DONE:
2327 ret = GST_FLOW_UNEXPECTED;
2329 case FLV_STATE_SEEK:
2330 /* seek issued with insufficient index;
2331 * scan for index in task thread from current maximum offset to
2332 * desired time and then perform seek */
2333 /* TODO maybe some buffering message or so to indicate scan progress */
2334 ret = gst_flv_demux_create_index (demux, demux->index_max_pos,
2336 if (ret != GST_FLOW_OK)
2338 /* position and state arranged by seek,
2339 * also unrefs event */
2340 gst_flv_demux_handle_seek_pull (demux, demux->seek_event, FALSE);
2341 demux->seek_event = NULL;
2344 ret = gst_flv_demux_pull_header (pad, demux);
2345 /* index scans start after header */
2346 demux->index_max_pos = demux->offset;
2350 if (demux->segment.rate < 0.0) {
2351 /* check end of section */
2352 if ((gint64) demux->offset >= demux->to_offset ||
2353 demux->segment.position >= demux->segment.stop + 2 * GST_SECOND ||
2354 (demux->audio_done && demux->video_done))
2355 ret = gst_flv_demux_seek_to_prev_keyframe (demux);
2357 /* check EOS condition */
2358 if ((demux->segment.stop != -1) &&
2359 (demux->segment.position >= demux->segment.stop)) {
2360 ret = GST_FLOW_UNEXPECTED;
2364 /* pause if something went wrong or at end */
2365 if (G_UNLIKELY (ret != GST_FLOW_OK))
2368 gst_object_unref (demux);
2374 const gchar *reason = gst_flow_get_name (ret);
2376 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
2377 gst_pad_pause_task (pad);
2379 if (ret == GST_FLOW_UNEXPECTED) {
2380 /* handle end-of-stream/segment */
2381 /* so align our position with the end of it, if there is one
2382 * this ensures a subsequent will arrive at correct base/acc time */
2383 if (demux->segment.rate > 0.0 &&
2384 GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
2385 demux->segment.position = demux->segment.stop;
2386 else if (demux->segment.rate < 0.0)
2387 demux->segment.position = demux->segment.start;
2389 /* perform EOS logic */
2390 if (!demux->no_more_pads) {
2391 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2392 demux->no_more_pads = TRUE;
2395 if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2398 /* for segment playback we need to post when (in stream time)
2399 * we stopped, this is either stop (when set) or the duration. */
2400 if ((stop = demux->segment.stop) == -1)
2401 stop = demux->segment.duration;
2403 if (demux->segment.rate >= 0) {
2404 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
2405 gst_element_post_message (GST_ELEMENT_CAST (demux),
2406 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2407 GST_FORMAT_TIME, stop));
2408 } else { /* Reverse playback */
2409 GST_LOG_OBJECT (demux, "Sending segment done, at beginning of "
2411 gst_element_post_message (GST_ELEMENT_CAST (demux),
2412 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2413 GST_FORMAT_TIME, demux->segment.start));
2416 /* normal playback, send EOS to all linked pads */
2417 if (!demux->no_more_pads) {
2418 gst_element_no_more_pads (GST_ELEMENT (demux));
2419 demux->no_more_pads = TRUE;
2422 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
2423 if (!gst_flv_demux_push_src_event (demux, gst_event_new_eos ()))
2424 GST_WARNING_OBJECT (demux, "failed pushing EOS on streams");
2426 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2427 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2428 ("Internal data stream error."),
2429 ("stream stopped, reason %s", reason));
2430 gst_flv_demux_push_src_event (demux, gst_event_new_eos ());
2432 gst_object_unref (demux);
2438 gst_flv_demux_find_offset (GstFlvDemux * demux, GstSegment * segment)
2442 GstIndexEntry *entry;
2444 g_return_val_if_fail (segment != NULL, 0);
2446 time = segment->position;
2449 /* Let's check if we have an index entry for that seek time */
2450 entry = gst_index_get_assoc_entry (demux->index, demux->index_id,
2451 GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT,
2452 GST_FORMAT_TIME, time);
2455 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
2456 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
2458 GST_DEBUG_OBJECT (demux, "found index entry for %" GST_TIME_FORMAT
2459 " at %" GST_TIME_FORMAT ", seeking to %" G_GINT64_FORMAT,
2460 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (time), bytes);
2462 /* Key frame seeking */
2463 if (segment->flags & GST_SEEK_FLAG_KEY_UNIT) {
2464 /* Adjust the segment so that the keyframe fits in */
2465 if (time < segment->start) {
2466 segment->start = segment->time = time;
2468 segment->position = time;
2471 GST_DEBUG_OBJECT (demux, "no index entry found for %" GST_TIME_FORMAT,
2472 GST_TIME_ARGS (segment->start));
2480 flv_demux_handle_seek_push (GstFlvDemux * demux, GstEvent * event)
2484 GstSeekType start_type, stop_type;
2487 gboolean update, flush, ret;
2488 GstSegment seeksegment;
2490 gst_event_parse_seek (event, &rate, &format, &flags,
2491 &start_type, &start, &stop_type, &stop);
2493 if (format != GST_FORMAT_TIME)
2496 flush = !!(flags & GST_SEEK_FLAG_FLUSH);
2497 /* FIXME : the keyframe flag is never used ! */
2499 /* Work on a copy until we are sure the seek succeeded. */
2500 memcpy (&seeksegment, &demux->segment, sizeof (GstSegment));
2502 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
2505 /* Apply the seek to our segment */
2506 gst_segment_do_seek (&seeksegment, rate, format, flags,
2507 start_type, start, stop_type, stop, &update);
2509 GST_DEBUG_OBJECT (demux, "segment configured %" GST_SEGMENT_FORMAT,
2512 if (flush || seeksegment.position != demux->segment.position) {
2513 /* Do the actual seeking */
2514 guint64 offset = gst_flv_demux_find_offset (demux, &seeksegment);
2516 GST_DEBUG_OBJECT (demux, "generating an upstream seek at position %"
2517 G_GUINT64_FORMAT, offset);
2518 ret = gst_pad_push_event (demux->sinkpad,
2519 gst_event_new_seek (seeksegment.rate, GST_FORMAT_BYTES,
2520 seeksegment.flags | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET,
2521 offset, GST_SEEK_TYPE_NONE, 0));
2522 if (G_UNLIKELY (!ret)) {
2523 GST_WARNING_OBJECT (demux, "upstream seek failed");
2526 /* Tell all the stream we moved to a different position (discont) */
2527 demux->audio_need_discont = TRUE;
2528 demux->video_need_discont = TRUE;
2534 /* Ok seek succeeded, take the newly configured segment */
2535 memcpy (&demux->segment, &seeksegment, sizeof (GstSegment));
2537 /* Tell all the stream a new segment is needed */
2538 demux->audio_need_segment = TRUE;
2539 demux->video_need_segment = TRUE;
2540 /* Clean any potential newsegment event kept for the streams. The first
2541 * stream needing a new segment will create a new one. */
2542 if (G_UNLIKELY (demux->new_seg_event)) {
2543 gst_event_unref (demux->new_seg_event);
2544 demux->new_seg_event = NULL;
2546 gst_event_unref (event);
2548 ret = gst_pad_push_event (demux->sinkpad, event);
2556 GST_WARNING_OBJECT (demux, "we only support seeking in TIME format");
2557 gst_event_unref (event);
2563 gst_flv_demux_handle_seek_push (GstFlvDemux * demux, GstEvent * event)
2567 gst_event_parse_seek (event, NULL, &format, NULL, NULL, NULL, NULL, NULL);
2569 if (format != GST_FORMAT_TIME) {
2570 GST_WARNING_OBJECT (demux, "we only support seeking in TIME format");
2571 gst_event_unref (event);
2575 /* First try upstream */
2576 if (gst_pad_push_event (demux->sinkpad, gst_event_ref (event))) {
2577 GST_DEBUG_OBJECT (demux, "Upstream successfully seeked");
2578 gst_event_unref (event);
2582 if (!demux->indexed) {
2583 guint64 seek_offset = 0;
2584 gboolean building_index;
2586 GST_OBJECT_LOCK (demux);
2587 /* handle the seek in the chain function */
2588 demux->seeking = TRUE;
2589 demux->state = FLV_STATE_SEEK;
2591 /* copy the event */
2592 if (demux->seek_event)
2593 gst_event_unref (demux->seek_event);
2594 demux->seek_event = gst_event_ref (event);
2596 /* set the building_index flag so that only one thread can setup the
2597 * structures for index seeking. */
2598 building_index = demux->building_index;
2599 if (!building_index) {
2600 demux->building_index = TRUE;
2601 if (!demux->file_size
2602 && !gst_pad_query_peer_duration (demux->sinkpad, GST_FORMAT_BYTES,
2603 &demux->file_size)) {
2604 GST_WARNING_OBJECT (demux, "Failed to query upstream file size");
2605 GST_OBJECT_UNLOCK (demux);
2609 /* we hope the last tag is a scriptdataobject containing an index
2610 * the size of the last tag is given in the last guint32 bits
2611 * then we seek to the beginning of the tag, parse it and hopefully obtain an index */
2612 seek_offset = demux->file_size - sizeof (guint32);
2613 GST_DEBUG_OBJECT (demux,
2614 "File size obtained, seeking to %" G_GUINT64_FORMAT, seek_offset);
2616 GST_OBJECT_UNLOCK (demux);
2618 if (!building_index) {
2619 GST_INFO_OBJECT (demux, "Seeking to last 4 bytes at %" G_GUINT64_FORMAT,
2621 return flv_demux_seek_to_offset (demux, seek_offset);
2624 /* FIXME: we have to always return true so that we don't block the seek
2626 * Note: maybe it is OK to return true if we're still building the index */
2630 return flv_demux_handle_seek_push (demux, event);
2634 gst_flv_demux_handle_seek_pull (GstFlvDemux * demux, GstEvent * event,
2639 GstSeekType start_type, stop_type;
2642 gboolean update, flush, ret = FALSE;
2643 GstSegment seeksegment;
2645 gst_event_parse_seek (event, &rate, &format, &flags,
2646 &start_type, &start, &stop_type, &stop);
2648 if (format != GST_FORMAT_TIME)
2651 /* mark seeking thread entering flushing/pausing */
2652 GST_OBJECT_LOCK (demux);
2654 demux->seeking = seeking;
2655 GST_OBJECT_UNLOCK (demux);
2657 flush = !!(flags & GST_SEEK_FLAG_FLUSH);
2658 /* FIXME : the keyframe flag is never used */
2661 /* Flush start up and downstream to make sure data flow and loops are
2663 gst_flv_demux_push_src_event (demux, gst_event_new_flush_start ());
2664 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
2666 /* Pause the pulling task */
2667 gst_pad_pause_task (demux->sinkpad);
2670 /* Take the stream lock */
2671 GST_PAD_STREAM_LOCK (demux->sinkpad);
2674 /* Stop flushing upstream we need to pull */
2675 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop (TRUE));
2678 /* Work on a copy until we are sure the seek succeeded. */
2679 memcpy (&seeksegment, &demux->segment, sizeof (GstSegment));
2681 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
2684 /* Apply the seek to our segment */
2685 gst_segment_do_seek (&seeksegment, rate, format, flags,
2686 start_type, start, stop_type, stop, &update);
2688 GST_DEBUG_OBJECT (demux, "segment configured %" GST_SEGMENT_FORMAT,
2691 if (flush || seeksegment.position != demux->segment.position) {
2692 /* Do the actual seeking */
2693 /* index is reliable if it is complete or we do not go to far ahead */
2694 if (seeking && !demux->indexed &&
2695 seeksegment.position > demux->index_max_time + 10 * GST_SECOND) {
2696 GST_DEBUG_OBJECT (demux, "delaying seek to post-scan; "
2697 " index only up to %" GST_TIME_FORMAT,
2698 GST_TIME_ARGS (demux->index_max_time));
2699 /* stop flushing for now */
2701 gst_flv_demux_push_src_event (demux, gst_event_new_flush_stop (TRUE));
2702 /* delegate scanning and index building to task thread to avoid
2703 * occupying main (UI) loop */
2704 if (demux->seek_event)
2705 gst_event_unref (demux->seek_event);
2706 demux->seek_event = gst_event_ref (event);
2707 demux->seek_time = seeksegment.position;
2708 demux->state = FLV_STATE_SEEK;
2709 /* do not know about succes yet, but we did care and handled it */
2713 /* now index should be as reliable as it can be for current purpose */
2714 gst_flv_demux_move_to_offset (demux,
2715 gst_flv_demux_find_offset (demux, &seeksegment), TRUE);
2722 /* Stop flushing, the sinks are at time 0 now */
2723 gst_flv_demux_push_src_event (demux, gst_event_new_flush_stop (TRUE));
2727 /* Ok seek succeeded, take the newly configured segment */
2728 memcpy (&demux->segment, &seeksegment, sizeof (GstSegment));
2730 /* Notify about the start of a new segment */
2731 if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2732 gst_element_post_message (GST_ELEMENT (demux),
2733 gst_message_new_segment_start (GST_OBJECT (demux),
2734 demux->segment.format, demux->segment.position));
2737 /* Tell all the stream a new segment is needed */
2738 demux->audio_need_segment = TRUE;
2739 demux->video_need_segment = TRUE;
2740 /* Clean any potential newsegment event kept for the streams. The first
2741 * stream needing a new segment will create a new one. */
2742 if (G_UNLIKELY (demux->new_seg_event)) {
2743 gst_event_unref (demux->new_seg_event);
2744 demux->new_seg_event = NULL;
2746 if (demux->segment.rate < 0.0) {
2747 /* we can't generate a segment by locking on
2748 * to the first timestamp we see */
2749 GST_DEBUG_OBJECT (demux, "preparing newsegment from %"
2750 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
2751 GST_TIME_ARGS (demux->segment.start),
2752 GST_TIME_ARGS (demux->segment.stop));
2753 demux->new_seg_event = gst_event_new_segment (&demux->segment);
2758 GST_OBJECT_LOCK (demux);
2759 seeking = demux->seeking && !seeking;
2760 demux->seeking = FALSE;
2761 GST_OBJECT_UNLOCK (demux);
2763 /* if we detect an external seek having started (and possibly already having
2764 * flushed), do not restart task to give it a chance.
2765 * Otherwise external one's flushing will take care to pause task */
2767 gst_pad_pause_task (demux->sinkpad);
2769 gst_pad_start_task (demux->sinkpad,
2770 (GstTaskFunction) gst_flv_demux_loop, demux->sinkpad);
2773 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
2775 gst_event_unref (event);
2781 GST_WARNING_OBJECT (demux, "we only support seeking in TIME format");
2782 gst_event_unref (event);
2787 /* If we can pull that's prefered */
2789 gst_flv_demux_sink_activate (GstPad * sinkpad)
2794 query = gst_query_new_scheduling ();
2796 if (!gst_pad_peer_query (sinkpad, query)) {
2797 gst_query_unref (query);
2801 gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
2802 gst_query_unref (query);
2807 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2808 return gst_pad_activate_pull (sinkpad, TRUE);
2812 GST_DEBUG_OBJECT (sinkpad, "activating push");
2813 return gst_pad_activate_push (sinkpad, TRUE);
2817 /* This function gets called when we activate ourselves in push mode.
2818 * We cannot seek (ourselves) in the stream */
2820 gst_flv_demux_sink_activate_push (GstPad * sinkpad, gboolean active)
2824 demux = GST_FLV_DEMUX (gst_pad_get_parent (sinkpad));
2826 demux->random_access = FALSE;
2828 gst_object_unref (demux);
2833 /* this function gets called when we activate ourselves in pull mode.
2834 * We can perform random access to the resource and we start a task
2835 * to start reading */
2837 gst_flv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
2841 demux = GST_FLV_DEMUX (gst_pad_get_parent (sinkpad));
2844 demux->random_access = TRUE;
2845 gst_object_unref (demux);
2846 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_flv_demux_loop,
2849 demux->random_access = FALSE;
2850 gst_object_unref (demux);
2851 return gst_pad_stop_task (sinkpad);
2856 gst_flv_demux_sink_event (GstPad * pad, GstEvent * event)
2859 gboolean ret = FALSE;
2861 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2863 GST_DEBUG_OBJECT (demux, "handling event %s", GST_EVENT_TYPE_NAME (event));
2865 switch (GST_EVENT_TYPE (event)) {
2866 case GST_EVENT_FLUSH_START:
2867 GST_DEBUG_OBJECT (demux, "trying to force chain function to exit");
2868 demux->flushing = TRUE;
2869 ret = gst_flv_demux_push_src_event (demux, event);
2871 case GST_EVENT_FLUSH_STOP:
2872 GST_DEBUG_OBJECT (demux, "flushing FLV demuxer");
2873 gst_flv_demux_flush (demux, TRUE);
2874 ret = gst_flv_demux_push_src_event (demux, event);
2877 GST_DEBUG_OBJECT (demux, "received EOS");
2879 GST_DEBUG_OBJECT (demux, "committing index");
2880 gst_index_commit (demux->index, demux->index_id);
2882 if (!demux->no_more_pads) {
2883 gst_element_no_more_pads (GST_ELEMENT (demux));
2884 demux->no_more_pads = TRUE;
2887 if (!gst_flv_demux_push_src_event (demux, event))
2888 GST_WARNING_OBJECT (demux, "failed pushing EOS on streams");
2891 case GST_EVENT_SEGMENT:
2893 GstSegment in_segment;
2895 GST_DEBUG_OBJECT (demux, "received new segment");
2897 gst_event_copy_segment (event, &in_segment);
2899 if (in_segment.format == GST_FORMAT_TIME) {
2900 /* time segment, this is perfect, copy over the values. */
2901 memcpy (&demux->segment, &in_segment, sizeof (in_segment));
2903 GST_DEBUG_OBJECT (demux, "NEWSEGMENT: %" GST_SEGMENT_FORMAT,
2907 ret = gst_flv_demux_push_src_event (demux, event);
2909 /* non-time format */
2910 demux->audio_need_segment = TRUE;
2911 demux->video_need_segment = TRUE;
2913 gst_event_unref (event);
2918 ret = gst_flv_demux_push_src_event (demux, event);
2922 gst_object_unref (demux);
2928 gst_flv_demux_src_event (GstPad * pad, GstEvent * event)
2931 gboolean ret = FALSE;
2933 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2935 GST_DEBUG_OBJECT (demux, "handling event %s", GST_EVENT_TYPE_NAME (event));
2937 switch (GST_EVENT_TYPE (event)) {
2938 case GST_EVENT_SEEK:
2939 if (demux->random_access) {
2940 ret = gst_flv_demux_handle_seek_pull (demux, event, TRUE);
2942 ret = gst_flv_demux_handle_seek_push (demux, event);
2946 ret = gst_pad_push_event (demux->sinkpad, event);
2950 gst_object_unref (demux);
2956 gst_flv_demux_query (GstPad * pad, GstQuery * query)
2958 gboolean res = TRUE;
2961 demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
2963 switch (GST_QUERY_TYPE (query)) {
2964 case GST_QUERY_DURATION:
2968 gst_query_parse_duration (query, &format, NULL);
2970 /* duration is time only */
2971 if (format != GST_FORMAT_TIME) {
2972 GST_DEBUG_OBJECT (demux, "duration query only supported for time "
2978 GST_DEBUG_OBJECT (pad, "duration query, replying %" GST_TIME_FORMAT,
2979 GST_TIME_ARGS (demux->duration));
2981 gst_query_set_duration (query, GST_FORMAT_TIME, demux->duration);
2985 case GST_QUERY_POSITION:
2989 gst_query_parse_position (query, &format, NULL);
2991 /* position is time only */
2992 if (format != GST_FORMAT_TIME) {
2993 GST_DEBUG_OBJECT (demux, "position query only supported for time "
2999 GST_DEBUG_OBJECT (pad, "position query, replying %" GST_TIME_FORMAT,
3000 GST_TIME_ARGS (demux->segment.position));
3002 gst_query_set_position (query, GST_FORMAT_TIME, demux->segment.position);
3007 case GST_QUERY_SEEKING:{
3010 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
3012 /* First ask upstream */
3013 if (fmt == GST_FORMAT_TIME && gst_pad_peer_query (demux->sinkpad, query)) {
3016 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
3023 if (fmt != GST_FORMAT_TIME || !demux->index) {
3024 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
3025 } else if (demux->random_access) {
3026 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
3029 GstQuery *peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
3030 gboolean seekable = gst_pad_peer_query (demux->sinkpad, peerquery);
3033 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
3034 gst_query_unref (peerquery);
3037 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
3040 gst_query_set_seeking (query, GST_FORMAT_TIME, FALSE, -1, -1);
3044 case GST_QUERY_LATENCY:
3049 if ((peer = gst_pad_get_peer (demux->sinkpad))) {
3050 /* query latency on peer pad */
3051 res = gst_pad_query (peer, query);
3052 gst_object_unref (peer);
3054 /* no peer, we don't know */
3062 gst_object_unref (demux);
3067 static GstStateChangeReturn
3068 gst_flv_demux_change_state (GstElement * element, GstStateChange transition)
3071 GstStateChangeReturn ret;
3073 demux = GST_FLV_DEMUX (element);
3075 switch (transition) {
3076 case GST_STATE_CHANGE_READY_TO_PAUSED:
3077 /* If this is our own index destroy it as the
3078 * old entries might be wrong for the new stream */
3079 if (demux->own_index) {
3080 gst_object_unref (demux->index);
3081 demux->index = NULL;
3082 demux->own_index = FALSE;
3085 /* If no index was created, generate one */
3086 if (G_UNLIKELY (!demux->index)) {
3087 GST_DEBUG_OBJECT (demux, "no index provided creating our own");
3089 demux->index = gst_index_factory_make ("memindex");
3091 gst_index_get_writer_id (demux->index, GST_OBJECT (demux),
3093 demux->own_index = TRUE;
3095 gst_flv_demux_cleanup (demux);
3101 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3102 if (ret == GST_STATE_CHANGE_FAILURE)
3105 switch (transition) {
3106 case GST_STATE_CHANGE_PAUSED_TO_READY:
3107 gst_flv_demux_cleanup (demux);
3117 gst_flv_demux_set_index (GstElement * element, GstIndex * index)
3119 GstFlvDemux *demux = GST_FLV_DEMUX (element);
3121 GST_OBJECT_LOCK (demux);
3123 gst_object_unref (demux->index);
3125 demux->index = gst_object_ref (index);
3126 demux->own_index = FALSE;
3128 demux->index = NULL;
3130 GST_OBJECT_UNLOCK (demux);
3131 /* object lock might be taken again */
3133 gst_index_get_writer_id (index, GST_OBJECT (element), &demux->index_id);
3134 GST_DEBUG_OBJECT (demux, "Set index %" GST_PTR_FORMAT, demux->index);
3139 gst_flv_demux_get_index (GstElement * element)
3141 GstIndex *result = NULL;
3143 GstFlvDemux *demux = GST_FLV_DEMUX (element);
3145 GST_OBJECT_LOCK (demux);
3147 result = gst_object_ref (demux->index);
3148 GST_OBJECT_UNLOCK (demux);
3154 gst_flv_demux_dispose (GObject * object)
3156 GstFlvDemux *demux = GST_FLV_DEMUX (object);
3158 GST_DEBUG_OBJECT (demux, "disposing FLV demuxer");
3160 if (demux->adapter) {
3161 gst_adapter_clear (demux->adapter);
3162 g_object_unref (demux->adapter);
3163 demux->adapter = NULL;
3166 if (demux->taglist) {
3167 gst_tag_list_free (demux->taglist);
3168 demux->taglist = NULL;
3171 if (demux->new_seg_event) {
3172 gst_event_unref (demux->new_seg_event);
3173 demux->new_seg_event = NULL;
3176 if (demux->audio_codec_data) {
3177 gst_buffer_unref (demux->audio_codec_data);
3178 demux->audio_codec_data = NULL;
3181 if (demux->video_codec_data) {
3182 gst_buffer_unref (demux->video_codec_data);
3183 demux->video_codec_data = NULL;
3186 if (demux->audio_pad) {
3187 gst_object_unref (demux->audio_pad);
3188 demux->audio_pad = NULL;
3191 if (demux->video_pad) {
3192 gst_object_unref (demux->video_pad);
3193 demux->video_pad = NULL;
3197 gst_object_unref (demux->index);
3198 demux->index = NULL;
3202 g_array_free (demux->times, TRUE);
3203 demux->times = NULL;
3206 if (demux->filepositions) {
3207 g_array_free (demux->filepositions, TRUE);
3208 demux->filepositions = NULL;
3211 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
3215 gst_flv_demux_class_init (GstFlvDemuxClass * klass)
3217 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
3218 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
3220 gobject_class->dispose = gst_flv_demux_dispose;
3222 gstelement_class->change_state =
3223 GST_DEBUG_FUNCPTR (gst_flv_demux_change_state);
3224 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_flv_demux_set_index);
3225 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_flv_demux_get_index);
3227 gst_element_class_add_pad_template (gstelement_class,
3228 gst_static_pad_template_get (&flv_sink_template));
3229 gst_element_class_add_pad_template (gstelement_class,
3230 gst_static_pad_template_get (&audio_src_template));
3231 gst_element_class_add_pad_template (gstelement_class,
3232 gst_static_pad_template_get (&video_src_template));
3233 gst_element_class_set_details_simple (gstelement_class, "FLV Demuxer",
3235 "Demux FLV feeds into digital streams",
3236 "Julien Moutte <julien@moutte.net>");
3240 gst_flv_demux_init (GstFlvDemux * demux)
3243 gst_pad_new_from_static_template (&flv_sink_template, "sink");
3245 gst_pad_set_event_function (demux->sinkpad,
3246 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_event));
3247 gst_pad_set_chain_function (demux->sinkpad,
3248 GST_DEBUG_FUNCPTR (gst_flv_demux_chain));
3249 gst_pad_set_activate_function (demux->sinkpad,
3250 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_activate));
3251 gst_pad_set_activatepull_function (demux->sinkpad,
3252 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_activate_pull));
3253 gst_pad_set_activatepush_function (demux->sinkpad,
3254 GST_DEBUG_FUNCPTR (gst_flv_demux_sink_activate_push));
3256 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
3258 demux->adapter = gst_adapter_new ();
3259 demux->taglist = gst_tag_list_new_empty ();
3260 gst_segment_init (&demux->segment, GST_FORMAT_TIME);
3262 demux->own_index = FALSE;
3264 gst_flv_demux_cleanup (demux);
3268 plugin_init (GstPlugin * plugin)
3270 GST_DEBUG_CATEGORY_INIT (flvdemux_debug, "flvdemux", 0, "FLV demuxer");
3272 if (!gst_element_register (plugin, "flvdemux", GST_RANK_PRIMARY,
3273 gst_flv_demux_get_type ()) ||
3274 !gst_element_register (plugin, "flvmux", GST_RANK_PRIMARY,
3275 gst_flv_mux_get_type ()))
3281 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
3282 "flv", "FLV muxing and demuxing plugin",
3283 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)