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/intreadwrite.h"
00030 #include "libavutil/mathematics.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/dict.h"
00033 #include "libavutil/time.h"
00034 #include "avformat.h"
00035 #include "internal.h"
00036 #include "avio_internal.h"
00037 #include "url.h"
00038
00039 #define INITIAL_BUFFER_SIZE 32768
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 enum KeyType {
00054 KEY_NONE,
00055 KEY_AES_128,
00056 };
00057
00058 struct segment {
00059 int duration;
00060 char url[MAX_URL_SIZE];
00061 char key[MAX_URL_SIZE];
00062 enum KeyType key_type;
00063 uint8_t iv[16];
00064 };
00065
00066
00067
00068
00069
00070
00071 struct variant {
00072 int bandwidth;
00073 char url[MAX_URL_SIZE];
00074 AVIOContext pb;
00075 uint8_t* read_buffer;
00076 URLContext *input;
00077 AVFormatContext *parent;
00078 int index;
00079 AVFormatContext *ctx;
00080 AVPacket pkt;
00081 int stream_offset;
00082
00083 int finished;
00084 int target_duration;
00085 int start_seq_no;
00086 int n_segments;
00087 struct segment **segments;
00088 int needed, cur_needed;
00089 int cur_seq_no;
00090 int64_t last_load_time;
00091
00092 char key_url[MAX_URL_SIZE];
00093 uint8_t key[16];
00094 };
00095
00096 typedef struct HLSContext {
00097 int n_variants;
00098 struct variant **variants;
00099 int cur_seq_no;
00100 int end_of_segment;
00101 int first_packet;
00102 int64_t first_timestamp;
00103 int64_t seek_timestamp;
00104 int seek_flags;
00105 AVIOInterruptCB *interrupt_callback;
00106 } HLSContext;
00107
00108 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
00109 {
00110 int len = ff_get_line(s, buf, maxlen);
00111 while (len > 0 && isspace(buf[len - 1]))
00112 buf[--len] = '\0';
00113 return len;
00114 }
00115
00116 static void free_segment_list(struct variant *var)
00117 {
00118 int i;
00119 for (i = 0; i < var->n_segments; i++)
00120 av_free(var->segments[i]);
00121 av_freep(&var->segments);
00122 var->n_segments = 0;
00123 }
00124
00125 static void free_variant_list(HLSContext *c)
00126 {
00127 int i;
00128 for (i = 0; i < c->n_variants; i++) {
00129 struct variant *var = c->variants[i];
00130 free_segment_list(var);
00131 av_free_packet(&var->pkt);
00132 av_free(var->pb.buffer);
00133 if (var->input)
00134 ffurl_close(var->input);
00135 if (var->ctx) {
00136 var->ctx->pb = NULL;
00137 avformat_close_input(&var->ctx);
00138 }
00139 av_free(var);
00140 }
00141 av_freep(&c->variants);
00142 c->n_variants = 0;
00143 }
00144
00145
00146
00147
00148
00149 static void reset_packet(AVPacket *pkt)
00150 {
00151 av_init_packet(pkt);
00152 pkt->data = NULL;
00153 }
00154
00155 static struct variant *new_variant(HLSContext *c, int bandwidth,
00156 const char *url, const char *base)
00157 {
00158 struct variant *var = av_mallocz(sizeof(struct variant));
00159 if (!var)
00160 return NULL;
00161 reset_packet(&var->pkt);
00162 var->bandwidth = bandwidth;
00163 ff_make_absolute_url(var->url, sizeof(var->url), base, url);
00164 dynarray_add(&c->variants, &c->n_variants, var);
00165 return var;
00166 }
00167
00168 struct variant_info {
00169 char bandwidth[20];
00170 };
00171
00172 static void handle_variant_args(struct variant_info *info, const char *key,
00173 int key_len, char **dest, int *dest_len)
00174 {
00175 if (!strncmp(key, "BANDWIDTH=", key_len)) {
00176 *dest = info->bandwidth;
00177 *dest_len = sizeof(info->bandwidth);
00178 }
00179 }
00180
00181 struct key_info {
00182 char uri[MAX_URL_SIZE];
00183 char method[10];
00184 char iv[35];
00185 };
00186
00187 static void handle_key_args(struct key_info *info, const char *key,
00188 int key_len, char **dest, int *dest_len)
00189 {
00190 if (!strncmp(key, "METHOD=", key_len)) {
00191 *dest = info->method;
00192 *dest_len = sizeof(info->method);
00193 } else if (!strncmp(key, "URI=", key_len)) {
00194 *dest = info->uri;
00195 *dest_len = sizeof(info->uri);
00196 } else if (!strncmp(key, "IV=", key_len)) {
00197 *dest = info->iv;
00198 *dest_len = sizeof(info->iv);
00199 }
00200 }
00201
00202 static int parse_playlist(HLSContext *c, const char *url,
00203 struct variant *var, AVIOContext *in)
00204 {
00205 int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
00206 enum KeyType key_type = KEY_NONE;
00207 uint8_t iv[16] = "";
00208 int has_iv = 0;
00209 char key[MAX_URL_SIZE] = "";
00210 char line[1024];
00211 const char *ptr;
00212 int close_in = 0;
00213
00214 if (!in) {
00215 close_in = 1;
00216 if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
00217 c->interrupt_callback, NULL)) < 0)
00218 return ret;
00219 }
00220
00221 read_chomp_line(in, line, sizeof(line));
00222 if (strcmp(line, "#EXTM3U")) {
00223 ret = AVERROR_INVALIDDATA;
00224 goto fail;
00225 }
00226
00227 if (var) {
00228 free_segment_list(var);
00229 var->finished = 0;
00230 }
00231 while (!url_feof(in)) {
00232 read_chomp_line(in, line, sizeof(line));
00233 if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
00234 struct variant_info info = {{0}};
00235 is_variant = 1;
00236 ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
00237 &info);
00238 bandwidth = atoi(info.bandwidth);
00239 } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {
00240 struct key_info info = {{0}};
00241 ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args,
00242 &info);
00243 key_type = KEY_NONE;
00244 has_iv = 0;
00245 if (!strcmp(info.method, "AES-128"))
00246 key_type = KEY_AES_128;
00247 if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {
00248 ff_hex_to_data(iv, info.iv + 2);
00249 has_iv = 1;
00250 }
00251 av_strlcpy(key, info.uri, sizeof(key));
00252 } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
00253 if (!var) {
00254 var = new_variant(c, 0, url, NULL);
00255 if (!var) {
00256 ret = AVERROR(ENOMEM);
00257 goto fail;
00258 }
00259 }
00260 var->target_duration = atoi(ptr);
00261 } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
00262 if (!var) {
00263 var = new_variant(c, 0, url, NULL);
00264 if (!var) {
00265 ret = AVERROR(ENOMEM);
00266 goto fail;
00267 }
00268 }
00269 var->start_seq_no = atoi(ptr);
00270 } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
00271 if (var)
00272 var->finished = 1;
00273 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
00274 is_segment = 1;
00275 duration = atoi(ptr);
00276 } else if (av_strstart(line, "#", NULL)) {
00277 continue;
00278 } else if (line[0]) {
00279 if (is_variant) {
00280 if (!new_variant(c, bandwidth, line, url)) {
00281 ret = AVERROR(ENOMEM);
00282 goto fail;
00283 }
00284 is_variant = 0;
00285 bandwidth = 0;
00286 }
00287 if (is_segment) {
00288 struct segment *seg;
00289 if (!var) {
00290 var = new_variant(c, 0, url, NULL);
00291 if (!var) {
00292 ret = AVERROR(ENOMEM);
00293 goto fail;
00294 }
00295 }
00296 seg = av_malloc(sizeof(struct segment));
00297 if (!seg) {
00298 ret = AVERROR(ENOMEM);
00299 goto fail;
00300 }
00301 seg->duration = duration;
00302 seg->key_type = key_type;
00303 if (has_iv) {
00304 memcpy(seg->iv, iv, sizeof(iv));
00305 } else {
00306 int seq = var->start_seq_no + var->n_segments;
00307 memset(seg->iv, 0, sizeof(seg->iv));
00308 AV_WB32(seg->iv + 12, seq);
00309 }
00310 ff_make_absolute_url(seg->key, sizeof(seg->key), url, key);
00311 ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
00312 dynarray_add(&var->segments, &var->n_segments, seg);
00313 is_segment = 0;
00314 }
00315 }
00316 }
00317 if (var)
00318 var->last_load_time = av_gettime();
00319
00320 fail:
00321 if (close_in)
00322 avio_close(in);
00323 return ret;
00324 }
00325
00326 static int open_input(struct variant *var)
00327 {
00328 struct segment *seg = var->segments[var->cur_seq_no - var->start_seq_no];
00329 if (seg->key_type == KEY_NONE) {
00330 return ffurl_open(&var->input, seg->url, AVIO_FLAG_READ,
00331 &var->parent->interrupt_callback, NULL);
00332 } else if (seg->key_type == KEY_AES_128) {
00333 char iv[33], key[33], url[MAX_URL_SIZE];
00334 int ret;
00335 if (strcmp(seg->key, var->key_url)) {
00336 URLContext *uc;
00337 if (ffurl_open(&uc, seg->key, AVIO_FLAG_READ,
00338 &var->parent->interrupt_callback, NULL) == 0) {
00339 if (ffurl_read_complete(uc, var->key, sizeof(var->key))
00340 != sizeof(var->key)) {
00341 av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
00342 seg->key);
00343 }
00344 ffurl_close(uc);
00345 } else {
00346 av_log(NULL, AV_LOG_ERROR, "Unable to open key file %s\n",
00347 seg->key);
00348 }
00349 av_strlcpy(var->key_url, seg->key, sizeof(var->key_url));
00350 }
00351 ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
00352 ff_data_to_hex(key, var->key, sizeof(var->key), 0);
00353 iv[32] = key[32] = '\0';
00354 if (strstr(seg->url, "://"))
00355 snprintf(url, sizeof(url), "crypto+%s", seg->url);
00356 else
00357 snprintf(url, sizeof(url), "crypto:%s", seg->url);
00358 if ((ret = ffurl_alloc(&var->input, url, AVIO_FLAG_READ,
00359 &var->parent->interrupt_callback)) < 0)
00360 return ret;
00361 av_opt_set(var->input->priv_data, "key", key, 0);
00362 av_opt_set(var->input->priv_data, "iv", iv, 0);
00363 if ((ret = ffurl_connect(var->input, NULL)) < 0) {
00364 ffurl_close(var->input);
00365 var->input = NULL;
00366 return ret;
00367 }
00368 return 0;
00369 }
00370 return AVERROR(ENOSYS);
00371 }
00372
00373 static int read_data(void *opaque, uint8_t *buf, int buf_size)
00374 {
00375 struct variant *v = opaque;
00376 HLSContext *c = v->parent->priv_data;
00377 int ret, i;
00378
00379 restart:
00380 if (!v->input) {
00381
00382
00383 int64_t reload_interval = v->n_segments > 0 ?
00384 v->segments[v->n_segments - 1]->duration :
00385 v->target_duration;
00386 reload_interval *= 1000000;
00387
00388 reload:
00389 if (!v->finished &&
00390 av_gettime() - v->last_load_time >= reload_interval) {
00391 if ((ret = parse_playlist(c, v->url, v, NULL)) < 0)
00392 return ret;
00393
00394
00395
00396 reload_interval = v->target_duration * 500000;
00397 }
00398 if (v->cur_seq_no < v->start_seq_no) {
00399 av_log(NULL, AV_LOG_WARNING,
00400 "skipping %d segments ahead, expired from playlists\n",
00401 v->start_seq_no - v->cur_seq_no);
00402 v->cur_seq_no = v->start_seq_no;
00403 }
00404 if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
00405 if (v->finished)
00406 return AVERROR_EOF;
00407 while (av_gettime() - v->last_load_time < reload_interval) {
00408 if (ff_check_interrupt(c->interrupt_callback))
00409 return AVERROR_EXIT;
00410 av_usleep(100*1000);
00411 }
00412
00413 goto reload;
00414 }
00415
00416 ret = open_input(v);
00417 if (ret < 0)
00418 return ret;
00419 }
00420 ret = ffurl_read(v->input, buf, buf_size);
00421 if (ret > 0)
00422 return ret;
00423 ffurl_close(v->input);
00424 v->input = NULL;
00425 v->cur_seq_no++;
00426
00427 c->end_of_segment = 1;
00428 c->cur_seq_no = v->cur_seq_no;
00429
00430 if (v->ctx && v->ctx->nb_streams && v->parent->nb_streams >= v->stream_offset + v->ctx->nb_streams) {
00431 v->needed = 0;
00432 for (i = v->stream_offset; i < v->stream_offset + v->ctx->nb_streams;
00433 i++) {
00434 if (v->parent->streams[i]->discard < AVDISCARD_ALL)
00435 v->needed = 1;
00436 }
00437 }
00438 if (!v->needed) {
00439 av_log(v->parent, AV_LOG_INFO, "No longer receiving variant %d\n",
00440 v->index);
00441 return AVERROR_EOF;
00442 }
00443 goto restart;
00444 }
00445
00446 static int hls_read_header(AVFormatContext *s)
00447 {
00448 HLSContext *c = s->priv_data;
00449 int ret = 0, i, j, stream_offset = 0;
00450
00451 c->interrupt_callback = &s->interrupt_callback;
00452
00453 if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
00454 goto fail;
00455
00456 if (c->n_variants == 0) {
00457 av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
00458 ret = AVERROR_EOF;
00459 goto fail;
00460 }
00461
00462
00463 if (c->n_variants > 1 || c->variants[0]->n_segments == 0) {
00464 for (i = 0; i < c->n_variants; i++) {
00465 struct variant *v = c->variants[i];
00466 if ((ret = parse_playlist(c, v->url, v, NULL)) < 0)
00467 goto fail;
00468 }
00469 }
00470
00471 if (c->variants[0]->n_segments == 0) {
00472 av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
00473 ret = AVERROR_EOF;
00474 goto fail;
00475 }
00476
00477
00478
00479 if (c->variants[0]->finished) {
00480 int64_t duration = 0;
00481 for (i = 0; i < c->variants[0]->n_segments; i++)
00482 duration += c->variants[0]->segments[i]->duration;
00483 s->duration = duration * AV_TIME_BASE;
00484 }
00485
00486
00487 for (i = 0; i < c->n_variants; i++) {
00488 struct variant *v = c->variants[i];
00489 AVInputFormat *in_fmt = NULL;
00490 char bitrate_str[20];
00491 if (v->n_segments == 0)
00492 continue;
00493
00494 if (!(v->ctx = avformat_alloc_context())) {
00495 ret = AVERROR(ENOMEM);
00496 goto fail;
00497 }
00498
00499 v->index = i;
00500 v->needed = 1;
00501 v->parent = s;
00502
00503
00504
00505 v->cur_seq_no = v->start_seq_no;
00506 if (!v->finished && v->n_segments > 3)
00507 v->cur_seq_no = v->start_seq_no + v->n_segments - 3;
00508
00509 v->read_buffer = av_malloc(INITIAL_BUFFER_SIZE);
00510 ffio_init_context(&v->pb, v->read_buffer, INITIAL_BUFFER_SIZE, 0, v,
00511 read_data, NULL, NULL);
00512 v->pb.seekable = 0;
00513 ret = av_probe_input_buffer(&v->pb, &in_fmt, v->segments[0]->url,
00514 NULL, 0, 0);
00515 if (ret < 0) {
00516
00517
00518
00519
00520 av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", v->segments[0]->url);
00521 avformat_free_context(v->ctx);
00522 v->ctx = NULL;
00523 goto fail;
00524 }
00525 v->ctx->pb = &v->pb;
00526 ret = avformat_open_input(&v->ctx, v->segments[0]->url, in_fmt, NULL);
00527 if (ret < 0)
00528 goto fail;
00529
00530 v->stream_offset = stream_offset;
00531 v->ctx->ctx_flags &= ~AVFMTCTX_NOHEADER;
00532 ret = avformat_find_stream_info(v->ctx, NULL);
00533 if (ret < 0)
00534 goto fail;
00535 snprintf(bitrate_str, sizeof(bitrate_str), "%d", v->bandwidth);
00536
00537 for (j = 0; j < v->ctx->nb_streams; j++) {
00538 AVStream *st = avformat_new_stream(s, NULL);
00539 if (!st) {
00540 ret = AVERROR(ENOMEM);
00541 goto fail;
00542 }
00543 st->id = i;
00544 avcodec_copy_context(st->codec, v->ctx->streams[j]->codec);
00545 if (v->bandwidth)
00546 av_dict_set(&st->metadata, "variant_bitrate", bitrate_str,
00547 0);
00548 }
00549 stream_offset += v->ctx->nb_streams;
00550 }
00551
00552 c->first_packet = 1;
00553 c->first_timestamp = AV_NOPTS_VALUE;
00554 c->seek_timestamp = AV_NOPTS_VALUE;
00555
00556 return 0;
00557 fail:
00558 free_variant_list(c);
00559 return ret;
00560 }
00561
00562 static int recheck_discard_flags(AVFormatContext *s, int first)
00563 {
00564 HLSContext *c = s->priv_data;
00565 int i, changed = 0;
00566
00567
00568 for (i = 0; i < c->n_variants; i++)
00569 c->variants[i]->cur_needed = 0;
00570
00571 for (i = 0; i < s->nb_streams; i++) {
00572 AVStream *st = s->streams[i];
00573 struct variant *var = c->variants[s->streams[i]->id];
00574 if (st->discard < AVDISCARD_ALL)
00575 var->cur_needed = 1;
00576 }
00577 for (i = 0; i < c->n_variants; i++) {
00578 struct variant *v = c->variants[i];
00579 if (v->cur_needed && !v->needed) {
00580 v->needed = 1;
00581 changed = 1;
00582 v->cur_seq_no = c->cur_seq_no;
00583 v->pb.eof_reached = 0;
00584 av_log(s, AV_LOG_INFO, "Now receiving variant %d\n", i);
00585 } else if (first && !v->cur_needed && v->needed) {
00586 if (v->input)
00587 ffurl_close(v->input);
00588 v->input = NULL;
00589 v->needed = 0;
00590 changed = 1;
00591 av_log(s, AV_LOG_INFO, "No longer receiving variant %d\n", i);
00592 }
00593 }
00594 return changed;
00595 }
00596
00597 static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
00598 {
00599 HLSContext *c = s->priv_data;
00600 int ret, i, minvariant = -1;
00601
00602 if (c->first_packet) {
00603 recheck_discard_flags(s, 1);
00604 c->first_packet = 0;
00605 }
00606
00607 start:
00608 c->end_of_segment = 0;
00609 for (i = 0; i < c->n_variants; i++) {
00610 struct variant *var = c->variants[i];
00611
00612
00613 if (var->needed && !var->pkt.data) {
00614 while (1) {
00615 int64_t ts_diff;
00616 AVStream *st;
00617 ret = av_read_frame(var->ctx, &var->pkt);
00618 if (ret < 0) {
00619 if (!url_feof(&var->pb) && ret != AVERROR_EOF)
00620 return ret;
00621 reset_packet(&var->pkt);
00622 break;
00623 } else {
00624 if (c->first_timestamp == AV_NOPTS_VALUE)
00625 c->first_timestamp = var->pkt.dts;
00626 }
00627
00628 if (c->seek_timestamp == AV_NOPTS_VALUE)
00629 break;
00630
00631 if (var->pkt.dts == AV_NOPTS_VALUE) {
00632 c->seek_timestamp = AV_NOPTS_VALUE;
00633 break;
00634 }
00635
00636 st = var->ctx->streams[var->pkt.stream_index];
00637 ts_diff = av_rescale_rnd(var->pkt.dts, AV_TIME_BASE,
00638 st->time_base.den, AV_ROUND_DOWN) -
00639 c->seek_timestamp;
00640 if (ts_diff >= 0 && (c->seek_flags & AVSEEK_FLAG_ANY ||
00641 var->pkt.flags & AV_PKT_FLAG_KEY)) {
00642 c->seek_timestamp = AV_NOPTS_VALUE;
00643 break;
00644 }
00645 }
00646 }
00647
00648 if (var->pkt.data) {
00649 if(minvariant < 0) {
00650 minvariant = i;
00651 } else {
00652 struct variant *minvar = c->variants[minvariant];
00653 int64_t dts = var->pkt.dts;
00654 int64_t mindts = minvar->pkt.dts;
00655 AVStream *st = var->ctx->streams[ var->pkt.stream_index];
00656 AVStream *minst= minvar->ctx->streams[minvar->pkt.stream_index];
00657
00658 if( st->start_time != AV_NOPTS_VALUE) dts -= st->start_time;
00659 if(minst->start_time != AV_NOPTS_VALUE) mindts -= minst->start_time;
00660
00661 if (av_compare_ts(dts, st->time_base, mindts, minst->time_base) < 0)
00662 minvariant = i;
00663 }
00664 }
00665 }
00666 if (c->end_of_segment) {
00667 if (recheck_discard_flags(s, 0))
00668 goto start;
00669 }
00670
00671 if (minvariant >= 0) {
00672 *pkt = c->variants[minvariant]->pkt;
00673 pkt->stream_index += c->variants[minvariant]->stream_offset;
00674 reset_packet(&c->variants[minvariant]->pkt);
00675 return 0;
00676 }
00677 return AVERROR_EOF;
00678 }
00679
00680 static int hls_close(AVFormatContext *s)
00681 {
00682 HLSContext *c = s->priv_data;
00683
00684 free_variant_list(c);
00685 return 0;
00686 }
00687
00688 static int hls_read_seek(AVFormatContext *s, int stream_index,
00689 int64_t timestamp, int flags)
00690 {
00691 HLSContext *c = s->priv_data;
00692 int i, j, ret;
00693
00694 if ((flags & AVSEEK_FLAG_BYTE) || !c->variants[0]->finished)
00695 return AVERROR(ENOSYS);
00696
00697 c->seek_flags = flags;
00698 c->seek_timestamp = stream_index < 0 ? timestamp :
00699 av_rescale_rnd(timestamp, AV_TIME_BASE,
00700 s->streams[stream_index]->time_base.den,
00701 flags & AVSEEK_FLAG_BACKWARD ?
00702 AV_ROUND_DOWN : AV_ROUND_UP);
00703 timestamp = av_rescale_rnd(timestamp, 1, stream_index >= 0 ?
00704 s->streams[stream_index]->time_base.den :
00705 AV_TIME_BASE, flags & AVSEEK_FLAG_BACKWARD ?
00706 AV_ROUND_DOWN : AV_ROUND_UP);
00707 if (s->duration < c->seek_timestamp) {
00708 c->seek_timestamp = AV_NOPTS_VALUE;
00709 return AVERROR(EIO);
00710 }
00711
00712 ret = AVERROR(EIO);
00713 for (i = 0; i < c->n_variants; i++) {
00714
00715 struct variant *var = c->variants[i];
00716 int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? 0 :
00717 av_rescale_rnd(c->first_timestamp, 1, stream_index >= 0 ?
00718 s->streams[stream_index]->time_base.den :
00719 AV_TIME_BASE, flags & AVSEEK_FLAG_BACKWARD ?
00720 AV_ROUND_DOWN : AV_ROUND_UP);
00721 if (var->input) {
00722 ffurl_close(var->input);
00723 var->input = NULL;
00724 }
00725 av_free_packet(&var->pkt);
00726 reset_packet(&var->pkt);
00727 var->pb.eof_reached = 0;
00728
00729 var->pb.buf_end = var->pb.buf_ptr = var->pb.buffer;
00730
00731 var->pb.pos = 0;
00732
00733
00734 for (j = 0; j < var->n_segments; j++) {
00735 if (timestamp >= pos &&
00736 timestamp < pos + var->segments[j]->duration) {
00737 var->cur_seq_no = var->start_seq_no + j;
00738 ret = 0;
00739 break;
00740 }
00741 pos += var->segments[j]->duration;
00742 }
00743 if (ret)
00744 c->seek_timestamp = AV_NOPTS_VALUE;
00745 }
00746 return ret;
00747 }
00748
00749 static int hls_probe(AVProbeData *p)
00750 {
00751
00752
00753 if (strncmp(p->buf, "#EXTM3U", 7))
00754 return 0;
00755 if (strstr(p->buf, "#EXT-X-STREAM-INF:") ||
00756 strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
00757 strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:"))
00758 return AVPROBE_SCORE_MAX;
00759 return 0;
00760 }
00761
00762 AVInputFormat ff_hls_demuxer = {
00763 .name = "hls,applehttp",
00764 .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
00765 .priv_data_size = sizeof(HLSContext),
00766 .read_probe = hls_probe,
00767 .read_header = hls_read_header,
00768 .read_packet = hls_read_packet,
00769 .read_close = hls_close,
00770 .read_seek = hls_read_seek,
00771 };