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 AmrEnc_Test.c
26*
27* This file implements NBAMR 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\nbamr_enc\tests
31*
32* @rev  1.0
33*/
34/* ----------------------------------------------------------------------------
35*!
36*! Revision History
37*! ===================================
38*! 21-sept-2006 bk: updated review findings for alpha release
39*! 24-Aug-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests some more
40*! 18-July-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests validated for few cases
41*! 21-Jun-2006 bk: Khronos OpenMAX (TM) 1.0 migration done
42*! 22-May-2006 bk: DASF recording quality improved
43*! 19-Apr-2006 bk: DASF recording speed issue resloved
44*! 23-Feb-2006 bk: DASF functionality added
45*! 18-Jan-2006 bk: Repated recording issue fixed and LCML changes taken care
46*! 14-Dec-2005 bk: Initial Version
47*! 16-Nov-2005 bk: Initial Version
48*! 23-Sept-2005 bk: Initial Version
49*! 10-Sept-2005 bk: Initial Version
50*! 10-Sept-2005 bk:
51*! This is newest file
52* =========================================================================== */
53/* ------compilation control switches -------------------------*/
54/****************************************************************
55*  INCLUDE FILES
56****************************************************************/
57/* ----- system and platform files ----------------------------*/
58
59#include <unistd.h>
60#include <sys/ioctl.h>
61#include <sys/select.h>
62#include <errno.h>
63#include <linux/vt.h>
64#include <signal.h>
65#include <sys/stat.h>
66#include <pthread.h>
67#include <linux/soundcard.h>
68
69#include <string.h>
70#include <fcntl.h>
71#include <stdlib.h>
72#include <stdio.h>
73
74#include <OMX_Index.h>
75#include <OMX_Types.h>
76#include <OMX_Component.h>
77#include <OMX_Core.h>
78#include <OMX_Audio.h>
79#include <TIDspOmx.h>
80/* #include <AudioManagerAPI.h> */
81#include <time.h>
82
83#ifdef OMX_GETTIME
84    #include <OMX_Common_Utils.h>
85    #include <OMX_GetTime.h>     /*Headers for Performance & measuremet    */
86#endif
87
88FILE *fpRes;
89
90/* ======================================================================= */
91/**
92 * @def NBAMRENC_NUM_INPUT_BUFFERS   Default number of input buffers
93 */
94/* ======================================================================= */
95#define NBAPP_NUM_INPUT_BUFFERS 1
96/* ======================================================================= */
97/**
98 * @def NBAMRENC_NUM_INPUT_BUFFERS_DASF  Default No.of input buffers DASF
99 */
100/* ======================================================================= */
101#define NBAPP_NUM_INPUT_BUFFERS_DASF 2
102/* ======================================================================= */
103/**
104 * @def NBAMRENC_NUM_OUTPUT_BUFFERS   Default number of output buffers
105 */
106/* ======================================================================= */
107#define NBAPP_NUM_OUTPUT_BUFFERS 1
108/* ======================================================================= */
109/**
110 * @def NBAMRENC_INPUT_BUFFER_SIZE   	 Default input buffer size
111 *		NBAMRENC_INPUT_BUFFER_SIZE_DASF  Default input buffer size DASF
112 */
113/* ======================================================================= */
114#define NBAPP_INPUT_BUFFER_SIZE 320
115#define NBAPP_INPUT_BUFFER_SIZE_DASF 320
116/* ======================================================================= */
117/**
118 * @def NBAMRENC_OUTPUT_BUFFER_SIZE   Default output buffer size
119 */
120/* ======================================================================= */
121#define NBAPP_OUTPUT_BUFFER_SIZE 118
122/* ======================================================================= */
123/**
124 * @def NBAMRENC_OUTPUT_BUFFER_SIZE_MIME  Default input buffer size MIME
125 */
126/* ======================================================================= */
127#define NBAPP_OUTPUT_BUFFER_SIZE_MIME 34
128
129/* ======================================================================= */
130/*
131 * @def	NBAMRENC_APP_ID  App ID Value setting
132 */
133/* ======================================================================= */
134#define NBAMRENC_APP_ID 100
135
136#define SLEEP_TIME 5
137
138#define NBAMRENC_MIME_HEADER_LEN 6
139
140#define FIFO1 "/dev/fifo.1"
141#define FIFO2 "/dev/fifo.2"
142
143#define APP_INFO
144
145#undef APP_DEBUG
146
147#undef APP_MEMCHECK
148
149#undef USE_BUFFER
150/*#define USE_BUFFER*/
151
152#define STRESS_TEST_ITERATIONS 20
153
154#ifdef  APP_INFO
155        #define APP_IPRINT(...)    fprintf(stderr,__VA_ARGS__)			/* Information prints */
156#else
157        #define APP_IPRINT(...)
158#endif
159
160#ifdef APP_DEBUG
161    #define APP_DPRINT(...)    fprintf(stderr,__VA_ARGS__)
162#else
163    #define APP_DPRINT(...)
164#endif
165
166#ifdef APP_MEMCHECK
167    #define APP_MEMPRINT(...)    fprintf(stderr,__VA_ARGS__)
168#else
169    #define APP_MEMPRINT(...)
170#endif
171
172#ifdef OMX_GETTIME
173  OMX_ERRORTYPE eError = OMX_ErrorNone;
174  int GT_FlagE = 0;  /* Fill Buffer 1 = First Buffer,  0 = Not First Buffer  */
175  int GT_FlagF = 0;  /*Empty Buffer  1 = First Buffer,  0 = Not First Buffer  */
176  static OMX_NODE* pListHead = NULL;
177#endif
178
179/* ======================================================================= */
180/**
181 * @def    APP_DEBUGMEM   Turns memory leaks messaging on and off.
182 *         NBAMRENC_DEBUGMEM must be defined in OMX Comp in order to get
183 *         this functionality On.
184 */
185/* ======================================================================= */
186#undef APP_DEBUGMEM
187/*#define APP_DEBUGMEM*/
188
189#ifdef APP_DEBUGMEM
190void *arr[500];
191int lines[500];
192int bytes[500];
193char file[500][50];
194int ind=0;
195
196#define newmalloc(x) mynewmalloc(__LINE__,__FILE__,x)
197#define newfree(z) mynewfree(z,__LINE__,__FILE__)
198
199void * mynewmalloc(int line, char *s, int size)
200{
201   void *p;
202   int e=0;
203   p = calloc(1,size);
204   if(p==NULL){
205       APP_IPRINT("Memory not available\n");
206       exit(1);
207       }
208   else{
209         while((lines[e]!=0)&& (e<500) ){
210              e++;
211         }
212         arr[e]=p;
213         lines[e]=line;
214         bytes[e]=size;
215         strcpy(file[e],s);
216         APP_IPRINT("Allocating %d bytes on address %p, line %d file %s pos %d\n", size, p, line, s, e);
217         return p;
218   }
219}
220
221int mynewfree(void *dp, int line, char *s){
222    int q;
223    if(dp==NULL){
224                 APP_IPRINT("NULL can't be deleted\n");
225                 return 0;
226    }
227    for(q=0;q<500;q++){
228        if(arr[q]==dp){
229           APP_IPRINT("Deleting %d bytes on address %p, line %d file %s\n", bytes[q],dp, line, s);
230           free(dp);
231           dp = NULL;
232           lines[q]=0;
233           strcpy(file[q],"");
234           break;
235        }
236     }
237     if(500==q)
238         APP_IPRINT("\n\nPointer not found. Line:%d    File%s!!\n\n",line, s);
239}
240#else
241#define newmalloc(x) malloc(x)
242#define newfree(z) free(z)
243#endif
244
245
246typedef struct NBAMRENC_BUFDATA {
247   OMX_U8 nFrames;
248}NBAMRENC_BUFDATA;
249
250/* ======================================================================= */
251/**
252 *  M A C R O S FOR MALLOC and MEMORY FREE and CLOSING PIPES
253 */
254/* ======================================================================= */
255
256#define OMX_NBAPP_CONF_INIT_STRUCT(_s_, _name_)	\
257    memset((_s_), 0x0, sizeof(_name_));	\
258    (_s_)->nSize = sizeof(_name_);		\
259    (_s_)->nVersion.s.nVersionMajor = 0x1;	\
260    (_s_)->nVersion.s.nVersionMinor = 0x0;	\
261    (_s_)->nVersion.s.nRevision = 0x0;		\
262    (_s_)->nVersion.s.nStep = 0x0
263
264#define OMX_NBAPP_INIT_STRUCT(_s_, _name_)	\
265    memset((_s_), 0x0, sizeof(_name_));	\
266
267#define OMX_NBAPP_MALLOC_STRUCT(_pStruct_, _sName_)   \
268    _pStruct_ = (_sName_*)newmalloc(sizeof(_sName_));      \
269    if(_pStruct_ == NULL){      \
270        APP_IPRINT("***********************************\n"); \
271        APP_IPRINT("%d :: Malloc Failed\n",__LINE__); \
272        APP_IPRINT("***********************************\n"); \
273        eError = OMX_ErrorInsufficientResources; \
274        goto EXIT;      \
275    } \
276    APP_MEMPRINT("%d :: ALLOCATING MEMORY = %p\n",__LINE__,_pStruct_);
277
278/* ======================================================================= */
279/** NBAPP_COMP_PORT_TYPE  Port types
280 *
281 *  @param  NBAPP_INPUT_PORT			Input port
282 *
283 *  @param  NBAPP_OUTPUT_PORT			Output port
284 */
285/*  ====================================================================== */
286/*This enum must not be changed. */
287typedef enum NBAPP_COMP_PORT_TYPE {
288    NBAPP_INPUT_PORT = 0,
289    NBAPP_OUTPUT_PORT
290}NBAPP_COMP_PORT_TYPE;
291
292/* ======================================================================= */
293/**
294 * @def NBAPP_MAX_NUM_OF_BUFS   	Maximum number of buffers
295 * @def	NBAPP_NUM_OF_CHANNELS 		Number of Channels
296 * @def NBAPP_SAMPLING_FREQUENCY    Sampling frequency
297 */
298/* ======================================================================= */
299#define NBAPP_MAX_NUM_OF_BUFS 10
300#define NBAPP_NUM_OF_CHANNELS 1
301#define NBAPP_SAMPLING_FREQUENCY 8000
302
303
304#undef  WAITFORRESOURCES
305pthread_mutex_t WaitForState_mutex;
306pthread_cond_t  WaitForState_threshold;
307OMX_U8          WaitForState_flag;
308OMX_U8      TargetedState;
309
310
311static OMX_BOOL bInvalidState;
312void* ArrayOfPointers[6];
313OMX_ERRORTYPE StopComponent(OMX_HANDLETYPE *pHandle);
314OMX_ERRORTYPE PauseComponent(OMX_HANDLETYPE *pHandle);
315OMX_ERRORTYPE PlayComponent(OMX_HANDLETYPE *pHandle);
316OMX_ERRORTYPE send_input_buffer(OMX_HANDLETYPE pHandle, OMX_BUFFERHEADERTYPE* pBuffer, FILE *fIn);
317int maxint(int a, int b);
318
319int inputPortDisabled = 0;
320int outputPortDisabled = 0;
321OMX_U8 NextBuffer[NBAPP_INPUT_BUFFER_SIZE*3];
322int FirstTime = 1;
323int nRead;
324NBAMRENC_BUFDATA* OutputFrames;
325
326#ifdef DSP_RENDERING_ON
327AM_COMMANDDATATYPE cmd_data;
328#endif
329OMX_STRING strAmrEncoder = "OMX.TI.AMR.encode";
330
331#ifndef USE_BUFFER
332int FreeAllResources( OMX_HANDLETYPE *pHandle,
333			                OMX_BUFFERHEADERTYPE* pBufferIn,
334			                OMX_BUFFERHEADERTYPE* pBufferOut,
335			                int NIB, int NOB,
336			                FILE* fIn, FILE* fOut);
337#else
338int  FreeAllResources(OMX_HANDLETYPE *pHandle,
339						  OMX_U8* UseInpBuf[],
340						  OMX_U8* UseOutBuf[],
341						  int NIB, int NOB,
342						  FILE* fIn, FILE* fOut);
343#endif
344int IpBuf_Pipe[2];
345int OpBuf_Pipe[2];
346int Event_Pipe[2];
347
348fd_set rfds;
349int DasfMode;
350int TeeMode=0;
351int mframe=0;
352
353int preempted = 0;
354
355/* safe routine to get the maximum of 2 integers */
356int maxint(int a, int b)
357{
358   return (a>b) ? a : b;
359}
360
361
362/* This method will wait for the component to get to the state
363 * specified by the DesiredState input. */
364static OMX_ERRORTYPE WaitForState(OMX_HANDLETYPE* pHandle,
365                                  OMX_STATETYPE DesiredState)
366{
367     OMX_STATETYPE CurState = OMX_StateInvalid;
368     OMX_ERRORTYPE eError = OMX_ErrorNone;
369     /* OMX_S16 nCnt = 0; */
370     OMX_COMPONENTTYPE *pComponent = (OMX_COMPONENTTYPE *)pHandle;
371
372     eError = pComponent->GetState(pHandle, &CurState);
373     if (CurState == OMX_StateInvalid && bInvalidState == OMX_TRUE)
374	 {
375		 	eError = OMX_ErrorInvalidState;
376	 }
377
378eError = OMX_GetState(pHandle, &CurState);
379    if (CurState == OMX_StateInvalid) {
380        eError = OMX_ErrorInvalidState;
381    }
382    if(CurState != DesiredState){
383        WaitForState_flag = 1;
384        TargetedState = DesiredState;
385        pthread_mutex_lock(&WaitForState_mutex);
386        pthread_cond_wait(&WaitForState_threshold, &WaitForState_mutex);/*Going to sleep till signal arrives*/
387        pthread_mutex_unlock(&WaitForState_mutex);
388    }
389
390	return eError;
391}
392
393OMX_ERRORTYPE EventHandler(
394        OMX_HANDLETYPE hComponent,
395        OMX_PTR pAppData,
396        OMX_EVENTTYPE eEvent,
397        OMX_U32 nData1,
398        OMX_U32 nData2,
399        OMX_PTR pEventData)
400{
401   APP_DPRINT( "%d :: App: Entering EventHandler \n", __LINE__);
402   OMX_ERRORTYPE eError = OMX_ErrorNone;
403   OMX_COMPONENTTYPE *pComponent = (OMX_COMPONENTTYPE *)hComponent;
404   OMX_STATETYPE state;
405
406   OMX_U8 writeValue;
407
408   eError = pComponent->GetState (hComponent, &state);
409   if(eError != OMX_ErrorNone) {
410       APP_DPRINT("%d :: App: Error returned from GetState\n",__LINE__);
411       goto EXIT;
412   }
413   APP_DPRINT( "%d :: App: Component eEvent = %d\n", __LINE__,eEvent);
414   switch (eEvent) {
415	   APP_DPRINT( "%d :: App: Component State Changed To %d\n", __LINE__,state);
416	   case OMX_EventCmdComplete:
417           APP_DPRINT( "%d :: App: Component State Changed To %d\n", __LINE__,state);
418		if (nData1 == OMX_CommandPortDisable) {
419			if (nData2 == NBAPP_INPUT_PORT) {
420				inputPortDisabled = 1;
421			}
422			if (nData2 == NBAPP_OUTPUT_PORT) {
423				outputPortDisabled = 1;
424			}
425		}
426        if ((nData1 == OMX_CommandStateSet) && (TargetedState == nData2) &&
427            (WaitForState_flag)){
428            WaitForState_flag = 0;
429            pthread_mutex_lock(&WaitForState_mutex);
430            pthread_cond_signal(&WaitForState_threshold);
431            pthread_mutex_unlock(&WaitForState_mutex);
432        }
433           break;
434       case OMX_EventError:
435		   if (nData1 == OMX_ErrorInvalidState) {
436		   		bInvalidState =OMX_TRUE;
437		   		APP_IPRINT("EventHandler: Invalid State!!!!\n");
438		   }
439		   else if(nData1 == OMX_ErrorResourcesPreempted) {
440            preempted=1;
441            writeValue = 0;
442            write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
443	       }
444	       else if (nData1 == OMX_ErrorResourcesLost) {
445	            WaitForState_flag = 0;
446	            pthread_mutex_lock(&WaitForState_mutex);
447	            pthread_cond_signal(&WaitForState_threshold);/*Sending Waking Up Signal*/
448	            pthread_mutex_unlock(&WaitForState_mutex);
449	        }
450
451           break;
452       case OMX_EventMax:
453       	   APP_DPRINT( "%d :: App: Component OMX_EventMax = %d\n", __LINE__,eEvent);
454           break;
455       case OMX_EventMark:
456       	   APP_DPRINT( "%d :: App: Component OMX_EventMark = %d\n", __LINE__,eEvent);
457           break;
458       case OMX_EventPortSettingsChanged:
459       	   APP_DPRINT( "%d :: App: Component OMX_EventPortSettingsChanged = %d\n", __LINE__,eEvent);
460       	   break;
461       case OMX_EventBufferFlag:
462       	   APP_DPRINT( "%d :: App: Component OMX_EventBufferFlag = %d\n", __LINE__,eEvent);
463		   writeValue = 2;
464	       write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
465           break;
466       case OMX_EventResourcesAcquired:
467       	   APP_DPRINT( "%d :: App: Component OMX_EventResourcesAcquired = %d\n", __LINE__,eEvent);
468           writeValue = 1;
469           write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
470           preempted=0;
471
472       	   break;
473       default:
474           break;
475
476   }
477EXIT:
478	APP_DPRINT( "%d :: App: Exiting EventHandler \n", __LINE__);
479	return eError;
480}
481
482void FillBufferDone (OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
483{
484
485    write(OpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
486/*    OutputFrames = pBuffer->pOutputPortPrivate;
487    printf("Receiving %d Frames\n",OutputFrames->nFrames);*/
488#ifdef OMX_GETTIME
489	if (GT_FlagF == 1 ) /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
490    {
491		GT_END("Call to FillBufferDone  <First: FillBufferDone>");
492        GT_FlagF = 0 ;   /* 1 = First Buffer,  0 = Not First Buffer  */
493    }
494#endif
495}
496
497void EmptyBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
498{
499	if (!preempted)
500    	write(IpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
501
502#ifdef OMX_GETTIME
503    if (GT_FlagE == 1 ) /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
504    {
505      GT_END("Call to EmptyBufferDone <First: EmptyBufferDone>");
506	  GT_FlagE = 0;   /* 1 = First Buffer,  0 = Not First Buffer  */
507    }
508#endif
509}
510
511
512
513int main(int argc, char* argv[])
514{
515    OMX_CALLBACKTYPE AmrCaBa = {(void *)EventHandler,
516				(void*)EmptyBufferDone,
517                                (void*)FillBufferDone};
518    OMX_HANDLETYPE pHandle;
519    OMX_ERRORTYPE eError = OMX_ErrorNone;
520    OMX_U32 AppData = NBAMRENC_APP_ID;
521    OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct;
522    OMX_AUDIO_PARAM_AMRTYPE *pAmrParam;
523    OMX_COMPONENTTYPE *pComponent;
524    OMX_STATETYPE state;
525    OMX_BUFFERHEADERTYPE* pInputBufferHeader[NBAPP_MAX_NUM_OF_BUFS];
526    OMX_BUFFERHEADERTYPE* pOutputBufferHeader[NBAPP_MAX_NUM_OF_BUFS];
527    bInvalidState=OMX_FALSE;
528#ifdef USE_BUFFER
529    OMX_U8* pInputBuffer[NBAPP_MAX_NUM_OF_BUFS];
530	OMX_U8* pOutputBuffer[NBAPP_MAX_NUM_OF_BUFS];
531#endif
532	TI_OMX_DSP_DEFINITION* audioinfo;
533	FILE* fIn = NULL;
534	FILE* fOut = NULL;
535    struct timeval tv;
536    int retval, i, j, k, kk, tcID = 0;
537    int frmCount = 0;
538    int frmCnt = 1;
539	int testcnt = 0;
540	int testcnt1 = 0;
541	int status = 0;
542	int fdmax = 0;
543    int nFrameCount = 1;
544    int nFrameLen = 0;
545    int nOutBuff = 1;
546    int NoDataRead=0;
547	OMX_INDEXTYPE index;
548   	OMX_U32	streamId;
549   	int numInputBuffers=0,numOutputBuffers=0;
550    TI_OMX_DATAPATH dataPath;
551    int FrameMode=0;
552int nbamrencdfwrite;
553int nbamrencfdread;
554
555    OMX_AUDIO_CONFIG_VOLUMETYPE* pCompPrivateStructGain = NULL;
556    srand ( time(NULL) );
557
558    pthread_mutex_init(&WaitForState_mutex, NULL);
559    pthread_cond_init (&WaitForState_threshold, NULL);
560    WaitForState_flag = 0;
561
562	APP_IPRINT("------------------------------------------------------\n");
563    APP_IPRINT("This is Main Thread In NBAMR ENCODER Test Application:\n");
564    APP_IPRINT("Test Core 1.5 - " __DATE__ ":" __TIME__ "\n");
565    APP_IPRINT("------------------------------------------------------\n");
566
567#ifdef OMX_GETTIME
568    APP_IPRINT("Line %d\n",__LINE__);
569      GTeError = OMX_ListCreate(&pListHead);
570        APP_IPRINT("Line %d\n",__LINE__);
571      APP_IPRINT("eError = %d\n",GTeError);
572      GT_START();
573  APP_IPRINT("Line %d\n",__LINE__);
574#endif
575
576    /* check the input parameters */
577    if((argc < 14) || (argc > 15)) {
578        APP_IPRINT("%d :: Usage: [TestApp] [O/P] [FUNC_ID_X] [FM/DM] [NBAMR/EFR] [BITRATE] [DTXON/OFF] [NONMIME/MIME/IF2] [ACDNON/OFF] [FRAMES] [1 to N] [1 to N] [MFON] [1 to N (optional)]\n",__LINE__);
579        goto EXIT;
580    }
581
582    /* check to see that the input file exists */
583    struct stat sb = {0};
584    status = stat(argv[1], &sb);
585    if( status != 0 ) {
586        APP_DPRINT("Cannot find file %s. (%u)\n", argv[1], errno);
587        goto EXIT;
588    }
589
590    /* Open the file of data to be encoded. */
591    fIn = fopen(argv[1], "r");
592    if( fIn == NULL ) {
593        APP_DPRINT("Error:  failed to open the input file %s\n", argv[1]);
594        goto EXIT;
595    }
596    /* Open the file of data to be written. */
597    fOut = fopen(argv[2], "w");
598    if( fOut == NULL ) {
599        APP_DPRINT("Error:  failed to open the output file %s\n", argv[2]);
600        goto EXIT;
601    }
602
603    if(!strcmp(argv[3],"FUNC_ID_1")) {
604        APP_IPRINT("%d :: ### Testing TESTCASE 1 PLAY TILL END ###\n",__LINE__);
605        testcnt = 1;
606        testcnt1 = 1;
607        tcID = 1;
608    } else if(!strcmp(argv[3],"FUNC_ID_2")) {
609        APP_IPRINT("%d :: ### Testing TESTCASE 2 STOP IN THE END ###\n",__LINE__);
610        testcnt = 1;
611        testcnt1 = 1;
612        tcID = 2;
613    } else if(!strcmp(argv[3],"FUNC_ID_3")) {
614        APP_IPRINT("%d :: ### Testing TESTCASE 3 PAUSE - RESUME IN BETWEEN ###\n",__LINE__);
615        testcnt = 1;
616        testcnt1 = 1;
617        tcID = 3;
618    } else if(!strcmp(argv[3],"FUNC_ID_4")) {
619        APP_IPRINT("%d :: ### Testing TESTCASE 4 STOP IN BETWEEN ###\n",__LINE__);
620        testcnt = 2;
621        testcnt1 = 1;
622        tcID = 4;
623        APP_IPRINT("######## testcnt = %d #########\n",testcnt);
624    }
625    if(!strcmp(argv[3],"FUNC_ID_5")){
626        APP_IPRINT("%d :: ### Testing TESTCASE 5 ENCODE without Deleting component Here ###\n",__LINE__);
627        if (argc == 15)
628        {
629            testcnt = atoi(argv[14]);
630        }
631        else
632        {
633            testcnt = STRESS_TEST_ITERATIONS;  /*20 cycles by default*/
634        }
635        testcnt1 = 1;
636        tcID = 5;
637    }
638    if(!strcmp(argv[3],"FUNC_ID_6")) {
639        APP_IPRINT("%d :: ### Testing TESTCASE 6 ENCODE with Deleting component Here ###\n",__LINE__);
640        if (argc == 15)
641        {
642            testcnt1 = atoi(argv[14]);
643        }
644        else
645        {
646            testcnt1 = STRESS_TEST_ITERATIONS;  /*20 cycles by default*/
647        }
648        testcnt = 1;
649        tcID = 6;
650    }
651    if(!strcmp(argv[3],"FUNC_ID_7")) {
652        APP_IPRINT("%d :: ### Testing TESTCASE 7 ENCODE with Volume Control ###\n",__LINE__);
653        testcnt = 1;
654        testcnt1 = 1;
655        tcID = 7;
656    }
657    if(!strcmp(argv[3],"FUNC_ID_8")) {
658        APP_IPRINT("%d :: ### Testing PLAY TILL END  WITH TWO FRAMES BY BUFFER###\n",__LINE__);
659            testcnt = 1;
660        testcnt1 = 1;
661        tcID = 1;
662        mframe = 1;
663    }
664    for(j = 0; j < testcnt1; j++) {
665
666#ifdef DSP_RENDERING_ON
667		if((nbamrencdfwrite=open(FIFO1,O_WRONLY))<0) {
668            APP_IPRINT("[AMRTEST] - failure to open WRITE pipe\n");
669        }
670        else {
671            APP_IPRINT("[AMRTEST] - opened WRITE pipe\n");
672        }
673
674        if((nbamrencfdread=open(FIFO2,O_RDONLY))<0) {
675            APP_IPRINT("[AMRTEST] - failure to open READ pipe\n");
676            goto EXIT;
677        }
678        else {
679            APP_IPRINT("[AMRTEST] - opened READ pipe\n");
680        }
681
682#endif
683
684    /* Create a pipe used to queue data from the callback. */
685    retval = pipe(IpBuf_Pipe);
686    if( retval != 0) {
687        APP_DPRINT("Error:Fill Data Pipe failed to open\n");
688        goto EXIT;
689    }
690
691    retval = pipe(OpBuf_Pipe);
692    if( retval != 0) {
693        APP_DPRINT("Error:Empty Data Pipe failed to open\n");
694        goto EXIT;
695    }
696
697	retval = pipe(Event_Pipe);
698    if( retval != 0) {
699		    APP_DPRINT( "Error:Empty Event Pipe failed to open\n");
700		    goto EXIT;
701	    }
702
703    /* save off the "max" of the handles for the selct statement */
704    fdmax = maxint(IpBuf_Pipe[0], OpBuf_Pipe[0]);
705	fdmax = maxint(fdmax,Event_Pipe[0]);
706
707    eError = TIOMX_Init();
708
709    if(eError != OMX_ErrorNone) {
710        APP_DPRINT("%d :: Error returned by OMX_Init()\n",__LINE__);
711        goto EXIT;
712    }
713
714	TI_OMX_STREAM_INFO *streaminfo;
715    OMX_NBAPP_MALLOC_STRUCT(streaminfo, TI_OMX_STREAM_INFO);
716    OMX_NBAPP_MALLOC_STRUCT(audioinfo, TI_OMX_DSP_DEFINITION);
717	OMX_NBAPP_INIT_STRUCT(audioinfo, TI_OMX_DSP_DEFINITION);
718
719    ArrayOfPointers[0]=(TI_OMX_STREAM_INFO*)streaminfo;
720    ArrayOfPointers[1]=(TI_OMX_DSP_DEFINITION*)audioinfo;
721	if(j > 0) {
722		APP_IPRINT ("%d :: Encoding the file for %d Time in TESTCASE 6\n",__LINE__,j+1);
723		fIn = fopen(argv[1], "r");
724		if( fIn == NULL ) {
725			fprintf(stderr, "Error:  failed to open the file %s for read only access\n",argv[1]);
726			goto EXIT;
727		}
728
729		fOut = fopen("TC6_Amr1.amr", "w");
730		if( fOut == NULL ) {
731			fprintf(stderr, "Error:  failed to create the output file %s\n",argv[2]);
732			goto EXIT;
733		}
734	 }
735
736    /* Load the NBAMR Encoder Component */
737
738
739#ifdef OMX_GETTIME
740	GT_START();
741    eError = OMX_GetHandle(&pHandle, strAmrEncoder, &AppData, &AmrCaBa);
742	GT_END("Call to GetHandle");
743#else
744    eError = TIOMX_GetHandle(&pHandle, strAmrEncoder, &AppData, &AmrCaBa);
745#endif
746    if((eError != OMX_ErrorNone) || (pHandle == NULL)) {
747        APP_DPRINT("Error in Get Handle function\n");
748        goto EXIT;
749    }
750
751
752	/* Setting No.Of Input and Output Buffers for the Component */
753	numInputBuffers = atoi(argv[11]);
754	APP_DPRINT("\n%d :: App: numInputBuffers = %d \n",__LINE__,numInputBuffers);
755
756	numOutputBuffers = atoi(argv[12]);
757	APP_DPRINT("\n%d :: App: numOutputBuffers = %d \n",__LINE__,numOutputBuffers);
758
759
760    OMX_NBAPP_MALLOC_STRUCT(pCompPrivateStruct, OMX_PARAM_PORTDEFINITIONTYPE);
761	OMX_NBAPP_CONF_INIT_STRUCT(pCompPrivateStruct, OMX_PARAM_PORTDEFINITIONTYPE);
762    OMX_NBAPP_MALLOC_STRUCT(pAmrParam, OMX_AUDIO_PARAM_AMRTYPE);
763	OMX_NBAPP_CONF_INIT_STRUCT(pAmrParam, OMX_AUDIO_PARAM_AMRTYPE);
764
765    ArrayOfPointers[2]=(OMX_PARAM_PORTDEFINITIONTYPE*)pCompPrivateStruct;
766    ArrayOfPointers[3] = (OMX_AUDIO_PARAM_AMRTYPE *)pAmrParam;
767
768    APP_DPRINT("%d :: Setting input port config\n",__LINE__);
769    pCompPrivateStruct->nSize 							   = sizeof (OMX_PARAM_PORTDEFINITIONTYPE);
770    pCompPrivateStruct->nVersion.s.nVersionMajor           = 0xF1;
771    pCompPrivateStruct->nVersion.s.nVersionMinor 		   = 0xF2;
772    pCompPrivateStruct->nPortIndex                         = NBAPP_INPUT_PORT;
773    pCompPrivateStruct->eDir                               = OMX_DirInput;
774    pCompPrivateStruct->nBufferCountActual                 = numInputBuffers;
775    pCompPrivateStruct->nBufferCountMin                    = numInputBuffers;
776    pCompPrivateStruct->nBufferSize                        = NBAPP_INPUT_BUFFER_SIZE;
777    pCompPrivateStruct->bEnabled                           = OMX_TRUE;
778    pCompPrivateStruct->bPopulated                         = OMX_FALSE;
779    pCompPrivateStruct->eDomain                            = OMX_PortDomainAudio;
780	pCompPrivateStruct->format.audio.eEncoding             = OMX_AUDIO_CodingAMR;
781	pCompPrivateStruct->format.audio.cMIMEType             = NULL;
782	pCompPrivateStruct->format.audio.pNativeRender         = NULL;
783	pCompPrivateStruct->format.audio.bFlagErrorConcealment = OMX_FALSE;    /*Send input port config*/
784	APP_DPRINT("%d :: Setting input port config\n",__LINE__);
785	if(!(strcmp(argv[8],"NONMIME"))) {
786		pCompPrivateStruct->format.audio.cMIMEType = "NONMIME";
787		FrameMode = 0;
788		pAmrParam->eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatConformance;
789    	APP_DPRINT("\n%d :: App: pCompPrivateStruct->format.audio.cMIMEType --> %s \n",
790    											__LINE__,pCompPrivateStruct->format.audio.cMIMEType);
791	}else if(!(strcmp(argv[8],"MIME"))) {
792    	pCompPrivateStruct->format.audio.cMIMEType = "MIME";
793		FrameMode = 1;
794		pAmrParam->eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
795    	APP_DPRINT("\n%d :: App: pCompPrivateStruct->format.audio.cMIMEType --> %s \n",
796    											__LINE__,pCompPrivateStruct->format.audio.cMIMEType);
797	}
798	else if(!(strcmp(argv[8],"IF2"))) {
799    	pCompPrivateStruct->format.audio.cMIMEType = "IF2";
800		FrameMode = 2;
801		pAmrParam->eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatIF2;
802    	APP_DPRINT("\n%d :: App: pCompPrivateStruct->format.audio.cMIMEType --> %s \n",
803    											__LINE__,pCompPrivateStruct->format.audio.cMIMEType);
804	}
805    else {
806		eError = OMX_ErrorBadParameter;
807		APP_IPRINT("%d :: App: Should Be One of these Modes MIME, NONMIME\n",__LINE__);
808		goto EXIT;
809	}
810
811	APP_DPRINT("%d :: Setting input port config\n",__LINE__);
812/*	if(!(strcmp(argv[5],"EFR"))) {
813		pAmrParam->eAMRDTXMode = OMX_AUDIO_AMRDTXasEFR;
814
815    	APP_DPRINT("\n%d :: App: pCompPrivateStruct->EFR = %s \n",__LINE__,argv[5]);
816	} else if(!(strcmp(argv[5],"NBAMR"))) {
817		pAmrParam->eAMRDTXMode = OMX_AUDIO_AMRDTXModeOff;
818
819    	APP_DPRINT("\n%d :: App: pCompPrivateStruct->NBAMR = %s \n",__LINE__,argv[5]);
820	} else {
821		eError = OMX_ErrorBadParameter;
822		printf("%d :: App: Should Be One of these Modes EFR, NBAMR\n",__LINE__);
823		goto EXIT;
824	}
825*/
826
827	if(!(strcmp(argv[4],"FM"))) {
828		audioinfo->dasfMode = 0;
829		DasfMode = 0;
830    	APP_DPRINT("\n%d :: App: audioinfo->dasfMode = %x \n",__LINE__,audioinfo->dasfMode);
831	} else if(!(strcmp(argv[4],"DM"))){
832		 audioinfo->dasfMode =  1;
833		 DasfMode = 1;
834    	 APP_DPRINT("\n%d :: App: audioinfo->dasfMode = %x \n",__LINE__,audioinfo->dasfMode);
835		 APP_DPRINT("%d :: NBAMR ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
836		 pCompPrivateStruct->nBufferCountActual = 0;
837	}
838	else if(!(strcmp(argv[4],"TMP"))){
839		 audioinfo->dasfMode =  1;
840		 DasfMode = 1;
841		 audioinfo->teeMode = TEEMODE_PLAYBACK;
842		 TeeMode = TEEMODE_PLAYBACK;
843    	 APP_DPRINT("\n%d :: App: audioinfo->dasfMode = %x \n",__LINE__,audioinfo->dasfMode);
844		 APP_DPRINT("%d :: NBAMR ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
845		 pCompPrivateStruct->nBufferCountActual = 0;
846	}
847	else if(!(strcmp(argv[4],"TML"))){
848		 audioinfo->dasfMode =  1;
849		 DasfMode = 1;
850 		 audioinfo->teeMode = TEEMODE_LOOPBACK;
851		 TeeMode = TEEMODE_LOOPBACK;
852    	 APP_DPRINT("\n%d :: App: audioinfo->dasfMode = %x \n",__LINE__,audioinfo->dasfMode);
853		 APP_DPRINT("%d :: NBAMR ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
854		 pCompPrivateStruct->nBufferCountActual = 0;
855	}
856	else if(!(strcmp(argv[4],"TMLP"))){
857		 audioinfo->dasfMode =  1;
858 		 audioinfo->teeMode = TEEMODE_PLAYLOOPBACK;
859		 DasfMode = 1;
860		 TeeMode = TEEMODE_PLAYLOOPBACK;
861    	 APP_DPRINT("\n%d :: App: audioinfo->dasfMode = %x \n",__LINE__,audioinfo->dasfMode);
862		 APP_DPRINT("%d :: NBAMR ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
863		 pCompPrivateStruct->nBufferCountActual = 0;
864	}
865	else {
866		eError = OMX_ErrorBadParameter;
867		APP_IPRINT("\n%d :: App: audioinfo->dasfMode Sending Bad Parameter\n",__LINE__);
868		APP_IPRINT("%d :: App: Should Be One of these Modes FM, DM\n",__LINE__);
869		goto EXIT;
870	}
871
872	if(audioinfo->dasfMode == 0) {
873		if((atoi(argv[10])) != 0) {
874			eError = OMX_ErrorBadParameter;
875			APP_IPRINT("\n%d :: App: No. of Frames Sending Bad Parameter\n",__LINE__);
876			APP_IPRINT("%d :: App: For FILE mode argv[10] Should Be --> 0\n",__LINE__);
877			APP_IPRINT("%d :: App: For DASF mode argv[10] Should be greater than zero depends on number of frames user want to encode\n",__LINE__);
878			goto EXIT;
879		}
880	} else {
881		if((atoi(argv[10])) == 0) {
882			eError = OMX_ErrorBadParameter;
883			APP_IPRINT("\n%d :: App: No. of Frames Sending Bad Parameter\n",__LINE__);
884			APP_IPRINT("%d :: App: For DASF mode argv[10] Should be greater than zero depends on number of frames user want to encode\n",__LINE__);
885			APP_IPRINT("%d :: App: For FILE mode argv[10] Should Be --> 0\n",__LINE__);
886			goto EXIT;
887		}
888	}
889
890	if(!(strcmp(argv[9],"ACDNOFF"))) {
891		audioinfo->acousticMode = 0;
892    	APP_DPRINT("\n%d :: App: audioinfo->acousticMode = %x \n",__LINE__,audioinfo->acousticMode);
893	} else if(!(strcmp(argv[9],"ACDNON"))) {
894		audioinfo->acousticMode = 1;
895		APP_DPRINT("\n%d :: App: audioinfo->acdnacousticModeMode = %x \n",__LINE__,audioinfo->acousticMode);
896	} else {
897		eError = OMX_ErrorBadParameter;
898		APP_IPRINT("\n%d :: App: audioinfo->acdnMode Sending Bad Parameter\n",__LINE__);
899		APP_IPRINT("%d :: App: Should Be One of these Modes ACDNON, ACDNOFF\n",__LINE__);
900		goto EXIT;
901	}
902#ifdef OMX_GETTIME
903	GT_START();
904    eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
905	GT_END("Set Parameter Test-SetParameter");
906#else
907    eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
908#endif
909    if (eError != OMX_ErrorNone) {
910        eError = OMX_ErrorBadParameter;
911        APP_DPRINT("%d :: OMX_ErrorBadParameter\n",__LINE__);
912        goto EXIT;
913    }
914    APP_MEMPRINT("%d :: Setting output port config\n",__LINE__);
915    pCompPrivateStruct->nSize 							   = sizeof (OMX_PARAM_PORTDEFINITIONTYPE);
916    pCompPrivateStruct->nVersion.s.nVersionMajor           = 0xF1;
917    pCompPrivateStruct->nVersion.s.nVersionMinor 		   = 0xF2;
918    pCompPrivateStruct->nPortIndex                         = NBAPP_OUTPUT_PORT;
919    pCompPrivateStruct->eDir                               = OMX_DirOutput;
920    pCompPrivateStruct->nBufferCountActual                 = numOutputBuffers;
921    pCompPrivateStruct->nBufferCountMin                    = numOutputBuffers;
922    pCompPrivateStruct->nBufferSize                        = NBAPP_OUTPUT_BUFFER_SIZE;
923    pCompPrivateStruct->bEnabled                           = OMX_TRUE;
924    pCompPrivateStruct->bPopulated                         = OMX_FALSE;
925    pCompPrivateStruct->eDomain                            = OMX_PortDomainAudio;
926	pCompPrivateStruct->format.audio.eEncoding             = OMX_AUDIO_CodingAMR;
927	pCompPrivateStruct->format.audio.cMIMEType             = NULL;
928	pCompPrivateStruct->format.audio.pNativeRender         = NULL;
929	pCompPrivateStruct->format.audio.bFlagErrorConcealment = OMX_FALSE;    /*Send input port config*/
930#ifdef OMX_GETTIME
931	GT_START();
932	eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
933	GT_END("Set Parameter Test-SetParameter");
934#else
935	eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
936#endif
937    if (eError != OMX_ErrorNone) {
938        eError = OMX_ErrorBadParameter;
939        APP_DPRINT("%d :: OMX_ErrorBadParameter\n",__LINE__);
940        goto EXIT;
941    }
942
943    pAmrParam->nSize                    = sizeof(OMX_AUDIO_PARAM_AMRTYPE);
944    pAmrParam->nVersion.s.nVersionMajor = 0xF1;
945    pAmrParam->nVersion.s.nVersionMinor = 0xF2;
946    pAmrParam->nPortIndex               = NBAPP_OUTPUT_PORT;
947    pAmrParam->nChannels                = NBAPP_NUM_OF_CHANNELS;
948	pAmrParam->eAMRBandMode 			= 0;
949	if(!(strcmp(argv[6],"BR122"))) {
950		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB7;
951    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
952	} else if(!(strcmp(argv[6],"BR102"))) {
953		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB6;
954    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
955	} else if(!(strcmp(argv[6],"BR795"))) {
956		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB5;
957    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
958	} else if(!(strcmp(argv[6],"BR74"))) {
959		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB4;
960    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
961	} else if(!(strcmp(argv[6],"BR67"))) {
962		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB3;
963    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
964	} else if(!(strcmp(argv[6],"BR59"))) {
965		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB2;
966    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
967	} else if(!(strcmp(argv[6],"BR515"))) {
968		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB1;
969    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
970	} else if(!(strcmp(argv[6],"BR475"))) {
971		pAmrParam->eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
972    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode = %d \n",__LINE__,pAmrParam->eAMRBandMode);
973	} else {
974		eError = OMX_ErrorBadParameter;
975		APP_IPRINT("\n%d :: App: pAmrParam->eAMRBandMode Sending Bad Parameter\n",__LINE__);
976		APP_IPRINT("%d :: App: Should Be One of these BitRates BR122, BR102, BR795, BR74, BR67, BR59, BR515, BR475\n",__LINE__);
977		goto EXIT;
978	}
979
980    APP_DPRINT("\n%d :: App: pAmrParam->eAMRBandMode --> %d \n",__LINE__,pAmrParam->eAMRBandMode);
981
982    if(!(strcmp(argv[7],"DTXON"))) {
983    	/**< AMR Discontinuous Transmission Mode is enabled  */
984    	pAmrParam->eAMRDTXMode = OMX_AUDIO_AMRDTXModeOnAuto;
985    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRDTXMode --> %s \n",__LINE__,argv[7]);
986	}else if(!(strcmp(argv[7],"DTXOFF"))) {
987		/**< AMR Discontinuous Transmission Mode is disabled */
988		pAmrParam->eAMRDTXMode = OMX_AUDIO_AMRDTXModeOff;
989    	APP_DPRINT("\n%d :: App: pAmrParam->eAMRDTXMode --> %s \n",__LINE__,argv[7]);
990	} else {
991		eError = OMX_ErrorBadParameter;
992		APP_IPRINT("\n%d :: App: pAmrParam->eAMRDTXMode Sending Bad Parameter\n",__LINE__);
993		APP_IPRINT("%d :: App: Should Be One of these Modes DTXON, DTXOFF\n",__LINE__);
994		goto EXIT;
995	}
996
997	APP_DPRINT("%d :: Setting input port config\n",__LINE__);
998	if(!(strcmp(argv[5],"EFR"))) {
999		pAmrParam->eAMRDTXMode = OMX_AUDIO_AMRDTXasEFR;
1000    	APP_DPRINT("\n%d :: App: pCompPrivateStruct->EFR = %s \n",__LINE__,argv[5]);
1001	} else if(!(strcmp(argv[5],"NBAMR"))) {
1002        /*Do nothing, leave eAMRDTXMode as specified before*/
1003    	APP_DPRINT("\n%d :: App: pCompPrivateStruct->NBAMR = %s \n",__LINE__,argv[5]);
1004	}
1005	else {
1006		eError = OMX_ErrorBadParameter;
1007		APP_IPRINT("\n%d :: App: pAmrParam->eAMRDTXMode Sending Bad Parameter\n",__LINE__);
1008		APP_IPRINT("%d :: App: Should Be One of these Modes NBAMR, EFR\n",__LINE__);
1009		goto EXIT;
1010	}
1011
1012#ifdef OMX_GETTIME
1013	GT_START();
1014    eError = OMX_SetParameter (pHandle, OMX_IndexParamAudioAmr, pAmrParam);
1015	GT_END("Set Parameter Test-SetParameter");
1016#else
1017    eError = OMX_SetParameter (pHandle, OMX_IndexParamAudioAmr, pAmrParam);
1018#endif
1019    if (eError != OMX_ErrorNone) {
1020        eError = OMX_ErrorBadParameter;
1021        APP_DPRINT("%d :: OMX_ErrorBadParameter\n",__LINE__);
1022        goto EXIT;
1023    }
1024
1025#ifndef USE_BUFFER
1026	APP_DPRINT("%d :: About to call OMX_AllocateBuffer\n",__LINE__);
1027	for(i = 0; i < numInputBuffers; i++) {
1028		/* allocate input buffer */
1029		APP_DPRINT("%d :: About to call OMX_AllocateBuffer for pInputBufferHeader[%d]\n",__LINE__, i);
1030		eError = OMX_AllocateBuffer(pHandle, &pInputBufferHeader[i], 0, NULL, NBAPP_INPUT_BUFFER_SIZE*3);/*To allow two frames by buffer*/
1031		if(eError != OMX_ErrorNone) {
1032			APP_DPRINT("%d :: Error returned by OMX_AllocateBuffer for pInputBufferHeader[%d]\n",__LINE__, i);
1033			goto EXIT;
1034		}
1035	}
1036	APP_DPRINT("\n%d :: App: pCompPrivateStruct->nBufferSize --> %ld \n",__LINE__,pCompPrivateStruct->nBufferSize);
1037	for(i = 0; i < numOutputBuffers; i++) {
1038		/* allocate output buffer */
1039		APP_DPRINT("%d :: About to call OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
1040		eError = OMX_AllocateBuffer(pHandle, &pOutputBufferHeader[i], 1, NULL, NBAPP_OUTPUT_BUFFER_SIZE*3);
1041		if(eError != OMX_ErrorNone) {
1042			APP_DPRINT("%d :: Error returned by OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
1043			goto EXIT;
1044		}
1045	}
1046#else
1047	for(i = 0; i < numInputBuffers; i++) {
1048		pInputBuffer[i] = (OMX_U8*)newmalloc(NBAPP_INPUT_BUFFER_SIZE*3 + 256);
1049		APP_MEMPRINT("%d :: [TESTAPP ALLOC] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
1050		if(NULL == pInputBuffer[i]) {
1051			APP_DPRINT("%d :: Malloc Failed\n",__LINE__);
1052			eError = OMX_ErrorInsufficientResources;
1053			goto EXIT;
1054		}
1055		pInputBuffer[i] = pInputBuffer[i] + 128;
1056
1057		/*	allocate input buffer */
1058		APP_DPRINT("%d :: About to call OMX_UseBuffer\n",__LINE__);
1059		eError = OMX_UseBuffer(pHandle, &pInputBufferHeader[i], 0, NULL, NBAPP_INPUT_BUFFER_SIZE*13, pInputBuffer[i]);
1060		if(eError != OMX_ErrorNone) {
1061			APP_DPRINT("%d :: Error returned by OMX_UseBuffer()\n",__LINE__);
1062			goto EXIT;
1063		}
1064	}
1065
1066	for(i = 0; i < numOutputBuffers; i++) {
1067		pOutputBuffer[i] = newmalloc (NBAPP_OUTPUT_BUFFER_SIZE*3 + 256);
1068		APP_MEMPRINT("%d :: [TESTAPP ALLOC] pOutputBuffer[%d] = %p\n",__LINE__,i,pOutputBuffer[i]);
1069		if(NULL == pOutputBuffer[i]) {
1070			APP_DPRINT("%d :: Malloc Failed\n",__LINE__);
1071			eError = OMX_ErrorInsufficientResources;
1072			goto EXIT;
1073		}
1074		pOutputBuffer[i] = pOutputBuffer[i] + 128;
1075
1076		/* allocate output buffer */
1077		APP_DPRINT("%d :: About to call OMX_UseBuffer\n",__LINE__);
1078		eError = OMX_UseBuffer(pHandle, &pOutputBufferHeader[i], 1, NULL, NBAPP_OUTPUT_BUFFER_SIZE*13, pOutputBuffer[i]);
1079		if(eError != OMX_ErrorNone) {
1080			APP_DPRINT("%d :: Error returned by OMX_UseBuffer()\n",__LINE__);
1081			goto EXIT;
1082		}
1083	}
1084#endif
1085
1086
1087		pCompPrivateStructGain = newmalloc (sizeof(OMX_AUDIO_CONFIG_VOLUMETYPE));
1088		if(pCompPrivateStructGain == NULL)
1089		{
1090			APP_DPRINT("%d :: App: Malloc Failed\n",__LINE__);
1091			goto EXIT;
1092		}
1093        ArrayOfPointers[4] = (OMX_AUDIO_CONFIG_VOLUMETYPE*) pCompPrivateStructGain;
1094
1095		/* default setting for gain */
1096		pCompPrivateStructGain->nSize = sizeof(OMX_AUDIO_CONFIG_VOLUMETYPE);
1097		pCompPrivateStructGain->nVersion.s.nVersionMajor	= 0xF1;
1098		pCompPrivateStructGain->nVersion.s.nVersionMinor	= 0xF2;
1099		pCompPrivateStructGain->nPortIndex					= OMX_DirOutput;
1100		pCompPrivateStructGain->bLinear						= OMX_FALSE;
1101		pCompPrivateStructGain->sVolume.nValue				= 50;				/* actual volume */
1102		pCompPrivateStructGain->sVolume.nMin				= 0;				/* min volume */
1103		pCompPrivateStructGain->sVolume.nMax				= 100;				/* max volume */
1104
1105    if (audioinfo->acousticMode == OMX_TRUE) {
1106        APP_IPRINT("Using Acoustic Device Node Path\n");
1107        dataPath = DATAPATH_ACDN;
1108    }
1109    else if (audioinfo->dasfMode) {
1110#ifdef RTM_PATH
1111        APP_IPRINT("Using Real Time Mixer Path\n");
1112        dataPath = DATAPATH_APPLICATION_RTMIXER;
1113#endif
1114
1115#ifdef ETEEDN_PATH
1116        APP_IPRINT("Using Eteeden Path\n");
1117        if (TeeMode == TEEMODE_NONE) {
1118            dataPath = DATAPATH_APPLICATION;
1119        }
1120        else {
1121            dataPath = DATAPATH_APPLICATION_TEE;
1122        }
1123#endif
1124    }
1125
1126	eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.tispecific",&index);
1127	if (eError != OMX_ErrorNone) {
1128		APP_DPRINT("Error returned from OMX_GetExtensionIndex\n");
1129		goto EXIT;
1130	}
1131
1132#ifdef DSP_RENDERING_ON
1133    cmd_data.hComponent = pHandle;
1134    cmd_data.AM_Cmd = AM_CommandIsInputStreamAvailable;
1135
1136    cmd_data.param1 = 0;
1137    if((write(nbamrencdfwrite, &cmd_data, sizeof(cmd_data)))<0) {
1138        APP_IPRINT("%d ::OMX_AmrDecoder.c ::[NBAMR Dec Component] - send command to audio manager\n", __LINE__);
1139    }
1140    if((read(nbamrencfdread, &cmd_data, sizeof(cmd_data)))<0) {
1141        APP_IPRINT("%d ::OMX_AmrDecoder.c ::[NBAMR Dec Component] - failure to get data from the audio manager\n", __LINE__);
1142		goto EXIT;
1143    }
1144    audioinfo->streamId = cmd_data.streamID;
1145    streamId = audioinfo->streamId;
1146#endif
1147
1148	eError = OMX_SetConfig (pHandle, index, audioinfo);
1149    if(eError != OMX_ErrorNone) {
1150        eError = OMX_ErrorBadParameter;
1151        APP_DPRINT("%d :: Error from OMX_SetConfig() function\n",__LINE__);
1152        goto EXIT;
1153    }
1154
1155    eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.nbamr.datapath",&index);
1156	if (eError != OMX_ErrorNone) {
1157		APP_IPRINT("Error getting extension index\n");
1158		goto EXIT;
1159	}
1160
1161	eError = OMX_SetConfig (pHandle, index, &dataPath);
1162    if(eError != OMX_ErrorNone) {
1163        eError = OMX_ErrorBadParameter;
1164        APP_DPRINT("%d :: AmrDecTest.c :: Error from OMX_SetConfig() function\n",__LINE__);
1165        goto EXIT;
1166    }
1167#ifdef OMX_GETTIME
1168	GT_START();
1169#endif
1170	eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
1171    if(eError != OMX_ErrorNone) {
1172        APP_DPRINT("Error from SendCommand-Idle(Init) State function\n");
1173        goto EXIT;
1174    }
1175
1176    /* Wait for startup to complete */
1177
1178    eError = WaitForState(pHandle, OMX_StateIdle);
1179#ifdef OMX_GETTIME
1180	GT_END("Call to SendCommand <OMX_StateIdle>");
1181#endif
1182    if(eError != OMX_ErrorNone) {
1183        APP_DPRINT( "Error:  hAmrEncoder->WaitForState reports an eError %X\n", eError);
1184        goto EXIT;
1185    }
1186
1187   if (audioinfo->dasfMode ==  1) {
1188        /* get streamID back to application */
1189	    eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.nbamrstreamIDinfo",&index);
1190     	if (eError != OMX_ErrorNone) {
1191      		APP_IPRINT("Error getting extension index\n");
1192        		goto EXIT;
1193          	}
1194
1195       	eError = OMX_GetConfig (pHandle, index, streaminfo);
1196        if(eError != OMX_ErrorNone) {
1197            eError = OMX_ErrorBadParameter;
1198            APP_DPRINT("%d :: AmrEncTest.c :: Error from OMX_GetConfig() function\n",__LINE__);
1199            goto EXIT;
1200        }
1201
1202        streamId = streaminfo->streamId;
1203        APP_IPRINT("***************StreamId=%ld******************\n", streamId);
1204    }
1205
1206    for(i = 0; i < testcnt; i++) {
1207	frmCnt = 1;
1208	nFrameCount = 1;
1209	nOutBuff = 1;
1210	if(i > 0) {
1211		APP_IPRINT("%d :: Encoding the file for %d Time in TESTCASE 5 OR TESTCSE 4\n",__LINE__,i+1);
1212		fIn = fopen(argv[1], "r");
1213		if(fIn == NULL) {
1214			fprintf(stderr, "Error:  failed to open the file %s for readonly access\n", argv[1]);
1215			goto EXIT;
1216		}
1217		fOut = fopen("TC5_Amr1.amr", "w");
1218		if(fOut == NULL) {
1219			fprintf(stderr, "Error:  failed to create the output file %s\n", argv[2]);
1220			goto EXIT;
1221		}
1222	}
1223
1224	APP_IPRINT("%d :: App: Sending OMX_StateExecuting Command\n",__LINE__);
1225#ifdef OMX_GETTIME
1226	GT_START();
1227#endif
1228    eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
1229    if(eError != OMX_ErrorNone) {
1230        APP_IPRINT("Error from SendCommand-Executing State function\n");
1231        goto EXIT;
1232    }
1233    eError = WaitForState(pHandle, OMX_StateExecuting);
1234#ifdef OMX_GETTIME
1235	GT_END("Call to SendCommand <OMX_StateExecuting>");
1236#endif
1237    if(eError != OMX_ErrorNone) {
1238        APP_DPRINT( "Error:  hAmrEncoder->WaitForState reports an eError %X\n", eError);
1239        goto EXIT;
1240    }
1241
1242    if (audioinfo->dasfMode ==  1)
1243    {
1244		APP_IPRINT("%d :: App: No.of Frames Encoding = %d\n",__LINE__, atoi(argv[10]));
1245	}
1246
1247    pComponent = (OMX_COMPONENTTYPE *)pHandle;
1248
1249	if(audioinfo->dasfMode == 0) {
1250		for (k=0; k < numInputBuffers; k++) {
1251			OMX_BUFFERHEADERTYPE* pBuffer = pInputBufferHeader[k];
1252			pBuffer->nFlags=0;
1253		#ifdef OMX_GETTIME
1254			if (k==0)
1255			{
1256				GT_FlagE=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
1257				GT_START(); /* Empty Bufffer */
1258			}
1259		#endif
1260            eError =  send_input_buffer(pHandle, pBuffer, fIn);
1261		}
1262	}
1263
1264	for (kk = 0; kk < numOutputBuffers; kk++) {
1265		APP_DPRINT("%d :: App: Calling FillThisBuffer \n",__LINE__);
1266	#ifdef OMX_GETTIME
1267         if (kk==0)
1268         {
1269            GT_FlagF=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
1270            GT_START(); /* Fill Buffer */
1271         }
1272	#endif
1273		pComponent->FillThisBuffer(pHandle, pOutputBufferHeader[kk]);
1274	}
1275
1276	eError = pComponent->GetState(pHandle, &state);
1277	if(eError != OMX_ErrorNone) {
1278		APP_DPRINT("%d :: pComponent->GetState has returned status %X\n",__LINE__, eError);
1279		goto EXIT;
1280	}
1281    retval = 1;
1282
1283#ifndef WAITFORRESOURCES
1284    while((eError == OMX_ErrorNone) && (state != OMX_StateIdle) && (state != OMX_StateInvalid) ){
1285	if(1){
1286#else
1287    while(1) {
1288        if((eError == OMX_ErrorNone) && (state != OMX_StateIdle) && (state != OMX_StateInvalid) ){
1289#endif
1290        FD_ZERO(&rfds);
1291        FD_SET(IpBuf_Pipe[0], &rfds);
1292        FD_SET(OpBuf_Pipe[0], &rfds);
1293		FD_SET(Event_Pipe[0], &rfds);
1294
1295        tv.tv_sec = 1;
1296        tv.tv_usec = 0;
1297		frmCount++;
1298
1299        retval = select(fdmax+1, &rfds, NULL, NULL, &tv);
1300        if(retval == -1) {
1301            perror("select()");
1302            APP_DPRINT( " :: Error \n");
1303            break;
1304        }
1305
1306        if(!retval){
1307              NoDataRead++;
1308              if(NoDataRead==2){
1309                      APP_IPRINT("Stoping component since No data is read from the pipes\n");
1310                      StopComponent(pHandle);
1311                      }
1312              }
1313        else{
1314                 NoDataRead=0;
1315             }
1316
1317		switch (tcID) {
1318		case 1:
1319		case 2:
1320		case 3:
1321        case 4:
1322		case 5:
1323		case 6:
1324        case 7:
1325			if(audioinfo->dasfMode == 0) {
1326				if(FD_ISSET(IpBuf_Pipe[0], &rfds)) {
1327					OMX_BUFFERHEADERTYPE* pBuffer;
1328					read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1329					if (frmCount==15 && tcID ==3){ /*Pause the component*/
1330						APP_IPRINT("App: Pausing Component for 5 Seconds\n");
1331						PauseComponent(pHandle);
1332						sleep(2);
1333						APP_IPRINT("App: Resume Component\n");
1334						PlayComponent(pHandle);
1335					}
1336					if (frmCount==20 && tcID ==4){ /*Stop the component*/
1337                        tcID = 1;
1338						StopComponent(pHandle);
1339						break;
1340                    }
1341					eError =  send_input_buffer(pHandle, pBuffer, fIn);
1342				}
1343			} else {
1344                   if (frmCount==15 && tcID ==3){  /*Pause the component*/
1345                        tcID = 1;
1346						APP_IPRINT("App: Pausing Component for 5 Seconds\n");
1347						PauseComponent(pHandle);
1348						sleep(2);
1349						APP_IPRINT("App: Resume Component\n");
1350						PlayComponent(pHandle);
1351					}
1352					if (nFrameCount==50 && tcID ==4){ /*Stop the component*/
1353					    APP_IPRINT("Stoping the Component And Starting Again\n");
1354						StopComponent(pHandle);
1355						nFrameCount = 0;
1356						break;
1357                    }
1358					if(nFrameCount == 10 && tcID == 7)
1359					{
1360							/* set high gain for record stream */
1361							APP_IPRINT("[NBAMR encoder] --- will set stream gain to high\n");
1362            				pCompPrivateStructGain->sVolume.nValue = 0x8000;
1363							eError = OMX_SetConfig(pHandle, OMX_IndexConfigAudioVolume, pCompPrivateStructGain);
1364							if (eError != OMX_ErrorNone)
1365							{
1366								eError = OMX_ErrorBadParameter;
1367								goto EXIT;
1368							}
1369					}
1370					if(nFrameCount == 250 && tcID == 7)
1371					{
1372							/* set low gain for record stream */
1373							APP_IPRINT("[NBAMR encoder] --- will set stream gain to low\n");
1374            				pCompPrivateStructGain->sVolume.nValue = 0x2000;
1375							eError = OMX_SetConfig(pHandle, OMX_IndexConfigAudioVolume, pCompPrivateStructGain);
1376							if (eError != OMX_ErrorNone)
1377							{
1378								eError = OMX_ErrorBadParameter;
1379								goto EXIT;
1380							}
1381					}
1382					APP_DPRINT("%d :: NBAMR ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
1383					if(nFrameCount == atoi(argv[10])) {
1384						StopComponent(pHandle);
1385					}
1386					APP_DPRINT("%d :: NBAMR ENCODER READING DATA FROM DASF  \n",__LINE__);
1387				}
1388		break;
1389		default:
1390				APP_DPRINT("%d :: ### Simple DEFAULT Case Here ###\n",__LINE__);
1391		}
1392
1393        if( FD_ISSET(OpBuf_Pipe[0], &rfds) ) {
1394            OMX_BUFFERHEADERTYPE* pBuf;
1395            read(OpBuf_Pipe[0], &pBuf, sizeof(pBuf));
1396            APP_DPRINT("%d :: App: pBuf->nFilledLen = %ld\n",__LINE__, pBuf->nFilledLen);
1397            nFrameLen = pBuf->nFilledLen;
1398            if(FrameMode==1) { /* Mime Mode */
1399	            if(1 == nFrameCount) {
1400                   char MimeHeader[] = {0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a};
1401                   fwrite(MimeHeader, 1, NBAMRENC_MIME_HEADER_LEN, fOut);
1402	               fflush(fOut);
1403                   APP_IPRINT("%d :: App: MIME Supported:: FrameLen = %d\n",__LINE__, nFrameLen);
1404                }
1405            }
1406			APP_DPRINT("%d :: App: nFrameLen = %d \n",__LINE__, nFrameLen);
1407            if (nFrameLen != 0) {
1408				APP_DPRINT("%d :: Writing OutputBuffer No: %d to the file nWrite = %d \n",__LINE__, nOutBuff, nFrameLen);
1409
1410				fwrite(pBuf->pBuffer, 1, nFrameLen, fOut);
1411				fflush(fOut);
1412			}
1413			if(pBuf->nFlags == OMX_BUFFERFLAG_EOS) {
1414				   APP_IPRINT("%d :: App: OMX_BUFFERFLAG_EOS is received\n",__LINE__);
1415				   APP_IPRINT("%d :: App: Shutting down ---------- \n",__LINE__);
1416				   StopComponent(pHandle);
1417				   pBuf->nFlags = 0;
1418			}
1419			else{
1420				nFrameCount++;
1421				nOutBuff++;
1422                pComponent->FillThisBuffer(pHandle, pBuf);
1423				APP_DPRINT("%d :: App: pBuf->nFlags = %ld\n",__LINE__, pBuf->nFlags);
1424			}
1425        }
1426
1427
1428if( FD_ISSET(Event_Pipe[0], &rfds) ) {
1429                OMX_U8 pipeContents;
1430                read(Event_Pipe[0], &pipeContents, sizeof(OMX_U8));
1431
1432                if (pipeContents == 0) {
1433                    APP_IPRINT("Test app received OMX_ErrorResourcesPreempted\n");
1434                    WaitForState(pHandle,OMX_StateIdle);
1435
1436				for(i=0; i < numInputBuffers; i++) {
1437					APP_DPRINT("%d :: App: About to newfree pInputBufferHeader[%d]\n",__LINE__, i);
1438					eError = OMX_FreeBuffer(pHandle, NBAPP_INPUT_PORT, pInputBufferHeader[i]);
1439					if((eError != OMX_ErrorNone)) {
1440						APP_DPRINT("%d:: Error in FreeBuffer function\n",__LINE__);
1441						goto EXIT;
1442					}
1443
1444				}
1445
1446				for(i=0; i < numOutputBuffers; i++) {
1447					APP_DPRINT("%d :: App: About to newfree pOutputBufferHeader[%d]\n",__LINE__, i);
1448					eError = OMX_FreeBuffer(pHandle, NBAPP_OUTPUT_PORT, pOutputBufferHeader[i]);
1449					if((eError != OMX_ErrorNone)) {
1450						APP_DPRINT("%d :: Error in Free Buffer function\n",__LINE__);
1451						goto EXIT;
1452					}
1453
1454			    }
1455
1456#ifdef USE_BUFFER
1457
1458		for(i=0; i < numInputBuffers; i++) {
1459			if(pInputBuffer[i] != NULL){
1460	       		APP_MEMPRINT("%d :: App: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
1461	            pInputBuffer[i] = pInputBuffer[i] - 128;
1462				newfree(pInputBuffer[i]);
1463				pInputBuffer[i] = NULL;
1464			}
1465		}
1466#endif
1467
1468                	OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
1469                    WaitForState(pHandle, OMX_StateLoaded);
1470
1471                    OMX_SendCommand(pHandle,OMX_CommandStateSet,OMX_StateWaitForResources,NULL);
1472                    WaitForState(pHandle,OMX_StateWaitForResources);
1473                }
1474                else if (pipeContents == 1) {
1475                    APP_IPRINT("Test app received OMX_ErrorResourcesAcquired\n");
1476
1477                    OMX_SendCommand(pHandle,OMX_CommandStateSet,OMX_StateIdle,NULL);
1478                    	for(i = 0; i < numOutputBuffers; i++) {
1479							/* allocate output buffer */
1480							APP_DPRINT("%d :: About to call OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
1481							eError = OMX_AllocateBuffer(pHandle, &pOutputBufferHeader[i], 1, NULL, NBAPP_OUTPUT_BUFFER_SIZE*3);
1482							if(eError != OMX_ErrorNone) {
1483								APP_DPRINT("%d :: Error returned by OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
1484								goto EXIT;
1485							}
1486						}
1487
1488                    WaitForState(pHandle,OMX_StateIdle);
1489
1490                    OMX_SendCommand(pHandle,OMX_CommandStateSet,OMX_StateExecuting,NULL);
1491                    WaitForState(pHandle,OMX_StateExecuting);
1492
1493                    rewind(fIn);
1494
1495                    for(i = 0; i < numOutputBuffers; i++) {
1496						send_input_buffer (pHandle, pOutputBufferHeader[i], fIn);
1497                    }
1498                }
1499
1500                if (pipeContents == 2) {
1501
1502#ifdef OMX_GETTIME
1503                    GT_START();
1504#endif
1505
1506                    OMX_SendCommand(pHandle,OMX_CommandStateSet,OMX_StateIdle,NULL);
1507                    WaitForState(pHandle,OMX_StateIdle);
1508
1509#ifdef OMX_GETTIME
1510                    GT_END("Call to SendCommand <OMX_StateIdle>");
1511#endif
1512
1513#ifdef WAITFORRESOURCES
1514					for(i=0; i < numInputBuffers; i++) {
1515						APP_DPRINT("%d :: App: About to newfree pInputBufferHeader[%d]\n",__LINE__, i);
1516						eError = OMX_FreeBuffer(pHandle, NBAPP_INPUT_PORT, pInputBufferHeader[i]);
1517						if((eError != OMX_ErrorNone)) {
1518							APP_DPRINT("%d:: Error in FreeBuffer function\n",__LINE__);
1519							goto EXIT;
1520						}
1521
1522					}
1523
1524					for(i=0; i < numOutputBuffers; i++) {
1525						APP_DPRINT("%d :: App: About to newfree pOutputBufferHeader[%d]\n",__LINE__, i);
1526						eError = OMX_FreeBuffer(pHandle, NBAPP_OUTPUT_PORT, pOutputBufferHeader[i]);
1527						if((eError != OMX_ErrorNone)) {
1528							APP_DPRINT("%d :: Error in Free Buffer function\n",__LINE__);
1529							goto EXIT;
1530						}
1531
1532				    }
1533
1534                	OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
1535                    WaitForState(pHandle, OMX_StateLoaded);
1536
1537                    goto SHUTDOWN;
1538#endif
1539                }
1540            }
1541
1542
1543
1544
1545            eError = pComponent->GetState(pHandle, &state);
1546            if(eError != OMX_ErrorNone) {
1547                APP_DPRINT("%d :: pComponent->GetState has returned status %X\n",__LINE__, eError);
1548                goto EXIT;
1549            }
1550
1551 }
1552            else if (preempted) {
1553                sched_yield();
1554            }
1555            else {
1556                goto SHUTDOWN;
1557            }
1558
1559    } /* While Loop Ending Here */
1560    	APP_IPRINT("%d :: App: The current state of the component = %d \n",__LINE__,state);
1561    	fclose(fOut);
1562    	fclose(fIn);
1563        FirstTime = 1;
1564        NoDataRead = 0;
1565		if(tcID == 4)
1566		    tcID =1;
1567		APP_IPRINT("%d :: App: NBAMR Encoded = %d Frames \n",__LINE__,(nOutBuff));
1568    } /*Test Case 4 & 5 Inner for loop ends here  */
1569
1570	/* newfree the Allocate and Use Buffers */
1571	APP_IPRINT("%d :: App: Freeing the Allocate OR Use Buffers in TestApp\n",__LINE__);
1572	for(i=0; i < numInputBuffers; i++) {
1573		APP_DPRINT("%d :: App: About to newfree pInputBufferHeader[%d]\n",__LINE__, i);
1574		eError = OMX_FreeBuffer(pHandle, NBAPP_INPUT_PORT, pInputBufferHeader[i]);
1575		if((eError != OMX_ErrorNone)) {
1576			APP_DPRINT("%d:: Error in FreeBuffer function\n",__LINE__);
1577			goto EXIT;
1578		}
1579		pInputBufferHeader[i] = NULL;
1580	}
1581
1582	for(i=0; i < numOutputBuffers; i++) {
1583		APP_DPRINT("%d :: App: About to newfree pOutputBufferHeader[%d]\n",__LINE__, i);
1584		eError = OMX_FreeBuffer(pHandle, NBAPP_OUTPUT_PORT, pOutputBufferHeader[i]);
1585		if((eError != OMX_ErrorNone)) {
1586			APP_DPRINT("%d :: Error in Free Buffer function\n",__LINE__);
1587			goto EXIT;
1588		}
1589		pOutputBufferHeader[i] = NULL;
1590    }
1591
1592#ifdef USE_BUFFER
1593	/* newfree the App Allocated Buffers */
1594	APP_IPRINT("%d :: App: Freeing the App Allocated Buffers in TestApp\n",__LINE__);
1595
1596	for(i=0; i < numInputBuffers; i++) {
1597		if(pInputBuffer[i] != NULL){
1598	        APP_MEMPRINT("%d :: App: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
1599	        pInputBuffer[i] = pInputBuffer[i] - 128;
1600			newfree(pInputBuffer[i]);
1601			pInputBuffer[i] = NULL;
1602		}
1603	}
1604
1605	for(i=0; i < numOutputBuffers; i++) {
1606		if(pOutputBuffer[i] != NULL){
1607    		APP_MEMPRINT("%d :: App: [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, pOutputBuffer[i]);
1608	    	pOutputBuffer[i] = pOutputBuffer[i] - 128;
1609			newfree(pOutputBuffer[i]);
1610			pOutputBuffer[i] = NULL;
1611		}
1612	}
1613#endif
1614
1615	APP_IPRINT ("%d :: App: Sending the OMX_StateLoaded Command\n",__LINE__);
1616#ifdef OMX_GETTIME
1617	GT_START();
1618#endif
1619    eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
1620    if(eError != OMX_ErrorNone) {
1621        APP_DPRINT("%d:: Error from SendCommand-Idle State function\n",__LINE__);
1622        goto EXIT;
1623    }
1624    eError = WaitForState(pHandle, OMX_StateLoaded);
1625#ifdef OMX_GETTIME
1626	GT_END("Call to SendCommand <OMX_StateLoaded>");
1627#endif
1628    if ( eError != OMX_ErrorNone ){
1629        APP_IPRINT("Error: WaitForState has timed out %d", eError);
1630		goto EXIT;
1631    }
1632
1633	APP_IPRINT ("%d :: App: Sending the OMX_CommandPortDisable Command\n",__LINE__);
1634	eError = OMX_SendCommand(pHandle, OMX_CommandPortDisable, -1, NULL);
1635    if(eError != OMX_ErrorNone) {
1636        APP_DPRINT("%d:: Error from SendCommand OMX_CommandPortDisable\n",__LINE__);
1637        goto EXIT;
1638    }
1639
1640
1641#ifdef WAITFORRESOURCES
1642    eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateWaitForResources, NULL);
1643    if(eError != OMX_ErrorNone) {
1644        APP_DPRINT ("%d Error from SendCommand-Idle State function\n",__LINE__);
1645        goto EXIT;
1646    }
1647    eError = WaitForState(pHandle, OMX_StateWaitForResources);
1648
1649    /* temporarily put this here until I figure out what should really happen here */
1650    sleep(10);
1651    /* temporarily put this here until I figure out what should really happen here */
1652#endif
1653SHUTDOWN:
1654
1655	APP_IPRINT("%d :: App: Freeing the Memory Allocated in TestApp\n",__LINE__);
1656
1657    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pAmrParam);
1658    if(pAmrParam != NULL){
1659	    newfree(pAmrParam);
1660	    pAmrParam = NULL;
1661	}
1662    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pCompPrivateStruct);
1663	if(pCompPrivateStruct != NULL){
1664		newfree(pCompPrivateStruct);
1665		pCompPrivateStruct = NULL;
1666	}
1667    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,audioinfo);
1668	if(audioinfo != NULL){
1669		newfree(audioinfo);
1670		audioinfo = NULL;
1671	}
1672    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,streaminfo);
1673    if(streaminfo != NULL){
1674		newfree(streaminfo);
1675		streaminfo = NULL;
1676	}
1677
1678	APP_IPRINT("%d :: App: Closing the Input and Output Pipes\n",__LINE__);
1679	eError = close (IpBuf_Pipe[0]);
1680	if (0 != eError && OMX_ErrorNone == eError) {
1681		eError = OMX_ErrorHardware;
1682		APP_DPRINT("%d :: Error while closing IpBuf_Pipe[0]\n",__LINE__);
1683		goto EXIT;
1684	}
1685	eError = close (IpBuf_Pipe[1]);
1686	if (0 != eError && OMX_ErrorNone == eError) {
1687		eError = OMX_ErrorHardware;
1688		APP_DPRINT("%d :: Error while closing IpBuf_Pipe[1]\n",__LINE__);
1689		goto EXIT;
1690	}
1691	eError = close (OpBuf_Pipe[0]);
1692	if (0 != eError && OMX_ErrorNone == eError) {
1693		eError = OMX_ErrorHardware;
1694		APP_DPRINT("%d :: Error while closing OpBuf_Pipe[0]\n",__LINE__);
1695		goto EXIT;
1696	}
1697	eError = close (OpBuf_Pipe[1]);
1698	if (0 != eError && OMX_ErrorNone == eError) {
1699		eError = OMX_ErrorHardware;
1700		APP_DPRINT("%d :: Error while closing OpBuf_Pipe[1]\n",__LINE__);
1701		goto EXIT;
1702	}
1703
1704	eError = close(Event_Pipe[0]);
1705	if (0 != eError && OMX_ErrorNone == eError) {
1706		eError = OMX_ErrorHardware;
1707		APP_DPRINT("%d :: Error while closing Event_Pipe[0]\n",__LINE__);
1708		goto EXIT;
1709	}
1710
1711	eError = close(Event_Pipe[1]);
1712	if (0 != eError && OMX_ErrorNone == eError) {
1713		eError = OMX_ErrorHardware;
1714		APP_DPRINT("%d :: Error while closing Event_Pipe[1]\n",__LINE__);
1715		goto EXIT;
1716	}
1717
1718    APP_IPRINT("%d :: App: Free the Component handle\n",__LINE__);
1719    /* Unload the NBAMR Encoder Component */
1720    eError = TIOMX_FreeHandle(pHandle);
1721	if((eError != OMX_ErrorNone)) {
1722		APP_DPRINT("%d :: Error in Free Handle function\n",__LINE__);
1723		goto EXIT;
1724	}
1725	APP_IPRINT("%d :: App: Free Handle returned Successfully\n",__LINE__);
1726
1727#ifdef DSP_RENDERING_ON
1728    cmd_data.hComponent = pHandle;
1729    cmd_data.AM_Cmd = AM_Exit;
1730
1731    if((write(nbamrencdfwrite, &cmd_data, sizeof(cmd_data)))<0)
1732        APP_IPRINT("%d ::- send command to audio manager\n",__LINE__);
1733
1734    close(nbamrencdfwrite);
1735    close(nbamrencfdread);
1736#endif
1737
1738
1739
1740	newfree(pCompPrivateStructGain);
1741
1742	} /*Outer for loop ends here */
1743
1744	pthread_mutex_destroy(&WaitForState_mutex);
1745    pthread_cond_destroy(&WaitForState_threshold);
1746
1747    APP_IPRINT("%d :: *********************************************************************\n",__LINE__);
1748    APP_IPRINT("%d :: NOTE: An output file %s has been created in file system\n",__LINE__,argv[2]);
1749    APP_IPRINT("%d :: *********************************************************************\n",__LINE__);
1750EXIT:
1751	if(bInvalidState==OMX_TRUE)
1752	{
1753#ifndef USE_BUFFER
1754		eError = FreeAllResources(pHandle,
1755								pInputBufferHeader[0],
1756								pOutputBufferHeader[0],
1757								numInputBuffers,
1758								numOutputBuffers,
1759								fIn,
1760                                fOut);
1761#else
1762		eError = FreeAllResources(pHandle,
1763									pInputBuffer,
1764									pOutputBuffer,
1765									numInputBuffers,
1766									numOutputBuffers,
1767									fIn,
1768                                    fOut);
1769#endif
1770   }
1771#ifdef APP_DEBUGMEM
1772    APP_IPRINT("\n-Printing memory not deleted-\n");
1773    for(i=0;i<500;i++){
1774        if (lines[i]!=0){
1775             APP_IPRINT(" --->%d Bytes allocated on File:%s Line: %d\n",bytes[i],file[i],lines[i]);
1776             }
1777    }
1778#endif
1779#ifdef OMX_GETTIME
1780	GT_END("AMR_Enc test <End>");
1781	OMX_ListDestroy(pListHead);
1782#endif
1783    return eError;
1784}
1785
1786OMX_ERRORTYPE send_input_buffer(OMX_HANDLETYPE pHandle, OMX_BUFFERHEADERTYPE* pBuffer, FILE *fIn)
1787{
1788	OMX_ERRORTYPE error = OMX_ErrorNone;
1789	OMX_COMPONENTTYPE *pComponent = (OMX_COMPONENTTYPE *)pHandle;
1790
1791	if(FirstTime){
1792		if(mframe){
1793			nRead = fread(pBuffer->pBuffer, 1, NBAPP_INPUT_BUFFER_SIZE*2, fIn);
1794		}
1795		else{
1796			nRead = fread(pBuffer->pBuffer, 1, NBAPP_INPUT_BUFFER_SIZE, fIn);
1797		}
1798		pBuffer->nFilledLen = nRead;
1799	}
1800	else{
1801		memcpy(pBuffer->pBuffer, NextBuffer,nRead);
1802		pBuffer->nFilledLen = nRead;
1803	}
1804
1805	if(mframe){
1806			nRead = fread(NextBuffer, 1, NBAPP_INPUT_BUFFER_SIZE*2, fIn);
1807	}
1808	else{
1809			nRead = fread(NextBuffer, 1, NBAPP_INPUT_BUFFER_SIZE, fIn);
1810	}
1811
1812	if(nRead < NBAPP_INPUT_BUFFER_SIZE && !DasfMode){
1813
1814
1815	#ifdef OMX_GETTIME
1816		GT_START();
1817	#endif
1818		error = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1819    	error = WaitForState(pHandle, OMX_StateIdle);
1820	#ifdef OMX_GETTIME
1821		GT_END("Call to SendCommand <OMX_StateIdle>");
1822	#endif
1823		if(error != OMX_ErrorNone) {
1824			APP_DPRINT ("%d :: Error from SendCommand-Idle(Stop) State function\n",__LINE__);
1825			goto EXIT;
1826		}
1827
1828
1829		pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
1830	}else{
1831          pBuffer->nFlags = 0;
1832          }
1833
1834	if(pBuffer->nFilledLen!=0){
1835    	/*APP_DPRINT("pBuffer->nFilledLen %d \n",pBuffer->nFilledLen);*/
1836    	if(pBuffer->nFlags == OMX_BUFFERFLAG_EOS){
1837                           APP_IPRINT("Sending Last Input Buffer from App\n");
1838        }
1839/*        APP_DPRINT("Sending %d bytes to Comp\n", pBuffer->nFilledLen);*/
1840        pBuffer->nTimeStamp = rand() % 100;
1841
1842	if (!preempted) {
1843	    error = pComponent->EmptyThisBuffer(pHandle, pBuffer);
1844        if (error == OMX_ErrorIncorrectStateOperation)
1845				error = 0;
1846		}
1847    }
1848   	FirstTime=0;
1849EXIT:
1850	return error;
1851}
1852OMX_ERRORTYPE StopComponent(OMX_HANDLETYPE *pHandle)
1853{
1854    OMX_ERRORTYPE error = OMX_ErrorNone;
1855#ifdef OMX_GETTIME
1856	GT_START();
1857#endif
1858    error = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1859	if(error != OMX_ErrorNone) {
1860                    fprintf (stderr,"\nError from SendCommand-Idle(Stop) State function!!!!!!!!\n");
1861                    goto EXIT;
1862		}
1863	error =	WaitForState(pHandle, OMX_StateIdle);
1864#ifdef OMX_GETTIME
1865	GT_END("Call to SendCommand <OMX_StateIdle>");
1866#endif
1867    if(error != OMX_ErrorNone) {
1868					fprintf(stderr, "\nError:  hAmrEncoder->WaitForState reports an error %X!!!!!!!\n", error);
1869					goto EXIT;
1870	}
1871EXIT:
1872    return error;
1873}
1874
1875OMX_ERRORTYPE PauseComponent(OMX_HANDLETYPE *pHandle)
1876{
1877    OMX_ERRORTYPE error = OMX_ErrorNone;
1878#ifdef OMX_GETTIME
1879	GT_START();
1880#endif
1881    error = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StatePause, NULL);
1882	if(error != OMX_ErrorNone) {
1883                    fprintf (stderr,"\nError from SendCommand-Idle(Stop) State function!!!!!!!!\n");
1884                    goto EXIT;
1885		}
1886	error =	WaitForState(pHandle, OMX_StatePause);
1887#ifdef OMX_GETTIME
1888	GT_END("Call to SendCommand <OMX_StatePause>");
1889#endif
1890    if(error != OMX_ErrorNone) {
1891					fprintf(stderr, "\nError:  hAmrEncoder->WaitForState reports an error %X!!!!!!!\n", error);
1892					goto EXIT;
1893	}
1894EXIT:
1895    return error;
1896}
1897
1898OMX_ERRORTYPE PlayComponent(OMX_HANDLETYPE *pHandle)
1899{
1900    OMX_ERRORTYPE error = OMX_ErrorNone;
1901#ifdef OMX_GETTIME
1902	GT_START();
1903#endif
1904    error = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateExecuting, NULL);
1905	if(error != OMX_ErrorNone) {
1906                    fprintf (stderr,"\nError from SendCommand-Idle(Stop) State function!!!!!!!!\n");
1907                    goto EXIT;
1908		}
1909	error =	WaitForState(pHandle, OMX_StateExecuting);
1910#ifdef OMX_GETTIME
1911	GT_END("Call to SendCommand <OMX_StateExecuting>");
1912#endif
1913    if(error != OMX_ErrorNone) {
1914					fprintf(stderr, "\nError:  hAmrEncoder->WaitForState reports an error %X!!!!!!!\n", error);
1915					goto EXIT;
1916	}
1917EXIT:
1918    return error;
1919}
1920
1921/*=================================================================
1922
1923							Freeing All allocated resources
1924
1925==================================================================*/
1926#ifndef USE_BUFFER
1927int FreeAllResources( OMX_HANDLETYPE *pHandle,
1928			                OMX_BUFFERHEADERTYPE* pBufferIn,
1929			                OMX_BUFFERHEADERTYPE* pBufferOut,
1930			                int NIB, int NOB,
1931			                FILE* fileIn, FILE* fileOut)
1932{
1933	APP_IPRINT("%d::Freeing all resources by state invalid \n",__LINE__);
1934	OMX_ERRORTYPE eError = OMX_ErrorNone;
1935	OMX_U16 i;
1936
1937	for(i=0; i < NIB; i++) {
1938		   if(pBufferIn+i!=NULL){
1939                APP_IPRINT("%d :: APP: About to newfree pInputBufferHeader[%d]\n",__LINE__, i);
1940		        eError = OMX_FreeBuffer(pHandle, OMX_DirInput, pBufferIn+i);
1941         }
1942
1943	}
1944
1945
1946	for(i=0; i < NOB; i++) {
1947          if(pBufferOut+i!=NULL){
1948		   APP_IPRINT("%d :: APP: About to newfree pOutputBufferHeader[%d]\n",__LINE__, i);
1949		   eError = OMX_FreeBuffer(pHandle, OMX_DirOutput, pBufferOut+i);
1950         }
1951	}
1952
1953	/*i value is fixed by the number calls to newmalloc in the App */
1954	for(i=0; i<5;i++)
1955	{
1956		if (ArrayOfPointers[i] != NULL)
1957			newfree(ArrayOfPointers[i]);
1958	}
1959
1960	TIOMX_FreeHandle(pHandle);
1961
1962	return eError;
1963}
1964
1965
1966/*=================================================================
1967
1968							Freeing the resources with USE_BUFFER define
1969
1970==================================================================*/
1971#else
1972
1973int FreeAllResources(OMX_HANDLETYPE *pHandle,
1974							OMX_U8* UseInpBuf[],
1975							OMX_U8* UseOutBuf[],
1976							int NIB,int NOB,
1977							FILE* fileIn, FILE* fileOut)
1978{
1979
1980		OMX_ERRORTYPE eError = OMX_ErrorNone;
1981		OMX_U16 i;
1982		APP_IPRINT("%d::Freeing all resources by state invalid \n",__LINE__);
1983    	/* newfree the UseBuffers */
1984	    for(i=0; i < NIB; i++) {
1985		   UseInpBuf[i] = UseInpBuf[i] - 128;
1986		   APP_IPRINT("%d :: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,(UseInpBuf[i]));
1987		   if(UseInpBuf[i] != NULL){
1988			  newfree(UseInpBuf[i]);
1989			  UseInpBuf[i] = NULL;
1990		   }
1991		}
1992
1993	    for(i=0; i < NOB; i++) {
1994		   UseOutBuf[i] = UseOutBuf[i] - 128;
1995		   APP_IPRINT("%d :: [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, UseOutBuf[i]);
1996		   if(UseOutBuf[i] != NULL){
1997			  newfree(UseOutBuf[i]);
1998			  UseOutBuf[i] = NULL;
1999		   }
2000		}
2001
2002	/*i value is fixed by the number calls to newmalloc in the App */
2003		for(i=0; i<4;i++)
2004		{
2005			if (ArrayOfPointers[i] != NULL)
2006				newfree(ArrayOfPointers[i]);
2007		}
2008
2009		OMX_FreeHandle(pHandle);
2010
2011		return eError;
2012}
2013
2014#endif
2015
2016