1 /*
2  * Copyright (c) 2023-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 #define HST_LOG_TAG "BaseStreamDemuxer"
17 
18 #include "base_stream_demuxer.h"
19 
20 #include <algorithm>
21 #include <map>
22 #include <memory>
23 
24 #include "avcodec_common.h"
25 #include "avcodec_trace.h"
26 #include "cpp_ext/type_traits_ext.h"
27 #include "buffer/avallocator.h"
28 #include "common/event.h"
29 #include "common/log.h"
30 #include "meta/media_types.h"
31 #include "meta/meta.h"
32 #include "osal/utils/dump_buffer.h"
33 #include "plugin/plugin_buffer.h"
34 #include "plugin/plugin_info.h"
35 #include "plugin/plugin_time.h"
36 
37 namespace {
38 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "BaseStreamDemuxer" };
39 }
40 
41 namespace OHOS {
42 namespace Media {
43 
BaseStreamDemuxer()44 BaseStreamDemuxer::BaseStreamDemuxer()
45 {
46     MEDIA_LOG_I("BaseStreamDemuxer called");
47     seekable_ = Plugins::Seekable::UNSEEKABLE;
48 }
49 
~BaseStreamDemuxer()50 BaseStreamDemuxer::~BaseStreamDemuxer()
51 {
52     MEDIA_LOG_D("~BaseStreamDemuxer called");
53 }
54 
SetSource(const std::shared_ptr<Source> & source)55 void BaseStreamDemuxer::SetSource(const std::shared_ptr<Source>& source)
56 {
57     MEDIA_LOG_I("BaseStreamDemuxer::SetSource");
58     source_ = source;
59     source_->GetSize(mediaDataSize_);
60     seekable_ = source_->GetSeekable();
61     isDataSrcNoSeek_ = (seekable_ == Plugins::Seekable::UNSEEKABLE && sourceType_ == SourceType::SOURCE_TYPE_STREAM);
62     MEDIA_LOG_I("mediaDataSize_: " PUBLIC_LOG_U64 ", seekable_: " PUBLIC_LOG_D32 ", source_->GetSourceType(): "
63         PUBLIC_LOG_D32 ", isDataSrcNoSeek_: " PUBLIC_LOG_D32, mediaDataSize_, seekable_, sourceType_, isDataSrcNoSeek_);
64 }
65 
GetIsDataSrcNoSeek()66 bool BaseStreamDemuxer::GetIsDataSrcNoSeek()
67 {
68     return isDataSrcNoSeek_;
69 }
70 
SetSourceType(SourceType type)71 void BaseStreamDemuxer::SetSourceType(SourceType type)
72 {
73     sourceType_ = type;
74 }
75 
SnifferMediaType(int32_t streamID)76 std::string BaseStreamDemuxer::SnifferMediaType(int32_t streamID)
77 {
78     MediaAVCodec::AVCodecTrace trace("BaseStreamDemuxer::SnifferMediaType");
79     MEDIA_LOG_I("BaseStreamDemuxer::SnifferMediaType called");
80     std::shared_ptr<TypeFinder> typeFinder = std::make_shared<TypeFinder>();
81     typeFinder->Init(uri_, mediaDataSize_, checkRange_, peekRange_, streamID);
82     std::string type = typeFinder->FindMediaType();
83     MEDIA_LOG_D("SnifferMediaType result type: " PUBLIC_LOG_S, type.c_str());
84     return type;
85 }
86 
SetDemuxerState(int32_t streamId,DemuxerState state)87 void BaseStreamDemuxer::SetDemuxerState(int32_t streamId, DemuxerState state)
88 {
89     pluginStateMap_[streamId] = state;
90     if ((IsDash() || streamId == 0) && state == DemuxerState::DEMUXER_STATE_PARSE_FRAME) {
91         source_->SetDemuxerState(streamId);
92     }
93 }
94 
SetBundleName(const std::string & bundleName)95 void BaseStreamDemuxer::SetBundleName(const std::string& bundleName)
96 {
97     MEDIA_LOG_I("SetBundleName bundleName: " PUBLIC_LOG_S, bundleName.c_str());
98     bundleName_ = bundleName;
99 }
100 
SetIsIgnoreParse(bool state)101 void BaseStreamDemuxer::SetIsIgnoreParse(bool state)
102 {
103     return isIgnoreParse_.store(state);
104 }
105 
GetIsIgnoreParse()106 bool BaseStreamDemuxer::GetIsIgnoreParse()
107 {
108     return isIgnoreParse_.load();
109 }
110 
GetSeekable()111 Plugins::Seekable BaseStreamDemuxer::GetSeekable()
112 {
113     return source_->GetSeekable();
114 }
115 
SetInterruptState(bool isInterruptNeeded)116 void BaseStreamDemuxer::SetInterruptState(bool isInterruptNeeded)
117 {
118     isInterruptNeeded_ = isInterruptNeeded;
119 }
120 
IsDash() const121 bool BaseStreamDemuxer::IsDash() const
122 {
123     return isDash_;
124 }
125 
SetIsDash(bool flag)126 void BaseStreamDemuxer::SetIsDash(bool flag)
127 {
128     isDash_ = flag;
129 }
130 
SetNewVideoStreamID(int32_t streamID)131 Status BaseStreamDemuxer::SetNewVideoStreamID(int32_t streamID)
132 {
133     MEDIA_LOG_I("SetNewVideoStreamID id: " PUBLIC_LOG_D32, streamID);
134     SetChangeFlag(false);
135     newVideoStreamID_.store(streamID);
136     return Status::OK;
137 }
138 
SetNewAudioStreamID(int32_t streamID)139 Status BaseStreamDemuxer::SetNewAudioStreamID(int32_t streamID)
140 {
141     MEDIA_LOG_I_SHORT("SetNewAudioStreamID id: " PUBLIC_LOG_D32, streamID);
142     SetChangeFlag(false);
143     newAudioStreamID_.store(streamID);
144     return Status::OK;
145 }
146 
SetNewSubtitleStreamID(int32_t streamID)147 Status BaseStreamDemuxer::SetNewSubtitleStreamID(int32_t streamID)
148 {
149     MEDIA_LOG_I_SHORT("SetNewSubtitleStreamID id: " PUBLIC_LOG_D32, streamID);
150     SetChangeFlag(false);
151     newSubtitleStreamID_.store(streamID);
152     return Status::OK;
153 }
154 
GetNewVideoStreamID()155 int32_t BaseStreamDemuxer::GetNewVideoStreamID()
156 {
157     return newVideoStreamID_.load();
158 }
159 
GetNewAudioStreamID()160 int32_t BaseStreamDemuxer::GetNewAudioStreamID()
161 {
162     return newAudioStreamID_.load();
163 }
164 
GetNewSubtitleStreamID()165 int32_t BaseStreamDemuxer::GetNewSubtitleStreamID()
166 {
167     return newSubtitleStreamID_.load();
168 }
169 
CanDoChangeStream()170 bool BaseStreamDemuxer::CanDoChangeStream()
171 {
172     return changeStreamFlag_.load();
173 }
174 
SetChangeFlag(bool flag)175 void BaseStreamDemuxer::SetChangeFlag(bool flag)
176 {
177     return changeStreamFlag_.store(flag);
178 }
179 
SetIsExtSubtitle(bool flag)180 void BaseStreamDemuxer::SetIsExtSubtitle(bool flag)
181 {
182     return isExSubtitle_.store(flag);
183 }
184 
GetIsExtSubtitle()185 bool BaseStreamDemuxer::GetIsExtSubtitle()
186 {
187     return isExSubtitle_.load();
188 }
189 } // namespace Media
190 } // namespace OHOS
191