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