1 /*****************************************************************************
2 * httplive.c: HTTP Live Streaming stream filter
3 *****************************************************************************
4 * Copyright (C) 2010 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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
36 #include <vlc_threads.h>
37 #include <vlc_arrays.h>
38 #include <vlc_stream.h>
41 #include <vlc_modules.h>
42 #include <vlc_access.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open (vlc_object_t *);
48 static void Close(vlc_object_t *);
51 set_category(CAT_INPUT)
52 set_subcategory(SUBCAT_INPUT_STREAM_FILTER)
53 set_description(N_("Http Live Streaming stream filter"))
54 set_capability("stream_filter", 20)
55 set_callbacks(Open, Close)
58 /*****************************************************************************
60 *****************************************************************************/
61 typedef struct segment_s
63 int sequence; /* unique sequence number */
64 int length; /* segment duration (ms) */
65 uint64_t size; /* segment size in bytes */
69 block_t *data; /* data */
72 typedef struct hls_stream_s
74 int id; /* program id */
75 int version; /* protocol version should be 1 */
76 int sequence; /* media sequence number */
77 int duration; /* maximum duration per segment (ms) */
78 uint64_t bandwidth; /* bandwidth usage of segments (kbps)*/
80 vlc_array_t *segments; /* list of segments */
81 int segment; /* current segment downloading */
83 vlc_url_t url; /* uri to m3u8 */
85 bool b_cache; /* allow caching */
93 int current; /* current hls_stream */
94 vlc_array_t *hls_stream;/* bandwidth adaptation */
101 access_t *p_access; /* HTTP access input */
104 hls_thread_t *thread;
105 vlc_array_t *hls_stream;/* bandwidth adaptation */
108 uint64_t offset; /* current offset in media */
109 int current; /* current hls_stream */
110 int segment; /* current segment for playback */
113 mtime_t last; /* playlist last loaded */
114 mtime_t wakeup; /* next reload time */
115 int tries; /* times it was not changed */
118 bool b_cache; /* can cache files */
119 bool b_meta; /* meta playlist */
120 bool b_live; /* live stream? or vod? */
121 bool b_error; /* parsing error */
124 /****************************************************************************
126 ****************************************************************************/
127 static int Read (stream_t *, void *p_read, unsigned int i_read);
128 static int Peek (stream_t *, const uint8_t **pp_peek, unsigned int i_peek);
129 static int Control(stream_t *, int i_query, va_list);
131 static int AccessOpen(stream_t *s, vlc_url_t *url);
132 static void AccessClose(stream_t *s);
133 static char *AccessReadLine(access_t *p_access, uint8_t *psz_tmp, size_t i_len);
134 static int AccessDownload(stream_t *s, segment_t *segment);
136 static void* hls_Thread(vlc_object_t *);
137 static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls);
139 static void segment_Free(segment_t *segment);
141 /****************************************************************************
143 ****************************************************************************/
144 static bool isHTTPLiveStreaming(stream_t *s)
146 const uint8_t *peek, *peek_end;
148 int64_t i_size = stream_Peek(s->p_source, &peek, 46);
152 if (strncasecmp((const char*)peek, "#EXTM3U", 7) != 0)
155 /* Parse stream and search for
156 * EXT-X-TARGETDURATION or EXT-X-STREAM-INF tag, see
157 * http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 */
158 peek_end = peek + i_size;
159 while(peek <= peek_end)
163 if (strncasecmp((const char*)peek, "#EXT-X-TARGETDURATION", 21) == 0)
165 else if (strncasecmp((const char*)peek, "#EXT-X-STREAM-INF", 17) == 0)
174 /* HTTP Live Streaming */
175 static hls_stream_t *hls_New(vlc_array_t *hls_stream, int id, uint64_t bw, char *uri)
177 hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
178 if (hls == NULL) return NULL;
182 hls->duration = -1;/* unknown */
183 hls->sequence = 0; /* default is 0 */
184 hls->version = 1; /* default protocol version */
187 vlc_UrlParse(&hls->url, uri, 0);
188 hls->segments = vlc_array_new();
189 vlc_array_append(hls_stream, hls);
190 vlc_mutex_init(&hls->lock);
194 static void hls_Free(hls_stream_t *hls)
196 vlc_mutex_destroy(&hls->lock);
200 for (int n = 0; n < vlc_array_count(hls->segments); n++)
202 segment_t *segment = (segment_t *)vlc_array_item_at_index(hls->segments, n);
203 if (segment) segment_Free(segment);
205 vlc_array_destroy(hls->segments);
208 vlc_UrlClean(&hls->url);
213 static hls_stream_t *hls_Get(vlc_array_t *hls_stream, int wanted)
215 int count = vlc_array_count(hls_stream);
218 if ((wanted < 0) || (wanted >= count))
220 return (hls_stream_t *) vlc_array_item_at_index(hls_stream, wanted);
223 static inline hls_stream_t *hls_GetFirst(vlc_array_t *hls_stream)
225 return (hls_stream_t*) hls_Get(hls_stream, 0);
228 static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream)
230 int count = vlc_array_count(hls_stream);
234 return (hls_stream_t *) hls_Get(hls_stream, count);
238 static segment_t *segment_New(hls_stream_t* hls, int duration, char *uri)
240 segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
244 segment->length = duration; /* seconds */
245 segment->size = 0; /* bytes */
246 segment->sequence = 0;
247 vlc_UrlParse(&segment->url, uri, 0);
248 segment->data = NULL;
249 vlc_array_append(hls->segments, segment);
250 vlc_mutex_init(&segment->lock);
254 static void segment_Free(segment_t *segment)
256 vlc_mutex_destroy(&segment->lock);
258 vlc_UrlClean(&segment->url);
260 block_Release(segment->data);
265 static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted)
269 int count = vlc_array_count(hls->segments);
272 if ((wanted < 0) || (wanted >= count))
274 return (segment_t *) vlc_array_item_at_index(hls->segments, wanted);
278 static char *parse_Attributes(const char *line, const char *attr)
281 char *begin = (char *) line;
282 char *end = begin + strlen(line);
284 /* Find start of attributes */
285 if ((p = strchr(begin, ':' )) == NULL)
291 if (strncasecmp(begin, attr, strlen(attr)) == 0)
293 /* <attr>=<value>[,]* */
294 p = strchr(begin, ',');
295 begin += strlen(attr) + 1;
298 if (p == NULL) /* last attribute */
299 return strndup(begin, end - begin);
301 return strndup(begin, p - begin);
304 } while(begin < end);
309 static char *relative_URI(stream_t *s, const char *uri, char *psz_uri)
311 char *p = strchr(uri, ':');
316 if (asprintf(&tmp,"%s://%s", s->psz_access, s->psz_path) < 0)
318 s->p_sys->b_error = true;
322 char *psz_path = strrchr(tmp, '/');
323 if (psz_path) *psz_path = '\0';
326 vlc_UrlParse(&url, tmp, 0);
327 if (asprintf(&psz_uri, "%s://%s%s/%s",
328 url.psz_protocol, url.psz_host, url.psz_path, uri) < 0)
332 s->p_sys->b_error = true;
340 static void parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read, char *uri)
342 stream_sys_t *p_sys = s->p_sys;
347 int ret = sscanf(p_read, "#EXTINF:%d,", &duration);
350 msg_Err(s, "expected #EXTINF:<s>,");
351 p_sys->b_error = true;
355 char *psz_uri = NULL;
356 psz_uri = relative_URI(s, uri, psz_uri);
358 vlc_mutex_lock(&hls->lock);
359 segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri);
361 segment->sequence = hls->sequence + vlc_array_count(hls->segments);
362 if (duration > hls->duration)
364 msg_Err(s, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d",
365 duration, hls->duration);
367 vlc_mutex_unlock(&hls->lock);
370 static void parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
372 stream_sys_t *p_sys = s->p_sys;
377 int ret = sscanf(p_read, "#EXT-X-TARGETDURATION:%d", &duration);
380 msg_Err(s, "expected #EXT-X-TARGETDURATION:<s>");
381 p_sys->b_error = true;
385 hls->duration = duration; /* seconds */
388 static void parse_StreamInformation(stream_t *s, char *p_read, char *uri)
390 stream_sys_t *p_sys = s->p_sys;
396 attr = parse_Attributes(p_read, "PROGRAM-ID");
399 msg_Err(s, "#EXT-X-STREAM-INF: expected PROGRAM-ID=<value>");
400 p_sys->b_error = true;
406 attr = parse_Attributes(p_read, "BANDWIDTH");
409 msg_Err(s, "#EXT-X-STREAM-INF: expected BANDWIDTH=<value>");
410 p_sys->b_error = true;
418 msg_Err(s, "#EXT-X-STREAM-INF: bandwidth cannot be 0");
419 p_sys->b_error = true;
423 msg_Info(s, "bandwidth adaption detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
425 char *psz_uri = NULL;
426 psz_uri = relative_URI(s, uri, psz_uri);
428 hls_stream_t *hls = hls_New(p_sys->hls_stream, id, bw, psz_uri ? psz_uri : uri);
430 p_sys->b_error = true;
433 static void parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
435 stream_sys_t *p_sys = s->p_sys;
440 int ret = sscanf(p_read, "#EXT-X-MEDIA-SEQUENCE:%d", &sequence);
443 msg_Err(s, "expected #EXT-X-MEDIA-SEQUENCE:<s>");
444 p_sys->b_error = true;
448 if (hls->sequence > 0)
449 msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist");
451 hls->sequence = sequence;
454 static void parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
456 stream_sys_t *p_sys = s->p_sys;
460 /* #EXT-X-KEY:METHOD=<method>[,URI="<URI>"][,IV=<IV>] */
462 attr = parse_Attributes(p_read, "METHOD");
465 msg_Err(s, "#EXT-X-KEY: expected METHOD=<value>");
466 p_sys->b_error = true;
468 else if (strncasecmp(attr, "NONE", 4) == 0)
470 char *uri = parse_Attributes(p_read, "URI");
473 msg_Err(s, "#EXT-X-KEY: URI not expected");
474 p_sys->b_error = true;
477 /* IV is only supported in version 2 and above */
478 if (hls->version >= 2)
480 char *iv = parse_Attributes(p_read, "IV");
483 msg_Err(s, "#EXT-X-KEY: IV not expected");
484 p_sys->b_error = true;
491 msg_Warn(s, "playback of encrypted HTTP Live media is not supported.");
492 p_sys->b_error = true;
497 static void parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read)
500 msg_Dbg(s, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read);
503 static void parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read)
505 stream_sys_t *p_sys = s->p_sys;
509 char answer[4] = "\0";
510 int ret = sscanf(p_read, "#EXT-X-ALLOW-CACHE:%3s", answer);
513 msg_Err(s, "#EXT-X-ALLOW-CACHE, ignoring ...");
514 p_sys->b_error = true;
518 hls->b_cache = (strncmp(answer, "NO", 2) != 0);
521 static void parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
523 stream_sys_t *p_sys = s->p_sys;
528 int ret = sscanf(p_read, "#EXT-X-VERSION:%d", &version);
531 msg_Err(s, "#EXT-X-VERSION: no protocol version found, should be version 1.");
532 p_sys->b_error = true;
537 hls->version = version;
538 if (hls->version != 1)
540 msg_Err(s, "#EXT-X-VERSION should be version 1 iso %d", version);
541 p_sys->b_error = true;
545 static void parse_EndList(stream_t *s, hls_stream_t *hls)
547 stream_sys_t *p_sys = s->p_sys;
551 p_sys->b_live = false;
552 msg_Info(s, "video on demand (vod) mode");
555 static void parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
559 /* FIXME: Do we need to act on discontinuity ?? */
560 msg_Dbg(s, "#EXT-X-DISCONTINUITY %s", p_read);
563 static void parse_M3U8ExtLine(stream_t *s, hls_stream_t *hls, char *line)
567 if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0)
568 parse_TargetDuration(s, hls, line);
569 else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 22) == 0)
570 parse_MediaSequence(s, hls, line);
571 else if (strncmp(line, "#EXT-X-KEY", 11) == 0)
572 parse_Key(s, hls, line);
573 else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 25) == 0)
574 parse_ProgramDateTime(s, hls, line);
575 else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 17) == 0)
576 parse_AllowCache(s, hls, line);
577 else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0)
578 parse_Discontinuity(s, hls, line);
579 else if (strncmp(line, "#EXT-X-VERSION", 14) == 0)
580 parse_Version(s, hls, line);
581 else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
582 parse_EndList(s, hls);
586 #define HTTPLIVE_MAX_LINE 4096
587 static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls)
589 stream_sys_t *p_sys = s->p_sys;
591 /* Download new playlist file from server */
592 if (AccessOpen(s, &hls->url) != VLC_SUCCESS)
595 /* Parse the rest of the reply */
596 uint8_t *tmp = calloc(1, HTTPLIVE_MAX_LINE);
603 char *line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
604 if (strncmp(line, "#EXTM3U", 7) != 0)
606 msg_Err(s, "missing #EXTM3U tag");
614 line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
617 msg_Dbg(s, "end of data");
621 if (!vlc_object_alive(s))
624 /* some more checks for actual data */
625 if (strncmp(line, "#EXTINF", 7) == 0)
627 char *uri = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
629 p_sys->b_error = true;
632 parse_SegmentInformation(s, hls, line, uri);
638 parse_M3U8ExtLine(s, hls, line);
641 /* Error during m3u8 parsing abort */
659 #undef HTTPLIVE_MAX_LINE
661 /* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8
662 * document defines the following new tags: EXT-X-TARGETDURATION,
663 * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X-
664 * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY,
667 static int parse_HTTPLiveStreaming(stream_t *s)
669 stream_sys_t *p_sys = s->p_sys;
670 char *p_read, *p_begin, *p_end;
672 assert(p_sys->hls_stream);
674 p_begin = p_read = stream_ReadLine(s->p_source);
679 int i_len = strlen(p_begin);
680 p_end = p_read + i_len;
682 if (strncmp(p_read, "#EXTM3U", 7) != 0)
684 msg_Err(s, "missing #EXTM3U tag .. aborting");
696 p_begin = stream_ReadLine(s->p_source);
700 i_len = strlen(p_begin);
702 p_end = p_read + i_len;
704 if (strncmp(p_read, "#EXT-X-STREAM-INF", 17) == 0)
706 p_sys->b_meta = true;
707 char *uri = stream_ReadLine(s->p_source);
709 p_sys->b_error = true;
712 parse_StreamInformation(s, p_read, uri);
716 else if (strncmp(p_read, "#EXTINF", 7) == 0)
718 char *uri = stream_ReadLine(s->p_source);
720 p_sys->b_error = true;
723 hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
725 parse_SegmentInformation(s, hls, p_read, uri);
727 p_sys->b_error = true;
733 hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
738 hls = hls_New(p_sys->hls_stream, -1, -1, NULL);
741 p_sys->b_error = true;
746 /* Parse M3U8 Ext Line */
747 parse_M3U8ExtLine(s, hls, p_read);
749 } while(p_read < p_end);
754 int count = vlc_array_count(p_sys->hls_stream);
755 for (int n = 0; n < count; n++)
757 hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
758 if (hls == NULL) break;
760 /* Is it a meta playlist? */
763 msg_Dbg(s, "parsing %s", hls->url.psz_path);
764 if (get_HTTPLivePlaylist(s, hls) != VLC_SUCCESS)
766 msg_Err(s, "could not parse playlist file from meta index." );
773 /* There should at least be 3 segments of hls->duration */
775 int num = vlc_array_count(hls->segments);
776 for (int i = 0; i < num; i++)
778 segment_t *segment = segment_GetSegment(hls, i);
779 if (segment && segment->length >= hls->duration)
784 msg_Err(s, "cannot start live playback at this time, try again later.");
787 /* Determine next time to reload playlist */
788 p_sys->wakeup = p_sys->last + (hls->duration * 2 * (mtime_t)1000000);
790 /* Can we cache files after playback */
791 p_sys->b_cache = hls->b_cache;
797 /****************************************************************************
799 ****************************************************************************/
800 static int BandwidthAdaptation(stream_t *s, int progid, uint64_t *bandwidth)
802 stream_sys_t *p_sys = s->p_sys;
804 uint64_t bw = *bandwidth;
806 msg_Dbg(s, "bandwidth (bits/s) %"PRIu64, bw * 1000); /* bits / s */
808 int count = vlc_array_count(p_sys->hls_stream);
809 for (int n = 0; n < count; n++)
811 /* Select best bandwidth match */
812 hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
813 if (hls == NULL) break;
815 /* only consider streams with the same PROGRAM-ID */
816 if (hls->id == progid)
818 if (bw <= hls->bandwidth)
820 *bandwidth = hls->bandwidth;
821 candidate = n; /* possible candidate */
828 static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur_stream)
833 vlc_mutex_lock(&segment->lock);
834 if (segment->data != NULL)
836 /* Segment already downloaded */
837 vlc_mutex_unlock(&segment->lock);
841 mtime_t start = mdate();
842 if (AccessDownload(s, segment) != VLC_SUCCESS)
844 vlc_mutex_unlock(&segment->lock);
847 mtime_t duration = mdate() - start;
849 vlc_mutex_unlock(&segment->lock);
851 uint64_t bw = (segment->size * 8) / (duration/1000); /* bits / ms */
852 if (hls->bandwidth != bw)
854 int newstream = BandwidthAdaptation(s, hls->id, &bw);
855 if ((newstream > 0) && (newstream != *cur_stream))
857 msg_Info(s, "switching to %s bandwidth (%"PRIu64") stream",
858 (hls->bandwidth <= bw) ? "faster" : "lower", bw);
859 *cur_stream = newstream;
865 static void* hls_Thread(vlc_object_t *p_this)
867 hls_thread_t *client = (hls_thread_t *) p_this;
868 stream_t *s = client->s;
869 stream_sys_t *p_sys = s->p_sys;
871 int canc = vlc_savecancel();
873 while (vlc_object_alive(p_this))
875 hls_stream_t *hls = hls_Get(client->hls_stream, client->current);
878 vlc_mutex_lock(&hls->lock);
879 segment_t *segment = segment_GetSegment(hls, hls->segment);
880 if (segment) hls->segment++;
881 vlc_mutex_unlock(&hls->lock);
883 /* Is there a new segment to process? */
886 if (!p_sys->b_live) break;
887 mwait(p_sys->wakeup);
889 else if (Download(client->s, hls, segment, &client->current) != VLC_SUCCESS)
891 if (!p_sys->b_live) break;
894 /* FIXME: Reread the m3u8 index file */
898 mtime_t now = mdate();
899 if (now >= p_sys->wakeup)
902 /** FIXME: Implement m3u8 playlist reloading */
903 if (!hls_ReloadPlaylist(client->s))
905 /* No change in playlist, then backoff */
907 if (p_sys->tries == 1) wait = 0.5;
908 else if (p_sys->tries == 2) wait = 1;
909 else if (p_sys->tries >= 3) wait = 3;
912 /* determine next time to update playlist */
914 p_sys->wakeup = now + ((mtime_t)(hls->duration * wait) * (mtime_t)1000000);
919 vlc_restorecancel(canc);
923 static int Prefetch(stream_t *s)
925 stream_sys_t *p_sys = s->p_sys;
929 current = p_sys->current;
931 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current);
935 segment_t *segment = segment_GetSegment(hls, hls->segment);
936 if (segment == NULL )
939 if (Download(s, hls, segment, &p_sys->current) != VLC_SUCCESS)
942 /* Bandwidth changed? */
943 if (current != p_sys->current)
949 /****************************************************************************
951 ****************************************************************************/
952 static int AccessOpen(stream_t *s, vlc_url_t *url)
954 stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
956 if ((url->psz_protocol == NULL) ||
957 (url->psz_path == NULL))
960 p_sys->p_access = vlc_object_create(s, sizeof(access_t));
961 if (p_sys->p_access == NULL)
964 p_sys->p_access->psz_access = strdup(url->psz_protocol);
965 p_sys->p_access->psz_filepath = strdup(url->psz_path);
966 if (url->psz_password || url->psz_username)
968 if (asprintf(&p_sys->p_access->psz_location, "%s:%s@%s%s",
969 url->psz_username, url->psz_password,
970 url->psz_host, url->psz_path) < 0)
972 msg_Err(s, "creating http access module");
978 if (asprintf(&p_sys->p_access->psz_location, "%s%s",
979 url->psz_host, url->psz_path) < 0)
981 msg_Err(s, "creating http access module");
985 vlc_object_attach(p_sys->p_access, s);
986 p_sys->p_access->p_module =
987 module_need(p_sys->p_access, "access", "http", true);
988 if (p_sys->p_access->p_module == NULL)
990 msg_Err(s, "could not load http access module");
997 vlc_object_release(p_sys->p_access);
998 p_sys->p_access = NULL;
1002 static void AccessClose(stream_t *s)
1004 stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
1006 if (p_sys->p_access)
1008 vlc_object_kill(p_sys->p_access);
1009 free(p_sys->p_access->psz_access);
1010 if (p_sys->p_access->p_module)
1011 module_unneed(p_sys->p_access,
1012 p_sys->p_access->p_module);
1014 vlc_object_release(p_sys->p_access);
1015 p_sys->p_access = NULL;
1019 static char *AccessReadLine(access_t *p_access, uint8_t *psz_tmp, size_t i_len)
1022 char *begin = (char *)psz_tmp;
1026 int skip = strlen(begin);
1027 ssize_t len = p_access->pf_read(p_access, psz_tmp + skip, i_len - skip);
1028 if (len < 0) return NULL;
1029 if ((len == 0) && (skip == 0))
1033 char *end = p + len + skip;
1044 line = calloc(1, p - begin + 1);
1047 /* copy line excluding \n */
1048 line = strndup(begin, p - begin);
1053 psz_tmp = memmove(begin, p, end - p);
1054 psz_tmp[end - p] = '\0';
1056 else memset(psz_tmp, 0, i_len);
1061 static int AccessDownload(stream_t *s, segment_t *segment)
1063 stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
1067 /* Download new playlist file from server */
1068 if (AccessOpen(s, &segment->url) != VLC_SUCCESS)
1069 return VLC_EGENERIC;
1071 segment->size = p_sys->p_access->info.i_size;
1072 assert(segment->size > 0);
1074 segment->data = block_Alloc(segment->size);
1075 if (segment->data == NULL)
1081 assert(segment->data->i_buffer == segment->size);
1083 ssize_t length = 0, curlen = 0;
1086 if (p_sys->p_access->info.i_size > segment->size)
1088 msg_Dbg(s, "size changed %"PRIu64, segment->size);
1089 segment->data = block_Realloc(segment->data, 0, p_sys->p_access->info.i_size);
1091 segment->size = p_sys->p_access->info.i_size;
1092 assert(segment->data->i_buffer == segment->size);
1094 length = p_sys->p_access->pf_read(p_sys->p_access,
1095 segment->data->p_buffer + curlen, segment->size - curlen);
1096 if ((length <= 0) || ((uint64_t)length >= segment->size))
1099 } while (vlc_object_alive(s));
1105 /****************************************************************************
1107 ****************************************************************************/
1108 static int Open(vlc_object_t *p_this)
1110 stream_t *s = (stream_t*)p_this;
1111 stream_sys_t *p_sys;
1113 if (!isHTTPLiveStreaming(s))
1114 return VLC_EGENERIC;
1116 msg_Info(p_this, "HTTP Live Streaming (%s)", s->psz_path);
1119 s->p_sys = p_sys = calloc(1, sizeof(*p_sys));
1123 p_sys->b_live = true;
1124 p_sys->b_meta = false;
1126 p_sys->hls_stream = vlc_array_new();
1127 if (p_sys->hls_stream == NULL)
1136 s->pf_control = Control;
1138 /* Select first segment to play */
1139 p_sys->last = mdate();
1140 if (parse_HTTPLiveStreaming(s) != VLC_SUCCESS)
1149 if (Prefetch(s) != VLC_SUCCESS)
1151 msg_Err(s, "fetching first segment.");
1155 p_sys->thread = vlc_object_create(s, sizeof(hls_thread_t));
1156 if( p_sys->thread == NULL )
1158 msg_Err(s, "creating HTTP Live Streaming client thread");
1162 p_sys->thread->hls_stream = p_sys->hls_stream;
1163 p_sys->thread->current = p_sys->current;
1164 p_sys->thread->s = s;
1166 if (vlc_thread_create(p_sys->thread, "HTTP Live Streaming client",
1167 hls_Thread, VLC_THREAD_PRIORITY_INPUT))
1172 vlc_object_attach(p_sys->thread, s);
1178 return VLC_EGENERIC;
1181 /****************************************************************************
1183 ****************************************************************************/
1184 static void Close(vlc_object_t *p_this)
1186 stream_t *s = (stream_t*)p_this;
1187 stream_sys_t *p_sys = s->p_sys;
1189 assert(p_sys->hls_stream);
1194 vlc_object_kill(p_sys->thread);
1195 vlc_thread_join(p_sys->thread);
1196 vlc_object_release(p_sys->thread);
1199 /* Free hls streams */
1200 for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
1203 hls = (hls_stream_t *)vlc_array_item_at_index(p_sys->hls_stream, i);
1204 if (hls) hls_Free(hls);
1206 vlc_array_destroy(p_sys->hls_stream);
1212 /****************************************************************************
1213 * Stream filters functions
1214 ****************************************************************************/
1215 static segment_t *NextSegment(stream_t *s)
1217 stream_sys_t *p_sys = s->p_sys;
1222 /* Is the next segment ready */
1223 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current);
1224 if (hls == NULL) return NULL;
1226 segment = segment_GetSegment(hls, p_sys->segment);
1227 if (segment == NULL) return NULL;
1229 /* This segment is ready? */
1230 if (segment->data != NULL)
1233 /* Was the stream changed to another bitrate? */
1234 if (!p_sys->b_meta) return NULL;
1236 if (p_sys->current != p_sys->thread->current)
1239 p_sys->current = p_sys->thread->current;
1244 /* Not downloaded yet, do it here */
1245 if (Download(s, hls, segment, &p_sys->current) != VLC_SUCCESS)
1252 while(vlc_object_alive(s));
1257 static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
1259 stream_sys_t *p_sys = s->p_sys;
1264 /* Determine next segment to read. If this is a meta playlist and
1265 * bandwidth conditions changed, then the stream might have switched
1266 * to another bandwidth. */
1267 segment_t *segment = NextSegment(s);
1268 if (segment == NULL)
1271 vlc_mutex_lock(&segment->lock);
1272 if (segment->data->i_buffer == 0)
1274 if (!p_sys->b_cache)
1276 block_Release(segment->data);
1277 segment->data = NULL;
1279 msg_Dbg(s, "switching to segment %d", p_sys->segment);
1281 vlc_mutex_unlock(&segment->lock);
1286 if (i_read <= segment->data->i_buffer)
1288 else if (i_read > segment->data->i_buffer)
1289 len = segment->data->i_buffer;
1293 memcpy(p_read + copied, segment->data->p_buffer, len);
1294 segment->data->i_buffer -= len;
1295 segment->data->p_buffer += len;
1299 vlc_mutex_unlock(&segment->lock);
1301 } while ((i_read > 0) && vlc_object_alive(s));
1306 static int Read(stream_t *s, void *buffer, unsigned int i_read)
1308 stream_sys_t *p_sys = s->p_sys;
1311 assert(p_sys->hls_stream);
1315 /* caller skips data, get big enough buffer */
1316 msg_Warn(s, "buffer is NULL (allocate %d)", i_read);
1317 buffer = calloc(1, i_read);
1319 return 0; /* NO MEMORY left*/
1322 length = hls_Read(s, (uint8_t*) buffer, i_read);
1326 p_sys->offset += length;
1330 static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
1332 stream_sys_t *p_sys = s->p_sys;
1335 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current);
1339 int peek_segment = p_sys->segment;
1342 segment_t *segment = segment_GetSegment(hls, peek_segment);
1343 if (segment == NULL) return 0;
1345 vlc_mutex_lock(&segment->lock);
1346 if (i_peek < segment->data->i_buffer)
1348 *pp_peek = segment->data->p_buffer;
1355 vlc_mutex_unlock(&segment->lock);
1356 } while ((curlen < i_peek) && vlc_object_alive(s));
1361 static bool hls_MaySeek(stream_t *s)
1363 stream_sys_t *p_sys = s->p_sys;
1365 if (p_sys->hls_stream == NULL)
1368 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current);
1369 if (hls == NULL) return false;
1373 int count = vlc_array_count(hls->segments);
1374 vlc_mutex_lock(&hls->lock);
1375 bool may_seek = (hls->segment < count - 2);
1376 vlc_mutex_unlock(&hls->lock);
1382 static uint64_t GetStreamSize(stream_t *s)
1384 stream_sys_t *p_sys = s->p_sys;
1389 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current);
1390 if (hls == NULL) return 0;
1392 /* FIXME: Stream size is not entirely exacts at this point */
1393 uint64_t length = 0UL;
1394 int count = vlc_array_count(hls->segments);
1395 for (int n = 0; n < count; n++)
1397 segment_t *segment = segment_GetSegment(hls, n);
1400 length += (segment->size > 0) ? segment->size :
1401 (segment->length * hls->bandwidth);
1407 static int segment_Seek(stream_t *s, uint64_t pos)
1409 stream_sys_t *p_sys = s->p_sys;
1411 /* Find the right offset */
1412 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current);
1414 return VLC_EGENERIC;
1416 uint64_t length = 0;
1417 bool b_found = false;
1419 int count = vlc_array_count(hls->segments);
1420 for (int n = 0; n < count; n++)
1422 /* FIXME: Seeking in segments not dowloaded is not supported. */
1423 if (n >= hls->segment)
1425 msg_Err(s, "seeking in segment not downloaded yet.");
1426 return VLC_EGENERIC;
1429 segment_t *segment = vlc_array_item_at_index(hls->segments, n);
1430 if (segment == NULL)
1431 return VLC_EGENERIC;
1433 vlc_mutex_lock(&segment->lock);
1434 length += segment->size;
1435 uint64_t size = segment->size -segment->data->i_buffer;
1438 segment->data->i_buffer += size;
1439 segment->data->p_buffer -= size;
1442 if (!b_found && (pos <= length))
1444 uint64_t used = length - pos;
1445 segment->data->i_buffer -= used;
1446 segment->data->p_buffer += used;
1448 count = p_sys->segment;
1452 vlc_mutex_unlock(&segment->lock);
1457 static int Control(stream_t *s, int i_query, va_list args)
1459 stream_sys_t *p_sys = s->p_sys;
1463 case STREAM_CAN_SEEK:
1464 case STREAM_CAN_FASTSEEK:
1465 *(va_arg (args, bool *)) = hls_MaySeek(s);
1467 case STREAM_GET_POSITION:
1468 *(va_arg (args, uint64_t *)) = p_sys->offset;
1470 case STREAM_SET_POSITION:
1473 uint64_t pos = (uint64_t)va_arg(args, uint64_t);
1474 if (segment_Seek(s, pos) == VLC_SUCCESS)
1476 p_sys->offset = pos;
1480 return VLC_EGENERIC;
1481 case STREAM_GET_SIZE:
1482 *(va_arg (args, uint64_t *)) = GetStreamSize(s);
1485 return VLC_EGENERIC;