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_G711Enc_ComponentThread.c
30 *
31 * This file implements G711 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\g711_enc\src
35 *
36 * @rev  1.0
37 */
38/* ----------------------------------------------------------------------------
39 *!
40 *! Revision History
41 *! ===================================
42 *! 12-Dec-2006: Initial Version
43 *! This is newest file
44 * =========================================================================== */
45
46/* ------compilation control switches -------------------------*/
47/****************************************************************
48 *  INCLUDE FILES
49 ****************************************************************/
50/* ----- system and platform files ----------------------------*/
51
52#ifdef UNDER_CE
53#include <windows.h>
54#include <oaf_osal.h>
55#include <omx_core.h>
56#else
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/*-------program files ----------------------------------------*/
71#include <OMX_Component.h>
72
73#include "OMX_G711Enc_Utils.h"
74//#include <encode_common_ti.h>
75//#include "g711enc_sn_uuid.h"
76
77/* ================================================================================= */
78/**
79 * @fn G711ENC_CompThread() Component thread
80 *
81 *  @see         OMX_G711Enc_ComponentThread.h
82 */
83/* ================================================================================ */
84
85void* G711ENC_CompThread(void* pThreadData)
86{
87    OMX_ERRORTYPE eError = OMX_ErrorNone;
88    int status = 0;
89    struct timespec tv;
90    int fdmax = 0;
91    int ret = 0;
92    fd_set rfds;
93    OMX_U32 nRet = 0;
94    OMX_BUFFERHEADERTYPE *pBufHeader = NULL;
95    G711ENC_COMPONENT_PRIVATE* pComponentPrivate = (G711ENC_COMPONENT_PRIVATE*)pThreadData;
96    OMX_COMPONENTTYPE *pHandle = pComponentPrivate->pHandle;
97    G711ENC_DPRINT("%d :: Entering G711ENC_CompThread\n", __LINE__);
98
99    fdmax = pComponentPrivate->cmdPipe[0];
100    if (pComponentPrivate->dataPipe[0] > fdmax) {
101        fdmax = pComponentPrivate->dataPipe[0];
102    }
103
104    while (1) {
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
121        if (0 == status) {
122            if (pComponentPrivate->bIsThreadstop == 1)   {
123                pComponentPrivate->bIsThreadstop = 0;
124                pComponentPrivate->lcml_nOpBuf = 0;
125                pComponentPrivate->lcml_nIpBuf = 0;
126                pComponentPrivate->app_nBuf = 0;
127                pComponentPrivate->num_Op_Issued = 0;
128                pComponentPrivate->num_Sent_Ip_Buff = 0;
129                pComponentPrivate->num_Reclaimed_Op_Buff = 0;
130                pComponentPrivate->bIsEOFSent = 0;
131                if (pComponentPrivate->curState != OMX_StateIdle) {
132                    G711ENC_DPRINT("%d :: pComponentPrivate->curState is not OMX_StateIdle\n",__LINE__);
133                    goto EXIT;
134                }
135            }
136            G711ENC_DPRINT("%d :: Component Time Out !!!!! \n",__LINE__);
137        } else if(-1 == status) {
138            G711ENC_DPRINT("%d :: Error in Select\n", __LINE__);
139            pComponentPrivate->cbInfo.EventHandler ( pHandle,
140                                                     pHandle->pApplicationPrivate,
141                                                     OMX_EventError,
142                                                     OMX_ErrorInsufficientResources,
143                                                     0,
144                                                     "Error from CompThread in select");
145            eError = OMX_ErrorInsufficientResources;
146
147        }
148
149        else if (FD_ISSET (pComponentPrivate->dataPipe[0], &rfds)){
150            G711ENC_DPRINT("%d :: DATA pipe is set in Component Thread\n",__LINE__);
151            ret = read(pComponentPrivate->dataPipe[0], &pBufHeader, sizeof(pBufHeader));
152            if (ret == -1) {
153                G711ENC_DPRINT("%d :: Error while reading from the pipe\n",__LINE__);
154                goto EXIT;
155            }
156            eError = G711ENC_HandleDataBufFromApp(pBufHeader,pComponentPrivate);
157            if (eError != OMX_ErrorNone) {
158                G711ENC_DPRINT("%d :: G711ENC_HandleDataBufFromApp returned error\n",__LINE__);
159                break;
160            }
161        }
162        else if(FD_ISSET (pComponentPrivate->cmdPipe[0], &rfds)) {
163            G711ENC_DPRINT("%d :: CMD pipe is set in Component Thread\n",__LINE__);
164            nRet = G711ENC_HandleCommand(pComponentPrivate);
165            if (nRet == G711ENC_EXIT_COMPONENT_THRD) {
166
167                pComponentPrivate->curState = OMX_StateLoaded;
168                if (pComponentPrivate->bPreempted == 0) {
169                    pComponentPrivate->cbInfo.EventHandler( pHandle,
170                                                            pHandle->pApplicationPrivate,
171                                                            OMX_EventCmdComplete,
172                                                            OMX_ErrorNone,
173                                                            pComponentPrivate->curState,
174                                                            NULL);
175                }
176                else {
177                    pComponentPrivate->cbInfo.EventHandler(pHandle,
178                                                           pHandle->pApplicationPrivate,
179                                                           OMX_EventError,
180                                                           OMX_ErrorResourcesLost,
181                                                           pComponentPrivate->curState,
182                                                           NULL);
183                    pComponentPrivate->bPreempted = 0;
184                }
185            }
186        }
187    }
188 EXIT:
189    G711ENC_DPRINT("%d :: Exiting G711ENC_CompThread\n", __LINE__);
190    G711ENC_DPRINT("%d :: Returning = 0x%x\n",__LINE__,eError);
191    return (void*)eError;
192}
193