• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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 "AudioDecoderFilter"
17 
18 #include "pipeline/filters/codec/audio_decoder/audio_decoder_filter.h"
19 #include "foundation/log.h"
20 #include "foundation/osal/utils/util.h"
21 #include "foundation/utils/steady_clock.h"
22 #include "pipeline/factory/filter_factory.h"
23 #include "pipeline/filters/codec/codec_filter_factory.h"
24 
25 namespace {
26 constexpr uint32_t DEFAULT_IN_BUFFER_POOL_SIZE = 5;
27 constexpr uint32_t DEFAULT_OUT_BUFFER_POOL_SIZE = 5;
28 constexpr int32_t MAX_SAMPLE_PER_FRAME = 10240; // 10240 set max samples per frame
29 };
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Pipeline {
34 #ifdef OHOS_LITE
35 static AutoRegisterFilter<AudioDecoderFilter> g_registerAudioDecoderFilter("builtin.player.audiodecoder",
__anondad5829d0202(const std::string& name) 36     [](const std::string& name) { return CreateCodecFilter(name, FilterCodecMode::AUDIO_SYNC_DECODER); });
37 #else
38 static AutoRegisterFilter<AudioDecoderFilter> g_registerAudioDecoderFilter("builtin.player.audiodecoder",
39     [](const std::string& name) { return CreateCodecFilter(name, FilterCodecMode::AUDIO_ASYNC_DECODER); });
40 #endif
41 
AudioDecoderFilter(const std::string & name,std::shared_ptr<CodecMode> codecMode)42 AudioDecoderFilter::AudioDecoderFilter(const std::string& name, std::shared_ptr<CodecMode> codecMode)
43     : CodecFilterBase(name)
44 {
45     MEDIA_LOG_D("audio decoder ctor called");
46     filterType_ = FilterType::AUDIO_DECODER;
47     bufferMetaType_ = Plugin::BufferMetaType::AUDIO;
48     pluginType_ = Plugin::PluginType::AUDIO_DECODER;
49     codecMode_ = std::move(codecMode);
50 }
51 
~AudioDecoderFilter()52 AudioDecoderFilter::~AudioDecoderFilter()
53 {
54     MEDIA_LOG_D("audio decoder dtor called");
55     if (plugin_) {
56         plugin_->Stop();
57         plugin_->Deinit();
58     }
59     (void)codecMode_->Release();
60 }
61 
Prepare()62 ErrorCode AudioDecoderFilter::Prepare()
63 {
64     MEDIA_LOG_I("audio decoder prepare called.");
65 #ifndef OHOH_LITE
66     codecMode_->SetBufferPoolSize(static_cast<uint32_t>(DEFAULT_IN_BUFFER_POOL_SIZE),
67                                   static_cast<uint32_t>(DEFAULT_OUT_BUFFER_POOL_SIZE));
68 #endif
69     (void)codecMode_->Prepare();
70     return CodecFilterBase::Prepare();
71 }
72 
Stop()73 ErrorCode AudioDecoderFilter::Stop()
74 {
75     MEDIA_LOG_D("audio decoder stop start.");
76     FAIL_RETURN(CodecFilterBase::Stop());
77     MEDIA_LOG_D("audio decoder stop end.");
78     return ErrorCode::SUCCESS;
79 }
80 
Negotiate(const std::string & inPort,const std::shared_ptr<const Plugin::Capability> & upstreamCap,Plugin::Capability & negotiatedCap,const Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)81 bool AudioDecoderFilter::Negotiate(const std::string& inPort,
82                                    const std::shared_ptr<const Plugin::Capability>& upstreamCap,
83                                    Plugin::Capability& negotiatedCap,
84                                    const Plugin::Meta& upstreamParams,
85                                    Plugin::Meta& downstreamParams)
86 {
87     FALSE_RETURN_V(CodecFilterBase::Negotiate(inPort, upstreamCap, negotiatedCap, upstreamParams, downstreamParams),
88                    false);
89     MEDIA_LOG_D("audio decoder negotiate end");
90     return true;
91 }
92 
Configure(const std::string & inPort,const std::shared_ptr<const Plugin::Meta> & upstreamMeta,Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)93 bool AudioDecoderFilter::Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta,
94                                    Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams)
95 {
96     PROFILE_BEGIN("audio decoder configure begin");
97     FALSE_RETURN_V(CodecFilterBase::Configure(inPort, upstreamMeta, upstreamParams, downstreamParams), false);
98     PROFILE_END("audio decoder configure end");
99     return true;
100 }
101 
FlushStart()102 void AudioDecoderFilter::FlushStart()
103 {
104     MEDIA_LOG_D("audio decoder FlushStart entered.");
105     codecMode_->FlushStart();
106     CodecFilterBase::FlushStart();
107     MEDIA_LOG_D("audio decoder FlushStart exit.");
108 }
109 
FlushEnd()110 void AudioDecoderFilter::FlushEnd()
111 {
112     MEDIA_LOG_I("audio decoder FlushEnd entered");
113     codecMode_->FlushEnd();
114     CodecFilterBase::FlushEnd();
115 }
116 
OnInputBufferDone(const std::shared_ptr<Plugin::Buffer> & input)117 void AudioDecoderFilter::OnInputBufferDone(const std::shared_ptr<Plugin::Buffer>& input)
118 {
119     MEDIA_LOG_D("AudioDecoderFilter::OnInputBufferDone");
120 }
121 
OnOutputBufferDone(const std::shared_ptr<Plugin::Buffer> & output)122 void AudioDecoderFilter::OnOutputBufferDone(const std::shared_ptr<Plugin::Buffer>& output)
123 {
124     codecMode_->OnOutputBufferDone(output);
125 }
126 
CalculateBufferSize(const std::shared_ptr<const Plugin::Meta> & meta)127 uint32_t AudioDecoderFilter::CalculateBufferSize(const std::shared_ptr<const Plugin::Meta>& meta)
128 {
129     using namespace OHOS::Media;
130     uint32_t samplesPerFrame;
131     FALSE_RETURN_V(meta->Get<Plugin::Tag::AUDIO_SAMPLE_PER_FRAME>(samplesPerFrame), 0);
132 
133     uint32_t channels;
134     FALSE_RETURN_V(meta->Get<Plugin::Tag::AUDIO_CHANNELS>(channels), 0);
135 
136     Plugin::AudioSampleFormat format;
137     FALSE_RETURN_V(meta->Get<Plugin::Tag::AUDIO_SAMPLE_FORMAT>(format), 0);
138 
139     return Pipeline::GetBytesPerSample(format) * samplesPerFrame * channels;
140 }
141 
GetRequiredOutCapKeys()142 std::vector<Capability::Key> AudioDecoderFilter::GetRequiredOutCapKeys()
143 {
144     std::vector<Capability::Key> capKey;
145     capKey.push_back(Capability::Key::AUDIO_SAMPLE_FORMAT);
146     return capKey;
147 }
148 
UpdateParams(const std::shared_ptr<const Plugin::Meta> & upMeta,std::shared_ptr<Plugin::Meta> & meta)149 void AudioDecoderFilter::UpdateParams(const std::shared_ptr<const Plugin::Meta>& upMeta,
150                                       std::shared_ptr<Plugin::Meta>& meta)
151 {
152     uint32_t samplesPerFrame = 0;
153     if (GetPluginParameterLocked(Tag::AUDIO_SAMPLE_PER_FRAME, samplesPerFrame) != ErrorCode::SUCCESS) {
154         MEDIA_LOG_W("Can't acquire samples per frame from decoder plugin: " PUBLIC_LOG_S, pluginInfo_->name.c_str());
155         samplesPerFrame = MAX_SAMPLE_PER_FRAME;
156     }
157     FALSE_LOG_MSG(meta->Set<Plugin::Tag::AUDIO_SAMPLE_PER_FRAME>(samplesPerFrame),
158                   "Set per sample frame failed.");
159     bool useStreamChannelParams {false};
160     auto iter = sinkParams_.Find(Plugin::Tag::AUDIO_OUTPUT_CHANNELS);
161     if (iter != std::end(sinkParams_) && iter->second.SameTypeWith(typeid(uint32_t))) {
162         auto outputChannels = Plugin::AnyCast<uint32_t>(iter->second);
163         uint32_t upChannels {0};
164         FALSE_LOG(upMeta->Get<Plugin::Tag::AUDIO_CHANNELS>(upChannels));
165         if (upChannels < outputChannels) {
166             outputChannels = upChannels;
167             useStreamChannelParams = true;
168         }
169         if (plugin_ != nullptr &&
170             plugin_->SetParameter(Plugin::Tag::AUDIO_OUTPUT_CHANNELS, outputChannels) != Plugin::Status::OK) {
171             MEDIA_LOG_W("Set outputChannels to plugin " PUBLIC_LOG_S " failed", plugin_->GetName().c_str());
172         }
173         FALSE_LOG_MSG(meta->Set<Plugin::Tag::AUDIO_OUTPUT_CHANNELS>(outputChannels), "Set channel failed.");
174     }
175     iter = sinkParams_.Find(Plugin::Tag::AUDIO_OUTPUT_CHANNEL_LAYOUT);
176     if (iter != std::end(sinkParams_) && iter->second.SameTypeWith(typeid(Plugin::AudioChannelLayout))) {
177         auto outputChanLayout = Plugin::AnyCast<Plugin::AudioChannelLayout>(iter->second);
178         Plugin::AudioChannelLayout upAudioChannelLayout;
179         if (useStreamChannelParams && upMeta->Get<Plugin::Tag::AUDIO_CHANNEL_LAYOUT>(upAudioChannelLayout)) {
180             outputChanLayout = upAudioChannelLayout;
181         }
182         if (plugin_ != nullptr &&
183             plugin_->SetParameter(Plugin::Tag::AUDIO_OUTPUT_CHANNEL_LAYOUT, outputChanLayout) != Plugin::Status::OK) {
184             MEDIA_LOG_W("Set outputChannelLayout to plugin " PUBLIC_LOG_S " failed", plugin_->GetName().c_str());
185         }
186         FALSE_LOG(meta->Set<Plugin::Tag::AUDIO_OUTPUT_CHANNEL_LAYOUT>(outputChanLayout));
187     }
188 }
189 } // Pipeline
190 } // Media
191 } // OHOS