2011-02-14 17:51:32 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 Marc-Andre Lureau <marcandre.lureau@gmail.com>
|
2015-12-05 11:12:33 +00:00
|
|
|
* Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
|
2011-02-14 17:51:32 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2015-11-01 13:21:45 +00:00
|
|
|
#include "gsthls.h"
|
2011-02-14 17:51:32 +00:00
|
|
|
#include "m3u8.h"
|
|
|
|
|
2015-11-01 13:21:45 +00:00
|
|
|
#define GST_CAT_DEFAULT hls_debug
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
static GstM3U8MediaFile *gst_m3u8_media_file_new (gchar * uri,
|
2012-02-28 15:40:31 +00:00
|
|
|
gchar * title, GstClockTime duration, guint sequence);
|
2017-11-04 11:15:33 +00:00
|
|
|
static void gst_m3u8_init_file_unref (GstM3U8InitFile * self);
|
2016-11-28 08:49:23 +00:00
|
|
|
static gchar *uri_join (const gchar * uri, const gchar * path);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GstM3U8 *
|
2011-02-14 17:51:32 +00:00
|
|
|
gst_m3u8_new (void)
|
|
|
|
{
|
|
|
|
GstM3U8 *m3u8;
|
|
|
|
|
|
|
|
m3u8 = g_new0 (GstM3U8, 1);
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->current_file = NULL;
|
|
|
|
m3u8->current_file_duration = GST_CLOCK_TIME_NONE;
|
|
|
|
m3u8->sequence = -1;
|
|
|
|
m3u8->sequence_position = 0;
|
|
|
|
m3u8->highest_sequence_number = -1;
|
|
|
|
m3u8->duration = GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
g_mutex_init (&m3u8->lock);
|
|
|
|
m3u8->ref_count = 1;
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
return m3u8;
|
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
/* call with M3U8_LOCK held */
|
2011-02-14 17:51:32 +00:00
|
|
|
static void
|
2015-11-27 19:26:02 +00:00
|
|
|
gst_m3u8_take_uri (GstM3U8 * self, gchar * uri, gchar * base_uri, gchar * name)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (self->uri != uri) {
|
|
|
|
g_free (self->uri);
|
|
|
|
self->uri = uri;
|
|
|
|
}
|
|
|
|
if (self->base_uri != base_uri) {
|
|
|
|
g_free (self->base_uri);
|
|
|
|
self->base_uri = base_uri;
|
|
|
|
}
|
|
|
|
if (self->name != name) {
|
|
|
|
g_free (self->name);
|
|
|
|
self->name = name;
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 08:19:40 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
void
|
|
|
|
gst_m3u8_set_uri (GstM3U8 * m3u8, const gchar * uri, const gchar * base_uri,
|
|
|
|
const gchar * name)
|
|
|
|
{
|
|
|
|
GST_M3U8_LOCK (m3u8);
|
|
|
|
gst_m3u8_take_uri (m3u8, g_strdup (uri), g_strdup (base_uri),
|
|
|
|
g_strdup (name));
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
|
|
|
}
|
|
|
|
|
|
|
|
GstM3U8 *
|
|
|
|
gst_m3u8_ref (GstM3U8 * m3u8)
|
|
|
|
{
|
|
|
|
g_assert (m3u8 != NULL && m3u8->ref_count > 0);
|
2014-05-30 22:34:18 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_atomic_int_add (&m3u8->ref_count, 1);
|
|
|
|
return m3u8;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
void
|
|
|
|
gst_m3u8_unref (GstM3U8 * self)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_if_fail (self != NULL && self->ref_count > 0);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (g_atomic_int_dec_and_test (&self->ref_count)) {
|
|
|
|
g_free (self->uri);
|
|
|
|
g_free (self->base_uri);
|
|
|
|
g_free (self->name);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_list_foreach (self->files, (GFunc) gst_m3u8_media_file_unref, NULL);
|
|
|
|
g_list_free (self->files);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_free (self->last_data);
|
2017-11-25 12:10:11 +00:00
|
|
|
g_mutex_clear (&self->lock);
|
2015-11-27 19:26:02 +00:00
|
|
|
g_free (self);
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-11-27 19:26:02 +00:00
|
|
|
file->ref_count = 1;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GstM3U8MediaFile *
|
|
|
|
gst_m3u8_media_file_ref (GstM3U8MediaFile * mfile)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
g_assert (mfile != NULL && mfile->ref_count > 0);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_atomic_int_add (&mfile->ref_count, 1);
|
|
|
|
return mfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_m3u8_media_file_unref (GstM3U8MediaFile * self)
|
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL && self->ref_count > 0);
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&self->ref_count)) {
|
2017-11-04 11:15:33 +00:00
|
|
|
if (self->init_file)
|
|
|
|
gst_m3u8_init_file_unref (self->init_file);
|
2015-11-27 19:26:02 +00:00
|
|
|
g_free (self->title);
|
|
|
|
g_free (self->uri);
|
|
|
|
g_free (self->key);
|
|
|
|
g_free (self);
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 11:15:33 +00:00
|
|
|
static GstM3U8InitFile *
|
|
|
|
gst_m3u8_init_file_new (gchar * uri)
|
|
|
|
{
|
|
|
|
GstM3U8InitFile *file;
|
|
|
|
|
|
|
|
file = g_new0 (GstM3U8InitFile, 1);
|
|
|
|
file->uri = uri;
|
|
|
|
file->ref_count = 1;
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstM3U8InitFile *
|
|
|
|
gst_m3u8_init_file_ref (GstM3U8InitFile * ifile)
|
|
|
|
{
|
|
|
|
g_assert (ifile != NULL && ifile->ref_count > 0);
|
|
|
|
|
|
|
|
g_atomic_int_add (&ifile->ref_count, 1);
|
|
|
|
return ifile;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_m3u8_init_file_unref (GstM3U8InitFile * self)
|
|
|
|
{
|
|
|
|
g_return_if_fail (self != NULL && self->ref_count > 0);
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&self->ref_count)) {
|
|
|
|
g_free (self->uri);
|
|
|
|
g_free (self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-11-10 16:23:59 +00:00
|
|
|
gchar *end = NULL, *p, *ve;
|
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) {
|
|
|
|
*p = '\0';
|
2015-11-10 16:23:59 +00:00
|
|
|
*v = g_utf8_next_char (*v);
|
|
|
|
if (**v == '"') {
|
|
|
|
ve = g_utf8_next_char (*v);
|
|
|
|
if (ve) {
|
|
|
|
ve = g_utf8_strchr (ve, -1, '"');
|
|
|
|
}
|
|
|
|
if (ve) {
|
|
|
|
*v = g_utf8_next_char (*v);
|
|
|
|
*ve = '\0';
|
|
|
|
} else {
|
|
|
|
GST_WARNING ("Cannot remove quotation marks from %s", *a);
|
|
|
|
}
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
} else {
|
|
|
|
GST_WARNING ("missing = after attribute");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = end;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2015-12-05 11:12:33 +00:00
|
|
|
gst_hls_variant_stream_compare_by_bitrate (gconstpointer a, gconstpointer b)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2015-12-05 11:12:33 +00:00
|
|
|
const GstHLSVariantStream *vs_a = (const GstHLSVariantStream *) a;
|
|
|
|
const GstHLSVariantStream *vs_b = (const GstHLSVariantStream *) b;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
if (vs_a->bandwidth == vs_b->bandwidth)
|
|
|
|
return g_strcmp0 (vs_a->name, vs_b->name);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
return vs_a->bandwidth - vs_b->bandwidth;
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
/* If we have MEDIA-SEQUENCE, ensure that it's consistent. If it is not,
|
|
|
|
* the client SHOULD halt playback (6.3.4), which is what we do then. */
|
2017-01-05 17:10:52 +00:00
|
|
|
static gboolean
|
2017-09-15 06:57:03 +00:00
|
|
|
check_media_seqnums (GstM3U8 * self, GList * previous_files)
|
2017-01-05 17:10:52 +00:00
|
|
|
{
|
2017-09-15 06:57:03 +00:00
|
|
|
GList *l, *m;
|
|
|
|
GstM3U8MediaFile *f1 = NULL, *f2 = NULL;
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
g_return_val_if_fail (previous_files, FALSE);
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-27 07:39:02 +00:00
|
|
|
if (!self->files) {
|
|
|
|
/* Empty playlists are trivially consistent */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-09-27 07:27:12 +00:00
|
|
|
/* Find first case of higher/equal sequence number in new playlist.
|
|
|
|
* From there on we can linearly step ahead */
|
2017-09-15 06:57:03 +00:00
|
|
|
for (l = self->files; l; l = l->next) {
|
|
|
|
gboolean match = FALSE;
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
f1 = l->data;
|
|
|
|
for (m = previous_files; m; m = m->next) {
|
|
|
|
f2 = m->data;
|
|
|
|
|
2017-09-27 07:27:12 +00:00
|
|
|
if (f1->sequence >= f2->sequence) {
|
2017-09-15 06:57:03 +00:00
|
|
|
match = TRUE;
|
2017-01-05 17:10:52 +00:00
|
|
|
break;
|
2017-09-15 06:57:03 +00:00
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
}
|
2017-09-15 06:57:03 +00:00
|
|
|
if (match)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-27 07:39:02 +00:00
|
|
|
/* We must have seen at least one entry on each list */
|
|
|
|
g_assert (f1 != NULL);
|
|
|
|
g_assert (f2 != NULL);
|
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
if (!l) {
|
|
|
|
/* No match, no sequence in the new playlist was higher than
|
2017-09-27 07:27:12 +00:00
|
|
|
* any in the old. This is bad! */
|
2017-09-27 07:17:07 +00:00
|
|
|
GST_ERROR ("Media sequence doesn't continue: last new %" G_GINT64_FORMAT
|
|
|
|
" < last old %" G_GINT64_FORMAT, f1->sequence, f2->sequence);
|
2017-09-15 06:57:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; l && m; l = l->next, m = m->next) {
|
|
|
|
f1 = l->data;
|
|
|
|
f2 = m->data;
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-27 07:27:12 +00:00
|
|
|
if (f1->sequence == f2->sequence && !g_str_equal (f1->uri, f2->uri)) {
|
|
|
|
/* Same sequence, different URI. This is bad! */
|
2017-09-27 07:17:07 +00:00
|
|
|
GST_ERROR ("Media URIs inconsistent (sequence %" G_GINT64_FORMAT
|
|
|
|
"): had '%s', got '%s'", f1->sequence, f2->uri, f1->uri);
|
2017-09-27 07:27:12 +00:00
|
|
|
return FALSE;
|
|
|
|
} else if (f1->sequence < f2->sequence) {
|
|
|
|
/* Not same sequence but by construction sequence must be higher in the
|
|
|
|
* new one. All good in that case, if it isn't then this means that
|
|
|
|
* sequence numbers are decreasing or files were inserted */
|
2017-09-27 07:17:07 +00:00
|
|
|
GST_ERROR ("Media sequences inconsistent: %" G_GINT64_FORMAT " < %"
|
|
|
|
G_GINT64_FORMAT ": URIs new '%s' old '%s'", f1->sequence,
|
|
|
|
f2->sequence, f1->uri, f2->uri);
|
2017-01-05 17:10:52 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2017-09-15 06:57:03 +00:00
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
/* All good if we're getting here */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
/* If we don't have MEDIA-SEQUENCE, we check URIs in the previous and
|
|
|
|
* current playlist to calculate the/a correct MEDIA-SEQUENCE for the new
|
|
|
|
* playlist in relation to the old. That is, same URIs get the same number
|
|
|
|
* and later URIs get higher numbers */
|
|
|
|
static void
|
|
|
|
generate_media_seqnums (GstM3U8 * self, GList * previous_files)
|
|
|
|
{
|
|
|
|
GList *l, *m;
|
|
|
|
GstM3U8MediaFile *f1 = NULL, *f2 = NULL;
|
|
|
|
gint64 mediasequence;
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
g_return_if_fail (previous_files);
|
|
|
|
|
|
|
|
/* Find first case of same URI in new playlist.
|
|
|
|
* From there on we can linearly step ahead */
|
|
|
|
for (l = self->files; l; l = l->next) {
|
|
|
|
gboolean match = FALSE;
|
|
|
|
|
|
|
|
f1 = l->data;
|
|
|
|
for (m = previous_files; m; m = m->next) {
|
|
|
|
f2 = m->data;
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
if (g_str_equal (f1->uri, f2->uri)) {
|
|
|
|
match = TRUE;
|
2017-01-05 17:10:52 +00:00
|
|
|
break;
|
2017-09-15 06:57:03 +00:00
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
if (match)
|
|
|
|
break;
|
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
if (l) {
|
|
|
|
/* Match, check that all following ones are matching too and continue
|
|
|
|
* sequence numbers from there on */
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
mediasequence = f2->sequence;
|
2017-01-05 17:10:52 +00:00
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
for (; l && m; l = l->next, m = m->next) {
|
2017-09-15 06:42:49 +00:00
|
|
|
f1 = l->data;
|
2017-09-15 06:57:03 +00:00
|
|
|
f2 = m->data;
|
2017-09-15 06:42:49 +00:00
|
|
|
|
|
|
|
f1->sequence = mediasequence;
|
|
|
|
mediasequence++;
|
2017-09-15 06:57:03 +00:00
|
|
|
|
|
|
|
if (!g_str_equal (f1->uri, f2->uri)) {
|
2017-09-27 07:17:07 +00:00
|
|
|
GST_WARNING ("Inconsistent URIs after playlist update: '%s' != '%s'",
|
|
|
|
f1->uri, f2->uri);
|
2017-09-15 06:57:03 +00:00
|
|
|
}
|
2017-09-15 06:42:49 +00:00
|
|
|
}
|
2017-09-15 06:57:03 +00:00
|
|
|
} else {
|
|
|
|
/* No match, this means f2 is the last item in the previous playlist
|
|
|
|
* and we have to start our new playlist at that sequence */
|
|
|
|
mediasequence = f2->sequence + 1;
|
|
|
|
l = self->files;
|
2017-01-05 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 06:57:03 +00:00
|
|
|
for (; l; l = l->next) {
|
|
|
|
f1 = l->data;
|
|
|
|
|
|
|
|
f1->sequence = mediasequence;
|
|
|
|
mediasequence++;
|
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
/*
|
|
|
|
* @data: a m3u8 playlist text data, taking ownership
|
|
|
|
*/
|
2015-11-27 19:26:02 +00:00
|
|
|
gboolean
|
|
|
|
gst_m3u8_update (GstM3U8 * self, gchar * data)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
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;
|
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;
|
2015-11-15 17:31:05 +00:00
|
|
|
gint64 mediasequence;
|
2017-01-05 17:10:52 +00:00
|
|
|
GList *previous_files = NULL;
|
|
|
|
gboolean have_mediasequence = FALSE;
|
2017-11-04 11:15:33 +00:00
|
|
|
GstM3U8InitFile *last_init_file = NULL;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_LOCK (self);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
|
|
|
/* 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");
|
|
|
|
g_free (data);
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_UNLOCK (self);
|
2011-02-14 17:51:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_str_has_prefix (data, "#EXTM3U")) {
|
|
|
|
GST_WARNING ("Data doesn't start with #EXTM3U");
|
|
|
|
g_free (data);
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_UNLOCK (self);
|
2011-02-14 17:51:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (g_strrstr (data, "\n#EXT-X-STREAM-INF:") != NULL) {
|
2015-12-05 11:12:33 +00:00
|
|
|
GST_WARNING ("Not a media playlist, but a master playlist!");
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_UNLOCK (self);
|
2015-12-05 11:12:33 +00:00
|
|
|
return FALSE;
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 15:41:05 +00:00
|
|
|
GST_TRACE ("data:\n%s", data);
|
|
|
|
|
2011-02-14 17:51:32 +00:00
|
|
|
g_free (self->last_data);
|
|
|
|
self->last_data = data;
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
self->current_file = NULL;
|
2017-01-05 17:10:52 +00:00
|
|
|
previous_files = self->files;
|
|
|
|
self->files = NULL;
|
2015-11-27 19:26:02 +00:00
|
|
|
self->duration = GST_CLOCK_TIME_NONE;
|
2015-11-15 17:31:05 +00:00
|
|
|
mediasequence = 0;
|
2011-04-01 23:10:37 +00:00
|
|
|
|
2014-05-05 07:34:56 +00:00
|
|
|
/* By default, allow caching */
|
|
|
|
self->allowcache = TRUE;
|
|
|
|
|
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') {
|
2015-11-27 19:26:02 +00:00
|
|
|
if (duration <= 0) {
|
|
|
|
GST_LOG ("%s: got line without EXTINF, dropping", data);
|
2011-02-14 17:51:32 +00:00
|
|
|
goto next_line;
|
|
|
|
}
|
|
|
|
|
2014-05-28 08:19:40 +00:00
|
|
|
data = uri_join (self->base_uri ? self->base_uri : self->uri, data);
|
2015-11-27 19:26:02 +00:00
|
|
|
if (data != NULL) {
|
2011-02-14 17:51:32 +00:00
|
|
|
GstM3U8MediaFile *file;
|
2015-11-15 17:31:05 +00:00
|
|
|
file = gst_m3u8_media_file_new (data, title, duration, 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 {
|
2015-01-07 12:21:33 +00:00
|
|
|
GstM3U8MediaFile *prev = self->files ? self->files->data : NULL;
|
2014-03-05 09:47:01 +00:00
|
|
|
|
|
|
|
if (!prev) {
|
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
offset = prev->offset + prev->size;
|
|
|
|
}
|
2014-09-04 14:49:23 +00:00
|
|
|
file->offset = offset;
|
2014-03-05 09:47:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file->size = -1;
|
|
|
|
file->offset = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-01 16:13:58 +00:00
|
|
|
file->discont = discontinuity;
|
2017-11-04 11:15:33 +00:00
|
|
|
if (last_init_file)
|
|
|
|
file->init_file = gst_m3u8_init_file_ref (last_init_file);
|
2014-03-01 16:13:58 +00:00
|
|
|
|
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;
|
2015-01-07 12:21:33 +00:00
|
|
|
self->files = g_list_prepend (self->files, file);
|
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;
|
2015-08-06 14:30:04 +00:00
|
|
|
if (self->targetduration > 0 && duration > self->targetduration) {
|
|
|
|
GST_WARNING ("EXTINF duration (%" GST_TIME_FORMAT
|
|
|
|
") > TARGETDURATION (%" GST_TIME_FORMAT ")",
|
|
|
|
GST_TIME_ARGS (duration), GST_TIME_ARGS (self->targetduration));
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
if (!data || *data != ',')
|
|
|
|
goto next_line;
|
|
|
|
data = g_utf8_next_char (data);
|
|
|
|
if (data != end) {
|
|
|
|
g_free (title);
|
|
|
|
title = g_strdup (data);
|
|
|
|
}
|
2015-01-07 12:30:00 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-")) {
|
|
|
|
gchar *data_ext_x = data + 7;
|
|
|
|
|
|
|
|
/* All these entries start with #EXT-X- */
|
|
|
|
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;
|
|
|
|
} else if (g_str_has_prefix (data_ext_x, "TARGETDURATION:")) {
|
|
|
|
if (int_from_string (data + 22, &data, &val))
|
|
|
|
self->targetduration = val * GST_SECOND;
|
|
|
|
} else if (g_str_has_prefix (data_ext_x, "MEDIA-SEQUENCE:")) {
|
2017-01-05 17:10:52 +00:00
|
|
|
if (int_from_string (data + 22, &data, &val)) {
|
2015-11-15 17:31:05 +00:00
|
|
|
mediasequence = val;
|
2017-01-05 17:10:52 +00:00
|
|
|
have_mediasequence = TRUE;
|
|
|
|
}
|
2016-10-10 22:41:15 +00:00
|
|
|
} else if (g_str_has_prefix (data_ext_x, "DISCONTINUITY-SEQUENCE:")) {
|
|
|
|
if (int_from_string (data + 30, &data, &val)
|
|
|
|
&& val != self->discont_sequence) {
|
|
|
|
self->discont_sequence = val;
|
|
|
|
discontinuity = TRUE;
|
|
|
|
}
|
2017-03-17 06:34:33 +00:00
|
|
|
} else if (g_str_has_prefix (data_ext_x, "DISCONTINUITY")) {
|
2016-10-10 22:41:15 +00:00
|
|
|
self->discont_sequence++;
|
2015-01-07 12:30:00 +00:00
|
|
|
discontinuity = TRUE;
|
|
|
|
} 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:")) {
|
|
|
|
self->allowcache = g_ascii_strcasecmp (data + 19, "YES") == 0;
|
|
|
|
} else if (g_str_has_prefix (data_ext_x, "KEY:")) {
|
|
|
|
gchar *v, *a;
|
|
|
|
|
|
|
|
data = data + 11;
|
|
|
|
|
|
|
|
/* IV and KEY are only valid until the next #EXT-X-KEY */
|
|
|
|
have_iv = FALSE;
|
|
|
|
g_free (current_key);
|
|
|
|
current_key = NULL;
|
|
|
|
while (data && parse_attributes (&data, &a, &v)) {
|
|
|
|
if (g_str_equal (a, "URI")) {
|
2015-11-10 16:23:59 +00:00
|
|
|
current_key =
|
|
|
|
uri_join (self->base_uri ? self->base_uri : self->uri, v);
|
2015-01-07 12:30:00 +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;
|
|
|
|
|
|
|
|
h = g_ascii_xdigit_value (*ivp);
|
|
|
|
ivp++;
|
|
|
|
l = g_ascii_xdigit_value (*ivp);
|
|
|
|
ivp++;
|
|
|
|
if (h == -1 || l == -1) {
|
|
|
|
i = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iv[i] = (h << 4) | l;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == -1) {
|
|
|
|
GST_WARNING ("Can't read IV");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
have_iv = TRUE;
|
|
|
|
} else if (g_str_equal (a, "METHOD")) {
|
|
|
|
if (!g_str_equal (v, "AES-128")) {
|
|
|
|
GST_WARNING ("Encryption method %s not supported", v);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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 {
|
2014-03-05 09:47:01 +00:00
|
|
|
goto next_line;
|
2015-01-07 12:30:00 +00:00
|
|
|
}
|
2017-11-04 11:15:33 +00:00
|
|
|
} else if (g_str_has_prefix (data_ext_x, "MAP:")) {
|
|
|
|
gchar *v, *a, *header_uri = NULL;
|
|
|
|
|
|
|
|
data = data + 11;
|
|
|
|
|
|
|
|
while (data != NULL && parse_attributes (&data, &a, &v)) {
|
|
|
|
if (strcmp (a, "URI") == 0) {
|
|
|
|
header_uri =
|
|
|
|
uri_join (self->base_uri ? self->base_uri : self->uri, v);
|
|
|
|
} else if (strcmp (a, "BYTERANGE") == 0) {
|
|
|
|
if (int64_from_string (v, &v, &size)) {
|
|
|
|
if (*v == '@' && !int64_from_string (v + 1, &v, &offset)) {
|
|
|
|
g_free (header_uri);
|
|
|
|
goto next_line;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_free (header_uri);
|
|
|
|
goto next_line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header_uri) {
|
|
|
|
GstM3U8InitFile *init_file;
|
|
|
|
init_file = gst_m3u8_init_file_new (header_uri);
|
|
|
|
|
|
|
|
if (size != -1) {
|
|
|
|
init_file->size = size;
|
|
|
|
if (offset != -1)
|
|
|
|
init_file->offset = offset;
|
|
|
|
else
|
|
|
|
init_file->offset = 0;
|
|
|
|
} else {
|
|
|
|
init_file->size = -1;
|
|
|
|
init_file->offset = 0;
|
|
|
|
}
|
|
|
|
if (last_init_file)
|
|
|
|
gst_m3u8_init_file_unref (last_init_file);
|
|
|
|
|
|
|
|
last_init_file = init_file;
|
|
|
|
}
|
2014-03-05 09:47:01 +00:00
|
|
|
} else {
|
2015-01-07 12:30:00 +00:00
|
|
|
GST_LOG ("Ignored line: %s", data);
|
2014-03-05 09:47:01 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2017-01-05 17:10:52 +00:00
|
|
|
self->files = g_list_reverse (self->files);
|
|
|
|
|
2017-11-04 11:15:33 +00:00
|
|
|
if (last_init_file)
|
|
|
|
gst_m3u8_init_file_unref (last_init_file);
|
|
|
|
|
2017-01-05 17:10:52 +00:00
|
|
|
if (previous_files) {
|
2017-09-15 06:57:03 +00:00
|
|
|
gboolean consistent = TRUE;
|
|
|
|
|
|
|
|
if (have_mediasequence) {
|
|
|
|
consistent = check_media_seqnums (self, previous_files);
|
|
|
|
} else {
|
|
|
|
generate_media_seqnums (self, previous_files);
|
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
|
|
|
|
g_list_foreach (previous_files, (GFunc) gst_m3u8_media_file_unref, NULL);
|
|
|
|
g_list_free (previous_files);
|
|
|
|
previous_files = NULL;
|
|
|
|
|
|
|
|
/* error was reported above already */
|
2017-03-17 07:47:07 +00:00
|
|
|
if (!consistent) {
|
|
|
|
GST_M3U8_UNLOCK (self);
|
2017-01-05 17:10:52 +00:00
|
|
|
return FALSE;
|
2017-03-17 07:47:07 +00:00
|
|
|
}
|
2017-01-05 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (self->files == NULL) {
|
|
|
|
GST_ERROR ("Invalid media playlist, it does not contain any media files");
|
|
|
|
GST_M3U8_UNLOCK (self);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2014-04-04 15:45:51 +00:00
|
|
|
/* calculate the start and end times of this media playlist. */
|
2015-11-27 19:26:02 +00:00
|
|
|
{
|
2014-04-04 15:45:51 +00:00
|
|
|
GList *walk;
|
|
|
|
GstM3U8MediaFile *file;
|
|
|
|
GstClockTime duration = 0;
|
|
|
|
|
2017-01-05 17:10:52 +00:00
|
|
|
mediasequence = -1;
|
|
|
|
|
2014-04-04 15:45:51 +00:00
|
|
|
for (walk = self->files; walk; walk = walk->next) {
|
|
|
|
file = walk->data;
|
2017-01-05 17:10:52 +00:00
|
|
|
|
|
|
|
if (mediasequence == -1) {
|
|
|
|
mediasequence = file->sequence;
|
|
|
|
} else if (mediasequence >= file->sequence) {
|
|
|
|
GST_ERROR ("Non-increasing media sequence");
|
|
|
|
GST_M3U8_UNLOCK (self);
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
mediasequence = file->sequence;
|
|
|
|
}
|
|
|
|
|
2014-04-04 15:45:51 +00:00
|
|
|
duration += file->duration;
|
2015-11-27 19:26:02 +00:00
|
|
|
if (file->sequence > self->highest_sequence_number) {
|
|
|
|
if (self->highest_sequence_number >= 0) {
|
2014-04-04 15:45:51 +00:00
|
|
|
/* if an update of the media playlist has been missed, there
|
|
|
|
will be a gap between self->highest_sequence_number and the
|
|
|
|
first sequence number in this media playlist. In this situation
|
|
|
|
assume that the missing fragments had a duration of
|
|
|
|
targetduration each */
|
2015-11-27 19:26:02 +00:00
|
|
|
self->last_file_end +=
|
|
|
|
(file->sequence - self->highest_sequence_number -
|
2014-04-04 15:45:51 +00:00
|
|
|
1) * self->targetduration;
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
self->last_file_end += file->duration;
|
|
|
|
self->highest_sequence_number = file->sequence;
|
2014-04-04 15:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
if (GST_M3U8_IS_LIVE (self)) {
|
|
|
|
self->first_file_start = self->last_file_end - duration;
|
2014-04-04 15:45:51 +00:00
|
|
|
GST_DEBUG ("Live playlist range %" GST_TIME_FORMAT " -> %"
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (self->first_file_start),
|
|
|
|
GST_TIME_ARGS (self->last_file_end));
|
2014-04-04 15:45:51 +00:00
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
self->duration = duration;
|
2014-04-04 15:45:51 +00:00
|
|
|
}
|
2011-08-18 23:54:59 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
/* first-time setup */
|
|
|
|
if (self->files && self->sequence == -1) {
|
|
|
|
GList *file;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (GST_M3U8_IS_LIVE (self)) {
|
|
|
|
gint i;
|
2017-01-26 23:50:10 +00:00
|
|
|
GstClockTime sequence_pos = 0;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
file = g_list_last (self->files);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2017-01-26 23:50:10 +00:00
|
|
|
if (self->last_file_end >= GST_M3U8_MEDIA_FILE (file->data)->duration) {
|
|
|
|
sequence_pos =
|
|
|
|
self->last_file_end - GST_M3U8_MEDIA_FILE (file->data)->duration;
|
|
|
|
}
|
|
|
|
|
2015-02-20 13:55:05 +00:00
|
|
|
/* for live streams, start GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE from
|
2017-01-24 12:32:13 +00:00
|
|
|
* the end of the playlist. See section 6.3.3 of HLS draft */
|
2017-01-26 23:50:10 +00:00
|
|
|
for (i = 0; i < GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE && file->prev &&
|
|
|
|
GST_M3U8_MEDIA_FILE (file->prev->data)->duration <= sequence_pos;
|
|
|
|
++i) {
|
2015-11-27 19:26:02 +00:00
|
|
|
file = file->prev;
|
2017-01-26 23:50:10 +00:00
|
|
|
sequence_pos -= GST_M3U8_MEDIA_FILE (file->data)->duration;
|
|
|
|
}
|
|
|
|
self->sequence_position = sequence_pos;
|
2015-08-07 09:53:23 +00:00
|
|
|
} else {
|
2015-11-27 19:26:02 +00:00
|
|
|
file = g_list_first (self->files);
|
2017-01-26 23:50:10 +00:00
|
|
|
self->sequence_position = 0;
|
2015-08-07 09:53:23 +00:00
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
self->current_file = file;
|
|
|
|
self->sequence = GST_M3U8_MEDIA_FILE (file->data)->sequence;
|
|
|
|
GST_DEBUG ("first sequence: %u", (guint) self->sequence);
|
2011-02-14 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_LOG ("processed media playlist %s, %u fragments", self->name,
|
|
|
|
g_list_length (self->files));
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_UNLOCK (self);
|
2014-05-30 22:34:18 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
return TRUE;
|
2014-05-30 22:34:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
/* call with M3U8_LOCK held */
|
2015-01-08 18:46:49 +00:00
|
|
|
static GList *
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8_find_next_fragment (GstM3U8 * m3u8, gboolean forward)
|
2015-01-08 11:46:48 +00:00
|
|
|
{
|
|
|
|
GstM3U8MediaFile *file;
|
2015-11-27 19:26:02 +00:00
|
|
|
GList *l = m3u8->files;
|
2015-01-08 11:46:48 +00:00
|
|
|
|
2015-01-08 18:46:49 +00:00
|
|
|
if (forward) {
|
|
|
|
while (l) {
|
|
|
|
file = l->data;
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (file->sequence >= m3u8->sequence)
|
2015-01-08 18:46:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
l = l->next;
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-08 11:46:48 +00:00
|
|
|
l = g_list_last (l);
|
|
|
|
|
2015-01-08 18:46:49 +00:00
|
|
|
while (l) {
|
|
|
|
file = l->data;
|
2015-01-08 11:46:48 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (file->sequence <= m3u8->sequence)
|
2015-01-08 18:46:49 +00:00
|
|
|
break;
|
2015-01-08 11:46:48 +00:00
|
|
|
|
2015-01-08 18:46:49 +00:00
|
|
|
l = l->prev;
|
|
|
|
}
|
2015-01-08 11:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 18:46:49 +00:00
|
|
|
return l;
|
2015-01-08 11:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GstM3U8MediaFile *
|
|
|
|
gst_m3u8_get_next_fragment (GstM3U8 * m3u8, gboolean forward,
|
|
|
|
GstClockTime * sequence_position, gboolean * discont)
|
2011-02-14 17:51:32 +00:00
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
GstM3U8MediaFile *file = NULL;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_val_if_fail (m3u8 != NULL, NULL);
|
2014-03-26 06:25:13 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_LOCK (m3u8);
|
2014-03-09 18:31:31 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_DEBUG ("Looking for fragment %" G_GINT64_FORMAT, m3u8->sequence);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (m3u8->sequence < 0) /* can't happen really */
|
|
|
|
goto out;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (m3u8->current_file == NULL)
|
|
|
|
m3u8->current_file = m3u8_find_next_fragment (m3u8, forward);
|
2015-01-08 19:24:29 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (m3u8->current_file == NULL)
|
|
|
|
goto out;
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
file = gst_m3u8_media_file_ref (m3u8->current_file->data);
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_DEBUG ("Got fragment with sequence %u (current sequence %u)",
|
|
|
|
(guint) file->sequence, (guint) m3u8->sequence);
|
2014-03-26 06:25:13 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
if (sequence_position)
|
|
|
|
*sequence_position = m3u8->sequence_position;
|
|
|
|
if (discont)
|
|
|
|
*discont = file->discont || (m3u8->sequence != file->sequence);
|
2015-11-27 19:26:02 +00:00
|
|
|
|
|
|
|
m3u8->current_file_duration = file->duration;
|
|
|
|
m3u8->sequence = file->sequence;
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
|
|
|
|
|
|
|
return file;
|
2014-02-17 08:19:32 +00:00
|
|
|
}
|
2011-02-14 17:51:32 +00:00
|
|
|
|
2015-01-08 11:46:48 +00:00
|
|
|
gboolean
|
2015-11-27 19:26:02 +00:00
|
|
|
gst_m3u8_has_next_fragment (GstM3U8 * m3u8, gboolean forward)
|
2015-01-08 11:46:48 +00:00
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
gboolean have_next;
|
|
|
|
GList *cur;
|
2015-01-08 11:46:48 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_val_if_fail (m3u8 != NULL, FALSE);
|
2015-01-08 11:46:48 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_LOCK (m3u8);
|
|
|
|
|
|
|
|
GST_DEBUG ("Checking next fragment %" G_GINT64_FORMAT,
|
|
|
|
m3u8->sequence + (forward ? 1 : -1));
|
|
|
|
|
|
|
|
if (m3u8->current_file) {
|
|
|
|
cur = m3u8->current_file;
|
2015-01-08 19:24:29 +00:00
|
|
|
} else {
|
2015-11-27 19:26:02 +00:00
|
|
|
cur = m3u8_find_next_fragment (m3u8, forward);
|
2015-01-08 19:24:29 +00:00
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2016-11-12 08:38:04 +00:00
|
|
|
have_next = cur && ((forward && cur->next) || (!forward && cur->prev));
|
2015-11-27 19:26:02 +00:00
|
|
|
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
|
|
|
|
|
|
|
return have_next;
|
2015-01-08 11:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
/* call with M3U8_LOCK held */
|
2015-05-28 12:30:46 +00:00
|
|
|
static void
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8_alternate_advance (GstM3U8 * m3u8, gboolean forward)
|
2015-05-28 12:30:46 +00:00
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
gint targetnum = m3u8->sequence;
|
2015-05-28 12:30:46 +00:00
|
|
|
GList *tmp;
|
|
|
|
GstM3U8MediaFile *mf;
|
|
|
|
|
|
|
|
/* figure out the target seqnum */
|
|
|
|
if (forward)
|
|
|
|
targetnum += 1;
|
|
|
|
else
|
|
|
|
targetnum -= 1;
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
for (tmp = m3u8->files; tmp; tmp = tmp->next) {
|
2015-05-28 12:30:46 +00:00
|
|
|
mf = (GstM3U8MediaFile *) tmp->data;
|
|
|
|
if (mf->sequence == targetnum)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tmp == NULL) {
|
2015-07-13 18:37:26 +00:00
|
|
|
GST_WARNING ("Can't find next fragment");
|
2015-05-28 12:30:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->current_file = tmp;
|
|
|
|
m3u8->sequence = targetnum;
|
|
|
|
m3u8->current_file_duration = GST_M3U8_MEDIA_FILE (tmp->data)->duration;
|
2015-05-28 12:30:46 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 08:19:32 +00:00
|
|
|
void
|
2015-11-27 19:26:02 +00:00
|
|
|
gst_m3u8_advance_fragment (GstM3U8 * m3u8, gboolean forward)
|
2014-02-17 08:19:32 +00:00
|
|
|
{
|
|
|
|
GstM3U8MediaFile *file;
|
2011-08-16 19:43:08 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_if_fail (m3u8 != NULL);
|
|
|
|
|
|
|
|
GST_M3U8_LOCK (m3u8);
|
2014-02-17 08:19:32 +00:00
|
|
|
|
2015-07-08 15:17:12 +00:00
|
|
|
GST_DEBUG ("Sequence position was %" GST_TIME_FORMAT,
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_TIME_ARGS (m3u8->sequence_position));
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (m3u8->current_file_duration)) {
|
2015-07-08 15:17:12 +00:00
|
|
|
/* Advance our position based on the previous fragment we played */
|
|
|
|
if (forward)
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->sequence_position += m3u8->current_file_duration;
|
|
|
|
else if (m3u8->current_file_duration < m3u8->sequence_position)
|
|
|
|
m3u8->sequence_position -= m3u8->current_file_duration;
|
2015-07-08 15:17:12 +00:00
|
|
|
else
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->sequence_position = 0;
|
|
|
|
m3u8->current_file_duration = GST_CLOCK_TIME_NONE;
|
2015-07-08 15:17:12 +00:00
|
|
|
GST_DEBUG ("Sequence position now %" GST_TIME_FORMAT,
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_TIME_ARGS (m3u8->sequence_position));
|
2015-07-08 15:17:12 +00:00
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
if (!m3u8->current_file) {
|
2015-01-08 19:24:29 +00:00
|
|
|
GList *l;
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_DEBUG ("Looking for fragment %" G_GINT64_FORMAT, m3u8->sequence);
|
|
|
|
for (l = m3u8->files; l != NULL; l = l->next) {
|
|
|
|
if (GST_M3U8_MEDIA_FILE (l->data)->sequence == m3u8->sequence) {
|
|
|
|
m3u8->current_file = l;
|
2015-09-02 15:40:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
if (m3u8->current_file == NULL) {
|
2015-05-28 12:30:46 +00:00
|
|
|
GST_DEBUG
|
|
|
|
("Could not find current fragment, trying next fragment directly");
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8_alternate_advance (m3u8, forward);
|
2015-12-03 09:46:10 +00:00
|
|
|
|
|
|
|
/* Resync sequence number if the above has failed for live streams */
|
2015-11-27 19:26:02 +00:00
|
|
|
if (m3u8->current_file == NULL && GST_M3U8_IS_LIVE (m3u8)) {
|
2015-12-03 09:46:10 +00:00
|
|
|
/* for live streams, start GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE from
|
|
|
|
the end of the playlist. See section 6.3.3 of HLS draft */
|
|
|
|
gint pos =
|
2015-11-27 19:26:02 +00:00
|
|
|
g_list_length (m3u8->files) - GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE;
|
|
|
|
m3u8->current_file = g_list_nth (m3u8->files, pos >= 0 ? pos : 0);
|
|
|
|
m3u8->current_file_duration =
|
|
|
|
GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->duration;
|
2015-12-03 09:46:10 +00:00
|
|
|
|
|
|
|
GST_WARNING ("Resyncing live playlist");
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
goto out;
|
2015-01-08 19:24:29 +00:00
|
|
|
}
|
2014-02-17 08:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
file = GST_M3U8_MEDIA_FILE (m3u8->current_file->data);
|
2014-03-09 18:31:31 +00:00
|
|
|
GST_DEBUG ("Advancing from sequence %u", (guint) file->sequence);
|
|
|
|
if (forward) {
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->current_file = m3u8->current_file->next;
|
|
|
|
if (m3u8->current_file) {
|
|
|
|
m3u8->sequence = GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->sequence;
|
2015-01-08 19:24:29 +00:00
|
|
|
} else {
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->sequence = file->sequence + 1;
|
2015-01-08 19:24:29 +00:00
|
|
|
}
|
2014-03-09 18:31:31 +00:00
|
|
|
} else {
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->current_file = m3u8->current_file->prev;
|
|
|
|
if (m3u8->current_file) {
|
|
|
|
m3u8->sequence = GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->sequence;
|
2015-01-08 19:24:29 +00:00
|
|
|
} else {
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->sequence = file->sequence - 1;
|
2015-01-08 19:24:29 +00:00
|
|
|
}
|
2015-07-08 15:17:12 +00:00
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
if (m3u8->current_file) {
|
2015-07-08 15:17:12 +00:00
|
|
|
/* Store duration of the fragment we're using to update the position
|
|
|
|
* the next time we advance */
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->current_file_duration =
|
|
|
|
GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->duration;
|
2014-03-09 18:31:31 +00:00
|
|
|
}
|
2011-03-12 11:20:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
out:
|
|
|
|
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
2011-03-12 11:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstClockTime
|
2015-11-27 19:26:02 +00:00
|
|
|
gst_m3u8_get_duration (GstM3U8 * m3u8)
|
2011-03-12 11:20:32 +00:00
|
|
|
{
|
2015-01-08 16:03:11 +00:00
|
|
|
GstClockTime duration = GST_CLOCK_TIME_NONE;
|
2011-03-12 11:20:32 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_val_if_fail (m3u8 != NULL, GST_CLOCK_TIME_NONE);
|
|
|
|
|
|
|
|
GST_M3U8_LOCK (m3u8);
|
2011-03-12 11:20:32 +00:00
|
|
|
|
2017-03-14 05:49:25 +00:00
|
|
|
/* We can only get the duration for on-demand streams */
|
|
|
|
if (!m3u8->endlist)
|
|
|
|
goto out;
|
2015-01-08 16:03:11 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (m3u8->duration) && m3u8->files != NULL) {
|
|
|
|
GList *f;
|
2015-01-08 16:03:11 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
m3u8->duration = 0;
|
|
|
|
for (f = m3u8->files; f != NULL; f = f->next)
|
|
|
|
m3u8->duration += GST_M3U8_MEDIA_FILE (f)->duration;
|
|
|
|
}
|
|
|
|
duration = m3u8->duration;
|
2011-03-12 11:28:42 +00:00
|
|
|
|
2017-03-14 05:49:25 +00:00
|
|
|
out:
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
2011-09-02 23:48:22 +00:00
|
|
|
|
2012-02-28 15:40:31 +00:00
|
|
|
return duration;
|
2011-09-02 23:48:22 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GstClockTime
|
|
|
|
gst_m3u8_get_target_duration (GstM3U8 * m3u8)
|
2011-03-12 11:28:42 +00:00
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
GstClockTime target_duration;
|
2011-09-02 23:48:03 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_val_if_fail (m3u8 != NULL, GST_CLOCK_TIME_NONE);
|
2011-03-12 11:28:42 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_LOCK (m3u8);
|
|
|
|
target_duration = m3u8->targetduration;
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
|
|
|
|
|
|
|
return target_duration;
|
2011-03-12 11:28:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:37:57 +00:00
|
|
|
gchar *
|
2015-11-27 19:26:02 +00:00
|
|
|
gst_m3u8_get_uri (GstM3U8 * m3u8)
|
2011-09-02 23:48:22 +00:00
|
|
|
{
|
2014-10-02 16:37:57 +00:00
|
|
|
gchar *uri;
|
2011-09-02 23:48:22 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_LOCK (m3u8);
|
|
|
|
uri = g_strdup (m3u8->uri);
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
2011-09-02 23:48:22 +00:00
|
|
|
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2011-03-12 11:28:42 +00:00
|
|
|
gboolean
|
2015-11-27 19:26:02 +00:00
|
|
|
gst_m3u8_is_live (GstM3U8 * m3u8)
|
2011-03-12 11:28:42 +00:00
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
gboolean is_live;
|
2011-09-02 23:48:03 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_val_if_fail (m3u8 != NULL, FALSE);
|
2011-03-12 11:28:42 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_LOCK (m3u8);
|
|
|
|
is_live = GST_M3U8_IS_LIVE (m3u8);
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
|
|
|
|
|
|
|
return is_live;
|
2011-03-12 11:28:42 +00:00
|
|
|
}
|
2012-05-10 14:10:14 +00:00
|
|
|
|
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
|
|
|
|
2014-04-04 15:45:51 +00:00
|
|
|
gboolean
|
2015-11-27 19:26:02 +00:00
|
|
|
gst_m3u8_get_seek_range (GstM3U8 * m3u8, gint64 * start, gint64 * stop)
|
2014-04-04 15:45:51 +00:00
|
|
|
{
|
|
|
|
GstClockTime duration = 0;
|
|
|
|
GList *walk;
|
|
|
|
GstM3U8MediaFile *file;
|
|
|
|
guint count;
|
2015-11-10 16:19:34 +00:00
|
|
|
guint min_distance = 0;
|
2014-04-04 15:45:51 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
g_return_val_if_fail (m3u8 != NULL, FALSE);
|
2014-04-04 15:45:51 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_M3U8_LOCK (m3u8);
|
2014-04-04 15:45:51 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (m3u8->files == NULL)
|
|
|
|
goto out;
|
2014-04-04 15:45:51 +00:00
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (GST_M3U8_IS_LIVE (m3u8)) {
|
2015-11-10 16:19:34 +00:00
|
|
|
/* min_distance is used to make sure the seek range is never closer than
|
|
|
|
GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE fragments from the end of a live
|
|
|
|
playlist - see 6.3.3. "Playing the Playlist file" of the HLS draft */
|
|
|
|
min_distance = GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE;
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
count = g_list_length (m3u8->files);
|
2014-04-04 15:45:51 +00:00
|
|
|
|
2017-01-24 12:32:13 +00:00
|
|
|
for (walk = m3u8->files; walk && count > min_distance; walk = walk->next) {
|
2014-04-04 15:45:51 +00:00
|
|
|
file = walk->data;
|
|
|
|
--count;
|
|
|
|
duration += file->duration;
|
|
|
|
}
|
|
|
|
|
2015-11-27 19:26:02 +00:00
|
|
|
if (duration <= 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
*start = m3u8->first_file_start;
|
|
|
|
*stop = *start + duration;
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
GST_M3U8_UNLOCK (m3u8);
|
|
|
|
return (duration > 0);
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
GstHLSMedia *
|
|
|
|
gst_hls_media_ref (GstHLSMedia * media)
|
2015-11-27 19:26:02 +00:00
|
|
|
{
|
2015-12-05 11:12:33 +00:00
|
|
|
g_assert (media != NULL && media->ref_count > 0);
|
|
|
|
g_atomic_int_add (&media->ref_count, 1);
|
|
|
|
return media;
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
void
|
|
|
|
gst_hls_media_unref (GstHLSMedia * media)
|
|
|
|
{
|
|
|
|
g_assert (media != NULL && media->ref_count > 0);
|
|
|
|
if (g_atomic_int_dec_and_test (&media->ref_count)) {
|
2017-02-02 12:06:20 +00:00
|
|
|
if (media->playlist)
|
|
|
|
gst_m3u8_unref (media->playlist);
|
2015-12-05 11:12:33 +00:00
|
|
|
g_free (media->group_id);
|
|
|
|
g_free (media->name);
|
|
|
|
g_free (media->uri);
|
2017-02-02 12:06:20 +00:00
|
|
|
g_free (media->lang);
|
2015-12-05 11:12:33 +00:00
|
|
|
g_free (media);
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
static GstHLSMediaType
|
|
|
|
gst_m3u8_get_hls_media_type_from_string (const gchar * type_name)
|
|
|
|
{
|
|
|
|
if (strcmp (type_name, "AUDIO") == 0)
|
|
|
|
return GST_HLS_MEDIA_TYPE_AUDIO;
|
|
|
|
if (strcmp (type_name, "VIDEO") == 0)
|
|
|
|
return GST_HLS_MEDIA_TYPE_VIDEO;
|
|
|
|
if (strcmp (type_name, "SUBTITLES") == 0)
|
|
|
|
return GST_HLS_MEDIA_TYPE_SUBTITLES;
|
|
|
|
if (strcmp (type_name, "CLOSED_CAPTIONS") == 0)
|
|
|
|
return GST_HLS_MEDIA_TYPE_CLOSED_CAPTIONS;
|
|
|
|
|
|
|
|
return GST_HLS_MEDIA_TYPE_INVALID;
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
#define GST_HLS_MEDIA_TYPE_NAME(mtype) gst_m3u8_hls_media_type_get_nick(mtype)
|
|
|
|
static inline const gchar *
|
|
|
|
gst_m3u8_hls_media_type_get_nick (GstHLSMediaType mtype)
|
|
|
|
{
|
|
|
|
static const gchar *nicks[GST_HLS_N_MEDIA_TYPES] = { "audio", "video",
|
|
|
|
"subtitle", "closed-captions"
|
|
|
|
};
|
|
|
|
|
2016-08-04 12:21:16 +00:00
|
|
|
if (mtype < 0 || mtype >= GST_HLS_N_MEDIA_TYPES)
|
2015-12-05 11:12:33 +00:00
|
|
|
return "invalid";
|
|
|
|
|
|
|
|
return nicks[mtype];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns unquoted copy of string */
|
|
|
|
static gchar *
|
|
|
|
gst_m3u8_unquote (const gchar * str)
|
|
|
|
{
|
|
|
|
const gchar *start, *end;
|
|
|
|
|
|
|
|
start = strchr (str, '"');
|
|
|
|
if (start == NULL)
|
|
|
|
return g_strdup (str);
|
|
|
|
end = strchr (start + 1, '"');
|
|
|
|
if (end == NULL) {
|
|
|
|
GST_WARNING ("Broken quoted string [%s] - can't find end quote", str);
|
|
|
|
return g_strdup (start + 1);
|
|
|
|
}
|
|
|
|
return g_strndup (start + 1, (gsize) (end - (start + 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstHLSMedia *
|
|
|
|
gst_m3u8_parse_media (gchar * desc, const gchar * base_uri)
|
|
|
|
{
|
|
|
|
GstHLSMedia *media;
|
|
|
|
gchar *a, *v;
|
|
|
|
|
|
|
|
media = g_new0 (GstHLSMedia, 1);
|
|
|
|
media->ref_count = 1;
|
|
|
|
media->playlist = gst_m3u8_new ();
|
2017-02-28 13:31:53 +00:00
|
|
|
media->mtype = GST_HLS_MEDIA_TYPE_INVALID;
|
2015-12-05 11:12:33 +00:00
|
|
|
|
|
|
|
GST_LOG ("parsing %s", desc);
|
|
|
|
while (desc != NULL && parse_attributes (&desc, &a, &v)) {
|
|
|
|
if (strcmp (a, "TYPE") == 0) {
|
|
|
|
media->mtype = gst_m3u8_get_hls_media_type_from_string (v);
|
|
|
|
} else if (strcmp (a, "GROUP-ID") == 0) {
|
|
|
|
g_free (media->group_id);
|
|
|
|
media->group_id = gst_m3u8_unquote (v);
|
|
|
|
} else if (strcmp (a, "NAME") == 0) {
|
|
|
|
g_free (media->name);
|
|
|
|
media->name = gst_m3u8_unquote (v);
|
|
|
|
} else if (strcmp (a, "URI") == 0) {
|
|
|
|
gchar *uri;
|
|
|
|
|
|
|
|
g_free (media->uri);
|
|
|
|
uri = gst_m3u8_unquote (v);
|
|
|
|
media->uri = uri_join (base_uri, uri);
|
|
|
|
g_free (uri);
|
|
|
|
} else if (strcmp (a, "LANGUAGE") == 0) {
|
|
|
|
g_free (media->lang);
|
|
|
|
media->lang = gst_m3u8_unquote (v);
|
|
|
|
} else if (strcmp (a, "DEFAULT") == 0) {
|
|
|
|
media->is_default = g_ascii_strcasecmp (v, "yes") == 0;
|
|
|
|
} else if (strcmp (a, "FORCED") == 0) {
|
|
|
|
media->forced = g_ascii_strcasecmp (v, "yes") == 0;
|
|
|
|
} else if (strcmp (a, "AUTOSELECT") == 0) {
|
|
|
|
media->autoselect = g_ascii_strcasecmp (v, "yes") == 0;
|
|
|
|
} else {
|
|
|
|
/* unhandled: ASSOC-LANGUAGE, INSTREAM-ID, CHARACTERISTICS */
|
|
|
|
GST_FIXME ("EXT-X-MEDIA: unhandled attribute: %s = %s", a, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (media->mtype == GST_HLS_MEDIA_TYPE_INVALID)
|
|
|
|
goto required_attributes_missing;
|
|
|
|
|
|
|
|
if (media->uri == NULL)
|
|
|
|
goto existing_stream;
|
|
|
|
|
|
|
|
if (media->group_id == NULL || media->name == NULL)
|
|
|
|
goto required_attributes_missing;
|
|
|
|
|
2017-02-28 13:31:53 +00:00
|
|
|
if (media->mtype == GST_HLS_MEDIA_TYPE_CLOSED_CAPTIONS)
|
2016-08-04 10:08:30 +00:00
|
|
|
goto uri_with_cc;
|
2015-12-05 11:12:33 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("media: %s, group '%s', name '%s', uri '%s', %s %s %s, lang=%s",
|
|
|
|
GST_HLS_MEDIA_TYPE_NAME (media->mtype), media->group_id, media->name,
|
|
|
|
media->uri, media->is_default ? "default" : "-",
|
|
|
|
media->autoselect ? "autoselect" : "-",
|
|
|
|
media->forced ? "forced" : "-", media->lang ? media->lang : "??");
|
|
|
|
|
|
|
|
return media;
|
|
|
|
|
|
|
|
uri_with_cc:
|
|
|
|
{
|
|
|
|
GST_WARNING ("closed captions EXT-X-MEDIA should not have URI specified");
|
|
|
|
goto out_error;
|
|
|
|
}
|
|
|
|
required_attributes_missing:
|
|
|
|
{
|
|
|
|
GST_WARNING ("EXT-X-MEDIA description is missing required attributes");
|
|
|
|
goto out_error;
|
|
|
|
/* fall through */
|
|
|
|
}
|
|
|
|
existing_stream:
|
|
|
|
{
|
|
|
|
GST_DEBUG ("EXT-X-MEDIA without URI, describes embedded stream, skipping");
|
|
|
|
/* fall through */
|
|
|
|
}
|
|
|
|
|
|
|
|
out_error:
|
|
|
|
{
|
|
|
|
gst_hls_media_unref (media);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstHLSVariantStream *
|
|
|
|
gst_hls_variant_stream_new (void)
|
|
|
|
{
|
|
|
|
GstHLSVariantStream *stream;
|
|
|
|
|
|
|
|
stream = g_new0 (GstHLSVariantStream, 1);
|
|
|
|
stream->m3u8 = gst_m3u8_new ();
|
|
|
|
stream->refcount = 1;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstHLSVariantStream *
|
|
|
|
gst_hls_variant_stream_ref (GstHLSVariantStream * stream)
|
|
|
|
{
|
|
|
|
g_atomic_int_inc (&stream->refcount);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_hls_variant_stream_unref (GstHLSVariantStream * stream)
|
|
|
|
{
|
|
|
|
if (g_atomic_int_dec_and_test (&stream->refcount)) {
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_free (stream->name);
|
|
|
|
g_free (stream->uri);
|
|
|
|
g_free (stream->codecs);
|
|
|
|
gst_m3u8_unref (stream->m3u8);
|
|
|
|
for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
|
|
|
|
g_free (stream->media_groups[i]);
|
|
|
|
g_list_free_full (stream->media[i], (GDestroyNotify) gst_hls_media_unref);
|
|
|
|
}
|
|
|
|
g_free (stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstHLSVariantStream *
|
|
|
|
find_variant_stream_by_name (GList * list, const gchar * name)
|
|
|
|
{
|
|
|
|
for (; list != NULL; list = list->next) {
|
|
|
|
GstHLSVariantStream *variant_stream = list->data;
|
|
|
|
|
|
|
|
if (variant_stream->name != NULL && !strcmp (variant_stream->name, name))
|
|
|
|
return variant_stream;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstHLSVariantStream *
|
|
|
|
find_variant_stream_by_uri (GList * list, const gchar * uri)
|
|
|
|
{
|
|
|
|
for (; list != NULL; list = list->next) {
|
|
|
|
GstHLSVariantStream *variant_stream = list->data;
|
|
|
|
|
|
|
|
if (variant_stream->uri != NULL && !strcmp (variant_stream->uri, uri))
|
|
|
|
return variant_stream;
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
2015-12-05 11:12:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstHLSMasterPlaylist *
|
|
|
|
gst_hls_master_playlist_new (void)
|
|
|
|
{
|
|
|
|
GstHLSMasterPlaylist *playlist;
|
|
|
|
|
|
|
|
playlist = g_new0 (GstHLSMasterPlaylist, 1);
|
|
|
|
playlist->refcount = 1;
|
|
|
|
playlist->is_simple = FALSE;
|
|
|
|
|
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_hls_master_playlist_unref (GstHLSMasterPlaylist * playlist)
|
|
|
|
{
|
|
|
|
if (g_atomic_int_dec_and_test (&playlist->refcount)) {
|
|
|
|
g_list_free_full (playlist->variants,
|
|
|
|
(GDestroyNotify) gst_hls_variant_stream_unref);
|
|
|
|
g_list_free_full (playlist->iframe_variants,
|
|
|
|
(GDestroyNotify) gst_hls_variant_stream_unref);
|
2017-02-02 12:06:20 +00:00
|
|
|
if (playlist->default_variant)
|
|
|
|
gst_hls_variant_stream_unref (playlist->default_variant);
|
2015-12-05 11:12:33 +00:00
|
|
|
g_free (playlist->last_data);
|
|
|
|
g_free (playlist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
hls_media_name_compare_func (gconstpointer media, gconstpointer name)
|
|
|
|
{
|
|
|
|
return strcmp (((GstHLSMedia *) media)->name, (const gchar *) name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Takes ownership of @data */
|
|
|
|
GstHLSMasterPlaylist *
|
|
|
|
gst_hls_master_playlist_new_from_data (gchar * data, const gchar * base_uri)
|
|
|
|
{
|
|
|
|
GHashTable *media_groups[GST_HLS_N_MEDIA_TYPES] = { NULL, };
|
|
|
|
GstHLSMasterPlaylist *playlist;
|
|
|
|
GstHLSVariantStream *pending_stream;
|
|
|
|
gchar *end, *free_data = data;
|
|
|
|
gint val, i;
|
|
|
|
GList *l;
|
2015-11-27 19:26:02 +00:00
|
|
|
|
|
|
|
if (!g_str_has_prefix (data, "#EXTM3U")) {
|
|
|
|
GST_WARNING ("Data doesn't start with #EXTM3U");
|
2015-12-05 11:12:33 +00:00
|
|
|
g_free (free_data);
|
|
|
|
return NULL;
|
2014-04-04 15:45:51 +00:00
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
playlist = gst_hls_master_playlist_new ();
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
/* store data before we modify it for parsing */
|
|
|
|
playlist->last_data = g_strdup (data);
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
GST_TRACE ("data:\n%s", data);
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
if (strstr (data, "\n#EXTINF:") != NULL) {
|
|
|
|
GST_INFO ("This is a simple media playlist, not a master playlist");
|
|
|
|
|
|
|
|
pending_stream = gst_hls_variant_stream_new ();
|
|
|
|
pending_stream->name = g_strdup (base_uri);
|
|
|
|
pending_stream->uri = g_strdup (base_uri);
|
|
|
|
gst_m3u8_set_uri (pending_stream->m3u8, base_uri, NULL, base_uri);
|
|
|
|
playlist->variants = g_list_append (playlist->variants, pending_stream);
|
|
|
|
playlist->default_variant = gst_hls_variant_stream_ref (pending_stream);
|
|
|
|
playlist->is_simple = TRUE;
|
|
|
|
|
|
|
|
if (!gst_m3u8_update (pending_stream->m3u8, data)) {
|
|
|
|
GST_WARNING ("Failed to parse media playlist");
|
|
|
|
gst_hls_master_playlist_unref (playlist);
|
|
|
|
playlist = NULL;
|
|
|
|
}
|
|
|
|
return playlist;
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
pending_stream = NULL;
|
2015-11-27 19:26:02 +00:00
|
|
|
data += 7;
|
|
|
|
while (TRUE) {
|
|
|
|
gchar *r;
|
|
|
|
|
|
|
|
end = g_utf8_strchr (data, -1, '\n');
|
|
|
|
if (end)
|
|
|
|
*end = '\0';
|
|
|
|
|
|
|
|
r = g_utf8_strchr (data, -1, '\r');
|
|
|
|
if (r)
|
|
|
|
*r = '\0';
|
|
|
|
|
|
|
|
if (data[0] != '#' && data[0] != '\0') {
|
2015-12-05 11:12:33 +00:00
|
|
|
gchar *name, *uri;
|
2015-11-27 19:26:02 +00:00
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
if (pending_stream == NULL) {
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_LOG ("%s: got line without EXT-STREAM-INF, dropping", data);
|
|
|
|
goto next_line;
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
name = data;
|
|
|
|
uri = uri_join (base_uri, name);
|
|
|
|
if (uri == NULL)
|
2015-11-27 19:26:02 +00:00
|
|
|
goto next_line;
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
pending_stream->name = g_strdup (name);
|
|
|
|
pending_stream->uri = uri;
|
|
|
|
|
|
|
|
if (find_variant_stream_by_name (playlist->variants, name)
|
|
|
|
|| find_variant_stream_by_uri (playlist->variants, uri)) {
|
|
|
|
GST_DEBUG ("Already have a list with this name or URI: %s", name);
|
|
|
|
gst_hls_variant_stream_unref (pending_stream);
|
2015-11-27 19:26:02 +00:00
|
|
|
} else {
|
2015-12-05 11:12:33 +00:00
|
|
|
GST_INFO ("stream %s @ %u: %s", name, pending_stream->bandwidth, uri);
|
|
|
|
gst_m3u8_set_uri (pending_stream->m3u8, uri, NULL, name);
|
|
|
|
playlist->variants = g_list_append (playlist->variants, pending_stream);
|
|
|
|
/* use first stream in the playlist as default */
|
|
|
|
if (playlist->default_variant == NULL) {
|
|
|
|
playlist->default_variant =
|
|
|
|
gst_hls_variant_stream_ref (pending_stream);
|
|
|
|
}
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
2015-12-05 11:12:33 +00:00
|
|
|
pending_stream = NULL;
|
2015-11-27 19:26:02 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-VERSION:")) {
|
|
|
|
if (int_from_string (data + 15, &data, &val))
|
2015-12-05 11:12:33 +00:00
|
|
|
playlist->version = val;
|
2015-11-27 19:26:02 +00:00
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-STREAM-INF:") ||
|
|
|
|
g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:")) {
|
2015-12-05 11:12:33 +00:00
|
|
|
GstHLSVariantStream *stream;
|
2015-11-27 19:26:02 +00:00
|
|
|
gchar *v, *a;
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
stream = gst_hls_variant_stream_new ();
|
|
|
|
stream->iframe = g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:");
|
|
|
|
data += stream->iframe ? 26 : 18;
|
2015-11-27 19:26:02 +00:00
|
|
|
while (data && parse_attributes (&data, &a, &v)) {
|
|
|
|
if (g_str_equal (a, "BANDWIDTH")) {
|
2017-11-25 13:24:39 +00:00
|
|
|
if (!stream->bandwidth) {
|
|
|
|
if (!int_from_string (v, NULL, &stream->bandwidth))
|
|
|
|
GST_WARNING ("Error while reading BANDWIDTH");
|
|
|
|
}
|
|
|
|
} else if (g_str_equal (a, "AVERAGE-BANDWIDTH")) {
|
|
|
|
GST_DEBUG
|
|
|
|
("AVERAGE-BANDWIDTH attribute available. Using it as stream bandwidth");
|
2015-12-05 11:12:33 +00:00
|
|
|
if (!int_from_string (v, NULL, &stream->bandwidth))
|
2017-11-25 13:24:39 +00:00
|
|
|
GST_WARNING ("Error while reading AVERAGE-BANDWIDTH");
|
2015-11-27 19:26:02 +00:00
|
|
|
} else if (g_str_equal (a, "PROGRAM-ID")) {
|
2015-12-05 11:12:33 +00:00
|
|
|
if (!int_from_string (v, NULL, &stream->program_id))
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_WARNING ("Error while reading PROGRAM-ID");
|
|
|
|
} else if (g_str_equal (a, "CODECS")) {
|
2015-12-05 11:12:33 +00:00
|
|
|
g_free (stream->codecs);
|
|
|
|
stream->codecs = g_strdup (v);
|
2015-11-27 19:26:02 +00:00
|
|
|
} else if (g_str_equal (a, "RESOLUTION")) {
|
2015-12-05 11:12:33 +00:00
|
|
|
if (!int_from_string (v, &v, &stream->width))
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_WARNING ("Error while reading RESOLUTION width");
|
|
|
|
if (!v || *v != 'x') {
|
|
|
|
GST_WARNING ("Missing height");
|
|
|
|
} else {
|
|
|
|
v = g_utf8_next_char (v);
|
2015-12-05 11:12:33 +00:00
|
|
|
if (!int_from_string (v, NULL, &stream->height))
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_WARNING ("Error while reading RESOLUTION height");
|
|
|
|
}
|
2015-12-05 11:12:33 +00:00
|
|
|
} else if (stream->iframe && g_str_equal (a, "URI")) {
|
|
|
|
stream->uri = uri_join (base_uri, v);
|
|
|
|
if (stream->uri != NULL) {
|
|
|
|
stream->name = g_strdup (stream->uri);
|
|
|
|
gst_m3u8_set_uri (stream->m3u8, stream->uri, NULL, stream->name);
|
2015-11-27 19:26:02 +00:00
|
|
|
} else {
|
2015-12-05 11:12:33 +00:00
|
|
|
gst_hls_variant_stream_unref (stream);
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
2015-12-05 11:12:33 +00:00
|
|
|
} else if (g_str_equal (a, "AUDIO")) {
|
|
|
|
g_free (stream->media_groups[GST_HLS_MEDIA_TYPE_AUDIO]);
|
|
|
|
stream->media_groups[GST_HLS_MEDIA_TYPE_AUDIO] = gst_m3u8_unquote (v);
|
|
|
|
} else if (g_str_equal (a, "SUBTITLES")) {
|
|
|
|
g_free (stream->media_groups[GST_HLS_MEDIA_TYPE_SUBTITLES]);
|
|
|
|
stream->media_groups[GST_HLS_MEDIA_TYPE_SUBTITLES] =
|
|
|
|
gst_m3u8_unquote (v);
|
|
|
|
} else if (g_str_equal (a, "VIDEO")) {
|
|
|
|
g_free (stream->media_groups[GST_HLS_MEDIA_TYPE_VIDEO]);
|
|
|
|
stream->media_groups[GST_HLS_MEDIA_TYPE_VIDEO] = gst_m3u8_unquote (v);
|
|
|
|
} else if (g_str_equal (a, "CLOSED-CAPTIONS")) {
|
|
|
|
/* closed captions will be embedded inside the video stream, ignore */
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
if (stream->iframe) {
|
|
|
|
if (find_variant_stream_by_uri (playlist->iframe_variants, stream->uri)) {
|
2015-11-27 19:26:02 +00:00
|
|
|
GST_DEBUG ("Already have a list with this URI");
|
2015-12-05 11:12:33 +00:00
|
|
|
gst_hls_variant_stream_unref (stream);
|
2015-11-27 19:26:02 +00:00
|
|
|
} else {
|
2015-12-05 11:12:33 +00:00
|
|
|
playlist->iframe_variants =
|
|
|
|
g_list_append (playlist->iframe_variants, stream);
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-12-05 11:12:33 +00:00
|
|
|
if (pending_stream != NULL) {
|
|
|
|
GST_WARNING ("variant stream without uri, dropping");
|
|
|
|
gst_hls_variant_stream_unref (pending_stream);
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
2015-12-05 11:12:33 +00:00
|
|
|
pending_stream = stream;
|
|
|
|
}
|
|
|
|
} else if (g_str_has_prefix (data, "#EXT-X-MEDIA:")) {
|
|
|
|
GstHLSMedia *media;
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
media = gst_m3u8_parse_media (data + strlen ("#EXT-X-MEDIA:"), base_uri);
|
|
|
|
|
|
|
|
if (media == NULL)
|
|
|
|
goto next_line;
|
|
|
|
|
|
|
|
if (media_groups[media->mtype] == NULL) {
|
|
|
|
media_groups[media->mtype] =
|
|
|
|
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
list = g_hash_table_lookup (media_groups[media->mtype], media->group_id);
|
|
|
|
|
|
|
|
/* make sure there isn't already a media with the same name */
|
|
|
|
if (!g_list_find_custom (list, media->name, hls_media_name_compare_func)) {
|
|
|
|
g_hash_table_replace (media_groups[media->mtype],
|
|
|
|
g_strdup (media->group_id), g_list_append (list, media));
|
|
|
|
GST_INFO ("Added media %s to group %s", media->name, media->group_id);
|
|
|
|
} else {
|
|
|
|
GST_WARNING (" media with name '%s' already exists in group '%s'!",
|
|
|
|
media->name, media->group_id);
|
|
|
|
gst_hls_media_unref (media);
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
|
|
|
} else if (*data != '\0') {
|
|
|
|
GST_LOG ("Ignored line: %s", data);
|
|
|
|
}
|
|
|
|
|
|
|
|
next_line:
|
|
|
|
if (!end)
|
|
|
|
break;
|
|
|
|
data = g_utf8_next_char (end); /* skip \n */
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
if (pending_stream != NULL) {
|
|
|
|
GST_WARNING ("#EXT-X-STREAM-INF without uri, dropping");
|
|
|
|
gst_hls_variant_stream_unref (pending_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (free_data);
|
|
|
|
|
|
|
|
/* Add alternative renditions media to variant streams */
|
|
|
|
for (l = playlist->variants; l != NULL; l = l->next) {
|
|
|
|
GstHLSVariantStream *stream = l->data;
|
|
|
|
GList *mlist;
|
|
|
|
|
|
|
|
for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
|
|
|
|
if (stream->media_groups[i] != NULL && media_groups[i] != NULL) {
|
|
|
|
GST_INFO ("Adding %s group '%s' to stream '%s'",
|
|
|
|
GST_HLS_MEDIA_TYPE_NAME (i), stream->media_groups[i], stream->name);
|
|
|
|
|
|
|
|
mlist = g_hash_table_lookup (media_groups[i], stream->media_groups[i]);
|
|
|
|
|
|
|
|
if (mlist == NULL)
|
|
|
|
GST_WARNING ("Group '%s' does not exist!", stream->media_groups[i]);
|
|
|
|
|
|
|
|
while (mlist != NULL) {
|
|
|
|
GstHLSMedia *media = mlist->data;
|
|
|
|
|
|
|
|
GST_DEBUG (" %s media %s, uri: %s", GST_HLS_MEDIA_TYPE_NAME (i),
|
|
|
|
media->name, media->uri);
|
|
|
|
|
|
|
|
stream->media[i] =
|
|
|
|
g_list_append (stream->media[i], gst_hls_media_ref (media));
|
|
|
|
mlist = mlist->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up our temporary alternative rendition groups hash tables */
|
|
|
|
for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
|
|
|
|
if (media_groups[i] != NULL) {
|
|
|
|
GList *groups, *mlist;
|
|
|
|
|
|
|
|
groups = g_hash_table_get_keys (media_groups[i]);
|
|
|
|
for (l = groups; l != NULL; l = l->next) {
|
|
|
|
mlist = g_hash_table_lookup (media_groups[i], l->data);
|
|
|
|
g_list_free_full (mlist, (GDestroyNotify) gst_hls_media_unref);
|
|
|
|
}
|
|
|
|
g_list_free (groups);
|
|
|
|
g_hash_table_unref (media_groups[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (playlist->variants == NULL) {
|
|
|
|
GST_WARNING ("Master playlist without any media playlists!");
|
|
|
|
gst_hls_master_playlist_unref (playlist);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reorder variants by bitrate */
|
|
|
|
playlist->variants =
|
|
|
|
g_list_sort (playlist->variants,
|
|
|
|
(GCompareFunc) gst_hls_variant_stream_compare_by_bitrate);
|
|
|
|
|
|
|
|
playlist->iframe_variants =
|
|
|
|
g_list_sort (playlist->iframe_variants,
|
|
|
|
(GCompareFunc) gst_hls_variant_stream_compare_by_bitrate);
|
|
|
|
|
|
|
|
/* FIXME: restore old current_variant after master playlist update
|
|
|
|
* (move into code that does that update) */
|
|
|
|
#if 0
|
|
|
|
{
|
2015-11-27 19:26:02 +00:00
|
|
|
gchar *top_variant_uri = NULL;
|
|
|
|
gboolean iframe = FALSE;
|
|
|
|
|
|
|
|
if (!self->current_variant) {
|
|
|
|
top_variant_uri = GST_M3U8 (self->lists->data)->uri;
|
|
|
|
} else {
|
|
|
|
top_variant_uri = GST_M3U8 (self->current_variant->data)->uri;
|
|
|
|
iframe = GST_M3U8 (self->current_variant->data)->iframe;
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
/* here we sorted the lists */
|
2015-11-27 19:26:02 +00:00
|
|
|
|
|
|
|
if (iframe)
|
2015-12-05 11:12:33 +00:00
|
|
|
playlist->current_variant =
|
|
|
|
find_variant_stream_by_uri (playlist->iframe_variants,
|
|
|
|
top_variant_uri);
|
2015-11-27 19:26:02 +00:00
|
|
|
else
|
2015-12-05 11:12:33 +00:00
|
|
|
playlist->current_variant =
|
|
|
|
find_variant_stream_by_uri (playlist->variants, top_variant_uri);
|
2015-11-27 19:26:02 +00:00
|
|
|
}
|
2015-12-05 11:12:33 +00:00
|
|
|
#endif
|
2015-11-27 19:26:02 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("parsed master playlist with %d streams and %d I-frame streams",
|
2015-12-05 11:12:33 +00:00
|
|
|
g_list_length (playlist->variants),
|
|
|
|
g_list_length (playlist->iframe_variants));
|
2015-11-27 19:26:02 +00:00
|
|
|
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_hls_variant_stream_is_live (GstHLSVariantStream * variant)
|
|
|
|
{
|
|
|
|
gboolean is_live;
|
|
|
|
|
|
|
|
g_return_val_if_fail (variant != NULL, FALSE);
|
|
|
|
|
|
|
|
is_live = gst_m3u8_is_live (variant->m3u8);
|
|
|
|
|
|
|
|
return is_live;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:07:22 +00:00
|
|
|
static gint
|
|
|
|
compare_media (const GstHLSMedia * a, const GstHLSMedia * b)
|
|
|
|
{
|
|
|
|
return strcmp (a->name, b->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
GstHLSMedia *
|
|
|
|
gst_hls_variant_find_matching_media (GstHLSVariantStream * stream,
|
|
|
|
GstHLSMedia * media)
|
|
|
|
{
|
|
|
|
GList *mlist = stream->media[media->mtype];
|
|
|
|
GList *match;
|
|
|
|
|
|
|
|
if (mlist == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
match = g_list_find_custom (mlist, media, (GCompareFunc) compare_media);
|
|
|
|
if (match == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return match->data;
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:12:33 +00:00
|
|
|
GstHLSVariantStream *
|
|
|
|
gst_hls_master_playlist_get_variant_for_bitrate (GstHLSMasterPlaylist *
|
|
|
|
playlist, GstHLSVariantStream * current_variant, guint bitrate)
|
|
|
|
{
|
|
|
|
GstHLSVariantStream *variant = current_variant;
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
/* variant lists are sorted low to high, so iterate from highest to lowest */
|
|
|
|
if (current_variant == NULL || !current_variant->iframe)
|
|
|
|
l = g_list_last (playlist->variants);
|
|
|
|
else
|
|
|
|
l = g_list_last (playlist->iframe_variants);
|
|
|
|
|
|
|
|
while (l != NULL) {
|
|
|
|
variant = l->data;
|
|
|
|
if (variant->bandwidth <= bitrate)
|
|
|
|
break;
|
|
|
|
l = l->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return variant;
|
2014-04-04 15:45:51 +00:00
|
|
|
}
|
2016-03-08 16:07:22 +00:00
|
|
|
|
|
|
|
GstHLSVariantStream *
|
|
|
|
gst_hls_master_playlist_get_matching_variant (GstHLSMasterPlaylist * playlist,
|
|
|
|
GstHLSVariantStream * current_variant)
|
|
|
|
{
|
|
|
|
if (current_variant->iframe) {
|
|
|
|
return find_variant_stream_by_uri (playlist->iframe_variants,
|
|
|
|
current_variant->uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
return find_variant_stream_by_uri (playlist->variants, current_variant->uri);
|
|
|
|
}
|