2 * Copyright (C) 2010 Marc-Andre Lureau <marcandre.lureau@gmail.com>
3 * Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
32 #define GST_CAT_DEFAULT hls_debug
34 static GstM3U8MediaFile *gst_m3u8_media_file_new (gchar * uri,
35 gchar * title, GstClockTime duration, guint sequence);
36 gchar *uri_join (const gchar * uri, const gchar * path);
43 m3u8 = g_new0 (GstM3U8, 1);
45 m3u8->current_file = NULL;
46 m3u8->current_file_duration = GST_CLOCK_TIME_NONE;
48 m3u8->sequence_position = 0;
49 m3u8->highest_sequence_number = -1;
50 m3u8->duration = GST_CLOCK_TIME_NONE;
52 g_mutex_init (&m3u8->lock);
58 /* call with M3U8_LOCK held */
60 gst_m3u8_take_uri (GstM3U8 * self, gchar * uri, gchar * base_uri, gchar * name)
62 g_return_if_fail (self != NULL);
64 if (self->uri != uri) {
68 if (self->base_uri != base_uri) {
69 g_free (self->base_uri);
70 self->base_uri = base_uri;
72 if (self->name != name) {
79 gst_m3u8_set_uri (GstM3U8 * m3u8, const gchar * uri, const gchar * base_uri,
83 gst_m3u8_take_uri (m3u8, g_strdup (uri), g_strdup (base_uri),
85 GST_M3U8_UNLOCK (m3u8);
89 gst_m3u8_ref (GstM3U8 * m3u8)
91 g_assert (m3u8 != NULL && m3u8->ref_count > 0);
93 g_atomic_int_add (&m3u8->ref_count, 1);
98 gst_m3u8_unref (GstM3U8 * self)
100 g_return_if_fail (self != NULL && self->ref_count > 0);
102 if (g_atomic_int_dec_and_test (&self->ref_count)) {
104 g_free (self->base_uri);
107 g_list_foreach (self->files, (GFunc) gst_m3u8_media_file_unref, NULL);
108 g_list_free (self->files);
110 g_free (self->last_data);
115 static GstM3U8MediaFile *
116 gst_m3u8_media_file_new (gchar * uri, gchar * title, GstClockTime duration,
119 GstM3U8MediaFile *file;
121 file = g_new0 (GstM3U8MediaFile, 1);
124 file->duration = duration;
125 file->sequence = sequence;
132 gst_m3u8_media_file_ref (GstM3U8MediaFile * mfile)
134 g_assert (mfile != NULL && mfile->ref_count > 0);
136 g_atomic_int_add (&mfile->ref_count, 1);
141 gst_m3u8_media_file_unref (GstM3U8MediaFile * self)
143 g_return_if_fail (self != NULL && self->ref_count > 0);
145 if (g_atomic_int_dec_and_test (&self->ref_count)) {
146 g_free (self->title);
154 int_from_string (gchar * ptr, gchar ** endptr, gint * val)
159 g_return_val_if_fail (ptr != NULL, FALSE);
160 g_return_val_if_fail (val != NULL, FALSE);
163 ret = g_ascii_strtoll (ptr, &end, 10);
164 if ((errno == ERANGE && (ret == G_MAXINT64 || ret == G_MININT64))
165 || (errno != 0 && ret == 0)) {
166 GST_WARNING ("%s", g_strerror (errno));
170 if (ret > G_MAXINT || ret < G_MININT) {
171 GST_WARNING ("%s", g_strerror (ERANGE));
184 int64_from_string (gchar * ptr, gchar ** endptr, gint64 * val)
189 g_return_val_if_fail (ptr != NULL, FALSE);
190 g_return_val_if_fail (val != NULL, FALSE);
193 ret = g_ascii_strtoll (ptr, &end, 10);
194 if ((errno == ERANGE && (ret == G_MAXINT64 || ret == G_MININT64))
195 || (errno != 0 && ret == 0)) {
196 GST_WARNING ("%s", g_strerror (errno));
209 double_from_string (gchar * ptr, gchar ** endptr, gdouble * val)
214 g_return_val_if_fail (ptr != NULL, FALSE);
215 g_return_val_if_fail (val != NULL, FALSE);
218 ret = g_ascii_strtod (ptr, &end);
219 if ((errno == ERANGE && (ret == HUGE_VAL || ret == -HUGE_VAL))
220 || (errno != 0 && ret == 0)) {
221 GST_WARNING ("%s", g_strerror (errno));
225 if (!isfinite (ret)) {
226 GST_WARNING ("%s", g_strerror (ERANGE));
233 *val = (gdouble) ret;
239 parse_attributes (gchar ** ptr, gchar ** a, gchar ** v)
241 gchar *end = NULL, *p, *ve;
243 g_return_val_if_fail (ptr != NULL, FALSE);
244 g_return_val_if_fail (*ptr != NULL, FALSE);
245 g_return_val_if_fail (a != NULL, FALSE);
246 g_return_val_if_fail (v != NULL, FALSE);
248 /* [attribute=value,]* */
251 end = p = g_utf8_strchr (*ptr, -1, ',');
253 gchar *q = g_utf8_strchr (*ptr, -1, '"');
255 /* special case, such as CODECS="avc1.77.30, mp4a.40.2" */
256 q = g_utf8_next_char (q);
258 q = g_utf8_strchr (q, -1, '"');
261 end = p = g_utf8_strchr (q, -1, ',');
267 end = g_utf8_next_char (end);
268 } while (end && *end == ' ');
272 *v = p = g_utf8_strchr (*ptr, -1, '=');
275 *v = g_utf8_next_char (*v);
277 ve = g_utf8_next_char (*v);
279 ve = g_utf8_strchr (ve, -1, '"');
282 *v = g_utf8_next_char (*v);
285 GST_WARNING ("Cannot remove quotation marks from %s", *a);
289 GST_WARNING ("missing = after attribute");
298 gst_hls_variant_stream_compare_by_bitrate (gconstpointer a, gconstpointer b)
300 const GstHLSVariantStream *vs_a = (const GstHLSVariantStream *) a;
301 const GstHLSVariantStream *vs_b = (const GstHLSVariantStream *) b;
303 if (vs_a->bandwidth == vs_b->bandwidth)
304 return g_strcmp0 (vs_a->name, vs_b->name);
306 return vs_a->bandwidth - vs_b->bandwidth;
310 * @data: a m3u8 playlist text data, taking ownership
313 gst_m3u8_update (GstM3U8 * self, gchar * data)
316 GstClockTime duration;
318 gboolean discontinuity = FALSE;
319 gchar *current_key = NULL;
320 gboolean have_iv = FALSE;
321 guint8 iv[16] = { 0, };
322 gint64 size = -1, offset = -1;
323 gint64 mediasequence;
325 g_return_val_if_fail (self != NULL, FALSE);
326 g_return_val_if_fail (data != NULL, FALSE);
328 GST_M3U8_LOCK (self);
330 /* check if the data changed since last update */
331 if (self->last_data && g_str_equal (self->last_data, data)) {
332 GST_DEBUG ("Playlist is the same as previous one");
334 GST_M3U8_UNLOCK (self);
338 if (!g_str_has_prefix (data, "#EXTM3U")) {
339 GST_WARNING ("Data doesn't start with #EXTM3U");
341 GST_M3U8_UNLOCK (self);
345 if (g_strrstr (data, "\n#EXT-X-STREAM-INF:") != NULL) {
346 GST_WARNING ("Not a media playlist, but a master playlist!");
347 GST_M3U8_UNLOCK (self);
351 GST_TRACE ("data:\n%s", data);
353 g_free (self->last_data);
354 self->last_data = data;
356 self->current_file = NULL;
358 g_list_foreach (self->files, (GFunc) gst_m3u8_media_file_unref, NULL);
359 g_list_free (self->files);
362 self->duration = GST_CLOCK_TIME_NONE;
365 /* By default, allow caching */
366 self->allowcache = TRUE;
374 end = g_utf8_strchr (data, -1, '\n');
378 r = g_utf8_strchr (data, -1, '\r');
382 if (data[0] != '#' && data[0] != '\0') {
384 GST_LOG ("%s: got line without EXTINF, dropping", data);
388 data = uri_join (self->base_uri ? self->base_uri : self->uri, data);
390 GstM3U8MediaFile *file;
391 file = gst_m3u8_media_file_new (data, title, duration, mediasequence++);
393 /* set encryption params */
394 file->key = current_key ? g_strdup (current_key) : NULL;
397 memcpy (file->iv, iv, sizeof (iv));
399 guint8 *iv = file->iv + 12;
400 GST_WRITE_UINT32_BE (iv, file->sequence);
407 file->offset = offset;
409 GstM3U8MediaFile *prev = self->files ? self->files->data : NULL;
414 offset = prev->offset + prev->size;
416 file->offset = offset;
423 file->discont = discontinuity;
427 discontinuity = FALSE;
429 self->files = g_list_prepend (self->files, file);
432 } else if (g_str_has_prefix (data, "#EXTINF:")) {
434 if (!double_from_string (data + 8, &data, &fval)) {
435 GST_WARNING ("Can't read EXTINF duration");
438 duration = fval * (gdouble) GST_SECOND;
439 if (self->targetduration > 0 && duration > self->targetduration) {
440 GST_WARNING ("EXTINF duration (%" GST_TIME_FORMAT
441 ") > TARGETDURATION (%" GST_TIME_FORMAT ")",
442 GST_TIME_ARGS (duration), GST_TIME_ARGS (self->targetduration));
444 if (!data || *data != ',')
446 data = g_utf8_next_char (data);
449 title = g_strdup (data);
451 } else if (g_str_has_prefix (data, "#EXT-X-")) {
452 gchar *data_ext_x = data + 7;
454 /* All these entries start with #EXT-X- */
455 if (g_str_has_prefix (data_ext_x, "ENDLIST")) {
456 self->endlist = TRUE;
457 } else if (g_str_has_prefix (data_ext_x, "VERSION:")) {
458 if (int_from_string (data + 15, &data, &val))
460 } else if (g_str_has_prefix (data_ext_x, "TARGETDURATION:")) {
461 if (int_from_string (data + 22, &data, &val))
462 self->targetduration = val * GST_SECOND;
463 } else if (g_str_has_prefix (data_ext_x, "MEDIA-SEQUENCE:")) {
464 if (int_from_string (data + 22, &data, &val))
466 } else if (g_str_has_prefix (data_ext_x, "DISCONTINUITY")) {
467 discontinuity = TRUE;
468 } else if (g_str_has_prefix (data_ext_x, "PROGRAM-DATE-TIME:")) {
469 /* <YYYY-MM-DDThh:mm:ssZ> */
470 GST_DEBUG ("FIXME parse date");
471 } else if (g_str_has_prefix (data_ext_x, "ALLOW-CACHE:")) {
472 self->allowcache = g_ascii_strcasecmp (data + 19, "YES") == 0;
473 } else if (g_str_has_prefix (data_ext_x, "KEY:")) {
478 /* IV and KEY are only valid until the next #EXT-X-KEY */
480 g_free (current_key);
482 while (data && parse_attributes (&data, &a, &v)) {
483 if (g_str_equal (a, "URI")) {
485 uri_join (self->base_uri ? self->base_uri : self->uri, v);
486 } else if (g_str_equal (a, "IV")) {
490 if (strlen (ivp) < 32 + 2 || (!g_str_has_prefix (ivp, "0x")
491 && !g_str_has_prefix (ivp, "0X"))) {
492 GST_WARNING ("Can't read IV");
497 for (i = 0; i < 16; i++) {
500 h = g_ascii_xdigit_value (*ivp);
502 l = g_ascii_xdigit_value (*ivp);
504 if (h == -1 || l == -1) {
508 iv[i] = (h << 4) | l;
512 GST_WARNING ("Can't read IV");
516 } else if (g_str_equal (a, "METHOD")) {
517 if (!g_str_equal (v, "AES-128")) {
518 GST_WARNING ("Encryption method %s not supported", v);
523 } else if (g_str_has_prefix (data_ext_x, "BYTERANGE:")) {
524 gchar *v = data + 17;
526 if (int64_from_string (v, &v, &size)) {
527 if (*v == '@' && !int64_from_string (v + 1, &v, &offset))
533 GST_LOG ("Ignored line: %s", data);
536 GST_LOG ("Ignored line: %s", data);
542 data = g_utf8_next_char (end); /* skip \n */
545 g_free (current_key);
548 if (self->files == NULL) {
549 GST_ERROR ("Invalid media playlist, it does not contain any media files");
550 GST_M3U8_UNLOCK (self);
554 self->files = g_list_reverse (self->files);
556 /* calculate the start and end times of this media playlist. */
559 GstM3U8MediaFile *file;
560 GstClockTime duration = 0;
562 for (walk = self->files; walk; walk = walk->next) {
564 duration += file->duration;
565 if (file->sequence > self->highest_sequence_number) {
566 if (self->highest_sequence_number >= 0) {
567 /* if an update of the media playlist has been missed, there
568 will be a gap between self->highest_sequence_number and the
569 first sequence number in this media playlist. In this situation
570 assume that the missing fragments had a duration of
571 targetduration each */
572 self->last_file_end +=
573 (file->sequence - self->highest_sequence_number -
574 1) * self->targetduration;
576 self->last_file_end += file->duration;
577 self->highest_sequence_number = file->sequence;
580 if (GST_M3U8_IS_LIVE (self)) {
581 self->first_file_start = self->last_file_end - duration;
582 GST_DEBUG ("Live playlist range %" GST_TIME_FORMAT " -> %"
583 GST_TIME_FORMAT, GST_TIME_ARGS (self->first_file_start),
584 GST_TIME_ARGS (self->last_file_end));
586 self->duration = duration;
589 /* first-time setup */
590 if (self->files && self->sequence == -1) {
593 if (GST_M3U8_IS_LIVE (self)) {
596 file = g_list_last (self->files);
598 /* for live streams, start GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE from
599 * the end of the playlist. See section 6.3.3 of HLS draft. Note
600 * the -1, because GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE = 1 means
601 * start 1 target-duration from the end */
602 for (i = 0; i < GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE - 1 && file->prev;
606 file = g_list_first (self->files);
608 self->current_file = file;
609 self->sequence = GST_M3U8_MEDIA_FILE (file->data)->sequence;
610 self->sequence_position = 0;
611 GST_DEBUG ("first sequence: %u", (guint) self->sequence);
614 GST_LOG ("processed media playlist %s, %u fragments", self->name,
615 g_list_length (self->files));
617 GST_M3U8_UNLOCK (self);
622 /* call with M3U8_LOCK held */
624 m3u8_find_next_fragment (GstM3U8 * m3u8, gboolean forward)
626 GstM3U8MediaFile *file;
627 GList *l = m3u8->files;
633 if (file->sequence >= m3u8->sequence)
644 if (file->sequence <= m3u8->sequence)
655 gst_m3u8_get_next_fragment (GstM3U8 * m3u8, gboolean forward,
656 GstClockTime * sequence_position, gboolean * discont)
658 GstM3U8MediaFile *file = NULL;
660 g_return_val_if_fail (m3u8 != NULL, NULL);
662 GST_M3U8_LOCK (m3u8);
664 GST_DEBUG ("Looking for fragment %" G_GINT64_FORMAT, m3u8->sequence);
666 if (m3u8->sequence < 0) /* can't happen really */
669 if (m3u8->current_file == NULL)
670 m3u8->current_file = m3u8_find_next_fragment (m3u8, forward);
672 if (m3u8->current_file == NULL)
675 file = gst_m3u8_media_file_ref (m3u8->current_file->data);
677 GST_DEBUG ("Got fragment with sequence %u (current sequence %u)",
678 (guint) file->sequence, (guint) m3u8->sequence);
680 if (sequence_position)
681 *sequence_position = m3u8->sequence_position;
683 *discont = file->discont || (m3u8->sequence != file->sequence);
685 m3u8->current_file_duration = file->duration;
686 m3u8->sequence = file->sequence;
690 GST_M3U8_UNLOCK (m3u8);
696 gst_m3u8_has_next_fragment (GstM3U8 * m3u8, gboolean forward)
701 g_return_val_if_fail (m3u8 != NULL, FALSE);
703 GST_M3U8_LOCK (m3u8);
705 GST_DEBUG ("Checking next fragment %" G_GINT64_FORMAT,
706 m3u8->sequence + (forward ? 1 : -1));
708 if (m3u8->current_file) {
709 cur = m3u8->current_file;
711 cur = m3u8_find_next_fragment (m3u8, forward);
714 have_next = cur && ((forward && cur->next) || (!forward && cur->prev));
716 GST_M3U8_UNLOCK (m3u8);
721 /* call with M3U8_LOCK held */
723 m3u8_alternate_advance (GstM3U8 * m3u8, gboolean forward)
725 gint targetnum = m3u8->sequence;
727 GstM3U8MediaFile *mf;
729 /* figure out the target seqnum */
735 for (tmp = m3u8->files; tmp; tmp = tmp->next) {
736 mf = (GstM3U8MediaFile *) tmp->data;
737 if (mf->sequence == targetnum)
741 GST_WARNING ("Can't find next fragment");
744 m3u8->current_file = tmp;
745 m3u8->sequence = targetnum;
746 m3u8->current_file_duration = GST_M3U8_MEDIA_FILE (tmp->data)->duration;
750 gst_m3u8_advance_fragment (GstM3U8 * m3u8, gboolean forward)
752 GstM3U8MediaFile *file;
754 g_return_if_fail (m3u8 != NULL);
756 GST_M3U8_LOCK (m3u8);
758 GST_DEBUG ("Sequence position was %" GST_TIME_FORMAT,
759 GST_TIME_ARGS (m3u8->sequence_position));
760 if (GST_CLOCK_TIME_IS_VALID (m3u8->current_file_duration)) {
761 /* Advance our position based on the previous fragment we played */
763 m3u8->sequence_position += m3u8->current_file_duration;
764 else if (m3u8->current_file_duration < m3u8->sequence_position)
765 m3u8->sequence_position -= m3u8->current_file_duration;
767 m3u8->sequence_position = 0;
768 m3u8->current_file_duration = GST_CLOCK_TIME_NONE;
769 GST_DEBUG ("Sequence position now %" GST_TIME_FORMAT,
770 GST_TIME_ARGS (m3u8->sequence_position));
772 if (!m3u8->current_file) {
775 GST_DEBUG ("Looking for fragment %" G_GINT64_FORMAT, m3u8->sequence);
776 for (l = m3u8->files; l != NULL; l = l->next) {
777 if (GST_M3U8_MEDIA_FILE (l->data)->sequence == m3u8->sequence) {
778 m3u8->current_file = l;
782 if (m3u8->current_file == NULL) {
784 ("Could not find current fragment, trying next fragment directly");
785 m3u8_alternate_advance (m3u8, forward);
787 /* Resync sequence number if the above has failed for live streams */
788 if (m3u8->current_file == NULL && GST_M3U8_IS_LIVE (m3u8)) {
789 /* for live streams, start GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE from
790 the end of the playlist. See section 6.3.3 of HLS draft */
792 g_list_length (m3u8->files) - GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE;
793 m3u8->current_file = g_list_nth (m3u8->files, pos >= 0 ? pos : 0);
794 m3u8->current_file_duration =
795 GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->duration;
797 GST_WARNING ("Resyncing live playlist");
803 file = GST_M3U8_MEDIA_FILE (m3u8->current_file->data);
804 GST_DEBUG ("Advancing from sequence %u", (guint) file->sequence);
806 m3u8->current_file = m3u8->current_file->next;
807 if (m3u8->current_file) {
808 m3u8->sequence = GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->sequence;
810 m3u8->sequence = file->sequence + 1;
813 m3u8->current_file = m3u8->current_file->prev;
814 if (m3u8->current_file) {
815 m3u8->sequence = GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->sequence;
817 m3u8->sequence = file->sequence - 1;
820 if (m3u8->current_file) {
821 /* Store duration of the fragment we're using to update the position
822 * the next time we advance */
823 m3u8->current_file_duration =
824 GST_M3U8_MEDIA_FILE (m3u8->current_file->data)->duration;
829 GST_M3U8_UNLOCK (m3u8);
833 gst_m3u8_get_duration (GstM3U8 * m3u8)
835 GstClockTime duration = GST_CLOCK_TIME_NONE;
837 g_return_val_if_fail (m3u8 != NULL, GST_CLOCK_TIME_NONE);
839 GST_M3U8_LOCK (m3u8);
841 /* We can only get the duration for on-demand streams */
845 if (!GST_CLOCK_TIME_IS_VALID (m3u8->duration) && m3u8->files != NULL) {
849 for (f = m3u8->files; f != NULL; f = f->next)
850 m3u8->duration += GST_M3U8_MEDIA_FILE (f)->duration;
852 duration = m3u8->duration;
856 GST_M3U8_UNLOCK (m3u8);
862 gst_m3u8_get_target_duration (GstM3U8 * m3u8)
864 GstClockTime target_duration;
866 g_return_val_if_fail (m3u8 != NULL, GST_CLOCK_TIME_NONE);
868 GST_M3U8_LOCK (m3u8);
869 target_duration = m3u8->targetduration;
870 GST_M3U8_UNLOCK (m3u8);
872 return target_duration;
876 gst_m3u8_get_uri (GstM3U8 * m3u8)
880 GST_M3U8_LOCK (m3u8);
881 uri = g_strdup (m3u8->uri);
882 GST_M3U8_UNLOCK (m3u8);
888 gst_m3u8_is_live (GstM3U8 * m3u8)
892 g_return_val_if_fail (m3u8 != NULL, FALSE);
894 GST_M3U8_LOCK (m3u8);
895 is_live = GST_M3U8_IS_LIVE (m3u8);
896 GST_M3U8_UNLOCK (m3u8);
902 uri_join (const gchar * uri1, const gchar * uri2)
904 gchar *uri_copy, *tmp, *ret = NULL;
906 if (gst_uri_is_valid (uri2))
907 return g_strdup (uri2);
909 uri_copy = g_strdup (uri1);
910 if (uri2[0] != '/') {
911 /* uri2 is a relative uri2 */
912 /* look for query params */
913 tmp = g_utf8_strchr (uri_copy, -1, '?');
915 /* find last / char, ignoring query params */
916 tmp = g_utf8_strrchr (uri_copy, tmp - uri_copy, '/');
918 /* find last / char in URL */
919 tmp = g_utf8_strrchr (uri_copy, -1, '/');
922 GST_WARNING ("Can't build a valid uri_copy");
927 ret = g_strdup_printf ("%s/%s", uri_copy, uri2);
929 /* uri2 is an absolute uri2 */
930 char *scheme, *hostname;
933 /* find the : in <scheme>:// */
934 tmp = g_utf8_strchr (uri_copy, -1, ':');
936 GST_WARNING ("Can't build a valid uri_copy");
945 tmp = g_utf8_strchr (hostname, -1, '/');
949 ret = g_strdup_printf ("%s://%s%s", scheme, hostname, uri2);
958 gst_m3u8_get_seek_range (GstM3U8 * m3u8, gint64 * start, gint64 * stop)
960 GstClockTime duration = 0;
962 GstM3U8MediaFile *file;
964 guint min_distance = 0;
966 g_return_val_if_fail (m3u8 != NULL, FALSE);
968 GST_M3U8_LOCK (m3u8);
970 if (m3u8->files == NULL)
973 if (GST_M3U8_IS_LIVE (m3u8)) {
974 /* min_distance is used to make sure the seek range is never closer than
975 GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE fragments from the end of a live
976 playlist - see 6.3.3. "Playing the Playlist file" of the HLS draft */
977 min_distance = GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE;
979 count = g_list_length (m3u8->files);
981 for (walk = m3u8->files; walk && count >= min_distance; walk = walk->next) {
984 duration += file->duration;
990 *start = m3u8->first_file_start;
991 *stop = *start + duration;
995 GST_M3U8_UNLOCK (m3u8);
996 return (duration > 0);
1000 gst_hls_media_ref (GstHLSMedia * media)
1002 g_assert (media != NULL && media->ref_count > 0);
1003 g_atomic_int_add (&media->ref_count, 1);
1008 gst_hls_media_unref (GstHLSMedia * media)
1010 g_assert (media != NULL && media->ref_count > 0);
1011 if (g_atomic_int_dec_and_test (&media->ref_count)) {
1012 g_free (media->group_id);
1013 g_free (media->name);
1014 g_free (media->uri);
1019 static GstHLSMediaType
1020 gst_m3u8_get_hls_media_type_from_string (const gchar * type_name)
1022 if (strcmp (type_name, "AUDIO") == 0)
1023 return GST_HLS_MEDIA_TYPE_AUDIO;
1024 if (strcmp (type_name, "VIDEO") == 0)
1025 return GST_HLS_MEDIA_TYPE_VIDEO;
1026 if (strcmp (type_name, "SUBTITLES") == 0)
1027 return GST_HLS_MEDIA_TYPE_SUBTITLES;
1028 if (strcmp (type_name, "CLOSED_CAPTIONS") == 0)
1029 return GST_HLS_MEDIA_TYPE_CLOSED_CAPTIONS;
1031 return GST_HLS_MEDIA_TYPE_INVALID;
1034 #define GST_HLS_MEDIA_TYPE_NAME(mtype) gst_m3u8_hls_media_type_get_nick(mtype)
1035 static inline const gchar *
1036 gst_m3u8_hls_media_type_get_nick (GstHLSMediaType mtype)
1038 static const gchar *nicks[GST_HLS_N_MEDIA_TYPES] = { "audio", "video",
1039 "subtitle", "closed-captions"
1042 if (mtype < 0 || mtype >= GST_HLS_N_MEDIA_TYPES)
1045 return nicks[mtype];
1048 /* returns unquoted copy of string */
1050 gst_m3u8_unquote (const gchar * str)
1052 const gchar *start, *end;
1054 start = strchr (str, '"');
1056 return g_strdup (str);
1057 end = strchr (start + 1, '"');
1059 GST_WARNING ("Broken quoted string [%s] - can't find end quote", str);
1060 return g_strdup (start + 1);
1062 return g_strndup (start + 1, (gsize) (end - (start + 1)));
1065 static GstHLSMedia *
1066 gst_m3u8_parse_media (gchar * desc, const gchar * base_uri)
1068 GstHLSMediaType mtype = GST_HLS_MEDIA_TYPE_INVALID;
1072 media = g_new0 (GstHLSMedia, 1);
1073 media->ref_count = 1;
1074 media->playlist = gst_m3u8_new ();
1076 GST_LOG ("parsing %s", desc);
1077 while (desc != NULL && parse_attributes (&desc, &a, &v)) {
1078 if (strcmp (a, "TYPE") == 0) {
1079 media->mtype = gst_m3u8_get_hls_media_type_from_string (v);
1080 } else if (strcmp (a, "GROUP-ID") == 0) {
1081 g_free (media->group_id);
1082 media->group_id = gst_m3u8_unquote (v);
1083 } else if (strcmp (a, "NAME") == 0) {
1084 g_free (media->name);
1085 media->name = gst_m3u8_unquote (v);
1086 } else if (strcmp (a, "URI") == 0) {
1089 g_free (media->uri);
1090 uri = gst_m3u8_unquote (v);
1091 media->uri = uri_join (base_uri, uri);
1093 } else if (strcmp (a, "LANGUAGE") == 0) {
1094 g_free (media->lang);
1095 media->lang = gst_m3u8_unquote (v);
1096 } else if (strcmp (a, "DEFAULT") == 0) {
1097 media->is_default = g_ascii_strcasecmp (v, "yes") == 0;
1098 } else if (strcmp (a, "FORCED") == 0) {
1099 media->forced = g_ascii_strcasecmp (v, "yes") == 0;
1100 } else if (strcmp (a, "AUTOSELECT") == 0) {
1101 media->autoselect = g_ascii_strcasecmp (v, "yes") == 0;
1103 /* unhandled: ASSOC-LANGUAGE, INSTREAM-ID, CHARACTERISTICS */
1104 GST_FIXME ("EXT-X-MEDIA: unhandled attribute: %s = %s", a, v);
1108 if (media->mtype == GST_HLS_MEDIA_TYPE_INVALID)
1109 goto required_attributes_missing;
1111 if (media->uri == NULL)
1112 goto existing_stream;
1114 if (media->group_id == NULL || media->name == NULL)
1115 goto required_attributes_missing;
1117 if (mtype == GST_HLS_MEDIA_TYPE_CLOSED_CAPTIONS)
1120 GST_DEBUG ("media: %s, group '%s', name '%s', uri '%s', %s %s %s, lang=%s",
1121 GST_HLS_MEDIA_TYPE_NAME (media->mtype), media->group_id, media->name,
1122 media->uri, media->is_default ? "default" : "-",
1123 media->autoselect ? "autoselect" : "-",
1124 media->forced ? "forced" : "-", media->lang ? media->lang : "??");
1130 GST_WARNING ("closed captions EXT-X-MEDIA should not have URI specified");
1133 required_attributes_missing:
1135 GST_WARNING ("EXT-X-MEDIA description is missing required attributes");
1141 GST_DEBUG ("EXT-X-MEDIA without URI, describes embedded stream, skipping");
1147 gst_hls_media_unref (media);
1152 static GstHLSVariantStream *
1153 gst_hls_variant_stream_new (void)
1155 GstHLSVariantStream *stream;
1157 stream = g_new0 (GstHLSVariantStream, 1);
1158 stream->m3u8 = gst_m3u8_new ();
1159 stream->refcount = 1;
1163 GstHLSVariantStream *
1164 gst_hls_variant_stream_ref (GstHLSVariantStream * stream)
1166 g_atomic_int_inc (&stream->refcount);
1171 gst_hls_variant_stream_unref (GstHLSVariantStream * stream)
1173 if (g_atomic_int_dec_and_test (&stream->refcount)) {
1176 g_free (stream->name);
1177 g_free (stream->uri);
1178 g_free (stream->codecs);
1179 gst_m3u8_unref (stream->m3u8);
1180 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
1181 g_free (stream->media_groups[i]);
1182 g_list_free_full (stream->media[i], (GDestroyNotify) gst_hls_media_unref);
1188 static GstHLSVariantStream *
1189 find_variant_stream_by_name (GList * list, const gchar * name)
1191 for (; list != NULL; list = list->next) {
1192 GstHLSVariantStream *variant_stream = list->data;
1194 if (variant_stream->name != NULL && !strcmp (variant_stream->name, name))
1195 return variant_stream;
1200 static GstHLSVariantStream *
1201 find_variant_stream_by_uri (GList * list, const gchar * uri)
1203 for (; list != NULL; list = list->next) {
1204 GstHLSVariantStream *variant_stream = list->data;
1206 if (variant_stream->uri != NULL && !strcmp (variant_stream->uri, uri))
1207 return variant_stream;
1212 static GstHLSMasterPlaylist *
1213 gst_hls_master_playlist_new (void)
1215 GstHLSMasterPlaylist *playlist;
1217 playlist = g_new0 (GstHLSMasterPlaylist, 1);
1218 playlist->refcount = 1;
1219 playlist->is_simple = FALSE;
1225 gst_hls_master_playlist_unref (GstHLSMasterPlaylist * playlist)
1227 if (g_atomic_int_dec_and_test (&playlist->refcount)) {
1228 g_list_free_full (playlist->variants,
1229 (GDestroyNotify) gst_hls_variant_stream_unref);
1230 g_list_free_full (playlist->iframe_variants,
1231 (GDestroyNotify) gst_hls_variant_stream_unref);
1232 g_free (playlist->last_data);
1238 hls_media_name_compare_func (gconstpointer media, gconstpointer name)
1240 return strcmp (((GstHLSMedia *) media)->name, (const gchar *) name);
1243 /* Takes ownership of @data */
1244 GstHLSMasterPlaylist *
1245 gst_hls_master_playlist_new_from_data (gchar * data, const gchar * base_uri)
1247 GHashTable *media_groups[GST_HLS_N_MEDIA_TYPES] = { NULL, };
1248 GstHLSMasterPlaylist *playlist;
1249 GstHLSVariantStream *pending_stream;
1250 gchar *end, *free_data = data;
1254 if (!g_str_has_prefix (data, "#EXTM3U")) {
1255 GST_WARNING ("Data doesn't start with #EXTM3U");
1260 playlist = gst_hls_master_playlist_new ();
1262 /* store data before we modify it for parsing */
1263 playlist->last_data = g_strdup (data);
1265 GST_TRACE ("data:\n%s", data);
1267 if (strstr (data, "\n#EXTINF:") != NULL) {
1268 GST_INFO ("This is a simple media playlist, not a master playlist");
1270 pending_stream = gst_hls_variant_stream_new ();
1271 pending_stream->name = g_strdup (base_uri);
1272 pending_stream->uri = g_strdup (base_uri);
1273 gst_m3u8_set_uri (pending_stream->m3u8, base_uri, NULL, base_uri);
1274 playlist->variants = g_list_append (playlist->variants, pending_stream);
1275 playlist->default_variant = gst_hls_variant_stream_ref (pending_stream);
1276 playlist->is_simple = TRUE;
1278 if (!gst_m3u8_update (pending_stream->m3u8, data)) {
1279 GST_WARNING ("Failed to parse media playlist");
1280 gst_hls_master_playlist_unref (playlist);
1286 pending_stream = NULL;
1291 end = g_utf8_strchr (data, -1, '\n');
1295 r = g_utf8_strchr (data, -1, '\r');
1299 if (data[0] != '#' && data[0] != '\0') {
1302 if (pending_stream == NULL) {
1303 GST_LOG ("%s: got line without EXT-STREAM-INF, dropping", data);
1308 uri = uri_join (base_uri, name);
1312 pending_stream->name = g_strdup (name);
1313 pending_stream->uri = uri;
1315 if (find_variant_stream_by_name (playlist->variants, name)
1316 || find_variant_stream_by_uri (playlist->variants, uri)) {
1317 GST_DEBUG ("Already have a list with this name or URI: %s", name);
1318 gst_hls_variant_stream_unref (pending_stream);
1320 GST_INFO ("stream %s @ %u: %s", name, pending_stream->bandwidth, uri);
1321 gst_m3u8_set_uri (pending_stream->m3u8, uri, NULL, name);
1322 playlist->variants = g_list_append (playlist->variants, pending_stream);
1323 /* use first stream in the playlist as default */
1324 if (playlist->default_variant == NULL) {
1325 playlist->default_variant =
1326 gst_hls_variant_stream_ref (pending_stream);
1329 pending_stream = NULL;
1330 } else if (g_str_has_prefix (data, "#EXT-X-VERSION:")) {
1331 if (int_from_string (data + 15, &data, &val))
1332 playlist->version = val;
1333 } else if (g_str_has_prefix (data, "#EXT-X-STREAM-INF:") ||
1334 g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:")) {
1335 GstHLSVariantStream *stream;
1338 stream = gst_hls_variant_stream_new ();
1339 stream->iframe = g_str_has_prefix (data, "#EXT-X-I-FRAME-STREAM-INF:");
1340 data += stream->iframe ? 26 : 18;
1341 while (data && parse_attributes (&data, &a, &v)) {
1342 if (g_str_equal (a, "BANDWIDTH")) {
1343 if (!int_from_string (v, NULL, &stream->bandwidth))
1344 GST_WARNING ("Error while reading BANDWIDTH");
1345 } else if (g_str_equal (a, "PROGRAM-ID")) {
1346 if (!int_from_string (v, NULL, &stream->program_id))
1347 GST_WARNING ("Error while reading PROGRAM-ID");
1348 } else if (g_str_equal (a, "CODECS")) {
1349 g_free (stream->codecs);
1350 stream->codecs = g_strdup (v);
1351 } else if (g_str_equal (a, "RESOLUTION")) {
1352 if (!int_from_string (v, &v, &stream->width))
1353 GST_WARNING ("Error while reading RESOLUTION width");
1354 if (!v || *v != 'x') {
1355 GST_WARNING ("Missing height");
1357 v = g_utf8_next_char (v);
1358 if (!int_from_string (v, NULL, &stream->height))
1359 GST_WARNING ("Error while reading RESOLUTION height");
1361 } else if (stream->iframe && g_str_equal (a, "URI")) {
1362 stream->uri = uri_join (base_uri, v);
1363 if (stream->uri != NULL) {
1364 stream->name = g_strdup (stream->uri);
1365 gst_m3u8_set_uri (stream->m3u8, stream->uri, NULL, stream->name);
1367 gst_hls_variant_stream_unref (stream);
1369 } else if (g_str_equal (a, "AUDIO")) {
1370 g_free (stream->media_groups[GST_HLS_MEDIA_TYPE_AUDIO]);
1371 stream->media_groups[GST_HLS_MEDIA_TYPE_AUDIO] = gst_m3u8_unquote (v);
1372 } else if (g_str_equal (a, "SUBTITLES")) {
1373 g_free (stream->media_groups[GST_HLS_MEDIA_TYPE_SUBTITLES]);
1374 stream->media_groups[GST_HLS_MEDIA_TYPE_SUBTITLES] =
1375 gst_m3u8_unquote (v);
1376 } else if (g_str_equal (a, "VIDEO")) {
1377 g_free (stream->media_groups[GST_HLS_MEDIA_TYPE_VIDEO]);
1378 stream->media_groups[GST_HLS_MEDIA_TYPE_VIDEO] = gst_m3u8_unquote (v);
1379 } else if (g_str_equal (a, "CLOSED-CAPTIONS")) {
1380 /* closed captions will be embedded inside the video stream, ignore */
1384 if (stream->iframe) {
1385 if (find_variant_stream_by_uri (playlist->iframe_variants, stream->uri)) {
1386 GST_DEBUG ("Already have a list with this URI");
1387 gst_hls_variant_stream_unref (stream);
1389 playlist->iframe_variants =
1390 g_list_append (playlist->iframe_variants, stream);
1393 if (pending_stream != NULL) {
1394 GST_WARNING ("variant stream without uri, dropping");
1395 gst_hls_variant_stream_unref (pending_stream);
1397 pending_stream = stream;
1399 } else if (g_str_has_prefix (data, "#EXT-X-MEDIA:")) {
1403 media = gst_m3u8_parse_media (data + strlen ("#EXT-X-MEDIA:"), base_uri);
1408 if (media_groups[media->mtype] == NULL) {
1409 media_groups[media->mtype] =
1410 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1413 list = g_hash_table_lookup (media_groups[media->mtype], media->group_id);
1415 /* make sure there isn't already a media with the same name */
1416 if (!g_list_find_custom (list, media->name, hls_media_name_compare_func)) {
1417 g_hash_table_replace (media_groups[media->mtype],
1418 g_strdup (media->group_id), g_list_append (list, media));
1419 GST_INFO ("Added media %s to group %s", media->name, media->group_id);
1421 GST_WARNING (" media with name '%s' already exists in group '%s'!",
1422 media->name, media->group_id);
1423 gst_hls_media_unref (media);
1425 } else if (*data != '\0') {
1426 GST_LOG ("Ignored line: %s", data);
1432 data = g_utf8_next_char (end); /* skip \n */
1435 if (pending_stream != NULL) {
1436 GST_WARNING ("#EXT-X-STREAM-INF without uri, dropping");
1437 gst_hls_variant_stream_unref (pending_stream);
1442 /* Add alternative renditions media to variant streams */
1443 for (l = playlist->variants; l != NULL; l = l->next) {
1444 GstHLSVariantStream *stream = l->data;
1447 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
1448 if (stream->media_groups[i] != NULL && media_groups[i] != NULL) {
1449 GST_INFO ("Adding %s group '%s' to stream '%s'",
1450 GST_HLS_MEDIA_TYPE_NAME (i), stream->media_groups[i], stream->name);
1452 mlist = g_hash_table_lookup (media_groups[i], stream->media_groups[i]);
1455 GST_WARNING ("Group '%s' does not exist!", stream->media_groups[i]);
1457 while (mlist != NULL) {
1458 GstHLSMedia *media = mlist->data;
1460 GST_DEBUG (" %s media %s, uri: %s", GST_HLS_MEDIA_TYPE_NAME (i),
1461 media->name, media->uri);
1464 g_list_append (stream->media[i], gst_hls_media_ref (media));
1465 mlist = mlist->next;
1471 /* clean up our temporary alternative rendition groups hash tables */
1472 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
1473 if (media_groups[i] != NULL) {
1474 GList *groups, *mlist;
1476 groups = g_hash_table_get_keys (media_groups[i]);
1477 for (l = groups; l != NULL; l = l->next) {
1478 mlist = g_hash_table_lookup (media_groups[i], l->data);
1479 g_list_free_full (mlist, (GDestroyNotify) gst_hls_media_unref);
1481 g_list_free (groups);
1482 g_hash_table_unref (media_groups[i]);
1486 if (playlist->variants == NULL) {
1487 GST_WARNING ("Master playlist without any media playlists!");
1488 gst_hls_master_playlist_unref (playlist);
1492 /* reorder variants by bitrate */
1493 playlist->variants =
1494 g_list_sort (playlist->variants,
1495 (GCompareFunc) gst_hls_variant_stream_compare_by_bitrate);
1497 playlist->iframe_variants =
1498 g_list_sort (playlist->iframe_variants,
1499 (GCompareFunc) gst_hls_variant_stream_compare_by_bitrate);
1501 /* FIXME: restore old current_variant after master playlist update
1502 * (move into code that does that update) */
1505 gchar *top_variant_uri = NULL;
1506 gboolean iframe = FALSE;
1508 if (!self->current_variant) {
1509 top_variant_uri = GST_M3U8 (self->lists->data)->uri;
1511 top_variant_uri = GST_M3U8 (self->current_variant->data)->uri;
1512 iframe = GST_M3U8 (self->current_variant->data)->iframe;
1515 /* here we sorted the lists */
1518 playlist->current_variant =
1519 find_variant_stream_by_uri (playlist->iframe_variants,
1522 playlist->current_variant =
1523 find_variant_stream_by_uri (playlist->variants, top_variant_uri);
1527 GST_DEBUG ("parsed master playlist with %d streams and %d I-frame streams",
1528 g_list_length (playlist->variants),
1529 g_list_length (playlist->iframe_variants));
1536 gst_hls_variant_stream_is_live (GstHLSVariantStream * variant)
1540 g_return_val_if_fail (variant != NULL, FALSE);
1542 is_live = gst_m3u8_is_live (variant->m3u8);
1548 compare_media (const GstHLSMedia * a, const GstHLSMedia * b)
1550 return strcmp (a->name, b->name);
1554 gst_hls_variant_find_matching_media (GstHLSVariantStream * stream,
1555 GstHLSMedia * media)
1557 GList *mlist = stream->media[media->mtype];
1563 match = g_list_find_custom (mlist, media, (GCompareFunc) compare_media);
1570 GstHLSVariantStream *
1571 gst_hls_master_playlist_get_variant_for_bitrate (GstHLSMasterPlaylist *
1572 playlist, GstHLSVariantStream * current_variant, guint bitrate)
1574 GstHLSVariantStream *variant = current_variant;
1577 /* variant lists are sorted low to high, so iterate from highest to lowest */
1578 if (current_variant == NULL || !current_variant->iframe)
1579 l = g_list_last (playlist->variants);
1581 l = g_list_last (playlist->iframe_variants);
1585 if (variant->bandwidth <= bitrate)
1593 GstHLSVariantStream *
1594 gst_hls_master_playlist_get_matching_variant (GstHLSMasterPlaylist * playlist,
1595 GstHLSVariantStream * current_variant)
1597 if (current_variant->iframe) {
1598 return find_variant_stream_by_uri (playlist->iframe_variants,
1599 current_variant->uri);
1602 return find_variant_stream_by_uri (playlist->variants, current_variant->uri);