/* * Copyright (C) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CODEC_AUDIO_CODEC_H #define CODEC_AUDIO_CODEC_H #include "audio_base_codec.h" #include "media_codec.h" #include "avcodec_common.h" #include "codecbase.h" #include "nocopyable.h" namespace OHOS { namespace MediaAVCodec { class AudioCodec; class AudioCodecCallback : public Media::AudioBaseCodecCallback { public: AudioCodecCallback(const std::shared_ptr &codec) : codec_(codec) {} virtual ~AudioCodecCallback() { codec_ = nullptr; } void OnError(Media::CodecErrorType errorType, int32_t errorCode) override; void OnOutputBufferDone(const std::shared_ptr &outputBuffer) override; private: std::shared_ptr codec_; }; class AudioCodec : public std::enable_shared_from_this, public CodecBase { public: explicit AudioCodec() { mediaCodec_ = std::make_shared(); } int32_t CreateCodecByName(const std::string &name) override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Init(name))); } int32_t Configure(const std::shared_ptr &meta) override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Configure(meta))); } int32_t SetOutputBufferQueue(const sptr &bufferQueueProducer) override { return StatusToAVCodecServiceErrCode( static_cast(mediaCodec_->SetOutputBufferQueue(bufferQueueProducer))); } int32_t SetCodecCallback(const std::shared_ptr &codecCallback) { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->SetCodecCallback(codecCallback))); } int32_t SetCallback(const std::shared_ptr &codecCallback) override { callback_ = codecCallback; mediaCallback_ = std::make_shared(shared_from_this()); return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->SetCodecCallback(mediaCallback_))); } int32_t Prepare() override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Prepare())); } sptr GetInputBufferQueue() override { return mediaCodec_->GetInputBufferQueue(); } int32_t Start() override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Start())); } int32_t Stop() override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Stop())); } int32_t Flush() override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Flush())); } int32_t Reset() override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Reset())); } int32_t Release() override { int32_t ret = StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->Release())); mediaCallback_ = nullptr; return ret; } int32_t NotifyEos() override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->NotifyEos())); } int32_t SetParameter(const std::shared_ptr ¶meter) override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->SetParameter(parameter))); } int32_t GetOutputFormat(std::shared_ptr ¶meter) override { return StatusToAVCodecServiceErrCode(static_cast(mediaCodec_->GetOutputFormat(parameter))); } int32_t Configure(const MediaAVCodec::Format &format) override { (void)format; return 0; } int32_t SetParameter(const MediaAVCodec::Format &format) override { (void)format; return 0; } int32_t GetOutputFormat(MediaAVCodec::Format &format) override { (void)format; return 0; } int32_t ReleaseOutputBuffer(uint32_t index) override { (void)index; return 0; } void OnError(CodecErrorType errorType, int32_t errorCode) { auto realPtr = callback_.lock(); if (realPtr == nullptr) { return; } switch (errorType) { case CodecErrorType::CODEC_DRM_DECRYTION_FAILED: realPtr->OnError(AVCodecErrorType::AVCODEC_ERROR_DECRYTION_FAILED, StatusToAVCodecServiceErrCode(static_cast(errorCode))); break; default: realPtr->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, StatusToAVCodecServiceErrCode(static_cast(errorCode))); break; } } void OnOutputBufferDone(const std::shared_ptr &outputBuffer) { auto realPtr = callback_.lock(); if (realPtr != nullptr) { realPtr->OnOutputBufferAvailable(0, outputBuffer); } } #ifdef SUPPORT_DRM int32_t SetAudioDecryptionConfig(const sptr &keySession, const bool svpFlag) override { return StatusToAVCodecServiceErrCode( static_cast(mediaCodec_->SetAudioDecryptionConfig(keySession, svpFlag))); } #endif private: std::shared_ptr mediaCodec_; // other callback from north interface(codec server) std::weak_ptr callback_; // self callback register in mediaCodec_ std::shared_ptr mediaCallback_ = nullptr; }; void AudioCodecCallback::OnError(Media::CodecErrorType errorType, int32_t errorCode) { if (codec_) { codec_->OnError(errorType, errorCode); } } void AudioCodecCallback::OnOutputBufferDone(const std::shared_ptr &outputBuffer) { if (codec_) { codec_->OnOutputBufferDone(outputBuffer); } } } // namespace MediaAVCodec } // namespace OHOS #endif