RateBasedAdaptationLogic.cpp 3.48 KB
Newer Older
Christopher Mueller's avatar
Christopher Mueller committed
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
Christopher Mueller's avatar
Christopher Mueller committed
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.
Christopher Mueller's avatar
Christopher Mueller committed
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.
Christopher Mueller's avatar
Christopher Mueller committed
23
 *****************************************************************************/
24
25
26

#define __STDC_CONSTANT_MACROS

Christopher Mueller's avatar
Christopher Mueller committed
27
28
29
30
31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "RateBasedAdaptationLogic.h"
32
#include "Representationselectors.hpp"
Christopher Mueller's avatar
Christopher Mueller committed
33

34
35
#include "../playlist/BaseRepresentation.h"
#include "../playlist/BasePeriod.h"
36

37
using namespace adaptative::logic;
Christopher Mueller's avatar
Christopher Mueller committed
38

39
40
RateBasedAdaptationLogic::RateBasedAdaptationLogic  (int w, int h) :
                          AbstractAdaptationLogic   (),
41
                          bpsAvg(0), bpsRemainder(0), bpsSamplecount(0),
42
                          currentBps(0)
Christopher Mueller's avatar
Christopher Mueller committed
43
{
44
45
    width  = w;
    height = h;
46
47
    cumulatedTime = 0;
    stabilizer = 16;
Christopher Mueller's avatar
Christopher Mueller committed
48
49
}

50
BaseRepresentation *RateBasedAdaptationLogic::getCurrentRepresentation(BaseAdaptationSet *adaptSet) const
51
{
52
    if(adaptSet == NULL)
53
54
55
        return NULL;

    RepresentationSelector selector;
56
    BaseRepresentation *rep = selector.select(adaptSet, currentBps, width, height);
57
58
    if ( rep == NULL )
    {
59
        rep = selector.select(adaptSet);
60
61
62
63
        if ( rep == NULL )
            return NULL;
    }
    return rep;
64
}
65
66
67
68
69
70

void RateBasedAdaptationLogic::updateDownloadRate(size_t size, mtime_t time)
{
    if(unlikely(time == 0))
        return;

71
    size_t current = bpsRemainder + size * 8000 / time;
72
73

    if (current >= bpsAvg)
74
75
76
77
    {
        bpsAvg += (current - bpsAvg) / ++bpsSamplecount;
        bpsRemainder = (current - bpsAvg) % bpsSamplecount;
    }
78
    else
79
80
81
82
    {
        bpsAvg -= (bpsAvg - current) / ++bpsSamplecount;
        bpsRemainder = (bpsAvg - current) % bpsSamplecount;
    }
83

84
85
86
87
88
89
90
91
92
93
94
    cumulatedTime += time;
    if(cumulatedTime > 4 * CLOCK_FREQ / stabilizer)
    {
        if( currentBps <= bpsAvg * 3/4 && stabilizer < 16 )
        {
            stabilizer++;
        }
        else if( currentBps > bpsAvg * 3/4 && stabilizer > 1 )
        {
            stabilizer /= 2;
        }
95

96
97
98
        currentBps = bpsAvg * 3/4;
        cumulatedTime = 0;
    }
99
}
100

101
102
FixedRateAdaptationLogic::FixedRateAdaptationLogic(size_t bps) :
    AbstractAdaptationLogic()
103
{
104
    currentBps = bps;
105
106
}

107
BaseRepresentation *FixedRateAdaptationLogic::getCurrentRepresentation(BaseAdaptationSet *adaptSet) const
108
{
109
    if(adaptSet == NULL)
110
111
112
        return NULL;

    RepresentationSelector selector;
113
    BaseRepresentation *rep = selector.select(adaptSet, currentBps);
114
115
    if ( rep == NULL )
    {
116
        rep = selector.select(adaptSet);
117
118
119
120
121
        if ( rep == NULL )
            return NULL;
    }
    return rep;
}