1 /*****************************************************************************
2 * httplive.c: HTTP Live Streaming stream filter
3 *****************************************************************************
4 * Copyright (C) 2010-2011 M2X BV
7 * Author: Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
39 #include <vlc_threads.h>
40 #include <vlc_arrays.h>
41 #include <vlc_stream.h>
43 #include <vlc_memory.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open (vlc_object_t
*);
49 static void Close(vlc_object_t
*);
52 set_category(CAT_INPUT
)
53 set_subcategory(SUBCAT_INPUT_STREAM_FILTER
)
54 set_description(N_("Http Live Streaming stream filter"))
55 set_capability("stream_filter", 20)
56 set_callbacks(Open
, Close
)
59 /*****************************************************************************
61 *****************************************************************************/
62 typedef struct segment_s
64 int sequence
; /* unique sequence number */
65 int duration
; /* segment duration (seconds) */
66 uint64_t size
; /* segment size in bytes */
67 uint64_t bandwidth
; /* bandwidth usage of segments (bits per second)*/
71 block_t
*data
; /* data */
74 typedef struct hls_stream_s
76 int id
; /* program id */
77 int version
; /* protocol version should be 1 */
78 int sequence
; /* media sequence number */
79 int duration
; /* maximum duration per segment (ms) */
80 uint64_t bandwidth
; /* bandwidth usage of segments (bits per second)*/
81 uint64_t size
; /* stream length (segment->duration * hls->bandwidth/8) */
83 vlc_array_t
*segments
; /* list of segments */
84 vlc_url_t url
; /* uri to m3u8 */
86 bool b_cache
; /* allow caching */
91 vlc_url_t m3u8
; /* M3U8 url */
92 vlc_thread_t reload
; /* HLS m3u8 reload thread */
93 vlc_thread_t thread
; /* HLS segment download thread */
98 vlc_array_t
*hls_stream
; /* bandwidth adaptation */
99 uint64_t bandwidth
; /* measured bandwidth (bits per second) */
102 struct hls_download_s
104 int stream
; /* current hls_stream */
105 int segment
; /* current segment for downloading */
106 int seek
; /* segment requested by seek (default -1) */
107 vlc_mutex_t lock_wait
; /* protect segment download counter */
108 vlc_cond_t wait
; /* some condition to wait on */
112 struct hls_playback_s
114 uint64_t offset
; /* current offset in media */
115 int stream
; /* current hls_stream */
116 int segment
; /* current segment for playback */
120 struct hls_playlist_s
122 mtime_t last
; /* playlist last loaded */
123 mtime_t wakeup
; /* next reload time */
124 int tries
; /* times it was not changed */
128 bool b_cache
; /* can cache files */
129 bool b_meta
; /* meta playlist */
130 bool b_live
; /* live stream? or vod? */
131 bool b_error
; /* parsing error */
134 /****************************************************************************
136 ****************************************************************************/
137 static int Read (stream_t
*, void *p_read
, unsigned int i_read
);
138 static int Peek (stream_t
*, const uint8_t **pp_peek
, unsigned int i_peek
);
139 static int Control(stream_t
*, int i_query
, va_list);
141 static ssize_t
read_M3U8_from_stream(stream_t
*s
, uint8_t **buffer
);
142 static ssize_t
read_M3U8_from_url(stream_t
*s
, vlc_url_t
*url
, uint8_t **buffer
);
143 static char *ReadLine(uint8_t *buffer
, uint8_t **pos
, size_t len
);
145 static int hls_Download(stream_t
*s
, segment_t
*segment
);
147 static void* hls_Thread(void *);
148 static void* hls_Reload(void *);
150 static segment_t
*segment_GetSegment(hls_stream_t
*hls
, int wanted
);
151 static void segment_Free(segment_t
*segment
);
153 /****************************************************************************
155 ****************************************************************************/
156 static const char *const ext
[] = {
157 "#EXT-X-TARGETDURATION",
158 "#EXT-X-MEDIA-SEQUENCE",
160 "#EXT-X-ALLOW-CACHE",
163 "#EXT-X-DISCONTINUITY",
167 static bool isHTTPLiveStreaming(stream_t
*s
)
169 const uint8_t *peek
, *peek_end
;
171 int64_t i_size
= stream_Peek(s
->p_source
, &peek
, 46);
175 if (strncasecmp((const char*)peek
, "#EXTM3U", 7) != 0)
178 /* Parse stream and search for
179 * EXT-X-TARGETDURATION or EXT-X-STREAM-INF tag, see
180 * http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 */
181 peek_end
= peek
+ i_size
;
182 while(peek
<= peek_end
)
186 for (unsigned int i
= 0; i
< ARRAY_SIZE(ext
); i
++)
188 char *p
= strstr((const char*)peek
, ext
[i
]);
199 /* HTTP Live Streaming */
200 static hls_stream_t
*hls_New(vlc_array_t
*hls_stream
, const int id
, const uint64_t bw
, const char *uri
)
202 hls_stream_t
*hls
= (hls_stream_t
*)malloc(sizeof(hls_stream_t
));
203 if (hls
== NULL
) return NULL
;
207 hls
->duration
= -1;/* unknown */
209 hls
->sequence
= 0; /* default is 0 */
210 hls
->version
= 1; /* default protocol version */
212 vlc_UrlParse(&hls
->url
, uri
, 0);
213 hls
->segments
= vlc_array_new();
214 vlc_array_append(hls_stream
, hls
);
215 vlc_mutex_init(&hls
->lock
);
219 static void hls_Free(hls_stream_t
*hls
)
221 vlc_mutex_destroy(&hls
->lock
);
225 for (int n
= 0; n
< vlc_array_count(hls
->segments
); n
++)
227 segment_t
*segment
= (segment_t
*)vlc_array_item_at_index(hls
->segments
, n
);
228 if (segment
) segment_Free(segment
);
230 vlc_array_destroy(hls
->segments
);
233 vlc_UrlClean(&hls
->url
);
238 static hls_stream_t
*hls_Get(vlc_array_t
*hls_stream
, const int wanted
)
240 int count
= vlc_array_count(hls_stream
);
243 if ((wanted
< 0) || (wanted
>= count
))
245 return (hls_stream_t
*) vlc_array_item_at_index(hls_stream
, wanted
);
248 static inline hls_stream_t
*hls_GetFirst(vlc_array_t
*hls_stream
)
250 return (hls_stream_t
*) hls_Get(hls_stream
, 0);
253 static hls_stream_t
*hls_GetLast(vlc_array_t
*hls_stream
)
255 int count
= vlc_array_count(hls_stream
);
259 return (hls_stream_t
*) hls_Get(hls_stream
, count
);
262 static hls_stream_t
*hls_Find(vlc_array_t
*hls_stream
, hls_stream_t
*hls_new
)
264 int count
= vlc_array_count(hls_stream
);
265 for (int n
= 0; n
< count
; n
++)
267 hls_stream_t
*hls
= vlc_array_item_at_index(hls_stream
, n
);
271 if ((hls
->id
== hls_new
->id
) &&
272 (hls
->bandwidth
== hls_new
->bandwidth
))
279 static uint64_t hls_GetStreamSize(hls_stream_t
*hls
)
281 /* NOTE: Stream size is calculated based on segment duration and
282 * HLS stream bandwidth from the .m3u8 file. If these are not correct
283 * then the deviation from exact byte size will be big and the seek/
284 * progressbar will not behave entirely as one expects. */
286 int count
= vlc_array_count(hls
->segments
);
287 for (int n
= 0; n
< count
; n
++)
289 segment_t
*segment
= segment_GetSegment(hls
, n
);
292 size
+= (segment
->duration
* (hls
->bandwidth
/ 8));
299 static segment_t
*segment_New(hls_stream_t
* hls
, const int duration
, const char *uri
)
301 segment_t
*segment
= (segment_t
*)malloc(sizeof(segment_t
));
305 segment
->duration
= duration
; /* seconds */
306 segment
->size
= 0; /* bytes */
307 segment
->sequence
= 0;
308 segment
->bandwidth
= 0;
309 vlc_UrlParse(&segment
->url
, uri
, 0);
310 segment
->data
= NULL
;
311 vlc_array_append(hls
->segments
, segment
);
312 vlc_mutex_init(&segment
->lock
);
316 static void segment_Free(segment_t
*segment
)
318 vlc_mutex_destroy(&segment
->lock
);
320 vlc_UrlClean(&segment
->url
);
322 block_Release(segment
->data
);
327 static segment_t
*segment_GetSegment(hls_stream_t
*hls
, const int wanted
)
331 int count
= vlc_array_count(hls
->segments
);
334 if ((wanted
< 0) || (wanted
>= count
))
336 return (segment_t
*) vlc_array_item_at_index(hls
->segments
, wanted
);
339 static segment_t
*segment_Find(hls_stream_t
*hls
, const int sequence
)
343 int count
= vlc_array_count(hls
->segments
);
344 if (count
<= 0) return NULL
;
345 for (int n
= 0; n
< count
; n
++)
347 segment_t
*segment
= vlc_array_item_at_index(hls
->segments
, n
);
348 if (segment
== NULL
) break;
349 if (segment
->sequence
== sequence
)
355 static int ChooseSegment(stream_t
*s
, const int current
)
357 stream_sys_t
*p_sys
= (stream_sys_t
*)s
->p_sys
;
358 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, current
);
359 if (hls
== NULL
) return 0;
361 /* Choose a segment to start which is no closer then
362 * 3 times the target duration from the end of the playlist.
367 int count
= vlc_array_count(hls
->segments
);
368 int i
= p_sys
->b_live
? count
- 1 : 0;
370 while((i
>= 0) && (i
< count
) && vlc_object_alive(s
))
372 segment_t
*segment
= segment_GetSegment(hls
, i
);
375 if (segment
->duration
> hls
->duration
)
377 msg_Err(s
, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d",
378 segment
->duration
, hls
->duration
);
381 duration
+= segment
->duration
;
382 if (duration
>= 3 * hls
->duration
)
384 /* Start point found */
385 wanted
= p_sys
->b_live
? i
: 0;
386 sequence
= segment
->sequence
;
396 msg_Info(s
, "Choose segment %d/%d (sequence=%d)", wanted
, count
, sequence
);
401 static char *parse_Attributes(const char *line
, const char *attr
)
404 char *begin
= (char *) line
;
405 char *end
= begin
+ strlen(line
);
407 /* Find start of attributes */
408 if ((p
= strchr(begin
, ':' )) == NULL
)
414 if (strncasecmp(begin
, attr
, strlen(attr
)) == 0)
416 /* <attr>=<value>[,]* */
417 p
= strchr(begin
, ',');
418 begin
+= strlen(attr
) + 1;
421 if (p
== NULL
) /* last attribute */
422 return strndup(begin
, end
- begin
);
424 return strndup(begin
, p
- begin
);
427 } while(begin
< end
);
432 static char *relative_URI(stream_t
*s
, const char *uri
, const char *path
)
434 stream_sys_t
*p_sys
= s
->p_sys
;
436 char *p
= strchr(uri
, ':');
440 if (p_sys
->m3u8
.psz_path
== NULL
)
443 char *psz_path
= strdup(p_sys
->m3u8
.psz_path
);
444 if (psz_path
== NULL
) return NULL
;
445 p
= strrchr(psz_path
, '/');
448 char *psz_uri
= NULL
;
449 if (p_sys
->m3u8
.psz_password
|| p_sys
->m3u8
.psz_username
)
451 if (asprintf(&psz_uri
, "%s://%s:%s@%s:%d%s/%s", p_sys
->m3u8
.psz_protocol
,
452 p_sys
->m3u8
.psz_username
, p_sys
->m3u8
.psz_password
,
453 p_sys
->m3u8
.psz_host
, p_sys
->m3u8
.i_port
,
454 path
? path
: psz_path
, uri
) < 0)
459 if (asprintf(&psz_uri
, "%s://%s:%d%s/%s", p_sys
->m3u8
.psz_protocol
,
460 p_sys
->m3u8
.psz_host
, p_sys
->m3u8
.i_port
,
461 path
? path
: psz_path
, uri
) < 0)
472 static char *ConstructUrl(vlc_url_t
*url
)
474 if ((url
->psz_protocol
== NULL
) ||
475 (url
->psz_path
== NULL
))
478 if (url
->i_port
<= 0)
480 if (strncmp(url
->psz_protocol
, "https", 5) == 0)
486 char *psz_url
= NULL
;
487 if (url
->psz_password
|| url
->psz_username
)
489 if (asprintf(&psz_url
, "%s://%s:%s@%s:%d%s",
491 url
->psz_username
, url
->psz_password
,
492 url
->psz_host
, url
->i_port
, url
->psz_path
) < 0)
497 if (asprintf(&psz_url
, "%s://%s:%d%s",
499 url
->psz_host
, url
->i_port
, url
->psz_path
) < 0)
506 static int parse_SegmentInformation(hls_stream_t
*hls
, char *p_read
, int *duration
)
511 /* strip of #EXTINF: */
513 char *token
= strtok_r(p_read
, ":", &p_next
);
518 token
= strtok_r(NULL
, ",", &p_next
);
523 if (hls
->version
< 3)
525 value
= strtol(token
, NULL
, 10);
535 double d
= strtod(token
, (char **) NULL
);
541 if ((d
) - ((int)d
) >= 0.5)
542 value
= ((int)d
) + 1;
547 /* Ignore the rest of the line */
552 static int parse_AddSegment(stream_t
*s
, hls_stream_t
*hls
, const int duration
, const char *uri
)
557 /* Store segment information */
558 char *psz_path
= NULL
;
559 if (hls
->url
.psz_path
!= NULL
)
561 psz_path
= strdup(hls
->url
.psz_path
);
562 if (psz_path
== NULL
)
564 char *p
= strrchr(psz_path
, '/');
567 char *psz_uri
= relative_URI(s
, uri
, psz_path
);
570 vlc_mutex_lock(&hls
->lock
);
571 segment_t
*segment
= segment_New(hls
, duration
, psz_uri
? psz_uri
: uri
);
573 segment
->sequence
= hls
->sequence
+ vlc_array_count(hls
->segments
) - 1;
574 vlc_mutex_unlock(&hls
->lock
);
577 return segment
? VLC_SUCCESS
: VLC_ENOMEM
;
580 static int parse_TargetDuration(stream_t
*s
, hls_stream_t
*hls
, char *p_read
)
585 int ret
= sscanf(p_read
, "#EXT-X-TARGETDURATION:%d", &duration
);
588 msg_Err(s
, "expected #EXT-X-TARGETDURATION:<s>");
592 hls
->duration
= duration
; /* seconds */
596 static int parse_StreamInformation(stream_t
*s
, vlc_array_t
**hls_stream
,
597 hls_stream_t
**hls
, char *p_read
, const char *uri
)
603 assert(*hls
== NULL
);
605 attr
= parse_Attributes(p_read
, "PROGRAM-ID");
608 msg_Err(s
, "#EXT-X-STREAM-INF: expected PROGRAM-ID=<value>");
614 attr
= parse_Attributes(p_read
, "BANDWIDTH");
617 msg_Err(s
, "#EXT-X-STREAM-INF: expected BANDWIDTH=<value>");
625 msg_Err(s
, "#EXT-X-STREAM-INF: bandwidth cannot be 0");
629 msg_Info(s
, "bandwidth adaption detected (program-id=%d, bandwidth=%"PRIu64
").", id
, bw
);
631 char *psz_uri
= relative_URI(s
, uri
, NULL
);
633 *hls
= hls_New(*hls_stream
, id
, bw
, psz_uri
? psz_uri
: uri
);
637 return (*hls
== NULL
) ? VLC_ENOMEM
: VLC_SUCCESS
;
640 static int parse_MediaSequence(stream_t
*s
, hls_stream_t
*hls
, char *p_read
)
645 int ret
= sscanf(p_read
, "#EXT-X-MEDIA-SEQUENCE:%d", &sequence
);
648 msg_Err(s
, "expected #EXT-X-MEDIA-SEQUENCE:<s>");
652 if (hls
->sequence
> 0)
653 msg_Err(s
, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)",
654 sequence
, hls
->sequence
);
656 hls
->sequence
= sequence
;
660 static int parse_Key(stream_t
*s
, hls_stream_t
*hls
, char *p_read
)
664 /* #EXT-X-KEY:METHOD=<method>[,URI="<URI>"][,IV=<IV>] */
665 int err
= VLC_SUCCESS
;
666 char *attr
= parse_Attributes(p_read
, "METHOD");
669 msg_Err(s
, "#EXT-X-KEY: expected METHOD=<value>");
673 if (strncasecmp(attr
, "NONE", 4) == 0)
676 char *uri
= parse_Attributes(p_read
, "URI");
679 msg_Err(s
, "#EXT-X-KEY: URI not expected");
683 /* IV is only supported in version 2 and above */
684 if (hls
->version
>= 2)
686 char *iv
= parse_Attributes(p_read
, "IV");
689 msg_Err(s
, "#EXT-X-KEY: IV not expected");
697 msg_Warn(s
, "playback of encrypted HTTP Live media is not supported.");
704 static int parse_ProgramDateTime(stream_t
*s
, hls_stream_t
*hls
, char *p_read
)
707 msg_Dbg(s
, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read
);
711 static int parse_AllowCache(stream_t
*s
, hls_stream_t
*hls
, char *p_read
)
715 char answer
[4] = "\0";
716 int ret
= sscanf(p_read
, "#EXT-X-ALLOW-CACHE:%3s", answer
);
719 msg_Err(s
, "#EXT-X-ALLOW-CACHE, ignoring ...");
723 hls
->b_cache
= (strncmp(answer
, "NO", 2) != 0);
727 static int parse_Version(stream_t
*s
, hls_stream_t
*hls
, char *p_read
)
732 int ret
= sscanf(p_read
, "#EXT-X-VERSION:%d", &version
);
735 msg_Err(s
, "#EXT-X-VERSION: no protocol version found, should be version 1.");
740 hls
->version
= version
;
741 if (hls
->version
!= 1)
743 msg_Err(s
, "#EXT-X-VERSION should be version 1 iso %d", version
);
749 static int parse_EndList(stream_t
*s
, hls_stream_t
*hls
)
753 s
->p_sys
->b_live
= false;
754 msg_Info(s
, "video on demand (vod) mode");
758 static int parse_Discontinuity(stream_t
*s
, hls_stream_t
*hls
, char *p_read
)
762 /* FIXME: Do we need to act on discontinuity ?? */
763 msg_Dbg(s
, "#EXT-X-DISCONTINUITY %s", p_read
);
767 /* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8
768 * document defines the following new tags: EXT-X-TARGETDURATION,
769 * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X-
770 * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY,
773 static int parse_M3U8(stream_t
*s
, vlc_array_t
*streams
, uint8_t *buffer
, const ssize_t len
)
775 stream_sys_t
*p_sys
= s
->p_sys
;
776 uint8_t *p_read
, *p_begin
, *p_end
;
781 msg_Dbg(s
, "parse_M3U8\n%s", buffer
);
783 p_end
= p_begin
+ len
;
785 char *line
= ReadLine(p_begin
, &p_read
, p_end
- p_begin
);
790 if (strncmp(line
, "#EXTM3U", 7) != 0)
792 msg_Err(s
, "missing #EXTM3U tag .. aborting");
800 /* What is the version ? */
802 uint8_t *p
= (uint8_t *)strstr((const char *)buffer
, "#EXT-X-VERSION:");
806 char *psz_version
= ReadLine(p
, &tmp
, p_end
- p
);
807 if (psz_version
== NULL
)
809 int ret
= sscanf((const char*)psz_version
, "#EXT-X-VERSION:%d", &version
);
812 msg_Warn(s
, "#EXT-X-VERSION: no protocol version found, assuming version 1.");
819 /* Is it a live stream ? */
820 p_sys
->b_live
= (strstr((const char *)buffer
, "#EXT-X-ENDLIST") == NULL
) ? true : false;
822 /* Is it a meta index file ? */
823 bool b_meta
= (strstr((const char *)buffer
, "#EXT-X-STREAM-INF") == NULL
) ? false : true;
825 int err
= VLC_SUCCESS
;
829 msg_Info(s
, "Meta playlist");
831 /* M3U8 Meta Index file */
834 line
= ReadLine(p_begin
, &p_read
, p_end
- p_begin
);
840 if (strncmp(line
, "#EXT-X-STREAM-INF", 17) == 0)
842 p_sys
->b_meta
= true;
843 char *uri
= ReadLine(p_begin
, &p_read
, p_end
- p_begin
);
848 hls_stream_t
*hls
= NULL
;
849 err
= parse_StreamInformation(s
, &streams
, &hls
, line
, uri
);
852 /* Download playlist file from server */
854 ssize_t len
= read_M3U8_from_url(s
, &hls
->url
, &buf
);
859 /* Parse HLS m3u8 content. */
860 err
= parse_M3U8(s
, streams
, buf
, len
);
866 hls
->version
= version
;
868 hls
->size
= hls_GetStreamSize(hls
); /* Stream size (approximate) */
877 if (p_begin
>= p_end
)
880 } while ((err
== VLC_SUCCESS
) && vlc_object_alive(s
));
885 msg_Info(s
, "%s Playlist HLS protocol version: %d", p_sys
->b_live
? "Live": "VOD", version
);
887 hls_stream_t
*hls
= NULL
;
889 hls
= hls_GetLast(streams
);
892 /* No Meta playlist used */
893 hls
= hls_New(streams
, 0, -1, NULL
);
896 /* Get TARGET-DURATION first */
897 p
= (uint8_t *)strstr((const char *)buffer
, "#EXT-X-TARGETDURATION:");
900 uint8_t *p_rest
= NULL
;
901 char *psz_duration
= ReadLine(p
, &p_rest
, p_end
- p
);
902 if (psz_duration
== NULL
)
904 err
= parse_TargetDuration(s
, hls
, psz_duration
);
910 hls
->version
= version
;
912 else return VLC_ENOMEM
;
917 int segment_duration
= -1;
921 line
= ReadLine(p_begin
, &p_read
, p_end
- p_begin
);
926 if (strncmp(line
, "#EXTINF", 7) == 0)
927 err
= parse_SegmentInformation(hls
, line
, &segment_duration
);
928 else if (strncmp(line
, "#EXT-X-TARGETDURATION", 21) == 0)
929 err
= parse_TargetDuration(s
, hls
, line
);
930 else if (strncmp(line
, "#EXT-X-MEDIA-SEQUENCE", 21) == 0)
931 err
= parse_MediaSequence(s
, hls
, line
);
932 else if (strncmp(line
, "#EXT-X-KEY", 10) == 0)
933 err
= parse_Key(s
, hls
, line
);
934 else if (strncmp(line
, "#EXT-X-PROGRAM-DATE-TIME", 24) == 0)
935 err
= parse_ProgramDateTime(s
, hls
, line
);
936 else if (strncmp(line
, "#EXT-X-ALLOW-CACHE", 18) == 0)
937 err
= parse_AllowCache(s
, hls
, line
);
938 else if (strncmp(line
, "#EXT-X-DISCONTINUITY", 20) == 0)
939 err
= parse_Discontinuity(s
, hls
, line
);
940 else if (strncmp(line
, "#EXT-X-VERSION", 14) == 0)
941 err
= parse_Version(s
, hls
, line
);
942 else if (strncmp(line
, "#EXT-X-ENDLIST", 14) == 0)
943 err
= parse_EndList(s
, hls
);
944 else if ((strncmp(line
, "#", 1) != 0) && (*line
!= '\0') )
946 err
= parse_AddSegment(s
, hls
, segment_duration
, line
);
947 segment_duration
= -1; /* reset duration */
953 if (p_begin
>= p_end
)
956 } while ((err
== VLC_SUCCESS
) && vlc_object_alive(s
));
964 static int get_HTTPLiveMetaPlaylist(stream_t
*s
, vlc_array_t
**streams
)
966 stream_sys_t
*p_sys
= s
->p_sys
;
969 /* Download new playlist file from server */
970 uint8_t *buffer
= NULL
;
971 ssize_t len
= read_M3U8_from_url(s
, &p_sys
->m3u8
, &buffer
);
975 /* Parse HLS m3u8 content. */
976 int err
= parse_M3U8(s
, *streams
, buffer
, len
);
982 /* Reload playlist */
983 static int hls_UpdatePlaylist(stream_t
*s
, hls_stream_t
*hls_new
, hls_stream_t
**hls
)
985 int count
= vlc_array_count(hls_new
->segments
);
987 msg_Info(s
, "updating hls stream (program-id=%d, bandwidth=%"PRIu64
") has %d segments",
988 hls_new
->id
, hls_new
->bandwidth
, count
);
989 for (int n
= 0; n
< count
; n
++)
991 segment_t
*p
= segment_GetSegment(hls_new
, n
);
992 if (p
== NULL
) return VLC_EGENERIC
;
994 vlc_mutex_lock(&(*hls
)->lock
);
995 segment_t
*segment
= segment_Find(*hls
, p
->sequence
);
998 /* they should be the same */
999 if ((p
->sequence
!= segment
->sequence
) ||
1000 (p
->duration
!= segment
->duration
) ||
1001 (strcmp(p
->url
.psz_path
, segment
->url
.psz_path
) != 0))
1003 msg_Err(s
, "existing segment found with different content - resetting");
1004 msg_Err(s
, "- sequence: new=%d, old=%d", p
->sequence
, segment
->sequence
);
1005 msg_Err(s
, "- duration: new=%d, old=%d", p
->duration
, segment
->duration
);
1006 msg_Err(s
, "- file: new=%s", p
->url
.psz_path
);
1007 msg_Err(s
, " old=%s", segment
->url
.psz_path
);
1009 /* Resetting content */
1010 segment
->sequence
= p
->sequence
;
1011 segment
->duration
= p
->duration
;
1012 vlc_UrlClean(&segment
->url
);
1013 vlc_UrlParse(&segment
->url
, p
->url
.psz_buffer
, 0);
1019 int last
= vlc_array_count((*hls
)->segments
) - 1;
1020 segment_t
*l
= segment_GetSegment(*hls
, last
);
1021 if (l
== NULL
) goto fail_and_unlock
;
1023 if ((l
->sequence
+ 1) != p
->sequence
)
1025 msg_Err(s
, "gap in sequence numbers found: new=%d expected %d",
1026 p
->sequence
, l
->sequence
+1);
1028 vlc_array_append((*hls
)->segments
, p
);
1029 msg_Info(s
, "- segment %d appended", p
->sequence
);
1031 vlc_mutex_unlock(&(*hls
)->lock
);
1036 vlc_mutex_unlock(&(*hls
)->lock
);
1037 return VLC_EGENERIC
;
1040 static int hls_ReloadPlaylist(stream_t
*s
)
1042 stream_sys_t
*p_sys
= s
->p_sys
;
1044 vlc_array_t
*hls_streams
= vlc_array_new();
1045 if (hls_streams
== NULL
)
1048 msg_Info(s
, "Reloading HLS live meta playlist");
1050 if (get_HTTPLiveMetaPlaylist(s
, &hls_streams
) != VLC_SUCCESS
)
1052 /* Free hls streams */
1053 for (int i
= 0; i
< vlc_array_count(hls_streams
); i
++)
1056 hls
= (hls_stream_t
*)vlc_array_item_at_index(hls_streams
, i
);
1057 if (hls
) hls_Free(hls
);
1059 vlc_array_destroy(hls_streams
);
1061 msg_Err(s
, "reloading playlist failed");
1062 return VLC_EGENERIC
;
1065 /* merge playlists */
1066 int count
= vlc_array_count(hls_streams
);
1067 for (int n
= 0; n
< count
; n
++)
1069 hls_stream_t
*hls_new
= hls_Get(hls_streams
, n
);
1070 if (hls_new
== NULL
)
1073 hls_stream_t
*hls_old
= hls_Find(p_sys
->hls_stream
, hls_new
);
1074 if (hls_old
== NULL
)
1075 { /* new hls stream - append */
1076 vlc_array_append(p_sys
->hls_stream
, hls_new
);
1077 msg_Info(s
, "new HLS stream appended (id=%d, bandwidth=%"PRIu64
")",
1078 hls_new
->id
, hls_new
->bandwidth
);
1080 else if (hls_UpdatePlaylist(s
, hls_new
, &hls_old
) != VLC_SUCCESS
)
1081 msg_Info(s
, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64
")",
1082 hls_new
->id
, hls_new
->bandwidth
);
1085 vlc_array_destroy(hls_streams
);
1089 /****************************************************************************
1091 ****************************************************************************/
1092 static int BandwidthAdaptation(stream_t
*s
, int progid
, uint64_t *bandwidth
)
1094 stream_sys_t
*p_sys
= s
->p_sys
;
1096 uint64_t bw
= *bandwidth
;
1097 uint64_t bw_candidate
= 0;
1099 int count
= vlc_array_count(p_sys
->hls_stream
);
1100 for (int n
= 0; n
< count
; n
++)
1102 /* Select best bandwidth match */
1103 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, n
);
1104 if (hls
== NULL
) break;
1106 /* only consider streams with the same PROGRAM-ID */
1107 if (hls
->id
== progid
)
1109 if ((bw
>= hls
->bandwidth
) && (bw_candidate
< hls
->bandwidth
))
1111 msg_Dbg(s
, "candidate %d bandwidth (bits/s) %"PRIu64
" >= %"PRIu64
,
1112 n
, bw
, hls
->bandwidth
); /* bits / s */
1113 bw_candidate
= hls
->bandwidth
;
1114 candidate
= n
; /* possible candidate */
1118 *bandwidth
= bw_candidate
;
1122 static int Download(stream_t
*s
, hls_stream_t
*hls
, segment_t
*segment
, int *cur_stream
)
1124 stream_sys_t
*p_sys
= s
->p_sys
;
1129 vlc_mutex_lock(&segment
->lock
);
1130 if (segment
->data
!= NULL
)
1132 /* Segment already downloaded */
1133 vlc_mutex_unlock(&segment
->lock
);
1137 /* sanity check - can we download this segment on time? */
1138 if ((p_sys
->bandwidth
> 0) && (hls
->bandwidth
> 0))
1140 uint64_t size
= (segment
->duration
* hls
->bandwidth
); /* bits */
1141 int estimated
= (int)(size
/ p_sys
->bandwidth
);
1142 if (estimated
> segment
->duration
)
1144 msg_Warn(s
,"downloading of segment %d takes %ds, which is longer then its playback (%ds)",
1145 segment
->sequence
, estimated
, segment
->duration
);
1149 mtime_t start
= mdate();
1150 if (hls_Download(s
, segment
) != VLC_SUCCESS
)
1152 vlc_mutex_unlock(&segment
->lock
);
1153 return VLC_EGENERIC
;
1155 mtime_t duration
= mdate() - start
;
1157 vlc_mutex_unlock(&segment
->lock
);
1159 msg_Info(s
, "downloaded segment %d from stream %d",
1160 segment
->sequence
, *cur_stream
);
1162 /* check for division by zero */
1163 double ms
= (double)duration
/ 1000.0; /* ms */
1167 uint64_t bw
= ((double)(segment
->size
* 8) / ms
) * 1000; /* bits / s */
1168 p_sys
->bandwidth
= bw
;
1169 if (p_sys
->b_meta
&& (hls
->bandwidth
!= bw
))
1171 int newstream
= BandwidthAdaptation(s
, hls
->id
, &bw
);
1173 /* FIXME: we need an average here */
1174 if ((newstream
>= 0) && (newstream
!= *cur_stream
))
1176 msg_Info(s
, "detected %s bandwidth (%"PRIu64
") stream",
1177 (bw
>= hls
->bandwidth
) ? "faster" : "lower", bw
);
1178 *cur_stream
= newstream
;
1184 static void* hls_Thread(void *p_this
)
1186 stream_t
*s
= (stream_t
*)p_this
;
1187 stream_sys_t
*p_sys
= s
->p_sys
;
1189 int canc
= vlc_savecancel();
1191 while (vlc_object_alive(s
))
1193 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, p_sys
->download
.stream
);
1196 /* Sliding window (~60 seconds worth of movie) */
1197 vlc_mutex_lock(&hls
->lock
);
1198 int count
= vlc_array_count(hls
->segments
);
1199 vlc_mutex_unlock(&hls
->lock
);
1201 /* Is there a new segment to process? */
1202 if ((!p_sys
->b_live
&& (p_sys
->playback
.segment
< (count
- 6))) ||
1203 (p_sys
->download
.segment
>= count
))
1206 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
1207 while (((p_sys
->download
.segment
- p_sys
->playback
.segment
> 6) ||
1208 (p_sys
->download
.segment
>= count
)) &&
1209 (p_sys
->download
.seek
== -1))
1211 vlc_cond_wait(&p_sys
->download
.wait
, &p_sys
->download
.lock_wait
);
1212 if (p_sys
->b_live
/*&& (mdate() >= p_sys->playlist.wakeup)*/)
1214 if (!vlc_object_alive(s
))
1218 if (p_sys
->download
.seek
>= 0)
1220 p_sys
->download
.segment
= p_sys
->download
.seek
;
1221 p_sys
->download
.seek
= -1;
1223 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
1226 if (!vlc_object_alive(s
)) break;
1228 vlc_mutex_lock(&hls
->lock
);
1229 segment_t
*segment
= segment_GetSegment(hls
, p_sys
->download
.segment
);
1230 vlc_mutex_unlock(&hls
->lock
);
1232 if ((segment
!= NULL
) &&
1233 (Download(s
, hls
, segment
, &p_sys
->download
.stream
) != VLC_SUCCESS
))
1235 if (!vlc_object_alive(s
)) break;
1239 p_sys
->b_error
= true;
1244 /* download succeeded */
1245 /* determine next segment to download */
1246 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
1247 if (p_sys
->download
.seek
>= 0)
1249 p_sys
->download
.segment
= p_sys
->download
.seek
;
1250 p_sys
->download
.seek
= -1;
1252 else if (p_sys
->download
.segment
< count
)
1253 p_sys
->download
.segment
++;
1254 vlc_cond_signal(&p_sys
->download
.wait
);
1255 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
1258 vlc_restorecancel(canc
);
1262 static void* hls_Reload(void *p_this
)
1264 stream_t
*s
= (stream_t
*)p_this
;
1265 stream_sys_t
*p_sys
= s
->p_sys
;
1267 assert(p_sys
->b_live
);
1269 int canc
= vlc_savecancel();
1271 while (vlc_object_alive(s
))
1274 mtime_t now
= mdate();
1275 if (now
>= p_sys
->playlist
.wakeup
)
1277 /* reload the m3u8 */
1278 if (hls_ReloadPlaylist(s
) != VLC_SUCCESS
)
1280 /* No change in playlist, then backoff */
1281 p_sys
->playlist
.tries
++;
1282 if (p_sys
->playlist
.tries
== 1) wait
= 0.5;
1283 else if (p_sys
->playlist
.tries
== 2) wait
= 1;
1284 else if (p_sys
->playlist
.tries
>= 3) wait
= 3;
1286 else p_sys
->playlist
.tries
= 0;
1288 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, p_sys
->download
.stream
);
1291 /* determine next time to update playlist */
1292 p_sys
->playlist
.last
= now
;
1293 p_sys
->playlist
.wakeup
= now
+ ((mtime_t
)(hls
->duration
* wait
)
1294 * (mtime_t
)1000000);
1297 mwait(p_sys
->playlist
.wakeup
);
1300 vlc_restorecancel(canc
);
1304 static int Prefetch(stream_t
*s
, int *current
)
1306 stream_sys_t
*p_sys
= s
->p_sys
;
1309 /* Try to pick best matching stream */;
1313 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, *current
);
1315 return VLC_EGENERIC
;
1317 segment_t
*segment
= segment_GetSegment(hls
, p_sys
->download
.segment
);
1318 if (segment
== NULL
)
1319 return VLC_EGENERIC
;
1321 if (Download(s
, hls
, segment
, current
) != VLC_SUCCESS
)
1322 return VLC_EGENERIC
;
1324 /* Found better bandwidth match, try again */
1325 if (*current
!= stream
)
1328 /* Download first 2 segments of this HLS stream */
1330 for (int i
= 0; i
< 2; i
++)
1332 segment_t
*segment
= segment_GetSegment(hls
, p_sys
->download
.segment
);
1333 if (segment
== NULL
)
1334 return VLC_EGENERIC
;
1338 p_sys
->download
.segment
++;
1342 if (Download(s
, hls
, segment
, current
) != VLC_SUCCESS
)
1343 return VLC_EGENERIC
;
1345 p_sys
->download
.segment
++;
1347 /* adapt bandwidth? */
1348 if (*current
!= stream
)
1350 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, *current
);
1352 return VLC_EGENERIC
;
1361 /****************************************************************************
1363 ****************************************************************************/
1364 static int hls_Download(stream_t
*s
, segment_t
*segment
)
1369 char *psz_url
= ConstructUrl(&segment
->url
);
1370 if (psz_url
== NULL
)
1373 stream_t
*p_ts
= stream_UrlNew(s
, psz_url
);
1376 return VLC_EGENERIC
;
1378 segment
->size
= stream_Size(p_ts
);
1379 assert(segment
->size
> 0);
1381 segment
->data
= block_Alloc(segment
->size
);
1382 if (segment
->data
== NULL
)
1384 stream_Delete(p_ts
);
1388 assert(segment
->data
->i_buffer
== segment
->size
);
1390 ssize_t length
= 0, curlen
= 0;
1394 size
= stream_Size(p_ts
);
1395 if (size
> segment
->size
)
1397 msg_Dbg(s
, "size changed %"PRIu64
, segment
->size
);
1398 segment
->data
= block_Realloc(segment
->data
, 0, size
);
1399 if (segment
->data
== NULL
)
1401 stream_Delete(p_ts
);
1404 segment
->size
= size
;
1405 assert(segment
->data
->i_buffer
== segment
->size
);
1407 length
= stream_Read(p_ts
, segment
->data
->p_buffer
+ curlen
, segment
->size
- curlen
);
1411 } while (vlc_object_alive(s
));
1413 stream_Delete(p_ts
);
1417 /* Read M3U8 file */
1418 static ssize_t
read_M3U8_from_stream(stream_t
*s
, uint8_t **buffer
)
1420 int64_t total_bytes
= 0;
1421 int64_t total_allocated
= 0;
1429 bytes
= stream_Read(s
, buf
, sizeof(buf
));
1435 if ( (total_bytes
+ bytes
+ 1) > total_allocated
)
1437 if (total_allocated
)
1438 total_allocated
*= 2;
1440 total_allocated
= __MIN(bytes
+1, sizeof(buf
));
1442 p
= realloc_or_free(p
, total_allocated
);
1447 memcpy(p
+total_bytes
, buf
, bytes
);
1448 total_bytes
+= bytes
;
1451 if (total_allocated
== 0)
1452 return VLC_EGENERIC
;
1454 p
[total_bytes
] = '\0';
1460 static ssize_t
read_M3U8_from_url(stream_t
*s
, vlc_url_t
*url
, uint8_t **buffer
)
1462 assert(*buffer
== NULL
);
1465 char *psz_url
= ConstructUrl(url
);
1466 if (psz_url
== NULL
)
1469 stream_t
*p_m3u8
= stream_UrlNew(s
, psz_url
);
1472 return VLC_EGENERIC
;
1474 ssize_t size
= read_M3U8_from_stream(p_m3u8
, buffer
);
1475 stream_Delete(p_m3u8
);
1480 static char *ReadLine(uint8_t *buffer
, uint8_t **pos
, const size_t len
)
1485 uint8_t *begin
= buffer
;
1487 uint8_t *end
= p
+ len
;
1491 if ((*p
== '\n') || (*p
== '\0'))
1496 /* copy line excluding \n or \0 */
1497 line
= strndup((char *)begin
, p
- begin
);
1503 /* next pass start after \n */
1511 /****************************************************************************
1513 ****************************************************************************/
1514 static int Open(vlc_object_t
*p_this
)
1516 stream_t
*s
= (stream_t
*)p_this
;
1517 stream_sys_t
*p_sys
;
1519 if (!isHTTPLiveStreaming(s
))
1520 return VLC_EGENERIC
;
1522 msg_Info(p_this
, "HTTP Live Streaming (%s)", s
->psz_path
);
1525 s
->p_sys
= p_sys
= calloc(1, sizeof(*p_sys
));
1529 char *psz_uri
= NULL
;
1530 if (asprintf(&psz_uri
,"%s://%s", s
->psz_access
, s
->psz_path
) < 0)
1535 vlc_UrlParse(&p_sys
->m3u8
, psz_uri
, 0);
1538 p_sys
->bandwidth
= -1;
1539 p_sys
->b_live
= true;
1540 p_sys
->b_meta
= false;
1541 p_sys
->b_error
= false;
1543 p_sys
->hls_stream
= vlc_array_new();
1544 if (p_sys
->hls_stream
== NULL
)
1546 vlc_UrlClean(&p_sys
->m3u8
);
1554 s
->pf_control
= Control
;
1556 /* Parse HLS m3u8 content. */
1557 uint8_t *buffer
= NULL
;
1558 ssize_t len
= read_M3U8_from_stream(s
->p_source
, &buffer
);
1561 if (parse_M3U8(s
, p_sys
->hls_stream
, buffer
, len
) != VLC_SUCCESS
)
1568 /* Choose first HLS stream to start with */
1569 int current
= p_sys
->playback
.stream
= 0;
1570 p_sys
->playback
.segment
= p_sys
->download
.segment
= ChooseSegment(s
, current
);
1572 if (p_sys
->b_live
&& (p_sys
->playback
.segment
< 0))
1574 msg_Warn(s
, "less data then 3 times 'target duration' available for live playback, playback may stall");
1577 if (Prefetch(s
, ¤t
) != VLC_SUCCESS
)
1579 msg_Err(s
, "fetching first segment failed.");
1584 p_sys
->download
.stream
= current
;
1585 p_sys
->playback
.stream
= current
;
1586 p_sys
->download
.seek
= -1;
1588 vlc_mutex_init(&p_sys
->download
.lock_wait
);
1589 vlc_cond_init(&p_sys
->download
.wait
);
1591 /* Initialize HLS live stream */
1594 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, current
);
1595 p_sys
->playlist
.last
= mdate();
1596 p_sys
->playlist
.wakeup
= p_sys
->playlist
.last
+
1597 ((mtime_t
)hls
->duration
* UINT64_C(1000000));
1599 if (vlc_clone(&p_sys
->reload
, hls_Reload
, s
, VLC_THREAD_PRIORITY_LOW
))
1605 if (vlc_clone(&p_sys
->thread
, hls_Thread
, s
, VLC_THREAD_PRIORITY_INPUT
))
1608 vlc_join(p_sys
->reload
, NULL
);
1615 vlc_mutex_destroy(&p_sys
->download
.lock_wait
);
1616 vlc_cond_destroy(&p_sys
->download
.wait
);
1619 /* Free hls streams */
1620 for (int i
= 0; i
< vlc_array_count(p_sys
->hls_stream
); i
++)
1623 hls
= (hls_stream_t
*)vlc_array_item_at_index(p_sys
->hls_stream
, i
);
1624 if (hls
) hls_Free(hls
);
1626 vlc_array_destroy(p_sys
->hls_stream
);
1629 vlc_UrlClean(&p_sys
->m3u8
);
1631 return VLC_EGENERIC
;
1634 /****************************************************************************
1636 ****************************************************************************/
1637 static void Close(vlc_object_t
*p_this
)
1639 stream_t
*s
= (stream_t
*)p_this
;
1640 stream_sys_t
*p_sys
= s
->p_sys
;
1642 assert(p_sys
->hls_stream
);
1645 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
1646 vlc_cond_signal(&p_sys
->download
.wait
);
1647 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
1651 vlc_join(p_sys
->reload
, NULL
);
1652 vlc_join(p_sys
->thread
, NULL
);
1653 vlc_mutex_destroy(&p_sys
->download
.lock_wait
);
1654 vlc_cond_destroy(&p_sys
->download
.wait
);
1656 /* Free hls streams */
1657 for (int i
= 0; i
< vlc_array_count(p_sys
->hls_stream
); i
++)
1660 hls
= (hls_stream_t
*)vlc_array_item_at_index(p_sys
->hls_stream
, i
);
1661 if (hls
) hls_Free(hls
);
1663 vlc_array_destroy(p_sys
->hls_stream
);
1666 vlc_UrlClean(&p_sys
->m3u8
);
1668 block_Release (p_sys
->peeked
);
1672 /****************************************************************************
1673 * Stream filters functions
1674 ****************************************************************************/
1675 static segment_t
*GetSegment(stream_t
*s
)
1677 stream_sys_t
*p_sys
= s
->p_sys
;
1678 segment_t
*segment
= NULL
;
1680 /* Is this segment of the current HLS stream ready? */
1681 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, p_sys
->playback
.stream
);
1684 vlc_mutex_lock(&hls
->lock
);
1685 segment
= segment_GetSegment(hls
, p_sys
->playback
.segment
);
1686 if (segment
!= NULL
)
1688 /* This segment is ready? */
1689 if (segment
->data
!= NULL
)
1691 p_sys
->b_cache
= hls
->b_cache
;
1692 vlc_mutex_unlock(&hls
->lock
);
1696 vlc_mutex_unlock(&hls
->lock
);
1699 /* Was the HLS stream changed to another bitrate? */
1702 while(vlc_object_alive(s
))
1704 /* Is the next segment ready */
1705 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, i_stream
);
1709 vlc_mutex_lock(&hls
->lock
);
1710 segment
= segment_GetSegment(hls
, p_sys
->playback
.segment
);
1711 if (segment
== NULL
)
1713 vlc_mutex_unlock(&hls
->lock
);
1717 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
1718 int i_segment
= p_sys
->download
.segment
;
1719 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
1721 /* This segment is ready? */
1722 if ((segment
->data
!= NULL
) &&
1723 (p_sys
->playback
.segment
< i_segment
))
1725 p_sys
->playback
.stream
= i_stream
;
1726 p_sys
->b_cache
= hls
->b_cache
;
1727 vlc_mutex_unlock(&hls
->lock
);
1730 vlc_mutex_unlock(&hls
->lock
);
1735 /* Was the stream changed to another bitrate? */
1737 if (i_stream
>= vlc_array_count(p_sys
->hls_stream
))
1745 if (segment
->data
->i_buffer
== 0)
1747 vlc_mutex_lock(&hls
->lock
);
1748 int count
= vlc_array_count(hls
->segments
);
1749 vlc_mutex_unlock(&hls
->lock
);
1751 if ((p_sys
->download
.segment
- p_sys
->playback
.segment
== 0) &&
1752 ((count
!= p_sys
->download
.segment
) || p_sys
->b_live
))
1753 msg_Err(s
, "playback will stall");
1754 else if ((p_sys
->download
.segment
- p_sys
->playback
.segment
< 3) &&
1755 ((count
!= p_sys
->download
.segment
) || p_sys
->b_live
))
1756 msg_Warn(s
, "playback in danger of stalling");
1761 static ssize_t
hls_Read(stream_t
*s
, uint8_t *p_read
, unsigned int i_read
)
1763 stream_sys_t
*p_sys
= s
->p_sys
;
1768 /* Determine next segment to read. If this is a meta playlist and
1769 * bandwidth conditions changed, then the stream might have switched
1770 * to another bandwidth. */
1771 segment_t
*segment
= GetSegment(s
);
1772 if (segment
== NULL
)
1775 vlc_mutex_lock(&segment
->lock
);
1776 if (segment
->data
->i_buffer
== 0)
1778 if (!p_sys
->b_cache
|| p_sys
->b_live
)
1780 block_Release(segment
->data
);
1781 segment
->data
= NULL
;
1784 { /* reset playback pointer to start of buffer */
1785 uint64_t size
= segment
->size
- segment
->data
->i_buffer
;
1788 segment
->data
->i_buffer
+= size
;
1789 segment
->data
->p_buffer
-= size
;
1792 p_sys
->playback
.segment
++;
1793 vlc_mutex_unlock(&segment
->lock
);
1795 /* signal download thread */
1796 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
1797 vlc_cond_signal(&p_sys
->download
.wait
);
1798 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
1802 if (segment
->size
== segment
->data
->i_buffer
)
1803 msg_Info(s
, "playing segment %d from stream %d",
1804 segment
->sequence
, p_sys
->playback
.stream
);
1807 if (i_read
<= segment
->data
->i_buffer
)
1809 else if (i_read
> segment
->data
->i_buffer
)
1810 len
= segment
->data
->i_buffer
;
1814 memcpy(p_read
+ copied
, segment
->data
->p_buffer
, len
);
1815 segment
->data
->i_buffer
-= len
;
1816 segment
->data
->p_buffer
+= len
;
1820 vlc_mutex_unlock(&segment
->lock
);
1822 } while ((i_read
> 0) && vlc_object_alive(s
));
1827 static int Read(stream_t
*s
, void *buffer
, unsigned int i_read
)
1829 stream_sys_t
*p_sys
= s
->p_sys
;
1832 assert(p_sys
->hls_stream
);
1839 /* caller skips data, get big enough buffer */
1840 msg_Warn(s
, "buffer is NULL (allocate %d)", i_read
);
1841 buffer
= calloc(1, i_read
);
1843 return 0; /* NO MEMORY left*/
1846 length
= hls_Read(s
, (uint8_t*) buffer
, i_read
);
1850 p_sys
->playback
.offset
+= length
;
1854 static int Peek(stream_t
*s
, const uint8_t **pp_peek
, unsigned int i_peek
)
1856 stream_sys_t
*p_sys
= s
->p_sys
;
1858 unsigned int len
= i_peek
;
1860 segment
= GetSegment(s
);
1861 if (segment
== NULL
)
1863 msg_Err(s
, "segment %d should have been available (stream %d)",
1864 p_sys
->playback
.segment
, p_sys
->playback
.stream
);
1865 return 0; /* eof? */
1868 vlc_mutex_lock(&segment
->lock
);
1870 size_t i_buff
= segment
->data
->i_buffer
;
1871 uint8_t *p_buff
= segment
->data
->p_buffer
;
1873 if (i_peek
< i_buff
)
1876 vlc_mutex_unlock(&segment
->lock
);
1880 else /* This will seldom be run */
1882 /* remember segment to read */
1883 int peek_segment
= p_sys
->playback
.segment
;
1885 segment_t
*nsegment
;
1886 p_sys
->playback
.segment
++;
1887 block_t
*peeked
= p_sys
->peeked
;
1890 peeked
= block_Alloc (i_peek
);
1891 else if (peeked
->i_buffer
< i_peek
)
1892 peeked
= block_Realloc (peeked
, 0, i_peek
);
1896 memcpy(peeked
->p_buffer
, p_buff
, i_buff
);
1899 vlc_mutex_unlock(&segment
->lock
);
1901 i_buff
= peeked
->i_buffer
;
1902 p_buff
= peeked
->p_buffer
;
1905 while ((curlen
< i_peek
) && vlc_object_alive(s
))
1907 nsegment
= GetSegment(s
);
1908 if (nsegment
== NULL
)
1910 msg_Err(s
, "segment %d should have been available (stream %d)",
1911 p_sys
->playback
.segment
, p_sys
->playback
.stream
);
1912 /* restore segment to read */
1913 p_sys
->playback
.segment
= peek_segment
;
1914 return curlen
; /* eof? */
1917 vlc_mutex_lock(&nsegment
->lock
);
1919 if (len
< nsegment
->data
->i_buffer
)
1921 memcpy(p_buff
+ curlen
, nsegment
->data
->p_buffer
, len
);
1926 size_t i_nbuff
= nsegment
->data
->i_buffer
;
1927 memcpy(p_buff
+ curlen
, nsegment
->data
->p_buffer
, i_nbuff
);
1931 p_sys
->playback
.segment
++;
1934 vlc_mutex_unlock(&nsegment
->lock
);
1937 /* restore segment to read */
1938 p_sys
->playback
.segment
= peek_segment
;
1943 static bool hls_MaySeek(stream_t
*s
)
1945 stream_sys_t
*p_sys
= s
->p_sys
;
1947 if (p_sys
->hls_stream
== NULL
)
1950 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, p_sys
->playback
.stream
);
1951 if (hls
== NULL
) return false;
1955 vlc_mutex_lock(&hls
->lock
);
1956 int count
= vlc_array_count(hls
->segments
);
1957 vlc_mutex_unlock(&hls
->lock
);
1959 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
1960 bool may_seek
= (p_sys
->download
.segment
< (count
- 2));
1961 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
1967 static uint64_t GetStreamSize(stream_t
*s
)
1969 stream_sys_t
*p_sys
= s
->p_sys
;
1974 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, p_sys
->playback
.stream
);
1975 if (hls
== NULL
) return 0;
1977 vlc_mutex_lock(&hls
->lock
);
1978 uint64_t size
= hls
->size
;
1979 vlc_mutex_unlock(&hls
->lock
);
1984 static int segment_Seek(stream_t
*s
, const uint64_t pos
)
1986 stream_sys_t
*p_sys
= s
->p_sys
;
1988 hls_stream_t
*hls
= hls_Get(p_sys
->hls_stream
, p_sys
->playback
.stream
);
1990 return VLC_EGENERIC
;
1992 vlc_mutex_lock(&hls
->lock
);
1994 bool b_found
= false;
1995 uint64_t length
= 0;
1996 uint64_t size
= hls
->size
;
1997 int count
= vlc_array_count(hls
->segments
);
1999 for (int n
= 0; n
< count
; n
++)
2001 segment_t
*segment
= vlc_array_item_at_index(hls
->segments
, n
);
2002 if (segment
== NULL
)
2004 vlc_mutex_unlock(&hls
->lock
);
2005 return VLC_EGENERIC
;
2008 vlc_mutex_lock(&segment
->lock
);
2009 length
+= segment
->duration
* (hls
->bandwidth
/8);
2010 vlc_mutex_unlock(&segment
->lock
);
2012 if (!b_found
&& (pos
<= length
))
2016 p_sys
->playback
.segment
= n
;
2020 /* Do not search in last 3 segments */
2021 vlc_mutex_unlock(&hls
->lock
);
2022 return VLC_EGENERIC
;
2027 if (!b_found
&& (pos
>= size
))
2029 p_sys
->playback
.segment
= count
- 1;
2036 /* restore segment to start position */
2037 segment_t
*segment
= segment_GetSegment(hls
, p_sys
->playback
.segment
);
2038 if (segment
== NULL
)
2040 vlc_mutex_unlock(&hls
->lock
);
2041 return VLC_EGENERIC
;
2044 vlc_mutex_lock(&segment
->lock
);
2047 uint64_t size
= segment
->size
-segment
->data
->i_buffer
;
2050 segment
->data
->i_buffer
+= size
;
2051 segment
->data
->p_buffer
-= size
;
2054 vlc_mutex_unlock(&segment
->lock
);
2056 /* start download at current playback segment */
2057 vlc_mutex_unlock(&hls
->lock
);
2059 /* Wake up download thread */
2060 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
2061 p_sys
->download
.seek
= p_sys
->playback
.segment
;
2062 vlc_cond_signal(&p_sys
->download
.wait
);
2063 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
2065 /* Wait for download to be finished */
2066 vlc_mutex_lock(&p_sys
->download
.lock_wait
);
2067 msg_Info(s
, "seek to segment %d", p_sys
->playback
.segment
);
2068 while (((p_sys
->download
.seek
!= -1) ||
2069 (p_sys
->download
.segment
- p_sys
->playback
.segment
< 3)) &&
2070 (p_sys
->download
.segment
< (count
- 6)))
2072 vlc_cond_wait(&p_sys
->download
.wait
, &p_sys
->download
.lock_wait
);
2073 if (!vlc_object_alive(s
) || s
->b_error
) break;
2075 vlc_mutex_unlock(&p_sys
->download
.lock_wait
);
2079 vlc_mutex_unlock(&hls
->lock
);
2081 return b_found
? VLC_SUCCESS
: VLC_EGENERIC
;
2084 static int Control(stream_t
*s
, int i_query
, va_list args
)
2086 stream_sys_t
*p_sys
= s
->p_sys
;
2090 case STREAM_CAN_SEEK
:
2091 case STREAM_CAN_FASTSEEK
:
2092 *(va_arg (args
, bool *)) = hls_MaySeek(s
);
2094 case STREAM_GET_POSITION
:
2095 *(va_arg (args
, uint64_t *)) = p_sys
->playback
.offset
;
2097 case STREAM_SET_POSITION
:
2100 uint64_t pos
= (uint64_t)va_arg(args
, uint64_t);
2101 if (segment_Seek(s
, pos
) == VLC_SUCCESS
)
2103 p_sys
->playback
.offset
= pos
;
2107 return VLC_EGENERIC
;
2108 case STREAM_GET_SIZE
:
2109 *(va_arg (args
, uint64_t *)) = GetStreamSize(s
);
2112 return VLC_EGENERIC
;