OpenMAXBellagio 0.9.3
omxrmtest.c
Go to the documentation of this file.
00001 
00027 #include "omxrmtest.h"
00028 #include <string.h>
00029 #include <extension_struct.h>
00030 
00031 #define MAX_COMPONENTS 5
00032 #define TIMEOUT 500
00033 /* Application private date: should go in the component field (segs...) */
00034 
00035 
00036 OMX_HANDLETYPE *handle;
00037 
00038 OMX_CALLBACKTYPE callbacks = { .EventHandler = rmEventHandler,
00039                                .EmptyBufferDone = rmEmptyBufferDone,
00040                                .FillBufferDone = rmFillBufferDone,
00041 };
00042 
00043 static void setHeader(OMX_PTR header, OMX_U32 size) {
00044   OMX_VERSIONTYPE* ver = (OMX_VERSIONTYPE*)(header + sizeof(OMX_U32));
00045   *((OMX_U32*)header) = size;
00046 
00047   ver->s.nVersionMajor = VERSIONMAJOR;
00048   ver->s.nVersionMinor = VERSIONMINOR;
00049   ver->s.nRevision = VERSIONREVISION;
00050   ver->s.nStep = VERSIONSTEP;
00051 }
00052 
00053 int convertStr2Int(char* str) {
00054     int val = 0;
00055     int len = strlen(str);
00056     int i = 0;
00057     while(i < len) {
00058         if ((*(str+i)<'0') || (*(str+i)>'9')) {
00059             return 0;
00060         }
00061         val = (val*10) + ((*(str+i))-'0');
00062         i++;
00063     }
00064     return val;
00065 }
00066 
00067 void display_help() {
00068   printf("\n");
00069   printf("Usage: omxrmtest OMX_name [-i max_comp]\n");
00070   printf("\n");
00071   exit(1);
00072 }
00073 
00074 int max_value;
00075 int main(int argc, char** argv) {
00076     int getMaxValue = 0;
00077     int flagInputReceived = 0;
00078     int argn_dec = 1;
00079     int i, j;
00080     int num_of_components;
00081     OMX_STATETYPE state;
00082     char* componentName=0;
00083     int global_err = 0;
00084     OMX_ERRORTYPE err;
00085     OMX_PORT_PARAM_TYPE sParam;
00086     int indexaudiostart = -1;
00087     int audioports = 0;
00088     int indexvideostart = -1;
00089     int videoports = 0;
00090     int indeximagestart = -1;
00091     int imageports = 0;
00092     int indexotherstart = -1;
00093     int otherports = 0;
00094 
00095     max_value = 0;
00096     if(argc < 2){
00097         display_help();
00098     } else {
00099         while (argn_dec < argc) {
00100             if (*(argv[argn_dec]) == '-') {
00101                 switch (*(argv[argn_dec] + 1)) {
00102                 case 'h':
00103                     display_help();
00104                     break;
00105                 case 'i':
00106                     getMaxValue = 1;
00107                     break;
00108                 default:
00109                     display_help();
00110                 }
00111             } else {
00112                 if (getMaxValue) {
00113                     max_value = convertStr2Int(argv[argn_dec]);
00114                     if (max_value == 0) {
00115                         display_help();
00116                     }
00117                 } else {
00118                     componentName = malloc(strlen(argv[argn_dec]) * sizeof(char) + 1);
00119                     strcpy(componentName, argv[argn_dec]);
00120                     flagInputReceived = 1;
00121                 }
00122             }
00123             argn_dec++;
00124         }
00125     }
00126     if (!flagInputReceived) {
00127         display_help();
00128     }
00129     if (max_value == 0) {
00130         max_value = MAX_COMPONENTS;
00131     }
00132     handle = malloc(sizeof(OMX_HANDLETYPE*) * max_value);
00133     /* Obtain file descriptor */
00134     eventSem = malloc(sizeof(tsem_t));
00135     tsem_init(eventSem, 0);
00136     bResourceErrorReceived = OMX_FALSE;
00137     err = OMX_Init();
00138     if(err != OMX_ErrorNone) {
00139         DEBUG(DEB_LEV_ERR, "OMX_Init() failed\n");
00140         exit(1);
00141     }
00142     DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_Init()\n");
00143 
00144     for (i = 0; i<max_value; i++) {
00145         err = OMX_GetHandle(&handle[i], componentName, NULL, &callbacks);
00146         if(err != OMX_ErrorNone) {
00147             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00148             DEBUG(DEFAULT_MESSAGES, "The OLD STYLE resource manager on %s\n", componentName);
00149             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00150             exit(1);
00151         }
00152         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_GetHandle() %i\n", i);
00153     }
00154     setHeader(&sParam, sizeof(OMX_PORT_PARAM_TYPE));
00155     err = OMX_GetParameter(handle[0], OMX_IndexParamAudioInit, &sParam);
00156     if (sParam.nPorts > 0) {
00157         indexaudiostart = sParam.nStartPortNumber;
00158         audioports = sParam.nPorts;
00159     }
00160     err = OMX_GetParameter(handle[0], OMX_IndexParamVideoInit, &sParam);
00161     if (sParam.nPorts > 0) {
00162         indexvideostart = sParam.nStartPortNumber;
00163         videoports = sParam.nPorts;
00164     }
00165     err = OMX_GetParameter(handle[0], OMX_IndexParamImageInit, &sParam);
00166     if (sParam.nPorts > 0) {
00167         indeximagestart = sParam.nStartPortNumber;
00168         imageports = sParam.nPorts;
00169     }
00170     err = OMX_GetParameter(handle[0], OMX_IndexParamOtherInit, &sParam);
00171     if (sParam.nPorts > 0) {
00172         indexotherstart = sParam.nStartPortNumber;
00173         otherports = sParam.nPorts;
00174     }
00175 
00176     for (i = 0; i<max_value; i++) {
00177         // todo this test is valid only for 2 ports components, not like mixer, sinks, sources
00178         if (indexaudiostart >= 0) {
00179             for (j = 0; j< audioports; j++) {
00180                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indexaudiostart, 0);
00181             }
00182         }
00183         if (indexvideostart >= 0) {
00184             for (j = 0; j< videoports; j++) {
00185                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indexvideostart, 0);
00186             }
00187         }
00188         if (indeximagestart >= 0) {
00189             for (j = 0; j< imageports; j++) {
00190                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indeximagestart, 0);
00191             }
00192         }
00193         if (indexotherstart >= 0) {
00194             for (j = 0; j< otherports; j++) {
00195                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indexotherstart, 0);
00196             }
00197         }
00198         err = OMX_SendCommand(handle[i], OMX_CommandStateSet, OMX_StateIdle, NULL);
00199         if(err != OMX_ErrorNone) {
00200             DEBUG(DEB_LEV_ERR, "The component %s can't go to Idle\n", componentName);
00201             break;
00202         }
00203         global_err = tsem_timed_down(eventSem, TIMEOUT);
00204         if (global_err != 0) {
00205             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00206             DEBUG(DEFAULT_MESSAGES, "The resource manager does not handle component %s\n", componentName);
00207             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00208             break;
00209         } else {
00210             DEBUG(DEB_LEV_SIMPLE_SEQ, "The component %i is set to Idle\n", i);
00211 
00212             if (bResourceErrorReceived) {
00213                 DEBUG(DEB_LEV_SIMPLE_SEQ, "The resources are exhausted\n");
00214                 DEBUG(DEB_LEV_SIMPLE_SEQ, "Send component %i to WaitForResources\n", i);
00215                 err = OMX_SendCommand(handle[i], OMX_CommandStateSet, OMX_StateWaitForResources, NULL);
00216                 tsem_down(eventSem);
00217                 DEBUG(DEB_LEV_SIMPLE_SEQ, "Send component %i to Loaded\n", i-1);
00218                 err = OMX_SendCommand(handle[i-1], OMX_CommandStateSet, OMX_StateLoaded, NULL);
00219                 tsem_down(eventSem);
00220                 DEBUG(DEB_LEV_SIMPLE_SEQ, "Wait for component %i to go to Idle\n", i);
00221                 tsem_down(eventSem);
00222                 DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00223                 DEBUG(DEFAULT_MESSAGES, "The resource manager has operated on %s\n", componentName);
00224                 DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00225                 break;
00226             }
00227         }
00228     }
00229     num_of_components = i;
00230 
00231     DEBUG(DEB_LEV_SIMPLE_SEQ, "Dispose the system\n");
00232     for (i = 0; i<num_of_components; i++) {
00233         err = OMX_GetState(handle[i], &state);
00234         if (state == OMX_StateIdle) {
00235             err = OMX_SendCommand(handle[i], OMX_CommandStateSet, OMX_StateLoaded, NULL);
00236             tsem_down(eventSem);
00237             DEBUG(DEB_LEV_SIMPLE_SEQ, "Component %i sent to Loaded\n", i);
00238         } else if (state == OMX_StateLoaded) {
00239             DEBUG(DEB_LEV_SIMPLE_SEQ, "Component %i already loaded\n", i);
00240         } else {
00241             DEBUG(DEB_LEV_SIMPLE_SEQ, "Component %i in the wrong state!\n", i);
00242         }
00243     }
00244     DEBUG(DEB_LEV_SIMPLE_SEQ, "All %i to loaded\n", num_of_components);
00245 
00246     for (i = 0; i<max_value; i++) {
00247         err = OMX_FreeHandle(handle[i]);
00248         if(err != OMX_ErrorNone) {
00249             DEBUG(DEB_LEV_ERR, "OMX_FreeHandle [%i] failed\n", i);
00250             exit(1);
00251         }
00252     }
00253 
00254     err = OMX_Deinit();
00255     if(err != OMX_ErrorNone) {
00256         DEBUG(DEB_LEV_ERR, "OMX_Deinit() failed\n");
00257         exit(1);
00258     }
00259     free(eventSem);
00260     DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_Deinit()\n");
00261     return 0;
00262 }
00263 
00264 /* Callbacks implementation */
00265 OMX_ERRORTYPE rmEventHandler(
00266   OMX_HANDLETYPE hComponent,
00267   OMX_PTR pAppData,
00268   OMX_EVENTTYPE eEvent,
00269   OMX_U32 Data1,
00270   OMX_U32 Data2,
00271   OMX_PTR pEventData) {
00272 
00273   if(eEvent == OMX_EventCmdComplete) {
00274     if (Data1 == OMX_CommandStateSet) {
00275       DEBUG(DEB_LEV_SIMPLE_SEQ, "Volume Component %p State changed in ", hComponent);
00276       switch ((int)Data2) {
00277       case OMX_StateInvalid:
00278         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateInvalid\n");
00279         break;
00280       case OMX_StateLoaded:
00281         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateLoaded\n");
00282         break;
00283       case OMX_StateIdle:
00284         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateIdle\n");
00285         break;
00286       case OMX_StateExecuting:
00287         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateExecuting\n");
00288         break;
00289       case OMX_StatePause:
00290         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StatePause\n");
00291         break;
00292       case OMX_StateWaitForResources:
00293         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateWaitForResources\n");
00294         break;
00295       }
00296       tsem_up(eventSem);
00297     } else if (Data1 == OMX_CommandPortDisable) {
00298         DEBUG(DEB_LEV_SIMPLE_SEQ, "Disabled port %i\n", (int)Data2);
00299     }
00300   } else if (eEvent == OMX_EventError) {
00301     if (Data1 == OMX_ErrorInsufficientResources) {
00302         DEBUG(DEB_LEV_SIMPLE_SEQ, "Received error OMX_ErrorInsufficientResources\n");
00303         bResourceErrorReceived = OMX_TRUE;
00304         tsem_up(eventSem);
00305     } else if (Data1 == OMX_ErrorResourcesLost) {
00306         DEBUG(DEFAULT_MESSAGES, "Received error OMX_ErrorResourcesLost\n");
00307     } else if (Data1 == OMX_ErrorResourcesPreempted) {
00308         DEBUG(DEFAULT_MESSAGES, "Received error OMX_ErrorResourcesPreempted\n");
00309     } else {
00310         DEBUG(DEFAULT_MESSAGES, "Received error %i\n", (int)Data1);
00311     }
00312   } else if(eEvent == OMX_EventResourcesAcquired) {
00313         DEBUG(DEFAULT_MESSAGES, "Received message OMX_EventResourcesAcquired\n");
00314   } else {
00315     DEBUG(DEB_LEV_SIMPLE_SEQ, "Param1 is %i\n", (int)Data1);
00316     DEBUG(DEB_LEV_SIMPLE_SEQ, "Param2 is %i\n", (int)Data2);
00317   }
00318   return OMX_ErrorNone;
00319 }
00320 
00321 OMX_ERRORTYPE rmEmptyBufferDone(
00322         OMX_HANDLETYPE hComponent,
00323         OMX_PTR pAppData,
00324         OMX_BUFFERHEADERTYPE* pBuffer) {
00325 
00326     return OMX_ErrorNone;
00327 }
00328 
00329 OMX_ERRORTYPE rmFillBufferDone(
00330         OMX_HANDLETYPE hComponent,
00331         OMX_PTR pAppData,
00332         OMX_BUFFERHEADERTYPE* pBuffer) {
00333 
00334     return OMX_ErrorNone;
00335 }