SegmentTracker.cpp 9.76 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
 *
 * 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.
 *****************************************************************************/
20 21 22 23 24

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

25
#include "SegmentTracker.hpp"
26 27
#include "playlist/AbstractPlaylist.hpp"
#include "playlist/BaseRepresentation.h"
28
#include "playlist/BaseAdaptationSet.h"
29
#include "playlist/Segment.h"
30
#include "playlist/SegmentChunk.hpp"
31
#include "logic/AbstractAdaptationLogic.h"
32

33 34 35
using namespace adaptive;
using namespace adaptive::logic;
using namespace adaptive::playlist;
36

37
SegmentTrackerEvent::SegmentTrackerEvent(SegmentChunk *s)
38 39
{
    type = DISCONTINUITY;
40
    u.discontinuity.sc = s;
41 42 43 44 45 46 47 48 49
}

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

50 51 52 53 54 55
SegmentTrackerEvent::SegmentTrackerEvent(const StreamFormat *fmt)
{
    type = FORMATCHANGE;
    u.format.f = fmt;
}

56 57 58 59 60 61 62
SegmentTrackerEvent::SegmentTrackerEvent(const ID &id, bool enabled)
{
    type = BUFFERING_STATE;
    u.buffering.enabled = enabled;
    u.buffering.id = &id;
}

63
SegmentTrackerEvent::SegmentTrackerEvent(const ID &id, mtime_t min, mtime_t current, mtime_t target)
64 65
{
    type = BUFFERING_LEVEL_CHANGE;
66
    u.buffering_level.minimum = min;
67 68 69 70 71
    u.buffering_level.current = current;
    u.buffering_level.target = target;
    u.buffering.id = &id;
}

72 73 74 75 76 77 78
SegmentTrackerEvent::SegmentTrackerEvent(const ID &id, mtime_t duration)
{
    type = SEGMENT_CHANGE;
    u.segment.duration = duration;
    u.segment.id = &id;
}

79
SegmentTracker::SegmentTracker(AbstractAdaptationLogic *logic_, BaseAdaptationSet *adaptSet)
80
{
81
    first = true;
82
    curNumber = next = 0;
83
    initializing = true;
84 85
    index_sent = false;
    init_sent = false;
86
    curRepresentation = NULL;
87
    setAdaptationLogic(logic_);
88
    adaptationSet = adaptSet;
89
    format = StreamFormat::UNSUPPORTED;
90 91 92 93
}

SegmentTracker::~SegmentTracker()
{
94
    reset();
95 96 97 98 99
}

void SegmentTracker::setAdaptationLogic(AbstractAdaptationLogic *logic_)
{
    logic = logic_;
100
    registerListener(logic);
101 102
}

103
StreamFormat SegmentTracker::getCurrentFormat() const
104 105 106 107 108
{
    BaseRepresentation *rep = curRepresentation;
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);
    if(rep)
109 110 111 112
    {
        /* Ensure ephemere content is updated/loaded */
        if(rep->needsUpdate())
            (void) rep->runLocalUpdates(0, curNumber, false);
113
        return rep->getStreamFormat();
114
    }
115 116 117
    return StreamFormat();
}

118 119 120 121 122 123
bool SegmentTracker::segmentsListReady() const
{
    BaseRepresentation *rep = curRepresentation;
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);
    if(rep && rep->getPlaylist()->isLive())
124
        return rep->getMinAheadTime(curNumber) > 0;
125 126 127
    return true;
}

128
void SegmentTracker::reset()
129
{
130 131
    notify(SegmentTrackerEvent(curRepresentation, NULL));
    curRepresentation = NULL;
132 133 134
    init_sent = false;
    index_sent = false;
    initializing = true;
135
    format = StreamFormat::UNSUPPORTED;
136 137
}

138 139
SegmentChunk * SegmentTracker::getNextChunk(bool switch_allowed,
                                            AbstractConnectionManager *connManager)
140
{
141
    BaseRepresentation *rep = NULL, *prevRep = NULL;
142 143
    ISegment *segment;

144
    if(!adaptationSet)
145 146
        return NULL;

147
    /* Ensure we don't keep chaining init/index without data */
148 149
    if( initializing )
    {
150
        if( curRepresentation )
151 152 153 154
            switch_allowed = false;
        else
            switch_allowed = true;
    }
155

156
    if( !switch_allowed ||
157 158
       (curRepresentation && curRepresentation->getSwitchPolicy() == SegmentInformation::SWITCH_UNAVAILABLE) )
        rep = curRepresentation;
159
    else
160
        rep = logic->getNextRepresentation(adaptationSet, curRepresentation);
161 162 163 164

    if ( rep == NULL )
            return NULL;

165

166
    if(rep != curRepresentation)
167
    {
168 169 170
        notify(SegmentTrackerEvent(curRepresentation, rep));
        prevRep = curRepresentation;
        curRepresentation = rep;
171
        init_sent = false;
172
        index_sent = false;
173 174 175
        initializing = true;
    }

176
    bool b_updated = false;
177 178
    /* Ensure ephemere content is updated/loaded */
    if(rep->needsUpdate())
179
        b_updated = rep->runLocalUpdates(getPlaybackTime(), curNumber, false);
180 181 182 183

    if(prevRep && !rep->consistentSegmentNumber())
    {
        /* Convert our segment number */
184
        next = rep->translateSegmentNumber(next, prevRep);
185
    }
186 187
    else if(first && rep->getPlaylist()->isLive())
    {
188
        next = rep->getLiveStartSegmentNumber(next);
189 190
        first = false;
    }
191

192 193 194
    if(b_updated)
    {
        if(!rep->consistentSegmentNumber())
195
            curRepresentation->pruneBySegmentNumber(curNumber);
196
        curRepresentation->scheduleNextUpdate(next);
197
    }
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    if(rep->getStreamFormat() != format)
    {
        /* Initial format ? */
        if(format == StreamFormat(StreamFormat::UNSUPPORTED))
        {
            format = rep->getStreamFormat();
        }
        else
        {
            format = rep->getStreamFormat();
            notify(SegmentTrackerEvent(&format)); /* Notify new demux format */
            return NULL; /* Force current demux to end */
        }
    }

    if(format == StreamFormat(StreamFormat::UNSUPPORTED))
    {
        return NULL; /* Can't return chunk because no demux will be created */
    }

219
    if(!init_sent)
220
    {
221
        init_sent = true;
222
        segment = rep->getSegment(BaseRepresentation::INFOTYPE_INIT);
223
        if(segment)
224
            return segment->toChunk(next, rep, connManager);
225 226
    }

227
    if(!index_sent)
228
    {
229
        index_sent = true;
230 231
        segment = rep->getSegment(BaseRepresentation::INFOTYPE_INDEX);
        if(segment)
232
            return segment->toChunk(next, rep, connManager);
233 234
    }

235
    bool b_gap = false;
236
    segment = rep->getNextSegment(BaseRepresentation::INFOTYPE_MEDIA, next, &next, &b_gap);
237 238
    if(!segment)
    {
239
        return NULL;
240
    }
241

242 243 244 245 246 247
    if(initializing)
    {
        b_gap = false;
        /* stop initializing after 1st chunk */
        initializing = false;
    }
248

249
    SegmentChunk *chunk = segment->toChunk(next, rep, connManager);
250

251 252 253 254 255 256 257 258
    /* Notify new segment length for stats / logic */
    if(chunk)
    {
        const Timescale timescale = rep->inheritTimescale();
        notify(SegmentTrackerEvent(rep->getAdaptationSet()->getID(),
                                   timescale.ToTime(segment->duration.Get())));
    }

259
    /* We need to check segment/chunk format changes, as we can't rely on representation's (HLS)*/
260
    if(chunk && format != chunk->getStreamFormat())
261 262 263 264 265 266
    {
        format = chunk->getStreamFormat();
        notify(SegmentTrackerEvent(&format));
    }

    /* Handle both implicit and explicit discontinuities */
267
    if( (b_gap && next) || (chunk && chunk->discontinuity) )
268 269 270 271
    {
        notify(SegmentTrackerEvent(chunk));
    }

272
    if(chunk)
273 274 275 276
    {
        curNumber = next;
        next++;
    }
277

278 279
    return chunk;
}
280

281
bool SegmentTracker::setPositionByTime(mtime_t time, bool restarted, bool tryonly)
282
{
283
    uint64_t segnumber;
284
    BaseRepresentation *rep = curRepresentation;
285 286 287 288 289
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);

    if(rep &&
       rep->getSegmentNumberByTime(time, &segnumber))
290 291
    {
        if(!tryonly)
292
            setPositionByNumber(segnumber, restarted);
293 294 295 296
        return true;
    }
    return false;
}
297

298 299 300 301 302 303 304 305
void SegmentTracker::setPositionByNumber(uint64_t segnumber, bool restarted)
{
    if(restarted)
    {
        initializing = true;
        index_sent = false;
        init_sent = false;
    }
306
    curNumber = next = segnumber;
307 308
}

309
mtime_t SegmentTracker::getPlaybackTime() const
310
{
311
    mtime_t time, duration;
312 313 314 315 316 317 318

    BaseRepresentation *rep = curRepresentation;
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);

    if(rep &&
       rep->getPlaybackTimeDurationBySegmentNumber(next, &time, &duration))
319 320 321 322
    {
        return time;
    }
    return 0;
323
}
324

325 326 327 328 329 330
mtime_t SegmentTracker::getMinAheadTime() const
{
    BaseRepresentation *rep = curRepresentation;
    if(!rep)
        rep = logic->getNextRepresentation(adaptationSet, NULL);
    if(rep)
331
        return rep->getMinAheadTime(curNumber);
332 333 334
    return 0;
}

335 336 337 338 339
void SegmentTracker::notifyBufferingState(bool enabled) const
{
    notify(SegmentTrackerEvent(adaptationSet->getID(), enabled));
}

340
void SegmentTracker::notifyBufferingLevel(mtime_t min, mtime_t current, mtime_t target) const
341
{
342
    notify(SegmentTrackerEvent(adaptationSet->getID(), min, current, target));
343 344
}

345 346 347 348 349
void SegmentTracker::registerListener(SegmentTrackerListenerInterface *listener)
{
    listeners.push_back(listener);
}

350 351
void SegmentTracker::updateSelected()
{
352 353
    if(curRepresentation && curRepresentation->needsUpdate())
    {
354 355
        curRepresentation->runLocalUpdates(getPlaybackTime(), curNumber, true);
        curRepresentation->scheduleNextUpdate(curNumber);
356
    }
357
}
358

359
void SegmentTracker::notify(const SegmentTrackerEvent &event) const
360 361 362
{
    std::list<SegmentTrackerListenerInterface *>::const_iterator it;
    for(it=listeners.begin();it != listeners.end(); ++it)
363
        (*it)->trackerEvent(event);
364
}