【发布时间】:2013-11-21 08:59:00
【问题描述】:
在 Android 4.0(API 级别 15)中,没有获取编解码器列表的 API。 New API 仅支持 API 级别 16 及更高版本。但是我必须支持 API 级别 15。所以我必须使用 JNI 编写一些本地代码,引用 here。
struct OMXPluginBase {
OMXPluginBase() {}
virtual ~OMXPluginBase() {}
virtual void dummy1() = 0;
virtual void dummy2() = 0;
virtual OMX_ERRORTYPE enumerateComponents(
OMX_STRING name,
size_t size,
OMX_U32 index) = 0;
virtual OMX_ERRORTYPE getRolesOfComponent(
const char *name,
Vector<String8> *roles) = 0;
private:
OMXPluginBase(const OMXPluginBase &);
OMXPluginBase &operator=(const OMXPluginBase &);
};
获取编解码器名称信息的代码:
void *handle = dlopen("libstagefrighthw.so", RTLD_NOW);
if (handle == NULL) {
return -1;
}
typedef OMXPluginBase *(*CreateOMXPluginFunc)();
CreateOMXPluginFunc createOMXPlugin =
(CreateOMXPluginFunc)dlsym(handle, "createOMXPlugin");
if (!createOMXPlugin) {
createOMXPlugin = (CreateOMXPluginFunc)dlsym(
handle, "_ZN7android15createOMXPluginEv");
}
OMXPluginBase *plugin = (*createOMXPlugin)();
if (plugin) {
OMX_U32 index = -1;
char name[128];
OMX_ERRORTYPE err;
while ((err = plugin->enumerateComponents(name, sizeof(name),
++index)) == OMX_ErrorNone) {
printf("%s\n", name);
}
if (err != OMX_ErrorNoMore) {
printf("0x%x\n", err);
}
}
我可以得到编解码器信息的名称,因为名称只是char * 类型。但是,现在我想使用该函数获取编解码器信息的角色
OMX_ERRORTYPE getRolesOfComponent(const char *name, Vector<String8> *roles)
但我不能这样做,因为roles 是Vector<String8> 类型,而我没有这两个类。更严重的是,Android 似乎没有在libutils.so 等共享库中提供这两个类的实现。那么如何调用这个函数并获取我想要的信息呢?请帮我。我为此头疼了一整天!
更新
感谢@Alex,我终于解决了我的问题。我输入了代码here。希望这会对遇到同样问题的人有所帮助。
【问题讨论】:
-
我喜欢我们通过
dlopen()使用非公共原生 API 的方法。不过,出于所有实际目的,您可以直接链接到 libstagefright.so 和其他文件,也可以链接到#include系统头文件android::Vector和android::String8。
标签: android c++ android-ndk java-native-interface codec