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 FRAME_WIDTH 2000
18 #define FRAME_HEIGHT 2000
19 #define FRAME_RATE (30 << 16)
20 #define BUFFER_SIZE 12
21 #define BUFFER_COUNT 2
22 #define EMPTY_BUFFER_DONE_CALLBACK_TIMEOUT_SEC 30
23 extern int numCallbackEmptyBufferDone;
24 sp<IAllocator> mAllocator = IAllocator::getService("ashmem");
allocateHidlPortBuffers(OMX_U32 portIndex,Vector<Buffer> * buffers,int BufferSize)25 int allocateHidlPortBuffers(OMX_U32 portIndex, Vector<Buffer> *buffers,
26 int BufferSize) {
27 buffers->clear();
28 OMX_PARAM_PORTDEFINITIONTYPE def;
29 int err = omxUtilsGetParameter(portIndex, &def);
30 omxExitOnError(err);
31 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
32 Buffer buffer;
33 buffer.mFlags = 0;
34 bool success;
35 auto transStatus = mAllocator->allocate(BufferSize, [&success, &buffer](
36 bool s,
37 hidl_memory const& m) {
38 success = s;
39 buffer.mHidlMemory = m;
40 });
41 omxExitOnError(!transStatus.isOk());
42 omxExitOnError(!success);
43 buffers->push(buffer);
44 }
45 return OK;
46 }
main()47 int main() {
48 status_t err;
49 /* Initialize OMX for the specified codec */
50 status_t ret = omxUtilsInit((char *) "OMX.google.h264.encoder");
51 omxExitOnError(ret);
52 int allCallbacksReceivedEmptyBufferDone = 0;
53 /* Get OMX input port parameters */
54 OMX_PARAM_PORTDEFINITIONTYPE *params =
55 (OMX_PARAM_PORTDEFINITIONTYPE *) malloc(
56 sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
57 params->nPortIndex = OMX_UTILS_IP_PORT;
58 params->format.video.nFrameWidth = FRAME_WIDTH;
59 params->format.video.nFrameHeight = FRAME_HEIGHT;
60 params->format.video.xFramerate = FRAME_RATE;
61 params->format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
62 params->format.video.eColorFormat = OMX_COLOR_FormatAndroidOpaque;
63 params->nBufferSize = BUFFER_SIZE;
64 params->nBufferCountActual = params->nBufferCountMin = BUFFER_COUNT;
65 err = omxUtilsSetParameter(OMX_UTILS_IP_PORT, params);
66 memset(params, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
67 err = omxUtilsGetParameter(OMX_UTILS_IP_PORT, params);
68 /* prepare input port buffers */
69 int inMemSize = params->nBufferCountActual * params->nBufferSize;
70 int inBufferCnt = params->nBufferCountActual;
71 int inBufferSize = inMemSize / inBufferCnt;
72 IOMX::buffer_id *inBufferId = new IOMX::buffer_id[inBufferCnt];
73 /* Get OMX output port parameters */
74 omxUtilsGetParameter(OMX_UTILS_OP_PORT, params);
75 /* prepare output port buffers */
76 int outMemSize = params->nBufferCountActual * params->nBufferSize;
77 int outBufferCnt = params->nBufferCountActual;
78 int outBufferSize = outMemSize / outBufferCnt;
79 IOMX::buffer_id *outBufferId = new IOMX::buffer_id[outBufferCnt];
80 Vector < Buffer > inputBuffers;
81 Vector < Buffer > outputBuffers;
82 /* Register input buffers with OMX component */
83 allocateHidlPortBuffers(OMX_UTILS_IP_PORT, &inputBuffers, inBufferSize);
84 for (int i = 0; i < inBufferCnt; i++) {
85 inBufferId[i] = inputBuffers[i].mID;
86 err = omxUtilsUseBuffer(OMX_UTILS_IP_PORT, inputBuffers[i].mHidlMemory,
87 &inBufferId[i]);
88 }
89 /* Register output buffers with OMX component */
90 allocateHidlPortBuffers(OMX_UTILS_OP_PORT, &outputBuffers, outBufferSize);
91 for (int i = 0; i < outBufferCnt; i++) {
92 outBufferId[i] = outputBuffers[i].mID;
93 err = omxUtilsUseBuffer(OMX_UTILS_OP_PORT, outputBuffers[i].mHidlMemory,
94 &outBufferId[i]);
95 }
96 /* Do OMX State change to Idle */
97 err = omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
98 /* Do OMX State change to Executing */
99 err = omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateExecuting);
100 for (int i = 0; i < inBufferCnt; i++) {
101 OMXBuffer omxBuf(0, inBufferSize);
102 err = omxUtilsEmptyBuffer(inBufferId[i], omxBuf, 0, 0, -1);
103 }
104 for (int i = 0; i < outBufferCnt; i++) {
105 OMXBuffer omxBuf(0, outBufferSize);
106 err = omxUtilsFillBuffer(outBufferId[i], omxBuf, -1);
107 }
108 /* Do OMX State change to Idle */
109 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
110 time_t currentTime = time(NULL);
111 time_t waitTimeInSeconds = EMPTY_BUFFER_DONE_CALLBACK_TIMEOUT_SEC;
112 time_t endTime = currentTime + waitTimeInSeconds;
113 while (currentTime < endTime) {
114 if (numCallbackEmptyBufferDone == inBufferCnt) {
115 allCallbacksReceivedEmptyBufferDone = 1;
116 break;
117 }
118 currentTime = time(NULL);
119 }
120 if (!allCallbacksReceivedEmptyBufferDone) {
121 ALOGE("Exiting the app");
122 exit (EXIT_FAILURE);
123 }
124 /* Free input and output buffers */
125 for (int i = 0; i < inBufferCnt; i++) {
126 omxUtilsFreeBuffer(OMX_UTILS_IP_PORT, inBufferId[i]);
127 }
128 for (int i = 0; i < outBufferCnt; i++) {
129 omxUtilsFreeBuffer(OMX_UTILS_OP_PORT, outBufferId[i]);
130 }
131 /* Free OMX resources */
132 omxUtilsFreeNode();
133 return EXIT_SUCCESS;
134 }
135