~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/applehttp.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
#include "libavutil/avstring.h"
29
29
#include "libavutil/intreadwrite.h"
 
30
#include "libavutil/mathematics.h"
30
31
#include "libavutil/opt.h"
31
32
#include "libavutil/dict.h"
32
33
#include "avformat.h"
98
99
    int cur_seq_no;
99
100
    int end_of_segment;
100
101
    int first_packet;
 
102
    int64_t first_timestamp;
 
103
    AVIOInterruptCB *interrupt_callback;
101
104
} AppleHTTPContext;
102
105
 
103
106
static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
129
132
            ffurl_close(var->input);
130
133
        if (var->ctx) {
131
134
            var->ctx->pb = NULL;
132
 
            av_close_input_file(var->ctx);
 
135
            avformat_close_input(&var->ctx);
133
136
        }
134
137
        av_free(var);
135
138
    }
201
204
    enum KeyType key_type = KEY_NONE;
202
205
    uint8_t iv[16] = "";
203
206
    int has_iv = 0;
204
 
    char key[MAX_URL_SIZE];
 
207
    char key[MAX_URL_SIZE] = "";
205
208
    char line[1024];
206
209
    const char *ptr;
207
210
    int close_in = 0;
208
211
 
209
212
    if (!in) {
210
213
        close_in = 1;
211
 
        if ((ret = avio_open(&in, url, AVIO_FLAG_READ)) < 0)
 
214
        if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
 
215
                              c->interrupt_callback, NULL)) < 0)
212
216
            return ret;
213
217
    }
214
218
 
321
325
{
322
326
    struct segment *seg = var->segments[var->cur_seq_no - var->start_seq_no];
323
327
    if (seg->key_type == KEY_NONE) {
324
 
        return ffurl_open(&var->input, seg->url, AVIO_FLAG_READ);
 
328
        return ffurl_open(&var->input, seg->url, AVIO_FLAG_READ,
 
329
                          &var->parent->interrupt_callback, NULL);
325
330
    } else if (seg->key_type == KEY_AES_128) {
326
331
        char iv[33], key[33], url[MAX_URL_SIZE];
327
332
        int ret;
328
333
        if (strcmp(seg->key, var->key_url)) {
329
334
            URLContext *uc;
330
 
            if (ffurl_open(&uc, seg->key, AVIO_FLAG_READ) == 0) {
 
335
            if (ffurl_open(&uc, seg->key, AVIO_FLAG_READ,
 
336
                           &var->parent->interrupt_callback, NULL) == 0) {
331
337
                if (ffurl_read_complete(uc, var->key, sizeof(var->key))
332
338
                    != sizeof(var->key)) {
333
339
                    av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
347
353
            snprintf(url, sizeof(url), "crypto+%s", seg->url);
348
354
        else
349
355
            snprintf(url, sizeof(url), "crypto:%s", seg->url);
350
 
        if ((ret = ffurl_alloc(&var->input, url, AVIO_FLAG_READ)) < 0)
 
356
        if ((ret = ffurl_alloc(&var->input, url, AVIO_FLAG_READ,
 
357
                               &var->parent->interrupt_callback)) < 0)
351
358
            return ret;
352
 
        av_set_string3(var->input->priv_data, "key", key, 0, NULL);
353
 
        av_set_string3(var->input->priv_data, "iv", iv, 0, NULL);
354
 
        if ((ret = ffurl_connect(var->input)) < 0) {
 
359
        av_opt_set(var->input->priv_data, "key", key, 0);
 
360
        av_opt_set(var->input->priv_data, "iv", iv, 0);
 
361
        if ((ret = ffurl_connect(var->input, NULL)) < 0) {
355
362
            ffurl_close(var->input);
356
363
            var->input = NULL;
357
364
            return ret;
369
376
 
370
377
restart:
371
378
    if (!v->input) {
 
379
        /* If this is a live stream and the reload interval has elapsed since
 
380
         * the last playlist reload, reload the variant playlists now. */
 
381
        int64_t reload_interval = v->n_segments > 0 ?
 
382
                                  v->segments[v->n_segments - 1]->duration :
 
383
                                  v->target_duration;
 
384
        reload_interval *= 1000000;
 
385
 
372
386
reload:
373
 
        /* If this is a live stream and target_duration has elapsed since
374
 
         * the last playlist reload, reload the variant playlists now. */
375
387
        if (!v->finished &&
376
 
            av_gettime() - v->last_load_time >= v->target_duration*1000000 &&
377
 
            (ret = parse_playlist(c, v->url, v, NULL)) < 0)
 
388
            av_gettime() - v->last_load_time >= reload_interval) {
 
389
            if ((ret = parse_playlist(c, v->url, v, NULL)) < 0)
378
390
                return ret;
 
391
            /* If we need to reload the playlist again below (if
 
392
             * there's still no more segments), switch to a reload
 
393
             * interval of half the target duration. */
 
394
            reload_interval = v->target_duration * 500000;
 
395
        }
379
396
        if (v->cur_seq_no < v->start_seq_no) {
380
397
            av_log(NULL, AV_LOG_WARNING,
381
398
                   "skipping %d segments ahead, expired from playlists\n",
385
402
        if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
386
403
            if (v->finished)
387
404
                return AVERROR_EOF;
388
 
            while (av_gettime() - v->last_load_time <
389
 
                   v->target_duration*1000000) {
390
 
                if (url_interrupt_cb())
 
405
            while (av_gettime() - v->last_load_time < reload_interval) {
 
406
                if (ff_check_interrupt(c->interrupt_callback))
391
407
                    return AVERROR_EXIT;
392
408
                usleep(100*1000);
393
409
            }
411
427
    c->end_of_segment = 1;
412
428
    c->cur_seq_no = v->cur_seq_no;
413
429
 
414
 
    if (v->ctx) {
 
430
    if (v->ctx && v->ctx->nb_streams) {
415
431
        v->needed = 0;
416
432
        for (i = v->stream_offset; i < v->stream_offset + v->ctx->nb_streams;
417
433
             i++) {
432
448
    AppleHTTPContext *c = s->priv_data;
433
449
    int ret = 0, i, j, stream_offset = 0;
434
450
 
 
451
    c->interrupt_callback = &s->interrupt_callback;
 
452
 
435
453
    if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
436
454
        goto fail;
437
455
 
494
512
        v->pb.seekable = 0;
495
513
        ret = av_probe_input_buffer(&v->pb, &in_fmt, v->segments[0]->url,
496
514
                                    NULL, 0, 0);
497
 
        if (ret < 0)
 
515
        if (ret < 0) {
 
516
            /* Free the ctx - it isn't initialized properly at this point,
 
517
             * so avformat_close_input shouldn't be called. If
 
518
             * avformat_open_input fails below, it frees and zeros the
 
519
             * context, so it doesn't need any special treatment like this. */
 
520
            avformat_free_context(v->ctx);
 
521
            v->ctx = NULL;
498
522
            goto fail;
 
523
        }
499
524
        v->ctx->pb       = &v->pb;
500
525
        ret = avformat_open_input(&v->ctx, v->segments[0]->url, in_fmt, NULL);
501
526
        if (ret < 0)
504
529
        snprintf(bitrate_str, sizeof(bitrate_str), "%d", v->bandwidth);
505
530
        /* Create new AVStreams for each stream in this variant */
506
531
        for (j = 0; j < v->ctx->nb_streams; j++) {
507
 
            AVStream *st = av_new_stream(s, i);
 
532
            AVStream *st = avformat_new_stream(s, NULL);
508
533
            if (!st) {
509
534
                ret = AVERROR(ENOMEM);
510
535
                goto fail;
511
536
            }
 
537
            st->id = i;
512
538
            avcodec_copy_context(st->codec, v->ctx->streams[j]->codec);
513
539
            if (v->bandwidth)
514
540
                av_dict_set(&st->metadata, "variant_bitrate", bitrate_str,
518
544
    }
519
545
 
520
546
    c->first_packet = 1;
 
547
    c->first_timestamp = AV_NOPTS_VALUE;
521
548
 
522
549
    return 0;
523
550
fail:
582
609
                if (!var->pb.eof_reached)
583
610
                    return ret;
584
611
                reset_packet(&var->pkt);
 
612
            } else {
 
613
                if (c->first_timestamp == AV_NOPTS_VALUE)
 
614
                    c->first_timestamp = var->pkt.dts;
585
615
            }
586
616
        }
587
617
        /* Check if this stream has the packet with the lowest dts */
630
660
    for (i = 0; i < c->n_variants; i++) {
631
661
        /* Reset reading */
632
662
        struct variant *var = c->variants[i];
633
 
        int64_t pos = 0;
 
663
        int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? 0 :
 
664
                      av_rescale_rnd(c->first_timestamp, 1,
 
665
                          stream_index >= 0 ? s->streams[stream_index]->time_base.den : AV_TIME_BASE,
 
666
                          flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
634
667
        if (var->input) {
635
668
            ffurl_close(var->input);
636
669
            var->input = NULL;
667
700
}
668
701
 
669
702
AVInputFormat ff_applehttp_demuxer = {
670
 
    "applehttp",
671
 
    NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming format"),
672
 
    sizeof(AppleHTTPContext),
673
 
    applehttp_probe,
674
 
    applehttp_read_header,
675
 
    applehttp_read_packet,
676
 
    applehttp_close,
677
 
    applehttp_read_seek,
 
703
    .name           = "applehttp",
 
704
    .long_name      = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming format"),
 
705
    .priv_data_size = sizeof(AppleHTTPContext),
 
706
    .read_probe     = applehttp_probe,
 
707
    .read_header    = applehttp_read_header,
 
708
    .read_packet    = applehttp_read_packet,
 
709
    .read_close     = applehttp_close,
 
710
    .read_seek      = applehttp_read_seek,
678
711
};