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 }
62
SnifferMediaType(int32_t streamID)63 std::string BaseStreamDemuxer::SnifferMediaType(int32_t streamID)
64 {
65 MediaAVCodec::AVCodecTrace trace("BaseStreamDemuxer::SnifferMediaType");
66 MEDIA_LOG_I("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("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("SetBundleName bundleName: " PUBLIC_LOG_S, bundleName.c_str());
85 bundleName_ = bundleName;
86 }
87
SetIsIgnoreParse(bool state)88 void BaseStreamDemuxer::SetIsIgnoreParse(bool state)
89 {
90 return isIgnoreParse_.store(state);
91 }
92
GetIsIgnoreParse()93 bool BaseStreamDemuxer::GetIsIgnoreParse()
94 {
95 return isIgnoreParse_.load();
96 }
97
GetSeekable()98 Plugins::Seekable BaseStreamDemuxer::GetSeekable()
99 {
100 return source_->GetSeekable();
101 }
102
SetInterruptState(bool isInterruptNeeded)103 void BaseStreamDemuxer::SetInterruptState(bool isInterruptNeeded)
104 {
105 isInterruptNeeded_ = isInterruptNeeded;
106 }
107
IsDash() const108 bool BaseStreamDemuxer::IsDash() const
109 {
110 return isDash_;
111 }
112
SetIsDash(bool flag)113 void BaseStreamDemuxer::SetIsDash(bool flag)
114 {
115 isDash_ = flag;
116 }
117
SetNewVideoStreamID(int32_t streamID)118 Status BaseStreamDemuxer::SetNewVideoStreamID(int32_t streamID)
119 {
120 MEDIA_LOG_I("SetNewVideoStreamID id: " PUBLIC_LOG_D32, streamID);
121 SetChangeFlag(false);
122 newVideoStreamID_.store(streamID);
123 return Status::OK;
124 }
125
SetNewAudioStreamID(int32_t streamID)126 Status BaseStreamDemuxer::SetNewAudioStreamID(int32_t streamID)
127 {
128 MEDIA_LOG_I_SHORT("SetNewAudioStreamID id: " PUBLIC_LOG_D32, streamID);
129 SetChangeFlag(false);
130 newAudioStreamID_.store(streamID);
131 return Status::OK;
132 }
133
SetNewSubtitleStreamID(int32_t streamID)134 Status BaseStreamDemuxer::SetNewSubtitleStreamID(int32_t streamID)
135 {
136 MEDIA_LOG_I_SHORT("SetNewSubtitleStreamID id: " PUBLIC_LOG_D32, streamID);
137 SetChangeFlag(false);
138 newSubtitleStreamID_.store(streamID);
139 return Status::OK;
140 }
141
GetNewVideoStreamID()142 int32_t BaseStreamDemuxer::GetNewVideoStreamID()
143 {
144 return newVideoStreamID_.load();
145 }
146
GetNewAudioStreamID()147 int32_t BaseStreamDemuxer::GetNewAudioStreamID()
148 {
149 return newAudioStreamID_.load();
150 }
151
GetNewSubtitleStreamID()152 int32_t BaseStreamDemuxer::GetNewSubtitleStreamID()
153 {
154 return newSubtitleStreamID_.load();
155 }
156
CanDoChangeStream()157 bool BaseStreamDemuxer::CanDoChangeStream()
158 {
159 return changeStreamFlag_.load();
160 }
161
SetChangeFlag(bool flag)162 void BaseStreamDemuxer::SetChangeFlag(bool flag)
163 {
164 return changeStreamFlag_.store(flag);
165 }
166
SetIsExtSubtitle(bool flag)167 void BaseStreamDemuxer::SetIsExtSubtitle(bool flag)
168 {
169 return isExSubtitle_.store(flag);
170 }
171
GetIsExtSubtitle()172 bool BaseStreamDemuxer::GetIsExtSubtitle()
173 {
174 return isExSubtitle_.load();
175 }
176 } // namespace Media
177 } // namespace OHOS
178