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