gstreamer/ext/dash/gstdashdemux.c

1099 lines
35 KiB
C
Raw Normal View History

2013-05-08 14:13:32 +00:00
/*
* DASH demux plugin for GStreamer
*
* gstdashdemux.c
*
* Copyright (C) 2012 Orange
2014-08-26 19:45:46 +00:00
*
2013-05-08 14:13:32 +00:00
* Authors:
* David Corvoysier <david.corvoysier@orange.com>
* Hamid Zakari <hamid.zakari@gmail.com>
*
* Copyright (C) 2013 Smart TV Alliance
* Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>, Collabora Ltd.
*
2013-05-08 14:13:32 +00:00
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
2012-09-28 10:55:27 +00:00
* License along with this library (COPYING); if not, write to the
2013-05-08 14:13:32 +00:00
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:element-dashdemux
*
* DASH demuxer element.
* <title>Example launch line</title>
* |[
* gst-launch playbin2 uri="http://www-itec.uni-klu.ac.at/ftp/datasets/mmsys12/RedBullPlayStreets/redbull_4s/RedBullPlayStreets_4s_isoffmain_DIS_23009_1_v_2_1c2_2011_08_30.mpd"
* ]|
*/
/* Implementation notes:
2014-08-26 19:45:46 +00:00
*
* The following section describes how dashdemux works internally.
2014-08-26 19:45:46 +00:00
*
* Introduction:
2014-08-26 19:45:46 +00:00
*
* dashdemux is a "fake" demux, as unlike traditional demux elements, it
* doesn't split data streams contained in an enveloppe to expose them
* to downstream decoding elements.
2014-08-26 19:45:46 +00:00
*
* Instead, it parses an XML file called a manifest to identify a set of
* individual stream fragments it needs to fetch and expose to the actual
2014-08-26 19:45:46 +00:00
* demux elements that will handle them (this behavior is sometimes
* referred as the "demux after a demux" scenario).
2014-08-26 19:45:46 +00:00
*
* For a given section of content, several representations corresponding
* to different bitrates may be available: dashdemux will select the most
2014-08-26 19:45:46 +00:00
* appropriate representation based on local conditions (typically the
* available bandwidth and the amount of buffering available, capped by
2014-08-26 19:45:46 +00:00
* a maximum allowed bitrate).
*
* The representation selection algorithm can be configured using
* specific properties: max bitrate, min/max buffering, bandwidth ratio.
2014-08-26 19:45:46 +00:00
*
*
* General Design:
2014-08-26 19:45:46 +00:00
*
* dashdemux has a single sink pad that accepts the data corresponding
* to the manifest, typically fetched from an HTTP or file source.
2014-08-26 19:45:46 +00:00
*
* dashdemux exposes the streams it recreates based on the fragments it
* fetches through dedicated src pads corresponding to the caps of the
* fragments container (ISOBMFF/MP4 or MPEG2TS).
2014-08-26 19:45:46 +00:00
*
* During playback, new representations will typically be exposed as a
* new set of pads (see 'Switching between representations' below).
2014-08-26 19:45:46 +00:00
*
* Fragments downloading is performed using a dedicated task that fills
* an internal queue. Another task is in charge of popping fragments
* from the queue and pushing them downstream.
2014-08-26 19:45:46 +00:00
*
* Switching between representations:
2014-08-26 19:45:46 +00:00
*
* Decodebin supports scenarios allowing to seamlessly switch from one
* stream to another inside the same "decoding chain".
2014-08-26 19:45:46 +00:00
*
* To achieve that, it combines the elements it autoplugged in chains
* and groups, allowing only one decoding group to be active at a given
* time for a given chain.
*
2014-08-26 19:45:46 +00:00
* A chain can signal decodebin that it is complete by sending a
* no-more-pads event, but even after that new pads can be added to
* create new subgroups, providing that a new no-more-pads event is sent.
*
* We take advantage of that to dynamically create a new decoding group
* in order to select a different representation during playback.
*
* Typically, assuming that each fragment contains both audio and video,
* the following tree would be created:
2014-08-26 19:45:46 +00:00
*
* chain "DASH Demux"
* |_ group "Representation set 1"
* | |_ chain "Qt Demux 0"
* | |_ group "Stream 0"
* | |_ chain "H264"
* | |_ chain "AAC"
* |_ group "Representation set 2"
* |_ chain "Qt Demux 1"
* |_ group "Stream 1"
* |_ chain "H264"
* |_ chain "AAC"
*
* Or, if audio and video are contained in separate fragments:
*
* chain "DASH Demux"
* |_ group "Representation set 1"
* | |_ chain "Qt Demux 0"
* | | |_ group "Stream 0"
* | | |_ chain "H264"
* | |_ chain "Qt Demux 1"
* | |_ group "Stream 1"
2014-08-26 19:45:46 +00:00
* | |_ chain "AAC"
* |_ group "Representation set 2"
* |_ chain "Qt Demux 3"
* | |_ group "Stream 2"
* | |_ chain "H264"
* |_ chain "Qt Demux 4"
* |_ group "Stream 3"
2014-08-26 19:45:46 +00:00
* |_ chain "AAC"
*
* In both cases, when switching from Set 1 to Set 2 an EOS is sent on
* each end pad corresponding to Rep 0, triggering the "drain" state to
* propagate upstream.
* Once both EOS have been processed, the "Set 1" group is completely
* drained, and decodebin2 will switch to the "Set 2" group.
2014-08-26 19:45:46 +00:00
*
* Note: nothing can be pushed to the new decoding group before the
* old one has been drained, which means that in order to be able to
* adapt quickly to bandwidth changes, we will not be able to rely
* on downstream buffering, and will instead manage an internal queue.
2014-08-26 19:45:46 +00:00
*
*/
2013-05-08 14:13:32 +00:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <inttypes.h>
2013-05-08 14:13:32 +00:00
#include <gst/base/gsttypefindhelper.h>
2014-08-26 19:45:46 +00:00
#include <gst/tag/tag.h>
#include "gst/gst-i18n-plugin.h"
2013-05-08 14:13:32 +00:00
#include "gstdashdemux.h"
#include "gstdash_debug.h"
2013-05-08 14:13:32 +00:00
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
2013-05-08 14:13:32 +00:00
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/dash+xml"));
2013-05-08 14:13:32 +00:00
GST_DEBUG_CATEGORY (gst_dash_demux_debug);
2013-05-08 14:13:32 +00:00
#define GST_CAT_DEFAULT gst_dash_demux_debug
enum
{
PROP_0,
PROP_MAX_BUFFERING_TIME,
PROP_BANDWIDTH_USAGE,
PROP_MAX_BITRATE,
PROP_LAST
};
/* Default values for properties */
2012-11-21 13:14:34 +00:00
#define DEFAULT_MAX_BUFFERING_TIME 30 /* in seconds */
#define DEFAULT_BANDWIDTH_USAGE 0.8 /* 0 to 1 */
#define DEFAULT_MAX_BITRATE 24000000 /* in bit/s */
2013-05-08 14:13:32 +00:00
/* GObject */
static void gst_dash_demux_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_dash_demux_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_dash_demux_dispose (GObject * obj);
2014-08-26 19:45:46 +00:00
/* GstAdaptiveDemux */
static GstClockTime gst_dash_demux_get_duration (GstAdaptiveDemux * ademux);
static gboolean gst_dash_demux_is_live (GstAdaptiveDemux * ademux);
static void gst_dash_demux_reset (GstAdaptiveDemux * ademux);
static gboolean gst_dash_demux_process_manifest (GstAdaptiveDemux * ademux,
GstBuffer * buf);
static gboolean gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
static GstFlowReturn
gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream);
static GstFlowReturn gst_dash_demux_stream_seek (GstAdaptiveDemuxStream *
stream, GstClockTime ts);
static GstFlowReturn
gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream);
static gboolean gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream *
stream, guint64 bitrate);
static gint64
gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux * demux);
static GstFlowReturn
gst_dash_demux_update_manifest (GstAdaptiveDemux * demux, GstBuffer * buf);
static gint64
gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
stream);
static void gst_dash_demux_advance_period (GstAdaptiveDemux * demux);
static gboolean gst_dash_demux_has_next_period (GstAdaptiveDemux * demux);
2013-05-08 14:13:32 +00:00
/* GstDashDemux */
2012-12-20 08:04:28 +00:00
static gboolean gst_dash_demux_setup_all_streams (GstDashDemux * demux);
2014-08-26 19:45:46 +00:00
static GstCaps *gst_dash_demux_get_input_caps (GstDashDemux * demux,
GstActiveStream * stream);
2014-08-26 19:45:46 +00:00
static GstPad *gst_dash_demux_create_pad (GstDashDemux * demux);
2013-05-08 14:13:32 +00:00
2013-04-18 10:41:09 +00:00
#define gst_dash_demux_parent_class parent_class
2014-08-26 19:45:46 +00:00
G_DEFINE_TYPE_WITH_CODE (GstDashDemux, gst_dash_demux, GST_TYPE_ADAPTIVE_DEMUX,
2013-04-18 10:41:09 +00:00
GST_DEBUG_CATEGORY_INIT (gst_dash_demux_debug, "dashdemux", 0,
2014-08-26 19:45:46 +00:00
"dashdemux element");
);
2013-05-08 14:13:32 +00:00
static void
gst_dash_demux_dispose (GObject * obj)
{
GstDashDemux *demux = GST_DASH_DEMUX (obj);
2014-08-26 19:45:46 +00:00
gst_dash_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
2014-08-26 19:45:46 +00:00
if (demux->client) {
gst_mpd_client_free (demux->client);
demux->client = NULL;
2013-05-08 14:13:32 +00:00
}
g_mutex_clear (&demux->client_lock);
2013-05-08 14:13:32 +00:00
G_OBJECT_CLASS (parent_class)->dispose (obj);
}
static void
gst_dash_demux_class_init (GstDashDemuxClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
2014-08-26 19:45:46 +00:00
GstAdaptiveDemuxClass *gstadaptivedemux_class;
2013-05-08 14:13:32 +00:00
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
2014-08-26 19:45:46 +00:00
gstadaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
2013-05-08 14:13:32 +00:00
gobject_class->set_property = gst_dash_demux_set_property;
gobject_class->get_property = gst_dash_demux_get_property;
gobject_class->dispose = gst_dash_demux_dispose;
#ifndef GST_REMOVE_DEPRECATED
2013-05-08 14:13:32 +00:00
g_object_class_install_property (gobject_class, PROP_MAX_BUFFERING_TIME,
g_param_spec_uint ("max-buffering-time", "Maximum buffering time",
"Maximum number of seconds of buffer accumulated during playback"
"(deprecated)",
2013-05-08 14:13:32 +00:00
2, G_MAXUINT, DEFAULT_MAX_BUFFERING_TIME,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
#endif
2013-05-08 14:13:32 +00:00
g_object_class_install_property (gobject_class, PROP_BANDWIDTH_USAGE,
g_param_spec_float ("bandwidth-usage",
"Bandwidth usage [0..1]",
"Percentage of the available bandwidth to use when selecting representations",
0, 1, DEFAULT_BANDWIDTH_USAGE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
g_param_spec_uint ("max-bitrate", "Max bitrate",
"Max of bitrate supported by target decoder",
1000, G_MAXUINT, DEFAULT_MAX_BITRATE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2013-04-18 10:41:09 +00:00
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&srctemplate));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&sinktemplate));
gst_element_class_set_static_metadata (gstelement_class,
"DASH Demuxer",
"Codec/Demuxer/Adaptive",
2013-04-18 10:41:09 +00:00
"Dynamic Adaptive Streaming over HTTP demuxer",
"David Corvoysier <david.corvoysier@orange.com>\n\
Hamid Zakari <hamid.zakari@gmail.com>\n\
Gianluca Gennari <gennarone@gmail.com>");
2014-08-26 19:45:46 +00:00
gstadaptivedemux_class->get_duration = gst_dash_demux_get_duration;
gstadaptivedemux_class->is_live = gst_dash_demux_is_live;
gstadaptivedemux_class->reset = gst_dash_demux_reset;
gstadaptivedemux_class->seek = gst_dash_demux_seek;
gstadaptivedemux_class->process_manifest = gst_dash_demux_process_manifest;
gstadaptivedemux_class->update_manifest = gst_dash_demux_update_manifest;
gstadaptivedemux_class->get_manifest_update_interval =
gst_dash_demux_get_manifest_update_interval;
gstadaptivedemux_class->has_next_period = gst_dash_demux_has_next_period;
gstadaptivedemux_class->advance_period = gst_dash_demux_advance_period;
gstadaptivedemux_class->stream_advance_fragment =
gst_dash_demux_stream_advance_fragment;
gstadaptivedemux_class->stream_get_fragment_waiting_time =
gst_dash_demux_stream_get_fragment_waiting_time;
gstadaptivedemux_class->stream_seek = gst_dash_demux_stream_seek;
gstadaptivedemux_class->stream_select_bitrate =
gst_dash_demux_stream_select_bitrate;
gstadaptivedemux_class->stream_update_fragment_info =
gst_dash_demux_stream_update_fragment_info;
2013-05-08 14:13:32 +00:00
}
static void
2013-04-18 10:41:09 +00:00
gst_dash_demux_init (GstDashDemux * demux)
2013-05-08 14:13:32 +00:00
{
gst_segment_init (&demux->segment, GST_FORMAT_TIME);
2013-05-08 14:13:32 +00:00
/* Properties */
demux->max_buffering_time = DEFAULT_MAX_BUFFERING_TIME * GST_SECOND;
demux->bandwidth_usage = DEFAULT_BANDWIDTH_USAGE;
demux->max_bitrate = DEFAULT_MAX_BITRATE;
g_mutex_init (&demux->client_lock);
2014-08-26 19:45:46 +00:00
gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
sizeof (GstDashDemuxStream));
2013-05-08 14:13:32 +00:00
}
static void
gst_dash_demux_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstDashDemux *demux = GST_DASH_DEMUX (object);
switch (prop_id) {
case PROP_MAX_BUFFERING_TIME:
demux->max_buffering_time = g_value_get_uint (value) * GST_SECOND;
break;
case PROP_BANDWIDTH_USAGE:
demux->bandwidth_usage = g_value_get_float (value);
break;
case PROP_MAX_BITRATE:
demux->max_bitrate = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_dash_demux_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstDashDemux *demux = GST_DASH_DEMUX (object);
switch (prop_id) {
case PROP_MAX_BUFFERING_TIME:
g_value_set_uint (value, demux->max_buffering_time / GST_SECOND);
2013-05-08 14:13:32 +00:00
break;
case PROP_BANDWIDTH_USAGE:
g_value_set_float (value, demux->bandwidth_usage);
break;
case PROP_MAX_BITRATE:
g_value_set_uint (value, demux->max_bitrate);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_dash_demux_setup_mpdparser_streams (GstDashDemux * demux,
GstMpdClient * client)
{
gboolean has_streams = FALSE;
GList *adapt_sets, *iter;
adapt_sets = gst_mpd_client_get_adaptation_sets (client);
for (iter = adapt_sets; iter; iter = g_list_next (iter)) {
GstAdaptationSetNode *adapt_set_node = iter->data;
gst_mpd_client_setup_streaming (client, adapt_set_node);
has_streams = TRUE;
}
if (!has_streams) {
GST_ELEMENT_ERROR (demux, STREAM, DEMUX, ("Manifest has no playable "
"streams"), ("No streams could be activated from the manifest"));
}
return has_streams;
}
static gboolean
gst_dash_demux_setup_all_streams (GstDashDemux * demux)
{
guint i;
GST_DEBUG_OBJECT (demux, "Setting up streams for period %d",
gst_mpd_client_get_period_index (demux->client));
/* clean old active stream list, if any */
gst_active_streams_free (demux->client);
if (!gst_dash_demux_setup_mpdparser_streams (demux, demux->client)) {
return FALSE;
}
GST_DEBUG_OBJECT (demux, "Creating stream objects");
for (i = 0; i < gst_mpdparser_get_nb_active_stream (demux->client); i++) {
GstDashDemuxStream *stream;
GstActiveStream *active_stream;
GstCaps *caps;
2014-08-26 19:45:46 +00:00
GstPad *srcpad;
gchar *lang = NULL;
2014-08-26 19:45:46 +00:00
GstTagList *tags = NULL;
active_stream = gst_mpdparser_get_active_stream_by_index (demux->client, i);
if (active_stream == NULL)
continue;
2014-08-26 19:45:46 +00:00
srcpad = gst_dash_demux_create_pad (demux);
caps = gst_dash_demux_get_input_caps (demux, active_stream);
GST_LOG_OBJECT (demux, "Creating stream %d %" GST_PTR_FORMAT, i, caps);
if (active_stream->cur_adapt_set) {
GstAdaptationSetNode *adp_set = active_stream->cur_adapt_set;
lang = adp_set->lang;
/* Fallback to the language in ContentComponent node */
if (lang == NULL && g_list_length (adp_set->ContentComponents) == 1) {
GstContentComponentNode *cc_node = adp_set->ContentComponents->data;
lang = cc_node->lang;
}
}
if (lang) {
if (gst_tag_check_language_code (lang))
tags = gst_tag_list_new (GST_TAG_LANGUAGE_CODE, lang, NULL);
else
tags = gst_tag_list_new (GST_TAG_LANGUAGE_NAME, lang, NULL);
}
2014-08-26 19:45:46 +00:00
stream = (GstDashDemuxStream *)
gst_adaptive_demux_stream_new (GST_ADAPTIVE_DEMUX_CAST (demux), srcpad);
stream->active_stream = active_stream;
gst_adaptive_demux_stream_set_caps (GST_ADAPTIVE_DEMUX_STREAM_CAST (stream),
caps);
if (tags)
gst_adaptive_demux_stream_set_tags (GST_ADAPTIVE_DEMUX_STREAM_CAST
(stream), tags);
stream->index = i;
}
return TRUE;
}
2014-08-26 19:45:46 +00:00
static GstClockTime
gst_dash_demux_get_duration (GstAdaptiveDemux * ademux)
2013-05-08 14:13:32 +00:00
{
2014-08-26 19:45:46 +00:00
GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
2014-08-26 19:45:46 +00:00
g_return_val_if_fail (demux->client != NULL, GST_CLOCK_TIME_NONE);
2014-08-26 19:45:46 +00:00
return gst_mpd_client_get_media_presentation_duration (demux->client);
}
2014-08-26 19:45:46 +00:00
static gboolean
gst_dash_demux_is_live (GstAdaptiveDemux * ademux)
{
GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
2014-08-26 19:45:46 +00:00
g_return_val_if_fail (demux->client != NULL, FALSE);
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
return gst_mpd_client_is_live (demux->client);
2013-05-08 14:13:32 +00:00
}
static gboolean
2014-08-26 19:45:46 +00:00
gst_dash_demux_setup_streams (GstAdaptiveDemux * demux)
2013-05-08 14:13:32 +00:00
{
2014-08-26 19:45:46 +00:00
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
gboolean ret = TRUE;
GstDateTime *now = NULL;
guint period_idx;
2014-08-26 19:45:46 +00:00
/* setup video, audio and subtitle streams, starting from first Period if
* non-live */
period_idx = 0;
if (gst_mpd_client_is_live (dashdemux->client)) {
/* get period index for period encompassing the current time */
now = gst_date_time_new_now_utc ();
if (dashdemux->client->mpd_node->suggestedPresentationDelay != -1) {
GstDateTime *target = gst_mpd_client_add_time_difference (now,
dashdemux->client->mpd_node->suggestedPresentationDelay * -1000);
gst_date_time_unref (now);
now = target;
}
period_idx =
gst_mpd_client_get_period_index_at_time (dashdemux->client, now);
if (period_idx == G_MAXUINT) {
#ifndef GST_DISABLE_GST_DEBUG
gchar *date_str = gst_date_time_to_iso8601_string (now);
GST_DEBUG_OBJECT (demux, "Unable to find live period active at %s",
date_str);
g_free (date_str);
#endif
ret = FALSE;
goto done;
}
}
if (!gst_mpd_client_set_period_index (dashdemux->client, period_idx) ||
2014-08-26 19:45:46 +00:00
!gst_dash_demux_setup_all_streams (dashdemux)) {
ret = FALSE;
goto done;
}
/* If stream is live, try to find the segment that
* is closest to current time */
if (gst_mpd_client_is_live (dashdemux->client)) {
GList *iter;
gint seg_idx;
GST_DEBUG_OBJECT (demux, "Seeking to current time of day for live stream ");
for (iter = demux->next_streams; iter; iter = g_list_next (iter)) {
2014-08-26 19:45:46 +00:00
GstDashDemuxStream *stream = iter->data;
GstActiveStream *active_stream = stream->active_stream;
/* Get segment index corresponding to current time. */
seg_idx =
gst_mpd_client_get_segment_index_at_time (dashdemux->client,
active_stream, now);
if (seg_idx < 0) {
GST_WARNING_OBJECT (demux,
"Failed to find a segment that is available "
"at this point in time for stream %d.", stream->index);
seg_idx = 0;
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
GST_INFO_OBJECT (demux,
"Segment index corresponding to current time for stream "
"%d is %d.", stream->index, seg_idx);
gst_mpd_client_set_segment_index (active_stream, seg_idx);
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
} else {
GST_DEBUG_OBJECT (demux, "Seeking to first segment for on-demand stream ");
2014-08-26 19:45:46 +00:00
/* start playing from the first segment */
gst_mpd_client_set_segment_index_for_all_streams (dashdemux->client, 0);
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
done:
if (now != NULL)
gst_date_time_unref (now);
2013-05-08 14:13:32 +00:00
return ret;
}
2014-08-26 19:45:46 +00:00
static gboolean
gst_dash_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
2013-05-08 14:13:32 +00:00
{
2014-08-26 19:45:46 +00:00
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
gboolean ret = FALSE;
gchar *manifest;
GstMapInfo mapinfo;
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
if (dashdemux->client)
gst_mpd_client_free (dashdemux->client);
dashdemux->client = gst_mpd_client_new ();
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
dashdemux->client->mpd_uri = g_strdup (demux->manifest_uri);
dashdemux->client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
2014-08-26 19:45:46 +00:00
GST_DEBUG_OBJECT (demux, "Fetched MPD file at URI: %s (base: %s)",
dashdemux->client->mpd_uri,
GST_STR_NULL (dashdemux->client->mpd_base_uri));
2014-08-26 19:45:46 +00:00
if (gst_buffer_map (buf, &mapinfo, GST_MAP_READ)) {
manifest = (gchar *) mapinfo.data;
if (gst_mpd_parse (dashdemux->client, manifest, mapinfo.size)) {
if (gst_mpd_client_setup_media_presentation (dashdemux->client)) {
ret = TRUE;
} else {
GST_ELEMENT_ERROR (demux, STREAM, DECODE,
("Incompatible manifest file."), (NULL));
}
}
gst_buffer_unmap (buf, &mapinfo);
} else {
GST_WARNING_OBJECT (demux, "Failed to map manifest buffer");
}
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
if (ret)
ret = gst_dash_demux_setup_streams (demux);
return ret;
2013-05-08 14:13:32 +00:00
}
static GstPad *
2014-08-26 19:45:46 +00:00
gst_dash_demux_create_pad (GstDashDemux * demux)
{
GstPad *pad;
GstPadTemplate *tmpl;
tmpl = gst_static_pad_template_get (&srctemplate);
/* Create and activate new pads */
pad = gst_ghost_pad_new_no_target_from_template (NULL, tmpl);
gst_object_unref (tmpl);
gst_pad_set_active (pad, TRUE);
GST_INFO_OBJECT (demux, "Creating srcpad %s:%s", GST_DEBUG_PAD_NAME (pad));
return pad;
}
2013-05-08 14:13:32 +00:00
static void
2014-08-26 19:45:46 +00:00
gst_dash_demux_reset (GstAdaptiveDemux * ademux)
2013-05-08 14:13:32 +00:00
{
2014-08-26 19:45:46 +00:00
GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
2014-08-26 19:45:46 +00:00
GST_DEBUG_OBJECT (demux, "Resetting demux");
2014-08-26 19:45:46 +00:00
demux->end_of_period = FALSE;
demux->end_of_manifest = FALSE;
2014-08-26 19:45:46 +00:00
if (demux->client) {
gst_mpd_client_free (demux->client);
demux->client = NULL;
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
demux->client = gst_mpd_client_new ();
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
gst_segment_init (&demux->segment, GST_FORMAT_TIME);
}
2014-08-26 19:45:46 +00:00
static GstCaps *
gst_dash_demux_get_video_input_caps (GstDashDemux * demux,
GstActiveStream * stream)
{
2014-08-26 19:45:46 +00:00
guint width = 0, height = 0;
const gchar *mimeType = NULL;
GstCaps *caps = NULL;
2014-08-26 19:45:46 +00:00
if (stream == NULL)
return NULL;
2014-08-26 19:45:46 +00:00
/* if bitstreamSwitching is true we dont need to swich pads on resolution change */
if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
width = gst_mpd_client_get_video_stream_width (stream);
height = gst_mpd_client_get_video_stream_height (stream);
}
2014-08-26 19:45:46 +00:00
mimeType = gst_mpd_client_get_stream_mimeType (stream);
if (mimeType == NULL)
return NULL;
2014-08-26 19:45:46 +00:00
caps = gst_caps_from_string (mimeType);
if (width > 0 && height > 0) {
gst_caps_set_simple (caps, "width", G_TYPE_INT, width, "height",
G_TYPE_INT, height, NULL);
}
2014-08-26 19:45:46 +00:00
return caps;
}
2014-08-26 19:45:46 +00:00
static GstCaps *
gst_dash_demux_get_audio_input_caps (GstDashDemux * demux,
GstActiveStream * stream)
{
2014-08-26 19:45:46 +00:00
guint rate = 0, channels = 0;
const gchar *mimeType;
GstCaps *caps = NULL;
2014-08-26 19:45:46 +00:00
if (stream == NULL)
return NULL;
2014-08-26 19:45:46 +00:00
/* if bitstreamSwitching is true we dont need to swich pads on rate/channels change */
if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
channels = gst_mpd_client_get_audio_stream_num_channels (stream);
rate = gst_mpd_client_get_audio_stream_rate (stream);
}
2014-08-26 19:45:46 +00:00
mimeType = gst_mpd_client_get_stream_mimeType (stream);
if (mimeType == NULL)
return NULL;
2014-08-26 19:45:46 +00:00
caps = gst_caps_from_string (mimeType);
if (rate > 0) {
gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
}
if (channels > 0) {
gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
}
2014-08-26 19:45:46 +00:00
return caps;
}
2014-08-26 19:45:46 +00:00
static GstCaps *
gst_dash_demux_get_application_input_caps (GstDashDemux * demux,
GstActiveStream * stream)
{
const gchar *mimeType;
GstCaps *caps = NULL;
2014-08-26 19:45:46 +00:00
if (stream == NULL)
return NULL;
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
mimeType = gst_mpd_client_get_stream_mimeType (stream);
if (mimeType == NULL)
return NULL;
2014-08-26 19:45:46 +00:00
caps = gst_caps_from_string (mimeType);
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
return caps;
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
static GstCaps *
gst_dash_demux_get_input_caps (GstDashDemux * demux, GstActiveStream * stream)
{
2014-08-26 19:45:46 +00:00
switch (stream->mimeType) {
case GST_STREAM_VIDEO:
return gst_dash_demux_get_video_input_caps (demux, stream);
case GST_STREAM_AUDIO:
return gst_dash_demux_get_audio_input_caps (demux, stream);
case GST_STREAM_APPLICATION:
return gst_dash_demux_get_application_input_caps (demux, stream);
default:
return GST_CAPS_NONE;
}
}
static GstFlowReturn
2014-08-26 19:45:46 +00:00
gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream)
{
2014-08-26 19:45:46 +00:00
GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
GstClockTime ts;
GstMediaFragmentInfo fragment;
gchar *path = NULL;
gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
dashstream->index, &ts)) {
if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream)) {
gst_mpd_client_get_next_header (dashdemux->client,
&path, dashstream->index,
&stream->fragment.header_range_start,
&stream->fragment.header_range_end);
if (path != NULL && strncmp (path, "http://", 7) != 0) {
stream->fragment.header_uri =
gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
dashstream->index), path);
g_free (path);
} else {
2014-08-26 19:45:46 +00:00
stream->fragment.header_uri = path;
}
2014-08-26 19:45:46 +00:00
path = NULL;
gst_mpd_client_get_next_header_index (dashdemux->client,
&path, dashstream->index,
&stream->fragment.index_range_start,
&stream->fragment.index_range_end);
if (path != NULL && strncmp (path, "http://", 7) != 0) {
stream->fragment.index_uri =
gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
dashstream->index), path);
g_free (path);
} else {
2014-08-26 19:45:46 +00:00
stream->fragment.index_uri = path;
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
}
2014-08-26 19:45:46 +00:00
gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
&fragment);
2012-10-17 08:02:39 +00:00
2014-08-26 19:45:46 +00:00
stream->fragment.uri = fragment.uri;
stream->fragment.timestamp = fragment.timestamp;
stream->fragment.duration = fragment.duration;
stream->fragment.range_start = fragment.range_start;
stream->fragment.range_end = fragment.range_end;
2014-08-26 19:45:46 +00:00
return GST_FLOW_OK;
}
2014-08-26 19:45:46 +00:00
return GST_FLOW_EOS;
}
2012-10-17 08:02:39 +00:00
2014-08-26 19:45:46 +00:00
static GstFlowReturn
gst_dash_demux_stream_seek (GstAdaptiveDemuxStream * stream, GstClockTime ts)
{
GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
2012-10-17 08:02:39 +00:00
2014-08-26 19:45:46 +00:00
gst_mpd_client_stream_seek (dashdemux->client, dashstream->active_stream, ts);
return GST_FLOW_OK;
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
static GstFlowReturn
gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream)
2013-05-08 14:13:32 +00:00
{
2014-08-26 19:45:46 +00:00
GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
2014-08-26 19:45:46 +00:00
return gst_mpd_client_advance_segment (dashdemux->client,
dashstream->active_stream, stream->demux->segment.rate > 0.0);
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
static gboolean
gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream * stream,
guint64 bitrate)
2013-05-08 14:13:32 +00:00
{
GstActiveStream *active_stream = NULL;
GList *rep_list = NULL;
2013-05-08 14:13:32 +00:00
gint new_index;
2014-08-26 19:45:46 +00:00
GstDashDemux *demux = GST_DASH_DEMUX_CAST (stream->demux);
GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
gboolean ret = FALSE;
2013-05-08 14:13:32 +00:00
2014-08-26 19:45:46 +00:00
active_stream = dashstream->active_stream;
if (active_stream == NULL) {
goto end;
}
/* retrieve representation list */
if (active_stream->cur_adapt_set)
rep_list = active_stream->cur_adapt_set->Representations;
2014-08-26 19:45:46 +00:00
if (!rep_list) {
goto end;
}
bitrate *= demux->bandwidth_usage;
GST_DEBUG_OBJECT (stream->pad,
"Trying to change to bitrate: %" G_GUINT64_FORMAT, bitrate);
/* get representation index with current max_bandwidth */
new_index = gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list, bitrate);
/* if no representation has the required bandwidth, take the lowest one */
if (new_index == -1)
new_index = gst_mpdparser_get_rep_idx_with_min_bandwidth (rep_list);
if (new_index != active_stream->representation_idx) {
GstRepresentationNode *rep = g_list_nth_data (rep_list, new_index);
GST_INFO_OBJECT (demux, "Changing representation idx: %d %d %u",
2014-08-26 19:45:46 +00:00
dashstream->index, new_index, rep->bandwidth);
if (gst_mpd_client_setup_representation (demux->client, active_stream, rep)) {
2014-08-26 19:45:46 +00:00
GstCaps *caps;
GST_INFO_OBJECT (demux, "Switching bitrate to %d",
active_stream->cur_representation->bandwidth);
2014-08-26 19:45:46 +00:00
caps = gst_dash_demux_get_input_caps (demux, active_stream);
gst_adaptive_demux_stream_set_caps (stream, caps);
ret = TRUE;
} else {
GST_WARNING_OBJECT (demux, "Can not switch representation, aborting...");
2013-05-08 14:13:32 +00:00
}
}
2014-08-26 19:45:46 +00:00
end:
return ret;
}
2014-08-26 19:45:46 +00:00
static gboolean
gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
2013-05-08 14:13:32 +00:00
{
2014-08-26 19:45:46 +00:00
gdouble rate;
GstFormat format;
GstSeekFlags flags;
GstSeekType start_type, stop_type;
gint64 start, stop;
GList *list;
GstClockTime current_pos, target_pos;
guint current_period;
GstStreamPeriod *period;
GList *iter;
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
&stop_type, &stop);
/* TODO check if start-type/stop-type is SET */
if (demux->segment.rate > 0.0)
target_pos = (GstClockTime) demux->segment.start;
else
target_pos = (GstClockTime) demux->segment.stop;
/* select the requested Period in the Media Presentation */
current_period = 0;
for (list = g_list_first (dashdemux->client->periods); list;
list = g_list_next (list)) {
period = list->data;
current_pos = period->start;
current_period = period->number;
GST_DEBUG_OBJECT (demux, "Looking at period %u pos %" GST_TIME_FORMAT,
current_period, GST_TIME_ARGS (current_pos));
if (current_pos <= target_pos
&& target_pos < current_pos + period->duration) {
break;
}
}
2014-08-26 19:45:46 +00:00
if (list == NULL) {
GST_WARNING_OBJECT (demux, "Could not find seeked Period");
return FALSE;
}
2014-08-26 19:45:46 +00:00
if (current_period != gst_mpd_client_get_period_index (dashdemux->client)) {
GST_DEBUG_OBJECT (demux, "Seeking to Period %d", current_period);
2014-08-26 19:45:46 +00:00
/* clean old active stream list, if any */
gst_active_streams_free (dashdemux->client);
2014-08-26 19:45:46 +00:00
/* setup video, audio and subtitle streams, starting from the new Period */
if (!gst_mpd_client_set_period_index (dashdemux->client, current_period)
|| !gst_dash_demux_setup_all_streams (dashdemux))
return FALSE;
}
2014-08-26 19:45:46 +00:00
/* Update the current sequence on all streams */
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
GstDashDemuxStream *dashstream = iter->data;
gst_mpd_client_stream_seek (dashdemux->client, dashstream->active_stream,
target_pos);
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
return TRUE;
2013-05-08 14:13:32 +00:00
}
2014-08-26 19:45:46 +00:00
static gint64
gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
{
2014-08-26 19:45:46 +00:00
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
return dashdemux->client->mpd_node->minimumUpdatePeriod * 1000;
}
static GstFlowReturn
2014-08-26 19:45:46 +00:00
gst_dash_demux_update_manifest (GstAdaptiveDemux * demux, GstBuffer * buffer)
{
2014-08-26 19:45:46 +00:00
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
GstMpdClient *new_client = NULL;
GstMapInfo mapinfo;
2014-08-26 19:45:46 +00:00
GST_DEBUG_OBJECT (demux, "Updating manifest file from URL");
2014-08-26 19:45:46 +00:00
/* parse the manifest file */
new_client = gst_mpd_client_new ();
new_client->mpd_uri = g_strdup (demux->manifest_uri);
new_client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
2014-08-26 19:45:46 +00:00
gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
2014-08-26 19:45:46 +00:00
if (gst_mpd_parse (new_client, (gchar *) mapinfo.data, mapinfo.size)) {
const gchar *period_id;
guint period_idx;
GList *iter;
GList *streams_iter;
2014-08-26 19:45:46 +00:00
/* prepare the new manifest and try to transfer the stream position
* status from the old manifest client */
2014-08-26 19:45:46 +00:00
GST_DEBUG_OBJECT (demux, "Updating manifest");
2014-08-26 19:45:46 +00:00
period_id = gst_mpd_client_get_period_id (dashdemux->client);
period_idx = gst_mpd_client_get_period_index (dashdemux->client);
2014-08-26 19:45:46 +00:00
/* setup video, audio and subtitle streams, starting from current Period */
if (!gst_mpd_client_setup_media_presentation (new_client)) {
/* TODO */
}
if (period_id) {
2014-08-26 19:45:46 +00:00
if (!gst_mpd_client_set_period_id (new_client, period_id)) {
GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
return GST_FLOW_EOS;
}
} else {
2014-08-26 19:45:46 +00:00
if (!gst_mpd_client_set_period_index (new_client, period_idx)) {
GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
return GST_FLOW_EOS;
}
}
2014-08-26 19:45:46 +00:00
if (!gst_dash_demux_setup_mpdparser_streams (dashdemux, new_client)) {
GST_ERROR_OBJECT (demux, "Failed to setup streams on manifest " "update");
return GST_FLOW_ERROR;
}
2014-08-26 19:45:46 +00:00
/* update the streams to play from the next segment */
for (iter = demux->streams, streams_iter = new_client->active_streams;
iter && streams_iter;
iter = g_list_next (iter), streams_iter = g_list_next (streams_iter)) {
2014-08-26 19:45:46 +00:00
GstDashDemuxStream *demux_stream = iter->data;
GstActiveStream *new_stream = streams_iter->data;
2014-08-26 19:45:46 +00:00
GstClockTime ts;
2014-08-26 19:45:46 +00:00
if (!new_stream) {
GST_DEBUG_OBJECT (demux,
"Stream of index %d is missing from manifest update",
demux_stream->index);
return GST_FLOW_EOS;
}
2014-08-26 19:45:46 +00:00
if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
demux_stream->index, &ts)) {
gst_mpd_client_stream_seek (new_client, new_stream, ts);
} else
if (gst_mpd_client_get_last_fragment_timestamp (dashdemux->client,
demux_stream->index, &ts)) {
/* try to set to the old timestamp + 1 */
gst_mpd_client_stream_seek (new_client, new_stream, ts + 1);
}
demux_stream->active_stream = new_stream;
}
2014-08-26 19:45:46 +00:00
gst_mpd_client_free (dashdemux->client);
dashdemux->client = new_client;
2014-08-26 19:45:46 +00:00
GST_DEBUG_OBJECT (demux, "Manifest file successfully updated");
} else {
2014-08-26 19:45:46 +00:00
/* In most cases, this will happen if we set a wrong url in the
* source element and we have received the 404 HTML response instead of
* the manifest */
GST_WARNING_OBJECT (demux, "Error parsing the manifest.");
gst_buffer_unmap (buffer, &mapinfo);
return GST_FLOW_ERROR;
}
2014-08-26 19:45:46 +00:00
gst_buffer_unmap (buffer, &mapinfo);
2014-08-26 19:45:46 +00:00
return GST_FLOW_OK;
}
2014-08-26 19:45:46 +00:00
static gint64
gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
stream)
{
2014-08-26 19:45:46 +00:00
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
GstDateTime *seg_end_time;
GstActiveStream *active_stream = dashstream->active_stream;
2014-08-26 19:45:46 +00:00
seg_end_time =
gst_mpd_client_get_next_segment_availability_end_time (dashdemux->client,
active_stream);
2014-08-26 19:45:46 +00:00
if (seg_end_time) {
gint64 diff;
GstDateTime *cur_time;
2014-08-26 19:45:46 +00:00
cur_time = gst_date_time_new_now_utc ();
diff = gst_mpd_client_calculate_time_difference (cur_time, seg_end_time);
gst_date_time_unref (seg_end_time);
gst_date_time_unref (cur_time);
return diff;
}
2014-08-26 19:45:46 +00:00
return 0;
}
2014-08-26 19:45:46 +00:00
static gboolean
gst_dash_demux_has_next_period (GstAdaptiveDemux * demux)
{
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2014-08-26 19:45:46 +00:00
if (demux->segment.rate >= 0)
return gst_mpd_client_has_next_period (dashdemux->client);
else
2014-08-26 19:45:46 +00:00
return gst_mpd_client_has_previous_period (dashdemux->client);
}
2014-08-26 19:45:46 +00:00
static void
gst_dash_demux_advance_period (GstAdaptiveDemux * demux)
2013-05-08 14:13:32 +00:00
{
2014-08-26 19:45:46 +00:00
GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2014-08-26 19:45:46 +00:00
g_return_if_fail (gst_mpd_client_has_next_period (dashdemux->client));
2014-08-26 19:45:46 +00:00
if (demux->segment.rate >= 0) {
if (!gst_mpd_client_set_period_index (dashdemux->client,
gst_mpd_client_get_period_index (dashdemux->client) + 1)) {
/* TODO error */
return;
}
} else {
if (!gst_mpd_client_set_period_index (dashdemux->client,
gst_mpd_client_get_period_index (dashdemux->client) - 1)) {
/* TODO error */
return;
}
}
2014-08-26 19:45:46 +00:00
gst_dash_demux_setup_all_streams (dashdemux);
gst_mpd_client_set_segment_index_for_all_streams (dashdemux->client, 0);
}