1 /*
2  * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DASH_MPD_PARSER_H
17 #define DASH_MPD_PARSER_H
18 
19 #include <cstdint>
20 #include "dash_mpd_def.h"
21 #include "i_dash_mpd_node.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Plugins {
26 namespace HttpPlugin {
27 struct DashElementList {
DashElementListDashElementList28     DashElementList()
29     {
30         segBaseElement_ = nullptr;
31         segListElement_ = nullptr;
32         segTmplElement_ = nullptr;
33     }
34 
35     std::shared_ptr<XmlElement> segBaseElement_;
36     std::shared_ptr<XmlElement> segListElement_;
37     std::shared_ptr<XmlElement> segTmplElement_;
38 };
39 
40 class DashMpdParser {
41 public:
42     DashMpdParser() = default;
43     ~DashMpdParser();
44 
45     void ParseMPD(const char *mpdData, uint32_t length);
46     void GetMPD(DashMpdInfo *&mpdInfo);
47     void Clear();
48     void StopParseMpd();
49 
50 private:
51     void ParsePeriod(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement);
52     void ParseAdaptationSet(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
53                             DashPeriodInfo *periodInfo);
54     void ParseRepresentation(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
55                              const DashPeriodInfo *periodInfo,
56                              DashAdptSetInfo *adptSetInfo);
57     void ParseSegmentBase(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
58                           DashSegBaseInfo **segBaseInfo);
59     void ParseSegmentList(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
60                           DashSegListInfo **segListInfo);
61     void ParseSegmentListElement(std::shared_ptr<XmlParser> &xmlParser, DashSegListInfo *segList,
62                                  std::shared_ptr<XmlElement> &childElement);
63     void ParseSegmentTemplate(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
64                               DashSegTmpltInfo **segTmpltInfo);
65     void ParseSegmentTimeline(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
66                               DashList<DashSegTimeline *> &segTmlineList);
67     void ParseUrlType(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
68                       const std::string urlTypeName,
69                       DashUrlType **urlTypeInfo);
70     void ParseContentProtection(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
71                                 DashList<DashDescriptor *> &contentProtectionList);
72     void ParseEssentialProperty(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
73                                 DashList<DashDescriptor *> &essentialPropertyList);
74     void ParseAudioChannelConfiguration(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
75                                         DashList<DashDescriptor *> &propertyList);
76     void ParseSegmentUrl(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
77                          DashList<DashSegUrl *> &segUrlList);
78     void ParseContentComponent(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
79                                DashList<DashContentCompInfo *> &contentCompInfoList);
80     void ParseProgramInfo(std::list<std::string> &programStrList) const;
81     void ParseBaseUrl(std::list<std::string> &baseUrlStrList) const;
82     void ParseLocation(std::list<std::string> &locationStrlist) const;
83     void ParseMetrics(std::list<std::string> &metricsStrList) const;
84 
85     void FreeSegmentBase(DashSegBaseInfo *segBaseInfo) const;
86     void FreeSegmentList(DashSegListInfo *segListInfo);
87     void FreeSegmentTemplate(DashSegTmpltInfo *segTmpltInfo);
88     void ClearAdaptationSet(DashList<DashAdptSetInfo *> &adptSetInfoList);
89     void ClearContentComp(DashList<DashContentCompInfo *> &contentCompList);
90     void ClearRepresentation(DashList<DashRepresentationInfo *> &representationList);
91     void ClearSegmentBase(DashSegBaseInfo *segBaseInfo) const;
92     void ClearMultSegBase(DashMultSegBaseInfo *multSegBaseInfo);
93     void ClearCommonAttrsAndElements(DashCommonAttrsAndElements *commonAttrsAndElements);
94     void ClearRoleList(DashList<DashDescriptor *> &roleList);
95 
96     void GetMpdAttr(IDashMpdNode *mpdNode);
97     void GetMpdElement(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement);
98     void ProcessMpdElement(const std::shared_ptr<XmlParser> &xmlParser,
99                            DashList <std::shared_ptr<XmlElement>> &periodElementList,
100                            std::shared_ptr<XmlElement> &childElement);
101     void GetAdaptationSetAttr(IDashMpdNode *adptSetNode, DashAdptSetInfo *adptSetInfo);
102     void GetAdaptationSetElement(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
103                                  const DashPeriodInfo *periodInfo,
104                                  DashAdptSetInfo *adptSetInfo);
105     void ProcessAdpSetElement(std::shared_ptr<XmlParser> &xmlParser, DashAdptSetInfo *adptSetInfo,
106                               DashList<std::shared_ptr<XmlElement>> &representationElementList,
107                               std::shared_ptr<XmlElement> &childElement, DashElementList &dashElementList);
108     void GetPeriodAttr(IDashMpdNode *periodNode, DashPeriodInfo *periodInfo);
109     void GetPeriodElement(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
110                           DashPeriodInfo *periodInfo);
111     void ProcessPeriodElement(const std::shared_ptr<XmlParser> &xmlParser, DashPeriodInfo *periodInfo,
112                               DashList <std::shared_ptr<XmlElement>> &adptSetElementList,
113                               std::shared_ptr<XmlElement> &childElement, DashElementList &dashElementList);
114     void GetRepresentationAttr(IDashMpdNode *representationNode, DashRepresentationInfo *representationInfo);
115     void GetRepresentationElement(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement,
116                                   const DashPeriodInfo *periodInfo,
117                                   const DashAdptSetInfo *adptSetInfo, DashRepresentationInfo *representationInfo);
118     void ProcessRepresentationElement(std::shared_ptr<XmlParser> &xmlParser, DashRepresentationInfo *representationInfo,
119                                       std::shared_ptr<XmlElement> &childElement, DashElementList &dashElementList);
120 
121     void InheritMultSegBase(DashMultSegBaseInfo *lowerMultSegBaseInfo,
122                             const DashMultSegBaseInfo *higherMultSegBaseInfo);
123     void InheritSegBase(DashSegBaseInfo *lowerSegBaseInfo, const DashSegBaseInfo *higherSegBaseInfo) const;
124     time_t String2Time(const std::string szTime);
125     void ParseElement(std::shared_ptr<XmlParser> &xmlParser, DashSegTmpltInfo *segTmplt,
126                       std::shared_ptr<XmlElement> &childElement);
127     void ParsePeriodElement(std::shared_ptr<XmlParser> &xmlParser,
128                             DashList<std::shared_ptr<XmlElement>> &periodElementList);
129     void GetContentProtection(const std::shared_ptr<XmlParser> &xmlParser, DashDescriptor *contentProtection,
130                               std::shared_ptr<XmlElement> &childElement) const;
131     void ParseElementUrlType(std::shared_ptr<XmlParser> &xmlParser, DashSegBaseInfo *segBase,
132                              std::shared_ptr<XmlElement> &childElement);
133     void GetAdptSetCommonAttr(IDashMpdNode *adptSetNode, DashAdptSetInfo *adptSetInfo) const;
134 
135 private:
136     static constexpr const char *MPD_LABEL_MPD = "MPD";
137     static constexpr const char *MPD_LABEL_BASE_URL = "BaseURL";
138     static constexpr const char *MPD_LABEL_PERIOD = "Period";
139     static constexpr const char *MPD_LABEL_SEGMENT_BASE = "SegmentBase";
140     static constexpr const char *MPD_LABEL_SEGMENT_LIST = "SegmentList";
141     static constexpr const char *MPD_LABEL_SEGMENT_TEMPLATE = "SegmentTemplate";
142     static constexpr const char *MPD_LABEL_ADAPTATIONSET = "AdaptationSet";
143     static constexpr const char *MPD_LABEL_REPRESENTATION = "Representation";
144     static constexpr const char *MPD_LABEL_INITIALIZATION = "Initialization";
145     static constexpr const char *MPD_LABEL_REPRESENTATION_INDEX = "RepresentationIndex";
146     static constexpr const char *MPD_LABEL_BITSTREAM_SWITCHING = "BitstreamSwitching";
147     static constexpr const char *MPD_LABEL_SEGMENT_TIMELINE = "SegmentTimeline";
148     static constexpr const char *MPD_LABEL_SEGMENT_TIMELINE_S = "S";
149     static constexpr const char *MPD_LABEL_SEGMENT_URL = "SegmentURL";
150     static constexpr const char *MPD_LABEL_CONTENT_COMPONENT = "ContentComponent";
151     static constexpr const char *MPD_LABEL_CONTENT_PROTECTION = "ContentProtection";
152     static constexpr const char *MPD_LABEL_ESSENTIAL_PROPERTY = "EssentialProperty";
153     static constexpr const char *MPD_LABEL_AUDIO_CHANNEL_CONFIGURATION = "AudioChannelConfiguration";
154 
155     DashMpdInfo dashMpdInfo_;
156     int32_t stopFlag_{0};
157 };
158 } // namespace HttpPluginLite
159 } // namespace Plugin
160 } // namespace Media
161 } // namespace OHOS
162 #endif // DASH_MPD_PARSER_H
163