1 /* 2 * Copyright (C) 2021 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 <thread> 22 #include <queue> 23 #include <string> 24 #include "avcodec_video_decoder.h" 25 #include "nocopyable.h" 26 27 namespace OHOS { 28 namespace Media { 29 class VDecSignal { 30 public: 31 std::mutex inMutex_; 32 std::mutex outMutex_; 33 std::condition_variable inCond_; 34 std::condition_variable outCond_; 35 std::queue<uint32_t> inQueue_; 36 std::queue<uint32_t> outQueue_; 37 }; 38 39 class VDecDemoCallback : public AVCodecCallback, public NoCopyable { 40 public: 41 explicit VDecDemoCallback(std::shared_ptr<VDecSignal> signal); 42 virtual ~VDecDemoCallback() = default; 43 44 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 45 void OnOutputFormatChanged(const Format &format) override; 46 void OnInputBufferAvailable(uint32_t index) override; 47 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; 48 49 private: 50 std::shared_ptr<VDecSignal> signal_; 51 }; 52 53 class VDecDemo : public NoCopyable { 54 public: 55 VDecDemo() = default; 56 virtual ~VDecDemo() = default; 57 void RunCase(); 58 59 private: 60 int32_t CreateVdec(); 61 int32_t Configure(const Format &format); 62 int32_t Prepare(); 63 int32_t Start(); 64 int32_t Stop(); 65 int32_t Flush(); 66 int32_t Reset(); 67 int32_t Release(); 68 int32_t SetSurface(); 69 void InputFunc(); 70 void OutputFunc(); 71 72 std::atomic<bool> isRunning_ = false; 73 std::unique_ptr<std::ifstream> testFile_; 74 std::unique_ptr<std::thread> inputLoop_; 75 std::unique_ptr<std::thread> outputLoop_; 76 std::shared_ptr<AVCodecVideoDecoder> vdec_; 77 std::shared_ptr<VDecSignal> signal_; 78 std::shared_ptr<VDecDemoCallback> cb_; 79 bool isFirstFrame_ = true; 80 int64_t timeStamp_ = 0; 81 uint32_t frameCount_ = 0; 82 }; 83 } // namespace Media 84 } // namespace OHOS 85 #endif // AVCODEC_VDEC_DEMO_H 86