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_manager.h"
17 #include "dash_manager_util.h"
18 
19 namespace OHOS {
20 namespace Media {
21 namespace Plugins {
22 namespace HttpPlugin {
DashRepresentationManager(DashRepresentationInfo * rep)23 DashRepresentationManager::DashRepresentationManager(DashRepresentationInfo *rep)
24 {
25     SetRepresentationInfo(rep);
26 }
27 
~DashRepresentationManager()28 DashRepresentationManager::~DashRepresentationManager()
29 {
30     Clear();
31 }
32 
Reset()33 void DashRepresentationManager::Reset()
34 {
35     this->representationInfo_ = nullptr;
36     this->previousRepresentationInfo_ = nullptr;
37     Clear();
38 }
39 
SetRepresentationInfo(DashRepresentationInfo * rep)40 void DashRepresentationManager::SetRepresentationInfo(DashRepresentationInfo *rep)
41 {
42     representationInfo_ = rep;
43     // if representationInfo changed, update it
44     if (previousRepresentationInfo_ == nullptr || previousRepresentationInfo_ != rep) {
45         Init();
46         previousRepresentationInfo_ = rep;
47     }
48 }
49 
Init()50 void DashRepresentationManager::Init()
51 {
52     if (this->representationInfo_ == nullptr) {
53         return;
54     }
55 
56     // before init, clear initSegment
57     Clear();
58 
59     // ----------  init initial segment -------------------
60     ParseInitSegment();
61 }
62 
Clear()63 void DashRepresentationManager::Clear()
64 {
65     if (initSegment_ != nullptr) {
66         delete initSegment_;
67         initSegment_ = nullptr;
68     }
69 
70     this->segTmpltFlag_ = 0;
71 }
72 
ParseInitSegment()73 void DashRepresentationManager::ParseInitSegment()
74 {
75     if (representationInfo_->representationSegBase_ && representationInfo_->representationSegBase_->initialization_) {
76         // first get initSegment from <Representation> <SegmentBase> <Initialization /> </SegmentBase> </Representation>
77         initSegment_ = CloneUrlType(representationInfo_->representationSegBase_->initialization_);
78     } else if (representationInfo_->representationSegList_ &&
79                representationInfo_->representationSegList_->multSegBaseInfo_.segBaseInfo_.initialization_) {
80         // Second get initSegment from <SegmentList> <Initialization sourceURL="seg-m-init.mp4"/> </SegmentList>
81         initSegment_ =
82             CloneUrlType(representationInfo_->representationSegList_->multSegBaseInfo_.segBaseInfo_.initialization_);
83     } else if (representationInfo_->representationSegTmplt_) {
84         ParseInitSegmentBySegTmplt();
85     } else if (initSegment_ != nullptr) {
86         delete initSegment_;
87         initSegment_ = nullptr;
88     }
89 
90     // if sourceUrl is not present, use BaseURL, use BaseURL in mpd
91 }
92 
ParseInitSegmentBySegTmplt()93 void DashRepresentationManager::ParseInitSegmentBySegTmplt()
94 {
95     if (representationInfo_->representationSegTmplt_->multSegBaseInfo_.segBaseInfo_.initialization_) {
96         initSegment_ =
97             CloneUrlType(representationInfo_->representationSegTmplt_->multSegBaseInfo_.segBaseInfo_.initialization_);
98     } else if (representationInfo_->representationSegTmplt_->segTmpltInitialization_.length() > 0) {
99         initSegment_ = new DashUrlType;
100         if (initSegment_ != nullptr) {
101             initSegment_->sourceUrl_ = representationInfo_->representationSegTmplt_->segTmpltInitialization_;
102             this->segTmpltFlag_ = 1;
103         }
104     } else if (initSegment_ != nullptr) {
105         delete initSegment_;
106         initSegment_ = nullptr;
107     }
108 }
109 
GetInitSegment(int32_t & flag)110 DashUrlType *DashRepresentationManager::GetInitSegment(int32_t &flag)
111 {
112     flag = this->segTmpltFlag_;
113     return this->initSegment_;
114 }
115 
GetRepresentationInfo()116 DashRepresentationInfo *DashRepresentationManager::GetRepresentationInfo()
117 {
118     return this->representationInfo_;
119 }
120 
GetPreviousRepresentationInfo()121 DashRepresentationInfo *DashRepresentationManager::GetPreviousRepresentationInfo()
122 {
123     return this->previousRepresentationInfo_;
124 }
125 } // namespace HttpPluginLite
126 } // namespace Plugin
127 } // namespace Media
128 } // namespace OHOS