Stagefright之三
——init Decode
这一节主要介绍 Stagefright是如何根据mime type选择合适的decoder。在Sniff*()中,变量confidence是指可信度参数。如果一个文件匹配几个sniff,那么根据confidence值的大小来选择。
(1)
在onPrepareAsyncEvent中,初始化decode是根据(mVideoTrack)和(mAudioTrack)去调用initVideoDecoder()和initAudioDecoder()。下面以initVideoDecoder()为例,详述这一过程。
OMXCodec::Create() 会回传video decoder给mVideoSource。
status_t AwesomePlayer::initVideoDecoder() ……AwesomePlayer.cpp
{
mVideoSource = OMXCodec::Create(mClient.interface(),
mVideoTrack->getFormat(),
false, // createEncoder
mVideoTrack);
}
sp<MediaSource> OMXCodec::Create(&omx, &meta, &source, matchComponentName,…)
{
meta->findCString(kKeyMIMEType, &mime);
findMatchingCodecs(mime, ..., &matchingCodecs); ........ (2)
for (size_t i = 0; i < matchingCodecs.size(); ++i)
{
componentName = matchingCodecs[i].string();
softwareCodec =
InstantiateSoftwareCodec(componentName, ...); ..... (3)
if (softwareCodec != NULL) return softwareCodec;
err = omx->allocateNode(componentName, ..., &node); ... (4)
if (err == OK)
{
codec = new OMXCodec(..., componentName, ...); ...... (5)
return codec;
}
}
}
(2) 根据mVideoTrack的mimetype从 kDecoderInfo挑出合适的components
void OMXCodec::findMatchingCodecs(mime, ..., matchingCodecs)
{
for (int index = 0;; ++index)
{
componentName = GetCodec(
kDecoderInfo,
sizeof(kDecoderInfo)/sizeof(kDecoderInfo[0]),
mime,
index);
matchingCodecs->push(String8(componentName));
}
}
static const CodecInfo kDecoderInfo[] =
{
...
{ MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
{ MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
{ MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
...
}
GetCodec会依据mime从kDecoderInfo挑出所有的 component name,然后存到matchingCodecs中。
(3) 根据matchingCodecs中component的顺序,我们会先去检查其是否为software decoder
static sp<MediaSource> InstantiateSoftwareCodec(name, ...)
{
FactoryInfo kFactoryInfo[] =
{
...
FACTORY_REF(M4vH263Decoder)
...
};
for (i = 0; i < sizeof(kFactoryInfo)/sizeof(kFactoryInfo[0]); ++i)
{
if (!strcmp(name, kFactoryInfo[i].name))
return (*kFactoryInfo[i].CreateFunc)(source);
}
}
所有的 software decoder都会被列在kFactoryInfo中,我们藉由传进来的name来对应到适合的decoder。
(4) 如果该component不是software decoder,那么就尝试去匹配对应的OMX component
status_t OMX::allocateNode(name, ..., node)
{
mMaster->makeComponentInstance(
name,
&OMXNodeInstance::kCallbacks,
instance,
handle);
}
OMX_ERRORTYPE OMXMaster::makeComponentInstance(name, ...)
{
plugin->makeComponentInstance(name, ...);
}
OMX_ERRORTYPE OMXPVCodecsPlugin::makeComponentInstance(name, ...)
{
return OMX_MasterGetHandle(..., name, ...);
}
OMX_ERRORTYPE OMX_MasterGetHandle(...)
{
return OMX_GetHandle(...);
}
(5) 若该component为OMX deocder,则回传;否则继续检查下一个component。