1 /* 2 * Copyright (C) 2024 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 AUDIO_ENCODER_DEMO_BASE_H 17 #define AUDIO_ENCODER_DEMO_BASE_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <queue> 22 #include <string> 23 #include <thread> 24 25 #include "native_avcodec_audioencoder.h" 26 #include "nocopyable.h" 27 #include "avcodec_audio_common.h" 28 29 namespace OHOS { 30 namespace MediaAVCodec { 31 namespace AudioEncDemoAuto { 32 extern void OnError(OH_AVCodec* codec, int32_t errorCode, void* userData); 33 extern void OnOutputFormatChanged(OH_AVCodec* codec, OH_AVFormat* format, void* userData); 34 extern void OnInputBufferAvailable(OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, void* userData); 35 extern void OnOutputBufferAvailable(OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, 36 OH_AVCodecBufferAttr* attr, void* userData); 37 38 enum AudioFormatType : int32_t { 39 TYPE_OPUS = 0, 40 TYPE_G711MU = 1, 41 TYPE_AAC = 2, 42 TYPE_FLAC = 3, 43 TYPE_MAX = 10, 44 }; 45 46 class AEncSignal { 47 public: 48 std::mutex inMutex_; 49 std::mutex outMutex_; 50 std::mutex startMutex_; 51 std::condition_variable inCond_; 52 std::condition_variable outCond_; 53 std::condition_variable startCond_; 54 std::queue<uint32_t> inQueue_; 55 std::queue<uint32_t> outQueue_; 56 std::queue<OH_AVMemory*> inBufferQueue_; 57 std::queue<OH_AVMemory*> outBufferQueue_; 58 std::queue<OH_AVCodecBufferAttr> attrQueue_; 59 }; 60 61 class AEncDemoAuto : public NoCopyable { 62 public: 63 AEncDemoAuto(); 64 virtual ~AEncDemoAuto(); 65 66 bool RunCase(const uint8_t *data, size_t size); 67 68 OH_AVCodec* CreateByMime(const char* mime); 69 70 OH_AVCodec* CreateByName(const char* mime); 71 72 OH_AVErrCode Destroy(OH_AVCodec* codec); 73 74 OH_AVErrCode SetCallback(OH_AVCodec* codec); 75 76 OH_AVErrCode Prepare(OH_AVCodec* codec); 77 78 OH_AVErrCode Start(OH_AVCodec* codec); 79 80 OH_AVErrCode Stop(OH_AVCodec* codec); 81 82 OH_AVErrCode Flush(OH_AVCodec* codec); 83 84 OH_AVErrCode Reset(OH_AVCodec* codec); 85 86 OH_AVErrCode PushInputData(OH_AVCodec* codec, uint32_t index, int32_t size, int32_t offset); 87 88 OH_AVErrCode PushInputDataEOS(OH_AVCodec* codec, uint32_t index); 89 90 OH_AVErrCode FreeOutputData(OH_AVCodec* codec, uint32_t index); 91 92 OH_AVErrCode IsValid(OH_AVCodec* codec, bool* isValid); 93 94 uint32_t GetInputIndex(); 95 96 uint32_t GetOutputIndex(); 97 98 void HandleEOS(const uint32_t& index); 99 bool InitFile(std::string inputFile); 100 private: 101 void ClearQueue(); 102 int32_t CreateEnd(); 103 int32_t Configure(OH_AVFormat* format); 104 int32_t Start(); 105 int32_t Stop(); 106 int32_t Flush(); 107 int32_t Reset(); 108 int32_t Release(); 109 void InputFunc(); 110 void OutputFunc(); 111 void HandleInputEOS(const uint32_t index); 112 void Setformat(OH_AVFormat *format); 113 int32_t HandleNormalInput(const uint32_t& index, const int64_t pts, const size_t size); 114 115 std::atomic<bool> isRunning_ = false; 116 std::unique_ptr<std::ifstream> testFile_; 117 std::unique_ptr<std::thread> inputLoop_; 118 std::unique_ptr<std::thread> outputLoop_; 119 OH_AVCodec* audioEnc_; 120 AEncSignal* signal_; 121 struct OH_AVCodecAsyncCallback cb_; 122 bool isFirstFrame_ = true; 123 uint32_t frameCount_ = 0; 124 size_t inputdatasize; 125 std::string inputdata; 126 AudioFormatType audioType_; 127 OH_AVFormat* format_; 128 }; 129 } // namespace AudioEncDemoAuto 130 } // namespace MediaAVCodec 131 } // namespace OHOS 132 #endif // AUDIO_DECODER_DEMO_BASE_H 133