Android平台OpenMax多媒体引擎架构

来源:互联网 发布:python restful框架 编辑:程序博客网 时间:2024/05/17 01:42

1.android中用openmax来做什么工作?

        android的播放器的上层api接口是MediaPlayer,通过JNI调用本地服务MediaPlayrService,本地服务再调用StagefrightPlayer,接着调用AwesomePlayer,而 AwesomePlayer就是用openmax来做(code)编解码。层次结构如图所示:
 
 
        其实在openmax接口设计中,openmax不光能用来当编解码。通过他的组件还可以组成一个完整的播放器,包括sourc、demux、decode、output。但是为什么android只用他来做code呢?我认为有以下几方面:

 

1.在整个播放器中,解码器不得不说是最重要的一部分,而且也是最耗资源的一块。如果全靠软解,直接通过cpu来运算,特别是高清视频。别的事你就可以啥都不干了。所以解码器是最需要硬件提供加速的部分。现在的高清解码芯片都是主芯片+DSP结构,解码的工作都是通过DSP来做,不会在过多的占用主芯片。所有将芯片中DSP硬件编解码的能力通过openmax标准接口呈现出来,提供上层播放器来用。我认为这块是openmax最重要的意义。

2.source 主要是和协议打交道,demux 分解容器部分,大多数的容器格式的分解是不需要通过硬件来支持。只是ts流这种格式最可能用到硬件的支持。因为ts格式比较特殊,单包的大小太小了,只有188字节。所以也是为什么现在常见的解码芯片都会提供硬件ts demux 的支持。

3.音视频输出部分video\audio output 这块和操作系统关系十分紧密。可以看看著名开源播放器vlc。vlc 在mac、linux、Windows都有,功能上差别也不大。所以说他是跨平台的,他跨平台跨在哪?主要的工作量还是在音视频解码完之后的输出模块。因为各个系统的图像渲染和音频输出实现方法不同,所以vlc需要针对每个平台实现不同的output。这部分内容放在openmax来显然不合适。

所以openmax 中硬件抽象的编解码是最为常用的,也是为什么android中只用它来抽象code。

2.android中openmax实现框架


Android
 
1.上面已经说过了,android系统中只用openmax来做code,所以android向上抽象了一层OMXCodec,提供给上层播放器用。
播放器中音视频解码器mVideosource、mAudiosource都是OMXCodec的实例。

2.OMXCodec通过IOMX 依赖binder机制 获得 OMX服务,OMX服务 才是openmax 在android中 实现。

3. OMX把软编解码和硬件编解码统一看作插件的形式管理起来。
 
3.代码逻辑分析

AwesomePlayer 中会构造一个OMX的客户端OMXClient,变量定义如下: 
    OMXClient mClient; 
让我们看看   OMXClient的结构:

class OMXClient { 
public: 
    OMXClient(); 
 
    status_t connect(); 
    void disconnect(); 
 
    sp<IOMX> interface() { 
        return mOMX; 
    } 
 
private: 
    sp<IOMX> mOMX; 
 
    OMXClient(const OMXClient &); 
    OMXClient &operator=(const OMXClient &); 
}; 
       其中的IOMX 变量 mOMX 就是和OMX服务进行binder通讯的。

        在 AwesomePlayer 的构造函数中会调用

CHECK_EQ(mClient.connect(), (status_t)OK); 

status_t OMXClient::connect() { 
    sp<IServiceManager> sm = defaultServiceManager(); 
    sp<IBinder> binder = sm->getService(String16("media.player")); 
    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 
 
    CHECK(service.get() != NULL); 
 
    mOMX = service->getOMX(); 
    CHECK(mOMX.get() != NULL); 
 
    if (!mOMX->livesLocally(NULL /* node */, getpid())) { 
        ALOGI("Using client-side OMX mux."); 
        mOMX = new MuxOMX(mOMX); 
    } 
 
    return OK; 


sp<IOMX> MediaPlayerService::getOMX() { 
    Mutex::Autolock autoLock(mLock); 
 
    if (mOMX.get() == NULL) { 
        mOMX = new OMX; 
    } 
 
    return mOMX; 

        OMXClient::connect函数通过binder机制 获得到MediaPlayerService,然后通过MediaPlayerService来创建OMX的实例。这样OMXClient就获得到了OMX的入口,接下来就可以通过binder机制来获得OMX提供的服务。也就是说OMXClient 是android中 openmax 的入口。

        在创建音视频解码mVideoSource、mAudioSource的时候会把OMXClient中的sp<IOMX> mOMX的实例 传给mVideoSource、mAudioSource来共享使用这个OMX的入口。
也就是说一个AwesomePlayer对应着 一个IOMX 变量,AwesomePlayer中的音视频解码器共用这个IOMX变量来获得OMX服务。

sp<IOMX> interface() { 
      return mOMX; 
  } 

mAudioSource = OMXCodec::Create( 
                mClient.interface(), mAudioTrack->getFormat(), 
                false, // createEncoder 
                mAudioTrack); 

mVideoSource = OMXCodec::Create( 
            mClient.interface(), mVideoTrack->getFormat(), 
            false, // createEncoder 
            mVideoTrack, 
            NULL, flags, USE_SURFACE_ALLOC ? mNativeWindow : NULL);
 

 

        每个AwesomePlayer 只有一个OMX服务的入口,但是AwesomePlayer不一定就只需要1种解码器。有可能音视频都有,或者有很多种。这个时候这些解码器都需要OMX的服务,也就是OMX那头需要建立不同的解码器的组件来对应着AwesomePlayer中不同的code。OMX中非常重要的2个成员就是 OMXMaster 和 OMXNodeInstance。OMX通过这俩个成员来创建和维护不同的openmax 解码器组件,为AwesomePlayer中不同解码提供服务。让我们看看他们是怎么实现这些工作的。

成员


1. OMX中 OMXNodeInstance 负责创建并维护不同的实例,这些实例是根据上面需求创建的,以node作为唯一标识。这样播放器中每个OMXCodec在OMX服务端都对应有了自己的OMXNodeInstance实例。

2.OMXMaster 维护底层软硬件解码库,根据OMXNodeInstance中想要的解码器来创建解码实体组件。

接下来我们假设视频解码器需要的是AVC,来看看解码器创建的流程。

(默认走软解码)

初始化OMXMaster

OMX构造函数中会进行初始化。

OMXMaster *mMaster;
OMX::OMX(): mMaster(new OMXMaster),mNodeCounter(0) {}
OMXMaster::OMXMaster(): mVendorLibHandle(NULL) {addVendorPlugin();addPlugin(new SoftOMXPlugin);}
OMXMaster 负责OMX中编解码器插件管理,软件解码和硬件解码都是使用OMX标准,挂载plugins的方式来进行管理。
软解通过addPlugin(new SoftOMXPlugin);会把这些编解码器的名字都放在mPluginByComponentName中。
android 默认会提供一系列的软件解码器。目前支持这些格式的软编解码。
kComponents[] = {{ "OMX.google.aac.decoder", "aacdec", "audio_decoder.aac" },{ "OMX.google.aac.encoder", "aacenc", "audio_encoder.aac" },{ "OMX.google.amrnb.decoder", "amrdec", "audio_decoder.amrnb" },{ "OMX.google.amrnb.encoder", "amrnbenc", "audio_encoder.amrnb" },{ "OMX.google.amrwb.decoder", "amrdec", "audio_decoder.amrwb" },{ "OMX.google.amrwb.encoder", "amrwbenc", "audio_encoder.amrwb" },{ "OMX.google.h264.decoder", "h264dec", "video_decoder.avc" },{ "OMX.google.h264.encoder", "h264enc", "video_encoder.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.h263.encoder", "mpeg4enc", "video_encoder.h263" },{ "OMX.google.mpeg4.decoder", "mpeg4dec", "video_decoder.mpeg4" },{ "OMX.google.mpeg4.encoder", "mpeg4enc", "video_encoder.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" },{ "OMX.google.raw.decoder", "rawdec", "audio_decoder.raw" },{ "OMX.google.flac.encoder", "flacenc", "audio_encoder.flac" },};
硬件编解码是通过 addVendorPlugin();加载libstagefrighthw.so.各个芯片平台可以遵循openmax 标准,生成libstagefrighthw.so的库来提供android应用。
void OMXMaster::addVendorPlugin() {    addPlugin("libstagefrighthw.so");}

然后通过dlopen、dlsym来调用库中的函数。

这部分准备工作是在AwesomePlayer的构造函数中CHECK_EQ(mClient.connect(), (status_t)OK); 已经完成了。

创建mVideoSource

有了上面的OMX,接下来会在AwesomePlayer::initVideoDecoder中创建mVideoSource 实例,下面代码只保留的主要部分:
status_t AwesomePlayer::initVideoDecoder(uint32_t flags) {  ATRACE_CALL();  mVideoSource = OMXCodec::Create(    mClient.interface(), mVideoTrack->getFormat(),    false, // createEncoder    mVideoTrack,    NULL, flags, USE_SURFACE_ALLOC ? mNativeWindow : NULL);  status_t err = mVideoSource->start();  return mVideoSource != NULL ? OK : UNKNOWN_ERROR;}
保留主要部分,去除编码相关
sp<MediaSource> OMXCodec::Create(  const sp<IOMX> &omx,  const sp<MetaData> &meta, bool createEncoder,  const sp<MediaSource> &source,  const char *matchComponentName,  uint32_t flags,  const sp<ANativeWindow> &nativeWindow) {    int32_t requiresSecureBuffers;    const char *mime;    bool success = meta->findCString(kKeyMIMEType, &mime);    CHECK(success);    Vector<String8> matchingCodecs;    Vector<uint32_t> matchingCodecQuirks;    findMatchingCodecs(      mime, createEncoder, matchComponentName, flags,      &matchingCodecs, &matchingCodecQuirks);    sp<OMXCodecObserver> observer = new OMXCodecObserver;    IOMX::node_id node = 0;    for (size_t i = 0; i < matchingCodecs.size(); ++i) {      const char *componentNameBase = matchingCodecs[i].string();      uint32_t quirks = matchingCodecQuirks[i];      const char *componentName = componentNameBase;      AString tmp;      status_t err = omx->allocateNode(componentName, observer, &node);      if (err == OK) {        ALOGV("Successfully allocated OMX node '%s'", componentName);        sp<OMXCodec> codec = new OMXCodec(          omx, node, quirks, flags,          createEncoder, mime, componentName,          source, nativeWindow);        observer->setCodec(codec);        err = codec->configureCodec(meta);        if (err == OK) {          if (!strcmp("OMX.Nvidia.mpeg2v.decode", componentName)) {            codec->mFlags |= kOnlySubmitOneInputBufferAtOneTime;          }          return codec;        }        ALOGV("Failed to configure codec '%s'", componentName);     }  }  return NULL;}

1.根据mVideoTrack传进来的视频信息,查找相匹配的解码器。

 bool success = meta->findCString(kKeyMIMEType, &mime); findMatchingCodecs(   mime, createEncoder, matchComponentName, flags,   &matchingCodecs, &matchingCodecQuirks);

2. 创建OMXCodecObserver 实例,OMXCodecObserver功能后续会详细介绍。创建一个node 并初始化为0.

sp<OMXCodecObserver> observer = new OMXCodecObserver;IOMX::node_id node = 0;

3. 通过omx入口 依靠binder 机制调用OMX服务中的allocateNode(),这一步把匹配得到的解码器组件名、OMXCodecObserver实例和初始化为0的node一并传入。

status_t err = omx->allocateNode(componentName, observer, &node);
这个allocateNode 就是文章最开始讲的,在OMX那头创建一个和mVideoSource相匹配的解码实例。用node值作为唯一标识。
让我们来看看真正的omx中allocateNode做了啥?
status_t OMX::allocateNode(const char *name, const sp<IOMXObserver> &observer, node_id *node) {    Mutex::Autolock autoLock(mLock);    *node = 0;    OMXNodeInstance *instance = new OMXNodeInstance(this, observer);    OMX_COMPONENTTYPE *handle;    OMX_ERRORTYPE err = mMaster->makeComponentInstance(      name, &OMXNodeInstance::kCallbacks,      instance, &handle);    if (err != OMX_ErrorNone) {      ALOGV("FAILED to allocate omx component '%s'", name);      instance->onGetHandleFailed();      return UNKNOWN_ERROR;    }    *node = makeNodeID(instance);    mDispatchers.add(*node, new CallbackDispatcher(instance));    instance->setHandle(*node, handle);    mLiveNodes.add(observer->asBinder(), instance);    observer->asBinder()->linkToDeath(this);    return OK;}

创建一个OMXNodeInstance实例。
通过mMaster->makeComponentInstance创建真正解码器的组件,并通过handle与OMXNodeInstance关联。
所以说mMaster->makeComponentInstance这里是建立解码器组件的核心。会把mVideoSource需要的解码器name一直传递下去。
OMX_ERRORTYPE OMXMaster::makeComponentInstance(const char *name,const OMX_CALLBACKTYPE *callbacks,OMX_PTR appData,OMX_COMPONENTTYPE **component) {  Mutex::Autolock autoLock(mLock);  *component = NULL;  ssize_t index = mPluginByComponentName.indexOfKey(String8(name));  if (index < 0) {    return OMX_ErrorInvalidComponentName;  }  OMXPluginBase *plugin = mPluginByComponentName.valueAt(index);  OMX_ERRORTYPE err =  plugin->makeComponentInstance(name, callbacks, appData, component);  if (err != OMX_ErrorNone) {    return err;  }  mPluginByInstance.add(*component, plugin);  return err;}

最开始OMXMaster通过 addPlugin(new SoftOMXPlugin);把支持的软解码放在mPluginByComponentName中,在makeComponentInstance中通过上面传下来的解码器的name值从mPluginByComponentName找到相对应的plugin,然后调用  plugin->makeComponentInstance(name, callbacks, appData, component);
这里的plugin 值得就是软解SoftOMXPlugin 也就是调用了
OMX_ERRORTYPE SoftOMXPlugin::makeComponentInstance(const char *name,const OMX_CALLBACKTYPE *callbacks,OMX_PTR appData,OMX_COMPONENTTYPE **component) {  ALOGV("makeComponentInstance '%s'", name);  for (size_t i = 0; i < kNumComponents; ++i) {    if (strcmp(name, kComponents[i].mName)) {      continue;    }    AString libName = "libstagefright_soft_";    libName.append(kComponents[i].mLibNameSuffix);    libName.append(".so");    void *libHandle = dlopen(libName.c_str(), RTLD_NOW);    if (libHandle == NULL) {      ALOGE("unable to dlopen %s", libName.c_str());      return OMX_ErrorComponentNotFound;    }    typedef SoftOMXComponent *(*CreateSoftOMXComponentFunc)(    const char *, const OMX_CALLBACKTYPE *,OMX_PTR, OMX_COMPONENTTYPE **);    CreateSoftOMXComponentFunc createSoftOMXComponent =      (CreateSoftOMXComponentFunc)dlsym(      libHandle,      "_Z22createSoftOMXComponentPKcPK16OMX_CALLBACKTYPE"      "PvPP17OMX_COMPONENTTYPE");    if (createSoftOMXComponent == NULL) {      dlclose(libHandle);      libHandle = NULL;      return OMX_ErrorComponentNotFound;    }    sp<SoftOMXComponent> codec =      (*createSoftOMXComponent)(name, callbacks, appData, component);    if (codec == NULL) {      dlclose(libHandle);      libHandle = NULL;      return OMX_ErrorInsufficientResources;    }    OMX_ERRORTYPE err = codec->initCheck();    if (err != OMX_ErrorNone) {      dlclose(libHandle);      libHandle = NULL;      return err;    }    codec->incStrong(this);    codec->setLibHandle(libHandle);    return OMX_ErrorNone;  }  return OMX_ErrorInvalidComponentName;}
通过上面传下来的解码器的name,找到对应库的名字。假如是264的话,要加载的库就是 libstagefright_soft_h264dec.so,也就是对应上层264解码的话,omx解码组件会加载对应的libstagefright_soft_h264dec.so库。相对应的软解代码在 Android4.1.1\frameworks\av\media\libstagefright\codecs\on2\h264dec 中。
加载完264解码库后 通过dlopen、dlsym来调用库中函数。
通过调用 SoftAVC 中的 createSoftOMXComponent 来创建真正264解码器实例SoftOMXComponent。以后真正视频解码的工作都是通过avc 这个SoftAVC实例完成的
android::SoftOMXComponent *createSoftOMXComponent(const char *name, const OMX_CALLBACKTYPE *callbacks,OMX_PTR appData, OMX_COMPONENTTYPE **component) {  return new android::SoftAVC(name, callbacks, appData, component);}

经过这一路下来,终于完成了解码器的创建工作。简单总结一下。
1.AwesomePlayer中通过initVideoDecoder 来创建video解码器mVideoSource
2.mVideoSource 中通过上部分demux后的视频流 mVideoTrack来获得解码器的类型,通过类型调用omx->allocateNode 创建omx node实例与自己对应。以后都是通过node实例来操作解码器。
3.在 omx->allocateNode中 通过mMaster->makeComponentInstance 来创建真正对应的解码器组件。这个解码器组件是完成之后解码实际工作的。
4.在创建mMaster->makeComponentInstance过程中,也是通过上面mVideoTrack 过来的解码器类型名,找到相对应的解码器的库,然后实例化。


 

原创粉丝点击