2 * Apple HTTP Live Streaming Protocol Handler
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav 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 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Apple HTTP Live Streaming Protocol Handler
25 * http://tools.ietf.org/html/draft-pantos-http-live-streaming
28 #include "libavutil/avstring.h"
29 #include "libavutil/time.h"
36 * An apple http stream consists of a playlist with media segment files,
37 * played sequentially. There may be several playlists with the same
38 * video content, in different bandwidth variants, that are played in
39 * parallel (preferably only one bandwidth variant at a time). In this case,
40 * the user supplied the url to a main playlist that only lists the variant
43 * If the main playlist doesn't point at any variants, we still create
44 * one anonymous toplevel variant for this, to maintain the structure.
49 char url[MAX_URL_SIZE];
54 char url[MAX_URL_SIZE];
57 typedef struct HLSContext {
58 char playlisturl[MAX_URL_SIZE];
59 int64_t target_duration;
63 struct segment **segments;
65 struct variant **variants;
68 int64_t last_load_time;
71 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
73 int len = ff_get_line(s, buf, maxlen);
74 while (len > 0 && av_isspace(buf[len - 1]))
79 static void free_segment_list(HLSContext *s)
82 for (i = 0; i < s->n_segments; i++)
83 av_free(s->segments[i]);
84 av_freep(&s->segments);
88 static void free_variant_list(HLSContext *s)
91 for (i = 0; i < s->n_variants; i++)
92 av_free(s->variants[i]);
93 av_freep(&s->variants);
101 static void handle_variant_args(struct variant_info *info, const char *key,
102 int key_len, char **dest, int *dest_len)
104 if (!strncmp(key, "BANDWIDTH=", key_len)) {
105 *dest = info->bandwidth;
106 *dest_len = sizeof(info->bandwidth);
110 static int parse_playlist(URLContext *h, const char *url)
112 HLSContext *s = h->priv_data;
114 int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
115 int64_t duration = 0;
119 if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
120 &h->interrupt_callback, NULL)) < 0)
123 read_chomp_line(in, line, sizeof(line));
124 if (strcmp(line, "#EXTM3U")) {
125 ret = AVERROR_INVALIDDATA;
129 free_segment_list(s);
131 while (!in->eof_reached) {
132 read_chomp_line(in, line, sizeof(line));
133 if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
134 struct variant_info info = {{0}};
136 ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
138 bandwidth = atoi(info.bandwidth);
139 } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
140 s->target_duration = atoi(ptr) * AV_TIME_BASE;
141 } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
142 s->start_seq_no = atoi(ptr);
143 } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
145 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
147 duration = atof(ptr) * AV_TIME_BASE;
148 } else if (av_strstart(line, "#", NULL)) {
150 } else if (line[0]) {
152 struct segment *seg = av_malloc(sizeof(struct segment));
154 ret = AVERROR(ENOMEM);
157 seg->duration = duration;
158 ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
159 dynarray_add(&s->segments, &s->n_segments, seg);
161 } else if (is_variant) {
162 struct variant *var = av_malloc(sizeof(struct variant));
164 ret = AVERROR(ENOMEM);
167 var->bandwidth = bandwidth;
168 ff_make_absolute_url(var->url, sizeof(var->url), url, line);
169 dynarray_add(&s->variants, &s->n_variants, var);
174 s->last_load_time = av_gettime_relative();
181 static int hls_close(URLContext *h)
183 HLSContext *s = h->priv_data;
185 free_segment_list(s);
186 free_variant_list(s);
187 ffurl_close(s->seg_hd);
191 static int hls_open(URLContext *h, const char *uri, int flags)
193 HLSContext *s = h->priv_data;
195 const char *nested_url;
197 if (flags & AVIO_FLAG_WRITE)
198 return AVERROR(ENOSYS);
202 if (av_strstart(uri, "hls+", &nested_url)) {
203 av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
204 } else if (av_strstart(uri, "hls://", &nested_url)) {
205 av_log(h, AV_LOG_ERROR,
206 "No nested protocol specified. Specify e.g. hls+http://%s\n",
208 ret = AVERROR(EINVAL);
211 av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
212 ret = AVERROR(EINVAL);
215 av_log(h, AV_LOG_WARNING,
216 "Using the hls protocol is discouraged, please try using the "
217 "hls demuxer instead. The hls demuxer should be more complete "
218 "and work as well as the protocol implementation. (If not, "
219 "please report it.) To use the demuxer, simply use %s as url.\n",
222 if ((ret = parse_playlist(h, s->playlisturl)) < 0)
225 if (s->n_segments == 0 && s->n_variants > 0) {
226 int max_bandwidth = 0, maxvar = -1;
227 for (i = 0; i < s->n_variants; i++) {
228 if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
229 max_bandwidth = s->variants[i]->bandwidth;
233 av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
234 sizeof(s->playlisturl));
235 if ((ret = parse_playlist(h, s->playlisturl)) < 0)
239 if (s->n_segments == 0) {
240 av_log(h, AV_LOG_WARNING, "Empty playlist\n");
244 s->cur_seq_no = s->start_seq_no;
245 if (!s->finished && s->n_segments >= 3)
246 s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
255 static int hls_read(URLContext *h, uint8_t *buf, int size)
257 HLSContext *s = h->priv_data;
260 int64_t reload_interval;
264 ret = ffurl_read(s->seg_hd, buf, size);
269 ffurl_close(s->seg_hd);
273 reload_interval = s->n_segments > 0 ?
274 s->segments[s->n_segments - 1]->duration :
278 int64_t now = av_gettime_relative();
279 if (now - s->last_load_time >= reload_interval) {
280 if ((ret = parse_playlist(h, s->playlisturl)) < 0)
282 /* If we need to reload the playlist again below (if
283 * there's still no more segments), switch to a reload
284 * interval of half the target duration. */
285 reload_interval = s->target_duration / 2;
288 if (s->cur_seq_no < s->start_seq_no) {
289 av_log(h, AV_LOG_WARNING,
290 "skipping %d segments ahead, expired from playlist\n",
291 s->start_seq_no - s->cur_seq_no);
292 s->cur_seq_no = s->start_seq_no;
294 if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
297 while (av_gettime_relative() - s->last_load_time < reload_interval) {
298 if (ff_check_interrupt(&h->interrupt_callback))
304 url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
305 av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
306 ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
307 &h->interrupt_callback, NULL);
309 if (ff_check_interrupt(&h->interrupt_callback))
311 av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
318 URLProtocol ff_hls_protocol = {
320 .url_open = hls_open,
321 .url_read = hls_read,
322 .url_close = hls_close,
323 .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
324 .priv_data_size = sizeof(HLSContext),