Android中初始化Codec2的具体流程
作者:小和尚念经敲木鱼 发布时间:2023-08-27 21:30:32
1、MediaCodec调用流程
首先,我们先看下MediaCodec::CreateByType函数里面做了什么:
sp<MediaCodec> MediaCodec::CreateByType(
const sp<ALooper> &looper, const AString &mime, bool encoder, status_t *err, pid_t pid,
uid_t uid) {
sp<AMessage> format;
return CreateByType(looper, mime, encoder, err, pid, uid, format);
}
sp<MediaCodec> MediaCodec::CreateByType(
const sp<ALooper> &looper, const AString &mime, bool encoder, status_t *err, pid_t pid,
uid_t uid, sp<AMessage> format) {
Vector<AString> matchingCodecs;
MediaCodecList::findMatchingCodecs(
mime.c_str(),
encoder,
0,
format,
&matchingCodecs);
if (err != NULL) {
*err = NAME_NOT_FOUND;
}
for (size_t i = 0; i < matchingCodecs.size(); ++i) {
sp<MediaCodec> codec = new MediaCodec(looper, pid, uid);
AString componentName = matchingCodecs[i];
status_t ret = codec->init(componentName);
if (err != NULL) {
*err = ret;
}
if (ret == OK) {
return codec;
}
ALOGD("Allocating component '%s' failed (%d), try next one.",
componentName.c_str(), ret);
}
return NULL;
}
CreateByType调用CreateByType的重载函数。
CreateByType(
const sp<ALooper> &looper, const AString &mime, bool encoder, status_t *err, pid_t pid,
uid_t uid, sp<AMessage> format)
里面主要是做了下面两件事:
1、查找支持的Codec。
2、根据matchingCodecs创建MediaCodec 对应的解码器调用init。
MediaCodec::init再根据创建来的名字调用mGetCodecBase这个 function
status_t MediaCodec::init(const AString &name) {
mResourceManagerProxy->init();
mInitName = name;
mCodecInfo.clear();
bool secureCodec = false;
const char *owner = "";
mCodec = mGetCodecBase(name, owner);
if (mIsVideo) {
if (mCodecLooper == NULL) {
mCodecLooper = new ALooper;
mCodecLooper->setName("CodecLooper");
mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
}
mCodecLooper->registerHandler(mCodec);
} else {
mLooper->registerHandler(mCodec);
}
mLooper->registerHandler(this);
mCodec->setCallback(
std::unique_ptr<CodecBase::CodecCallback>(
new CodecCallback(new AMessage(kWhatCodecNotify, this))));
mBufferChannel = mCodec->getBufferChannel();
mBufferChannel->setCallback(
std::unique_ptr<CodecBase::BufferCallback>(
new BufferCallback(new AMessage(kWhatCodecNotify, this))));
sp<AMessage> msg = new AMessage(kWhatInit, this);
if (mCodecInfo) {
msg->setObject("codecInfo", mCodecInfo);
// name may be different from mCodecInfo->getCodecName() if we stripped
// ".secure"
}
msg->setString("name", name);
}
mGetCodecBase指向的是下列函数:
创建一个父类的对象,具体这父类对象是走Codec2还是ACodec的决定在下列函数中:
sp<CodecBase> MediaCodec::GetCodecBase(const AString &name, const char *owner) {
if (owner) {
if (strcmp(owner, "default") == 0) {
return new ACodec;
} else if (strncmp(owner, "codec2", 6) == 0) {
return CreateCCodec();
}
}
if (name.startsWithIgnoreCase("c2.")) {
return CreateCCodec();
} else if (name.startsWithIgnoreCase("omx.")) {
// at this time only ACodec specifies a mime type.
return new ACodec;
} else if (name.startsWithIgnoreCase("android.filter.")) {
return new MediaFilter;
} else {
return NULL;
}
}
如果走CCodec里面调用MediaCodec.cpp文件中:
static CodecBase *CreateCCodec() {
return new CCodec;
}
这时候就走到的CCodec这个类中,它的构造函数:
// CCodec
CCodec::CCodec()
: mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))),
mConfig(new CCodecConfig) {
}
这里的 mChannel 和 mConfig 都是new出来的。
class CCodecBufferChannel : public BufferChannelBase;
上面的 mBufferChannel = mCodec->getBufferChannel(); 就是把CCodec的mChannel返回到MediaCodec中。
std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
return mChannel;
}
也就是说MediaCodec调用BufferChannelBase类型的mBufferChannel 实际上是调用CCodec里面的 mChannel
mBufferChannel设置一个new 的BufferCallback()对象的。
mCodec->setCallback(
std::unique_ptr<CodecBase::CodecCallback>(
new CodecCallback(new AMessage(kWhatCodecNotify, this))));
实际上设置的是CodecBase里面的CodecCallback mCallback
struct CodecBase : public AHandler{
void setCallback(std::unique_ptr<CodecCallback> &&callback) {
mCallback = std::move(callback);
}
protected:
std::unique_ptr<CodecCallback> mCallback;
}
之后设置了BufferCallBack。
mBufferChannel->setCallback(
std::unique_ptr<CodecBase::BufferCallback>(
new BufferCallback(new AMessage(kWhatCodecNotify, this))));
实际上设置的是BufferChannelBase::BufferCallback mCallback的指针。
class BufferChannelBase {
public:
void setCallback(std::unique_ptr<CodecBase::BufferCallback> &&callback) {
mCallback = std::move(callback);
}
protected:
std::unique_ptr<CodecBase::BufferCallback> mCallback;
};
之后Init发送kWhatInit消息,处理之后就调用了CCodec->initiateAllocateComponent()。接下来我们需要看CCodec里面的调用逻辑了。
2、CCodec调用流程
CCodec的源码路径如下:
frameworks/av/media/codec2
首先看下mConfig和mChannel的定义和初始化,具体如下:
//CCodec.h
class CCodec : public CodecBase {
Mutexed<std::unique_ptr<CCodecConfig>> mConfig;
std::shared_ptr<CCodecBufferChannel> mChannel;
}
//CCodec.cpp
CCodec::CCodec()
: mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))),
mConfig(new CCodecConfig){}
构造函数初始化的时候,就创建new CCodecCallbackImpl对象出来,CCodecCallbackImpl是继承CCodecCallBack的 就做一个适配封装处理。CCodecCallbackImpl 是CCodec的友元类。
上面调用了CCodec->initiateAllocateComponent(),其实CCodec::initiateAllocateComponent 也就是发送kWhatAllocate消息。一切都交给CCodec::onMessageReceived 进行处理。在接受 onMessageReceived 中的case语句中,kWhatAllocate 调用CCodec::allocate
接着使用client = Codec2Client::CreateFromService(“default”);创建一个服务,根据传入的setAsPreferredCodec2ComponentStore 设置SetPreferredCodec2ComponentStore 默认是false.但是默认是false,这里没有传入。
这里的client = Codec2Client::CreateFromService(“default”);创建成功后调用SetPreferredCodec2ComponentStore,将vendor下支持的Codec2的server设置进来。之后将重置的mClientListener、获得的componentName名字、Codec2Client::Component的组件comp及Codec2Client::CreateFromService(“default”)返回的client,一起作为参数,再重新调用CreateComponentByName创建组件。
之后给CCodecBufferChannel mChannel设置组件,用于绑定组件的回调。
class CCodecBufferChannel : public BufferChannelBase;
接着CCodec::allocate中调用CCodecConfig::initialize、CCodecConfig::queryConfiguration、CodecCallback::onComponentAllocated函数。
具体的代码调用逻辑,如下所示:
//Codec2Client::Component : public Codec2Client::Configurable
status_t CCodecConfig::initialize(
const std::shared_ptr<C2ParamReflector> &client,
const std::shared_ptr<Codec2Client::Configurable> &configurable);
//具体CCodec::allocate的调用逻辑如下(删除不必要语句):
void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) {
if (codecInfo == nullptr) {
mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
return;
}
ALOGD("allocate(%s)", codecInfo->getCodecName());
mClientListener.reset(new ClientListener(this));
AString componentName = codecInfo->getCodecName();
std::shared_ptr<Codec2Client> client;
// set up preferred component store to access vendor store parameters
client = Codec2Client::CreateFromService("default");
if (client) {
ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str());
SetPreferredCodec2ComponentStore(std::make_shared<Codec2ClientInterfaceWrapper>(client));
}
std::shared_ptr<Codec2Client::Component> comp;
c2_status_t status = Codec2Client::CreateComponentByName(
componentName.c_str(),
mClientListener,
&comp,
&client);
ALOGI("Created component [%s]", componentName.c_str());
mChannel->setComponent(comp);
Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
const std::unique_ptr<Config> &config = *configLocked;
status_t err = config->initialize(mClient->getParamReflector(), comp);
config->queryConfiguration(comp);
mCallback->onComponentAllocated(componentName.c_str());
}
小结:
1、MediaCodec创建CCodec的对象,并用赋值给mCodec。
2、设置mCodec的CodecCallback 和 mBufferChannel的BufferCallback。
3、调用mCodec的initiateAllocateComponent,并且根据传入的codecInfo创建Service服务,并获得平台硬件编解码支持的服务。
4、根据componentName创建解码组件,并且调用数据回调类CCodecBufferChannel::setComponent设置组件。
5、调用initialize、queryConfiguration、onComponentAllocated等函数初始化。
3、整体时序图
站在巨人的肩膀上!
参考连接:
1、一文搞懂Codec2框架解析
最后,如果错误,希望读者不吝赐教,共同进步!
来源:https://blog.csdn.net/anwh9295/article/details/120048151
猜你喜欢
- 前言本文我们主要实战Mybatis官方的代码生成器:Mybatis Generator(MBG),掌握它以后,可以简化大部分手写代码,我们只
- 1. 问题所示编译ssm的项目的时候出现了这个错误导致一直运行不起来SLF4J: Failed to load class "or
- 我们知道,在 Python 里面,如果你要运行一段 JavaScript,你可以使用execJS这种第三方库。那么在 Golang 里面,你
- Java中对象的创建clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象。所谓的复制对象,首先要分配一
- 如何解决某个节点故障的问题?如何解决数据一致性的问题?如何解决数据倾斜的问题?CAP理论先从定义开始:C(Consistence):一致性所
- 首先,看一下效果 可能各位在别处看到过类似的东西,我在微信的文章末尾看到有个玩意,感觉有意思,就用代码实现一下。这篇文章主要把握
- 开发环境:android4.1.1实验功能:在第一个Hello World!为标签的activity中显示good,该界面中有一个名为Nex
- 前言 之前在做一个Viewpager上面加载多个Fragment时总会实例化已经创建好的Fragmnet对象类似 viewPag
- 网上看了很多篇文章关于如何配置mybatis的logback日志的,复杂的简单的都有,但是有用的没几个,耽误了很多时间。通过对logback
- 本文实例讲述了C#实现用于操作wav声音文件的类。分享给大家供大家参考。具体如下:有了这个C#类,我们可以很轻易的调用本地wav文件进行同步
- 密封类和密封接口是 Kotlin 中允许创建受限类层次结构的两个特性。这两个构造用于定义一组有限的可能子类型,并防止在声明的层次结构之外定义
- 在开发过程中,我们需要统一返回前端json格式的数据,但有些接口的返回值存在 null或者""这种没有意义的字段。不仅影
- 本文为大家解析了java原码补码反码的关系,供大家参考,具体内容如下原码:不管源数据是十进制还是十六进制,统统将数字转成二进制形式反码:把原
- 本文以C#及VB.NET后端程序代码示例展示如何将HTML转为XML文件。转换时,调用Word API -Free Spire.Doc fo
- Java线程同步属于Java多线程与并发编程的核心点,需要重点掌握,下面我就来详解Java线程同步的4种主要的实现方式什么是Java线程同步
- 目录Demo展示介绍计时器功能Unity计时器Demo展示介绍游戏中有非常多的计时功能,比如:各种cd,以及需要延时调用的方法;一般实现有一
- 我们知道,Object类是所有类的父类,因此也被称为根类、祖先。那么,我们就来看一看Object类的最常用的两个方法是如何用的。1.toSt
- 通常的函数调用 一个通常的函数调用的例子://自行包含头文件void MyFun(int x); //此
- 本文实例为大家分享了AndAndroid实现网页图片浏览的具体代码,供大家参考,具体内容如下基本功能:输入图片的url然后点击按钮加载出来图
- 本文实例为大家分享了C语言实现简单弹跳小球的具体代码,供大家参考,具体内容如下本节利用 printf 函数 实现一个在屏幕上弹跳的小球,内容