Ticket #649: phmi-20111115T1452-ffmpeg-fix_seeking_when_using_apple_http_live_streaming.diff

File phmi-20111115T1452-ffmpeg-fix_seeking_when_using_apple_http_live_streaming.diff, 2.9 KB (added by takis, 15 years ago)

This patch changes this demuxers seeking implementation to use the initial DTS as an offset for searching the segments containing the requested timestamp.

  • libavformat/applehttp.c

    From 1adcc7e6eb6e369ea8456881199fd0264578b4d2 Mon Sep 17 00:00:00 2001
    From: "Panagiotis H.M. Issaris" <takis.issaris@uhasselt.be>
    Date: Tue, 15 Nov 2011 14:26:17 +0100
    Subject: [PATCH] Fix seeking when using Apple HTTP Live Streaming
    
    The Apple HTTP Live Streaming demuxer's implementation of seeking searches for
    the MPEG TS segment which contains the requested timestamp.  In its current
    implementation it assumes that the first segment will start from 0.
    
    But, MPEG TS streams do not necessarily start with timestamp (near) 0, causing
    seeking to fail for those streams.
    
    This also occurs when using live streaming of HTTP Live Streams. In this case
    sliding playlists may be used, which means that in that case only the last x
    encoded segments are stored, the earlier segments get deleted from disk and
    removed from the playlist. Because of this, when starting playback of a stream
    in the middle of such a broadcast, the initial segment fetched after parsing
    the m3u8 playlist will not start from timestamp (near) 0, causing (the
    admittedly limited live) seeking to fail.
    
    This patch changes this demuxers seeking implementation to use the initial DTS
    as an offset for searching the segments containing the requested timestamp.
    ---
     libavformat/applehttp.c |   12 ++++++++++--
     1 files changed, 10 insertions(+), 2 deletions(-)
    
    diff --git a/libavformat/applehttp.c b/libavformat/applehttp.c
    index 97bd74a..dc5bc07 100644
    a b typedef struct AppleHTTPContext {  
    9999    int cur_seq_no;
    100100    int end_of_segment;
    101101    int first_packet;
     102    int64_t first_timestamp;
    102103    AVIOInterruptCB *interrupt_callback;
    103104} AppleHTTPContext;
    104105
    … … static int applehttp_read_header(AVFormatContext *s, AVFormatParameters *ap)  
    527528    }
    528529
    529530    c->first_packet = 1;
     531    c->first_timestamp = AV_NOPTS_VALUE;
    530532
    531533    return 0;
    532534fail:
    … … start:  
    591593                if (!url_feof(&var->pb))
    592594                    return ret;
    593595                reset_packet(&var->pkt);
     596            } else {
     597                if (c->first_timestamp == AV_NOPTS_VALUE)
     598                    c->first_timestamp = var->pkt.dts;
    594599            }
    595600        }
    596601        /* Check if this stream has the packet with the lowest dts */
    … … static int applehttp_read_seek(AVFormatContext *s, int stream_index,  
    639644    for (i = 0; i < c->n_variants; i++) {
    640645        /* Reset reading */
    641646        struct variant *var = c->variants[i];
    642         int64_t pos = 0;
    643         if (var->input) {
     647        int64_t pos = av_rescale_rnd(c->first_timestamp, 1, stream_index >= 0 ?
     648                               s->streams[stream_index]->time_base.den :
     649                               AV_TIME_BASE, flags & AVSEEK_FLAG_BACKWARD ?
     650                               AV_ROUND_DOWN : AV_ROUND_UP);
     651         if (var->input) {
    644652            ffurl_close(var->input);
    645653            var->input = NULL;
    646654        }