stagefright框架(三)-选择Video Decode

在《Stagefright (1) – Video Playback的流程》中,我们并没有详述Stagefright是如何根据影片档的类型来选择适合的video decoder,现在,就让我们来看一看。
(1) Video decoder是在onPrepareAsyncEvent中的initVideoDecoder被决定的

OMXCodec::Create()会回传video decoder给mVideoSource。

status_t AwesomePlayer::initVideoDecoder()
{
  mVideoSource = OMXCodec::Create(mClient.interface(),
                                  mVideoTrack->getFormat(),
                                  false,
                                  mVideoTrack);
}

sp<MediaSource> OMXCodec::Create(&omx&meta, createEncoder&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 !NULLreturn softwareCodec;
        
    err = omx->allocateNode(componentName...&node)..(4)

    if (err == OK)
    {
      codec new OMXCodec(..., componentName...).....(5)
      return codec;
    }
  }
}


(2) 根据mVideoTrack的MIME从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

stagefright框架(三)-選擇Video <wbr>Decoder
 
posted @ 2014-11-14 13:46  rlandj  阅读(784)  评论(0编辑  收藏  举报