1 /* 2 * Copyright (c) 2022-2024 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 MediaAVCodec { 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 std::queue<std::shared_ptr<Media::AVSharedMemory>> availableInputBufferQueue_; 40 }; 41 42 class VDecDemoCallback : public AVCodecCallback, public NoCopyable { 43 public: VDecDemoCallback(std::shared_ptr<VDecSignal> signal)44 explicit VDecDemoCallback(std::shared_ptr<VDecSignal> signal) : signal_(signal) {}; 45 virtual ~VDecDemoCallback() = default; 46 47 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 48 void OnOutputFormatChanged(const Media::Format &format) override; 49 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<Media::AVSharedMemory> buffer) override; 50 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 51 std::shared_ptr<Media::AVSharedMemory> buffer) override; 52 53 private: 54 std::shared_ptr<VDecSignal> signal_; 55 }; 56 57 class VDecDemo : public NoCopyable { 58 public: 59 VDecDemo() = default; 60 virtual ~VDecDemo() = default; 61 void RunCase(); 62 void SetOutputSurface(sptr<Surface> surface); 63 void SetWindowSize(uint32_t width, uint32_t height); 64 65 private: 66 int32_t CreateVdec(); 67 int32_t Configure(const Media::Format &format); 68 int32_t Prepare(); 69 int32_t Start(); 70 int32_t Stop(); 71 int32_t Flush(); 72 int32_t Reset(); 73 int32_t Release(); 74 int32_t SetSurface(); 75 const int32_t *GetFrameLen(); 76 void InputFunc(); 77 void OutputFunc(); 78 void CheckCodecType(); 79 80 std::atomic<bool> isRunning_ = false; 81 sptr<Surface> surface_ = nullptr; 82 uint32_t width_ = 0; 83 uint32_t height_ = 0; 84 std::unique_ptr<std::ifstream> testFile_; 85 std::unique_ptr<std::thread> inputLoop_; 86 std::unique_ptr<std::thread> outputLoop_; 87 std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> vdec_; 88 std::shared_ptr<VDecSignal> signal_; 89 std::shared_ptr<VDecDemoCallback> cb_; 90 bool isFirstFrame_ = true; 91 bool isW = true; 92 int64_t timeStamp_ = 0; 93 uint32_t frameCount_ = 0; 94 uint32_t defaultFrameCount_ = 0; 95 }; 96 } // namespace MediaAVCodec 97 } // namespace OHOS 98 99 int StartMirror(int mode); 100 int StopMirror(int mode); 101 int StartExpand(int mode); 102 int StopExpand(int mode); 103 int CreateWindow(int mode); 104 int QueryRemoteDeviceInfo(int mode); 105 int QueryRemoteScreenInfo(int mode); 106 107 int SaTest(); 108 #endif // AVCODEC_VDEC_DEMO_H