aboutsummaryrefslogtreecommitdiffstats
path: root/Adaptation/Panda.cpp
blob: ca0c1f7d1478795ecf4fbd8f898e0ad477280a8e (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * 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 "Panda.h"
#include<stdio.h>


using namespace dash::mpd;
using namespace libdash::framework::adaptation;
using namespace libdash::framework::input;
using namespace libdash::framework::mpd;
using namespace viper::managers;

PandaAdaptation::PandaAdaptation(StreamType type, MPDWrapper *mpdWrapper, struct AdaptationParameters *params) :
    AbstractAdaptationLogic   (type, mpdWrapper)
{
    this->param_Alpha = params->Panda_Alpha;
    this->param_Beta = params->Panda_Beta;
    this->param_Bmin = params->Panda_Bmin;
    this->param_K = params->Panda_K;
    this->param_W = params->Panda_W;
    this->param_Epsilon = params->Panda_Epsilon;

    this->segmentDuration = params->segmentDuration;
    this->bufferMaxSizeSeconds = params->segmentBufferSize * this->segmentDuration;
    this->targetBw = 0;
    this->targetInterTime = 0.0;

    this->averageBw = 0;
    this->smoothBw = 0;
    this->instantBw = 0;
    this->targetBw = 0;

    this->targetInterTime = 0.0;
    this->interTime = 0.0;

    this->alpha_ewma = 0.8;

    this->bufferLevel = 0;
    this->bufferLevelSeconds = 0.0;

    this->downloadTime = 0.0;

//    this->mpd = mpd;
//    this->adaptationSet = adaptationSet;
//    this->period = period;
    this->multimediaManager = NULL;
    this->representation = NULL;
    this->currentBitrate = 0;
    this->current = 0;

    // Retrieve the available bitrates
    this->mpdWrapper->acquireLock();
    std::vector<IRepresentation* > representations = this->mpdWrapper->getRepresentations(this->type);
    this->mpdWrapper->releaseLock();
    this->availableBitrates.clear();
    qDebug("PANDA Available Bitrates...");
    for(size_t i = 0; i < representations.size(); i++)
    {
        this->availableBitrates.push_back((uint64_t)(representations.at(i)->GetBandwidth()));
        qDebug("%d  -  %I64u bps", i+1, this->availableBitrates[i]);
    }

    this->representation = representations.at(0);
    this->currentBitrate = (uint64_t) this->representation->GetBandwidth();

    qDebug("Panda parameters: K= %f, Bmin = %f, alpha = %f, beta = %f, W = %f ", param_K, param_Bmin, param_Alpha, param_Beta, param_W);
}

PandaAdaptation::~PandaAdaptation() {
}

LogicType PandaAdaptation::getType()
{
    return adaptation::Panda;
}

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

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

bool PandaAdaptation::isBufferBased()
{
    return true;
}

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

void PandaAdaptation::notifyBitrateChange()
{
    this->mpdWrapper->setRepresentation(this->type, this->representation);
    if(this->multimediaManager->isStarted() && !this->multimediaManager->isStopping())
        if(this->type == viper::managers::StreamType::VIDEO)
            this->multimediaManager->setVideoQuality();
        else
            this->multimediaManager->setAudioQuality();
}

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

void PandaAdaptation::quantizer()
{
    this->deltaUp = this->param_Epsilon * (double)this->smoothBw;
    this->deltaDown = 0.0;

    qDebug("** DELTA UP:\t%f", this->deltaUp);

    uint64_t smoothBw_UP = this->smoothBw - this->deltaUp;
    uint64_t smoothBw_DOWN = this->smoothBw - this->deltaDown;

    qDebug("** Smooth-BW UP:\t%d\t Smooth-BW DOWN:\t%d", smoothBw_UP, smoothBw_DOWN);

    std::vector<IRepresentation *> representations;
    representations = this->mpdWrapper->getRepresentations(this->type);
    uint32_t numQualLevels = representations.size();

    // We have to find bitrateMin and bitrateMax
    uint64_t bitrateDown, bitrateUp;

    // DOWN
    uint32_t iDown = 0;
    uint32_t i_d,i_u;
    for (i_d = 0; i_d < this->availableBitrates.size(); ++i_d) {
        if (this->availableBitrates[i_d] > smoothBw_DOWN) {
            break;
        }
    }
    if(i_d > 0)
        iDown = i_d-1;
    else
        iDown = 0;

    bitrateDown = (uint64_t) representations.at(iDown)->GetBandwidth();
    qDebug("** Bitrate DOWN:\t%d\t at Quality:\t%d", bitrateDown, iDown);

    // UP
    uint32_t iUp = 0;
    for (i_u = 0; i_u < this->availableBitrates.size(); ++i_u) {
        if (this->availableBitrates[i_u] > smoothBw_UP) {
            break;
        }
    }
    if(i_u > 0)
        iUp = i_u-1;
    else
        iUp = 0;

    bitrateUp = (uint64_t) representations.at(iUp)->GetBandwidth();
    qDebug("** Bitrate UP:\t%d\t at Quality:\t%d", bitrateUp, iUp);

    qDebug("** Current RATE:\t%d Current QUALITY:\t%d\n", this->currentBitrate, this->current);


    // Next bitrate computation
    if(this->currentBitrate < bitrateUp)
    {
        this->currentBitrate = bitrateUp;
        this->current = iUp;
    }
    else if(this->currentBitrate <= bitrateDown && this->currentBitrate >= bitrateUp)
    {
        qDebug("** CURRENT UNCHANGED **");
    }
    else
    {
        this->currentBitrate = bitrateDown;
        this->current = iDown;
    }
    this->representation = representations.at(this->current);
}

void PandaAdaptation::setBitrate(uint64_t bps)
{

    // 1. Calculating the targetBW
    if(this->targetBw)
    {
        //this->targetBw = this->targetBw + param_K * this->interTime * (param_W - ((this->targetBw - bps + this->param_W) > 0 ? this->targetBw - bps + this->param_W: 0));
        if ((double)this->targetBw - (double)bps + this->param_W > 0)
            this->targetBw = this->targetBw + (uint64_t)(param_K * this->interTime * (param_W - ((double)this->targetBw - (double)bps + this->param_W)));
        else
            this->targetBw = this->targetBw + (uint64_t)(param_K * this->interTime * param_W);
    }
    else
        this->targetBw = bps;

    qDebug("** INSTANTANEOUS BW:\t%d", bps);
    qDebug("** CLASSIC EWMA BW:\t%d", this->averageBw);
    qDebug("** PANDA TARGET BW:\t%d", this->targetBw);

    // 2. Calculating the smoothBW
    if(this->interTime)
        this->smoothBw = (uint64_t)((double)this->smoothBw - this->param_Alpha * this->interTime * ((double)this->smoothBw - (double)this->targetBw));
    else
        this->smoothBw = this->targetBw;

    qDebug("** PANDA SMOOTH BW:\t%d", this->smoothBw);

    // 3. Quantization
    this->quantizer();
    qDebug("ADAPTATION_LOGIC:\tFor %s:\tlast_buffer: %f\tbuffer_level: %f, instantaneousBw: %lu, AverageBW: %lu, choice: %d",(this->type == viper::managers::StreamType::VIDEO) ? "video" : "audio",(double)lastBufferLevel/100 , (double)bufferLevel/100, this->instantBw, this->averageBw , this->current);
    this->lastBufferLevel = this->bufferLevel;

    // 4. Computing the "actual inter time"
    this->bufferLevelSeconds = (double)((this->bufferLevel * this->bufferMaxSizeSeconds) *1./100);
    this->targetInterTime = ((double)this->currentBitrate * segmentDuration) * 1./this->smoothBw + param_Beta * (this->bufferLevelSeconds - param_Bmin);
    qDebug("** TARGET INTER TIME:\t%f", this->targetInterTime);
    qDebug("** DOWNLOAD TIME:\t%f", this->downloadTime);
    this->targetInterTime = (this->targetInterTime > 0) ? this->targetInterTime : 0.0;
    this->interTime = this->targetInterTime > this->downloadTime ? this->targetInterTime : this->downloadTime;
    this->interTime = this->interTime > 3 ? 3 : this->interTime;

    qDebug("** ACTUAL INTER TIME:\t%f", this->interTime);
    this->multimediaManager->setTargetDownloadingTime((this->type == viper::managers::StreamType::VIDEO), interTime);
}

void PandaAdaptation::bitrateUpdate(uint64_t bps, uint32_t segNum)
{
    this->instantBw = bps;

    // Avg bandwidth estimate with EWMA
    if(this->averageBw == 0)
    {
        this->averageBw = bps;
    }
    else
    {
        this->averageBw = this->alpha_ewma*this->averageBw + (1 - this->alpha_ewma)*bps;
    }
    this->mpdWrapper->acquireLock();
    this->setBitrate(bps);
    this->notifyBitrateChange();
    this->mpdWrapper->releaseLock();
}

void PandaAdaptation::dLTimeUpdate(double time)
{
    this->downloadTime = time;
}

void PandaAdaptation::bufferUpdate(uint32_t bufferfill, int maxC)
{
    qDebug("bufferlvl: %d", bufferfill);
    this->bufferLevel = bufferfill;
}

void PandaAdaptation::onEOS(bool value)
{
}

void PandaAdaptation::checkedByDASHReceiver()
{
}