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

Commit a61fbdb2 authored by Weiyin Jiang's avatar Weiyin Jiang Committed by Steve Kondik
Browse files

nuplayer: acquire wakelock during offload pause timeout

Nuplayer utilizes AMessage to post pause timeout message. Unlike timed
event queue used by awesome player, AMessage won't hold wakelock, which
results in tear down doesn't happen after kOffloadPauseMaxUs since ARM
cores are sleeping. As a result, audio output driver won't be closed
which blocks XO shutdown.

Implement wakelock request in Nuplayer render.

CRs-Fixed: 785828
Change-Id: I3d11fef633e3f2783f4aa31e71285e2fa09d234c
parent 4af8125f
Loading
Loading
Loading
Loading
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015, The Linux Foundation. All rights reserved.
 * Not a Contribution.
 * Copyright (C) 2010 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 A_WAKELOCK_H_
#define A_WAKELOCK_H_

#include <media/stagefright/foundation/ABase.h>
#include <powermanager/IPowerManager.h>
#include <utils/RefBase.h>

namespace android {

class AWakeLock : public RefBase {

public:
    AWakeLock();

    bool acquire();
    void release(bool force = false);

    virtual ~AWakeLock();

private:
    sp<IPowerManager>          mPowerManager;
    sp<IBinder>                mWakeLockToken;
    uint32_t                   mWakeLockCount;

    class PMDeathRecipient : public IBinder::DeathRecipient {
    public:
        PMDeathRecipient(AWakeLock *wakeLock) : mWakeLock(wakeLock) {}
        virtual ~PMDeathRecipient() {}

        // IBinder::DeathRecipient
        virtual void binderDied(const wp<IBinder> &who);

    private:
        PMDeathRecipient(const PMDeathRecipient&);
        PMDeathRecipient& operator=(const PMDeathRecipient&);

        AWakeLock *mWakeLock;
    };

    const sp<PMDeathRecipient> mDeathRecipient;

    void clearPowerManager();

    DISALLOW_EVIL_CONSTRUCTORS(AWakeLock);
};


}  // namespace android

#endif  // A_WAKELOCK_H_
+9 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/foundation/AUtils.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/foundation/AWakeLock.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/Utils.h>
@@ -91,7 +92,8 @@ NuPlayer::Renderer::Renderer(
      mAudioOffloadTornDown(false),
      mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
      mTotalBuffersQueued(0),
      mLastAudioBufferDrained(0) {
      mLastAudioBufferDrained(0),
      mWakeLock(new AWakeLock()) {

    readProperties();
    notify->findObject(MEDIA_EXTENDED_STATS, (sp<RefBase>*)&mPlayerExtendedStats);
@@ -483,6 +485,7 @@ void NuPlayer::Renderer::onMessageReceived(const sp<AMessage> &msg) {
            }
            ALOGV("Audio Offload tear down due to pause timeout.");
            onAudioOffloadTearDown(kDueToTimeout);
            mWakeLock->release();
            break;
        }

@@ -1378,6 +1381,10 @@ void NuPlayer::Renderer::onAudioOffloadTearDown(AudioOffloadTearDownReason reaso

void NuPlayer::Renderer::startAudioOffloadPauseTimeout() {
    if (offloadingAudio()) {
        bool granted = mWakeLock->acquire();
        if (!granted) {
            ALOGW("fail to acquire wake lock");
        }
        sp<AMessage> msg = new AMessage(kWhatAudioOffloadPauseTimeout, id());
        msg->setInt32("generation", mAudioOffloadPauseTimeoutGeneration);
        msg->post(kOffloadPauseMaxUs);
@@ -1386,6 +1393,7 @@ void NuPlayer::Renderer::startAudioOffloadPauseTimeout() {

void NuPlayer::Renderer::cancelAudioOffloadPauseTimeout() {
    if (offloadingAudio()) {
        mWakeLock->release(true);
        ++mAudioOffloadPauseTimeoutGeneration;
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
namespace android {

struct ABuffer;
class  AWakeLock;
struct VideoFrameScheduler;

struct NuPlayer::Renderer : public AHandler {
@@ -181,7 +182,7 @@ private:

    int32_t mTotalBuffersQueued;
    int32_t mLastAudioBufferDrained;

    sp<AWakeLock> mWakeLock;

    size_t fillAudioBuffer(void *buffer, size_t size);

+118 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015, The Linux Foundation. All rights reserved.
 * Not a Contribution.
 * Copyright (C) 2010 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.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "AWakeLock"
#include <utils/Log.h>

#include "ADebug.h"
#include "AWakeLock.h"

#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <powermanager/PowerManager.h>


namespace android {

AWakeLock::AWakeLock() :
    mPowerManager(NULL),
    mWakeLockToken(NULL),
    mWakeLockCount(0),
    mDeathRecipient(new PMDeathRecipient(this)) {}

AWakeLock::~AWakeLock() {
    if (mPowerManager != NULL) {
        sp<IBinder> binder = mPowerManager->asBinder();
        if (binder != NULL) {
            binder->unlinkToDeath(mDeathRecipient);
        }
    }
    clearPowerManager();
}

bool AWakeLock::acquire() {
    bool granted = false;

    if (mWakeLockCount == 0) {
        CHECK(mWakeLockToken == NULL);
        if (mPowerManager == NULL) {
            // use checkService() to avoid blocking if power service is not up yet
            sp<IBinder> binder =
                defaultServiceManager()->checkService(String16("power"));
            if (binder == NULL) {
                ALOGW("cannot connect to the power manager service");
            } else {
                mPowerManager = interface_cast<IPowerManager>(binder);
                binder->linkToDeath(mDeathRecipient);
            }
        }
        if (mPowerManager != NULL) {
            sp<IBinder> binder = new BBinder();
            int64_t token = IPCThreadState::self()->clearCallingIdentity();
            status_t status = mPowerManager->acquireWakeLock(
                    POWERMANAGER_PARTIAL_WAKE_LOCK,
                    binder,
                    String16("AWakeLock"),
                    String16("media"));    // not oneway
            IPCThreadState::self()->restoreCallingIdentity(token);
            if (status == NO_ERROR) {
                mWakeLockToken = binder;
                mWakeLockCount++;
                granted = true;
            }
        }
    } else {
        mWakeLockCount++;
        granted = true;
    }

    return granted;
}

void AWakeLock::release(bool force) {
    if (mWakeLockCount == 0) {
        return;
    }
    if (force) {
        // Force wakelock release below by setting reference count to 1.
        mWakeLockCount = 1;
    }
    if (--mWakeLockCount == 0) {
        CHECK(mWakeLockToken != NULL);
        if (mPowerManager != NULL) {
            int64_t token = IPCThreadState::self()->clearCallingIdentity();
            mPowerManager->releaseWakeLock(mWakeLockToken, 0 /* flags */);  // not oneway
            IPCThreadState::self()->restoreCallingIdentity(token);
        }
        mWakeLockToken.clear();
    }
}

void AWakeLock::clearPowerManager() {
    release(true);
    mPowerManager.clear();
}

void AWakeLock::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) {
    if(mWakeLock != NULL) {
        mWakeLock->clearPowerManager();
    }
}

}  // namespace android
+3 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ LOCAL_SRC_FILES:= \
    AMessage.cpp                  \
    ANetworkSession.cpp           \
    AString.cpp                   \
    AWakeLock.cpp                 \
    ParsedMessage.cpp             \
    base64.cpp                    \
    hexdump.cpp
@@ -22,7 +23,8 @@ LOCAL_C_INCLUDES:= \
LOCAL_SHARED_LIBRARIES := \
        libbinder         \
        libutils          \
        liblog
        liblog            \
        libpowermanager

LOCAL_CFLAGS += -Wno-multichar -Werror