1 /* 2 * Copyright (c) 2022-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 AVCODEC_VDEC_DEMO_H 17 #define AVCODEC_VDEC_DEMO_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <queue> 22 #include <string> 23 #include <thread> 24 25 #include "avcodec_video_decoder.h" 26 #include "nocopyable.h" 27 #include "distributed_hardware_log.h" 28 29 namespace OHOS { 30 namespace Media { 31 class VDecSignal { 32 public: 33 std::mutex inMutex_; 34 std::mutex outMutex_; 35 std::condition_variable inCond_; 36 std::condition_variable outCond_; 37 std::queue<uint32_t> inQueue_; 38 std::queue<uint32_t> outQueue_; 39 }; 40 41 class VDecDemoCallback : public AVCodecCallback, public NoCopyable { 42 public: VDecDemoCallback(std::shared_ptr<VDecSignal> signal)43 explicit VDecDemoCallback(std::shared_ptr<VDecSignal> signal) : signal_(signal) {}; 44 virtual ~VDecDemoCallback() = default; 45 46 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 47 void OnOutputFormatChanged(const Format &format) override; 48 void OnInputBufferAvailable(uint32_t index) override; 49 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; 50 51 private: 52 std::shared_ptr<VDecSignal> signal_; 53 }; 54 55 class VDecDemo : public NoCopyable { 56 public: 57 VDecDemo() = default; 58 virtual ~VDecDemo() = default; 59 void RunCase(); 60 void SetOutputSurface(sptr<Surface> surface); 61 void SetWindowSize(uint32_t width, uint32_t height); 62 63 private: 64 int32_t CreateVdec(); 65 int32_t Configure(const Format &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 int32_t SetSurface(); 73 const int32_t *GetFrameLen(); 74 void InputFunc(); 75 void OutputFunc(); 76 void CheckCodecType(); 77 78 std::atomic<bool> isRunning_ = false; 79 sptr<Surface> surface_ = nullptr; 80 uint32_t width_ = 0; 81 uint32_t height_ = 0; 82 std::unique_ptr<std::ifstream> testFile_; 83 std::unique_ptr<std::thread> inputLoop_; 84 std::unique_ptr<std::thread> outputLoop_; 85 std::shared_ptr<Media::AVCodecVideoDecoder> vdec_; 86 std::shared_ptr<VDecSignal> signal_; 87 std::shared_ptr<VDecDemoCallback> cb_; 88 bool isFirstFrame_ = true; 89 bool isW = true; 90 int64_t timeStamp_ = 0; 91 uint32_t frameCount_ = 0; 92 uint32_t defaultFrameCount_ = 0; 93 }; 94 } // namespace Media 95 } // namespace OHOS 96 97 int StartMirror(int mode); 98 int StopMirror(int mode); 99 int StartExpand(int mode); 100 int StopExpand(int mode); 101 int CreateWindow(int mode); 102 int QueryRemoteDeviceInfo(int mode); 103 int QueryRemoteScreenInfo(int mode); 104 105 int SaTest(); 106 #endif // AVCODEC_VDEC_DEMO_H