]> git.sesse.net Git - vlc/blob - modules/stream_filter/dash/mpd/IsoffMainManager.cpp
dash: fixed segfault when period is null
[vlc] / modules / stream_filter / dash / mpd / IsoffMainManager.cpp
1 /*
2  * IsoffMainManager.cpp
3  *****************************************************************************
4  * Copyright (C) 2010 - 2012 Klagenfurt University
5  *
6  * Created on: Jan 27, 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
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "IsoffMainManager.h"
30
31 using namespace dash::mpd;
32
33 IsoffMainManager::IsoffMainManager  (MPD *mpd)
34 {
35     this->mpd = mpd;
36 }
37 IsoffMainManager::~IsoffMainManager ()
38 {
39     delete this->mpd;
40 }
41
42 std::vector<Segment*>       IsoffMainManager::getSegments           (const Representation *rep)
43 {
44     std::vector<Segment *>  retSegments;
45     SegmentList*            list= rep->getSegmentList();
46     Segment*                initSegment = rep->getSegmentBase()->getInitSegment();
47
48     if(initSegment)
49         retSegments.push_back(initSegment);
50
51     retSegments.insert(retSegments.end(), list->getSegments().begin(), list->getSegments().end());
52     return retSegments;
53 }
54 const std::vector<Period*>& IsoffMainManager::getPeriods            () const
55 {
56     return this->mpd->getPeriods();
57 }
58 Representation*             IsoffMainManager::getBestRepresentation (Period *period)
59 {
60     std::vector<AdaptationSet *> adaptationSets = period->getAdaptationSets();
61
62     int             bitrate  = 0;
63     Representation  *best    = NULL;
64
65     for(size_t i = 0; i < adaptationSets.size(); i++)
66     {
67         std::vector<Representation *> reps = adaptationSets.at(i)->getRepresentations();
68         for(size_t j = 0; j < reps.size(); j++)
69         {
70             int currentBitrate = reps.at(j)->getBandwidth();
71
72             if(currentBitrate > bitrate)
73             {
74                 bitrate = currentBitrate;
75                 best    = reps.at(j);
76             }
77         }
78     }
79     return best;
80 }
81 Period*                     IsoffMainManager::getFirstPeriod        ()
82 {
83     std::vector<Period *> periods = this->mpd->getPeriods();
84
85     if(periods.size() == 0)
86         return NULL;
87
88     return periods.at(0);
89 }
90 Representation*             IsoffMainManager::getRepresentation     (Period *period, int bitrate) const
91 {
92     if(period == NULL)
93         return NULL;
94
95     std::vector<AdaptationSet *> adaptationSets = period->getAdaptationSets();
96
97     Representation  *best = NULL;
98     std::cout << "Searching for best representation with bitrate: " << bitrate << std::endl;
99
100     for(size_t i = 0; i < adaptationSets.size(); i++)
101     {
102         std::vector<Representation *> reps = adaptationSets.at(i)->getRepresentations();
103         for( size_t j = 0; j < reps.size(); j++ )
104         {
105             int currentBitrate = reps.at(j)->getBandwidth();
106
107             if(best == NULL || (currentBitrate > best->getBandwidth() && currentBitrate < bitrate))
108             {
109                 std::cout << "Found a better Representation bandwidth=" << reps.at(j)->getBandwidth() << " in adaptationSet #" << i << std::endl;
110                 best = reps.at( j );
111             }
112         }
113     }
114     return best;
115 }
116 Period*                     IsoffMainManager::getNextPeriod         (Period *period)
117 {
118     std::vector<Period *> periods = this->mpd->getPeriods();
119
120     for(size_t i = 0; i < periods.size(); i++)
121     {
122         if(periods.at(i) == period && (i + 1) < periods.size())
123             return periods.at(i + 1);
124     }
125
126     return NULL;
127 }
128 const MPD*                  IsoffMainManager::getMPD                () const
129 {
130     return this->mpd;
131 }
132 Representation*             IsoffMainManager::getRepresentation     (Period *period, int bitrate, int width, int height) const
133 {
134     if(period == NULL)
135         return NULL;
136
137     std::vector<AdaptationSet *> adaptationSets = period->getAdaptationSets();
138
139     std::cout << "Searching for best representation with bitrate: " << bitrate << " and resolution: " << width << "x" << height << std::endl;
140
141     std::vector<Representation *> resMatchReps;
142
143     int lowerWidth  = 0;
144     int lowerHeight = 0;
145
146     for(size_t i = 0; i < adaptationSets.size(); i++)
147     {
148         std::vector<Representation *> reps = adaptationSets.at(i)->getRepresentations();
149         for( size_t j = 0; j < reps.size(); j++ )
150         {
151             if(reps.at(j)->getWidth() == width && reps.at(j)->getHeight() == height)
152                 resMatchReps.push_back(reps.at(j));
153
154             if(reps.at(j)->getHeight() < height)
155             {
156                 lowerWidth  = reps.at(j)->getWidth();
157                 lowerHeight = reps.at(j)->getHeight();
158             }
159         }
160     }
161
162     if(resMatchReps.size() == 0)
163         return this->getRepresentation(period, bitrate, lowerWidth, lowerHeight);
164
165     Representation  *best = NULL;
166     for( size_t j = 0; j < resMatchReps.size(); j++ )
167     {
168         int currentBitrate = resMatchReps.at(j)->getBandwidth();
169
170         if(best == NULL || (currentBitrate > best->getBandwidth() && currentBitrate < bitrate))
171         {
172             std::cout << "Found a better Representation bandwidth=" << resMatchReps.at(j)->getBandwidth()
173                       << " and resolution: " << resMatchReps.at(j)->getWidth() << "x" << resMatchReps.at(j)->getHeight() << std::endl;
174             best = resMatchReps.at(j);
175         }
176     }
177
178     return best;
179 }