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 AVCODEC_AUDIO_DECODER_INNER_DEMO_H 17 #define AVCODEC_AUDIO_DECODER_INNER_DEMO_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <thread> 22 #include <queue> 23 #include <string> 24 #include "avcodec_common.h" 25 #include "avcodec_audio_decoder.h" 26 #include "nocopyable.h" 27 28 namespace OHOS { 29 namespace MediaAVCodec { 30 namespace InnerAudioDemo { 31 class ADecSignal { 32 public: 33 std::mutex inMutex_; 34 std::mutex outMutex_; 35 std::mutex startMutex_; 36 std::condition_variable inCond_; 37 std::condition_variable outCond_; 38 std::condition_variable startCond_; 39 std::queue<uint32_t> inQueue_; 40 std::queue<uint32_t> outQueue_; 41 std::queue<std::shared_ptr<AVSharedMemory>> inBufferQueue_; 42 std::queue<std::shared_ptr<AVSharedMemory>> outBufferQueue_; 43 std::queue<AVCodecBufferInfo> infoQueue_; 44 std::queue<AVCodecBufferFlag> flagQueue_; 45 }; 46 47 class ADecDemoCallback : public AVCodecCallback, public NoCopyable { 48 public: 49 explicit ADecDemoCallback(std::shared_ptr<ADecSignal> signal); 50 virtual ~ADecDemoCallback() = default; 51 52 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 53 void OnOutputFormatChanged(const Format &format) override; 54 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override; 55 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 56 std::shared_ptr<AVSharedMemory> buffer) override; 57 58 private: 59 std::shared_ptr<ADecSignal> signal_; 60 }; 61 62 class ADecInnerDemo : public NoCopyable { 63 public: 64 ADecInnerDemo() = default; 65 virtual ~ADecInnerDemo() = default; 66 void RunCase(); 67 68 private: 69 int32_t CreateDec(); 70 int32_t Configure(const Format &format); 71 int32_t Start(); 72 int32_t Stop(); 73 int32_t Flush(); 74 int32_t Reset(); 75 int32_t Release(); 76 void InputFunc(); 77 void OutputFunc(); 78 void HandleInputEOS(const uint32_t &index); 79 int32_t HandleNormalInput(const uint32_t &index, const int64_t &pts, const size_t &size); 80 81 std::atomic<bool> isRunning_ = false; 82 std::unique_ptr<std::ifstream> testFile_; 83 std::unique_ptr<std::thread> inputLoop_; 84 std::unique_ptr<std::thread> outputLoop_; 85 std::shared_ptr<AVCodecAudioDecoder> audioDec_; 86 std::shared_ptr<ADecSignal> signal_; 87 std::shared_ptr<ADecDemoCallback> cb_; 88 uint32_t frameCount_ = 0; 89 std::ifstream inputFile_; 90 }; 91 } // namespace InnerAudioDemo 92 } // namespace MediaAVCodec 93 } // namespace OHOS 94 #endif // AVCODEC_AUDIO_DECODER_INNER_DEMO_H 95