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 <C2Component.h>
21 #include <C2Config.h>
22
23 #include <codec2/hidl/client.h>
24 #include <getopt.h>
25 #include <gtest/gtest.h>
26 #include <hidl/HidlSupport.h>
27 #include <chrono>
28 #include <fstream>
29
30 #define FLAG_NON_DISPLAY_FRAME (1 << 4)
31 #define MAX_RETRY 20
32 #define TIME_OUT 400ms
33 #define MAX_INPUT_BUFFERS 8
34 #define FLUSH_INTERVAL 30
35
36 using ::android::hardware::hidl_string;
37 using ::android::hardware::hidl_vec;
38 using ::android::hardware::Return;
39 using ::android::hardware::Void;
40
41 using namespace ::std::chrono;
42
43 using TestParameters = std::tuple<std::string, std::string>;
44 static std::vector<TestParameters> gTestParameters;
45
46 // Resource directory
47 extern std::string sResourceDir;
48
49 // Component name prefix
50 extern std::string sComponentNamePrefix;
51
52 struct FrameInfo {
53 int bytesCount;
54 uint32_t flags;
55 int64_t timestamp;
56 };
57
58 template <typename... T>
PrintInstanceTupleNameToString(const testing::TestParamInfo<std::tuple<T...>> & info)59 static inline std::string PrintInstanceTupleNameToString(
60 const testing::TestParamInfo<std::tuple<T...>>& info) {
61 std::stringstream ss;
62 std::apply([&ss](auto&&... elems) { ((ss << elems << '_'), ...); }, info.param);
63 ss << info.index;
64 std::string param_string = ss.str();
65 auto isNotAlphaNum = [](char c) { return !std::isalnum(c); };
66 std::replace_if(param_string.begin(), param_string.end(), isNotAlphaNum, '_');
67 return param_string;
68 }
69
70 /*
71 * Handle Callback functions onWorkDone(), onTripped(),
72 * onError(), onDeath(), onFramesRendered()
73 */
74 struct CodecListener : public android::Codec2Client::Listener {
75 public:
76 CodecListener(
77 const std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> fn = nullptr)
78 : callBack(fn) {}
onWorkDoneCodecListener79 virtual void onWorkDone(const std::weak_ptr<android::Codec2Client::Component>& comp,
80 std::list<std::unique_ptr<C2Work>>& workItems) override {
81 /* TODO */
82 ALOGD("onWorkDone called");
83 (void)comp;
84 if (callBack) callBack(workItems);
85 }
86
onTrippedCodecListener87 virtual void onTripped(
88 const std::weak_ptr<android::Codec2Client::Component>& comp,
89 const std::vector<std::shared_ptr<C2SettingResult>>& settingResults) override {
90 /* TODO */
91 (void)comp;
92 (void)settingResults;
93 }
94
onErrorCodecListener95 virtual void onError(const std::weak_ptr<android::Codec2Client::Component>& comp,
96 uint32_t errorCode) override {
97 /* TODO */
98 (void)comp;
99 ALOGD("onError called");
100 if (errorCode != 0) ALOGE("Error : %u", errorCode);
101 }
102
onDeathCodecListener103 virtual void onDeath(const std::weak_ptr<android::Codec2Client::Component>& comp) override {
104 /* TODO */
105 (void)comp;
106 }
107
onInputBufferDoneCodecListener108 virtual void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) override {
109 /* TODO */
110 (void)frameIndex;
111 (void)arrayIndex;
112 }
113
onFrameRenderedCodecListener114 virtual void onFrameRendered(uint64_t bufferQueueId, int32_t slotId,
115 int64_t timestampNs) override {
116 /* TODO */
117 (void)bufferQueueId;
118 (void)slotId;
119 (void)timestampNs;
120 }
121 // std::mutex mQueueLock;
122 // std::condition_variable mQueueCondition;
123 // std::list<std::unique_ptr<C2Work>> mWorkQueue;
124 std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> callBack;
125 };
126
127 void parseArgs(int argc, char** argv);
128
129 // Return all test parameters, a list of tuple of <instance, component>.
130 const std::vector<TestParameters>& getTestParameters();
131
132 // Return all test parameters, a list of tuple of <instance, component> with matching domain and
133 // kind.
134 const std::vector<TestParameters>& getTestParameters(C2Component::domain_t domain,
135 C2Component::kind_t kind);
136
137 /*
138 * common functions declarations
139 */
140 void testInputBuffer(const std::shared_ptr<android::Codec2Client::Component>& component,
141 std::mutex& queueLock, std::list<std::unique_ptr<C2Work>>& workQueue,
142 uint32_t flags, bool isNullBuffer);
143
144 void waitOnInputConsumption(std::mutex& queueLock, std::condition_variable& queueCondition,
145 std::list<std::unique_ptr<C2Work>>& workQueue,
146 size_t bufferCount = MAX_INPUT_BUFFERS);
147
148 void workDone(const std::shared_ptr<android::Codec2Client::Component>& component,
149 std::unique_ptr<C2Work>& work, std::list<uint64_t>& flushedIndices,
150 std::mutex& queueLock, std::condition_variable& queueCondition,
151 std::list<std::unique_ptr<C2Work>>& workQueue, bool& eos, bool& csd,
152 uint32_t& framesReceived);
153
154 int64_t getNowUs();
155
156 int32_t populateInfoVector(std::string info, android::Vector<FrameInfo>* frameInfo,
157 bool timestampDevTest, std::list<uint64_t>* timestampUslist);
158
159 void verifyFlushOutput(std::list<std::unique_ptr<C2Work>>& flushedWork,
160 std::list<std::unique_ptr<C2Work>>& workQueue,
161 std::list<uint64_t>& flushedIndices, std::mutex& queueLock);
162 #endif // MEDIA_C2_HIDL_TEST_COMMON_H
163