2014-11-21 23:42:09 +00:00
|
|
|
/* GStreamer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Samsung Electronics. All rights reserved.
|
|
|
|
* Author: Thiago Santos <thiagoss@osg.samsung.com>
|
|
|
|
*
|
|
|
|
* 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 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
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gstadaptivedemux
|
|
|
|
* @short_description: Base class for adaptive demuxers
|
|
|
|
* @see_also:
|
|
|
|
*
|
|
|
|
* What is an adaptive demuxer?
|
|
|
|
* Adaptive demuxers are special demuxers in the sense that they don't
|
|
|
|
* actually demux data received from upstream but download the data
|
|
|
|
* themselves.
|
|
|
|
*
|
|
|
|
* Adaptive formats (HLS, DASH, MSS) are composed of a manifest file and
|
|
|
|
* a set of fragments. The manifest describes the available media and
|
|
|
|
* the sequence of fragments to use. Each fragments contains a small
|
|
|
|
* part of the media (typically only a few seconds). It is possible for
|
|
|
|
* the manifest to have the same media available in different configurations
|
|
|
|
* (bitrates for example) so that the client can select the one that
|
|
|
|
* best suits its scenario (network fluctuation, hardware requirements...).
|
|
|
|
* It is possible to switch from one representation of the media to another
|
|
|
|
* during playback. That's why it is called 'adaptive', because it can be
|
|
|
|
* adapted to the client's needs.
|
|
|
|
*
|
|
|
|
* Architectural overview:
|
|
|
|
* The manifest is received by the demuxer in its sink pad and, upon receiving
|
|
|
|
* EOS, it parses the manifest and exposes the streams available in it. For
|
|
|
|
* each stream a source element will be created and will download the list
|
|
|
|
* of fragments one by one. Once a fragment is finished downloading, the next
|
|
|
|
* URI is set to the source element and it starts fetching it and pushing
|
|
|
|
* through the stream's pad. This implies that each stream is independent from
|
|
|
|
* each other as it runs on a separate thread.
|
|
|
|
*
|
|
|
|
* After downloading each fragment, the download rate of it is calculated and
|
|
|
|
* the demuxer has a chance to switch to a different bitrate if needed. The
|
|
|
|
* switch can be done by simply pushing a new caps before the next fragment
|
|
|
|
* when codecs are the same, or by exposing a new pad group if it needs
|
|
|
|
* a codec change.
|
|
|
|
*
|
|
|
|
* Extra features:
|
|
|
|
* - Not linked streams: Streams that are not-linked have their download threads
|
|
|
|
* interrupted to save network bandwidth. When they are
|
|
|
|
* relinked a reconfigure event is received and the
|
|
|
|
* stream is restarted.
|
|
|
|
*
|
|
|
|
* Subclasses:
|
|
|
|
* While GstAdaptiveDemux is responsible for the workflow, it knows nothing
|
|
|
|
* about the intrinsics of the subclass formats, so the subclasses are
|
|
|
|
* resposible for maintaining the manifest data structures and stream
|
|
|
|
* information.
|
|
|
|
*/
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/*
|
|
|
|
MT safety.
|
|
|
|
The following rules were observed while implementing MT safety in adaptive demux:
|
|
|
|
1. If a variable is accessed from multiple threads and at least one thread
|
|
|
|
writes to it, then all the accesses needs to be done from inside a critical section.
|
|
|
|
2. If thread A wants to join thread B then at the moment it calls gst_task_join
|
|
|
|
it must not hold any mutexes that thread B might take.
|
|
|
|
|
|
|
|
Adaptive demux API can be called from several threads. More, adaptive demux
|
|
|
|
starts some threads to monitor the download of fragments. In order to protect
|
|
|
|
accesses to shared variables (demux and streams) all the API functions that
|
|
|
|
can be run in different threads will need to get a mutex (manifest_lock)
|
|
|
|
when they start and release it when they end. Because some of those functions
|
|
|
|
can indirectly call other API functions (eg they can generate events or messages
|
|
|
|
that are processed in the same thread) the manifest_lock must be recursive.
|
|
|
|
|
|
|
|
The manifest_lock will serialize the public API making access to shared
|
|
|
|
variables safe. But some of these functions will try at some moment to join
|
|
|
|
threads created by adaptive demux, or to change the state of src elements
|
|
|
|
(which will block trying to join the src element streaming thread). Because
|
|
|
|
of rule 2, those functions will need to release the manifest_lock during the
|
|
|
|
call of gst_task_join. During this time they can be interrupted by other API calls.
|
|
|
|
For example, during the precessing of a seek event, gst_adaptive_demux_stop_tasks
|
|
|
|
is called and this will join all threads. In order to prevent interruptions
|
|
|
|
during such period, all the API functions will also use a second lock: api_lock.
|
|
|
|
This will be taken at the beginning of the function and released at the end,
|
|
|
|
but this time this lock will not be temporarily released during join.
|
|
|
|
This lock will be used only by API calls (not by gst_adaptive_demux_stream_download_loop
|
|
|
|
or gst_adaptive_demux_updates_loop or _src_chain or _src_event) so it is safe
|
|
|
|
to hold it while joining the threads or changing the src element state. The
|
|
|
|
api_lock will serialise all external requests to adaptive demux. In order to
|
|
|
|
avoid deadlocks, if a function needs to acquire both manifest and api locks,
|
|
|
|
the api_lock will be taken first and the manifest_lock second.
|
|
|
|
|
|
|
|
By using the api_lock a thread is protected against other API calls. But when
|
|
|
|
temporarily dropping the manifest_lock, it will be vulnerable to changes from
|
|
|
|
threads that use only the manifest_lock and not the api_lock. These threads run
|
|
|
|
one of the following functions: gst_adaptive_demux_stream_download_loop,
|
|
|
|
gst_adaptive_demux_updates_loop, _src_chain, _src_event. In order to guarantee
|
|
|
|
that all operations during an API call are not impacted by other writes, the
|
|
|
|
above mentioned functions must check a cancelled flag every time they reacquire
|
|
|
|
the manifest_lock. If the flag is set, they must exit immediately, without
|
|
|
|
performing any changes on the shared data. In this way, an API call (eg seek
|
|
|
|
request) can set the cancel flag before releasing the manifest_lock and be sure
|
|
|
|
that the demux object and its streams are not changed by anybody else.
|
|
|
|
*/
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstadaptivedemux.h"
|
|
|
|
#include "gst/gst-i18n-plugin.h"
|
|
|
|
#include <gst/base/gstadapter.h>
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY (adaptivedemux_debug);
|
|
|
|
#define GST_CAT_DEFAULT adaptivedemux_debug
|
|
|
|
|
|
|
|
#define GST_ADAPTIVE_DEMUX_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_ADAPTIVE_DEMUX, \
|
|
|
|
GstAdaptiveDemuxPrivate))
|
|
|
|
|
|
|
|
#define MAX_DOWNLOAD_ERROR_COUNT 3
|
|
|
|
#define DEFAULT_FAILED_COUNT 3
|
2015-02-17 13:03:44 +00:00
|
|
|
#define DEFAULT_CONNECTION_SPEED 0
|
2015-02-17 13:40:06 +00:00
|
|
|
#define DEFAULT_BITRATE_LIMIT 0.8
|
2015-09-04 07:59:06 +00:00
|
|
|
#define SRC_QUEUE_MAX_BYTES 20 * 1024 * 1024 /* For safety. Large enough to hold a segment. */
|
2015-01-27 12:48:42 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
#define GST_MANIFEST_GET_LOCK(d) (&(GST_ADAPTIVE_DEMUX_CAST(d)->priv->manifest_lock))
|
|
|
|
#define GST_MANIFEST_LOCK(d) g_rec_mutex_lock (GST_MANIFEST_GET_LOCK (d));
|
|
|
|
#define GST_MANIFEST_UNLOCK(d) g_rec_mutex_unlock (GST_MANIFEST_GET_LOCK (d));
|
|
|
|
|
|
|
|
#define GST_API_GET_LOCK(d) (&(GST_ADAPTIVE_DEMUX_CAST(d)->priv->api_lock))
|
|
|
|
#define GST_API_LOCK(d) g_mutex_lock (GST_API_GET_LOCK (d));
|
|
|
|
#define GST_API_UNLOCK(d) g_mutex_unlock (GST_API_GET_LOCK (d));
|
|
|
|
|
2015-01-27 12:48:42 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2015-02-17 13:03:44 +00:00
|
|
|
PROP_CONNECTION_SPEED,
|
2015-02-17 13:40:06 +00:00
|
|
|
PROP_BITRATE_LIMIT,
|
2015-01-27 12:48:42 +00:00
|
|
|
PROP_LAST
|
|
|
|
};
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
enum GstAdaptiveDemuxFlowReturn
|
|
|
|
{
|
|
|
|
GST_ADAPTIVE_DEMUX_FLOW_SWITCH = GST_FLOW_CUSTOM_SUCCESS_2 + 1
|
|
|
|
};
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
struct _GstAdaptiveDemuxPrivate
|
|
|
|
{
|
2015-09-16 15:46:29 +00:00
|
|
|
GstAdapter *input_adapter; /* protected by manifest_lock */
|
|
|
|
gboolean have_manifest; /* protected by manifest_lock */
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GList *old_streams; /* protected by manifest_lock */
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GstTask *updates_task; /* MT safe */
|
2014-11-21 23:42:09 +00:00
|
|
|
GRecMutex updates_lock;
|
|
|
|
GMutex updates_timed_lock;
|
2015-09-16 15:46:29 +00:00
|
|
|
GCond updates_timed_cond; /* protected by updates_timed_lock */
|
|
|
|
gboolean stop_updates_task; /* protected by updates_timed_lock */
|
|
|
|
|
|
|
|
/* used only from updates_task, no need to protect it */
|
2014-11-21 23:42:09 +00:00
|
|
|
gint update_failed_count;
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
guint32 segment_seqnum; /* protected by manifest_lock */
|
|
|
|
|
|
|
|
/* main lock used to protect adaptive demux and all its streams.
|
|
|
|
* It serializes the adaptive demux public API.
|
|
|
|
*/
|
|
|
|
GRecMutex manifest_lock;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* condition to wait for manifest updates on a live stream.
|
|
|
|
* In order to signal the manifest_cond, the caller needs to hold both
|
|
|
|
* manifest_lock and manifest_update_lock (taken in this order)
|
|
|
|
*/
|
|
|
|
GCond manifest_cond;
|
|
|
|
GMutex manifest_update_lock;
|
|
|
|
|
|
|
|
GMutex api_lock;
|
2014-11-21 23:42:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static GstBinClass *parent_class = NULL;
|
|
|
|
static void gst_adaptive_demux_class_init (GstAdaptiveDemuxClass * klass);
|
|
|
|
static void gst_adaptive_demux_init (GstAdaptiveDemux * dec,
|
|
|
|
GstAdaptiveDemuxClass * klass);
|
|
|
|
static void gst_adaptive_demux_finalize (GObject * object);
|
|
|
|
static GstStateChangeReturn gst_adaptive_demux_change_state (GstElement *
|
|
|
|
element, GstStateChange transition);
|
|
|
|
|
|
|
|
static void gst_adaptive_demux_handle_message (GstBin * bin, GstMessage * msg);
|
|
|
|
|
|
|
|
static gboolean gst_adaptive_demux_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static GstFlowReturn gst_adaptive_demux_sink_chain (GstPad * pad,
|
|
|
|
GstObject * parent, GstBuffer * buffer);
|
|
|
|
static gboolean gst_adaptive_demux_src_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
|
|
|
static gboolean gst_adaptive_demux_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_push_src_event (GstAdaptiveDemux * demux, GstEvent * event);
|
|
|
|
|
|
|
|
static void gst_adaptive_demux_updates_loop (GstAdaptiveDemux * demux);
|
|
|
|
static void gst_adaptive_demux_stream_download_loop (GstAdaptiveDemuxStream *
|
|
|
|
stream);
|
|
|
|
static void gst_adaptive_demux_reset (GstAdaptiveDemux * demux);
|
2015-09-15 19:50:19 +00:00
|
|
|
static gboolean gst_adaptive_demux_expose_streams (GstAdaptiveDemux * demux,
|
|
|
|
gboolean first_and_live);
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean gst_adaptive_demux_is_live (GstAdaptiveDemux * demux);
|
|
|
|
static GstFlowReturn gst_adaptive_demux_stream_seek (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstClockTime ts);
|
|
|
|
static gboolean gst_adaptive_demux_stream_has_next_fragment (GstAdaptiveDemux *
|
|
|
|
demux, GstAdaptiveDemuxStream * stream);
|
|
|
|
static gboolean gst_adaptive_demux_stream_select_bitrate (GstAdaptiveDemux *
|
|
|
|
demux, GstAdaptiveDemuxStream * stream, guint64 bitrate);
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_update_fragment_info (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream);
|
|
|
|
static gint64
|
|
|
|
gst_adaptive_demux_stream_get_fragment_waiting_time (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream);
|
|
|
|
static GstFlowReturn gst_adaptive_demux_update_manifest (GstAdaptiveDemux *
|
|
|
|
demux);
|
2015-04-23 15:22:11 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_update_manifest_default (GstAdaptiveDemux * demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean gst_adaptive_demux_has_next_period (GstAdaptiveDemux * demux);
|
|
|
|
static void gst_adaptive_demux_advance_period (GstAdaptiveDemux * demux);
|
|
|
|
|
|
|
|
static void gst_adaptive_demux_stream_free (GstAdaptiveDemuxStream * stream);
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_push_event (GstAdaptiveDemuxStream * stream,
|
|
|
|
GstEvent * event);
|
|
|
|
|
|
|
|
static void gst_adaptive_demux_start_tasks (GstAdaptiveDemux * demux);
|
|
|
|
static void gst_adaptive_demux_stop_tasks (GstAdaptiveDemux * demux);
|
|
|
|
static GstFlowReturn gst_adaptive_demux_combine_flows (GstAdaptiveDemux *
|
|
|
|
demux);
|
2015-01-12 21:22:14 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_stream_fragment_download_finish (GstAdaptiveDemuxStream *
|
|
|
|
stream, GstFlowReturn ret, GError * err);
|
2015-01-15 20:44:45 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_data_received_default (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream);
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_finish_fragment_default (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream);
|
2015-09-16 15:46:29 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_advance_fragment_unlocked (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstClockTime duration);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
|
|
|
|
* method to get to the padtemplates */
|
|
|
|
GType
|
|
|
|
gst_adaptive_demux_get_type (void)
|
|
|
|
{
|
|
|
|
static volatile gsize type = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&type)) {
|
|
|
|
GType _type;
|
|
|
|
static const GTypeInfo info = {
|
|
|
|
sizeof (GstAdaptiveDemuxClass),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_adaptive_demux_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstAdaptiveDemux),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_adaptive_demux_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
_type = g_type_register_static (GST_TYPE_BIN,
|
|
|
|
"GstAdaptiveDemux", &info, G_TYPE_FLAG_ABSTRACT);
|
|
|
|
g_once_init_leave (&type, _type);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:48:42 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX (object);
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_LOCK (demux);
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2015-01-27 12:48:42 +00:00
|
|
|
switch (prop_id) {
|
2015-02-17 13:03:44 +00:00
|
|
|
case PROP_CONNECTION_SPEED:
|
|
|
|
demux->connection_speed = g_value_get_uint (value) * 1000;
|
|
|
|
GST_DEBUG_OBJECT (demux, "Connection speed set to %u",
|
|
|
|
demux->connection_speed);
|
|
|
|
break;
|
2015-02-17 13:40:06 +00:00
|
|
|
case PROP_BITRATE_LIMIT:
|
|
|
|
demux->bitrate_limit = g_value_get_float (value);
|
|
|
|
break;
|
2015-01-27 12:48:42 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
GST_API_UNLOCK (demux);
|
2015-01-27 12:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_adaptive_demux_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX (object);
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2015-01-27 12:48:42 +00:00
|
|
|
switch (prop_id) {
|
2015-02-17 13:03:44 +00:00
|
|
|
case PROP_CONNECTION_SPEED:
|
|
|
|
g_value_set_uint (value, demux->connection_speed / 1000);
|
|
|
|
break;
|
2015-02-17 13:40:06 +00:00
|
|
|
case PROP_BITRATE_LIMIT:
|
|
|
|
g_value_set_float (value, demux->bitrate_limit);
|
|
|
|
break;
|
2015-01-27 12:48:42 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-01-27 12:48:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_class_init (GstAdaptiveDemuxClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
GstBinClass *gstbin_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
gstelement_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
gstbin_class = GST_BIN_CLASS (klass);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (adaptivedemux_debug, "adaptivedemux", 0,
|
|
|
|
"Base Adaptive Demux");
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
g_type_class_add_private (klass, sizeof (GstAdaptiveDemuxPrivate));
|
|
|
|
|
2015-01-27 12:48:42 +00:00
|
|
|
gobject_class->set_property = gst_adaptive_demux_set_property;
|
|
|
|
gobject_class->get_property = gst_adaptive_demux_get_property;
|
2014-11-21 23:42:09 +00:00
|
|
|
gobject_class->finalize = gst_adaptive_demux_finalize;
|
|
|
|
|
2015-02-17 13:03:44 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
|
|
|
|
g_param_spec_uint ("connection-speed", "Connection Speed",
|
|
|
|
"Network connection speed in kbps (0 = calculate from downloaded"
|
|
|
|
" fragments)", 0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2015-02-17 13:40:06 +00:00
|
|
|
/* FIXME 2.0: rename this property to bandwidth-usage or any better name */
|
|
|
|
g_object_class_install_property (gobject_class, PROP_BITRATE_LIMIT,
|
|
|
|
g_param_spec_float ("bitrate-limit",
|
|
|
|
"Bitrate limit in %",
|
|
|
|
"Limit of the available bitrate to use when switching to alternates.",
|
|
|
|
0, 1, DEFAULT_BITRATE_LIMIT,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gstelement_class->change_state = gst_adaptive_demux_change_state;
|
|
|
|
|
|
|
|
gstbin_class->handle_message = gst_adaptive_demux_handle_message;
|
2015-01-15 20:44:45 +00:00
|
|
|
|
|
|
|
klass->data_received = gst_adaptive_demux_stream_data_received_default;
|
|
|
|
klass->finish_fragment = gst_adaptive_demux_stream_finish_fragment_default;
|
2015-04-23 15:22:11 +00:00
|
|
|
klass->update_manifest = gst_adaptive_demux_update_manifest_default;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_adaptive_demux_init (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxClass * klass)
|
|
|
|
{
|
|
|
|
GstPadTemplate *pad_template;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "gst_adaptive_demux_init");
|
|
|
|
|
|
|
|
demux->priv = GST_ADAPTIVE_DEMUX_GET_PRIVATE (demux);
|
|
|
|
demux->priv->input_adapter = gst_adapter_new ();
|
2015-04-23 15:02:44 +00:00
|
|
|
demux->downloader = gst_uri_downloader_new ();
|
2014-11-21 23:42:09 +00:00
|
|
|
demux->stream_struct_size = sizeof (GstAdaptiveDemuxStream);
|
2015-06-25 21:32:10 +00:00
|
|
|
demux->priv->segment_seqnum = gst_util_seqnum_next ();
|
|
|
|
demux->have_group_id = FALSE;
|
|
|
|
demux->group_id = G_MAXUINT;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_segment_init (&demux->segment, GST_FORMAT_TIME);
|
|
|
|
|
|
|
|
g_rec_mutex_init (&demux->priv->updates_lock);
|
|
|
|
demux->priv->updates_task =
|
|
|
|
gst_task_new ((GstTaskFunction) gst_adaptive_demux_updates_loop,
|
|
|
|
demux, NULL);
|
|
|
|
gst_task_set_lock (demux->priv->updates_task, &demux->priv->updates_lock);
|
|
|
|
|
|
|
|
g_mutex_init (&demux->priv->updates_timed_lock);
|
|
|
|
g_cond_init (&demux->priv->updates_timed_cond);
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
g_cond_init (&demux->priv->manifest_cond);
|
|
|
|
g_mutex_init (&demux->priv->manifest_update_lock);
|
|
|
|
|
|
|
|
g_rec_mutex_init (&demux->priv->manifest_lock);
|
|
|
|
g_mutex_init (&demux->priv->api_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
pad_template =
|
|
|
|
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
|
|
|
|
g_return_if_fail (pad_template != NULL);
|
|
|
|
|
2015-09-26 09:02:09 +00:00
|
|
|
demux->sinkpad = gst_pad_new_from_template (pad_template, "sink");
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_pad_set_event_function (demux->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_adaptive_demux_sink_event));
|
|
|
|
gst_pad_set_chain_function (demux->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_adaptive_demux_sink_chain));
|
|
|
|
|
2015-02-17 13:03:44 +00:00
|
|
|
/* Properties */
|
2015-02-17 13:40:06 +00:00
|
|
|
demux->bitrate_limit = DEFAULT_BITRATE_LIMIT;
|
2015-02-17 13:03:44 +00:00
|
|
|
demux->connection_speed = DEFAULT_CONNECTION_SPEED;
|
2015-01-27 12:48:42 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_adaptive_demux_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (object);
|
|
|
|
GstAdaptiveDemuxPrivate *priv = demux->priv;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (object, "finalize");
|
|
|
|
|
|
|
|
g_object_unref (priv->input_adapter);
|
2015-04-23 15:02:44 +00:00
|
|
|
g_object_unref (demux->downloader);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
g_mutex_clear (&priv->updates_timed_lock);
|
|
|
|
g_cond_clear (&priv->updates_timed_cond);
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_clear (&demux->priv->manifest_update_lock);
|
|
|
|
g_cond_clear (&demux->priv->manifest_cond);
|
2014-11-21 23:42:09 +00:00
|
|
|
g_object_unref (priv->updates_task);
|
|
|
|
g_rec_mutex_clear (&priv->updates_lock);
|
2015-09-16 15:46:29 +00:00
|
|
|
g_rec_mutex_clear (&demux->priv->manifest_lock);
|
|
|
|
g_mutex_clear (&demux->priv->api_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_adaptive_demux_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (element);
|
|
|
|
GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_LOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2015-10-29 23:23:05 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adaptive_demux_reset (demux);
|
2015-10-29 23:23:05 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-29 23:23:05 +00:00
|
|
|
/* this must be run without MANIFEST_LOCK taken.
|
|
|
|
* For PLAYING to PLAYING state changes, it will want to take a lock in
|
|
|
|
* src element and that lock is held while the streaming thread is running.
|
|
|
|
* The streaming thread will take the MANIFEST_LOCK, leading to a deadlock.
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (parent);
|
|
|
|
GstAdaptiveDemuxClass *demux_class;
|
2015-09-16 15:46:29 +00:00
|
|
|
gboolean ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_LOCK (demux);
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adaptive_demux_reset (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
GST_API_UNLOCK (demux);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
case GST_EVENT_EOS:{
|
|
|
|
GstQuery *query;
|
|
|
|
gboolean query_res;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
gsize available;
|
2015-04-23 15:22:11 +00:00
|
|
|
GstBuffer *manifest_buffer;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_LOCK (demux);
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
demux_class = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
available = gst_adapter_available (demux->priv->input_adapter);
|
|
|
|
|
|
|
|
if (available == 0) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Received EOS without a manifest.");
|
2015-09-16 15:46:29 +00:00
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
GST_API_UNLOCK (demux);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Got EOS on the sink pad: manifest fetched");
|
|
|
|
|
|
|
|
/* Need to get the URI to use it as a base to generate the fragment's
|
|
|
|
* uris */
|
|
|
|
query = gst_query_new_uri ();
|
|
|
|
query_res = gst_pad_peer_query (pad, query);
|
|
|
|
if (query_res) {
|
|
|
|
gchar *uri, *redirect_uri;
|
|
|
|
gboolean permanent;
|
|
|
|
|
|
|
|
gst_query_parse_uri (query, &uri);
|
|
|
|
gst_query_parse_uri_redirection (query, &redirect_uri);
|
|
|
|
gst_query_parse_uri_redirection_permanent (query, &permanent);
|
|
|
|
|
|
|
|
if (permanent && redirect_uri) {
|
|
|
|
demux->manifest_uri = redirect_uri;
|
|
|
|
demux->manifest_base_uri = NULL;
|
|
|
|
g_free (uri);
|
|
|
|
} else {
|
|
|
|
demux->manifest_uri = uri;
|
|
|
|
demux->manifest_base_uri = redirect_uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Fetched manifest at URI: %s (base: %s)",
|
|
|
|
demux->manifest_uri, GST_STR_NULL (demux->manifest_base_uri));
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (demux, "Upstream URI query failed.");
|
|
|
|
}
|
|
|
|
gst_query_unref (query);
|
|
|
|
|
|
|
|
/* Let the subclass parse the manifest */
|
2015-04-23 15:22:11 +00:00
|
|
|
manifest_buffer =
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adapter_take_buffer (demux->priv->input_adapter, available);
|
2015-04-23 15:22:11 +00:00
|
|
|
if (!demux_class->process_manifest (demux, manifest_buffer)) {
|
2014-11-21 23:42:09 +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_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid manifest."),
|
|
|
|
(NULL));
|
|
|
|
ret = FALSE;
|
2015-04-23 15:22:11 +00:00
|
|
|
} else {
|
|
|
|
demux->priv->have_manifest = TRUE;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
2015-04-23 15:22:11 +00:00
|
|
|
gst_buffer_unref (manifest_buffer);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_element (GST_OBJECT_CAST (demux),
|
2015-09-07 14:57:05 +00:00
|
|
|
gst_structure_new (GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME,
|
2014-11-21 23:42:09 +00:00
|
|
|
"manifest-uri", G_TYPE_STRING,
|
|
|
|
demux->manifest_uri, "uri", G_TYPE_STRING,
|
|
|
|
demux->manifest_uri,
|
|
|
|
"manifest-download-start", GST_TYPE_CLOCK_TIME,
|
|
|
|
GST_CLOCK_TIME_NONE,
|
|
|
|
"manifest-download-stop", GST_TYPE_CLOCK_TIME,
|
|
|
|
gst_util_get_timestamp (), NULL)));
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
/* Send duration message */
|
|
|
|
if (!gst_adaptive_demux_is_live (demux)) {
|
|
|
|
GstClockTime duration = demux_class->get_duration (demux);
|
|
|
|
|
|
|
|
if (duration != GST_CLOCK_TIME_NONE) {
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Sending duration message : %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (duration));
|
|
|
|
gst_element_post_message (GST_ELEMENT (demux),
|
|
|
|
gst_message_new_duration_changed (GST_OBJECT (demux)));
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"media duration unknown, can not send the duration message");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (demux->next_streams) {
|
2015-09-15 19:50:19 +00:00
|
|
|
gst_adaptive_demux_expose_streams (demux,
|
|
|
|
gst_adaptive_demux_is_live (demux));
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adaptive_demux_start_tasks (demux);
|
|
|
|
if (gst_adaptive_demux_is_live (demux)) {
|
|
|
|
/* Task to periodically update the manifest */
|
|
|
|
gst_task_start (demux->priv->updates_task);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* no streams */
|
|
|
|
GST_WARNING_OBJECT (demux, "No streams created from manifest");
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
|
|
|
|
(_("This file contains no playable streams.")),
|
|
|
|
("No known stream formats found at the Manifest"));
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
case GST_EVENT_SEGMENT:
|
|
|
|
/* Swallow newsegments, we'll push our own */
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_pad_event_default (pad, parent, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_sink_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (parent);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adapter_push (demux->priv->input_adapter, buffer);
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "Received manifest buffer, total size is %i bytes",
|
|
|
|
(gint) gst_adapter_available (demux->priv->input_adapter));
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_reset (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
GList *iter;
|
2015-09-16 15:46:29 +00:00
|
|
|
GList *old_streams;
|
|
|
|
GstEvent *eos;
|
|
|
|
|
|
|
|
/* take ownership of old_streams before releasing the manifest_lock in
|
|
|
|
* gst_adaptive_demux_stop_tasks
|
|
|
|
*/
|
|
|
|
old_streams = demux->priv->old_streams;
|
|
|
|
demux->priv->old_streams = NULL;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_adaptive_demux_stop_tasks (demux);
|
2015-04-23 15:02:44 +00:00
|
|
|
gst_uri_downloader_reset (demux->downloader);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
if (klass->reset)
|
|
|
|
klass->reset (demux);
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
eos = gst_event_new_eos ();
|
2014-11-21 23:42:09 +00:00
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
2015-09-16 15:46:29 +00:00
|
|
|
if (stream->pad) {
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_ref (eos));
|
|
|
|
gst_pad_set_active (stream->pad, FALSE);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad);
|
2015-09-16 15:46:29 +00:00
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adaptive_demux_stream_free (stream);
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
gst_event_unref (eos);
|
2014-11-21 23:42:09 +00:00
|
|
|
g_list_free (demux->streams);
|
|
|
|
demux->streams = NULL;
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
if (old_streams) {
|
|
|
|
g_list_free_full (old_streams,
|
2014-11-21 23:42:09 +00:00
|
|
|
(GDestroyNotify) gst_adaptive_demux_stream_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (demux->manifest_uri);
|
|
|
|
g_free (demux->manifest_base_uri);
|
|
|
|
demux->manifest_uri = NULL;
|
|
|
|
demux->manifest_base_uri = NULL;
|
|
|
|
|
|
|
|
gst_adapter_clear (demux->priv->input_adapter);
|
2015-04-23 15:22:11 +00:00
|
|
|
demux->priv->have_manifest = FALSE;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_segment_init (&demux->segment, GST_FORMAT_TIME);
|
|
|
|
|
|
|
|
demux->have_group_id = FALSE;
|
|
|
|
demux->group_id = G_MAXUINT;
|
2015-06-25 21:32:10 +00:00
|
|
|
demux->priv->segment_seqnum = gst_util_seqnum_next ();
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_adaptive_demux_handle_message (GstBin * bin, GstMessage * msg)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (bin);
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE (msg)) {
|
|
|
|
case GST_MESSAGE_ERROR:{
|
|
|
|
GList *iter;
|
|
|
|
GstAdaptiveDemuxStream *stream;
|
|
|
|
GError *err = NULL;
|
|
|
|
gchar *debug = NULL;
|
|
|
|
gchar *new_error = NULL;
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
stream = iter->data;
|
2015-11-11 16:51:23 +00:00
|
|
|
if (gst_object_has_as_ancestor (GST_MESSAGE_SRC (msg),
|
|
|
|
GST_OBJECT_CAST (stream->src))) {
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_message_parse_error (msg, &err, &debug);
|
|
|
|
|
|
|
|
GST_WARNING_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (stream),
|
|
|
|
"Source posted error: %d:%d %s (%s)", err->domain, err->code,
|
|
|
|
err->message, debug);
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
new_error = g_strdup_printf ("%s: %s\n", err->message, debug);
|
|
|
|
if (new_error) {
|
|
|
|
g_free (err->message);
|
|
|
|
err->message = new_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* error, but ask to retry */
|
2015-01-12 21:22:14 +00:00
|
|
|
gst_adaptive_demux_stream_fragment_download_finish (stream,
|
|
|
|
GST_FLOW_CUSTOM_ERROR, err);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
g_error_free (err);
|
|
|
|
g_free (debug);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_message_unref (msg);
|
|
|
|
msg = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg)
|
|
|
|
GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_adaptive_demux_set_stream_struct_size (GstAdaptiveDemux * demux,
|
|
|
|
gsize struct_size)
|
|
|
|
{
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_LOCK (demux);
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
demux->stream_struct_size = struct_size;
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
GST_API_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_expose_stream (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstPad *pad = stream->pad;
|
|
|
|
gchar *name = gst_pad_get_name (pad);
|
|
|
|
GstEvent *event;
|
|
|
|
gchar *stream_id;
|
|
|
|
|
|
|
|
gst_pad_set_active (pad, TRUE);
|
|
|
|
stream->need_header = TRUE;
|
|
|
|
|
|
|
|
stream_id = gst_pad_create_stream_id (pad, GST_ELEMENT_CAST (demux), name);
|
|
|
|
|
|
|
|
event =
|
|
|
|
gst_pad_get_sticky_event (GST_ADAPTIVE_DEMUX_SINK_PAD (demux),
|
|
|
|
GST_EVENT_STREAM_START, 0);
|
|
|
|
if (event) {
|
|
|
|
if (gst_event_parse_group_id (event, &demux->group_id))
|
|
|
|
demux->have_group_id = TRUE;
|
|
|
|
else
|
|
|
|
demux->have_group_id = FALSE;
|
|
|
|
gst_event_unref (event);
|
|
|
|
} else if (!demux->have_group_id) {
|
|
|
|
demux->have_group_id = TRUE;
|
|
|
|
demux->group_id = gst_util_group_id_next ();
|
|
|
|
}
|
|
|
|
event = gst_event_new_stream_start (stream_id);
|
|
|
|
if (demux->have_group_id)
|
|
|
|
gst_event_set_group_id (event, demux->group_id);
|
|
|
|
|
|
|
|
gst_pad_push_event (pad, event);
|
|
|
|
g_free (stream_id);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
|
|
|
|
GST_DEBUG_PAD_NAME (pad), stream->pending_caps);
|
|
|
|
|
|
|
|
if (stream->pending_caps) {
|
|
|
|
gst_pad_set_caps (pad, stream->pending_caps);
|
|
|
|
gst_caps_unref (stream->pending_caps);
|
|
|
|
stream->pending_caps = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->discont = FALSE;
|
|
|
|
|
|
|
|
gst_object_ref (pad);
|
|
|
|
|
|
|
|
return gst_element_add_pad (GST_ELEMENT_CAST (demux), pad);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-03-02 12:53:03 +00:00
|
|
|
static GstClockTime
|
|
|
|
gst_adaptive_demux_stream_get_presentation_offset (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass;
|
|
|
|
|
|
|
|
klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
2015-03-10 14:31:21 +00:00
|
|
|
if (klass->get_presentation_offset == NULL)
|
|
|
|
return 0;
|
2015-03-02 12:53:03 +00:00
|
|
|
|
|
|
|
return klass->get_presentation_offset (demux, stream);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-09-02 15:29:43 +00:00
|
|
|
static GstClockTime
|
|
|
|
gst_adaptive_demux_get_period_start_time (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass;
|
|
|
|
|
|
|
|
klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
if (klass->get_period_start_time == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return klass->get_period_start_time (demux);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
2015-09-15 19:50:19 +00:00
|
|
|
gst_adaptive_demux_expose_streams (GstAdaptiveDemux * demux,
|
|
|
|
gboolean first_and_live)
|
2014-11-21 23:42:09 +00:00
|
|
|
{
|
|
|
|
GList *iter;
|
|
|
|
GList *old_streams;
|
2015-09-15 19:50:19 +00:00
|
|
|
GstClockTime period_start, min_pts = GST_CLOCK_TIME_NONE;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (demux->next_streams != NULL, FALSE);
|
|
|
|
|
|
|
|
old_streams = demux->streams;
|
|
|
|
demux->streams = demux->next_streams;
|
|
|
|
demux->next_streams = NULL;
|
|
|
|
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
|
|
|
|
|
|
|
if (!gst_adaptive_demux_expose_stream (demux,
|
|
|
|
GST_ADAPTIVE_DEMUX_STREAM_CAST (stream))) {
|
|
|
|
/* TODO act on error */
|
|
|
|
}
|
2015-09-15 19:50:19 +00:00
|
|
|
|
|
|
|
if (first_and_live) {
|
|
|
|
/* TODO we only need the first timestamp, maybe create a simple function */
|
|
|
|
gst_adaptive_demux_stream_update_fragment_info (demux, stream);
|
|
|
|
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (min_pts)) {
|
|
|
|
min_pts = MIN (min_pts, stream->fragment.timestamp);
|
|
|
|
} else {
|
|
|
|
min_pts = stream->fragment.timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For live streams, the subclass is supposed to seek to the current
|
|
|
|
* fragment and then tell us its timestamp in stream->fragment.timestamp.
|
|
|
|
* We now also have to seek our demuxer segment to reflect this.
|
|
|
|
*
|
|
|
|
* FIXME: This needs some refactoring at some point.
|
|
|
|
*/
|
|
|
|
if (first_and_live) {
|
|
|
|
gst_segment_do_seek (&demux->segment, demux->segment.rate, GST_FORMAT_TIME,
|
|
|
|
GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, min_pts, GST_SEEK_TYPE_NONE, -1,
|
|
|
|
NULL);
|
2014-11-29 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 15:29:43 +00:00
|
|
|
period_start = gst_adaptive_demux_get_period_start_time (demux);
|
|
|
|
|
2014-11-29 13:27:25 +00:00
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
2015-03-02 12:53:03 +00:00
|
|
|
GstClockTime offset;
|
2014-11-29 13:27:25 +00:00
|
|
|
|
2015-03-02 12:53:03 +00:00
|
|
|
offset = gst_adaptive_demux_stream_get_presentation_offset (demux, stream);
|
2014-11-29 13:27:25 +00:00
|
|
|
stream->segment = demux->segment;
|
2015-06-23 16:19:35 +00:00
|
|
|
|
2015-09-02 15:29:43 +00:00
|
|
|
/* The demuxer segment is just built from seek events, but for each stream
|
|
|
|
* we have to adjust segments according to the current period and the
|
|
|
|
* stream specific presentation time offset.
|
|
|
|
*
|
|
|
|
* For each period, buffer timestamps start again from 0. Additionally the
|
|
|
|
* buffer timestamps are shifted by the stream specific presentation time
|
|
|
|
* offset, so the first buffer timestamp of a period is 0 + presentation
|
|
|
|
* time offset. If the stream contains timestamps itself, this is also
|
|
|
|
* supposed to be the presentation time stored inside the stream.
|
|
|
|
*
|
|
|
|
* The stream time over periods is supposed to be continuous, that is the
|
|
|
|
* buffer timestamp 0 + presentation time offset should map to the start
|
|
|
|
* time of the current period.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The adjustment of the stream segments as such works the following.
|
|
|
|
*
|
|
|
|
* If the demuxer segment start is bigger than the period start, this
|
|
|
|
* means that we have to drop some media at the beginning of the current
|
|
|
|
* period, e.g. because a seek into the middle of the period has
|
|
|
|
* happened. The amount of media to drop is the difference between the
|
|
|
|
* period start and the demuxer segment start, and as each period starts
|
|
|
|
* again from 0, this difference is going to be the actual stream's
|
|
|
|
* segment start. As all timestamps of the stream are shifted by the
|
|
|
|
* presentation time offset, we will also have to move the segment start
|
|
|
|
* by that offset.
|
|
|
|
*
|
|
|
|
* Now the running time and stream time at the stream's segment start has to
|
|
|
|
* be the one that is stored inside the demuxer's segment, which means
|
|
|
|
* that segment.base and segment.time have to be copied over.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* If the demuxer segment start is smaller than the period start time,
|
|
|
|
* this means that the whole period is inside the segment. As each period
|
|
|
|
* starts timestamps from 0, and additionally timestamps are shifted by
|
|
|
|
* the presentation time offset, the stream's first timestamp (and as such
|
|
|
|
* the stream's segment start) has to be the presentation time offset.
|
|
|
|
* The stream time at the segment start is supposed to be the stream time
|
|
|
|
* of the period start according to the demuxer segment, so the stream
|
|
|
|
* segment's time would be set to that. The same goes for the stream
|
|
|
|
* segment's base, which is supposed to be the running time of the period
|
|
|
|
* start according to the demuxer's segment.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* For the first case where not the complete period is inside the segment,
|
|
|
|
* the segment time and base as calculated by the second case would be
|
|
|
|
* equivalent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (demux->segment.start > period_start) {
|
|
|
|
stream->segment.start = demux->segment.start - period_start + offset;
|
|
|
|
stream->segment.position = offset;
|
|
|
|
stream->segment.time = demux->segment.time;
|
|
|
|
stream->segment.base = demux->segment.base;
|
|
|
|
} else {
|
|
|
|
stream->segment.start = offset;
|
|
|
|
stream->segment.position = offset;
|
|
|
|
stream->segment.time =
|
|
|
|
gst_segment_to_stream_time (&demux->segment, GST_FORMAT_TIME,
|
|
|
|
period_start);
|
2015-06-25 21:32:10 +00:00
|
|
|
stream->segment.base =
|
2015-09-02 15:29:43 +00:00
|
|
|
gst_segment_to_running_time (&demux->segment, GST_FORMAT_TIME,
|
|
|
|
period_start);
|
|
|
|
}
|
2015-06-23 16:19:35 +00:00
|
|
|
|
2014-11-29 13:27:25 +00:00
|
|
|
stream->pending_segment = gst_event_new_segment (&stream->segment);
|
2015-06-25 21:32:10 +00:00
|
|
|
gst_event_set_seqnum (stream->pending_segment, demux->priv->segment_seqnum);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
|
|
|
|
|
|
|
|
if (old_streams) {
|
|
|
|
GstEvent *eos = gst_event_new_eos ();
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* before we put streams in the demux->priv->old_streams list,
|
|
|
|
* we ask the download task to stop. In this way, it will no longer be
|
|
|
|
* allowed to change the demux object.
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
for (iter = old_streams; iter; iter = g_list_next (iter)) {
|
2015-03-11 05:55:14 +00:00
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (stream->pad, "Removing stream");
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_ref (eos));
|
|
|
|
gst_pad_set_active (stream->pad, FALSE);
|
|
|
|
gst_element_remove_pad (GST_ELEMENT (demux), stream->pad);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
/* ask the download task to stop.
|
|
|
|
* We will not join it now, because our thread can be one of these tasks.
|
|
|
|
* We will do the joining later, from another stream download task or
|
|
|
|
* from gst_adaptive_demux_stop_tasks.
|
|
|
|
* We also cannot change the state of the stream->src element, because
|
|
|
|
* that will wait on the streaming thread (which could be this thread)
|
|
|
|
* to stop first.
|
|
|
|
* Because we sent an EOS to the downstream element, the stream->src
|
|
|
|
* element should detect this in its streaming task and stop.
|
|
|
|
* Even if it doesn't do that, we will change its state later in
|
|
|
|
* gst_adaptive_demux_stop_tasks.
|
|
|
|
*/
|
|
|
|
gst_task_stop (stream->download_task);
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
stream->cancelled = TRUE;
|
|
|
|
g_cond_signal (&stream->fragment_download_cond);
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
gst_event_unref (eos);
|
|
|
|
|
|
|
|
/* The list should be freed from another thread as we can't properly
|
|
|
|
* cleanup a GstTask from itself */
|
|
|
|
demux->priv->old_streams =
|
|
|
|
g_list_concat (demux->priv->old_streams, old_streams);
|
|
|
|
}
|
2014-11-29 13:27:25 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
GstAdaptiveDemuxStream *
|
|
|
|
gst_adaptive_demux_stream_new (GstAdaptiveDemux * demux, GstPad * pad)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxStream *stream;
|
|
|
|
|
|
|
|
stream = g_malloc0 (demux->stream_struct_size);
|
|
|
|
|
|
|
|
/* Downloading task */
|
|
|
|
g_rec_mutex_init (&stream->download_lock);
|
|
|
|
stream->download_task =
|
|
|
|
gst_task_new ((GstTaskFunction) gst_adaptive_demux_stream_download_loop,
|
|
|
|
stream, NULL);
|
|
|
|
gst_task_set_lock (stream->download_task, &stream->download_lock);
|
|
|
|
|
|
|
|
stream->pad = pad;
|
|
|
|
stream->demux = demux;
|
|
|
|
gst_pad_set_element_private (pad, stream);
|
|
|
|
|
|
|
|
gst_pad_set_query_function (pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_adaptive_demux_src_query));
|
|
|
|
gst_pad_set_event_function (pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_adaptive_demux_src_event));
|
|
|
|
|
|
|
|
gst_segment_init (&stream->segment, GST_FORMAT_TIME);
|
|
|
|
g_cond_init (&stream->fragment_download_cond);
|
|
|
|
g_mutex_init (&stream->fragment_download_lock);
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->adapter = gst_adapter_new ();
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
demux->next_streams = g_list_append (demux->next_streams, stream);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken.
|
|
|
|
* It will temporarily drop the manifest_lock in order to join the task.
|
|
|
|
* It will join only the old_streams (the demux->streams are joined by
|
|
|
|
* gst_adaptive_demux_stop_tasks before gst_adaptive_demux_stream_free is
|
|
|
|
* called)
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_stream_free (GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
if (klass->stream_free)
|
|
|
|
klass->stream_free (stream);
|
|
|
|
|
|
|
|
g_clear_error (&stream->last_error);
|
|
|
|
if (stream->download_task) {
|
|
|
|
if (GST_TASK_STATE (stream->download_task) != GST_TASK_STOPPED) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Leaving streaming task %s:%s",
|
|
|
|
GST_DEBUG_PAD_NAME (stream->pad));
|
|
|
|
|
|
|
|
gst_task_stop (stream->download_task);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
stream->cancelled = TRUE;
|
|
|
|
g_cond_signal (&stream->fragment_download_cond);
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
2014-12-23 04:51:10 +00:00
|
|
|
GST_LOG_OBJECT (demux, "Waiting for task to finish");
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
/* temporarily drop the manifest lock to join the task */
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2014-12-23 04:51:10 +00:00
|
|
|
gst_task_join (stream->download_task);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2014-12-23 04:51:10 +00:00
|
|
|
GST_LOG_OBJECT (demux, "Finished");
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_object_unref (stream->download_task);
|
|
|
|
g_rec_mutex_clear (&stream->download_lock);
|
|
|
|
stream->download_task = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
|
|
|
|
|
|
|
|
if (stream->pending_segment) {
|
|
|
|
gst_event_unref (stream->pending_segment);
|
|
|
|
stream->pending_segment = NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:22:14 +00:00
|
|
|
if (stream->pending_events) {
|
|
|
|
g_list_free_full (stream->pending_events, (GDestroyNotify) gst_event_unref);
|
|
|
|
stream->pending_events = NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
if (stream->src_srcpad) {
|
|
|
|
gst_object_unref (stream->src_srcpad);
|
|
|
|
stream->src_srcpad = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->src) {
|
|
|
|
gst_element_set_state (stream->src, GST_STATE_NULL);
|
|
|
|
gst_bin_remove (GST_BIN_CAST (demux), stream->src);
|
|
|
|
stream->src = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_cond_clear (&stream->fragment_download_cond);
|
|
|
|
g_mutex_clear (&stream->fragment_download_lock);
|
|
|
|
|
|
|
|
if (stream->pad) {
|
|
|
|
gst_object_unref (stream->pad);
|
|
|
|
stream->pad = NULL;
|
|
|
|
}
|
|
|
|
if (stream->pending_caps)
|
|
|
|
gst_caps_unref (stream->pending_caps);
|
2015-01-15 20:44:45 +00:00
|
|
|
|
|
|
|
g_object_unref (stream->adapter);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
g_free (stream);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-01-05 20:58:54 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_get_live_seek_range (GstAdaptiveDemux * demux,
|
|
|
|
gint64 * range_start, gint64 * range_stop)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass;
|
|
|
|
|
|
|
|
klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
g_return_val_if_fail (klass->get_live_seek_range, FALSE);
|
|
|
|
|
|
|
|
return klass->get_live_seek_range (demux, range_start, range_stop);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-01-05 20:58:54 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_can_seek (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass;
|
|
|
|
|
|
|
|
klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
if (gst_adaptive_demux_is_live (demux)) {
|
|
|
|
return klass->get_live_seek_range != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return klass->seek != NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux;
|
|
|
|
GstAdaptiveDemuxClass *demux_class;
|
|
|
|
|
|
|
|
demux = GST_ADAPTIVE_DEMUX_CAST (parent);
|
|
|
|
demux_class = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case GST_EVENT_SEEK:
|
|
|
|
{
|
|
|
|
gdouble rate;
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
2014-12-12 00:01:48 +00:00
|
|
|
guint32 seqnum;
|
2014-11-21 23:42:09 +00:00
|
|
|
gboolean update;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
GstSegment oldsegment;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "Received seek event");
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_LOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2015-01-05 20:58:54 +00:00
|
|
|
if (!gst_adaptive_demux_can_seek (demux)) {
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
|
|
|
|
&stop_type, &stop);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME) {
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
GST_API_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-01-05 20:58:54 +00:00
|
|
|
if (gst_adaptive_demux_is_live (demux)) {
|
|
|
|
gint64 range_start, range_stop;
|
|
|
|
if (!gst_adaptive_demux_get_live_seek_range (demux, &range_start,
|
|
|
|
&range_stop)) {
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
GST_API_UNLOCK (demux);
|
2015-01-05 20:58:54 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (start < range_start || start >= range_stop) {
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
GST_API_UNLOCK (demux);
|
2015-01-05 20:58:54 +00:00
|
|
|
GST_WARNING_OBJECT (demux, "Seek to invalid position");
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
seqnum = gst_event_get_seqnum (event);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"seek event, rate: %f type: %d start: %" GST_TIME_FORMAT " stop: %"
|
|
|
|
GST_TIME_FORMAT, rate, start_type, GST_TIME_ARGS (start),
|
|
|
|
GST_TIME_ARGS (stop));
|
|
|
|
|
|
|
|
/* have a backup in case seek fails */
|
|
|
|
gst_segment_copy_into (&demux->segment, &oldsegment);
|
|
|
|
|
|
|
|
gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
|
|
|
|
start, stop_type, stop, &update);
|
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
if (flags & GST_SEEK_FLAG_FLUSH) {
|
|
|
|
GstEvent *fevent;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "sending flush start");
|
|
|
|
fevent = gst_event_new_flush_start ();
|
|
|
|
gst_event_set_seqnum (fevent, seqnum);
|
|
|
|
gst_adaptive_demux_push_src_event (demux, fevent);
|
|
|
|
}
|
|
|
|
gst_adaptive_demux_stop_tasks (demux);
|
|
|
|
GST_DEBUG_OBJECT (demux, "Seeking to segment %" GST_SEGMENT_FORMAT,
|
|
|
|
&demux->segment);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
ret = demux_class->seek (demux, event);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-06-25 21:32:10 +00:00
|
|
|
if (!ret) {
|
2014-12-12 00:01:48 +00:00
|
|
|
/* Is there anything else we can do if it fails? */
|
|
|
|
gst_segment_copy_into (&oldsegment, &demux->segment);
|
2015-06-25 21:32:10 +00:00
|
|
|
} else {
|
|
|
|
demux->priv->segment_seqnum = seqnum;
|
2014-12-12 00:01:48 +00:00
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
if (flags & GST_SEEK_FLAG_FLUSH) {
|
|
|
|
GstEvent *fevent;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Sending flush stop on all pad");
|
|
|
|
fevent = gst_event_new_flush_stop (TRUE);
|
|
|
|
gst_event_set_seqnum (fevent, seqnum);
|
|
|
|
gst_adaptive_demux_push_src_event (demux, fevent);
|
|
|
|
}
|
2015-06-25 21:32:10 +00:00
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
if (demux->next_streams) {
|
2015-09-15 19:50:19 +00:00
|
|
|
gst_adaptive_demux_expose_streams (demux, FALSE);
|
2015-06-25 21:32:10 +00:00
|
|
|
} else {
|
|
|
|
GList *iter;
|
2015-09-02 15:29:43 +00:00
|
|
|
GstClockTime period_start =
|
|
|
|
gst_adaptive_demux_get_period_start_time (demux);
|
2015-06-25 21:32:10 +00:00
|
|
|
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
2015-06-25 21:49:10 +00:00
|
|
|
GstEvent *seg_evt;
|
2015-06-25 21:32:10 +00:00
|
|
|
GstClockTime offset;
|
|
|
|
|
2015-09-02 15:29:43 +00:00
|
|
|
/* See comments in gst_adaptive_demux_get_period_start_time() for
|
|
|
|
* an explanation of the segment modifications */
|
2015-06-25 21:32:10 +00:00
|
|
|
stream->segment = demux->segment;
|
|
|
|
offset =
|
|
|
|
gst_adaptive_demux_stream_get_presentation_offset (demux, stream);
|
2015-09-02 15:29:43 +00:00
|
|
|
stream->segment.start += offset - period_start;
|
|
|
|
stream->segment.position = stream->segment.start;
|
2015-06-25 21:32:10 +00:00
|
|
|
seg_evt = gst_event_new_segment (&stream->segment);
|
|
|
|
gst_event_set_seqnum (seg_evt, demux->priv->segment_seqnum);
|
|
|
|
gst_event_replace (&stream->pending_segment, seg_evt);
|
2015-06-25 21:49:10 +00:00
|
|
|
gst_event_unref (seg_evt);
|
2015-06-25 21:32:10 +00:00
|
|
|
}
|
2014-12-12 00:01:48 +00:00
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2014-12-12 00:01:48 +00:00
|
|
|
/* Restart the demux */
|
|
|
|
gst_adaptive_demux_start_tasks (demux);
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_API_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
case GST_EVENT_RECONFIGURE:{
|
|
|
|
GList *iter;
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
|
|
|
|
|
|
|
if (stream->pad == pad) {
|
|
|
|
if (stream->last_ret == GST_FLOW_NOT_LINKED) {
|
|
|
|
stream->last_ret = GST_FLOW_OK;
|
|
|
|
stream->restart_download = TRUE;
|
|
|
|
stream->need_header = TRUE;
|
|
|
|
stream->discont = TRUE;
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Restarting download loop");
|
|
|
|
gst_task_start (stream->download_task);
|
|
|
|
}
|
|
|
|
gst_event_unref (event);
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_EVENT_LATENCY:{
|
|
|
|
/* Upstream and our internal source are irrelevant
|
|
|
|
* for latency, and we should not fail here to
|
|
|
|
* configure the latency */
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_pad_event_default (pad, parent, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_src_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux;
|
|
|
|
GstAdaptiveDemuxClass *demux_class;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
if (query == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
demux = GST_ADAPTIVE_DEMUX_CAST (parent);
|
|
|
|
demux_class = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
switch (query->type) {
|
|
|
|
case GST_QUERY_DURATION:{
|
|
|
|
GstClockTime duration = -1;
|
|
|
|
GstFormat fmt;
|
|
|
|
|
|
|
|
gst_query_parse_duration (query, &fmt, NULL);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
2015-04-26 17:08:00 +00:00
|
|
|
if (fmt == GST_FORMAT_TIME && demux->priv->have_manifest
|
|
|
|
&& !gst_adaptive_demux_is_live (demux)) {
|
2014-11-21 23:42:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2015-08-15 17:04:27 +00:00
|
|
|
GST_LOG_OBJECT (demux, "GST_QUERY_DURATION returns %s with duration %"
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_QUERY_LATENCY:{
|
2015-04-23 15:43:36 +00:00
|
|
|
gst_query_set_latency (query, FALSE, 0, -1);
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_QUERY_SEEKING:{
|
|
|
|
GstFormat fmt;
|
|
|
|
gint64 stop = -1;
|
2015-01-05 20:58:54 +00:00
|
|
|
gint64 start = 0;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2015-04-29 13:41:07 +00:00
|
|
|
if (!demux->priv->have_manifest) {
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-04-29 13:41:07 +00:00
|
|
|
GST_INFO_OBJECT (demux,
|
|
|
|
"Don't have manifest yet, can't answer seeking query");
|
2014-11-21 23:42:09 +00:00
|
|
|
return FALSE; /* can't answer without manifest */
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
|
|
|
|
GST_INFO_OBJECT (demux, "Received GST_QUERY_SEEKING with format %d", fmt);
|
|
|
|
if (fmt == GST_FORMAT_TIME) {
|
|
|
|
GstClockTime duration;
|
2015-01-05 20:58:54 +00:00
|
|
|
gboolean can_seek = gst_adaptive_demux_can_seek (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = TRUE;
|
2015-01-05 20:58:54 +00:00
|
|
|
if (can_seek) {
|
|
|
|
if (gst_adaptive_demux_is_live (demux)) {
|
|
|
|
ret = gst_adaptive_demux_get_live_seek_range (demux, &start, &stop);
|
|
|
|
} else {
|
|
|
|
duration = demux_class->get_duration (demux);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
|
|
|
|
stop = duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_query_set_seeking (query, fmt, can_seek, start, stop);
|
|
|
|
GST_INFO_OBJECT (demux, "GST_QUERY_SEEKING returning with start : %"
|
|
|
|
GST_TIME_FORMAT ", stop : %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_QUERY_URI:
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
/* TODO HLS can answer this differently it seems */
|
2014-11-21 23:42:09 +00:00
|
|
|
if (demux->manifest_uri) {
|
|
|
|
/* FIXME: (hls) Do we answer with the variant playlist, with the current
|
|
|
|
* playlist or the the uri of the last downlowaded fragment? */
|
|
|
|
gst_query_set_uri (query, demux->manifest_uri);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Don't forward queries upstream because of the special nature of this
|
|
|
|
* "demuxer", which relies on the upstream element only to be fed
|
|
|
|
* the Manifest
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_start_tasks (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GList *iter;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "Starting streams' tasks");
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
stream->cancelled = FALSE;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
stream->last_ret = GST_FLOW_OK;
|
|
|
|
gst_task_start (stream->download_task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken
|
|
|
|
* This function will temporarily release manifest_lock in order to join the
|
|
|
|
* download threads.
|
|
|
|
* The api_lock will still protect it against other threads trying to modify
|
|
|
|
* the demux element.
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_stop_tasks (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GList *iter;
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
gst_task_stop (demux->priv->updates_task);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_lock (&demux->priv->updates_timed_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
demux->priv->stop_updates_task = TRUE;
|
|
|
|
g_cond_signal (&demux->priv->updates_timed_cond);
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_unlock (&demux->priv->updates_timed_lock);
|
2014-12-03 21:31:06 +00:00
|
|
|
|
2015-04-23 15:02:44 +00:00
|
|
|
gst_uri_downloader_cancel (demux->downloader);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
2015-09-16 15:46:29 +00:00
|
|
|
stream->cancelled = TRUE;
|
|
|
|
gst_task_stop (stream->download_task);
|
2014-11-21 23:42:09 +00:00
|
|
|
g_cond_signal (&stream->fragment_download_cond);
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_lock (&demux->priv->manifest_update_lock);
|
|
|
|
g_cond_broadcast (&demux->priv->manifest_cond);
|
|
|
|
g_mutex_unlock (&demux->priv->manifest_update_lock);
|
|
|
|
|
|
|
|
/* need to release manifest_lock before stopping the src element.
|
|
|
|
* The streams were asked to cancel, so they will not make any writes to demux
|
|
|
|
* object. Even if we temporarily release manifest_lock, the demux->streams
|
|
|
|
* cannot change and iter cannot be invalidated.
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
2015-09-16 15:46:29 +00:00
|
|
|
GstElement *src = stream->src;
|
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
if (src) {
|
|
|
|
gst_element_set_state (src, GST_STATE_READY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stream->download_task value never changes, so it is safe to read it
|
|
|
|
* outside critical section
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_task_join (stream->download_task);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
|
|
|
/* demux->priv->updates_task value never changes, so it is safe to read it
|
|
|
|
* outside critical section
|
|
|
|
*/
|
|
|
|
gst_task_join (demux->priv->updates_task);
|
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
stream->download_error_count = 0;
|
|
|
|
stream->need_header = TRUE;
|
2015-01-15 20:44:45 +00:00
|
|
|
gst_adapter_clear (stream->adapter);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_push_src_event (GstAdaptiveDemux * demux, GstEvent * event)
|
|
|
|
{
|
|
|
|
GList *iter;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
|
|
|
gst_event_ref (event);
|
|
|
|
ret = ret & gst_pad_push_event (stream->pad, event);
|
|
|
|
}
|
|
|
|
gst_event_unref (event);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
void
|
|
|
|
gst_adaptive_demux_stream_set_caps (GstAdaptiveDemuxStream * stream,
|
|
|
|
GstCaps * caps)
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "setting new caps for stream %" GST_PTR_FORMAT,
|
|
|
|
caps);
|
|
|
|
gst_caps_replace (&stream->pending_caps, caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
void
|
|
|
|
gst_adaptive_demux_stream_set_tags (GstAdaptiveDemuxStream * stream,
|
|
|
|
GstTagList * tags)
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "setting new tags for stream %" GST_PTR_FORMAT,
|
|
|
|
tags);
|
|
|
|
if (stream->pending_tags) {
|
|
|
|
gst_tag_list_unref (stream->pending_tags);
|
|
|
|
}
|
|
|
|
stream->pending_tags = tags;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-02-06 13:22:14 +00:00
|
|
|
void
|
|
|
|
gst_adaptive_demux_stream_queue_event (GstAdaptiveDemuxStream * stream,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
stream->pending_events = g_list_append (stream->pending_events, event);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-01-27 12:48:42 +00:00
|
|
|
static guint64
|
|
|
|
gst_adaptive_demux_stream_update_current_bitrate (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
2014-11-21 23:42:09 +00:00
|
|
|
{
|
2015-09-04 07:59:06 +00:00
|
|
|
guint64 bitrate;
|
2015-01-27 12:48:42 +00:00
|
|
|
|
2015-02-17 13:03:44 +00:00
|
|
|
if (demux->connection_speed) {
|
|
|
|
GST_LOG_OBJECT (demux, "Connection-speed is set to %u kbps, using it",
|
|
|
|
demux->connection_speed / 1000);
|
|
|
|
return demux->connection_speed;
|
|
|
|
}
|
|
|
|
|
2015-09-04 07:59:06 +00:00
|
|
|
g_object_get (stream->queue, "avg-in-rate", &bitrate, NULL);
|
|
|
|
bitrate *= 8;
|
|
|
|
GST_DEBUG_OBJECT (demux, "Download bitrate is : %" G_GUINT64_FORMAT " bps",
|
|
|
|
bitrate);
|
|
|
|
stream->current_download_rate = bitrate;
|
|
|
|
return bitrate;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_combine_flows (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
gboolean all_notlinked = TRUE;
|
|
|
|
gboolean all_eos = TRUE;
|
|
|
|
GList *iter;
|
|
|
|
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *stream = iter->data;
|
|
|
|
|
|
|
|
if (stream->last_ret != GST_FLOW_NOT_LINKED) {
|
|
|
|
all_notlinked = FALSE;
|
|
|
|
if (stream->last_ret != GST_FLOW_EOS)
|
|
|
|
all_eos = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->last_ret <= GST_FLOW_NOT_NEGOTIATED
|
|
|
|
|| stream->last_ret == GST_FLOW_FLUSHING) {
|
|
|
|
return stream->last_ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (all_notlinked)
|
|
|
|
return GST_FLOW_NOT_LINKED;
|
|
|
|
else if (all_eos)
|
|
|
|
return GST_FLOW_EOS;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken.
|
|
|
|
* Temporarily releases manifest_lock
|
|
|
|
*/
|
2015-01-15 20:44:45 +00:00
|
|
|
GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_push_buffer (GstAdaptiveDemuxStream * stream,
|
|
|
|
GstBuffer * buffer)
|
2014-11-21 23:42:09 +00:00
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
gboolean discont = FALSE;
|
|
|
|
|
|
|
|
if (stream->first_fragment_buffer) {
|
2015-06-23 16:19:35 +00:00
|
|
|
GstClockTime offset =
|
|
|
|
gst_adaptive_demux_stream_get_presentation_offset (demux, stream);
|
2015-09-02 15:29:43 +00:00
|
|
|
GstClockTime period_start =
|
|
|
|
gst_adaptive_demux_get_period_start_time (demux);
|
2015-06-23 16:19:35 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
if (demux->segment.rate < 0)
|
|
|
|
/* Set DISCONT flag for every first buffer in reverse playback mode
|
|
|
|
* as each fragment for its own has to be reversed */
|
|
|
|
discont = TRUE;
|
|
|
|
|
|
|
|
GST_BUFFER_PTS (buffer) = stream->fragment.timestamp;
|
2015-01-06 21:44:15 +00:00
|
|
|
if (GST_BUFFER_PTS_IS_VALID (buffer))
|
2015-06-23 16:19:35 +00:00
|
|
|
GST_BUFFER_PTS (buffer) += offset;
|
|
|
|
|
|
|
|
if (GST_BUFFER_PTS_IS_VALID (buffer)) {
|
2015-01-06 21:44:15 +00:00
|
|
|
stream->segment.position = GST_BUFFER_PTS (buffer);
|
2015-09-02 15:29:43 +00:00
|
|
|
|
|
|
|
/* Convert from position inside the stream's segment to the demuxer's
|
|
|
|
* segment, they are not necessarily the same */
|
|
|
|
if (stream->segment.position - offset + period_start >
|
|
|
|
demux->segment.position)
|
|
|
|
demux->segment.position =
|
|
|
|
stream->segment.position - offset + period_start;
|
2015-06-23 16:19:35 +00:00
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
} else {
|
|
|
|
GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->discont) {
|
|
|
|
discont = TRUE;
|
|
|
|
stream->discont = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (discont) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Marking fragment as discontinuous");
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->first_fragment_buffer = FALSE;
|
|
|
|
|
|
|
|
GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (stream->pending_caps)) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Setting pending caps: %" GST_PTR_FORMAT,
|
|
|
|
stream->pending_caps);
|
|
|
|
gst_pad_set_caps (stream->pad, stream->pending_caps);
|
|
|
|
gst_caps_unref (stream->pending_caps);
|
|
|
|
stream->pending_caps = NULL;
|
|
|
|
}
|
|
|
|
if (G_UNLIKELY (stream->pending_segment)) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Sending pending seg: %" GST_PTR_FORMAT,
|
|
|
|
stream->pending_segment);
|
|
|
|
gst_pad_push_event (stream->pad, stream->pending_segment);
|
|
|
|
stream->pending_segment = NULL;
|
|
|
|
}
|
2015-11-10 16:54:51 +00:00
|
|
|
if (G_UNLIKELY (stream->pending_tags || stream->bitrate_changed)) {
|
|
|
|
GstTagList *tags = stream->pending_tags;
|
|
|
|
|
|
|
|
stream->pending_tags = NULL;
|
|
|
|
stream->bitrate_changed = 0;
|
|
|
|
|
|
|
|
if (stream->fragment.bitrate != 0) {
|
|
|
|
if (tags)
|
|
|
|
tags = gst_tag_list_make_writable (tags);
|
|
|
|
else
|
|
|
|
tags = gst_tag_list_new_empty ();
|
|
|
|
|
|
|
|
gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
|
|
|
|
GST_TAG_NOMINAL_BITRATE, stream->fragment.bitrate, NULL);
|
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Sending pending tags: %" GST_PTR_FORMAT,
|
|
|
|
stream->pending_tags);
|
2015-11-10 16:54:51 +00:00
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_tag (tags));
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
2015-07-18 20:18:23 +00:00
|
|
|
while (stream->pending_events != NULL) {
|
|
|
|
GstEvent *event = stream->pending_events->data;
|
2015-02-06 13:22:14 +00:00
|
|
|
|
2015-07-18 20:18:23 +00:00
|
|
|
if (!gst_pad_push_event (stream->pad, event))
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Failed to send pending event");
|
|
|
|
|
|
|
|
stream->pending_events =
|
|
|
|
g_list_delete_link (stream->pending_events, stream->pending_events);
|
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
ret = gst_pad_push (stream->pad, buffer);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
ret = stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
GST_LOG_OBJECT (stream->pad, "Push result: %d %s", ret,
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-01-15 20:44:45 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_finish_fragment_default (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
2015-01-20 17:55:05 +00:00
|
|
|
/* No need to advance, this isn't a real fragment */
|
|
|
|
if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
return gst_adaptive_demux_stream_advance_fragment (demux, stream,
|
|
|
|
stream->fragment.duration);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken.
|
|
|
|
* Can temporarily release manifest_lock
|
|
|
|
*/
|
2015-01-15 20:44:45 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_data_received_default (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
|
|
|
|
|
|
|
buffer = gst_adapter_take_buffer (stream->adapter,
|
|
|
|
gst_adapter_available (stream->adapter));
|
|
|
|
return gst_adaptive_demux_stream_push_buffer (stream, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
_src_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstPad *srcpad = (GstPad *) parent;
|
|
|
|
GstAdaptiveDemuxStream *stream = gst_pad_get_element_private (srcpad);
|
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
/* do not make any changes if the stream is cancelled */
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
ret = stream->last_ret = GST_FLOW_FLUSHING;
|
2015-10-30 12:24:22 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
if (stream->starting_fragment) {
|
2015-06-23 16:19:35 +00:00
|
|
|
GstClockTime offset =
|
|
|
|
gst_adaptive_demux_stream_get_presentation_offset (demux, stream);
|
2015-09-02 15:29:43 +00:00
|
|
|
GstClockTime period_start =
|
|
|
|
gst_adaptive_demux_get_period_start_time (demux);
|
2015-06-23 16:19:35 +00:00
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->starting_fragment = FALSE;
|
|
|
|
if (klass->start_fragment) {
|
2015-11-29 01:02:15 +00:00
|
|
|
if (!klass->start_fragment (demux, stream)) {
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
2015-01-15 20:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_BUFFER_PTS (buffer) = stream->fragment.timestamp;
|
2015-06-23 16:19:35 +00:00
|
|
|
if (GST_BUFFER_PTS_IS_VALID (buffer))
|
|
|
|
GST_BUFFER_PTS (buffer) += offset;
|
2015-01-15 20:44:45 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (stream->pad, "set fragment pts=%" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
|
|
|
|
|
2015-06-23 16:19:35 +00:00
|
|
|
if (GST_BUFFER_PTS_IS_VALID (buffer)) {
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->segment.position = GST_BUFFER_PTS (buffer);
|
2015-09-02 15:29:43 +00:00
|
|
|
|
|
|
|
/* Convert from position inside the stream's segment to the demuxer's
|
|
|
|
* segment, they are not necessarily the same */
|
|
|
|
if (stream->segment.position - offset + period_start >
|
|
|
|
demux->segment.position)
|
|
|
|
demux->segment.position =
|
|
|
|
stream->segment.position - offset + period_start;
|
2015-06-23 16:19:35 +00:00
|
|
|
}
|
2015-01-15 20:44:45 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
2015-11-10 16:54:51 +00:00
|
|
|
if (stream->downloading_first_buffer) {
|
|
|
|
gint64 chunk_size = 0;
|
|
|
|
|
|
|
|
stream->downloading_first_buffer = FALSE;
|
|
|
|
|
|
|
|
if (!stream->downloading_header && !stream->downloading_index) {
|
|
|
|
/* If this is the first buffer of a fragment (not the headers or index)
|
|
|
|
* and we don't have a birate from the sub-class, then see if we
|
|
|
|
* can work it out from the fragment size and duration */
|
|
|
|
if (stream->fragment.bitrate == 0 &&
|
|
|
|
stream->fragment.duration != 0 &&
|
|
|
|
gst_element_query_duration (stream->uri_handler, GST_FORMAT_BYTES,
|
|
|
|
&chunk_size)) {
|
|
|
|
guint bitrate = MIN (G_MAXUINT, gst_util_uint64_scale (chunk_size,
|
|
|
|
8 * GST_SECOND, stream->fragment.duration));
|
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"Fragment has size %" G_GUINT64_FORMAT " duration %" GST_TIME_FORMAT
|
|
|
|
" = bitrate %u", chunk_size,
|
|
|
|
GST_TIME_ARGS (stream->fragment.duration), bitrate);
|
|
|
|
stream->fragment.bitrate = bitrate;
|
|
|
|
}
|
|
|
|
if (stream->fragment.bitrate) {
|
|
|
|
stream->bitrate_changed = TRUE;
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (demux, "Bitrate for fragment not available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 20:44:45 +00:00
|
|
|
|
|
|
|
stream->download_total_time +=
|
|
|
|
g_get_monotonic_time () - stream->download_chunk_start_time;
|
|
|
|
stream->download_total_bytes += gst_buffer_get_size (buffer);
|
|
|
|
|
|
|
|
gst_adapter_push (stream->adapter, buffer);
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Received buffer of size %" G_GSIZE_FORMAT
|
|
|
|
". Now %" G_GSIZE_FORMAT " on adapter", gst_buffer_get_size (buffer),
|
|
|
|
gst_adapter_available (stream->adapter));
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
ret = klass->data_received (demux, stream);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
if (ret == GST_FLOW_FLUSHING) {
|
|
|
|
/* do not make any changes if the stream is cancelled */
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
}
|
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->download_chunk_start_time = g_get_monotonic_time ();
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
if (ret < GST_FLOW_EOS) {
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL),
|
|
|
|
("stream stopped, reason %s", gst_flow_get_name (ret)));
|
|
|
|
|
|
|
|
/* TODO push this on all pads */
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_eos ());
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "stream stopped, reason %s",
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:22:14 +00:00
|
|
|
gst_adaptive_demux_stream_fragment_download_finish (stream, ret, NULL);
|
2015-01-15 20:44:45 +00:00
|
|
|
if (ret == (GstFlowReturn) GST_ADAPTIVE_DEMUX_FLOW_SWITCH)
|
|
|
|
ret = GST_FLOW_EOS; /* return EOS to make the source stop */
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 01:02:15 +00:00
|
|
|
error:
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-01-12 21:22:14 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_stream_fragment_download_finish (GstAdaptiveDemuxStream *
|
|
|
|
stream, GstFlowReturn ret, GError * err)
|
|
|
|
{
|
2015-09-15 13:00:43 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Download finish: %d %s - err: %p", ret,
|
|
|
|
gst_flow_get_name (ret), err);
|
|
|
|
|
2015-01-12 21:22:14 +00:00
|
|
|
/* if we have an error, only replace last_ret if it was OK before to avoid
|
|
|
|
* overwriting the first error we got */
|
2015-09-15 13:00:43 +00:00
|
|
|
if (stream->last_ret == GST_FLOW_OK) {
|
|
|
|
stream->last_ret = ret;
|
|
|
|
if (err) {
|
2015-01-12 21:22:14 +00:00
|
|
|
g_clear_error (&stream->last_error);
|
|
|
|
stream->last_error = g_error_copy (err);
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
stream->download_finished = TRUE;
|
2015-01-12 21:22:14 +00:00
|
|
|
g_cond_signal (&stream->fragment_download_cond);
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
}
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstPad *srcpad = GST_PAD_CAST (parent);
|
|
|
|
GstAdaptiveDemuxStream *stream = gst_pad_get_element_private (srcpad);
|
2015-09-16 15:46:29 +00:00
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_EOS:{
|
2015-01-15 20:44:45 +00:00
|
|
|
GstAdaptiveDemuxClass *klass;
|
2015-01-12 21:22:14 +00:00
|
|
|
GstFlowReturn ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
ret = klass->finish_fragment (demux, stream);
|
2015-01-12 21:22:14 +00:00
|
|
|
gst_adaptive_demux_stream_fragment_download_finish (stream, ret, NULL);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
|
|
|
{
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_ALLOCATION:
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_pad_query_default (pad, parent, query);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken.
|
|
|
|
* Can temporarily release manifest_lock
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_stream_wait_manifest_update (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
/* Wait until we're cancelled or there's something for
|
|
|
|
* us to download in the playlist or the playlist
|
|
|
|
* became non-live */
|
|
|
|
while (TRUE) {
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "No fragment left but live playlist, wait a bit");
|
|
|
|
|
|
|
|
/* get the manifest_update_lock while still holding the manifest_lock.
|
|
|
|
* This will prevent other threads to signal the condition (they will need
|
|
|
|
* both manifest_lock and manifest_update_lock in order to signal).
|
|
|
|
* It cannot deadlock because all threads always get the manifest_lock first
|
|
|
|
* and manifest_update_lock second.
|
|
|
|
*/
|
|
|
|
g_mutex_lock (&demux->priv->manifest_update_lock);
|
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
|
|
|
g_cond_wait (&demux->priv->manifest_cond,
|
|
|
|
&demux->priv->manifest_update_lock);
|
|
|
|
g_mutex_unlock (&demux->priv->manifest_update_lock);
|
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
/* check for cancelled every time we get the manifest_lock */
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = FALSE;
|
2015-09-16 15:46:29 +00:00
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2015-08-15 17:01:00 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
/* Got a new fragment or not live anymore? */
|
|
|
|
if (gst_adaptive_demux_stream_has_next_fragment (demux, stream)) {
|
2014-12-03 21:31:06 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "new fragment available, "
|
|
|
|
"not waiting for manifest update");
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_adaptive_demux_is_live (demux)) {
|
2014-12-03 21:31:06 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Not live anymore, "
|
|
|
|
"not waiting for manifest update");
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (demux, "Retrying now");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_adaptive_demux_pad_remove_eos_sticky (GstPad * pad, GstEvent ** event,
|
|
|
|
gpointer udata)
|
|
|
|
{
|
|
|
|
if (GST_EVENT_TYPE (*event) == GST_EVENT_EOS) {
|
|
|
|
gst_event_replace (event, NULL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
2014-11-22 20:27:03 +00:00
|
|
|
gst_adaptive_demux_stream_clear_eos_and_flush_state (GstAdaptiveDemuxStream *
|
|
|
|
stream)
|
2014-11-21 23:42:09 +00:00
|
|
|
{
|
|
|
|
GstPad *internal_pad;
|
|
|
|
|
|
|
|
internal_pad =
|
|
|
|
GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->pad)));
|
|
|
|
gst_pad_sticky_events_foreach (internal_pad,
|
|
|
|
_adaptive_demux_pad_remove_eos_sticky, NULL);
|
|
|
|
GST_OBJECT_FLAG_UNSET (internal_pad, GST_PAD_FLAG_EOS);
|
2014-11-22 20:27:03 +00:00
|
|
|
/* In case the stream is recovering from a flushing seek it is also needed
|
|
|
|
* to remove the flushing state from this pad. The flushing state is set
|
|
|
|
* because of the flow return propagating until the source element */
|
|
|
|
GST_PAD_UNSET_FLUSHING (internal_pad);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_object_unref (internal_pad);
|
|
|
|
}
|
|
|
|
|
2015-09-04 07:59:06 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_stream_queue_overrun (GstElement * queue, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxStream *stream = (GstAdaptiveDemuxStream *) user_data;
|
|
|
|
GST_WARNING_OBJECT (stream->pad,
|
|
|
|
"Queue overrun! The fragment to download is too big according to the current queue size limit");
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_stream_update_source (GstAdaptiveDemuxStream * stream,
|
|
|
|
const gchar * uri, const gchar * referer, gboolean refresh,
|
|
|
|
gboolean allow_cache)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
|
|
|
|
|
|
|
if (!gst_uri_is_valid (uri)) {
|
|
|
|
GST_WARNING_OBJECT (stream->pad, "Invalid URI: %s", uri);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->src != NULL) {
|
|
|
|
gchar *old_protocol, *new_protocol;
|
|
|
|
gchar *old_uri;
|
|
|
|
|
2015-09-04 07:59:06 +00:00
|
|
|
old_uri = gst_uri_handler_get_uri (GST_URI_HANDLER (stream->uri_handler));
|
2014-11-21 23:42:09 +00:00
|
|
|
old_protocol = gst_uri_get_protocol (old_uri);
|
|
|
|
new_protocol = gst_uri_get_protocol (uri);
|
|
|
|
|
|
|
|
if (!g_str_equal (old_protocol, new_protocol)) {
|
|
|
|
gst_object_unref (stream->src_srcpad);
|
|
|
|
gst_element_set_state (stream->src, GST_STATE_NULL);
|
|
|
|
gst_bin_remove (GST_BIN_CAST (demux), stream->src);
|
|
|
|
stream->src = NULL;
|
|
|
|
stream->src_srcpad = NULL;
|
|
|
|
GST_DEBUG_OBJECT (demux, "Can't re-use old source element");
|
|
|
|
} else {
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Re-using old source element");
|
2015-09-04 07:59:06 +00:00
|
|
|
if (!gst_uri_handler_set_uri (GST_URI_HANDLER (stream->uri_handler), uri,
|
|
|
|
&err)) {
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Failed to re-use old source element: %s",
|
|
|
|
err->message);
|
|
|
|
g_clear_error (&err);
|
|
|
|
gst_object_unref (stream->src_srcpad);
|
|
|
|
gst_element_set_state (stream->src, GST_STATE_NULL);
|
|
|
|
gst_bin_remove (GST_BIN_CAST (demux), stream->src);
|
|
|
|
stream->src = NULL;
|
|
|
|
stream->src_srcpad = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free (old_uri);
|
|
|
|
g_free (old_protocol);
|
|
|
|
g_free (new_protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->src == NULL) {
|
2015-09-04 07:59:06 +00:00
|
|
|
GstPad *uri_handler_src;
|
|
|
|
GstPad *queue_sink;
|
|
|
|
GstPad *queue_src;
|
|
|
|
GstElement *uri_handler;
|
|
|
|
GstElement *queue;
|
|
|
|
GstPadLinkReturn pad_link_ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstPad *internal_pad;
|
|
|
|
|
2015-09-04 07:59:06 +00:00
|
|
|
/* Our src consists of a bin containing uri_handler -> queue2 . The
|
|
|
|
* purpose of the queue2 is to allow the uri_handler to download an
|
|
|
|
* entire fragment without blocking, so we can accurately measure the
|
|
|
|
* download bitrate. */
|
|
|
|
|
|
|
|
queue = gst_element_factory_make ("queue2", NULL);
|
|
|
|
if (queue == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_object_set (queue, "max-size-bytes", (guint) SRC_QUEUE_MAX_BYTES, NULL);
|
|
|
|
g_object_set (queue, "max-size-buffers", (guint) 0, NULL);
|
|
|
|
g_object_set (queue, "max-size-time", (guint64) 0, NULL);
|
|
|
|
g_signal_connect (queue, "overrun",
|
|
|
|
G_CALLBACK (gst_adaptive_demux_stream_queue_overrun), stream);
|
|
|
|
|
|
|
|
uri_handler = gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
|
|
|
|
if (uri_handler == NULL) {
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_ELEMENT_ERROR (demux, CORE, MISSING_PLUGIN,
|
|
|
|
("Missing plugin to handle URI: '%s'", uri), (NULL));
|
2015-09-04 07:59:06 +00:00
|
|
|
gst_object_unref (queue);
|
2014-11-21 23:42:09 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-09-04 07:59:06 +00:00
|
|
|
gobject_class = G_OBJECT_GET_CLASS (uri_handler);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
if (g_object_class_find_property (gobject_class, "compress"))
|
2015-09-04 07:59:06 +00:00
|
|
|
g_object_set (uri_handler, "compress", FALSE, NULL);
|
2014-11-21 23:42:09 +00:00
|
|
|
if (g_object_class_find_property (gobject_class, "keep-alive"))
|
2015-09-04 07:59:06 +00:00
|
|
|
g_object_set (uri_handler, "keep-alive", TRUE, NULL);
|
2014-11-21 23:42:09 +00:00
|
|
|
if (g_object_class_find_property (gobject_class, "extra-headers")) {
|
|
|
|
if (referer || refresh || !allow_cache) {
|
|
|
|
GstStructure *extra_headers = gst_structure_new_empty ("headers");
|
|
|
|
|
|
|
|
if (referer)
|
|
|
|
gst_structure_set (extra_headers, "Referer", G_TYPE_STRING, referer,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!allow_cache)
|
|
|
|
gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
|
|
|
|
"no-cache", NULL);
|
|
|
|
else if (refresh)
|
|
|
|
gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
|
|
|
|
"max-age=0", NULL);
|
|
|
|
|
2015-09-04 07:59:06 +00:00
|
|
|
g_object_set (uri_handler, "extra-headers", extra_headers, NULL);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
gst_structure_free (extra_headers);
|
|
|
|
} else {
|
2015-09-04 07:59:06 +00:00
|
|
|
g_object_set (uri_handler, "extra-headers", NULL, NULL);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 07:59:06 +00:00
|
|
|
/* Source bin creation */
|
|
|
|
stream->src = gst_bin_new (NULL);
|
|
|
|
if (stream->src == NULL) {
|
|
|
|
gst_object_unref (queue);
|
|
|
|
gst_object_unref (uri_handler);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_bin_add (GST_BIN_CAST (stream->src), queue);
|
|
|
|
gst_bin_add (GST_BIN_CAST (stream->src), uri_handler);
|
|
|
|
|
|
|
|
uri_handler_src = gst_element_get_static_pad (uri_handler, "src");
|
|
|
|
queue_sink = gst_element_get_static_pad (queue, "sink");
|
|
|
|
|
|
|
|
pad_link_ret =
|
|
|
|
gst_pad_link_full (uri_handler_src, queue_sink,
|
|
|
|
GST_PAD_LINK_CHECK_NOTHING);
|
|
|
|
if (GST_PAD_LINK_FAILED (pad_link_ret)) {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Could not link pads %s:%s to %s:%s for reason %d",
|
|
|
|
GST_DEBUG_PAD_NAME (uri_handler_src), GST_DEBUG_PAD_NAME (queue_sink),
|
|
|
|
pad_link_ret);
|
2015-11-10 01:02:38 +00:00
|
|
|
g_object_unref (queue_sink);
|
|
|
|
g_object_unref (uri_handler_src);
|
2015-09-04 07:59:06 +00:00
|
|
|
gst_object_unref (stream->src);
|
|
|
|
stream->src = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-10 01:02:38 +00:00
|
|
|
g_object_unref (queue_sink);
|
|
|
|
g_object_unref (uri_handler_src);
|
2015-09-04 07:59:06 +00:00
|
|
|
queue_src = gst_element_get_static_pad (queue, "src");
|
|
|
|
stream->src_srcpad = gst_ghost_pad_new ("src", queue_src);
|
|
|
|
g_object_unref (queue_src);
|
|
|
|
gst_element_add_pad (stream->src, stream->src_srcpad);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_element_set_locked_state (stream->src, TRUE);
|
|
|
|
gst_bin_add (GST_BIN_CAST (demux), stream->src);
|
|
|
|
stream->src_srcpad = gst_element_get_static_pad (stream->src, "src");
|
|
|
|
|
|
|
|
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (stream->pad),
|
|
|
|
stream->src_srcpad);
|
|
|
|
|
|
|
|
/* set up our internal pad to drop all events from
|
|
|
|
* the http src we don't care about. On the chain function
|
|
|
|
* we just push the buffer forward, but this way dash can get
|
|
|
|
* the flow return from downstream */
|
|
|
|
internal_pad =
|
|
|
|
GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->pad)));
|
|
|
|
gst_pad_set_chain_function (GST_PAD_CAST (internal_pad), _src_chain);
|
|
|
|
gst_pad_set_event_function (GST_PAD_CAST (internal_pad), _src_event);
|
|
|
|
/* need to set query otherwise deadlocks happen with allocation queries */
|
|
|
|
gst_pad_set_query_function (GST_PAD_CAST (internal_pad), _src_query);
|
|
|
|
gst_object_unref (internal_pad);
|
2015-09-04 07:59:06 +00:00
|
|
|
stream->uri_handler = uri_handler;
|
|
|
|
stream->queue = queue;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken.
|
|
|
|
* Can temporarily release manifest_lock
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_download_uri (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, const gchar * uri, gint64 start,
|
|
|
|
gint64 end)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Downloading uri: %s, range:%" G_GINT64_FORMAT
|
|
|
|
" - %" G_GINT64_FORMAT, uri, start, end);
|
|
|
|
|
|
|
|
if (!gst_adaptive_demux_stream_update_source (stream, uri, NULL, FALSE, TRUE)) {
|
|
|
|
ret = stream->last_ret = GST_FLOW_ERROR;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gst_element_set_state (stream->src,
|
|
|
|
GST_STATE_READY) != GST_STATE_CHANGE_FAILURE) {
|
|
|
|
if (start != 0 || end != -1) {
|
2015-10-14 07:34:58 +00:00
|
|
|
/* HTTP ranges are inclusive, GStreamer segments are exclusive for the
|
|
|
|
* stop position */
|
|
|
|
if (end != -1)
|
|
|
|
end += 1;
|
2015-09-04 07:59:06 +00:00
|
|
|
/* Send the seek event to the uri_handler, as the other pipeline elements
|
|
|
|
* can't handle it when READY. */
|
|
|
|
if (!gst_element_send_event (stream->uri_handler, gst_event_new_seek (1.0,
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_FORMAT_BYTES, (GstSeekFlags) GST_SEEK_FLAG_FLUSH,
|
|
|
|
GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_SET, end))) {
|
|
|
|
|
|
|
|
/* looks like the source can't handle seeks in READY */
|
|
|
|
g_clear_error (&stream->last_error);
|
|
|
|
stream->last_error = g_error_new (GST_CORE_ERROR,
|
|
|
|
GST_CORE_ERROR_NOT_IMPLEMENTED,
|
|
|
|
"Source element can't handle range requests");
|
|
|
|
stream->last_ret = GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_LIKELY (stream->last_ret == GST_FLOW_OK)) {
|
|
|
|
stream->download_start_time = g_get_monotonic_time ();
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->download_chunk_start_time = g_get_monotonic_time ();
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2015-10-29 23:41:46 +00:00
|
|
|
/* src element is in state READY. Before we start it, we reset
|
|
|
|
* download_finished
|
|
|
|
*/
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
stream->download_finished = FALSE;
|
2015-11-10 16:54:51 +00:00
|
|
|
stream->downloading_first_buffer = TRUE;
|
2015-10-29 23:41:46 +00:00
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2015-10-07 22:20:51 +00:00
|
|
|
if (!gst_element_sync_state_with_parent (stream->src)) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Could not sync state for src element");
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
ret = stream->last_ret = GST_FLOW_ERROR;
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
/* wait for the fragment to be completely downloaded */
|
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"Waiting for fragment download to finish: %s", uri);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
ret = stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
while (!stream->cancelled && !stream->download_finished) {
|
2014-11-21 23:42:09 +00:00
|
|
|
g_cond_wait (&stream->fragment_download_cond,
|
|
|
|
&stream->fragment_download_lock);
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
ret = stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = stream->last_ret;
|
|
|
|
|
2015-09-15 13:00:43 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Fragment download finished: %s %d %s",
|
|
|
|
uri, stream->last_ret, gst_flow_get_name (stream->last_ret));
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (stream->last_ret == GST_FLOW_OK)
|
|
|
|
stream->last_ret = GST_FLOW_CUSTOM_ERROR;
|
|
|
|
ret = GST_FLOW_CUSTOM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flush the proxypads so that the EOS state is reset */
|
|
|
|
gst_pad_push_event (stream->src_srcpad, gst_event_new_flush_start ());
|
|
|
|
|
2015-10-30 00:04:12 +00:00
|
|
|
/* sending flush stop event will serialiase on stream->src_srcpad.
|
|
|
|
* But the _src_chain function will first get the pad lock and then the
|
|
|
|
* manifest lock, so we cannot hold the manifest lock here while
|
|
|
|
* we will try to get the pad lock (taking locks in reversing order
|
|
|
|
* will lead to deadlock)
|
|
|
|
*
|
|
|
|
* In conclusion, we need to release the manifest lock before flushing
|
2015-09-16 15:46:29 +00:00
|
|
|
*/
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2015-10-30 00:04:12 +00:00
|
|
|
gst_pad_push_event (stream->src_srcpad, gst_event_new_flush_stop (TRUE));
|
|
|
|
|
|
|
|
/* changing src element state might try to join the streaming thread, so
|
|
|
|
* we must not hold the manifest lock.
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_element_set_state (stream->src, GST_STATE_READY);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
ret = stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2014-11-22 20:27:03 +00:00
|
|
|
gst_adaptive_demux_stream_clear_eos_and_flush_state (stream);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken.
|
|
|
|
* Can temporarily release manifest_lock
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_download_header_fragment (GstAdaptiveDemuxStream *
|
|
|
|
stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
|
|
|
|
if (stream->fragment.header_uri != NULL) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Fetching header %s %" G_GINT64_FORMAT "-%"
|
|
|
|
G_GINT64_FORMAT, stream->fragment.header_uri,
|
|
|
|
stream->fragment.header_range_start, stream->fragment.header_range_end);
|
|
|
|
|
2015-01-09 20:19:54 +00:00
|
|
|
stream->downloading_header = TRUE;
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = gst_adaptive_demux_stream_download_uri (demux, stream,
|
|
|
|
stream->fragment.header_uri, stream->fragment.header_range_start,
|
|
|
|
stream->fragment.header_range_end);
|
2015-01-09 20:19:54 +00:00
|
|
|
stream->downloading_header = FALSE;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we have an index */
|
2015-09-16 15:46:29 +00:00
|
|
|
if (ret == GST_FLOW_OK) { /* TODO check for other valid types */
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
if (stream->fragment.index_uri != NULL) {
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Fetching index %s %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT,
|
|
|
|
stream->fragment.index_uri,
|
|
|
|
stream->fragment.index_range_start, stream->fragment.index_range_end);
|
2015-01-09 20:19:54 +00:00
|
|
|
stream->downloading_index = TRUE;
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = gst_adaptive_demux_stream_download_uri (demux, stream,
|
|
|
|
stream->fragment.index_uri, stream->fragment.index_range_start,
|
|
|
|
stream->fragment.index_range_end);
|
2015-01-09 20:19:54 +00:00
|
|
|
stream->downloading_index = FALSE;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken.
|
|
|
|
* Can temporarily release manifest_lock
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_download_fragment (GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
|
|
|
gchar *url = NULL;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
|
|
|
|
stream->starting_fragment = TRUE;
|
|
|
|
stream->last_ret = GST_FLOW_OK;
|
|
|
|
stream->first_fragment_buffer = TRUE;
|
|
|
|
|
2015-01-13 13:13:47 +00:00
|
|
|
if (stream->fragment.uri == NULL && stream->fragment.header_uri == NULL &&
|
|
|
|
stream->fragment.index_uri == NULL)
|
|
|
|
goto no_url_error;
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
if (stream->need_header) {
|
|
|
|
ret = gst_adaptive_demux_stream_download_header_fragment (stream);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-08 11:35:44 +00:00
|
|
|
stream->need_header = FALSE;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
url = stream->fragment.uri;
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Got url '%s' for stream %p", url, stream);
|
2015-01-13 13:13:47 +00:00
|
|
|
if (url) {
|
2015-01-13 13:15:21 +00:00
|
|
|
ret =
|
|
|
|
gst_adaptive_demux_stream_download_uri (demux, stream, url,
|
|
|
|
stream->fragment.range_start, stream->fragment.range_end);
|
2015-01-13 13:13:47 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Fragment download result: %d %s",
|
|
|
|
stream->last_ret, gst_flow_get_name (stream->last_ret));
|
2015-01-15 20:44:45 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
2015-09-16 15:46:29 +00:00
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
|
|
|
/* TODO check if we are truly stopping */
|
2015-07-07 16:48:23 +00:00
|
|
|
if (ret == GST_FLOW_CUSTOM_ERROR && gst_adaptive_demux_is_live (demux)) {
|
2015-06-05 09:26:11 +00:00
|
|
|
if (++stream->download_error_count <= MAX_DOWNLOAD_ERROR_COUNT) {
|
|
|
|
/* looks like there is no way of knowing when a live stream has ended
|
|
|
|
* Have to assume we are falling behind and cause a manifest reload */
|
2015-07-07 15:37:24 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"Converting error of live stream to EOS");
|
2015-06-05 09:26:11 +00:00
|
|
|
return GST_FLOW_EOS;
|
|
|
|
}
|
2015-07-07 16:48:23 +00:00
|
|
|
} else if (ret == GST_FLOW_CUSTOM_ERROR
|
2015-07-07 15:37:24 +00:00
|
|
|
&& !gst_adaptive_demux_stream_has_next_fragment (demux, stream)) {
|
|
|
|
/* If this is the last fragment, consider failures EOS and not actual
|
|
|
|
* errors. Due to rounding errors in the durations, the last fragment
|
|
|
|
* might not actually exist */
|
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"Converting error for last fragment to EOS");
|
|
|
|
return GST_FLOW_EOS;
|
2015-01-13 13:13:47 +00:00
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
no_url_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
|
|
|
|
(_("Failed to get fragment URL.")),
|
|
|
|
("An error happened when getting fragment URL"));
|
2014-12-03 16:52:07 +00:00
|
|
|
gst_task_stop (stream->download_task);
|
2014-11-21 23:42:09 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* this function will take the manifest_lock and will keep it until the end.
|
|
|
|
* It will release it temporarily only when going to sleep.
|
|
|
|
* Every time it takes the manifest_lock, it will check for cancelled condition
|
|
|
|
*/
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_stream_download_loop (GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = stream->demux;
|
2015-10-08 10:45:25 +00:00
|
|
|
guint64 next_download = g_get_monotonic_time ();
|
2014-11-21 23:42:09 +00:00
|
|
|
GstFlowReturn ret;
|
|
|
|
gboolean live;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (stream->pad, "download loop start");
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
goto cancelled;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
/* Check if we're done with our segment */
|
|
|
|
if (demux->segment.rate > 0) {
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop)
|
2015-09-02 15:29:43 +00:00
|
|
|
&& stream->segment.position >= stream->segment.stop) {
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = GST_FLOW_EOS;
|
2014-12-03 16:52:07 +00:00
|
|
|
gst_task_stop (stream->download_task);
|
2014-11-21 23:42:09 +00:00
|
|
|
goto end_of_manifest;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (demux->segment.start)
|
2015-09-02 15:29:43 +00:00
|
|
|
&& stream->segment.position < stream->segment.start) {
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = GST_FLOW_EOS;
|
2014-12-03 16:52:07 +00:00
|
|
|
gst_task_stop (stream->download_task);
|
2014-11-21 23:42:09 +00:00
|
|
|
goto end_of_manifest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup old streams if any */
|
|
|
|
if (G_UNLIKELY (demux->priv->old_streams != NULL)) {
|
|
|
|
GList *old_streams = demux->priv->old_streams;
|
|
|
|
demux->priv->old_streams = NULL;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Cleaning up old streams");
|
|
|
|
g_list_free_full (old_streams,
|
|
|
|
(GDestroyNotify) gst_adaptive_demux_stream_free);
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Cleaning up old streams (done)");
|
2015-09-16 15:46:29 +00:00
|
|
|
|
|
|
|
/* gst_adaptive_demux_stream_free had temporarily released the manifest_lock.
|
|
|
|
* Recheck the cancelled flag.
|
|
|
|
*/
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
goto cancelled;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (G_UNLIKELY (stream->restart_download)) {
|
|
|
|
GstSegment segment;
|
|
|
|
GstEvent *seg_event;
|
2015-09-02 15:29:43 +00:00
|
|
|
GstClockTime cur, ts;
|
2014-11-21 23:42:09 +00:00
|
|
|
gint64 pos;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"Activating stream due to reconfigure event");
|
|
|
|
|
2015-09-02 15:29:43 +00:00
|
|
|
cur = ts =
|
|
|
|
gst_segment_to_stream_time (&stream->segment, GST_FORMAT_TIME,
|
|
|
|
stream->segment.position);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
if (gst_pad_peer_query_position (stream->pad, GST_FORMAT_TIME, &pos)) {
|
|
|
|
ts = (GstClockTime) pos;
|
|
|
|
GST_DEBUG_OBJECT (demux, "Downstream position: %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (ts));
|
|
|
|
} else {
|
|
|
|
/* query other pads as some faulty element in the pad's branch might
|
|
|
|
* reject position queries. This should be better than using the
|
|
|
|
* demux segment position that can be much ahead */
|
2015-05-26 14:24:52 +00:00
|
|
|
GList *iter;
|
|
|
|
|
|
|
|
for (iter = demux->streams; iter != NULL; iter = g_list_next (iter)) {
|
2015-05-12 15:55:17 +00:00
|
|
|
GstAdaptiveDemuxStream *cur_stream =
|
|
|
|
(GstAdaptiveDemuxStream *) iter->data;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-05-12 15:55:17 +00:00
|
|
|
if (gst_pad_peer_query_position (cur_stream->pad, GST_FORMAT_TIME,
|
|
|
|
&pos)) {
|
2014-11-21 23:42:09 +00:00
|
|
|
ts = (GstClockTime) pos;
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Downstream position: %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (ts));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we might have already pushed this data */
|
|
|
|
ts = MAX (ts, cur);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Restarting stream at "
|
|
|
|
"position %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_segment_copy_into (&demux->segment, &segment);
|
|
|
|
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (ts)) {
|
2015-09-02 15:29:43 +00:00
|
|
|
GstClockTime offset, period_start;
|
|
|
|
|
|
|
|
offset =
|
|
|
|
gst_adaptive_demux_stream_get_presentation_offset (demux, stream);
|
|
|
|
period_start = gst_adaptive_demux_get_period_start_time (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
/* TODO check return */
|
|
|
|
gst_adaptive_demux_stream_seek (demux, stream, ts);
|
|
|
|
|
2015-09-02 15:29:43 +00:00
|
|
|
segment.position = ts - period_start + offset;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
2015-09-02 15:29:43 +00:00
|
|
|
|
|
|
|
/* The stream's segment is still correct except for
|
|
|
|
* the position, so let's send a new one with the
|
|
|
|
* updated position */
|
2015-06-25 21:32:10 +00:00
|
|
|
seg_event = gst_event_new_segment (&stream->segment);
|
|
|
|
gst_event_set_seqnum (seg_event, demux->priv->segment_seqnum);
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Sending restart segment: %"
|
|
|
|
GST_PTR_FORMAT, seg_event);
|
|
|
|
gst_pad_push_event (stream->pad, seg_event);
|
|
|
|
|
|
|
|
stream->restart_download = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
live = gst_adaptive_demux_is_live (demux);
|
|
|
|
|
|
|
|
ret = gst_adaptive_demux_stream_update_fragment_info (demux, stream);
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Fragment info update result: %d %s",
|
|
|
|
ret, gst_flow_get_name (ret));
|
|
|
|
if (ret == GST_FLOW_OK) {
|
|
|
|
|
|
|
|
/* wait for live fragments to be available */
|
|
|
|
if (live) {
|
|
|
|
gint64 wait_time =
|
|
|
|
gst_adaptive_demux_stream_get_fragment_waiting_time (demux, stream);
|
2015-09-16 15:46:29 +00:00
|
|
|
if (wait_time > 0) {
|
|
|
|
gint64 end_time = g_get_monotonic_time () + wait_time / GST_USECOND;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Download waiting for %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (wait_time));
|
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
goto cancelled;
|
|
|
|
}
|
|
|
|
g_cond_wait_until (&stream->fragment_download_cond,
|
|
|
|
&stream->fragment_download_lock, end_time);
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Download finished waiting");
|
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
goto cancelled;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->last_ret = GST_FLOW_OK;
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
next_download = g_get_monotonic_time ();
|
|
|
|
ret = gst_adaptive_demux_stream_download_fragment (stream);
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
if (ret == GST_FLOW_FLUSHING) {
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
goto cancelled;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:13:49 +00:00
|
|
|
} else {
|
|
|
|
stream->last_ret = ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case GST_FLOW_OK:
|
|
|
|
break; /* all is good, let's go */
|
|
|
|
case GST_FLOW_EOS:
|
2014-12-01 00:54:38 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "EOS, checking to stop download loop");
|
2014-11-21 23:42:09 +00:00
|
|
|
/* we push the EOS after releasing the object lock */
|
2014-12-01 00:54:38 +00:00
|
|
|
if (gst_adaptive_demux_is_live (demux)) {
|
|
|
|
if (gst_adaptive_demux_stream_wait_manifest_update (demux, stream)) {
|
2015-09-16 15:46:29 +00:00
|
|
|
goto end;
|
2014-12-01 00:54:38 +00:00
|
|
|
}
|
2014-12-03 16:52:07 +00:00
|
|
|
gst_task_stop (stream->download_task);
|
2014-12-01 00:54:38 +00:00
|
|
|
} else {
|
2014-12-03 16:52:07 +00:00
|
|
|
gst_task_stop (stream->download_task);
|
2014-12-01 00:54:38 +00:00
|
|
|
if (gst_adaptive_demux_combine_flows (demux) == GST_FLOW_EOS) {
|
|
|
|
if (gst_adaptive_demux_has_next_period (demux)) {
|
|
|
|
gst_adaptive_demux_advance_period (demux);
|
|
|
|
ret = GST_FLOW_OK;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_FLOW_NOT_LINKED:
|
2014-12-03 16:52:07 +00:00
|
|
|
gst_task_stop (stream->download_task);
|
2014-11-21 23:42:09 +00:00
|
|
|
if (gst_adaptive_demux_combine_flows (demux)
|
|
|
|
== GST_FLOW_NOT_LINKED) {
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, FAILED,
|
|
|
|
(_("Internal data stream error.")), ("stream stopped, reason %s",
|
|
|
|
gst_flow_get_name (GST_FLOW_NOT_LINKED)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_FLOW_FLUSHING:{
|
|
|
|
GList *iter;
|
|
|
|
|
|
|
|
for (iter = demux->streams; iter; iter = g_list_next (iter)) {
|
|
|
|
GstAdaptiveDemuxStream *other;
|
|
|
|
|
|
|
|
other = iter->data;
|
2014-12-03 16:52:07 +00:00
|
|
|
gst_task_stop (other->download_task);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (ret <= GST_FLOW_ERROR) {
|
|
|
|
gboolean is_live = gst_adaptive_demux_is_live (demux);
|
|
|
|
GST_WARNING_OBJECT (demux, "Error while downloading fragment");
|
|
|
|
if (++stream->download_error_count > MAX_DOWNLOAD_ERROR_COUNT) {
|
|
|
|
goto download_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_clear_error (&stream->last_error);
|
|
|
|
|
|
|
|
/* First try to update the playlist for non-live playlists
|
|
|
|
* in case the URIs have changed in the meantime. But only
|
|
|
|
* try it the first time, after that we're going to wait a
|
|
|
|
* a bit to not flood the server */
|
|
|
|
if (stream->download_error_count == 1 && !is_live) {
|
|
|
|
/* TODO hlsdemux had more options to this function (boolean and err) */
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-12-10 21:23:02 +00:00
|
|
|
if (gst_adaptive_demux_update_manifest (demux) == GST_FLOW_OK) {
|
2014-11-21 23:42:09 +00:00
|
|
|
/* Retry immediately, the playlist actually has changed */
|
|
|
|
GST_DEBUG_OBJECT (demux, "Updated the playlist");
|
2015-09-16 15:46:29 +00:00
|
|
|
goto end;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait half the fragment duration before retrying */
|
|
|
|
next_download +=
|
|
|
|
gst_util_uint64_scale
|
|
|
|
(stream->fragment.duration, G_USEC_PER_SEC, 2 * GST_SECOND);
|
|
|
|
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
2015-09-16 15:46:29 +00:00
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
2014-11-21 23:42:09 +00:00
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
goto cancelled;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
g_cond_wait_until (&stream->fragment_download_cond,
|
|
|
|
&stream->fragment_download_lock, next_download);
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Retrying now");
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
g_mutex_lock (&stream->fragment_download_lock);
|
|
|
|
if (G_UNLIKELY (stream->cancelled)) {
|
|
|
|
stream->last_ret = GST_FLOW_FLUSHING;
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
goto cancelled;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&stream->fragment_download_lock);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
/* Refetch the playlist now after we waited */
|
2014-12-10 21:23:02 +00:00
|
|
|
if (!is_live
|
|
|
|
&& gst_adaptive_demux_update_manifest (demux) == GST_FLOW_OK) {
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Updated the playlist");
|
|
|
|
}
|
2015-09-16 15:46:29 +00:00
|
|
|
goto end;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-08 11:53:00 +00:00
|
|
|
end_of_manifest:
|
2014-11-21 23:42:09 +00:00
|
|
|
if (G_UNLIKELY (ret == GST_FLOW_EOS)) {
|
|
|
|
gst_adaptive_demux_stream_push_event (stream, gst_event_new_eos ());
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
GST_LOG_OBJECT (stream->pad, "download loop end");
|
|
|
|
return;
|
|
|
|
|
|
|
|
cancelled:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Stream has been cancelled");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
download_error:
|
|
|
|
{
|
|
|
|
GstMessage *msg;
|
|
|
|
|
|
|
|
if (stream->last_error) {
|
|
|
|
gchar *debug = g_strdup_printf ("Error on stream %s:%s",
|
|
|
|
GST_DEBUG_PAD_NAME (stream->pad));
|
|
|
|
msg =
|
|
|
|
gst_message_new_error (GST_OBJECT_CAST (demux), stream->last_error,
|
|
|
|
debug);
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Download error: %s",
|
|
|
|
stream->last_error->message);
|
|
|
|
g_free (debug);
|
|
|
|
} else {
|
|
|
|
GError *err =
|
|
|
|
g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
|
|
|
|
_("Couldn't download fragments"));
|
|
|
|
msg =
|
|
|
|
gst_message_new_error (GST_OBJECT_CAST (demux), err,
|
|
|
|
"Fragment downloading has failed consecutive times");
|
|
|
|
g_error_free (err);
|
|
|
|
GST_ERROR_OBJECT (stream->pad,
|
|
|
|
"Download error: Couldn't download fragments, too many failures");
|
|
|
|
}
|
|
|
|
|
2015-10-30 12:49:20 +00:00
|
|
|
gst_task_stop (stream->download_task);
|
|
|
|
if (stream->src) {
|
|
|
|
gst_element_set_state (stream->src, GST_STATE_NULL);
|
|
|
|
gst_bin_remove (GST_BIN_CAST (demux), stream->src);
|
|
|
|
stream->src = NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux), msg);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_adaptive_demux_updates_loop (GstAdaptiveDemux * demux)
|
|
|
|
{
|
2015-09-16 15:46:29 +00:00
|
|
|
gint64 next_update;
|
2014-11-21 23:42:09 +00:00
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
/* Loop for updating of the playlist. This periodically checks if
|
|
|
|
* the playlist is updated and does so, then signals the streaming
|
|
|
|
* thread in case it can continue downloading now. */
|
|
|
|
|
|
|
|
/* block until the next scheduled update or the signal to quit this thread */
|
|
|
|
GST_DEBUG_OBJECT (demux, "Started updates task");
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
next_update = g_get_monotonic_time () +
|
|
|
|
klass->get_manifest_update_interval (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
/* Updating playlist only needed for live playlists */
|
|
|
|
while (gst_adaptive_demux_is_live (demux)) {
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* Wait here until we should do the next update or we're cancelled */
|
|
|
|
GST_DEBUG_OBJECT (demux, "Wait for next playlist update");
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
g_mutex_lock (&demux->priv->updates_timed_lock);
|
|
|
|
if (demux->priv->stop_updates_task) {
|
|
|
|
g_mutex_unlock (&demux->priv->updates_timed_lock);
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
g_cond_wait_until (&demux->priv->updates_timed_cond,
|
2015-09-16 15:46:29 +00:00
|
|
|
&demux->priv->updates_timed_lock, next_update);
|
|
|
|
g_mutex_unlock (&demux->priv->updates_timed_lock);
|
|
|
|
|
|
|
|
GST_MANIFEST_LOCK (demux);
|
|
|
|
|
|
|
|
g_mutex_lock (&demux->priv->updates_timed_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
if (demux->priv->stop_updates_task) {
|
|
|
|
g_mutex_unlock (&demux->priv->updates_timed_lock);
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
2014-11-21 23:42:09 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&demux->priv->updates_timed_lock);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Updating playlist");
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
ret = gst_adaptive_demux_update_manifest (demux);
|
2015-09-16 15:46:29 +00:00
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
if (ret == GST_FLOW_EOS) {
|
|
|
|
} else if (ret != GST_FLOW_OK) {
|
2015-09-16 15:46:29 +00:00
|
|
|
/* update_failed_count is used only here, no need to protect it */
|
2014-11-21 23:42:09 +00:00
|
|
|
demux->priv->update_failed_count++;
|
|
|
|
if (demux->priv->update_failed_count <= DEFAULT_FAILED_COUNT) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Could not update the playlist");
|
2015-09-16 15:46:29 +00:00
|
|
|
next_update = g_get_monotonic_time () +
|
2014-11-21 23:42:09 +00:00
|
|
|
klass->get_manifest_update_interval (demux);
|
|
|
|
} else {
|
2015-04-21 20:04:56 +00:00
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, FAILED,
|
|
|
|
(_("Internal data stream error.")), ("Could not update playlist"));
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Stopped updates task because of error");
|
|
|
|
gst_task_stop (demux->priv->updates_task);
|
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
goto end;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Updated playlist successfully");
|
2015-09-16 15:46:29 +00:00
|
|
|
next_update = g_get_monotonic_time () +
|
|
|
|
klass->get_manifest_update_interval (demux);
|
|
|
|
|
|
|
|
/* Wake up download tasks */
|
|
|
|
g_mutex_lock (&demux->priv->manifest_update_lock);
|
|
|
|
g_cond_broadcast (&demux->priv->manifest_cond);
|
|
|
|
g_mutex_unlock (&demux->priv->manifest_update_lock);
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_MANIFEST_UNLOCK (demux);
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
quit:
|
|
|
|
{
|
2015-09-16 15:46:29 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Stop updates task request detected.");
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
end:
|
2014-11-21 23:42:09 +00:00
|
|
|
{
|
2015-09-16 15:46:29 +00:00
|
|
|
return;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_stream_push_event (GstAdaptiveDemuxStream * stream,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
|
|
|
|
stream->eos = TRUE;
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (stream),
|
|
|
|
"Pushing event %" GST_PTR_FORMAT, event);
|
|
|
|
ret = gst_pad_push_event (GST_ADAPTIVE_DEMUX_STREAM_PAD (stream), event);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_is_live (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
if (klass->is_live)
|
|
|
|
return klass->is_live (demux);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_seek (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstClockTime ts)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
if (klass->stream_seek)
|
|
|
|
return klass->stream_seek (stream, ts);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_stream_has_next_fragment (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
if (klass->stream_has_next_fragment)
|
|
|
|
ret = klass->stream_has_next_fragment (stream);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-01-15 20:44:45 +00:00
|
|
|
GstFlowReturn
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adaptive_demux_stream_advance_fragment (GstAdaptiveDemux * demux,
|
2015-01-15 20:44:45 +00:00
|
|
|
GstAdaptiveDemuxStream * stream, GstClockTime duration)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
|
|
|
if (stream->last_ret == GST_FLOW_OK) {
|
|
|
|
stream->last_ret =
|
|
|
|
gst_adaptive_demux_stream_advance_fragment_unlocked (demux, stream,
|
|
|
|
duration);
|
|
|
|
}
|
|
|
|
ret = stream->last_ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-01-15 20:44:45 +00:00
|
|
|
GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_advance_fragment_unlocked (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstClockTime duration)
|
2014-11-21 23:42:09 +00:00
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
2015-01-15 20:44:45 +00:00
|
|
|
GstFlowReturn ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (klass->stream_advance_fragment != NULL, GST_FLOW_ERROR);
|
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->download_error_count = 0;
|
|
|
|
g_clear_error (&stream->last_error);
|
|
|
|
|
|
|
|
/* FIXME - url has no indication of byte ranges for subsegments */
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_element (GST_OBJECT_CAST (demux),
|
2015-09-07 14:57:05 +00:00
|
|
|
gst_structure_new (GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME,
|
2015-01-15 20:44:45 +00:00
|
|
|
"manifest-uri", G_TYPE_STRING,
|
|
|
|
demux->manifest_uri, "uri", G_TYPE_STRING,
|
|
|
|
stream->fragment.uri, "fragment-start-time",
|
|
|
|
GST_TYPE_CLOCK_TIME, stream->download_start_time,
|
|
|
|
"fragment-stop-time", GST_TYPE_CLOCK_TIME,
|
|
|
|
gst_util_get_timestamp (), "fragment-size", G_TYPE_UINT64,
|
|
|
|
stream->download_total_bytes, "fragment-download-time",
|
|
|
|
GST_TYPE_CLOCK_TIME,
|
|
|
|
stream->download_total_time * GST_USECOND, NULL)));
|
|
|
|
|
2015-06-23 16:19:35 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (duration)) {
|
2015-09-02 15:29:43 +00:00
|
|
|
GstClockTime offset =
|
|
|
|
gst_adaptive_demux_stream_get_presentation_offset (demux, stream);
|
|
|
|
GstClockTime period_start =
|
|
|
|
gst_adaptive_demux_get_period_start_time (demux);
|
|
|
|
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->segment.position += duration;
|
2015-09-02 15:29:43 +00:00
|
|
|
|
|
|
|
/* Convert from position inside the stream's segment to the demuxer's
|
|
|
|
* segment, they are not necessarily the same */
|
|
|
|
if (stream->segment.position - offset + period_start >
|
|
|
|
demux->segment.position)
|
|
|
|
demux->segment.position =
|
|
|
|
stream->segment.position - offset + period_start;
|
2015-06-23 16:19:35 +00:00
|
|
|
}
|
2015-03-27 03:27:34 +00:00
|
|
|
|
|
|
|
if (gst_adaptive_demux_is_live (demux)
|
|
|
|
|| gst_adaptive_demux_stream_has_next_fragment (demux, stream)) {
|
|
|
|
ret = klass->stream_advance_fragment (stream);
|
|
|
|
} else {
|
|
|
|
ret = GST_FLOW_EOS;
|
|
|
|
}
|
2015-01-15 20:44:45 +00:00
|
|
|
|
|
|
|
stream->download_start_time = stream->download_chunk_start_time =
|
|
|
|
g_get_monotonic_time ();
|
|
|
|
|
|
|
|
if (ret == GST_FLOW_OK) {
|
|
|
|
if (gst_adaptive_demux_stream_select_bitrate (demux, stream,
|
2015-01-27 12:48:42 +00:00
|
|
|
gst_adaptive_demux_stream_update_current_bitrate (demux, stream))) {
|
2015-01-15 20:44:45 +00:00
|
|
|
stream->need_header = TRUE;
|
|
|
|
gst_adapter_clear (stream->adapter);
|
2015-01-20 12:23:02 +00:00
|
|
|
ret = (GstFlowReturn) GST_ADAPTIVE_DEMUX_FLOW_SWITCH;
|
2015-01-15 20:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the subclass might want to switch pads */
|
|
|
|
if (G_UNLIKELY (demux->next_streams)) {
|
|
|
|
gst_task_stop (stream->download_task);
|
|
|
|
/* TODO only allow switching streams if other downloads are not ongoing */
|
|
|
|
GST_DEBUG_OBJECT (demux, "Subclass wants new pads "
|
|
|
|
"to do bitrate switching");
|
2015-09-15 19:50:19 +00:00
|
|
|
gst_adaptive_demux_expose_streams (demux, FALSE);
|
2015-01-15 20:44:45 +00:00
|
|
|
gst_adaptive_demux_start_tasks (demux);
|
|
|
|
ret = GST_FLOW_EOS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_stream_select_bitrate (GstAdaptiveDemux *
|
|
|
|
demux, GstAdaptiveDemuxStream * stream, guint64 bitrate)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
/* FIXME: Currently several issues have be found when letting bitrate adaptation
|
|
|
|
* happen using trick modes (such as 'All streams finished without buffers') and
|
|
|
|
* the adaptive algorithm does not properly behave. */
|
|
|
|
if (demux->segment.rate != 1.0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (klass->stream_select_bitrate)
|
|
|
|
return klass->stream_select_bitrate (stream, bitrate);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_stream_update_fragment_info (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
g_return_val_if_fail (klass->stream_update_fragment_info != NULL,
|
|
|
|
GST_FLOW_ERROR);
|
|
|
|
|
2015-11-10 16:54:51 +00:00
|
|
|
/* Make sure the sub-class will update bitrate, or else
|
|
|
|
* we will later */
|
|
|
|
stream->fragment.bitrate = 0;
|
|
|
|
|
2014-11-21 23:42:09 +00:00
|
|
|
return klass->stream_update_fragment_info (stream);
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gint64
|
|
|
|
gst_adaptive_demux_stream_get_fragment_waiting_time (GstAdaptiveDemux *
|
|
|
|
demux, GstAdaptiveDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
if (klass->stream_get_fragment_waiting_time)
|
|
|
|
return klass->stream_get_fragment_waiting_time (stream);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static GstFlowReturn
|
2015-04-23 15:22:11 +00:00
|
|
|
gst_adaptive_demux_update_manifest_default (GstAdaptiveDemux * demux)
|
2014-11-21 23:42:09 +00:00
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
GstFragment *download;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
2015-04-23 15:02:44 +00:00
|
|
|
download = gst_uri_downloader_fetch_uri (demux->downloader,
|
2014-11-21 23:42:09 +00:00
|
|
|
demux->manifest_uri, NULL, TRUE, TRUE, TRUE, NULL);
|
|
|
|
if (download) {
|
|
|
|
g_free (demux->manifest_uri);
|
|
|
|
g_free (demux->manifest_base_uri);
|
|
|
|
if (download->redirect_permanent && download->redirect_uri) {
|
|
|
|
demux->manifest_uri = g_strdup (download->redirect_uri);
|
|
|
|
demux->manifest_base_uri = NULL;
|
|
|
|
} else {
|
|
|
|
demux->manifest_uri = g_strdup (download->uri);
|
|
|
|
demux->manifest_base_uri = g_strdup (download->redirect_uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = gst_fragment_get_buffer (download);
|
|
|
|
g_object_unref (download);
|
2015-04-23 15:22:11 +00:00
|
|
|
ret = klass->update_manifest_data (demux, buffer);
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
/* FIXME: Should the manifest uri vars be reverted to original
|
|
|
|
* values if updating fails? */
|
|
|
|
} else {
|
|
|
|
ret = GST_FLOW_NOT_LINKED;
|
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-04-23 15:22:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-11-21 23:42:09 +00:00
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2015-04-23 15:22:11 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adaptive_demux_update_manifest (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
|
|
|
ret = klass->update_manifest (demux);
|
|
|
|
|
|
|
|
if (ret == GST_FLOW_OK) {
|
|
|
|
GstClockTime duration;
|
|
|
|
/* Send an updated duration message */
|
|
|
|
duration = klass->get_duration (demux);
|
|
|
|
if (duration != GST_CLOCK_TIME_NONE) {
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Sending duration message : %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (duration));
|
|
|
|
gst_element_post_message (GST_ELEMENT (demux),
|
|
|
|
gst_message_new_duration_changed (GST_OBJECT (demux)));
|
2014-11-21 23:42:09 +00:00
|
|
|
} else {
|
2015-04-23 15:22:11 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Duration unknown, can not send the duration message");
|
2014-11-21 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_adaptive_demux_stream_fragment_clear (GstAdaptiveDemuxStreamFragment * f)
|
|
|
|
{
|
|
|
|
g_free (f->uri);
|
|
|
|
f->uri = NULL;
|
|
|
|
f->range_start = 0;
|
|
|
|
f->range_end = -1;
|
|
|
|
|
|
|
|
g_free (f->header_uri);
|
|
|
|
f->header_uri = NULL;
|
|
|
|
f->header_range_start = 0;
|
|
|
|
f->header_range_end = -1;
|
|
|
|
|
|
|
|
g_free (f->index_uri);
|
|
|
|
f->index_uri = NULL;
|
|
|
|
f->index_range_start = 0;
|
|
|
|
f->index_range_end = -1;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adaptive_demux_has_next_period (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
if (klass->has_next_period)
|
|
|
|
ret = klass->has_next_period (demux);
|
|
|
|
GST_DEBUG_OBJECT (demux, "Has next period: %d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:46:29 +00:00
|
|
|
/* must be called with manifest_lock taken */
|
2014-11-21 23:42:09 +00:00
|
|
|
static void
|
|
|
|
gst_adaptive_demux_advance_period (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
|
|
|
|
|
|
|
|
g_return_if_fail (klass->advance_period != NULL);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Advancing to next period");
|
|
|
|
klass->advance_period (demux);
|
2015-09-15 19:50:19 +00:00
|
|
|
gst_adaptive_demux_expose_streams (demux, FALSE);
|
2014-11-21 23:42:09 +00:00
|
|
|
gst_adaptive_demux_start_tasks (demux);
|
|
|
|
}
|