1 /**
2 * Copyright (C) 2020 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 #include "../includes/omxUtils.h"
17 #define TIMESTAMP_MICROSECONDS 0x00010001
18 #define OUT_BUFFER_COUNT 8
19 #define EMPTY_BUFFER_DONE_CALLBACK_TIMEOUT_SEC 30
20 extern int numCallbackEmptyBufferDone;
21 sp<IAllocator> mAllocator = IAllocator::getService("ashmem");
allocateHidlPortBuffers(OMX_U32 portIndex,Vector<Buffer> * buffers)22 int allocateHidlPortBuffers(OMX_U32 portIndex, Vector<Buffer> *buffers) {
23 buffers->clear();
24 OMX_PARAM_PORTDEFINITIONTYPE def;
25 int err = omxUtilsGetParameter(portIndex, &def);
26 omxExitOnError(err);
27 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
28 Buffer buffer;
29 buffer.mFlags = 0;
30 bool success;
31 auto transStatus = mAllocator->allocate(def.nBufferSize,
32 [&success, &buffer](
33 bool s,
34 hidl_memory const& m) {
35 success = s;
36 buffer.mHidlMemory = m;
37 });
38 omxExitOnError(!transStatus.isOk());
39 omxExitOnError(!success);
40 omxUtilsUseBuffer(portIndex, buffer.mHidlMemory, &buffer.mID);
41 buffers->push(buffer);
42 }
43 return OK;
44 }
main()45 int main() {
46 /* Initialize OMX for the specified codec */
47 status_t ret = omxUtilsInit((char *) "OMX.google.h263.decoder");
48 omxExitOnError(ret);
49 int allCallbacksReceivedEmptyBufferDone = 0;
50 /* Get OMX input port parameters */
51 OMX_PARAM_PORTDEFINITIONTYPE *params =
52 (OMX_PARAM_PORTDEFINITIONTYPE *) malloc(
53 sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
54 memset(params, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
55 omxUtilsGetParameter(OMX_UTILS_IP_PORT, params);
56 /* Prepare input port buffers */
57 int inBufferCnt = params->nBufferCountActual;
58 IOMX::buffer_id *inBufferId = new IOMX::buffer_id[inBufferCnt];
59 /* Get OMX output port parameters */
60 memset(params, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
61 omxUtilsGetParameter(OMX_UTILS_OP_PORT, params);
62 params->nBufferCountActual = OUT_BUFFER_COUNT;
63 omxUtilsSetParameter(OMX_UTILS_OP_PORT, params);
64 /* Prepare output port buffers */
65 int outBufferCnt = params->nBufferCountActual;
66 IOMX::buffer_id *outBufferId = new IOMX::buffer_id[outBufferCnt];
67 Vector<Buffer> inputBuffers, outputBuffers;
68 /* Register input buffers with OMX component */
69 allocateHidlPortBuffers(OMX_UTILS_IP_PORT, &inputBuffers);
70 for (int i = 0; i < inBufferCnt; i++) {
71 inBufferId[i] = inputBuffers[i].mID;
72 }
73 /* Register output buffers with OMX component */
74 allocateHidlPortBuffers(OMX_UTILS_OP_PORT, &outputBuffers);
75 for (int i = 0; i < outBufferCnt; i++) {
76 outBufferId[i] = outputBuffers[i].mID;
77 }
78 /* Do OMX State change to Idle */
79 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
80 /* Do OMX State change to Executing */
81 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateExecuting);
82 OMX_U32 flags = OMX_BUFFERFLAG_EOS;
83 int64_t timeUs = TIMESTAMP_MICROSECONDS;
84 for (int i = 0; i < inBufferCnt; i++) {
85 omxUtilsEmptyBuffer(inBufferId[i], OMXBuffer::sPreset, flags, timeUs,
86 -1);
87 }
88 for (int i = 0; i < outBufferCnt; i++) {
89 omxUtilsFillBuffer(outBufferId[i], OMXBuffer::sPreset, -1);
90 }
91 /* Do OMX State change to Idle */
92 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
93 time_t currentTime = time(NULL);
94 time_t waitTimeInSeconds = EMPTY_BUFFER_DONE_CALLBACK_TIMEOUT_SEC;
95 time_t endTime = currentTime + waitTimeInSeconds;
96 while (currentTime < endTime) {
97 if (numCallbackEmptyBufferDone == inBufferCnt) {
98 allCallbacksReceivedEmptyBufferDone = 1;
99 break;
100 }
101 currentTime = time(NULL);
102 }
103 if (!allCallbacksReceivedEmptyBufferDone) {
104 ALOGE("Exiting the app");
105 exit (EXIT_FAILURE);
106 }
107 /* Do OMX State change to Loaded */
108 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateLoaded);
109 /* Free input and output buffers */
110 for (int i = 0; i < inBufferCnt; i++) {
111 omxUtilsFreeBuffer(OMX_UTILS_IP_PORT, inBufferId[i]);
112 }
113 for (int i = 0; i < outBufferCnt; i++) {
114 omxUtilsFreeBuffer(OMX_UTILS_OP_PORT, outBufferId[i]);
115 }
116 /* Free OMX resources */
117 omxUtilsFreeNode();
118 return EXIT_SUCCESS;
119 }
120