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.
|
2014-02-17 08:19:32 +00:00
|
|
|
* Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
|
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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
2014-04-07 16:57:26 +00:00
|
|
|
#include <gst/base/gsttypefindhelper.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
|
|
|
|
};
|
|
|
|
|
2014-02-17 08:19:32 +00:00
|
|
|
#define DEFAULT_FRAGMENTS_CACHE 1
|
2011-02-14 17:51:32 +00:00
|
|
|
#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-05-08 15:34:47 +00:00
|
|
|
static gboolean gst_hls_demux_update_playlist (GstHLSDemux * demux,
|
2014-02-11 13:57:16 +00:00
|
|
|
gboolean update, GError ** err);
|
2011-03-30 01:34:39 +00:00
|
|
|
static gboolean gst_hls_demux_set_location (GstHLSDemux * demux,
|
2014-05-28 08:19:40 +00:00
|
|
|
const gchar * uri, const gchar * base_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
|
|
|
|
2014-03-09 18:31:31 +00:00
|
|
|
static gboolean gst_hls_demux_change_playlist (GstHLSDemux * demux,
|
2014-08-27 19:26:19 +00:00
|
|
|
guint max_bitrate, gboolean * changed);
|
2014-04-08 20:31:32 +00:00
|
|
|
static GstBuffer *gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
|
|
|
|
GstBuffer * encrypted_buffer, GError ** err);
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
|
|
|
|
const guint8 * iv_data);
|
|
|
|
static void gst_hls_demux_decrypt_end (GstHLSDemux * demux);
|
2014-03-09 18:31:31 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static gboolean gst_hls_demux_is_live (GstAdaptiveDemux * demux);
|
|
|
|
static GstClockTime gst_hls_demux_get_duration (GstAdaptiveDemux * demux);
|
|
|
|
static gint64 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux *
|
|
|
|
demux);
|
|
|
|
static gboolean gst_hls_demux_process_manifest (GstAdaptiveDemux * demux,
|
|
|
|
GstBuffer * buf);
|
|
|
|
static GstFlowReturn gst_hls_demux_update_manifest (GstAdaptiveDemux * demux,
|
|
|
|
GstBuffer * buf);
|
|
|
|
static gboolean gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream);
|
|
|
|
static void gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstBuffer ** buffer);
|
|
|
|
static GstFlowReturn gst_hls_demux_chunk_received (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstBuffer ** chunk);
|
|
|
|
static GstFlowReturn gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream *
|
|
|
|
stream);
|
|
|
|
static GstFlowReturn gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream
|
|
|
|
* stream);
|
|
|
|
static gboolean gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream,
|
|
|
|
guint64 bitrate);
|
|
|
|
static void gst_hls_demux_reset (GstAdaptiveDemux * demux);
|
2014-04-04 15:45:51 +00:00
|
|
|
static gboolean gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux,
|
|
|
|
gint64 * start, gint64 * stop);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
#define gst_hls_demux_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ADAPTIVE_DEMUX);
|
2014-02-25 10:58:57 +00:00
|
|
|
|
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 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
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
|
|
|
|
gst_m3u8_client_free (demux->client);
|
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;
|
2014-08-27 19:26:19 +00:00
|
|
|
GstAdaptiveDemuxClass *adaptivedemux_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;
|
2014-08-27 19:26:19 +00:00
|
|
|
adaptivedemux_class = (GstAdaptiveDemuxClass *) 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;
|
|
|
|
|
2014-11-02 17:18:12 +00:00
|
|
|
#ifndef GST_REMOVE_DEPRECATED
|
2011-02-14 17:51:32 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_FRAGMENTS_CACHE,
|
|
|
|
g_param_spec_uint ("fragments-cache", "Fragments cache",
|
2014-02-23 10:01:57 +00:00
|
|
|
"Number of fragments needed to be cached to start playing "
|
|
|
|
"(DEPRECATED: Has no effect since 1.3.1)",
|
2014-02-10 17:29:23 +00:00
|
|
|
1, G_MAXUINT, DEFAULT_FRAGMENTS_CACHE,
|
2014-11-02 17:18:12 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
|
|
|
|
#endif
|
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",
|
2014-02-20 14:09:36 +00:00
|
|
|
"Codec/Demuxer/Adaptive",
|
2012-04-12 12:17:14 +00:00
|
|
|
"HTTP Live Streaming demuxer",
|
|
|
|
"Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
|
|
|
|
"Andoni Morales Alastruey <ylatuya@gmail.com>");
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
adaptivedemux_class->is_live = gst_hls_demux_is_live;
|
2014-04-04 15:45:51 +00:00
|
|
|
adaptivedemux_class->get_live_seek_range = gst_hls_demux_get_live_seek_range;
|
2014-08-27 19:26:19 +00:00
|
|
|
adaptivedemux_class->get_duration = gst_hls_demux_get_duration;
|
|
|
|
adaptivedemux_class->get_manifest_update_interval =
|
|
|
|
gst_hls_demux_get_manifest_update_interval;
|
|
|
|
adaptivedemux_class->process_manifest = gst_hls_demux_process_manifest;
|
|
|
|
adaptivedemux_class->update_manifest = gst_hls_demux_update_manifest;
|
|
|
|
adaptivedemux_class->reset = gst_hls_demux_reset;
|
|
|
|
adaptivedemux_class->seek = gst_hls_demux_seek;
|
|
|
|
adaptivedemux_class->stream_advance_fragment = gst_hls_demux_advance_fragment;
|
|
|
|
adaptivedemux_class->stream_update_fragment_info =
|
|
|
|
gst_hls_demux_update_fragment_info;
|
|
|
|
adaptivedemux_class->stream_select_bitrate = gst_hls_demux_select_bitrate;
|
|
|
|
|
|
|
|
adaptivedemux_class->start_fragment = gst_hls_demux_start_fragment;
|
|
|
|
adaptivedemux_class->finish_fragment = gst_hls_demux_finish_fragment;
|
|
|
|
adaptivedemux_class->chunk_received = gst_hls_demux_chunk_received;
|
2014-04-29 18:19:51 +00:00
|
|
|
|
2012-04-12 12:17:14 +00:00
|
|
|
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
|
|
|
{
|
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 */
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
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:
|
2014-02-23 10:01:57 +00:00
|
|
|
g_value_set_uint (value, 1);
|
2011-02-14 17:51:32 +00:00
|
|
|
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:
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
|
2013-09-17 21:41:39 +00:00
|
|
|
gst_uri_downloader_reset (demux->downloader);
|
2011-02-14 17:51:32 +00:00
|
|
|
break;
|
2014-04-08 20:31:32 +00:00
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
demux->adapter = gst_adapter_new ();
|
|
|
|
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) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
|
2011-02-15 02:41:43 +00:00
|
|
|
break;
|
2014-04-08 20:31:32 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
gst_object_unref (demux->adapter);
|
|
|
|
demux->adapter = NULL;
|
|
|
|
break;
|
2011-02-15 02:41:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static GstPad *
|
|
|
|
gst_hls_demux_create_pad (GstHLSDemux * hlsdemux)
|
2014-04-29 18:19:51 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
gchar *name;
|
|
|
|
GstPadTemplate *tmpl;
|
|
|
|
GstPad *pad;
|
2014-04-29 18:19:51 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
name = g_strdup_printf ("src_%u", hlsdemux->srcpad_counter++);
|
|
|
|
tmpl = gst_static_pad_template_get (&srctemplate);
|
|
|
|
pad = gst_ghost_pad_new_no_target_from_template (name, tmpl);
|
|
|
|
gst_object_unref (tmpl);
|
|
|
|
g_free (name);
|
2014-04-29 18:19:51 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return pad;
|
|
|
|
}
|
2014-06-19 07:56:09 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static guint64
|
|
|
|
gst_hls_demux_get_bitrate (GstHLSDemux * hlsdemux)
|
|
|
|
{
|
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (hlsdemux);
|
2014-04-29 18:19:51 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* Valid because hlsdemux only has a single output */
|
|
|
|
if (demux->streams) {
|
|
|
|
GstAdaptiveDemuxStream *stream = demux->streams->data;
|
|
|
|
return stream->current_download_rate;
|
2014-04-29 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return 0;
|
2014-04-29 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2011-04-13 21:06:18 +00:00
|
|
|
static gboolean
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
|
2011-04-13 21:06:18 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
|
|
|
gdouble rate;
|
|
|
|
GList *walk;
|
|
|
|
GstClockTime current_pos, target_pos;
|
|
|
|
gint64 current_sequence;
|
|
|
|
GstM3U8MediaFile *file;
|
|
|
|
guint64 bitrate;
|
|
|
|
|
|
|
|
gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
|
|
|
|
&stop_type, &stop);
|
|
|
|
|
|
|
|
bitrate = gst_hls_demux_get_bitrate (hlsdemux);
|
|
|
|
|
|
|
|
/* properly cleanup pending decryption status */
|
|
|
|
if (flags & GST_SEEK_FLAG_FLUSH) {
|
|
|
|
if (hlsdemux->adapter)
|
|
|
|
gst_adapter_clear (hlsdemux->adapter);
|
|
|
|
if (hlsdemux->pending_buffer)
|
|
|
|
gst_buffer_unref (hlsdemux->pending_buffer);
|
|
|
|
hlsdemux->pending_buffer = NULL;
|
|
|
|
gst_hls_demux_decrypt_end (hlsdemux);
|
|
|
|
}
|
2011-08-22 23:41:39 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* Use I-frame variants for trick modes */
|
|
|
|
if (hlsdemux->client->main->iframe_lists && rate < -1.0
|
|
|
|
&& demux->segment.rate >= -1.0 && demux->segment.rate <= 1.0) {
|
|
|
|
GError *err = NULL;
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (hlsdemux->client);
|
|
|
|
/* Switch to I-frame variant */
|
|
|
|
hlsdemux->client->main->current_variant =
|
|
|
|
hlsdemux->client->main->iframe_lists;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
|
|
|
|
gst_m3u8_client_set_current (hlsdemux->client,
|
|
|
|
hlsdemux->client->main->iframe_lists->data);
|
|
|
|
gst_uri_downloader_reset (hlsdemux->downloader);
|
|
|
|
if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
|
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
//hlsdemux->discont = TRUE;
|
|
|
|
hlsdemux->new_playlist = TRUE;
|
|
|
|
hlsdemux->do_typefind = TRUE;
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_change_playlist (hlsdemux, bitrate / ABS (rate), NULL);
|
|
|
|
} else if (rate > -1.0 && rate <= 1.0 && (demux->segment.rate < -1.0
|
|
|
|
|| demux->segment.rate > 1.0)) {
|
|
|
|
GError *err = NULL;
|
|
|
|
GST_M3U8_CLIENT_LOCK (hlsdemux->client);
|
|
|
|
/* Switch to normal variant */
|
|
|
|
hlsdemux->client->main->current_variant = hlsdemux->client->main->lists;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
|
|
|
|
gst_m3u8_client_set_current (hlsdemux->client,
|
|
|
|
hlsdemux->client->main->lists->data);
|
|
|
|
gst_uri_downloader_reset (hlsdemux->downloader);
|
|
|
|
if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
|
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
//hlsdemux->discont = TRUE;
|
|
|
|
hlsdemux->new_playlist = TRUE;
|
|
|
|
hlsdemux->do_typefind = TRUE;
|
|
|
|
/* TODO why not continue using the same? that was being used up to now? */
|
|
|
|
gst_hls_demux_change_playlist (hlsdemux, bitrate * hlsdemux->bitrate_limit,
|
|
|
|
NULL);
|
|
|
|
}
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (hlsdemux->client);
|
|
|
|
file = GST_M3U8_MEDIA_FILE (hlsdemux->client->current->files->data);
|
|
|
|
current_sequence = file->sequence;
|
|
|
|
current_pos = 0;
|
|
|
|
target_pos = rate > 0 ? start : stop;
|
|
|
|
/* FIXME: Here we need proper discont handling */
|
|
|
|
for (walk = hlsdemux->client->current->files; walk; walk = walk->next) {
|
|
|
|
file = walk->data;
|
|
|
|
|
|
|
|
current_sequence = file->sequence;
|
|
|
|
if (current_pos <= target_pos && target_pos < current_pos + file->duration) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_pos += file->duration;
|
|
|
|
}
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
|
2014-03-26 06:22:54 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
if (walk == NULL) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "seeking further than track duration");
|
|
|
|
current_sequence++;
|
|
|
|
}
|
2014-03-09 18:31:31 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (hlsdemux->client);
|
|
|
|
GST_DEBUG_OBJECT (demux, "seeking to sequence %u", (guint) current_sequence);
|
|
|
|
hlsdemux->client->sequence = current_sequence;
|
|
|
|
hlsdemux->client->sequence_position = current_pos;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
|
2014-03-09 18:31:31 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2014-03-09 18:31:31 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_hls_demux_update_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
|
|
|
if (!gst_hls_demux_update_playlist (hlsdemux, TRUE, NULL))
|
|
|
|
return GST_FLOW_ERROR;
|
2014-04-28 23:01:48 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2014-03-09 18:31:31 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
2014-03-09 18:31:31 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* only 1 output supported */
|
|
|
|
gst_adaptive_demux_stream_new (demux, gst_hls_demux_create_pad (hlsdemux));
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2011-09-02 23:49:38 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
|
|
|
gchar *playlist = NULL;
|
2014-03-06 22:14:10 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_set_location (hlsdemux, demux->manifest_uri,
|
|
|
|
demux->manifest_base_uri);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
playlist = gst_hls_src_buf_to_utf8_playlist (buf);
|
|
|
|
if (playlist == NULL) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Error validating first playlist.");
|
|
|
|
return FALSE;
|
|
|
|
} else if (!gst_m3u8_client_update (hlsdemux->client, playlist)) {
|
|
|
|
/* In most cases, this will happen if we set a wrong url in the
|
|
|
|
* source element and we have received the 404 HTML response instead of
|
|
|
|
* the playlist */
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."), (NULL));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this playlist is a variant playlist, select the first one
|
|
|
|
* and update it */
|
|
|
|
if (gst_m3u8_client_has_variant_playlist (hlsdemux->client)) {
|
|
|
|
GstM3U8 *child = NULL;
|
|
|
|
GError *err = NULL;
|
2014-02-17 08:19:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* TODO seems like something that could be simplified */
|
|
|
|
if (hlsdemux->connection_speed == 0) {
|
|
|
|
GST_M3U8_CLIENT_LOCK (hlsdemux->client);
|
|
|
|
child = hlsdemux->client->main->current_variant->data;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
|
|
|
|
} else {
|
|
|
|
GList *tmp = gst_m3u8_client_get_playlist_for_bitrate (hlsdemux->client,
|
|
|
|
hlsdemux->connection_speed);
|
2011-08-16 19:46:49 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
child = GST_M3U8 (tmp->data);
|
2014-05-01 13:59:28 +00:00
|
|
|
}
|
2014-08-27 19:26:19 +00:00
|
|
|
|
|
|
|
gst_m3u8_client_set_current (hlsdemux->client, child);
|
|
|
|
if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
|
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (demux, "Could not fetch the child playlist",
|
|
|
|
err);
|
|
|
|
return FALSE;
|
2011-08-16 19:46:49 +00:00
|
|
|
}
|
2011-04-13 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return gst_hls_demux_setup_streams (demux);
|
2011-04-13 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static GstClockTime
|
|
|
|
gst_hls_demux_get_duration (GstAdaptiveDemux * demux)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return gst_m3u8_client_get_duration (hlsdemux->client);
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_is_live (GstAdaptiveDemux * demux)
|
|
|
|
{
|
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return gst_m3u8_client_is_live (hlsdemux->client);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 11:50:25 +00:00
|
|
|
static gboolean
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream)
|
2011-03-12 11:50:25 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
2011-03-12 11:50:25 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
if (hlsdemux->current_key) {
|
|
|
|
GError *err = NULL;
|
|
|
|
GstFragment *key_fragment;
|
|
|
|
GstBuffer *key_buffer;
|
|
|
|
GstMapInfo key_info;
|
|
|
|
|
|
|
|
/* new key? */
|
|
|
|
if (hlsdemux->key_url
|
|
|
|
&& strcmp (hlsdemux->key_url, hlsdemux->current_key) == 0) {
|
|
|
|
key_fragment = g_object_ref (hlsdemux->key_fragment);
|
|
|
|
} else {
|
|
|
|
g_free (hlsdemux->key_url);
|
|
|
|
hlsdemux->key_url = NULL;
|
|
|
|
|
|
|
|
if (hlsdemux->key_fragment)
|
|
|
|
g_object_unref (hlsdemux->key_fragment);
|
|
|
|
hlsdemux->key_fragment = NULL;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "Fetching key %s", hlsdemux->current_key);
|
|
|
|
key_fragment =
|
|
|
|
gst_uri_downloader_fetch_uri (hlsdemux->downloader,
|
|
|
|
hlsdemux->current_key, hlsdemux->client->main ?
|
|
|
|
hlsdemux->client->main->uri : NULL, FALSE, FALSE,
|
|
|
|
hlsdemux->client->current ? hlsdemux->client->current->
|
|
|
|
allowcache : TRUE, &err);
|
|
|
|
if (key_fragment == NULL)
|
|
|
|
goto key_failed;
|
|
|
|
hlsdemux->key_url = g_strdup (hlsdemux->current_key);
|
|
|
|
hlsdemux->key_fragment = g_object_ref (key_fragment);
|
|
|
|
}
|
2011-03-12 11:50:25 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
key_buffer = gst_fragment_get_buffer (key_fragment);
|
|
|
|
gst_buffer_map (key_buffer, &key_info, GST_MAP_READ);
|
2011-03-12 11:50:25 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_decrypt_start (hlsdemux, key_info.data, hlsdemux->current_iv);
|
2011-03-12 11:50:25 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_buffer_unmap (key_buffer, &key_info);
|
|
|
|
gst_buffer_unref (key_buffer);
|
|
|
|
g_object_unref (key_fragment);
|
2011-03-12 11:50:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
key_failed:
|
|
|
|
/* TODO Raise this error to the user */
|
|
|
|
GST_WARNING_OBJECT (demux, "Failed to decrypt data");
|
|
|
|
return FALSE;
|
2011-03-12 11:50:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static void
|
|
|
|
gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstBuffer ** buffer)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
if (hlsdemux->current_key)
|
|
|
|
gst_hls_demux_decrypt_end (hlsdemux);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* ideally this should be empty, but this eos might have been
|
|
|
|
* caused by an error on the source element */
|
|
|
|
GST_DEBUG_OBJECT (demux, "Data still on the adapter when EOS was received"
|
|
|
|
": %" G_GSIZE_FORMAT, gst_adapter_available (hlsdemux->adapter));
|
|
|
|
gst_adapter_clear (hlsdemux->adapter);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* pending buffer is only used for encrypted streams */
|
|
|
|
if (stream->last_ret == GST_FLOW_OK) {
|
|
|
|
if (hlsdemux->pending_buffer) {
|
|
|
|
GstMapInfo info;
|
|
|
|
gsize unpadded_size;
|
2012-10-15 07:35:16 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* Handle pkcs7 unpadding here */
|
|
|
|
gst_buffer_map (hlsdemux->pending_buffer, &info, GST_MAP_READ);
|
|
|
|
unpadded_size = info.size - info.data[info.size - 1];
|
|
|
|
gst_buffer_unmap (hlsdemux->pending_buffer, &info);
|
2012-10-15 07:35:16 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_buffer_resize (hlsdemux->pending_buffer, 0, unpadded_size);
|
2011-02-15 03:39:34 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
*buffer = hlsdemux->pending_buffer;
|
|
|
|
hlsdemux->pending_buffer = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hlsdemux->pending_buffer)
|
|
|
|
gst_buffer_unref (hlsdemux->pending_buffer);
|
|
|
|
hlsdemux->pending_buffer = NULL;
|
2012-10-15 07:24:01 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:57:26 +00:00
|
|
|
static GstFlowReturn
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_chunk_received (GstAdaptiveDemux * demux,
|
|
|
|
GstAdaptiveDemuxStream * stream, GstBuffer ** chunk)
|
2014-04-07 16:57:26 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
|
|
|
GstBuffer *buffer = *chunk;
|
2014-04-07 16:57:26 +00:00
|
|
|
|
2014-04-08 20:31:32 +00:00
|
|
|
/* Is it encrypted? */
|
2014-08-27 19:26:19 +00:00
|
|
|
if (hlsdemux->current_key) {
|
2014-04-08 20:31:32 +00:00
|
|
|
GError *err = NULL;
|
|
|
|
GstBuffer *tmp_buffer;
|
|
|
|
gsize available;
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_adapter_push (hlsdemux->adapter, buffer);
|
|
|
|
*chunk = NULL;
|
2014-04-08 20:31:32 +00:00
|
|
|
|
|
|
|
/* must be a multiple of 16 */
|
2014-08-27 19:26:19 +00:00
|
|
|
available = gst_adapter_available (hlsdemux->adapter) & (~0xF);
|
2014-04-08 20:31:32 +00:00
|
|
|
|
|
|
|
if (available == 0) {
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
buffer = gst_adapter_take_buffer (hlsdemux->adapter, available);
|
|
|
|
buffer = gst_hls_demux_decrypt_fragment (hlsdemux, buffer, &err);
|
2014-04-08 20:31:32 +00:00
|
|
|
if (buffer == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Failed to decrypt buffer"),
|
|
|
|
("decryption failed %s", err->message));
|
|
|
|
g_error_free (err);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
tmp_buffer = hlsdemux->pending_buffer;
|
|
|
|
hlsdemux->pending_buffer = buffer;
|
|
|
|
*chunk = tmp_buffer;
|
2014-11-24 19:18:36 +00:00
|
|
|
} else if (hlsdemux->pending_buffer) {
|
|
|
|
*chunk = gst_buffer_append (hlsdemux->pending_buffer, buffer);
|
|
|
|
hlsdemux->pending_buffer = NULL;
|
2014-04-08 20:31:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:18:36 +00:00
|
|
|
buffer = *chunk;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (hlsdemux->do_typefind && buffer != NULL)) {
|
2014-12-30 11:07:18 +00:00
|
|
|
GstCaps *caps = NULL;
|
|
|
|
GstMapInfo info;
|
|
|
|
guint buffer_size;
|
|
|
|
GstTypeFindProbability prob = GST_TYPE_FIND_NONE;
|
|
|
|
|
|
|
|
gst_buffer_map (buffer, &info, GST_MAP_READ);
|
|
|
|
buffer_size = info.size;
|
|
|
|
|
|
|
|
/* Typefind could miss if buffer is too small. In this case we
|
|
|
|
* will retry later */
|
|
|
|
if (buffer_size >= (2 * 1024)) {
|
|
|
|
caps =
|
|
|
|
gst_type_find_helper_for_data (GST_OBJECT_CAST (hlsdemux), info.data,
|
|
|
|
info.size, &prob);
|
|
|
|
}
|
|
|
|
gst_buffer_unmap (buffer, &info);
|
2014-05-01 13:29:37 +00:00
|
|
|
|
2014-04-25 13:40:50 +00:00
|
|
|
if (G_UNLIKELY (!caps)) {
|
2014-12-30 11:07:18 +00:00
|
|
|
/* Only fail typefinding if we already a good amount of data
|
|
|
|
* and we still don't know the type */
|
|
|
|
if (buffer_size > (2 * 1024 * 1024)) {
|
|
|
|
GST_ELEMENT_ERROR (hlsdemux, STREAM, TYPE_NOT_FOUND,
|
|
|
|
("Could not determine type of stream"), (NULL));
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
} else {
|
2014-11-24 19:18:36 +00:00
|
|
|
if (hlsdemux->pending_buffer)
|
|
|
|
hlsdemux->pending_buffer =
|
|
|
|
gst_buffer_append (buffer, hlsdemux->pending_buffer);
|
|
|
|
else
|
|
|
|
hlsdemux->pending_buffer = buffer;
|
|
|
|
*chunk = NULL;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2014-04-25 13:40:50 +00:00
|
|
|
}
|
2014-12-30 11:07:18 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (hlsdemux, "Typefind result: %" GST_PTR_FORMAT " prob:%d",
|
|
|
|
caps, prob);
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
if (!hlsdemux->input_caps
|
|
|
|
|| !gst_caps_is_equal (caps, hlsdemux->input_caps)) {
|
|
|
|
gst_caps_replace (&hlsdemux->input_caps, caps);
|
2014-04-25 13:40:50 +00:00
|
|
|
GST_INFO_OBJECT (demux, "Input source caps: %" GST_PTR_FORMAT,
|
2014-08-27 19:26:19 +00:00
|
|
|
hlsdemux->input_caps);
|
2014-04-25 13:40:50 +00:00
|
|
|
}
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_adaptive_demux_stream_set_caps (stream, caps);
|
|
|
|
hlsdemux->do_typefind = FALSE;
|
2014-04-07 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return GST_FLOW_OK;
|
2014-09-17 20:27:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream * stream)
|
2014-09-17 20:27:25 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
|
2014-09-17 20:27:25 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_m3u8_client_advance_fragment (hlsdemux->client,
|
|
|
|
stream->demux->segment.rate > 0);
|
|
|
|
return GST_FLOW_OK;
|
2014-09-17 20:27:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream * stream)
|
2011-07-26 15:02:05 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
|
|
|
|
gchar *next_fragment_uri;
|
|
|
|
GstClockTime duration;
|
|
|
|
GstClockTime timestamp;
|
|
|
|
gboolean discont;
|
|
|
|
gint64 range_start, range_end;
|
|
|
|
gchar *key = NULL;
|
|
|
|
guint8 *iv = NULL;
|
2014-04-07 16:57:26 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
if (!gst_m3u8_client_get_next_fragment (hlsdemux->client, &discont,
|
|
|
|
&next_fragment_uri, &duration, ×tamp, &range_start, &range_end,
|
|
|
|
&key, &iv, stream->demux->segment.rate > 0)) {
|
|
|
|
GST_INFO_OBJECT (hlsdemux, "This playlist doesn't contain more fragments");
|
|
|
|
return GST_FLOW_EOS;
|
2013-07-23 08:07:24 +00:00
|
|
|
}
|
2012-08-06 11:59:27 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
/* set up our source for download */
|
|
|
|
stream->fragment.timestamp = timestamp;
|
|
|
|
stream->fragment.duration = duration;
|
|
|
|
if (hlsdemux->current_key)
|
|
|
|
g_free (hlsdemux->current_key);
|
|
|
|
hlsdemux->current_key = key;
|
|
|
|
if (hlsdemux->current_iv)
|
|
|
|
g_free (hlsdemux->current_iv);
|
|
|
|
hlsdemux->current_iv = iv;
|
|
|
|
g_free (stream->fragment.uri);
|
|
|
|
stream->fragment.uri = next_fragment_uri;
|
|
|
|
stream->fragment.range_start = range_start;
|
|
|
|
stream->fragment.range_end = range_end;
|
|
|
|
if (discont)
|
|
|
|
stream->discont = discont;
|
2011-07-26 15:02:05 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return GST_FLOW_OK;
|
2014-02-27 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream, guint64 bitrate)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (stream->demux);
|
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
|
|
|
|
gboolean changed = FALSE;
|
2014-03-30 16:12:07 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (hlsdemux->client);
|
|
|
|
if (!hlsdemux->client->main->lists) {
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
|
|
|
|
return FALSE;
|
2014-02-17 08:19:32 +00:00
|
|
|
}
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-07 13:25:32 +00:00
|
|
|
/* FIXME: Currently several issues have be found when letting bitrate adaptation
|
|
|
|
* happen using trick modes (such as 'All streams finished without buffers') and
|
|
|
|
* the adaptive algorithm does not properly behave. */
|
2014-08-27 19:26:19 +00:00
|
|
|
if (demux->segment.rate != 1.0)
|
|
|
|
return FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_change_playlist (hlsdemux, bitrate * hlsdemux->bitrate_limit,
|
|
|
|
&changed);
|
|
|
|
if (changed)
|
|
|
|
gst_hls_demux_setup_streams (GST_ADAPTIVE_DEMUX_CAST (hlsdemux));
|
|
|
|
return changed;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_reset (GstAdaptiveDemux * ademux)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *demux = GST_HLS_DEMUX_CAST (ademux);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
demux->do_typefind = TRUE;
|
2014-02-17 08:19:32 +00:00
|
|
|
|
2014-02-12 09:59:38 +00:00
|
|
|
g_free (demux->key_url);
|
|
|
|
demux->key_url = NULL;
|
|
|
|
|
|
|
|
if (demux->key_fragment)
|
|
|
|
g_object_unref (demux->key_fragment);
|
|
|
|
demux->key_fragment = NULL;
|
|
|
|
|
2011-02-15 20:49:20 +00:00
|
|
|
if (demux->input_caps) {
|
|
|
|
gst_caps_unref (demux->input_caps);
|
|
|
|
demux->input_caps = 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;
|
|
|
|
}
|
2014-08-27 19:26:19 +00:00
|
|
|
/* TODO recreated on hls only if reset was not for disposing */
|
|
|
|
demux->client = gst_m3u8_client_new ("", NULL);
|
2014-02-12 17:27:21 +00:00
|
|
|
|
|
|
|
demux->srcpad_counter = 0;
|
2014-04-08 20:31:32 +00:00
|
|
|
if (demux->adapter)
|
|
|
|
gst_adapter_clear (demux->adapter);
|
|
|
|
if (demux->pending_buffer)
|
|
|
|
gst_buffer_unref (demux->pending_buffer);
|
|
|
|
demux->pending_buffer = NULL;
|
2014-10-02 16:37:57 +00:00
|
|
|
if (demux->current_key) {
|
|
|
|
g_free (demux->current_key);
|
|
|
|
demux->current_key = NULL;
|
|
|
|
}
|
|
|
|
if (demux->current_iv) {
|
|
|
|
g_free (demux->current_iv);
|
|
|
|
demux->current_iv = NULL;
|
|
|
|
}
|
2014-04-08 20:31:32 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_decrypt_end (demux);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-05-28 08:19:40 +00:00
|
|
|
gst_hls_demux_set_location (GstHLSDemux * demux, const gchar * uri,
|
|
|
|
const gchar * base_uri)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
|
|
|
if (demux->client)
|
|
|
|
gst_m3u8_client_free (demux->client);
|
2014-05-28 08:19:40 +00:00
|
|
|
demux->client = gst_m3u8_client_new (uri, base_uri);
|
|
|
|
GST_INFO_OBJECT (demux, "Changed location: %s (base uri: %s)", uri,
|
|
|
|
GST_STR_NULL (base_uri));
|
2011-02-14 17:51:32 +00:00
|
|
|
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))
|
2013-10-25 10:11:30 +00:00
|
|
|
goto map_error;
|
2011-04-01 23:21:34 +00:00
|
|
|
|
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);
|
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);
|
2013-10-25 10:11:30 +00:00
|
|
|
map_error:
|
2012-04-12 12:17:14 +00:00
|
|
|
return NULL;
|
2011-04-01 23:21:34 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean
|
2014-02-11 13:57:16 +00:00
|
|
|
gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update,
|
|
|
|
GError ** err)
|
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;
|
2014-05-30 22:34:18 +00:00
|
|
|
gboolean main_checked = FALSE, updated = FALSE;
|
2014-10-02 16:37:57 +00:00
|
|
|
gchar *uri, *main_uri;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-05-30 22:34:18 +00:00
|
|
|
retry:
|
|
|
|
uri = gst_m3u8_client_get_current_uri (demux->client);
|
2014-10-02 16:37:57 +00:00
|
|
|
main_uri = gst_m3u8_client_get_uri (demux->client);
|
2014-04-28 07:32:54 +00:00
|
|
|
download =
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_uri_downloader_fetch_uri (demux->downloader, uri, main_uri,
|
|
|
|
TRUE, TRUE, TRUE, err);
|
2014-10-02 16:37:57 +00:00
|
|
|
g_free (main_uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
if (download == NULL) {
|
|
|
|
if (update && !main_checked
|
|
|
|
&& gst_m3u8_client_has_variant_playlist (demux->client)
|
2014-10-02 16:37:57 +00:00
|
|
|
&& gst_m3u8_client_has_main (demux->client)) {
|
2014-05-30 22:34:18 +00:00
|
|
|
GError *err2 = NULL;
|
2014-10-02 16:37:57 +00:00
|
|
|
main_uri = gst_m3u8_client_get_uri (demux->client);
|
2014-05-30 22:34:18 +00:00
|
|
|
GST_INFO_OBJECT (demux,
|
|
|
|
"Updating playlist %s failed, attempt to refresh variant playlist %s",
|
2014-10-02 16:37:57 +00:00
|
|
|
uri, main_uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
download =
|
|
|
|
gst_uri_downloader_fetch_uri (demux->downloader,
|
2014-10-02 16:37:57 +00:00
|
|
|
main_uri, NULL, TRUE, TRUE, TRUE, &err2);
|
|
|
|
g_free (main_uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
g_clear_error (&err2);
|
|
|
|
if (download != NULL) {
|
|
|
|
gchar *base_uri;
|
|
|
|
|
|
|
|
buf = gst_fragment_get_buffer (download);
|
|
|
|
playlist = gst_hls_src_buf_to_utf8_playlist (buf);
|
|
|
|
|
|
|
|
if (playlist == NULL) {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Failed to validate variant playlist encoding");
|
2014-08-27 19:26:19 +00:00
|
|
|
g_free (uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:37:57 +00:00
|
|
|
g_free (uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
if (download->redirect_permanent && download->redirect_uri) {
|
|
|
|
uri = download->redirect_uri;
|
|
|
|
base_uri = NULL;
|
|
|
|
} else {
|
|
|
|
uri = download->uri;
|
|
|
|
base_uri = download->redirect_uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_m3u8_client_update_variant_playlist (demux->client, playlist,
|
|
|
|
uri, base_uri)) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Failed to update the variant playlist");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (download);
|
|
|
|
|
|
|
|
g_clear_error (err);
|
|
|
|
main_checked = TRUE;
|
|
|
|
goto retry;
|
|
|
|
} else {
|
2014-10-02 16:37:57 +00:00
|
|
|
g_free (uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2014-10-02 16:37:57 +00:00
|
|
|
g_free (uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-10-02 16:37:57 +00:00
|
|
|
g_free (uri);
|
|
|
|
|
2014-05-28 08:19:40 +00:00
|
|
|
/* Set the base URI of the playlist to the redirect target if any */
|
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
|
|
|
g_free (demux->client->current->uri);
|
|
|
|
g_free (demux->client->current->base_uri);
|
2014-05-28 10:44:38 +00:00
|
|
|
if (download->redirect_permanent && download->redirect_uri) {
|
2014-05-28 08:19:40 +00:00
|
|
|
demux->client->current->uri = g_strdup (download->redirect_uri);
|
|
|
|
demux->client->current->base_uri = NULL;
|
|
|
|
} else {
|
|
|
|
demux->client->current->uri = g_strdup (download->uri);
|
|
|
|
demux->client->current->base_uri = g_strdup (download->redirect_uri);
|
|
|
|
}
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
|
|
|
|
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);
|
2014-08-27 19:26:19 +00:00
|
|
|
g_object_unref (download);
|
2012-03-15 18:42:44 +00:00
|
|
|
|
2011-04-01 23:21:34 +00:00
|
|
|
if (playlist == NULL) {
|
2014-02-11 13:57:16 +00:00
|
|
|
GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
|
|
|
|
g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
|
|
|
|
"Couldn't validate playlist encoding");
|
2011-04-01 23:21:34 +00:00
|
|
|
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);
|
2014-02-11 13:57:16 +00:00
|
|
|
if (!updated) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Couldn't update playlist");
|
|
|
|
g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
|
|
|
|
"Couldn't update playlist");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-05-08 15:33:50 +00:00
|
|
|
|
2014-04-10 12:59:57 +00:00
|
|
|
/* If it's a live source, do not let the sequence number go beyond
|
2012-05-08 15:33:50 +00:00
|
|
|
* three fragments before the end of the list */
|
2014-04-10 12:46:01 +00:00
|
|
|
if (update == FALSE && demux->client->current &&
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_m3u8_client_is_live (demux->client)) {
|
2014-03-09 18:31:31 +00:00
|
|
|
gint64 last_sequence;
|
2012-05-08 15:33:50 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
2012-05-08 15:33:50 +00:00
|
|
|
last_sequence =
|
2014-05-30 22:34:18 +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) {
|
2014-03-09 18:31:31 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Sequence is beyond playlist. Moving back to %u",
|
|
|
|
(guint) (last_sequence - 3));
|
2014-08-27 19:26:19 +00:00
|
|
|
//demux->need_segment = TRUE;
|
2012-05-08 15:33:50 +00:00
|
|
|
demux->client->sequence = last_sequence - 3;
|
|
|
|
}
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
|
|
|
} else if (demux->client->current && !gst_m3u8_client_is_live (demux->client)) {
|
2014-04-10 12:46:01 +00:00
|
|
|
GstClockTime current_pos, target_pos;
|
|
|
|
guint sequence = 0;
|
|
|
|
GList *walk;
|
|
|
|
|
|
|
|
/* Sequence numbers are not guaranteed to be the same in different
|
|
|
|
* playlists, so get the correct fragment here based on the current
|
|
|
|
* position
|
|
|
|
*/
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
|
|
|
|
|
|
|
/* Valid because hlsdemux only has a single output */
|
|
|
|
if (GST_ADAPTIVE_DEMUX_CAST (demux)->streams) {
|
|
|
|
GstAdaptiveDemuxStream *stream =
|
|
|
|
GST_ADAPTIVE_DEMUX_CAST (demux)->streams->data;
|
|
|
|
target_pos = stream->segment.position;
|
|
|
|
} else {
|
|
|
|
target_pos = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-10 12:46:01 +00:00
|
|
|
current_pos = 0;
|
|
|
|
for (walk = demux->client->current->files; walk; walk = walk->next) {
|
|
|
|
GstM3U8MediaFile *file = walk->data;
|
|
|
|
|
|
|
|
sequence = file->sequence;
|
|
|
|
if (current_pos <= target_pos
|
|
|
|
&& target_pos < current_pos + file->duration) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_pos += file->duration;
|
|
|
|
}
|
|
|
|
/* End of playlist */
|
|
|
|
if (!walk)
|
|
|
|
sequence++;
|
|
|
|
demux->client->sequence = sequence;
|
2014-04-10 14:53:42 +00:00
|
|
|
demux->client->sequence_position = current_pos;
|
2014-08-27 19:26:19 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
2012-05-08 15:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-08-27 19:26:19 +00:00
|
|
|
gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate,
|
|
|
|
gboolean * changed)
|
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;
|
2014-08-27 19:26:19 +00:00
|
|
|
GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX_CAST (demux);
|
|
|
|
GstAdaptiveDemuxStream *stream;
|
|
|
|
|
|
|
|
g_return_val_if_fail (adaptive_demux->streams != NULL, FALSE);
|
|
|
|
|
|
|
|
stream = adaptive_demux->streams->data;
|
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
|
|
|
|
2014-09-18 16:16:59 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (demux->client);
|
|
|
|
|
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) {
|
2014-09-18 16:16:59 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (demux->client);
|
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);
|
2014-08-27 19:26:19 +00:00
|
|
|
stream->discont = TRUE;
|
2014-04-29 18:17:00 +00:00
|
|
|
demux->new_playlist = TRUE;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-02-11 13:57:16 +00:00
|
|
|
if (gst_hls_demux_update_playlist (demux, FALSE, NULL)) {
|
2014-08-27 19:26:19 +00:00
|
|
|
gchar *uri;
|
|
|
|
gchar *main_uri;
|
2014-10-02 16:37:57 +00:00
|
|
|
uri = gst_m3u8_client_get_current_uri (demux->client);
|
|
|
|
main_uri = gst_m3u8_client_get_uri (demux->client);
|
2011-09-16 00:47:54 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
2014-07-18 13:09:54 +00:00
|
|
|
gst_message_new_element (GST_OBJECT_CAST (demux),
|
|
|
|
gst_structure_new (STATISTICS_MESSAGE_NAME,
|
2014-08-27 19:26:19 +00:00
|
|
|
"manifest-uri", G_TYPE_STRING,
|
|
|
|
main_uri, "uri", G_TYPE_STRING,
|
|
|
|
uri, "bitrate", G_TYPE_INT, new_bandwidth, NULL)));
|
2014-10-02 16:37:57 +00:00
|
|
|
g_free (uri);
|
|
|
|
g_free (main_uri);
|
2014-08-27 19:26:19 +00:00
|
|
|
if (changed)
|
|
|
|
*changed = TRUE;
|
2011-09-16 00:47:54 +00:00
|
|
|
} 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) */
|
2014-03-09 18:31:31 +00:00
|
|
|
if (GST_M3U8 (previous_variant->data)->iframe && new_bandwidth ==
|
2014-05-30 22:34:18 +00:00
|
|
|
GST_M3U8 (g_list_first (demux->client->main->iframe_lists)->data)->
|
|
|
|
bandwidth)
|
2014-03-09 18:31:31 +00:00
|
|
|
return FALSE;
|
|
|
|
else if (!GST_M3U8 (previous_variant->data)->iframe && new_bandwidth ==
|
2012-05-08 17:04:35 +00:00
|
|
|
GST_M3U8 (g_list_first (demux->client->main->lists)->data)->bandwidth)
|
|
|
|
return FALSE;
|
|
|
|
else
|
2014-08-27 19:26:19 +00:00
|
|
|
return gst_hls_demux_change_playlist (demux, new_bandwidth - 1, changed);
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-22 21:18:59 +00:00
|
|
|
#if defined(HAVE_OPENSSL)
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
|
|
|
|
const guint8 * iv_data)
|
|
|
|
{
|
|
|
|
EVP_CIPHER_CTX_init (&demux->aes_ctx);
|
|
|
|
if (!EVP_DecryptInit_ex (&demux->aes_ctx, EVP_aes_128_cbc (), NULL, key_data,
|
|
|
|
iv_data))
|
|
|
|
return FALSE;
|
|
|
|
EVP_CIPHER_CTX_set_padding (&demux->aes_ctx, 0);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
decrypt_fragment (GstHLSDemux * demux, gsize length,
|
|
|
|
const guint8 * encrypted_data, guint8 * decrypted_data)
|
|
|
|
{
|
|
|
|
int len, flen = 0;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (length > G_MAXINT || length % 16 != 0))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
len = (int) length;
|
|
|
|
if (!EVP_DecryptUpdate (&demux->aes_ctx, decrypted_data, &len, encrypted_data,
|
|
|
|
len))
|
|
|
|
return FALSE;
|
|
|
|
EVP_DecryptFinal_ex (&demux->aes_ctx, decrypted_data + len, &flen);
|
|
|
|
g_return_val_if_fail (len + flen == length, FALSE);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_decrypt_end (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
EVP_CIPHER_CTX_cleanup (&demux->aes_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_NETTLE)
|
2014-02-09 17:49:49 +00:00
|
|
|
static gboolean
|
2014-04-08 20:31:32 +00:00
|
|
|
gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
|
|
|
|
const guint8 * iv_data)
|
2014-02-09 17:49:49 +00:00
|
|
|
{
|
2014-04-08 20:31:32 +00:00
|
|
|
aes_set_decrypt_key (&demux->aes_ctx.ctx, 16, key_data);
|
|
|
|
CBC_SET_IV (&demux->aes_ctx, iv_data);
|
2014-02-09 17:49:49 +00:00
|
|
|
|
2014-04-08 20:31:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
decrypt_fragment (GstHLSDemux * demux, gsize length,
|
|
|
|
const guint8 * encrypted_data, guint8 * decrypted_data)
|
|
|
|
{
|
2014-02-09 17:49:49 +00:00
|
|
|
if (length % 16 != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-04-08 20:31:32 +00:00
|
|
|
CBC_DECRYPT (&demux->aes_ctx, aes_decrypt, length, decrypted_data,
|
|
|
|
encrypted_data);
|
2014-02-09 17:49:49 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2014-04-08 20:31:32 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_decrypt_end (GstHLSDemux * demux)
|
|
|
|
{
|
|
|
|
/* NOP */
|
|
|
|
}
|
|
|
|
|
2014-02-09 17:49:49 +00:00
|
|
|
#else
|
|
|
|
static gboolean
|
2014-04-08 20:31:32 +00:00
|
|
|
gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
|
|
|
|
const guint8 * iv_data)
|
2014-02-09 17:49:49 +00:00
|
|
|
{
|
|
|
|
gcry_error_t err = 0;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
err =
|
2014-04-08 20:31:32 +00:00
|
|
|
gcry_cipher_open (&demux->aes_ctx, GCRY_CIPHER_AES128,
|
|
|
|
GCRY_CIPHER_MODE_CBC, 0);
|
2014-02-09 17:49:49 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2014-04-08 20:31:32 +00:00
|
|
|
err = gcry_cipher_setkey (demux->aes_ctx, key_data, 16);
|
2014-02-09 17:49:49 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2014-04-08 20:31:32 +00:00
|
|
|
err = gcry_cipher_setiv (demux->aes_ctx, iv_data, 16);
|
|
|
|
if (!err)
|
|
|
|
ret = TRUE;
|
2014-02-09 17:49:49 +00:00
|
|
|
|
|
|
|
out:
|
2014-04-08 20:31:32 +00:00
|
|
|
if (!ret)
|
|
|
|
if (demux->aes_ctx)
|
|
|
|
gcry_cipher_close (demux->aes_ctx);
|
2014-02-09 17:49:49 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-08 20:31:32 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
decrypt_fragment (GstHLSDemux * demux, gsize length,
|
|
|
|
const guint8 * encrypted_data, guint8 * decrypted_data)
|
|
|
|
{
|
|
|
|
gcry_error_t err = 0;
|
|
|
|
|
|
|
|
err = gcry_cipher_decrypt (demux->aes_ctx, decrypted_data, length,
|
|
|
|
encrypted_data, length);
|
|
|
|
|
|
|
|
return err == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_hls_demux_decrypt_end (GstHLSDemux * demux)
|
|
|
|
{
|
2014-04-28 23:01:48 +00:00
|
|
|
if (demux->aes_ctx) {
|
2014-04-08 20:31:32 +00:00
|
|
|
gcry_cipher_close (demux->aes_ctx);
|
2014-04-28 23:01:48 +00:00
|
|
|
demux->aes_ctx = NULL;
|
|
|
|
}
|
2014-04-08 20:31:32 +00:00
|
|
|
}
|
2014-02-09 17:49:49 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-08 20:31:32 +00:00
|
|
|
static GstBuffer *
|
2013-03-19 07:49:21 +00:00
|
|
|
gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
|
2014-04-08 20:31:32 +00:00
|
|
|
GstBuffer * encrypted_buffer, GError ** err)
|
2013-03-19 07:49:21 +00:00
|
|
|
{
|
2014-04-08 20:31:32 +00:00
|
|
|
GstBuffer *decrypted_buffer = NULL;
|
|
|
|
GstMapInfo encrypted_info, decrypted_info;
|
2013-03-19 07:49:21 +00:00
|
|
|
|
|
|
|
decrypted_buffer =
|
|
|
|
gst_buffer_new_allocate (NULL, gst_buffer_get_size (encrypted_buffer),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gst_buffer_map (encrypted_buffer, &encrypted_info, GST_MAP_READ);
|
|
|
|
gst_buffer_map (decrypted_buffer, &decrypted_info, GST_MAP_WRITE);
|
|
|
|
|
2014-02-09 17:49:49 +00:00
|
|
|
if (!decrypt_fragment (demux, encrypted_info.size,
|
2014-04-08 20:31:32 +00:00
|
|
|
encrypted_info.data, decrypted_info.data))
|
2014-02-09 17:49:49 +00:00
|
|
|
goto decrypt_error;
|
2013-03-19 07:49:21 +00:00
|
|
|
|
2013-07-23 11:29:18 +00:00
|
|
|
|
2013-03-19 07:49:21 +00:00
|
|
|
gst_buffer_unmap (decrypted_buffer, &decrypted_info);
|
|
|
|
gst_buffer_unmap (encrypted_buffer, &encrypted_info);
|
2014-02-09 17:09:36 +00:00
|
|
|
|
2014-04-25 13:39:19 +00:00
|
|
|
gst_buffer_unref (encrypted_buffer);
|
|
|
|
|
|
|
|
return decrypted_buffer;
|
|
|
|
|
2014-02-09 17:49:49 +00:00
|
|
|
decrypt_error:
|
|
|
|
GST_ERROR_OBJECT (demux, "Failed to decrypt fragment");
|
2014-02-11 13:57:16 +00:00
|
|
|
g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_DECRYPT,
|
|
|
|
"Failed to decrypt fragment");
|
2014-02-09 17:09:36 +00:00
|
|
|
|
|
|
|
gst_buffer_unmap (decrypted_buffer, &decrypted_info);
|
|
|
|
gst_buffer_unmap (encrypted_buffer, &encrypted_info);
|
|
|
|
|
2014-02-12 09:58:45 +00:00
|
|
|
gst_buffer_unref (encrypted_buffer);
|
|
|
|
gst_buffer_unref (decrypted_buffer);
|
|
|
|
|
2014-04-25 13:39:19 +00:00
|
|
|
return NULL;
|
2013-03-19 07:49:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
static gint64
|
|
|
|
gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
|
2014-04-16 23:02:35 +00:00
|
|
|
{
|
2014-08-27 19:26:19 +00:00
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
2014-04-16 23:02:35 +00:00
|
|
|
|
2014-08-27 19:26:19 +00:00
|
|
|
return gst_util_uint64_scale (gst_m3u8_client_get_target_duration
|
|
|
|
(hlsdemux->client), G_USEC_PER_SEC, GST_SECOND);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
2014-04-04 15:45:51 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
|
|
|
|
gint64 * stop)
|
|
|
|
{
|
|
|
|
GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
|
|
|
|
|
|
|
|
return gst_m3u8_client_get_seek_range (hlsdemux->client, start, stop);
|
|
|
|
}
|