• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 VDEC_SAMPLE_H
17 #define VDEC_SAMPLE_H
18 #include <atomic>
19 #include <fstream>
20 #include <iostream>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 #include <thread>
25 #include "securec.h"
26 #include "vcodec_mock.h"
27 
28 namespace OHOS {
29 namespace MediaAVCodec {
30 struct VDecSignal {
31 public:
32     std::mutex mutex_;
33     std::mutex inMutex_;
34     std::mutex outMutex_;
35     std::condition_variable cond_;
36     std::condition_variable inCond_;
37     std::condition_variable outCond_;
38     std::queue<uint32_t> inIndexQueue_;
39     std::queue<uint32_t> outIndexQueue_;
40     std::queue<OH_AVCodecBufferAttr> outAttrQueue_;
41     std::queue<std::shared_ptr<AVMemoryMock>> inBufferQueue_;
42     std::queue<std::shared_ptr<AVMemoryMock>> outBufferQueue_;
43     int32_t errorNum_ = 0;
44     std::atomic<bool> isRunning_ = false;
45 };
46 
47 class VDecCallbackTest : public AVCodecCallbackMock {
48 public:
49     explicit VDecCallbackTest(std::shared_ptr<VDecSignal> signal);
50     virtual ~VDecCallbackTest();
51     void OnError(int32_t errorCode) override;
52     void OnStreamChanged(std::shared_ptr<FormatMock> format) override;
53     void OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data) override;
54     void OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, OH_AVCodecBufferAttr attr) override;
55 
56 private:
57     std::shared_ptr<VDecSignal> signal_ = nullptr;
58 };
59 
60 class TestConsumerListener : public IBufferConsumerListener {
61 public:
62     TestConsumerListener(sptr<Surface> cs, std::string_view name);
63     ~TestConsumerListener();
64     void OnBufferAvailable() override;
65 
66 private:
67     int64_t timestamp_ = 0;
68     Rect damage_ = {};
69     sptr<Surface> cs_ = nullptr;
70     std::unique_ptr<std::ofstream> outFile_;
71 };
72 
73 class VideoDecSample : public NoCopyable {
74 public:
75     explicit VideoDecSample(std::shared_ptr<VDecSignal> signal);
76     virtual ~VideoDecSample();
77     bool CreateVideoDecMockByMime(const std::string &mime);
78     bool CreateVideoDecMockByName(const std::string &name);
79 
80     int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb);
81     int32_t SetOutputSurface();
82     int32_t Configure(std::shared_ptr<FormatMock> format);
83     int32_t Start();
84     int32_t Stop();
85     int32_t Flush();
86     int32_t Reset();
87     int32_t Release();
88     std::shared_ptr<FormatMock> GetOutputDescription();
89     int32_t SetParameter(std::shared_ptr<FormatMock> format);
90     int32_t PushInputData(uint32_t index, OH_AVCodecBufferAttr &attr);
91     int32_t RenderOutputData(uint32_t index);
92     int32_t FreeOutputData(uint32_t index);
93     bool IsValid();
94 
95     void SetOutPath(const std::string &path);
96     void SetSource(const std::string &path);
97 
98 private:
99     void FlushInner();
100     void RunInner();
101     void PrepareInner();
102     void OutputLoopFunc();
103     void InputLoopFunc();
104     int32_t OutputLoopInner();
105     int32_t InputLoopInner();
106     std::shared_ptr<VideoDecMock> videoDec_ = nullptr;
107     std::unique_ptr<std::ifstream> inFile_;
108     std::unique_ptr<std::ofstream> outFile_;
109     std::unique_ptr<std::thread> inputLoop_;
110     std::unique_ptr<std::thread> outputLoop_;
111     std::shared_ptr<VDecSignal> signal_ = nullptr;
112     std::string inPath_;
113     std::string outPath_;
114     std::string outSurfacePath_;
115     uint32_t datSize_ = 0;
116     uint32_t frameInputCount_ = 0;
117     uint32_t frameOutputCount_ = 0;
118     bool isFirstFrame_ = true;
119     bool isSurfaceMode_ = false;
120     bool isDump_ = true;
121     int64_t time_ = 0;
122     sptr<Surface> consumer_ = nullptr;
123     sptr<Surface> producer_ = nullptr;
124 };
125 } // namespace MediaAVCodec
126 } // namespace OHOS
127 #endif // VDEC_SAMPLE_H