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 <hardware/google/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 ::hardware::google::media::c2::V1_0;
32 using namespace ::hardware::google::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,
59         size_t numDiscardedInputBuffers) override {
60         /* TODO */
61         ALOGD("onWorkDone called");
62         (void)comp;
63         (void)numDiscardedInputBuffers;
64         if (callBack) callBack(workItems);
65     }
66 
onTrippedCodecListener67     virtual void onTripped(
68         const std::weak_ptr<android::Codec2Client::Component>& comp,
69         const std::vector<std::shared_ptr<C2SettingResult>>& settingResults)
70         override {
71         /* TODO */
72         (void)comp;
73         (void)settingResults;
74     }
75 
onErrorCodecListener76     virtual void onError(
77         const std::weak_ptr<android::Codec2Client::Component>& comp,
78         uint32_t errorCode) override {
79         /* TODO */
80         (void)comp;
81         ALOGD("onError called");
82         if (errorCode != 0) ALOGE("Error : %u", errorCode);
83     }
84 
onDeathCodecListener85     virtual void onDeath(
86         const std::weak_ptr<android::Codec2Client::Component>& comp) override {
87         /* TODO */
88         (void)comp;
89     }
90 
onInputBufferDoneCodecListener91     virtual void onInputBufferDone(
92         const std::shared_ptr<C2Buffer>& buffer) override {
93         /* TODO */
94         (void)buffer;
95     }
96 
onFramesRenderedCodecListener97     virtual void onFramesRendered(
98         const std::vector<RenderedFrame>& renderedFrames) override {
99         /* TODO */
100         (void)renderedFrames;
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 // A class for test environment setup
109 class ComponentTestEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
110    private:
111     typedef ::testing::VtsHalHidlTargetTestEnvBase Super;
112 
113    public:
registerTestServices()114     virtual void registerTestServices() override {
115         registerTestService<IComponentStore>();
116     }
117 
ComponentTestEnvironment()118     ComponentTestEnvironment() : res("/sdcard/media/") {}
119 
setComponent(const char * _component)120     void setComponent(const char* _component) { component = _component; }
121 
setInstance(const char * _instance)122     void setInstance(const char* _instance) { instance = _instance; }
123 
setRes(const char * _res)124     void setRes(const char* _res) { res = _res; }
125 
getInstance()126     const hidl_string getInstance() const { return instance; }
127 
getComponent()128     const hidl_string getComponent() const { return component; }
129 
getRes()130     const hidl_string getRes() const { return res; }
131 
initFromOptions(int argc,char ** argv)132     int initFromOptions(int argc, char** argv) {
133         static struct option options[] = {
134             {"instance", required_argument, 0, 'I'},
135             {"component", required_argument, 0, 'C'},
136             {"res", required_argument, 0, 'P'},
137             {0, 0, 0, 0}};
138 
139         while (true) {
140             int index = 0;
141             int c = getopt_long(argc, argv, "I:C:P:", options, &index);
142             if (c == -1) {
143                 break;
144             }
145 
146             switch (c) {
147                 case 'I':
148                     setInstance(optarg);
149                     break;
150                 case 'C':
151                     setComponent(optarg);
152                     break;
153                 case 'P':
154                     setRes(optarg);
155                     break;
156                 case '?':
157                     break;
158             }
159         }
160 
161         if (optind < argc) {
162             fprintf(stderr,
163                     "unrecognized option: %s\n\n"
164                     "usage: %s <gtest options> <test options>\n\n"
165                     "test options are:\n\n"
166                     "-I, --instance: software for C2 components, else default\n"
167                     "-C, --component: C2 component to test\n"
168                     "-P, --res: Resource files directory location\n",
169                     argv[optind ?: 1], argv[0]);
170             return 2;
171         }
172         return 0;
173     }
174 
175    private:
176     hidl_string instance;
177     hidl_string component;
178     hidl_string res;
179 };
180 
181 /*
182  * common functions declarations
183  */
184 void testInputBuffer(
185     const std::shared_ptr<android::Codec2Client::Component>& component,
186     std::mutex& queueLock, std::list<std::unique_ptr<C2Work>>& workQueue,
187     uint32_t flags, bool isNullBuffer);
188 
189 void waitOnInputConsumption(std::mutex& queueLock,
190                             std::condition_variable& queueCondition,
191                             std::list<std::unique_ptr<C2Work>>& workQueue,
192                             size_t bufferCount = MAX_INPUT_BUFFERS);
193 
194 void workDone(
195     const std::shared_ptr<android::Codec2Client::Component>& component,
196     std::unique_ptr<C2Work>& work, std::list<uint64_t>& flushedIndices,
197     std::mutex& queueLock, std::condition_variable& queueCondition,
198     std::list<std::unique_ptr<C2Work>>& workQueue, bool& eos, bool& csd,
199     uint32_t& framesReceived);
200 
201 #endif  // MEDIA_C2_HIDL_TEST_COMMON_H
202