• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       mApi = nullptr;
52     } else {
53       LOGD("Opened audio PAL version 0x%08" PRIx32, mApi->moduleVersion);
54     }
55   } else {
56     LOGW("Requested audio PAL (version 0x%08" PRIx32 ") not found",
57          CHRE_PAL_AUDIO_API_CURRENT_VERSION);
58   }
59 }
60 
setHandleEnabled(uint32_t handle,bool enabled)61 void PlatformAudio::setHandleEnabled(uint32_t handle, bool enabled) {
62   UNUSED_VAR(handle);
63   UNUSED_VAR(enabled);
64 }
65 
requestAudioDataEvent(uint32_t handle,uint32_t numSamples,Nanoseconds eventDelay)66 bool PlatformAudio::requestAudioDataEvent(uint32_t handle, uint32_t numSamples,
67                                           Nanoseconds eventDelay) {
68   if (mApi != nullptr) {
69     prePalApiCall(PalType::AUDIO);
70     return mApi->requestAudioDataEvent(handle, numSamples,
71                                        eventDelay.toRawNanoseconds());
72   }
73 
74   return false;
75 }
76 
cancelAudioDataEventRequest(uint32_t handle)77 void PlatformAudio::cancelAudioDataEventRequest(uint32_t handle) {
78   if (mApi != nullptr) {
79     prePalApiCall(PalType::AUDIO);
80     mApi->cancelAudioDataEvent(handle);
81   }
82 }
83 
releaseAudioDataEvent(struct chreAudioDataEvent * event)84 void PlatformAudio::releaseAudioDataEvent(struct chreAudioDataEvent *event) {
85   if (mApi != nullptr) {
86     prePalApiCall(PalType::AUDIO);
87     mApi->releaseAudioDataEvent(event);
88   }
89 }
90 
getSourceCount()91 size_t PlatformAudio::getSourceCount() {
92   if (mApi != nullptr) {
93     prePalApiCall(PalType::AUDIO);
94     return static_cast<size_t>(mApi->getSourceCount());
95   }
96 
97   return 0;
98 }
99 
getAudioSource(uint32_t handle,chreAudioSource * audioSource) const100 bool PlatformAudio::getAudioSource(uint32_t handle,
101                                    chreAudioSource *audioSource) const {
102   if (mApi != nullptr) {
103     prePalApiCall(PalType::AUDIO);
104     return mApi->getAudioSource(handle, audioSource);
105   }
106 
107   return false;
108 }
109 
audioDataEventCallback(struct chreAudioDataEvent * event)110 void PlatformAudioBase::audioDataEventCallback(
111     struct chreAudioDataEvent *event) {
112   EventLoopManagerSingleton::get()
113       ->getAudioRequestManager()
114       .handleAudioDataEvent(event);
115 }
116 
audioAvailabilityCallback(uint32_t handle,bool available)117 void PlatformAudioBase::audioAvailabilityCallback(uint32_t handle,
118                                                   bool available) {
119   EventLoopManagerSingleton::get()
120       ->getAudioRequestManager()
121       .handleAudioAvailability(handle, available);
122 }
123 
124 }  // namespace chre