From 99e69716215cd0665379bc90d708f2ea8689831d Mon Sep 17 00:00:00 2001 From: Ruben Brunk Date: Tue, 26 May 2015 17:25:07 -0700 Subject: Track camera and flashlight usage in battery stats. Bug: 15986092 Change-Id: I9dc6828332e4091fd93bf2d82839e8e3862a2fc2 --- media/libmediaplayerservice/Android.mk | 1 + media/libmediaplayerservice/MediaPlayerService.cpp | 16 +- media/libstagefright/Android.mk | 1 + media/libstagefright/MediaCodec.cpp | 130 +------------ media/utils/Android.mk | 39 ++++ media/utils/BatteryNotifier.cpp | 213 +++++++++++++++++++++ media/utils/README | 4 + media/utils/include/mediautils/BatteryNotifier.h | 73 +++++++ 8 files changed, 336 insertions(+), 141 deletions(-) create mode 100644 media/utils/Android.mk create mode 100644 media/utils/BatteryNotifier.cpp create mode 100644 media/utils/README create mode 100644 media/utils/include/mediautils/BatteryNotifier.h (limited to 'media') diff --git a/media/libmediaplayerservice/Android.mk b/media/libmediaplayerservice/Android.mk index 2c4e719..ce04505 100644 --- a/media/libmediaplayerservice/Android.mk +++ b/media/libmediaplayerservice/Android.mk @@ -33,6 +33,7 @@ LOCAL_SHARED_LIBRARIES := \ libdl \ libgui \ libmedia \ + libmediautils \ libsonivox \ libstagefright \ libstagefright_foundation \ diff --git a/media/libmediaplayerservice/MediaPlayerService.cpp b/media/libmediaplayerservice/MediaPlayerService.cpp index 891a9e9..3e2d967 100644 --- a/media/libmediaplayerservice/MediaPlayerService.cpp +++ b/media/libmediaplayerservice/MediaPlayerService.cpp @@ -34,7 +34,6 @@ #include -#include #include #include #include @@ -60,6 +59,7 @@ #include #include #include +#include #include @@ -287,17 +287,9 @@ MediaPlayerService::MediaPlayerService() // reset battery stats // if the mediaserver has crashed, battery stats could be left // in bad state, reset the state upon service start. - const sp sm(defaultServiceManager()); - if (sm != NULL) { - const String16 name("batterystats"); - // use checkService() to avoid blocking if service is not up yet - sp batteryStats = - interface_cast(sm->checkService(name)); - if (batteryStats != NULL) { - batteryStats->noteResetVideo(); - batteryStats->noteResetAudio(); - } - } + BatteryNotifier& notifier(BatteryNotifier::getInstance()); + notifier.noteResetVideo(); + notifier.noteResetAudio(); MediaPlayerFactory::registerBuiltinFactories(); } diff --git a/media/libstagefright/Android.mk b/media/libstagefright/Android.mk index 6010558..99f9d94 100644 --- a/media/libstagefright/Android.mk +++ b/media/libstagefright/Android.mk @@ -90,6 +90,7 @@ LOCAL_SHARED_LIBRARIES := \ libicuuc \ liblog \ libmedia \ + libmediautils \ libnetd_client \ libopus \ libsonivox \ diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp index 46c154d..c8dcef1 100644 --- a/media/libstagefright/MediaCodec.cpp +++ b/media/libstagefright/MediaCodec.cpp @@ -21,7 +21,6 @@ #include "include/avc_utils.h" #include "include/SoftwareRenderer.h" -#include #include #include #include @@ -47,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -107,134 +107,6 @@ private: DISALLOW_EVIL_CONSTRUCTORS(ResourceManagerClient); }; -struct MediaCodec::BatteryNotifier : public Singleton { - BatteryNotifier(); - virtual ~BatteryNotifier(); - - void noteStartVideo(); - void noteStopVideo(); - void noteStartAudio(); - void noteStopAudio(); - void onBatteryStatServiceDied(); - -private: - struct DeathNotifier : public IBinder::DeathRecipient { - DeathNotifier() {} - virtual void binderDied(const wp& /*who*/) { - BatteryNotifier::getInstance().onBatteryStatServiceDied(); - } - }; - - Mutex mLock; - int32_t mVideoRefCount; - int32_t mAudioRefCount; - sp mBatteryStatService; - sp mDeathNotifier; - - sp getBatteryService_l(); - - DISALLOW_EVIL_CONSTRUCTORS(BatteryNotifier); -}; - -ANDROID_SINGLETON_STATIC_INSTANCE(MediaCodec::BatteryNotifier) - -MediaCodec::BatteryNotifier::BatteryNotifier() : - mVideoRefCount(0), - mAudioRefCount(0) { -} - -sp MediaCodec::BatteryNotifier::getBatteryService_l() { - if (mBatteryStatService != NULL) { - return mBatteryStatService; - } - // get battery service from service manager - const sp sm(defaultServiceManager()); - if (sm != NULL) { - const String16 name("batterystats"); - mBatteryStatService = - interface_cast(sm->getService(name)); - if (mBatteryStatService == NULL) { - ALOGE("batterystats service unavailable!"); - return NULL; - } - mDeathNotifier = new DeathNotifier(); - IInterface::asBinder(mBatteryStatService)->linkToDeath(mDeathNotifier); - // notify start now if media already started - if (mVideoRefCount > 0) { - mBatteryStatService->noteStartVideo(AID_MEDIA); - } - if (mAudioRefCount > 0) { - mBatteryStatService->noteStartAudio(AID_MEDIA); - } - } - return mBatteryStatService; -} - -MediaCodec::BatteryNotifier::~BatteryNotifier() { - if (mDeathNotifier != NULL) { - IInterface::asBinder(mBatteryStatService)-> - unlinkToDeath(mDeathNotifier); - } -} - -void MediaCodec::BatteryNotifier::noteStartVideo() { - Mutex::Autolock _l(mLock); - sp batteryService = getBatteryService_l(); - if (mVideoRefCount == 0 && batteryService != NULL) { - batteryService->noteStartVideo(AID_MEDIA); - } - mVideoRefCount++; -} - -void MediaCodec::BatteryNotifier::noteStopVideo() { - Mutex::Autolock _l(mLock); - if (mVideoRefCount == 0) { - ALOGW("BatteryNotifier::noteStop(): video refcount is broken!"); - return; - } - - sp batteryService = getBatteryService_l(); - - mVideoRefCount--; - if (mVideoRefCount == 0 && batteryService != NULL) { - batteryService->noteStopVideo(AID_MEDIA); - } -} - -void MediaCodec::BatteryNotifier::noteStartAudio() { - Mutex::Autolock _l(mLock); - sp batteryService = getBatteryService_l(); - if (mAudioRefCount == 0 && batteryService != NULL) { - batteryService->noteStartAudio(AID_MEDIA); - } - mAudioRefCount++; -} - -void MediaCodec::BatteryNotifier::noteStopAudio() { - Mutex::Autolock _l(mLock); - if (mAudioRefCount == 0) { - ALOGW("BatteryNotifier::noteStop(): audio refcount is broken!"); - return; - } - - sp batteryService = getBatteryService_l(); - - mAudioRefCount--; - if (mAudioRefCount == 0 && batteryService != NULL) { - batteryService->noteStopAudio(AID_MEDIA); - } -} - -void MediaCodec::BatteryNotifier::onBatteryStatServiceDied() { - Mutex::Autolock _l(mLock); - mBatteryStatService.clear(); - mDeathNotifier.clear(); - // Do not reset mVideoRefCount and mAudioRefCount here. The ref - // counting is independent of the battery service availability. - // We need this if battery service becomes available after media - // started. -} - MediaCodec::ResourceManagerServiceProxy::ResourceManagerServiceProxy() { } diff --git a/media/utils/Android.mk b/media/utils/Android.mk new file mode 100644 index 0000000..dfadbc8 --- /dev/null +++ b/media/utils/Android.mk @@ -0,0 +1,39 @@ +# Copyright 2015 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + BatteryNotifier.cpp \ + +LOCAL_SHARED_LIBRARIES := \ + libbinder \ + libcutils \ + liblog \ + libutils \ + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include + +LOCAL_CFLAGS += \ + -Wall \ + -Wextra \ + -Werror \ + +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include + +LOCAL_MODULE := libmediautils + +include $(BUILD_SHARED_LIBRARY) diff --git a/media/utils/BatteryNotifier.cpp b/media/utils/BatteryNotifier.cpp new file mode 100644 index 0000000..7f9cd7a --- /dev/null +++ b/media/utils/BatteryNotifier.cpp @@ -0,0 +1,213 @@ +/* + * Copyright 2015, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "include/mediautils/BatteryNotifier.h" + +#include +#include +#include + +namespace android { + +void BatteryNotifier::DeathNotifier::binderDied(const wp& /*who*/) { + BatteryNotifier::getInstance().onBatteryStatServiceDied(); +} + +BatteryNotifier::BatteryNotifier() : mVideoRefCount(0), mAudioRefCount(0) {} + +BatteryNotifier::~BatteryNotifier() { + Mutex::Autolock _l(mLock); + if (mDeathNotifier != nullptr) { + IInterface::asBinder(mBatteryStatService)->unlinkToDeath(mDeathNotifier); + } +} + +void BatteryNotifier::noteStartVideo() { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + if (mVideoRefCount == 0 && batteryService != nullptr) { + batteryService->noteStartVideo(AID_MEDIA); + } + mVideoRefCount++; +} + +void BatteryNotifier::noteStopVideo() { + Mutex::Autolock _l(mLock); + if (mVideoRefCount == 0) { + ALOGW("%s: video refcount is broken.", __FUNCTION__); + return; + } + + sp batteryService = getBatteryService_l(); + + mVideoRefCount--; + if (mVideoRefCount == 0 && batteryService != nullptr) { + batteryService->noteStopVideo(AID_MEDIA); + } +} + +void BatteryNotifier::noteResetVideo() { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + mVideoRefCount = 0; + if (batteryService != nullptr) { + batteryService->noteResetAudio(); + } +} + +void BatteryNotifier::noteStartAudio() { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + if (mAudioRefCount == 0 && batteryService != nullptr) { + batteryService->noteStartAudio(AID_MEDIA); + } + mAudioRefCount++; +} + +void BatteryNotifier::noteStopAudio() { + Mutex::Autolock _l(mLock); + if (mAudioRefCount == 0) { + ALOGW("%s: audio refcount is broken.", __FUNCTION__); + return; + } + + sp batteryService = getBatteryService_l(); + + mAudioRefCount--; + if (mAudioRefCount == 0 && batteryService != nullptr) { + batteryService->noteStopAudio(AID_MEDIA); + } +} + +void BatteryNotifier::noteResetAudio() { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + mAudioRefCount = 0; + if (batteryService != nullptr) { + batteryService->noteResetAudio(); + } +} + +void BatteryNotifier::noteFlashlightOn(const String8& id, int uid) { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + + std::pair k = std::make_pair(id, uid); + if (!mFlashlightState[k]) { + mFlashlightState[k] = true; + if (batteryService != nullptr) { + batteryService->noteFlashlightOn(uid); + } + } +} + +void BatteryNotifier::noteFlashlightOff(const String8& id, int uid) { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + + std::pair k = std::make_pair(id, uid); + if (mFlashlightState[k]) { + mFlashlightState[k] = false; + if (batteryService != nullptr) { + batteryService->noteFlashlightOff(uid); + } + } +} + +void BatteryNotifier::noteResetFlashlight() { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + mFlashlightState.clear(); + if (batteryService != nullptr) { + batteryService->noteResetFlashlight(); + } +} + +void BatteryNotifier::noteStartCamera(const String8& id, int uid) { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + std::pair k = std::make_pair(id, uid); + if (!mCameraState[k]) { + mCameraState[k] = true; + if (batteryService != nullptr) { + batteryService->noteStartCamera(uid); + } + } +} + +void BatteryNotifier::noteStopCamera(const String8& id, int uid) { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + std::pair k = std::make_pair(id, uid); + if (mCameraState[k]) { + mCameraState[k] = false; + if (batteryService != nullptr) { + batteryService->noteStopCamera(uid); + } + } +} + +void BatteryNotifier::noteResetCamera() { + Mutex::Autolock _l(mLock); + sp batteryService = getBatteryService_l(); + mCameraState.clear(); + if (batteryService != nullptr) { + batteryService->noteResetCamera(); + } +} + +void BatteryNotifier::onBatteryStatServiceDied() { + Mutex::Autolock _l(mLock); + mBatteryStatService.clear(); + mDeathNotifier.clear(); + // Do not reset mVideoRefCount and mAudioRefCount here. The ref + // counting is independent of the battery service availability. + // We need this if battery service becomes available after media + // started. + +} + +sp BatteryNotifier::getBatteryService_l() { + if (mBatteryStatService != nullptr) { + return mBatteryStatService; + } + // Get battery service from service manager + const sp sm(defaultServiceManager()); + if (sm != nullptr) { + const String16 name("batterystats"); + mBatteryStatService = interface_cast(sm->checkService(name)); + if (mBatteryStatService == nullptr) { + ALOGE("batterystats service unavailable!"); + return nullptr; + } + + mDeathNotifier = new DeathNotifier(); + IInterface::asBinder(mBatteryStatService)->linkToDeath(mDeathNotifier); + + // Notify start now if media already started + if (mVideoRefCount > 0) { + mBatteryStatService->noteStartVideo(AID_MEDIA); + } + if (mAudioRefCount > 0) { + mBatteryStatService->noteStartAudio(AID_MEDIA); + } + } + return mBatteryStatService; +} + +ANDROID_SINGLETON_STATIC_INSTANCE(BatteryNotifier); + +} // namespace android diff --git a/media/utils/README b/media/utils/README new file mode 100644 index 0000000..65ab0b8 --- /dev/null +++ b/media/utils/README @@ -0,0 +1,4 @@ +This is a common shared library for media utility classes. + +Consider adding your utility class/function here if it will +be used across several of the media libraries. diff --git a/media/utils/include/mediautils/BatteryNotifier.h b/media/utils/include/mediautils/BatteryNotifier.h new file mode 100644 index 0000000..4904804 --- /dev/null +++ b/media/utils/include/mediautils/BatteryNotifier.h @@ -0,0 +1,73 @@ +/* + * Copyright 2015, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MEDIA_BATTERY_NOTIFIER_H +#define MEDIA_BATTERY_NOTIFIER_H + +#include +#include +#include + +#include +#include + +namespace android { + +/** + * Class used for logging battery life events in mediaserver. + */ +class BatteryNotifier : public Singleton { + + friend class Singleton; + BatteryNotifier(); + +public: + ~BatteryNotifier(); + + void noteStartVideo(); + void noteStopVideo(); + void noteResetVideo(); + void noteStartAudio(); + void noteStopAudio(); + void noteResetAudio(); + void noteFlashlightOn(const String8& id, int uid); + void noteFlashlightOff(const String8& id, int uid); + void noteResetFlashlight(); + void noteStartCamera(const String8& id, int uid); + void noteStopCamera(const String8& id, int uid); + void noteResetCamera(); + +private: + void onBatteryStatServiceDied(); + + class DeathNotifier : public IBinder::DeathRecipient { + virtual void binderDied(const wp& /*who*/); + }; + + Mutex mLock; + int mVideoRefCount; + int mAudioRefCount; + std::map, bool> mFlashlightState; + std::map, bool> mCameraState; + sp mBatteryStatService; + sp mDeathNotifier; + + sp getBatteryService_l(); +}; + +} // namespace android + +#endif // MEDIA_BATTERY_NOTIFIER_H -- cgit v1.1