1 /* 2 * Copyright (C) 2023 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 VENC_SAMPLE_H 17 #define VENC_SAMPLE_H 18 #include <atomic> 19 #include <fstream> 20 #include <iostream> 21 #include <mutex> 22 #include <queue> 23 #include <string> 24 #include <thread> 25 #include "securec.h" 26 #include "vcodec_mock.h" 27 28 namespace OHOS { 29 namespace MediaAVCodec { 30 struct VEncSignal { 31 public: 32 std::mutex mutex_; 33 std::mutex inMutex_; 34 std::mutex outMutex_; 35 std::condition_variable cond_; 36 std::condition_variable inCond_; 37 std::condition_variable outCond_; 38 std::queue<uint32_t> inIndexQueue_; 39 std::queue<uint32_t> outIndexQueue_; 40 std::queue<OH_AVCodecBufferAttr> outAttrQueue_; 41 std::queue<std::shared_ptr<AVMemoryMock>> inBufferQueue_; 42 std::queue<std::shared_ptr<AVMemoryMock>> outBufferQueue_; 43 int32_t errorNum_ = 0; 44 std::atomic<bool> isRunning_ = false; 45 }; 46 47 class VEncCallbackTest : public AVCodecCallbackMock { 48 public: 49 explicit VEncCallbackTest(std::shared_ptr<VEncSignal> signal); 50 virtual ~VEncCallbackTest(); 51 void OnError(int32_t errorCode) override; 52 void OnStreamChanged(std::shared_ptr<FormatMock> format) override; 53 void OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data) override; 54 void OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, OH_AVCodecBufferAttr attr) override; 55 56 private: 57 std::shared_ptr<VEncSignal> signal_ = nullptr; 58 }; 59 60 class VideoEncSample : public NoCopyable { 61 public: 62 explicit VideoEncSample(std::shared_ptr<VEncSignal> signal); 63 virtual ~VideoEncSample(); 64 bool CreateVideoEncMockByMime(const std::string &mime); 65 bool CreateVideoEncMockByName(const std::string &name); 66 67 int32_t Release(); 68 int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb); 69 int32_t Configure(std::shared_ptr<FormatMock> format); 70 int32_t Start(); 71 int32_t Stop(); 72 int32_t Flush(); 73 int32_t Reset(); 74 std::shared_ptr<FormatMock> GetOutputDescription(); 75 int32_t SetParameter(std::shared_ptr<FormatMock> format); 76 int32_t FreeOutputData(uint32_t index); 77 int32_t NotifyEos(); 78 int32_t PushInputData(uint32_t index, OH_AVCodecBufferAttr &attr); 79 std::shared_ptr<SurfaceMock> CreateInputSurface(); 80 bool IsValid(); 81 82 private: 83 std::shared_ptr<VideoEncMock> videoEnc_ = nullptr; 84 std::shared_ptr<VEncSignal> signal_ = nullptr; 85 }; 86 } // namespace MediaAVCodec 87 } // namespace OHOS 88 #endif // VENC_SAMPLE_H