Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 99e69716 authored by Ruben Brunk's avatar Ruben Brunk
Browse files

Track camera and flashlight usage in battery stats.

Bug: 15986092

Change-Id: I9dc6828332e4091fd93bf2d82839e8e3862a2fc2
parent 4a95e694
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -60,8 +60,6 @@ struct MediaCodec : public AHandler {
        CB_RESOURCE_RECLAIMED = 5,
    };

    struct BatteryNotifier;

    static sp<MediaCodec> CreateByType(
            const sp<ALooper> &looper, const char *mime, bool encoder, status_t *err = NULL);

+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ LOCAL_SHARED_LIBRARIES := \
    libdl                       \
    libgui                      \
    libmedia                    \
    libmediautils               \
    libsonivox                  \
    libstagefright              \
    libstagefright_foundation   \
+4 −12
Original line number Diff line number Diff line
@@ -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();
}
+1 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ LOCAL_SHARED_LIBRARIES := \
        libicuuc \
        liblog \
        libmedia \
        libmediautils \
        libnetd_client \
        libopus \
        libsonivox \
+1 −129
Original line number Diff line number Diff line
@@ -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() {
}

Loading