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 DEMUXER_PLUGIN_UNIT_TEST_H 17 #define DEMUXER_PLUGIN_UNIT_TEST_H 18 19 #define DEFAULT_BUFFSIZE (3840 * 2160 * 3) 20 #include <gtest/gtest.h> 21 #include <memory> 22 #include <string> 23 #include <map> 24 #include <sys/stat.h> 25 #include <fcntl.h> 26 #include "stream_demuxer.h" 27 #include "common/media_source.h" 28 #include "demuxer_plugin_manager.h" 29 #include "plugin/plugin_manager_v2.h" 30 #include "ffmpeg_demuxer_plugin.h" 31 32 using MediaAVBuffer = OHOS::Media::AVBuffer; 33 namespace OHOS { 34 namespace Media { 35 class DemuxerPluginUnitTest : public testing::Test { 36 public: 37 static void SetUpTestCase(); 38 static void TearDownTestCase(); 39 void SetUp() override; 40 void TearDown() override; 41 void InitResource(const std::string &filePath, std::string pluginName); 42 void InitResourceURI(const std::string &filePath, std::string pluginName); 43 void InitWeakNetworkDemuxerPlugin( 44 const std::string& filePath, std::string pluginName, int64_t failOffset, size_t maxFailCount); 45 void InitWeakNetworkDemuxerPluginURI( 46 const std::string& filePath, std::string pluginName, int64_t failOffset, size_t maxFailCount); 47 void SetInitValue(); 48 bool isEOS(std::map<uint32_t, bool>& countFlag); 49 bool CheckKeyFrameIndex(std::vector<uint32_t> keyFrameIndexList, const uint32_t frameIndex, const bool isKeyFrame); 50 void CountFrames(uint32_t index); 51 void SetEosValue(); 52 void ReadData(); 53 void RemoveValue(); 54 void CheckAllFrames(const std::vector<int>& expectedFrames, const std::vector<int>& expectedKeyFrames, 55 const std::vector<uint32_t>& keyFrameIndex); 56 protected: 57 int fd_ = -1; 58 std::shared_ptr<OHOS::Media::Plugins::Ffmpeg::FFmpegDemuxerPlugin> demuxerPlugin_ = nullptr; 59 bool initStatus_ = false; 60 int32_t nbStreams_ = 0; 61 int32_t videoHeight_ = 0; 62 int32_t videoWidth_ = 0; 63 int32_t numbers_ = 0; 64 std::vector<uint32_t> selectedTrackIds_; 65 std::map<uint32_t, int32_t> frames_; 66 std::map<uint32_t, int32_t> keyFrames_; 67 std::map<uint32_t, bool> eosFlag_; 68 MediaInfo mediaInfo_; 69 uint32_t flag_; 70 }; 71 72 struct AVBufferWrapper { 73 std::shared_ptr<MediaAVBuffer> mediaAVBuffer; AVBufferWrapperAVBufferWrapper74 explicit AVBufferWrapper(uint32_t size) 75 { 76 if (size == 0) { 77 size = DEFAULT_BUFFSIZE; 78 } 79 ptr = std::make_unique<uint8_t []>(size); 80 mediaAVBuffer = MediaAVBuffer::CreateAVBuffer(ptr.get(), size, 0); 81 } 82 private: 83 AVBufferWrapper() = delete; 84 std::unique_ptr<uint8_t []> ptr; 85 }; 86 87 class SourceCallback : public Plugins::Callback { 88 public: SourceCallback(std::shared_ptr<DemuxerPluginManager> demuxerPluginManager)89 explicit SourceCallback(std::shared_ptr<DemuxerPluginManager> demuxerPluginManager) 90 : demuxerPluginManager_(demuxerPluginManager) {} OnEvent(const Plugins::PluginEvent & event)91 void OnEvent(const Plugins::PluginEvent &event) override 92 { 93 switch (event.type) { 94 case PluginEventType::INITIAL_BUFFER_SUCCESS: { 95 demuxerPluginManager_->NotifyInitialBufferingEnd(true); 96 break; 97 } 98 default: 99 break; 100 } 101 } 102 std::shared_ptr<DemuxerPluginManager> demuxerPluginManager_; 103 }; 104 105 class StreamDemuxerPullDataFailMock : public StreamDemuxer { 106 public: StreamDemuxerPullDataFailMock(size_t failOffset,size_t maxFailCount)107 StreamDemuxerPullDataFailMock(size_t failOffset, size_t maxFailCount) 108 : StreamDemuxer(), failOffset_(failOffset), maxFailCount_(maxFailCount) {} 109 private: CallbackReadAt(int32_t streamID,int64_t offset,std::shared_ptr<Buffer> & buffer,size_t expectedLen)110 Status CallbackReadAt(int32_t streamID, int64_t offset, std::shared_ptr<Buffer>& buffer, 111 size_t expectedLen) override 112 { 113 num += expectedLen; 114 if (num > failOffset_ && failCount_ < maxFailCount_) { 115 failCount_++; 116 return Status::ERROR_AGAIN; 117 } 118 return StreamDemuxer::CallbackReadAt(streamID, offset, buffer, expectedLen); 119 } 120 size_t failOffset_ = 0; 121 size_t maxFailCount_ = 0; 122 size_t failCount_ = 0; 123 int num = 0; 124 }; 125 } // namespace Media 126 } // namespace OHOS 127 #endif // DEMUXER_PLUGIN_UNIT_TEST_H