summaryrefslogtreecommitdiffstats
path: root/hal/audio_perf.cpp
blob: 671a324eb0a3e3308e0ed0514940d38f37e36ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2017 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_TAG "audio_hw_primary"

#include <cinttypes>

#include <utils/Log.h>
#include <utils/Mutex.h>

#include <android/hardware/power/1.2/IPower.h>

#include "audio_perf.h"

using android::hardware::power::V1_2::IPower;
using android::hardware::power::V1_2::PowerHint;
using android::hardware::power::V1_2::toString;
using android::hardware::Return;
using android::hardware::Void;
using android::hardware::hidl_death_recipient;
using android::hidl::base::V1_0::IBase;

// Do not use gPowerHAL, use getPowerHal to retrieve a copy instead
static android::sp<IPower> gPowerHal_ = nullptr;
// Protect gPowerHal_
static std::mutex gPowerHalMutex;

// PowerHalDeathRecipient to invalid the client when service dies
struct PowerHalDeathRecipient : virtual public hidl_death_recipient {
    // hidl_death_recipient interface
    virtual void serviceDied(uint64_t, const android::wp<IBase>&) override {
        std::lock_guard<std::mutex> lock(gPowerHalMutex);
        ALOGE("PowerHAL just died");
        gPowerHal_ = nullptr;
    }
};

// Retrieve a copy of client
static android::sp<IPower> getPowerHal() {
    std::lock_guard<std::mutex> lock(gPowerHalMutex);
    static android::sp<PowerHalDeathRecipient> gPowerHalDeathRecipient = nullptr;
    static bool gPowerHalExists = true;

    if (gPowerHalExists && gPowerHal_ == nullptr) {
        gPowerHal_ = IPower::getService();

        if (gPowerHal_ == nullptr) {
            ALOGE("Unable to get Power service");
            gPowerHalExists = false;
        } else {
            if (gPowerHalDeathRecipient == nullptr) {
                gPowerHalDeathRecipient = new PowerHalDeathRecipient();
            }
            Return<bool> linked = gPowerHal_->linkToDeath(
                gPowerHalDeathRecipient, 0 /* cookie */);
            if (!linked.isOk()) {
                ALOGE("Transaction error in linking to PowerHAL death: %s",
                      linked.description().c_str());
                gPowerHal_ = nullptr;
            } else if (!linked) {
                ALOGW("Unable to link to PowerHal death notifications");
                gPowerHal_ = nullptr;
            } else {
                ALOGD("Connect to PowerHAL and link to death "
                      "notification successfully");
            }
        }
    }
    return gPowerHal_;
}

static bool powerHint(PowerHint hint, int32_t data) {
    android::sp<IPower> powerHal = getPowerHal();
    if (powerHal == nullptr) {
        return false;
    }

    auto ret = powerHal->powerHintAsync_1_2(hint, data);

    if (!ret.isOk()) {
        ALOGE("powerHint failed, hint: %s, data: %" PRId32 ",  error: %s",
              toString(hint).c_str(),
              data,
              ret.description().c_str());
    }
    return ret.isOk();
}

int audio_streaming_hint_start() {
    return powerHint(PowerHint::AUDIO_STREAMING, 1);
}

int audio_streaming_hint_end() {
    return powerHint(PowerHint::AUDIO_STREAMING, 0);
}

int audio_low_latency_hint_start() {
    return powerHint(PowerHint::AUDIO_LOW_LATENCY, 1);
}

int audio_low_latency_hint_end() {
    return powerHint(PowerHint::AUDIO_LOW_LATENCY, 0);
}