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_FLUSH_DEMO_BASE_H 17#define AUDIO_ENCODER_FLUSH_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 29namespace OHOS { 30namespace MediaAVCodec { 31namespace AudioEncDemoAuto { 32extern void OnError(OH_AVCodec* codec, int32_t errorCode, void* userData); 33extern void OnOutputFormatChanged(OH_AVCodec* codec, OH_AVFormat* format, void* userData); 34extern void OnInputBufferAvailable(OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, void* userData); 35extern void OnOutputBufferAvailable(OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, 36 OH_AVCodecBufferAttr* attr, void* userData); 37 38enum 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 46class AEncSignal { 47public: 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 61class AEncDemoAuto : public NoCopyable { 62public: 63 AEncDemoAuto(); 64 virtual ~AEncDemoAuto(); 65 66 bool RunCaseFlush(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_AVFormat* GetOutputDescription(OH_AVCodec* codec); 87 88 OH_AVErrCode PushInputData(OH_AVCodec* codec, uint32_t index, int32_t size, int32_t offset); 89 90 OH_AVErrCode PushInputDataEOS(OH_AVCodec* codec, uint32_t index); 91 92 OH_AVErrCode FreeOutputData(OH_AVCodec* codec, uint32_t index); 93 94 OH_AVErrCode IsValid(OH_AVCodec* codec, bool* isValid); 95 96 uint32_t GetInputIndex(); 97 98 uint32_t GetOutputIndex(); 99 100 void HandleEOS(const uint32_t& index); 101 bool InitFile(std::string inputFile); 102private: 103 void ClearQueue(); 104 int32_t CreateEnd(); 105 int32_t CreateEndByMime(); 106 int32_t Configure(OH_AVFormat* format); 107 int32_t Start(); 108 int32_t Stop(); 109 int32_t Flush(); 110 int32_t Reset(); 111 int32_t Release(); 112 void InputFunc(); 113 void OutputFunc(); 114 void HandleInputEOS(const uint32_t index); 115 void SetFormat(OH_AVFormat *format); 116 int32_t HandleNormalInput(const uint32_t& index, const int64_t pts, const size_t size); 117 118 std::atomic<bool> isRunning_ = false; 119 std::unique_ptr<std::ifstream> testFile_; 120 std::unique_ptr<std::thread> inputLoop_; 121 std::unique_ptr<std::thread> outputLoop_; 122 OH_AVCodec* audioEnc_; 123 AEncSignal* signal_; 124 struct OH_AVCodecAsyncCallback cb_; 125 bool isFirstFrame_ = true; 126 size_t inputdatasize; 127 std::string inputdata; 128 AudioFormatType audioType_; 129 OH_AVFormat* format_; 130}; 131} // namespace AudioEncDemoAuto 132} // namespace MediaAVCodec 133} // namespace OHOS 134#endif // AUDIO_ENCODER_FLUSH_DEMO_BASE_H 135