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 16 #ifndef AV_CODEC_ADIO_CODEC_WORKER_H 17 #define AV_CODEC_ADIO_CODEC_WORKER_H 18 19 #include "audio_buffers_manager.h" 20 #include "audio_base_codec.h" 21 #include "avcodec_common.h" 22 #include "avcodec_errors.h" 23 #include "nocopyable.h" 24 #include "task_thread.h" 25 26 #include <condition_variable> 27 #include <mutex> 28 #include <queue> 29 30 namespace OHOS { 31 namespace MediaAVCodec { 32 class AudioCodecWorker : public NoCopyable { 33 public: 34 AudioCodecWorker(const std::shared_ptr<AudioBaseCodec> &codec, const std::shared_ptr<AVCodecCallback> &callback); 35 36 ~AudioCodecWorker(); 37 38 bool PushInputData(const uint32_t &index); 39 40 bool Configure(); 41 42 bool Start(); 43 44 bool Stop(); 45 46 bool Pause(); 47 48 bool Resume(); 49 50 bool Release(); 51 52 std::shared_ptr<AudioBuffersManager> GetInputBuffer() const noexcept; 53 54 std::shared_ptr<AudioBuffersManager> GetOutputBuffer() const noexcept; 55 56 std::shared_ptr<AudioBufferInfo> GetOutputBufferInfo(const uint32_t &index) const noexcept; 57 58 std::shared_ptr<AudioBufferInfo> GetInputBufferInfo(const uint32_t &index) const noexcept; 59 60 private: 61 void ProduceInputBuffer(); 62 void ConsumerOutputBuffer(); 63 void Dispose(); 64 bool Begin(); 65 bool HandInputBuffer(int32_t &ret); 66 void ReleaseOutputBuffer(const uint32_t &index, const int32_t &ret); 67 void SetFirstAndEosStatus(std::shared_ptr<AudioBufferInfo> &outBuffer, bool isEos, uint32_t index); 68 void ReleaseAllInBufferQueue(); 69 void ReleaseAllInBufferAvaQueue(); 70 void ResetTask(); 71 72 private: 73 bool isFirFrame_; 74 std::atomic<bool> isRunning; 75 std::shared_ptr<AudioBaseCodec> codec_; 76 int32_t inputBufferSize; 77 int32_t outputBufferSize; 78 const int16_t bufferCount; 79 const std::string_view name_; 80 std::mutex stateMutex_; 81 std::mutex inAvaMutex_; 82 std::mutex inputMutex_; 83 std::mutex outputMutex_; 84 std::condition_variable inputCondition_; 85 std::condition_variable outputCondition_; 86 87 std::unique_ptr<TaskThread> inputTask_; 88 std::unique_ptr<TaskThread> outputTask_; 89 std::shared_ptr<AVCodecCallback> callback_; 90 std::shared_ptr<AudioBuffersManager> inputBuffer_; 91 std::shared_ptr<AudioBuffersManager> outputBuffer_; 92 std::queue<uint32_t> inBufIndexQue_; 93 std::queue<uint32_t> inBufAvaIndexQue_; 94 }; 95 } // namespace MediaAVCodec 96 } // namespace OHOS 97 98 #endif