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>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
* |[
|
2011-04-13 20:25:57 +00:00
|
|
|
* gst-launch souphttpsrc location=http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8 ! hlsdemux ! decodebin2 ! ffmpegcolorspace ! 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
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
2011-02-15 20:49:20 +00:00
|
|
|
#include <gst/base/gsttypefindhelper.h>
|
2011-02-14 17:51:32 +00:00
|
|
|
#include "gsthlsdemux.h"
|
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
|
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
|
|
|
|
|
|
|
static GstStaticPadTemplate fetchertemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_hls_demux_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_hls_demux_debug
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
|
|
|
|
PROP_FRAGMENTS_CACHE,
|
|
|
|
PROP_BITRATE_SWITCH_TOLERANCE,
|
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
static const float update_interval_factor[] = { 1, 0.5, 1.5, 3 };
|
|
|
|
|
|
|
|
#define DEFAULT_FRAGMENTS_CACHE 3
|
|
|
|
#define DEFAULT_FAILED_COUNT 3
|
|
|
|
#define DEFAULT_BITRATE_SWITCH_TOLERANCE 0.4
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
static GstBusSyncReply gst_hls_demux_fetcher_bus_handler (GstBus * bus,
|
|
|
|
GstMessage * message, gpointer data);
|
|
|
|
static GstFlowReturn gst_hls_demux_chain (GstPad * pad, GstBuffer * buf);
|
|
|
|
static gboolean gst_hls_demux_sink_event (GstPad * pad, GstEvent * event);
|
2011-04-13 21:06:18 +00:00
|
|
|
static gboolean gst_hls_demux_src_event (GstPad * pad, GstEvent * event);
|
2011-03-12 11:50:25 +00:00
|
|
|
static gboolean gst_hls_demux_src_query (GstPad * pad, GstQuery * query);
|
2011-03-30 01:34:39 +00:00
|
|
|
static GstFlowReturn gst_hls_demux_fetcher_chain (GstPad * pad,
|
|
|
|
GstBuffer * buf);
|
|
|
|
static gboolean gst_hls_demux_fetcher_sink_event (GstPad * pad,
|
|
|
|
GstEvent * event);
|
2011-02-14 17:51:32 +00:00
|
|
|
static void gst_hls_demux_loop (GstHLSDemux * demux);
|
|
|
|
static void gst_hls_demux_stop (GstHLSDemux * demux);
|
2011-03-30 01:34:39 +00:00
|
|
|
static void gst_hls_demux_stop_fetcher (GstHLSDemux * demux,
|
|
|
|
gboolean cancelled);
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean gst_hls_demux_start_update (GstHLSDemux * demux);
|
|
|
|
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);
|
2011-03-30 01:34:39 +00:00
|
|
|
static gboolean gst_hls_demux_get_next_fragment (GstHLSDemux * demux,
|
|
|
|
gboolean retry);
|
|
|
|
static gboolean gst_hls_demux_update_playlist (GstHLSDemux * demux,
|
|
|
|
gboolean retry);
|
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);
|
2011-04-01 23:21:34 +00:00
|
|
|
static gchar *gst_hls_src_buf_to_utf8_playlist (gchar * string, guint size);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
_do_init (GType type)
|
|
|
|
{
|
2011-03-30 01:34:39 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
|
|
|
|
"hlsdemux element");
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_BOILERPLATE_FULL (GstHLSDemux, gst_hls_demux, GstElement,
|
|
|
|
GST_TYPE_ELEMENT, _do_init);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
gst_element_class_set_details_simple (element_class,
|
2011-03-30 01:34:39 +00:00
|
|
|
"HLS Demuxer",
|
2011-02-15 20:49:20 +00:00
|
|
|
"Demuxer/URIList",
|
2011-03-30 01:34:39 +00:00
|
|
|
"HTTP Live Streaming demuxer",
|
2011-02-14 17:51:32 +00:00
|
|
|
"Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
|
|
|
|
"Andoni Morales Alastruey <ylatuya@gmail.com>");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_dispose (GObject * obj)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (obj);
|
|
|
|
|
|
|
|
g_cond_free (demux->fetcher_cond);
|
|
|
|
g_mutex_free (demux->fetcher_lock);
|
|
|
|
|
|
|
|
g_cond_free (demux->thread_cond);
|
|
|
|
g_mutex_free (demux->thread_lock);
|
|
|
|
|
|
|
|
if (GST_TASK_STATE (demux->task) != GST_TASK_STOPPED) {
|
|
|
|
gst_task_stop (demux->task);
|
|
|
|
gst_task_join (demux->task);
|
|
|
|
}
|
|
|
|
gst_object_unref (demux->task);
|
|
|
|
g_static_rec_mutex_free (&demux->task_lock);
|
|
|
|
|
|
|
|
gst_object_unref (demux->fetcher_bus);
|
|
|
|
gst_object_unref (demux->fetcherpad);
|
|
|
|
|
2011-02-15 20:55:26 +00:00
|
|
|
gst_hls_demux_reset (demux, TRUE);
|
|
|
|
|
2011-04-01 23:08:02 +00:00
|
|
|
gst_object_unref (demux->download);
|
|
|
|
|
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;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2011-03-30 08:11:24 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-02-14 17:51:32 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_BITRATE_SWITCH_TOLERANCE,
|
|
|
|
g_param_spec_float ("bitrate-switch-tolerance",
|
|
|
|
"Bitrate switch tolerance",
|
|
|
|
"Tolerance with respect of the fragment duration to switch to "
|
|
|
|
"a different bitrate if the client is too slow/fast.",
|
2011-03-30 08:11:24 +00:00
|
|
|
0, 1, DEFAULT_BITRATE_SWITCH_TOLERANCE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-03-30 08:11:24 +00:00
|
|
|
gstelement_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_init (GstHLSDemux * demux, GstHLSDemuxClass * klass)
|
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* fetcher pad */
|
2011-03-30 01:34:39 +00:00
|
|
|
demux->fetcherpad =
|
|
|
|
gst_pad_new_from_static_template (&fetchertemplate, "sink");
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_pad_set_chain_function (demux->fetcherpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_hls_demux_fetcher_chain));
|
|
|
|
gst_pad_set_event_function (demux->fetcherpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_hls_demux_fetcher_sink_event));
|
|
|
|
gst_pad_set_element_private (demux->fetcherpad, demux);
|
|
|
|
gst_pad_activate_push (demux->fetcherpad, TRUE);
|
|
|
|
|
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;
|
|
|
|
demux->bitrate_switch_tol = DEFAULT_BITRATE_SWITCH_TOLERANCE;
|
|
|
|
|
2011-04-01 23:08:02 +00:00
|
|
|
demux->download = gst_adapter_new ();
|
2011-02-14 17:51:32 +00:00
|
|
|
demux->fetcher_bus = gst_bus_new ();
|
2011-03-30 01:34:39 +00:00
|
|
|
gst_bus_set_sync_handler (demux->fetcher_bus,
|
|
|
|
gst_hls_demux_fetcher_bus_handler, demux);
|
2011-02-14 17:51:32 +00:00
|
|
|
demux->thread_cond = g_cond_new ();
|
|
|
|
demux->thread_lock = g_mutex_new ();
|
|
|
|
demux->fetcher_cond = g_cond_new ();
|
|
|
|
demux->fetcher_lock = g_mutex_new ();
|
|
|
|
demux->queue = g_queue_new ();
|
|
|
|
g_static_rec_mutex_init (&demux->task_lock);
|
|
|
|
demux->task = gst_task_create ((GstTaskFunction) gst_hls_demux_loop, demux);
|
|
|
|
gst_task_set_lock (demux->task, &demux->task_lock);
|
2011-08-01 16:48:03 +00:00
|
|
|
|
|
|
|
demux->position = 0;
|
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;
|
|
|
|
case PROP_BITRATE_SWITCH_TOLERANCE:
|
|
|
|
demux->bitrate_switch_tol = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
case PROP_BITRATE_SWITCH_TOLERANCE:
|
|
|
|
g_value_set_float (value, demux->bitrate_switch_tol);
|
|
|
|
break;
|
|
|
|
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) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2011-02-15 20:55:26 +00:00
|
|
|
gst_hls_demux_reset (demux, FALSE);
|
2011-02-14 17:51:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
2011-02-15 02:41:43 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2011-02-15 21:40:21 +00:00
|
|
|
demux->cancelled = TRUE;
|
2011-04-13 20:17:05 +00:00
|
|
|
g_cond_signal (demux->fetcher_cond);
|
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
|
|
|
|
gst_hls_demux_src_event (GstPad * pad, GstEvent * event)
|
|
|
|
{
|
|
|
|
switch (event->type) {
|
|
|
|
/* FIXME: ignore seek event for the moment */
|
|
|
|
case GST_EVENT_SEEK:
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_pad_event_default (pad, event);
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_sink_event (GstPad * pad, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (gst_pad_get_parent (pad));
|
|
|
|
GstQuery *query;
|
2011-02-15 01:13:56 +00:00
|
|
|
gboolean ret;
|
2011-02-14 17:51:32 +00:00
|
|
|
gchar *uri;
|
|
|
|
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case GST_EVENT_EOS:{
|
|
|
|
gchar *playlist;
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-04-01 23:21:34 +00:00
|
|
|
playlist = gst_hls_src_buf_to_utf8_playlist ((gchar *)
|
|
|
|
GST_BUFFER_DATA (demux->playlist), GST_BUFFER_SIZE (demux->playlist));
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_buffer_unref (demux->playlist);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_task_start (demux->task);
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-08-01 16:48:03 +00:00
|
|
|
case GST_EVENT_NEWSEGMENT:
|
|
|
|
/* Swallow newsegments, we'll push our own */
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
2011-02-14 17:51:32 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_pad_event_default (pad, event);
|
|
|
|
}
|
|
|
|
|
2011-03-12 11:50:25 +00:00
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_src_query (GstPad * pad, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstHLSDemux *hlsdemux;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
if (query == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hlsdemux = GST_HLS_DEMUX (gst_pad_get_element_private (pad));
|
|
|
|
|
|
|
|
switch (query->type) {
|
|
|
|
case GST_QUERY_DURATION:{
|
|
|
|
GstClockTime duration;
|
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
|
|
|
}
|
|
|
|
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? */
|
|
|
|
gst_query_set_uri (query, hlsdemux->client->current->uri);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2011-05-03 10:51:44 +00:00
|
|
|
case GST_QUERY_SEEKING:{
|
|
|
|
GstFormat fmt;
|
|
|
|
gint stop = -1;
|
|
|
|
|
|
|
|
gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
gst_query_set_seeking (query, fmt, FALSE, 0, stop);
|
|
|
|
ret = TRUE;
|
|
|
|
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 gboolean
|
|
|
|
gst_hls_demux_fetcher_sink_event (GstPad * pad, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (gst_pad_get_element_private (pad));
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case GST_EVENT_EOS:{
|
|
|
|
GST_DEBUG_OBJECT (demux, "Got EOS on the fetcher pad");
|
|
|
|
/* signal we have fetched the URI */
|
2011-02-15 21:40:21 +00:00
|
|
|
if (!demux->cancelled)
|
|
|
|
g_cond_signal (demux->fetcher_cond);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_hls_demux_chain (GstPad * pad, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (gst_pad_get_parent (pad));
|
|
|
|
|
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
|
2011-02-14 17:51:32 +00:00
|
|
|
demux->playlist = gst_buffer_join (demux->playlist, buf);
|
|
|
|
|
|
|
|
gst_object_unref (demux);
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_hls_demux_fetcher_chain (GstPad * pad, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (gst_pad_get_element_private (pad));
|
|
|
|
|
|
|
|
/* The source element can be an http source element. In case we get a 404,
|
2011-04-01 23:08:02 +00:00
|
|
|
* the html response will be sent downstream and the adapter
|
2011-02-14 17:51:32 +00:00
|
|
|
* will not be null, which might make us think that the request proceed
|
2011-02-15 03:39:34 +00:00
|
|
|
* successfully. But it will also post an error message in the bus that
|
2011-02-14 17:51:32 +00:00
|
|
|
* is handled synchronously and that will set demux->fetcher_error to TRUE,
|
|
|
|
* which is used to discard this buffer with the html response. */
|
|
|
|
if (demux->fetcher_error) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-04-01 19:52:48 +00:00
|
|
|
GST_LOG_OBJECT (demux, "The uri fetcher received a new buffer of size %u",
|
2011-02-15 03:39:34 +00:00
|
|
|
GST_BUFFER_SIZE (buf));
|
2011-04-01 23:08:02 +00:00
|
|
|
gst_adapter_push (demux->download, buf);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
{
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_stop_fetcher (GstHLSDemux * demux, gboolean cancelled)
|
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
|
2011-02-15 03:39:34 +00:00
|
|
|
/* When the fetcher is stopped while it's downloading, we will get an EOS that
|
|
|
|
* unblocks the fetcher thread and tries to stop it again from that thread.
|
|
|
|
* Here we check if the fetcher as already been stopped before continuing */
|
2011-02-15 02:41:01 +00:00
|
|
|
if (demux->fetcher == NULL || demux->stopping_fetcher)
|
2011-02-14 17:51:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Stopping fetcher.");
|
2011-02-15 02:41:01 +00:00
|
|
|
demux->stopping_fetcher = TRUE;
|
2011-02-15 03:39:34 +00:00
|
|
|
/* set the element state to NULL */
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_element_set_state (demux->fetcher, GST_STATE_NULL);
|
2011-02-16 02:49:49 +00:00
|
|
|
gst_element_get_state (demux->fetcher, NULL, NULL, GST_CLOCK_TIME_NONE);
|
2011-02-15 03:39:34 +00:00
|
|
|
/* unlink it from the internal pad */
|
2011-02-14 17:51:32 +00:00
|
|
|
pad = gst_pad_get_peer (demux->fetcherpad);
|
|
|
|
if (pad) {
|
|
|
|
gst_pad_unlink (pad, demux->fetcherpad);
|
|
|
|
gst_object_unref (pad);
|
|
|
|
}
|
2011-02-15 03:39:34 +00:00
|
|
|
/* and finally unref it */
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_object_unref (demux->fetcher);
|
|
|
|
demux->fetcher = NULL;
|
2011-02-15 03:39:34 +00:00
|
|
|
|
|
|
|
/* if we stopped it to cancell a download, free the cached buffer */
|
2011-04-01 23:08:02 +00:00
|
|
|
if (cancelled && !gst_adapter_available (demux->download)) {
|
|
|
|
gst_adapter_clear (demux->download);
|
2011-02-15 03:39:34 +00:00
|
|
|
/* signal the fetcher thread that the download has finished/cancelled */
|
2011-02-14 17:51:32 +00:00
|
|
|
g_cond_signal (demux->fetcher_cond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_stop (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
gst_hls_demux_stop_fetcher (demux, TRUE);
|
|
|
|
if (GST_TASK_STATE (demux->task) != GST_TASK_STOPPED)
|
|
|
|
gst_task_stop (demux->task);
|
|
|
|
g_cond_signal (demux->thread_cond);
|
|
|
|
}
|
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
static void
|
|
|
|
switch_pads (GstHLSDemux * demux, GstCaps * newcaps)
|
|
|
|
{
|
|
|
|
GstPad *oldpad = demux->srcpad;
|
|
|
|
|
|
|
|
GST_DEBUG ("Switching pads (oldpad:%p)", oldpad);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
gst_pad_set_caps (demux->srcpad, newcaps);
|
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
|
|
|
|
gst_hls_demux_loop (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
GstBuffer *buf;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
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-02-15 03:39:34 +00:00
|
|
|
/* we can start now the updates thread */
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_hls_demux_start_update (demux);
|
|
|
|
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-04-13 21:35:50 +00:00
|
|
|
GST_TASK_WAIT (demux->task);
|
|
|
|
/* If the queue is still empty check again if it's the end of the
|
|
|
|
* playlist in case we reached it after beeing woken up */
|
|
|
|
if (g_queue_is_empty (demux->queue) && demux->end_of_playlist)
|
|
|
|
goto end_of_playlist;
|
2011-03-29 21:06:14 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
buf = g_queue_pop_head (demux->queue);
|
2011-07-26 15:02:05 +00:00
|
|
|
|
|
|
|
/* Figure out if we need to create/switch pads */
|
|
|
|
if (G_UNLIKELY (!demux->srcpad
|
|
|
|
|| GST_BUFFER_CAPS (buf) != GST_PAD_CAPS (demux->srcpad))) {
|
|
|
|
switch_pads (demux, GST_BUFFER_CAPS (buf));
|
2011-08-01 16:48:03 +00:00
|
|
|
/* And send a newsegment */
|
|
|
|
gst_pad_push_event (demux->srcpad,
|
|
|
|
gst_event_new_new_segment (0, 1.0, GST_FORMAT_TIME, demux->position,
|
|
|
|
GST_CLOCK_TIME_NONE, demux->position));
|
2011-07-26 15:02:05 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 16:48:03 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buf)))
|
|
|
|
demux->position += GST_BUFFER_DURATION (buf);
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
ret = gst_pad_push (demux->srcpad, buf);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
end_of_playlist:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "Reached end of playlist, sending EOS");
|
|
|
|
gst_pad_push_event (demux->srcpad, gst_event_new_eos ());
|
|
|
|
gst_hls_demux_stop (demux);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
|
2011-07-27 10:02:41 +00:00
|
|
|
("Could not cache the first fragments"), (NULL));
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_hls_demux_stop (demux);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
/* FIXME: handle error */
|
|
|
|
gst_hls_demux_stop (demux);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBusSyncReply
|
|
|
|
gst_hls_demux_fetcher_bus_handler (GstBus * bus,
|
|
|
|
GstMessage * message, gpointer data)
|
|
|
|
{
|
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX (data);
|
|
|
|
|
|
|
|
if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
|
|
|
|
demux->fetcher_error = TRUE;
|
|
|
|
g_cond_signal (demux->fetcher_cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_message_unref (message);
|
|
|
|
return GST_BUS_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_make_fetcher (GstHLSDemux * demux, const gchar * uri)
|
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
|
|
|
|
if (!gst_uri_is_valid (uri))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Creating fetcher for the URI:%s", uri);
|
|
|
|
demux->fetcher = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
|
|
|
|
if (!demux->fetcher)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
demux->fetcher_error = FALSE;
|
2011-02-15 02:41:01 +00:00
|
|
|
demux->stopping_fetcher = FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_element_set_bus (GST_ELEMENT (demux->fetcher), demux->fetcher_bus);
|
|
|
|
|
|
|
|
g_object_set (G_OBJECT (demux->fetcher), "location", uri, NULL);
|
|
|
|
pad = gst_element_get_static_pad (demux->fetcher, "src");
|
|
|
|
if (pad) {
|
|
|
|
gst_pad_link (pad, demux->fetcherpad);
|
|
|
|
gst_object_unref (pad);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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->thread_return = FALSE;
|
|
|
|
demux->accumulated_delay = 0;
|
|
|
|
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-04-01 23:08:02 +00:00
|
|
|
gst_adapter_clear (demux->download);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
if (demux->client)
|
|
|
|
gst_m3u8_client_free (demux->client);
|
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)) {
|
|
|
|
GstBuffer *buf = g_queue_pop_head (demux->queue);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
}
|
|
|
|
g_queue_clear (demux->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_update_thread (GstHLSDemux * demux)
|
|
|
|
{
|
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 */
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
g_mutex_lock (demux->thread_lock);
|
|
|
|
while (TRUE) {
|
|
|
|
/* block until the next scheduled update or the signal to quit this thread */
|
|
|
|
if (g_cond_timed_wait (demux->thread_cond, demux->thread_lock,
|
|
|
|
&demux->next_update)) {
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update the playlist for live sources */
|
|
|
|
if (gst_m3u8_client_is_live (demux->client)) {
|
|
|
|
if (!gst_hls_demux_update_playlist (demux, TRUE)) {
|
|
|
|
GST_ERROR_OBJECT (demux, "Could not update the playlist");
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* schedule the next update */
|
|
|
|
gst_hls_demux_schedule (demux);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fetch the next fragment */
|
|
|
|
if (!gst_hls_demux_get_next_fragment (demux, TRUE)) {
|
2011-02-16 02:51:08 +00:00
|
|
|
if (!demux->end_of_playlist && !demux->cancelled)
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_ERROR_OBJECT (demux, "Could not fetch the next fragment");
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to switch to another bitrate if needed */
|
|
|
|
gst_hls_demux_switch_playlist (demux);
|
|
|
|
}
|
|
|
|
|
|
|
|
quit:
|
|
|
|
{
|
|
|
|
g_mutex_unlock (demux->thread_lock);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_start_update (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
GError *error;
|
|
|
|
|
2011-02-15 03:39:34 +00:00
|
|
|
/* creates a new thread for the updates */
|
2011-02-14 17:51:32 +00:00
|
|
|
demux->updates_thread = g_thread_create (
|
|
|
|
(GThreadFunc) gst_hls_demux_update_thread, demux, TRUE, &error);
|
|
|
|
return (error != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_cache_fragments (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
/* Start parsing the main playlist */
|
|
|
|
gst_m3u8_client_set_current (demux->client, demux->client->main);
|
|
|
|
|
|
|
|
if (gst_m3u8_client_is_live (demux->client)) {
|
|
|
|
if (!gst_hls_demux_update_playlist (demux, FALSE)) {
|
|
|
|
GST_ERROR_OBJECT (demux, "Could not fetch the main playlist %s",
|
|
|
|
demux->client->main->uri);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
GstM3U8 *child = demux->client->main->lists->data;
|
|
|
|
gst_m3u8_client_set_current (demux->client, child);
|
|
|
|
if (!gst_hls_demux_update_playlist (demux, FALSE)) {
|
|
|
|
GST_ERROR_OBJECT (demux, "Could not fetch the child playlist %s",
|
|
|
|
child->uri);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it's a live source, set the sequence number to the end of the list
|
|
|
|
* and substract the 'fragmets_cache' to start from the last fragment*/
|
|
|
|
if (gst_m3u8_client_is_live (demux->client)) {
|
|
|
|
demux->client->sequence += g_list_length (demux->client->current->files);
|
|
|
|
if (demux->client->sequence >= demux->fragments_cache)
|
|
|
|
demux->client->sequence -= demux->fragments_cache;
|
|
|
|
else
|
|
|
|
demux->client->sequence = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cache the first fragments */
|
|
|
|
for (i = 0; i < demux->fragments_cache - 1; i++) {
|
2011-08-02 19:21:47 +00:00
|
|
|
g_get_current_time (&demux->next_update);
|
|
|
|
g_time_val_add (&demux->next_update,
|
|
|
|
demux->client->current->targetduration * 1000000);
|
2011-02-14 17:51:32 +00:00
|
|
|
if (!gst_hls_demux_get_next_fragment (demux, FALSE)) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
g_get_current_time (&demux->next_update);
|
|
|
|
|
|
|
|
demux->need_cache = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_fetch_location (GstHLSDemux * demux, const gchar * uri)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
2011-02-15 23:55:30 +00:00
|
|
|
gboolean bret = FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_mutex_lock (demux->fetcher_lock);
|
|
|
|
|
|
|
|
if (!gst_hls_demux_make_fetcher (demux, uri)) {
|
|
|
|
goto uri_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_element_set_state (demux->fetcher, GST_STATE_PLAYING);
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
goto state_change_error;
|
|
|
|
|
2011-02-15 03:39:34 +00:00
|
|
|
/* wait until we have fetched the uri */
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Waiting to fetch the URI");
|
|
|
|
g_cond_wait (demux->fetcher_cond, demux->fetcher_lock);
|
|
|
|
|
|
|
|
gst_hls_demux_stop_fetcher (demux, FALSE);
|
|
|
|
|
2011-04-01 23:08:02 +00:00
|
|
|
if (gst_adapter_available (demux->download)) {
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_INFO_OBJECT (demux, "URI fetched successfully");
|
2011-02-15 23:55:30 +00:00
|
|
|
bret = TRUE;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
goto quit;
|
|
|
|
|
|
|
|
uri_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (demux, RESOURCE, OPEN_READ,
|
|
|
|
("Could not create an element to fetch the given URI."), ("URI: \"%s\"",
|
|
|
|
uri));
|
2011-02-15 23:55:30 +00:00
|
|
|
bret = FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_change_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (demux, CORE, STATE_CHANGE,
|
2011-07-27 10:02:41 +00:00
|
|
|
("Error changing state of the fetcher element."), (NULL));
|
2011-02-15 23:55:30 +00:00
|
|
|
bret = FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
quit:
|
|
|
|
{
|
|
|
|
g_mutex_unlock (demux->fetcher_lock);
|
2011-02-15 23:55:30 +00:00
|
|
|
return bret;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-01 23:21:34 +00:00
|
|
|
static gchar *
|
|
|
|
gst_hls_src_buf_to_utf8_playlist (gchar * data, guint size)
|
|
|
|
{
|
|
|
|
gchar *playlist;
|
|
|
|
|
|
|
|
if (!g_utf8_validate (data, size, NULL))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* alloc size + 1 to end with a null character */
|
|
|
|
playlist = g_malloc0 (size + 1);
|
|
|
|
memcpy (playlist, data, size + 1);
|
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean retry)
|
|
|
|
{
|
2011-04-01 23:21:34 +00:00
|
|
|
const guint8 *data;
|
2011-02-14 17:51:32 +00:00
|
|
|
gchar *playlist;
|
2011-04-01 23:08:02 +00:00
|
|
|
guint avail;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-03-30 01:34:39 +00:00
|
|
|
GST_INFO_OBJECT (demux, "Updating the playlist %s",
|
|
|
|
demux->client->current->uri);
|
2011-02-14 17:51:32 +00:00
|
|
|
if (!gst_hls_demux_fetch_location (demux, demux->client->current->uri))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-04-01 23:08:02 +00:00
|
|
|
avail = gst_adapter_available (demux->download);
|
2011-04-01 23:21:34 +00:00
|
|
|
data = gst_adapter_peek (demux->download, avail);
|
|
|
|
playlist = gst_hls_src_buf_to_utf8_playlist ((gchar *) data, avail);
|
2011-04-01 23:08:02 +00:00
|
|
|
gst_adapter_clear (demux->download);
|
2011-04-01 23:21:34 +00:00
|
|
|
if (playlist == NULL) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Couldn't not validate playlist encoding");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
gst_m3u8_client_update (demux->client, playlist);
|
2011-02-14 17:51:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_change_playlist (GstHLSDemux * demux, gboolean is_fast)
|
|
|
|
{
|
2011-03-12 12:15:52 +00:00
|
|
|
GList *list;
|
2011-03-12 12:32:57 +00:00
|
|
|
GstStructure *s;
|
2011-03-12 12:15:52 +00:00
|
|
|
|
|
|
|
if (is_fast)
|
|
|
|
list = g_list_next (demux->client->main->lists);
|
|
|
|
else
|
|
|
|
list = g_list_previous (demux->client->main->lists);
|
|
|
|
|
|
|
|
/* Don't do anything else if the playlist is the same */
|
|
|
|
if (!list || list->data == demux->client->current)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
demux->client->main->lists = list;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
gst_m3u8_client_set_current (demux->client, demux->client->main->lists->data);
|
|
|
|
gst_hls_demux_update_playlist (demux, TRUE);
|
|
|
|
GST_INFO_OBJECT (demux, "Client is %s, switching to bitrate %d",
|
|
|
|
is_fast ? "fast" : "slow", demux->client->current->bandwidth);
|
|
|
|
|
2011-03-12 12:32:57 +00:00
|
|
|
s = gst_structure_new ("playlist",
|
|
|
|
"uri", G_TYPE_STRING, demux->client->current->uri,
|
|
|
|
"bitrate", G_TYPE_INT, demux->client->current->bandwidth, NULL);
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_element (GST_OBJECT_CAST (demux), s));
|
|
|
|
|
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,
|
|
|
|
demux->client->current->targetduration * update_factor * 1000000);
|
|
|
|
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;
|
|
|
|
gint64 diff, limit;
|
|
|
|
|
|
|
|
g_get_current_time (&now);
|
|
|
|
if (!demux->client->main->lists)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* compare the time when the fragment was downloaded with the time when it was
|
|
|
|
* scheduled */
|
|
|
|
diff = (GST_TIMEVAL_TO_TIME (demux->next_update) - GST_TIMEVAL_TO_TIME (now));
|
|
|
|
limit = demux->client->current->targetduration * GST_SECOND *
|
|
|
|
demux->bitrate_switch_tol;
|
|
|
|
|
2011-07-26 15:02:05 +00:00
|
|
|
GST_DEBUG ("diff:%s%" GST_TIME_FORMAT ", limit:%" GST_TIME_FORMAT,
|
|
|
|
diff < 0 ? "-" : " ", GST_TIME_ARGS (ABS (diff)), GST_TIME_ARGS (limit));
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
/* if we are on time switch to a higher bitrate */
|
|
|
|
if (diff > limit) {
|
|
|
|
gst_hls_demux_change_playlist (demux, TRUE);
|
|
|
|
} else if (diff < 0) {
|
2011-02-15 03:39:34 +00:00
|
|
|
/* if the client is too slow wait until it has accumulated a certain delay to
|
2011-02-14 17:51:32 +00:00
|
|
|
* switch to a lower bitrate */
|
|
|
|
demux->accumulated_delay -= diff;
|
|
|
|
if (demux->accumulated_delay >= limit) {
|
|
|
|
gst_hls_demux_change_playlist (demux, FALSE);
|
|
|
|
} else if (demux->accumulated_delay < 0) {
|
|
|
|
demux->accumulated_delay = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_get_next_fragment (GstHLSDemux * demux, gboolean retry)
|
|
|
|
{
|
2011-02-15 20:49:20 +00:00
|
|
|
GstBuffer *buf;
|
2011-04-01 23:08:02 +00:00
|
|
|
guint avail;
|
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;
|
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;
|
|
|
|
GST_TASK_SIGNAL (demux->task);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "Fetching next fragment %s", next_fragment_uri);
|
|
|
|
|
|
|
|
if (!gst_hls_demux_fetch_location (demux, next_fragment_uri))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-04-01 23:08:02 +00:00
|
|
|
avail = gst_adapter_available (demux->download);
|
|
|
|
buf = gst_adapter_take_buffer (demux->download, avail);
|
2011-04-14 11:34:53 +00:00
|
|
|
GST_BUFFER_DURATION (buf) = duration;
|
2011-08-16 19:43:08 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (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)) {
|
|
|
|
GstCaps *caps = gst_type_find_helper_for_buffer (NULL, buf, NULL);
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else
|
|
|
|
gst_caps_unref (caps);
|
2011-02-15 20:49:20 +00:00
|
|
|
}
|
2011-07-26 15:02:05 +00:00
|
|
|
gst_buffer_set_caps (buf, 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
|
|
|
}
|
|
|
|
|
2011-02-15 20:49:20 +00:00
|
|
|
g_queue_push_tail (demux->queue, buf);
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_TASK_SIGNAL (demux->task);
|
2011-04-01 23:08:02 +00:00
|
|
|
gst_adapter_clear (demux->download);
|
2011-02-14 17:51:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|