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 對(duì)應(yīng)的解碼器調(diào)用init。
MediaCodec::init再根據(jù)創(chuàng)建來(lái)的名字調(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è)父類的對(duì)象,具體這父類對(duì)象是走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出來(lái)的。
class CCodecBufferChannel : public BufferChannelBase;
上面的 mBufferChannel = mCodec->getBufferChannel(); 就是把CCodec的mChannel返回到MediaCodec中。
std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
return mChannel;
}
也就是說(shuō)MediaCodec調(diào)用BufferChannelBase類型的mBufferChannel 實(shí)際上是調(diào)用CCodec里面的 mChannel
mBufferChannel設(shè)置一個(gè)new 的BufferCallback()對(duì)象的。
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()。接下來(lái)我們需要看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對(duì)象出來(lái),CCodecCallbackImpl是繼承CCodecCallBack的 就做一個(gè)適配封裝處理。CCodecCallbackImpl 是CCodec的友元類。
上面調(diào)用了CCodec->initiateAllocateComponent(),其實(shí)CCodec::initiateAllocateComponent 也就是發(fā)送kWhatAllocate消息。一切都交給CCodec::onMessageReceived 進(jìn)行處理。在接受 onMessageReceived 中的case語(yǔ)句中,kWhatAllocate 調(diào)用CCodec::allocate
接著使用client = Codec2Client::CreateFromService(“default”);創(chuàng)建一個(gè)服務(wù),根據(jù)傳入的setAsPreferredCodec2ComponentStore 設(shè)置SetPreferredCodec2ComponentStore 默認(rèn)是false.但是默認(rèn)是false,這里沒(méi)有傳入。
這里的client = Codec2Client::CreateFromService(“default”);創(chuàng)建成功后調(diào)用SetPreferredCodec2ComponentStore,將vendor下支持的Codec2的server設(shè)置進(jìn)來(lái)。之后將重置的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)用邏輯如下(刪除不必要語(yǔ)句):
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的對(duì)象,并用賦值給mCodec。
2、設(shè)置mCodec的CodecCallback 和 mBufferChannel的BufferCallback。
3、調(diào)用mCodec的initiateAllocateComponent,并且根據(jù)傳入的codecInfo創(chuàng)建Service服務(wù),并獲得平臺(tái)硬件編解碼支持的服務(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Android HTML5 audio autoplay無(wú)效問(wèn)題的解決方案
這篇文章主要介紹了關(guān)于Android HTML5 audio autoplay無(wú)效問(wèn)題的解決方案,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android啟動(dòng)初始化方案App StartUp的應(yīng)用詳解
這篇文章主要介紹了Android啟動(dòng)初始化方案App StartUp的使用方法,StartUp是為了App的啟動(dòng)提供的一套簡(jiǎn)單、高效的初始化方案,下面我們來(lái)詳細(xì)了解2022-09-09
Android編程實(shí)現(xiàn)Dialog窗體監(jiān)聽(tīng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)Dialog窗體監(jiān)聽(tīng)的方法,結(jié)合實(shí)例形式分析了Android針對(duì)Dialog對(duì)話框窗體事件監(jiān)聽(tīng)與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android Compose實(shí)現(xiàn)聯(lián)系人列表流程
聲明式UI,更簡(jiǎn)單的自定義,實(shí)時(shí)帶交互的預(yù)覽功能Compose并不是類似于Recyclerview的高級(jí)控件,而是直接拋棄了View,ViewGroup那套東西,從上到下魯了一套全新的框架,直白點(diǎn)說(shuō)就是它的渲染機(jī)制,布局機(jī)制,觸摸算法,以及UI具體寫(xiě)法全都是新的2023-03-03
Android實(shí)現(xiàn)打開(kāi)各種文件的intent方法小結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)打開(kāi)各種文件的intent方法,結(jié)合實(shí)例形式總結(jié)分析了Android針對(duì)HTML、圖片文件、pdf文件、文本文件、音頻文件、視頻文件等的intent打開(kāi)方法,需要的朋友可以參考下2016-08-08
Android實(shí)現(xiàn)截屏與截長(zhǎng)圖功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)截屏,以及Android實(shí)現(xiàn)截長(zhǎng)圖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android源代碼倉(cāng)庫(kù)及其管理工具Repo分析詳解
本篇文章主要介紹了Android源代碼倉(cāng)庫(kù)及其管理工具Repo分析詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android this與Activity.this的區(qū)別
這篇文章主要介紹了 Android this與Activity.this的區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android ksoap調(diào)用webservice批量上傳多張圖片詳解
這篇文章主要介紹了Android ksoap調(diào)用webservice批量上傳多張圖片詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02

