OpenMAXBellagio  0.9.3
omxvolcontroltest.c
Go to the documentation of this file.
1 
27 #include "omxvolcontroltest.h"
28 #include <extension_struct.h>
29 
30 /* Application private date: should go in the component field (segs...) */
32 int fd = 0;
33 unsigned int filesize;
36 
38  .EmptyBufferDone = volcEmptyBufferDone,
39  .FillBufferDone = volcFillBufferDone,
40 };
41 
42 static void setHeader(OMX_PTR header, OMX_U32 size) {
43  OMX_VERSIONTYPE* ver = (OMX_VERSIONTYPE*)(header + sizeof(OMX_U32));
44  *((OMX_U32*)header) = size;
45 
49  ver->s.nStep = VERSIONSTEP;
50 }
51 
52 void display_help() {
53  printf("\n");
54  printf("Usage: omxvolcontroltest [-o outfile] [-g gain] filename\n");
55  printf("\n");
56  printf(" -o outfile: If this option is specified, the output stream is written to outfile\n");
57  printf(" otherwise redirected to std output\n");
58  printf(" -g: Gain of PCM data [0...100]\n");
59  printf(" -h: Displays this help\n");
60  printf("\n");
61  exit(1);
62 }
63 
69 static OMX_BOOL bEOS=OMX_FALSE;
70 FILE *outfile;
71 
72 int main(int argc, char** argv) {
73 
74  OMX_PORT_PARAM_TYPE param;
75  OMX_BUFFERHEADERTYPE *inBuffer1, *inBuffer2, *outBuffer1, *outBuffer2;
76  int data_read1;
77  int data_read2;
80  int gain=100;
81  int argn_dec;
83  OMX_INDEXTYPE custom_index;
84 
85  /* Obtain file descriptor */
86  if(argc < 2){
87  display_help();
88  } else {
89  flagIsOutputExpected = 0;
90  flagOutputReceived = 0;
91  flagInputReceived = 0;
92  flagIsGain = 0;
93 
94  argn_dec = 1;
95  while (argn_dec<argc) {
96  if (*(argv[argn_dec]) =='-') {
97  if (flagIsOutputExpected) {
98  display_help();
99  }
100  switch (*(argv[argn_dec]+1)) {
101  case 'h':
102  display_help();
103  break;
104  case 'o':
105  flagIsOutputExpected = 1;
106  break;
107  case 'g':
108  flagIsGain = 1;
109  break;
110  default:
111  display_help();
112  }
113  } else {
114  if (flagIsGain) {
115  gain = (int)atoi(argv[argn_dec]);
116  flagIsGain = 0;
117  if(gain > 100) {
118  DEBUG(DEFAULT_MESSAGES, "Gain should be between [0..100]\n");
119  gain = 100;
120  }
121  } else if (flagIsOutputExpected) {
122  output_file = malloc(strlen(argv[argn_dec]) + 1);
123  strcpy(output_file,argv[argn_dec]);
124  flagIsOutputExpected = 0;
125  flagOutputReceived = 1;
126  } else {
127  input_file = malloc(strlen(argv[argn_dec]) + 1);
128  strcpy(input_file,argv[argn_dec]);
129  flagInputReceived = 1;
130  }
131  }
132  argn_dec++;
133  }
134  if (!flagInputReceived) {
135  display_help();
136  }
137  DEBUG(DEFAULT_MESSAGES, "Input file %s", input_file);
138  DEBUG(DEFAULT_MESSAGES, " to ");
139  if (flagOutputReceived) {
140  DEBUG(DEFAULT_MESSAGES, " %s\n", output_file);
141  }
142  }
143 
144 
145  fd = open(input_file, O_RDONLY);
146  if(fd < 0){
147  perror("Error opening input file\n");
148  exit(1);
149  }
150 
151  if (flagOutputReceived) {
152  outfile = fopen(output_file,"wb");
153  if(outfile == NULL) {
154  DEBUG(DEB_LEV_ERR, "Error at opening the output file");
155  exit(1);
156  }
157  }
158 
159  filesize = getFileSize(fd);
160  /* Initialize application private data */
161  appPriv = malloc(sizeof(appPrivateType));
162  pthread_cond_init(&appPriv->condition, NULL);
163  pthread_mutex_init(&appPriv->mutex, NULL);
164  appPriv->eventSem = malloc(sizeof(tsem_t));
165  tsem_init(appPriv->eventSem, 0);
166  appPriv->eofSem = malloc(sizeof(tsem_t));
167  tsem_init(appPriv->eofSem, 0);
168 
169  err = OMX_Init();
170  if(err != OMX_ErrorNone) {
171  DEBUG(DEB_LEV_ERR, "OMX_Init() failed\n");
172  exit(1);
173  }
176  err = OMX_GetHandle(&handle, "OMX.st.volume.component", NULL /*appPriv */, &callbacks);
177  if(err != OMX_ErrorNone) {
178  DEBUG(DEB_LEV_ERR, "OMX_GetHandle failed\n");
179  exit(1);
180  }
181 
182  /* test the new feature of thread ID detection
183  */
184  err = OMX_GetExtensionIndex(handle, "OMX.st.index.param.BellagioThreadsID", &custom_index);
185  if(err != OMX_ErrorNone) {
186  DEBUG(DEB_LEV_ERR, "OMX_GetExtensionIndex failed\n");
187  exit(1);
188  }
189 
190  setHeader(&threadsID, sizeof(OMX_PARAM_BELLAGIOTHREADS_ID));
191  err = OMX_GetParameter(handle, custom_index, &threadsID);
192  if(err != OMX_ErrorNone) {
193  DEBUG(DEB_LEV_ERR, "OMX_GetParameter of extended index failed\n");
194  } else {
195  DEBUG(DEFAULT_MESSAGES, "threadsID messages %i buffers %i\n", (int)threadsID.nThreadMessageID, (int)threadsID.nThreadBufferMngtID);
196  }
197 
198  if((gain >= 0) && (gain <100)) {
200  if(err!=OMX_ErrorNone) {
201  DEBUG(DEB_LEV_ERR,"Error %08x In OMX_GetConfig 0 \n",err);
202  }
203  sVolume.sVolume.nValue = gain;
204  DEBUG(DEFAULT_MESSAGES, "Setting Gain %d \n", gain);
206  if(err!=OMX_ErrorNone) {
207  DEBUG(DEB_LEV_ERR,"Error %08x In OMX_SetConfig 0 \n",err);
208  }
209  }
210 
212  param.nPorts = 2;
213  setHeader(&param, sizeof(OMX_PORT_PARAM_TYPE));
215  if(err != OMX_ErrorNone){
216  DEBUG(DEB_LEV_ERR, "Error in getting OMX_PORT_PARAM_TYPE parameter\n");
217  exit(1);
218  }
219 
220  setHeader(&sPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
221  sPortDef.nPortIndex = 0;
223 
224  sPortDef.nBufferCountActual = 2;
226  if(err != OMX_ErrorNone){
227  DEBUG(DEB_LEV_ERR, "Error in getting OMX_PORT_PARAM_TYPE parameter\n");
228  exit(1);
229  }
230  sPortDef.nPortIndex = 1;
232 
233  sPortDef.nBufferCountActual = 2;
235  if(err != OMX_ErrorNone){
236  DEBUG(DEB_LEV_ERR, "Error in getting OMX_PORT_PARAM_TYPE parameter\n");
237  exit(1);
238  }
239 
241 
242  inBuffer1 = inBuffer2 = outBuffer1 = outBuffer2 = NULL;
243  err = OMX_AllocateBuffer(handle, &inBuffer1, 0, NULL, BUFFER_IN_SIZE);
244  if (err != OMX_ErrorNone) {
245  DEBUG(DEB_LEV_ERR, "Error on AllocateBuffer in 1%i\n", err);
246  exit(1);
247  }
248  err = OMX_AllocateBuffer(handle, &inBuffer2, 0, NULL, BUFFER_IN_SIZE);
249  if (err != OMX_ErrorNone) {
250  DEBUG(DEB_LEV_ERR, "Error on AllocateBuffer in 2 %i\n", err);
251  exit(1);
252  }
253  err = OMX_AllocateBuffer(handle, &outBuffer1, 1, NULL, BUFFER_IN_SIZE);
254  if (err != OMX_ErrorNone) {
255  DEBUG(DEB_LEV_ERR, "Error on AllocateBuffer out 1 %i\n", err);
256  exit(1);
257  }
258  err = OMX_AllocateBuffer(handle, &outBuffer2, 1, NULL, BUFFER_IN_SIZE);
259  if (err != OMX_ErrorNone) {
260  DEBUG(DEB_LEV_ERR, "Error on AllocateBuffer out 2 %i\n", err);
261  exit(1);
262  }
263 
264  tsem_down(appPriv->eventSem);
265  /* in Idle the second thread has bee created, and the second thread ID
266  * should be available
267  */
268  err = OMX_GetParameter(handle, custom_index, &threadsID);
269  if(err != OMX_ErrorNone) {
270  DEBUG(DEB_LEV_ERR, "OMX_GetParameter of extended index failed\n");
271  } else {
272  DEBUG(DEFAULT_MESSAGES, "threadsID messages %i buffers %i\n", (int)threadsID.nThreadMessageID, (int)threadsID.nThreadBufferMngtID);
273  }
274 
276 
277  /* Wait for commands to complete */
278  tsem_down(appPriv->eventSem);
279 
280  DEBUG(DEB_LEV_PARAMS, "Had buffers at:\n0x%p\n0x%p\n0x%p\n0x%p\n",
281  inBuffer1->pBuffer, inBuffer2->pBuffer, outBuffer1->pBuffer, outBuffer2->pBuffer);
282  DEBUG(DEB_LEV_PARAMS, "After switch to executing\n");
283 
284  data_read1 = read(fd, inBuffer1->pBuffer, BUFFER_IN_SIZE);
285  inBuffer1->nFilledLen = data_read1;
286  filesize -= data_read1;
287 
288  data_read2 = read(fd, inBuffer2->pBuffer, BUFFER_IN_SIZE);
289  inBuffer2->nFilledLen = data_read2;
290  filesize -= data_read2;
291 
292  DEBUG(DEB_LEV_PARAMS, "Empty first buffer %p\n", inBuffer1);
293  err = OMX_EmptyThisBuffer(handle, inBuffer1);
294  DEBUG(DEB_LEV_PARAMS, "Empty second buffer %p\n", inBuffer2);
295  err = OMX_EmptyThisBuffer(handle, inBuffer2);
296 
300  err = OMX_FillThisBuffer(handle, outBuffer1);
301  err = OMX_FillThisBuffer(handle, outBuffer2);
302 
303  tsem_down(appPriv->eofSem);
304 
306  /* Wait for commands to complete */
307  tsem_down(appPriv->eventSem);
308 
310  err = OMX_FreeBuffer(handle, 0, inBuffer1);
311  err = OMX_FreeBuffer(handle, 0, inBuffer2);
312  err = OMX_FreeBuffer(handle, 1, outBuffer1);
313  err = OMX_FreeBuffer(handle, 1, outBuffer2);
314 
315  /* Wait for commands to complete */
316  tsem_down(appPriv->eventSem);
317 
319 
320  free(appPriv->eventSem);
321  free(appPriv);
322 
323  if (flagOutputReceived) {
324  if(fclose(outfile) != 0) {
325  DEBUG(DEB_LEV_ERR,"Error in closing output file\n");
326  exit(1);
327  }
328  free(output_file);
329  }
330 
331  close(fd);
332  free(input_file);
333 
334  return 0;
335 }
336 
337 /* Callbacks implementation */
339  OMX_HANDLETYPE hComponent,
340  OMX_PTR pAppData,
341  OMX_EVENTTYPE eEvent,
342  OMX_U32 Data1,
343  OMX_U32 Data2,
344  OMX_PTR pEventData) {
345 
346  DEBUG(DEB_LEV_SIMPLE_SEQ, "Hi there, I am in the %s callback\n", __func__);
347  if(eEvent == OMX_EventCmdComplete) {
348  if (Data1 == OMX_CommandStateSet) {
349  DEBUG(DEB_LEV_SIMPLE_SEQ, "Volume Component State changed in ");
350  switch ((int)Data2) {
351  case OMX_StateInvalid:
352  DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateInvalid\n");
353  break;
354  case OMX_StateLoaded:
355  DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateLoaded\n");
356  break;
357  case OMX_StateIdle:
358  DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateIdle\n");
359  break;
360  case OMX_StateExecuting:
361  DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateExecuting\n");
362  break;
363  case OMX_StatePause:
364  DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StatePause\n");
365  break;
367  DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateWaitForResources\n");
368  break;
369  }
370  tsem_up(appPriv->eventSem);
371  } else if (Data1 == OMX_CommandPortEnable){
372  tsem_up(appPriv->eventSem);
373  } else if (Data1 == OMX_CommandPortDisable){
374  tsem_up(appPriv->eventSem);
375  }
376  } else if(eEvent == OMX_EventBufferFlag) {
377  if((int)Data2 == OMX_BUFFERFLAG_EOS) {
378  tsem_up(appPriv->eofSem);
379  }
380  } else {
381  DEBUG(DEB_LEV_SIMPLE_SEQ, "Param1 is %i\n", (int)Data1);
382  DEBUG(DEB_LEV_SIMPLE_SEQ, "Param2 is %i\n", (int)Data2);
383  }
384 
385  return OMX_ErrorNone;
386 }
387 
389  OMX_HANDLETYPE hComponent,
390  OMX_PTR pAppData,
391  OMX_BUFFERHEADERTYPE* pBuffer) {
392 
393  int data_read;
394  static int iBufferDropped=0;
395 
396  DEBUG(DEB_LEV_FULL_SEQ, "Hi there, I am in the %s callback.\n", __func__);
397  data_read = read(fd, pBuffer->pBuffer, BUFFER_IN_SIZE);
398  pBuffer->nFilledLen = data_read;
399  pBuffer->nOffset = 0;
400  filesize -= data_read;
401  if (data_read <= 0) {
402  DEBUG(DEB_LEV_SIMPLE_SEQ, "In the %s no more input data available\n", __func__);
403  iBufferDropped++;
404  if(iBufferDropped>=2) {
405  tsem_up(appPriv->eofSem);
406  return OMX_ErrorNone;
407  }
408  pBuffer->nFilledLen=0;
409  pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
410  bEOS=OMX_TRUE;
411  err = OMX_EmptyThisBuffer(hComponent, pBuffer);
412  return OMX_ErrorNone;
413  }
414  if(!bEOS) {
415  DEBUG(DEB_LEV_FULL_SEQ, "Empty buffer %p\n", pBuffer);
416  err = OMX_EmptyThisBuffer(hComponent, pBuffer);
417  }else {
418  DEBUG(DEB_LEV_FULL_SEQ, "In %s Dropping Empty This buffer to Audio Dec\n", __func__);
419  }
420 
421  return OMX_ErrorNone;
422 }
423 
425  OMX_HANDLETYPE hComponent,
426  OMX_PTR pAppData,
427  OMX_BUFFERHEADERTYPE* pBuffer) {
428 
429  int i;
430 
431  DEBUG(DEB_LEV_FULL_SEQ, "Hi there, I am in the %s callback. Got buflen %i for buffer at 0x%p\n",
432  __func__, (int)pBuffer->nFilledLen, pBuffer);
433 
434  /* Output data to standard output */
435  if(pBuffer != NULL) {
436  if (pBuffer->nFilledLen == 0) {
437  DEBUG(DEB_LEV_ERR, "Ouch! In %s: no data in the output buffer!\n", __func__);
438  return OMX_ErrorNone;
439  }
440  if (flagOutputReceived) {
441  if(pBuffer->nFilledLen > 0) {
442  fwrite(pBuffer->pBuffer, 1, pBuffer->nFilledLen, outfile);
443  }
444  } else {
445  for(i=0;i<pBuffer->nFilledLen;i++) {
446  putchar(*(char*)(pBuffer->pBuffer + i));
447  }
448  }
449  pBuffer->nFilledLen = 0;
450  } else {
451  DEBUG(DEB_LEV_ERR, "Ouch! In %s: had NULL buffer to output...\n", __func__);
452  }
453  /* Reschedule the fill buffer request */
454  if(!bEOS) {
455  OMX_FillThisBuffer(hComponent, pBuffer);
456  }
457  return OMX_ErrorNone;
458 }
459 
464 static int getFileSize(int fd) {
465 
466  struct stat input_file_stat;
467  int err;
468 
469  /* Obtain input file length */
470  err = fstat(fd, &input_file_stat);
471  if(err){
472  DEBUG(DEB_LEV_ERR, "fstat failed");
473  exit(-1);
474  }
475  return input_file_stat.st_size;
476 }
void display_help()
void * OMX_HANDLETYPE
Definition: OMX_Types.h:295
#define OMX_EmptyThisBuffer(hComponent,pBuffer)
Definition: OMX_Core.h:1096
#define DEB_LEV_PARAMS
#define VERSIONMAJOR
unsigned long OMX_U32
Definition: OMX_Types.h:145
int flagIsOutputExpected
#define OMX_GetConfig(hComponent,nConfigIndex,pComponentConfigStructure)
Definition: OMX_Core.h:861
#define DEB_LEV_SIMPLE_SEQ
OMX_ERRORTYPE volcEmptyBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR pAppData, OMX_BUFFERHEADERTYPE *pBuffer)
OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_Init(void)
The OMX_Init standard function.
Definition: omxcore.c:94
OMX_HANDLETYPE handle
OMX_BOOL bEOS[4]
int flagIsGain
int flagOutputReceived
#define DEBUG(n, fmt, args...)
struct OMX_VERSIONTYPE::@1 s
int flagInputReceived
char * input_file
pthread_mutex_t mutex
#define DEFAULT_MESSAGES
void * OMX_PTR
Definition: OMX_Types.h:199
OMX_BOOL
Definition: OMX_Types.h:189
OMX_S32 nValue
Definition: OMX_Types.h:263
OMX_INDEXTYPE
Definition: OMX_Index.h:60
int fd
#define DEB_LEV_ERR
OMX_EVENTTYPE
Definition: OMX_Core.h:479
int main(int argc, char **argv)
void tsem_up(tsem_t *tsem)
Definition: tsemaphore.c:110
void setHeader(OMX_PTR header, OMX_U32 size)
Simply fills the first two fields in any OMX structure with the size and the version.
void tsem_down(tsem_t *tsem)
Definition: tsemaphore.c:97
#define OMX_GetParameter(hComponent,nParamIndex,pComponentParameterStructure)
Definition: OMX_Core.h:786
OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_GetHandle(OMX_OUT OMX_HANDLETYPE *pHandle, OMX_IN OMX_STRING cComponentName, OMX_IN OMX_PTR pAppData, OMX_IN OMX_CALLBACKTYPE *pCallBacks)
OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_FreeHandle(OMX_IN OMX_HANDLETYPE hComponent)
#define OMX_BUFFERFLAG_EOS
Definition: OMX_Core.h:299
#define OMX_SetParameter(hComponent,nParamIndex,pComponentParameterStructure)
Definition: OMX_Core.h:825
#define OMX_SendCommand(hComponent,Cmd,nParam,pCmdData)
Definition: OMX_Core.h:745
#define OMX_AllocateBuffer(hComponent,ppBuffer,nPortIndex,pAppPrivate,nSizeBytes)
Definition: OMX_Core.h:1028
#define OMX_FreeBuffer(hComponent,nPortIndex,pBuffer)
Definition: OMX_Core.h:1064
OMX_ERRORTYPE err
#define OMX_GetExtensionIndex(hComponent,cParameterName,pIndexType)
Definition: OMX_Core.h:931
#define VERSIONMINOR
OMX_CALLBACKTYPE callbacks
appPrivateType * appPriv
OMX_U8 nVersionMinor
Definition: OMX_Types.h:333
OMX_ERRORTYPE volcFillBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR pAppData, OMX_BUFFERHEADERTYPE *pBuffer)
OMX_U8 nVersionMajor
Definition: OMX_Types.h:332
#define BUFFER_IN_SIZE
#define DEB_LEV_FULL_SEQ
char * output_file
int tsem_init(tsem_t *tsem, unsigned int val)
Definition: tsemaphore.c:39
#define OMX_SetConfig(hComponent,nConfigIndex,pComponentConfigStructure)
Definition: OMX_Core.h:897
pthread_cond_t condition
#define VERSIONREVISION
OMX_ERRORTYPE volcEventHandler(OMX_HANDLETYPE hComponent, OMX_PTR pAppData, OMX_EVENTTYPE eEvent, OMX_U32 Data1, OMX_U32 Data2, OMX_PTR pEventData)
FILE * outfile
#define OMX_FillThisBuffer(hComponent,pBuffer)
Definition: OMX_Core.h:1126
#define VERSIONSTEP
OMX_ERRORTYPE(* EventHandler)(OMX_IN OMX_HANDLETYPE hComponent, OMX_IN OMX_PTR pAppData, OMX_IN OMX_EVENTTYPE eEvent, OMX_IN OMX_U32 nData1, OMX_IN OMX_U32 nData2, OMX_IN OMX_PTR pEventData)
Definition: OMX_Core.h:530
unsigned int filesize
OMX_ERRORTYPE
Definition: OMX_Core.h:126
OMX_U8 nRevision
Definition: OMX_Types.h:334

Generated for OpenMAX Bellagio rel. 0.9.3 by  doxygen 1.5.1
SourceForge.net Logo