xref: /aosp_15_r20/frameworks/av/media/codec2/hal/hidl/1.0/vts/functional/common/media_c2_hidl_test_common.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MEDIA_C2_HIDL_TEST_COMMON_H
18 #define MEDIA_C2_HIDL_TEST_COMMON_H
19 
20 #include <C2BufferPriv.h>
21 #include <C2Component.h>
22 #include <C2Config.h>
23 #include <C2PlatformSupport.h>
24 
25 #include <codec2/hidl/client.h>
26 #include <getopt.h>
27 #include <gtest/gtest.h>
28 #include <hidl/HidlSupport.h>
29 #include <chrono>
30 #include <fstream>
31 
32 #define MAX_RETRY 20
33 #define TIME_OUT 400ms
34 #define MAX_INPUT_BUFFERS 8
35 #define FLUSH_INTERVAL 30
36 
37 using ::android::hardware::hidl_string;
38 using ::android::hardware::hidl_vec;
39 using ::android::hardware::Return;
40 using ::android::hardware::Void;
41 
42 using namespace ::std::chrono;
43 
44 using TestParameters = std::tuple<std::string, std::string>;
45 static std::vector<TestParameters> gTestParameters;
46 
47 // Resource directory
48 extern std::string sResourceDir;
49 
50 // Component name prefix
51 extern std::string sComponentNamePrefix;
52 
53 enum c2_vts_flags_t {
54     VTS_BIT_FLAG_SYNC_FRAME = 1,
55     VTS_BIT_FLAG_NO_SHOW_FRAME = 2,
56     VTS_BIT_FLAG_CSD_FRAME = 3,
57     VTS_BIT_FLAG_LARGE_AUDIO_FRAME = 4,
58 };
59 
60 struct FrameInfo {
61     int bytesCount;
62     uint32_t vtsFlags;
63     int64_t timestamp;
64     // This is used when access-units are marked with
65     // VTS_BIT_FLAG_LARGE_AUDIO_FRAME
66     std::vector<C2AccessUnitInfosStruct> largeFrameInfo;
67 };
68 
69 template <typename... T>
PrintInstanceTupleNameToString(const testing::TestParamInfo<std::tuple<T...>> & info)70 static inline std::string PrintInstanceTupleNameToString(
71         const testing::TestParamInfo<std::tuple<T...>>& info) {
72     std::stringstream ss;
73     std::apply([&ss](auto&&... elems) { ((ss << elems << '_'), ...); }, info.param);
74     ss << info.index;
75     std::string param_string = ss.str();
76     auto isNotAlphaNum = [](char c) { return !std::isalnum(c); };
77     std::replace_if(param_string.begin(), param_string.end(), isNotAlphaNum, '_');
78     return param_string;
79 }
80 
81 /*
82  * Handle Callback functions onWorkDone(), onTripped(),
83  * onError(), onDeath(), onFramesRendered()
84  */
85 struct CodecListener : public android::Codec2Client::Listener {
86   public:
87     CodecListener(
88             const std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> fn = nullptr)
callBackCodecListener89         : callBack(fn) {}
onWorkDoneCodecListener90     virtual void onWorkDone(const std::weak_ptr<android::Codec2Client::Component>& comp,
91                             std::list<std::unique_ptr<C2Work>>& workItems) override {
92         /* TODO */
93         (void)comp;
94         if (callBack) callBack(workItems);
95     }
96 
onTrippedCodecListener97     virtual void onTripped(
98             const std::weak_ptr<android::Codec2Client::Component>& comp,
99             const std::vector<std::shared_ptr<C2SettingResult>>& settingResults) override {
100         /* TODO */
101         (void)comp;
102         (void)settingResults;
103     }
104 
onErrorCodecListener105     virtual void onError(const std::weak_ptr<android::Codec2Client::Component>& comp,
106                          uint32_t errorCode) override {
107         /* TODO */
108         (void)comp;
109         if (errorCode != 0) ALOGE("Error : %u", errorCode);
110     }
111 
onDeathCodecListener112     virtual void onDeath(const std::weak_ptr<android::Codec2Client::Component>& comp) override {
113         /* TODO */
114         (void)comp;
115     }
116 
onInputBufferDoneCodecListener117     virtual void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) override {
118         /* TODO */
119         (void)frameIndex;
120         (void)arrayIndex;
121     }
122 
onFrameRenderedCodecListener123     virtual void onFrameRendered(uint64_t bufferQueueId, int32_t slotId,
124                                  int64_t timestampNs) override {
125         /* TODO */
126         (void)bufferQueueId;
127         (void)slotId;
128         (void)timestampNs;
129     }
130     // std::mutex mQueueLock;
131     // std::condition_variable mQueueCondition;
132     // std::list<std::unique_ptr<C2Work>> mWorkQueue;
133     std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> callBack;
134 };
135 
136 C2PooledBlockPool::BufferPoolVer getBufferPoolVer();
137 
138 void parseArgs(int argc, char** argv);
139 
140 // Return all test parameters, a list of tuple of <instance, component>.
141 const std::vector<TestParameters>& getTestParameters();
142 
143 // Return all test parameters, a list of tuple of <instance, component> with matching domain and
144 // kind.
145 const std::vector<TestParameters>& getTestParameters(C2Component::domain_t domain,
146                                                      C2Component::kind_t kind);
147 
148 /*
149  * common functions declarations
150  */
151 void testInputBuffer(const std::shared_ptr<android::Codec2Client::Component>& component,
152                      std::mutex& queueLock, std::list<std::unique_ptr<C2Work>>& workQueue,
153                      uint32_t flags, bool isNullBuffer);
154 
155 void waitOnInputConsumption(std::mutex& queueLock, std::condition_variable& queueCondition,
156                             std::list<std::unique_ptr<C2Work>>& workQueue,
157                             size_t bufferCount = MAX_INPUT_BUFFERS);
158 
159 void workDone(const std::shared_ptr<android::Codec2Client::Component>& component,
160               std::unique_ptr<C2Work>& work, std::list<uint64_t>& flushedIndices,
161               std::mutex& queueLock, std::condition_variable& queueCondition,
162               std::list<std::unique_ptr<C2Work>>& workQueue, bool& eos, bool& csd,
163               uint32_t& framesReceived);
164 
165 int64_t getNowUs();
166 
167 int32_t populateInfoVector(std::string info, android::Vector<FrameInfo>* frameInfo,
168                            bool timestampDevTest, std::list<uint64_t>* timestampUslist);
169 
170 void verifyFlushOutput(std::list<std::unique_ptr<C2Work>>& flushedWork,
171                        std::list<std::unique_ptr<C2Work>>& workQueue,
172                        std::list<uint64_t>& flushedIndices, std::mutex& queueLock);
173 
174 int mapInfoFlagstoVtsFlags(int infoFlags);
175 
176 #endif  // MEDIA_C2_HIDL_TEST_COMMON_H
177