]> git.sesse.net Git - vlc/blob - modules/stream_filter/dash/xml/DOMParser.cpp
dash: Reworking segments.
[vlc] / modules / stream_filter / dash / xml / DOMParser.cpp
1 /*
2  * DOMParser.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 "DOMParser.h"
29
30 using namespace dash::xml;
31 using namespace dash::http;
32 using namespace dash::mpd;
33
34 DOMParser::DOMParser    (stream_t *stream) :
35     root( NULL ),
36     stream( stream ),
37     vlc_xml( NULL ),
38     vlc_reader( NULL )
39 {
40 }
41
42 DOMParser::~DOMParser   ()
43 {
44     delete this->root;
45     if(this->vlc_reader)
46         xml_ReaderDelete(this->vlc_reader);
47     if ( this->vlc_xml )
48         xml_Delete( this->vlc_xml );
49 }
50
51 Node*   DOMParser::getRootNode              ()
52 {
53     return this->root;
54 }
55 bool    DOMParser::parse                    ()
56 {
57     this->vlc_xml = xml_Create(this->stream);
58
59     if(!this->vlc_xml)
60         return false;
61
62     this->vlc_reader = xml_ReaderCreate(this->vlc_xml, this->stream);
63
64     if(!this->vlc_reader)
65         return false;
66
67     this->root = this->processNode();
68
69     return true;
70 }
71 Node*   DOMParser::processNode              ()
72 {
73     const char *data;
74     int type = xml_ReaderNextNode(this->vlc_reader, &data);
75     if(type != -1 && type != XML_READER_TEXT && type != XML_READER_NONE && type != XML_READER_ENDELEM)
76     {
77         Node *node = new Node();
78
79         std::string name    = data;
80         bool        isEmpty = xml_ReaderIsEmptyElement(this->vlc_reader);
81         node->setName(name);
82
83         this->addAttributesToNode(node);
84
85         if(isEmpty)
86             return node;
87
88         Node *subnode = NULL;
89
90         while((subnode = this->processNode()) != NULL)
91             node->addSubNode(subnode);
92
93         return node;
94     }
95     return NULL;
96 }
97 void    DOMParser::addAttributesToNode      (Node *node)
98 {
99     const char *attrValue;
100     const char *attrName;
101
102     while((attrName = xml_ReaderNextAttr(this->vlc_reader, &attrValue)) != NULL)
103     {
104         std::string key     = attrName;
105         std::string value   = attrValue;
106         node->addAttribute(key, value);
107     }
108 }
109 void    DOMParser::print                    (Node *node, int offset)
110 {
111     for(int i = 0; i < offset; i++)
112         msg_Dbg(this->stream, " ");
113
114     msg_Dbg(this->stream, "%s", node->getName().c_str());
115
116     std::vector<std::string> keys = node->getAttributeKeys();
117
118     for(size_t i = 0; i < keys.size(); i++)
119         msg_Dbg(this->stream, " %s=%s", keys.at(i).c_str(), node->getAttributeValue(keys.at(i)).c_str());
120
121     msg_Dbg(this->stream, "\n");
122
123     offset++;
124
125     for(size_t i = 0; i < node->getSubNodes().size(); i++)
126     {
127         this->print(node->getSubNodes().at(i), offset);
128     }
129 }
130
131 void    DOMParser::print                    ()
132 {
133     this->print(this->root, 0);
134 }
135 Profile DOMParser::getProfile               (dash::xml::Node *node)
136 {
137     std::string profile = node->getAttributeValue("profiles");
138
139     if(!profile.compare("urn:mpeg:mpegB:profile:dash:isoff-basic-on-demand:cm"))
140         return dash::mpd::BasicCM;
141
142     return dash::mpd::NotValid;
143 }
144 bool    DOMParser::isDash                   (stream_t *stream)
145 {
146     const char* psz_namespace = "urn:mpeg:mpegB:schema:DASH:MPD:DIS2011";
147
148     const uint8_t *peek;
149     int peek_size = stream_Peek(stream, &peek, 1024);
150     if (peek_size < (int)strlen(psz_namespace))
151         return false;
152
153     std::string header((const char*)peek, peek_size);
154     return header.find(psz_namespace) != std::string::npos;
155 }