Skip to content

Commit

Permalink
Merge "Report video battery on/off from mediaserver" into qt-qpr1-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Chong Zhang authored and Android (Google) Code Review committed Aug 20, 2019
2 parents 90f56b7 + ee33d64 commit 57eeeb0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 39 deletions.
5 changes: 4 additions & 1 deletion media/libmedia/IResourceManagerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ class BpResourceManagerService : public BpInterface<IResourceManagerService>

virtual void addResource(
int pid,
int uid,
int64_t clientId,
const sp<IResourceManagerClient> client,
const Vector<MediaResource> &resources) {
Parcel data, reply;
data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
data.writeInt32(pid);
data.writeInt32(uid);
data.writeInt64(clientId);
data.writeStrongBinder(IInterface::asBinder(client));
writeToParcel(&data, resources);
Expand Down Expand Up @@ -129,6 +131,7 @@ status_t BnResourceManagerService::onTransact(
case ADD_RESOURCE: {
CHECK_INTERFACE(IResourceManagerService, data, reply);
int pid = data.readInt32();
int uid = data.readInt32();
int64_t clientId = data.readInt64();
sp<IResourceManagerClient> client(
interface_cast<IResourceManagerClient>(data.readStrongBinder()));
Expand All @@ -137,7 +140,7 @@ status_t BnResourceManagerService::onTransact(
}
Vector<MediaResource> resources;
readFromParcel(data, &resources);
addResource(pid, clientId, client, resources);
addResource(pid, uid, clientId, client, resources);
return NO_ERROR;
} break;

Expand Down
1 change: 1 addition & 0 deletions media/libmedia/include/media/IResourceManagerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class IResourceManagerService: public IInterface

virtual void addResource(
int pid,
int uid,
int64_t clientId,
const sp<IResourceManagerClient> client,
const Vector<MediaResource> &resources) = 0;
Expand Down
3 changes: 2 additions & 1 deletion media/libmedia/include/media/MediaResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ class MediaResource {
kNonSecureCodec,
kGraphicMemory,
kCpuBoost,
kBattery,
};

enum SubType {
kUnspecifiedSubType = 0,
kAudioCodec,
kVideoCodec
kVideoCodec,
};

MediaResource();
Expand Down
33 changes: 11 additions & 22 deletions media/libstagefright/MediaCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#include <media/stagefright/OMXClient.h>
#include <media/stagefright/PersistentSurface.h>
#include <media/stagefright/SurfaceUtils.h>
#include <mediautils/BatteryNotifier.h>
#include <private/android_filesystem_config.h>
#include <utils/Singleton.h>

Expand Down Expand Up @@ -166,8 +165,9 @@ struct ResourceManagerClient : public BnResourceManagerClient {
DISALLOW_EVIL_CONSTRUCTORS(ResourceManagerClient);
};

MediaCodec::ResourceManagerServiceProxy::ResourceManagerServiceProxy(pid_t pid)
: mPid(pid) {
MediaCodec::ResourceManagerServiceProxy::ResourceManagerServiceProxy(
pid_t pid, uid_t uid)
: mPid(pid), mUid(uid) {
if (mPid == MediaCodec::kNoPid) {
mPid = IPCThreadState::self()->getCallingPid();
}
Expand Down Expand Up @@ -204,7 +204,7 @@ void MediaCodec::ResourceManagerServiceProxy::addResource(
if (mService == NULL) {
return;
}
mService->addResource(mPid, clientId, client, resources);
mService->addResource(mPid, mUid, clientId, client, resources);
}

void MediaCodec::ResourceManagerServiceProxy::removeResource(int64_t clientId) {
Expand Down Expand Up @@ -517,8 +517,6 @@ MediaCodec::MediaCodec(const sp<ALooper> &looper, pid_t pid, uid_t uid)
mStickyError(OK),
mSoftRenderer(NULL),
mAnalyticsItem(NULL),
mResourceManagerClient(new ResourceManagerClient(this)),
mResourceManagerService(new ResourceManagerServiceProxy(pid)),
mBatteryStatNotified(false),
mIsVideo(false),
mVideoWidth(0),
Expand All @@ -537,6 +535,8 @@ MediaCodec::MediaCodec(const sp<ALooper> &looper, pid_t pid, uid_t uid)
} else {
mUid = uid;
}
mResourceManagerClient = new ResourceManagerClient(this);
mResourceManagerService = new ResourceManagerServiceProxy(pid, mUid);

initAnalyticsItem();
}
Expand Down Expand Up @@ -1977,6 +1977,11 @@ void MediaCodec::onMessageReceived(const sp<AMessage> &msg) {
if (mIsVideo) {
// audio codec is currently ignored.
addResource(resourceType, MediaResource::kVideoCodec, 1);
// TODO: track battery on/off by actual queueing/dequeueing
// For now, keep existing behavior and request battery on/off
// together with codec init/uninit. We'll improve the tracking
// later by adding/removing this based on queue/dequeue timing.
addResource(MediaResource::kBattery, MediaResource::kVideoCodec, 1);
}

(new AMessage)->postReply(mReplyID);
Expand Down Expand Up @@ -3126,8 +3131,6 @@ void MediaCodec::setState(State newState) {
mState = newState;

cancelPendingDequeueOperations();

updateBatteryStat();
}

void MediaCodec::returnBuffersToCodec(bool isReclaim) {
Expand Down Expand Up @@ -3631,20 +3634,6 @@ status_t MediaCodec::amendOutputFormatWithCodecSpecificData(
return OK;
}

void MediaCodec::updateBatteryStat() {
if (!mIsVideo) {
return;
}

if (mState == CONFIGURED && !mBatteryStatNotified) {
BatteryNotifier::getInstance().noteStartVideo(mUid);
mBatteryStatNotified = true;
} else if (mState == UNINITIALIZED && mBatteryStatNotified) {
BatteryNotifier::getInstance().noteStopVideo(mUid);
mBatteryStatNotified = false;
}
}

std::string MediaCodec::stateString(State state) {
const char *rval = NULL;
char rawbuffer[16]; // room for "%d"
Expand Down
4 changes: 2 additions & 2 deletions media/libstagefright/include/media/stagefright/MediaCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ struct MediaCodec : public AHandler {
};

struct ResourceManagerServiceProxy : public IBinder::DeathRecipient {
ResourceManagerServiceProxy(pid_t pid);
ResourceManagerServiceProxy(pid_t pid, uid_t uid);
~ResourceManagerServiceProxy();

void init();
Expand All @@ -304,6 +304,7 @@ struct MediaCodec : public AHandler {
Mutex mLock;
sp<IResourceManagerService> mService;
pid_t mPid;
uid_t mUid;
};

State mState;
Expand Down Expand Up @@ -425,7 +426,6 @@ struct MediaCodec : public AHandler {
status_t onSetParameters(const sp<AMessage> &params);

status_t amendOutputFormatWithCodecSpecificData(const sp<MediaCodecBuffer> &buffer);
void updateBatteryStat();
bool isExecuting() const;

uint64_t getGraphicBufferSize();
Expand Down
24 changes: 20 additions & 4 deletions services/mediaresourcemanager/ResourceManagerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

#include <binder/IMediaResourceMonitor.h>
#include <binder/IServiceManager.h>
#include <cutils/sched_policy.h>
#include <dirent.h>
#include <media/stagefright/ProcessInfo.h>
#include <mediautils/BatteryNotifier.h>
#include <mediautils/SchedulingPolicyService.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand All @@ -31,8 +34,7 @@

#include "ResourceManagerService.h"
#include "ServiceLog.h"
#include "mediautils/SchedulingPolicyService.h"
#include <cutils/sched_policy.h>

namespace android {

namespace {
Expand Down Expand Up @@ -101,6 +103,7 @@ static ResourceInfos& getResourceInfosForEdit(
}

static ResourceInfo& getResourceInfoForEdit(
uid_t uid,
int64_t clientId,
const sp<IResourceManagerClient>& client,
ResourceInfos& infos) {
Expand All @@ -110,9 +113,11 @@ static ResourceInfo& getResourceInfoForEdit(
}
}
ResourceInfo info;
info.uid = uid;
info.clientId = clientId;
info.client = client;
info.cpuBoost = false;
info.batteryNoted = false;
infos.push_back(info);
return infos.editItemAt(infos.size() - 1);
}
Expand Down Expand Up @@ -204,7 +209,9 @@ ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processI
mServiceLog(new ServiceLog()),
mSupportsMultipleSecureCodecs(true),
mSupportsSecureWithNonSecureCodec(true),
mCpuBoostCount(0) {}
mCpuBoostCount(0) {
BatteryNotifier::getInstance().noteResetVideo();
}

ResourceManagerService::~ResourceManagerService() {}

Expand All @@ -226,6 +233,7 @@ void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies)

void ResourceManagerService::addResource(
int pid,
int uid,
int64_t clientId,
const sp<IResourceManagerClient> client,
const Vector<MediaResource> &resources) {
Expand All @@ -239,7 +247,7 @@ void ResourceManagerService::addResource(
return;
}
ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos);
ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
// TODO: do the merge instead of append.
info.resources.appendVector(resources);

Expand All @@ -253,6 +261,11 @@ void ResourceManagerService::addResource(
ALOGW("couldn't request cpuset boost");
}
mCpuBoostCount++;
} else if (resources[i].mType == MediaResource::kBattery
&& resources[i].mSubType == MediaResource::kVideoCodec
&& !info.batteryNoted) {
info.batteryNoted = true;
BatteryNotifier::getInstance().noteStartVideo(info.uid);
}
}
if (info.deathNotifier == nullptr) {
Expand Down Expand Up @@ -291,6 +304,9 @@ void ResourceManagerService::removeResource(int pid, int64_t clientId, bool chec
requestCpusetBoost(false, this);
}
}
if (infos[j].batteryNoted) {
BatteryNotifier::getInstance().noteStopVideo(infos[j].uid);
}
IInterface::asBinder(infos[j].client)->unlinkToDeath(infos[j].deathNotifier);
j = infos.removeAt(j);
found = true;
Expand Down
3 changes: 3 additions & 0 deletions services/mediaresourcemanager/ResourceManagerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ struct ProcessInfoInterface;

struct ResourceInfo {
int64_t clientId;
uid_t uid;
sp<IResourceManagerClient> client;
sp<IBinder::DeathRecipient> deathNotifier;
Vector<MediaResource> resources;
bool cpuBoost;
bool batteryNoted;
};

typedef Vector<ResourceInfo> ResourceInfos;
Expand All @@ -61,6 +63,7 @@ class ResourceManagerService

virtual void addResource(
int pid,
int uid,
int64_t clientId,
const sp<IResourceManagerClient> client,
const Vector<MediaResource> &resources);
Expand Down
24 changes: 15 additions & 9 deletions services/mediaresourcemanager/test/ResourceManagerService_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ struct TestClient : public BnResourceManagerClient {
};

static const int kTestPid1 = 30;
static const int kTestUid1 = 1010;

static const int kTestPid2 = 20;
static const int kTestUid2 = 1011;

static const int kLowPriorityPid = 40;
static const int kMidPriorityPid = 25;
Expand Down Expand Up @@ -115,8 +118,11 @@ class ResourceManagerServiceTest : public ::testing::Test {
return true;
}

static void expectEqResourceInfo(const ResourceInfo &info, sp<IResourceManagerClient> client,
static void expectEqResourceInfo(const ResourceInfo &info,
int uid,
sp<IResourceManagerClient> client,
const Vector<MediaResource> &resources) {
EXPECT_EQ(uid, info.uid);
EXPECT_EQ(client, info.client);
EXPECT_TRUE(isEqualResources(resources, info.resources));
}
Expand Down Expand Up @@ -153,39 +159,39 @@ class ResourceManagerServiceTest : public ::testing::Test {
// kTestPid1 mTestClient1
Vector<MediaResource> resources1;
resources1.push_back(MediaResource(MediaResource::kSecureCodec, 1));
mService->addResource(kTestPid1, getId(mTestClient1), mTestClient1, resources1);
mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
resources1.push_back(MediaResource(MediaResource::kGraphicMemory, 200));
Vector<MediaResource> resources11;
resources11.push_back(MediaResource(MediaResource::kGraphicMemory, 200));
mService->addResource(kTestPid1, getId(mTestClient1), mTestClient1, resources11);
mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11);

// kTestPid2 mTestClient2
Vector<MediaResource> resources2;
resources2.push_back(MediaResource(MediaResource::kNonSecureCodec, 1));
resources2.push_back(MediaResource(MediaResource::kGraphicMemory, 300));
mService->addResource(kTestPid2, getId(mTestClient2), mTestClient2, resources2);
mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2);

// kTestPid2 mTestClient3
Vector<MediaResource> resources3;
mService->addResource(kTestPid2, getId(mTestClient3), mTestClient3, resources3);
mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3);
resources3.push_back(MediaResource(MediaResource::kSecureCodec, 1));
resources3.push_back(MediaResource(MediaResource::kGraphicMemory, 100));
mService->addResource(kTestPid2, getId(mTestClient3), mTestClient3, resources3);
mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3);

const PidResourceInfosMap &map = mService->mMap;
EXPECT_EQ(2u, map.size());
ssize_t index1 = map.indexOfKey(kTestPid1);
ASSERT_GE(index1, 0);
const ResourceInfos &infos1 = map[index1];
EXPECT_EQ(1u, infos1.size());
expectEqResourceInfo(infos1[0], mTestClient1, resources1);
expectEqResourceInfo(infos1[0], kTestUid1, mTestClient1, resources1);

ssize_t index2 = map.indexOfKey(kTestPid2);
ASSERT_GE(index2, 0);
const ResourceInfos &infos2 = map[index2];
EXPECT_EQ(2u, infos2.size());
expectEqResourceInfo(infos2[0], mTestClient2, resources2);
expectEqResourceInfo(infos2[1], mTestClient3, resources3);
expectEqResourceInfo(infos2[0], kTestUid2, mTestClient2, resources2);
expectEqResourceInfo(infos2[1], kTestUid2, mTestClient3, resources3);
}

void testConfig() {
Expand Down

0 comments on commit 57eeeb0

Please sign in to comment.