00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/time.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032 #include "url.h"
00033 #include "version.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 struct segment {
00048 int duration;
00049 char url[MAX_URL_SIZE];
00050 };
00051
00052 struct variant {
00053 int bandwidth;
00054 char url[MAX_URL_SIZE];
00055 };
00056
00057 typedef struct HLSContext {
00058 char playlisturl[MAX_URL_SIZE];
00059 int target_duration;
00060 int start_seq_no;
00061 int finished;
00062 int n_segments;
00063 struct segment **segments;
00064 int n_variants;
00065 struct variant **variants;
00066 int cur_seq_no;
00067 URLContext *seg_hd;
00068 int64_t last_load_time;
00069 } HLSContext;
00070
00071 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
00072 {
00073 int len = ff_get_line(s, buf, maxlen);
00074 while (len > 0 && isspace(buf[len - 1]))
00075 buf[--len] = '\0';
00076 return len;
00077 }
00078
00079 static void free_segment_list(HLSContext *s)
00080 {
00081 int i;
00082 for (i = 0; i < s->n_segments; i++)
00083 av_free(s->segments[i]);
00084 av_freep(&s->segments);
00085 s->n_segments = 0;
00086 }
00087
00088 static void free_variant_list(HLSContext *s)
00089 {
00090 int i;
00091 for (i = 0; i < s->n_variants; i++)
00092 av_free(s->variants[i]);
00093 av_freep(&s->variants);
00094 s->n_variants = 0;
00095 }
00096
00097 struct variant_info {
00098 char bandwidth[20];
00099 };
00100
00101 static void handle_variant_args(struct variant_info *info, const char *key,
00102 int key_len, char **dest, int *dest_len)
00103 {
00104 if (!strncmp(key, "BANDWIDTH=", key_len)) {
00105 *dest = info->bandwidth;
00106 *dest_len = sizeof(info->bandwidth);
00107 }
00108 }
00109
00110 static int parse_playlist(URLContext *h, const char *url)
00111 {
00112 HLSContext *s = h->priv_data;
00113 AVIOContext *in;
00114 int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
00115 char line[1024];
00116 const char *ptr;
00117
00118 if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
00119 &h->interrupt_callback, NULL)) < 0)
00120 return ret;
00121
00122 read_chomp_line(in, line, sizeof(line));
00123 if (strcmp(line, "#EXTM3U"))
00124 return AVERROR_INVALIDDATA;
00125
00126 free_segment_list(s);
00127 s->finished = 0;
00128 while (!url_feof(in)) {
00129 read_chomp_line(in, line, sizeof(line));
00130 if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
00131 struct variant_info info = {{0}};
00132 is_variant = 1;
00133 ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
00134 &info);
00135 bandwidth = atoi(info.bandwidth);
00136 } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
00137 s->target_duration = atoi(ptr);
00138 } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
00139 s->start_seq_no = atoi(ptr);
00140 } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
00141 s->finished = 1;
00142 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
00143 is_segment = 1;
00144 duration = atoi(ptr);
00145 } else if (av_strstart(line, "#", NULL)) {
00146 continue;
00147 } else if (line[0]) {
00148 if (is_segment) {
00149 struct segment *seg = av_malloc(sizeof(struct segment));
00150 if (!seg) {
00151 ret = AVERROR(ENOMEM);
00152 goto fail;
00153 }
00154 seg->duration = duration;
00155 ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
00156 dynarray_add(&s->segments, &s->n_segments, seg);
00157 is_segment = 0;
00158 } else if (is_variant) {
00159 struct variant *var = av_malloc(sizeof(struct variant));
00160 if (!var) {
00161 ret = AVERROR(ENOMEM);
00162 goto fail;
00163 }
00164 var->bandwidth = bandwidth;
00165 ff_make_absolute_url(var->url, sizeof(var->url), url, line);
00166 dynarray_add(&s->variants, &s->n_variants, var);
00167 is_variant = 0;
00168 }
00169 }
00170 }
00171 s->last_load_time = av_gettime();
00172
00173 fail:
00174 avio_close(in);
00175 return ret;
00176 }
00177
00178 static int hls_close(URLContext *h)
00179 {
00180 HLSContext *s = h->priv_data;
00181
00182 free_segment_list(s);
00183 free_variant_list(s);
00184 ffurl_close(s->seg_hd);
00185 return 0;
00186 }
00187
00188 static int hls_open(URLContext *h, const char *uri, int flags)
00189 {
00190 HLSContext *s = h->priv_data;
00191 int ret, i;
00192 const char *nested_url;
00193
00194 if (flags & AVIO_FLAG_WRITE)
00195 return AVERROR(ENOSYS);
00196
00197 h->is_streamed = 1;
00198
00199 if (av_strstart(uri, "hls+", &nested_url)) {
00200 av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
00201 } else if (av_strstart(uri, "hls://", &nested_url)) {
00202 av_log(h, AV_LOG_ERROR,
00203 "No nested protocol specified. Specify e.g. hls+http://%s\n",
00204 nested_url);
00205 ret = AVERROR(EINVAL);
00206 goto fail;
00207 #if FF_API_APPLEHTTP_PROTO
00208 } else if (av_strstart(uri, "applehttp+", &nested_url)) {
00209 av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
00210 av_log(h, AV_LOG_WARNING,
00211 "The applehttp protocol is deprecated, use hls+%s as url "
00212 "instead.\n", nested_url);
00213 } else if (av_strstart(uri, "applehttp://", &nested_url)) {
00214 av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
00215 av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
00216 av_log(h, AV_LOG_WARNING,
00217 "The applehttp protocol is deprecated, use hls+http://%s as url "
00218 "instead.\n", nested_url);
00219 #endif
00220 } else {
00221 av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
00222 ret = AVERROR(EINVAL);
00223 goto fail;
00224 }
00225 av_log(h, AV_LOG_WARNING,
00226 "Using the hls protocol is discouraged, please try using the "
00227 "hls demuxer instead. The hls demuxer should be more complete "
00228 "and work as well as the protocol implementation. (If not, "
00229 "please report it.) To use the demuxer, simply use %s as url.\n",
00230 s->playlisturl);
00231
00232 if ((ret = parse_playlist(h, s->playlisturl)) < 0)
00233 goto fail;
00234
00235 if (s->n_segments == 0 && s->n_variants > 0) {
00236 int max_bandwidth = 0, maxvar = -1;
00237 for (i = 0; i < s->n_variants; i++) {
00238 if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
00239 max_bandwidth = s->variants[i]->bandwidth;
00240 maxvar = i;
00241 }
00242 }
00243 av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
00244 sizeof(s->playlisturl));
00245 if ((ret = parse_playlist(h, s->playlisturl)) < 0)
00246 goto fail;
00247 }
00248
00249 if (s->n_segments == 0) {
00250 av_log(h, AV_LOG_WARNING, "Empty playlist\n");
00251 ret = AVERROR(EIO);
00252 goto fail;
00253 }
00254 s->cur_seq_no = s->start_seq_no;
00255 if (!s->finished && s->n_segments >= 3)
00256 s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
00257
00258 return 0;
00259
00260 fail:
00261 hls_close(h);
00262 return ret;
00263 }
00264
00265 static int hls_read(URLContext *h, uint8_t *buf, int size)
00266 {
00267 HLSContext *s = h->priv_data;
00268 const char *url;
00269 int ret;
00270 int64_t reload_interval;
00271
00272 start:
00273 if (s->seg_hd) {
00274 ret = ffurl_read(s->seg_hd, buf, size);
00275 if (ret > 0)
00276 return ret;
00277 }
00278 if (s->seg_hd) {
00279 ffurl_close(s->seg_hd);
00280 s->seg_hd = NULL;
00281 s->cur_seq_no++;
00282 }
00283 reload_interval = s->n_segments > 0 ?
00284 s->segments[s->n_segments - 1]->duration :
00285 s->target_duration;
00286 reload_interval *= 1000000;
00287 retry:
00288 if (!s->finished) {
00289 int64_t now = av_gettime();
00290 if (now - s->last_load_time >= reload_interval) {
00291 if ((ret = parse_playlist(h, s->playlisturl)) < 0)
00292 return ret;
00293
00294
00295
00296 reload_interval = s->target_duration * 500000;
00297 }
00298 }
00299 if (s->cur_seq_no < s->start_seq_no) {
00300 av_log(h, AV_LOG_WARNING,
00301 "skipping %d segments ahead, expired from playlist\n",
00302 s->start_seq_no - s->cur_seq_no);
00303 s->cur_seq_no = s->start_seq_no;
00304 }
00305 if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
00306 if (s->finished)
00307 return AVERROR_EOF;
00308 while (av_gettime() - s->last_load_time < reload_interval) {
00309 if (ff_check_interrupt(&h->interrupt_callback))
00310 return AVERROR_EXIT;
00311 av_usleep(100*1000);
00312 }
00313 goto retry;
00314 }
00315 url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
00316 av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
00317 ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
00318 &h->interrupt_callback, NULL);
00319 if (ret < 0) {
00320 if (ff_check_interrupt(&h->interrupt_callback))
00321 return AVERROR_EXIT;
00322 av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
00323 s->cur_seq_no++;
00324 goto retry;
00325 }
00326 goto start;
00327 }
00328
00329 #if FF_API_APPLEHTTP_PROTO
00330 URLProtocol ff_applehttp_protocol = {
00331 .name = "applehttp",
00332 .url_open = hls_open,
00333 .url_read = hls_read,
00334 .url_close = hls_close,
00335 .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
00336 .priv_data_size = sizeof(HLSContext),
00337 };
00338 #endif
00339
00340 URLProtocol ff_hls_protocol = {
00341 .name = "hls",
00342 .url_open = hls_open,
00343 .url_read = hls_read,
00344 .url_close = hls_close,
00345 .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
00346 .priv_data_size = sizeof(HLSContext),
00347 };