1 /* 2 * Copyright (C) 2022 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 VDEC_MOCK_H 17 #define VDEC_MOCK_H 18 #include <iostream> 19 #include <fstream> 20 #include <thread> 21 #include <mutex> 22 #include <queue> 23 #include <string> 24 #include "avcodec_mock.h" 25 #include "unittest_log.h" 26 #include "test_params_config.h" 27 #include "securec.h" 28 namespace OHOS { 29 namespace Media { 30 struct VDecSignal { 31 public: 32 std::mutex inMutex_; 33 std::mutex outMutex_; 34 std::condition_variable inCond_; 35 std::condition_variable outCond_; 36 std::queue<uint32_t> inIndexQueue_; 37 std::queue<uint32_t> outIndexQueue_; 38 std::queue<uint32_t> inSizeQueue_; 39 std::queue<uint32_t> outSizeQueue_; 40 std::queue<std::shared_ptr<AVMemoryMock>> inBufferQueue_; 41 std::queue<std::shared_ptr<AVMemoryMock>> outBufferQueue_; 42 std::atomic<bool> isRunning_ = false; 43 }; 44 45 class VDecCallbackTest : public AVCodecCallbackMock { 46 public: 47 explicit VDecCallbackTest(std::shared_ptr<VDecSignal> signal); 48 virtual ~VDecCallbackTest(); 49 void OnError(int32_t errorCode) override; 50 void OnStreamChanged(std::shared_ptr<FormatMock> format) override; 51 void OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data) override; 52 void OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, AVCodecBufferAttrMock attr) override; 53 private: 54 std::shared_ptr<VDecSignal> signal_ = nullptr; 55 }; 56 57 class VDecMock : public NoCopyable { 58 public: 59 explicit VDecMock(std::shared_ptr<VDecSignal> signal); 60 virtual ~VDecMock(); 61 bool CreateVideoDecMockByMime(const std::string &mime); 62 bool CreateVideoDecMockByName(const std::string &name); 63 int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb); 64 int32_t SetOutputSurface(std::shared_ptr<SurfaceMock> surface); 65 int32_t Configure(std::shared_ptr<FormatMock> format); 66 int32_t Prepare(); 67 int32_t Start(); 68 int32_t Stop(); 69 int32_t Flush(); 70 int32_t Reset(); 71 int32_t Release(); 72 std::shared_ptr<FormatMock> GetOutputMediaDescription(); 73 int32_t SetParameter(std::shared_ptr<FormatMock> format); 74 int32_t PushInputData(uint32_t index, AVCodecBufferAttrMock &attr); 75 int32_t RenderOutputData(uint32_t index); 76 int32_t FreeOutputData(uint32_t index); 77 void SetSource(const std::string &path, const uint32_t es[], const uint32_t &size); 78 private: 79 void FlushInner(); 80 std::unique_ptr<std::ifstream> testFile_; 81 std::unique_ptr<std::thread> inputLoop_; 82 std::unique_ptr<std::thread> outputLoop_; 83 std::shared_ptr<VideoDecMock> videoDec_ = nullptr; 84 std::shared_ptr<VDecSignal> signal_ = nullptr; 85 int32_t PushInputDataMock(uint32_t index, uint32_t bufferSize); 86 void InpLoopFunc(); 87 void OutLoopFunc(); 88 bool isFirstFrame_ = true; 89 int64_t timestamp_ = 0; 90 uint32_t frameCount_ = 0; 91 const uint32_t *es_; 92 uint32_t esLength_; 93 std::string inpPath_; 94 }; 95 } // namespace Media 96 } // namespace OHOS 97 #endif // VDEC_MOCK_H