1 /*
2  * Copyright (c) 2023-2023 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_D_SHORT("BaseStreamDemuxer called");
47     seekable_ = Plugins::Seekable::UNSEEKABLE;
48 }
49 
~BaseStreamDemuxer()50 BaseStreamDemuxer::~BaseStreamDemuxer()
51 {
52     MEDIA_LOG_D_SHORT("~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_D_SHORT("BaseStreamDemuxer::SetSource");
58     source_ = source;
59     source_->GetSize(mediaDataSize_);
60     seekable_ = source_->GetSeekable();
61 }
62 
SnifferMediaType(int32_t streamID)63 std::string BaseStreamDemuxer::SnifferMediaType(int32_t streamID)
64 {
65     MediaAVCodec::AVCodecTrace trace("BaseStreamDemuxer::SnifferMediaType");
66     MEDIA_LOG_I_SHORT("BaseStreamDemuxer::SnifferMediaType called");
67     std::shared_ptr<TypeFinder> typeFinder = std::make_shared<TypeFinder>();
68     typeFinder->Init(uri_, mediaDataSize_, checkRange_, peekRange_, streamID);
69     std::string type = typeFinder->FindMediaType();
70     MEDIA_LOG_D_SHORT("SnifferMediaType result type: " PUBLIC_LOG_S, type.c_str());
71     return type;
72 }
73 
SetDemuxerState(int32_t streamId, DemuxerState state)74 void BaseStreamDemuxer::SetDemuxerState(int32_t streamId, DemuxerState state)
75 {
76     pluginStateMap_[streamId] = state;
77     if ((IsDash() || streamId == 0) && state == DemuxerState::DEMUXER_STATE_PARSE_FRAME) {
78         source_->SetDemuxerState(streamId);
79     }
80 }
81 
SetBundleName(const std::string& bundleName)82 void BaseStreamDemuxer::SetBundleName(const std::string& bundleName)
83 {
84     MEDIA_LOG_I_SHORT("SetBundleName bundleName: " PUBLIC_LOG_S, bundleName.c_str());
85     bundleName_ = bundleName;
86 }
87 
SetInterruptState(bool isInterruptNeeded)88 void BaseStreamDemuxer::SetInterruptState(bool isInterruptNeeded)
89 {
90     isInterruptNeeded_ = isInterruptNeeded;
91 }
92 
SetIsIgnoreParse(bool state)93 void BaseStreamDemuxer::SetIsIgnoreParse(bool state)
94 {
95     return isIgnoreParse_.store(state);
96 }
97 
GetIsIgnoreParse()98 bool BaseStreamDemuxer::GetIsIgnoreParse()
99 {
100     return isIgnoreParse_.load();
101 }
102 
GetSeekable()103 Plugins::Seekable BaseStreamDemuxer::GetSeekable()
104 {
105     return source_->GetSeekable();
106 }
107 
IsDash() const108 bool BaseStreamDemuxer::IsDash() const
109 {
110     return isDash_;
111 }
SetIsDash(bool flag)112 void BaseStreamDemuxer::SetIsDash(bool flag)
113 {
114     isDash_ = flag;
115 }
116 
SetNewVideoStreamID(int32_t streamID)117 Status BaseStreamDemuxer::SetNewVideoStreamID(int32_t streamID)
118 {
119     MEDIA_LOG_I_SHORT("SetNewVideoStreamID id: " PUBLIC_LOG_D32, streamID);
120     SetChangeFlag(false);
121     newVideoStreamID_.store(streamID);
122     return Status::OK;
123 }
124 
SetNewAudioStreamID(int32_t streamID)125 Status BaseStreamDemuxer::SetNewAudioStreamID(int32_t streamID)
126 {
127     MEDIA_LOG_I("SetNewAudioStreamID id: " PUBLIC_LOG_D32, streamID);
128     SetChangeFlag(false);
129     newAudioStreamID_.store(streamID);
130     return Status::OK;
131 }
132 
SetNewSubtitleStreamID(int32_t streamID)133 Status BaseStreamDemuxer::SetNewSubtitleStreamID(int32_t streamID)
134 {
135     MEDIA_LOG_I("SetNewSubtitleStreamID id: " PUBLIC_LOG_D32, streamID);
136     SetChangeFlag(false);
137     newSubtitleStreamID_.store(streamID);
138     return Status::OK;
139 }
140 
GetNewVideoStreamID()141 int32_t BaseStreamDemuxer::GetNewVideoStreamID()
142 {
143     return newVideoStreamID_.load();
144 }
145 
GetNewAudioStreamID()146 int32_t BaseStreamDemuxer::GetNewAudioStreamID()
147 {
148     return newAudioStreamID_.load();
149 }
150 
GetNewSubtitleStreamID()151 int32_t BaseStreamDemuxer::GetNewSubtitleStreamID()
152 {
153     return newSubtitleStreamID_.load();
154 }
155 
CanDoChangeStream()156 bool BaseStreamDemuxer::CanDoChangeStream()
157 {
158     return changeStreamFlag_.load();
159 }
160 
SetChangeFlag(bool flag)161 void BaseStreamDemuxer::SetChangeFlag(bool flag)
162 {
163     return changeStreamFlag_.store(flag);
164 }
165 } // namespace Media
166 } // namespace OHOS
167