Android音视频——AwesomePlayer到OMX服务过程
Posted 程思扬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android音视频——AwesomePlayer到OMX服务过程相关的知识,希望对你有一定的参考价值。
接着上一篇Android音视频——OMX 中 Nodeinstance 列表的管理与节点的操作
最后,来对前面的内容进行一下总结
-
在 AwesomePlayer 初始化过程中,通过 initVideoDecoder/initAudioDecoder 函数来创建音视频解码器 mVideoSource/mAudiosource。
-
在mVideoSource中通过mVideoTrack来解复用媒体文件,从中获取文件编码格式,继
而得到需要的解码器类型,通过类型调用omx・>allocateNodc创建0MX node实例,打
编码格式对应。以后都是通过Node实例来操作实际的硬件解码器的。 -
初始化MediaPlayerService对象的时候会创建OMX对象,OMX对象的构造函数会创建mMaster, mMaster负责获得与管理硬件平台的硬件解码器组件库。
-
在 omx->allocateNode 中通过 mMaster->makeComponentInstance 来创建真正对应的解码 器组件。这个解码器组件将完成之后实质的解码工作。
-
在创建mMaster->makeComponentInstance的过程中,通过上面mVideoTrack传递过来的解码器类型名,找到相对应的解码器的库,然后实例化。
-
解码Component通过输入Port和输出Port进彳亍交互,通过和OMXCodec共享Buffer进 行编解码。
-
AwesomePlayer包含了 mVideoSource,当初始化时指向OMXCodec的实际对象。 OMXCodec使用了 Binder机制,实现了对OMX服务的远程调用,其中IOMX作为接 口类定义了 OMX的大部分接口函数。
-
当具体实现OMX时,OMXMaster类用于管理OMX的插件,OMXNodelnstance类代
表OMX的具体实例,完成和Component的调用和交互。 -
CallbackDispatcher用于调度处理回调函数传回的消息。OMXNodelnstance和
CallbackDispatcher 对应,协同工作,完成不同实例的消息处理。 -
OMXNodelnstance是OMX端的概念,是服务器端的概念。其服务器端与OMX在一个 进程空间中。
-
OMXObserver是0MXCodec端的概念,是客户端的概念。其客户端与0MXCodec在一个进程空间中。其Bn、Bp方向和OMX、OMXNodelnstance相反,主要用于反向通知 onMessage 消息 。
到此,就介绍完AwesomePlayer是如何对OMX进行初始化的,以及如何关联到对应硬件 平台上的Hardware解码器的回调流程。
转:android中多媒体解码openmax的实现
转自:http://blog.csdn.net/hellofeiya/article/details/8601168
每个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;
- OMXMaster *mMaster;
- OMX::OMX()
- : mMaster(new OMXMaster),
- mNodeCounter(0) {
- }
- OMX::OMX()
- : mMaster(new OMXMaster),
- mNodeCounter(0) {
- }
- OMXMaster::OMXMaster()
- : mVendorLibHandle(NULL) {
- addVendorPlugin();
- addPlugin(new SoftOMXPlugin);
- }
- 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" },
- };
- 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");
- }
- 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;
- }
- 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;
- }
- 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);
- 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;
- 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);
- 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;
- }
- 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;
- }
- 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;
- }
- 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);
- }
- android::SoftOMXComponent *createSoftOMXComponent(
- const char *name, const OMX_CALLBACKTYPE *callbacks,
- OMX_PTR appData, OMX_COMPONENTTYPE **component) {
- return new android::SoftAVC(name, callbacks, appData, component);
- }
经过这一路下来,终于完成了解码器的创建工作。简单总结一下。
以上是关于Android音视频——AwesomePlayer到OMX服务过程的主要内容,如果未能解决你的问题,请参考以下文章
Android Multimedia框架总结Stagefright框架之AwesomePlayer及数据解析器