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 static std::vector<std::tuple<std::string, std::string>> kTestParameters;
44 
45 struct FrameInfo {
46     int bytesCount;
47     uint32_t flags;
48     int64_t timestamp;
49 };
50 
51 /*
52  * Handle Callback functions onWorkDone(), onTripped(),
53  * onError(), onDeath(), onFramesRendered()
54  */
55 struct CodecListener : public android::Codec2Client::Listener {
56   public:
57     CodecListener(
58             const std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> fn = nullptr)
callBackCodecListener59         : callBack(fn) {}
onWorkDoneCodecListener60     virtual void onWorkDone(const std::weak_ptr<android::Codec2Client::Component>& comp,
61                             std::list<std::unique_ptr<C2Work>>& workItems) override {
62         /* TODO */
63         ALOGD("onWorkDone called");
64         (void)comp;
65         if (callBack) callBack(workItems);
66     }
67 
onTrippedCodecListener68     virtual void onTripped(
69             const std::weak_ptr<android::Codec2Client::Component>& comp,
70             const std::vector<std::shared_ptr<C2SettingResult>>& settingResults) override {
71         /* TODO */
72         (void)comp;
73         (void)settingResults;
74     }
75 
onErrorCodecListener76     virtual void onError(const std::weak_ptr<android::Codec2Client::Component>& comp,
77                          uint32_t errorCode) override {
78         /* TODO */
79         (void)comp;
80         ALOGD("onError called");
81         if (errorCode != 0) ALOGE("Error : %u", errorCode);
82     }
83 
onDeathCodecListener84     virtual void onDeath(const std::weak_ptr<android::Codec2Client::Component>& comp) override {
85         /* TODO */
86         (void)comp;
87     }
88 
onInputBufferDoneCodecListener89     virtual void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) override {
90         /* TODO */
91         (void)frameIndex;
92         (void)arrayIndex;
93     }
94 
onFrameRenderedCodecListener95     virtual void onFrameRendered(uint64_t bufferQueueId, int32_t slotId,
96                                  int64_t timestampNs) override {
97         /* TODO */
98         (void)bufferQueueId;
99         (void)slotId;
100         (void)timestampNs;
101     }
102     // std::mutex mQueueLock;
103     // std::condition_variable mQueueCondition;
104     // std::list<std::unique_ptr<C2Work>> mWorkQueue;
105     std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> callBack;
106 };
107 
108 // Return all test parameters, a list of tuple of <instance, component>.
109 const std::vector<std::tuple<std::string, std::string>>& getTestParameters();
110 
111 // Return all test parameters, a list of tuple of <instance, component> with matching domain and
112 // kind.
113 const std::vector<std::tuple<std::string, std::string>>& getTestParameters(
114         C2Component::domain_t domain, C2Component::kind_t kind);
115 
116 /*
117  * common functions declarations
118  */
119 void testInputBuffer(const std::shared_ptr<android::Codec2Client::Component>& component,
120                      std::mutex& queueLock, std::list<std::unique_ptr<C2Work>>& workQueue,
121                      uint32_t flags, bool isNullBuffer);
122 
123 void waitOnInputConsumption(std::mutex& queueLock, std::condition_variable& queueCondition,
124                             std::list<std::unique_ptr<C2Work>>& workQueue,
125                             size_t bufferCount = MAX_INPUT_BUFFERS);
126 
127 void workDone(const std::shared_ptr<android::Codec2Client::Component>& component,
128               std::unique_ptr<C2Work>& work, std::list<uint64_t>& flushedIndices,
129               std::mutex& queueLock, std::condition_variable& queueCondition,
130               std::list<std::unique_ptr<C2Work>>& workQueue, bool& eos, bool& csd,
131               uint32_t& framesReceived);
132 
133 int64_t getNowUs();
134 
135 int32_t populateInfoVector(std::string info, android::Vector<FrameInfo>* frameInfo,
136                            bool timestampDevTest, std::list<uint64_t>* timestampUslist);
137 
138 void verifyFlushOutput(std::list<std::unique_ptr<C2Work>>& flushedWork,
139                        std::list<std::unique_ptr<C2Work>>& workQueue,
140                        std::list<uint64_t>& flushedIndices, std::mutex& queueLock);
141 #endif  // MEDIA_C2_HIDL_TEST_COMMON_H
142