Stagefright的編解碼功能是利用OpenMAX框架,而且用的還是OpenCORE之OMX的實作,我們 來看一下Stagefright和OMX是如何運作的。
(1) OMX_Init
OMXClient mClient;
AwesomePlayer::AwesomePlayer()
{
mClient.connect();
}
status_t OMXClient::connect()
{
mOMX = service->getOMX();
}
sp<IOMX> MediaPlayerService::getOMX()
{
mOMX = new OMX;
}
OMX::OMX() : mMaster(new OMXMaster)
OMXMaster::OMXMaster()
{
addPlugin(new OMXPVCodecsPlugin);
}
OMXPVCodecsPlugin::OMXPVCodecsPlugin()
{
OMX_MasterInit();
}
OMX_ERRORTYPE OMX_MasterInit() <-- under OpenCORE
{
return OMX_Init();
}
(2) OMX_SendCommand
OMXCodec::function_name()
{
mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
}
status_t OMX::sendCommand(node, cmd, param)
{
return findInstance(node)->sendCommand(cmd, param);
}
status_t OMXNodeInstance::sendCommand(cmd, param)
{
OMX_SendCommand(mHandle, cmd, param, NULL);
}
(3) 其他作用在 OMX 元件的指令
其他 作用在OMX元件的指令也和OMX_SendCommand的call path一樣,請見下表:
OMXCodec | OMX | OMXNodeInstance |
useBuffer | useBuffer (OMX_UseBuffer) | |
getParameter | getParameter (OMX_GetParameter) | |
fillBuffer | fillBuffer (OMX_FillThisBuffer) | |
emptyBuffer | emptyBuffer (OMX_EmptyThisBuffer) |
awesomplayer.cpp 中 awesomeplay有一个成员 OMXClient mClient;
这个mClient是通过MediaPlayerService的getOmx()方法创建(new OMX),代码如下
sp<IOMX> MediaPlayerService::getOMX() {
if (mOMX.get() == NULL) {
mOMX = new OMX;
return mOMX;
}
可以跟综OMX.cpp这个文件,查看创建过程
OMX::OMX()
: mMaster(new OMXMaster),
mNodeCounter(0) {
}
mNodeCounter:omxnode 计数器,在OmxCodec.cpp中返回nodeid
mMaster 这个对象是omx核心 可以理解为服务器端,之后在Omxcodec.cpp中create时传入的mOmx是mclient,它的所有调用都会通过IOMX.cpp转到Omx.cpp中。
下面我们来重点分析一下 OMXMaster
OMXMaster::OMXMaster()
: mVendorLibHandle(NULL) {
addVendorPlugin();
addPlugin(new SoftOMXPlugin);
}
通过addPlugin添加openmax外挂的插件,可以拿 addPlugin(new SoftOMXPlugin)为例,在SoftOMXPlugin插件中包含了支持的所有解码器组件,在我们的系统中支持的解码组件如下:
static const struct {
const char *mName;
const char *mLibNameSuffix;
} kComponents[] = {
{ "OMX.google.aac.decoder", "aacdec", "audio_decoder.aac" },
{ "OMX.google.amrnb.decoder", "amrdec", "audio_decoder.amrnb" },
{ "OMX.google.amrwb.decoder", "amrdec", "audio_decoder.amrwb" },
{ "OMX.google.h264.decoder", "h264dec", "video_decoder.avc" },
{ "OMX.google.g711.alaw.decoder", "g711dec", "audio_decoder.g711alaw" },
{ "OMX.google.g711.mlaw.decoder", "g711dec", "audio_decoder.g711mlaw" },
{ "OMX.google.h263.decoder", "mpeg4dec", "video_decoder.h263" },
{ "OMX.google.mpeg4.decoder", "mpeg4dec", "video_decoder.mpeg4" },
{ "OMX.google.mp3.decoder", "mp3dec", "audio_decoder.mp3" },
{ "OMX.google.vorbis.decoder", "vorbisdec", "audio_decoder.vorbis" },
{ "OMX.google.vpx.decoder", "vpxdec", "video_decoder.vpx" },
};
addPlugin(softomxplugin)时,会把调用SoftOmXPlugin的enumerateComponents,依次把上面这些组件添加到OMXMaster 的mPluginByComponentName中。
在omxcodec.cpp中创建组件时:
status_t err = omx->allocateNode(componentName, observer, &node);
会依次根据componentName,通过iomx.cpp(ibinder)->omx.cpp->OMXMaster.cpp中的makeComponentInstance
OMXMaster的makeComponentInstance会根据componentName名字查找到具体插件,比如componentName是OMX.google.vorbis.decoder,则会找到SoftOMXPlugin插件(vorbis这个组件包含在SoftOMXPlugin插件中,从上数据可以看出来),然后要据componentname调用插件SoftOMXPlugin->makeComponentInstance的方法,生成真正的组件(对应的解码器)。