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 <codec2/hidl/client.h>
21 
22 #include <android/hardware/media/c2/1.0/types.h>
23 
24 #include <C2Component.h>
25 #include <C2Config.h>
26 #include <getopt.h>
27 #include <hidl/HidlSupport.h>
28 #include <media/stagefright/foundation/ALooper.h>
29 #include <media/stagefright/foundation/Mutexed.h>
30 
31 using namespace ::android::hardware::media::c2::V1_0;
32 using namespace ::android::hardware::media::c2::V1_0::utils;
33 
34 using ::android::Mutexed;
35 using ::android::hardware::Void;
36 using ::android::hardware::Return;
37 using ::android::hardware::hidl_vec;
38 using ::android::hardware::hidl_string;
39 
40 #include <VtsHalHidlTargetTestEnvBase.h>
41 
42 #define MAX_RETRY 20
43 #define TIME_OUT 400ms
44 #define MAX_INPUT_BUFFERS 8
45 
46 /*
47  * Handle Callback functions onWorkDone(), onTripped(),
48  * onError(), onDeath(), onFramesRendered()
49  */
50 struct CodecListener : public android::Codec2Client::Listener {
51    public:
52     CodecListener(
53         const std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> fn =
54             nullptr)
callBackCodecListener55         : callBack(fn) {}
onWorkDoneCodecListener56     virtual void onWorkDone(
57         const std::weak_ptr<android::Codec2Client::Component>& comp,
58         std::list<std::unique_ptr<C2Work>>& workItems) override {
59         /* TODO */
60         ALOGD("onWorkDone called");
61         (void)comp;
62         if (callBack) callBack(workItems);
63     }
64 
onTrippedCodecListener65     virtual void onTripped(
66         const std::weak_ptr<android::Codec2Client::Component>& comp,
67         const std::vector<std::shared_ptr<C2SettingResult>>& settingResults)
68         override {
69         /* TODO */
70         (void)comp;
71         (void)settingResults;
72     }
73 
onErrorCodecListener74     virtual void onError(
75         const std::weak_ptr<android::Codec2Client::Component>& comp,
76         uint32_t errorCode) override {
77         /* TODO */
78         (void)comp;
79         ALOGD("onError called");
80         if (errorCode != 0) ALOGE("Error : %u", errorCode);
81     }
82 
onDeathCodecListener83     virtual void onDeath(
84         const std::weak_ptr<android::Codec2Client::Component>& comp) override {
85         /* TODO */
86         (void)comp;
87     }
88 
onInputBufferDoneCodecListener89     virtual void onInputBufferDone(
90         uint64_t frameIndex, size_t arrayIndex) override {
91         /* TODO */
92         (void)frameIndex;
93         (void)arrayIndex;
94     }
95 
onFrameRenderedCodecListener96     virtual void onFrameRendered(
97         uint64_t bufferQueueId,
98         int32_t slotId,
99         int64_t timestampNs) override {
100         /* TODO */
101         (void)bufferQueueId;
102         (void)slotId;
103         (void)timestampNs;
104     }
105     // std::mutex mQueueLock;
106     // std::condition_variable mQueueCondition;
107     // std::list<std::unique_ptr<C2Work>> mWorkQueue;
108     std::function<void(std::list<std::unique_ptr<C2Work>>& workItems)> callBack;
109 };
110 
111 // A class for test environment setup
112 class ComponentTestEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
113    private:
114     typedef ::testing::VtsHalHidlTargetTestEnvBase Super;
115 
116    public:
registerTestServices()117     virtual void registerTestServices() override {
118         registerTestService<IComponentStore>();
119     }
120 
ComponentTestEnvironment()121     ComponentTestEnvironment() : res("/data/local/tmp/media/") {}
122 
setComponent(const char * _component)123     void setComponent(const char* _component) { component = _component; }
124 
setInstance(const char * _instance)125     void setInstance(const char* _instance) { instance = _instance; }
126 
setRes(const char * _res)127     void setRes(const char* _res) { res = _res; }
128 
getInstance()129     const hidl_string getInstance() const { return instance; }
130 
getComponent()131     const hidl_string getComponent() const { return component; }
132 
getRes()133     const hidl_string getRes() const { return res; }
134 
initFromOptions(int argc,char ** argv)135     int initFromOptions(int argc, char** argv) {
136         static struct option options[] = {
137             {"instance", required_argument, 0, 'I'},
138             {"component", required_argument, 0, 'C'},
139             {"res", required_argument, 0, 'P'},
140             {0, 0, 0, 0}};
141 
142         while (true) {
143             int index = 0;
144             int c = getopt_long(argc, argv, "I:C:P:", options, &index);
145             if (c == -1) {
146                 break;
147             }
148 
149             switch (c) {
150                 case 'I':
151                     setInstance(optarg);
152                     break;
153                 case 'C':
154                     setComponent(optarg);
155                     break;
156                 case 'P':
157                     setRes(optarg);
158                     break;
159                 case '?':
160                     break;
161             }
162         }
163 
164         if (optind < argc) {
165             fprintf(stderr,
166                     "unrecognized option: %s\n\n"
167                     "usage: %s <gtest options> <test options>\n\n"
168                     "test options are:\n\n"
169                     "-I, --instance: software for C2 components, else default\n"
170                     "-C, --component: C2 component to test\n"
171                     "-P, --res: Resource files directory location\n",
172                     argv[optind ?: 1], argv[0]);
173             return 2;
174         }
175         return 0;
176     }
177 
178    private:
179     hidl_string instance;
180     hidl_string component;
181     hidl_string res;
182 };
183 
184 /*
185  * common functions declarations
186  */
187 void testInputBuffer(
188     const std::shared_ptr<android::Codec2Client::Component>& component,
189     std::mutex& queueLock, std::list<std::unique_ptr<C2Work>>& workQueue,
190     uint32_t flags, bool isNullBuffer);
191 
192 void waitOnInputConsumption(std::mutex& queueLock,
193                             std::condition_variable& queueCondition,
194                             std::list<std::unique_ptr<C2Work>>& workQueue,
195                             size_t bufferCount = MAX_INPUT_BUFFERS);
196 
197 void workDone(
198     const std::shared_ptr<android::Codec2Client::Component>& component,
199     std::unique_ptr<C2Work>& work, std::list<uint64_t>& flushedIndices,
200     std::mutex& queueLock, std::condition_variable& queueCondition,
201     std::list<std::unique_ptr<C2Work>>& workQueue, bool& eos, bool& csd,
202     uint32_t& framesReceived);
203 
204 int64_t getNowUs();
205 
206 #endif  // MEDIA_C2_HIDL_TEST_COMMON_H
207