1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "chre/platform/platform_audio.h"
18
19 #include <cstring>
20
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/platform/host_link.h"
23 #include "chre/platform/log.h"
24 #include "chre/platform/shared/pal_system_api.h"
25 #include "chre/platform/slpi/power_control_util.h"
26
27 namespace chre {
28 namespace {
29
handleAudioDataEvent(struct chreAudioDataEvent * event)30 void handleAudioDataEvent(struct chreAudioDataEvent *event) {
31 EventLoopManagerSingleton::get()
32 ->getAudioRequestManager()
33 .handleAudioDataEvent(event);
34 }
35
handleAudioAvailability(uint32_t handle,bool available)36 void handleAudioAvailability(uint32_t handle, bool available) {
37 LOGD("SPI audio handle %" PRIu32 " available: %d", handle, available);
38 EventLoopManagerSingleton::get()
39 ->getAudioRequestManager()
40 .handleAudioAvailability(handle, available);
41 }
42
43 } // anonymous namespace
44
45 const chrePalAudioCallbacks PlatformAudioBase::sAudioCallbacks = {
46 handleAudioDataEvent,
47 handleAudioAvailability,
48 };
49
PlatformAudio()50 PlatformAudio::PlatformAudio() {}
51
~PlatformAudio()52 PlatformAudio::~PlatformAudio() {
53 if (mAudioApi != nullptr) {
54 LOGV("Platform audio closing");
55 mAudioApi->close();
56 LOGV("Platform audio closed");
57 }
58 }
59
init()60 void PlatformAudio::init() {
61 mAudioApi = chrePalAudioGetApi(CHRE_PAL_AUDIO_API_CURRENT_VERSION);
62 if (mAudioApi != nullptr) {
63 if (!mAudioApi->open(&gChrePalSystemApi, &sAudioCallbacks)) {
64 LOGD("Audio PAL open returned false");
65 mAudioApi = nullptr;
66 } else {
67 LOGD("Opened audio PAL version 0x%08" PRIx32, mAudioApi->moduleVersion);
68 }
69 } else {
70 LOGW("Requested audio PAL (version 0x%08" PRIx32 ") not found",
71 CHRE_PAL_AUDIO_API_CURRENT_VERSION);
72 }
73 }
74
setHandleEnabled(uint32_t handle,bool enabled)75 void PlatformAudio::setHandleEnabled(uint32_t handle, bool enabled) {
76 uint32_t lastNumAudioClients = mNumAudioClients;
77
78 if (enabled) {
79 mNumAudioClients++;
80 } else if (mNumAudioClients > 0) {
81 mNumAudioClients--;
82 } else {
83 LOGE("Invalid request to change handle enabled state");
84 }
85
86 if (lastNumAudioClients == 0 && mNumAudioClients > 0) {
87 mTargetAudioEnabled = true;
88 if (!mCurrentAudioEnabled) {
89 LOGD("Enabling audio");
90 mCurrentAudioEnabled = true;
91 sendAudioRequest();
92 }
93 } else if (lastNumAudioClients > 0 && mNumAudioClients == 0) {
94 mTargetAudioEnabled = false;
95 if (EventLoopManagerSingleton::get()
96 ->getEventLoop()
97 .getPowerControlManager()
98 .hostIsAwake()) {
99 onHostAwake();
100 } else {
101 LOGD("Deferring disable audio");
102 }
103 }
104 }
105
requestAudioDataEvent(uint32_t handle,uint32_t numSamples,Nanoseconds eventDelay)106 bool PlatformAudio::requestAudioDataEvent(uint32_t handle, uint32_t numSamples,
107 Nanoseconds eventDelay) {
108 bool success = false;
109 if (mAudioApi != nullptr) {
110 success = mAudioApi->requestAudioDataEvent(handle, numSamples,
111 eventDelay.toRawNanoseconds());
112 }
113
114 return success;
115 }
116
cancelAudioDataEventRequest(uint32_t handle)117 void PlatformAudio::cancelAudioDataEventRequest(uint32_t handle) {
118 if (mAudioApi != nullptr) {
119 mAudioApi->cancelAudioDataEvent(handle);
120 }
121 }
122
releaseAudioDataEvent(struct chreAudioDataEvent * event)123 void PlatformAudio::releaseAudioDataEvent(struct chreAudioDataEvent *event) {
124 if (mAudioApi != nullptr) {
125 mAudioApi->releaseAudioDataEvent(event);
126 }
127 }
128
getSourceCount()129 size_t PlatformAudio::getSourceCount() {
130 size_t sourceCount = 0;
131 if (mAudioApi != nullptr) {
132 sourceCount = mAudioApi->getSourceCount();
133 }
134
135 return sourceCount;
136 }
137
getAudioSource(uint32_t handle,struct chreAudioSource * source) const138 bool PlatformAudio::getAudioSource(uint32_t handle,
139 struct chreAudioSource *source) const {
140 bool success = false;
141 if (mAudioApi != nullptr) {
142 success = mAudioApi->getAudioSource(handle, source);
143 }
144
145 return success;
146 }
147
onHostAwake()148 void PlatformAudioBase::onHostAwake() {
149 if (mCurrentAudioEnabled && !mTargetAudioEnabled) {
150 LOGD("Disabling audio");
151 mCurrentAudioEnabled = mTargetAudioEnabled;
152 sendAudioRelease();
153 }
154 }
155
156 } // namespace chre
157