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_AmrEnc_ComponentThread.c
30*
31* This file implements NBAMR Encoder Component Thread and its functionality
32* that is fully compliant with the Khronos OpenMAX (TM) 1.0 Specification
33*
34* @path  $(CSLPATH)\OMAPSW_MPU\linux\audio\src\openmax_il\nbamr_enc\src
35*
36* @rev  1.0
37*/
38/* ----------------------------------------------------------------------------
39*!
40*! Revision History
41*! ===================================
42*! 21-sept-2006 bk: updated review findings for alpha release
43*! 24-Aug-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests some more
44*! 18-July-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests validated for few cases
45*! 21-Jun-2006 bk: Khronos OpenMAX (TM) 1.0 migration done
46*! 22-May-2006 bk: DASF recording quality improved
47*! 19-Apr-2006 bk: DASF recording speed issue resloved
48*! 23-Feb-2006 bk: DASF functionality added
49*! 18-Jan-2006 bk: Repated recording issue fixed and LCML changes taken care
50*! 14-Dec-2005 bk: Initial Version
51*! 16-Nov-2005 bk: Initial Version
52*! 23-Sept-2005 bk: Initial Version
53*! 10-Sept-2005 bk: Initial Version
54*! 10-Sept-2005 bk:
55*! This is newest file
56* =========================================================================== */
57
58/* ------compilation control switches -------------------------*/
59/****************************************************************
60*  INCLUDE FILES
61****************************************************************/
62/* ----- system and platform files ----------------------------*/
63
64#ifdef UNDER_CE
65#include <windows.h>
66#include <oaf_osal.h>
67#include <omx_core.h>
68#else
69#include <wchar.h>
70#include <dbapi.h>
71#include <unistd.h>
72#include <sys/time.h>
73#include <sys/types.h>
74#include <sys/ioctl.h>
75#include <sys/select.h>
76#include <string.h>
77#include <fcntl.h>
78#include <errno.h>
79#include <stdlib.h>
80#include <stdio.h>
81#include <signal.h>
82#endif
83/*-------program files ----------------------------------------*/
84#include "OMX_AmrEnc_Utils.h"
85#include "OMX_AmrEnc_ComponentThread.h"
86
87/* ================================================================================= */
88/**
89* @fn NBAMRENC_CompThread() Component thread
90*
91*  @see         OMX_AmrEnc_ComponentThread.h
92*/
93/* ================================================================================ */
94
95void* NBAMRENC_CompThread(void* pThreadData)
96{
97    OMX_ERRORTYPE eError = OMX_ErrorNone;
98    int status;
99    struct timespec tv;
100    int fdmax;
101    int ret = 0;
102    fd_set rfds;
103    OMX_U32 nRet;
104    OMX_BUFFERHEADERTYPE *pBufHeader = NULL;
105    AMRENC_COMPONENT_PRIVATE* pComponentPrivate = (AMRENC_COMPONENT_PRIVATE*)pThreadData;
106    OMX_COMPONENTTYPE *pHandle = pComponentPrivate->pHandle;
107    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENC_CompThread\n", __LINE__);
108
109#ifdef __PERF_INSTRUMENTATION__
110    pComponentPrivate->pPERFcomp = PERF_Create(PERF_FOURCC('N', 'B', '_', 'E'),
111                                               PERF_ModuleComponent |
112                                               PERF_ModuleAudioDecode);
113#endif
114
115    fdmax = pComponentPrivate->cmdPipe[0];
116
117    if (pComponentPrivate->dataPipe[0] > fdmax) {
118        fdmax = pComponentPrivate->dataPipe[0];
119    }
120
121    while (1) {
122        FD_ZERO (&rfds);
123        FD_SET (pComponentPrivate->cmdPipe[0], &rfds);
124        FD_SET (pComponentPrivate->dataPipe[0], &rfds);
125        tv.tv_sec = 1;
126        tv.tv_nsec = 0;
127
128#ifndef UNDER_CE
129        sigset_t set;
130        sigemptyset (&set);
131        sigaddset (&set, SIGALRM);
132        status = pselect (fdmax+1, &rfds, NULL, NULL, &tv, &set);
133#else
134        status = select (fdmax+1, &rfds, NULL, NULL, &tv);
135#endif
136
137        if (pComponentPrivate->bIsThreadstop == 1) {
138            OMX_ERROR2(pComponentPrivate->dbg, ":: Comp Thrd Exiting here...\n");
139            goto EXIT;
140        }
141
142        if (0 == status) {
143            OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: bIsThreadstop = %ld\n",__LINE__,pComponentPrivate->bIsThreadstop);
144            OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: lcml_nOpBuf = %ld\n",__LINE__,pComponentPrivate->lcml_nOpBuf);
145            OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: lcml_nIpBuf = %ld\n",__LINE__,pComponentPrivate->lcml_nIpBuf);
146            OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: app_nBuf = %ld\n",__LINE__,pComponentPrivate->app_nBuf);
147            if (pComponentPrivate->bIsThreadstop == 1)  {
148                pComponentPrivate->bIsStopping = 0;
149                pComponentPrivate->bIsThreadstop = 0;
150                pComponentPrivate->lcml_nOpBuf = 0;
151                pComponentPrivate->lcml_nIpBuf = 0;
152                pComponentPrivate->app_nBuf = 0;
153                pComponentPrivate->num_Op_Issued = 0;
154                if (pComponentPrivate->curState != OMX_StateIdle) {
155                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: pComponentPrivate->curState is not OMX_StateIdle\n",__LINE__);
156                    goto EXIT;
157                }
158             }
159             OMX_PRINT1(pComponentPrivate->dbg, "%d :: Component Time Out !!!!! \n",__LINE__);
160        } else if(-1 == status) {
161            OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error in Select\n", __LINE__);
162            pComponentPrivate->cbInfo.EventHandler ( pHandle,
163                                                     pHandle->pApplicationPrivate,
164                                                     OMX_EventError,
165                                                     OMX_ErrorInsufficientResources,
166                                                     OMX_TI_ErrorSevere,
167                                                     "Error from Component Thread in select");
168            eError = OMX_ErrorInsufficientResources;
169
170        } else if ((FD_ISSET (pComponentPrivate->dataPipe[0], &rfds))
171                   && (pComponentPrivate->curState != OMX_StatePause)) {
172            OMX_PRCOMM2(pComponentPrivate->dbg, "%d :: DATA pipe is set in Component Thread\n",__LINE__);
173            ret = read(pComponentPrivate->dataPipe[0], &pBufHeader, sizeof(pBufHeader));
174            if (ret == -1) {
175                OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error while reading from the pipe\n",__LINE__);
176                goto EXIT;
177            }
178            eError = NBAMRENC_HandleDataBufFromApp(pBufHeader,pComponentPrivate);
179            if (eError != OMX_ErrorNone) {
180                OMX_ERROR4(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleDataBufFromApp returned error\n",__LINE__);
181                break;
182            }
183        }
184
185        else if(FD_ISSET (pComponentPrivate->cmdPipe[0], &rfds)) {
186            /* Do not accept any command when the component is stopping */
187            OMX_PRCOMM2(pComponentPrivate->dbg, "%d :: CMD pipe is set in Component Thread\n",__LINE__);
188            nRet = NBAMRENC_HandleCommand(pComponentPrivate);
189            if (nRet == NBAMRENC_EXIT_COMPONENT_THRD) {
190
191                if(eError != OMX_ErrorNone) {
192                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: NBAMRENC_CleanupInitParams returned error\n",__LINE__);
193                    goto EXIT;
194                }
195                pComponentPrivate->curState = OMX_StateLoaded;
196#ifdef __PERF_INSTRUMENTATION__
197                PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryComplete | PERF_BoundaryCleanup);
198#endif
199
200                if(pComponentPrivate->bPreempted==0){
201
202                    if(RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
203                        return OMX_ErrorUndefined;
204                    }
205
206                    pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
207                                                           pComponentPrivate->pHandle->pApplicationPrivate,
208                                                           OMX_EventCmdComplete,
209                                                           OMX_CommandStateSet,
210                                                           pComponentPrivate->curState,
211                                                           NULL);
212
213                }
214                else{
215                    pComponentPrivate->cbInfo.EventHandler( pHandle,
216                                                            pHandle->pApplicationPrivate,
217                                                            OMX_EventError,
218                                                            OMX_ErrorResourcesLost,
219                                                            OMX_TI_ErrorMajor,
220                                                            NULL);
221                    pComponentPrivate->bPreempted = 0;
222                }
223                goto EXIT;
224            }
225        }
226
227    }
228EXIT:
229    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_CompThread\n", __LINE__);
230    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
231#ifdef __PERF_INSTRUMENTATION__
232    PERF_Done(pComponentPrivate->pPERFcomp);
233#endif
234    return (void*)eError;
235}
236