Android多媒体开发-- android中OpenMax的实现整体框架
1.android中用openmax来干啥?
2.android中openmax实现框架
AwesomePlayer 中有个变量
- OMXClient mClient;
- 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 &);
- };
- 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;
- }
- 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,来看看解码器创建的流程。
(默认走软解码)
1.准备工作初始化OMXMaster
OMX构造函数中会进行初始化。
- OMXMaster *mMaster;
- OMX::OMX()
- : mMaster(new OMXMaster),
- mNodeCounter(0) {
- }
- OMXMaster::OMXMaster()
- : mVendorLibHandle(NULL) {
- addVendorPlugin();
- addPlugin(new SoftOMXPlugin);
- }
- 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" },
- };
- void OMXMaster::addVendorPlugin() {
- addPlugin("libstagefrighthw.so");
- }
然后通过dlopen、dlsym来调用库中的函数。
2.创建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);
让我们来看看真正的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实例。
- 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);
- 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;
- }
- android::SoftOMXComponent *createSoftOMXComponent(
- const char *name, const OMX_CALLBACKTYPE *callbacks,
- OMX_PTR appData, OMX_COMPONENTTYPE **component) {
- return new android::SoftAVC(name, callbacks, appData, component);
- }
经过这一路下来,终于完成了解码器的创建工作。简单总结一下。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。