OpenMAXBellagio 0.9.3
omxprioritytest.c
Go to the documentation of this file.
00001 
00027 #include "omxprioritytest.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: omxprioritytest 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     OMX_PRIORITYMGMTTYPE oPriority;
00095     
00096     max_value = 0;
00097     if(argc < 2){
00098         display_help();
00099     } else {
00100         while (argn_dec < argc) {
00101             if (*(argv[argn_dec]) == '-') {
00102                 switch (*(argv[argn_dec] + 1)) {
00103                 case 'h':
00104                     display_help();
00105                     break;
00106                 case 'i':
00107                     getMaxValue = 1;
00108                     break;
00109                 default:
00110                     display_help();
00111                 }
00112             } else {
00113                 if (getMaxValue) {
00114                     max_value = convertStr2Int(argv[argn_dec]);
00115                     if (max_value == 0) {
00116                         display_help();
00117                     }
00118                 } else {
00119                     componentName = malloc(strlen(argv[argn_dec]) * sizeof(char) + 1);
00120                     strcpy(componentName, argv[argn_dec]);
00121                     flagInputReceived = 1;
00122                 }
00123             }
00124             argn_dec++;
00125         }
00126     }
00127     if (!flagInputReceived) {
00128         display_help();
00129     }
00130     if (max_value == 0) {
00131         max_value = MAX_COMPONENTS;
00132     }
00133     handle = malloc(sizeof(OMX_HANDLETYPE*) * max_value);
00134     /* Obtain file descriptor */
00135     eventSem = malloc(sizeof(tsem_t));
00136     tsem_init(eventSem, 0);
00137     bResourceErrorReceived = OMX_FALSE;
00138     err = OMX_Init();
00139     if(err != OMX_ErrorNone) {
00140         DEBUG(DEB_LEV_ERR, "OMX_Init() failed\n");
00141         exit(1);
00142     }
00143     DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_Init()\n");
00144 
00145     for (i = 0; i<max_value; i++) {
00146         err = OMX_GetHandle(&handle[i], componentName, NULL, &callbacks);
00147         if(err != OMX_ErrorNone) {
00148             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00149             DEBUG(DEFAULT_MESSAGES, "The OLD STYLE resource manager on %s\n", componentName);
00150             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00151             exit(1);
00152         }
00153         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_GetHandle() %i\n", i);
00154     }
00155     setHeader(&sParam, sizeof(OMX_PORT_PARAM_TYPE));
00156     err = OMX_GetParameter(handle[0], OMX_IndexParamAudioInit, &sParam);
00157     if (sParam.nPorts > 0) {
00158         indexaudiostart = sParam.nStartPortNumber;
00159         audioports = sParam.nPorts;
00160     }
00161     err = OMX_GetParameter(handle[0], OMX_IndexParamVideoInit, &sParam);
00162     if (sParam.nPorts > 0) {
00163         indexvideostart = sParam.nStartPortNumber;
00164         videoports = sParam.nPorts;
00165     }
00166     err = OMX_GetParameter(handle[0], OMX_IndexParamImageInit, &sParam);
00167     if (sParam.nPorts > 0) {
00168         indeximagestart = sParam.nStartPortNumber;
00169         imageports = sParam.nPorts;
00170     }
00171     err = OMX_GetParameter(handle[0], OMX_IndexParamOtherInit, &sParam);
00172     if (sParam.nPorts > 0) {
00173         indexotherstart = sParam.nStartPortNumber;
00174         otherports = sParam.nPorts;
00175     }
00176 
00177     for (i = 0; i<max_value; i++) {
00178         // todo this test is valid only for 2 ports components, not like mixer, sinks, sources
00179         if (indexaudiostart >= 0) {
00180             for (j = 0; j< audioports; j++) {
00181                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indexaudiostart, 0);
00182             }
00183         }
00184         if (indexvideostart >= 0) {
00185             for (j = 0; j< videoports; j++) {
00186                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indexvideostart, 0);
00187             }
00188         }
00189         if (indeximagestart >= 0) {
00190             for (j = 0; j< imageports; j++) {
00191                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indeximagestart, 0);
00192             }
00193         }
00194         if (indexotherstart >= 0) {
00195             for (j = 0; j< otherports; j++) {
00196                 err = OMX_SendCommand(handle[i], OMX_CommandPortDisable, j + indexotherstart, 0);
00197             }
00198         }
00199         err = OMX_SendCommand(handle[i], OMX_CommandStateSet, OMX_StateIdle, NULL);
00200         if(err != OMX_ErrorNone) {
00201             DEBUG(DEB_LEV_ERR, "The component %s can't go to Idle\n", componentName);
00202             break;
00203         }
00204         global_err = tsem_timed_down(eventSem, TIMEOUT);
00205         if (global_err != 0) {
00206             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00207             DEBUG(DEFAULT_MESSAGES, "The resource manager does not handle component %s\n", componentName);
00208             DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00209             break;
00210         } else {
00211             DEBUG(DEB_LEV_SIMPLE_SEQ, "The component %i is set to Idle\n", i);
00212 
00213             if (bResourceErrorReceived) {
00216                 DEBUG(DEB_LEV_SIMPLE_SEQ, "The resources are exhausted\n");
00217                 DEBUG(DEB_LEV_SIMPLE_SEQ, "Raising the priority of component %i\n", i);
00218                 setHeader(&oPriority, sizeof(OMX_PRIORITYMGMTTYPE));
00219                 oPriority.nGroupPriority = 1;
00220                 err = OMX_SetParameter(handle[i], OMX_IndexParamPriorityMgmt, &oPriority);
00221                 err = OMX_SendCommand(handle[i], OMX_CommandStateSet, OMX_StateIdle, NULL);
00222                 tsem_down(eventSem);
00223                 DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00224                 DEBUG(DEFAULT_MESSAGES, "The resource manager has operated on %s\n", componentName);
00225                 DEBUG(DEFAULT_MESSAGES, "#########################################################################\n");
00226                 break;
00227             }
00228         }
00229     }
00230     num_of_components = i;
00231 
00232     DEBUG(DEB_LEV_SIMPLE_SEQ, "Dispose the system\n");
00233     for (i = 0; i<num_of_components; i++) {
00234         err = OMX_GetState(handle[i], &state);
00235         if (state == OMX_StateIdle) {
00236             err = OMX_SendCommand(handle[i], OMX_CommandStateSet, OMX_StateLoaded, NULL);
00237             tsem_down(eventSem);
00238             DEBUG(DEB_LEV_SIMPLE_SEQ, "Component %i sent to Loaded\n", i);
00239         } else if (state == OMX_StateLoaded) {
00240             DEBUG(DEB_LEV_SIMPLE_SEQ, "Component %i already loaded\n", i);
00241         } else {
00242             DEBUG(DEB_LEV_SIMPLE_SEQ, "Component %i in the wrong state!\n", i);
00243         }
00244     }
00245     DEBUG(DEB_LEV_SIMPLE_SEQ, "All %i to loaded\n", num_of_components);
00246 
00247     for (i = 0; i<max_value; i++) {
00248         err = OMX_FreeHandle(handle[i]);
00249         if(err != OMX_ErrorNone) {
00250             DEBUG(DEB_LEV_ERR, "OMX_FreeHandle [%i] failed\n", i);
00251             exit(1);
00252         }
00253     }
00254 
00255     err = OMX_Deinit();
00256     if(err != OMX_ErrorNone) {
00257         DEBUG(DEB_LEV_ERR, "OMX_Deinit() failed\n");
00258         exit(1);
00259     }
00260     free(eventSem);
00261     DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_Deinit()\n");
00262     return 0;
00263 }
00264 
00265 /* Callbacks implementation */
00266 OMX_ERRORTYPE rmEventHandler(
00267   OMX_HANDLETYPE hComponent,
00268   OMX_PTR pAppData,
00269   OMX_EVENTTYPE eEvent,
00270   OMX_U32 Data1,
00271   OMX_U32 Data2,
00272   OMX_PTR pEventData) {
00273 
00274   if(eEvent == OMX_EventCmdComplete) {
00275     if (Data1 == OMX_CommandStateSet) {
00276       DEBUG(DEB_LEV_SIMPLE_SEQ, "Volume Component %p State changed in ", hComponent);
00277       switch ((int)Data2) {
00278       case OMX_StateInvalid:
00279         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateInvalid\n");
00280         break;
00281       case OMX_StateLoaded:
00282         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateLoaded\n");
00283         break;
00284       case OMX_StateIdle:
00285         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateIdle\n");
00286         break;
00287       case OMX_StateExecuting:
00288         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateExecuting\n");
00289         break;
00290       case OMX_StatePause:
00291         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StatePause\n");
00292         break;
00293       case OMX_StateWaitForResources:
00294         DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateWaitForResources\n");
00295         break;
00296       }
00297       tsem_up(eventSem);
00298     } else if (Data1 == OMX_CommandPortDisable) {
00299         DEBUG(DEB_LEV_SIMPLE_SEQ, "Disabled port %i\n", (int)Data2);
00300     }
00301   } else if (eEvent == OMX_EventError) {
00302     if (Data1 == OMX_ErrorInsufficientResources) {
00303         DEBUG(DEB_LEV_SIMPLE_SEQ, "Received error OMX_ErrorInsufficientResources\n");
00304         bResourceErrorReceived = OMX_TRUE;
00305         tsem_up(eventSem);
00306     } else if (Data1 == OMX_ErrorResourcesLost) {
00307         DEBUG(DEFAULT_MESSAGES, "Received error OMX_ErrorResourcesLost\n");
00308     } else if (Data1 == OMX_ErrorResourcesPreempted) {
00309         DEBUG(DEFAULT_MESSAGES, "Received error OMX_ErrorResourcesPreempted\n");
00310     } else {
00311         DEBUG(DEFAULT_MESSAGES, "Received error %i\n", (int)Data1);
00312     }
00313   } else if(eEvent == OMX_EventResourcesAcquired) {
00314         DEBUG(DEFAULT_MESSAGES, "Received message OMX_EventResourcesAcquired\n");
00315   } else {
00316     DEBUG(DEB_LEV_SIMPLE_SEQ, "Param1 is %i\n", (int)Data1);
00317     DEBUG(DEB_LEV_SIMPLE_SEQ, "Param2 is %i\n", (int)Data2);
00318   }
00319   return OMX_ErrorNone;
00320 }
00321 
00322 OMX_ERRORTYPE rmEmptyBufferDone(
00323         OMX_HANDLETYPE hComponent,
00324         OMX_PTR pAppData,
00325         OMX_BUFFERHEADERTYPE* pBuffer) {
00326 
00327     return OMX_ErrorNone;
00328 }
00329 
00330 OMX_ERRORTYPE rmFillBufferDone(
00331         OMX_HANDLETYPE hComponent,
00332         OMX_PTR pAppData,
00333         OMX_BUFFERHEADERTYPE* pBuffer) {
00334 
00335     return OMX_ErrorNone;
00336 }