]> git.sesse.net Git - vlc/blob - modules/demux/dash/SegmentTracker.cpp
minimal_macosx: simplify killer thread loop
[vlc] / modules / demux / dash / SegmentTracker.cpp
1 /*
2  * SegmentTracker.cpp
3  *****************************************************************************
4  * Copyright (C) 2014 - VideoLAN authors
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20 #include "SegmentTracker.hpp"
21 #include "mpd/MPD.h"
22
23 using namespace dash;
24 using namespace dash::logic;
25 using namespace dash::http;
26 using namespace dash::mpd;
27
28 SegmentTracker::SegmentTracker(AbstractAdaptationLogic *logic_, mpd::MPD *mpd_)
29 {
30     count = 0;
31     initializing = true;
32     prevRepresentation = NULL;
33     currentPeriod = mpd_->getFirstPeriod();
34     setAdaptationLogic(logic_);
35     mpd = mpd_;
36 }
37
38 SegmentTracker::~SegmentTracker()
39 {
40
41 }
42
43 void SegmentTracker::setAdaptationLogic(AbstractAdaptationLogic *logic_)
44 {
45     logic = logic_;
46 }
47
48 void SegmentTracker::resetCounter()
49 {
50     count = 0;
51     prevRepresentation = NULL;
52 }
53
54 Chunk * SegmentTracker::getNextChunk(Streams::Type type)
55 {
56     Representation *rep;
57     ISegment *segment;
58
59     if(!currentPeriod)
60         return NULL;
61
62     if(prevRepresentation && !prevRepresentation->canBitswitch())
63         rep = prevRepresentation;
64     else
65         rep = logic->getCurrentRepresentation(type, currentPeriod);
66
67     if ( rep == NULL )
68             return NULL;
69
70     if(rep != prevRepresentation)
71     {
72         prevRepresentation = rep;
73         initializing = true;
74     }
75
76     if(initializing)
77     {
78         initializing = false;
79         segment = rep->getSegment(Representation::INFOTYPE_INIT);
80         if(segment)
81             return segment->toChunk(count, rep);
82     }
83
84     segment = rep->getSegment(Representation::INFOTYPE_MEDIA, count);
85     if(!segment)
86     {
87         currentPeriod = mpd->getNextPeriod(currentPeriod);
88         resetCounter();
89         return getNextChunk(type);
90     }
91
92     Chunk *chunk = segment->toChunk(count, rep);
93     if(chunk)
94         count++;
95
96     return chunk;
97 }
98
99 bool SegmentTracker::setPosition(mtime_t time, bool tryonly)
100 {
101     uint64_t segcount;
102     if(prevRepresentation &&
103        prevRepresentation->getSegmentNumberByTime(time, &segcount))
104     {
105         if(!tryonly)
106             count = segcount;
107         return true;
108     }
109     return false;
110 }
111
112 mtime_t SegmentTracker::getSegmentStart() const
113 {
114     if(prevRepresentation)
115         return prevRepresentation->getPlaybackTimeBySegmentNumber(count);
116     else
117         return 0;
118 }