SystemCallbackInterface 接口定义在头文件中,定义了四个public的函数
frameworks/av/services/mediaresourcemanager/ResourceManagerService.h
class ResourceManagerService : public BnResourceManagerService {
public:
struct SystemCallbackInterface : public RefBase {
// 通知 uid 启动视频播放
virtual void noteStartVideo(int uid) = 0;
// 通知 uid 停止视频播放
virtual void noteStopVideo(int uid) = 0;
// 通知 uid 重置视频播放
virtual void noteResetVideo() = 0;
// 请求将 media.codec 所在进程的cpu等级移动到 SP_FOREGROUND 和 SP_FOREGROUND中间的级别
virtual bool requestCpusetBoost(bool enable) = 0;
};
}
frameworks/av/services/mediaresourcemanager/ResourceManagerService.cpp
struct SystemCallbackImpl :
public ResourceManagerService::SystemCallbackInterface {
SystemCallbackImpl() : mClientToken(new BBinder()) {}
virtual void noteStartVideo(int uid) override {
BatteryNotifier::getInstance().noteStartVideo(uid);
}
virtual void noteStopVideo(int uid) override {
BatteryNotifier::getInstance().noteStopVideo(uid);
}
virtual void noteResetVideo() override {
BatteryNotifier::getInstance().noteResetVideo();
}
virtual bool requestCpusetBoost(bool enable) override {
return android::requestCpusetBoost(enable, mClientToken);
}
protected:
virtual ~SystemCallbackImpl() {}
private:
DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);// 定义拷贝构造函数和赋值拷贝构造函数为私有,防止对象赋值
sp<IBinder> mClientToken;
};
从以上实现来看,主要是 BatteryNotifier 这个类的单例代理完成了功能。BatteryNotifier 是 media 模块的工具类, 主要功能是在 mediaserver 进程中记录电池寿命事件的类。详细分析见《BatteryNotifier 分析》
有个特别的函数需要说明一下 requestCpusetBoost() 实现在
frameworks/av/media/utils/SchedulingPolicyService.cpp
static sp<ISchedulingPolicyService> sSchedulingPolicyService;
static const String16 _scheduling_policy("scheduling_policy");
static Mutex sMutex;
int requestPriority(pid_t pid, pid_t tid, int32_t prio, bool isForApp, bool asynchronous)
{
// FIXME merge duplicated code related to service lookup, caching, and error recovery
int ret;
for (;;) {
sMutex.lock();
sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
sMutex.unlock();
if (sps == 0) {
sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
if (binder == 0) {
sleep(1);
continue;
}
sps = interface_cast<ISchedulingPolicyService>(binder);
sMutex.lock();
sSchedulingPolicyService = sps;
sMutex.unlock();
}
ret = sps->requestPriority(pid, tid, prio, isForApp, asynchronous);
if (ret != DEAD_OBJECT) {
break;
}
ALOGW("SchedulingPolicyService died");
sMutex.lock();
sSchedulingPolicyService.clear();
sMutex.unlock();
}
return ret;
}
int requestCpusetBoost(bool enable, const sp<IBinder> &client)
{
int ret;
sMutex.lock();
sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
sMutex.unlock();
if (sps == 0) {
sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
if (binder == 0) {
return DEAD_OBJECT;
}
sps = interface_cast<ISchedulingPolicyService>(binder);
sMutex.lock();
sSchedulingPolicyService = sps;
sMutex.unlock();
}
// 调用 SchedulingPolicyService 服务的 requestCpusetBoost 函数完成。
ret = sps->requestCpusetBoost(enable, client);
if (ret != DEAD_OBJECT) {
return ret;
}
ALOGW("SchedulingPolicyService died");
sMutex.lock();
sSchedulingPolicyService.clear();
sMutex.unlock();
return ret;
}
关于 SchedulingPolicyService 的详细分析,见《SchedulingPolicyService 分析》