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