1 /* 2 * Copyright (C) 2023 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 #ifndef AVCODEC_AUDIO_CODEC_INNER_IMPL_H 16 #define AVCODEC_AUDIO_CODEC_INNER_IMPL_H 17 18 #include "avcodec_audio_codec.h" 19 #include "nocopyable.h" 20 #include "i_avcodec_service.h" 21 #include "drm_i_keysession_service.h" 22 23 namespace OHOS { 24 namespace MediaAVCodec { 25 class AVCodecAudioCodecInnerImpl : public AVCodecAudioCodec, public NoCopyable { 26 public: 27 AVCodecAudioCodecInnerImpl(); 28 ~AVCodecAudioCodecInnerImpl(); 29 30 int32_t Init(AVCodecType type, bool isMimeType, const std::string &name); 31 32 int32_t Configure(const std::shared_ptr<Media::Meta> &meta) override; 33 34 int32_t SetOutputBufferQueue(const sptr<Media::AVBufferQueueProducer> &bufferQueueProducer) override; 35 36 int32_t Prepare() override; 37 38 sptr<Media::AVBufferQueueProducer> GetInputBufferQueue() override; 39 40 sptr<Media::AVBufferQueueConsumer> GetInputBufferQueueConsumer() override; 41 42 sptr<Media::AVBufferQueueProducer> GetOutputBufferQueueProducer() override; 43 44 void ProcessInputBufferInner(bool isTriggeredByOutPort, bool isFlushed, uint32_t &bufferStatus) override; 45 46 int32_t Start() override; 47 48 int32_t Stop() override; 49 50 int32_t Flush() override; 51 52 int32_t Reset() override; 53 54 int32_t Release() override; 55 56 int32_t NotifyEos() override; 57 58 int32_t SetParameter(const std::shared_ptr<Media::Meta> ¶meter) override; 59 60 int32_t GetOutputFormat(std::shared_ptr<Media::Meta> ¶meter) override; 61 62 int32_t ChangePlugin(const std::string &mime, bool isEncoder, const std::shared_ptr<Media::Meta> &meta) override; 63 64 int32_t SetCodecCallback(const std::shared_ptr<MediaCodecCallback> &codecCallback) override; 65 66 int32_t SetAudioDecryptionConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession, 67 const bool svpFlag) override; 68 69 void ProcessInputBuffer() override; 70 71 void SetDumpInfo(bool isDump, uint64_t instanceId) override; 72 73 int32_t QueryInputBuffer(uint32_t *index, int32_t bufferSize, int64_t timeoutUs) override; 74 75 std::shared_ptr<AVBuffer> GetInputBuffer(uint32_t index) override; 76 77 std::shared_ptr<AVBuffer> GetOutputBuffer(int64_t timeoutUs) override; 78 79 int32_t PushInputBuffer(uint32_t index, bool available) override; 80 81 int32_t ReleaseOutputBuffer(const std::shared_ptr<AVBuffer> &buffer) override; 82 83 private: 84 class SyncCodecAdapter : public Media::IConsumerListener, public std::enable_shared_from_this<SyncCodecAdapter> { 85 public: 86 explicit SyncCodecAdapter(size_t outputBufferNum); 87 ~SyncCodecAdapter(); 88 89 int32_t Prepare(const sptr<Media::AVBufferQueueProducer> &bufferQueueProducer); 90 91 int32_t QueryInputBuffer(uint32_t *index, int32_t bufferSize, int64_t timeoutUs); 92 93 std::shared_ptr<AVBuffer> GetInputBuffer(uint32_t index); 94 95 std::shared_ptr<AVBuffer> GetOutputBuffer(int64_t timeoutUs); 96 97 int32_t PushInputBuffer(uint32_t index, bool available); 98 99 int32_t ReleaseOutputBuffer(const std::shared_ptr<AVBuffer> &buffer); 100 101 sptr<Media::AVBufferQueueProducer> GetProducer(); 102 103 private: 104 void OnBufferAvailable(); 105 bool WaitFor(std::unique_lock<std::mutex> &lock, int64_t timeoutUs); 106 107 bool init_; 108 uint32_t inputIndex_; 109 std::vector<std::shared_ptr<AVBuffer>> inputBuffers_; 110 std::unordered_map<AVBuffer *, std::shared_ptr<AVBuffer>> outputBuffers_; 111 Media::AVBufferConfig avBufferConfig_; 112 std::shared_ptr<Media::AVBufferQueue> innerBufferQueue_; 113 sptr<Media::AVBufferQueueProducer> bufferQueueProducer_; 114 sptr<Media::AVBufferQueueConsumer> bufferQueueConsumer_; 115 std::mutex outputMutex_; 116 std::condition_variable outputCV_; 117 size_t outputAvaliableNum_; 118 }; 119 120 std::shared_ptr<ICodecService> codecService_ = nullptr; 121 std::shared_ptr<SyncCodecAdapter> syncCodecAdapter_ = nullptr; 122 }; 123 } // namespace MediaAVCodec 124 } // namespace OHOS 125 #endif // AVCODEC_AUDIO_CODEC_INNER_IMPL_H