| 1 |
/* GStreamer |
| 2 |
* Copyright (C) 2010 Marc-Andre Lureau <[email protected]> |
| 3 |
* Copyright (C) 2015 Tim-Philipp Müller <[email protected]> |
| 4 |
* |
| 5 |
* Copyright (C) 2021-2022 Centricular Ltd |
| 6 |
* Author: Edward Hervey <[email protected]> |
| 7 |
* Author: Jan Schmidt <[email protected]> |
| 8 |
* |
| 9 |
* m3u8.c: |
| 10 |
* |
| 11 |
* This library is free software; you can redistribute it and/or |
| 12 |
* modify it under the terms of the GNU Library General Public |
| 13 |
* License as published by the Free Software Foundation; either |
| 14 |
* version 2 of the License, or (at your option) any later version. |
| 15 |
* |
| 16 |
* This library is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
* Library General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU Library General Public |
| 22 |
* License along with this library; if not, write to the |
| 23 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 24 |
* Boston, MA 02110-1301, USA. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <math.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <glib.h> |
| 31 |
#include <gmodule.h> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
#include <gst/pbutils/pbutils.h> |
| 35 |
#include "m3u8.h" |
| 36 |
#include "gstadaptivedemux.h" |
| 37 |
#include "gsthlselements.h" |
| 38 |
|
| 39 |
#define GST_CAT_DEFAULT hls2_debug |
| 40 |
|
| 41 |
static gchar *uri_join (const gchar * uri, const gchar * path); |
| 42 |
static void |
| 43 |
gst_m3u8_media_segment_fill_partial_stream_times (GstM3U8MediaSegment * |
| 44 |
segment); |
| 45 |
|
| 46 |
GstHLSMediaPlaylist * |
| 47 |
gst_hls_media_playlist_ref (GstHLSMediaPlaylist * m3u8) |
| 48 |
{ |
| 49 |
g_assert (m3u8 != NULL && m3u8->ref_count > 0); |
| 50 |
|
| 51 |
g_atomic_int_add (&m3u8->ref_count, 1); |
| 52 |
return m3u8; |
| 53 |
} |
| 54 |
|
| 55 |
void |
| 56 |
gst_hls_media_playlist_unref (GstHLSMediaPlaylist * self) |
| 57 |
{ |
| 58 |
g_return_if_fail (self != NULL && self->ref_count > 0); |
| 59 |
|
| 60 |
if (g_atomic_int_dec_and_test (&self->ref_count)) { |
| 61 |
g_free (self->uri); |
| 62 |
g_free (self->base_uri); |
| 63 |
|
| 64 |
g_ptr_array_free (self->segments, TRUE); |
| 65 |
|
| 66 |
if (self->preload_hints != NULL) |
| 67 |
g_ptr_array_free (self->preload_hints, TRUE); |
| 68 |
|
| 69 |
if (self->removed_date_ranges != NULL) |
| 70 |
g_strfreev (self->removed_date_ranges); |
| 71 |
|
| 72 |
g_free (self->last_data); |
| 73 |
g_mutex_clear (&self->lock); |
| 74 |
g_free (self); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static GstM3U8MediaSegment * |
| 79 |
gst_m3u8_media_segment_new (gchar * uri, gchar * title, GstClockTime duration, |
| 80 |
gint64 sequence, gint64 discont_sequence, gint64 size, gint64 offset) |
| 81 |
{ |
| 82 |
GstM3U8MediaSegment *file; |
| 83 |
|
| 84 |
file = g_new0 (GstM3U8MediaSegment, 1); |
| 85 |
file->uri = uri; |
| 86 |
file->title = title; |
| 87 |
file->duration = duration; |
| 88 |
file->sequence = sequence; |
| 89 |
file->discont_sequence = discont_sequence; |
| 90 |
file->ref_count = 1; |
| 91 |
|
| 92 |
file->stream_time = GST_CLOCK_STIME_NONE; |
| 93 |
|
| 94 |
file->size = size; |
| 95 |
if (size != -1 && offset != -1) { |
| 96 |
file->offset = offset; |
| 97 |
} |
| 98 |
|
| 99 |
return file; |
| 100 |
} |
| 101 |
|
| 102 |
GstM3U8MediaSegment * |
| 103 |
gst_m3u8_media_segment_ref (GstM3U8MediaSegment * mfile) |
| 104 |
{ |
| 105 |
g_assert (mfile != NULL && mfile->ref_count > 0); |
| 106 |
|
| 107 |
g_atomic_int_add (&mfile->ref_count, 1); |
| 108 |
return mfile; |
| 109 |
} |
| 110 |
|
| 111 |
void |
| 112 |
gst_m3u8_media_segment_unref (GstM3U8MediaSegment * self) |
| 113 |
{ |
| 114 |
g_return_if_fail (self != NULL && self->ref_count > 0); |
| 115 |
|
| 116 |
if (g_atomic_int_dec_and_test (&self->ref_count)) { |
| 117 |
if (self->init_file) |
| 118 |
gst_m3u8_init_file_unref (self->init_file); |
| 119 |
g_free (self->title); |
| 120 |
g_free (self->uri); |
| 121 |
g_free (self->key); |
| 122 |
if (self->datetime) |
| 123 |
g_date_time_unref (self->datetime); |
| 124 |
if (self->partial_segments) |
| 125 |
g_ptr_array_free (self->partial_segments, TRUE); |
| 126 |
g_free (self); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
GstM3U8PartialSegment * |
| 131 |
gst_m3u8_partial_segment_ref (GstM3U8PartialSegment * part) |
| 132 |
{ |
| 133 |
g_assert (part != NULL && part->ref_count > 0); |
| 134 |
|
| 135 |
g_atomic_int_add (&part->ref_count, 1); |
| 136 |
return part; |
| 137 |
} |
| 138 |
|
| 139 |
void |
| 140 |
gst_m3u8_partial_segment_unref (GstM3U8PartialSegment * part) |
| 141 |
{ |
| 142 |
g_return_if_fail (part != NULL && part->ref_count > 0); |
| 143 |
|
| 144 |
if (g_atomic_int_dec_and_test (&part->ref_count)) { |
| 145 |
g_free (part->uri); |
| 146 |
g_free (part); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
GstM3U8PreloadHint * |
| 151 |
gst_m3u8_preload_hint_ref (GstM3U8PreloadHint * hint) |
| 152 |
{ |
| 153 |
g_assert (hint != NULL && hint->ref_count > 0); |
| 154 |
|
| 155 |
g_atomic_int_add (&hint->ref_count, 1); |
| 156 |
return hint; |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
gst_m3u8_preload_hint_unref (GstM3U8PreloadHint * hint) |
| 161 |
{ |
| 162 |
g_return_if_fail (hint != NULL && hint->ref_count > 0); |
| 163 |
|
| 164 |
if (g_atomic_int_dec_and_test (&hint->ref_count)) { |
| 165 |
g_free (hint->uri); |
| 166 |
g_free (hint); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
gboolean |
| 171 |
gst_m3u8_preload_hint_equal (GstM3U8PreloadHint * hint1, |
| 172 |
GstM3U8PreloadHint * hint2) |
| 173 |
{ |
| 174 |
if (hint1->hint_type != hint2->hint_type) |
| 175 |
return FALSE; |
| 176 |
|
| 177 |
if (g_strcmp0 (hint1->uri, hint2->uri)) |
| 178 |
return FALSE; |
| 179 |
|
| 180 |
if (hint1->offset != hint2->offset) |
| 181 |
return FALSE; |
| 182 |
|
| 183 |
if (hint1->size != hint2->size) |
| 184 |
return FALSE; |
| 185 |
|
| 186 |
return TRUE; |
| 187 |
} |
| 188 |
|
| 189 |
static GstM3U8InitFile * |
| 190 |
gst_m3u8_init_file_new (gchar * uri, gint64 size, gint64 offset) |
| 191 |
{ |
| 192 |
GstM3U8InitFile *file; |
| 193 |
|
| 194 |
file = g_new0 (GstM3U8InitFile, 1); |
| 195 |
file->uri = uri; |
| 196 |
file->ref_count = 1; |
| 197 |
|
| 198 |
file->size = size; |
| 199 |
if (size != -1 && offset != -1) { |
| 200 |
file->offset = offset; |
| 201 |
} |
| 202 |
|
| 203 |
return file; |
| 204 |
} |
| 205 |
|
| 206 |
GstM3U8InitFile * |
| 207 |
gst_m3u8_init_file_ref (GstM3U8InitFile * ifile) |
| 208 |
{ |
| 209 |
g_assert (ifile != NULL && ifile->ref_count > 0); |
| 210 |
|
| 211 |
g_atomic_int_add (&ifile->ref_count, 1); |
| 212 |
return ifile; |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
gst_m3u8_init_file_unref (GstM3U8InitFile * self) |
| 217 |
{ |
| 218 |
g_return_if_fail (self != NULL && self->ref_count > 0); |
| 219 |
|
| 220 |
if (g_atomic_int_dec_and_test (&self->ref_count)) { |
| 221 |
g_free (self->uri); |
| 222 |
g_free (self); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
gboolean |
| 227 |
gst_m3u8_init_file_equal (const GstM3U8InitFile * ifile1, |
| 228 |
const GstM3U8InitFile * ifile2) |
| 229 |
{ |
| 230 |
if (ifile1 == ifile2) |
| 231 |
return TRUE; |
| 232 |
|
| 233 |
if (ifile1 == NULL && ifile2 != NULL) |
| 234 |
return FALSE; |
| 235 |
if (ifile1 != NULL && ifile2 == NULL) |
| 236 |
return FALSE; |
| 237 |
|
| 238 |
if (g_strcmp0 (ifile1->uri, ifile2->uri)) |
| 239 |
return FALSE; |
| 240 |
if (ifile1->offset != ifile2->offset) |
| 241 |
return FALSE; |
| 242 |
if (ifile1->size != ifile2->size) |
| 243 |
return FALSE; |
| 244 |
|
| 245 |
return TRUE; |
| 246 |
} |
| 247 |
|
| 248 |
static gboolean |
| 249 |
int_from_string (gchar * ptr, gchar ** endptr, gint * val) |
| 250 |
{ |
| 251 |
gchar *end; |
| 252 |
gint64 ret; |
| 253 |
|
| 254 |
g_return_val_if_fail (ptr != NULL, FALSE); |
| 255 |
g_return_val_if_fail (val != NULL, FALSE); |
| 256 |
|
| 257 |
errno = 0; |
| 258 |
ret = g_ascii_strtoll (ptr, &end, 10); |
| 259 |
if ((errno == ERANGE && (ret == G_MAXINT64 || ret == G_MININT64)) |
| 260 |
|| (errno != 0 && ret == 0)) { |
| 261 |
GST_WARNING ("%s", g_strerror (errno)); |
| 262 |
return FALSE; |
| 263 |
} |
| 264 |
|
| 265 |
if (ret > G_MAXINT || ret < G_MININT) { |
| 266 |
GST_WARNING ("%s", g_strerror (ERANGE)); |
| 267 |
return FALSE; |
| 268 |
} |
| 269 |
|
| 270 |
if (endptr) |
| 271 |
*endptr = end; |
| 272 |
|
| 273 |
*val = (gint) ret; |
| 274 |
|
| 275 |
return end != ptr; |
| 276 |
} |
| 277 |
|
| 278 |
static gboolean |
| 279 |
int64_from_string (gchar * ptr, gchar ** endptr, gint64 * val) |
| 280 |
{ |
| 281 |
gchar *end; |
| 282 |
gint64 ret; |
| 283 |
|
| 284 |
g_return_val_if_fail (ptr != NULL, FALSE); |
| 285 |
g_return_val_if_fail (val != NULL, FALSE); |
| 286 |
|
| 287 |
errno = 0; |
| 288 |
ret = g_ascii_strtoll (ptr, &end, 10); |
| 289 |
if ((errno == ERANGE && (ret == G_MAXINT64 || ret == G_MININT64)) |
| 290 |
|| (errno != 0 && ret == 0)) { |
| 291 |
GST_WARNING ("%s", g_strerror (errno)); |
| 292 |
return FALSE; |
| 293 |
} |
| 294 |
|
| 295 |
if (endptr) |
| 296 |
*endptr = end; |
| 297 |
|
| 298 |
*val = ret; |
| 299 |
|
| 300 |
return end != ptr; |
| 301 |
} |
| 302 |
|
| 303 |
static gboolean |
| 304 |
double_from_string (gchar * ptr, gchar ** endptr, gdouble * val) |
| 305 |
{ |
| 306 |
gchar *end; |
| 307 |
gdouble ret; |
| 308 |
|
| 309 |
g_return_val_if_fail (ptr != NULL, FALSE); |
| 310 |
g_return_val_if_fail (val != NULL, FALSE); |
| 311 |
|
| 312 |
errno = 0; |
| 313 |
ret = g_ascii_strtod (ptr, &end); |
| 314 |
if ((errno == ERANGE && (ret == HUGE_VAL || ret == -HUGE_VAL)) |
| 315 |
|| (errno != 0 && ret == 0)) { |
| 316 |
GST_WARNING ("%s", g_strerror (errno)); |
| 317 |
return FALSE; |
| 318 |
} |
| 319 |
|
| 320 |
if (!isfinite (ret)) { |
| 321 |
GST_WARNING ("%s", g_strerror (ERANGE)); |
| 322 |
return FALSE; |
| 323 |
} |
| 324 |
|
| 325 |
if (endptr) |
| 326 |
*endptr = end; |
| 327 |
|
| 328 |
*val = (gdouble) ret; |
| 329 |
|
| 330 |
return end != ptr; |
| 331 |
} |
| 332 |
|
| 333 |
static gboolean |
| 334 |
time_from_double_in_string (gchar * ptr, gchar ** endptr, GstClockTime * val) |
| 335 |
{ |
| 336 |
double fval; |
| 337 |
if (!double_from_string (ptr, endptr, &fval)) { |
| 338 |
return FALSE; |
| 339 |
} |
| 340 |
*val = fval * (gdouble) GST_SECOND; |
| 341 |
return TRUE; |
| 342 |
} |
| 343 |
|
| 344 |
static gboolean |
| 345 |
parse_attributes (gchar ** ptr, gchar ** a, gchar ** v) |
| 346 |
{ |
| 347 |
gchar *end = NULL, *p, *ve; |
| 348 |
|
| 349 |
g_return_val_if_fail (ptr != NULL, FALSE); |
| 350 |
g_return_val_if_fail (*ptr != NULL, FALSE); |
| 351 |
g_return_val_if_fail (a != NULL, FALSE); |
| 352 |
g_return_val_if_fail (v != NULL, FALSE); |
| 353 |
|
| 354 |
/* [attribute=value,]* */ |
| 355 |
|
| 356 |
*a = *ptr; |
| 357 |
end = p = g_utf8_strchr (*ptr, -1, ','); |
| 358 |
if (end) { |
| 359 |
gchar *q = g_utf8_strchr (*ptr, -1, '"'); |
| 360 |
if (q && q < end) { |
| 361 |
/* special case, such as CODECS="avc1.77.30, mp4a.40.2" */ |
| 362 |
q = g_utf8_next_char (q); |
| 363 |
if (q) { |
| 364 |
q = g_utf8_strchr (q, -1, '"'); |
| 365 |
} |
| 366 |
if (q) { |
| 367 |
end = p = g_utf8_strchr (q, -1, ','); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
if (end) { |
| 372 |
do { |
| 373 |
end = g_utf8_next_char (end); |
| 374 |
} while (end && *end == ' '); |
| 375 |
*p = '\0'; |
| 376 |
} |
| 377 |
|
| 378 |
*v = p = g_utf8_strchr (*ptr, -1, '='); |
| 379 |
if (*v) { |
| 380 |
*p = '\0'; |
| 381 |
*v = g_utf8_next_char (*v); |
| 382 |
if (**v == '"') { |
| 383 |
ve = g_utf8_next_char (*v); |
| 384 |
if (ve) { |
| 385 |
ve = g_utf8_strchr (ve, -1, '"'); |
| 386 |
} |
| 387 |
if (ve) { |
| 388 |
*v = g_utf8_next_char (*v); |
| 389 |
*ve = '\0'; |
| 390 |
} else { |
| 391 |
GST_WARNING ("Cannot remove quotation marks from %s", *a); |
| 392 |
} |
| 393 |
} |
| 394 |
} else { |
| 395 |
GST_WARNING ("missing = after attribute"); |
| 396 |
return FALSE; |
| 397 |
} |
| 398 |
|
| 399 |
*ptr = end; |
| 400 |
return TRUE; |
| 401 |
} |
| 402 |
|
| 403 |
GstHLSMediaPlaylist * |
| 404 |
gst_hls_media_playlist_new (const gchar * uri, const gchar * base_uri) |
| 405 |
{ |
| 406 |
GstHLSMediaPlaylist *m3u8; |
| 407 |
|
| 408 |
m3u8 = g_new0 (GstHLSMediaPlaylist, 1); |
| 409 |
|
| 410 |
m3u8->playlist_ts = GST_CLOCK_TIME_NONE; |
| 411 |
m3u8->uri = g_strdup (uri); |
| 412 |
m3u8->base_uri = g_strdup (base_uri); |
| 413 |
m3u8->request_time = GST_CLOCK_TIME_NONE; |
| 414 |
|
| 415 |
m3u8->version = 1; |
| 416 |
m3u8->type = GST_HLS_PLAYLIST_TYPE_UNDEFINED; |
| 417 |
m3u8->targetduration = GST_CLOCK_TIME_NONE; |
| 418 |
m3u8->partial_targetduration = GST_CLOCK_TIME_NONE; |
| 419 |
m3u8->media_sequence = 0; |
| 420 |
m3u8->discont_sequence = -1; |
| 421 |
m3u8->endlist = FALSE; |
| 422 |
m3u8->i_frame = FALSE; |
| 423 |
m3u8->allowcache = TRUE; |
| 424 |
|
| 425 |
m3u8->ext_x_key_present = FALSE; |
| 426 |
m3u8->ext_x_pdt_present = FALSE; |
| 427 |
|
| 428 |
m3u8->segments = |
| 429 |
g_ptr_array_new_full (16, (GDestroyNotify) gst_m3u8_media_segment_unref); |
| 430 |
|
| 431 |
m3u8->duration = 0; |
| 432 |
|
| 433 |
m3u8->skip_boundary = GST_CLOCK_TIME_NONE; |
| 434 |
m3u8->hold_back = GST_CLOCK_TIME_NONE; |
| 435 |
m3u8->part_hold_back = GST_CLOCK_TIME_NONE; |
| 436 |
|
| 437 |
g_mutex_init (&m3u8->lock); |
| 438 |
m3u8->ref_count = 1; |
| 439 |
|
| 440 |
return m3u8; |
| 441 |
} |
| 442 |
|
| 443 |
void |
| 444 |
gst_hls_media_playlist_dump (GstHLSMediaPlaylist * self) |
| 445 |
{ |
| 446 |
#ifndef GST_DISABLE_GST_DEBUG |
| 447 |
guint idx; |
| 448 |
gchar *datestring; |
| 449 |
|
| 450 |
GST_DEBUG ("uri : %s", self->uri); |
| 451 |
GST_DEBUG ("base_uri : %s", self->base_uri); |
| 452 |
|
| 453 |
GST_DEBUG ("version : %d", self->version); |
| 454 |
|
| 455 |
GST_DEBUG ("targetduration : %" GST_TIME_FORMAT, |
| 456 |
GST_TIME_ARGS (self->targetduration)); |
| 457 |
GST_DEBUG ("partial segment targetduration : %" GST_TIME_FORMAT, |
| 458 |
GST_TIME_ARGS (self->partial_targetduration)); |
| 459 |
GST_DEBUG ("media_sequence : %" G_GINT64_FORMAT, self->media_sequence); |
| 460 |
GST_DEBUG ("discont_sequence : %" G_GINT64_FORMAT, self->discont_sequence); |
| 461 |
|
| 462 |
GST_DEBUG ("endlist : %s", |
| 463 |
self->endlist ? "present" : "NOT present"); |
| 464 |
GST_DEBUG ("i_frame : %s", self->i_frame ? "YES" : "NO"); |
| 465 |
|
| 466 |
GST_DEBUG ("EXT-X-KEY : %s", |
| 467 |
self->ext_x_key_present ? "present" : "NOT present"); |
| 468 |
GST_DEBUG ("EXT-X-PROGRAM-DATE-TIME : %s", |
| 469 |
self->ext_x_pdt_present ? "present" : "NOT present"); |
| 470 |
|
| 471 |
GST_DEBUG ("duration : %" GST_TIME_FORMAT, |
| 472 |
GST_TIME_ARGS (self->duration)); |
| 473 |
|
| 474 |
GST_DEBUG ("skip boundary : %" GST_TIME_FORMAT, |
| 475 |
GST_TIME_ARGS (self->skip_boundary)); |
| 476 |
|
| 477 |
GST_DEBUG ("skip dateranges : %s", self->can_skip_dateranges ? "YES" : "NO"); |
| 478 |
|
| 479 |
GST_DEBUG ("hold back : %" GST_TIME_FORMAT, |
| 480 |
GST_TIME_ARGS (self->hold_back)); |
| 481 |
GST_DEBUG ("part hold back : %" GST_TIME_FORMAT, |
| 482 |
GST_TIME_ARGS (self->part_hold_back)); |
| 483 |
|
| 484 |
GST_DEBUG ("can block reloads: %s", self->can_block_reload ? "YES" : "NO"); |
| 485 |
|
| 486 |
GST_DEBUG ("skipped segments: %d", self->skipped_segments); |
| 487 |
|
| 488 |
if (self->num_removed_date_ranges && self->removed_date_ranges) { |
| 489 |
GST_DEBUG ("Removed date ranges: %u", self->num_removed_date_ranges); |
| 490 |
gchar **cur = self->removed_date_ranges; |
| 491 |
while (*cur != NULL) { |
| 492 |
GST_DEBUG (" ID: %s", *cur); |
| 493 |
cur++; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
GST_DEBUG ("Segments : %d", self->segments->len); |
| 498 |
for (idx = 0; idx < self->segments->len; idx++) { |
| 499 |
GstM3U8MediaSegment *segment = g_ptr_array_index (self->segments, idx); |
| 500 |
|
| 501 |
GST_DEBUG (" sequence:%" G_GINT64_FORMAT " discont_sequence:%" |
| 502 |
G_GINT64_FORMAT, segment->sequence, segment->discont_sequence); |
| 503 |
GST_DEBUG (" partial only: %s", segment->partial_only ? "YES" : "NO"); |
| 504 |
GST_DEBUG (" stream_time : %" GST_STIME_FORMAT, |
| 505 |
GST_STIME_ARGS (segment->stream_time)); |
| 506 |
GST_DEBUG (" duration : %" GST_TIME_FORMAT, |
| 507 |
GST_TIME_ARGS (segment->duration)); |
| 508 |
if (segment->title) |
| 509 |
GST_DEBUG (" title : %s", segment->title); |
| 510 |
GST_DEBUG (" discont : %s", segment->discont ? "YES" : "NO"); |
| 511 |
if (segment->datetime) { |
| 512 |
datestring = g_date_time_format_iso8601 (segment->datetime); |
| 513 |
GST_DEBUG (" date/time : %s", datestring); |
| 514 |
g_free (datestring); |
| 515 |
} |
| 516 |
if (segment->init_file) { |
| 517 |
GST_DEBUG (" init file : %s %" G_GUINT64_FORMAT " %" G_GINT64_FORMAT, |
| 518 |
segment->init_file->uri, segment->init_file->offset, |
| 519 |
segment->init_file->size); |
| 520 |
} |
| 521 |
|
| 522 |
GST_DEBUG (" uri : %s %" G_GUINT64_FORMAT " %" G_GINT64_FORMAT, |
| 523 |
segment->uri, segment->offset, segment->size); |
| 524 |
|
| 525 |
GST_DEBUG (" is gap : %s", segment->is_gap ? "YES" : "NO"); |
| 526 |
|
| 527 |
if (segment->partial_segments != NULL) { |
| 528 |
guint part_idx; |
| 529 |
for (part_idx = 0; part_idx < segment->partial_segments->len; part_idx++) { |
| 530 |
GstM3U8PartialSegment *part = |
| 531 |
g_ptr_array_index (segment->partial_segments, part_idx); |
| 532 |
GST_DEBUG (" partial segment %u:", part_idx); |
| 533 |
GST_DEBUG (" uri : %s %" G_GUINT64_FORMAT " %" |
| 534 |
G_GINT64_FORMAT, part->uri, part->offset, part->size); |
| 535 |
GST_DEBUG (" stream_time : %" GST_STIME_FORMAT, |
| 536 |
GST_STIME_ARGS (part->stream_time)); |
| 537 |
GST_DEBUG (" duration : %" GST_TIME_FORMAT, |
| 538 |
GST_TIME_ARGS (part->duration)); |
| 539 |
GST_DEBUG (" is gap : %s", part->is_gap ? "YES" : "NO"); |
| 540 |
GST_DEBUG (" independent : %s", part->independent ? "YES" : "NO"); |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
if (self->preload_hints) { |
| 546 |
GST_DEBUG ("Preload Hints: %d", self->preload_hints->len); |
| 547 |
for (idx = 0; idx < self->preload_hints->len; idx++) { |
| 548 |
GstM3U8PreloadHint *hint = g_ptr_array_index (self->preload_hints, idx); |
| 549 |
const gchar *hint_type_str; |
| 550 |
switch (hint->hint_type) { |
| 551 |
case M3U8_PRELOAD_HINT_MAP: |
| 552 |
hint_type_str = "MAP"; |
| 553 |
break; |
| 554 |
case M3U8_PRELOAD_HINT_PART: |
| 555 |
hint_type_str = "PART"; |
| 556 |
break; |
| 557 |
default: |
| 558 |
g_assert_not_reached (); |
| 559 |
} |
| 560 |
|
| 561 |
GST_DEBUG (" preload hint %u: type %s", idx, hint_type_str); |
| 562 |
GST_DEBUG (" uri : %s %" G_GUINT64_FORMAT " %" |
| 563 |
G_GINT64_FORMAT, hint->uri, hint->offset, hint->size); |
| 564 |
} |
| 565 |
} |
| 566 |
#endif |
| 567 |
} |
| 568 |
|
| 569 |
static void |
| 570 |
gst_hls_media_playlist_postprocess_pdt (GstHLSMediaPlaylist * self) |
| 571 |
{ |
| 572 |
gint idx, len = self->segments->len; |
| 573 |
gint first_pdt = -1; |
| 574 |
GstM3U8MediaSegment *previous = NULL; |
| 575 |
GstM3U8MediaSegment *segment = NULL; |
| 576 |
|
| 577 |
/* Iterate forward, and make sure datetimes are coherent */ |
| 578 |
for (idx = 0; idx < len; idx++, previous = segment) { |
| 579 |
segment = g_ptr_array_index (self->segments, idx); |
| 580 |
|
| 581 |
#define ABSDIFF(a,b) ((a) > (b) ? (a) - (b) : (b) - (a)) |
| 582 |
|
| 583 |
if (segment->datetime) { |
| 584 |
if (first_pdt == -1) |
| 585 |
first_pdt = idx; |
| 586 |
if (!segment->discont && previous && previous->datetime) { |
| 587 |
GstClockTimeDiff diff = g_date_time_difference (segment->datetime, |
| 588 |
previous->datetime) * GST_USECOND; |
| 589 |
if (ABSDIFF (diff, previous->duration) > 500 * GST_MSECOND) { |
| 590 |
GST_LOG ("PDT diff %" GST_STIME_FORMAT " previous duration %" |
| 591 |
GST_TIME_FORMAT, GST_STIME_ARGS (diff), |
| 592 |
GST_TIME_ARGS (previous->duration)); |
| 593 |
g_date_time_unref (segment->datetime); |
| 594 |
segment->datetime = |
| 595 |
g_date_time_add (previous->datetime, |
| 596 |
previous->duration / GST_USECOND); |
| 597 |
} |
| 598 |
} |
| 599 |
} else { |
| 600 |
if (segment->discont) { |
| 601 |
GST_WARNING ("Discont segment doesn't have a PDT !"); |
| 602 |
} else if (previous) { |
| 603 |
if (previous->datetime) { |
| 604 |
segment->datetime = |
| 605 |
g_date_time_add (previous->datetime, |
| 606 |
previous->duration / GST_USECOND); |
| 607 |
GST_LOG |
| 608 |
("Generated new PDT based on previous segment PDT and duration"); |
| 609 |
} else { |
| 610 |
GST_LOG ("Missing PDT, but can't generate it from previous one"); |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
if (first_pdt != -1 && first_pdt != 0) { |
| 617 |
GST_LOG ("Scanning backwards from %d", first_pdt); |
| 618 |
previous = g_ptr_array_index (self->segments, first_pdt); |
| 619 |
for (idx = first_pdt - 1; idx >= 0; idx = idx - 1) { |
| 620 |
GST_LOG ("%d", idx); |
| 621 |
segment = g_ptr_array_index (self->segments, idx); |
| 622 |
if (!segment->datetime && previous->datetime) { |
| 623 |
segment->datetime = |
| 624 |
g_date_time_add (previous->datetime, |
| 625 |
-(segment->duration / GST_USECOND)); |
| 626 |
} |
| 627 |
previous = segment; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
static GstM3U8PartialSegment * |
| 633 |
gst_m3u8_parse_partial_segment (gchar * data, const gchar * base_uri) |
| 634 |
{ |
| 635 |
gchar *v, *a; |
| 636 |
gboolean have_duration = FALSE; |
| 637 |
GstM3U8PartialSegment *part = g_new0 (GstM3U8PartialSegment, 1); |
| 638 |
|
| 639 |
part->ref_count = 1; |
| 640 |
part->stream_time = GST_CLOCK_STIME_NONE; |
| 641 |
part->size = -1; |
| 642 |
|
| 643 |
while (data != NULL && parse_attributes (&data, &a, &v)) { |
| 644 |
if (strcmp (a, "URI") == 0) { |
| 645 |
g_free (part->uri); |
| 646 |
part->uri = uri_join (base_uri, v); |
| 647 |
} else if (strcmp (a, "DURATION") == 0) { |
| 648 |
if (!time_from_double_in_string (v, NULL, &part->duration)) { |
| 649 |
GST_WARNING ("Can't read EXT-X-PART duration"); |
| 650 |
goto malformed_line; |
| 651 |
} |
| 652 |
have_duration = TRUE; |
| 653 |
} else if (strcmp (a, "INDEPENDENT") == 0) { |
| 654 |
part->independent = g_ascii_strcasecmp (v, "yes") == 0; |
| 655 |
} else if (strcmp (a, "GAP") == 0) { |
| 656 |
part->is_gap = g_ascii_strcasecmp (v, "yes") == 0; |
| 657 |
} else if (strcmp (a, "BYTERANGE") == 0) { |
| 658 |
if (int64_from_string (v, &v, &part->size)) { |
| 659 |
goto malformed_line; |
| 660 |
} |
| 661 |
if (*v == '@' && !int64_from_string (v + 1, &v, &part->offset)) { |
| 662 |
goto malformed_line; |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
if (part->uri == NULL || !have_duration) { |
| 668 |
goto required_attributes_missing; |
| 669 |
} |
| 670 |
|
| 671 |
return part; |
| 672 |
|
| 673 |
required_attributes_missing: |
| 674 |
{ |
| 675 |
GST_WARNING |
| 676 |
("EXT-X-PART description is missing required URI or DURATION attributes"); |
| 677 |
gst_m3u8_partial_segment_unref (part); |
| 678 |
return NULL; |
| 679 |
} |
| 680 |
malformed_line: |
| 681 |
{ |
| 682 |
GST_WARNING ("Invalid EXT-X-PART entry in playlist"); |
| 683 |
gst_m3u8_partial_segment_unref (part); |
| 684 |
return NULL; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
static GstM3U8PreloadHint * |
| 689 |
gst_m3u8_parse_preload_hint (gchar * data, const gchar * base_uri) |
| 690 |
{ |
| 691 |
gchar *v, *a; |
| 692 |
GstM3U8PreloadHint *hint = g_new0 (GstM3U8PreloadHint, 1); |
| 693 |
gboolean have_hint_type = FALSE; |
| 694 |
|
| 695 |
hint->ref_count = 1; |
| 696 |
hint->size = -1; |
| 697 |
|
| 698 |
while (data != NULL && parse_attributes (&data, &a, &v)) { |
| 699 |
if (strcmp (a, "TYPE") == 0) { |
| 700 |
if (g_ascii_strcasecmp (v, "MAP") == 0) { |
| 701 |
hint->hint_type = M3U8_PRELOAD_HINT_MAP; |
| 702 |
} else if (g_ascii_strcasecmp (v, "PART") == 0) { |
| 703 |
hint->hint_type = M3U8_PRELOAD_HINT_PART; |
| 704 |
} else { |
| 705 |
GST_WARNING ("Unknown Preload Hint type %s", v); |
| 706 |
goto malformed_line; |
| 707 |
} |
| 708 |
have_hint_type = TRUE; |
| 709 |
} else if (strcmp (a, "URI") == 0) { |
| 710 |
g_free (hint->uri); |
| 711 |
hint->uri = uri_join (base_uri, v); |
| 712 |
} else if (strcmp (a, "BYTERANGE-START") == 0) { |
| 713 |
if (int64_from_string (v, NULL, &hint->offset)) { |
| 714 |
goto malformed_line; |
| 715 |
} |
| 716 |
} else if (strcmp (a, "BYTERANGE-LENGTH") == 0) { |
| 717 |
if (int64_from_string (v, NULL, &hint->size)) { |
| 718 |
goto malformed_line; |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
if (hint->uri == NULL || !have_hint_type) { |
| 724 |
goto required_attributes_missing; |
| 725 |
} |
| 726 |
|
| 727 |
return hint; |
| 728 |
|
| 729 |
required_attributes_missing: |
| 730 |
{ |
| 731 |
GST_WARNING |
| 732 |
("EXT-X-PRELOAD-HINT is missing required URI or TYPE attributes"); |
| 733 |
gst_m3u8_preload_hint_unref (hint); |
| 734 |
return NULL; |
| 735 |
} |
| 736 |
malformed_line: |
| 737 |
{ |
| 738 |
GST_WARNING ("Invalid EXT-X-PRELOAD-HINT entry in playlist"); |
| 739 |
gst_m3u8_preload_hint_unref (hint); |
| 740 |
return NULL; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
static void |
| 745 |
parse_server_control (GstHLSMediaPlaylist * self, gchar * data) |
| 746 |
{ |
| 747 |
gchar *v, *a; |
| 748 |
|
| 749 |
while (data != NULL && parse_attributes (&data, &a, &v)) { |
| 750 |
if (strcmp (a, "CAN-SKIP-UNTIL") == 0) { |
| 751 |
if (!time_from_double_in_string (v, NULL, &self->skip_boundary)) { |
| 752 |
GST_WARNING ("Can't read Skip Boundary value"); |
| 753 |
goto malformed_line; |
| 754 |
} |
| 755 |
} else if (strcmp (a, "CAN-SKIP-DATERANGES") == 0) { |
| 756 |
self->can_skip_dateranges = g_ascii_strcasecmp (v, "YES") == 0; |
| 757 |
} else if (strcmp (a, "HOLD-BACK") == 0) { |
| 758 |
if (!time_from_double_in_string (v, NULL, &self->hold_back)) { |
| 759 |
GST_WARNING ("Can't read Hold-Back value"); |
| 760 |
goto malformed_line; |
| 761 |
} |
| 762 |
} else if (strcmp (a, "PART-HOLD-BACK") == 0) { |
| 763 |
if (!time_from_double_in_string (v, NULL, &self->part_hold_back)) { |
| 764 |
GST_WARNING ("Can't read Part-Hold-Back value"); |
| 765 |
goto malformed_line; |
| 766 |
} |
| 767 |
} else if (strcmp (a, "CAN-BLOCK-RELOAD") == 0) { |
| 768 |
self->can_block_reload = g_ascii_strcasecmp (v, "YES") == 0; |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
return; |
| 773 |
malformed_line: |
| 774 |
{ |
| 775 |
GST_WARNING ("Invalid EXT-X-SERVER-CONTROL entry in playlist"); |
| 776 |
return; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
/* Parse EXT-X-SKIP */ |
| 781 |
static void |
| 782 |
parse_skip_tag (GstHLSMediaPlaylist * self, gchar * data) |
| 783 |
{ |
| 784 |
gchar *v, *a; |
| 785 |
|
| 786 |
while (data != NULL && parse_attributes (&data, &a, &v)) { |
| 787 |
if (strcmp (a, "SKIPPED-SEGMENTS") == 0) { |
| 788 |
if (!int_from_string (v, NULL, &self->skipped_segments) |
| 789 |
|| self->skipped_segments < 0) { |
| 790 |
GST_WARNING ("Can't read skipped segments from EXT-X-SKIP value"); |
| 791 |
self->skipped_segments = 0; |
| 792 |
goto malformed_line; |
| 793 |
} |
| 794 |
} else if (strcmp (a, "RECENTLY-REMOVED-DATERANGES") == 0) { |
| 795 |
gchar **removed_date_ranges = g_strsplit (v, "\t", -1); |
| 796 |
|
| 797 |
g_strfreev (self->removed_date_ranges); |
| 798 |
self->removed_date_ranges = removed_date_ranges; |
| 799 |
self->num_removed_date_ranges = g_strv_length (removed_date_ranges); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
return; |
| 804 |
malformed_line: |
| 805 |
{ |
| 806 |
GST_WARNING ("Invalid EXT-X-SKIP entry in playlist"); |
| 807 |
return; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
/* Parse and create a new GstHLSMediaPlaylist */ |
| 812 |
GstHLSMediaPlaylist * |
| 813 |
gst_hls_media_playlist_parse (gchar * data, |
| 814 |
GstClockTime playlist_ts, const gchar * uri, const gchar * base_uri) |
| 815 |
{ |
| 816 |
gchar *input_data = data; |
| 817 |
GstHLSMediaPlaylist *self; |
| 818 |
gint val; |
| 819 |
GstClockTime duration, partial_duration; |
| 820 |
gchar *title, *end; |
| 821 |
gboolean discontinuity = FALSE; |
| 822 |
gchar *current_key = NULL; |
| 823 |
gboolean have_iv = FALSE; |
| 824 |
guint8 iv[16] = { 0, }; |
| 825 |
gint64 size = -1, offset = -1; |
| 826 |
gint64 mediasequence = 0; |
| 827 |
gint64 dsn = 0; |
| 828 |
GDateTime *date_time = NULL; |
| 829 |
GstM3U8InitFile *last_init_file = NULL; |
| 830 |
GstM3U8MediaSegment *previous = NULL; |
| 831 |
GPtrArray *partial_segments = NULL; |
| 832 |
gboolean is_gap = FALSE; |
| 833 |
|
| 834 |
GST_LOG ("playlist ts: %" GST_TIMEP_FORMAT, &playlist_ts); |
| 835 |
GST_LOG ("uri: %s", uri); |
| 836 |
GST_LOG ("base_uri: %s", base_uri); |
| 837 |
GST_TRACE ("data:\n%s", data); |
| 838 |
|
| 839 |
if (!g_str_has_prefix (data, "#EXTM3U")) { |
| 840 |
GST_WARNING ("Data doesn't start with #EXTM3U"); |
| 841 |
g_free (data); |
| 842 |
return NULL; |
| 843 |
} |
| 844 |
|
| 845 |
if (g_strrstr (data, "\n#EXT-X-STREAM-INF:") != NULL) { |
| 846 |
GST_WARNING ("Not a media playlist, but a master playlist!"); |
| 847 |
g_free (data); |
| 848 |
return NULL; |
| 849 |
} |
| 850 |
|
| 851 |
self = gst_hls_media_playlist_new (uri, base_uri); |
| 852 |
self->playlist_ts = playlist_ts; |
| 853 |
|
| 854 |
/* Store a copy of the data */ |
| 855 |
self->last_data = g_strdup (data); |
| 856 |
|
| 857 |
duration = 0; |
| 858 |
partial_duration = 0; |
| 859 |
title = NULL; |
| 860 |
data += 7; |
| 861 |
while (TRUE) { |
| 862 |
gchar *r; |
| 863 |
|
| 864 |
end = g_utf8_strchr (data, -1, '\n'); |
| 865 |
if (end) |
| 866 |
*end = '\0'; |
| 867 |
|
| 868 |
r = g_utf8_strchr (data, -1, '\r'); |
| 869 |
if (r) |
| 870 |
*r = '\0'; |
| 871 |
|
| 872 |
if (data[0] != '#' && data[0] != '\0') { |
| 873 |
if (duration <= 0) { |
| 874 |
GST_LOG ("%s: got line without EXTINF, dropping", data); |
| 875 |
goto next_line; |
| 876 |
} |
| 877 |
|
| 878 |
data = uri_join (self->base_uri ? self->base_uri : self->uri, data); |
| 879 |
|
| 880 |
/* Let's check this is not a bogus duplicate entry */ |
| 881 |
if (previous && !discontinuity && !g_strcmp0 (data, previous->uri) |
| 882 |
&& (offset == -1 || previous->offset == offset)) { |
| 883 |
GST_WARNING ("Dropping duplicate segment entry"); |
| 884 |
g_free (data); |
| 885 |
data = NULL; |
| 886 |
date_time = NULL; |
| 887 |
duration = 0; |
| 888 |
partial_duration = 0; |
| 889 |
g_free (title); |
| 890 |
title = NULL; |
| 891 |
discontinuity = FALSE; |
| 892 |
size = offset = -1; |
| 893 |
is_gap = FALSE; |
| 894 |
if (partial_segments != NULL) { |
| 895 |
g_ptr_array_free (partial_segments, TRUE); |
| 896 |
partial_segments = NULL; |
| 897 |
} |
| 898 |
goto next_line; |
| 899 |
} |
| 900 |
if (data != NULL) { |
| 901 |
GstM3U8MediaSegment *file; |
| 902 |
/* We can finally create the segment */ |
| 903 |
/* The discontinuity sequence number is only stored if the header has |
| 904 |
* EXT-X-DISCONTINUITY-SEQUENCE present. */ |
| 905 |
file = |
| 906 |
gst_m3u8_media_segment_new (data, title, duration, mediasequence++, |
| 907 |
dsn, size, offset); |
| 908 |
self->duration += duration; |
| 909 |
|
| 910 |
file->is_gap = is_gap; |
| 911 |
|
| 912 |
/* set encryption params */ |
| 913 |
if (current_key != NULL) { |
| 914 |
file->key = g_strdup (current_key); |
| 915 |
if (have_iv) { |
| 916 |
memcpy (file->iv, iv, sizeof (iv)); |
| 917 |
} else { |
| 918 |
/* An EXT-X-KEY tag with a KEYFORMAT of "identity" that does |
| 919 |
* not have an IV attribute indicates that the Media Sequence |
| 920 |
* Number is to be used as the IV when decrypting a Media |
| 921 |
* Segment, by putting its big-endian binary representation |
| 922 |
* into a 16-octet (128-bit) buffer and padding (on the left) |
| 923 |
* with zeros. */ |
| 924 |
guint8 *iv = file->iv + 12; |
| 925 |
GST_WRITE_UINT32_BE (iv, file->sequence); |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
file->datetime = date_time; |
| 930 |
file->discont = discontinuity; |
| 931 |
if (last_init_file) |
| 932 |
file->init_file = gst_m3u8_init_file_ref (last_init_file); |
| 933 |
|
| 934 |
file->partial_segments = partial_segments; |
| 935 |
partial_segments = NULL; |
| 936 |
|
| 937 |
date_time = NULL; /* Ownership was passed to the segment */ |
| 938 |
duration = 0; |
| 939 |
partial_duration = 0; |
| 940 |
title = NULL; /* Ownership was passed to the segment */ |
| 941 |
discontinuity = FALSE; |
| 942 |
size = offset = -1; |
| 943 |
g_ptr_array_add (self->segments, file); |
| 944 |
previous = file; |
| 945 |
} |
| 946 |
|
| 947 |
} else if (g_str_has_prefix (data, "#EXTINF:")) { |
| 948 |
if (!time_from_double_in_string (data + 8, &data, &duration)) { |
| 949 |
GST_WARNING ("Can't read EXTINF duration"); |
| 950 |
goto next_line; |
| 951 |
} |
| 952 |
|
| 953 |
/* As of protocol version 6, targetduration is maximum segment duration |
| 954 |
* rounded to nearest integer seconds,so can be up to 0.5 seconds too low */ |
| 955 |
if (self->targetduration > 0 |
| 956 |
&& duration > (self->targetduration + GST_SECOND / 2)) { |
| 957 |
GST_DEBUG ("EXTINF duration (%" GST_TIME_FORMAT ") > TARGETDURATION (%" |
| 958 |
GST_TIME_FORMAT ")", GST_TIME_ARGS (duration), |
| 959 |
GST_TIME_ARGS (self->targetduration)); |
| 960 |
} |
| 961 |
if (!data || *data != ',') |
| 962 |
goto next_line; |
| 963 |
data = g_utf8_next_char (data); |
| 964 |
if (data != end) { |
| 965 |
g_free (title); |
| 966 |
title = g_strdup (data); |
| 967 |
} |
| 968 |
} else if (g_str_has_prefix (data, "#EXT-X-")) { |
| 969 |
gchar *data_ext_x = data + 7; |
| 970 |
|
| 971 |
/* All these entries start with #EXT-X- */ |
| 972 |
if (g_str_has_prefix (data_ext_x, "ENDLIST")) { |
| 973 |
self->endlist = TRUE; |
| 974 |
} else if (g_str_has_prefix (data_ext_x, "VERSION:")) { |
| 975 |
if (int_from_string (data + 15, &data, &val)) |
| 976 |
self->version = val; |
| 977 |
} else if (g_str_has_prefix (data_ext_x, "PLAYLIST-TYPE:")) { |
| 978 |
if (!g_strcmp0 (data + 21, "VOD")) |
| 979 |
self->type = GST_HLS_PLAYLIST_TYPE_VOD; |
| 980 |
else if (!g_strcmp0 (data + 21, "EVENT")) |
| 981 |
self->type = GST_HLS_PLAYLIST_TYPE_EVENT; |
| 982 |
else |
| 983 |
GST_WARNING ("Unknown playlist type '%s'", data + 21); |
| 984 |
} else if (g_str_has_prefix (data_ext_x, "TARGETDURATION:")) { |
| 985 |
if (int_from_string (data + 22, &data, &val)) |
| 986 |
self->targetduration = val * GST_SECOND; |
| 987 |
} else if (g_str_has_prefix (data_ext_x, "MEDIA-SEQUENCE:")) { |
| 988 |
if (int_from_string (data + 22, &data, &val)) |
| 989 |
self->media_sequence = mediasequence = val; |
| 990 |
} else if (g_str_has_prefix (data_ext_x, "DISCONTINUITY-SEQUENCE:")) { |
| 991 |
if (int_from_string (data + 30, &data, &val) |
| 992 |
&& val != self->discont_sequence) { |
| 993 |
dsn = self->discont_sequence = val; |
| 994 |
self->has_ext_x_dsn = TRUE; |
| 995 |
} |
| 996 |
} else if (g_str_has_prefix (data_ext_x, "DISCONTINUITY")) { |
| 997 |
dsn++; |
| 998 |
discontinuity = TRUE; |
| 999 |
} else if (g_str_has_prefix (data_ext_x, "PROGRAM-DATE-TIME:")) { |
| 1000 |
date_time = g_date_time_new_from_iso8601 (data + 25, NULL); |
| 1001 |
if (date_time) |
| 1002 |
self->ext_x_pdt_present = TRUE; |
| 1003 |
} else if (g_str_has_prefix (data_ext_x, "ALLOW-CACHE:")) { |
| 1004 |
self->allowcache = g_ascii_strcasecmp (data + 19, "YES") == 0; |
| 1005 |
} else if (g_str_has_prefix (data_ext_x, "KEY:")) { |
| 1006 |
gchar *v, *a; |
| 1007 |
|
| 1008 |
data = data + 11; |
| 1009 |
|
| 1010 |
/* IV and KEY are only valid until the next #EXT-X-KEY */ |
| 1011 |
have_iv = FALSE; |
| 1012 |
g_free (current_key); |
| 1013 |
current_key = NULL; |
| 1014 |
while (data && parse_attributes (&data, &a, &v)) { |
| 1015 |
if (g_str_equal (a, "URI")) { |
| 1016 |
current_key = |
| 1017 |
uri_join (self->base_uri ? self->base_uri : self->uri, v); |
| 1018 |
} else if (g_str_equal (a, "IV")) { |
| 1019 |
gchar *ivp = v; |
| 1020 |
gint i; |
| 1021 |
|
| 1022 |
if (strlen (ivp) < 32 + 2 || (!g_str_has_prefix (ivp, "0x") |
| 1023 |
&& !g_str_has_prefix (ivp, "0X"))) { |
| 1024 |
GST_WARNING ("Can't read IV"); |
| 1025 |
continue; |
| 1026 |
} |
| 1027 |
|
| 1028 |
ivp += 2; |
| 1029 |
for (i = 0; i < 16; i++) { |
| 1030 |
gint h, l; |
| 1031 |
|
| 1032 |
h = g_ascii_xdigit_value (*ivp); |
| 1033 |
ivp++; |
| 1034 |
l = g_ascii_xdigit_value (*ivp); |
| 1035 |
ivp++; |
| 1036 |
if (h == -1 || l == -1) { |
| 1037 |
i = -1; |
| 1038 |
break; |
| 1039 |
} |
| 1040 |
iv[i] = (h << 4) | l; |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (i == -1) { |
| 1044 |
GST_WARNING ("Can't read IV"); |
| 1045 |
continue; |
| 1046 |
} |
| 1047 |
have_iv = TRUE; |
| 1048 |
} else if (g_str_equal (a, "METHOD")) { |
| 1049 |
if (!g_str_equal (v, "AES-128") && !g_str_equal (v, "NONE")) { |
| 1050 |
GST_WARNING ("Encryption method %s not supported", v); |
| 1051 |
continue; |
| 1052 |
} |
| 1053 |
self->ext_x_key_present = TRUE; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
} else if (g_str_has_prefix (data_ext_x, "BYTERANGE:")) { |
| 1057 |
gchar *v = data + 17; |
| 1058 |
|
| 1059 |
size = -1; |
| 1060 |
offset = -1; |
| 1061 |
|
| 1062 |
if (!int64_from_string (v, &v, &size)) |
| 1063 |
goto next_line; |
| 1064 |
|
| 1065 |
if (*v == '@' && !int64_from_string (v + 1, &v, &offset)) |
| 1066 |
goto next_line; |
| 1067 |
|
| 1068 |
/* Either there must be an offset, or there must |
| 1069 |
* be a previous segment to calculate from */ |
| 1070 |
if (offset == -1) { |
| 1071 |
if (previous == NULL) |
| 1072 |
goto next_line; |
| 1073 |
|
| 1074 |
offset = previous->offset + previous->size; |
| 1075 |
} |
| 1076 |
} else if (g_str_has_prefix (data_ext_x, "MAP:")) { |
| 1077 |
gchar *v, *a, *header_uri = NULL; |
| 1078 |
gint64 init_size = -1, init_offset = -1; |
| 1079 |
|
| 1080 |
data = data + 11; |
| 1081 |
|
| 1082 |
while (data != NULL && parse_attributes (&data, &a, &v)) { |
| 1083 |
if (strcmp (a, "URI") == 0) { |
| 1084 |
header_uri = |
| 1085 |
uri_join (self->base_uri ? self->base_uri : self->uri, v); |
| 1086 |
} else if (strcmp (a, "BYTERANGE") == 0) { |
| 1087 |
if (!int64_from_string (v, &v, &init_size)) { |
| 1088 |
g_free (header_uri); |
| 1089 |
goto next_line; |
| 1090 |
} |
| 1091 |
if (*v == '@' && !int64_from_string (v + 1, &v, &init_offset)) { |
| 1092 |
g_free (header_uri); |
| 1093 |
goto next_line; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (header_uri) { |
| 1099 |
GstM3U8InitFile *init_file; |
| 1100 |
init_file = |
| 1101 |
gst_m3u8_init_file_new (header_uri, init_size, init_offset); |
| 1102 |
|
| 1103 |
if (last_init_file) |
| 1104 |
gst_m3u8_init_file_unref (last_init_file); |
| 1105 |
|
| 1106 |
last_init_file = init_file; |
| 1107 |
} |
| 1108 |
} else if (g_str_has_prefix (data_ext_x, "GAP:")) { |
| 1109 |
is_gap = TRUE; |
| 1110 |
} else if (g_str_has_prefix (data_ext_x, "PART:")) { |
| 1111 |
GstM3U8PartialSegment *part = NULL; |
| 1112 |
|
| 1113 |
part = |
| 1114 |
gst_m3u8_parse_partial_segment (data + strlen ("#EXT-X-PART:"), |
| 1115 |
self->base_uri ? self->base_uri : self->uri); |
| 1116 |
if (part == NULL) |
| 1117 |
goto next_line; |
| 1118 |
|
| 1119 |
if (partial_segments == NULL) { |
| 1120 |
partial_segments = g_ptr_array_new_full (2, |
| 1121 |
(GDestroyNotify) gst_m3u8_partial_segment_unref); |
| 1122 |
} |
| 1123 |
g_ptr_array_add (partial_segments, part); |
| 1124 |
partial_duration += part->duration; |
| 1125 |
|
| 1126 |
} else if (g_str_has_prefix (data_ext_x, "PART-INF:")) { |
| 1127 |
gchar *v, *a; |
| 1128 |
|
| 1129 |
data += strlen ("#EXT-X-PART-INF:"); |
| 1130 |
|
| 1131 |
while (data != NULL && parse_attributes (&data, &a, &v)) { |
| 1132 |
if (strcmp (a, "PART-TARGET") == 0) { |
| 1133 |
if (!time_from_double_in_string (v, NULL, |
| 1134 |
&self->partial_targetduration)) { |
| 1135 |
GST_WARNING ("Invalid PART-TARGET"); |
| 1136 |
goto next_line; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
} |
| 1140 |
} else if (g_str_has_prefix (data_ext_x, "SERVER-CONTROL:")) { |
| 1141 |
data += strlen ("#EXT-X-SERVER-CONTROL:"); |
| 1142 |
parse_server_control (self, data); |
| 1143 |
} else if (g_str_has_prefix (data_ext_x, "PRELOAD-HINT:")) { |
| 1144 |
GstM3U8PreloadHint *hint = NULL; |
| 1145 |
|
| 1146 |
hint = |
| 1147 |
gst_m3u8_parse_preload_hint (data + strlen ("#EXT-X-PRELOAD-HINT:"), |
| 1148 |
self->base_uri ? self->base_uri : self->uri); |
| 1149 |
if (hint == NULL) |
| 1150 |
goto next_line; |
| 1151 |
|
| 1152 |
if (self->preload_hints == NULL) { |
| 1153 |
self->preload_hints = g_ptr_array_new_full (1, |
| 1154 |
(GDestroyNotify) gst_m3u8_preload_hint_unref); |
| 1155 |
} |
| 1156 |
g_ptr_array_add (self->preload_hints, hint); |
| 1157 |
} else if (g_str_has_prefix (data_ext_x, "SKIP:")) { |
| 1158 |
data += strlen ("#EXT-X-SKIP:"); |
| 1159 |
parse_skip_tag (self, data); |
| 1160 |
/* Increment the current MSN by the number |
| 1161 |
* of segments that were removed */ |
| 1162 |
mediasequence += self->skipped_segments; |
| 1163 |
} else { |
| 1164 |
GST_LOG ("Ignored line: %s", data); |
| 1165 |
} |
| 1166 |
} else if (data[0]) { |
| 1167 |
/* Log non-empty lines */ |
| 1168 |
GST_LOG ("Ignored line: `%s`", data); |
| 1169 |
} |
| 1170 |
|
| 1171 |
next_line: |
| 1172 |
if (!end) |
| 1173 |
break; |
| 1174 |
data = g_utf8_next_char (end); /* skip \n */ |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* If there are trailing partial segments at the end, |
| 1178 |
* create a dummy segment to hold them */ |
| 1179 |
if (partial_segments != NULL) { |
| 1180 |
GstM3U8MediaSegment *file; |
| 1181 |
GST_DEBUG ("Creating dummy segment for trailing partial segments"); |
| 1182 |
|
| 1183 |
/* The discontinuity sequence number is only stored if the header has |
| 1184 |
* EXT-X-DISCONTINUITY-SEQUENCE present. */ |
| 1185 |
file = |
| 1186 |
gst_m3u8_media_segment_new (NULL, title, partial_duration, |
| 1187 |
mediasequence++, dsn, size, offset); |
| 1188 |
|
| 1189 |
file->partial_only = TRUE; |
| 1190 |
|
| 1191 |
self->duration += partial_duration; |
| 1192 |
|
| 1193 |
file->is_gap = is_gap; |
| 1194 |
|
| 1195 |
/* set encryption params */ |
| 1196 |
if (current_key != NULL) { |
| 1197 |
file->key = g_strdup (current_key); |
| 1198 |
if (have_iv) { |
| 1199 |
memcpy (file->iv, iv, sizeof (iv)); |
| 1200 |
} else { |
| 1201 |
/* An EXT-X-KEY tag with a KEYFORMAT of "identity" that does |
| 1202 |
* not have an IV attribute indicates that the Media Sequence |
| 1203 |
* Number is to be used as the IV when decrypting a Media |
| 1204 |
* Segment, by putting its big-endian binary representation |
| 1205 |
* into a 16-octet (128-bit) buffer and padding (on the left) |
| 1206 |
* with zeros. */ |
| 1207 |
guint8 *iv = file->iv + 12; |
| 1208 |
GST_WRITE_UINT32_BE (iv, file->sequence); |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
file->datetime = date_time; |
| 1213 |
file->discont = discontinuity; |
| 1214 |
if (last_init_file) |
| 1215 |
file->init_file = gst_m3u8_init_file_ref (last_init_file); |
| 1216 |
|
| 1217 |
file->partial_segments = partial_segments; |
| 1218 |
partial_segments = NULL; |
| 1219 |
|
| 1220 |
date_time = NULL; /* Ownership was passed to the partial segment */ |
| 1221 |
duration = 0; |
| 1222 |
partial_duration = 0; |
| 1223 |
title = NULL; /* Ownership was passed to the partial segment */ |
| 1224 |
discontinuity = FALSE; |
| 1225 |
size = offset = -1; |
| 1226 |
g_ptr_array_add (self->segments, file); |
| 1227 |
previous = file; |
| 1228 |
} |
| 1229 |
|
| 1230 |
/* Clean up date that wasn't freed / handed to a segment */ |
| 1231 |
g_free (current_key); |
| 1232 |
current_key = NULL; |
| 1233 |
if (date_time) |
| 1234 |
g_date_time_unref (date_time); |
| 1235 |
g_free (title); |
| 1236 |
|
| 1237 |
g_free (input_data); |
| 1238 |
|
| 1239 |
if (last_init_file) |
| 1240 |
gst_m3u8_init_file_unref (last_init_file); |
| 1241 |
|
| 1242 |
if (self->segments->len == 0) { |
| 1243 |
GST_ERROR ("Invalid media playlist, it does not contain any media files"); |
| 1244 |
gst_hls_media_playlist_unref (self); |
| 1245 |
return NULL; |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (!self->has_ext_x_dsn) |
| 1249 |
self->discont_sequence = 0; |
| 1250 |
|
| 1251 |
/* Now go over the parsed data to ensure MSN and/or PDT are set */ |
| 1252 |
if (self->ext_x_pdt_present) |
| 1253 |
gst_hls_media_playlist_postprocess_pdt (self); |
| 1254 |
|
| 1255 |
/* If we are not live, the stream time can be directly applied */ |
| 1256 |
if (!GST_HLS_MEDIA_PLAYLIST_IS_LIVE (self)) { |
| 1257 |
gint iter, len = self->segments->len; |
| 1258 |
GstClockTimeDiff stream_time = 0; |
| 1259 |
|
| 1260 |
for (iter = 0; iter < len; iter++) { |
| 1261 |
GstM3U8MediaSegment *segment = g_ptr_array_index (self->segments, iter); |
| 1262 |
segment->stream_time = stream_time; |
| 1263 |
gst_m3u8_media_segment_fill_partial_stream_times (segment); |
| 1264 |
|
| 1265 |
stream_time += segment->duration; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
gst_hls_media_playlist_dump (self); |
| 1270 |
return self; |
| 1271 |
} |
| 1272 |
|
| 1273 |
/* Returns TRUE if the m3u8 as the same data as playlist_data */ |
| 1274 |
gboolean |
| 1275 |
gst_hls_media_playlist_has_same_data (GstHLSMediaPlaylist * self, |
| 1276 |
gchar * playlist_data) |
| 1277 |
{ |
| 1278 |
gboolean ret; |
| 1279 |
|
| 1280 |
GST_HLS_MEDIA_PLAYLIST_LOCK (self); |
| 1281 |
|
| 1282 |
ret = self->last_data && g_str_equal (self->last_data, playlist_data); |
| 1283 |
|
| 1284 |
GST_HLS_MEDIA_PLAYLIST_UNLOCK (self); |
| 1285 |
|
| 1286 |
return ret; |
| 1287 |
} |
| 1288 |
|
| 1289 |
/* gst_hls_media_playlist_seek() is used when performing |
| 1290 |
* an actual seek. It finds a suitable segment (or partial segment |
| 1291 |
* for LL-HLS) at which to resume playback. Only partial segments |
| 1292 |
* in the last 2 target durations of the live edge are considered |
| 1293 |
* when playing live, otherwise we might start playing a partial |
| 1294 |
* segment group that disappears before we're done with it. |
| 1295 |
* We want a segment or partial that contains a keyframe if possible |
| 1296 |
*/ |
| 1297 |
gboolean |
| 1298 |
gst_hls_media_playlist_seek (GstHLSMediaPlaylist * playlist, gboolean forward, |
| 1299 |
GstSeekFlags flags, GstClockTimeDiff ts, GstM3U8SeekResult * seek_result) |
| 1300 |
{ |
| 1301 |
gboolean snap_nearest = |
| 1302 |
(flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST; |
| 1303 |
gboolean snap_after = |
| 1304 |
(flags & GST_SEEK_FLAG_SNAP_AFTER) == GST_SEEK_FLAG_SNAP_AFTER; |
| 1305 |
gboolean want_keyunit = (flags & GST_SEEK_FLAG_KEY_UNIT); |
| 1306 |
guint idx; |
| 1307 |
GstM3U8MediaSegment *res = NULL; |
| 1308 |
guint res_part_idx = 0; |
| 1309 |
GstClockTime partial_window_start = GST_CLOCK_TIME_NONE; |
| 1310 |
|
| 1311 |
GST_DEBUG ("target ts:%" GST_STIME_FORMAT " forward:%d playlist uri: %s", |
| 1312 |
GST_STIME_ARGS (ts), forward, playlist->uri); |
| 1313 |
|
| 1314 |
/* Can't seek if there's no segments */ |
| 1315 |
if (playlist->segments->len < 1) |
| 1316 |
return FALSE; |
| 1317 |
|
| 1318 |
/* Calculate the threshold at which we might start inspecting partial segments */ |
| 1319 |
if (flags & GST_HLS_M3U8_SEEK_FLAG_ALLOW_PARTIAL) { |
| 1320 |
GstM3U8MediaSegment *last_seg = |
| 1321 |
g_ptr_array_index (playlist->segments, playlist->segments->len - 1); |
| 1322 |
GstClockTime playlist_end = last_seg->stream_time + last_seg->duration; |
| 1323 |
|
| 1324 |
if (playlist_end >= 2 * playlist->targetduration) |
| 1325 |
partial_window_start = playlist_end - 2 * playlist->targetduration; |
| 1326 |
else |
| 1327 |
partial_window_start = last_seg->stream_time; |
| 1328 |
|
| 1329 |
GST_DEBUG ("Partial segment threshold %" GST_TIME_FORMAT, |
| 1330 |
GST_TIME_ARGS (partial_window_start)); |
| 1331 |
} |
| 1332 |
|
| 1333 |
for (idx = 0; idx < playlist->segments->len; idx++) { |
| 1334 |
GstM3U8MediaSegment *cand = g_ptr_array_index (playlist->segments, idx); |
| 1335 |
|
| 1336 |
if (flags & GST_HLS_M3U8_SEEK_FLAG_ALLOW_PARTIAL && |
| 1337 |
GST_CLOCK_TIME_IS_VALID (partial_window_start) && |
| 1338 |
cand->stream_time + cand->duration > partial_window_start) { |
| 1339 |
/* Permitted to land at a partial segment, but only do so if |
| 1340 |
* they are in the last 2 target durations of the playlist, so we can |
| 1341 |
* be fairly sure we'll have to time download them all before |
| 1342 |
* they get removed. |
| 1343 |
* |
| 1344 |
* 6.2.2: EXT-X-PART tags SHOULD be removed from the Playlist after they are |
| 1345 |
* greater than three Target Durations from the end of the Playlist. |
| 1346 |
* Clients MUST be able to download the Partial Segment for at least |
| 1347 |
* three Target Durations after the EXT-X-PART tag is removed from the |
| 1348 |
* Playlist. |
| 1349 |
*/ |
| 1350 |
if (cand->partial_segments != NULL) { |
| 1351 |
guint part_idx; |
| 1352 |
guint last_independent_idx = 0; |
| 1353 |
|
| 1354 |
for (part_idx = 0; part_idx < cand->partial_segments->len; part_idx++) { |
| 1355 |
GstM3U8PartialSegment *part = |
| 1356 |
g_ptr_array_index (cand->partial_segments, part_idx); |
| 1357 |
|
| 1358 |
GST_LOG ("Inspecting partial segment sn:%" G_GINT64_FORMAT |
| 1359 |
" idx %u stream_time:%" GST_STIME_FORMAT " duration:%" |
| 1360 |
GST_TIME_FORMAT, cand->sequence, part_idx, |
| 1361 |
GST_STIME_ARGS (part->stream_time), |
| 1362 |
GST_TIME_ARGS (part->duration)); |
| 1363 |
|
| 1364 |
if ((forward & snap_after) || snap_nearest) { |
| 1365 |
if (!want_keyunit || part->independent) { |
| 1366 |
if (part->stream_time >= ts || |
| 1367 |
(snap_nearest |
| 1368 |
&& (ts - part->stream_time < part->duration / 2))) { |
| 1369 |
res = cand; |
| 1370 |
res_part_idx = part_idx; |
| 1371 |
goto partial_seg_out; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
} else if (!forward && snap_after) { |
| 1375 |
GstClockTime next_pos = cand->stream_time + cand->duration; |
| 1376 |
|
| 1377 |
if (!want_keyunit || part->independent) { |
| 1378 |
if (next_pos <= ts && ts < next_pos + cand->duration) { |
| 1379 |
res = cand; |
| 1380 |
res_part_idx = part_idx; |
| 1381 |
goto partial_seg_out; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
} else if (part->stream_time <= ts |
| 1385 |
&& ts < (GstClockTimeDiff) (part->stream_time + part->duration)) { |
| 1386 |
res = cand; |
| 1387 |
if (!want_keyunit || part->independent) |
| 1388 |
res_part_idx = part_idx; |
| 1389 |
else |
| 1390 |
res_part_idx = last_independent_idx; |
| 1391 |
goto partial_seg_out; |
| 1392 |
} |
| 1393 |
|
| 1394 |
if (part->independent) |
| 1395 |
last_independent_idx = part_idx; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
} else if (cand->partial_only) { |
| 1399 |
/* If only full segments were requested or we're still outside the partial segment |
| 1400 |
* window, skip the last segment if it only has EXT-X-PARTs attached */ |
| 1401 |
continue; |
| 1402 |
} |
| 1403 |
|
| 1404 |
/* For full segment alignment, we ignore the KEY_UNIT flag and assume |
| 1405 |
* all segments have a keyframe, since HLS doesn't give us reliable info |
| 1406 |
* about that */ |
| 1407 |
if ((forward & snap_after) || snap_nearest) { |
| 1408 |
if (cand->stream_time >= ts || |
| 1409 |
(snap_nearest && (ts - cand->stream_time < cand->duration / 2))) { |
| 1410 |
res = cand; |
| 1411 |
goto out; |
| 1412 |
} |
| 1413 |
} else if (!forward && snap_after) { |
| 1414 |
GstClockTime next_pos = cand->stream_time + cand->duration; |
| 1415 |
|
| 1416 |
if (next_pos <= ts && ts < next_pos + cand->duration) { |
| 1417 |
res = cand; |
| 1418 |
goto out; |
| 1419 |
} |
| 1420 |
} else if ((cand->stream_time <= ts || idx == 0) |
| 1421 |
&& ts < (GstClockTimeDiff) (cand->stream_time + cand->duration)) { |
| 1422 |
res = cand; |
| 1423 |
goto out; |
| 1424 |
} |
| 1425 |
} |
| 1426 |
|
| 1427 |
out: |
| 1428 |
if (res) { |
| 1429 |
GST_DEBUG ("Returning segment sn:%" G_GINT64_FORMAT " stream_time:%" |
| 1430 |
GST_STIME_FORMAT " duration:%" GST_TIME_FORMAT, res->sequence, |
| 1431 |
GST_STIME_ARGS (res->stream_time), GST_TIME_ARGS (res->duration)); |
| 1432 |
|
| 1433 |
seek_result->stream_time = res->stream_time; |
| 1434 |
seek_result->segment = gst_m3u8_media_segment_ref (res); |
| 1435 |
seek_result->found_partial_segment = res->partial_only; |
| 1436 |
seek_result->part_idx = 0; |
| 1437 |
return TRUE; |
| 1438 |
} |
| 1439 |
|
| 1440 |
GST_DEBUG ("Couldn't find a match"); |
| 1441 |
return FALSE; |
| 1442 |
|
| 1443 |
partial_seg_out: |
| 1444 |
if (res && res->partial_segments != NULL |
| 1445 |
&& res_part_idx < res->partial_segments->len) { |
| 1446 |
GstM3U8PartialSegment *part = |
| 1447 |
g_ptr_array_index (res->partial_segments, res_part_idx); |
| 1448 |
|
| 1449 |
GST_DEBUG ("Returning partial segment sn:%" G_GINT64_FORMAT |
| 1450 |
" part_idx %u stream_time:%" GST_STIME_FORMAT " duration:%" |
| 1451 |
GST_TIME_FORMAT, res->sequence, res_part_idx, |
| 1452 |
GST_STIME_ARGS (part->stream_time), GST_TIME_ARGS (part->duration)); |
| 1453 |
|
| 1454 |
seek_result->stream_time = part->stream_time; |
| 1455 |
seek_result->segment = gst_m3u8_media_segment_ref (res); |
| 1456 |
seek_result->found_partial_segment = TRUE; |
| 1457 |
seek_result->part_idx = res_part_idx; |
| 1458 |
return TRUE; |
| 1459 |
} |
| 1460 |
|
| 1461 |
GST_DEBUG ("Couldn't find a match"); |
| 1462 |
return FALSE; |
| 1463 |
} |
| 1464 |
|
| 1465 |
static gboolean |
| 1466 |
gst_hls_media_playlist_find_partial_position (GstHLSMediaPlaylist * playlist, |
| 1467 |
GstM3U8MediaSegment * seg, GstClockTimeDiff ts, |
| 1468 |
GstM3U8SeekResult * seek_result) |
| 1469 |
{ |
| 1470 |
guint i; |
| 1471 |
|
| 1472 |
/* As with full segment search below, we more often want to find our position |
| 1473 |
* near the end of a live playlist, so iterate segments backward */ |
| 1474 |
for (i = seg->partial_segments->len; i > 0; i--) { |
| 1475 |
guint part_idx = i - 1; |
| 1476 |
GstM3U8PartialSegment *cand = |
| 1477 |
g_ptr_array_index (seg->partial_segments, part_idx); |
| 1478 |
|
| 1479 |
GST_DEBUG ("partial segment %d ts:%" GST_STIME_FORMAT " end:%" |
| 1480 |
GST_STIME_FORMAT, part_idx, GST_STIME_ARGS (cand->stream_time), |
| 1481 |
GST_STIME_ARGS ((GstClockTimeDiff) (cand->stream_time + |
| 1482 |
cand->duration))); |
| 1483 |
|
| 1484 |
/* If the target timestamp is before this partial segment, or in the first half, this |
| 1485 |
* is the partial segment to land in */ |
| 1486 |
if ((GstClockTimeDiff) (cand->stream_time + (cand->duration / 2)) >= ts && |
| 1487 |
cand->stream_time <= (GstClockTimeDiff) (ts + (cand->duration / 2))) { |
| 1488 |
GST_DEBUG ("choosing partial segment %d", part_idx); |
| 1489 |
seek_result->segment = gst_m3u8_media_segment_ref (seg); |
| 1490 |
seek_result->found_partial_segment = TRUE; |
| 1491 |
seek_result->part_idx = part_idx; |
| 1492 |
seek_result->stream_time = cand->stream_time; |
| 1493 |
return TRUE; |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
return FALSE; |
| 1498 |
} |
| 1499 |
|
| 1500 |
/* gst_hls_media_playlist_find_position() is used when finding the segment or |
| 1501 |
* partial segment that corresponds to our current playback position. |
| 1502 |
* |
| 1503 |
* If we're "playing partial segments", we want to find the partial segment |
| 1504 |
* whose stream_time matches the target position most closely (or fail if |
| 1505 |
* there's no partial segment, since the target partial segment was removed from |
| 1506 |
* the playlist and we lost sync. |
| 1507 |
* |
| 1508 |
* If not currently playing partial segment, find the segment with a stream_time |
| 1509 |
* that matches, or the partial segment exactly at the start of the |
| 1510 |
* 'partial_only' segment. |
| 1511 |
*/ |
| 1512 |
gboolean |
| 1513 |
gst_hls_media_playlist_find_position (GstHLSMediaPlaylist * playlist, |
| 1514 |
GstClockTimeDiff ts, gboolean in_partial_segments, |
| 1515 |
GstM3U8SeekResult * seek_result) |
| 1516 |
{ |
| 1517 |
guint i; |
| 1518 |
GstM3U8MediaSegment *seg = NULL, *following = NULL; |
| 1519 |
|
| 1520 |
GST_DEBUG ("ts:%" GST_STIME_FORMAT |
| 1521 |
" in_partial_segments %d (live %d) playlist uri: %s", GST_STIME_ARGS (ts), |
| 1522 |
in_partial_segments, GST_HLS_MEDIA_PLAYLIST_IS_LIVE (playlist), |
| 1523 |
playlist->uri); |
| 1524 |
|
| 1525 |
/* The *common* case is that we want to find our position in a live playback |
| 1526 |
* scenario, when we're playing close to the live edge, so start at the end |
| 1527 |
* of the segments and go backward */ |
| 1528 |
for (i = playlist->segments->len; i != 0; i--) { |
| 1529 |
guint seg_idx = i - 1; |
| 1530 |
GstM3U8MediaSegment *cand = g_ptr_array_index (playlist->segments, seg_idx); |
| 1531 |
|
| 1532 |
GST_DEBUG ("segment %d ts:%" GST_STIME_FORMAT " end:%" GST_STIME_FORMAT |
| 1533 |
" partial only: %d", |
| 1534 |
seg_idx, GST_STIME_ARGS (cand->stream_time), |
| 1535 |
GST_STIME_ARGS ((GstClockTimeDiff) (cand->stream_time + |
| 1536 |
cand->duration)), cand->partial_only); |
| 1537 |
|
| 1538 |
/* Ignore any (disallowed by the spec) partial_only segment if |
| 1539 |
* the playlist is no longer live */ |
| 1540 |
if (cand->partial_only && !GST_HLS_MEDIA_PLAYLIST_IS_LIVE (playlist)) |
| 1541 |
continue; |
| 1542 |
|
| 1543 |
/* If the target stream time is definitely past the end |
| 1544 |
* of this segment, no earlier segment (with lower stream time) |
| 1545 |
* could match, so we fail */ |
| 1546 |
if (ts >= (GstClockTimeDiff) (cand->stream_time + (3 * cand->duration / 2))) { |
| 1547 |
break; |
| 1548 |
} |
| 1549 |
|
| 1550 |
if (in_partial_segments || cand->partial_only) { |
| 1551 |
if (cand->partial_segments == NULL) { |
| 1552 |
GstClockTime partial_targetduration = playlist->partial_targetduration; |
| 1553 |
|
| 1554 |
/* Default, if the playlist fails to give us a part duration (REQUIRED attribute, but |
| 1555 |
* maybe it got removed) */ |
| 1556 |
if (!GST_CLOCK_TIME_IS_VALID (partial_targetduration)) { |
| 1557 |
partial_targetduration = 200 * GST_MSECOND; |
| 1558 |
} |
| 1559 |
|
| 1560 |
/* If we want to match a partial segment but this segment doesn't have |
| 1561 |
* any, then the partial segment we want got removed from the playlist, |
| 1562 |
* so we need to fail, except in the specific case that our target |
| 1563 |
* timestamp is within half a part duration of the segment start |
| 1564 |
* itself (ie, we wanted the *first* partial segment |
| 1565 |
*/ |
| 1566 |
if (cand->stream_time + (partial_targetduration / 2) >= ts && |
| 1567 |
cand->stream_time <= ts + (partial_targetduration / 2)) { |
| 1568 |
GST_DEBUG ("choosing full segment %d", seg_idx); |
| 1569 |
seek_result->stream_time = seg->stream_time; |
| 1570 |
seek_result->segment = gst_m3u8_media_segment_ref (seg); |
| 1571 |
seek_result->found_partial_segment = FALSE; |
| 1572 |
return TRUE; |
| 1573 |
} |
| 1574 |
|
| 1575 |
GST_DEBUG ("Couldn't find a matching partial segment"); |
| 1576 |
return FALSE; |
| 1577 |
} |
| 1578 |
|
| 1579 |
/* If our partial segment target ts is within half a partial duration |
| 1580 |
* of this segment start/finish, check the partial segments for a match */ |
| 1581 |
if (gst_hls_media_playlist_find_partial_position (playlist, cand, ts, |
| 1582 |
seek_result)) { |
| 1583 |
GST_DEBUG ("Returning partial segment sn:%" G_GINT64_FORMAT |
| 1584 |
" part %u stream_time:%" GST_STIME_FORMAT, cand->sequence, |
| 1585 |
seek_result->part_idx, GST_STIME_ARGS (seek_result->stream_time)); |
| 1586 |
return TRUE; |
| 1587 |
} |
| 1588 |
} |
| 1589 |
|
| 1590 |
/* Otherwise, we're doing a full segment match so check that the timestamp |
| 1591 |
* is within half a segment duration of this segment stream_time. |
| 1592 |
* |
| 1593 |
* If the final segment has partial fragments, the target duration is used |
| 1594 |
* instead, because the partial-only last segment might be quite small (as |
| 1595 |
* it's still being created), which can cause a missed match otherwise */ |
| 1596 |
GstClockTimeDiff match_threshold = |
| 1597 |
(cand->partial_only ? playlist->targetduration : cand->duration) / 2; |
| 1598 |
|
| 1599 |
/* If the requesting position is beyond the halfway point of this segment, |
| 1600 |
* we should return the following segment, which is closer to the requested |
| 1601 |
* position. Note that it can be NULL */ |
| 1602 |
if (ts > cand->stream_time + match_threshold) { |
| 1603 |
if (following) |
| 1604 |
GST_DEBUG ("choosing following segment %d", seg_idx + 1); |
| 1605 |
else |
| 1606 |
GST_DEBUG ("After last segment"); |
| 1607 |
seg = following; |
| 1608 |
break; |
| 1609 |
} |
| 1610 |
|
| 1611 |
/* Else if the requested position is within the first half we are sure it's |
| 1612 |
* this segment */ |
| 1613 |
if (ts >= cand->stream_time) { |
| 1614 |
GST_DEBUG ("choosing segment %d", seg_idx); |
| 1615 |
seg = cand; |
| 1616 |
break; |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* Last check for the very first segment in the playlist */ |
| 1620 |
if (i == 1 && cand->stream_time <= ts + match_threshold) { |
| 1621 |
GST_DEBUG ("choosing first segment"); |
| 1622 |
seg = cand; |
| 1623 |
break; |
| 1624 |
} |
| 1625 |
|
| 1626 |
/* We are scanning backwards, remember this as the following segment */ |
| 1627 |
following = cand; |
| 1628 |
} |
| 1629 |
|
| 1630 |
if (seg == NULL) { |
| 1631 |
GST_DEBUG ("Couldn't find a matching segment"); |
| 1632 |
return FALSE; |
| 1633 |
} |
| 1634 |
|
| 1635 |
/* The partial_only segment case should have been handled above |
| 1636 |
* by gst_hls_media_playlist_find_partial_position(). If it |
| 1637 |
* wasn't, it implies the segment we're looking for was not |
| 1638 |
* present in the available partial segments at all, |
| 1639 |
* so we need to return FALSE */ |
| 1640 |
if (seg->partial_only) { |
| 1641 |
GST_DEBUG |
| 1642 |
("Couldn't find a matching partial segment in the partial_only segment"); |
| 1643 |
return FALSE; |
| 1644 |
} |
| 1645 |
|
| 1646 |
seek_result->stream_time = seg->stream_time; |
| 1647 |
seek_result->segment = gst_m3u8_media_segment_ref (seg); |
| 1648 |
seek_result->found_partial_segment = FALSE; |
| 1649 |
|
| 1650 |
GST_DEBUG ("Returning segment sn:%" G_GINT64_FORMAT " stream_time:%" |
| 1651 |
GST_STIME_FORMAT " duration:%" GST_TIME_FORMAT, seg->sequence, |
| 1652 |
GST_STIME_ARGS (seg->stream_time), GST_TIME_ARGS (seg->duration)); |
| 1653 |
|
| 1654 |
return TRUE; |
| 1655 |
} |
| 1656 |
|
| 1657 |
/* Recalculate all segment DSN based on the DSN of the provided anchor segment |
| 1658 |
* (which must belong to the playlist). */ |
| 1659 |
static void |
| 1660 |
gst_hls_media_playlist_recalculate_dsn (GstHLSMediaPlaylist * playlist, |
| 1661 |
GstM3U8MediaSegment * anchor) |
| 1662 |
{ |
| 1663 |
guint idx; |
| 1664 |
gint iter; |
| 1665 |
GstM3U8MediaSegment *cand, *prev; |
| 1666 |
|
| 1667 |
if (!g_ptr_array_find (playlist->segments, anchor, &idx)) { |
| 1668 |
g_assert (FALSE); |
| 1669 |
} |
| 1670 |
|
| 1671 |
g_assert (idx != -1); |
| 1672 |
|
| 1673 |
GST_DEBUG ("Re-calculating DSN from segment #%d %" G_GINT64_FORMAT, |
| 1674 |
idx, anchor->discont_sequence); |
| 1675 |
|
| 1676 |
/* Forward */ |
| 1677 |
prev = anchor; |
| 1678 |
for (iter = idx + 1; iter < playlist->segments->len; iter++) { |
| 1679 |
cand = g_ptr_array_index (playlist->segments, iter); |
| 1680 |
if (cand->discont) |
| 1681 |
cand->discont_sequence = prev->discont_sequence + 1; |
| 1682 |
else |
| 1683 |
cand->discont_sequence = prev->discont_sequence; |
| 1684 |
prev = cand; |
| 1685 |
} |
| 1686 |
|
| 1687 |
/* Backward */ |
| 1688 |
prev = anchor; |
| 1689 |
for (iter = idx - 1; iter >= 0; iter--) { |
| 1690 |
cand = g_ptr_array_index (playlist->segments, iter); |
| 1691 |
if (prev->discont) |
| 1692 |
cand->discont_sequence = prev->discont_sequence - 1; |
| 1693 |
else |
| 1694 |
cand->discont_sequence = prev->discont_sequence; |
| 1695 |
prev = cand; |
| 1696 |
} |
| 1697 |
} |
| 1698 |
|
| 1699 |
|
| 1700 |
static void |
| 1701 |
gst_m3u8_media_segment_fill_partial_stream_times (GstM3U8MediaSegment * segment) |
| 1702 |
{ |
| 1703 |
guint idx; |
| 1704 |
GstClockTimeDiff stream_time = segment->stream_time; |
| 1705 |
|
| 1706 |
if (segment->partial_segments == NULL) |
| 1707 |
return; |
| 1708 |
|
| 1709 |
for (idx = 0; idx < segment->partial_segments->len; idx++) { |
| 1710 |
GstM3U8PartialSegment *part = |
| 1711 |
g_ptr_array_index (segment->partial_segments, idx); |
| 1712 |
|
| 1713 |
part->stream_time = stream_time; |
| 1714 |
stream_time += part->duration; |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
/* Recalculate all segment stream time based on the stream time of the provided |
| 1719 |
* anchor segment (which must belong to the playlist) */ |
| 1720 |
void |
| 1721 |
gst_hls_media_playlist_recalculate_stream_time (GstHLSMediaPlaylist * playlist, |
| 1722 |
GstM3U8MediaSegment * anchor) |
| 1723 |
{ |
| 1724 |
guint idx; |
| 1725 |
gint iter; |
| 1726 |
GstM3U8MediaSegment *cand, *prev; |
| 1727 |
|
| 1728 |
if (!g_ptr_array_find (playlist->segments, anchor, &idx)) { |
| 1729 |
g_assert (FALSE); |
| 1730 |
} |
| 1731 |
|
| 1732 |
g_assert (GST_CLOCK_TIME_IS_VALID (anchor->stream_time)); |
| 1733 |
g_assert (idx != -1); |
| 1734 |
|
| 1735 |
GST_DEBUG ("Re-calculating stream times from segment #%d %" GST_TIME_FORMAT, |
| 1736 |
idx, GST_TIME_ARGS (anchor->stream_time)); |
| 1737 |
gst_m3u8_media_segment_fill_partial_stream_times (anchor); |
| 1738 |
|
| 1739 |
/* Forward */ |
| 1740 |
prev = anchor; |
| 1741 |
for (iter = idx + 1; iter < playlist->segments->len; iter++) { |
| 1742 |
cand = g_ptr_array_index (playlist->segments, iter); |
| 1743 |
cand->stream_time = prev->stream_time + prev->duration; |
| 1744 |
GST_DEBUG ("Forward iter %d %" GST_STIME_FORMAT, iter, |
| 1745 |
GST_STIME_ARGS (cand->stream_time)); |
| 1746 |
gst_m3u8_media_segment_fill_partial_stream_times (cand); |
| 1747 |
prev = cand; |
| 1748 |
} |
| 1749 |
|
| 1750 |
/* Backward */ |
| 1751 |
prev = anchor; |
| 1752 |
for (iter = idx - 1; iter >= 0; iter--) { |
| 1753 |
cand = g_ptr_array_index (playlist->segments, iter); |
| 1754 |
cand->stream_time = prev->stream_time - cand->duration; |
| 1755 |
GST_DEBUG ("Backward iter %d %" GST_STIME_FORMAT, iter, |
| 1756 |
GST_STIME_ARGS (cand->stream_time)); |
| 1757 |
gst_m3u8_media_segment_fill_partial_stream_times (cand); |
| 1758 |
prev = cand; |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
void |
| 1763 |
gst_hls_media_playlist_recalculate_stream_time_from_part (GstHLSMediaPlaylist * |
| 1764 |
playlist, GstM3U8MediaSegment * anchor, guint part_idx) |
| 1765 |
{ |
| 1766 |
g_assert (anchor->partial_segments != NULL |
| 1767 |
&& part_idx < anchor->partial_segments->len); |
| 1768 |
|
| 1769 |
GstClockTimeDiff last_stream_time; |
| 1770 |
GstM3U8PartialSegment *part = |
| 1771 |
g_ptr_array_index (anchor->partial_segments, part_idx); |
| 1772 |
GstM3U8PartialSegment *cand, *prev; |
| 1773 |
gint iter; |
| 1774 |
|
| 1775 |
/* Work backward from the target partial segment, assigning stream times until |
| 1776 |
* we update the segment time itself, then recalculate all stream times */ |
| 1777 |
prev = part; |
| 1778 |
last_stream_time = part->stream_time; |
| 1779 |
for (iter = part_idx - 1; iter >= 0; iter--) { |
| 1780 |
cand = g_ptr_array_index (anchor->partial_segments, iter); |
| 1781 |
last_stream_time = cand->stream_time = prev->stream_time - cand->duration; |
| 1782 |
GST_DEBUG ("Backward partial segment iter %d %" GST_STIME_FORMAT, iter, |
| 1783 |
GST_STIME_ARGS (cand->stream_time)); |
| 1784 |
prev = cand; |
| 1785 |
} |
| 1786 |
anchor->stream_time = last_stream_time; |
| 1787 |
|
| 1788 |
gst_hls_media_playlist_recalculate_stream_time (playlist, anchor); |
| 1789 |
} |
| 1790 |
|
| 1791 |
/* If a segment with the same URI, size, offset, SN and DSN is present in the |
| 1792 |
* playlist, returns that one */ |
| 1793 |
static GstM3U8MediaSegment * |
| 1794 |
gst_hls_media_playlist_find_by_uri (GstHLSMediaPlaylist * playlist, |
| 1795 |
GstM3U8MediaSegment * segment) |
| 1796 |
{ |
| 1797 |
guint idx; |
| 1798 |
|
| 1799 |
for (idx = 0; idx < playlist->segments->len; idx++) { |
| 1800 |
GstM3U8MediaSegment *cand = g_ptr_array_index (playlist->segments, idx); |
| 1801 |
|
| 1802 |
if (cand->sequence == segment->sequence && |
| 1803 |
cand->discont_sequence == segment->discont_sequence && |
| 1804 |
cand->offset == segment->offset && cand->size == segment->size && |
| 1805 |
!g_strcmp0 (cand->uri, segment->uri)) { |
| 1806 |
return cand; |
| 1807 |
} |
| 1808 |
} |
| 1809 |
|
| 1810 |
return NULL; |
| 1811 |
} |
| 1812 |
|
| 1813 |
/* Find the equivalent segment in the given playlist. |
| 1814 |
* |
| 1815 |
* The returned segment does *NOT* have increased reference ! |
| 1816 |
* |
| 1817 |
* If the provided segment is just before the first entry of the playlist, it |
| 1818 |
* will be added to the playlist (with a reference) and is_before will be set to |
| 1819 |
* TRUE. |
| 1820 |
*/ |
| 1821 |
static GstM3U8MediaSegment * |
| 1822 |
find_segment_in_playlist (GstHLSMediaPlaylist * playlist, |
| 1823 |
GstM3U8MediaSegment * segment, gboolean * is_before, gboolean * matched_pdt) |
| 1824 |
{ |
| 1825 |
GstM3U8MediaSegment *res = NULL; |
| 1826 |
guint idx; |
| 1827 |
|
| 1828 |
*is_before = FALSE; |
| 1829 |
*matched_pdt = FALSE; |
| 1830 |
|
| 1831 |
/* The easy one. Happens when stream times need to be re-synced in an existing |
| 1832 |
* playlist */ |
| 1833 |
if (g_ptr_array_find (playlist->segments, segment, NULL)) { |
| 1834 |
GST_DEBUG ("Present as-is in playlist"); |
| 1835 |
return segment; |
| 1836 |
} |
| 1837 |
|
| 1838 |
/* If there is an identical segment with the same URI and SN, use that one */ |
| 1839 |
res = gst_hls_media_playlist_find_by_uri (playlist, segment); |
| 1840 |
if (res) { |
| 1841 |
GST_DEBUG ("Using same URI/DSN/SN match"); |
| 1842 |
return res; |
| 1843 |
} |
| 1844 |
|
| 1845 |
/* Try with PDT */ |
| 1846 |
if (segment->datetime && playlist->ext_x_pdt_present) { |
| 1847 |
#ifndef GST_DISABLE_GST_DEBUG |
| 1848 |
gchar *pdtstring = g_date_time_format_iso8601 (segment->datetime); |
| 1849 |
GST_DEBUG ("Search by datetime for %s", pdtstring); |
| 1850 |
g_free (pdtstring); |
| 1851 |
#endif |
| 1852 |
for (idx = 0; idx < playlist->segments->len; idx++) { |
| 1853 |
GstM3U8MediaSegment *cand = g_ptr_array_index (playlist->segments, idx); |
| 1854 |
|
| 1855 |
if (idx == 0 && cand->datetime) { |
| 1856 |
/* Special case for segments which are just before the 1st one (within |
| 1857 |
* 20ms). We add another reference because it now also belongs to the |
| 1858 |
* current playlist */ |
| 1859 |
GDateTime *seg_end = g_date_time_add (segment->datetime, |
| 1860 |
segment->duration / GST_USECOND); |
| 1861 |
GstClockTimeDiff ddiff = |
| 1862 |
g_date_time_difference (cand->datetime, seg_end) * GST_USECOND; |
| 1863 |
g_date_time_unref (seg_end); |
| 1864 |
if (ABS (ddiff) < 20 * GST_MSECOND) { |
| 1865 |
/* The reference segment ends within 20ms of the first segment, it is just before */ |
| 1866 |
GST_DEBUG ("Reference segment ends within %" GST_STIME_FORMAT |
| 1867 |
" of first playlist segment, inserting before", |
| 1868 |
GST_STIME_ARGS (ddiff)); |
| 1869 |
g_ptr_array_insert (playlist->segments, 0, |
| 1870 |
gst_m3u8_media_segment_ref (segment)); |
| 1871 |
*is_before = TRUE; |
| 1872 |
*matched_pdt = TRUE; |
| 1873 |
return segment; |
| 1874 |
} |
| 1875 |
if (ddiff > 0) { |
| 1876 |
/* If the reference segment is completely before the first segment, bail out */ |
| 1877 |
GST_DEBUG ("Reference segment ends before first segment"); |
| 1878 |
break; |
| 1879 |
} |
| 1880 |
} |
| 1881 |
|
| 1882 |
/* The reported PDT might not be 100% identical for matching segments |
| 1883 |
* across playlists, we therefore need to take into account a certain |
| 1884 |
* tolerance otherwise we would fail to match candidates with a PDT which |
| 1885 |
* is slightly before. We therefore check whether the segment starts |
| 1886 |
* within the first third of the candidate segment. |
| 1887 |
*/ |
| 1888 |
if (cand->datetime) { |
| 1889 |
GstClockTimeDiff pdtdiff = g_date_time_difference (cand->datetime, |
| 1890 |
segment->datetime) * GST_USECOND + cand->duration / 3; |
| 1891 |
if (pdtdiff >= 0) { |
| 1892 |
#ifndef GST_DISABLE_GST_DEBUG |
| 1893 |
gchar *pdtstring = g_date_time_format_iso8601 (cand->datetime); |
| 1894 |
GST_DEBUG ("Picking segment with datetime %s", pdtstring); |
| 1895 |
g_free (pdtstring); |
| 1896 |
#endif |
| 1897 |
*matched_pdt = TRUE; |
| 1898 |
return cand; |
| 1899 |
} |
| 1900 |
} |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
/* If not live, we can match by stream time */ |
| 1905 |
if (!GST_HLS_MEDIA_PLAYLIST_IS_LIVE (playlist)) { |
| 1906 |
GST_DEBUG ("Search by Stream time for %" GST_STIME_FORMAT " duration:%" |
| 1907 |
GST_TIME_FORMAT, GST_STIME_ARGS (segment->stream_time), |
| 1908 |
GST_TIME_ARGS (segment->duration)); |
| 1909 |
for (idx = 0; idx < playlist->segments->len; idx++) { |
| 1910 |
GstM3U8MediaSegment *cand = g_ptr_array_index (playlist->segments, idx); |
| 1911 |
|
| 1912 |
/* If the candidate starts at or after the previous stream time */ |
| 1913 |
if (cand->stream_time >= segment->stream_time) { |
| 1914 |
return cand; |
| 1915 |
} |
| 1916 |
|
| 1917 |
/* If the previous end stream time is before the candidate end stream time */ |
| 1918 |
if ((segment->stream_time + segment->duration) < |
| 1919 |
(cand->stream_time + cand->duration)) { |
| 1920 |
return cand; |
| 1921 |
} |
| 1922 |
} |
| 1923 |
} |
| 1924 |
|
| 1925 |
/* Fallback with MSN */ |
| 1926 |
GST_DEBUG ("Search by Media Sequence Number for sn:%" G_GINT64_FORMAT " dsn:%" |
| 1927 |
G_GINT64_FORMAT, segment->sequence, segment->discont_sequence); |
| 1928 |
for (idx = 0; idx < playlist->segments->len; idx++) { |
| 1929 |
GstM3U8MediaSegment *cand = g_ptr_array_index (playlist->segments, idx); |
| 1930 |
|
| 1931 |
/* Ignore non-matching DSN if needed */ |
| 1932 |
if ((segment->discont_sequence != cand->discont_sequence) |
| 1933 |
&& playlist->has_ext_x_dsn) |
| 1934 |
continue; |
| 1935 |
|
| 1936 |
if (idx == 0 && cand->sequence == segment->sequence + 1) { |
| 1937 |
/* Special case for segments just before the 1st one. We add another |
| 1938 |
* reference because it now also belongs to the current playlist */ |
| 1939 |
GST_DEBUG ("reference segment is just before 1st segment, inserting"); |
| 1940 |
g_ptr_array_insert (playlist->segments, 0, |
| 1941 |
gst_m3u8_media_segment_ref (segment)); |
| 1942 |
*is_before = TRUE; |
| 1943 |
return segment; |
| 1944 |
} |
| 1945 |
|
| 1946 |
if (cand->sequence == segment->sequence) { |
| 1947 |
return cand; |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
return NULL; |
| 1952 |
} |
| 1953 |
|
| 1954 |
/* Match up the first segment in a delta playlist against the reference and transfer |
| 1955 |
* over preceding segments if we can */ |
| 1956 |
gboolean |
| 1957 |
gst_hls_media_playlist_sync_skipped_segments (GstHLSMediaPlaylist * m3u8, |
| 1958 |
GstHLSMediaPlaylist * reference) |
| 1959 |
{ |
| 1960 |
/* Trivially there might be nothing to do (not a delta playlist) */ |
| 1961 |
if (m3u8->skipped_segments < 1 || m3u8->segments->len < 1) |
| 1962 |
return TRUE; |
| 1963 |
|
| 1964 |
/* find the first non-skipped segment from this playlist |
| 1965 |
* in the reference playlist, then transfer over as many |
| 1966 |
* skipped segments as we can */ |
| 1967 |
GstM3U8MediaSegment *first = g_ptr_array_index (m3u8->segments, 0); |
| 1968 |
|
| 1969 |
guint ref_idx; |
| 1970 |
gboolean found_ref_seg = FALSE; |
| 1971 |
for (ref_idx = 0; ref_idx < reference->segments->len; ref_idx++) { |
| 1972 |
GstM3U8MediaSegment *cand = |
| 1973 |
g_ptr_array_index (reference->segments, ref_idx); |
| 1974 |
|
| 1975 |
if (cand->sequence == first->sequence && |
| 1976 |
cand->discont_sequence == first->discont_sequence && |
| 1977 |
cand->offset == first->offset && cand->size == first->size && |
| 1978 |
!g_strcmp0 (cand->uri, first->uri)) { |
| 1979 |
found_ref_seg = TRUE; |
| 1980 |
break; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
if (!found_ref_seg) |
| 1985 |
return FALSE; /* Couldn't match the segment */ |
| 1986 |
|
| 1987 |
/* Found the first segment of this playlist in the reference. Transfer over |
| 1988 |
* as many skipped segments as we can */ |
| 1989 |
guint segs_avail = MIN (ref_idx, m3u8->skipped_segments); |
| 1990 |
if (segs_avail < 1) |
| 1991 |
return FALSE; |
| 1992 |
|
| 1993 |
GST_DEBUG |
| 1994 |
("Transferring %u skipped segments from reference playlist starting at index %u", |
| 1995 |
segs_avail, ref_idx - segs_avail); |
| 1996 |
|
| 1997 |
/* Reduce the skipped_segments count by the number we will transfer */ |
| 1998 |
m3u8->skipped_segments -= segs_avail; |
| 1999 |
|
| 2000 |
/* And copy over the segments */ |
| 2001 |
guint cur_idx; |
| 2002 |
for (cur_idx = ref_idx - 1; segs_avail > 0; segs_avail--, cur_idx--) { |
| 2003 |
GstM3U8MediaSegment *segment = |
| 2004 |
g_ptr_array_index (reference->segments, cur_idx); |
| 2005 |
g_ptr_array_insert (m3u8->segments, 0, |
| 2006 |
gst_m3u8_media_segment_ref (segment)); |
| 2007 |
} |
| 2008 |
|
| 2009 |
return TRUE; |
| 2010 |
} |
| 2011 |
|
| 2012 |
/* Given a media segment (potentially from another media playlist), find the |
| 2013 |
* equivalent media segment in this playlist. |
| 2014 |
* |
| 2015 |
* This will also recalculate all stream times based on that segment stream |
| 2016 |
* time (i.e. "sync" the playlist to that previous time). |
| 2017 |
* |
| 2018 |
* If an equivalent/identical one is found it is returned with |
| 2019 |
* the reference count incremented |
| 2020 |
*/ |
| 2021 |
GstM3U8MediaSegment * |
| 2022 |
gst_hls_media_playlist_sync_to_segment (GstHLSMediaPlaylist * playlist, |
| 2023 |
GstM3U8MediaSegment * segment) |
| 2024 |
{ |
| 2025 |
GstM3U8MediaSegment *res = NULL; |
| 2026 |
gboolean is_before; |
| 2027 |
#ifndef GST_DISABLE_GST_DEBUG |
| 2028 |
gchar *pdtstring; |
| 2029 |
#endif |
| 2030 |
|
| 2031 |
g_return_val_if_fail (playlist, NULL); |
| 2032 |
g_return_val_if_fail (segment, NULL); |
| 2033 |
|
| 2034 |
GST_DEBUG ("Re-syncing to segment %" GST_STIME_FORMAT " duration:%" |
| 2035 |
GST_TIME_FORMAT " sn:%" G_GINT64_FORMAT "/dsn:%" G_GINT64_FORMAT |
| 2036 |
" uri:%s in playlist %s", GST_STIME_ARGS (segment->stream_time), |
| 2037 |
GST_TIME_ARGS (segment->duration), segment->sequence, |
| 2038 |
segment->discont_sequence, segment->uri, playlist->uri); |
| 2039 |
|
| 2040 |
gboolean matched_pdt = FALSE; |
| 2041 |
res = find_segment_in_playlist (playlist, segment, &is_before, &matched_pdt); |
| 2042 |
|
| 2043 |
/* For live playlists we re-calculate all stream times based on the existing |
| 2044 |
* stream time. Non-live playlists have their stream time calculated at |
| 2045 |
* parsing time. */ |
| 2046 |
if (res) { |
| 2047 |
if (!is_before) |
| 2048 |
gst_m3u8_media_segment_ref (res); |
| 2049 |
if (res->stream_time == GST_CLOCK_STIME_NONE) { |
| 2050 |
GstClockTimeDiff stream_time_offset = 0; |
| 2051 |
/* If there is a PDT on both segments, adjust the stream time |
| 2052 |
* by the difference to align them precisely (hopefully). |
| 2053 |
*/ |
| 2054 |
if (matched_pdt) { |
| 2055 |
/* If matched_pdt is TRUE, there must be PDT present in both segments */ |
| 2056 |
g_assert (res->datetime); |
| 2057 |
g_assert (segment->datetime); |
| 2058 |
|
| 2059 |
stream_time_offset = |
| 2060 |
g_date_time_difference (res->datetime, |
| 2061 |
segment->datetime) * GST_USECOND; |
| 2062 |
|
| 2063 |
GST_DEBUG ("Transferring stream time %" GST_STIMEP_FORMAT |
| 2064 |
" adjusted by PDT offset %" GST_STIMEP_FORMAT, |
| 2065 |
&segment->stream_time, &stream_time_offset); |
| 2066 |
} |
| 2067 |
res->stream_time = segment->stream_time + stream_time_offset; |
| 2068 |
gst_m3u8_media_segment_fill_partial_stream_times (res); |
| 2069 |
} |
| 2070 |
if (GST_HLS_MEDIA_PLAYLIST_IS_LIVE (playlist)) |
| 2071 |
gst_hls_media_playlist_recalculate_stream_time (playlist, res); |
| 2072 |
/* If the playlist didn't specify a reference discont sequence number, we |
| 2073 |
* carry over the one from the reference segment */ |
| 2074 |
if (!playlist->has_ext_x_dsn |
| 2075 |
&& res->discont_sequence != segment->discont_sequence) { |
| 2076 |
res->discont_sequence = segment->discont_sequence; |
| 2077 |
gst_hls_media_playlist_recalculate_dsn (playlist, res); |
| 2078 |
} |
| 2079 |
if (is_before) { |
| 2080 |
GST_DEBUG ("Dropping segment from before the playlist"); |
| 2081 |
g_ptr_array_remove_index (playlist->segments, 0); |
| 2082 |
res = NULL; |
| 2083 |
} |
| 2084 |
} |
| 2085 |
#ifndef GST_DISABLE_GST_DEBUG |
| 2086 |
if (res) { |
| 2087 |
pdtstring = |
| 2088 |
res->datetime ? g_date_time_format_iso8601 (res->datetime) : NULL; |
| 2089 |
GST_DEBUG ("Returning segment sn:%" G_GINT64_FORMAT " dsn:%" G_GINT64_FORMAT |
| 2090 |
" stream_time:%" GST_STIME_FORMAT " duration:%" GST_TIME_FORMAT |
| 2091 |
" datetime:%s", res->sequence, res->discont_sequence, |
| 2092 |
GST_STIME_ARGS (res->stream_time), GST_TIME_ARGS (res->duration), |
| 2093 |
pdtstring); |
| 2094 |
g_free (pdtstring); |
| 2095 |
} else { |
| 2096 |
GST_DEBUG ("Could not find a match"); |
| 2097 |
} |
| 2098 |
#endif |
| 2099 |
|
| 2100 |
return res; |
| 2101 |
} |
| 2102 |
|
| 2103 |
gboolean |
| 2104 |
gst_hls_media_playlist_get_starting_segment (GstHLSMediaPlaylist * self, |
| 2105 |
GstM3U8SeekResult * seek_result) |
| 2106 |
{ |
| 2107 |
GstM3U8MediaSegment *res = NULL; |
| 2108 |
|
| 2109 |
GST_DEBUG ("playlist %s", self->uri); |
| 2110 |
|
| 2111 |
if (!GST_HLS_MEDIA_PLAYLIST_IS_LIVE (self)) { |
| 2112 |
/* For non-live, we just grab the first one */ |
| 2113 |
res = g_ptr_array_index (self->segments, 0); |
| 2114 |
} else { |
| 2115 |
GstClockTime hold_back = GST_CLOCK_TIME_NONE; |
| 2116 |
GstM3U8MediaSegment *last_seg; |
| 2117 |
g_assert (self->segments->len); |
| 2118 |
last_seg = g_ptr_array_index (self->segments, self->segments->len - 1); |
| 2119 |
|
| 2120 |
/* Live playlist. If low-latency, use the PART-HOLD-BACK specified distance |
| 2121 |
* from the end, otherwise HOLD-BACK distance */ |
| 2122 |
if (GST_CLOCK_TIME_IS_VALID (self->part_hold_back)) |
| 2123 |
hold_back = self->part_hold_back; |
| 2124 |
else if (GST_CLOCK_TIME_IS_VALID (self->partial_targetduration)) |
| 2125 |
hold_back = 3 * self->partial_targetduration; |
| 2126 |
else if (GST_CLOCK_TIME_IS_VALID (self->hold_back)) |
| 2127 |
hold_back = self->hold_back; |
| 2128 |
|
| 2129 |
if (hold_back == GST_CLOCK_TIME_NONE) { |
| 2130 |
/* If low-latency is not enabled, or none of the above were present, |
| 2131 |
* fallback to the standard behaviour: |
| 2132 |
* |
| 2133 |
* RFC 8216 6.3.3. Playing the Media Playlist File: |
| 2134 |
* |
| 2135 |
* The client SHALL choose which Media Segment to play first from the |
| 2136 |
* Media Playlist when playback starts. If the EXT-X-ENDLIST tag is not |
| 2137 |
* present and the client intends to play the media normally, the client |
| 2138 |
* SHOULD NOT choose a segment that starts less than three target |
| 2139 |
* durations from the end of the Playlist file. Doing so can trigger |
| 2140 |
* playback stalls. |
| 2141 |
*/ |
| 2142 |
hold_back = GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE * self->targetduration; |
| 2143 |
} |
| 2144 |
|
| 2145 |
if (GST_CLOCK_TIME_IS_VALID (hold_back) |
| 2146 |
&& GST_CLOCK_STIME_IS_VALID (last_seg->stream_time)) { |
| 2147 |
GstSeekFlags flags = |
| 2148 |
GST_SEEK_FLAG_SNAP_BEFORE | GST_SEEK_FLAG_KEY_UNIT | |
| 2149 |
GST_HLS_M3U8_SEEK_FLAG_ALLOW_PARTIAL; |
| 2150 |
GstClockTime playlist_duration = |
| 2151 |
last_seg->stream_time + last_seg->duration; |
| 2152 |
GstClockTime target_ts; |
| 2153 |
|
| 2154 |
/* Clamp the hold back so we don't go below zero */ |
| 2155 |
if (hold_back > playlist_duration) |
| 2156 |
hold_back = playlist_duration; |
| 2157 |
|
| 2158 |
target_ts = playlist_duration - hold_back; |
| 2159 |
|
| 2160 |
GST_DEBUG ("Hold back is %" GST_TIME_FORMAT |
| 2161 |
" Looking for a segment before %" GST_TIME_FORMAT, |
| 2162 |
GST_TIME_ARGS (hold_back), GST_TIME_ARGS (target_ts)); |
| 2163 |
|
| 2164 |
if (gst_hls_media_playlist_seek (self, TRUE, flags, target_ts, |
| 2165 |
seek_result)) { |
| 2166 |
#ifndef GST_DISABLE_GST_DEBUG |
| 2167 |
GstClockTime distance_from_edge = |
| 2168 |
playlist_duration - seek_result->stream_time; |
| 2169 |
|
| 2170 |
GST_DEBUG ("Found starting position %" GST_TIME_FORMAT " which is %" |
| 2171 |
GST_TIME_FORMAT " from the live edge", |
| 2172 |
GST_TIME_ARGS (seek_result->stream_time), |
| 2173 |
GST_TIME_ARGS (distance_from_edge)); |
| 2174 |
#endif |
| 2175 |
return TRUE; |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
/* Worst case fallback, start 3 fragments from the end */ |
| 2180 |
if (res == NULL) { |
| 2181 |
res = |
| 2182 |
g_ptr_array_index (self->segments, |
| 2183 |
MAX ((gint) self->segments->len - |
| 2184 |
GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE - 1, 0)); |
| 2185 |
} |
| 2186 |
} |
| 2187 |
|
| 2188 |
if (res) { |
| 2189 |
GST_DEBUG ("Using segment sn:%" G_GINT64_FORMAT " dsn:%" G_GINT64_FORMAT, |
| 2190 |
res->sequence, res->discont_sequence); |
| 2191 |
|
| 2192 |
seek_result->stream_time = res->stream_time; |
| 2193 |
seek_result->segment = gst_m3u8_media_segment_ref (res); |
| 2194 |
seek_result->found_partial_segment = FALSE; |
| 2195 |
seek_result->part_idx = 0; |
| 2196 |
return TRUE; |
| 2197 |
} |
| 2198 |
|
| 2199 |
return FALSE; |
| 2200 |
} |
| 2201 |
|
| 2202 |
/* Calls this to carry over stream time, DSN, ... from one playlist to another. |
| 2203 |
* |
| 2204 |
* This should be used when a reference media segment couldn't be matched in the |
| 2205 |
* playlist, but we still want to carry over the information from a reference |
| 2206 |
* playlist to an updated one. This can happen with live playlists where the |
| 2207 |
* reference media segment is no longer present but the playlists intersect |
| 2208 |
* |
| 2209 |
* If the sync is sucessfull, discont will be set to TRUE if it was a perfect |
| 2210 |
* URI fragment match, else it will be FALSE (ex: match was done on PDT or |
| 2211 |
* SN/DSN). |
| 2212 |
**/ |
| 2213 |
gboolean |
| 2214 |
gst_hls_media_playlist_sync_to_playlist (GstHLSMediaPlaylist * playlist, |
| 2215 |
GstHLSMediaPlaylist * reference, gboolean * discont) |
| 2216 |
{ |
| 2217 |
GstM3U8MediaSegment *res = NULL; |
| 2218 |
GstM3U8MediaSegment *cand = NULL; |
| 2219 |
guint idx; |
| 2220 |
gboolean is_before; |
| 2221 |
gboolean matched_pdt = FALSE; |
| 2222 |
|
| 2223 |
if (discont) |
| 2224 |
*discont = FALSE; |
| 2225 |
|
| 2226 |
g_return_val_if_fail (playlist && reference, FALSE); |
| 2227 |
|
| 2228 |
retry_without_dsn: |
| 2229 |
/* The new playlist is supposed to be an update of the reference playlist, |
| 2230 |
* or a more recently fetched playlist from another rendition. In either case, |
| 2231 |
* it's best to start from the last segment of the (older) reference playlist and |
| 2232 |
* go backwards to find an overlap */ |
| 2233 |
for (idx = reference->segments->len; idx; idx--) { |
| 2234 |
cand = g_ptr_array_index (reference->segments, idx - 1); |
| 2235 |
res = find_segment_in_playlist (playlist, cand, &is_before, &matched_pdt); |
| 2236 |
if (res) |
| 2237 |
break; |
| 2238 |
} |
| 2239 |
|
| 2240 |
if (res == NULL) { |
| 2241 |
if (playlist->has_ext_x_dsn) { |
| 2242 |
/* There is a possibility that the server doesn't have coherent DSN |
| 2243 |
* across variants/renditions. If we reach this section, this means that |
| 2244 |
* we have already attempted matching by PDT, URI, stream time. The last |
| 2245 |
* matching would have been by MSN/DSN, therefore try it again without |
| 2246 |
* taking DSN into account. */ |
| 2247 |
GST_DEBUG ("Retrying matching without taking DSN into account"); |
| 2248 |
playlist->has_ext_x_dsn = FALSE; |
| 2249 |
goto retry_without_dsn; |
| 2250 |
} |
| 2251 |
GST_WARNING ("Could not synchronize media playlists"); |
| 2252 |
return FALSE; |
| 2253 |
} |
| 2254 |
|
| 2255 |
if (discont) { |
| 2256 |
/* If not a perfect match, mark as such */ |
| 2257 |
GST_DEBUG ("Checking match uri cand: %s", cand->uri); |
| 2258 |
GST_DEBUG ("Checking match uri res : %s", res->uri); |
| 2259 |
*discont = g_strcmp0 (res->uri, cand->uri) != 0; |
| 2260 |
} |
| 2261 |
|
| 2262 |
/* Carry over reference stream time */ |
| 2263 |
if (res->stream_time == GST_CLOCK_STIME_NONE) { |
| 2264 |
GstClockTimeDiff stream_time_offset = 0; |
| 2265 |
/* If there is a PDT on both segments, adjust the stream time |
| 2266 |
* by the difference to align them precisely (hopefully). |
| 2267 |
*/ |
| 2268 |
if (matched_pdt) { |
| 2269 |
/* If matched_pdt is TRUE, there must be PDT present in both segments */ |
| 2270 |
g_assert (playlist->ext_x_pdt_present && res->datetime); |
| 2271 |
g_assert (reference->ext_x_pdt_present && cand->datetime); |
| 2272 |
|
| 2273 |
stream_time_offset = |
| 2274 |
g_date_time_difference (res->datetime, cand->datetime) * GST_USECOND; |
| 2275 |
GST_DEBUG ("Transferring stream time %" GST_STIMEP_FORMAT |
| 2276 |
" adjusted by PDT offset %" GST_STIMEP_FORMAT, &cand->stream_time, |
| 2277 |
&stream_time_offset); |
| 2278 |
|
| 2279 |
} |
| 2280 |
res->stream_time = cand->stream_time + stream_time_offset; |
| 2281 |
gst_m3u8_media_segment_fill_partial_stream_times (res); |
| 2282 |
} |
| 2283 |
|
| 2284 |
if (GST_HLS_MEDIA_PLAYLIST_IS_LIVE (playlist)) |
| 2285 |
gst_hls_media_playlist_recalculate_stream_time (playlist, res); |
| 2286 |
/* If the playlist didn't specify a reference discont sequence number, we |
| 2287 |
* carry over the one from the reference segment */ |
| 2288 |
if (!playlist->has_ext_x_dsn |
| 2289 |
&& res->discont_sequence != cand->discont_sequence) { |
| 2290 |
res->discont_sequence = cand->discont_sequence; |
| 2291 |
gst_hls_media_playlist_recalculate_dsn (playlist, res); |
| 2292 |
} |
| 2293 |
if (is_before) { |
| 2294 |
g_ptr_array_remove_index (playlist->segments, 0); |
| 2295 |
} |
| 2296 |
|
| 2297 |
return TRUE; |
| 2298 |
} |
| 2299 |
|
| 2300 |
gboolean |
| 2301 |
gst_hls_media_playlist_has_next_fragment (GstHLSMediaPlaylist * m3u8, |
| 2302 |
GstM3U8MediaSegment * current, gboolean forward) |
| 2303 |
{ |
| 2304 |
guint idx; |
| 2305 |
gboolean have_next = TRUE; |
| 2306 |
|
| 2307 |
g_return_val_if_fail (m3u8 != NULL, FALSE); |
| 2308 |
g_return_val_if_fail (current != NULL, FALSE); |
| 2309 |
|
| 2310 |
GST_DEBUG ("playlist %s", m3u8->uri); |
| 2311 |
|
| 2312 |
GST_HLS_MEDIA_PLAYLIST_LOCK (m3u8); |
| 2313 |
|
| 2314 |
if (!g_ptr_array_find (m3u8->segments, current, &idx)) |
| 2315 |
have_next = FALSE; |
| 2316 |
else if (idx == 0 && !forward) |
| 2317 |
have_next = FALSE; |
| 2318 |
else if (forward && idx == (m3u8->segments->len - 1)) |
| 2319 |
have_next = FALSE; |
| 2320 |
|
| 2321 |
GST_HLS_MEDIA_PLAYLIST_UNLOCK (m3u8); |
| 2322 |
|
| 2323 |
GST_DEBUG ("Returning %d", have_next); |
| 2324 |
|
| 2325 |
return have_next; |
| 2326 |
} |
| 2327 |
|
| 2328 |
|
| 2329 |
GstM3U8MediaSegment * |
| 2330 |
gst_hls_media_playlist_advance_fragment (GstHLSMediaPlaylist * m3u8, |
| 2331 |
GstM3U8MediaSegment * current, gboolean forward) |
| 2332 |
{ |
| 2333 |
GstM3U8MediaSegment *file = NULL; |
| 2334 |
guint idx; |
| 2335 |
|
| 2336 |
g_return_val_if_fail (m3u8 != NULL, NULL); |
| 2337 |
g_return_val_if_fail (current != NULL, NULL); |
| 2338 |
|
| 2339 |
GST_HLS_MEDIA_PLAYLIST_LOCK (m3u8); |
| 2340 |
|
| 2341 |
GST_DEBUG ("playlist %s", m3u8->uri); |
| 2342 |
|
| 2343 |
if (m3u8->segments->len < 2) { |
| 2344 |
GST_DEBUG ("Playlist only contains one fragment, can't advance"); |
| 2345 |
goto out; |
| 2346 |
} |
| 2347 |
|
| 2348 |
if (!g_ptr_array_find (m3u8->segments, current, &idx)) { |
| 2349 |
GST_ERROR ("Requested to advance froma fragment not present in playlist"); |
| 2350 |
goto out; |
| 2351 |
} |
| 2352 |
|
| 2353 |
if (forward && idx < (m3u8->segments->len - 1)) { |
| 2354 |
file = |
| 2355 |
gst_m3u8_media_segment_ref (g_ptr_array_index (m3u8->segments, |
| 2356 |
idx + 1)); |
| 2357 |
} else if (!forward && idx > 0) { |
| 2358 |
file = |
| 2359 |
gst_m3u8_media_segment_ref (g_ptr_array_index (m3u8->segments, |
| 2360 |
idx - 1)); |
| 2361 |
} |
| 2362 |
|
| 2363 |
if (file) |
| 2364 |
GST_DEBUG ("Advanced to segment sn:%" G_GINT64_FORMAT " dsn:%" |
| 2365 |
G_GINT64_FORMAT, file->sequence, file->discont_sequence); |
| 2366 |
else |
| 2367 |
GST_DEBUG ("Could not find %s fragment", forward ? "next" : "previous"); |
| 2368 |
|
| 2369 |
out: |
| 2370 |
GST_HLS_MEDIA_PLAYLIST_UNLOCK (m3u8); |
| 2371 |
|
| 2372 |
return file; |
| 2373 |
} |
| 2374 |
|
| 2375 |
GstClockTime |
| 2376 |
gst_hls_media_playlist_get_end_stream_time (GstHLSMediaPlaylist * m3u8) |
| 2377 |
{ |
| 2378 |
if (m3u8->segments->len == 0) |
| 2379 |
return GST_CLOCK_TIME_NONE; |
| 2380 |
|
| 2381 |
GstM3U8MediaSegment *last = |
| 2382 |
g_ptr_array_index (m3u8->segments, m3u8->segments->len - 1); |
| 2383 |
|
| 2384 |
return last->stream_time + last->duration; |
| 2385 |
} |
| 2386 |
|
| 2387 |
GstClockTime |
| 2388 |
gst_hls_media_playlist_get_duration (GstHLSMediaPlaylist * m3u8) |
| 2389 |
{ |
| 2390 |
GstClockTime duration = GST_CLOCK_TIME_NONE; |
| 2391 |
|
| 2392 |
g_return_val_if_fail (m3u8 != NULL, GST_CLOCK_TIME_NONE); |
| 2393 |
|
| 2394 |
GST_DEBUG ("playlist %s", m3u8->uri); |
| 2395 |
|
| 2396 |
GST_HLS_MEDIA_PLAYLIST_LOCK (m3u8); |
| 2397 |
/* We can only get the duration for on-demand streams */ |
| 2398 |
if (m3u8->endlist) { |
| 2399 |
if (m3u8->segments->len) { |
| 2400 |
GstM3U8MediaSegment *first = g_ptr_array_index (m3u8->segments, 0); |
| 2401 |
GstM3U8MediaSegment *last = |
| 2402 |
g_ptr_array_index (m3u8->segments, m3u8->segments->len - 1); |
| 2403 |
duration = last->stream_time + last->duration - first->stream_time; |
| 2404 |
if (duration != m3u8->duration) |
| 2405 |
GST_ERROR ("difference in calculated duration ? %" GST_TIME_FORMAT |
| 2406 |
" vs %" GST_TIME_FORMAT, GST_TIME_ARGS (duration), |
| 2407 |
GST_TIME_ARGS (m3u8->duration)); |
| 2408 |
} |
| 2409 |
duration = m3u8->duration; |
| 2410 |
} |
| 2411 |
GST_HLS_MEDIA_PLAYLIST_UNLOCK (m3u8); |
| 2412 |
|
| 2413 |
GST_DEBUG ("duration %" GST_TIME_FORMAT, GST_TIME_ARGS (duration)); |
| 2414 |
|
| 2415 |
return duration; |
| 2416 |
} |
| 2417 |
|
| 2418 |
void |
| 2419 |
gst_hls_media_playlist_get_next_msn_and_part (GstHLSMediaPlaylist * m3u8, |
| 2420 |
gint64 * next_msn, gint64 * next_part) |
| 2421 |
{ |
| 2422 |
/* Return the MSN and part number that are 1 past the end of the current playlist */ |
| 2423 |
if (m3u8->segments->len == 0) { |
| 2424 |
*next_msn = -1; |
| 2425 |
*next_part = -1; |
| 2426 |
return; |
| 2427 |
} |
| 2428 |
|
| 2429 |
GstM3U8MediaSegment *last = |
| 2430 |
g_ptr_array_index (m3u8->segments, m3u8->segments->len - 1); |
| 2431 |
|
| 2432 |
/* If low_latency mode and the last segment contains partial segments, the next playlist update is |
| 2433 |
* when one extra partial segment gets added */ |
| 2434 |
if (last->partial_segments != NULL) { |
| 2435 |
*next_msn = last->sequence; |
| 2436 |
*next_part = last->partial_segments->len; |
| 2437 |
return; |
| 2438 |
} |
| 2439 |
|
| 2440 |
*next_msn = last->sequence + 1; |
| 2441 |
*next_part = -1; |
| 2442 |
} |
| 2443 |
|
| 2444 |
gchar * |
| 2445 |
gst_hls_media_playlist_get_uri (GstHLSMediaPlaylist * m3u8) |
| 2446 |
{ |
| 2447 |
gchar *uri; |
| 2448 |
|
| 2449 |
GST_HLS_MEDIA_PLAYLIST_LOCK (m3u8); |
| 2450 |
uri = g_strdup (m3u8->uri); |
| 2451 |
GST_HLS_MEDIA_PLAYLIST_UNLOCK (m3u8); |
| 2452 |
|
| 2453 |
return uri; |
| 2454 |
} |
| 2455 |
|
| 2456 |
gboolean |
| 2457 |
gst_hls_media_playlist_is_live (GstHLSMediaPlaylist * m3u8) |
| 2458 |
{ |
| 2459 |
gboolean is_live; |
| 2460 |
|
| 2461 |
g_return_val_if_fail (m3u8 != NULL, FALSE); |
| 2462 |
|
| 2463 |
GST_HLS_MEDIA_PLAYLIST_LOCK (m3u8); |
| 2464 |
is_live = GST_HLS_MEDIA_PLAYLIST_IS_LIVE (m3u8); |
| 2465 |
GST_HLS_MEDIA_PLAYLIST_UNLOCK (m3u8); |
| 2466 |
|
| 2467 |
return is_live; |
| 2468 |
} |
| 2469 |
|
| 2470 |
static gchar * |
| 2471 |
uri_join (const gchar * uri1, const gchar * uri2) |
| 2472 |
{ |
| 2473 |
gchar *uri_copy, *tmp, *ret = NULL; |
| 2474 |
|
| 2475 |
if (gst_uri_is_valid (uri2)) |
| 2476 |
return g_strdup (uri2); |
| 2477 |
|
| 2478 |
uri_copy = g_strdup (uri1); |
| 2479 |
if (uri2[0] != '/') { |
| 2480 |
/* uri2 is a relative uri2 */ |
| 2481 |
/* look for query params */ |
| 2482 |
tmp = g_utf8_strchr (uri_copy, -1, '?'); |
| 2483 |
if (tmp) { |
| 2484 |
/* find last / char, ignoring query params */ |
| 2485 |
tmp = g_utf8_strrchr (uri_copy, tmp - uri_copy, '/'); |
| 2486 |
} else { |
| 2487 |
/* find last / char in URL */ |
| 2488 |
tmp = g_utf8_strrchr (uri_copy, -1, '/'); |
| 2489 |
} |
| 2490 |
if (!tmp) |
| 2491 |
goto out; |
| 2492 |
|
| 2493 |
|
| 2494 |
*tmp = '\0'; |
| 2495 |
ret = g_strdup_printf ("%s/%s", uri_copy, uri2); |
| 2496 |
} else { |
| 2497 |
/* uri2 is an absolute uri2 */ |
| 2498 |
char *scheme, *hostname; |
| 2499 |
|
| 2500 |
scheme = uri_copy; |
| 2501 |
/* find the : in <scheme>:// */ |
| 2502 |
tmp = g_utf8_strchr (uri_copy, -1, ':'); |
| 2503 |
if (!tmp) |
| 2504 |
goto out; |
| 2505 |
|
| 2506 |
*tmp = '\0'; |
| 2507 |
|
| 2508 |
/* skip :// */ |
| 2509 |
hostname = tmp + 3; |
| 2510 |
|
| 2511 |
tmp = g_utf8_strchr (hostname, -1, '/'); |
| 2512 |
if (tmp) |
| 2513 |
*tmp = '\0'; |
| 2514 |
|
| 2515 |
ret = g_strdup_printf ("%s://%s%s", scheme, hostname, uri2); |
| 2516 |
} |
| 2517 |
|
| 2518 |
out: |
| 2519 |
g_free (uri_copy); |
| 2520 |
if (!ret) |
| 2521 |
GST_WARNING ("Can't build a valid uri from '%s' '%s'", uri1, uri2); |
| 2522 |
|
| 2523 |
return ret; |
| 2524 |
} |
| 2525 |
|
| 2526 |
gboolean |
| 2527 |
gst_hls_media_playlist_has_lost_sync (GstHLSMediaPlaylist * m3u8, |
| 2528 |
GstClockTime position) |
| 2529 |
{ |
| 2530 |
GstM3U8MediaSegment *first; |
| 2531 |
|
| 2532 |
if (m3u8->segments->len < 1) |
| 2533 |
return TRUE; |
| 2534 |
first = g_ptr_array_index (m3u8->segments, 0); |
| 2535 |
|
| 2536 |
GST_DEBUG ("position %" GST_TIME_FORMAT " first %" GST_STIME_FORMAT |
| 2537 |
" duration %" GST_TIME_FORMAT, GST_TIME_ARGS (position), |
| 2538 |
GST_STIME_ARGS (first->stream_time), GST_TIME_ARGS (first->duration)); |
| 2539 |
|
| 2540 |
if (first->stream_time <= 0) |
| 2541 |
return FALSE; |
| 2542 |
|
| 2543 |
/* If we're definitely before the first fragment, we lost sync */ |
| 2544 |
if ((position + (first->duration / 2)) < first->stream_time) |
| 2545 |
return TRUE; |
| 2546 |
return FALSE; |
| 2547 |
} |
| 2548 |
|
| 2549 |
gboolean |
| 2550 |
gst_hls_media_playlist_get_seek_range (GstHLSMediaPlaylist * m3u8, |
| 2551 |
gint64 * start, gint64 * stop) |
| 2552 |
{ |
| 2553 |
GstM3U8MediaSegment *first, *last; |
| 2554 |
|
| 2555 |
g_return_val_if_fail (m3u8 != NULL, FALSE); |
| 2556 |
|
| 2557 |
if (m3u8->segments->len < 1) |
| 2558 |
return FALSE; |
| 2559 |
|
| 2560 |
first = g_ptr_array_index (m3u8->segments, 0); |
| 2561 |
*start = first->stream_time; |
| 2562 |
|
| 2563 |
/* Default is the end of the playlist */ |
| 2564 |
last = g_ptr_array_index (m3u8->segments, m3u8->segments->len - 1); |
| 2565 |
*stop = last->stream_time + last->duration; |
| 2566 |
|
| 2567 |
/* For live playlists, take the minimum hold back into account |
| 2568 |
* for the end of the seek range */ |
| 2569 |
if (GST_HLS_MEDIA_PLAYLIST_IS_LIVE (m3u8)) { |
| 2570 |
GstM3U8SeekResult seek_result; |
| 2571 |
|
| 2572 |
if (gst_hls_media_playlist_get_starting_segment (m3u8, &seek_result)) { |
| 2573 |
if (seek_result.found_partial_segment) { |
| 2574 |
GstM3U8PartialSegment *part = |
| 2575 |
g_ptr_array_index (seek_result.segment->partial_segments, |
| 2576 |
seek_result.part_idx); |
| 2577 |
*stop = part->stream_time + part->duration; |
| 2578 |
} else { |
| 2579 |
*stop = |
| 2580 |
seek_result.segment->stream_time + seek_result.segment->duration; |
| 2581 |
} |
| 2582 |
|
| 2583 |
gst_m3u8_media_segment_unref (seek_result.segment); |
| 2584 |
} |
| 2585 |
} |
| 2586 |
|
| 2587 |
return TRUE; |
| 2588 |
} |
| 2589 |
|
| 2590 |
GstClockTime |
| 2591 |
gst_hls_media_playlist_recommended_buffering_threshold (GstHLSMediaPlaylist * |
| 2592 |
playlist) |
| 2593 |
{ |
| 2594 |
if (!playlist->duration || !GST_CLOCK_TIME_IS_VALID (playlist->duration) |
| 2595 |
|| playlist->segments->len == 0) |
| 2596 |
return GST_CLOCK_TIME_NONE; |
| 2597 |
|
| 2598 |
/* The recommended buffering threshold is 1.5 average segment duration */ |
| 2599 |
GstClockTime threshold = |
| 2600 |
3 * (playlist->duration / playlist->segments->len) / 2; |
| 2601 |
|
| 2602 |
if (GST_HLS_MEDIA_PLAYLIST_IS_LIVE (playlist)) { |
| 2603 |
/* For live playlists, reduce the recommended buffering threshold |
| 2604 |
* to match the starting hold back distance if needed, otherwise |
| 2605 |
* we'll hit the live edge and have to wait before we hit 100% */ |
| 2606 |
if (GST_CLOCK_TIME_IS_VALID (playlist->hold_back) |
| 2607 |
&& threshold > playlist->hold_back) |
| 2608 |
threshold = playlist->hold_back; |
| 2609 |
else if (GST_CLOCK_TIME_IS_VALID (playlist->targetduration) |
| 2610 |
&& threshold > 3 * playlist->targetduration) |
| 2611 |
threshold = 3 * playlist->targetduration; |
| 2612 |
|
| 2613 |
if (GST_CLOCK_TIME_IS_VALID (playlist->part_hold_back) |
| 2614 |
&& threshold > playlist->part_hold_back) |
| 2615 |
threshold = playlist->part_hold_back; |
| 2616 |
else if (GST_CLOCK_TIME_IS_VALID (playlist->partial_targetduration) |
| 2617 |
&& threshold > 3 * playlist->partial_targetduration) |
| 2618 |
threshold = 3 * playlist->partial_targetduration; |
| 2619 |
} |
| 2620 |
|
| 2621 |
return threshold; |
| 2622 |
} |
| 2623 |
|
| 2624 |
GstHLSRenditionStream * |
| 2625 |
gst_hls_rendition_stream_ref (GstHLSRenditionStream * media) |
| 2626 |
{ |
| 2627 |
g_assert (media != NULL && media->ref_count > 0); |
| 2628 |
g_atomic_int_add (&media->ref_count, 1); |
| 2629 |
return media; |
| 2630 |
} |
| 2631 |
|
| 2632 |
void |
| 2633 |
gst_hls_rendition_stream_unref (GstHLSRenditionStream * media) |
| 2634 |
{ |
| 2635 |
g_assert (media != NULL && media->ref_count > 0); |
| 2636 |
if (g_atomic_int_dec_and_test (&media->ref_count)) { |
| 2637 |
if (media->caps) |
| 2638 |
gst_caps_unref (media->caps); |
| 2639 |
g_free (media->group_id); |
| 2640 |
g_free (media->name); |
| 2641 |
g_free (media->uri); |
| 2642 |
g_free (media->lang); |
| 2643 |
g_free (media); |
| 2644 |
} |
| 2645 |
} |
| 2646 |
|
| 2647 |
static GstHLSRenditionStreamType |
| 2648 |
gst_m3u8_get_hls_media_type_from_string (const gchar * type_name) |
| 2649 |
{ |
| 2650 |
if (strcmp (type_name, "AUDIO") == 0) |
| 2651 |
return GST_HLS_RENDITION_STREAM_TYPE_AUDIO; |
| 2652 |
if (strcmp (type_name, "VIDEO") == 0) |
| 2653 |
return GST_HLS_RENDITION_STREAM_TYPE_VIDEO; |
| 2654 |
if (strcmp (type_name, "SUBTITLES") == 0) |
| 2655 |
return GST_HLS_RENDITION_STREAM_TYPE_SUBTITLES; |
| 2656 |
if (strcmp (type_name, "CLOSED-CAPTIONS") == 0) |
| 2657 |
return GST_HLS_RENDITION_STREAM_TYPE_CLOSED_CAPTIONS; |
| 2658 |
|
| 2659 |
return GST_HLS_RENDITION_STREAM_TYPE_INVALID; |
| 2660 |
} |
| 2661 |
|
| 2662 |
#define GST_HLS_RENDITION_STREAM_TYPE_NAME(mtype) gst_hls_rendition_stream_type_get_name(mtype) |
| 2663 |
const gchar * |
| 2664 |
gst_hls_rendition_stream_type_get_name (GstHLSRenditionStreamType mtype) |
| 2665 |
{ |
| 2666 |
static const gchar *nicks[GST_HLS_N_MEDIA_TYPES] = { "audio", "video", |
| 2667 |
"subtitle", "closed-captions" |
| 2668 |
}; |
| 2669 |
|
| 2670 |
if (mtype < 0 || mtype >= GST_HLS_N_MEDIA_TYPES) |
| 2671 |
return "invalid"; |
| 2672 |
|
| 2673 |
return nicks[mtype]; |
| 2674 |
} |
| 2675 |
|
| 2676 |
/* returns copy of string with surrounding quotation marks removed */ |
| 2677 |
static gchar * |
| 2678 |
gst_m3u8_unquote (const gchar * str) |
| 2679 |
{ |
| 2680 |
const gchar *start, *end; |
| 2681 |
|
| 2682 |
start = strchr (str, '"'); |
| 2683 |
if (start == NULL) |
| 2684 |
return g_strdup (str); |
| 2685 |
end = strchr (start + 1, '"'); |
| 2686 |
if (end == NULL) { |
| 2687 |
GST_WARNING ("Broken quoted string [%s] - can't find end quote", str); |
| 2688 |
return g_strdup (start + 1); |
| 2689 |
} |
| 2690 |
return g_strndup (start + 1, (gsize) (end - (start + 1))); |
| 2691 |
} |
| 2692 |
|
| 2693 |
static GstHLSRenditionStream * |
| 2694 |
gst_m3u8_parse_media (gchar * desc, const gchar * base_uri) |
| 2695 |
{ |
| 2696 |
GstHLSRenditionStream *media; |
| 2697 |
gchar *a, *v; |
| 2698 |
|
| 2699 |
media = g_new0 (GstHLSRenditionStream, 1); |
| 2700 |
media->ref_count = 1; |
| 2701 |
media->mtype = GST_HLS_RENDITION_STREAM_TYPE_INVALID; |
| 2702 |
|
| 2703 |
GST_LOG ("parsing %s", desc); |
| 2704 |
while (desc != NULL && parse_attributes (&desc, &a, &v)) { |
| 2705 |
if (strcmp (a, "TYPE") == 0) { |
| 2706 |
media->mtype = gst_m3u8_get_hls_media_type_from_string (v); |
| 2707 |
} else if (strcmp (a, "GROUP-ID") == 0) { |
| 2708 |
g_free (media->group_id); |
| 2709 |
media->group_id = gst_m3u8_unquote (v); |
| 2710 |
} else if (strcmp (a, "NAME") == 0) { |
| 2711 |
g_free (media->name); |
| 2712 |
media->name = gst_m3u8_unquote (v); |
| 2713 |
} else if (strcmp (a, "URI") == 0) { |
| 2714 |
gchar *uri; |
| 2715 |
|
| 2716 |
g_free (media->uri); |
| 2717 |
uri = gst_m3u8_unquote (v); |
| 2718 |
media->uri = uri_join (base_uri, uri); |
| 2719 |
g_free (uri); |
| 2720 |
} else if (strcmp (a, "LANGUAGE") == 0) { |
| 2721 |
g_free (media->lang); |
| 2722 |
media->lang = gst_m3u8_unquote (v); |
| 2723 |
} else if (strcmp (a, "DEFAULT") == 0) { |
| 2724 |
media->is_default = g_ascii_strcasecmp (v, "yes") == 0; |
| 2725 |
} else if (strcmp (a, "FORCED") == 0) { |
| 2726 |
media->forced = g_ascii_strcasecmp (v, "yes") == 0; |
| 2727 |
} else if (strcmp (a, "AUTOSELECT") == 0) { |
| 2728 |
media->autoselect = g_ascii_strcasecmp (v, "yes") == 0; |
| 2729 |
} else { |
| 2730 |
/* unhandled: ASSOC-LANGUAGE, INSTREAM-ID, CHARACTERISTICS, |
| 2731 |
* STABLE-RENDITION-ID, CHANNELS */ |
| 2732 |
GST_FIXME ("EXT-X-MEDIA: unhandled attribute: %s = %s", a, v); |
| 2733 |
} |
| 2734 |
} |
| 2735 |
|
| 2736 |
if (media->mtype == GST_HLS_RENDITION_STREAM_TYPE_INVALID) |
| 2737 |
goto required_attributes_missing; |
| 2738 |
|
| 2739 |
if (media->group_id == NULL || media->name == NULL) |
| 2740 |
goto required_attributes_missing; |
| 2741 |
|
| 2742 |
if (media->mtype == GST_HLS_RENDITION_STREAM_TYPE_CLOSED_CAPTIONS |
| 2743 |
&& media->uri != NULL) |
| 2744 |
goto uri_with_cc; |
| 2745 |
|
| 2746 |
GST_DEBUG ("media: %s, group '%s', name '%s', uri '%s', %s %s %s, lang=%s", |
| 2747 |
GST_HLS_RENDITION_STREAM_TYPE_NAME (media->mtype), media->group_id, |
| 2748 |
media->name, media->uri, media->is_default ? "default" : "-", |
| 2749 |
media->autoselect ? "autoselect" : "-", media->forced ? "forced" : "-", |
| 2750 |
media->lang ? media->lang : "??"); |
| 2751 |
|
| 2752 |
return media; |
| 2753 |
|
| 2754 |
uri_with_cc: |
| 2755 |
{ |
| 2756 |
GST_WARNING ("closed captions EXT-X-MEDIA should not have URI specified"); |
| 2757 |
goto out_error; |
| 2758 |
} |
| 2759 |
required_attributes_missing: |
| 2760 |
{ |
| 2761 |
GST_WARNING ("EXT-X-MEDIA description is missing required attributes"); |
| 2762 |
goto out_error; |
| 2763 |
} |
| 2764 |
|
| 2765 |
out_error: |
| 2766 |
{ |
| 2767 |
gst_hls_rendition_stream_unref (media); |
| 2768 |
return NULL; |
| 2769 |
} |
| 2770 |
} |
| 2771 |
|
| 2772 |
GstStreamType |
| 2773 |
gst_hls_get_stream_type_from_structure (GstStructure * st) |
| 2774 |
{ |
| 2775 |
const gchar *name = gst_structure_get_name (st); |
| 2776 |
|
| 2777 |
if (g_str_has_prefix (name, "audio/")) |
| 2778 |
return GST_STREAM_TYPE_AUDIO; |
| 2779 |
|
| 2780 |
if (g_str_has_prefix (name, "video/")) |
| 2781 |
return GST_STREAM_TYPE_VIDEO; |
| 2782 |
|
| 2783 |
if (g_str_has_prefix (name, "application/x-subtitle")) |
| 2784 |
return GST_STREAM_TYPE_TEXT; |
| 2785 |
|
| 2786 |
return 0; |
| 2787 |
} |
| 2788 |
|
| 2789 |
GstStreamType |
| 2790 |
gst_hls_get_stream_type_from_caps (GstCaps * caps) |
| 2791 |
{ |
| 2792 |
GstStreamType ret = 0; |
| 2793 |
guint i, nb; |
| 2794 |
nb = gst_caps_get_size (caps); |
| 2795 |
for (i = 0; i < nb; i++) { |
| 2796 |
GstStructure *cand = gst_caps_get_structure (caps, i); |
| 2797 |
|
| 2798 |
ret |= gst_hls_get_stream_type_from_structure (cand); |
| 2799 |
} |
| 2800 |
|
| 2801 |
return ret; |
| 2802 |
} |
| 2803 |
|
| 2804 |
static GstHLSVariantStream * |
| 2805 |
gst_hls_variant_stream_new (void) |
| 2806 |
{ |
| 2807 |
GstHLSVariantStream *stream; |
| 2808 |
|
| 2809 |
stream = g_new0 (GstHLSVariantStream, 1); |
| 2810 |
stream->refcount = 1; |
| 2811 |
stream->codecs_stream_type = 0; |
| 2812 |
return stream; |
| 2813 |
} |
| 2814 |
|
| 2815 |
GstHLSVariantStream * |
| 2816 |
hls_variant_stream_ref (GstHLSVariantStream * stream) |
| 2817 |
{ |
| 2818 |
g_atomic_int_inc (&stream->refcount); |
| 2819 |
return stream; |
| 2820 |
} |
| 2821 |
|
| 2822 |
void |
| 2823 |
hls_variant_stream_unref (GstHLSVariantStream * stream) |
| 2824 |
{ |
| 2825 |
if (g_atomic_int_dec_and_test (&stream->refcount)) { |
| 2826 |
gint i; |
| 2827 |
|
| 2828 |
g_free (stream->name); |
| 2829 |
g_free (stream->uri); |
| 2830 |
g_free (stream->codecs); |
| 2831 |
if (stream->caps) |
| 2832 |
gst_caps_unref (stream->caps); |
| 2833 |
for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) { |
| 2834 |
g_free (stream->media_groups[i]); |
| 2835 |
} |
| 2836 |
g_list_free_full (stream->fallback, g_free); |
| 2837 |
g_free (stream); |
| 2838 |
} |
| 2839 |
} |
| 2840 |
|
| 2841 |
static GstHLSVariantStream * |
| 2842 |
gst_hls_variant_parse (gchar * data, const gchar * base_uri) |
| 2843 |
{ |
| 2844 |
GstHLSVariantStream *stream; |
| 2845 |
gchar *v, *a; |
| 2846 |
|
| 2847 |
stream = gst_hls_variant_stream_new (); |
| 2848 |
stream->iframe = g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:"); |
| 2849 |
data += stream->iframe ? 26 : 18; |
| 2850 |
|
| 2851 |
while (data && parse_attributes (&data, &a, &v)) { |
| 2852 |
if (g_str_equal (a, "BANDWIDTH")) { |
| 2853 |
if (!stream->bandwidth) { |
| 2854 |
if (!int_from_string (v, NULL, &stream->bandwidth)) |
| 2855 |
GST_WARNING ("Error while reading BANDWIDTH"); |
| 2856 |
} |
| 2857 |
} else if (g_str_equal (a, "AVERAGE-BANDWIDTH")) { |
| 2858 |
GST_DEBUG |
| 2859 |
("AVERAGE-BANDWIDTH attribute available. Using it as stream bandwidth"); |
| 2860 |
if (!int_from_string (v, NULL, &stream->bandwidth)) |
| 2861 |
GST_WARNING ("Error while reading AVERAGE-BANDWIDTH"); |
| 2862 |
} else if (g_str_equal (a, "PROGRAM-ID")) { |
| 2863 |
if (!int_from_string (v, NULL, &stream->program_id)) |
| 2864 |
GST_WARNING ("Error while reading PROGRAM-ID"); |
| 2865 |
} else if (g_str_equal (a, "CODECS")) { |
| 2866 |
g_free (stream->codecs); |
| 2867 |
stream->codecs = g_strdup (v); |
| 2868 |
stream->caps = gst_codec_utils_caps_from_mime_codec (stream->codecs); |
| 2869 |
if (stream->caps != NULL) { |
| 2870 |
stream->codecs_stream_type = |
| 2871 |
gst_hls_get_stream_type_from_caps (stream->caps); |
| 2872 |
} else { |
| 2873 |
GST_WARNING ("Unhandled codec in CODECS tags: %s", v); |
| 2874 |
} |
| 2875 |
} else if (g_str_equal (a, "RESOLUTION")) { |
| 2876 |
if (!int_from_string (v, &v, &stream->width)) |
| 2877 |
GST_WARNING ("Error while reading RESOLUTION width"); |
| 2878 |
if (!v || *v != 'x') { |
| 2879 |
GST_WARNING ("Missing height"); |
| 2880 |
} else { |
| 2881 |
v = g_utf8_next_char (v); |
| 2882 |
if (!int_from_string (v, NULL, &stream->height)) |
| 2883 |
GST_WARNING ("Error while reading RESOLUTION height"); |
| 2884 |
} |
| 2885 |
} else if (stream->iframe && g_str_equal (a, "URI")) { |
| 2886 |
stream->uri = uri_join (base_uri, v); |
| 2887 |
} else if (g_str_equal (a, "AUDIO")) { |
| 2888 |
g_free (stream->media_groups[GST_HLS_RENDITION_STREAM_TYPE_AUDIO]); |
| 2889 |
stream->media_groups[GST_HLS_RENDITION_STREAM_TYPE_AUDIO] = |
| 2890 |
gst_m3u8_unquote (v); |
| 2891 |
} else if (g_str_equal (a, "SUBTITLES")) { |
| 2892 |
g_free (stream->media_groups[GST_HLS_RENDITION_STREAM_TYPE_SUBTITLES]); |
| 2893 |
stream->media_groups[GST_HLS_RENDITION_STREAM_TYPE_SUBTITLES] = |
| 2894 |
gst_m3u8_unquote (v); |
| 2895 |
} else if (g_str_equal (a, "VIDEO")) { |
| 2896 |
g_free (stream->media_groups[GST_HLS_RENDITION_STREAM_TYPE_VIDEO]); |
| 2897 |
stream->media_groups[GST_HLS_RENDITION_STREAM_TYPE_VIDEO] = |
| 2898 |
gst_m3u8_unquote (v); |
| 2899 |
} else if (g_str_equal (a, "CLOSED-CAPTIONS")) { |
| 2900 |
/* closed captions will be embedded inside the video stream, ignore */ |
| 2901 |
} |
| 2902 |
} |
| 2903 |
|
| 2904 |
return stream; |
| 2905 |
} |
| 2906 |
|
| 2907 |
static gchar * |
| 2908 |
generate_variant_stream_name (gchar * uri, gint bandwidth) |
| 2909 |
{ |
| 2910 |
gchar *checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA1, uri, -1); |
| 2911 |
gchar *res = g_strdup_printf ("variant-%dbps-%s", bandwidth, checksum); |
| 2912 |
|
| 2913 |
g_free (checksum); |
| 2914 |
return res; |
| 2915 |
} |
| 2916 |
|
| 2917 |
static GstHLSVariantStream * |
| 2918 |
find_variant_stream_by_name (GList * list, const gchar * name) |
| 2919 |
{ |
| 2920 |
for (; list != NULL; list = list->next) { |
| 2921 |
GstHLSVariantStream *variant_stream = list->data; |
| 2922 |
|
| 2923 |
if (variant_stream->name != NULL && !strcmp (variant_stream->name, name)) |
| 2924 |
return variant_stream; |
| 2925 |
} |
| 2926 |
return NULL; |
| 2927 |
} |
| 2928 |
|
| 2929 |
static GstHLSVariantStream * |
| 2930 |
find_variant_stream_by_uri (GList * list, const gchar * uri) |
| 2931 |
{ |
| 2932 |
for (; list != NULL; list = list->next) { |
| 2933 |
GstHLSVariantStream *variant_stream = list->data; |
| 2934 |
|
| 2935 |
if (variant_stream->uri != NULL && !strcmp (variant_stream->uri, uri)) |
| 2936 |
return variant_stream; |
| 2937 |
} |
| 2938 |
return NULL; |
| 2939 |
} |
| 2940 |
|
| 2941 |
static GstHLSVariantStream * |
| 2942 |
find_variant_stream_for_fallback (GList * list, GstHLSVariantStream * fallback) |
| 2943 |
{ |
| 2944 |
for (; list != NULL; list = list->next) { |
| 2945 |
GstHLSVariantStream *variant_stream = list->data; |
| 2946 |
|
| 2947 |
if (variant_stream->bandwidth == fallback->bandwidth && |
| 2948 |
variant_stream->width == fallback->width && |
| 2949 |
variant_stream->height == fallback->height && |
| 2950 |
variant_stream->iframe == fallback->iframe && |
| 2951 |
!g_strcmp0 (variant_stream->codecs, fallback->codecs)) |
| 2952 |
return variant_stream; |
| 2953 |
} |
| 2954 |
return NULL; |
| 2955 |
} |
| 2956 |
|
| 2957 |
static GstHLSMasterPlaylist * |
| 2958 |
gst_hls_master_playlist_new (void) |
| 2959 |
{ |
| 2960 |
GstHLSMasterPlaylist *playlist; |
| 2961 |
|
| 2962 |
playlist = g_new0 (GstHLSMasterPlaylist, 1); |
| 2963 |
playlist->refcount = 1; |
| 2964 |
playlist->is_simple = FALSE; |
| 2965 |
|
| 2966 |
return playlist; |
| 2967 |
} |
| 2968 |
|
| 2969 |
void |
| 2970 |
hls_master_playlist_unref (GstHLSMasterPlaylist * playlist) |
| 2971 |
{ |
| 2972 |
if (g_atomic_int_dec_and_test (&playlist->refcount)) { |
| 2973 |
g_list_free_full (playlist->renditions, |
| 2974 |
(GDestroyNotify) gst_hls_rendition_stream_unref); |
| 2975 |
g_list_free_full (playlist->variants, |
| 2976 |
(GDestroyNotify) gst_hls_variant_stream_unref); |
| 2977 |
g_list_free_full (playlist->iframe_variants, |
| 2978 |
(GDestroyNotify) gst_hls_variant_stream_unref); |
| 2979 |
if (playlist->default_variant) |
| 2980 |
gst_hls_variant_stream_unref (playlist->default_variant); |
| 2981 |
g_free (playlist->last_data); |
| 2982 |
g_free (playlist); |
| 2983 |
} |
| 2984 |
} |
| 2985 |
|
| 2986 |
static gint |
| 2987 |
hls_media_compare_func (GstHLSRenditionStream * ma, GstHLSRenditionStream * mb) |
| 2988 |
{ |
| 2989 |
if (ma->mtype != mb->mtype) |
| 2990 |
return ma->mtype - mb->mtype; |
| 2991 |
|
| 2992 |
return strcmp (ma->name, mb->name) || strcmp (ma->group_id, mb->group_id); |
| 2993 |
} |
| 2994 |
|
| 2995 |
static GstCaps * |
| 2996 |
stream_get_media_caps (GstHLSVariantStream * stream, |
| 2997 |
GstHLSRenditionStreamType mtype) |
| 2998 |
{ |
| 2999 |
GstStructure *st = NULL; |
| 3000 |
GstCaps *ret; |
| 3001 |
guint i, nb; |
| 3002 |
|
| 3003 |
if (stream->caps == NULL) |
| 3004 |
return NULL; |
| 3005 |
|
| 3006 |
nb = gst_caps_get_size (stream->caps); |
| 3007 |
for (i = 0; i < nb; i++) { |
| 3008 |
GstStructure *cand = gst_caps_get_structure (stream->caps, i); |
| 3009 |
const gchar *name = gst_structure_get_name (cand); |
| 3010 |
gboolean matched; |
| 3011 |
|
| 3012 |
switch (mtype) { |
| 3013 |
case GST_HLS_RENDITION_STREAM_TYPE_AUDIO: |
| 3014 |
matched = g_str_has_prefix (name, "audio/"); |
| 3015 |
break; |
| 3016 |
case GST_HLS_RENDITION_STREAM_TYPE_VIDEO: |
| 3017 |
matched = g_str_has_prefix (name, "video/"); |
| 3018 |
break; |
| 3019 |
case GST_HLS_RENDITION_STREAM_TYPE_SUBTITLES: |
| 3020 |
matched = g_str_has_prefix (name, "application/x-subtitle"); |
| 3021 |
break; |
| 3022 |
default: |
| 3023 |
matched = FALSE; |
| 3024 |
break; |
| 3025 |
} |
| 3026 |
|
| 3027 |
if (!matched) |
| 3028 |
continue; |
| 3029 |
|
| 3030 |
if (st) { |
| 3031 |
GST_WARNING ("More than one caps for the same type, can't match"); |
| 3032 |
return NULL; |
| 3033 |
} |
| 3034 |
|
| 3035 |
st = cand; |
| 3036 |
} |
| 3037 |
|
| 3038 |
if (!st) |
| 3039 |
return NULL; |
| 3040 |
|
| 3041 |
ret = gst_caps_new_empty (); |
| 3042 |
gst_caps_append_structure (ret, gst_structure_copy (st)); |
| 3043 |
return ret; |
| 3044 |
|
| 3045 |
} |
| 3046 |
|
| 3047 |
static gint |
| 3048 |
gst_hls_variant_stream_compare_by_bitrate (gconstpointer a, gconstpointer b) |
| 3049 |
{ |
| 3050 |
const GstHLSVariantStream *vs_a = (const GstHLSVariantStream *) a; |
| 3051 |
const GstHLSVariantStream *vs_b = (const GstHLSVariantStream *) b; |
| 3052 |
|
| 3053 |
if (vs_a->bandwidth == vs_b->bandwidth) |
| 3054 |
return g_strcmp0 (vs_a->name, vs_b->name); |
| 3055 |
|
| 3056 |
return vs_a->bandwidth - vs_b->bandwidth; |
| 3057 |
} |
| 3058 |
|
| 3059 |
/** |
| 3060 |
* gst_hls_master_playlist_new_from_data: |
| 3061 |
* @data: (transfer full): The manifest to parse |
| 3062 |
* @base_uri: The URI of the manifest |
| 3063 |
* |
| 3064 |
* Parse the provided manifest and construct the master playlist. |
| 3065 |
* |
| 3066 |
* Returns: The parse GstHLSMasterPlaylist , or NULL if there was an error. |
| 3067 |
*/ |
| 3068 |
GstHLSMasterPlaylist * |
| 3069 |
hls_master_playlist_new_from_data (gchar * data, const gchar * base_uri) |
| 3070 |
{ |
| 3071 |
GstHLSMasterPlaylist *playlist; |
| 3072 |
GstHLSVariantStream *pending_stream, *existing_stream; |
| 3073 |
gchar *end, *free_data = data; |
| 3074 |
gint val; |
| 3075 |
GList *tmp; |
| 3076 |
GstStreamType most_seen_types = 0; |
| 3077 |
|
| 3078 |
if (!g_str_has_prefix (data, "#EXTM3U")) { |
| 3079 |
GST_WARNING ("Data doesn't start with #EXTM3U"); |
| 3080 |
g_free (free_data); |
| 3081 |
return NULL; |
| 3082 |
} |
| 3083 |
|
| 3084 |
playlist = gst_hls_master_playlist_new (); |
| 3085 |
|
| 3086 |
/* store data before we modify it for parsing */ |
| 3087 |
playlist->last_data = g_strdup (data); |
| 3088 |
|
| 3089 |
GST_TRACE ("data:\n%s", data); |
| 3090 |
|
| 3091 |
/* Detect early whether this manifest describes a simple media playlist or |
| 3092 |
* not */ |
| 3093 |
if (strstr (data, "\n#EXTINF:") != NULL) { |
| 3094 |
GST_INFO ("This is a simple media playlist, not a master playlist"); |
| 3095 |
|
| 3096 |
pending_stream = gst_hls_variant_stream_new (); |
| 3097 |
pending_stream->name = g_strdup ("media-playlist"); |
| 3098 |
pending_stream->uri = g_strdup (base_uri); |
| 3099 |
playlist->variants = g_list_append (playlist->variants, pending_stream); |
| 3100 |
playlist->default_variant = gst_hls_variant_stream_ref (pending_stream); |
| 3101 |
playlist->is_simple = TRUE; |
| 3102 |
|
| 3103 |
return playlist; |
| 3104 |
} |
| 3105 |
|
| 3106 |
/* Beginning of the actual master playlist parsing */ |
| 3107 |
pending_stream = NULL; |
| 3108 |
data += 7; |
| 3109 |
while (TRUE) { |
| 3110 |
gchar *r; |
| 3111 |
|
| 3112 |
end = g_utf8_strchr (data, -1, '\n'); |
| 3113 |
if (end) |
| 3114 |
*end = '\0'; |
| 3115 |
|
| 3116 |
r = g_utf8_strchr (data, -1, '\r'); |
| 3117 |
if (r) |
| 3118 |
*r = '\0'; |
| 3119 |
|
| 3120 |
if (data[0] != '#' && data[0] != '\0') { |
| 3121 |
gchar *name, *uri; |
| 3122 |
|
| 3123 |
if (pending_stream == NULL) { |
| 3124 |
GST_LOG ("%s: got non-empty line without EXT-STREAM-INF, dropping", |
| 3125 |
data); |
| 3126 |
goto next_line; |
| 3127 |
} |
| 3128 |
|
| 3129 |
uri = uri_join (base_uri, data); |
| 3130 |
if (uri == NULL) |
| 3131 |
goto next_line; |
| 3132 |
|
| 3133 |
pending_stream->name = name = |
| 3134 |
generate_variant_stream_name (uri, pending_stream->bandwidth); |
| 3135 |
pending_stream->uri = uri; |
| 3136 |
|
| 3137 |
if (find_variant_stream_by_name (playlist->variants, name) |
| 3138 |
|| find_variant_stream_by_uri (playlist->variants, uri)) { |
| 3139 |
GST_DEBUG ("Already have a list with this name or URI: %s", name); |
| 3140 |
gst_hls_variant_stream_unref (pending_stream); |
| 3141 |
} else if ((existing_stream = |
| 3142 |
find_variant_stream_for_fallback (playlist->variants, |
| 3143 |
pending_stream))) { |
| 3144 |
GST_DEBUG ("Adding to %s fallback URI %s", existing_stream->name, |
| 3145 |
pending_stream->uri); |
| 3146 |
existing_stream->fallback = |
| 3147 |
g_list_append (existing_stream->fallback, |
| 3148 |
g_strdup (pending_stream->uri)); |
| 3149 |
gst_hls_variant_stream_unref (pending_stream); |
| 3150 |
} else { |
| 3151 |
GST_INFO ("stream %s @ %u: %s", name, pending_stream->bandwidth, uri); |
| 3152 |
playlist->variants = g_list_append (playlist->variants, pending_stream); |
| 3153 |
/* use first stream in the playlist as default */ |
| 3154 |
if (playlist->default_variant == NULL) { |
| 3155 |
playlist->default_variant = |
| 3156 |
gst_hls_variant_stream_ref (pending_stream); |
| 3157 |
} |
| 3158 |
} |
| 3159 |
pending_stream = NULL; |
| 3160 |
} else if (g_str_has_prefix (data, "#EXT-X-VERSION:")) { |
| 3161 |
if (int_from_string (data + 15, &data, &val)) |
| 3162 |
playlist->version = val; |
| 3163 |
} else if (g_str_has_prefix (data, "#EXT-X-STREAM-INF:") || |
| 3164 |
g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:")) { |
| 3165 |
GstHLSVariantStream *stream = gst_hls_variant_parse (data, base_uri); |
| 3166 |
|
| 3167 |
if (stream->iframe) { |
| 3168 |
if (find_variant_stream_by_uri (playlist->iframe_variants, stream->uri)) { |
| 3169 |
GST_DEBUG ("Already have a list with this URI"); |
| 3170 |
gst_hls_variant_stream_unref (stream); |
| 3171 |
} else { |
| 3172 |
playlist->iframe_variants = |
| 3173 |
g_list_append (playlist->iframe_variants, stream); |
| 3174 |
} |
| 3175 |
} else { |
| 3176 |
if (pending_stream != NULL) { |
| 3177 |
GST_WARNING ("variant stream without uri, dropping"); |
| 3178 |
gst_hls_variant_stream_unref (pending_stream); |
| 3179 |
} |
| 3180 |
pending_stream = stream; |
| 3181 |
} |
| 3182 |
} else if (g_str_has_prefix (data, "#EXT-X-MEDIA:")) { |
| 3183 |
GstHLSRenditionStream *media; |
| 3184 |
|
| 3185 |
media = gst_m3u8_parse_media (data + strlen ("#EXT-X-MEDIA:"), base_uri); |
| 3186 |
|
| 3187 |
if (media == NULL) |
| 3188 |
goto next_line; |
| 3189 |
|
| 3190 |
if (g_list_find_custom (playlist->renditions, media, |
| 3191 |
(GCompareFunc) hls_media_compare_func)) { |
| 3192 |
GST_DEBUG ("Dropping duplicate alternate rendition group : %s", data); |
| 3193 |
gst_hls_rendition_stream_unref (media); |
| 3194 |
goto next_line; |
| 3195 |
} |
| 3196 |
playlist->renditions = g_list_append (playlist->renditions, media); |
| 3197 |
GST_INFO ("Stored media %s / group %s", media->name, media->group_id); |
| 3198 |
} else if (*data != '\0') { |
| 3199 |
GST_LOG ("Ignored line: %s", data); |
| 3200 |
} |
| 3201 |
|
| 3202 |
next_line: |
| 3203 |
if (!end) |
| 3204 |
break; |
| 3205 |
data = g_utf8_next_char (end); /* skip \n */ |
| 3206 |
} |
| 3207 |
|
| 3208 |
if (pending_stream != NULL) { |
| 3209 |
GST_WARNING ("#EXT-X-STREAM-INF without uri, dropping"); |
| 3210 |
gst_hls_variant_stream_unref (pending_stream); |
| 3211 |
} |
| 3212 |
|
| 3213 |
g_free (free_data); |
| 3214 |
|
| 3215 |
if (playlist->variants == NULL) { |
| 3216 |
GST_WARNING ("Master playlist without any media playlists!"); |
| 3217 |
gst_hls_master_playlist_unref (playlist); |
| 3218 |
return NULL; |
| 3219 |
} |
| 3220 |
|
| 3221 |
/* reorder variants by bitrate */ |
| 3222 |
playlist->variants = |
| 3223 |
g_list_sort (playlist->variants, |
| 3224 |
(GCompareFunc) gst_hls_variant_stream_compare_by_bitrate); |
| 3225 |
|
| 3226 |
playlist->iframe_variants = |
| 3227 |
g_list_sort (playlist->iframe_variants, |
| 3228 |
(GCompareFunc) gst_hls_variant_stream_compare_by_bitrate); |
| 3229 |
|
| 3230 |
#ifndef GST_DISABLE_GST_DEBUG |
| 3231 |
/* Sanity check : If there are no codecs, a stream shouldn't point to |
| 3232 |
* alternate rendition groups. |
| 3233 |
* |
| 3234 |
* Write a warning to help with further debugging if this causes issues |
| 3235 |
* later */ |
| 3236 |
for (tmp = playlist->variants; tmp; tmp = tmp->next) { |
| 3237 |
GstHLSVariantStream *stream = tmp->data; |
| 3238 |
|
| 3239 |
if (stream->codecs == NULL) { |
| 3240 |
if (stream->media_groups[0] || stream->media_groups[1] |
| 3241 |
|| stream->media_groups[2] || stream->media_groups[3]) { |
| 3242 |
GST_WARNING |
| 3243 |
("Variant specifies alternate rendition groups but has no codecs specified"); |
| 3244 |
} |
| 3245 |
} |
| 3246 |
} |
| 3247 |
#endif |
| 3248 |
|
| 3249 |
/* Filter out audio-only variants from audio+video stream */ |
| 3250 |
for (tmp = playlist->variants; tmp; tmp = tmp->next) { |
| 3251 |
GstHLSVariantStream *stream = tmp->data; |
| 3252 |
|
| 3253 |
most_seen_types |= stream->codecs_stream_type; |
| 3254 |
} |
| 3255 |
|
| 3256 |
/* Flag the playlist to indicate whether all codecs are known or not on variants */ |
| 3257 |
playlist->have_codecs = most_seen_types != 0; |
| 3258 |
|
| 3259 |
GST_DEBUG ("have_codecs:%d most_seen_types:%d", playlist->have_codecs, |
| 3260 |
most_seen_types); |
| 3261 |
|
| 3262 |
/* Filter out audio-only variants from audio+video stream */ |
| 3263 |
if (playlist->have_codecs && most_seen_types != GST_STREAM_TYPE_AUDIO) { |
| 3264 |
tmp = playlist->variants; |
| 3265 |
while (tmp) { |
| 3266 |
GstHLSVariantStream *stream = tmp->data; |
| 3267 |
|
| 3268 |
if (stream->codecs_stream_type != most_seen_types && |
| 3269 |
stream->codecs_stream_type == GST_STREAM_TYPE_AUDIO) { |
| 3270 |
GST_DEBUG ("Remove variant with partial stream types %s", stream->name); |
| 3271 |
tmp = playlist->variants = g_list_remove (playlist->variants, stream); |
| 3272 |
gst_hls_variant_stream_unref (stream); |
| 3273 |
} else |
| 3274 |
tmp = tmp->next; |
| 3275 |
} |
| 3276 |
} |
| 3277 |
|
| 3278 |
if (playlist->renditions) { |
| 3279 |
guint i; |
| 3280 |
/* Assign information from variants to alternate rendition groups. Note that |
| 3281 |
* at this point we know that there are caps present on the variants */ |
| 3282 |
for (tmp = playlist->variants; tmp; tmp = tmp->next) { |
| 3283 |
GstHLSVariantStream *stream = tmp->data; |
| 3284 |
|
| 3285 |
GST_DEBUG ("Post-processing Variant Stream '%s'", stream->name); |
| 3286 |
|
| 3287 |
for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) { |
| 3288 |
gchar *alt_rend_group = stream->media_groups[i]; |
| 3289 |
|
| 3290 |
if (alt_rend_group) { |
| 3291 |
gboolean alt_in_variant = FALSE; |
| 3292 |
GstCaps *media_caps = stream_get_media_caps (stream, i); |
| 3293 |
GList *altlist; |
| 3294 |
if (!media_caps) |
| 3295 |
continue; |
| 3296 |
for (altlist = playlist->renditions; altlist; altlist = altlist->next) { |
| 3297 |
GstHLSRenditionStream *media = altlist->data; |
| 3298 |
if (media->mtype != i |
| 3299 |
|| g_strcmp0 (media->group_id, alt_rend_group)) |
| 3300 |
continue; |
| 3301 |
GST_DEBUG (" %s caps:%" GST_PTR_FORMAT " media %s, uri: %s", |
| 3302 |
GST_HLS_RENDITION_STREAM_TYPE_NAME (i), media_caps, media->name, |
| 3303 |
media->uri); |
| 3304 |
if (media->uri == NULL) { |
| 3305 |
GST_DEBUG (" Media is present in main variant stream"); |
| 3306 |
alt_in_variant = TRUE; |
| 3307 |
} else { |
| 3308 |
/* Assign caps to media */ |
| 3309 |
if (media->caps && !gst_caps_is_equal (media->caps, media_caps)) { |
| 3310 |
GST_ERROR (" Media already has different caps %" |
| 3311 |
GST_PTR_FORMAT, media->caps); |
| 3312 |
} else { |
| 3313 |
GST_DEBUG (" Assigning caps %" GST_PTR_FORMAT, media_caps); |
| 3314 |
gst_caps_replace (&media->caps, media_caps); |
| 3315 |
} |
| 3316 |
} |
| 3317 |
} |
| 3318 |
if (!alt_in_variant) { |
| 3319 |
GstCaps *new_caps = gst_caps_subtract (stream->caps, media_caps); |
| 3320 |
gst_caps_replace (&stream->caps, new_caps); |
| 3321 |
gst_caps_unref (new_caps); |
| 3322 |
} |
| 3323 |
gst_caps_unref (media_caps); |
| 3324 |
} |
| 3325 |
} |
| 3326 |
GST_DEBUG ("Stream Ends up with caps %" GST_PTR_FORMAT, stream->caps); |
| 3327 |
} |
| 3328 |
} |
| 3329 |
|
| 3330 |
GST_DEBUG |
| 3331 |
("parsed master playlist with %d streams, %d I-frame streams and %d alternative rendition groups", |
| 3332 |
g_list_length (playlist->variants), |
| 3333 |
g_list_length (playlist->iframe_variants), |
| 3334 |
g_list_length (playlist->renditions)); |
| 3335 |
|
| 3336 |
|
| 3337 |
return playlist; |
| 3338 |
} |
| 3339 |
|
| 3340 |
GstHLSVariantStream * |
| 3341 |
hls_master_playlist_get_variant_for_bitrate (GstHLSMasterPlaylist * |
| 3342 |
playlist, gboolean iframe_variant, guint bitrate, |
| 3343 |
guint min_bitrate, GList * failed_variants) |
| 3344 |
{ |
| 3345 |
GstHLSVariantStream *variant = NULL; |
| 3346 |
GstHLSVariantStream *variant_by_min = NULL; |
| 3347 |
GList *l; |
| 3348 |
|
| 3349 |
/* variant lists are sorted low to high, so iterate from highest to lowest */ |
| 3350 |
if (iframe_variant && playlist->iframe_variants != NULL) |
| 3351 |
l = g_list_last (playlist->iframe_variants); |
| 3352 |
else |
| 3353 |
l = g_list_last (playlist->variants); |
| 3354 |
|
| 3355 |
while (l != NULL) { |
| 3356 |
if (g_list_find (failed_variants, l->data) != NULL) { |
| 3357 |
/* Ignore all variants from the failed list */ |
| 3358 |
l = l->prev; |
| 3359 |
continue; |
| 3360 |
} |
| 3361 |
|
| 3362 |
variant = l->data; |
| 3363 |
if (variant->bandwidth >= min_bitrate) |
| 3364 |
variant_by_min = variant; |
| 3365 |
if (variant->bandwidth <= bitrate) |
| 3366 |
break; |
| 3367 |
l = l->prev; |
| 3368 |
} |
| 3369 |
|
| 3370 |
/* If variant bitrate is above the min_bitrate (or min_bitrate == 0) |
| 3371 |
* return it now */ |
| 3372 |
if (variant && variant->bandwidth >= min_bitrate) |
| 3373 |
return variant; |
| 3374 |
|
| 3375 |
/* Otherwise, return the last (lowest bitrate) variant we saw that |
| 3376 |
* was higher than the min_bitrate */ |
| 3377 |
return variant_by_min; |
| 3378 |
} |
| 3379 |
|
| 3380 |
static gboolean |
| 3381 |
remove_uncommon (const GstIdStr * fieldname, GValue * value, GstStructure * st2) |
| 3382 |
{ |
| 3383 |
const GValue *other; |
| 3384 |
GValue dest = G_VALUE_INIT; |
| 3385 |
|
| 3386 |
other = gst_structure_id_str_get_value (st2, fieldname); |
| 3387 |
|
| 3388 |
if (other == NULL || (G_VALUE_TYPE (value) != G_VALUE_TYPE (other))) |
| 3389 |
return FALSE; |
| 3390 |
|
| 3391 |
if (!gst_value_intersect (&dest, value, other)) |
| 3392 |
return FALSE; |
| 3393 |
|
| 3394 |
g_value_reset (value); |
| 3395 |
g_value_copy (&dest, value); |
| 3396 |
g_value_reset (&dest); |
| 3397 |
|
| 3398 |
return TRUE; |
| 3399 |
} |
| 3400 |
|
| 3401 |
/* Merge all common structures from caps1 and caps2 |
| 3402 |
* |
| 3403 |
* Returns empty caps if a structure is not present in both */ |
| 3404 |
static GstCaps * |
| 3405 |
gst_caps_merge_common (GstCaps * caps1, GstCaps * caps2) |
| 3406 |
{ |
| 3407 |
guint it1, it2; |
| 3408 |
GstCaps *res = gst_caps_new_empty (); |
| 3409 |
|
| 3410 |
for (it1 = 0; it1 < gst_caps_get_size (caps1); it1++) { |
| 3411 |
GstStructure *st1 = gst_caps_get_structure (caps1, it1); |
| 3412 |
GstStructure *merged = NULL; |
| 3413 |
const gchar *name1 = gst_structure_get_name (st1); |
| 3414 |
|
| 3415 |
for (it2 = 0; it2 < gst_caps_get_size (caps2); it2++) { |
| 3416 |
GstStructure *st2 = gst_caps_get_structure (caps2, it2); |
| 3417 |
if (gst_structure_has_name (st2, name1)) { |
| 3418 |
if (merged == NULL) |
| 3419 |
merged = gst_structure_copy (st1); |
| 3420 |
gst_structure_filter_and_map_in_place_id_str (merged, |
| 3421 |
(GstStructureFilterMapIdStrFunc) remove_uncommon, st2); |
| 3422 |
} |
| 3423 |
} |
| 3424 |
|
| 3425 |
if (merged == NULL) |
| 3426 |
goto fail; |
| 3427 |
gst_caps_append_structure (res, merged); |
| 3428 |
} |
| 3429 |
|
| 3430 |
return res; |
| 3431 |
|
| 3432 |
fail: |
| 3433 |
{ |
| 3434 |
GST_WARNING ("Failed to create common caps of %" |
| 3435 |
GST_PTR_FORMAT " and %" GST_PTR_FORMAT, caps1, caps2); |
| 3436 |
gst_caps_unref (res); |
| 3437 |
return NULL; |
| 3438 |
} |
| 3439 |
} |
| 3440 |
|
| 3441 |
GstCaps * |
| 3442 |
hls_master_playlist_get_common_caps (GstHLSMasterPlaylist * playlist) |
| 3443 |
{ |
| 3444 |
GList *tmp; |
| 3445 |
GstCaps *res = NULL; |
| 3446 |
|
| 3447 |
for (tmp = playlist->variants; tmp; tmp = tmp->next) { |
| 3448 |
GstHLSVariantStream *stream = tmp->data; |
| 3449 |
|
| 3450 |
GST_DEBUG ("stream caps %" GST_PTR_FORMAT, stream->caps); |
| 3451 |
if (!stream->caps) { |
| 3452 |
/* If one of the stream doesn't have *any* caps, we can't reliably return |
| 3453 |
* any common caps */ |
| 3454 |
if (res) |
| 3455 |
gst_caps_unref (res); |
| 3456 |
res = NULL; |
| 3457 |
goto beach; |
| 3458 |
} |
| 3459 |
if (!res) { |
| 3460 |
res = gst_caps_copy (stream->caps); |
| 3461 |
} else { |
| 3462 |
GstCaps *common_caps = gst_caps_merge_common (res, stream->caps); |
| 3463 |
gst_caps_unref (res); |
| 3464 |
res = common_caps; |
| 3465 |
if (!res) |
| 3466 |
goto beach; |
| 3467 |
} |
| 3468 |
} |
| 3469 |
|
| 3470 |
res = gst_caps_simplify (res); |
| 3471 |
|
| 3472 |
beach: |
| 3473 |
GST_DEBUG ("Returning common caps %" GST_PTR_FORMAT, res); |
| 3474 |
|
| 3475 |
return res; |
| 3476 |
} |
| 3477 |
|
| 3478 |
GstStreamType |
| 3479 |
gst_stream_type_from_hls_type (GstHLSRenditionStreamType mtype) |
| 3480 |
{ |
| 3481 |
switch (mtype) { |
| 3482 |
case GST_HLS_RENDITION_STREAM_TYPE_AUDIO: |
| 3483 |
return GST_STREAM_TYPE_AUDIO; |
| 3484 |
case GST_HLS_RENDITION_STREAM_TYPE_VIDEO: |
| 3485 |
return GST_STREAM_TYPE_VIDEO; |
| 3486 |
case GST_HLS_RENDITION_STREAM_TYPE_SUBTITLES: |
| 3487 |
return GST_STREAM_TYPE_TEXT; |
| 3488 |
default: |
| 3489 |
return GST_STREAM_TYPE_UNKNOWN; |
| 3490 |
} |
| 3491 |
} |