diff --git a/ext/dash/gstdashdemux.c b/ext/dash/gstdashdemux.c index 4ec68236d2..3d091ddc7f 100644 --- a/ext/dash/gstdashdemux.c +++ b/ext/dash/gstdashdemux.c @@ -488,9 +488,37 @@ gst_dash_demux_setup_streams (GstAdaptiveDemux * demux) { GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux); gboolean ret = TRUE; + GstDateTime *now = NULL; + guint period_idx; - /* setup video, audio and subtitle streams, starting from first Period */ - if (!gst_mpd_client_set_period_index (dashdemux->client, 0) || + /* 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) || !gst_dash_demux_setup_all_streams (dashdemux)) { ret = FALSE; goto done; @@ -500,16 +528,9 @@ gst_dash_demux_setup_streams (GstAdaptiveDemux * demux) * is closest to current time */ if (gst_mpd_client_is_live (dashdemux->client)) { GList *iter; - GstDateTime *now = gst_date_time_new_now_utc (); gint seg_idx; GST_DEBUG_OBJECT (demux, "Seeking to current time of day for live stream "); - 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; - } for (iter = demux->streams; iter; iter = g_list_next (iter)) { GstDashDemuxStream *stream = iter->data; GstActiveStream *active_stream = stream->active_stream; @@ -539,6 +560,8 @@ gst_dash_demux_setup_streams (GstAdaptiveDemux * demux) } done: + if (now != NULL) + gst_date_time_unref (now); return ret; } diff --git a/ext/dash/gstmpdparser.c b/ext/dash/gstmpdparser.c index 93cb9fdd3c..8a4b5c897d 100644 --- a/ext/dash/gstmpdparser.c +++ b/ext/dash/gstmpdparser.c @@ -2598,6 +2598,40 @@ gst_mpdparser_build_URL_from_template (const gchar * url_template, return ret; } +guint +gst_mpd_client_get_period_index_at_time (GstMpdClient * client, + GstDateTime * time) +{ + GList *iter; + guint period_idx = G_MAXUINT; + guint idx; + gint64 time_offset; + GstDateTime *avail_start = + gst_mpd_client_get_availability_start_time (client); + GstStreamPeriod *stream_period; + + if (avail_start == NULL) + return 0; + + time_offset = gst_mpd_client_calculate_time_difference (avail_start, time); + gst_date_time_unref (avail_start); + + if (time_offset < 0) + return 0; + + for (idx = 0, iter = client->periods; iter; + idx++, iter = g_list_next (iter)) { + stream_period = iter->data; + if (stream_period->start <= time_offset + && stream_period->start + stream_period->duration > time_offset) { + period_idx = idx; + break; + } + } + + return period_idx; +} + static GstStreamPeriod * gst_mpdparser_get_stream_period (GstMpdClient * client) { diff --git a/ext/dash/gstmpdparser.h b/ext/dash/gstmpdparser.h index 0cccb93c61..8b907762d3 100644 --- a/ext/dash/gstmpdparser.h +++ b/ext/dash/gstmpdparser.h @@ -502,6 +502,7 @@ gint gst_mpd_client_get_segment_index_at_time (GstMpdClient *client, GstActiveSt gint gst_mpd_client_check_time_position (GstMpdClient * client, GstActiveStream * stream, GstClockTime ts, gint64 * diff); /* Period selection */ +guint gst_mpd_client_get_period_index_at_time (GstMpdClient * client, GstDateTime * time); gboolean gst_mpd_client_set_period_index (GstMpdClient *client, guint period_idx); gboolean gst_mpd_client_set_period_id (GstMpdClient *client, const gchar * period_id); guint gst_mpd_client_get_period_index (GstMpdClient *client);