VideoDecoder的初始化实际就是OMX的创建,Stagefright提供了OMX的封装OMXCodec
在平台上stagefright -l 可以看到所有的组件
VideoDecoder的创建流程
status_t AwesomePlayer::initVideoDecoder(uint32_t flags) {
//最重要的创建mVideoSource
sp<MediaSource> decoder = OMXCodec::Create(
mClient.interface(), mVideoTrack->getFormat(),
false, // createEncoder
mVideoTrack,
NULL, flags, USE_SURFACE_ALLOC ? mNativeWindow : NULL);
status_t err = mVideoSource->start();
}
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) {
//得到MediaExtractor解析的MIME
bool success = meta->findCString(kKeyMIMEType, &mime);
//找到对应的decoder name
findMatchingCodecs(
mime, createEncoder, matchComponentName, flags,
&matchingCodecs, &matchingCodecQuirks);
//可能会找到多个匹配的codec
for (size_t i = 0; i < matchingCodecs.size(); ++i) {
//得到对应的component name
const char *componentNameBase = matchingCodecs[i].string();
//创建软件codec
softwareCodec = InstantiateSoftwareDecoder(componentName, source);
//根据component name创建OMX NODE
status_t err = omx->allocateNode(componentName, observer, &node);
//创建OMXCodec
sp<OMXCodec> codec = new OMXCodec(
omx, node, quirks, flags,
createEncoder, mime, componentName,
source, nativeWindow);
observer->setCodec(codec);
//根据meta配置codec
err = codec->configureCodec(meta);
//最终返回的是OMXCodec
return codec;
}
如何找到对应的codec name?
void OMXCodec::findMatchingCodecs(
const char *mime,
bool createEncoder, const char *matchComponentName,
uint32_t flags,
Vector<String8> *matchingCodecs,
Vector<uint32_t> *matchingCodecQuirks) {
//得到codec list
const MediaCodecList *list = MediaCodecList::getInstance();
for (;;) {
//从list中找到一个对应的codec name
ssize_t matchIndex =
list->findCodecByType(mime, createEncoder, index);
if (matchIndex < 0) {
break;
}
//找到后,就从找到的index的下一个继续查找
index = matchIndex + 1;
//得到ComponentName,并把其放到matchingCodecs中
const char *componentName = list->getCodecName(matchIndex);
matchingCodecs->push(String8(componentName));
}
}
MediaCodecList是怎么创建的?
//这是SINGLETON模型
const MediaCodecList *MediaCodecList::getInstance() {
Mutex::Autolock autoLock(sInitMutex);
if (sCodecList == NULL) {
sCodecList = new MediaCodecList;
}
return sCodecList->initCheck() == OK ? sCodecList : NULL;
}
//这个list是从/etc/media_codecs.xml中解析读到的
MediaCodecList::MediaCodecList()
: mInitCheck(NO_INIT) {
FILE *file = fopen("/etc/media_codecs.xml", "r");
if (file == NULL) {
ALOGW("unable to open media codecs configuration xml file.");
return;
}
//解析的协议遵从xml标准,具体可以看device中对应的设备media_codecs.xml
parseXMLFile(file);
if (mInitCheck == OK) {
// These are currently still used by the video editing suite.
addMediaCodec(true /* encoder */, "AACEncoder", "audio/mp4a-latm");
addMediaCodec(
false /* encoder */, "OMX.google.raw.decoder", "audio/raw");
Vector<AString> QcomAACQuirks;
QcomAACQuirks.push(AString("requires-allocate-on-input-ports"));
QcomAACQuirks.push(AString("requires-allocate-on-output-ports"));
addMediaCodec(false, "OMX.qcom.audio.decoder.multiaac",
"audio/mp4a-latm", getCodecSpecificQuirks(QcomAACQuirks));
}
}
OMXCodec::Create中的OMX是哪来的?
在AwesomePlayer中调用OMXCodec::Create时,传入的OMX是mClient.interface()
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 &);
};
mClient的初始化是在AwesomePlayer构造时完成的,connect实际是其初始化。
AwesomePlayer::AwesomePlayer() {
CHECK_EQ(mClient.connect(), (status_t)OK);
}
status_t OMXClient::connect() {
//OMX实际上是从MediaPlayerService中得到的
sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
mOMX = service->getOMX();
}
//MediaPlayerService只有一个OMX实例
sp<IOMX> MediaPlayerService::getOMX() {
Mutex::Autolock autoLock(mLock);
if (mOMX.get() == NULL) {
mOMX = new OMX;
}
return mOMX;
}
OMX如何创建codec对应的component
status_t OMX::allocateNode(
const char *name, const sp<IOMXObserver> &observer, node_id *node) {
//name是通过findMatchingCodecs找到的component name
OMXNodeInstance *instance = new OMXNodeInstance(this, observer);
//得到实际的ComponentInstance
OMX_ERRORTYPE err = mMaster->makeComponentInstance(
name, &OMXNodeInstance::kCallbacks,
instance, &handle);
}
OMX_ERRORTYPE OMXMaster::makeComponentInstance(
const char *name,
const OMX_CALLBACKTYPE *callbacks,
OMX_PTR appData,
OMX_COMPONENTTYPE **component) {
//根据name得到list中的index
ssize_t index = mPluginByComponentName.indexOfKey(String8(name));
//得到对应的plugin
OMXPluginBase *plugin = mPluginByComponentName.valueAt(index);
//创建ComponentInstance
OMX_ERRORTYPE err =
plugin->makeComponentInstance(name, callbacks, appData, component);
if (err != OMX_ErrorNone) {
return err;
}
mPluginByInstance.add(*component, plugin);
return err;
}
codec 参数配置
//通过meta进行配置
err = codec->configureCodec(meta);
status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
//得到ESDS,解析出来得到codec_specific_data
if (meta->findData(kKeyESDS, &type, &data, &size)) {
ESDS esds((const char *)data, size);
CHECK_EQ(esds.InitCheck(), (status_t)OK);
const void *codec_specific_data;
size_t codec_specific_data_size;
esds.getCodecSpecificInfo(
&codec_specific_data, &codec_specific_data_size);
meta->findCString(kKeyMIMEType, &mime_type);
if (strncmp(mime_type, MEDIA_MIMETYPE_AUDIO_MPEG, 10)) {
addCodecSpecificData(
codec_specific_data, codec_specific_data_size);
}
//后面都是类似的操作,根据不同的codec,配置不同的信息,例如:
else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AC3, mMIME)) {
int32_t numChannels, sampleRate;
CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
setAC3Format(numChannels, sampleRate);
}
//初始化OutputFormat
initOutputFormat(meta);
//初始化NativeWindow
if (mNativeWindow != NULL
&& !mIsEncoder
&& !strncasecmp(mMIME, "video/", 6)
&& !strncmp(mComponentName, "OMX.", 4)) {
status_t err = initNativeWindow();
if (err != OK) {
return err;
}
}