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服務,并獲得平臺硬體編解碼支持的服務,
2、根據componentName創建解碼組件,并且呼叫資料回呼類CCodecBufferChannel::setComponent設定組件,
3、呼叫initialize、queryConfiguration、onComponentAllocated等函式初始化,
3、整體時序圖

站在巨人的肩膀上!
參考連接:
1、Codec2入門:框架決議
最后,如果錯誤,希望讀者不吝賜教,共同進步!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/298403.html
標籤:其他
上一篇:Flutter Key的原理和使用(五) 需要key的實體:可拖動改變順序的Listview
下一篇:王者戰力查詢介面(免費)
