Skip to content

Commit

Permalink
avformat/hls: fix duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedirc Fung authored and bbcallen committed Jul 11, 2013
1 parent 8cccac7 commit 7d154d2
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions libavformat/hls.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Apple HTTP Live Streaming demuxer
* Copyright (c) 2010 Martin Storsjo
* Copyright (c) 2011 Cedirc Fung (wolfplanet@gmail.com)
*
* This file is part of FFmpeg.
*
Expand Down Expand Up @@ -56,7 +57,8 @@ enum KeyType {
};

struct segment {
int duration;
int previous_duration; // in seconds
int duration; // in seconds
char url[MAX_URL_SIZE];
char key[MAX_URL_SIZE];
enum KeyType key_type;
Expand Down Expand Up @@ -207,7 +209,7 @@ static void handle_key_args(struct key_info *info, const char *key,
static int parse_playlist(HLSContext *c, const char *url,
struct variant *var, AVIOContext *in)
{
int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0, previous_duration1 = 0, previous_duration = 0;
enum KeyType key_type = KEY_NONE;
uint8_t iv[16] = "";
int has_iv = 0;
Expand Down Expand Up @@ -299,6 +301,8 @@ static int parse_playlist(HLSContext *c, const char *url,
} else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
if (var)
var->finished = 1;
} else if (av_strstart(line, "#EXT-X-DISCONTINUITY", &ptr)) {
previous_duration = previous_duration1;
} else if (av_strstart(line, "#EXTINF:", &ptr)) {
is_segment = 1;
duration = atoi(ptr);
Expand Down Expand Up @@ -327,6 +331,8 @@ static int parse_playlist(HLSContext *c, const char *url,
ret = AVERROR(ENOMEM);
goto fail;
}
previous_duration1 += duration;
seg->previous_duration = previous_duration;
seg->duration = duration;
seg->key_type = key_type;
if (has_iv) {
Expand Down Expand Up @@ -750,9 +756,16 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
}
/* If we got a packet, return it */
if (minvariant >= 0) {
*pkt = c->variants[minvariant]->pkt;
pkt->stream_index += c->variants[minvariant]->stream_offset;
reset_packet(&c->variants[minvariant]->pkt);
struct variant *v = c->variants[minvariant];
*pkt = v->pkt;
pkt->stream_index += v->stream_offset;
int seq_no = v->cur_seq_no - v->start_seq_no;
if (seq_no < v->n_segments && s->streams[pkt->stream_index]) {
int64_t pred = v->segments[seq_no]->previous_duration / av_q2d(s->streams[pkt->stream_index]->time_base);
if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < pred) pkt->dts += pred;
if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < pred) pkt->pts += pred;
}
reset_packet(&v->pkt);
return 0;
}
return AVERROR_EOF;
Expand Down

0 comments on commit 7d154d2

Please sign in to comment.