Skip to content

Instantly share code, notes, and snippets.

@commshare
Last active October 21, 2015 06:32
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/820aec4bdf44e5a345b9 to your computer and use it in GitHub Desktop.
Save commshare/820aec4bdf44e5a345b9 to your computer and use it in GitHub Desktop.
jan老师的OpenMAX state切换示范程序
/*
https://jan.newmarch.name/RPi/OpenMAX/State/wontwork.c
*/
/*
* WARNING: THIS PROGRAM DOESN'T WORK 这个程序不可以直接运行
*/
#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) {
printf("Hi there, I am in the %s callback\n", __func__);
printf("Event is %i\n", (int)eEvent);
printf("Param1 is %i\n", (int)Data1);
printf("Param2 is %i\n", (int)Data2);
return OMX_ErrorNone;
}
回调
OMX_CALLBACKTYPE callbacks = { .EventHandler = cEventHandler,
.EmptyBufferDone = NULL,
.FillBufferDone = NULL
};
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);
}
// check our current state - should be Loaded
printState(handle);
// request a move to idle 切换状态到idle
OMX_SendCommand(handle,
OMX_CommandStateSet,
OMX_StateIdle,
NULL);
int n = 0;
while (n++ < 10) {
sleep(1);
// are we there yet?
printState(handle); 这个时候应该切换成功了吧
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment