2011-02-14 17:51:32 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 Marc-Andre Lureau <marcandre.lureau@gmail.com>
|
|
|
|
* Copyright (C) 2010 Andoni Morales Alastruey <ylatuya@gmail.com>
|
2011-09-08 22:13:19 +00:00
|
|
|
* Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
|
|
|
|
* Author: Youness Alaoui <youness.alaoui@collabora.co.uk>, Collabora Ltd.
|
|
|
|
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
|
2011-02-14 17:51:32 +00:00
|
|
|
*
|
|
|
|
* Gsthlsdemux.c:
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-02-14 17:51:32 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* SECTION:element-hlsdemux
|
|
|
|
*
|
2011-03-30 01:34:39 +00:00
|
|
|
* HTTP Live Streaming demuxer element.
|
2011-02-14 17:51:32 +00:00
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
* |[
|
2012-09-14 14:45:34 +00:00
|
|
|
* gst-launch souphttpsrc location=http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8 ! hlsdemux ! decodebin2 ! videoconvert ! videoscale ! autovideosink
|
2011-02-14 17:51:32 +00:00
|
|
|
* ]|
|
|
|
|
* </refsect2>
|
|
|
|
*
|
|
|
|
* Last reviewed on 2010-10-07
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-27 14:49:58 +00:00
|
|
|
/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
|
|
|
|
* with newer GLib versions (>= 2.31.0) */
|
|
|
|
#define GLIB_DISABLE_DEPRECATION_WARNINGS
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2012-01-27 14:49:58 +00:00
|
|
|
#include <gst/glib-compat-private.h>
|
2011-02-14 17:51:32 +00:00
|
|
|
#include "gsthlsdemux.h"
|
|
|
|
|
2011-11-04 15:34:11 +00:00
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_PAD_SRC,
|
2011-07-26 15:02:05 +00:00
|
|
|
GST_PAD_SOMETIMES,
|
2011-02-15 20:49:20 +00:00
|
|
|
GST_STATIC_CAPS_ANY);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-03-30 14:53:12 +00:00
|
|
|
GST_STATIC_CAPS ("application/x-hls"));
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_hls_demux_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_hls_demux_debug
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
|
|
|
|
PROP_FRAGMENTS_CACHE,
|
2012-05-08 17:04:35 +00:00
|
|
|
PROP_BITRATE_LIMIT,
|
2012-05-10 14:11:18 +00:00
|
|
|
PROP_CONNECTION_SPEED,
|
2011-02-14 17:51:32 +00:00
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
static const float update_interval_factor[] = { 1, 0.5, 1.5, 3 };
|
|
|
|
|
|
|
|
#define DEFAULT_FRAGMENTS_CACHE 3
|
|
|
|
#define DEFAULT_FAILED_COUNT 3
|
2012-05-08 17:04:35 +00:00
|
|
|
#define DEFAULT_BITRATE_LIMIT 0.8
|
2012-05-10 14:11:18 +00:00
|
|
|
#define DEFAULT_CONNECTION_SPEED 0
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
/* GObject */
|
|
|
|
static void gst_hls_demux_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_hls_demux_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_hls_demux_dispose (GObject * obj);
|
|
|
|
|
|
|
|
/* GstElement */
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_hls_demux_change_state (GstElement * element, GstStateChange transition);
|
|
|
|
|
|
|
|
/* GstHLSDemux */
|
2012-04-12 12:17:14 +00:00
|
|
|
static GstFlowReturn gst_hls_demux_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buf);
|
|
|
|
static gboolean gst_hls_demux_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static gboolean gst_hls_demux_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static gboolean gst_hls_demux_src_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
2012-03-15 22:21:58 +00:00
|
|
|
static void gst_hls_demux_stream_loop (GstHLSDemux * demux);
|
|
|
|
static void gst_hls_demux_updates_loop (GstHLSDemux * demux);
|
2011-02-14 17:51:32 +00:00
|
|
|
static void gst_hls_demux_stop (GstHLSDemux * demux);
|
2012-10-15 08:38:16 +00:00
|
|
|
static void gst_hls_demux_pause_tasks (GstHLSDemux * demux, gboolean caching);
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean gst_hls_demux_cache_fragments (GstHLSDemux * demux);
|
|
|
|
static gboolean gst_hls_demux_schedule (GstHLSDemux * demux);
|
|
|
|
static gboolean gst_hls_demux_switch_playlist (GstHLSDemux * demux);
|
2012-03-15 18:42:44 +00:00
|
|
|
static gboolean gst_hls_demux_get_next_fragment (GstHLSDemux * demux,
|
|
|
|
gboolean caching);
|
2012-05-08 15:34:47 +00:00
|
|
|
static gboolean gst_hls_demux_update_playlist (GstHLSDemux * demux,
|
|
|
|
gboolean update);
|
2011-02-15 20:55:26 +00:00
|
|
|
static void gst_hls_demux_reset (GstHLSDemux * demux, gboolean dispose);
|
2011-03-30 01:34:39 +00:00
|
|
|
static gboolean gst_hls_demux_set_location (GstHLSDemux * demux,
|
|
|
|
const gchar * uri);
|
2012-03-15 18:42:44 +00:00
|
|
|
static gchar *gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
#define gst_hls_demux_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ELEMENT);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_dispose (GObject * obj)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (obj);
|
|
|
|
|
2012-03-15 22:21:58 +00:00
|
|
|
if (demux->stream_task) {
|
|
|
|
if (GST_TASK_STATE (demux->stream_task) != GST_TASK_STOPPED) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Leaving streaming task");
|
|
|
|
gst_task_stop (demux->stream_task);
|
2012-10-15 07:47:35 +00:00
|
|
|
g_rec_mutex_lock (&demux->stream_lock);
|
|
|
|
g_rec_mutex_unlock (&demux->stream_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_join (demux->stream_task);
|
|
|
|
}
|
|
|
|
gst_object_unref (demux->stream_task);
|
2012-04-12 12:17:14 +00:00
|
|
|
g_rec_mutex_clear (&demux->stream_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
demux->stream_task = NULL;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-03-15 22:21:58 +00:00
|
|
|
if (demux->updates_task) {
|
|
|
|
if (GST_TASK_STATE (demux->updates_task) != GST_TASK_STOPPED) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Leaving updates task");
|
2012-10-15 09:45:04 +00:00
|
|
|
demux->cancelled = TRUE;
|
2012-10-15 08:53:58 +00:00
|
|
|
gst_uri_downloader_cancel (demux->downloader);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_stop (demux->updates_task);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_lock (&demux->updates_timed_lock);
|
2012-10-15 07:24:01 +00:00
|
|
|
GST_TASK_SIGNAL (demux->updates_task);
|
2012-10-15 07:47:35 +00:00
|
|
|
g_rec_mutex_lock (&demux->updates_lock);
|
|
|
|
g_rec_mutex_unlock (&demux->updates_lock);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_unlock (&demux->updates_timed_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_join (demux->updates_task);
|
|
|
|
}
|
|
|
|
gst_object_unref (demux->updates_task);
|
2012-04-12 12:17:14 +00:00
|
|
|
g_mutex_clear (&demux->updates_timed_lock);
|
|
|
|
g_rec_mutex_clear (&demux->updates_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
demux->updates_task = NULL;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-03-15 18:42:44 +00:00
|
|
|
if (demux->downloader != NULL) {
|
|
|
|
g_object_unref (demux->downloader);
|
|
|
|
demux->downloader = NULL;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-02-15 20:55:26 +00:00
|
|
|
gst_hls_demux_reset (demux, TRUE);
|
|
|
|
|
2012-01-12 15:58:36 +00:00
|
|
|
g_queue_free (demux->queue);
|
2011-04-01 23:08:02 +00:00
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_class_init (GstHLSDemuxClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-04-12 12:17:14 +00:00
|
|
|
GstElementClass *element_class;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-03-30 08:11:24 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2012-04-12 12:17:14 +00:00
|
|
|
element_class = (GstElementClass *) klass;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_hls_demux_set_property;
|
|
|
|
gobject_class->get_property = gst_hls_demux_get_property;
|
|
|
|
gobject_class->dispose = gst_hls_demux_dispose;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_FRAGMENTS_CACHE,
|
|
|
|
g_param_spec_uint ("fragments-cache", "Fragments cache",
|
|
|
|
"Number of fragments needed to be cached to start playing",
|
2011-03-30 08:11:24 +00:00
|
|
|
2, G_MAXUINT, DEFAULT_FRAGMENTS_CACHE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
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,
|
2011-03-30 08:11:24 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-05-10 14:11:18 +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 = unknown)",
|
|
|
|
0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&srctemplate));
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sinktemplate));
|
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2012-04-12 12:17:14 +00:00
|
|
|
"HLS Demuxer",
|
|
|
|
"Demuxer/URIList",
|
|
|
|
"HTTP Live Streaming demuxer",
|
|
|
|
"Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
|
|
|
|
"Andoni Morales Alastruey <ylatuya@gmail.com>");
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
|
|
|
|
"hlsdemux element");
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_hls_demux_init (GstHLSDemux * demux)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
|
|
|
/* sink pad */
|
|
|
|
demux->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
|
|
|
|
gst_pad_set_chain_function (demux->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_hls_demux_chain));
|
|
|
|
gst_pad_set_event_function (demux->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_hls_demux_sink_event));
|
|
|
|
gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
|
|
|
|
|
2012-03-15 18:42:44 +00:00
|
|
|
/* Downloader */
|
|
|
|
demux->downloader = gst_uri_downloader_new ();
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
demux->do_typefind = TRUE;
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
/* Properties */
|
|
|
|
demux->fragments_cache = DEFAULT_FRAGMENTS_CACHE;
|
2012-05-08 17:04:35 +00:00
|
|
|
demux->bitrate_limit = DEFAULT_BITRATE_LIMIT;
|
2012-05-10 14:11:18 +00:00
|
|
|
demux->connection_speed = DEFAULT_CONNECTION_SPEED;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
demux->queue = g_queue_new ();
|
2012-03-15 22:21:58 +00:00
|
|
|
|
|
|
|
/* Updates task */
|
2012-04-12 12:17:14 +00:00
|
|
|
g_rec_mutex_init (&demux->updates_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
demux->updates_task =
|
2012-06-20 08:34:48 +00:00
|
|
|
gst_task_new ((GstTaskFunction) gst_hls_demux_updates_loop, demux, NULL);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_set_lock (demux->updates_task, &demux->updates_lock);
|
2012-04-12 12:17:14 +00:00
|
|
|
g_mutex_init (&demux->updates_timed_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
|
|
|
|
/* Streaming task */
|
2012-04-12 12:17:14 +00:00
|
|
|
g_rec_mutex_init (&demux->stream_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
demux->stream_task =
|
2012-06-20 08:34:48 +00:00
|
|
|
gst_task_new ((GstTaskFunction) gst_hls_demux_stream_loop, demux, NULL);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_set_lock (demux->stream_task, &demux->stream_lock);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-30 01:34:39 +00:00
|
|
|
gst_hls_demux_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_FRAGMENTS_CACHE:
|
|
|
|
demux->fragments_cache = g_value_get_uint (value);
|
|
|
|
break;
|
2012-05-08 17:04:35 +00:00
|
|
|
case PROP_BITRATE_LIMIT:
|
|
|
|
demux->bitrate_limit = g_value_get_float (value);
|
2011-02-14 17:51:32 +00:00
|
|
|
break;
|
2012-05-10 14:11:18 +00:00
|
|
|
case PROP_CONNECTION_SPEED:
|
|
|
|
demux->connection_speed = g_value_get_uint (value) * 1000;
|
|
|
|
break;
|
2011-02-14 17:51:32 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_FRAGMENTS_CACHE:
|
|
|
|
g_value_set_uint (value, demux->fragments_cache);
|
|
|
|
break;
|
2012-05-08 17:04:35 +00:00
|
|
|
case PROP_BITRATE_LIMIT:
|
|
|
|
g_value_set_float (value, demux->bitrate_limit);
|
2011-02-14 17:51:32 +00:00
|
|
|
break;
|
2012-05-10 14:11:18 +00:00
|
|
|
case PROP_CONNECTION_SPEED:
|
|
|
|
g_value_set_uint (value, demux->connection_speed / 1000);
|
|
|
|
break;
|
2011-02-14 17:51:32 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
2011-09-02 12:55:45 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
2011-02-15 20:55:26 +00:00
|
|
|
gst_hls_demux_reset (demux, FALSE);
|
2011-02-14 17:51:32 +00:00
|
|
|
break;
|
2011-08-31 03:07:48 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
/* Start the streaming loop in paused only if we already received
|
|
|
|
the main playlist. It might have been stopped if we were in PAUSED
|
|
|
|
state and we filled our queue with enough cached fragments
|
|
|
|
*/
|
|
|
|
if (gst_m3u8_client_get_uri (demux->client)[0] != '\0')
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_start (demux->updates_task);
|
2011-08-31 03:07:48 +00:00
|
|
|
break;
|
2011-02-14 17:51:32 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
2011-02-15 02:41:43 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
2011-08-31 03:07:48 +00:00
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
2012-10-15 09:45:04 +00:00
|
|
|
demux->cancelled = TRUE;
|
2012-10-15 08:53:58 +00:00
|
|
|
gst_uri_downloader_cancel (demux->downloader);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_stop (demux->updates_task);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_lock (&demux->updates_timed_lock);
|
2012-10-15 07:24:01 +00:00
|
|
|
GST_TASK_SIGNAL (demux->updates_task);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_unlock (&demux->updates_timed_lock);
|
2012-10-15 07:47:35 +00:00
|
|
|
g_rec_mutex_lock (&demux->updates_lock);
|
|
|
|
g_rec_mutex_unlock (&demux->updates_lock);
|
2012-10-15 09:45:04 +00:00
|
|
|
demux->cancelled = FALSE;
|
2011-08-31 03:07:48 +00:00
|
|
|
break;
|
2011-02-15 02:41:43 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2011-02-15 21:40:21 +00:00
|
|
|
demux->cancelled = TRUE;
|
2011-08-23 20:49:33 +00:00
|
|
|
gst_hls_demux_stop (demux);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_join (demux->stream_task);
|
2011-09-02 17:44:31 +00:00
|
|
|
gst_hls_demux_reset (demux, FALSE);
|
2011-02-15 02:41:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-13 21:06:18 +00:00
|
|
|
static gboolean
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_hls_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2011-04-13 21:06:18 +00:00
|
|
|
{
|
2011-08-16 19:46:49 +00:00
|
|
|
GstHLSDemux *demux;
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
demux = GST_HLS_DEMUX (parent);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2011-04-13 21:06:18 +00:00
|
|
|
switch (event->type) {
|
|
|
|
case GST_EVENT_SEEK:
|
2011-08-16 19:46:49 +00:00
|
|
|
{
|
|
|
|
gdouble rate;
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
|
|
|
GList *walk;
|
2012-05-08 15:09:28 +00:00
|
|
|
GstClockTime position, current_pos, target_pos;
|
2011-08-16 19:46:49 +00:00
|
|
|
gint current_sequence;
|
|
|
|
GstM3U8MediaFile *file;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "Received GST_EVENT_SEEK");
|
2011-08-22 23:41:39 +00:00
|
|
|
|
|
|
|
if (gst_m3u8_client_is_live (demux->client)) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Received seek event for live stream");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-08-16 19:46:49 +00:00
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
|
|
|
|
&stop_type, &stop);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT
|
|
|
|
" stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
|
|
|
|
GST_TIME_ARGS (stop));
|
|
|
|
|
2011-09-02 23:49:38 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
2011-08-16 19:46:49 +00:00
|
|
|
file = GST_M3U8_MEDIA_FILE (demux->client->current->files->data);
|
|
|
|
current_sequence = file->sequence;
|
|
|
|
current_pos = 0;
|
2012-02-28 15:40:31 +00:00
|
|
|
target_pos = (GstClockTime) start;
|
2011-08-16 19:46:49 +00:00
|
|
|
for (walk = demux->client->current->files; walk; walk = walk->next) {
|
|
|
|
file = walk->data;
|
|
|
|
|
|
|
|
current_sequence = file->sequence;
|
2012-02-28 15:40:31 +00:00
|
|
|
if (current_pos <= target_pos
|
|
|
|
&& target_pos < current_pos + file->duration) {
|
2011-08-16 19:46:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_pos += file->duration;
|
|
|
|
}
|
2011-09-02 23:49:38 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
|
|
|
if (walk == NULL) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Could not find seeked fragment");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & GST_SEEK_FLAG_FLUSH) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "sending flush start");
|
|
|
|
gst_pad_push_event (demux->srcpad, gst_event_new_flush_start ());
|
|
|
|
}
|
|
|
|
|
2011-08-23 20:49:33 +00:00
|
|
|
demux->cancelled = TRUE;
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_pause (demux->stream_task);
|
2012-03-15 18:42:44 +00:00
|
|
|
gst_uri_downloader_cancel (demux->downloader);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_stop (demux->updates_task);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_lock (&demux->updates_timed_lock);
|
2012-10-15 07:24:01 +00:00
|
|
|
GST_TASK_SIGNAL (demux->updates_task);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_unlock (&demux->updates_timed_lock);
|
2012-10-15 07:47:35 +00:00
|
|
|
g_rec_mutex_lock (&demux->updates_lock);
|
|
|
|
g_rec_mutex_unlock (&demux->updates_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_pause (demux->stream_task);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
|
|
|
/* wait for streaming to finish */
|
2012-04-12 12:17:14 +00:00
|
|
|
g_rec_mutex_lock (&demux->stream_lock);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
|
|
|
demux->need_cache = TRUE;
|
|
|
|
while (!g_queue_is_empty (demux->queue)) {
|
2012-04-12 12:17:14 +00:00
|
|
|
GstFragment *fragment = g_queue_pop_head (demux->queue);
|
|
|
|
g_object_unref (fragment);
|
2011-08-16 19:46:49 +00:00
|
|
|
}
|
2011-08-26 22:47:35 +00:00
|
|
|
g_queue_clear (demux->queue);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2011-09-02 23:49:38 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
2011-08-16 19:46:49 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "seeking to sequence %d", current_sequence);
|
|
|
|
demux->client->sequence = current_sequence;
|
2012-05-08 15:09:28 +00:00
|
|
|
gst_m3u8_client_get_current_position (demux->client, &position);
|
|
|
|
demux->position_shift = start - position;
|
2011-08-16 19:46:49 +00:00
|
|
|
demux->need_segment = TRUE;
|
2011-09-02 23:49:38 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
|
|
|
|
2011-08-16 19:46:49 +00:00
|
|
|
|
|
|
|
if (flags & GST_SEEK_FLAG_FLUSH) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "sending flush stop");
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_pad_push_event (demux->srcpad, gst_event_new_flush_stop (TRUE));
|
2011-08-16 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
2011-08-23 20:49:33 +00:00
|
|
|
demux->cancelled = FALSE;
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_start (demux->stream_task);
|
2012-04-12 12:17:14 +00:00
|
|
|
g_rec_mutex_unlock (&demux->stream_lock);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-04-13 21:06:18 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
return gst_pad_event_default (pad, parent, event);
|
2011-04-13 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_hls_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2012-04-12 12:17:14 +00:00
|
|
|
GstHLSDemux *demux;
|
2011-02-14 17:51:32 +00:00
|
|
|
GstQuery *query;
|
2011-02-15 01:13:56 +00:00
|
|
|
gboolean ret;
|
2011-02-14 17:51:32 +00:00
|
|
|
gchar *uri;
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
demux = GST_HLS_DEMUX (parent);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case GST_EVENT_EOS:{
|
2012-04-12 12:17:14 +00:00
|
|
|
gchar *playlist = NULL;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
if (demux->playlist == NULL) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Received EOS without a playlist.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-30 01:34:39 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Got EOS on the sink pad: main playlist fetched");
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-02-15 01:13:56 +00:00
|
|
|
query = gst_query_new_uri ();
|
|
|
|
ret = gst_pad_peer_query (demux->sinkpad, query);
|
|
|
|
if (ret) {
|
|
|
|
gst_query_parse_uri (query, &uri);
|
|
|
|
gst_hls_demux_set_location (demux, uri);
|
|
|
|
g_free (uri);
|
|
|
|
}
|
2011-03-29 21:18:24 +00:00
|
|
|
gst_query_unref (query);
|
2011-02-15 01:13:56 +00:00
|
|
|
|
2012-03-15 18:42:44 +00:00
|
|
|
playlist = gst_hls_src_buf_to_utf8_playlist (demux->playlist);
|
2011-09-08 20:29:42 +00:00
|
|
|
demux->playlist = NULL;
|
2011-04-01 23:21:34 +00:00
|
|
|
if (playlist == NULL) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Error validating first playlist.");
|
|
|
|
} else if (!gst_m3u8_client_update (demux->client, playlist)) {
|
2011-02-15 03:39:34 +00:00
|
|
|
/* In most cases, this will happen if we set a wrong url in the
|
2011-02-15 02:42:29 +00:00
|
|
|
* source element and we have received the 404 HTML response instead of
|
|
|
|
* the playlist */
|
2011-07-27 10:02:41 +00:00
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."),
|
|
|
|
(NULL));
|
2011-02-15 02:42:29 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-02-15 01:13:56 +00:00
|
|
|
if (!ret && gst_m3u8_client_is_live (demux->client)) {
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
|
|
|
|
("Failed querying the playlist uri, "
|
2011-07-27 10:02:41 +00:00
|
|
|
"required for live sources."), (NULL));
|
2011-02-14 17:51:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_start (demux->stream_task);
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-04-12 12:17:14 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2011-08-01 16:48:03 +00:00
|
|
|
/* Swallow newsegments, we'll push our own */
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
2011-02-14 17:51:32 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
return gst_pad_event_default (pad, parent, event);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 11:50:25 +00:00
|
|
|
static gboolean
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_hls_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
2011-03-12 11:50:25 +00:00
|
|
|
{
|
|
|
|
GstHLSDemux *hlsdemux;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
if (query == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
hlsdemux = GST_HLS_DEMUX (parent);
|
2011-03-12 11:50:25 +00:00
|
|
|
|
|
|
|
switch (query->type) {
|
|
|
|
case GST_QUERY_DURATION:{
|
2011-08-23 01:54:03 +00:00
|
|
|
GstClockTime duration = -1;
|
2011-05-03 11:01:25 +00:00
|
|
|
GstFormat fmt;
|
2011-03-12 11:50:25 +00:00
|
|
|
|
2011-05-03 11:01:25 +00:00
|
|
|
gst_query_parse_duration (query, &fmt, NULL);
|
|
|
|
if (fmt == GST_FORMAT_TIME) {
|
|
|
|
duration = gst_m3u8_client_get_duration (hlsdemux->client);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
|
|
|
|
gst_query_set_duration (query, GST_FORMAT_TIME, duration);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2011-03-12 11:50:25 +00:00
|
|
|
}
|
2011-08-23 01:54:03 +00:00
|
|
|
GST_INFO_OBJECT (hlsdemux, "GST_QUERY_DURATION returns %s with duration %"
|
|
|
|
GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
|
2011-03-12 11:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-03-12 12:00:06 +00:00
|
|
|
case GST_QUERY_URI:
|
|
|
|
if (hlsdemux->client) {
|
|
|
|
/* FIXME: Do we answer with the variant playlist, with the current
|
|
|
|
* playlist or the the uri of the least downlowaded fragment? */
|
2011-09-02 23:49:38 +00:00
|
|
|
gst_query_set_uri (query, gst_m3u8_client_get_uri (hlsdemux->client));
|
2011-03-12 12:00:06 +00:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2011-05-03 10:51:44 +00:00
|
|
|
case GST_QUERY_SEEKING:{
|
|
|
|
GstFormat fmt;
|
2011-08-23 01:54:26 +00:00
|
|
|
gint64 stop = -1;
|
2011-05-03 10:51:44 +00:00
|
|
|
|
|
|
|
gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
|
2011-08-23 01:54:26 +00:00
|
|
|
GST_INFO_OBJECT (hlsdemux, "Received GST_QUERY_SEEKING with format %d",
|
|
|
|
fmt);
|
2011-05-03 10:51:44 +00:00
|
|
|
if (fmt == GST_FORMAT_TIME) {
|
|
|
|
GstClockTime duration;
|
|
|
|
|
|
|
|
duration = gst_m3u8_client_get_duration (hlsdemux->client);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
|
|
|
|
stop = duration;
|
2011-08-23 01:54:26 +00:00
|
|
|
|
|
|
|
gst_query_set_seeking (query, fmt,
|
|
|
|
!gst_m3u8_client_is_live (hlsdemux->client), 0, stop);
|
|
|
|
ret = TRUE;
|
|
|
|
GST_INFO_OBJECT (hlsdemux, "GST_QUERY_SEEKING returning with stop : %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (stop));
|
2011-05-03 10:51:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-03-12 11:50:25 +00:00
|
|
|
default:
|
|
|
|
/* Don't fordward queries upstream because of the special nature of this
|
2011-05-03 10:51:44 +00:00
|
|
|
* "demuxer", which relies on the upstream element only to be fed with the
|
2011-03-12 11:50:25 +00:00
|
|
|
* first playlist */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static GstFlowReturn
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_hls_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2012-04-12 12:17:14 +00:00
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (parent);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-02-16 00:19:45 +00:00
|
|
|
if (demux->playlist == NULL)
|
2011-02-14 17:51:32 +00:00
|
|
|
demux->playlist = buf;
|
2011-02-16 00:19:45 +00:00
|
|
|
else
|
2012-03-28 10:49:54 +00:00
|
|
|
demux->playlist = gst_buffer_append (demux->playlist, buf);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-15 07:35:16 +00:00
|
|
|
static void
|
2012-10-15 08:38:16 +00:00
|
|
|
gst_hls_demux_pause_tasks (GstHLSDemux * demux, gboolean caching)
|
2012-10-15 07:35:16 +00:00
|
|
|
{
|
|
|
|
if (GST_TASK_STATE (demux->updates_task) != GST_TASK_STOPPED) {
|
2012-10-15 09:45:04 +00:00
|
|
|
demux->cancelled = TRUE;
|
|
|
|
gst_uri_downloader_cancel (demux->downloader);
|
2012-10-15 07:35:16 +00:00
|
|
|
gst_task_pause (demux->updates_task);
|
2012-10-15 08:38:16 +00:00
|
|
|
if (!caching)
|
|
|
|
g_mutex_lock (&demux->updates_timed_lock);
|
2012-10-15 07:35:16 +00:00
|
|
|
GST_TASK_SIGNAL (demux->updates_task);
|
2012-10-15 08:38:16 +00:00
|
|
|
if (!caching)
|
|
|
|
g_mutex_unlock (&demux->updates_timed_lock);
|
2012-10-15 07:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_TASK_STATE (demux->stream_task) != GST_TASK_STOPPED) {
|
2012-10-15 09:45:04 +00:00
|
|
|
demux->stop_stream_task = TRUE;
|
2012-10-15 07:35:16 +00:00
|
|
|
gst_task_pause (demux->stream_task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static void
|
|
|
|
gst_hls_demux_stop (GstHLSDemux * demux)
|
|
|
|
{
|
2012-03-15 18:42:44 +00:00
|
|
|
gst_uri_downloader_cancel (demux->downloader);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-03-15 22:21:58 +00:00
|
|
|
if (GST_TASK_STATE (demux->updates_task) != GST_TASK_STOPPED) {
|
2012-10-15 09:45:04 +00:00
|
|
|
demux->cancelled = TRUE;
|
2012-10-15 08:53:58 +00:00
|
|
|
gst_uri_downloader_cancel (demux->downloader);
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_stop (demux->updates_task);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_lock (&demux->updates_timed_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
GST_TASK_SIGNAL (demux->updates_task);
|
2012-10-15 08:32:47 +00:00
|
|
|
g_mutex_unlock (&demux->updates_timed_lock);
|
2012-10-15 07:47:35 +00:00
|
|
|
g_rec_mutex_lock (&demux->updates_lock);
|
|
|
|
g_rec_mutex_unlock (&demux->updates_lock);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
2011-02-15 03:39:34 +00:00
|
|
|
|
2012-10-15 07:24:01 +00:00
|
|
|
if (GST_TASK_STATE (demux->stream_task) != GST_TASK_STOPPED) {
|
2012-10-15 09:45:04 +00:00
|
|
|
demux->stop_stream_task = TRUE;
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_stop (demux->stream_task);
|
2012-10-15 07:47:35 +00:00
|
|
|
g_rec_mutex_lock (&demux->stream_lock);
|
|
|
|
g_rec_mutex_unlock (&demux->stream_lock);
|
2012-10-15 07:24:01 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
static void
|
|
|
|
switch_pads (GstHLSDemux * demux, GstCaps * newcaps)
|
|
|
|
{
|
|
|
|
GstPad *oldpad = demux->srcpad;
|
2012-08-06 11:59:27 +00:00
|
|
|
gchar *stream_id;
|
2011-07-26 15:02:05 +00:00
|
|
|
|
2012-03-15 18:42:44 +00:00
|
|
|
GST_DEBUG ("Switching pads (oldpad:%p) with caps: %" GST_PTR_FORMAT, oldpad,
|
|
|
|
newcaps);
|
2011-07-26 15:02:05 +00:00
|
|
|
|
|
|
|
/* First create and activate new pad */
|
|
|
|
demux->srcpad = gst_pad_new_from_static_template (&srctemplate, NULL);
|
|
|
|
gst_pad_set_event_function (demux->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_hls_demux_src_event));
|
|
|
|
gst_pad_set_query_function (demux->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_hls_demux_src_query));
|
|
|
|
gst_pad_set_element_private (demux->srcpad, demux);
|
|
|
|
gst_pad_set_active (demux->srcpad, TRUE);
|
2012-08-06 11:59:27 +00:00
|
|
|
|
|
|
|
stream_id =
|
|
|
|
gst_pad_create_stream_id (demux->srcpad, GST_ELEMENT_CAST (demux), NULL);
|
|
|
|
gst_pad_push_event (demux->srcpad, gst_event_new_stream_start (stream_id));
|
|
|
|
g_free (stream_id);
|
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
gst_pad_set_caps (demux->srcpad, newcaps);
|
2012-08-06 11:59:27 +00:00
|
|
|
|
2011-08-02 19:21:18 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (demux), demux->srcpad);
|
2011-07-26 15:02:05 +00:00
|
|
|
|
|
|
|
gst_element_no_more_pads (GST_ELEMENT (demux));
|
|
|
|
|
|
|
|
if (oldpad) {
|
|
|
|
/* Push out EOS */
|
|
|
|
gst_pad_push_event (oldpad, gst_event_new_eos ());
|
|
|
|
gst_pad_set_active (oldpad, FALSE);
|
|
|
|
gst_element_remove_pad (GST_ELEMENT (demux), oldpad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static void
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_hls_demux_stream_loop (GstHLSDemux * demux)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2012-04-12 12:17:14 +00:00
|
|
|
GstFragment *fragment;
|
2011-02-14 17:51:32 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
GstFlowReturn ret;
|
2012-04-12 12:17:14 +00:00
|
|
|
GstCaps *bufcaps, *srccaps = NULL;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-02-15 03:39:34 +00:00
|
|
|
/* Loop for the source pad task. The task is started when we have
|
|
|
|
* received the main playlist from the source element. It tries first to
|
|
|
|
* cache the first fragments and then it waits until it has more data in the
|
|
|
|
* queue. This task is woken up when we push a new fragment to the queue or
|
|
|
|
* when we reached the end of the playlist */
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
if (G_UNLIKELY (demux->need_cache)) {
|
2011-02-15 23:55:30 +00:00
|
|
|
if (!gst_hls_demux_cache_fragments (demux))
|
2011-02-14 17:51:32 +00:00
|
|
|
goto cache_error;
|
2011-02-15 23:55:30 +00:00
|
|
|
|
2011-08-31 03:07:48 +00:00
|
|
|
/* we can start now the updates thread (only if on playing) */
|
|
|
|
if (GST_STATE (demux) == GST_STATE_PLAYING)
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_start (demux->updates_task);
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_INFO_OBJECT (demux, "First fragments cached successfully");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_queue_is_empty (demux->queue)) {
|
2011-04-13 21:35:50 +00:00
|
|
|
if (demux->end_of_playlist)
|
2011-02-14 17:51:32 +00:00
|
|
|
goto end_of_playlist;
|
|
|
|
|
2011-08-31 03:07:48 +00:00
|
|
|
goto pause_task;
|
2011-03-29 21:06:14 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
fragment = g_queue_pop_head (demux->queue);
|
|
|
|
buf = gst_fragment_get_buffer (fragment);
|
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
/* Figure out if we need to create/switch pads */
|
2012-04-12 12:17:14 +00:00
|
|
|
if (G_LIKELY (demux->srcpad))
|
|
|
|
srccaps = gst_pad_get_current_caps (demux->srcpad);
|
|
|
|
bufcaps = gst_fragment_get_caps (fragment);
|
|
|
|
if (G_UNLIKELY (!srccaps || !gst_caps_is_equal_fixed (bufcaps, srccaps)
|
2011-08-16 19:53:31 +00:00
|
|
|
|| demux->need_segment)) {
|
2012-04-12 12:17:14 +00:00
|
|
|
switch_pads (demux, bufcaps);
|
2011-08-16 19:53:31 +00:00
|
|
|
demux->need_segment = TRUE;
|
|
|
|
}
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_caps_unref (bufcaps);
|
|
|
|
if (G_LIKELY (srccaps))
|
|
|
|
gst_caps_unref (srccaps);
|
|
|
|
g_object_unref (fragment);
|
|
|
|
|
2011-08-16 19:53:31 +00:00
|
|
|
if (demux->need_segment) {
|
2012-04-12 12:17:14 +00:00
|
|
|
GstSegment segment;
|
2012-05-08 15:09:28 +00:00
|
|
|
GstClockTime start = GST_BUFFER_PTS (buf);
|
|
|
|
|
|
|
|
start += demux->position_shift;
|
2011-08-01 16:48:03 +00:00
|
|
|
/* And send a newsegment */
|
2011-09-08 23:56:33 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Sending new-segment. segment start:%"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (start));
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_segment_init (&segment, GST_FORMAT_TIME);
|
|
|
|
segment.start = start;
|
|
|
|
segment.time = start;
|
|
|
|
gst_pad_push_event (demux->srcpad, gst_event_new_segment (&segment));
|
2011-08-16 19:53:31 +00:00
|
|
|
demux->need_segment = FALSE;
|
2011-09-08 23:56:33 +00:00
|
|
|
demux->position_shift = 0;
|
2011-07-26 15:02:05 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
ret = gst_pad_push (demux->srcpad, buf);
|
2011-02-14 17:51:32 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
2012-03-15 18:42:44 +00:00
|
|
|
goto error_pushing;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
end_of_playlist:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "Reached end of playlist, sending EOS");
|
|
|
|
gst_pad_push_event (demux->srcpad, gst_event_new_eos ());
|
2012-10-15 08:38:16 +00:00
|
|
|
gst_hls_demux_pause_tasks (demux, FALSE);
|
2011-02-14 17:51:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache_error:
|
|
|
|
{
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_pause (demux->stream_task);
|
2011-08-25 23:37:25 +00:00
|
|
|
if (!demux->cancelled) {
|
|
|
|
GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
|
|
|
|
("Could not cache the first fragments"), (NULL));
|
2012-10-15 08:38:16 +00:00
|
|
|
gst_hls_demux_pause_tasks (demux, FALSE);
|
2011-08-25 23:37:25 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-15 18:42:44 +00:00
|
|
|
error_pushing:
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2012-10-15 07:35:16 +00:00
|
|
|
if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL),
|
|
|
|
("stream stopped, reason %s", gst_flow_get_name (ret)));
|
|
|
|
gst_pad_push_event (demux->srcpad, gst_event_new_eos ());
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (demux, "stream stopped, reason %s",
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
}
|
2012-10-15 08:38:16 +00:00
|
|
|
gst_hls_demux_pause_tasks (demux, FALSE);
|
2011-02-14 17:51:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-08-16 19:41:08 +00:00
|
|
|
|
2011-08-31 03:07:48 +00:00
|
|
|
pause_task:
|
2011-08-16 19:41:08 +00:00
|
|
|
{
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_pause (demux->stream_task);
|
2011-08-16 19:41:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-15 20:55:26 +00:00
|
|
|
gst_hls_demux_reset (GstHLSDemux * demux, gboolean dispose)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
|
|
|
demux->need_cache = TRUE;
|
|
|
|
demux->end_of_playlist = FALSE;
|
2011-02-15 21:40:21 +00:00
|
|
|
demux->cancelled = FALSE;
|
2011-07-26 15:02:05 +00:00
|
|
|
demux->do_typefind = TRUE;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-02-15 20:49:20 +00:00
|
|
|
if (demux->input_caps) {
|
|
|
|
gst_caps_unref (demux->input_caps);
|
|
|
|
demux->input_caps = NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
if (demux->playlist) {
|
|
|
|
gst_buffer_unref (demux->playlist);
|
|
|
|
demux->playlist = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-02 12:54:56 +00:00
|
|
|
if (demux->client) {
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_m3u8_client_free (demux->client);
|
2011-09-02 12:54:56 +00:00
|
|
|
demux->client = NULL;
|
|
|
|
}
|
2011-02-15 20:55:26 +00:00
|
|
|
|
|
|
|
if (!dispose) {
|
|
|
|
demux->client = gst_m3u8_client_new ("");
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
while (!g_queue_is_empty (demux->queue)) {
|
2012-04-12 12:17:14 +00:00
|
|
|
GstFragment *fragment = g_queue_pop_head (demux->queue);
|
|
|
|
g_object_unref (fragment);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
g_queue_clear (demux->queue);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2011-09-08 23:56:33 +00:00
|
|
|
demux->position_shift = 0;
|
2011-08-16 19:46:49 +00:00
|
|
|
demux->need_segment = TRUE;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_set_location (GstHLSDemux * demux, const gchar * uri)
|
|
|
|
{
|
|
|
|
if (demux->client)
|
|
|
|
gst_m3u8_client_free (demux->client);
|
|
|
|
demux->client = gst_m3u8_client_new (uri);
|
|
|
|
GST_INFO_OBJECT (demux, "Changed location: %s", uri);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-15 22:21:58 +00:00
|
|
|
void
|
|
|
|
gst_hls_demux_updates_loop (GstHLSDemux * demux)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2011-02-15 03:39:34 +00:00
|
|
|
/* Loop for the updates. It's started when the first fragments are cached and
|
|
|
|
* schedules the next update of the playlist (for lives sources) and the next
|
|
|
|
* update of fragments. When a new fragment is downloaded, it compares the
|
|
|
|
* download time with the next scheduled update to check if we can or should
|
|
|
|
* switch to a different bitrate */
|
|
|
|
|
2012-03-15 22:21:58 +00:00
|
|
|
/* block until the next scheduled update or the signal to quit this thread */
|
2012-04-12 12:17:14 +00:00
|
|
|
g_mutex_lock (&demux->updates_timed_lock);
|
2012-03-15 22:21:58 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Started updates task");
|
2011-02-14 17:51:32 +00:00
|
|
|
while (TRUE) {
|
2012-10-15 09:45:04 +00:00
|
|
|
if (demux->cancelled)
|
|
|
|
goto quit;
|
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
/* schedule the next update */
|
|
|
|
gst_hls_demux_schedule (demux);
|
|
|
|
|
|
|
|
/* block until the next scheduled update or the signal to quit this thread */
|
2012-03-15 22:21:58 +00:00
|
|
|
if (g_cond_timed_wait (GST_TASK_GET_COND (demux->updates_task),
|
2012-04-12 12:17:14 +00:00
|
|
|
&demux->updates_timed_lock, &demux->next_update)) {
|
2011-02-14 17:51:32 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
2012-10-15 09:45:04 +00:00
|
|
|
|
|
|
|
if (demux->cancelled)
|
|
|
|
goto quit;
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
/* update the playlist for live sources */
|
|
|
|
if (gst_m3u8_client_is_live (demux->client)) {
|
2012-05-08 15:34:47 +00:00
|
|
|
if (!gst_hls_demux_update_playlist (demux, TRUE)) {
|
2012-10-15 09:45:04 +00:00
|
|
|
if (demux->cancelled)
|
|
|
|
goto quit;
|
2011-09-01 23:18:51 +00:00
|
|
|
demux->client->update_failed_count++;
|
|
|
|
if (demux->client->update_failed_count < DEFAULT_FAILED_COUNT) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Could not update the playlist");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
|
|
|
|
("Could not update the playlist"), (NULL));
|
2012-10-15 07:35:16 +00:00
|
|
|
goto error;
|
2011-09-01 23:18:51 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it's a live source and the playlist couldn't be updated, there aren't
|
2011-02-15 03:39:34 +00:00
|
|
|
* more fragments in the playlist, so we just wait for the next schedulled
|
2011-02-14 17:51:32 +00:00
|
|
|
* update */
|
|
|
|
if (gst_m3u8_client_is_live (demux->client) &&
|
|
|
|
demux->client->update_failed_count > 0) {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"The playlist hasn't been updated, failed count is %d",
|
|
|
|
demux->client->update_failed_count);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-10-15 09:45:04 +00:00
|
|
|
if (demux->cancelled)
|
|
|
|
goto quit;
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
/* fetch the next fragment */
|
2011-08-31 03:07:48 +00:00
|
|
|
if (g_queue_is_empty (demux->queue)) {
|
2012-03-15 18:42:44 +00:00
|
|
|
if (!gst_hls_demux_get_next_fragment (demux, FALSE)) {
|
2012-10-15 09:45:04 +00:00
|
|
|
if (demux->cancelled) {
|
|
|
|
goto quit;
|
|
|
|
} else if (!demux->end_of_playlist && !demux->cancelled) {
|
2011-09-01 23:18:51 +00:00
|
|
|
demux->client->update_failed_count++;
|
|
|
|
if (demux->client->update_failed_count < DEFAULT_FAILED_COUNT) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Could not fetch the next fragment");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
|
|
|
|
("Could not fetch the next fragment"), (NULL));
|
2012-10-15 07:35:16 +00:00
|
|
|
goto error;
|
2011-09-01 23:18:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
demux->client->update_failed_count = 0;
|
|
|
|
|
2012-10-15 09:45:04 +00:00
|
|
|
if (demux->cancelled)
|
|
|
|
goto quit;
|
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
/* try to switch to another bitrate if needed */
|
|
|
|
gst_hls_demux_switch_playlist (demux);
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quit:
|
|
|
|
{
|
2012-03-15 18:42:44 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Stopped updates task");
|
2012-10-15 07:35:16 +00:00
|
|
|
g_mutex_unlock (&demux->updates_timed_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "Stopped updates task because of error");
|
2012-10-15 08:38:16 +00:00
|
|
|
gst_hls_demux_pause_tasks (demux, TRUE);
|
2012-04-12 12:17:14 +00:00
|
|
|
g_mutex_unlock (&demux->updates_timed_lock);
|
2011-08-31 03:01:58 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_cache_fragments (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
2011-02-15 03:39:34 +00:00
|
|
|
/* If this playlist is a variant playlist, select the first one
|
2011-02-14 17:51:32 +00:00
|
|
|
* and update it */
|
|
|
|
if (gst_m3u8_client_has_variant_playlist (demux->client)) {
|
2011-09-02 23:49:38 +00:00
|
|
|
GstM3U8 *child = NULL;
|
|
|
|
|
2012-05-10 14:11:18 +00:00
|
|
|
if (demux->connection_speed == 0) {
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
|
|
|
child = demux->client->main->current_variant->data;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
|
|
|
} else {
|
|
|
|
GList *tmp = gst_m3u8_client_get_playlist_for_bitrate (demux->client,
|
|
|
|
demux->connection_speed);
|
|
|
|
|
|
|
|
child = GST_M3U8 (tmp->data);
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_m3u8_client_set_current (demux->client, child);
|
2012-05-08 15:34:47 +00:00
|
|
|
if (!gst_hls_demux_update_playlist (demux, FALSE)) {
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_ERROR_OBJECT (demux, "Could not fetch the child playlist %s",
|
|
|
|
child->uri);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:13:26 +00:00
|
|
|
if (!gst_m3u8_client_is_live (demux->client)) {
|
2011-08-31 02:05:08 +00:00
|
|
|
GstClockTime duration = gst_m3u8_client_get_duration (demux->client);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Sending duration message : %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (duration));
|
|
|
|
if (duration != GST_CLOCK_TIME_NONE)
|
|
|
|
gst_element_post_message (GST_ELEMENT (demux),
|
2012-09-02 00:42:30 +00:00
|
|
|
gst_message_new_duration_changed (GST_OBJECT (demux)));
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cache the first fragments */
|
2011-08-23 22:38:05 +00:00
|
|
|
for (i = 0; i < demux->fragments_cache; i++) {
|
2011-08-23 22:38:26 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT (demux),
|
|
|
|
gst_message_new_buffering (GST_OBJECT (demux),
|
|
|
|
100 * i / demux->fragments_cache));
|
2011-08-02 19:21:47 +00:00
|
|
|
g_get_current_time (&demux->next_update);
|
2012-03-15 18:42:44 +00:00
|
|
|
if (!gst_hls_demux_get_next_fragment (demux, TRUE)) {
|
2012-02-24 08:54:35 +00:00
|
|
|
if (demux->end_of_playlist)
|
|
|
|
break;
|
2011-02-15 21:40:21 +00:00
|
|
|
if (!demux->cancelled)
|
|
|
|
GST_ERROR_OBJECT (demux, "Error caching the first fragments");
|
2011-02-14 17:51:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-02-15 21:40:21 +00:00
|
|
|
/* make sure we stop caching fragments if something cancelled it */
|
|
|
|
if (demux->cancelled)
|
|
|
|
return FALSE;
|
2011-08-02 19:21:47 +00:00
|
|
|
gst_hls_demux_switch_playlist (demux);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
2011-08-23 22:38:26 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT (demux),
|
|
|
|
gst_message_new_buffering (GST_OBJECT (demux), 100));
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_get_current_time (&demux->next_update);
|
|
|
|
|
|
|
|
demux->need_cache = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-01 23:21:34 +00:00
|
|
|
static gchar *
|
2012-03-15 18:42:44 +00:00
|
|
|
gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf)
|
2011-04-01 23:21:34 +00:00
|
|
|
{
|
2012-04-12 12:17:14 +00:00
|
|
|
GstMapInfo info;
|
2011-04-01 23:21:34 +00:00
|
|
|
gchar *playlist;
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
if (!gst_buffer_map (buf, &info, GST_MAP_READ))
|
2011-04-01 23:21:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
if (!g_utf8_validate ((gchar *) info.data, info.size, NULL))
|
|
|
|
goto validate_error;
|
|
|
|
|
2011-04-01 23:21:34 +00:00
|
|
|
/* alloc size + 1 to end with a null character */
|
2012-04-12 12:17:14 +00:00
|
|
|
playlist = g_malloc0 (info.size + 1);
|
2012-10-12 13:09:07 +00:00
|
|
|
memcpy (playlist, info.data, info.size);
|
2012-03-15 18:42:44 +00:00
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
gst_buffer_unmap (buf, &info);
|
2012-03-15 18:42:44 +00:00
|
|
|
gst_buffer_unref (buf);
|
2011-04-01 23:21:34 +00:00
|
|
|
return playlist;
|
2012-04-12 12:17:14 +00:00
|
|
|
|
|
|
|
validate_error:
|
|
|
|
gst_buffer_unmap (buf, &info);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return NULL;
|
2011-04-01 23:21:34 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean
|
2012-05-08 15:34:47 +00:00
|
|
|
gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2012-03-15 18:42:44 +00:00
|
|
|
GstFragment *download;
|
|
|
|
GstBuffer *buf;
|
2011-02-14 17:51:32 +00:00
|
|
|
gchar *playlist;
|
2012-05-08 15:33:50 +00:00
|
|
|
gboolean updated = FALSE;
|
2012-03-15 18:42:44 +00:00
|
|
|
|
2011-09-02 23:49:38 +00:00
|
|
|
const gchar *uri = gst_m3u8_client_get_current_uri (demux->client);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-03-15 18:42:44 +00:00
|
|
|
download = gst_uri_downloader_fetch_uri (demux->downloader, uri);
|
|
|
|
|
|
|
|
if (download == NULL)
|
2011-02-14 17:51:32 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
buf = gst_fragment_get_buffer (download);
|
2012-03-15 18:42:44 +00:00
|
|
|
playlist = gst_hls_src_buf_to_utf8_playlist (buf);
|
|
|
|
g_object_unref (download);
|
|
|
|
|
2011-04-01 23:21:34 +00:00
|
|
|
if (playlist == NULL) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Couldn't not validate playlist encoding");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-09-16 00:47:54 +00:00
|
|
|
|
2012-05-08 15:33:50 +00:00
|
|
|
updated = gst_m3u8_client_update (demux->client, playlist);
|
|
|
|
|
|
|
|
/* If it's a live source, do not let the sequence number go beyond
|
|
|
|
* three fragments before the end of the list */
|
2012-05-08 15:34:47 +00:00
|
|
|
if (updated && update == FALSE && demux->client->current &&
|
2012-05-08 15:33:50 +00:00
|
|
|
gst_m3u8_client_is_live (demux->client)) {
|
|
|
|
guint last_sequence;
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
|
|
|
last_sequence =
|
2012-05-08 17:04:35 +00:00
|
|
|
GST_M3U8_MEDIA_FILE (g_list_last (demux->client->current->
|
|
|
|
files)->data)->sequence;
|
2012-05-08 15:33:50 +00:00
|
|
|
|
|
|
|
if (demux->client->sequence >= last_sequence - 3) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Sequence is beyond playlist. Moving back to %d",
|
|
|
|
last_sequence - 3);
|
|
|
|
demux->need_segment = TRUE;
|
|
|
|
demux->client->sequence = last_sequence - 3;
|
|
|
|
}
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-05-08 17:04:35 +00:00
|
|
|
gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2012-05-10 14:10:14 +00:00
|
|
|
GList *previous_variant, *current_variant;
|
2012-05-08 17:04:35 +00:00
|
|
|
gint old_bandwidth, new_bandwidth;
|
2011-03-12 12:15:52 +00:00
|
|
|
|
2012-05-10 14:11:18 +00:00
|
|
|
/* If user specifies a connection speed never use a playlist with a bandwidth
|
|
|
|
* superior than it */
|
|
|
|
if (demux->connection_speed != 0 && max_bitrate > demux->connection_speed)
|
|
|
|
max_bitrate = demux->connection_speed;
|
|
|
|
|
2012-05-10 14:10:14 +00:00
|
|
|
previous_variant = demux->client->main->current_variant;
|
|
|
|
current_variant = gst_m3u8_client_get_playlist_for_bitrate (demux->client,
|
|
|
|
max_bitrate);
|
2011-03-12 12:15:52 +00:00
|
|
|
|
2012-05-08 17:27:15 +00:00
|
|
|
retry_failover_protection:
|
|
|
|
old_bandwidth = GST_M3U8 (previous_variant->data)->bandwidth;
|
|
|
|
new_bandwidth = GST_M3U8 (current_variant->data)->bandwidth;
|
|
|
|
|
2011-03-12 12:15:52 +00:00
|
|
|
/* Don't do anything else if the playlist is the same */
|
2012-05-08 17:27:15 +00:00
|
|
|
if (new_bandwidth == old_bandwidth) {
|
2011-03-12 12:15:52 +00:00
|
|
|
return TRUE;
|
2011-09-02 23:49:38 +00:00
|
|
|
}
|
2011-03-12 12:15:52 +00:00
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
demux->client->main->current_variant = current_variant;
|
2011-09-02 23:49:38 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
gst_m3u8_client_set_current (demux->client, current_variant->data);
|
2011-09-02 23:49:38 +00:00
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
|
|
|
|
" to bitrate %dbps", old_bandwidth, max_bitrate, new_bandwidth);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-05-08 15:34:47 +00:00
|
|
|
if (gst_hls_demux_update_playlist (demux, FALSE)) {
|
2012-05-08 17:04:35 +00:00
|
|
|
GstStructure *s;
|
|
|
|
|
2011-09-16 00:47:54 +00:00
|
|
|
s = gst_structure_new ("playlist",
|
|
|
|
"uri", G_TYPE_STRING, gst_m3u8_client_get_current_uri (demux->client),
|
|
|
|
"bitrate", G_TYPE_INT, new_bandwidth, NULL);
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_element (GST_OBJECT_CAST (demux), s));
|
|
|
|
} else {
|
2012-05-08 17:27:15 +00:00
|
|
|
GList *failover = NULL;
|
|
|
|
|
2011-09-16 00:47:54 +00:00
|
|
|
GST_INFO_OBJECT (demux, "Unable to update playlist. Switching back");
|
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
2012-05-08 17:27:15 +00:00
|
|
|
|
|
|
|
failover = g_list_previous (current_variant);
|
|
|
|
if (failover && new_bandwidth == GST_M3U8 (failover->data)->bandwidth) {
|
|
|
|
current_variant = failover;
|
|
|
|
goto retry_failover_protection;
|
|
|
|
}
|
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
demux->client->main->current_variant = previous_variant;
|
2011-09-16 00:47:54 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
2012-05-08 17:04:35 +00:00
|
|
|
gst_m3u8_client_set_current (demux->client, previous_variant->data);
|
|
|
|
/* Try a lower bitrate (or stop if we just tried the lowest) */
|
|
|
|
if (new_bandwidth ==
|
|
|
|
GST_M3U8 (g_list_first (demux->client->main->lists)->data)->bandwidth)
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return gst_hls_demux_change_playlist (demux, new_bandwidth - 1);
|
2011-09-16 00:47:54 +00:00
|
|
|
}
|
2011-03-12 12:32:57 +00:00
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
/* Force typefinding since we might have changed media type */
|
|
|
|
demux->do_typefind = TRUE;
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_schedule (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
gfloat update_factor;
|
|
|
|
gint count;
|
|
|
|
|
|
|
|
/* As defined in §6.3.4. Reloading the Playlist file:
|
|
|
|
* "If the client reloads a Playlist file and finds that it has not
|
|
|
|
* changed then it MUST wait for a period of time before retrying. The
|
|
|
|
* minimum delay is a multiple of the target duration. This multiple is
|
|
|
|
* 0.5 for the first attempt, 1.5 for the second, and 3.0 thereafter."
|
|
|
|
*/
|
|
|
|
count = demux->client->update_failed_count;
|
|
|
|
if (count < 3)
|
|
|
|
update_factor = update_interval_factor[count];
|
|
|
|
else
|
|
|
|
update_factor = update_interval_factor[3];
|
|
|
|
|
|
|
|
/* schedule the next update using the target duration field of the
|
|
|
|
* playlist */
|
|
|
|
g_time_val_add (&demux->next_update,
|
2011-09-02 23:49:38 +00:00
|
|
|
gst_m3u8_client_get_target_duration (demux->client)
|
|
|
|
/ GST_SECOND * G_USEC_PER_SEC * update_factor);
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Next update scheduled at %s",
|
|
|
|
g_time_val_to_iso8601 (&demux->next_update));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_switch_playlist (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
GTimeVal now;
|
2012-05-08 17:04:35 +00:00
|
|
|
GstClockTime diff;
|
|
|
|
gsize size;
|
|
|
|
gint bitrate;
|
|
|
|
GstFragment *fragment = g_queue_peek_tail (demux->queue);
|
|
|
|
GstBuffer *buffer;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-09-02 23:49:38 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
|
|
|
if (!demux->client->main->lists) {
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
2011-02-14 17:51:32 +00:00
|
|
|
return TRUE;
|
2011-09-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
/* compare the time when the fragment was downloaded with the time when it was
|
|
|
|
* scheduled */
|
2012-05-08 17:04:35 +00:00
|
|
|
g_get_current_time (&now);
|
|
|
|
diff = (GST_TIMEVAL_TO_TIME (now) - GST_TIMEVAL_TO_TIME (demux->next_update));
|
|
|
|
buffer = gst_fragment_get_buffer (fragment);
|
|
|
|
size = gst_buffer_get_size (buffer);
|
|
|
|
bitrate = (size * 8) / ((double) diff / GST_SECOND);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
GST_DEBUG ("Downloaded %d bytes in %" GST_TIME_FORMAT ". Bitrate is : %d",
|
2012-07-15 11:27:12 +00:00
|
|
|
(guint) size, GST_TIME_ARGS (diff), bitrate);
|
2011-07-26 15:02:05 +00:00
|
|
|
|
2012-05-08 17:04:35 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
return gst_hls_demux_change_playlist (demux, bitrate * demux->bitrate_limit);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-03-15 18:42:44 +00:00
|
|
|
gst_hls_demux_get_next_fragment (GstHLSDemux * demux, gboolean caching)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2012-03-15 18:42:44 +00:00
|
|
|
GstFragment *download;
|
2011-02-14 17:51:32 +00:00
|
|
|
const gchar *next_fragment_uri;
|
2011-04-14 11:34:53 +00:00
|
|
|
GstClockTime duration;
|
2011-08-16 19:43:08 +00:00
|
|
|
GstClockTime timestamp;
|
2012-03-15 18:42:44 +00:00
|
|
|
GstBuffer *buf;
|
2011-02-14 17:51:32 +00:00
|
|
|
gboolean discont;
|
|
|
|
|
2011-04-14 11:34:53 +00:00
|
|
|
if (!gst_m3u8_client_get_next_fragment (demux->client, &discont,
|
2011-08-16 19:43:08 +00:00
|
|
|
&next_fragment_uri, &duration, ×tamp)) {
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_INFO_OBJECT (demux, "This playlist doesn't contain more fragments");
|
|
|
|
demux->end_of_playlist = TRUE;
|
2012-03-15 22:21:58 +00:00
|
|
|
gst_task_start (demux->stream_task);
|
2011-02-14 17:51:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "Fetching next fragment %s", next_fragment_uri);
|
|
|
|
|
2012-03-15 18:42:44 +00:00
|
|
|
download = gst_uri_downloader_fetch_uri (demux->downloader,
|
|
|
|
next_fragment_uri);
|
|
|
|
|
|
|
|
if (download == NULL)
|
|
|
|
goto error;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
buf = gst_fragment_get_buffer (download);
|
2011-04-14 11:34:53 +00:00
|
|
|
GST_BUFFER_DURATION (buf) = duration;
|
2012-04-12 12:17:14 +00:00
|
|
|
GST_BUFFER_PTS (buf) = timestamp;
|
2011-02-15 20:49:20 +00:00
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
/* We actually need to do this every time we switch bitrate */
|
|
|
|
if (G_UNLIKELY (demux->do_typefind)) {
|
2012-04-12 12:17:14 +00:00
|
|
|
GstCaps *caps = gst_fragment_get_caps (download);
|
2011-07-26 15:02:05 +00:00
|
|
|
|
|
|
|
if (!demux->input_caps || !gst_caps_is_equal (caps, demux->input_caps)) {
|
|
|
|
gst_caps_replace (&demux->input_caps, caps);
|
|
|
|
/* gst_pad_set_caps (demux->srcpad, demux->input_caps); */
|
2011-02-15 20:49:20 +00:00
|
|
|
GST_INFO_OBJECT (demux, "Input source caps: %" GST_PTR_FORMAT,
|
|
|
|
demux->input_caps);
|
2011-07-26 15:02:05 +00:00
|
|
|
demux->do_typefind = FALSE;
|
2012-04-12 12:17:14 +00:00
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
} else {
|
|
|
|
gst_fragment_set_caps (download, demux->input_caps);
|
2011-02-15 20:49:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
if (discont) {
|
2011-02-15 03:39:34 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Marking fragment as discontinuous");
|
2011-02-15 20:49:20 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
g_queue_push_tail (demux->queue, download);
|
2012-03-15 18:42:44 +00:00
|
|
|
if (!caching) {
|
|
|
|
GST_TASK_SIGNAL (demux->updates_task);
|
2012-04-25 11:31:36 +00:00
|
|
|
gst_task_start (demux->stream_task);
|
2012-03-15 18:42:44 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
return TRUE;
|
2012-03-15 18:42:44 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
gst_hls_demux_stop (demux);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|