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 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 28 namespace OHOS { 29 namespace Media { 30 class 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> inQueue_; 37 std::queue<uint32_t> outQueue_; 38 }; 39 40 class VDecDemoCallback : public AVCodecCallback, public NoCopyable { 41 public: VDecDemoCallback(std::shared_ptr<VDecSignal> signal)42 explicit VDecDemoCallback(std::shared_ptr<VDecSignal> signal) : signal_(signal) {}; 43 virtual ~VDecDemoCallback() = default; 44 45 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 46 void OnOutputFormatChanged(const Format &format) override; 47 void OnInputBufferAvailable(uint32_t index) override; 48 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; 49 50 private: 51 std::shared_ptr<VDecSignal> signal_; 52 }; 53 54 class VDecDemo : public NoCopyable { 55 public: 56 VDecDemo() = default; 57 virtual ~VDecDemo() = default; 58 void RunCase(); 59 void SetOutputSurface(sptr<Surface> surface); 60 void SetWindowSize(uint32_t width, uint32_t height); 61 62 private: 63 int32_t CreateVdec(); 64 int32_t Configure(const Format &format); 65 int32_t Prepare(); 66 int32_t Start(); 67 int32_t Stop(); 68 int32_t Flush(); 69 int32_t Reset(); 70 int32_t Release(); 71 int32_t SetSurface(); 72 const int32_t *GetFrameLen(); 73 void InputFunc(); 74 void OutputFunc(); 75 void CheckCodecType(); 76 77 std::atomic<bool> isRunning_ = false; 78 sptr<Surface> surface_ = nullptr; 79 uint32_t width_ = 0; 80 uint32_t height_ = 0; 81 std::unique_ptr<std::ifstream> testFile_; 82 std::unique_ptr<std::thread> inputLoop_; 83 std::unique_ptr<std::thread> outputLoop_; 84 std::shared_ptr<Media::AVCodecVideoDecoder> vdec_; 85 std::shared_ptr<VDecSignal> signal_; 86 std::shared_ptr<VDecDemoCallback> cb_; 87 bool isFirstFrame_ = true; 88 bool isW = true; 89 int64_t timeStamp_ = 0; 90 uint32_t frameCount_ = 0; 91 uint32_t defaultFrameCount_ = 0; 92 }; 93 } // namespace Media 94 } // namespace OHOS 95 #endif // AVCODEC_VDEC_DEMO_H