1 /*
2 * Copyright (C) 2022 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 <cinttypes>
20
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/platform/log.h"
23 #include "chre/platform/shared/pal_system_api.h"
24 #include "chre/util/macros.h"
25 #include "chre_api/chre/audio.h"
26
27 namespace chre {
28
29 const chrePalAudioCallbacks PlatformAudioBase::sCallbacks = {
30 .audioDataEventCallback = PlatformAudioBase::audioDataEventCallback,
31 .audioAvailabilityCallback = PlatformAudioBase::audioAvailabilityCallback,
32 };
33
PlatformAudio()34 PlatformAudio::PlatformAudio() {}
35
~PlatformAudio()36 PlatformAudio::~PlatformAudio() {
37 if (mApi != nullptr) {
38 LOGD("Platform audio closing");
39 prePalApiCall(PalType::AUDIO);
40 mApi->close();
41 LOGD("Platform audio closed");
42 }
43 }
44
init()45 void PlatformAudio::init() {
46 prePalApiCall(PalType::AUDIO);
47 mApi = chrePalAudioGetApi(CHRE_PAL_AUDIO_API_CURRENT_VERSION);
48 if (mApi != nullptr) {
49 if (!mApi->open(&gChrePalSystemApi, &sCallbacks)) {
50 LOGE("Audio PAL open returned false");
51
52 #ifdef CHRE_TELEMETRY_SUPPORT_ENABLED
53 EventLoopManagerSingleton::get()->getTelemetryManager().onPalOpenFailure(
54 TelemetryManager::PalType::AUDIO);
55 #endif // CHRE_TELEMETRY_SUPPORT_ENABLED
56
57 mApi = nullptr;
58 } else {
59 LOGD("Opened audio PAL version 0x%08" PRIx32, mApi->moduleVersion);
60 }
61 } else {
62 LOGW("Requested audio PAL (version 0x%08" PRIx32 ") not found",
63 CHRE_PAL_AUDIO_API_CURRENT_VERSION);
64 }
65 }
66
setHandleEnabled(uint32_t handle,bool enabled)67 void PlatformAudio::setHandleEnabled(uint32_t handle, bool enabled) {
68 UNUSED_VAR(handle);
69 UNUSED_VAR(enabled);
70 }
71
requestAudioDataEvent(uint32_t handle,uint32_t numSamples,Nanoseconds eventDelay)72 bool PlatformAudio::requestAudioDataEvent(uint32_t handle, uint32_t numSamples,
73 Nanoseconds eventDelay) {
74 if (mApi != nullptr) {
75 prePalApiCall(PalType::AUDIO);
76 return mApi->requestAudioDataEvent(handle, numSamples,
77 eventDelay.toRawNanoseconds());
78 }
79
80 return false;
81 }
82
cancelAudioDataEventRequest(uint32_t handle)83 void PlatformAudio::cancelAudioDataEventRequest(uint32_t handle) {
84 if (mApi != nullptr) {
85 prePalApiCall(PalType::AUDIO);
86 mApi->cancelAudioDataEvent(handle);
87 }
88 }
89
releaseAudioDataEvent(struct chreAudioDataEvent * event)90 void PlatformAudio::releaseAudioDataEvent(struct chreAudioDataEvent *event) {
91 if (mApi != nullptr) {
92 prePalApiCall(PalType::AUDIO);
93 mApi->releaseAudioDataEvent(event);
94 }
95 }
96
getSourceCount()97 size_t PlatformAudio::getSourceCount() {
98 if (mApi != nullptr) {
99 prePalApiCall(PalType::AUDIO);
100 return static_cast<size_t>(mApi->getSourceCount());
101 }
102
103 return 0;
104 }
105
getAudioSource(uint32_t handle,chreAudioSource * audioSource) const106 bool PlatformAudio::getAudioSource(uint32_t handle,
107 chreAudioSource *audioSource) const {
108 if (mApi != nullptr) {
109 prePalApiCall(PalType::AUDIO);
110 return mApi->getAudioSource(handle, audioSource);
111 }
112
113 return false;
114 }
115
audioDataEventCallback(struct chreAudioDataEvent * event)116 void PlatformAudioBase::audioDataEventCallback(
117 struct chreAudioDataEvent *event) {
118 EventLoopManagerSingleton::get()
119 ->getAudioRequestManager()
120 .handleAudioDataEvent(event);
121 }
122
audioAvailabilityCallback(uint32_t handle,bool available)123 void PlatformAudioBase::audioAvailabilityCallback(uint32_t handle,
124 bool available) {
125 EventLoopManagerSingleton::get()
126 ->getAudioRequestManager()
127 .handleAudioAvailability(handle, available);
128 }
129
130 } // namespace chre