diff options
author | Ruben Brunk <rubenbrunk@google.com> | 2015-05-26 17:25:07 -0700 |
---|---|---|
committer | Ruben Brunk <rubenbrunk@google.com> | 2015-06-01 16:53:45 -0700 |
commit | 99e69716215cd0665379bc90d708f2ea8689831d (patch) | |
tree | d013b8e9273ea6032ddf24ba701eccb755b672bb /media | |
parent | 4a95e69406aa2e9896d865962d6d947ebbdac6fc (diff) | |
download | frameworks_av-99e69716215cd0665379bc90d708f2ea8689831d.zip frameworks_av-99e69716215cd0665379bc90d708f2ea8689831d.tar.gz frameworks_av-99e69716215cd0665379bc90d708f2ea8689831d.tar.bz2 |
Track camera and flashlight usage in battery stats.
Bug: 15986092
Change-Id: I9dc6828332e4091fd93bf2d82839e8e3862a2fc2
Diffstat (limited to 'media')
-rw-r--r-- | media/libmediaplayerservice/Android.mk | 1 | ||||
-rw-r--r-- | media/libmediaplayerservice/MediaPlayerService.cpp | 16 | ||||
-rw-r--r-- | media/libstagefright/Android.mk | 1 | ||||
-rw-r--r-- | media/libstagefright/MediaCodec.cpp | 130 | ||||
-rw-r--r-- | media/utils/Android.mk | 39 | ||||
-rw-r--r-- | media/utils/BatteryNotifier.cpp | 213 | ||||
-rw-r--r-- | media/utils/README | 4 | ||||
-rw-r--r-- | media/utils/include/mediautils/BatteryNotifier.h | 73 |
8 files changed, 336 insertions, 141 deletions
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 <utils/misc.h> -#include <binder/IBatteryStats.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #include <binder/MemoryHeapBase.h> @@ -60,6 +59,7 @@ #include <media/stagefright/AudioPlayer.h> #include <media/stagefright/foundation/ADebug.h> #include <media/stagefright/foundation/ALooperRoster.h> +#include <mediautils/BatteryNotifier.h> #include <system/audio.h> @@ -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<IServiceManager> sm(defaultServiceManager()); - if (sm != NULL) { - const String16 name("batterystats"); - // use checkService() to avoid blocking if service is not up yet - sp<IBatteryStats> batteryStats = - interface_cast<IBatteryStats>(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 <binder/IBatteryStats.h> #include <binder/IMemory.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> @@ -47,6 +46,7 @@ #include <media/stagefright/OMXCodec.h> #include <media/stagefright/PersistentSurface.h> #include <media/stagefright/SurfaceUtils.h> +#include <mediautils/BatteryNotifier.h> #include <private/android_filesystem_config.h> #include <utils/Log.h> #include <utils/Singleton.h> @@ -107,134 +107,6 @@ private: DISALLOW_EVIL_CONSTRUCTORS(ResourceManagerClient); }; -struct MediaCodec::BatteryNotifier : public Singleton<BatteryNotifier> { - 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<IBinder>& /*who*/) { - BatteryNotifier::getInstance().onBatteryStatServiceDied(); - } - }; - - Mutex mLock; - int32_t mVideoRefCount; - int32_t mAudioRefCount; - sp<IBatteryStats> mBatteryStatService; - sp<DeathNotifier> mDeathNotifier; - - sp<IBatteryStats> getBatteryService_l(); - - DISALLOW_EVIL_CONSTRUCTORS(BatteryNotifier); -}; - -ANDROID_SINGLETON_STATIC_INSTANCE(MediaCodec::BatteryNotifier) - -MediaCodec::BatteryNotifier::BatteryNotifier() : - mVideoRefCount(0), - mAudioRefCount(0) { -} - -sp<IBatteryStats> MediaCodec::BatteryNotifier::getBatteryService_l() { - if (mBatteryStatService != NULL) { - return mBatteryStatService; - } - // get battery service from service manager - const sp<IServiceManager> sm(defaultServiceManager()); - if (sm != NULL) { - const String16 name("batterystats"); - mBatteryStatService = - interface_cast<IBatteryStats>(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<IBatteryStats> 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<IBatteryStats> batteryService = getBatteryService_l(); - - mVideoRefCount--; - if (mVideoRefCount == 0 && batteryService != NULL) { - batteryService->noteStopVideo(AID_MEDIA); - } -} - -void MediaCodec::BatteryNotifier::noteStartAudio() { - Mutex::Autolock _l(mLock); - sp<IBatteryStats> 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<IBatteryStats> 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 <binder/IServiceManager.h> +#include <utils/Log.h> +#include <private/android_filesystem_config.h> + +namespace android { + +void BatteryNotifier::DeathNotifier::binderDied(const wp<IBinder>& /*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<IBatteryStats> 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<IBatteryStats> batteryService = getBatteryService_l(); + + mVideoRefCount--; + if (mVideoRefCount == 0 && batteryService != nullptr) { + batteryService->noteStopVideo(AID_MEDIA); + } +} + +void BatteryNotifier::noteResetVideo() { + Mutex::Autolock _l(mLock); + sp<IBatteryStats> batteryService = getBatteryService_l(); + mVideoRefCount = 0; + if (batteryService != nullptr) { + batteryService->noteResetAudio(); + } +} + +void BatteryNotifier::noteStartAudio() { + Mutex::Autolock _l(mLock); + sp<IBatteryStats> 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<IBatteryStats> batteryService = getBatteryService_l(); + + mAudioRefCount--; + if (mAudioRefCount == 0 && batteryService != nullptr) { + batteryService->noteStopAudio(AID_MEDIA); + } +} + +void BatteryNotifier::noteResetAudio() { + Mutex::Autolock _l(mLock); + sp<IBatteryStats> batteryService = getBatteryService_l(); + mAudioRefCount = 0; + if (batteryService != nullptr) { + batteryService->noteResetAudio(); + } +} + +void BatteryNotifier::noteFlashlightOn(const String8& id, int uid) { + Mutex::Autolock _l(mLock); + sp<IBatteryStats> batteryService = getBatteryService_l(); + + std::pair<String8, int> 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<IBatteryStats> batteryService = getBatteryService_l(); + + std::pair<String8, int> 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<IBatteryStats> batteryService = getBatteryService_l(); + mFlashlightState.clear(); + if (batteryService != nullptr) { + batteryService->noteResetFlashlight(); + } +} + +void BatteryNotifier::noteStartCamera(const String8& id, int uid) { + Mutex::Autolock _l(mLock); + sp<IBatteryStats> batteryService = getBatteryService_l(); + std::pair<String8, int> 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<IBatteryStats> batteryService = getBatteryService_l(); + std::pair<String8, int> 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<IBatteryStats> 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<IBatteryStats> BatteryNotifier::getBatteryService_l() { + if (mBatteryStatService != nullptr) { + return mBatteryStatService; + } + // Get battery service from service manager + const sp<IServiceManager> sm(defaultServiceManager()); + if (sm != nullptr) { + const String16 name("batterystats"); + mBatteryStatService = interface_cast<IBatteryStats>(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 <binder/IBatteryStats.h> +#include <utils/Singleton.h> +#include <utils/String8.h> + +#include <map> +#include <utility> + +namespace android { + +/** + * Class used for logging battery life events in mediaserver. + */ +class BatteryNotifier : public Singleton<BatteryNotifier> { + + friend class Singleton<BatteryNotifier>; + 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<IBinder>& /*who*/); + }; + + Mutex mLock; + int mVideoRefCount; + int mAudioRefCount; + std::map<std::pair<String8, int>, bool> mFlashlightState; + std::map<std::pair<String8, int>, bool> mCameraState; + sp<IBatteryStats> mBatteryStatService; + sp<DeathNotifier> mDeathNotifier; + + sp<IBatteryStats> getBatteryService_l(); +}; + +} // namespace android + +#endif // MEDIA_BATTERY_NOTIFIER_H |