aboutsummaryrefslogtreecommitdiffstats
path: root/Adaptation/RateBasedAdaptation.cpp
blob: 73d4ee9b60dca38cb64f94d621e6c9214366ba4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "RateBasedAdaptation.h"
#include<stdio.h>
using namespace dash::mpd;
using namespace libdash::framework::adaptation;
using namespace libdash::framework::input;
using namespace libdash::framework::mpd;

RateBasedAdaptation::RateBasedAdaptation(IMPD *mpd, IPeriod *period, IAdaptationSet *adaptationSet, bool isVid, struct AdaptationParameters *params) :
    AbstractAdaptationLogic(mpd, period, adaptationSet, isVid)
{
    std::vector<IRepresentation* > representations = this->adaptationSet->GetRepresentation();

    this->availableBitrates.clear();
    for(size_t i = 0; i < representations.size(); i++)
    {
        this->availableBitrates.push_back((uint64_t)(representations.at(i)->GetBandwidth()));
    }
    this->currentBitrate = this->availableBitrates.at(0);
    this->representation = representations.at(0);
    this->multimediaManager = NULL;
    this->alpha = params->Rate_Alpha;
    Debug("RateBasedParams:\t%f\n",alpha);
    this->averageBw = 0;
}

RateBasedAdaptation::~RateBasedAdaptation()
{
}

LogicType RateBasedAdaptation::getType()
{
    return adaptation::RateBased;
}

bool RateBasedAdaptation::isUserDependent()
{
    return false;
}

bool RateBasedAdaptation::isRateBased()
{
    return true;
}

bool RateBasedAdaptation::isBufferBased()
{
    return false;
}

void RateBasedAdaptation::setMultimediaManager(viper::managers::IMultimediaManagerBase *_mmManager)
{
    this->multimediaManager = _mmManager;
}

void RateBasedAdaptation::notifyBitrateChange()
{
    if(this->multimediaManager->isStarted() && !this->multimediaManager->isStopping())
        if(this->isVideo)
            this->multimediaManager->setVideoQuality(this->period, this->adaptationSet, this->representation);
        else
            this->multimediaManager->setAudioQuality(this->period, this->adaptationSet, this->representation);
}

uint64_t RateBasedAdaptation::getBitrate()
{
    return this->currentBitrate;
}

void RateBasedAdaptation::setBitrate(uint64_t bps)
{
    std::vector<IRepresentation *> representations;
    representations = this->adaptationSet->GetRepresentation();
    size_t i = 0;
    this->ewma(bps);
    for(i = 0;i < representations.size();i++)
    {
        if(representations.at(i)->GetBandwidth() > this->averageBw)
        {
            if(i > 0)
                i--;
            break;
        }
    }
    if((size_t)i == (size_t)(representations.size()))
        i = i-1;

    Debug("ADAPTATION_LOGIC:\tFor %s:\tBW_estimation(ewma): %lu, choice: %lu\n", (this->isVideo ? "video" : "audio"), this->averageBw, i);
    this->representation = representations.at(i);
    this->currentBitrate = this->representation->GetBandwidth();
}

void RateBasedAdaptation::bitrateUpdate(uint64_t bps, uint32_t segNum)
{
    Debug("Rate Based adaptation: speed received: %lu\n", bps);
    this->setBitrate(bps);
    this->notifyBitrateChange();
}

void RateBasedAdaptation::ewma(uint64_t bps)
{
    if(averageBw)
    {
        averageBw = alpha*averageBw + (1-alpha)*bps;
    }
    else
    {
        averageBw = bps;
    }
}

void RateBasedAdaptation::onEOS(bool value)
{
}

void RateBasedAdaptation::checkedByDASHReceiver()
{
}

void RateBasedAdaptation::bufferUpdate(uint32_t bufferfill, int maxC)
{
}

void RateBasedAdaptation::dLTimeUpdate(double time)
{
}