G711EncTest.c revision b65f253fdc60f6208a54911bee793d225f23cdf8
1
2/*
3 *  Copyright 2001-2008 Texas Instruments - http://www.ti.com/
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/* =============================================================================
18 *             Texas Instruments OMAP (TM) Platform Software
19 *  (c) Copyright Texas Instruments, Incorporated.  All Rights Reserved.
20 *
21 *  Use of this software is controlled by the terms and conditions found
22 *  in the license agreement under which this software has been supplied.
23 * =========================================================================== */
24/**
25 * @file G711Enc_Test.c
26 *
27 * This file implements G711 Encoder Component Test Application to verify
28 * which is fully compliant with the Khronos OpenMAX (TM) 1.0 Specification
29 *
30 * @path  $(CSLPATH)\OMAPSW_MPU\linux\audio\src\openmax_il\g711_enc\tests
31 *
32 * @rev  1.0
33 */
34/* ----------------------------------------------------------------------------
35 *!
36 *! Revision History
37 *! ===================================
38 *! 12-Dec-2006: Initial Version
39 *! This is newest file
40 * =========================================================================== */
41/* ------compilation control switches -------------------------*/
42/****************************************************************
43 *  INCLUDE FILES
44 ****************************************************************/
45/* ----- system and platform files ----------------------------*/
46
47#include <unistd.h>
48#include <sys/ioctl.h>
49#include <sys/select.h>
50#include <errno.h>
51#include <linux/vt.h>
52#include <signal.h>
53#include <sys/stat.h>
54#include <pthread.h>
55#include <linux/soundcard.h>
56
57#include <string.h>
58#include <fcntl.h>
59#include <stdlib.h>
60#include <stdio.h>
61
62#include <OMX_Index.h>
63#include <OMX_Types.h>
64#include <OMX_Core.h>
65#include <OMX_Audio.h>
66#include <TIDspOmx.h>
67#include <OMX_Component.h>
68
69
70#ifdef OMX_GETTIME
71#include <OMX_Common_Utils.h>
72#include <OMX_GetTime.h>     /*Headers for Performance & measuremet    */
73#endif
74
75/* ======================================================================= */
76/**
77 * @def G711ENC_INPUT_BUFFER_SIZE        Default input buffer size
78 */
79/* ======================================================================= */
80#define G711ENC_INPUT_BUFFER_SIZE 160
81/* ======================================================================= */
82/**
83 * @def G711ENC_OUTPUT_BUFFER_SIZE   Default output buffer size
84 */
85/* ======================================================================= */
86#define G711ENC_OUTPUT_BUFFER_SIZE 80 /*Component default output buffer size*/
87
88/* ======================================================================= */
89/*
90 * @def    G711ENC_APP_ID  App ID Value setting
91 */
92/* ======================================================================= */
93#define G711ENC_APP_ID 100
94
95/* ======================================================================= */
96/*
97 * @def    FIFO Communication with audiomanager
98 */
99/* ======================================================================= */
100#define FIFO1 "/dev/fifo.1"
101#define FIFO2 "/dev/fifo.2"
102
103#undef APP_DEBUG
104
105#undef APP_MEMCHECK
106
107#undef USE_BUFFER
108
109/*For timestamp and tickcount*/
110#undef APP_TIME_TIC_DEBUG
111
112#ifdef APP_DEBUG
113#define APP_DPRINT(...)    fprintf(stderr,__VA_ARGS__)
114#else
115#define APP_DPRINT(...)
116#endif
117
118#ifdef APP_MEMCHECK
119#define APP_MEMPRINT(...)    fprintf(stderr,__VA_ARGS__)
120#else
121#define APP_MEMPRINT(...)
122#endif
123
124#ifdef APP_TIME_TIC_DEBUG
125#define TIME_PRINT(...)   fprintf(stderr,__VA_ARGS__)
126#define TICK_PRINT(...)   fprintf(stderr,__VA_ARGS__)
127#else
128#define TIME_PRINT(...)
129#define TICK_PRINT(...)
130#endif
131
132#ifdef OMX_GETTIME
133OMX_ERRORTYPE eError = OMX_ErrorNone;
134int GT_FlagE = 0;  /* Fill Buffer 1 = First Buffer,  0 = Not First Buffer  */
135int GT_FlagF = 0;  /*Empty Buffer  1 = First Buffer,  0 = Not First Buffer  */
136static OMX_NODE* pListHead = NULL;
137#endif
138
139pthread_mutex_t WaitForState_mutex;
140pthread_cond_t  WaitForState_threshold;
141OMX_U8          WaitForState_flag = 0;
142OMX_U8          TargetedState = 0;
143
144typedef struct AUDIO_INFO {
145    OMX_U32 acdnMode;
146    OMX_U32 dasfMode;
147    OMX_U32 nIpBufs;
148    OMX_U32 nIpBufSize;
149    OMX_U32 nOpBufs;
150    OMX_U32 nOpBufSize;
151    OMX_U32 nMFrameMode;
152} AUDIO_INFO;
153
154/* ======================================================================= */
155/**
156 *  M A C R O S FOR MALLOC and MEMORY FREE and CLOSING PIPES
157 */
158/* ======================================================================= */
159
160#define OMX_G711ENC_CONF_INIT_STRUCT(_s_, _name_)   \
161    memset((_s_), 0x0, sizeof(_name_));             \
162    (_s_)->nSize = sizeof(_name_);                  \
163    (_s_)->nVersion.s.nVersionMajor = 0x1;          \
164    (_s_)->nVersion.s.nVersionMinor = 0x0;          \
165    (_s_)->nVersion.s.nRevision = 0x0;              \
166    (_s_)->nVersion.s.nStep = 0x0
167
168#define OMX_G711ENC_INIT_STRUCT(_s_, _name_)    \
169    memset((_s_), 0x0, sizeof(_name_));    \
170
171#define OMX_G711ENC_MALLOC_STRUCT(_pStruct_, _sName_)               \
172    _pStruct_ = (_sName_*)malloc(sizeof(_sName_));                  \
173    if(_pStruct_ == NULL){                                          \
174        printf("***********************************\n");            \
175        printf("%d Malloc Failed\n",__LINE__);                      \
176        printf("***********************************\n");            \
177        eError = OMX_ErrorInsufficientResources;                    \
178        goto EXIT;                                                  \
179    }                                                               \
180    APP_MEMPRINT("%d ALLOCATING MEMORY = %p\n",__LINE__,_pStruct_);
181
182/* ======================================================================= */
183/**
184 * @def G711ENC_MAX_NUM_OF_BUFS       Maximum number of buffers
185 * @def G711ENC_NUM_OF_CHANNELS         Number of Channels
186 * @def G711ENC_SAMPLING_FREQUENCY    Sampling frequency
187 */
188/* ======================================================================= */
189#define G711ENC_MAX_NUM_OF_BUFS 10
190#define G711ENC_NUM_OF_CHANNELS 1
191#define G711ENC_SAMPLING_FREQUENCY 8000
192
193int maxint(int a, int b);
194
195int inputPortDisabled = 0;
196int outputPortDisabled = 0;
197OMX_BOOL playcompleted = 0;
198
199OMX_STRING strG711Encoder = "OMX.TI.G711.encode";
200
201int IpBuf_Pipe[2] = {0};
202int OpBuf_Pipe[2] = {0};
203int Event_Pipe[2] = {0};
204
205OMX_STATETYPE gState  = OMX_StateInvalid;
206
207fd_set rfds;
208static OMX_BOOL bInvalidState;
209
210/******************************************************************************/
211OMX_S16 numInputBuffers = 0;
212OMX_S16 numOutputBuffers = 0;
213#ifdef USE_BUFFER
214OMX_U8* pInputBuffer[10] = {NULL};
215OMX_U8* pOutputBuffer[10] = {NULL};
216#endif
217OMX_BUFFERHEADERTYPE* pInputBufferHeader[10] = {NULL};
218OMX_BUFFERHEADERTYPE* pOutputBufferHeader[10] = {NULL};
219
220int timeToExit = 0;
221/* RM control */
222int preempted = 0;
223/******************************************************************************/
224
225OMX_ERRORTYPE send_input_buffer (OMX_HANDLETYPE pHandle,
226                                 OMX_BUFFERHEADERTYPE* pBuffer,
227                                 FILE *fIn);
228/* safe routine to get the maximum of 2 integers */
229int maxint(int a, int b)
230{
231    return (a>b) ? a : b;
232}
233
234/* This method will wait for the component to get to the state
235 * specified by the DesiredState input. */
236static OMX_ERRORTYPE WaitForState(OMX_HANDLETYPE pHandle,
237                                  OMX_STATETYPE DesiredState)
238{
239    OMX_STATETYPE CurState = OMX_StateInvalid;
240    OMX_ERRORTYPE eError = OMX_ErrorNone;
241
242    eError = OMX_GetState(pHandle, &CurState);
243    if(eError != OMX_ErrorNone) {
244        APP_DPRINT("%d [TEST APP] Error returned from GetState\n",__LINE__);
245        return eError;
246
247    }
248    if(CurState != DesiredState){
249        WaitForState_flag = 1;
250        TargetedState = DesiredState;
251        pthread_mutex_lock(&WaitForState_mutex);
252        pthread_cond_wait(&WaitForState_threshold,
253                          &WaitForState_mutex);
254        pthread_mutex_unlock(&WaitForState_mutex);
255    }
256
257    return eError;
258}
259
260OMX_ERRORTYPE EventHandler(OMX_HANDLETYPE hComponent,
261                           OMX_PTR pAppData,
262                           OMX_EVENTTYPE eEvent,
263                           OMX_U32 nData1,
264                           OMX_U32 nData2,
265                           OMX_PTR pEventData)
266{
267    OMX_U8 writeValue = 0;
268
269    APP_DPRINT("%d [TEST APP] Component eEvent = %d\n", __LINE__,eEvent);
270    APP_DPRINT("%d [TEST APP] Entering EventHandler \n", __LINE__);
271    switch (eEvent) {
272
273    case OMX_EventCmdComplete:
274        gState = (OMX_STATETYPE)nData2;
275        APP_DPRINT( "%d [TEST APP] Component eEvent Completed  = %d\n", __LINE__,eEvent);
276        if (nData1 == OMX_CommandPortDisable) {
277            if (nData2 == OMX_DirInput) {
278                inputPortDisabled = 1;
279                APP_DPRINT( "%d [TEST APP] Input Port disabled \n", __LINE__);
280            }
281            if (nData2 == OMX_DirOutput) {
282                outputPortDisabled = 1;
283                APP_DPRINT( "%d [TEST APP] output Port disabled \n", __LINE__);
284            }
285        }
286        if ((nData1 == OMX_CommandStateSet) && (TargetedState == nData2) &&
287            (WaitForState_flag)){
288            WaitForState_flag = 0;
289            pthread_mutex_lock(&WaitForState_mutex);
290            pthread_cond_signal(&WaitForState_threshold);
291            pthread_mutex_unlock(&WaitForState_mutex);
292        }
293        APP_DPRINT( "%d [TEST APP] Exit OMX_EventCmdComplete = %ld\n", __LINE__,nData2);
294        break;
295
296    case OMX_EventError:
297        printf( "%d [TEST APP] Enter to OMX_EventError = %d\n", __LINE__,eEvent);
298
299        if (nData1 == OMX_ErrorInvalidState) {
300            bInvalidState = OMX_TRUE;
301        }
302        else if(nData1 == OMX_ErrorResourcesPreempted) {
303            writeValue = 0;
304            preempted = 1;
305            write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
306        }
307        else if (nData1 == OMX_ErrorResourcesLost) {
308            WaitForState_flag = 0;
309            pthread_mutex_lock(&WaitForState_mutex);
310            pthread_cond_signal(&WaitForState_threshold);
311            pthread_mutex_unlock(&WaitForState_mutex);
312        }
313        else if(nData1 == OMX_ErrorResourcesPreempted) {
314            writeValue = 0;
315            preempted = 1;
316            write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
317        }
318        APP_DPRINT( "%d [TEST APP] Component OMX_EventError = %d\n", __LINE__,eEvent);
319        break;
320
321    case OMX_EventMax:
322        APP_DPRINT( "%d [TEST APP] Component OMX_EventMax = %d\n", __LINE__,eEvent);
323        break;
324
325    case OMX_EventMark:
326        APP_DPRINT( "%d [TEST APP] Component OMX_EventMark = %d\n", __LINE__,eEvent);
327        break;
328
329    case OMX_EventPortSettingsChanged:
330        APP_DPRINT( "%d [TEST APP] Component OMX_EventPortSettingsChanged = %d\n", __LINE__,eEvent);
331        break;
332
333    case OMX_EventBufferFlag:
334        if(nData2 == 0)
335            printf("EOS received from INPUT %ld %ld\n",nData1,nData2);
336        playcompleted = 1;
337        writeValue = 2;
338        write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
339        APP_DPRINT( "%d [TEST APP] Component OMX_EventBufferFlag = %d\n", __LINE__,eEvent);
340        break;
341
342    case OMX_EventResourcesAcquired:
343        APP_DPRINT( "%d [TEST APP] Component OMX_EventResourcesAcquired = %d\n", __LINE__,eEvent);
344        writeValue = 1;
345        preempted = 0;
346        write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
347        APP_DPRINT( "%d [TEST APP] Component OMX_EventResourcesAquired = %d\n", __LINE__,eEvent);
348        break;
349
350    default:
351        break;
352
353    }
354    return OMX_ErrorNone;
355}
356
357void FillBufferDone (OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
358{
359    /*add on: TimeStamp & TickCount EmptyBufferDone*/
360    TIME_PRINT("TimeStamp Output: %lld\n",pBuffer->nTimeStamp);
361    TICK_PRINT("TickCount Output: %ld\n\n",pBuffer->nTickCount);
362    write(OpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
363#ifdef OMX_GETTIME
364    if (GT_FlagF == 1 ){ /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
365        GT_END("Call to FillBufferDone  <First: FillBufferDone>");
366        GT_FlagF = 0 ;   /* 1 = First Buffer,  0 = Not First Buffer  */
367    }
368#endif
369}
370
371void EmptyBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
372{
373
374    if (!preempted) {
375        write(IpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
376    }
377#ifdef OMX_GETTIME
378    if (GT_FlagE == 1 ){ /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
379        GT_END("Call to EmptyBufferDone <First: EmptyBufferDone>");
380        GT_FlagE = 0;   /* 1 = First Buffer,  0 = Not First Buffer  */
381    }
382#endif
383}
384typedef struct G711ENC_FTYPES{
385    OMX_S16   FrameSizeType;
386    OMX_S16   VAUMode;
387    OMX_S16   VAUThresOffset;
388    OMX_S16   VAUNum;
389    OMX_S16   NMUNoise;
390    OMX_S16   LPOrder;
391}G711ENC_FTYPES;
392/* ----------------------------------------------------------------------------
393 * main()
394 *
395 * This function is called at application startup
396 * and drives the G711 Encoder OMX component
397 * ---------------------------------------------------------------------------- */
398int main(int argc, char* argv[])
399{
400    OMX_CALLBACKTYPE G711CaBa = {(void *)EventHandler,
401                                 (void*)EmptyBufferDone,
402                                 (void*)FillBufferDone};
403    OMX_HANDLETYPE pHandle = NULL;
404    OMX_ERRORTYPE eError = OMX_ErrorNone;
405    OMX_U32 AppData = G711ENC_APP_ID;
406    OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct = NULL;
407    OMX_AUDIO_PARAM_PCMMODETYPE *pG711Param = NULL;
408    OMX_BUFFERHEADERTYPE* pInputBufferHeader[G711ENC_MAX_NUM_OF_BUFS] = {NULL};
409    OMX_BUFFERHEADERTYPE* pOutputBufferHeader[G711ENC_MAX_NUM_OF_BUFS] = {NULL};
410
411    G711ENC_FTYPES *g711eframeinfo =  malloc(sizeof(G711ENC_FTYPES));
412
413#ifdef USE_BUFFER
414    OMX_U8* pInputBuffer[G711ENC_MAX_NUM_OF_BUFS] = {NULL};
415    OMX_U8* pOutputBuffer[G711ENC_MAX_NUM_OF_BUFS] = {NULL};
416#endif
417    AUDIO_INFO* audioinfo = NULL;
418    TI_OMX_DSP_DEFINITION tiOmxDspDefinition;
419    FILE* fIn = NULL;
420    FILE* fOut = NULL;
421    struct timeval tv;
422    int retval= 0, i= 0, j= 0, k= 0, kk= 0, tcID = 0;
423    int frmCount = 0;
424    int frmCnt = 1;
425    int testcnt = 1;
426    int testcnt1 = 1;
427    int fdmax = 0;
428    int nFrameCount = 1;
429    int nFrameLen = 0;
430    int nIpBuff = 1;
431    int nOutBuff = 1;
432    OMX_INDEXTYPE index = 0;
433    int status = 0;
434    TI_OMX_DATAPATH dataPath;
435
436    int G711E_fdwrite = 0;
437    int G711E_fdread = 0;
438
439    printf("------------------------------------------------------\n");
440    printf("This is Main Thread In G711 ENCODER Test Application:\n");
441    printf("Test Core 1.5 - " __DATE__ ":" __TIME__ "\n");
442    printf("------------------------------------------------------\n");
443#ifdef OMX_GETTIME
444    printf("Line %d\n",__LINE__);
445    GTeError = OMX_ListCreate(&pListHead);
446    printf("Line %d\n",__LINE__);
447    printf("eError = %d\n",GTeError);
448    GT_START();
449    printf("Line %d\n",__LINE__);
450#endif
451    /* check the input parameters */
452    if(argc != 18) {
453        printf("[TestApp] [Input File] [Output File] [FUNC_ID_X] [FM/DM] [ACDNO\
454N/ACDNOFF] [NB BUFFERS (DASF mode)] [NB INPUT BUF] [INPUT BUF SIZE] [NB OUTPUT \
455BUF] [OUTPUT BUF SIZE] [ALaw/MULaw] [Frame Size Type] [VAU Mode] [VAU OFFSET] [\
456VAU NUM] [NMU Mode] [LP Order]\n");
457        goto EXIT;
458    }
459
460    pthread_mutex_init(&WaitForState_mutex, NULL);
461    pthread_cond_init (&WaitForState_threshold, NULL);
462
463    /* check to see that the input file exists */
464    struct stat sb = {0};
465    status = stat(argv[1], &sb);
466    if( status != 0 ) {
467        printf("Cannot find file %s. (%u)\n", argv[1], errno);
468        goto EXIT;
469    }
470
471    if(!strcmp(argv[3],"FUNC_ID_1")) {
472        printf("### Testing TESTCASE 1 PLAY TILL END ###\n");
473        tcID = 1;
474    } else if(!strcmp(argv[3],"FUNC_ID_2")) {
475        printf("### Testing TESTCASE 2 STOP IN THE END ###\n");
476        tcID = 2;
477    } else if(!strcmp(argv[3],"FUNC_ID_3")) {
478        printf("### Testing TESTCASE 3 PAUSE - RESUME IN BETWEEN ###\n");
479        tcID = 3;
480    } else if(!strcmp(argv[3],"FUNC_ID_4")) {
481        printf("### Testing TESTCASE 4 STOP IN BETWEEN ###\n");
482        testcnt = 2;
483        tcID = 4;
484    }
485    if(!strcmp(argv[3],"FUNC_ID_5")){
486        printf("### Testing TESTCASE 5 ENCODE without Deleting component Here ###\n");
487        testcnt = 20;
488        tcID = 5;
489    }
490    if(!strcmp(argv[3],"FUNC_ID_6")) {
491        printf("### Testing TESTCASE 6 ENCODE with Deleting component Here ###\n");
492        testcnt1 = 20;
493        tcID = 6;
494    }
495
496    /*----------------------------------------------
497      Main Loop for Deleting component test
498      ----------------------------------------------*/
499    for(j = 0; j < testcnt1; j++) {
500        if(tcID == 6)
501            printf ("Encoding the file for %d Time in TESTCASE 6\n",j);
502
503        /* Open communication with Audio manger */
504#ifdef DSP_RENDERING_ON
505        if((G711E_fdwrite=open(FIFO1,O_WRONLY))<0) {
506            printf("APP: - failure to open WRITE pipe\n");
507        }
508        else {
509            APP_DPRINT("APP: - opened WRITE pipe\n");
510        }
511
512        if((G711E_fdread=open(FIFO2,O_RDONLY))<0) {
513            printf("APP: - failure to open READ pipe\n");
514            goto EXIT;
515        }
516        else {
517            APP_DPRINT("APP: - opened READ pipe\n");
518        }
519#endif
520        /* Create a pipe used to queue data from the callback. */
521        retval = pipe(IpBuf_Pipe);
522        if( retval != 0) {
523            APP_DPRINT("Error:Fill Data Pipe failed to open\n");
524            goto EXIT;
525        }
526
527        retval = pipe(OpBuf_Pipe);
528        if( retval != 0) {
529            APP_DPRINT("Error:Empty Data Pipe failed to open\n");
530            goto EXIT;
531        }
532        retval = pipe(Event_Pipe);
533        if( retval != 0) {
534            APP_DPRINT( "%d %s Error: Empty Data Pipe failed to open\n",__LINE__, __FUNCTION__);
535            goto EXIT;
536        }
537        /* save off the "max" of the handles for the selct statement */
538        fdmax = maxint(IpBuf_Pipe[0], OpBuf_Pipe[0]);
539        fdmax = maxint(fdmax,Event_Pipe[0]);
540
541        eError = TIOMX_Init();
542        if(eError != OMX_ErrorNone) {
543            APP_DPRINT("%d Error returned by OMX_Init()\n",__LINE__);
544            goto EXIT;
545        }
546
547        /* Load the G711 Encoder Component */
548#ifdef OMX_GETTIME
549        GT_START();
550        eError = OMX_GetHandle(&pHandle, strG711Encoder, &AppData, &G711CaBa);
551        GT_END("Call to GetHandle");
552#else
553        eError = TIOMX_GetHandle(&pHandle, strG711Encoder, &AppData, &G711CaBa);
554#endif
555
556        if((eError != OMX_ErrorNone) || (pHandle == NULL)) {
557            APP_DPRINT("Error in Get Handle function\n");
558            goto EXIT;
559        }
560        APP_DPRINT("%d [TEST APP] Got Phandle =  %p \n",__LINE__,pHandle);
561
562        OMX_G711ENC_MALLOC_STRUCT(audioinfo, AUDIO_INFO);
563        OMX_G711ENC_INIT_STRUCT(audioinfo, AUDIO_INFO);
564
565        /* Setting Input and Output Buffers features for the Component */
566        audioinfo->nIpBufs = atoi(argv[7]);
567        if(audioinfo->nIpBufs > 4 && audioinfo->nIpBufs < 1){
568            APP_DPRINT( "Cannot support %li Input buffers\n", audioinfo->nIpBufs);
569            goto EXIT;
570        }
571        APP_DPRINT("%d [TEST APP] number of input buffers = %ld \n",__LINE__,audioinfo->nIpBufs);
572        audioinfo->nIpBufSize = atoi(argv[8]);
573        APP_DPRINT("%d [TEST APP] input buffer size = %ld \n",__LINE__,audioinfo->nIpBufSize);
574        audioinfo->nOpBufs = atoi(argv[9]);
575        if(audioinfo->nOpBufs > 4){
576            APP_DPRINT( "Cannot support %ld Output buffers\n", audioinfo->nOpBufs);
577            goto EXIT;
578        }
579        APP_DPRINT("%d [TEST APP] number of output buffers = %ld \n",__LINE__,audioinfo->nOpBufs);
580        audioinfo->nOpBufSize = atoi(argv[10]);
581        APP_DPRINT("%d [TEST APP] output buffer size = %ld \n",__LINE__,audioinfo->nOpBufSize);
582
583
584        OMX_G711ENC_MALLOC_STRUCT(pCompPrivateStruct, OMX_PARAM_PORTDEFINITIONTYPE);
585        OMX_G711ENC_CONF_INIT_STRUCT(pCompPrivateStruct, OMX_PARAM_PORTDEFINITIONTYPE);
586
587        pCompPrivateStruct->nPortIndex = OMX_DirInput;
588        eError = OMX_GetParameter (pHandle,
589                                   OMX_IndexParamPortDefinition,
590                                   pCompPrivateStruct);
591        if (eError != OMX_ErrorNone) {
592            eError = OMX_ErrorBadParameter;
593            APP_DPRINT("%d OMX_ErrorBadParameter\n",__LINE__);
594            goto EXIT;
595        }
596        APP_DPRINT("%d Setting input port config\n",__LINE__);
597        pCompPrivateStruct->nBufferCountActual                 = audioinfo->nIpBufs;
598        pCompPrivateStruct->nBufferCountMin                    = audioinfo->nIpBufs;
599        pCompPrivateStruct->nBufferSize                        = audioinfo->nIpBufSize;
600
601
602#ifdef OMX_GETTIME
603        GT_START();
604        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
605        GT_END("Set Parameter Test-SetParameter");
606#else
607        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
608#endif
609
610        if (eError != OMX_ErrorNone) {
611            eError = OMX_ErrorBadParameter;
612            APP_DPRINT("%d OMX_ErrorBadParameter\n",__LINE__);
613            goto EXIT;
614        }
615
616        if(!(strcmp(argv[4],"FM"))) {
617            audioinfo->dasfMode = 0;
618            tiOmxDspDefinition.dasfMode = OMX_FALSE;
619            APP_DPRINT("%d G711 ENCODER RUNNING UNDER FILE MODE \n",__LINE__);
620
621        } else if(!(strcmp(argv[4],"DM"))){
622            audioinfo->dasfMode = 1;
623            tiOmxDspDefinition.dasfMode = OMX_TRUE;
624            APP_DPRINT("%d G711 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
625
626        } else {
627            eError = OMX_ErrorBadParameter;
628            printf("\n%d [TEST APP] audioinfo->dasfMode Sending Bad Parameter\n",__LINE__);
629            printf("%d [TEST APP] Should Be One of these Modes FM, DM\n",__LINE__);
630            goto EXIT;
631        }
632
633        if(audioinfo->dasfMode == 0) {
634            if((atoi(argv[6])) != 0) {
635                eError = OMX_ErrorBadParameter;
636                printf("\n%d [TEST APP] No. of Buffers Sending Bad Parameter\n",__LINE__);
637                printf("%d [TEST APP] For FILE mode argv[6] Should Be --> 0\n",__LINE__);
638                printf("%d [TEST APP] For DASF mode argv[6] Should be greater than zero depends on number of buffers user want to encode\n",__LINE__);
639                goto EXIT;
640            }
641        } else {
642            if((atoi(argv[6])) == 0) {
643                eError = OMX_ErrorBadParameter;
644                printf("\n%d [TEST APP] No. of Buffers Sending Bad Parameter\n",__LINE__);
645                printf("%d [TEST APP] For DASF mode argv[6] Should be greater than zero depends on number of buffers user want to encode\n",__LINE__);
646                printf("%d [TEST APP] For FILE mode argv[6] Should Be --> 0\n",__LINE__);
647                goto EXIT;
648            }
649        }
650
651        if(!(strcmp(argv[5],"ACDNOFF"))) {
652            audioinfo->acdnMode = 0;
653            tiOmxDspDefinition.acousticMode = OMX_FALSE;
654            APP_DPRINT("\n%d [TEST APP] audioinfo->acdnMode = %ld \n",__LINE__,audioinfo->acdnMode);
655        } else if(!(strcmp(argv[5],"ACDNON"))) {
656            audioinfo->acdnMode = 1;
657            tiOmxDspDefinition.acousticMode = OMX_TRUE;
658            APP_DPRINT("\n%d [TEST APP] audioinfo->acdnMode = %ld \n",__LINE__,audioinfo->acdnMode);
659        } else {
660            eError = OMX_ErrorBadParameter;
661            printf("\n%d [TEST APP] audioinfo->acdnMode Sending Bad Parameter\n",__LINE__);
662            printf("%d [TEST APP] Should Be One of these Modes ACDNON, ACDNOFF\n",__LINE__);
663            goto EXIT;
664        }
665
666
667        pCompPrivateStruct->nPortIndex = OMX_DirOutput;
668        eError = OMX_GetParameter (pHandle,
669                                   OMX_IndexParamPortDefinition,
670                                   pCompPrivateStruct);
671        if (eError != OMX_ErrorNone) {
672            eError = OMX_ErrorBadParameter;
673            APP_DPRINT("%d OMX_ErrorBadParameter\n",__LINE__);
674            goto EXIT;
675        }
676
677        APP_MEMPRINT("%d Setting output port config\n",__LINE__);
678        pCompPrivateStruct->nBufferCountActual                 = audioinfo->nOpBufs;
679        pCompPrivateStruct->nBufferCountMin                    = audioinfo->nOpBufs;
680        pCompPrivateStruct->nBufferSize                        = audioinfo->nOpBufSize;
681
682#ifdef OMX_GETTIME
683        GT_START();
684        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
685        GT_END("Set Parameter Test-SetParameter");
686#else
687        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
688#endif
689        if (eError != OMX_ErrorNone) {
690            eError = OMX_ErrorBadParameter;
691            APP_DPRINT("%d OMX_ErrorBadParameter\n",__LINE__);
692            goto EXIT;
693        }
694
695        OMX_G711ENC_MALLOC_STRUCT(pG711Param, OMX_AUDIO_PARAM_PCMMODETYPE);
696        OMX_G711ENC_CONF_INIT_STRUCT(pG711Param, OMX_AUDIO_PARAM_PCMMODETYPE);
697
698        /* Send  G711 config for output */
699        pG711Param->nPortIndex = OMX_DirOutput;
700        eError = OMX_GetParameter (pHandle, OMX_IndexParamAudioPcm, pG711Param);
701        if (eError != OMX_ErrorNone) {
702            APP_DPRINT("%d Get paramter reported %d\n",__LINE__,eError);
703            goto EXIT;
704        }
705
706        pG711Param->nChannels = G711ENC_NUM_OF_CHANNELS;
707
708        /* extract compression format from command line */
709        if (!(strcmp(argv[11],"ALaw")))
710            pG711Param->ePCMMode             = OMX_AUDIO_PCMModeALaw;
711        else if (!(strcmp(argv[11],"MULaw")))
712            pG711Param->ePCMMode             = OMX_AUDIO_PCMModeMULaw;
713        else {
714            printf("\n%d [TEST APP] Bad Parameter: \n",__LINE__);
715            printf("%d [TEST APP] Please enter proper G711 mode: ALaw or MULaw\n",__LINE__);
716            goto EXIT;
717        }
718#ifdef OMX_GETTIME
719        GT_START();
720        eError = OMX_SetParameter (pHandle, OMX_IndexParamAudioPcm, pG711Param);
721        GT_END("Set Parameter Test-SetParameter");
722#else
723        eError = OMX_SetParameter (pHandle, OMX_IndexParamAudioPcm, pG711Param);
724#endif
725        if (eError != OMX_ErrorNone) {
726            eError = OMX_ErrorBadParameter;
727            APP_DPRINT("%d OMX_ErrorBadParameter\n",__LINE__);
728            goto EXIT;
729        }
730
731        /* Send  G711 config for input */
732        pG711Param->nPortIndex = OMX_DirInput;
733        eError = OMX_GetParameter (pHandle,OMX_IndexParamAudioPcm, pG711Param);
734        if (eError != OMX_ErrorNone) {
735            eError = OMX_ErrorBadParameter;
736            printf ("%d [TEST APP] OMX_ErrorBadParameter\n",__LINE__);
737            goto EXIT;
738        }
739
740#ifdef OMX_GETTIME
741        GT_START();
742        eError = OMX_SetParameter (pHandle,OMX_IndexParamAudioPcm, pG711Param);
743        GT_END("Set Parameter Test-SetParameter");
744#else
745        eError = OMX_SetParameter (pHandle,OMX_IndexParamAudioPcm, pG711Param);
746#endif
747        if (eError != OMX_ErrorNone) {
748            eError = OMX_ErrorBadParameter;
749            printf ("%d [TEST APP] OMX_ErrorBadParameter\n",__LINE__);
750            goto EXIT;
751        }
752
753        /** Getting the frame params */
754        g711eframeinfo->FrameSizeType = atoi(argv[12]);
755        g711eframeinfo->VAUMode = atoi(argv[13]);
756        g711eframeinfo->VAUThresOffset = atoi(argv[14]);
757        g711eframeinfo->VAUNum = atoi(argv[15]);
758        g711eframeinfo->NMUNoise = atoi(argv[16]);
759        g711eframeinfo->LPOrder = atoi(argv[17]);
760
761        eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.g711.frameparamters",&index);
762        if (eError != OMX_ErrorNone) {
763            printf("Error getting extension index\n");
764            goto EXIT;
765        }
766
767        eError = OMX_SetConfig (pHandle, index, g711eframeinfo);
768        if(eError != OMX_ErrorNone) {
769            eError = OMX_ErrorBadParameter;
770            APP_DPRINT("%d Error from OMX_SetConfig() function\n",__LINE__);
771            goto EXIT;
772        }
773
774
775        eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.tispecific",&index);
776        if (eError != OMX_ErrorNone) {
777            APP_DPRINT("Error returned from OMX_GetExtensionIndex\n");
778            goto EXIT;
779        }
780
781#ifdef DSP_RENDERING_ON
782        if((write(G711E_fdwrite, &cmd_data, sizeof(cmd_data)))<0) {
783            printf("%d [TEST APP] - failure to send command to audio manager\n", __LINE__);
784            goto EXIT;
785        }
786        if((read(G711E_fdread, &cmd_data, sizeof(cmd_data)))<0) {
787            printf("%d [TEST APP] - failure to get data from the audio manager\n", __LINE__);
788            goto EXIT;
789        }
790#endif
791
792        eError = OMX_SetConfig (pHandle, index, &tiOmxDspDefinition);
793        if(eError != OMX_ErrorNone) {
794            eError = OMX_ErrorBadParameter;
795            APP_DPRINT("%d Error from OMX_SetConfig() function\n",__LINE__);
796            goto EXIT;
797        }
798
799        /* Data path  for DASF */
800        if (audioinfo->dasfMode) {
801            printf("***************StreamId=%d******************\n", (int)tiOmxDspDefinition.streamId);
802#ifdef RTM_PATH
803            dataPath = DATAPATH_APPLICATION_RTMIXER;
804#endif
805
806#ifdef ETEEDN_PATH
807            dataPath = DATAPATH_APPLICATION;
808#endif
809        }
810
811        eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.g711.datapath",&index);
812        if (eError != OMX_ErrorNone) {
813            printf("Error getting extension index\n");
814            goto EXIT;
815        }
816
817        eError = OMX_SetConfig (pHandle, index, &dataPath);
818        if(eError != OMX_ErrorNone) {
819            eError = OMX_ErrorBadParameter;
820            APP_DPRINT("%d G711EencTest.c :: Error from OMX_SetConfig() function\n",__LINE__);
821            goto EXIT;
822        }
823
824#ifdef OMX_GETTIME
825        GT_START();
826#endif
827        eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
828        if(eError != OMX_ErrorNone) {
829            APP_DPRINT("Error from SendCommand-Idle(Init) State function\n");
830            goto EXIT;
831        }
832#ifndef USE_BUFFER
833        if(!audioinfo->dasfMode){
834            for(i = 0; i < audioinfo->nIpBufs; i++) {
835                /* allocate input buffer */
836                APP_DPRINT("%d About to call OMX_AllocateBuffer for pInputBufferHeader[%d]\n\n",__LINE__, i);
837                eError = OMX_AllocateBuffer(pHandle, &pInputBufferHeader[i], 0, NULL, audioinfo->nIpBufSize);
838                if(eError != OMX_ErrorNone) {
839                    APP_DPRINT("%d Error returned by OMX_AllocateBuffer for pInputBufferHeader[%d]\n",__LINE__, i);
840                    goto EXIT;
841                }
842            }
843        }
844
845        for(i = 0; i < audioinfo->nOpBufs; i++) {
846            /* allocate output buffer */
847            APP_DPRINT("%d About to call OMX_AllocateBuffer for pOutputBufferHeader[%d]\n\n",__LINE__, i);
848            eError = OMX_AllocateBuffer(pHandle, &pOutputBufferHeader[i], 1, NULL, pCompPrivateStruct->nBufferSize);
849            if(eError != OMX_ErrorNone) {
850                APP_DPRINT("%d Error returned by OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
851                goto EXIT;
852            }
853        }
854#else
855        if(!audioinfo->dasfMode){
856            for(i = 0; i < audioinfo->nIpBufs; i++) {
857                pInputBuffer[i] = (OMX_U8*)malloc(audioinfo->nIpBufSize);
858                APP_MEMPRINT("%d [TESTAPP ALLOC] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
859                if(NULL == pInputBuffer[i]) {
860                    APP_DPRINT("%d Malloc Failed\n",__LINE__);
861                    eError = OMX_ErrorInsufficientResources;
862                    goto EXIT;
863                }
864                /*  allocate input buffer */
865                APP_DPRINT("%d About to call OMX_UseBuffer\n",__LINE__);
866                eError = OMX_UseBuffer(pHandle, &pInputBufferHeader[i], 0, NULL, audioinfo->nIpBufSize, pInputBuffer[i]);
867                if(eError != OMX_ErrorNone) {
868                    APP_DPRINT("%d Error returned by OMX_UseBuffer()\n",__LINE__);
869                    goto EXIT;
870                }
871            }
872        }
873        for(i = 0; i < audioinfo->nOpBufs; i++) {
874            pOutputBuffer[i] = malloc (pCompPrivateStruct->nBufferSize + 256);
875            APP_MEMPRINT("%d [TESTAPP ALLOC] pOutputBuffer[%d] = %p\n",__LINE__,i,pOutputBuffer[i]);
876            if(NULL == pOutputBuffer[i]) {
877                APP_DPRINT("%d Malloc Failed\n",__LINE__);
878                eError = OMX_ErrorInsufficientResources;
879                goto EXIT;
880            }
881            pOutputBuffer[i] = pOutputBuffer[i] + 128;
882
883            /* allocate output buffer */
884            APP_DPRINT("%d About to call OMX_UseBuffer\n",__LINE__);
885            eError = OMX_UseBuffer(pHandle, &pOutputBufferHeader[i], 1, NULL, pCompPrivateStruct->nBufferSize, pOutputBuffer[i]);
886            if(eError != OMX_ErrorNone) {
887                APP_DPRINT("%d Error returned by OMX_UseBuffer()\n",__LINE__);
888                goto EXIT;
889            }
890        }
891#endif
892        /* Wait for startup to complete */
893        eError = WaitForState(pHandle, OMX_StateIdle);
894#ifdef OMX_GETTIME
895        GT_END("Call to SendCommand <OMX_StateIdle>");
896#endif
897        if(eError != OMX_ErrorNone) {
898            APP_DPRINT( "Error:  G711Encoder->WaitForState reports an eError %X\n", eError);
899            goto EXIT;
900        }
901
902
903        /*----------------------------------------------
904          Main Loop for Non Deleting component test
905          ----------------------------------------------*/
906        for(i = 0; i < testcnt; i++) {
907            if(tcID == 5)
908                printf ("Encoding the file for %d Time in TESTCASE 5\n",i);
909
910            if(i > 0){
911                /* Create a pipe used to queue data from the callback. */
912                retval = pipe(IpBuf_Pipe);
913                if( retval != 0) {
914                    APP_DPRINT( "%d APP: Error: Fill Data Pipe failed to open\n",__LINE__);
915                    goto EXIT;
916                }
917                retval = pipe(OpBuf_Pipe);
918                if( retval != 0) {
919                    APP_DPRINT( "%d APP: Error: Empty Data Pipe failed to open\n",__LINE__);
920                    goto EXIT;
921                }
922                retval = pipe(Event_Pipe);
923                if( retval != 0) {
924                    APP_DPRINT( "%d APP: Error: Event Pipe failed to open\n",__LINE__);
925                    goto EXIT;
926                }
927            }
928
929            frmCnt = 1;
930            nFrameCount = 1;
931            nOutBuff = 1;
932            nIpBuff  = 1;
933
934            fIn = fopen(argv[1], "r");
935            if( fIn == NULL ) {
936                fprintf(stderr, "Error:  failed to open the file %s for read only access\n",argv[1]);
937                goto EXIT;
938            }
939            fOut = fopen(argv[2], "w");
940            if( fOut == NULL ) {
941                printf("Error:  failed to open the output file %s\n", argv[2]);
942                goto EXIT;
943            }
944            APP_DPRINT("%d [TEST APP] Sending OMX_StateExecuting Command\n",__LINE__);
945#ifdef OMX_GETTIME
946            GT_START();
947#endif
948            eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
949            if(eError != OMX_ErrorNone) {
950                printf("Error from SendCommand-Executing State function\n");
951                goto EXIT;
952            }
953            eError = WaitForState(pHandle, OMX_StateExecuting);
954#ifdef OMX_GETTIME
955            GT_END("Call to SendCommand <OMX_StateExecuting>");
956#endif
957            if(eError != OMX_ErrorNone) {
958                APP_DPRINT( "Error:  G711Encoder->WaitForState reports an eError %X\n", eError);
959                goto EXIT;
960            }
961
962            if(audioinfo->dasfMode == 1) {
963                printf("%d [TEST APP] No.of Buffers Encoding = %d\n",__LINE__, atoi(argv[6]));
964            }
965
966            if(audioinfo->dasfMode == 0) {
967                for (k=0; k < audioinfo->nIpBufs; k++) {
968#ifdef OMX_GETTIME
969                    if (k==0){
970                        GT_FlagE=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
971                        GT_START(); /* Empty Bufffer */
972                    }
973#endif
974                    send_input_buffer(pHandle,pInputBufferHeader[k],fIn);
975                    nIpBuff++;
976                }
977            }
978
979            for (kk = 0; kk < audioinfo->nOpBufs; kk++) {
980                APP_DPRINT("%d [TEST APP] Calling OMX_FillThisBuffer \n",__LINE__);
981#ifdef OMX_GETTIME
982                if (kk==0){
983                    GT_FlagF=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
984                    GT_START(); /* Fill Buffer */
985                }
986#endif
987                OMX_FillThisBuffer(pHandle, pOutputBufferHeader[kk]);
988            }
989
990            while((eError == OMX_ErrorNone) && (gState != OMX_StateIdle) &&
991                  (gState != OMX_StateInvalid)) {
992
993                FD_ZERO(&rfds);
994                FD_SET(IpBuf_Pipe[0], &rfds);
995                FD_SET(OpBuf_Pipe[0], &rfds);
996                FD_SET(Event_Pipe[0], &rfds);
997
998                tv.tv_sec = 1;
999                tv.tv_usec = 0;
1000                frmCount++;
1001
1002                retval = select(fdmax+1, &rfds, NULL, NULL, &tv);
1003                if(retval == -1) {
1004                    perror("select()");
1005                    APP_DPRINT( " :: Error \n");
1006                    break;
1007                }
1008
1009                if(retval == 0) {
1010                    APP_DPRINT("%d BasicFn App Timeout !!!!!!!!!!! \n",__LINE__);
1011                }
1012
1013                switch (tcID) {
1014                case 1:
1015                case 2:
1016                case 5:
1017                case 6:
1018                    if(audioinfo->dasfMode == 0) {
1019                        if(FD_ISSET(IpBuf_Pipe[0], &rfds)) {
1020                            OMX_BUFFERHEADERTYPE* pBuffer;
1021                            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1022                            pBuffer->nFlags = 0;
1023                            send_input_buffer(pHandle,pBuffer,fIn);
1024                            nIpBuff++;
1025                        }
1026                        if(tcID == 2 && nIpBuff == 200) {
1027                            printf("%d [TEST APP] Sending Stop.........From APP\n",__LINE__);
1028                            printf("%d [TEST APP] Shutting down ---------- \n",__LINE__);
1029#ifdef OMX_GETTIME
1030                            GT_START();
1031#endif
1032                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1033                            if(eError != OMX_ErrorNone) {
1034                                fprintf (stderr,"Error from SendCommand-Idle(Stop) State function\n");
1035                                goto EXIT;
1036                            }
1037                            eError = WaitForState(pHandle, OMX_StateIdle);
1038#ifdef OMX_GETTIME
1039                            GT_END("Call to SendCommand <OMX_StateIdle>");
1040#endif
1041                            if (eError != OMX_ErrorNone ){
1042                                printf("Error:WaitForState has timed out %d", eError);
1043                                goto EXIT;
1044                            }
1045                        }
1046                    } else {
1047                        APP_DPRINT("%d G711 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
1048                        if(nFrameCount == atoi(argv[6])) {
1049                            APP_DPRINT("%d [TEST APP] Sending Stop.........From APP \n",__LINE__);
1050                            APP_DPRINT("%d [TEST APP] Shutting down ---------- \n",__LINE__);
1051#ifdef OMX_GETTIME
1052                            GT_START();
1053#endif
1054                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1055                            if(eError != OMX_ErrorNone) {
1056                                fprintf (stderr,"Error from SendCommand-Idle(Stop) State function\n");
1057                                goto EXIT;
1058                            }
1059                            eError = WaitForState(pHandle, OMX_StateIdle);
1060#ifdef OMX_GETTIME
1061                            GT_END("Call to SendCommand <OMX_StateIdle>");
1062#endif
1063                            if ( eError != OMX_ErrorNone ){
1064                                printf("Error:WaitForState has timed out %d", eError);
1065                                goto EXIT;
1066                            }
1067                        }
1068                        APP_DPRINT("%d G711 ENCODER READING DATA FROM DASF  \n",__LINE__);
1069                    }
1070                    break;
1071
1072                case 3:
1073                    if(audioinfo->dasfMode == 0) {
1074                        APP_DPRINT("%d G711 ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
1075                        if(FD_ISSET(IpBuf_Pipe[0], &rfds)) {
1076                            OMX_BUFFERHEADERTYPE* pBuffer;
1077                            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1078                            pBuffer->nFlags = 0;
1079                            send_input_buffer(pHandle,pBuffer,fIn);
1080                        }
1081                    } else {
1082                        APP_DPRINT("%d G711 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
1083                        if(nFrameCount == atoi(argv[6])) {
1084                            printf("%d [TEST APP] Sending Stop.........From APP \n",__LINE__);
1085                            printf("%d [TEST APP] Shutting down ---------- \n",__LINE__);
1086#ifdef OMX_GETTIME
1087                            GT_START();
1088#endif
1089                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1090                            if(eError != OMX_ErrorNone) {
1091                                fprintf (stderr,"Error from SendCommand-Idle(Stop) State function\n");
1092                                goto EXIT;
1093                            }
1094                            eError = WaitForState(pHandle, OMX_StateIdle);
1095#ifdef OMX_GETTIME
1096                            GT_END("Call to SendCommand <OMX_StateIdle>");
1097#endif
1098                            if (eError != OMX_ErrorNone ){
1099                                printf("Error:WaitForState has timed out %d", eError);
1100                                goto EXIT;
1101                            }
1102                        }
1103                        APP_DPRINT("%d G711 ENCODER READING DATA FROM DASF  \n",__LINE__);
1104                    }
1105                    if (frmCount == 15) {
1106                        printf ("%d [TEST APP] $$$$$ Sending Resume command to Codec $$$$$$$\n",__LINE__);
1107#ifdef OMX_GETTIME
1108                        GT_START();
1109#endif
1110                        eError = OMX_SendCommand(pHandle, OMX_CommandStateSet,OMX_StateExecuting, NULL);
1111                        if(eError != OMX_ErrorNone) {
1112                            fprintf (stderr,"Error from SendCommand-Executing State function\n");
1113                            goto EXIT;
1114                        }
1115                        /* Wait for startup to complete */
1116                        eError = WaitForState(pHandle, OMX_StateExecuting);
1117#ifdef OMX_GETTIME
1118                        GT_END("Call to SendCommand <OMX_StateExecuting>");
1119#endif
1120                        if(eError != OMX_ErrorNone) {
1121                            fprintf(stderr, "Error:  hPcmDecoder->WaitForState reports an eError %X\n", eError);
1122                            goto EXIT;
1123                        }
1124                    }
1125                    if(frmCount == 10) {
1126                        printf ("%d [TEST APP] $$$$$ Sending Pause command to Codec $$$$$$\n",__LINE__);
1127#ifdef OMX_GETTIME
1128                        GT_START();
1129#endif
1130                        eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StatePause, NULL);
1131                        if(eError != OMX_ErrorNone) {
1132                            printf("Error from SendCommand-Pasue State function\n");
1133                            goto EXIT;
1134                        }
1135                        /* Wait for startup to complete */
1136                        eError = WaitForState(pHandle, OMX_StatePause);
1137#ifdef OMX_GETTIME
1138                        GT_END("Call to SendCommand <OMX_StatePause>");
1139#endif
1140                        if(eError != OMX_ErrorNone) {
1141                            printf("G711Encoder->WaitForState reports error\n");
1142                            goto EXIT;
1143                        }
1144                    }
1145                    break;
1146                case 4:
1147                    if(audioinfo->dasfMode == 0) {
1148                        APP_DPRINT("%d G711 ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
1149                        if( FD_ISSET(IpBuf_Pipe[0], &rfds) ) {
1150                            if(frmCnt > 20) {
1151                                printf("Stop Playback......\n");
1152                                eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
1153                                if(eError != OMX_ErrorNone) {
1154                                    printf("Error from SendCommand-Pasue State function\n");
1155                                    goto EXIT;
1156                                }
1157                                eError = WaitForState(pHandle, OMX_StateIdle);
1158                                if(eError != OMX_ErrorNone) {
1159                                    printf("G711Encoder->WaitForState reports error\n");
1160                                    goto EXIT;
1161                                }
1162                                sleep(2);
1163                                printf("Resume Playback.....\n");
1164                                tcID = 1;
1165                                frmCnt++;
1166                            } else {
1167                                OMX_BUFFERHEADERTYPE* pBuffer;
1168                                read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1169                                pBuffer->nFlags = 0;
1170                                send_input_buffer(pHandle,pBuffer,fIn);
1171                            }
1172                        }
1173                        frmCnt++;
1174                    } else {
1175                        APP_DPRINT("%d G711 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
1176                        if(nFrameCount == atoi(argv[6])) {
1177                            printf("%d [TEST APP] Shutting down ---------- \n",__LINE__);
1178#ifdef OMX_GETTIME
1179                            GT_START();
1180#endif
1181
1182                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1183                            if(eError != OMX_ErrorNone) {
1184                                printf("Error from SendCommand-Idle(Stop) State function\n");
1185                                goto EXIT;
1186                            }
1187                            eError = WaitForState(pHandle, OMX_StateIdle);
1188#ifdef OMX_GETTIME
1189                            GT_END("Call to SendCommand <OMX_StateIdle>");
1190#endif
1191                            if (eError != OMX_ErrorNone ){
1192                                printf("Error:WaitForState has timed out %d", eError);
1193                                goto EXIT;
1194                            }
1195                        }
1196                        if(nFrameCount == 20) {
1197                            printf("%d [TEST APP] Sending Stop After %d frames \n",__LINE__,nFrameCount);
1198#ifdef OMX_GETTIME
1199                            GT_START();
1200#endif
1201                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1202                            if(eError != OMX_ErrorNone) {
1203                                printf("Error from SendCommand-Idle(Stop) State function\n");
1204                                goto EXIT;
1205                            }
1206                            eError = WaitForState(pHandle, OMX_StateIdle);
1207#ifdef OMX_GETTIME
1208                            GT_END("Call to SendCommand <OMX_StateIdle>");
1209#endif
1210                            if (eError != OMX_ErrorNone ){
1211                                printf("Error:WaitForState has timed out %d", eError);
1212                                goto EXIT;
1213                            }
1214                        }
1215                    }
1216                    break;
1217                default:
1218                    APP_DPRINT("%d ### Simple DEFAULT Case Here ###\n",__LINE__);
1219                }
1220
1221                if( FD_ISSET(OpBuf_Pipe[0], &rfds) ) {
1222                    OMX_BUFFERHEADERTYPE* pBuf;
1223                    read(OpBuf_Pipe[0], &pBuf, sizeof(pBuf));
1224                    APP_DPRINT("%d [TEST APP] pBuf->nFilledLen = %ld\n",__LINE__, pBuf->nFilledLen);
1225                    nFrameLen = pBuf->nFilledLen;
1226                    if (nFrameLen != 0) {
1227                        APP_DPRINT("%d Writing OutputBuffer No: %d to the file nWrite = %d \n",__LINE__, nOutBuff, nFrameLen);
1228                        fwrite(pBuf->pBuffer, 1, pBuf->nFilledLen, fOut);
1229                        fflush(fOut);
1230                    }
1231                    nFrameCount++;
1232                    nOutBuff++;
1233                    OMX_FillThisBuffer(pHandle, pBuf);
1234                    APP_DPRINT("%d [TEST APP] pBuf->nFlags = %ld\n",__LINE__, pBuf->nFlags);
1235                }
1236                if( FD_ISSET(Event_Pipe[0], &rfds) ) {
1237                    OMX_U8 pipeContents;
1238                    read(Event_Pipe[0], &pipeContents, sizeof(OMX_U8));
1239
1240                    if(pipeContents==2){
1241                      eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
1242                      if(eError != OMX_ErrorNone) {
1243                        printf("Error from SendCommand-Idle(Stop) State function\n");
1244                        goto EXIT;
1245                      }
1246                      eError = WaitForState(pHandle, OMX_StateIdle);
1247#ifdef OMX_GETTIME
1248                      GT_END("Call to SendCommand <OMX_StateIdle>");
1249#endif
1250                      if ( eError != OMX_ErrorNone ){
1251                        printf("Error:WaitForState has timed out %d", eError);
1252                        goto EXIT;
1253                      }
1254                      audioinfo->dasfMode = 0;
1255                      //pBuf->nFlags = 0;
1256                      APP_DPRINT("%d [TEST APP] Shutting down ---------- \n",__LINE__);
1257                    }
1258                }
1259            } /* While Loop Ending Here */
1260
1261            APP_DPRINT("%d [TEST APP] The current state of the component = %d \n",__LINE__,gState);
1262            fclose(fOut);
1263            fclose(fIn);
1264            playcompleted = 0;
1265
1266            close(IpBuf_Pipe[0]);
1267            close(IpBuf_Pipe[1]);
1268            close(OpBuf_Pipe[0]);
1269            close(OpBuf_Pipe[1]);
1270            close(Event_Pipe[0]);
1271            close(Event_Pipe[1]);
1272
1273            if(tcID == 5 || tcID == 4) {
1274                sleep (1);
1275            }
1276            APP_DPRINT("%d [TEST APP] G711 Encoded = %d Frames \n",__LINE__,(nOutBuff));
1277        } /*Test Case 4 & 5 Inner for loop ends here  */
1278
1279
1280        APP_DPRINT ("%d [TEST APP] Sending the OMX_CommandPortDisable Command\n",__LINE__);
1281#ifdef OMX_GETTIME
1282        GT_START();
1283#endif
1284        eError = OMX_SendCommand(pHandle, OMX_CommandPortDisable, -1, NULL);
1285        if(eError != OMX_ErrorNone) {
1286            APP_DPRINT("%d:: Error from SendCommand OMX_CommandPortDisable\n",__LINE__);
1287            goto EXIT;
1288        }
1289
1290        APP_DPRINT ("%d [TEST APP] Sending the OMX_StateLoaded Command\n",__LINE__);
1291        eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
1292        if(eError != OMX_ErrorNone) {
1293            APP_DPRINT("%d:: Error from SendCommand-Idle State function\n",__LINE__);
1294            goto EXIT;
1295        }
1296
1297        /* free the Allocate and Use Buffers */
1298#ifndef USE_BUFFER
1299        APP_DPRINT("%d [TEST APP] Freeing the Allocate OR Use Buffers in TestApp\n",__LINE__);
1300        if(!audioinfo->dasfMode){
1301            for(i=0; i < audioinfo->nIpBufs; i++) {
1302                APP_DPRINT("%d [TEST APP] About to free pInputBufferHeader[%d]\n",__LINE__, i);
1303                eError = OMX_FreeBuffer(pHandle, OMX_DirInput, pInputBufferHeader[i]);
1304                if((eError != OMX_ErrorNone)) {
1305                    APP_DPRINT("%d:: Error in FreeBuffer function\n",__LINE__);
1306                    goto EXIT;
1307                }
1308            }
1309        }
1310        for(i=0; i < audioinfo->nOpBufs; i++) {
1311            APP_DPRINT("%d [TEST APP] About to free pOutputBufferHeader[%d]\n",__LINE__, i);
1312            eError = OMX_FreeBuffer(pHandle, OMX_DirOutput, pOutputBufferHeader[i]);
1313            if((eError != OMX_ErrorNone)) {
1314                APP_DPRINT("%d Error in Free Buffer function\n",__LINE__);
1315                goto EXIT;
1316            }
1317        }
1318#else
1319        /* free the App Allocated Buffers */
1320        APP_DPRINT("%d [TEST APP] Freeing the App Allocated Buffers in TestApp\n",__LINE__);
1321        if(!audioinfo->dasfmode){
1322            for(i=0; i < audioinfo->nIpBufs; i++) {
1323                APP_MEMPRINT("%d [TEST APP] [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
1324                if(pInputBuffer[i] != NULL){
1325                    free(pInputBuffer[i]);
1326                    pInputBuffer[i] = NULL;
1327                }
1328            }
1329        }
1330        for(i=0; i < audioinfo->nOpBufs; i++) {
1331            pOutputBuffer[i] = pOutputBuffer[i] - 128;
1332            APP_MEMPRINT("%d [TEST APP] [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, pOutputBuffer[i]);
1333            if(pOutputBuffer[i] != NULL){
1334                free(pOutputBuffer[i]);
1335                pOutputBuffer[i] = NULL;
1336            }
1337        }
1338#endif
1339
1340
1341        eError = WaitForState(pHandle, OMX_StateLoaded);
1342#ifdef OMX_GETTIME
1343        GT_END("Call to SendCommand <OMX_CommandPortDisable, Buffer set then OMX_StateLoaded>");
1344#endif
1345        if(eError != OMX_ErrorNone) {
1346            APP_DPRINT("APP: Error:  WaitForState reports an error %X\n", eError);
1347            goto EXIT;
1348        }
1349
1350        APP_DPRINT("%d [TEST APP] Freeing the Memory Allocated in TestApp\n",__LINE__);
1351        APP_MEMPRINT("%d [TEST APP] [TESTAPPFREE] %p\n",__LINE__,pG711Param);
1352        if(pG711Param != NULL){
1353            free(pG711Param);
1354            pG711Param = NULL;
1355        }
1356        APP_MEMPRINT("%d [TEST APP] [TESTAPPFREE] %p\n",__LINE__,pCompPrivateStruct);
1357        if(pCompPrivateStruct != NULL){
1358            free(pCompPrivateStruct);
1359            pCompPrivateStruct = NULL;
1360        }
1361        APP_MEMPRINT("%d [TEST APP] [TESTAPPFREE] %p\n",__LINE__,audioinfo);
1362        if(audioinfo != NULL){
1363            free(audioinfo);
1364            audioinfo = NULL;
1365        }
1366
1367        APP_DPRINT("%d [TEST APP] Closing the Input and Output Pipes\n",__LINE__);
1368        eError = close (IpBuf_Pipe[0]);
1369        if (0 != eError && OMX_ErrorNone == eError) {
1370            eError = OMX_ErrorHardware;
1371            APP_DPRINT("%d Error while closing IpBuf_Pipe[0]\n",__LINE__);
1372            goto EXIT;
1373        }
1374        eError = close (IpBuf_Pipe[1]);
1375        if (0 != eError && OMX_ErrorNone == eError) {
1376            eError = OMX_ErrorHardware;
1377            APP_DPRINT("%d Error while closing IpBuf_Pipe[1]\n",__LINE__);
1378            goto EXIT;
1379        }
1380        eError = close (OpBuf_Pipe[0]);
1381        if (0 != eError && OMX_ErrorNone == eError) {
1382            eError = OMX_ErrorHardware;
1383            APP_DPRINT("%d Error while closing OpBuf_Pipe[0]\n",__LINE__);
1384            goto EXIT;
1385        }
1386        eError = close (OpBuf_Pipe[1]);
1387        if (0 != eError && OMX_ErrorNone == eError) {
1388            eError = OMX_ErrorHardware;
1389            APP_DPRINT("%d Error while closing OpBuf_Pipe[1]\n",__LINE__);
1390            goto EXIT;
1391        }
1392        eError = close (Event_Pipe[0]);
1393        if (0 != eError && OMX_ErrorNone == eError) {
1394            eError = OMX_ErrorHardware;
1395            APP_DPRINT("%d Error while closing Event_Pipe[0]\n",__LINE__);
1396            goto EXIT;
1397        }
1398        eError = close (Event_Pipe[1]);
1399        if (0 != eError && OMX_ErrorNone == eError) {
1400            eError = OMX_ErrorHardware;
1401            APP_DPRINT("%d Error while closing Event_Pipe[1]\n",__LINE__);
1402            goto EXIT;
1403        }
1404        /* exit audio manger */
1405#ifdef DSP_RENDERING_ON
1406        if((write(G711E_fdwrite, &cmd_data, sizeof(cmd_data)))<0){
1407            printf("%d [TEST APP] - failure to send command to audio manager\n",__LINE__);
1408        }
1409        close(G711E_fdwrite);
1410        close(G711E_fdread);
1411#endif
1412
1413        APP_DPRINT("%d [TEST APP] Free the Component handle\n",__LINE__);
1414        /* Unload the G711 Encoder Component */
1415        eError = TIOMX_FreeHandle(pHandle);
1416        if((eError != OMX_ErrorNone)) {
1417            APP_DPRINT("%d Error in Free Handle function\n",__LINE__);
1418            goto EXIT;
1419        }
1420        APP_DPRINT("%d [TEST APP] Free Handle returned Successfully\n",__LINE__);
1421
1422    } /*Outer for loop ends here */
1423
1424    pthread_mutex_destroy(&WaitForState_mutex);
1425    pthread_cond_destroy(&WaitForState_threshold);
1426
1427#ifdef OMX_GETTIME
1428    GT_END("G711_Enc test <End>");
1429    OMX_ListDestroy(pListHead);
1430#endif
1431
1432    eError = TIOMX_Deinit();
1433    if( (eError != OMX_ErrorNone)) {
1434        APP_DPRINT("%d:: APP: Error in Deinit Core function\n",__LINE__);
1435        goto EXIT;
1436    }
1437
1438    printf("*********************************************************************\n");
1439    printf("NOTE: An output file %s has been created in file system\n",argv[2]);
1440    printf("*********************************************************************\n");
1441
1442 EXIT:
1443
1444    return eError;
1445}
1446
1447OMX_ERRORTYPE send_input_buffer (OMX_HANDLETYPE pHandle,
1448                                 OMX_BUFFERHEADERTYPE* pBuffer,
1449                                 FILE *fIn)
1450{
1451    OMX_ERRORTYPE error = OMX_ErrorNone;
1452
1453    pBuffer->nFilledLen = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
1454
1455    if(pBuffer->nFilledLen == 0) {
1456        APP_DPRINT("%d Sending Last Input Buffer from TestApp.............. \n",__LINE__);
1457        pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
1458    }else{
1459        pBuffer->nFlags = 0;
1460    }
1461    APP_DPRINT("%d [TEST APP] Input Buffer: Calling OMX_EmptyThisBuffer: %p\n",__LINE__,pBuffer);
1462
1463    pBuffer->nTimeStamp = rand()% 100;
1464    pBuffer->nTickCount = rand() % 70;
1465
1466    if(!playcompleted){
1467        error = OMX_EmptyThisBuffer(pHandle, pBuffer);
1468        if( (error != OMX_ErrorNone)) {
1469            APP_DPRINT("%d [TEST APP] EmptyThisBuffer\n",__LINE__);
1470            goto EXIT;
1471        }
1472    }
1473
1474
1475 EXIT:
1476    return error;
1477}
1478
1479