Revert "adaptivedemux: answer duration queries for live streams"

Completely disabling duration reporting with live streams is not cool.

This reverts commit e1b68d9a65ba512a52c3a2b298fa830a445eb451.

https://bugzilla.gnome.org/show_bug.cgi?id=753879
diff --git a/ext/dash/gstmpdparser.c b/ext/dash/gstmpdparser.c
index 25eaab7..baf5dbc 100644
--- a/ext/dash/gstmpdparser.c
+++ b/ext/dash/gstmpdparser.c
@@ -5483,12 +5483,18 @@
 GstClockTime
 gst_mpd_client_get_media_presentation_duration (GstMpdClient * client)
 {
+  GstClockTime duration;
+
   g_return_val_if_fail (client != NULL, GST_CLOCK_TIME_NONE);
 
-  /* Note: adaptivedemux makes sure we only get duration queries for on-demand streams */
-  g_return_val_if_fail (client->mpd_node->mediaPresentationDuration != -1,
-      GST_CLOCK_TIME_NONE);
-  return client->mpd_node->mediaPresentationDuration * GST_MSECOND;
+  if (client->mpd_node->mediaPresentationDuration != -1) {
+    duration = client->mpd_node->mediaPresentationDuration * GST_MSECOND;
+  } else {
+    /* We can only get the duration for on-demand streams */
+    duration = GST_CLOCK_TIME_NONE;
+  }
+
+  return duration;
 }
 
 gboolean
diff --git a/ext/hls/m3u8.c b/ext/hls/m3u8.c
index edf08ac..807002f 100644
--- a/ext/hls/m3u8.c
+++ b/ext/hls/m3u8.c
@@ -1015,7 +1015,9 @@
 
   GST_M3U8_LOCK (m3u8);
 
-  /* Note: adaptivedemux makes sure we only get duration queries for on-demand streams */
+  /* We can only get the duration for on-demand streams */
+  if (!m3u8->endlist)
+    goto out;
 
   if (!GST_CLOCK_TIME_IS_VALID (m3u8->duration) && m3u8->files != NULL) {
     GList *f;
@@ -1026,6 +1028,8 @@
   }
   duration = m3u8->duration;
 
+out:
+
   GST_M3U8_UNLOCK (m3u8);
 
   return duration;
diff --git a/ext/smoothstreaming/gstmssmanifest.c b/ext/smoothstreaming/gstmssmanifest.c
index 111fb78..7ef1114 100644
--- a/ext/smoothstreaming/gstmssmanifest.c
+++ b/ext/smoothstreaming/gstmssmanifest.c
@@ -934,7 +934,7 @@
 gst_mss_manifest_get_duration (GstMssManifest * manifest)
 {
   gchar *duration;
-  guint64 dur = GST_CLOCK_TIME_NONE;
+  guint64 dur = -1;
 
   /* try the property */
   duration =
diff --git a/gst-libs/gst/adaptivedemux/gstadaptivedemux.c b/gst-libs/gst/adaptivedemux/gstadaptivedemux.c
index 0ffb530..e46dc3a 100644
--- a/gst-libs/gst/adaptivedemux/gstadaptivedemux.c
+++ b/gst-libs/gst/adaptivedemux/gstadaptivedemux.c
@@ -1748,12 +1748,12 @@
       GST_MANIFEST_LOCK (demux);
 
       if (fmt == GST_FORMAT_TIME && demux->priv->have_manifest) {
-        if (gst_adaptive_demux_is_live (demux))
-          duration = GST_CLOCK_TIME_NONE;
-        else
-          duration = demux_class->get_duration (demux);
-        gst_query_set_duration (query, GST_FORMAT_TIME, duration);
-        ret = TRUE;
+        duration = demux_class->get_duration (demux);
+
+        if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
+          gst_query_set_duration (query, GST_FORMAT_TIME, duration);
+          ret = TRUE;
+        }
       }
 
       GST_MANIFEST_UNLOCK (demux);
diff --git a/gst-libs/gst/adaptivedemux/gstadaptivedemux.h b/gst-libs/gst/adaptivedemux/gstadaptivedemux.h
index d2d2f01..8301714 100644
--- a/gst-libs/gst/adaptivedemux/gstadaptivedemux.h
+++ b/gst-libs/gst/adaptivedemux/gstadaptivedemux.h
@@ -293,18 +293,6 @@
   GstFlowReturn (*update_manifest_data) (GstAdaptiveDemux * demux, GstBuffer * buf);
 
   gboolean      (*is_live)          (GstAdaptiveDemux * demux);
-
-  /**
-   * get_duration:
-   * @demux: #GstAdaptiveDemux
-   *
-   * For non-live streams, this will be called to query the duration of the
-   * stream.
-   *
-   * Returns: The duration of the stream, or #GST_CLOCK_TIME_NONE if the
-   * duration is unknown
-   * Since: 1.6
-   */
   GstClockTime  (*get_duration)     (GstAdaptiveDemux * demux);
 
   /**
diff --git a/tests/check/elements/hlsdemux_m3u8.c b/tests/check/elements/hlsdemux_m3u8.c
index 28952d7..a77aca1 100644
--- a/tests/check/elements/hlsdemux_m3u8.c
+++ b/tests/check/elements/hlsdemux_m3u8.c
@@ -843,6 +843,13 @@
 
   assert_equals_uint64 (gst_m3u8_get_duration (pl), 40 * GST_SECOND);
   gst_hls_master_playlist_unref (master);
+
+  /* Test duration for live playlists */
+  master = load_playlist (LIVE_PLAYLIST);
+  pl = master->default_variant->m3u8;
+  assert_equals_uint64 (gst_m3u8_get_duration (pl), GST_CLOCK_TIME_NONE);
+
+  gst_hls_master_playlist_unref (master);
 }
 
 GST_END_TEST;