Android中初始化Codec2的具體流程
1、MediaCodec調(diào)用流程
首先,我們先看下MediaCodec::CreateByType函數(shù)里面做了什么:
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調(diào)用CreateByType的重載函數(shù)。
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、根據(jù)matchingCodecs創(chuàng)建MediaCodec 對應(yīng)的解碼器調(diào)用init。
MediaCodec::init再根據(jù)創(chuàng)建來的名字調(diào)用mGetCodecBase這個(gè) 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指向的是下列函數(shù):
創(chuàng)建一個(gè)父類的對象,具體這父類對象是走Codec2還是ACodec的決定在下列函數(shù)中:
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里面調(diào)用MediaCodec.cpp文件中:
static CodecBase *CreateCCodec() {
return new CCodec;
}
這時(shí)候就走到的CCodec這個(gè)類中,它的構(gòu)造函數(shù):
// 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調(diào)用BufferChannelBase類型的mBufferChannel 實(shí)際上是調(diào)用CCodec里面的 mChannel
mBufferChannel設(shè)置一個(gè)new 的BufferCallback()對象的。
mCodec->setCallback(
std::unique_ptr<CodecBase::CodecCallback>(
new CodecCallback(new AMessage(kWhatCodecNotify, this))));
實(shí)際上設(shè)置的是CodecBase里面的CodecCallback mCallback
struct CodecBase : public AHandler{
void setCallback(std::unique_ptr<CodecCallback> &&callback) {
mCallback = std::move(callback);
}
protected:
std::unique_ptr<CodecCallback> mCallback;
}
之后設(shè)置了BufferCallBack。
mBufferChannel->setCallback(
std::unique_ptr<CodecBase::BufferCallback>(
new BufferCallback(new AMessage(kWhatCodecNotify, this))));
實(shí)際上設(shè)置的是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發(fā)送kWhatInit消息,處理之后就調(diào)用了CCodec->initiateAllocateComponent()。接下來我們需要看CCodec里面的調(diào)用邏輯了。
2、CCodec調(diào)用流程
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){}
構(gòu)造函數(shù)初始化的時(shí)候,就創(chuàng)建new CCodecCallbackImpl對象出來,CCodecCallbackImpl是繼承CCodecCallBack的 就做一個(gè)適配封裝處理。CCodecCallbackImpl 是CCodec的友元類。
上面調(diào)用了CCodec->initiateAllocateComponent(),其實(shí)CCodec::initiateAllocateComponent 也就是發(fā)送kWhatAllocate消息。一切都交給CCodec::onMessageReceived 進(jìn)行處理。在接受 onMessageReceived 中的case語句中,kWhatAllocate 調(diào)用CCodec::allocate
接著使用client = Codec2Client::CreateFromService(“default”);創(chuàng)建一個(gè)服務(wù),根據(jù)傳入的setAsPreferredCodec2ComponentStore 設(shè)置SetPreferredCodec2ComponentStore 默認(rèn)是false.但是默認(rèn)是false,這里沒有傳入。
這里的client = Codec2Client::CreateFromService(“default”);創(chuàng)建成功后調(diào)用SetPreferredCodec2ComponentStore,將vendor下支持的Codec2的server設(shè)置進(jìn)來。之后將重置的mClientListener、獲得的componentName名字、Codec2Client::Component的組件comp及Codec2Client::CreateFromService(“default”)返回的client,一起作為參數(shù),再重新調(diào)用CreateComponentByName創(chuàng)建組件。
之后給CCodecBufferChannel mChannel設(shè)置組件,用于綁定組件的回調(diào)。
class CCodecBufferChannel : public BufferChannelBase;
接著CCodec::allocate中調(diào)用CCodecConfig::initialize、CCodecConfig::queryConfiguration、CodecCallback::onComponentAllocated函數(shù)。
具體的代碼調(diào)用邏輯,如下所示:
//Codec2Client::Component : public Codec2Client::Configurable
status_t CCodecConfig::initialize(
const std::shared_ptr<C2ParamReflector> &client,
const std::shared_ptr<Codec2Client::Configurable> &configurable);
//具體CCodec::allocate的調(diào)用邏輯如下(刪除不必要語句):
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());
}
小結(jié):
1、MediaCodec創(chuàng)建CCodec的對象,并用賦值給mCodec。
2、設(shè)置mCodec的CodecCallback 和 mBufferChannel的BufferCallback。
3、調(diào)用mCodec的initiateAllocateComponent,并且根據(jù)傳入的codecInfo創(chuàng)建Service服務(wù),并獲得平臺硬件編解碼支持的服務(wù)。
4、根據(jù)componentName創(chuàng)建解碼組件,并且調(diào)用數(shù)據(jù)回調(diào)類CCodecBufferChannel::setComponent設(shè)置組件。
5、調(diào)用initialize、queryConfiguration、onComponentAllocated等函數(shù)初始化。
3、整體時(shí)序圖

站在巨人的肩膀上!
參考連接:
最后,如果錯(cuò)誤,希望讀者不吝賜教,共同進(jìn)步!
到此這篇關(guān)于Android中初始化Codec2的具體流程的文章就介紹到這了,更多相關(guān)Android Codec2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android組合式自定義控件實(shí)現(xiàn)購物車加減商品操作
這篇文章主要介紹了Android組合式自定義控件實(shí)現(xiàn)購物車加減商品操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
android底部菜單欄實(shí)現(xiàn)原理與代碼
底部菜單欄很重要,我看了一下很多應(yīng)用軟件都是用了底部菜單欄做,我這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數(shù)量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個(gè)人看起來還是不錯(cuò)的,所以貼出來方便以后使用2013-01-01
Android實(shí)現(xiàn)拖動(dòng)小球跟隨手指移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拖動(dòng)小球跟隨手指移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android 通過TCP協(xié)議上傳指定目錄文件的方法
這篇文章主要介紹了Android 通過TCP協(xié)議上傳指定目錄文件的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
怎么發(fā)布打包并發(fā)布自己的Android應(yīng)用(APP)
前面我為大家講的都是關(guān)于Android開發(fā)方面的知識點(diǎn)和技術(shù),不少朋友可能會感到疑惑--究竟我該怎么打包、發(fā)布自己開發(fā)的APP,怎樣將我的APP放到網(wǎng)上工別人下載,怎樣保證我的APP安全及版權(quán)問題呢2013-11-11
Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
這篇文章主要介紹了Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法,涉及Android界面布局相關(guān)屬性與狀態(tài)設(shè)置操作技巧,需要的朋友可以參考下2019-03-03
Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼
本篇文章主要介紹了Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01

