SegmentTracker.cpp 7.61 KB
Newer Older
1 2 3
/*
 * SegmentTracker.cpp
 *****************************************************************************
4
 * Copyright (C) 2014 - VideoLAN and VLC authors
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#include "SegmentTracker.hpp"
21 22
#include "playlist/AbstractPlaylist.hpp"
#include "playlist/BaseRepresentation.h"
23
#include "playlist/BaseAdaptationSet.h"
24
#include "playlist/Segment.h"
25
#include "playlist/SegmentChunk.hpp"
26
#include "logic/AbstractAdaptationLogic.h"
27

28 29 30
using namespace adaptive;
using namespace adaptive::logic;
using namespace adaptive::playlist;
31

32
SegmentTrackerEvent::SegmentTrackerEvent(SegmentChunk *s)
33 34
{
    type = DISCONTINUITY;
35
    u.discontinuity.sc = s;
36 37 38 39 40 41 42 43 44
}

SegmentTrackerEvent::SegmentTrackerEvent(BaseRepresentation *prev, BaseRepresentation *next)
{
    type = SWITCHING;
    u.switching.prev = prev;
    u.switching.next = next;
}

45 46 47 48 49 50
SegmentTrackerEvent::SegmentTrackerEvent(const StreamFormat *fmt)
{
    type = FORMATCHANGE;
    u.format.f = fmt;
}

51
SegmentTracker::SegmentTracker(AbstractAdaptationLogic *logic_, BaseAdaptationSet *adaptSet)
52
{
53
    first = true;
54
    curNumber = next = 0;
55
    initializing = true;
56 57
    index_sent = false;
    init_sent = false;
58
    curRepresentation = NULL;
59
    setAdaptationLogic(logic_);
60
    adaptationSet = adaptSet;
61
    format = StreamFormat::UNSUPPORTED;
62 63 64 65
}

SegmentTracker::~SegmentTracker()
{
66
    reset();
67 68 69 70 71
}

void SegmentTracker::setAdaptationLogic(AbstractAdaptationLogic *logic_)
{
    logic = logic_;
72
    registerListener(logic);
73 74
}

75 76 77 78 79 80 81 82 83 84
StreamFormat SegmentTracker::initialFormat() const
{
    BaseRepresentation *rep = curRepresentation;
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);
    if(rep)
        return rep->getStreamFormat();
    return StreamFormat();
}

85 86 87 88 89 90
bool SegmentTracker::segmentsListReady() const
{
    BaseRepresentation *rep = curRepresentation;
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);
    if(rep && rep->getPlaylist()->isLive())
91
        return rep->getMinAheadTime(curNumber) > 0;
92 93 94
    return true;
}

95
void SegmentTracker::reset()
96
{
97 98
    notify(SegmentTrackerEvent(curRepresentation, NULL));
    curRepresentation = NULL;
99 100 101
    init_sent = false;
    index_sent = false;
    initializing = true;
102
    format = StreamFormat::UNSUPPORTED;
103 104
}

105
SegmentChunk * SegmentTracker::getNextChunk(bool switch_allowed, HTTPConnectionManager *connManager)
106
{
107
    BaseRepresentation *rep = NULL, *prevRep = NULL;
108 109
    ISegment *segment;

110
    if(!adaptationSet)
111 112
        return NULL;

113
    /* Ensure we don't keep chaining init/index without data */
114 115
    if( initializing )
    {
116
        if( curRepresentation )
117 118 119 120
            switch_allowed = false;
        else
            switch_allowed = true;
    }
121

122
    if( !switch_allowed ||
123 124
       (curRepresentation && curRepresentation->getSwitchPolicy() == SegmentInformation::SWITCH_UNAVAILABLE) )
        rep = curRepresentation;
125
    else
126
        rep = logic->getNextRepresentation(adaptationSet, curRepresentation);
127 128 129 130

    if ( rep == NULL )
            return NULL;

131

132
    if(rep != curRepresentation)
133
    {
134 135 136
        notify(SegmentTrackerEvent(curRepresentation, rep));
        prevRep = curRepresentation;
        curRepresentation = rep;
137
        init_sent = false;
138
        index_sent = false;
139 140 141
        initializing = true;
    }

142
    bool b_updated = false;
143 144
    /* Ensure ephemere content is updated/loaded */
    if(rep->needsUpdate())
145
        b_updated = rep->runLocalUpdates(getPlaybackTime(), curNumber, false);
146 147 148 149

    if(prevRep && !rep->consistentSegmentNumber())
    {
        /* Convert our segment number */
150
        next = rep->translateSegmentNumber(next, prevRep);
151
    }
152 153
    else if(first && rep->getPlaylist()->isLive())
    {
154
        next = rep->getLiveStartSegmentNumber(next);
155 156
        first = false;
    }
157

158 159 160
    if(b_updated)
    {
        if(!rep->consistentSegmentNumber())
161 162
            curRepresentation->pruneBySegmentNumber(curNumber);
        curRepresentation->scheduleNextUpdate(curNumber);
163
    }
164

165
    if(!init_sent)
166
    {
167
        init_sent = true;
168
        segment = rep->getSegment(BaseRepresentation::INFOTYPE_INIT);
169
        if(segment)
170
            return segment->toChunk(next, rep, connManager);
171 172
    }

173
    if(!index_sent)
174
    {
175
        index_sent = true;
176 177
        segment = rep->getSegment(BaseRepresentation::INFOTYPE_INDEX);
        if(segment)
178
            return segment->toChunk(next, rep, connManager);
179 180
    }

181
    bool b_gap = false;
182
    segment = rep->getNextSegment(BaseRepresentation::INFOTYPE_MEDIA, next, &next, &b_gap);
183 184
    if(!segment)
    {
185
        reset();
186
        return NULL;
187
    }
188

189 190 191 192 193 194
    if(initializing)
    {
        b_gap = false;
        /* stop initializing after 1st chunk */
        initializing = false;
    }
195

196
    SegmentChunk *chunk = segment->toChunk(next, rep, connManager);
197

198
    /* We need to check segment/chunk format changes, as we can't rely on representation's (HLS)*/
199
    if(chunk && format != chunk->getStreamFormat())
200 201 202 203 204 205
    {
        format = chunk->getStreamFormat();
        notify(SegmentTrackerEvent(&format));
    }

    /* Handle both implicit and explicit discontinuities */
206
    if( (b_gap && next) || (chunk && chunk->discontinuity) )
207 208 209 210
    {
        notify(SegmentTrackerEvent(chunk));
    }

211
    if(chunk)
212 213 214 215
    {
        curNumber = next;
        next++;
    }
216

217 218
    return chunk;
}
219

220
bool SegmentTracker::setPositionByTime(mtime_t time, bool restarted, bool tryonly)
221
{
222
    uint64_t segnumber;
223
    BaseRepresentation *rep = curRepresentation;
224 225 226 227 228
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);

    if(rep &&
       rep->getSegmentNumberByTime(time, &segnumber))
229 230
    {
        if(!tryonly)
231
            setPositionByNumber(segnumber, restarted);
232 233 234 235
        return true;
    }
    return false;
}
236

237 238 239 240 241 242 243 244
void SegmentTracker::setPositionByNumber(uint64_t segnumber, bool restarted)
{
    if(restarted)
    {
        initializing = true;
        index_sent = false;
        init_sent = false;
    }
245
    curNumber = next = segnumber;
246 247
}

248
mtime_t SegmentTracker::getPlaybackTime() const
249
{
250
    if(curRepresentation)
251
        return curRepresentation->getPlaybackTimeBySegmentNumber(next);
252 253 254
    else
        return 0;
}
255

256 257 258 259 260 261
mtime_t SegmentTracker::getMinAheadTime() const
{
    BaseRepresentation *rep = curRepresentation;
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);
    if(rep)
262
        return rep->getMinAheadTime(curNumber);
263 264 265
    return 0;
}

266 267 268 269 270
void SegmentTracker::registerListener(SegmentTrackerListenerInterface *listener)
{
    listeners.push_back(listener);
}

271 272
void SegmentTracker::updateSelected()
{
273 274
    if(curRepresentation && curRepresentation->needsUpdate())
    {
275 276
        curRepresentation->runLocalUpdates(getPlaybackTime(), curNumber, true);
        curRepresentation->scheduleNextUpdate(curNumber);
277
    }
278
}
279

280
void SegmentTracker::notify(const SegmentTrackerEvent &event)
281 282 283
{
    std::list<SegmentTrackerListenerInterface *>::const_iterator it;
    for(it=listeners.begin();it != listeners.end(); ++it)
284
        (*it)->trackerEvent(event);
285
}