Segment.cpp 6.72 KB
Newer Older
Christopher Mueller's avatar
Christopher Mueller committed
1 2 3 4 5 6 7 8 9
/*
 * Segment.cpp
 *****************************************************************************
 * Copyright (C) 2010 - 2011 Klagenfurt University
 *
 * Created on: Aug 10, 2010
 * Authors: Christopher Mueller <christopher.mueller@itec.uni-klu.ac.at>
 *          Christian Timmerer  <christian.timmerer@itec.uni-klu.ac.at>
 *
10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
Christopher Mueller's avatar
Christopher Mueller committed
12 13 14 15 16
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Christopher Mueller's avatar
Christopher Mueller committed
19 20
 *
 * You should have received a copy of the GNU Lesser General Public License
21 22
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Christopher Mueller's avatar
Christopher Mueller committed
23
 *****************************************************************************/
24

Christopher Mueller's avatar
Christopher Mueller committed
25 26 27 28 29
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "Segment.h"
30
#include "BaseRepresentation.h"
31
#include "AbstractPlaylist.hpp"
32
#include "SegmentChunk.hpp"
33
#include "../http/BytesRange.hpp"
34 35
#include "../http/HTTPConnectionManager.h"
#include "../http/Downloader.hpp"
36
#include <cassert>
37

38 39
using namespace adaptive::http;
using namespace adaptive::playlist;
Christopher Mueller's avatar
Christopher Mueller committed
40

41 42 43
const int ISegment::SEQUENCE_INVALID = 0;
const int ISegment::SEQUENCE_FIRST   = 1;

44 45 46
ISegment::ISegment(const ICanonicalUrl *parent):
    ICanonicalUrl( parent ),
    startByte  (0),
47
    endByte    (0)
48
{
49
    debugName = "Segment";
50
    classId = CLASSID_ISEGMENT;
51
    startTime.Set(0);
52
    duration.Set(0);
53
    chunksuse.Set(0);
54
    sequence = SEQUENCE_INVALID;
55
    templated = false;
56
    discontinuity = false;
57 58 59 60 61
}

ISegment::~ISegment()
{
    assert(chunksuse.Get() == 0);
62
}
63

64
SegmentChunk * ISegment::getChunk(const std::string &url, HTTPConnectionManager *connManager)
65
{
66
    HTTPChunkBufferedSource *source = new HTTPChunkBufferedSource(url, connManager);
67 68
    if(startByte != endByte)
        source->setBytesRange(BytesRange(startByte, endByte));
69
    connManager->downloader->schedule(source);
70
    return new (std::nothrow) SegmentChunk(this, source);
71 72
}

73
void ISegment::onChunkDownload(block_t **, SegmentChunk *, BaseRepresentation *)
74 75 76 77
{

}

78
SegmentChunk* ISegment::toChunk(size_t index, BaseRepresentation *ctxrep, HTTPConnectionManager *connManager)
79
{
80
    SegmentChunk *chunk;
81 82
    try
    {
83
        chunk = getChunk(getUrlSegment().toString(index, ctxrep), connManager);
84 85 86 87 88
        if (!chunk)
            return NULL;
    }
    catch (int)
    {
89
        return NULL;
90
    };
91

92 93
    chunk->setRepresentation(ctxrep);

94 95 96
    return chunk;
}

97 98 99 100 101
bool ISegment::isTemplate() const
{
    return templated;
}

102 103 104 105 106 107
void ISegment::setByteRange(size_t start, size_t end)
{
    startByte = start;
    endByte   = end;
}

108 109 110 111 112 113 114 115 116 117
void ISegment::setSequenceNumber(uint64_t seq)
{
    sequence = SEQUENCE_FIRST + seq;
}

uint64_t ISegment::getSequenceNumber() const
{
    return sequence;
}

118 119 120 121 122
size_t ISegment::getOffset() const
{
    return startByte;
}

123
void ISegment::debug(vlc_object_t *obj, int indent) const
124
{
125
    std::stringstream ss;
126
    ss.imbue(std::locale("C"));
127 128
    ss << std::string(indent, ' ') << debugName << " #" << getSequenceNumber();
    ss << " url=" << getUrlSegment().toString();
129 130
    if(startByte!=endByte)
        ss << " @" << startByte << ".." << endByte;
131 132
    if(startTime.Get() > 0)
    	 ss << " stime " << startTime.Get();
133
    ss << " duration " << duration.Get();
134
    msg_Dbg(obj, "%s", ss.str().c_str());
135 136
}

137 138 139 140 141 142 143 144
bool ISegment::contains(size_t byte) const
{
    if (startByte == endByte)
        return false;
    return (byte >= startByte &&
            (!endByte || byte <= endByte) );
}

145 146
int ISegment::compare(ISegment *other) const
{
147
    if(duration.Get())
148
    {
149
        stime_t diff = startTime.Get() - other->startTime.Get();
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        if(diff)
            return diff / diff;
    }

    size_t diff = startByte - other->startByte;
    if(diff)
        return diff / diff;

    diff = endByte - other->endByte;
    if(diff)
        return diff / diff;

    return 0;
}

165 166 167 168 169
int ISegment::getClassId() const
{
    return classId;
}

170
Segment::Segment(ICanonicalUrl *parent) :
171
        ISegment(parent)
172 173 174 175 176
{
    size = -1;
    classId = CLASSID_SEGMENT;
}

177 178
void Segment::addSubSegment(SubSegment *subsegment)
{
179 180 181 182 183 184 185
    if(!subsegments.empty())
    {
        /* Use our own sequence number, and since it it now
           uneffective, also for next subsegments numbering */
        subsegment->setSequenceNumber(getSequenceNumber());
        setSequenceNumber(getSequenceNumber());
    }
186 187 188
    subsegments.push_back(subsegment);
}

189 190 191
Segment::~Segment()
{
    std::vector<SubSegment*>::iterator it;
192
    for(it=subsegments.begin();it!=subsegments.end();++it)
193 194 195
        delete *it;
}

196
void                    Segment::setSourceUrl   ( const std::string &url )
Christopher Mueller's avatar
Christopher Mueller committed
197
{
198
    if ( url.empty() == false )
199
        this->sourceUrl = Url(url);
Christopher Mueller's avatar
Christopher Mueller committed
200
}
201

202
void Segment::debug(vlc_object_t *obj, int indent) const
203
{
204 205
    if (subsegments.empty())
    {
206
        ISegment::debug(obj, indent);
207 208 209
    }
    else
    {
210 211 212
        std::string text(indent, ' ');
        text.append("Segment");
        msg_Dbg(obj, "%s", text.c_str());
213
        std::vector<SubSegment *>::const_iterator l;
214
        for(l = subsegments.begin(); l != subsegments.end(); ++l)
215
            (*l)->debug(obj, indent + 1);
216
    }
217
}
218

219
Url Segment::getUrlSegment() const
220
{
221 222 223 224 225 226 227 228 229 230 231
    if(sourceUrl.hasScheme())
    {
        return sourceUrl;
    }
    else
    {
        Url ret = getParentUrlSegment();
        if (!sourceUrl.empty())
            ret.append(sourceUrl);
        return ret;
    }
232
}
233

234 235 236 237
std::vector<ISegment*> Segment::subSegments()
{
    std::vector<ISegment*> list;
    if(!subsegments.empty())
238
    {
239
        std::vector<SubSegment*>::iterator it;
240
        for(it=subsegments.begin();it!=subsegments.end();++it)
241
            list.push_back(*it);
242
    }
243 244 245 246 247 248
    else
    {
        list.push_back(this);
    }
    return list;
}
249

250
InitSegment::InitSegment(ICanonicalUrl *parent) :
251 252 253
    Segment(parent)
{
    debugName = "InitSegment";
254
    classId = CLASSID_INITSEGMENT;
255 256
}

257
IndexSegment::IndexSegment(ICanonicalUrl *parent) :
258 259 260
    Segment(parent)
{
    debugName = "IndexSegment";
261
    classId = CLASSID_INDEXSEGMENT;
262 263
}

264
SubSegment::SubSegment(ISegment *main, size_t start, size_t end) :
265 266 267
    ISegment(main), parent(main)
{
    setByteRange(start, end);
268
    debugName = "SubSegment";
269
    classId = CLASSID_SUBSEGMENT;
270
}
271

272
Url SubSegment::getUrlSegment() const
273
{
274
    return getParentUrlSegment();
275
}
276

277
std::vector<ISegment*> SubSegment::subSegments()
278
{
279 280 281
    std::vector<ISegment*> list;
    list.push_back(this);
    return list;
282
}
283 284 285 286 287

void SubSegment::addSubSegment(SubSegment *)
{

}