RateBasedAdaptationLogic.cpp 3.27 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * RateBasedAdaptationLogic.cpp
 *****************************************************************************
 * Copyright (C) 2010 - 2011 Klagenfurt University
 *
 * Created on: Aug 10, 2010
 * Authors: Christopher Mueller <christopher.mueller@itec.uni-klu.ac.at>
 *          Christian Timmerer  <christian.timmerer@itec.uni-klu.ac.at>
 *
10 11
 * 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
12 13 14 15 16
 * 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
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19 20
 *
 * You should have received a copy of the GNU Lesser General Public License
21 22
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "RateBasedAdaptationLogic.h"
29
#include "Representationselectors.hpp"
30 31 32 33 34 35

using namespace dash::logic;
using namespace dash::xml;
using namespace dash::http;
using namespace dash::mpd;

36
RateBasedAdaptationLogic::RateBasedAdaptationLogic  (MPDManager *mpdManager, stream_t *stream) :
37 38 39 40 41 42
                          AbstractAdaptationLogic   (mpdManager, stream),
                          mpdManager                (mpdManager),
                          count                     (0),
                          currentPeriod             (mpdManager->getFirstPeriod()),
                          width                     (0),
                          height                    (0)
43
{
44 45
    this->width  = var_InheritInteger(stream, "dash-prefwidth");
    this->height = var_InheritInteger(stream, "dash-prefheight");
46 47
}

48
Chunk*  RateBasedAdaptationLogic::getNextChunk()
49 50
{
    if(this->currentPeriod == NULL)
51
        return NULL;
52

53 54
    const Representation *rep = getCurrentRepresentation();
    if (!rep)
55
        return NULL;
56

57
    std::vector<Segment *> segments = rep->getSegments();
58

59
    if ( this->count == segments.size() )
60 61 62 63 64 65
    {
        this->currentPeriod = this->mpdManager->getNextPeriod(this->currentPeriod);
        this->count = 0;
        return this->getNextChunk();
    }

66
    if ( segments.size() > this->count )
67
    {
68
        Segment *seg = segments.at( this->count );
69
        Chunk *chunk = seg->toChunk();
70 71 72 73
        //In case of UrlTemplate, we must stay on the same segment.
        if ( seg->isSingleShot() == true )
            this->count++;
        seg->done();
74
        return chunk;
75
    }
76
    return NULL;
77
}
78 79 80

const Representation *RateBasedAdaptationLogic::getCurrentRepresentation() const
{
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if(currentPeriod == NULL)
        return NULL;

    uint64_t bitrate = this->getBpsAvg();
    if(getBufferPercent() < MINBUFFER)
        bitrate = 0;

    RepresentationSelector selector;
    Representation *rep = selector.select(currentPeriod, bitrate, width, height);
    if ( rep == NULL )
    {
        rep = selector.select(currentPeriod);
        if ( rep == NULL )
            return NULL;
    }
    return rep;
97
}