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 AVCODEC_AUDIO_AVBUFFER_ENCODER_RESET_DEMO_H 17 #define AVCODEC_AUDIO_AVBUFFER_ENCODER_RESET_DEMO_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <fstream> 22 #include <queue> 23 #include <string> 24 #include <thread> 25 #include "nocopyable.h" 26 #include "common/native_mfmagic.h" 27 #include "native_avcodec_audiocodec.h" 28 29 namespace OHOS { 30 namespace MediaAVCodec { 31 namespace AudioAacEncDemo { 32 33 enum class AudioBufferFormatType : int32_t { 34 TYPE_AAC = 0, 35 TYPE_FLAC = 1, 36 TYPE_MP3 = 2, 37 TYPE_VORBIS = 3, 38 TYPE_AMRNB = 4, 39 TYPE_AMRWB = 5, 40 TYPE_VIVID = 6, 41 TYPE_OPUS = 7, 42 TYPE_G711MU = 8, 43 TYPE_LBVC = 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_AVBuffer *> inBufferQueue_; 57 std::queue<OH_AVBuffer *> outBufferQueue_; 58 }; 59 60 class AudioBufferAacEncDemo : public NoCopyable { 61 public: 62 AudioBufferAacEncDemo(); 63 virtual ~AudioBufferAacEncDemo(); 64 65 bool RunCaseReset(const uint8_t *data, size_t size); 66 67 OH_AVCodec* CreateByMime(const char* mime); 68 69 OH_AVCodec* CreateByName(const char* name); 70 71 OH_AVErrCode Destroy(OH_AVCodec* codec); 72 73 OH_AVErrCode SetCallback(OH_AVCodec* codec); 74 75 OH_AVErrCode Prepare(OH_AVCodec* codec); 76 77 OH_AVErrCode Start(OH_AVCodec* codec); 78 79 OH_AVErrCode Stop(OH_AVCodec* codec); 80 81 OH_AVErrCode Flush(OH_AVCodec* codec); 82 83 OH_AVErrCode Reset(OH_AVCodec* codec); 84 85 OH_AVFormat* GetOutputDescription(OH_AVCodec* codec); 86 87 OH_AVErrCode PushInputData(OH_AVCodec* codec, uint32_t index); 88 89 OH_AVErrCode PushInputDataEOS(OH_AVCodec* codec, uint32_t index); 90 91 OH_AVErrCode FreeOutputData(OH_AVCodec* codec, uint32_t index); 92 93 OH_AVErrCode IsValid(OH_AVCodec* codec, bool* isValid); 94 95 uint32_t GetInputIndex(); 96 97 uint32_t GetOutputIndex(); 98 99 bool InitFile(const std::string& inputFile); 100 private: 101 void ClearQueue(); 102 int32_t CreateEnc(); 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 Setformat(OH_AVFormat *format); 112 void HandleEOS(const uint32_t &index); 113 int32_t GetFileSize(const std::string &filePath); 114 115 std::atomic<bool> isRunning_; 116 std::ifstream inputFile_; 117 std::ofstream outputFile_; 118 std::unique_ptr<std::thread> inputLoop_; 119 std::unique_ptr<std::thread> outputLoop_; 120 OH_AVCodec *audioEnc_; 121 AEncSignal *signal_; 122 struct OH_AVCodecCallback cb_; 123 bool isFirstFrame_ = true; 124 int64_t timeStamp_ = 0; 125 uint32_t frameCount_ = 0; 126 size_t inputdatasize = 0; 127 std::string inputdata; 128 AudioBufferFormatType audioType_ = AudioBufferFormatType::TYPE_AAC; 129 }; 130 } // namespace AudioAacEncDemo 131 } // namespace MediaAVCodec 132 } // namespace OHOS 133 #endif // AVCODEC_AUDIO_AVBUFFER_ENCODER_RESET_DEMO_H 134