]> git.sesse.net Git - vlc/blob - modules/stream_filter/dash/mpd/BasicCMManager.cpp
dash: dont use best rep without information
[vlc] / modules / stream_filter / dash / mpd / BasicCMManager.cpp
1 /*
2  * BasicCMManager.cpp
3  *****************************************************************************
4  * Copyright (C) 2010 - 2011 Klagenfurt University
5  *
6  * Created on: Aug 10, 2010
7  * Authors: Christopher Mueller <christopher.mueller@itec.uni-klu.ac.at>
8  *          Christian Timmerer  <christian.timmerer@itec.uni-klu.ac.at>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published
12  * by the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "BasicCMManager.h"
29
30 #include <cassert>
31
32 using namespace dash::mpd;
33
34 BasicCMManager::BasicCMManager  (MPD *mpd)
35 {
36     this->mpd = mpd;
37 }
38 BasicCMManager::~BasicCMManager ()
39 {
40     delete this->mpd;
41 }
42
43 std::vector<Segment*>   BasicCMManager::getSegments( const Representation *rep )
44 {
45     std::vector<Segment *>          retSegments;
46     SegmentInfo*                    info = rep->getSegmentInfo();
47     Segment*                        initSegment = info->getInitialisationSegment();
48
49     if ( initSegment )
50         retSegments.push_back( initSegment );
51     retSegments.insert( retSegments.end(), info->getSegments().begin(),
52                                             info->getSegments().end() );
53     return retSegments;
54 }
55 const std::vector<Period*>&    BasicCMManager::getPeriods              () const
56 {
57     return this->mpd->getPeriods();
58 }
59
60 Representation*         BasicCMManager::getBestRepresentation   (Period *period)
61 {
62     std::vector<Group *> groups = period->getGroups();
63
64     int             bitrate  = 0;
65     Representation  *best    = NULL;
66
67     for(size_t i = 0; i < groups.size(); i++)
68     {
69         std::vector<Representation *> reps = groups.at(i)->getRepresentations();
70         for(size_t j = 0; j < reps.size(); j++)
71         {
72             int currentBitrate = reps.at(j)->getBandwidth();
73             assert( currentBitrate != -1 );
74
75             if( currentBitrate > bitrate)
76             {
77                 bitrate = currentBitrate;
78                 best    = reps.at(j);
79             }
80         }
81     }
82     return best;
83 }
84 Period*                 BasicCMManager::getFirstPeriod          ()
85 {
86     std::vector<Period *> periods = this->mpd->getPeriods();
87
88     if(periods.size() == 0)
89         return NULL;
90
91     return periods.at(0);
92 }
93
94 Representation*         BasicCMManager::getRepresentation(Period *period, int bitrate ) const
95 {
96     std::vector<Group *>    groups = period->getGroups();
97
98     Representation  *best = NULL;
99     std::cout << "Sarching for best representation with bitrate: " << bitrate << std::endl;
100
101     for(size_t i = 0; i < groups.size(); i++)
102     {
103         std::vector<Representation *> reps = groups.at(i)->getRepresentations();
104         for( size_t j = 0; j < reps.size(); j++ )
105         {
106             int     currentBitrate = reps.at(j)->getBandwidth();
107             assert( currentBitrate != -1 );
108
109             if ( best == NULL ||
110                  ( currentBitrate > best->getBandwidth() &&
111                    currentBitrate < bitrate ) )
112             {
113                 std::cout << "Found a better Representation (#" << j << ") in group #" << i << std::endl;
114                 best = reps.at( j );
115             }
116         }
117     }
118     return best;
119 }
120 Period*                 BasicCMManager::getNextPeriod           (Period *period)
121 {
122     std::vector<Period *> periods = this->mpd->getPeriods();
123
124     for(size_t i = 0; i < periods.size(); i++)
125     {
126         if(periods.at(i) == period && (i + 1) < periods.size())
127             return periods.at(i + 1);
128     }
129
130     return NULL;
131 }
132
133 const MPD*      BasicCMManager::getMPD() const
134 {
135     return this->mpd;
136 }
137 Representation*         BasicCMManager::getRepresentation (Period *period, int bitrate, int width, int height) const
138 {
139     return this->getRepresentation(period, bitrate);
140 }