1 /* 2 * Copyright (C) 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 16 #ifndef AVCODEC_AUDIO_AVBUFFER_DECODER_DEMO_H 17 #define AVCODEC_AUDIO_AVBUFFER_DECODER_DEMO_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <queue> 22 #include <string> 23 #include <thread> 24 25 #include "native_avcodec_audiocodec.h" 26 #include "nocopyable.h" 27 #include "common/native_mfmagic.h" 28 #include "native_avdemuxer.h" 29 #include "avcodec_audio_common.h" 30 31 namespace OHOS { 32 namespace MediaAVCodec { 33 namespace AudioBufferDemo { 34 enum class AudioBufferFormatType : int32_t { 35 TYPE_AAC = 0, 36 TYPE_FLAC = 1, 37 TYPE_MP3 = 2, 38 TYPE_VORBIS = 3, 39 TYPE_AMRNB = 4, 40 TYPE_AMRWB = 5, 41 TYPE_VIVID = 6, 42 TYPE_OPUS = 7, 43 TYPE_G711MU = 8, 44 TYPE_G711A = 9, 45 TYPE_APE = 10, 46 TYPE_LBVC = 11, 47 TYPE_MAX = 20, 48 }; 49 50 class ADecBufferSignal { 51 public: 52 std::mutex inMutex_; 53 std::mutex outMutex_; 54 std::mutex startMutex_; 55 std::condition_variable inCond_; 56 std::condition_variable outCond_; 57 std::condition_variable startCond_; 58 std::queue<uint32_t> inQueue_; 59 std::queue<uint32_t> outQueue_; 60 std::queue<OH_AVBuffer *> inBufferQueue_; 61 std::queue<OH_AVBuffer *> outBufferQueue_; 62 }; 63 64 class ADecBufferDemo : public NoCopyable { 65 public: 66 ADecBufferDemo(); 67 virtual ~ADecBufferDemo(); 68 /** 69 * @functionTest 70 * @input inputFile 71 * @output outputFile 72 **/ 73 bool InitFile(const std::string& inputFile); 74 bool RunCase(const uint8_t *data, size_t size); 75 OH_AVCodec* CreateByMime(const char* mime); 76 OH_AVCodec* CreateByName(const char* name); 77 OH_AVErrCode Destroy(OH_AVCodec* codec); 78 OH_AVErrCode SetCallback(OH_AVCodec* codec); 79 OH_AVErrCode Configure(OH_AVCodec* codec, OH_AVFormat* format, int32_t channel, int32_t sampleRate); 80 OH_AVErrCode Prepare(OH_AVCodec* codec); 81 OH_AVErrCode Start(OH_AVCodec* codec); 82 OH_AVErrCode Stop(OH_AVCodec* codec); 83 OH_AVErrCode Flush(OH_AVCodec* codec); 84 OH_AVErrCode Reset(OH_AVCodec* codec); 85 OH_AVFormat* GetOutputDescription(OH_AVCodec* codec); 86 OH_AVErrCode PushInputData(OH_AVCodec* codec, uint32_t index); 87 OH_AVErrCode FreeOutputData(OH_AVCodec* codec, uint32_t index); 88 OH_AVErrCode IsValid(OH_AVCodec* codec, bool* isValid); 89 uint32_t GetInputIndex(); 90 OH_AVErrCode PushInputDataEOS(OH_AVCodec* codec, uint32_t index); 91 uint32_t GetOutputIndex(); 92 OH_AVErrCode SetParameter(OH_AVCodec* codec, OH_AVFormat* format, int32_t channel, int32_t sampleRate); 93 94 private: 95 void Running(); 96 int32_t CreateDec(); 97 int32_t InitFormat(); 98 int32_t Configure(OH_AVFormat *format); 99 int32_t Start(); 100 int32_t Stop(); 101 int32_t Flush(); 102 int32_t Reset(); 103 int32_t Release(); 104 void InputFunc(); 105 void OutputFunc(); 106 void HandleInputEOS(const uint32_t index); 107 bool InitFormat(OH_AVFormat *format); 108 bool ConfigVorbisExtraData(OH_AVFormat *format); 109 std::atomic<bool> isRunning_ = false; 110 std::unique_ptr<std::thread> inputLoop_; 111 std::unique_ptr<std::thread> outputLoop_; 112 OH_AVCodec *audioDec_; 113 ADecBufferSignal *signal_; 114 struct OH_AVCodecCallback cb_; 115 bool isFirstFrame_ = true; 116 uint32_t frameCount_ = 0; 117 AudioBufferFormatType audioType_; 118 size_t inputdatasize = 0; 119 size_t inputOffset = 0; 120 std::string inputdata; 121 bool eosFlag = false; 122 }; 123 } // namespace AudioBufferDemo 124 } // namespace MediaAVCodec 125 } // namespace OHOS 126 #endif // AVCODEC_AUDIO_AVBUFFER_DECODER_DEMO_H 127