1.AudioFlinger启动流程
service media /system/bin/mediaserver
class main
user media
group audio camera inet net_bt net_bt_admin net_bw_acct drmrpc mediadrm
ioprio rt 4
当init进程启动后,会去启动mediaServer服务。MediaServer服务启动后,会call到main_mediaserver.cpp的main函数,main函数会去初始化各种service
InitializeIcuOrDie();
sp proc(ProcessState::self());
sp sm = defaultServiceManager();
ALOGI("ServiceManager: %p", sm.get());
AudioFlinger::instantiate();
MediaPlayerService::instantiate();
ResourceManagerService::instantiate();
CameraService::instantiate();
AudioPolicyService::instantiate();
SoundTriggerHwService::instantiate();
RadioService::instantiate();
registerExtensions();
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
这里我们比较关心的是AudioFlinger和AudioPolicyService,在AudioFlinger.cpp和AudioPolicyService.cpp没有对应的instantiate()函数实现,而是直接call到BinderService.h的instantiate函数:
static void instantiate() { publish(); }
BinderService.h的publish函数,主要是将service注册到ServiceManager中:
static status_t publish(bool allowIsolated = false) {
sp sm(defaultServiceManager());
return sm->addService(
String16(SERVICE::getServiceName()),
new SERVICE(), allowIsolated);
}
// establish binder interface to AudioFlinger service
const sp AudioSystem::get_audio_flinger()
{
sp af;
sp afc;
{
Mutex::Autolock _l(gLock);
if (gAudioFlinger == 0) {
sp sm = defaultServiceManager();
sp binder;
do {
binder = sm->getService(String16("media.audio_flinger"));
if (binder != 0)
break;
ALOGW("AudioFlinger not published, waiting...");
usleep(500000); // 0.5 s
} while (true);
if (gAudioFlingerClient == NULL) {
gAudioFlingerClient = new AudioFlingerClient();
} else {
if (gAudioErrorCallback) {
gAudioErrorCallback(NO_ERROR);
}
}
binder->linkToDeath(gAudioFlingerClient);
gAudioFlinger = interface_cast(binder);
LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
afc = gAudioFlingerClient;
}
af = gAudioFlinger;
}
if (afc != 0) {
af->registerClient(afc);
}
return af;
}
在上面的函数中可以看到通过ServiceManager拿到AudioFlinger的实例(Binder Client端)并作为函数返回值。AudioFlingerClient实例化以后,会注册到AudioFlinger中。
status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
uint32_t sampleRate, audio_format_t format,
audio_channel_mask_t channelMask, size_t* buffSize)
{
const sp& af = AudioSystem::get_audio_flinger();
if (af == 0) {
return PERMISSION_DENIED;
}
Mutex::Autolock _l(mLock);
// Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
|| (channelMask != mInChannelMask)) {
size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
if (inBuffSize == 0) {
ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
sampleRate, format, channelMask);
return BAD_VALUE;
}
// A benign race is possible here: we could overwrite a fresher cache entry
// save the request params
mInSamplingRate = sampleRate;
mInFormat = format;
mInChannelMask = channelMask;
mInBuffSize = inBuffSize;
}
*buffSize = mInBuffSize;
return NO_ERROR;
}
但是在AudioSystem中不会直接call AudioFlinger的getInputBufferSize函数,而是先call AudioFlingerClient中的getInputBufferSize,再由AudioFlingerClient call到AudioFlinger。
// establish binder interface to AudioPolicy service
const sp AudioSystem::get_audio_policy_service()
{
sp ap;
sp apc;
{
Mutex::Autolock _l(gLockAPS);
if (gAudioPolicyService == 0) {
sp sm = defaultServiceManager();
sp binder;
do {
binder = sm->getService(String16("media.audio_policy"));
if (binder != 0)
break;
ALOGW("AudioPolicyService not published, waiting...");
usleep(500000); // 0.5 s
} while (true);
if (gAudioPolicyServiceClient == NULL) {
gAudioPolicyServiceClient = new AudioPolicyServiceClient();
}
binder->linkToDeath(gAudioPolicyServiceClient);
gAudioPolicyService = interface_cast(binder);
LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
apc = gAudioPolicyServiceClient;
}
ap = gAudioPolicyService;
}
if (apc != 0) {
ap->registerClient(apc);
}
return ap;
}
通过SysytemManager拿到IAudioPolicyService Binder Client端的实例,并作为函数返回值返回。同时实例化AudioPolicyServiceClient,并注册到AudioPolicyService中。