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 AUDIO_CODEC_INNER_AVBUFFER_UNIT_TEST 17 #define AUDIO_CODEC_INNER_AVBUFFER_UNIT_TEST 18 19 #include <gtest/gtest.h> 20 #include <atomic> 21 #include <fstream> 22 #include <thread> 23 #include <queue> 24 #include <string> 25 #include "avcodec_common.h" 26 #include "avcodec_audio_codec.h" 27 #include "buffer/avbuffer.h" 28 #include "buffer/avbuffer_queue.h" 29 #include "buffer/avbuffer_queue_consumer.h" 30 #include "buffer/avbuffer_queue_define.h" 31 #include "nocopyable.h" 32 33 namespace OHOS { 34 namespace MediaAVCodec { 35 36 class AVCodecAudioCodecUnitTest : public testing::Test { 37 public: 38 static void SetUpTestCase(void); 39 static void TearDownTestCase(void); 40 void SetUp(); 41 void TearDown(); 42 }; 43 44 class AudioDecInnerAvBuffer { 45 public: 46 AudioDecInnerAvBuffer() = default; 47 virtual ~AudioDecInnerAvBuffer() = default; 48 int32_t RunCase( 49 const std::string_view &codecName, const std::string_view &inputPath, const std::string_view &outputPath); 50 void InputFunc(); 51 void OutputFunc(); 52 void SyncFunc(); 53 void SyncOutputFunc(); 54 void EnableAsyncChangePluginTest(); GetAudioCodec()55 std::shared_ptr<AVCodecAudioCodec>& GetAudioCodec() 56 { 57 return audioCodec_; 58 } GetAudioMeta()59 std::shared_ptr<Media::Meta>& GetAudioMeta() 60 { 61 return meta_; 62 } GetInnerBufferQueue()63 std::shared_ptr<Media::AVBufferQueue>& GetInnerBufferQueue() 64 { 65 return innerBufferQueue_; 66 } GetImplConsumer()67 sptr<Media::AVBufferQueueConsumer>& GetImplConsumer() 68 { 69 return implConsumer_; 70 } 71 72 private: 73 int32_t GetInputBufferSize(); 74 int32_t fileSize_ = 0; 75 std::atomic<int32_t> bufferConsumerAvailableCount_ = 0; 76 std::atomic<bool> isRunning_ = false; 77 std::unique_ptr<std::ifstream> testFile_; 78 std::shared_ptr<AVCodecAudioCodec> audioCodec_; 79 std::shared_ptr<Media::Meta> meta_; 80 std::shared_ptr<Media::AVBufferQueue> innerBufferQueue_; 81 sptr<Media::AVBufferQueueConsumer> implConsumer_; 82 sptr<Media::AVBufferQueueProducer> mediaCodecProducer_; 83 std::unique_ptr<std::ifstream> inputFile_; 84 std::unique_ptr<std::ofstream> outputFile_; 85 86 bool enableAsyncChangePluginTest_ = false; 87 bool isChangePluginThreadRunning_ = false; 88 std::unique_ptr<std::thread> changePluginThread_; 89 90 size_t outputSize_ = 0; 91 }; 92 93 class AudioCodecConsumerListener : public OHOS::Media::IConsumerListener { 94 public: 95 explicit AudioCodecConsumerListener(AudioDecInnerAvBuffer *demo); 96 97 void OnBufferAvailable() override; 98 99 private: 100 AudioDecInnerAvBuffer *demo_; 101 }; 102 103 class AVCodecInnerCallback : public MediaCodecCallback { 104 public: 105 AVCodecInnerCallback() = default; 106 virtual ~AVCodecInnerCallback() = default; 107 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 108 void OnOutputFormatChanged(const Format &format) override; 109 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 110 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 111 }; 112 } // namespace MediaAVCodec 113 } // namespace OHOS 114 #endif // AUDIO_CODEC_INNER_AVBUFFER_UNIT_TEST