summaryrefslogtreecommitdiffstats
blob: de043b2c6692fa87bb67f63703efcff3674de444 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MEDIA_HIDL_TEST_COMMON_H
#define MEDIA_HIDL_TEST_COMMON_H

#ifdef __LP64__
#define OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS
#endif

#include <media/stagefright/foundation/ALooper.h>
#include <utils/Condition.h>
#include <utils/List.h>
#include <utils/Mutex.h>

#include <media/openmax/OMX_Index.h>
#include <media/openmax/OMX_Core.h>
#include <media/openmax/OMX_Component.h>
#include <media/openmax/OMX_IndexExt.h>
#include <media/openmax/OMX_AudioExt.h>
#include <media/openmax/OMX_VideoExt.h>

/* TIME OUTS (Wait time in dequeueMessage()) */

/* As component is switching states (loaded<->idle<->execute), dequeueMessage()
 * expects the events to be received within this duration */
#define DEFAULT_TIMEOUT 100000
/* Time interval between successive Input/Output enqueues */
#define DEFAULT_TIMEOUT_Q 2000
/* While the component is amidst a process call, asynchronous commands like
 * flush, change states can get delayed (at max by process call time). Instead
 * of waiting on DEFAULT_TIMEOUT, we give an additional leeway. */
#define DEFAULT_TIMEOUT_PE 500000

/* Breakout Timeout :: 5 sec*/
#define TIMEOUT_COUNTER_Q (5000000 / DEFAULT_TIMEOUT_Q)
#define TIMEOUT_COUNTER_PE (5000000 / DEFAULT_TIMEOUT_PE)

/*
 * Random Index used for monkey testing while get/set parameters
 */
#define RANDOM_INDEX 1729

#define ALIGN_POWER_OF_TWO(value, n) \
    (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))

enum bufferOwner {
    client,
    component,
    unknown,
};

/*
 * TODO: below definitions are borrowed from Conversion.h.
 * This is not the ideal way to do it. Loose these definitions once you
 * include Conversion.h
 */
inline uint32_t toRawIndexType(OMX_INDEXTYPE l) {
    return static_cast<uint32_t>(l);
}

inline android::hardware::media::omx::V1_0::Status toStatus(
    android::status_t l) {
    return static_cast<android::hardware::media::omx::V1_0::Status>(l);
}

inline hidl_vec<uint8_t> inHidlBytes(void const* l, size_t size) {
    hidl_vec<uint8_t> t;
    t.setToExternal(static_cast<uint8_t*>(const_cast<void*>(l)), size, false);
    return t;
}

inline uint32_t toRawCommandType(OMX_COMMANDTYPE l) {
    return static_cast<uint32_t>(l);
}

/*
 * struct definitions
 */
struct BufferInfo {
    uint32_t id;
    bufferOwner owner;
    android::hardware::media::omx::V1_0::CodecBuffer omxBuffer;
    ::android::sp<IMemory> mMemory;
    int32_t slot;
};

struct FrameData {
    int bytesCount;
    uint32_t flags;
    uint32_t timestamp;
};

/*
 * Handle Callback functions EmptythisBuffer(), FillthisBuffer(),
 * EventHandler()
 */
struct CodecObserver : public IOmxObserver {
   public:
    CodecObserver(std::function<void(Message, const BufferInfo*)> fn)
        : callBack(fn) {}
    Return<void> onMessages(const hidl_vec<Message>& messages) override {
        android::Mutex::Autolock autoLock(msgLock);
        for (hidl_vec<Message>::const_iterator it = messages.begin();
             it != messages.end(); ++it) {
            msgQueue.push_back(*it);
        }
        msgCondition.signal();
        return Void();
    }
    android::hardware::media::omx::V1_0::Status dequeueMessage(
        Message* msg, int64_t timeoutUs,
        android::Vector<BufferInfo>* iBuffers = nullptr,
        android::Vector<BufferInfo>* oBuffers = nullptr) {
        int64_t finishBy = android::ALooper::GetNowUs() + timeoutUs;
        for (;;) {
            android::Mutex::Autolock autoLock(msgLock);
            android::List<Message>::iterator it = msgQueue.begin();
            while (it != msgQueue.end()) {
                if (it->type ==
                    android::hardware::media::omx::V1_0::Message::Type::EVENT) {
                    *msg = *it;
                    if (callBack) callBack(*it, nullptr);
                    it = msgQueue.erase(it);
                    // OMX_EventBufferFlag event is sent when the component has
                    // processed a buffer with its EOS flag set. This event is
                    // not sent by soft omx components. Vendor components can
                    // send this. From IOMX point of view, we will ignore this
                    // event.
                    if (msg->data.eventData.event == OMX_EventBufferFlag)
                        continue;
                    return ::android::hardware::media::omx::V1_0::Status::OK;
                } else if (it->type == android::hardware::media::omx::V1_0::
                                           Message::Type::FILL_BUFFER_DONE) {
                    if (oBuffers) {
                        size_t i;
                        for (i = 0; i < oBuffers->size(); ++i) {
                            if ((*oBuffers)[i].id ==
                                it->data.bufferData.buffer) {
                                if (callBack) callBack(*it, &(*oBuffers)[i]);
                                oBuffers->editItemAt(i).owner = client;
                                it = msgQueue.erase(it);
                                break;
                            }
                        }
                        EXPECT_LE(i, oBuffers->size());
                    }
                } else if (it->type == android::hardware::media::omx::V1_0::
                                           Message::Type::EMPTY_BUFFER_DONE) {
                    if (iBuffers) {
                        size_t i;
                        for (i = 0; i < iBuffers->size(); ++i) {
                            if ((*iBuffers)[i].id ==
                                it->data.bufferData.buffer) {
                                if (callBack) callBack(*it, &(*iBuffers)[i]);
                                iBuffers->editItemAt(i).owner = client;
                                it = msgQueue.erase(it);
                                break;
                            }
                        }
                        EXPECT_LE(i, iBuffers->size());
                    }
                } else {
                    EXPECT_TRUE(false) << "Received unexpected message";
                    ++it;
                }
            }
            int64_t delayUs = finishBy - android::ALooper::GetNowUs();
            if (delayUs < 0) return toStatus(android::TIMED_OUT);
            (timeoutUs < 0)
                ? msgCondition.wait(msgLock)
                : msgCondition.waitRelative(msgLock, delayUs * 1000ll);
        }
    }

    android::List<Message> msgQueue;
    android::Mutex msgLock;
    android::Condition msgCondition;
    std::function<void(Message, const BufferInfo*)> callBack;
};

/*
 * Useful Wrapper utilities
 */
template <class T>
void InitOMXParams(T* params) {
    params->nSize = sizeof(T);
    params->nVersion.s.nVersionMajor = 1;
    params->nVersion.s.nVersionMinor = 0;
    params->nVersion.s.nRevision = 0;
    params->nVersion.s.nStep = 0;
}

template <class T>
Return<android::hardware::media::omx::V1_0::Status> getParam(
    sp<IOmxNode> omxNode, OMX_INDEXTYPE omxIdx, T* params) {
    android::hardware::media::omx::V1_0::Status status;
    InitOMXParams(params);
    omxNode->getParameter(
        toRawIndexType(omxIdx), inHidlBytes(params, sizeof(*params)),
        [&status, &params](android::hardware::media::omx::V1_0::Status _s,
                           hidl_vec<uint8_t> const& outParams) {
            status = _s;
            std::copy(outParams.data(), outParams.data() + outParams.size(),
                      static_cast<uint8_t*>(static_cast<void*>(params)));
        });
    return status;
}

template <class T>
Return<android::hardware::media::omx::V1_0::Status> setParam(
    sp<IOmxNode> omxNode, OMX_INDEXTYPE omxIdx, T* params) {
    InitOMXParams(params);
    return omxNode->setParameter(toRawIndexType(omxIdx),
                                 inHidlBytes(params, sizeof(*params)));
}

template <class T>
Return<android::hardware::media::omx::V1_0::Status> getPortParam(
    sp<IOmxNode> omxNode, OMX_INDEXTYPE omxIdx, OMX_U32 nPortIndex, T* params) {
    android::hardware::media::omx::V1_0::Status status;
    InitOMXParams(params);
    params->nPortIndex = nPortIndex;
    omxNode->getParameter(
        toRawIndexType(omxIdx), inHidlBytes(params, sizeof(*params)),
        [&status, &params](android::hardware::media::omx::V1_0::Status _s,
                           hidl_vec<uint8_t> const& outParams) {
            status = _s;
            std::copy(outParams.data(), outParams.data() + outParams.size(),
                      static_cast<uint8_t*>(static_cast<void*>(params)));
        });
    return status;
}

template <class T>
Return<android::hardware::media::omx::V1_0::Status> setPortParam(
    sp<IOmxNode> omxNode, OMX_INDEXTYPE omxIdx, OMX_U32 nPortIndex, T* params) {
    InitOMXParams(params);
    params->nPortIndex = nPortIndex;
    return omxNode->setParameter(toRawIndexType(omxIdx),
                                 inHidlBytes(params, sizeof(*params)));
}

template <class T>
Return<android::hardware::media::omx::V1_0::Status> getPortConfig(
    sp<IOmxNode> omxNode, OMX_INDEXTYPE omxIdx, OMX_U32 nPortIndex, T* params) {
    android::hardware::media::omx::V1_0::Status status;
    InitOMXParams(params);
    params->nPortIndex = nPortIndex;
    omxNode->getConfig(
        toRawIndexType(omxIdx), inHidlBytes(params, sizeof(*params)),
        [&status, &params](android::hardware::media::omx::V1_0::Status _s,
                           hidl_vec<uint8_t> const& outParams) {
            status = _s;
            std::copy(outParams.data(), outParams.data() + outParams.size(),
                      static_cast<uint8_t*>(static_cast<void*>(params)));
        });
    return status;
}

template <class T>
Return<android::hardware::media::omx::V1_0::Status> setPortConfig(
    sp<IOmxNode> omxNode, OMX_INDEXTYPE omxIdx, OMX_U32 nPortIndex, T* params) {
    InitOMXParams(params);
    params->nPortIndex = nPortIndex;
    return omxNode->setConfig(toRawIndexType(omxIdx),
                              inHidlBytes(params, sizeof(*params)));
}

/*
 * common functions declarations
 */
Return<android::hardware::media::omx::V1_0::Status> setRole(
    sp<IOmxNode> omxNode, const char* role);

Return<android::hardware::media::omx::V1_0::Status> setPortBufferSize(
    sp<IOmxNode> omxNode, OMX_U32 portIndex, OMX_U32 size);

Return<android::hardware::media::omx::V1_0::Status> setVideoPortFormat(
    sp<IOmxNode> omxNode, OMX_U32 portIndex,
    OMX_VIDEO_CODINGTYPE eCompressionFormat, OMX_COLOR_FORMATTYPE eColorFormat,
    OMX_U32 xFramerate);

Return<android::hardware::media::omx::V1_0::Status> setAudioPortFormat(
    sp<IOmxNode> omxNode, OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE eEncoding);

void allocateBuffer(sp<IOmxNode> omxNode, BufferInfo* buffer, OMX_U32 portIndex,
                    OMX_U32 nBufferSize, PortMode portMode);

void allocatePortBuffers(sp<IOmxNode> omxNode,
                         android::Vector<BufferInfo>* buffArray,
                         OMX_U32 portIndex,
                         PortMode portMode = PortMode::PRESET_BYTE_BUFFER,
                         bool allocGrap = false);

void changeStateLoadedtoIdle(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
                             android::Vector<BufferInfo>* iBuffer,
                             android::Vector<BufferInfo>* oBuffer,
                             OMX_U32 kPortIndexInput, OMX_U32 kPortIndexOutput,
                             PortMode* portMode = nullptr,
                             bool allocGrap = false);

void changeStateIdletoLoaded(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
                             android::Vector<BufferInfo>* iBuffer,
                             android::Vector<BufferInfo>* oBuffer,
                             OMX_U32 kPortIndexInput, OMX_U32 kPortIndexOutput);

void changeStateIdletoExecute(sp<IOmxNode> omxNode, sp<CodecObserver> observer);

void changeStateExecutetoIdle(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
                              android::Vector<BufferInfo>* iBuffer,
                              android::Vector<BufferInfo>* oBuffer);

size_t getEmptyBufferID(android::Vector<BufferInfo>* buffArray);

void dispatchOutputBuffer(sp<IOmxNode> omxNode,
                          android::Vector<BufferInfo>* buffArray,
                          size_t bufferIndex,
                          PortMode portMode = PortMode::PRESET_BYTE_BUFFER);

void dispatchInputBuffer(sp<IOmxNode> omxNode,
                         android::Vector<BufferInfo>* buffArray,
                         size_t bufferIndex, int bytesCount, uint32_t flags,
                         uint64_t timestamp,
                         PortMode portMode = PortMode::PRESET_BYTE_BUFFER);

void flushPorts(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
                android::Vector<BufferInfo>* iBuffer,
                android::Vector<BufferInfo>* oBuffer, OMX_U32 kPortIndexInput,
                OMX_U32 kPortIndexOutput,
                int64_t timeoutUs = DEFAULT_TIMEOUT_PE);

typedef void (*portreconfig)(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
                             android::Vector<BufferInfo>* iBuffer,
                             android::Vector<BufferInfo>* oBuffer,
                             OMX_U32 kPortIndexInput, OMX_U32 kPortIndexOutput,
                             Message msg, PortMode oPortMode, void* args);
void testEOS(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
             android::Vector<BufferInfo>* iBuffer,
             android::Vector<BufferInfo>* oBuffer, bool signalEOS,
             bool& eosFlag, PortMode* portMode = nullptr,
             portreconfig fptr = nullptr, OMX_U32 kPortIndexInput = 0,
             OMX_U32 kPortIndexOutput = 1, void* args = nullptr);

#endif  // MEDIA_HIDL_TEST_COMMON_H