mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-12-03 15:06:34 +00:00
[MOVED FROM BAD 03/57] gst/flv/gstflvdemux.c: First method for seeking in pull mode using the index built step by step or coming from metadata.
Original commit message from CVS: 2007-08-14 Julien MOUTTE <julien@moutte.net> * gst/flv/gstflvdemux.c: (gst_flv_demux_cleanup), (gst_flv_demux_adapter_flush), (gst_flv_demux_chain), (gst_flv_demux_pull_tag), (gst_flv_demux_do_seek), (gst_flv_demux_handle_seek), (gst_flv_demux_sink_event), (gst_flv_demux_src_event), (gst_flv_demux_query), (gst_flv_demux_change_state), (gst_flv_demux_set_index), (gst_flv_demux_get_index), (gst_flv_demux_dispose), (gst_flv_demux_class_init): First method for seeking in pull mode using the index built step by step or coming from metadata. * gst/flv/gstflvdemux.h: * gst/flv/gstflvparse.c: (FLV_GET_STRING), (gst_flv_parse_metadata_item), (gst_flv_parse_tag_script), (gst_flv_parse_tag_audio), (gst_flv_parse_tag_video): Parse more metadata types and keyframes index.
This commit is contained in:
parent
11e27e7856
commit
c3228fbb67
3 changed files with 493 additions and 68 deletions
|
@ -93,7 +93,8 @@ gst_flv_demux_cleanup (GstFLVDemux * demux)
|
||||||
|
|
||||||
demux->video_offset = 0;
|
demux->video_offset = 0;
|
||||||
demux->audio_offset = 0;
|
demux->audio_offset = 0;
|
||||||
demux->offset = demux->tag_size = demux->tag_data_size = 0;
|
demux->offset = demux->cur_tag_offset = 0;
|
||||||
|
demux->tag_size = demux->tag_data_size = 0;
|
||||||
demux->duration = GST_CLOCK_TIME_NONE;
|
demux->duration = GST_CLOCK_TIME_NONE;
|
||||||
|
|
||||||
if (demux->new_seg_event) {
|
if (demux->new_seg_event) {
|
||||||
|
@ -114,6 +115,24 @@ gst_flv_demux_cleanup (GstFLVDemux * demux)
|
||||||
gst_object_unref (demux->video_pad);
|
gst_object_unref (demux->video_pad);
|
||||||
demux->video_pad = NULL;
|
demux->video_pad = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (demux->times) {
|
||||||
|
g_array_free (demux->times, TRUE);
|
||||||
|
demux->times = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demux->filepositions) {
|
||||||
|
g_array_free (demux->filepositions, TRUE);
|
||||||
|
demux->filepositions = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
gst_flv_demux_adapter_flush (GstFLVDemux * demux, guint64 bytes)
|
||||||
|
{
|
||||||
|
demux->offset += bytes;
|
||||||
|
|
||||||
|
gst_adapter_flush (demux->adapter, bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstFlowReturn
|
static GstFlowReturn
|
||||||
|
@ -137,7 +156,7 @@ parse:
|
||||||
|
|
||||||
ret = gst_flv_parse_header (demux, data, FLV_HEADER_SIZE);
|
ret = gst_flv_parse_header (demux, data, FLV_HEADER_SIZE);
|
||||||
|
|
||||||
gst_adapter_flush (demux->adapter, FLV_HEADER_SIZE);
|
gst_flv_demux_adapter_flush (demux, FLV_HEADER_SIZE);
|
||||||
|
|
||||||
demux->state = FLV_STATE_TAG_TYPE;
|
demux->state = FLV_STATE_TAG_TYPE;
|
||||||
goto parse;
|
goto parse;
|
||||||
|
@ -150,11 +169,14 @@ parse:
|
||||||
if (gst_adapter_available (demux->adapter) >= FLV_TAG_TYPE_SIZE) {
|
if (gst_adapter_available (demux->adapter) >= FLV_TAG_TYPE_SIZE) {
|
||||||
const guint8 *data;
|
const guint8 *data;
|
||||||
|
|
||||||
|
/* Remember the tag offset in bytes */
|
||||||
|
demux->cur_tag_offset = demux->offset;
|
||||||
|
|
||||||
data = gst_adapter_peek (demux->adapter, FLV_TAG_TYPE_SIZE);
|
data = gst_adapter_peek (demux->adapter, FLV_TAG_TYPE_SIZE);
|
||||||
|
|
||||||
ret = gst_flv_parse_tag_type (demux, data, FLV_TAG_TYPE_SIZE);
|
ret = gst_flv_parse_tag_type (demux, data, FLV_TAG_TYPE_SIZE);
|
||||||
|
|
||||||
gst_adapter_flush (demux->adapter, FLV_TAG_TYPE_SIZE);
|
gst_flv_demux_adapter_flush (demux, FLV_TAG_TYPE_SIZE);
|
||||||
|
|
||||||
goto parse;
|
goto parse;
|
||||||
} else {
|
} else {
|
||||||
|
@ -170,7 +192,7 @@ parse:
|
||||||
|
|
||||||
ret = gst_flv_parse_tag_video (demux, data, demux->tag_size);
|
ret = gst_flv_parse_tag_video (demux, data, demux->tag_size);
|
||||||
|
|
||||||
gst_adapter_flush (demux->adapter, demux->tag_size);
|
gst_flv_demux_adapter_flush (demux, demux->tag_size);
|
||||||
|
|
||||||
demux->state = FLV_STATE_TAG_TYPE;
|
demux->state = FLV_STATE_TAG_TYPE;
|
||||||
goto parse;
|
goto parse;
|
||||||
|
@ -187,7 +209,7 @@ parse:
|
||||||
|
|
||||||
ret = gst_flv_parse_tag_audio (demux, data, demux->tag_size);
|
ret = gst_flv_parse_tag_audio (demux, data, demux->tag_size);
|
||||||
|
|
||||||
gst_adapter_flush (demux->adapter, demux->tag_size);
|
gst_flv_demux_adapter_flush (demux, demux->tag_size);
|
||||||
|
|
||||||
demux->state = FLV_STATE_TAG_TYPE;
|
demux->state = FLV_STATE_TAG_TYPE;
|
||||||
goto parse;
|
goto parse;
|
||||||
|
@ -204,7 +226,7 @@ parse:
|
||||||
|
|
||||||
ret = gst_flv_parse_tag_script (demux, data, demux->tag_size);
|
ret = gst_flv_parse_tag_script (demux, data, demux->tag_size);
|
||||||
|
|
||||||
gst_adapter_flush (demux->adapter, demux->tag_size);
|
gst_flv_demux_adapter_flush (demux, demux->tag_size);
|
||||||
|
|
||||||
demux->state = FLV_STATE_TAG_TYPE;
|
demux->state = FLV_STATE_TAG_TYPE;
|
||||||
goto parse;
|
goto parse;
|
||||||
|
@ -235,6 +257,9 @@ gst_flv_demux_pull_tag (GstPad * pad, GstFLVDemux * demux)
|
||||||
GstBuffer *buffer = NULL;
|
GstBuffer *buffer = NULL;
|
||||||
GstFlowReturn ret = GST_FLOW_OK;
|
GstFlowReturn ret = GST_FLOW_OK;
|
||||||
|
|
||||||
|
/* Store tag offset */
|
||||||
|
demux->cur_tag_offset = demux->offset;
|
||||||
|
|
||||||
/* Get the first 4 bytes to identify tag type and size */
|
/* Get the first 4 bytes to identify tag type and size */
|
||||||
ret = gst_pad_pull_range (pad, demux->offset, FLV_TAG_TYPE_SIZE, &buffer);
|
ret = gst_pad_pull_range (pad, demux->offset, FLV_TAG_TYPE_SIZE, &buffer);
|
||||||
if (G_UNLIKELY (ret != GST_FLOW_OK)) {
|
if (G_UNLIKELY (ret != GST_FLOW_OK)) {
|
||||||
|
@ -471,6 +496,175 @@ pause:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_flv_demux_do_seek (GstFLVDemux * demux, GstSegment * segment)
|
||||||
|
{
|
||||||
|
gboolean ret = FALSE;
|
||||||
|
GstIndexEntry *entry;
|
||||||
|
|
||||||
|
/* Let's check if we have an index entry for that seek time */
|
||||||
|
entry = gst_index_get_assoc_entry (demux->index, demux->index_id,
|
||||||
|
GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME,
|
||||||
|
segment->start);
|
||||||
|
|
||||||
|
if (entry) {
|
||||||
|
gint64 bytes = 0;
|
||||||
|
gint64 time = 0;
|
||||||
|
|
||||||
|
gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
|
||||||
|
gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
|
||||||
|
|
||||||
|
demux->offset = bytes;
|
||||||
|
demux->state = FLV_STATE_TAG_TYPE;
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (demux, "found index entry for %" GST_TIME_FORMAT
|
||||||
|
" at %" GST_TIME_FORMAT ", seeking to %" G_GINT64_FORMAT,
|
||||||
|
GST_TIME_ARGS (segment->start), GST_TIME_ARGS (time), bytes);
|
||||||
|
|
||||||
|
/* Key frame seeking */
|
||||||
|
if (demux->segment->flags & GST_SEEK_FLAG_KEY_UNIT) {
|
||||||
|
/* Adjust the segment so that the keyframe fits in */
|
||||||
|
if (time < segment->start) {
|
||||||
|
segment->start = segment->time = time;
|
||||||
|
}
|
||||||
|
segment->last_stop = time;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = TRUE;
|
||||||
|
} else {
|
||||||
|
GST_DEBUG_OBJECT (demux, "no index entry found for %" GST_TIME_FORMAT,
|
||||||
|
GST_TIME_ARGS (segment->start));
|
||||||
|
ret = FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_flv_demux_handle_seek (GstFLVDemux * demux, GstEvent * event)
|
||||||
|
{
|
||||||
|
GstFormat format;
|
||||||
|
GstSeekFlags flags;
|
||||||
|
GstSeekType start_type, stop_type;
|
||||||
|
gint64 start, stop;
|
||||||
|
gdouble rate;
|
||||||
|
gboolean update, flush, keyframe, ret;
|
||||||
|
GstSegment seeksegment;
|
||||||
|
|
||||||
|
gst_event_parse_seek (event, &rate, &format, &flags,
|
||||||
|
&start_type, &start, &stop_type, &stop);
|
||||||
|
|
||||||
|
if (format != GST_FORMAT_TIME)
|
||||||
|
goto wrong_format;
|
||||||
|
|
||||||
|
flush = flags & GST_SEEK_FLAG_FLUSH;
|
||||||
|
keyframe = flags & GST_SEEK_FLAG_KEY_UNIT;
|
||||||
|
|
||||||
|
if (flush) {
|
||||||
|
/* Flush start up and downstream to make sure data flow and loops are
|
||||||
|
idle */
|
||||||
|
gst_pad_event_default (demux->sinkpad, gst_event_new_flush_start ());
|
||||||
|
gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
|
||||||
|
} else {
|
||||||
|
/* Pause the pulling task */
|
||||||
|
gst_pad_pause_task (demux->sinkpad);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Take the stream lock */
|
||||||
|
GST_PAD_STREAM_LOCK (demux->sinkpad);
|
||||||
|
|
||||||
|
if (flush) {
|
||||||
|
/* Stop flushing upstream we need to pull */
|
||||||
|
gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop ());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Work on a copy until we are sure the seek succeeded. */
|
||||||
|
memcpy (&seeksegment, demux->segment, sizeof (GstSegment));
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
|
||||||
|
demux->segment);
|
||||||
|
|
||||||
|
/* Apply the seek to our segment */
|
||||||
|
gst_segment_set_seek (&seeksegment, rate, format, flags,
|
||||||
|
start_type, start, stop_type, stop, &update);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (demux, "segment configured %" GST_SEGMENT_FORMAT,
|
||||||
|
&seeksegment);
|
||||||
|
|
||||||
|
if (flush || seeksegment.last_stop != demux->segment->last_stop) {
|
||||||
|
/* Do the actual seeking */
|
||||||
|
ret = gst_flv_demux_do_seek (demux, &seeksegment);
|
||||||
|
} else {
|
||||||
|
ret = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flush) {
|
||||||
|
/* Stop flushing, the sinks are at time 0 now */
|
||||||
|
gst_pad_event_default (demux->sinkpad, gst_event_new_flush_stop ());
|
||||||
|
} else {
|
||||||
|
GST_DEBUG_OBJECT (demux, "closing running segment %" GST_SEGMENT_FORMAT,
|
||||||
|
demux->segment);
|
||||||
|
|
||||||
|
/* Close the current segment for a linear playback, FIXME, queue for
|
||||||
|
* streaming thread. */
|
||||||
|
if (demux->segment->rate >= 0) {
|
||||||
|
/* for forward playback, we played from start to last_stop */
|
||||||
|
gst_pad_event_default (demux->sinkpad, gst_event_new_new_segment (TRUE,
|
||||||
|
demux->segment->rate, demux->segment->format,
|
||||||
|
demux->segment->start, demux->segment->last_stop,
|
||||||
|
demux->segment->time));
|
||||||
|
} else {
|
||||||
|
gint64 stop;
|
||||||
|
|
||||||
|
if ((stop = demux->segment->stop) == -1)
|
||||||
|
stop = demux->segment->duration;
|
||||||
|
|
||||||
|
/* for reverse playback, we played from stop to last_stop. */
|
||||||
|
gst_pad_event_default (demux->sinkpad, gst_event_new_new_segment (TRUE,
|
||||||
|
demux->segment->rate, demux->segment->format,
|
||||||
|
demux->segment->last_stop, stop, demux->segment->last_stop));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret) {
|
||||||
|
/* Ok seek succeeded, take the newly configured segment */
|
||||||
|
memcpy (demux->segment, &seeksegment, sizeof (GstSegment));
|
||||||
|
|
||||||
|
/* Notify about the start of a new segment */
|
||||||
|
if (demux->segment->flags & GST_SEEK_FLAG_SEGMENT) {
|
||||||
|
gst_element_post_message (GST_ELEMENT (demux),
|
||||||
|
gst_message_new_segment_start (GST_OBJECT (demux),
|
||||||
|
demux->segment->format, demux->segment->last_stop));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Tell all the stream a new segment is needed */
|
||||||
|
{
|
||||||
|
demux->audio_need_segment = TRUE;
|
||||||
|
demux->video_need_segment = TRUE;
|
||||||
|
/* Clean any potential newsegment event kept for the streams. The first
|
||||||
|
* stream needing a new segment will create a new one. */
|
||||||
|
if (G_UNLIKELY (demux->new_seg_event)) {
|
||||||
|
gst_event_unref (demux->new_seg_event);
|
||||||
|
demux->new_seg_event = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gst_pad_start_task (demux->sinkpad,
|
||||||
|
(GstTaskFunction) gst_flv_demux_loop, demux->sinkpad);
|
||||||
|
|
||||||
|
GST_PAD_STREAM_UNLOCK (demux->sinkpad);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
/* ERRORS */
|
||||||
|
wrong_format:
|
||||||
|
{
|
||||||
|
GST_WARNING_OBJECT (demux, "we only support seeking in TIME format");
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* If we can pull that's prefered */
|
/* If we can pull that's prefered */
|
||||||
static gboolean
|
static gboolean
|
||||||
gst_flv_demux_sink_activate (GstPad * sinkpad)
|
gst_flv_demux_sink_activate (GstPad * sinkpad)
|
||||||
|
@ -538,6 +732,10 @@ gst_flv_demux_sink_event (GstPad * pad, GstEvent * event)
|
||||||
break;
|
break;
|
||||||
case GST_EVENT_EOS:
|
case GST_EVENT_EOS:
|
||||||
GST_DEBUG_OBJECT (demux, "received EOS");
|
GST_DEBUG_OBJECT (demux, "received EOS");
|
||||||
|
if (demux->index) {
|
||||||
|
GST_DEBUG_OBJECT (demux, "committing index");
|
||||||
|
gst_index_commit (demux->index, demux->index_id);
|
||||||
|
}
|
||||||
gst_element_no_more_pads (GST_ELEMENT (demux));
|
gst_element_no_more_pads (GST_ELEMENT (demux));
|
||||||
if (!gst_pad_event_default (demux->sinkpad, event)) {
|
if (!gst_pad_event_default (demux->sinkpad, event)) {
|
||||||
GST_WARNING_OBJECT (demux, "failed pushing EOS on streams");
|
GST_WARNING_OBJECT (demux, "failed pushing EOS on streams");
|
||||||
|
@ -591,6 +789,83 @@ gst_flv_demux_sink_event (GstPad * pad, GstEvent * event)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
gst_flv_demux_src_event (GstPad * pad, GstEvent * event)
|
||||||
|
{
|
||||||
|
GstFLVDemux *demux;
|
||||||
|
gboolean ret = FALSE;
|
||||||
|
|
||||||
|
demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
|
||||||
|
|
||||||
|
switch (GST_EVENT_TYPE (event)) {
|
||||||
|
case GST_EVENT_SEEK:
|
||||||
|
ret = gst_flv_demux_handle_seek (demux, event);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ret = gst_pad_push_event (demux->sinkpad, event);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
gst_object_unref (demux);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
gst_flv_demux_query (GstPad * pad, GstQuery * query)
|
||||||
|
{
|
||||||
|
gboolean res = TRUE;
|
||||||
|
GstFLVDemux *demux;
|
||||||
|
|
||||||
|
demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
|
||||||
|
|
||||||
|
switch (GST_QUERY_TYPE (query)) {
|
||||||
|
case GST_QUERY_DURATION:
|
||||||
|
{
|
||||||
|
GstFormat format;
|
||||||
|
|
||||||
|
gst_query_parse_duration (query, &format, NULL);
|
||||||
|
|
||||||
|
/* duration is time only */
|
||||||
|
if (format != GST_FORMAT_TIME) {
|
||||||
|
GST_DEBUG_OBJECT (demux, "duration query only supported for time "
|
||||||
|
"format");
|
||||||
|
res = FALSE;
|
||||||
|
goto beach;
|
||||||
|
}
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (pad, "duration query, replying %" GST_TIME_FORMAT,
|
||||||
|
GST_TIME_ARGS (demux->duration));
|
||||||
|
|
||||||
|
gst_query_set_duration (query, GST_FORMAT_TIME, demux->duration);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GST_QUERY_LATENCY:
|
||||||
|
{
|
||||||
|
GstPad *peer;
|
||||||
|
|
||||||
|
if ((peer = gst_pad_get_peer (demux->sinkpad))) {
|
||||||
|
/* query latency on peer pad */
|
||||||
|
res = gst_pad_query (peer, query);
|
||||||
|
gst_object_unref (peer);
|
||||||
|
} else {
|
||||||
|
/* no peer, we don't know */
|
||||||
|
res = FALSE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
res = FALSE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
beach:
|
||||||
|
gst_object_unref (demux);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static GstStateChangeReturn
|
static GstStateChangeReturn
|
||||||
gst_flv_demux_change_state (GstElement * element, GstStateChange transition)
|
gst_flv_demux_change_state (GstElement * element, GstStateChange transition)
|
||||||
{
|
{
|
||||||
|
@ -601,6 +876,15 @@ gst_flv_demux_change_state (GstElement * element, GstStateChange transition)
|
||||||
|
|
||||||
switch (transition) {
|
switch (transition) {
|
||||||
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
||||||
|
/* If no index was created, generate one */
|
||||||
|
if (G_UNLIKELY (!demux->index)) {
|
||||||
|
GST_DEBUG_OBJECT (demux, "no index provided creating our own");
|
||||||
|
|
||||||
|
demux->index = gst_index_factory_make ("memindex");
|
||||||
|
|
||||||
|
gst_index_get_writer_id (demux->index, GST_OBJECT (demux),
|
||||||
|
&demux->index_id);
|
||||||
|
}
|
||||||
gst_flv_demux_cleanup (demux);
|
gst_flv_demux_cleanup (demux);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -622,6 +906,24 @@ gst_flv_demux_change_state (GstElement * element, GstStateChange transition)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
gst_flv_demux_set_index (GstElement * element, GstIndex * index)
|
||||||
|
{
|
||||||
|
GstFLVDemux *demux = GST_FLV_DEMUX (element);
|
||||||
|
|
||||||
|
demux->index = gst_object_ref (index);
|
||||||
|
|
||||||
|
gst_index_get_writer_id (index, GST_OBJECT (element), &demux->index_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static GstIndex *
|
||||||
|
gst_flv_demux_get_index (GstElement * element)
|
||||||
|
{
|
||||||
|
GstFLVDemux *demux = GST_FLV_DEMUX (element);
|
||||||
|
|
||||||
|
return demux->index;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_flv_demux_dispose (GObject * object)
|
gst_flv_demux_dispose (GObject * object)
|
||||||
{
|
{
|
||||||
|
@ -660,6 +962,21 @@ gst_flv_demux_dispose (GObject * object)
|
||||||
demux->video_pad = NULL;
|
demux->video_pad = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (demux->index) {
|
||||||
|
gst_object_unref (demux->index);
|
||||||
|
demux->index = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demux->times) {
|
||||||
|
g_array_free (demux->times, TRUE);
|
||||||
|
demux->times = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demux->filepositions) {
|
||||||
|
g_array_free (demux->filepositions, TRUE);
|
||||||
|
demux->filepositions = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
|
GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -687,6 +1004,8 @@ gst_flv_demux_class_init (GstFLVDemuxClass * klass)
|
||||||
|
|
||||||
gstelement_class->change_state =
|
gstelement_class->change_state =
|
||||||
GST_DEBUG_FUNCPTR (gst_flv_demux_change_state);
|
GST_DEBUG_FUNCPTR (gst_flv_demux_change_state);
|
||||||
|
gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_flv_demux_set_index);
|
||||||
|
gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_flv_demux_get_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
|
@ -56,6 +56,12 @@ struct _GstFLVDemux
|
||||||
|
|
||||||
GstPad *audio_pad;
|
GstPad *audio_pad;
|
||||||
GstPad *video_pad;
|
GstPad *video_pad;
|
||||||
|
|
||||||
|
GstIndex *index;
|
||||||
|
gint index_id;
|
||||||
|
|
||||||
|
GArray * times;
|
||||||
|
GArray * filepositions;
|
||||||
|
|
||||||
GstAdapter *adapter;
|
GstAdapter *adapter;
|
||||||
|
|
||||||
|
@ -68,6 +74,7 @@ struct _GstFLVDemux
|
||||||
GstFLVDemuxState state;
|
GstFLVDemuxState state;
|
||||||
|
|
||||||
guint64 offset;
|
guint64 offset;
|
||||||
|
guint64 cur_tag_offset;
|
||||||
GstClockTime duration;
|
GstClockTime duration;
|
||||||
guint64 tag_size;
|
guint64 tag_size;
|
||||||
guint64 tag_data_size;
|
guint64 tag_data_size;
|
||||||
|
@ -106,5 +113,8 @@ struct _GstFLVDemuxClass
|
||||||
|
|
||||||
GType gst_flv_demux_get_type (void);
|
GType gst_flv_demux_get_type (void);
|
||||||
|
|
||||||
|
gboolean gst_flv_demux_query (GstPad * pad, GstQuery * query);
|
||||||
|
gboolean gst_flv_demux_src_event (GstPad * pad, GstEvent * event);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
#endif /* __FLV_DEMUX_H__ */
|
#endif /* __FLV_DEMUX_H__ */
|
||||||
|
|
|
@ -44,8 +44,8 @@ FLV_GET_STRING (const guint8 * data, size_t data_size)
|
||||||
guint32 string_size = 0;
|
guint32 string_size = 0;
|
||||||
gchar *string = NULL;
|
gchar *string = NULL;
|
||||||
|
|
||||||
g_return_val_if_fail (data != NULL, 0);
|
g_return_val_if_fail (data != NULL, NULL);
|
||||||
g_return_val_if_fail (data_size >= 3, 0);
|
g_return_val_if_fail (data_size >= 2, NULL);
|
||||||
|
|
||||||
string_size = GST_READ_UINT16_BE (data);
|
string_size = GST_READ_UINT16_BE (data);
|
||||||
if (G_UNLIKELY (string_size > data_size)) {
|
if (G_UNLIKELY (string_size > data_size)) {
|
||||||
|
@ -73,69 +73,17 @@ gst_flv_demux_query_types (GstPad * pad)
|
||||||
return query_types;
|
return query_types;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
|
||||||
gst_flv_demux_query (GstPad * pad, GstQuery * query)
|
|
||||||
{
|
|
||||||
gboolean res = TRUE;
|
|
||||||
GstFLVDemux *demux;
|
|
||||||
|
|
||||||
demux = GST_FLV_DEMUX (gst_pad_get_parent (pad));
|
|
||||||
|
|
||||||
switch (GST_QUERY_TYPE (query)) {
|
|
||||||
case GST_QUERY_DURATION:
|
|
||||||
{
|
|
||||||
GstFormat format;
|
|
||||||
|
|
||||||
gst_query_parse_duration (query, &format, NULL);
|
|
||||||
|
|
||||||
/* duration is time only */
|
|
||||||
if (format != GST_FORMAT_TIME) {
|
|
||||||
GST_DEBUG_OBJECT (demux, "duration query only supported for time "
|
|
||||||
"format");
|
|
||||||
res = FALSE;
|
|
||||||
goto beach;
|
|
||||||
}
|
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (pad, "duration query, replying %" GST_TIME_FORMAT,
|
|
||||||
GST_TIME_ARGS (demux->duration));
|
|
||||||
|
|
||||||
gst_query_set_duration (query, GST_FORMAT_TIME, demux->duration);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case GST_QUERY_LATENCY:
|
|
||||||
{
|
|
||||||
GstPad *peer;
|
|
||||||
|
|
||||||
if ((peer = gst_pad_get_peer (demux->sinkpad))) {
|
|
||||||
/* query latency on peer pad */
|
|
||||||
res = gst_pad_query (peer, query);
|
|
||||||
gst_object_unref (peer);
|
|
||||||
} else {
|
|
||||||
/* no peer, we don't know */
|
|
||||||
res = FALSE;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
res = FALSE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
beach:
|
|
||||||
gst_object_unref (demux);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t
|
static size_t
|
||||||
gst_flv_parse_metadata_item (GstFLVDemux * demux, const guint8 * data,
|
gst_flv_parse_metadata_item (GstFLVDemux * demux, const guint8 * data,
|
||||||
size_t data_size)
|
size_t data_size, gboolean * end_marker)
|
||||||
{
|
{
|
||||||
gchar *tag_name = NULL;
|
gchar *tag_name = NULL;
|
||||||
guint8 tag_type = 0;
|
guint8 tag_type = 0;
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
|
||||||
|
/* Initialize the end_marker flag to FALSE */
|
||||||
|
*end_marker = FALSE;
|
||||||
|
|
||||||
/* Name of the tag */
|
/* Name of the tag */
|
||||||
tag_name = FLV_GET_STRING (data, data_size);
|
tag_name = FLV_GET_STRING (data, data_size);
|
||||||
if (G_UNLIKELY (!tag_name)) {
|
if (G_UNLIKELY (!tag_name)) {
|
||||||
|
@ -246,6 +194,114 @@ gst_flv_parse_metadata_item (GstFLVDemux * demux, const guint8 * data,
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 3: // Object
|
||||||
|
{
|
||||||
|
gboolean end_of_object_marker = FALSE;
|
||||||
|
|
||||||
|
while (!end_of_object_marker && offset < data_size) {
|
||||||
|
size_t read = gst_flv_parse_metadata_item (demux, data + offset,
|
||||||
|
data_size - offset, &end_of_object_marker);
|
||||||
|
|
||||||
|
if (G_UNLIKELY (!read)) {
|
||||||
|
GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += read;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 9: // End marker
|
||||||
|
{
|
||||||
|
GST_DEBUG_OBJECT (demux, "end marker ?");
|
||||||
|
if (tag_name[0] == '\0') {
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (demux, "end marker detected");
|
||||||
|
|
||||||
|
*end_marker = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 10: // Array
|
||||||
|
{
|
||||||
|
guint32 nb_elems = GST_READ_UINT32_BE (data + offset);
|
||||||
|
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (demux, "array has %d elements", nb_elems);
|
||||||
|
|
||||||
|
if (!strcmp (tag_name, "times")) {
|
||||||
|
if (demux->times) {
|
||||||
|
g_array_free (demux->times, TRUE);
|
||||||
|
}
|
||||||
|
demux->times = g_array_new (FALSE, TRUE, sizeof (gdouble));
|
||||||
|
} else if (!strcmp (tag_name, "filepositions")) {
|
||||||
|
if (demux->filepositions) {
|
||||||
|
g_array_free (demux->filepositions, TRUE);
|
||||||
|
}
|
||||||
|
demux->filepositions = g_array_new (FALSE, TRUE, sizeof (gdouble));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (nb_elems--) {
|
||||||
|
guint8 elem_type = GST_READ_UINT8 (data + offset);
|
||||||
|
|
||||||
|
offset++;
|
||||||
|
|
||||||
|
switch (elem_type) {
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
union
|
||||||
|
{
|
||||||
|
guint64 value_uint64;
|
||||||
|
gdouble value_double;
|
||||||
|
} value_union;
|
||||||
|
|
||||||
|
value_union.value_uint64 = GST_READ_UINT64_BE (data + offset);
|
||||||
|
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (demux, "element is a double %f",
|
||||||
|
value_union.value_double);
|
||||||
|
|
||||||
|
if (!strcmp (tag_name, "times") && demux->times) {
|
||||||
|
g_array_append_val (demux->times, value_union.value_double);
|
||||||
|
} else if (!strcmp (tag_name, "filepositions") &&
|
||||||
|
demux->filepositions) {
|
||||||
|
g_array_append_val (demux->filepositions,
|
||||||
|
value_union.value_double);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
GST_WARNING_OBJECT (demux, "unsupported array element type %d",
|
||||||
|
elem_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 11: // Date
|
||||||
|
{
|
||||||
|
union
|
||||||
|
{
|
||||||
|
guint64 value_uint64;
|
||||||
|
gdouble value_double;
|
||||||
|
} value_union;
|
||||||
|
|
||||||
|
value_union.value_uint64 = GST_READ_UINT64_BE (data + offset);
|
||||||
|
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
|
/* There are 2 additional bytes */
|
||||||
|
offset += 2;
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (demux, "%s => (date as a double) %f", tag_name,
|
||||||
|
value_union.value_double);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
GST_WARNING_OBJECT (demux, "unsupported tag type %d", tag_type);
|
GST_WARNING_OBJECT (demux, "unsupported tag type %d", tag_type);
|
||||||
}
|
}
|
||||||
|
@ -266,12 +322,14 @@ gst_flv_parse_tag_script (GstFLVDemux * demux, const guint8 * data,
|
||||||
GST_LOG_OBJECT (demux, "parsing a script tag");
|
GST_LOG_OBJECT (demux, "parsing a script tag");
|
||||||
|
|
||||||
if (GST_READ_UINT8 (data + offset++) == 2) {
|
if (GST_READ_UINT8 (data + offset++) == 2) {
|
||||||
|
guint i;
|
||||||
gchar *function_name = FLV_GET_STRING (data + offset, data_size - offset);
|
gchar *function_name = FLV_GET_STRING (data + offset, data_size - offset);
|
||||||
|
|
||||||
GST_LOG_OBJECT (demux, "function name is %s", function_name);
|
GST_LOG_OBJECT (demux, "function name is %s", function_name);
|
||||||
|
|
||||||
if (!strcmp (function_name, "onMetaData")) {
|
if (!strcmp (function_name, "onMetaData")) {
|
||||||
guint32 nb_elems = 0;
|
guint32 nb_elems = 0;
|
||||||
|
gboolean end_marker = FALSE;
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (demux, "we have a metadata script object");
|
GST_DEBUG_OBJECT (demux, "we have a metadata script object");
|
||||||
|
|
||||||
|
@ -285,9 +343,9 @@ gst_flv_parse_tag_script (GstFLVDemux * demux, const guint8 * data,
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (demux, "there are %d elements in the array", nb_elems);
|
GST_DEBUG_OBJECT (demux, "there are %d elements in the array", nb_elems);
|
||||||
|
|
||||||
while (nb_elems--) {
|
while (nb_elems-- && !end_marker) {
|
||||||
size_t read = gst_flv_parse_metadata_item (demux, data + offset,
|
size_t read = gst_flv_parse_metadata_item (demux, data + offset,
|
||||||
data_size - offset);
|
data_size - offset, &end_marker);
|
||||||
|
|
||||||
if (G_UNLIKELY (!read)) {
|
if (G_UNLIKELY (!read)) {
|
||||||
GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
|
GST_WARNING_OBJECT (demux, "failed reading a tag, skipping");
|
||||||
|
@ -300,6 +358,21 @@ gst_flv_parse_tag_script (GstFLVDemux * demux, const guint8 * data,
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (function_name);
|
g_free (function_name);
|
||||||
|
|
||||||
|
if (demux->index) {
|
||||||
|
/* If an index was found, insert associations */
|
||||||
|
for (i = 0; i < MIN (demux->times->len, demux->filepositions->len); i++) {
|
||||||
|
guint64 time, fileposition;
|
||||||
|
|
||||||
|
time = g_array_index (demux->times, gdouble, i) * GST_SECOND;
|
||||||
|
fileposition = g_array_index (demux->filepositions, gdouble, i);
|
||||||
|
GST_LOG_OBJECT (demux, "adding association %" GST_TIME_FORMAT "-> %"
|
||||||
|
G_GUINT64_FORMAT, GST_TIME_ARGS (time), fileposition);
|
||||||
|
gst_index_add_association (demux->index, demux->index_id,
|
||||||
|
GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time,
|
||||||
|
GST_FORMAT_BYTES, fileposition, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -427,6 +500,8 @@ gst_flv_parse_tag_audio (GstFLVDemux * demux, const guint8 * data,
|
||||||
GST_DEBUG_FUNCPTR (gst_flv_demux_query_types));
|
GST_DEBUG_FUNCPTR (gst_flv_demux_query_types));
|
||||||
gst_pad_set_query_function (demux->audio_pad,
|
gst_pad_set_query_function (demux->audio_pad,
|
||||||
GST_DEBUG_FUNCPTR (gst_flv_demux_query));
|
GST_DEBUG_FUNCPTR (gst_flv_demux_query));
|
||||||
|
gst_pad_set_event_function (demux->audio_pad,
|
||||||
|
GST_DEBUG_FUNCPTR (gst_flv_demux_src_event));
|
||||||
|
|
||||||
/* We need to set caps before adding */
|
/* We need to set caps before adding */
|
||||||
gst_element_add_pad (GST_ELEMENT (demux),
|
gst_element_add_pad (GST_ELEMENT (demux),
|
||||||
|
@ -528,6 +603,8 @@ gst_flv_parse_tag_audio (GstFLVDemux * demux, const guint8 * data,
|
||||||
demux->new_seg_event = gst_event_new_new_segment (FALSE,
|
demux->new_seg_event = gst_event_new_new_segment (FALSE,
|
||||||
demux->segment->rate, demux->segment->format,
|
demux->segment->rate, demux->segment->format,
|
||||||
demux->segment->last_stop, -1, demux->segment->last_stop);
|
demux->segment->last_stop, -1, demux->segment->last_stop);
|
||||||
|
} else {
|
||||||
|
GST_DEBUG_OBJECT (demux, "pushing pre-generated newsegment event");
|
||||||
}
|
}
|
||||||
|
|
||||||
gst_pad_push_event (demux->audio_pad, gst_event_ref (demux->new_seg_event));
|
gst_pad_push_event (demux->audio_pad, gst_event_ref (demux->new_seg_event));
|
||||||
|
@ -556,14 +633,19 @@ gst_flv_parse_tag_video (GstFLVDemux * demux, const guint8 * data,
|
||||||
{
|
{
|
||||||
GstFlowReturn ret = GST_FLOW_OK;
|
GstFlowReturn ret = GST_FLOW_OK;
|
||||||
GstBuffer *buffer = NULL;
|
GstBuffer *buffer = NULL;
|
||||||
guint32 pts = 0, codec_tag = 0, codec_data = 0;
|
guint32 pts = 0, codec_tag = 0, codec_data = 0, pts_ext = 0;
|
||||||
gboolean keyframe = FALSE;
|
gboolean keyframe = FALSE;
|
||||||
guint8 flags = 0;
|
guint8 flags = 0;
|
||||||
|
|
||||||
GST_LOG_OBJECT (demux, "parsing a video tag");
|
GST_LOG_OBJECT (demux, "parsing a video tag");
|
||||||
|
|
||||||
/* Grab information about audio tag */
|
/* Grab information about video tag */
|
||||||
pts = FLV_GET_BEUI24 (data, data_size);
|
pts = FLV_GET_BEUI24 (data, data_size);
|
||||||
|
/* read the pts extension to 32 bits integer */
|
||||||
|
pts_ext = GST_READ_UINT8 (data + 3);
|
||||||
|
/* Combine them */
|
||||||
|
pts |= pts_ext << 24;
|
||||||
|
/* Skip the stream id and go directly to the flags */
|
||||||
flags = GST_READ_UINT8 (data + 7);
|
flags = GST_READ_UINT8 (data + 7);
|
||||||
|
|
||||||
/* Keyframe */
|
/* Keyframe */
|
||||||
|
@ -644,6 +726,8 @@ gst_flv_parse_tag_video (GstFLVDemux * demux, const guint8 * data,
|
||||||
GST_DEBUG_FUNCPTR (gst_flv_demux_query_types));
|
GST_DEBUG_FUNCPTR (gst_flv_demux_query_types));
|
||||||
gst_pad_set_query_function (demux->video_pad,
|
gst_pad_set_query_function (demux->video_pad,
|
||||||
GST_DEBUG_FUNCPTR (gst_flv_demux_query));
|
GST_DEBUG_FUNCPTR (gst_flv_demux_query));
|
||||||
|
gst_pad_set_event_function (demux->video_pad,
|
||||||
|
GST_DEBUG_FUNCPTR (gst_flv_demux_src_event));
|
||||||
|
|
||||||
/* We need to set caps before adding */
|
/* We need to set caps before adding */
|
||||||
gst_element_add_pad (GST_ELEMENT (demux),
|
gst_element_add_pad (GST_ELEMENT (demux),
|
||||||
|
@ -727,6 +811,16 @@ gst_flv_parse_tag_video (GstFLVDemux * demux, const guint8 * data,
|
||||||
|
|
||||||
if (!keyframe) {
|
if (!keyframe) {
|
||||||
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
|
||||||
|
} else {
|
||||||
|
if (demux->index) {
|
||||||
|
GST_LOG_OBJECT (demux, "adding association %" GST_TIME_FORMAT "-> %"
|
||||||
|
G_GUINT64_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
|
||||||
|
demux->cur_tag_offset);
|
||||||
|
gst_index_add_association (demux->index, demux->index_id,
|
||||||
|
GST_ASSOCIATION_FLAG_KEY_UNIT,
|
||||||
|
GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buffer),
|
||||||
|
GST_FORMAT_BYTES, demux->cur_tag_offset, NULL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (G_UNLIKELY (demux->video_need_discont)) {
|
if (G_UNLIKELY (demux->video_need_discont)) {
|
||||||
|
@ -746,6 +840,8 @@ gst_flv_parse_tag_video (GstFLVDemux * demux, const guint8 * data,
|
||||||
demux->new_seg_event = gst_event_new_new_segment (FALSE,
|
demux->new_seg_event = gst_event_new_new_segment (FALSE,
|
||||||
demux->segment->rate, demux->segment->format,
|
demux->segment->rate, demux->segment->format,
|
||||||
demux->segment->last_stop, -1, demux->segment->last_stop);
|
demux->segment->last_stop, -1, demux->segment->last_stop);
|
||||||
|
} else {
|
||||||
|
GST_DEBUG_OBJECT (demux, "pushing pre-generated newsegment event");
|
||||||
}
|
}
|
||||||
|
|
||||||
gst_pad_push_event (demux->video_pad, gst_event_ref (demux->new_seg_event));
|
gst_pad_push_event (demux->video_pad, gst_event_ref (demux->new_seg_event));
|
||||||
|
|
Loading…
Reference in a new issue