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 void InputFunc(); 50 void OutputFunc(); GetAudioCodec()51 std::shared_ptr<AVCodecAudioCodec>& GetAudioCodec() 52 { 53 return audioCodec_; 54 } GetAudioMeta()55 std::shared_ptr<Media::Meta>& GetAudioMeta() 56 { 57 return meta_; 58 } GetInnerBufferQueue()59 std::shared_ptr<Media::AVBufferQueue>& GetInnerBufferQueue() 60 { 61 return innerBufferQueue_; 62 } GetImplConsumer()63 sptr<Media::AVBufferQueueConsumer>& GetImplConsumer() 64 { 65 return implConsumer_; 66 } 67 68 private: 69 int32_t GetInputBufferSize(); 70 int32_t fileSize_ = 0; 71 std::atomic<int32_t> bufferConsumerAvailableCount_ = 0; 72 std::atomic<bool> isRunning_ = false; 73 std::unique_ptr<std::ifstream> testFile_; 74 std::shared_ptr<AVCodecAudioCodec> audioCodec_; 75 std::shared_ptr<Media::Meta> meta_; 76 std::shared_ptr<Media::AVBufferQueue> innerBufferQueue_; 77 sptr<Media::AVBufferQueueConsumer> implConsumer_; 78 sptr<Media::AVBufferQueueProducer> mediaCodecProducer_; 79 std::unique_ptr<std::ifstream> inputFile_; 80 std::unique_ptr<std::ofstream> outputFile_; 81 }; 82 83 class AudioCodecConsumerListener : public OHOS::Media::IConsumerListener { 84 public: 85 explicit AudioCodecConsumerListener(AudioDecInnerAvBuffer *demo); 86 87 void OnBufferAvailable() override; 88 89 private: 90 AudioDecInnerAvBuffer *demo_; 91 }; 92 93 class AVCodecInnerCallback : public MediaCodecCallback { 94 public: 95 AVCodecInnerCallback() = default; 96 virtual ~AVCodecInnerCallback() = default; 97 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 98 void OnOutputFormatChanged(const Format &format) override; 99 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 100 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 101 }; 102 } // namespace MediaAVCodec 103 } // namespace OHOS 104 #endif // AUDIO_CODEC_INNER_AVBUFFER_UNIT_TEST