1
2/*
3 * Copyright (C) Texas Instruments - http://www.ti.com/
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21/* ==============================================================================
22*             Texas Instruments OMAP (TM) Platform Software
23*  (c) Copyright Texas Instruments, Incorporated.  All Rights Reserved.
24*
25*  Use of this software is controlled by the terms and conditions found
26*  in the license agreement under which this software has been supplied.
27* ============================================================================ */
28/**
29* @file OMX_AacEnc_CompThread.c
30*
31* This file implements OMX Component for AAC encoder that
32* is fully compliant with the OMX Audio specification 1.0.
33*
34* @path  $(CSLPATH)\
35*
36* @rev  1.0
37*/
38/* ----------------------------------------------------------------------------
39*!
40*! Revision History
41*! ===================================
42*! 13-Dec-2005 mf:  Initial Version. Change required per OMAPSWxxxxxxxxx
43*! to provide _________________.
44*!
45* ============================================================================= */
46
47
48/* ------compilation control switches -------------------------*/
49/****************************************************************
50*  INCLUDE FILES
51****************************************************************/
52/* ----- system and platform files ----------------------------*/
53#ifdef UNDER_CE
54#include <windows.h>
55#else
56#include <wchar.h>
57#include <dbapi.h>
58#include <unistd.h>
59#include <sys/time.h>
60#include <sys/types.h>
61#include <sys/ioctl.h>
62#include <sys/select.h>
63#include <string.h>
64#include <fcntl.h>
65#include <errno.h>
66#include <stdlib.h>
67#include <stdio.h>
68#include <signal.h>
69#endif
70#include "OMX_AacEncoder.h"
71#include "OMX_AacEnc_Utils.h"
72#include "OMX_AacEnc_CompThread.h"
73
74
75void* AACENC_ComponentThread (void* pThreadData)
76{
77    int status;
78    struct timespec tv;
79    int fdmax;
80    int ret = 0;
81    fd_set rfds;
82    OMX_U32 nRet;
83    OMX_ERRORTYPE eError = OMX_ErrorNone;
84    OMX_BUFFERHEADERTYPE *pBufHeader;
85
86    /* Recover the pointer to my component specific data */
87    AACENC_COMPONENT_PRIVATE* pComponentPrivate = (AACENC_COMPONENT_PRIVATE*)pThreadData;
88    OMX_COMPONENTTYPE *pHandle = pComponentPrivate->pHandle;
89
90
91#ifdef __PERF_INSTRUMENTATION__
92    pComponentPrivate->pPERFcomp = PERF_Create(PERF_FOURCC('A', 'A', 'C', 'E'),
93                                                   PERF_ModuleComponent |
94                                                   PERF_ModuleAudioDecode);
95#endif
96
97    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering ComponentThread\n", __LINE__);
98    fdmax = pComponentPrivate->cmdPipe[0];
99
100    if (pComponentPrivate->dataPipe[0] > fdmax)
101        fdmax = pComponentPrivate->dataPipe[0];
102
103    while (1)
104    {
105        FD_ZERO (&rfds);
106        FD_SET (pComponentPrivate->cmdPipe[0], &rfds);
107        FD_SET (pComponentPrivate->dataPipe[0], &rfds);
108        tv.tv_sec = 1;
109        tv.tv_nsec = 0;
110
111#ifndef UNDER_CE
112        sigset_t set;
113        sigemptyset (&set);
114        sigaddset (&set, SIGALRM);
115        status = pselect (fdmax+1, &rfds, NULL, NULL, &tv, &set);
116#else
117        status = select (fdmax+1, &rfds, NULL, NULL, &tv);
118#endif
119
120        if (pComponentPrivate->bIsThreadstop == 1) {
121            OMX_ERROR4(pComponentPrivate->dbg, ":: Comp Thrd Exiting here...\n");
122            goto EXIT;
123        }
124
125        if (status == 0)
126        {
127
128            OMX_PRINT1(pComponentPrivate->dbg, "%d : bIsStopping = %ld\n",__LINE__, pComponentPrivate->bIsStopping);
129            OMX_PRINT1(pComponentPrivate->dbg, "%d : lcml_nOpBuf = %ld\n",__LINE__, pComponentPrivate->lcml_nOpBuf);
130            OMX_PRINT1(pComponentPrivate->dbg, "%d : lcml_nIpBuf = %ld\n",__LINE__, pComponentPrivate->lcml_nIpBuf);
131
132            if (pComponentPrivate->bIsThreadstop == 1)
133            {
134                OMX_PRINT1(pComponentPrivate->dbg, "%d  :: OMX_AACENC_ComponentThread \n",__LINE__);
135                pComponentPrivate->bIsStopping = 0;
136                pComponentPrivate->bIsThreadstop = 0;
137                pComponentPrivate->lcml_nOpBuf = 0;
138                pComponentPrivate->lcml_nIpBuf = 0;
139                pComponentPrivate->app_nBuf = 0;            /* NOT USED */
140                pComponentPrivate->num_Op_Issued = 0;
141                pComponentPrivate->num_Sent_Ip_Buff = 0;
142                pComponentPrivate->num_Reclaimed_Op_Buff = 0;
143                pComponentPrivate->bIsEOFSent = 0;
144                OMX_PRINT1(pComponentPrivate->dbg, "%d :: OMX_AACENC_ComponentThread \n",__LINE__);
145                if (pComponentPrivate->curState != OMX_StateIdle)
146                {
147                    OMX_PRINT1(pComponentPrivate->dbg, "%d ::OMX_AACENC_ComponentThread \n",__LINE__);
148                    goto EXIT;
149                }
150             }
151             OMX_PRINT2(pComponentPrivate->dbg, "%d :: Component Time Out !!!!! \n",__LINE__);
152        }
153        else if(status == -1)
154        {
155            OMX_ERROR2(pComponentPrivate->dbg, "%d :: Error in Select\n", __LINE__);
156            pComponentPrivate->cbInfo.EventHandler (pHandle, pHandle->pApplicationPrivate,
157                                                    OMX_EventError,
158                                                    OMX_ErrorInsufficientResources,
159                                                    OMX_TI_ErrorSevere,
160                                                    "Error from Component Thread in select");
161            eError = OMX_ErrorInsufficientResources;
162        }
163
164        else if ((FD_ISSET (pComponentPrivate->dataPipe[0], &rfds)) && (pComponentPrivate->curState != OMX_StatePause))
165        {
166            OMX_PRCOMM2(pComponentPrivate->dbg, "%d :: DATA pipe is set in Component Thread\n",__LINE__);
167            OMX_PRDSP2(pComponentPrivate->dbg, "%d :: pHandle: %p \n",__LINE__, pHandle);
168            OMX_PRDSP1(pComponentPrivate->dbg, "%d :: pHandle->pComponentPrivate:%p \n",__LINE__, pHandle->pComponentPrivate);
169
170            OMX_PRDSP1(pComponentPrivate->dbg, "%d :: pComponentPrivate:%p \n",__LINE__, pComponentPrivate);
171            pBufHeader = NULL;
172            ret = read(pComponentPrivate->dataPipe[0], &pBufHeader, sizeof(pBufHeader));
173            if (ret == -1)
174            {
175                OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error while reading from the pipe\n",__LINE__);
176                eError = OMX_ErrorHardware;
177                goto EXIT;
178            }
179            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBufHeader:%p \n",__LINE__, pBufHeader);
180            eError = AACENCHandleDataBuf_FromApp(pBufHeader,pComponentPrivate);
181            if (eError != OMX_ErrorNone)
182            {
183                OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error From AACENCHandleDataBuf_FromApp\n",__LINE__);
184                break;
185            }
186
187        }
188
189
190
191        else if(FD_ISSET (pComponentPrivate->cmdPipe[0], &rfds))
192        {
193            OMX_PRDSP2(pComponentPrivate->dbg, "%d :: pHandle: %p \n",__LINE__,pHandle);
194            /* Do not accept any command when the component is stopping */
195            OMX_PRCOMM2(pComponentPrivate->dbg, "%d :: CMD pipe is set in Component Thread\n",__LINE__);
196            nRet = AACENCHandleCommand (pComponentPrivate);
197            if (nRet == EXIT_COMPONENT_THRD)
198            {
199                OMX_ERROR2(pComponentPrivate->dbg, " %d :: Exiting from Component thread\n",__LINE__);
200
201                AACENC_CleanupInitParams(pHandle);
202                if(eError != OMX_ErrorNone)
203                {
204                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: AACENC_CleanupInitParams returned error\n",__LINE__);
205                    goto EXIT;
206                }
207                OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: ARM Side Resources Have Been Freed\n",__LINE__);
208
209                pComponentPrivate->curState = OMX_StateLoaded;
210
211#ifdef __PERF_INSTRUMENTATION__
212                PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryComplete | PERF_BoundaryCleanup);
213#endif
214
215                if(pComponentPrivate->bPreempted==0){
216                    if (RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
217                        return OMX_ErrorUndefined;
218                    }
219                    pComponentPrivate->cbInfo.EventHandler(pHandle,
220                                                           pHandle->pApplicationPrivate,
221                                                           OMX_EventCmdComplete,
222                                                           OMX_CommandStateSet,
223                                                           pComponentPrivate->curState,
224                                                           NULL);
225
226                }
227                else{
228                    pComponentPrivate->cbInfo.EventHandler(pHandle,
229                                                           pHandle->pApplicationPrivate,
230                                                           OMX_EventError,
231                                                           OMX_ErrorResourcesLost,
232                                                           OMX_TI_ErrorMajor,
233                                                           NULL);
234                    pComponentPrivate->bPreempted = 0;
235                }
236
237                pComponentPrivate->bLoadedCommandPending = OMX_FALSE;
238                goto EXIT;
239            }
240
241        }
242    }
243
244EXIT:
245
246#ifdef __PERF_INSTRUMENTATION__
247    PERF_Done(pComponentPrivate->pPERFcomp);
248#endif
249    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting ComponentThread\n", __LINE__);
250    return (void*)eError;
251}
252
253