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 #include "dash_representation_node.h"
17 #include "dash_mpd_util.h"
18 
19 namespace OHOS {
20 namespace Media {
21 namespace Plugins {
22 namespace HttpPlugin {
DashRepresentationNode()23 DashRepresentationNode::DashRepresentationNode()
24 {
25     for (uint32_t index = 0; index < DASH_REPRESENTATION_ATTR_NUM; index++) {
26         representationAttr_[index].attr_.assign(representationAttrs_[index]);
27         representationAttr_[index].val_.assign("");
28     }
29 }
30 
~DashRepresentationNode()31 DashRepresentationNode::~DashRepresentationNode() {}
32 
ParseNode(std::shared_ptr<XmlParser> xmlParser,std::shared_ptr<XmlElement> rootElement)33 void DashRepresentationNode::ParseNode(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement)
34 {
35     if (xmlParser != nullptr) {
36         comAttrsElements_.ParseAttrs(xmlParser, rootElement);
37 
38         for (uint32_t index = 0; index < DASH_REPRESENTATION_ATTR_NUM; index++) {
39             xmlParser->GetAttribute(rootElement, representationAttr_[index].attr_, representationAttr_[index].val_);
40         }
41     }
42 }
43 
GetAttr(const std::string & attrName,std::string & sAttrVal)44 void DashRepresentationNode::GetAttr(const std::string &attrName, std::string &sAttrVal)
45 {
46     uint32_t index = DashGetAttrIndex(attrName, representationAttrs_, DASH_REPRESENTATION_ATTR_NUM);
47     if (index < DASH_REPRESENTATION_ATTR_NUM) {
48         if (representationAttr_[index].val_.length() > 0) {
49             sAttrVal = representationAttr_[index].val_;
50         } else {
51             sAttrVal.assign("");
52         }
53     } else {
54         comAttrsElements_.GetAttr(attrName, sAttrVal);
55     }
56 }
57 
GetAttr(const std::string & attrName,uint32_t & uiAttrVal)58 void DashRepresentationNode::GetAttr(const std::string &attrName, uint32_t &uiAttrVal)
59 {
60     uint32_t index = DashGetAttrIndex(attrName, representationAttrs_, DASH_REPRESENTATION_ATTR_NUM);
61     if (index < DASH_REPRESENTATION_ATTR_NUM) {
62         if (representationAttr_[index].val_.length() > 0) {
63             uiAttrVal = static_cast<uint32_t>(std::atoll(representationAttr_[index].val_.c_str()));
64         } else {
65             uiAttrVal = 0;
66         }
67     } else {
68         comAttrsElements_.GetAttr(attrName, uiAttrVal);
69     }
70 }
71 
GetAttr(const std::string & attrName,int32_t & iAttrVal)72 void DashRepresentationNode::GetAttr(const std::string &attrName, int32_t &iAttrVal)
73 {
74     uint32_t index = DashGetAttrIndex(attrName, representationAttrs_, DASH_REPRESENTATION_ATTR_NUM);
75     if (index < DASH_REPRESENTATION_ATTR_NUM) {
76         if (representationAttr_[index].val_.length() > 0) {
77             iAttrVal = static_cast<int32_t>(std::atoll(representationAttr_[index].val_.c_str()));
78         } else {
79             iAttrVal = 0;
80         }
81     } else {
82         comAttrsElements_.GetAttr(attrName, iAttrVal);
83     }
84 }
85 
GetAttr(const std::string & attrName,uint64_t & ullAttrVal)86 void DashRepresentationNode::GetAttr(const std::string &attrName, uint64_t &ullAttrVal)
87 {
88     uint32_t index = DashGetAttrIndex(attrName, representationAttrs_, DASH_REPRESENTATION_ATTR_NUM);
89     if (index < DASH_REPRESENTATION_ATTR_NUM) {
90         if (representationAttr_[index].val_.length() > 0) {
91             ullAttrVal = static_cast<uint64_t>(std::atoll(representationAttr_[index].val_.c_str()));
92         } else {
93             ullAttrVal = 0;
94         }
95     } else {
96         comAttrsElements_.GetAttr(attrName, ullAttrVal);
97     }
98 }
99 
GetAttr(const std::string & attrName,double & dAttrVal)100 void DashRepresentationNode::GetAttr(const std::string &attrName, double &dAttrVal)
101 {
102     uint32_t index = DashGetAttrIndex(attrName, representationAttrs_, DASH_REPRESENTATION_ATTR_NUM);
103     if (index < DASH_REPRESENTATION_ATTR_NUM) {
104         if (representationAttr_[index].val_.length() > 0) {
105             dAttrVal = atof(representationAttr_[index].val_.c_str());
106         } else {
107             dAttrVal = 0.0;
108         }
109     } else {
110         comAttrsElements_.GetAttr(attrName, dAttrVal);
111     }
112 }
113 } // namespace HttpPluginLite
114 } // namespace Plugin
115 } // namespace Media
116 } // namespace OHOS