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 對應的解碼器調(diào)用init。
MediaCodec::init再根據(jù)創(chuàng)建來的名字調(diào)用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指向的是下列函數(shù):
創(chuàng)建一個父類的對象,具體這父類對象是走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;
}
這時候就走到的CCodec這個類中,它的構(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 實際上是調(diào)用CCodec里面的 mChannel
mBufferChannel設(shè)置一個new 的BufferCallback()對象的。
mCodec->setCallback(
std::unique_ptr<CodecBase::CodecCallback>(
new CodecCallback(new AMessage(kWhatCodecNotify, this))));
實際上設(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è)置的是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ù)初始化的時候,就創(chuàng)建new CCodecCallbackImpl對象出來,CCodecCallbackImpl是繼承CCodecCallBack的 就做一個適配封裝處理。CCodecCallbackImpl 是CCodec的友元類。
上面調(diào)用了CCodec->initiateAllocateComponent(),其實CCodec::initiateAllocateComponent 也就是發(fā)送kWhatAllocate消息。一切都交給CCodec::onMessageReceived 進行處理。在接受 onMessageReceived 中的case語句中,kWhatAllocate 調(diào)用CCodec::allocate
接著使用client = Codec2Client::CreateFromService(“default”);創(chuàng)建一個服務(wù),根據(jù)傳入的setAsPreferredCodec2ComponentStore 設(shè)置SetPreferredCodec2ComponentStore 默認是false.但是默認是false,這里沒有傳入。
這里的client = Codec2Client::CreateFromService(“default”);創(chuàng)建成功后調(diào)用SetPreferredCodec2ComponentStore,將vendor下支持的Codec2的server設(shè)置進來。之后將重置的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、整體時序圖

站在巨人的肩膀上!
參考連接:
最后,如果錯誤,希望讀者不吝賜教,共同進步!
到此這篇關(guān)于Android中初始化Codec2的具體流程的文章就介紹到這了,更多相關(guān)Android Codec2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Android HTML5 audio autoplay無效問題的解決方案
這篇文章主要介紹了關(guān)于Android HTML5 audio autoplay無效問題的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android啟動初始化方案App StartUp的應用詳解
這篇文章主要介紹了Android啟動初始化方案App StartUp的使用方法,StartUp是為了App的啟動提供的一套簡單、高效的初始化方案,下面我們來詳細了解2022-09-09
Android編程實現(xiàn)Dialog窗體監(jiān)聽的方法
這篇文章主要介紹了Android編程實現(xiàn)Dialog窗體監(jiān)聽的方法,結(jié)合實例形式分析了Android針對Dialog對話框窗體事件監(jiān)聽與響應相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android Compose實現(xiàn)聯(lián)系人列表流程
聲明式UI,更簡單的自定義,實時帶交互的預覽功能Compose并不是類似于Recyclerview的高級控件,而是直接拋棄了View,ViewGroup那套東西,從上到下魯了一套全新的框架,直白點說就是它的渲染機制,布局機制,觸摸算法,以及UI具體寫法全都是新的2023-03-03
Android實現(xiàn)打開各種文件的intent方法小結(jié)
這篇文章主要介紹了Android實現(xiàn)打開各種文件的intent方法,結(jié)合實例形式總結(jié)分析了Android針對HTML、圖片文件、pdf文件、文本文件、音頻文件、視頻文件等的intent打開方法,需要的朋友可以參考下2016-08-08
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

