IsoffMainParser.cpp 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * IsoffMainParser.cpp
 *****************************************************************************
 * Copyright (C) 2010 - 2012 Klagenfurt University
 *
 * Created on: Jan 27, 2012
 * 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
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.
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.
23 24
 *****************************************************************************/

25 26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29
#include "IsoffMainParser.h"
30 31
#include "xml/DOMHelper.h"
#include <vlc_strings.h>
32 33
#include <vlc_stream.h>
#include <cstdio>
34 35 36 37 38

using namespace dash::mpd;
using namespace dash::xml;

IsoffMainParser::IsoffMainParser    (Node *root, stream_t *p_stream) :
39
                 IMPDParser(root, NULL, p_stream, NULL)
40 41 42 43 44 45
{
}
IsoffMainParser::~IsoffMainParser   ()
{
}

46
bool    IsoffMainParser::parse              (Profile profile)
47
{
48
    mpd = new MPD(p_stream, profile);
49 50 51 52
    setMPDAttributes();
    setMPDBaseUrl(root);
    setPeriods(root);
    print();
53 54
    return true;
}
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
void    IsoffMainParser::setMPDAttributes   ()
{
    const std::map<std::string, std::string> attr = this->root->getAttributes();

    std::map<std::string, std::string>::const_iterator it;

    it = attr.find("mediaPresentationDuration");
    if(it != attr.end())
        this->mpd->setDuration(str_duration(it->second.c_str()));

    it = attr.find("minBufferTime");
    if(it != attr.end())
        this->mpd->setMinBufferTime(str_duration( it->second.c_str()));

}

void    IsoffMainParser::setAdaptationSets  (Node *periodNode, Period *period)
{
    std::vector<Node *> adaptationSets = DOMHelper::getElementByTagName(periodNode, "AdaptationSet", false);
75
    std::vector<Node *>::const_iterator it;
76

77
    for(it = adaptationSets.begin(); it != adaptationSets.end(); it++)
78 79
    {
        AdaptationSet *adaptationSet = new AdaptationSet();
80 81 82 83 84
        if(!adaptationSet)
            continue;
        if((*it)->hasAttribute("mimeType"))
            adaptationSet->setMimeType((*it)->getAttributeValue("mimeType"));
        setRepresentations((*it), adaptationSet);
85 86 87 88 89 90 91 92 93
        period->addAdaptationSet(adaptationSet);
    }
}
void    IsoffMainParser::setRepresentations (Node *adaptationSetNode, AdaptationSet *adaptationSet)
{
    std::vector<Node *> representations = DOMHelper::getElementByTagName(adaptationSetNode, "Representation", false);

    for(size_t i = 0; i < representations.size(); i++)
    {
94
        this->currentRepresentation = new Representation(getMPD());
95 96
        Node *repNode = representations.at(i);

97 98 99 100
        std::vector<Node *> baseUrls = DOMHelper::getChildElementByTagName(repNode, "BaseURL");
        if(!baseUrls.empty())
            currentRepresentation->setBaseUrl( new BaseUrl( baseUrls.front()->getText() ) );

101 102 103 104 105 106 107 108 109
        if(repNode->hasAttribute("width"))
            this->currentRepresentation->setWidth(atoi(repNode->getAttributeValue("width").c_str()));

        if(repNode->hasAttribute("height"))
            this->currentRepresentation->setHeight(atoi(repNode->getAttributeValue("height").c_str()));

        if(repNode->hasAttribute("bandwidth"))
            this->currentRepresentation->setBandwidth(atoi(repNode->getAttributeValue("bandwidth").c_str()));

110 111 112
        if(repNode->hasAttribute("mimeType"))
            currentRepresentation->setMimeType(repNode->getAttributeValue("mimeType"));

113 114 115
        this->setSegmentBase(repNode, this->currentRepresentation);
        this->setSegmentList(repNode, this->currentRepresentation);
        adaptationSet->addRepresentation(this->currentRepresentation);
116 117
    }
}
118

119 120 121 122
void    IsoffMainParser::setSegmentBase     (dash::xml::Node *repNode, Representation *rep)
{
    std::vector<Node *> segmentBase = DOMHelper::getElementByTagName(repNode, "SegmentBase", false);

123 124 125
    if(segmentBase.front()->hasAttribute("indexRange"))
    {
        SegmentList *list = new SegmentList();
126
        Segment *seg;
127 128 129 130

        size_t start = 0, end = 0;
        if (std::sscanf(segmentBase.front()->getAttributeValue("indexRange").c_str(), "%"PRIu64"-%"PRIu64, &start, &end) == 2)
        {
131
            seg = new IndexSegment(rep);
132 133 134 135 136 137
            seg->setByteRange(start, end);
            list->addSegment(seg);
            /* index must be before data, so data starts at index end */
            seg = new Segment(rep);
            seg->setByteRange(end + 1, 0);
        }
138 139 140 141
        else
        {
            seg = new Segment(rep);
        }
142 143 144 145 146 147 148 149 150 151 152 153 154

        list->addSegment(seg);
        rep->setSegmentList(list);

        std::vector<Node *> initSeg = DOMHelper::getElementByTagName(segmentBase.front(), "Initialization", false);
        if(!initSeg.empty())
        {
            SegmentBase *base = new SegmentBase();
            setInitSegment(segmentBase.front(), base);
            rep->setSegmentBase(base);
        }
    }
    else if(!segmentBase.empty())
155 156
    {
        SegmentBase *base = new SegmentBase();
157
        setInitSegment(segmentBase.front(), base);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        rep->setSegmentBase(base);
    }
}
void    IsoffMainParser::setSegmentList     (dash::xml::Node *repNode, Representation *rep)
{
    std::vector<Node *> segmentList = DOMHelper::getElementByTagName(repNode, "SegmentList", false);

    if(segmentList.size() > 0)
    {
        SegmentList *list = new SegmentList();
        this->setSegments(segmentList.at(0), list);
        rep->setSegmentList(list);
    }

}
void    IsoffMainParser::setInitSegment     (dash::xml::Node *segBaseNode, SegmentBase *base)
{
    std::vector<Node *> initSeg = DOMHelper::getElementByTagName(segBaseNode, "Initialisation", false);

177 178 179
    if(initSeg.size() == 0)
        initSeg = DOMHelper::getElementByTagName(segBaseNode, "Initialization", false);

180 181
    if(initSeg.size() > 0)
    {
182
        Segment *seg = new InitSegment( currentRepresentation );
183 184 185 186 187 188
        seg->setSourceUrl(initSeg.at(0)->getAttributeValue("sourceURL"));

        if(initSeg.at(0)->hasAttribute("range"))
        {
            std::string range = initSeg.at(0)->getAttributeValue("range");
            size_t pos = range.find("-");
189
            seg->setByteRange(atoi(range.substr(0, pos).c_str()), atoi(range.substr(pos + 1, range.size()).c_str()));
190 191 192 193 194 195 196 197 198 199 200
        }

        base->addInitSegment(seg);
    }
}
void    IsoffMainParser::setSegments        (dash::xml::Node *segListNode, SegmentList *list)
{
    std::vector<Node *> segments = DOMHelper::getElementByTagName(segListNode, "SegmentURL", false);

    for(size_t i = 0; i < segments.size(); i++)
    {
201
        Segment *seg = new Segment( this->currentRepresentation );
202 203
        seg->setSourceUrl(segments.at(i)->getAttributeValue("media"));

204
        if(segments.at(i)->hasAttribute("mediaRange"))
205
        {
206
            std::string range = segments.at(i)->getAttributeValue("mediaRange");
207
            size_t pos = range.find("-");
208
            seg->setByteRange(atoi(range.substr(0, pos).c_str()), atoi(range.substr(pos + 1, range.size()).c_str()));
209 210 211 212 213 214 215
        }

        list->addSegment(seg);
    }
}
void    IsoffMainParser::print              ()
{
216
    if(mpd)
217
    {
218 219 220 221
        msg_Dbg(p_stream, "MPD profile=%s mediaPresentationDuration=%ld minBufferTime=%ld",
                static_cast<std::string>(mpd->getProfile()).c_str(),
                mpd->getDuration(),
                mpd->getMinBufferTime());
222
        msg_Dbg(p_stream, "BaseUrl=%s", mpd->getUrlSegment().c_str());
223 224 225

        std::vector<Period *>::const_iterator i;
        for(i = mpd->getPeriods().begin(); i != mpd->getPeriods().end(); i++)
226
        {
227 228 229
            msg_Dbg(p_stream, " Period");
            std::vector<AdaptationSet *>::const_iterator j;
            for(j = (*i)->getAdaptationSets().begin(); j != (*i)->getAdaptationSets().end(); j++)
230
            {
231 232
                msg_Dbg(p_stream, "  AdaptationSet");
                std::vector<Representation *>::const_iterator k;
233
                for(k = (*j)->getRepresentations().begin(); k != (*j)->getRepresentations().end(); k++)
234
                {
235 236 237
                    std::vector<std::string> debug = (*k)->toString();
                    std::vector<std::string>::const_iterator l;
                    for(l = debug.begin(); l < debug.end(); l++)
238
                    {
239
                        msg_Dbg(p_stream, "%s", (*l).c_str());
240 241 242 243 244 245
                    }
                }
            }
        }
    }
}