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