2011-02-14 17:51:32 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 Marc-Andre Lureau <marcandre.lureau@gmail.com>
|
|
|
|
*
|
|
|
|
* m3u8.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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2012-02-28 15:40:31 +00:00
|
|
|
#include <math.h>
|
2011-02-14 17:51:32 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <glib.h>
|
2013-03-18 06:34:13 +00:00
|
|
|
#include <string.h>
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
#include "gstfragmented.h"
|
|
|
|
#include "m3u8.h"
|
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT fragmented_debug
|
|
|
|
|
2014-06-12 09:17:10 +00:00
|
|
|
#if !GLIB_CHECK_VERSION (2, 33, 4)
|
|
|
|
#define g_list_copy_deep gst_g_list_copy_deep
|
|
|
|
static GList *
|
|
|
|
gst_g_list_copy_deep (GList * list, GCopyFunc func, gpointer user_data)
|
|
|
|
{
|
|
|
|
list = g_list_copy (list);
|
|
|
|
|
|
|
|
if (func != NULL) {
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = list; l != NULL; l = l->next) {
|
|
|
|
l->data = func (l->data, user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static GstM3U8 *gst_m3u8_new (void);
|
|
|
|
static void gst_m3u8_free (GstM3U8 * m3u8);
|
|
|
|
static gboolean gst_m3u8_update (GstM3U8 * m3u8, gchar * data,
|
|
|
|
gboolean * updated);
|
|
|
|
static GstM3U8MediaFile *gst_m3u8_media_file_new (gchar * uri,
|
2012-02-28 15:40:31 +00:00
|
|
|
gchar * title, GstClockTime duration, guint sequence);
|
2011-02-14 17:51:32 +00:00
|
|
|
static void gst_m3u8_media_file_free (GstM3U8MediaFile * self);
|
2013-03-18 06:22:36 +00:00
|
|
|
gchar *uri_join (const gchar * uri, const gchar * path);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
static GstM3U8 *
|
|
|
|
gst_m3u8_new (void)
|
|
|
|
{
|
|
|
|
GstM3U8 *m3u8;
|
|
|
|
|
|
|
|
m3u8 = g_new0 (GstM3U8, 1);
|
|
|
|
|
|
|
|
return m3u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-30 22:34:18 +00:00
|
|
|
gst_m3u8_set_uri (GstM3U8 * self, gchar * uri, gchar * base_uri, gchar * name)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
2014-05-28 08:19:40 +00:00
|
|
|
g_free (self->uri);
|
2011-02-14 17:51:32 +00:00
|
|
|
self->uri = uri;
|
2014-05-28 08:19:40 +00:00
|
|
|
|
|
|
|
g_free (self->base_uri);
|
|
|
|
self->base_uri = base_uri;
|
2014-05-30 22:34:18 +00:00
|
|
|
|
|
|
|
g_free (self->name);
|
|
|
|
self->name = name;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_m3u8_free (GstM3U8 * self)
|
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
|
|
|
g_free (self->uri);
|
2014-05-28 08:19:40 +00:00
|
|
|
g_free (self->base_uri);
|
2014-05-30 22:34:18 +00:00
|
|
|
g_free (self->name);
|
2011-02-14 17:51:32 +00:00
|
|
|
g_free (self->codecs);
|
|
|
|
|
|
|
|
g_list_foreach (self->files, (GFunc) gst_m3u8_media_file_free, NULL);
|
|
|
|
g_list_free (self->files);
|
|
|
|
|
|
|
|
g_free (self->last_data);
|
|
|
|
g_list_foreach (self->lists, (GFunc) gst_m3u8_free, NULL);
|
|
|
|
g_list_free (self->lists);
|
2014-03-09 17:24:50 +00:00
|
|
|
g_list_foreach (self->iframe_lists, (GFunc) gst_m3u8_free, NULL);
|
|
|
|
g_list_free (self->iframe_lists);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstM3U8MediaFile *
|
2012-02-28 15:40:31 +00:00
|
|
|
gst_m3u8_media_file_new (gchar * uri, gchar * title, GstClockTime duration,
|
2011-02-14 17:51:32 +00:00
|
|
|
guint sequence)
|
|
|
|
{
|
|
|
|
GstM3U8MediaFile *file;
|
|
|
|
|
|
|
|
file = g_new0 (GstM3U8MediaFile, 1);
|
|
|
|
file->uri = uri;
|
|
|
|
file->title = title;
|
|
|
|
file->duration = duration;
|
|
|
|
file->sequence = sequence;
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_m3u8_media_file_free (GstM3U8MediaFile * self)
|
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
|
|
|
g_free (self->title);
|
|
|
|
g_free (self->uri);
|
2013-03-19 07:49:21 +00:00
|
|
|
g_free (self->key);
|
2011-02-14 17:51:32 +00:00
|
|
|
g_free (self);
|
|
|
|
}
|
|
|
|
|
2014-05-30 22:34:18 +00:00
|
|
|
static GstM3U8MediaFile *
|
|
|
|
gst_m3u8_media_file_copy (const GstM3U8MediaFile * self, gpointer user_data)
|
|
|
|
{
|
2014-06-06 10:04:04 +00:00
|
|
|
g_return_val_if_fail (self != NULL, NULL);
|
2014-05-30 22:34:18 +00:00
|
|
|
|
|
|
|
return gst_m3u8_media_file_new (g_strdup (self->uri), g_strdup (self->title),
|
|
|
|
self->duration, self->sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstM3U8 *
|
|
|
|
_m3u8_copy (const GstM3U8 * self, GstM3U8 * parent)
|
|
|
|
{
|
|
|
|
GstM3U8 *dup;
|
|
|
|
|
2014-06-06 10:04:04 +00:00
|
|
|
g_return_val_if_fail (self != NULL, NULL);
|
2014-05-30 22:34:18 +00:00
|
|
|
|
|
|
|
dup = gst_m3u8_new ();
|
|
|
|
dup->uri = g_strdup (self->uri);
|
|
|
|
dup->base_uri = g_strdup (self->base_uri);
|
|
|
|
dup->name = g_strdup (self->name);
|
|
|
|
dup->endlist = self->endlist;
|
|
|
|
dup->version = self->version;
|
|
|
|
dup->targetduration = self->targetduration;
|
|
|
|
dup->allowcache = self->allowcache;
|
|
|
|
dup->bandwidth = self->bandwidth;
|
|
|
|
dup->program_id = self->program_id;
|
|
|
|
dup->codecs = g_strdup (self->codecs);
|
|
|
|
dup->width = self->width;
|
|
|
|
dup->height = self->height;
|
|
|
|
dup->iframe = self->iframe;
|
|
|
|
dup->files =
|
|
|
|
g_list_copy_deep (self->files, (GCopyFunc) gst_m3u8_media_file_copy,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* private */
|
|
|
|
dup->last_data = g_strdup (self->last_data);
|
|
|
|
dup->lists = g_list_copy_deep (self->lists, (GCopyFunc) _m3u8_copy, dup);
|
|
|
|
dup->iframe_lists =
|
|
|
|
g_list_copy_deep (self->iframe_lists, (GCopyFunc) _m3u8_copy, dup);
|
|
|
|
/* NOTE: current_variant will get set in gst_m3u8_copy () */
|
|
|
|
dup->parent = parent;
|
|
|
|
dup->mediasequence = self->mediasequence;
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstM3U8 *
|
|
|
|
gst_m3u8_copy (const GstM3U8 * self)
|
|
|
|
{
|
|
|
|
GList *entry;
|
|
|
|
guint n;
|
|
|
|
|
|
|
|
GstM3U8 *dup = _m3u8_copy (self, NULL);
|
|
|
|
|
|
|
|
if (self->current_variant != NULL) {
|
|
|
|
for (n = 0, entry = self->lists; entry; entry = entry->next, n++) {
|
|
|
|
if (entry == self->current_variant) {
|
|
|
|
dup->current_variant = g_list_nth (dup->lists, n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dup->current_variant) {
|
|
|
|
for (n = 0, entry = self->iframe_lists; entry; entry = entry->next, n++) {
|
|
|
|
if (entry == self->current_variant) {
|
|
|
|
dup->current_variant = g_list_nth (dup->iframe_lists, n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dup->current_variant) {
|
|
|
|
GST_ERROR ("Failed to determine current playlist");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean
|
|
|
|
int_from_string (gchar * ptr, gchar ** endptr, gint * val)
|
|
|
|
{
|
|
|
|
gchar *end;
|
2014-02-10 16:25:57 +00:00
|
|
|
gint64 ret;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (ptr != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (val != NULL, FALSE);
|
|
|
|
|
|
|
|
errno = 0;
|
2014-02-10 16:25:57 +00:00
|
|
|
ret = g_ascii_strtoll (ptr, &end, 10);
|
|
|
|
if ((errno == ERANGE && (ret == G_MAXINT64 || ret == G_MININT64))
|
2011-05-15 08:00:44 +00:00
|
|
|
|| (errno != 0 && ret == 0)) {
|
2011-04-01 19:12:50 +00:00
|
|
|
GST_WARNING ("%s", g_strerror (errno));
|
2011-02-14 17:51:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-10 16:25:57 +00:00
|
|
|
if (ret > G_MAXINT || ret < G_MININT) {
|
2011-05-15 08:00:44 +00:00
|
|
|
GST_WARNING ("%s", g_strerror (ERANGE));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
if (endptr)
|
|
|
|
*endptr = end;
|
|
|
|
|
2011-05-15 08:00:44 +00:00
|
|
|
*val = (gint) ret;
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
return end != ptr;
|
|
|
|
}
|
|
|
|
|
2014-03-05 09:47:01 +00:00
|
|
|
static gboolean
|
|
|
|
int64_from_string (gchar * ptr, gchar ** endptr, gint64 * val)
|
|
|
|
{
|
|
|
|
gchar *end;
|
|
|
|
gint64 ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (ptr != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (val != NULL, FALSE);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ret = g_ascii_strtoll (ptr, &end, 10);
|
|
|
|
if ((errno == ERANGE && (ret == G_MAXINT64 || ret == G_MININT64))
|
|
|
|
|| (errno != 0 && ret == 0)) {
|
|
|
|
GST_WARNING ("%s", g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endptr)
|
|
|
|
*endptr = end;
|
|
|
|
|
|
|
|
*val = ret;
|
|
|
|
|
|
|
|
return end != ptr;
|
|
|
|
}
|
|
|
|
|
2012-02-28 15:40:31 +00:00
|
|
|
static gboolean
|
|
|
|
double_from_string (gchar * ptr, gchar ** endptr, gdouble * val)
|
|
|
|
{
|
|
|
|
gchar *end;
|
|
|
|
gdouble ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (ptr != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (val != NULL, FALSE);
|
|
|
|
|
|
|
|
errno = 0;
|
2014-02-10 16:25:57 +00:00
|
|
|
ret = g_ascii_strtod (ptr, &end);
|
2012-02-28 15:40:31 +00:00
|
|
|
if ((errno == ERANGE && (ret == HUGE_VAL || ret == -HUGE_VAL))
|
|
|
|
|| (errno != 0 && ret == 0)) {
|
|
|
|
GST_WARNING ("%s", g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isfinite (ret)) {
|
|
|
|
GST_WARNING ("%s", g_strerror (ERANGE));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endptr)
|
|
|
|
*endptr = end;
|
|
|
|
|
2014-02-10 16:26:27 +00:00
|
|
|
*val = (gdouble) ret;
|
2012-02-28 15:40:31 +00:00
|
|
|
|
|
|
|
return end != ptr;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gboolean
|
|
|
|
parse_attributes (gchar ** ptr, gchar ** a, gchar ** v)
|
|
|
|
{
|
2014-03-05 09:47:01 +00:00
|
|
|
gchar *end = NULL, *p;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (ptr != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (*ptr != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (a != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (v != NULL, FALSE);
|
|
|
|
|
|
|
|
/* [attribute=value,]* */
|
|
|
|
|
|
|
|
*a = *ptr;
|
|
|
|
end = p = g_utf8_strchr (*ptr, -1, ',');
|
2014-03-05 09:47:01 +00:00
|
|
|
if (end) {
|
|
|
|
gchar *q = g_utf8_strchr (*ptr, -1, '"');
|
|
|
|
if (q && q < end) {
|
|
|
|
/* special case, such as CODECS="avc1.77.30, mp4a.40.2" */
|
|
|
|
q = g_utf8_next_char (q);
|
|
|
|
if (q) {
|
|
|
|
q = g_utf8_strchr (q, -1, '"');
|
|
|
|
}
|
|
|
|
if (q) {
|
|
|
|
end = p = g_utf8_strchr (q, -1, ',');
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 09:36:51 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
if (end) {
|
|
|
|
do {
|
|
|
|
end = g_utf8_next_char (end);
|
|
|
|
} while (end && *end == ' ');
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = p = g_utf8_strchr (*ptr, -1, '=');
|
|
|
|
if (*v) {
|
|
|
|
*v = g_utf8_next_char (*v);
|
|
|
|
*p = '\0';
|
|
|
|
} else {
|
|
|
|
GST_WARNING ("missing = after attribute");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = end;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:34:44 +00:00
|
|
|
static gchar *
|
|
|
|
unquote_string (gchar * string)
|
|
|
|
{
|
|
|
|
gchar *string_ret;
|
|
|
|
|
|
|
|
string_ret = strchr (string, '"');
|
|
|
|
if (string_ret != NULL) {
|
|
|
|
/* found initialization quotation mark of string */
|
|
|
|
string = string_ret + 1;
|
|
|
|
string_ret = strchr (string, '"');
|
|
|
|
if (string_ret != NULL) {
|
|
|
|
/* found finalizing quotation mark of string */
|
|
|
|
string_ret[0] = '\0';
|
|
|
|
} else {
|
|
|
|
GST_WARNING
|
|
|
|
("wrong string unqouting - cannot find finalizing quotation mark");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
static gint
|
|
|
|
_m3u8_compare_uri (GstM3U8 * a, gchar * uri)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (a != NULL, 0);
|
|
|
|
g_return_val_if_fail (uri != NULL, 0);
|
|
|
|
|
|
|
|
return g_strcmp0 (a->uri, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gst_m3u8_compare_playlist_by_bitrate (gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
return ((GstM3U8 *) (a))->bandwidth - ((GstM3U8 *) (b))->bandwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @data: a m3u8 playlist text data, taking ownership
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gst_m3u8_update (GstM3U8 * self, gchar * data, gboolean * updated)
|
|
|
|
{
|
2012-02-28 15:40:31 +00:00
|
|
|
gint val;
|
|
|
|
GstClockTime duration;
|
2011-02-14 17:51:32 +00:00
|
|
|
gchar *title, *end;
|
2014-03-01 16:13:58 +00:00
|
|
|
gboolean discontinuity = FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
GstM3U8 *list;
|
2014-06-06 10:08:04 +00:00
|
|
|
gchar *current_key = NULL;
|
2014-02-11 17:15:07 +00:00
|
|
|
gboolean have_iv = FALSE;
|
|
|
|
guint8 iv[16] = { 0, };
|
2014-03-05 09:47:01 +00:00
|
|
|
gint64 size = -1, offset = -1;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (updated != NULL, FALSE);
|
|
|
|
|
|
|
|
*updated = TRUE;
|
|
|
|
|
|
|
|
/* check if the data changed since last update */
|
|
|
|
if (self->last_data && g_str_equal (self->last_data, data)) {
|
|
|
|
GST_DEBUG ("Playlist is the same as previous one");
|
|
|
|
*updated = FALSE;
|
|
|
|
g_free (data);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_str_has_prefix (data, "#EXTM3U")) {
|
|
|
|
GST_WARNING ("Data doesn't start with #EXTM3U");
|
2011-08-18 23:54:59 +00:00
|
|
|
*updated = FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
g_free (data);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (self->last_data);
|
|
|
|
self->last_data = data;
|
|
|
|
|
2011-04-01 23:10:37 +00:00
|
|
|
if (self->files) {
|
|
|
|
g_list_foreach (self->files, (GFunc) gst_m3u8_media_file_free, NULL);
|
|
|
|
g_list_free (self->files);
|
|
|
|
self->files = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-05 07:34:56 +00:00
|
|
|
/* By default, allow caching */
|
|
|
|
self->allowcache = TRUE;
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
list = NULL;
|
2012-02-28 15:40:31 +00:00
|
|
|
duration = 0;
|
2011-02-14 17:51:32 +00:00
|
|
|
title = NULL;
|
|
|
|
data += 7;
|
|
|
|
while (TRUE) {
|
2014-02-12 17:47:45 +00:00
|
|
|
gchar *r;
|
|
|
|
|
2011-09-02 13:00:58 +00:00
|
|
|
end = g_utf8_strchr (data, -1, '\n');
|
2011-02-14 17:51:32 +00:00
|
|
|
if (end)
|
|
|
|
*end = '\0';
|
|
|
|
|
2014-02-12 17:47:45 +00:00
|
|
|
r = g_utf8_strchr (data, -1, '\r');
|
|
|
|
if (r)
|
|
|
|
*r = '\0';
|
|
|
|
|
2014-02-12 17:49:13 +00:00
|
|
|
if (data[0] != '#' && data[0] != '\0') {
|
2014-05-30 22:34:18 +00:00
|
|
|
gchar *name = data;
|
2012-02-28 15:40:31 +00:00
|
|
|
if (duration <= 0 && list == NULL) {
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_LOG ("%s: got line without EXTINF or EXTSTREAMINF, dropping", data);
|
|
|
|
goto next_line;
|
|
|
|
}
|
|
|
|
|
2014-05-28 08:19:40 +00:00
|
|
|
data = uri_join (self->base_uri ? self->base_uri : self->uri, data);
|
2013-03-18 06:22:36 +00:00
|
|
|
if (data == NULL)
|
|
|
|
goto next_line;
|
2011-08-26 19:29:06 +00:00
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
if (list != NULL) {
|
|
|
|
if (g_list_find_custom (self->lists, data,
|
|
|
|
(GCompareFunc) _m3u8_compare_uri)) {
|
|
|
|
GST_DEBUG ("Already have a list with this URI");
|
|
|
|
gst_m3u8_free (list);
|
|
|
|
g_free (data);
|
|
|
|
} else {
|
2014-05-30 22:34:18 +00:00
|
|
|
gst_m3u8_set_uri (list, data, NULL, g_strdup (name));
|
2011-02-14 17:51:32 +00:00
|
|
|
self->lists = g_list_append (self->lists, list);
|
|
|
|
}
|
|
|
|
list = NULL;
|
|
|
|
} else {
|
|
|
|
GstM3U8MediaFile *file;
|
|
|
|
file =
|
|
|
|
gst_m3u8_media_file_new (data, title, duration,
|
|
|
|
self->mediasequence++);
|
2013-03-19 07:49:21 +00:00
|
|
|
|
|
|
|
/* set encryption params */
|
2014-06-06 10:08:04 +00:00
|
|
|
file->key = current_key ? g_strdup (current_key) : NULL;
|
2013-03-19 07:49:21 +00:00
|
|
|
if (file->key) {
|
2014-02-11 17:15:07 +00:00
|
|
|
if (have_iv) {
|
|
|
|
memcpy (file->iv, iv, sizeof (iv));
|
|
|
|
} else {
|
|
|
|
guint8 *iv = file->iv + 12;
|
2014-05-22 10:54:40 +00:00
|
|
|
GST_WRITE_UINT32_BE (iv, file->sequence);
|
2014-02-11 17:15:07 +00:00
|
|
|
}
|
2013-03-19 07:49:21 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 09:47:01 +00:00
|
|
|
if (size != -1) {
|
|
|
|
file->size = size;
|
|
|
|
if (offset != -1) {
|
|
|
|
file->offset = offset;
|
|
|
|
} else {
|
|
|
|
GstM3U8MediaFile *prev =
|
|
|
|
self->files ? g_list_last (self->files)->data : NULL;
|
|
|
|
|
|
|
|
if (!prev) {
|
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
offset = prev->offset + prev->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file->size = -1;
|
|
|
|
file->offset = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-01 16:13:58 +00:00
|
|
|
file->discont = discontinuity;
|
|
|
|
|
2012-02-28 15:40:31 +00:00
|
|
|
duration = 0;
|
2011-02-14 17:51:32 +00:00
|
|
|
title = NULL;
|
2014-03-01 16:13:58 +00:00
|
|
|
discontinuity = FALSE;
|
2014-03-05 09:47:01 +00:00
|
|
|
size = offset = -1;
|
2011-02-14 17:51:32 +00:00
|
|
|
self->files = g_list_append (self->files, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-ENDLIST")) {
|
|
|
|
self->endlist = TRUE;
|
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-VERSION:")) {
|
|
|
|
if (int_from_string (data + 15, &data, &val))
|
|
|
|
self->version = val;
|
2014-03-09 17:24:50 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-STREAM-INF:") ||
|
|
|
|
g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:")) {
|
2011-02-14 17:51:32 +00:00
|
|
|
gchar *v, *a;
|
2014-03-09 17:24:50 +00:00
|
|
|
gboolean iframe = g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:");
|
|
|
|
GstM3U8 *new_list;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-03-09 17:24:50 +00:00
|
|
|
new_list = gst_m3u8_new ();
|
2014-05-30 22:34:18 +00:00
|
|
|
new_list->parent = self;
|
2014-03-09 17:24:50 +00:00
|
|
|
new_list->iframe = iframe;
|
|
|
|
data = data + (iframe ? 26 : 18);
|
2011-02-14 17:51:32 +00:00
|
|
|
while (data && parse_attributes (&data, &a, &v)) {
|
|
|
|
if (g_str_equal (a, "BANDWIDTH")) {
|
2014-03-09 17:24:50 +00:00
|
|
|
if (!int_from_string (v, NULL, &new_list->bandwidth))
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_WARNING ("Error while reading BANDWIDTH");
|
|
|
|
} else if (g_str_equal (a, "PROGRAM-ID")) {
|
2014-03-09 17:24:50 +00:00
|
|
|
if (!int_from_string (v, NULL, &new_list->program_id))
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_WARNING ("Error while reading PROGRAM-ID");
|
|
|
|
} else if (g_str_equal (a, "CODECS")) {
|
2014-03-09 17:24:50 +00:00
|
|
|
g_free (new_list->codecs);
|
|
|
|
new_list->codecs = g_strdup (v);
|
2011-02-14 17:51:32 +00:00
|
|
|
} else if (g_str_equal (a, "RESOLUTION")) {
|
2014-03-09 17:24:50 +00:00
|
|
|
if (!int_from_string (v, &v, &new_list->width))
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_WARNING ("Error while reading RESOLUTION width");
|
2014-02-21 09:36:51 +00:00
|
|
|
if (!v || *v != 'x') {
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_WARNING ("Missing height");
|
|
|
|
} else {
|
|
|
|
v = g_utf8_next_char (v);
|
2014-03-09 17:24:50 +00:00
|
|
|
if (!int_from_string (v, NULL, &new_list->height))
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_WARNING ("Error while reading RESOLUTION height");
|
|
|
|
}
|
2014-03-09 17:24:50 +00:00
|
|
|
} else if (iframe && g_str_equal (a, "URI")) {
|
2014-05-30 22:34:18 +00:00
|
|
|
gchar *name;
|
2014-03-09 17:24:50 +00:00
|
|
|
gchar *uri = g_strdup (v);
|
|
|
|
gchar *urip = uri;
|
|
|
|
|
2014-06-04 17:34:44 +00:00
|
|
|
uri = unquote_string (uri);
|
|
|
|
if (uri) {
|
|
|
|
uri = uri_join (self->base_uri ? self->base_uri : self->uri, uri);
|
2014-03-09 17:24:50 +00:00
|
|
|
|
2014-06-04 17:34:44 +00:00
|
|
|
uri = uri_join (self->base_uri ? self->base_uri : self->uri, uri);
|
|
|
|
if (uri == NULL) {
|
|
|
|
g_free (urip);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
name = g_strdup (uri);
|
2014-03-09 17:24:50 +00:00
|
|
|
|
2014-06-04 17:34:44 +00:00
|
|
|
gst_m3u8_set_uri (new_list, uri, NULL, name);
|
|
|
|
} else {
|
|
|
|
GST_WARNING
|
|
|
|
("Cannot remove quotation marks from i-frame-stream URI");
|
2014-05-30 22:34:18 +00:00
|
|
|
}
|
2014-06-04 17:34:44 +00:00
|
|
|
g_free (urip);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-09 17:24:50 +00:00
|
|
|
|
|
|
|
if (iframe) {
|
|
|
|
if (g_list_find_custom (self->iframe_lists, new_list->uri,
|
|
|
|
(GCompareFunc) _m3u8_compare_uri)) {
|
|
|
|
GST_DEBUG ("Already have a list with this URI");
|
|
|
|
gst_m3u8_free (new_list);
|
|
|
|
} else {
|
|
|
|
self->iframe_lists = g_list_append (self->iframe_lists, new_list);
|
|
|
|
}
|
|
|
|
} else if (list != NULL) {
|
|
|
|
GST_WARNING ("Found a list without a uri..., dropping");
|
|
|
|
gst_m3u8_free (list);
|
|
|
|
} else {
|
|
|
|
list = new_list;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-TARGETDURATION:")) {
|
|
|
|
if (int_from_string (data + 22, &data, &val))
|
2012-02-28 15:40:31 +00:00
|
|
|
self->targetduration = val * GST_SECOND;
|
2011-02-14 17:51:32 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-MEDIA-SEQUENCE:")) {
|
|
|
|
if (int_from_string (data + 22, &data, &val))
|
|
|
|
self->mediasequence = val;
|
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-DISCONTINUITY")) {
|
2014-03-01 16:13:58 +00:00
|
|
|
discontinuity = TRUE;
|
2011-02-14 17:51:32 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-PROGRAM-DATE-TIME:")) {
|
|
|
|
/* <YYYY-MM-DDThh:mm:ssZ> */
|
|
|
|
GST_DEBUG ("FIXME parse date");
|
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-ALLOW-CACHE:")) {
|
2014-05-05 07:34:56 +00:00
|
|
|
self->allowcache = g_ascii_strcasecmp (data + 19, "YES") == 0;
|
2013-03-18 06:34:13 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-KEY:")) {
|
|
|
|
gchar *v, *a;
|
|
|
|
|
|
|
|
data = data + 11;
|
2014-02-11 17:15:07 +00:00
|
|
|
|
|
|
|
/* IV and KEY are only valid until the next #EXT-X-KEY */
|
|
|
|
have_iv = FALSE;
|
2014-06-06 10:08:04 +00:00
|
|
|
g_free (current_key);
|
|
|
|
current_key = NULL;
|
2013-03-18 06:34:13 +00:00
|
|
|
while (data && parse_attributes (&data, &a, &v)) {
|
|
|
|
if (g_str_equal (a, "URI")) {
|
|
|
|
gchar *key = g_strdup (v);
|
2013-03-18 20:01:59 +00:00
|
|
|
gchar *keyp = key;
|
2013-03-18 06:34:13 +00:00
|
|
|
|
2014-06-04 17:34:44 +00:00
|
|
|
key = unquote_string (key);
|
|
|
|
if (key) {
|
|
|
|
current_key =
|
|
|
|
uri_join (self->base_uri ? self->base_uri : self->uri, key);
|
|
|
|
} else {
|
|
|
|
GST_WARNING
|
|
|
|
("Cannot remove quotation marks from decryption key URI");
|
|
|
|
}
|
2013-03-18 20:01:59 +00:00
|
|
|
g_free (keyp);
|
2014-02-11 17:15:07 +00:00
|
|
|
} else if (g_str_equal (a, "IV")) {
|
|
|
|
gchar *ivp = v;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (strlen (ivp) < 32 + 2 || (!g_str_has_prefix (ivp, "0x")
|
|
|
|
&& !g_str_has_prefix (ivp, "0X"))) {
|
|
|
|
GST_WARNING ("Can't read IV");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ivp += 2;
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
gint h, l;
|
|
|
|
|
2014-02-12 13:10:02 +00:00
|
|
|
h = g_ascii_xdigit_value (*ivp);
|
|
|
|
ivp++;
|
|
|
|
l = g_ascii_xdigit_value (*ivp);
|
|
|
|
ivp++;
|
2014-02-11 17:15:07 +00:00
|
|
|
if (h == -1 || l == -1) {
|
|
|
|
i = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iv[i] = (h << 4) | l;
|
|
|
|
}
|
2014-02-12 13:10:02 +00:00
|
|
|
|
2014-02-11 17:15:07 +00:00
|
|
|
if (i == -1) {
|
|
|
|
GST_WARNING ("Can't read IV");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
have_iv = TRUE;
|
2014-02-11 17:15:45 +00:00
|
|
|
} else if (g_str_equal (a, "METHOD")) {
|
|
|
|
if (!g_str_equal (v, "AES-128")) {
|
|
|
|
GST_WARNING ("Encryption method %s not supported", v);
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-18 06:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXTINF:")) {
|
2012-02-28 15:40:31 +00:00
|
|
|
gdouble fval;
|
|
|
|
if (!double_from_string (data + 8, &data, &fval)) {
|
2011-02-14 17:51:32 +00:00
|
|
|
GST_WARNING ("Can't read EXTINF duration");
|
|
|
|
goto next_line;
|
|
|
|
}
|
2012-02-28 15:40:31 +00:00
|
|
|
duration = fval * (gdouble) GST_SECOND;
|
2011-02-14 17:51:32 +00:00
|
|
|
if (duration > self->targetduration)
|
|
|
|
GST_WARNING ("EXTINF duration > TARGETDURATION");
|
|
|
|
if (!data || *data != ',')
|
|
|
|
goto next_line;
|
|
|
|
data = g_utf8_next_char (data);
|
|
|
|
if (data != end) {
|
|
|
|
g_free (title);
|
|
|
|
title = g_strdup (data);
|
|
|
|
}
|
2014-03-05 09:47:01 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-BYTERANGE:")) {
|
|
|
|
gchar *v = data + 17;
|
|
|
|
|
|
|
|
if (int64_from_string (v, &v, &size)) {
|
|
|
|
if (*v == '@' && !int64_from_string (v + 1, &v, &offset))
|
|
|
|
goto next_line;
|
|
|
|
} else {
|
|
|
|
goto next_line;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
} else {
|
|
|
|
GST_LOG ("Ignored line: %s", data);
|
|
|
|
}
|
|
|
|
|
|
|
|
next_line:
|
|
|
|
if (!end)
|
|
|
|
break;
|
|
|
|
data = g_utf8_next_char (end); /* skip \n */
|
|
|
|
}
|
|
|
|
|
2014-06-06 10:08:04 +00:00
|
|
|
g_free (current_key);
|
|
|
|
current_key = NULL;
|
|
|
|
|
2014-03-09 18:31:31 +00:00
|
|
|
/* reorder playlists by bitrate */
|
2011-08-18 23:54:59 +00:00
|
|
|
if (self->lists) {
|
|
|
|
gchar *top_variant_uri = NULL;
|
2014-03-09 17:24:50 +00:00
|
|
|
gboolean iframe = FALSE;
|
2011-08-18 23:54:59 +00:00
|
|
|
|
2014-03-09 17:24:50 +00:00
|
|
|
if (!self->current_variant) {
|
2011-08-18 23:54:59 +00:00
|
|
|
top_variant_uri = GST_M3U8 (self->lists->data)->uri;
|
2014-03-09 17:24:50 +00:00
|
|
|
} else {
|
2011-08-18 23:54:59 +00:00
|
|
|
top_variant_uri = GST_M3U8 (self->current_variant->data)->uri;
|
2014-03-09 17:24:50 +00:00
|
|
|
iframe = GST_M3U8 (self->current_variant->data)->iframe;
|
|
|
|
}
|
2011-08-18 23:54:59 +00:00
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
self->lists =
|
|
|
|
g_list_sort (self->lists,
|
|
|
|
(GCompareFunc) gst_m3u8_compare_playlist_by_bitrate);
|
|
|
|
|
2014-03-09 17:24:50 +00:00
|
|
|
self->iframe_lists =
|
|
|
|
g_list_sort (self->iframe_lists,
|
|
|
|
(GCompareFunc) gst_m3u8_compare_playlist_by_bitrate);
|
|
|
|
|
|
|
|
if (iframe)
|
|
|
|
self->current_variant =
|
|
|
|
g_list_find_custom (self->iframe_lists, top_variant_uri,
|
|
|
|
(GCompareFunc) _m3u8_compare_uri);
|
|
|
|
else
|
|
|
|
self->current_variant = g_list_find_custom (self->lists, top_variant_uri,
|
|
|
|
(GCompareFunc) _m3u8_compare_uri);
|
2011-08-18 23:54:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstM3U8Client *
|
2014-05-28 08:19:40 +00:00
|
|
|
gst_m3u8_client_new (const gchar * uri, const gchar * base_uri)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
|
|
|
GstM3U8Client *client;
|
|
|
|
|
|
|
|
g_return_val_if_fail (uri != NULL, NULL);
|
|
|
|
|
|
|
|
client = g_new0 (GstM3U8Client, 1);
|
|
|
|
client->main = gst_m3u8_new ();
|
|
|
|
client->current = NULL;
|
|
|
|
client->sequence = -1;
|
2014-02-23 14:18:22 +00:00
|
|
|
client->sequence_position = 0;
|
2011-02-14 17:51:32 +00:00
|
|
|
client->update_failed_count = 0;
|
2014-02-09 17:19:33 +00:00
|
|
|
g_mutex_init (&client->lock);
|
2014-05-30 22:34:18 +00:00
|
|
|
gst_m3u8_set_uri (client->main, g_strdup (uri), g_strdup (base_uri), NULL);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_m3u8_client_free (GstM3U8Client * self)
|
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
|
|
|
gst_m3u8_free (self->main);
|
2014-02-09 17:19:33 +00:00
|
|
|
g_mutex_clear (&self->lock);
|
2011-02-14 17:51:32 +00:00
|
|
|
g_free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_m3u8_client_set_current (GstM3U8Client * self, GstM3U8 * m3u8)
|
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (self);
|
2011-02-14 17:51:32 +00:00
|
|
|
if (m3u8 != self->current) {
|
|
|
|
self->current = m3u8;
|
|
|
|
self->update_failed_count = 0;
|
|
|
|
}
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (self);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_m3u8_client_update (GstM3U8Client * self, gchar * data)
|
|
|
|
{
|
|
|
|
GstM3U8 *m3u8;
|
|
|
|
gboolean updated = FALSE;
|
2011-09-02 23:48:03 +00:00
|
|
|
gboolean ret = FALSE;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, FALSE);
|
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (self);
|
2011-02-14 17:51:32 +00:00
|
|
|
m3u8 = self->current ? self->current : self->main;
|
|
|
|
|
|
|
|
if (!gst_m3u8_update (m3u8, data, &updated))
|
2011-09-02 23:48:03 +00:00
|
|
|
goto out;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
if (!updated) {
|
|
|
|
self->update_failed_count++;
|
2011-09-02 23:48:03 +00:00
|
|
|
goto out;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 11:45:46 +00:00
|
|
|
if (self->current && !self->current->files) {
|
|
|
|
GST_ERROR ("Invalid media playlist, it does not contain any media files");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
/* select the first playlist, for now */
|
|
|
|
if (!self->current) {
|
|
|
|
if (self->main->lists) {
|
2011-08-18 23:54:59 +00:00
|
|
|
self->current = self->main->current_variant->data;
|
2011-02-14 17:51:32 +00:00
|
|
|
} else {
|
|
|
|
self->current = self->main;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m3u8->files && self->sequence == -1) {
|
|
|
|
self->sequence =
|
|
|
|
GST_M3U8_MEDIA_FILE (g_list_first (m3u8->files)->data)->sequence;
|
2014-02-23 14:18:22 +00:00
|
|
|
self->sequence_position = 0;
|
2014-03-09 18:31:31 +00:00
|
|
|
GST_DEBUG ("Setting first sequence at %u", (guint) self->sequence);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
ret = TRUE;
|
|
|
|
out:
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (self);
|
|
|
|
return ret;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 22:34:18 +00:00
|
|
|
static gint
|
|
|
|
_find_m3u8_list_match (const GstM3U8 * a, const GstM3U8 * b)
|
|
|
|
{
|
|
|
|
if (g_strcmp0 (a->name, b->name) == 0 &&
|
|
|
|
a->bandwidth == b->bandwidth &&
|
|
|
|
a->program_id == b->program_id &&
|
|
|
|
g_strcmp0 (a->codecs, b->codecs) == 0 &&
|
|
|
|
a->width == b->width &&
|
|
|
|
a->height == b->height && a->iframe == b->iframe) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_m3u8_client_update_variant_playlist (GstM3U8Client * self, gchar * data,
|
|
|
|
const gchar * uri, const gchar * base_uri)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
GList *list_entry, *unmatched_lists;
|
|
|
|
GstM3U8Client *new_client;
|
|
|
|
GstM3U8 *old;
|
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, FALSE);
|
|
|
|
|
|
|
|
new_client = gst_m3u8_client_new (uri, base_uri);
|
|
|
|
if (gst_m3u8_client_update (new_client, data)) {
|
|
|
|
if (!new_client->main->lists) {
|
|
|
|
GST_ERROR
|
|
|
|
("Cannot update variant playlist: New playlist is not a variant playlist");
|
|
|
|
gst_m3u8_client_free (new_client);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (self);
|
|
|
|
|
|
|
|
if (!self->main->lists) {
|
|
|
|
GST_ERROR
|
|
|
|
("Cannot update variant playlist: Current playlist is not a variant playlist");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now see if the variant playlist still has the same lists */
|
|
|
|
unmatched_lists = g_list_copy (self->main->lists);
|
|
|
|
for (list_entry = new_client->main->lists; list_entry;
|
|
|
|
list_entry = list_entry->next) {
|
|
|
|
GList *match = g_list_find_custom (unmatched_lists, list_entry->data,
|
|
|
|
(GCompareFunc) _find_m3u8_list_match);
|
|
|
|
if (match)
|
|
|
|
unmatched_lists = g_list_remove_link (unmatched_lists, match);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unmatched_lists != NULL) {
|
|
|
|
g_list_free (unmatched_lists);
|
|
|
|
|
|
|
|
/* We should attempt to handle the case where playlists are dropped/replaced,
|
|
|
|
* and possibly switch over to a comparable (not neccessarily identical)
|
|
|
|
* playlist.
|
|
|
|
*/
|
|
|
|
GST_FIXME
|
|
|
|
("Cannot update variant playlist, unable to match all playlists");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch out the variant playlist */
|
|
|
|
old = self->main;
|
|
|
|
|
|
|
|
self->main = gst_m3u8_copy (new_client->main);
|
|
|
|
if (self->main->lists)
|
|
|
|
self->current = self->main->current_variant->data;
|
|
|
|
else
|
|
|
|
self->current = self->main;
|
|
|
|
|
|
|
|
gst_m3u8_free (old);
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_m3u8_client_free (new_client);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-26 06:25:13 +00:00
|
|
|
static gboolean
|
2013-07-25 17:36:48 +00:00
|
|
|
_find_current (GstM3U8MediaFile * file, GstM3U8Client * client)
|
|
|
|
{
|
2014-03-26 06:25:13 +00:00
|
|
|
return file->sequence != client->sequence;
|
2013-07-25 17:36:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 06:25:13 +00:00
|
|
|
static GList *
|
|
|
|
find_next_fragment (GstM3U8Client * client, GList * l, gboolean forward)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2014-03-26 06:25:13 +00:00
|
|
|
GstM3U8MediaFile *file;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-03-26 06:25:13 +00:00
|
|
|
if (!forward)
|
|
|
|
l = g_list_last (l);
|
|
|
|
|
|
|
|
while (l) {
|
|
|
|
file = l->data;
|
|
|
|
|
|
|
|
if (forward && file->sequence >= client->sequence)
|
|
|
|
break;
|
|
|
|
else if (!forward && file->sequence <= client->sequence)
|
|
|
|
break;
|
|
|
|
|
|
|
|
l = (forward ? l->next : l->prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
2014-03-09 18:31:31 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 11:34:53 +00:00
|
|
|
gboolean
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_m3u8_client_get_next_fragment (GstM3U8Client * client,
|
2011-08-16 19:43:08 +00:00
|
|
|
gboolean * discontinuity, const gchar ** uri, GstClockTime * duration,
|
2014-03-05 09:47:01 +00:00
|
|
|
GstClockTime * timestamp, gint64 * range_start, gint64 * range_end,
|
2014-03-09 18:31:31 +00:00
|
|
|
const gchar ** key, const guint8 ** iv, gboolean forward)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2011-09-09 10:27:57 +00:00
|
|
|
GList *l;
|
2011-02-14 17:51:32 +00:00
|
|
|
GstM3U8MediaFile *file;
|
|
|
|
|
2011-04-14 11:34:53 +00:00
|
|
|
g_return_val_if_fail (client != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (client->current != NULL, FALSE);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
2014-03-09 18:31:31 +00:00
|
|
|
GST_DEBUG ("Looking for fragment %" G_GINT64_FORMAT, client->sequence);
|
|
|
|
if (client->sequence < 0) {
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-03-26 06:25:13 +00:00
|
|
|
l = find_next_fragment (client, client->current->files, forward);
|
|
|
|
if (!l) {
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
2011-04-14 11:34:53 +00:00
|
|
|
return FALSE;
|
2011-09-02 23:48:03 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
file = GST_M3U8_MEDIA_FILE (l->data);
|
2014-02-17 08:19:32 +00:00
|
|
|
GST_DEBUG ("Got fragment with sequence %u (client sequence %u)",
|
2014-03-09 18:31:31 +00:00
|
|
|
(guint) file->sequence, (guint) client->sequence);
|
2014-02-17 08:19:32 +00:00
|
|
|
|
|
|
|
if (timestamp)
|
2014-02-23 14:18:22 +00:00
|
|
|
*timestamp = client->sequence_position;
|
2014-02-17 08:19:32 +00:00
|
|
|
|
|
|
|
if (discontinuity)
|
2014-03-01 16:13:58 +00:00
|
|
|
*discontinuity = client->sequence != file->sequence || file->discont;
|
2014-02-17 08:19:32 +00:00
|
|
|
if (uri)
|
|
|
|
*uri = file->uri;
|
|
|
|
if (duration)
|
|
|
|
*duration = file->duration;
|
2014-03-05 09:47:01 +00:00
|
|
|
if (range_start)
|
|
|
|
*range_start = file->offset;
|
|
|
|
if (range_end)
|
2014-03-07 15:22:50 +00:00
|
|
|
*range_end = file->size != -1 ? file->offset + file->size - 1 : -1;
|
2014-02-17 08:19:32 +00:00
|
|
|
if (key)
|
|
|
|
*key = file->key;
|
|
|
|
if (iv)
|
|
|
|
*iv = file->iv;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-03-26 06:25:13 +00:00
|
|
|
client->sequence = file->sequence;
|
|
|
|
|
2014-02-17 08:19:32 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-02-17 08:19:32 +00:00
|
|
|
void
|
2014-03-09 18:31:31 +00:00
|
|
|
gst_m3u8_client_advance_fragment (GstM3U8Client * client, gboolean forward)
|
2014-02-17 08:19:32 +00:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
GstM3U8MediaFile *file;
|
2011-08-16 19:43:08 +00:00
|
|
|
|
2014-02-17 08:19:32 +00:00
|
|
|
g_return_if_fail (client != NULL);
|
|
|
|
g_return_if_fail (client->current != NULL);
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
2014-03-09 18:31:31 +00:00
|
|
|
GST_DEBUG ("Looking for fragment %" G_GINT64_FORMAT, client->sequence);
|
2014-02-17 08:19:32 +00:00
|
|
|
l = g_list_find_custom (client->current->files, client,
|
2014-03-09 18:31:31 +00:00
|
|
|
(GCompareFunc) _find_current);
|
2014-02-17 08:19:32 +00:00
|
|
|
if (l == NULL) {
|
|
|
|
GST_ERROR ("Could not find current fragment");
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file = GST_M3U8_MEDIA_FILE (l->data);
|
2014-03-09 18:31:31 +00:00
|
|
|
GST_DEBUG ("Advancing from sequence %u", (guint) file->sequence);
|
|
|
|
if (forward) {
|
|
|
|
client->sequence = file->sequence + 1;
|
|
|
|
client->sequence_position += file->duration;
|
|
|
|
} else {
|
|
|
|
client->sequence = file->sequence - 1;
|
|
|
|
if (client->sequence_position > file->duration)
|
|
|
|
client->sequence_position -= file->duration;
|
|
|
|
else
|
|
|
|
client->sequence_position = 0;
|
|
|
|
}
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
2011-03-12 11:20:32 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
_sum_duration (GstM3U8MediaFile * self, GstClockTime * duration)
|
|
|
|
{
|
|
|
|
*duration += self->duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstClockTime
|
|
|
|
gst_m3u8_client_get_duration (GstM3U8Client * client)
|
|
|
|
{
|
|
|
|
GstClockTime duration = 0;
|
|
|
|
|
|
|
|
g_return_val_if_fail (client != NULL, GST_CLOCK_TIME_NONE);
|
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
2011-03-12 11:20:32 +00:00
|
|
|
/* We can only get the duration for on-demand streams */
|
2014-04-08 16:10:27 +00:00
|
|
|
if (!client->current || !client->current->endlist) {
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
2011-03-12 11:20:32 +00:00
|
|
|
return GST_CLOCK_TIME_NONE;
|
2011-09-02 23:48:03 +00:00
|
|
|
}
|
2014-04-08 15:46:56 +00:00
|
|
|
if (client->current->files)
|
2014-02-25 11:45:46 +00:00
|
|
|
g_list_foreach (client->current->files, (GFunc) _sum_duration, &duration);
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
2012-02-28 15:40:31 +00:00
|
|
|
return duration;
|
2011-03-12 11:20:32 +00:00
|
|
|
}
|
2011-03-12 11:28:42 +00:00
|
|
|
|
2011-09-02 23:48:22 +00:00
|
|
|
GstClockTime
|
|
|
|
gst_m3u8_client_get_target_duration (GstM3U8Client * client)
|
|
|
|
{
|
|
|
|
GstClockTime duration = 0;
|
|
|
|
|
|
|
|
g_return_val_if_fail (client != NULL, GST_CLOCK_TIME_NONE);
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
|
|
|
duration = client->current->targetduration;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
2012-02-28 15:40:31 +00:00
|
|
|
return duration;
|
2011-09-02 23:48:22 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 11:28:42 +00:00
|
|
|
const gchar *
|
|
|
|
gst_m3u8_client_get_uri (GstM3U8Client * client)
|
|
|
|
{
|
2011-09-02 23:48:03 +00:00
|
|
|
const gchar *uri;
|
|
|
|
|
2011-03-12 11:28:42 +00:00
|
|
|
g_return_val_if_fail (client != NULL, NULL);
|
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
|
|
|
uri = client->main->uri;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return uri;
|
2011-03-12 11:28:42 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 23:48:22 +00:00
|
|
|
const gchar *
|
|
|
|
gst_m3u8_client_get_current_uri (GstM3U8Client * client)
|
|
|
|
{
|
|
|
|
const gchar *uri;
|
|
|
|
|
|
|
|
g_return_val_if_fail (client != NULL, NULL);
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
|
|
|
uri = client->current->uri;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2011-03-12 11:28:42 +00:00
|
|
|
gboolean
|
|
|
|
gst_m3u8_client_has_variant_playlist (GstM3U8Client * client)
|
|
|
|
{
|
2011-09-02 23:48:03 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
2011-03-12 11:28:42 +00:00
|
|
|
g_return_val_if_fail (client != NULL, FALSE);
|
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
|
|
|
ret = (client->main->lists != NULL);
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return ret;
|
2011-03-12 11:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_m3u8_client_is_live (GstM3U8Client * client)
|
|
|
|
{
|
2011-09-02 23:48:03 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
2011-03-12 11:28:42 +00:00
|
|
|
g_return_val_if_fail (client != NULL, FALSE);
|
|
|
|
|
2011-09-02 23:48:03 +00:00
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
2011-03-12 11:28:42 +00:00
|
|
|
if (!client->current || client->current->endlist)
|
2011-09-02 23:48:03 +00:00
|
|
|
ret = FALSE;
|
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return ret;
|
2011-03-12 11:28:42 +00:00
|
|
|
}
|
2012-05-10 14:10:14 +00:00
|
|
|
|
|
|
|
GList *
|
|
|
|
gst_m3u8_client_get_playlist_for_bitrate (GstM3U8Client * client, guint bitrate)
|
|
|
|
{
|
|
|
|
GList *list, *current_variant;
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
|
|
|
current_variant = client->main->current_variant;
|
|
|
|
|
|
|
|
/* Go to the highest possible bandwidth allowed */
|
2014-03-09 18:38:39 +00:00
|
|
|
while (GST_M3U8 (current_variant->data)->bandwidth <= bitrate) {
|
2012-05-10 14:10:14 +00:00
|
|
|
list = g_list_next (current_variant);
|
|
|
|
if (!list)
|
|
|
|
break;
|
|
|
|
current_variant = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (GST_M3U8 (current_variant->data)->bandwidth > bitrate) {
|
|
|
|
list = g_list_previous (current_variant);
|
|
|
|
if (!list)
|
|
|
|
break;
|
|
|
|
current_variant = list;
|
|
|
|
}
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
|
|
|
|
return current_variant;
|
|
|
|
}
|
2013-03-18 06:22:36 +00:00
|
|
|
|
|
|
|
gchar *
|
|
|
|
uri_join (const gchar * uri1, const gchar * uri2)
|
|
|
|
{
|
|
|
|
gchar *uri_copy, *tmp, *ret = NULL;
|
|
|
|
|
|
|
|
if (gst_uri_is_valid (uri2))
|
|
|
|
return g_strdup (uri2);
|
|
|
|
|
|
|
|
uri_copy = g_strdup (uri1);
|
|
|
|
if (uri2[0] != '/') {
|
|
|
|
/* uri2 is a relative uri2 */
|
2014-02-21 09:30:49 +00:00
|
|
|
/* look for query params */
|
|
|
|
tmp = g_utf8_strchr (uri_copy, -1, '?');
|
|
|
|
if (tmp) {
|
|
|
|
/* find last / char, ignoring query params */
|
|
|
|
tmp = g_utf8_strrchr (uri_copy, tmp - uri_copy, '/');
|
|
|
|
} else {
|
|
|
|
/* find last / char in URL */
|
|
|
|
tmp = g_utf8_strrchr (uri_copy, -1, '/');
|
|
|
|
}
|
2013-03-18 06:22:36 +00:00
|
|
|
if (!tmp) {
|
|
|
|
GST_WARNING ("Can't build a valid uri_copy");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
*tmp = '\0';
|
|
|
|
ret = g_strdup_printf ("%s/%s", uri_copy, uri2);
|
|
|
|
} else {
|
|
|
|
/* uri2 is an absolute uri2 */
|
|
|
|
char *scheme, *hostname;
|
|
|
|
|
|
|
|
scheme = uri_copy;
|
|
|
|
/* find the : in <scheme>:// */
|
|
|
|
tmp = g_utf8_strchr (uri_copy, -1, ':');
|
|
|
|
if (!tmp) {
|
|
|
|
GST_WARNING ("Can't build a valid uri_copy");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
/* skip :// */
|
|
|
|
hostname = tmp + 3;
|
|
|
|
|
|
|
|
tmp = g_utf8_strchr (hostname, -1, '/');
|
|
|
|
if (tmp)
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
ret = g_strdup_printf ("%s://%s%s", scheme, hostname, uri2);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_free (uri_copy);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-25 17:36:48 +00:00
|
|
|
|
|
|
|
guint64
|
|
|
|
gst_m3u8_client_get_current_fragment_duration (GstM3U8Client * client)
|
|
|
|
{
|
|
|
|
guint64 dur;
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
g_return_val_if_fail (client != NULL, 0);
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_LOCK (client);
|
|
|
|
|
|
|
|
list = g_list_find_custom (client->current->files, client,
|
|
|
|
(GCompareFunc) _find_current);
|
|
|
|
if (list == NULL) {
|
|
|
|
dur = -1;
|
|
|
|
} else {
|
|
|
|
dur = GST_M3U8_MEDIA_FILE (list->data)->duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_M3U8_CLIENT_UNLOCK (client);
|
|
|
|
return dur;
|
|
|
|
}
|