Skip to content

Instantly share code, notes, and snippets.

@commshare
Last active October 21, 2015 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save commshare/294ae41cfdeb7fe90f0e to your computer and use it in GitHub Desktop.
Save commshare/294ae41cfdeb7fe90f0e to your computer and use it in GitHub Desktop.
jan老师的OpenMAX state示例2:monitor events
/*
https://jan.newmarch.name/RPi/OpenMAX/State/event.c
The program event.c monitors events. When run on the OMX.broadcom.clock component,
it reports disabling all five ports, transitioning to Idle and then Executing
*/
#include <stdio.h>
#include <stdlib.h>
#include <OMX_Core.h>
#include <OMX_Component.h>
#include <bcm_host.h>
char *err2str(int err) {
switch (err) {
case OMX_ErrorInsufficientResources: return "OMX_ErrorInsufficientResources";
case OMX_ErrorUndefined: return "OMX_ErrorUndefined";
case OMX_ErrorInvalidComponentName: return "OMX_ErrorInvalidComponentName";
case OMX_ErrorComponentNotFound: return "OMX_ErrorComponentNotFound";
case OMX_ErrorInvalidComponent: return "OMX_ErrorInvalidComponent";
case OMX_ErrorBadParameter: return "OMX_ErrorBadParameter";
case OMX_ErrorNotImplemented: return "OMX_ErrorNotImplemented";
case OMX_ErrorUnderflow: return "OMX_ErrorUnderflow";
case OMX_ErrorOverflow: return "OMX_ErrorOverflow";
case OMX_ErrorHardware: return "OMX_ErrorHardware";
case OMX_ErrorInvalidState: return "OMX_ErrorInvalidState";
case OMX_ErrorStreamCorrupt: return "OMX_ErrorStreamCorrupt";
case OMX_ErrorPortsNotCompatible: return "OMX_ErrorPortsNotCompatible";
case OMX_ErrorResourcesLost: return "OMX_ErrorResourcesLost";
case OMX_ErrorNoMore: return "OMX_ErrorNoMore";
case OMX_ErrorVersionMismatch: return "OMX_ErrorVersionMismatch";
case OMX_ErrorNotReady: return "OMX_ErrorNotReady";
case OMX_ErrorTimeout: return "OMX_ErrorTimeout";
case OMX_ErrorSameState: return "OMX_ErrorSameState";
case OMX_ErrorResourcesPreempted: return "OMX_ErrorResourcesPreempted";
case OMX_ErrorPortUnresponsiveDuringAllocation: return "OMX_ErrorPortUnresponsiveDuringAllocation";
case OMX_ErrorPortUnresponsiveDuringDeallocation: return "OMX_ErrorPortUnresponsiveDuringDeallocation";
case OMX_ErrorPortUnresponsiveDuringStop: return "OMX_ErrorPortUnresponsiveDuringStop";
case OMX_ErrorIncorrectStateTransition: return "OMX_ErrorIncorrectStateTransition";
case OMX_ErrorIncorrectStateOperation: return "OMX_ErrorIncorrectStateOperation";
case OMX_ErrorUnsupportedSetting: return "OMX_ErrorUnsupportedSetting";
case OMX_ErrorUnsupportedIndex: return "OMX_ErrorUnsupportedIndex";
case OMX_ErrorBadPortIndex: return "OMX_ErrorBadPortIndex";
case OMX_ErrorPortUnpopulated: return "OMX_ErrorPortUnpopulated";
case OMX_ErrorComponentSuspended: return "OMX_ErrorComponentSuspended";
case OMX_ErrorDynamicResourcesUnavailable: return "OMX_ErrorDynamicResourcesUnavailable";
case OMX_ErrorMbErrorsInFrame: return "OMX_ErrorMbErrorsInFrame";
case OMX_ErrorFormatNotDetected: return "OMX_ErrorFormatNotDetected";
case OMX_ErrorContentPipeOpenFailed: return "OMX_ErrorContentPipeOpenFailed";
case OMX_ErrorContentPipeCreationFailed: return "OMX_ErrorContentPipeCreationFailed";
case OMX_ErrorSeperateTablesUsed: return "OMX_ErrorSeperateTablesUsed";
case OMX_ErrorTunnelingUnsupported: return "OMX_ErrorTunnelingUnsupported";
default: return "unknown error";
}
}
void printState(OMX_HANDLETYPE handle) {
OMX_STATETYPE state;
OMX_ERRORTYPE err;
err = OMX_GetState(handle, &state);
if (err != OMX_ErrorNone) {
fprintf(stderr, "Error on getting state\n");
exit(1);
}
switch (state) {
case OMX_StateLoaded: printf("StateLoaded\n"); break;
case OMX_StateIdle: printf("StateIdle\n"); break;
case OMX_StateExecuting: printf("StateExecuting\n"); break;
case OMX_StatePause: printf("StatePause\n"); break;
case OMX_StateWaitForResources: printf("StateWait\n"); break;
case OMX_StateInvalid: printf("StateInvalid\n"); break;
default: printf("State unknown\n"); break;
}
}
OMX_ERRORTYPE cEventHandler(
OMX_HANDLETYPE hComponent,
OMX_PTR pAppData,
OMX_EVENTTYPE eEvent,
OMX_U32 Data1,
OMX_U32 Data2,
OMX_PTR pEventData) {
if(eEvent == OMX_EventCmdComplete) {
if (Data1 == OMX_CommandStateSet) {
printf("Component State changed to ");
switch ((int)Data2) {
case OMX_StateInvalid:
printf("OMX_StateInvalid\n");
break;
case OMX_StateLoaded:
printf("OMX_StateLoaded\n");
break;
case OMX_StateIdle:
printf("OMX_StateIdle\n");
break;
case OMX_StateExecuting:
printf("OMX_StateExecuting\n");
break;
case OMX_StatePause:
printf("OMX_StatePause\n");
break;
case OMX_StateWaitForResources:
printf("OMX_StateWaitForResources\n");
break;
}
} else if (Data1 == OMX_CommandPortEnable){
printf("OMX State Port enabled %d\n", (int) Data2);
} else if (Data1 == OMX_CommandPortDisable){
printf("OMX State Port disabled %d\n", (int) Data2);
}
} else if(eEvent == OMX_EventBufferFlag) {
if((int)Data2 == OMX_BUFFERFLAG_EOS) {
printf("Event is buffer end of stream\n");
}
} else if(eEvent == OMX_EventError) {
if (Data1 == OMX_ErrorSameState) {
printf("Already in requested state\n");
} else {
printf("Event is Error %X\n", Data1);
}
} else if(eEvent == OMX_EventMark) {
printf("Event is Buffer Mark\n");
} else if(eEvent == OMX_EventPortSettingsChanged) {
printf("Event is PortSettingsChanged\n");
}
return OMX_ErrorNone;
}
OMX_CALLBACKTYPE callbacks = { .EventHandler = cEventHandler,
.EmptyBufferDone = NULL,
.FillBufferDone = NULL
};
void disableSomePorts(OMX_HANDLETYPE handle, OMX_INDEXTYPE indexType) {
OMX_PORT_PARAM_TYPE param;
int startPortNumber, endPortNumber;
int nPorts;
int n;
OMX_ERRORTYPE err;
//setHeader(&param, sizeof(OMX_PORT_PARAM_TYPE));
memset(&param, 0, sizeof(OMX_PORT_PARAM_TYPE));
param.nSize = sizeof(OMX_PORT_PARAM_TYPE);
param.nVersion.nVersion = OMX_VERSION;
err = OMX_GetParameter(handle, indexType, &param);
if(err != OMX_ErrorNone){
fprintf(stderr, "Error in getting image OMX_PORT_PARAM_TYPE parameter\n"
, 0);
return;
}
startPortNumber = param.nStartPortNumber;
nPorts = param.nPorts;
endPortNumber = startPortNumber + nPorts;
for (n = startPortNumber; n < endPortNumber; n++) {
OMX_SendCommand(handle, OMX_CommandPortDisable,
n, NULL);
}
}
void disableAllPorts(OMX_HANDLETYPE handle) {
disableSomePorts(handle, OMX_IndexParamVideoInit);
disableSomePorts(handle, OMX_IndexParamImageInit);
disableSomePorts(handle, OMX_IndexParamAudioInit);
disableSomePorts(handle, OMX_IndexParamOtherInit);
}
int main(int argc, char** argv) {
int i;
char *componentName;
OMX_ERRORTYPE err;
OMX_HANDLETYPE handle;
if (argc < 2) {
fprintf(stderr, "Usage: %s component-name\n", argv[0]);
exit(1);
}
componentName = argv[1];
bcm_host_init();
err = OMX_Init();
if(err != OMX_ErrorNone) {
fprintf(stderr, "OMX_Init() failed %s\n", err2str(err));
exit(1);
}
/** Ask the core for a handle to the component
*/
err = OMX_GetHandle(&handle, componentName,
// the next two fields are discussed later
NULL, &callbacks);
if (err != OMX_ErrorNone) {
fprintf(stderr, "OMX_GetHandle failed %s\n", err2str(err));
exit(1);
}
sleep(1);
// check our current state - should be Loaded
printState(handle);
disableAllPorts(handle);
// request a move to idle
OMX_SendCommand(handle,
OMX_CommandStateSet,
OMX_StateIdle,
NULL);
sleep(2);
printState(handle);
// and to executing
OMX_SendCommand(handle,
OMX_CommandStateSet,
OMX_StateExecuting,
NULL);
sleep(2);
printState(handle);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment