• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 #define MEDIA_PIPELINE
16 
17 #include "audio_decoder_filter.h"
18 #include "avcodec_trace.h"
19 #include "parameters.h"
20 #include "filter/filter_factory.h"
21 #include "common/log.h"
22 #include "common/media_core.h"
23 #include "sink/audio_sampleformat.h"
24 #include "avcodec_info.h"
25 #include "avcodec_sysevent.h"
26 #include "avcodec_trace.h"
27 #include "scoped_timer.h"
28 #ifdef SUPPORT_DRM
29 #include "imedia_key_session_service.h"
30 #endif
31 
32 namespace {
33 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "AudioDecoderFilter" };
34 }
35 
36 namespace OHOS {
37 namespace Media {
38 namespace Pipeline {
39 namespace {
40 constexpr uint32_t BUFFER_STATUS_INIT_PROCESS_ALWAYS =
41     static_cast<uint32_t>(InOutPortBufferStatus::INIT_PROCESS_ALWAYS);
42 constexpr uint32_t BUFFER_STATUS_INIT_IGNORE_RET = static_cast<uint32_t>(InOutPortBufferStatus::INIT_IGNORE_RET);
43 constexpr uint32_t BUFFER_STATUS_INIT = static_cast<uint32_t>(InOutPortBufferStatus::INIT);
44 constexpr uint32_t BUFFER_STATUS_AVAIL_IN_OUT = static_cast<uint32_t>(InOutPortBufferStatus::INPORT_AVAIL) |
45     static_cast<uint32_t>(InOutPortBufferStatus::OUTPORT_AVAIL);
46 constexpr uint32_t BUFFER_STATUS_AVAIL_IN = static_cast<uint32_t>(InOutPortBufferStatus::INPORT_AVAIL);
47 constexpr uint32_t BUFFER_STATUS_AVAIL_OUT = static_cast<uint32_t>(InOutPortBufferStatus::OUTPORT_AVAIL);
48 constexpr uint32_t BUFFER_STATUS_AVAIL_NONE = 0;
49 constexpr uint32_t BUFFER_STATUS_OUT_EOS_START = static_cast<uint32_t>(InOutPortBufferStatus::OUT_EOS_START);
50 constexpr uint32_t BUFFER_STATUS_AVAIL_OUT_OUT_EOS_START_DONE =
51     static_cast<uint32_t>(InOutPortBufferStatus::OUTPORT_AVAIL) |
52     static_cast<uint32_t>(InOutPortBufferStatus::OUT_EOS_START) |
53     static_cast<uint32_t>(InOutPortBufferStatus::OUT_EOS_DONE);
54 constexpr uint32_t BUFFER_STATUS_AVAIL_OUT_OUT_EOS_START = static_cast<uint32_t>(InOutPortBufferStatus::OUTPORT_AVAIL) |
55     static_cast<uint32_t>(InOutPortBufferStatus::OUT_EOS_START);
56 constexpr uint32_t BUFFER_STATUS_OUT_EOS_START_DONE = static_cast<uint32_t>(InOutPortBufferStatus::OUT_EOS_START) |
57     static_cast<uint32_t>(InOutPortBufferStatus::OUT_EOS_DONE);
58 }
59 
60 using namespace MediaAVCodec;
61 using namespace OHOS::Media::Plugins;
62 constexpr int32_t SAMPLE_RATE_48K = 48000;
63 constexpr int32_t SAMPLE_FORMAT_BIT_DEPTH_16 = 16;
64 constexpr int64_t DECODER_INIT_WARNING_MS = 50;
65 static AutoRegisterFilter<AudioDecoderFilter> g_registerAudioDecoderFilter("builtin.player.audiodecoder",
__anon1b26d54a0302(const std::string& name, const FilterType type) 66     FilterType::FILTERTYPE_ADEC, [](const std::string& name, const FilterType type) {
67         bool isAsyncMode = system::GetParameter("debug.media_service.audio.audiodecoder_async", "1") == "1";
68         return std::make_shared<AudioDecoderFilter>(name, FilterType::FILTERTYPE_ADEC, isAsyncMode);
69     });
70 
71 static const bool IS_FILTER_ASYNC = system::GetParameter("persist.media_service.async_filter", "1") == "1";
72 
73 class AudioDecoderFilterLinkCallback : public FilterLinkCallback {
74 public:
AudioDecoderFilterLinkCallback(std::shared_ptr<AudioDecoderFilter> codecFilter)75     explicit AudioDecoderFilterLinkCallback(std::shared_ptr<AudioDecoderFilter> codecFilter)
76         : codecFilter_(codecFilter) {}
77 
78     ~AudioDecoderFilterLinkCallback() = default;
79 
OnLinkedResult(const sptr<AVBufferQueueProducer> & queue,std::shared_ptr<Meta> & meta)80     void OnLinkedResult(const sptr<AVBufferQueueProducer> &queue, std::shared_ptr<Meta> &meta) override
81     {
82         if (auto codecFilter = codecFilter_.lock()) {
83             codecFilter->OnLinkedResult(queue, meta);
84         } else {
85             MEDIA_LOG_I("invalid codecFilter");
86         }
87     }
88 
OnUnlinkedResult(std::shared_ptr<Meta> & meta)89     void OnUnlinkedResult(std::shared_ptr<Meta> &meta) override
90     {
91         if (auto codecFilter = codecFilter_.lock()) {
92             codecFilter->OnUnlinkedResult(meta);
93         } else {
94             MEDIA_LOG_I("invalid codecFilter");
95         }
96     }
97 
OnUpdatedResult(std::shared_ptr<Meta> & meta)98     void OnUpdatedResult(std::shared_ptr<Meta> &meta) override
99     {
100         if (auto codecFilter = codecFilter_.lock()) {
101             codecFilter->OnUpdatedResult(meta);
102         } else {
103             MEDIA_LOG_I("invalid codecFilter");
104         }
105     }
106 private:
107     std::weak_ptr<AudioDecoderFilter> codecFilter_;
108 };
109 
110 class AudioDecInputPortConsumerListener : public OHOS::Media::IConsumerListener {
111 public:
AudioDecInputPortConsumerListener(std::shared_ptr<AudioDecoderFilter> audioDecoderFilter)112     explicit AudioDecInputPortConsumerListener(std::shared_ptr<AudioDecoderFilter> audioDecoderFilter)
113     {
114         audioDecoderFilter_ = audioDecoderFilter;
115     }
116     ~AudioDecInputPortConsumerListener() = default;
117 
OnBufferAvailable()118     void OnBufferAvailable() override
119     {
120         MEDIA_LOG_D("AudioDecInputPortConsumerListener OnBufferAvailable");
121         if (auto audioDecoderFilter = audioDecoderFilter_.lock()) {
122             audioDecoderFilter->HandleInputBuffer(false);
123         } else {
124             MEDIA_LOG_I("invalid audioDecoderFilter");
125         }
126     }
127 
128 private:
129     std::weak_ptr<AudioDecoderFilter> audioDecoderFilter_;
130 };
131 
132 class AudioDecOutPortProducerListener : public IRemoteStub<IProducerListener> {
133 public:
AudioDecOutPortProducerListener(std::shared_ptr<AudioDecoderFilter> audioDecoderFilter)134     explicit AudioDecOutPortProducerListener(std::shared_ptr<AudioDecoderFilter> audioDecoderFilter)
135     {
136         audioDecoderFilter_ = audioDecoderFilter;
137     }
138     virtual ~AudioDecOutPortProducerListener() = default;
139 
OnRemoteRequest(uint32_t code,MessageParcel & arguments,MessageParcel & reply,MessageOption & option)140     int OnRemoteRequest(uint32_t code, MessageParcel& arguments, MessageParcel& reply, MessageOption& option) override
141     {
142         return IPCObjectStub::OnRemoteRequest(code, arguments, reply, option);
143     }
144 
OnBufferAvailable()145     void OnBufferAvailable() override
146     {
147         MEDIA_LOG_D("AudioDecOutPortProducerListener OnBufferAvailable");
148         if (auto audioDecoderFilter = audioDecoderFilter_.lock()) {
149             audioDecoderFilter->HandleInputBuffer(true);
150         } else {
151             MEDIA_LOG_I("invalid audioDecoderFilter");
152         }
153     }
154 
155 private:
156     std::weak_ptr<AudioDecoderFilter> audioDecoderFilter_;
157 };
158 
AudioDecoderFilter(std::string name,FilterType type)159 AudioDecoderFilter::AudioDecoderFilter(std::string name, FilterType type): Filter(name, type, IS_FILTER_ASYNC)
160 {
161     filterType_ = type;
162     MEDIA_LOG_I_SHORT("audio decoder filter create");
163 }
164 
AudioDecoderFilter(std::string name,FilterType type,bool isAsyncMode)165 AudioDecoderFilter::AudioDecoderFilter(
166     std::string name, FilterType type, bool isAsyncMode): Filter(name, type, isAsyncMode)
167 {
168     filterType_ = type;
169     MEDIA_LOG_I_SHORT("audio decoder filter create, isAsyncMode:" PUBLIC_LOG_D32, isAsyncMode);
170 }
171 
~AudioDecoderFilter()172 AudioDecoderFilter::~AudioDecoderFilter()
173 {
174     Filter::StopFilterTask();
175     {
176         std::lock_guard<std::mutex> lock(releaseMutex_);
177         if (isReleased_.load()) {
178             MEDIA_LOG_W_SHORT("AudioDecoderFilter has beed released.");
179         } else {
180             isReleased_.store(true);
181             decoder_->Release();
182         }
183     }
184     MEDIA_LOG_I_SHORT("audio decoder filter destroy");
185 }
186 
Init(const std::shared_ptr<EventReceiver> & receiver,const std::shared_ptr<FilterCallback> & callback)187 void AudioDecoderFilter::Init(const std::shared_ptr<EventReceiver> &receiver,
188     const std::shared_ptr<FilterCallback> &callback)
189 {
190     MEDIA_LOG_I("AudioDecoderFilter::Init.");
191     eventReceiver_ = receiver;
192     filterCallback_ = callback;
193     decoder_ = std::make_shared<AudioDecoderAdapter>();
194 }
195 
DoPrepare()196 Status AudioDecoderFilter::DoPrepare()
197 {
198     MEDIA_LOG_I("AudioDecoderFilter::Prepare.");
199     Status ret = Status::OK;
200     switch (filterType_) {
201         case FilterType::FILTERTYPE_AENC:
202             MEDIA_LOG_I("AudioDecoderFilter::FILTERTYPE_AENC.");
203             ret = filterCallback_->OnCallback(shared_from_this(), FilterCallBackCommand::NEXT_FILTER_NEEDED,
204                 StreamType::STREAMTYPE_ENCODED_AUDIO);
205             break;
206         case FilterType::FILTERTYPE_ADEC:
207             ret = filterCallback_->OnCallback(shared_from_this(), FilterCallBackCommand::NEXT_FILTER_NEEDED,
208                 StreamType::STREAMTYPE_RAW_AUDIO);
209             break;
210         default:
211             break;
212     }
213     state_ = FilterState::READY;
214     return ret;
215 }
216 
DoStart()217 Status AudioDecoderFilter::DoStart()
218 {
219     MEDIA_LOG_I("AudioDecoderFilter::Start.");
220     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
221 
222     std::unique_lock<std::mutex> lock(bufferStatusMutex_);
223     bufferStatus_ = BUFFER_STATUS_INIT_PROCESS_ALWAYS;
224     auto ret = decoder_->Start();
225     if (ret != Status::OK) {
226         std::string mime;
227         meta_->GetData(Tag::MIME_TYPE, mime);
228         std::string instanceId = std::to_string(instanceId_);
229         struct AudioCodecFaultInfo audioCodecFaultInfo;
230         audioCodecFaultInfo.appName = appName_;
231         audioCodecFaultInfo.instanceId = instanceId;
232         audioCodecFaultInfo.callerType = "player_framework";
233         audioCodecFaultInfo.audioCodec = mime;
234         audioCodecFaultInfo.errMsg = "AudioDecoder start failed";
235         FaultAudioCodecEventWrite(audioCodecFaultInfo);
236     }
237     state_ = ret == Status::OK ? FilterState::RUNNING : FilterState::ERROR;
238     return ret;
239 }
240 
DoPause()241 Status AudioDecoderFilter::DoPause()
242 {
243     MEDIA_LOG_I("AudioDecoderFilter::Pause.");
244     latestPausedTime_ = latestBufferTime_;
245 
246     state_ = FilterState::PAUSED;
247     return Status::OK;
248 }
249 
DoFreeze()250 Status AudioDecoderFilter::DoFreeze()
251 {
252     MEDIA_LOG_E("AudioDecoderFilter::Freeze.");
253     FALSE_RETURN_V_MSG(state_ == FilterState::RUNNING, Status::OK, "current state is %{public}d", state_);
254     latestPausedTime_ = latestBufferTime_;
255     state_ = FilterState::FROZEN;
256     return Status::OK;
257 }
258 
DoPauseAudioAlign()259 Status AudioDecoderFilter::DoPauseAudioAlign()
260 {
261     return DoPause();
262 }
263 
DoResume()264 Status AudioDecoderFilter::DoResume()
265 {
266     MEDIA_LOG_I("AudioDecoderFilter::Resume.");
267 
268     std::unique_lock<std::mutex> lock(bufferStatusMutex_);
269     bufferStatus_ = BUFFER_STATUS_INIT_PROCESS_ALWAYS;
270     refreshTotalPauseTime_ = true;
271     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
272     auto ret = decoder_->Start();
273     state_ = ret == Status::OK ? FilterState::RUNNING : FilterState::ERROR;
274     return ret;
275 }
276 
DoUnFreeze()277 Status AudioDecoderFilter::DoUnFreeze()
278 {
279     MEDIA_LOG_E("AudioDecoderFilter::UnFreeze.");
280     FALSE_RETURN_V_MSG(state_ == FilterState::FROZEN, Status::OK, "current state is %{public}d", state_);
281     refreshTotalPauseTime_ = true;
282     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
283     auto ret = decoder_->Start();
284     state_ = ret == Status::OK ? FilterState::RUNNING : FilterState::ERROR;
285     return ret;
286 }
287 
DoResumeAudioAlign()288 Status AudioDecoderFilter::DoResumeAudioAlign()
289 {
290     return DoResume();
291 }
292 
DoStop()293 Status AudioDecoderFilter::DoStop()
294 {
295     MEDIA_LOG_I("AudioDecoderFilter::Stop.");
296     latestBufferTime_ = HST_TIME_NONE;
297     latestPausedTime_ = HST_TIME_NONE;
298     totalPausedTime_ = 0;
299     refreshTotalPauseTime_ = false;
300     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
301     auto ret = decoder_->Stop();
302     state_ = ret == Status::OK ? FilterState::STOPPED : FilterState::ERROR;
303     return ret;
304 }
305 
DoFlush()306 Status AudioDecoderFilter::DoFlush()
307 {
308     MEDIA_LOG_I("AudioDecoderFilter::Flush.");
309 
310     std::unique_lock<std::mutex> lock(bufferStatusMutex_);
311     bufferStatus_ = BUFFER_STATUS_INIT_PROCESS_ALWAYS;
312     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
313     return decoder_->Flush();
314 }
315 
DoRelease()316 Status AudioDecoderFilter::DoRelease()
317 {
318     std::lock_guard<std::mutex> lock(releaseMutex_);
319     MEDIA_LOG_I_SHORT("AudioDecoderFilter::Release.");
320     if (isReleased_.load()) {
321         MEDIA_LOG_W_SHORT("AudioDecoderFilter has beed released.");
322         return Status::OK;
323     }
324     isReleased_.store(true);
325     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
326     return decoder_->Release();
327 }
328 
SetParameter(const std::shared_ptr<Meta> & parameter)329 void AudioDecoderFilter::SetParameter(const std::shared_ptr<Meta> &parameter)
330 {
331     MEDIA_LOG_I("AudioDecoderFilter::SetParameter");
332     FALSE_RETURN_MSG(decoder_ != nullptr, "decoder_ is nullptr");
333     decoder_->SetParameter(parameter);
334 }
335 
GetParameter(std::shared_ptr<Meta> & parameter)336 void AudioDecoderFilter::GetParameter(std::shared_ptr<Meta> &parameter)
337 {
338     FALSE_RETURN_MSG(decoder_ != nullptr, "decoder_ is nullptr");
339     decoder_->GetOutputFormat(parameter);
340 }
341 
LinkNext(const std::shared_ptr<Filter> & nextFilter,StreamType outType)342 Status AudioDecoderFilter::LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType)
343 {
344     MEDIA_LOG_E("AudioDecoderFilter::LinkNext.");
345     nextFilter_ = nextFilter;
346     nextFiltersMap_[outType].push_back(nextFilter_);
347     std::shared_ptr<FilterLinkCallback> filterLinkCallback =
348         std::make_shared<AudioDecoderFilterLinkCallback>(shared_from_this());
349     return nextFilter->OnLinked(outType, meta_, filterLinkCallback);
350 }
351 
UpdateNext(const std::shared_ptr<Filter> & nextFilter,StreamType outType)352 Status AudioDecoderFilter::UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType)
353 {
354     return Status::OK;
355 }
356 
UnLinkNext(const std::shared_ptr<Filter> & nextFilter,StreamType outType)357 Status AudioDecoderFilter::UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType)
358 {
359     return Status::OK;
360 }
361 
ChangePlugin(std::shared_ptr<Meta> meta)362 Status AudioDecoderFilter::ChangePlugin(std::shared_ptr<Meta> meta)
363 {
364     MEDIA_LOG_I("AudioDecoderFilter::ChangePlugin.");
365     FALSE_RETURN_V_MSG(meta != nullptr, Status::ERROR_NULL_POINTER, "meta is nullptr");
366     std::string mime;
367     bool mimeGetRes = meta->GetData(Tag::MIME_TYPE, mime);
368     if (!mimeGetRes && eventReceiver_ != nullptr) {
369         MEDIA_LOG_I("AudioDecoderFilter cannot get mime");
370         eventReceiver_->OnEvent({"audioDecoder", EventType::EVENT_ERROR, MSERR_UNSUPPORT_AUD_DEC_TYPE});
371         return Status::ERROR_UNSUPPORTED_FORMAT;
372     }
373     UpdateTrackInfoSampleFormat(mime, meta);
374     meta_ = meta;
375     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
376     Status ret = decoder_->ChangePlugin(mime, false, meta);
377     FALSE_RETURN_V_MSG(ret == Status::OK, ret, "ChangePlugin failed");
378 
379     if (IsAsyncMode()) {
380         ret = SetInputBufferQueueConsumerListener();
381         FALSE_RETURN_V_MSG(ret == Status::OK, ret, "ChangePlugin SetInputBufferQueueConsumerListener failed");
382 
383         ret = SetOutputBufferQueueProducerListener();
384         FALSE_RETURN_V_MSG(ret == Status::OK, ret, "ChangePlugin SetOutputBufferQueueProducerListener failed");
385     }
386 
387     return Status::OK;
388 }
389 
GetFilterType()390 FilterType AudioDecoderFilter::GetFilterType()
391 {
392     return filterType_;
393 }
394 
OnLinked(StreamType inType,const std::shared_ptr<Meta> & meta,const std::shared_ptr<FilterLinkCallback> & callback)395 Status AudioDecoderFilter::OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta,
396     const std::shared_ptr<FilterLinkCallback> &callback)
397 {
398     MEDIA_LOG_I("AudioDecoderFilter::OnLinked.");
399     FALSE_RETURN_V_MSG(decoder_ != nullptr, Status::ERROR_NULL_POINTER, "decoder_ is nullptr");
400     onLinkedResultCallback_ = callback;
401     std::string mime;
402     bool mimeGetRes = meta->GetData(Tag::MIME_TYPE, mime);
403     if (!mimeGetRes && eventReceiver_ != nullptr) {
404         MEDIA_LOG_I("AudioDecoderFilter cannot get mime");
405         eventReceiver_->OnEvent({"audioDecoder", EventType::EVENT_ERROR, MSERR_UNSUPPORT_AUD_DEC_TYPE});
406         return Status::ERROR_UNSUPPORTED_FORMAT;
407     }
408     UpdateTrackInfoSampleFormat(mime, meta);
409     meta_ = meta;
410     SetParameter(meta);
411     Status ret = Status::OK;
412     {
413         ScopedTimer timer("AudioDecoder Init", DECODER_INIT_WARNING_MS);
414         ret = decoder_->Init(true, mime);
415     }
416     FALSE_RETURN_V(ret == Status::OK, Status::ERROR_INVALID_PARAMETER);
417 
418     std::shared_ptr<AudioDecoderCallback> mediaCodecCallback
419         = std::make_shared<AudioDecoderCallback>(shared_from_this());
420     decoder_->SetCodecCallback(mediaCodecCallback);
421 
422     ret = decoder_->Configure(meta);
423     if (ret != Status::OK && ret != Status::ERROR_INVALID_STATE) {
424         MEDIA_LOG_I_SHORT("AudioDecoderFilter unsupport format");
425         if (eventReceiver_ != nullptr) {
426             eventReceiver_->OnEvent({"audioDecoder", EventType::EVENT_ERROR, MSERR_UNSUPPORT_AUD_DEC_TYPE});
427         }
428         return Status::ERROR_UNSUPPORTED_FORMAT;
429     }
430     decoder_->SetDumpInfo(isDump_, instanceId_);
431     if (isDrmProtected_) {
432         MEDIA_LOG_D("AudioDecoderFilter::isDrmProtected_ true.");
433 #ifdef SUPPORT_DRM
434         decoder_->SetAudioDecryptionConfig(keySessionServiceProxy_, svpFlag_);
435 #endif
436     }
437     return Status::OK;
438 }
439 
UpdateTrackInfoSampleFormat(const std::string & mime,const std::shared_ptr<Meta> & meta)440 void AudioDecoderFilter::UpdateTrackInfoSampleFormat(const std::string& mime, const std::shared_ptr<Meta> &meta)
441 {
442     MEDIA_LOG_I_SHORT("UpdateTrackInfoSampleFormat mime:" PUBLIC_LOG_S, mime.c_str());
443     FALSE_RETURN_NOLOG(mime != CodecMimeType::AUDIO_RAW);
444     if (mime != CodecMimeType::AUDIO_APE && mime != CodecMimeType::AUDIO_FLAC) {
445         meta->SetData(Tag::AUDIO_SAMPLE_FORMAT, Plugins::SAMPLE_S16LE);
446         return;
447     }
448     int32_t sampleRate = 0;
449     bool sampleRateGetRes = meta->GetData(Tag::AUDIO_SAMPLE_RATE, sampleRate);
450     MEDIA_LOG_I_SHORT("UpdateTrackInfoSampleFormat sampleRate:" PUBLIC_LOG_D32, sampleRate);
451     if (!sampleRateGetRes || sampleRate < SAMPLE_RATE_48K) {
452         meta->SetData(Tag::AUDIO_SAMPLE_FORMAT, Plugins::SAMPLE_S16LE);
453         return;
454     }
455     Plugins::AudioSampleFormat sampleFormat = Plugins::SAMPLE_U8;
456     bool sampleFormatGetRes = meta->GetData(Tag::AUDIO_SAMPLE_FORMAT, sampleFormat);
457     MEDIA_LOG_I_SHORT("sampleFormat before is: " PUBLIC_LOG_D32, sampleFormat);
458     if (sampleFormatGetRes && AudioSampleFormatToBitDepth(sampleFormat) > SAMPLE_FORMAT_BIT_DEPTH_16) {
459         MEDIA_LOG_I_SHORT("sampleFormat after is: " PUBLIC_LOG_D32, Plugins::SAMPLE_S32LE);
460         meta->SetData(Tag::AUDIO_SAMPLE_FORMAT, Plugins::SAMPLE_S32LE);
461         return;
462     }
463 
464     int32_t sampleDepth = 0;
465     bool hasSampleDepthData = meta->GetData(Tag::AUDIO_BITS_PER_CODED_SAMPLE, sampleDepth);
466     if (hasSampleDepthData && sampleDepth > SAMPLE_FORMAT_BIT_DEPTH_16) {
467         meta->SetData(Tag::AUDIO_SAMPLE_FORMAT, Plugins::SAMPLE_S32LE);
468         MEDIA_LOG_I_SHORT("sampleFormat after is: " PUBLIC_LOG_D32, Plugins::SAMPLE_S32LE);
469         return;
470     }
471     bool hasPerRawSampleData = meta->GetData(Tag::AUDIO_BITS_PER_RAW_SAMPLE, sampleDepth);
472     if (hasPerRawSampleData && sampleDepth > SAMPLE_FORMAT_BIT_DEPTH_16) {
473         meta->SetData(Tag::AUDIO_SAMPLE_FORMAT, Plugins::SAMPLE_S32LE);
474         MEDIA_LOG_I_SHORT("sampleFormat after is: " PUBLIC_LOG_D32, Plugins::SAMPLE_S32LE);
475         return;
476     }
477     MEDIA_LOG_I_SHORT("sampleFormat after is: " PUBLIC_LOG_D32, Plugins::SAMPLE_S16LE);
478     meta->SetData(Tag::AUDIO_SAMPLE_FORMAT, Plugins::SAMPLE_S16LE);
479 }
480 
OnUpdated(StreamType inType,const std::shared_ptr<Meta> & meta,const std::shared_ptr<FilterLinkCallback> & callback)481 Status AudioDecoderFilter::OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta,
482     const std::shared_ptr<FilterLinkCallback> &callback)
483 {
484     return Status::OK;
485 }
486 
OnUnLinked(StreamType inType,const std::shared_ptr<FilterLinkCallback> & callback)487 Status AudioDecoderFilter::OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback>& callback)
488 {
489     return Status::OK;
490 }
491 
SetDecryptionConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySessionProxy,bool svp)492 Status AudioDecoderFilter::SetDecryptionConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy,
493     bool svp)
494 {
495     MEDIA_LOG_I("AudioDecoderFilter SetDecryptionConfig enter.");
496     if (keySessionProxy == nullptr) {
497         MEDIA_LOG_E("SetDecryptionConfig keySessionProxy is nullptr.");
498         return Status::ERROR_INVALID_PARAMETER;
499     }
500     isDrmProtected_ = true;
501     (void)svp;
502     // audio svp: false
503     svpFlag_ = false;
504 #ifdef SUPPORT_DRM
505     keySessionServiceProxy_ = keySessionProxy;
506 #endif
507     return Status::OK;
508 }
509 
SetDumpFlag(bool isDump)510 void AudioDecoderFilter::SetDumpFlag(bool isDump)
511 {
512     isDump_ = isDump;
513 }
514 
OnLinkedResult(const sptr<AVBufferQueueProducer> & outputBufferQueue,std::shared_ptr<Meta> & meta)515 void AudioDecoderFilter::OnLinkedResult(const sptr<AVBufferQueueProducer> &outputBufferQueue,
516     std::shared_ptr<Meta> &meta)
517 {
518     MEDIA_LOG_I_SHORT("AudioDecoderFilter::OnLinkedResult");
519     FALSE_RETURN_MSG(decoder_ != nullptr, "decoder_ is nullptr");
520     decoder_->SetOutputBufferQueue(outputBufferQueue);
521     decoder_->Prepare();
522 
523     if (IsAsyncMode()) {
524         Status ret = SetInputBufferQueueConsumerListener();
525         FALSE_RETURN_MSG(ret == Status::OK, "OnLinkedResult SetInputBufferQueueConsumerListener failed");
526 
527         ret = SetOutputBufferQueueProducerListener();
528         FALSE_RETURN_MSG(ret == Status::OK, "OnLinkedResult SetOutputBufferQueueProducerListener failed");
529     }
530 
531     sptr<AVBufferQueueProducer> inputProducer = decoder_->GetInputBufferQueue();
532     FALSE_RETURN(inputProducer != nullptr && onLinkedResultCallback_ != nullptr);
533 
534     onLinkedResultCallback_->OnLinkedResult(inputProducer, meta_);
535 }
536 
OnUpdatedResult(std::shared_ptr<Meta> & meta)537 void AudioDecoderFilter::OnUpdatedResult(std::shared_ptr<Meta> &meta)
538 {
539     meta_ = meta;
540 }
541 
OnUnlinkedResult(std::shared_ptr<Meta> & meta)542 void AudioDecoderFilter::OnUnlinkedResult(std::shared_ptr<Meta> &meta)
543 {
544     meta_ = meta;
545 }
546 
UpdateIsAsyncMode(bool isAsyncMode)547 void AudioDecoderFilter::UpdateIsAsyncMode(bool isAsyncMode)
548 {
549     MEDIA_LOG_I_SHORT("AudioDecoderFilter::UpdateIsAsyncMode, isAsyncMode:" PUBLIC_LOG_D32, isAsyncMode);
550     SetIsAsyncMode(isAsyncMode);
551 }
552 
HandleInputBuffer(bool isTriggeredByOutPort)553 Status AudioDecoderFilter::HandleInputBuffer(bool isTriggeredByOutPort)
554 {
555     ProcessInputBuffer(static_cast<int>(isTriggeredByOutPort ?
556         BufferQueueBufferAVailable::BUFFER_AVAILABLE_OUT_PORT : BufferQueueBufferAVailable::BUFFER_AVAILABLE_IN_PORT));
557     return Status::OK;
558 }
559 
IsNeedProcessInput(bool isOutPort)560 bool AudioDecoderFilter::IsNeedProcessInput(bool isOutPort)
561 {
562     MEDIA_LOG_DD("AudioDecoderFilter::IsNeedProcessInput bufferStatus:" PUBLIC_LOG_U32X ", isOutPort:" PUBLIC_LOG_D32,
563         bufferStatus_, isOutPort);
564     FALSE_RETURN_V_MSG_DD((bufferStatus_ != BUFFER_STATUS_AVAIL_IN), isOutPort, "IN avail, need process outport");
565     FALSE_RETURN_V_MSG_DD((bufferStatus_ != BUFFER_STATUS_AVAIL_OUT), !isOutPort, "OUT avail, need process inport");
566     FALSE_RETURN_V_MSG_DD((bufferStatus_ != BUFFER_STATUS_AVAIL_IN_OUT), true, "IN and OUT avail, need process");
567     FALSE_RETURN_V_MSG_DD(
568         (bufferStatus_ != BUFFER_STATUS_INIT), !isOutPort, "initial stage:" PUBLIC_LOG_D32, isOutPort);
569     FALSE_RETURN_V_MSG_DD((bufferStatus_ != BUFFER_STATUS_OUT_EOS_START), isOutPort, "EOS START, need process outport");
570     FALSE_RETURN_V_MSG_DD((bufferStatus_ != BUFFER_STATUS_AVAIL_OUT_OUT_EOS_START_DONE), false,
571         "OUT avail, EOS START and DONE, no need process");
572     FALSE_RETURN_V_MSG_I((bufferStatus_ != BUFFER_STATUS_AVAIL_OUT_OUT_EOS_START), false,
573         "OUT avail and EOS START, should not happen, no need process");
574     FALSE_RETURN_V_MSG_I((bufferStatus_ != BUFFER_STATUS_OUT_EOS_START_DONE), false,
575         "EOS START and DONE, should not happen, no need process");
576     FALSE_RETURN_V_MSG_D((bufferStatus_ != BUFFER_STATUS_INIT_PROCESS_ALWAYS), true, "state change, need process");
577     if (bufferStatus_ == BUFFER_STATUS_AVAIL_NONE) {
578         bufferStatus_ = isOutPort ? bufferStatus_ : static_cast<uint32_t>(InOutPortBufferStatus::INPORT_AVAIL);
579         MEDIA_LOG_D("neither IN nor OUT avail, need process outport");
580         return isOutPort;
581     }
582 
583     MEDIA_LOG_I("AudioDecoderFilter::IsNeedProcessInput, should not happen, bufferStatus:" PUBLIC_LOG_U32X
584         ", isOutPort:" PUBLIC_LOG_D32, bufferStatus_, isOutPort);
585     return true; // DO ProcessInput by default
586 }
587 
DoProcessInputBuffer(int recvArg,bool dropFrame)588 Status AudioDecoderFilter::DoProcessInputBuffer(int recvArg, bool dropFrame)
589 {
590     bool isOutPort = recvArg == static_cast<int>(BufferQueueBufferAVailable::BUFFER_AVAILABLE_OUT_PORT);
591     uint32_t lastBufferStatus = BUFFER_STATUS_INIT_PROCESS_ALWAYS; // DO ProcessInput by default
592     MEDIA_TRACE_DEBUG_POSTFIX(std::string("AudioDecoderFilter::DoProcessInputBuffer-In:") +
593         std::to_string(isOutPort) + "," + std::to_string(dropFrame) + "," + std::to_string(bufferStatusMutex_), "1");
594     {
595         std::unique_lock<std::mutex> lock(bufferStatusMutex_, std::try_to_lock);
596         if (lock.owns_lock()) {
597             FALSE_RETURN_V_MSG_W(!dropFrame, Status::OK, "task created before flush, ignore obsolete process request");
598             FALSE_RETURN_V_NOLOG(IsNeedProcessInput(isOutPort), Status::OK);
599             lastBufferStatus = bufferStatus_;
600         }
601     }
602     MEDIA_TRACE_DEBUG_POSTFIX(std::string("AudioDecoderFilter::DoProcessInputBuffer-Process:") +
603         std::to_string(isOutPort) + "," + std::to_string(dropFrame) + "," + std::to_string(lastBufferStatus), "2");
604 
605     uint32_t bufferStatus = BUFFER_STATUS_INIT_IGNORE_RET;
606     decoder_->ProcessInputBufferInner(isOutPort, dropFrame, bufferStatus);
607     FALSE_RETURN_V_MSG_W((bufferStatus != BUFFER_STATUS_INIT_IGNORE_RET), Status::OK,
608         "bufferStatus not updated, should not happen");
609 
610     std::unique_lock<std::mutex> lock(bufferStatusMutex_, std::try_to_lock);
611     if (lock.owns_lock()) {
612         // If bufferStatus_ changed, the return bufferStatus is obsolete, should discard
613         if (bufferStatus_ == lastBufferStatus || bufferStatus_ != BUFFER_STATUS_INIT_PROCESS_ALWAYS) {
614             MEDIA_LOG_DD("bufferStatus:" PUBLIC_LOG_U32X ", lastBufferStatus:" PUBLIC_LOG_U32X
615                 ", curBufferStatus:" PUBLIC_LOG_U32X ", isOutPort:" PUBLIC_LOG_D32,
616                 bufferStatus, lastBufferStatus, bufferStatus_, isOutPort);
617             bufferStatus_ = bufferStatus;
618         } else {
619             MEDIA_LOG_W("bufferStatus_ change, ignore returned bufferStatus:" PUBLIC_LOG_U32X
620                 ", lastBufferStatus:" PUBLIC_LOG_U32X ", curBufferStatus:" PUBLIC_LOG_U32X
621                 ", isOutPort:" PUBLIC_LOG_D32, bufferStatus, lastBufferStatus, bufferStatus_, isOutPort);
622         }
623     } else {
624         MEDIA_LOG_W("bufferStatus_ change may occur, ignore returned bufferStatus:" PUBLIC_LOG_U32X
625             ", lastBufferStatus:" PUBLIC_LOG_U32X ", isOutPort:" PUBLIC_LOG_D32,
626         bufferStatus, lastBufferStatus, isOutPort);
627     }
628     return Status::OK;
629 }
630 
SetInputBufferQueueConsumerListener()631 Status AudioDecoderFilter::SetInputBufferQueueConsumerListener()
632 {
633     sptr<IConsumerListener> consumerListener =
634         OHOS::sptr<AudioDecInputPortConsumerListener>::MakeSptr(shared_from_this());
635     FALSE_RETURN_V_MSG(consumerListener != nullptr, Status::ERROR_NULL_POINTER, "consumerListener is nullptr");
636     sptr<Media::AVBufferQueueConsumer> inputConsumer = decoder_->GetInputBufferQueueConsumer();
637     FALSE_RETURN_V_MSG(inputConsumer != nullptr, Status::ERROR_NULL_POINTER, "inputConsumer is nullptr");
638     return inputConsumer->SetBufferAvailableListener(consumerListener);
639 }
640 
SetOutputBufferQueueProducerListener()641 Status AudioDecoderFilter::SetOutputBufferQueueProducerListener()
642 {
643     sptr<IProducerListener> producerListener =
644         OHOS::sptr<AudioDecOutPortProducerListener>::MakeSptr(shared_from_this());
645     FALSE_RETURN_V_MSG(producerListener != nullptr, Status::ERROR_NULL_POINTER, "producerListener is nullptr");
646     sptr<Media::AVBufferQueueProducer> outputProducer = decoder_->GetOutputBufferQueueProducer();
647     FALSE_RETURN_V_MSG(outputProducer != nullptr, Status::ERROR_NULL_POINTER, "outputProducer is nullptr");
648     return outputProducer->SetBufferAvailableListener(producerListener);
649 }
650 
OnInterrupted(bool isInterruptNeeded)651 void AudioDecoderFilter::OnInterrupted(bool isInterruptNeeded)
652 {
653     FALSE_RETURN_MSG(decoder_ != nullptr, "audioDecoder is nullptr");
654     if (isInterruptNeeded) {
655         decoder_->Flush();
656     }
657 }
658 
OnDumpInfo(int32_t fd)659 void AudioDecoderFilter::OnDumpInfo(int32_t fd)
660 {
661     MEDIA_LOG_D("AudioDecoderFilter::OnDumpInfo called.");
662     if (decoder_ != nullptr) {
663         decoder_->OnDumpInfo(fd);
664     }
665 }
666 
SetCallerInfo(uint64_t instanceId,const std::string & appName)667 void AudioDecoderFilter::SetCallerInfo(uint64_t instanceId, const std::string& appName)
668 {
669     instanceId_ = instanceId;
670     appName_ = appName;
671 }
672 
OnError(CodecErrorType errorType,int32_t errorCode)673 void AudioDecoderFilter::OnError(CodecErrorType errorType, int32_t errorCode)
674 {
675     MEDIA_LOG_E("Audio Decoder error happened. ErrorType: %{public}d, errorCode: %{public}d",
676         static_cast<int32_t>(errorType), errorCode);
677     if (eventReceiver_ == nullptr) {
678         MEDIA_LOG_E("Audio Decoder OnError failed due to eventReceiver is nullptr.");
679         return;
680     }
681     switch (errorType) {
682         case CodecErrorType::CODEC_DRM_DECRYTION_FAILED:
683             eventReceiver_->OnEvent({"audioDecoder", EventType::EVENT_ERROR, MSERR_DRM_VERIFICATION_FAILED});
684             break;
685         default:
686             break;
687     }
688 }
689 
OnOutputFormatChanged(const Format & format)690 void AudioDecoderFilter::OnOutputFormatChanged(const Format& format)
691 {
692     FALSE_RETURN_NOLOG(meta_ && nextFilter_);
693     std::string mime;
694     FALSE_RETURN_MSG(meta_->GetData(Tag::MIME_TYPE, mime) && mime != CodecMimeType::AUDIO_VIVID,
695         "Audio mimeType %{public}s unsupport format change", mime.c_str());
696     int32_t sampleRate = 0;
697     int32_t channels = 0;
698     int32_t sampleFormat = 0;
699     FALSE_RETURN(format.GetIntValue(Tag::AUDIO_SAMPLE_RATE, sampleRate));
700     FALSE_RETURN(format.GetIntValue(Tag::AUDIO_CHANNEL_COUNT, channels));
701     FALSE_RETURN(format.GetIntValue(Tag::AUDIO_SAMPLE_FORMAT, sampleFormat));
702     std::string formatChangeInfo = "AudioFormatChange sampleRate " + std::to_string(sampleRate) + " channels "
703                                     + std::to_string(channels) + " smpFmt " + std::to_string(sampleFormat);
704     MediaAVCodec::AVCodecTrace trace(formatChangeInfo);
705     MEDIA_LOG_I("%{public}s", formatChangeInfo.c_str());
706     int32_t preSampleRate = 0;
707     int32_t preChannels = 0;
708     int32_t preSampleFormat = 0;
709     meta_->GetData(Tag::AUDIO_SAMPLE_RATE, preSampleRate);
710     meta_->GetData(Tag::AUDIO_OUTPUT_CHANNELS, preChannels);
711     meta_->GetData(Tag::AUDIO_SAMPLE_FORMAT, preSampleFormat);
712     FALSE_RETURN_NOLOG(preSampleRate != sampleRate || preChannels != channels || preSampleFormat != sampleFormat);
713 
714     meta_->SetData(Tag::AUDIO_SAMPLE_RATE, sampleRate);
715     meta_->SetData(Tag::AUDIO_OUTPUT_CHANNELS, channels);
716     meta_->SetData(Tag::AUDIO_SAMPLE_FORMAT, sampleFormat);
717     nextFilter_->HandleFormatChange(meta_);
718 }
719 
AudioDecoderCallback(std::shared_ptr<AudioDecoderFilter> audioDecoderFilter)720 AudioDecoderCallback::AudioDecoderCallback(std::shared_ptr<AudioDecoderFilter> audioDecoderFilter)
721     : audioDecoderFilter_(audioDecoderFilter)
722 {
723     MEDIA_LOG_I("AudioDecoderCallback");
724 }
725 
~AudioDecoderCallback()726 AudioDecoderCallback::~AudioDecoderCallback()
727 {
728     MEDIA_LOG_I("~AudioDecoderCallback");
729 }
730 
OnError(MediaAVCodec::AVCodecErrorType errorType,int32_t errorCode)731 void AudioDecoderCallback::OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode)
732 {
733     if (auto codecFilter = audioDecoderFilter_.lock()) {
734         switch (errorType) {
735             case AVCodecErrorType::AVCODEC_ERROR_DECRYTION_FAILED:
736                 codecFilter->OnError(CodecErrorType::CODEC_DRM_DECRYTION_FAILED, errorCode);
737                 break;
738             default:
739                 codecFilter->OnError(CodecErrorType::CODEC_ERROR_INTERNAL, errorCode);
740                 break;
741         }
742     } else {
743         MEDIA_LOG_I("OnError failed due to the codecFilter is invalid");
744     }
745 }
746 
OnOutputFormatChanged(const Format & format)747 void AudioDecoderCallback::OnOutputFormatChanged(const Format &format)
748 {
749     auto codecFilter = audioDecoderFilter_.lock();
750     FALSE_RETURN_MSG(codecFilter != nullptr, "AudioDecoderFilter is nullptr");
751     codecFilter->OnOutputFormatChanged(format);
752 }
753 
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)754 void AudioDecoderCallback::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
755 {
756     (void)index;
757     (void)buffer;
758 }
759 
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)760 void AudioDecoderCallback::OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
761 {
762     (void)index;
763     (void)buffer;
764 }
765 } // namespace Pipeline
766 } // namespace MEDIA
767 } // namespace OHOS
768