1 /*
2  * Copyright (C) 2009 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 #define LOG_TAG "AudioPolicyClientImpl"
18 //#define LOG_NDEBUG 0
19 
20 #include "AudioPolicyService.h"
21 
22 #include <utils/Log.h>
23 
24 #include "BinderProxy.h"
25 
26 namespace android {
27 
28 /* implementation of the client interface from the policy manager */
29 
getAudioPolicyConfig(media::AudioPolicyConfig * config)30 status_t AudioPolicyService::AudioPolicyClient::getAudioPolicyConfig(
31         media::AudioPolicyConfig *config)
32 {
33     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
34     if (af == 0) {
35         ALOGW("%s: could not get AudioFlinger", __func__);
36         return AUDIO_MODULE_HANDLE_NONE;
37     }
38 
39     return af->getAudioPolicyConfig(config);
40 }
41 
loadHwModule(const char * name)42 audio_module_handle_t AudioPolicyService::AudioPolicyClient::loadHwModule(const char *name)
43 {
44     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
45     if (af == 0) {
46         ALOGW("%s: could not get AudioFlinger", __func__);
47         return AUDIO_MODULE_HANDLE_NONE;
48     }
49 
50     return af->loadHwModule(name);
51 }
52 
openOutput(audio_module_handle_t module,audio_io_handle_t * output,audio_config_t * halConfig,audio_config_base_t * mixerConfig,const sp<DeviceDescriptorBase> & device,uint32_t * latencyMs,audio_output_flags_t flags)53 status_t AudioPolicyService::AudioPolicyClient::openOutput(audio_module_handle_t module,
54                                                            audio_io_handle_t *output,
55                                                            audio_config_t *halConfig,
56                                                            audio_config_base_t *mixerConfig,
57                                                            const sp<DeviceDescriptorBase>& device,
58                                                            uint32_t *latencyMs,
59                                                            audio_output_flags_t flags)
60 {
61     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
62     if (af == 0) {
63         ALOGW("%s: could not get AudioFlinger", __func__);
64         return PERMISSION_DENIED;
65     }
66 
67     media::OpenOutputRequest request;
68     media::OpenOutputResponse response;
69 
70     request.module = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_module_handle_t_int32_t(module));
71     request.halConfig = VALUE_OR_RETURN_STATUS(
72             legacy2aidl_audio_config_t_AudioConfig(*halConfig, false /*isInput*/));
73     request.mixerConfig = VALUE_OR_RETURN_STATUS(
74             legacy2aidl_audio_config_base_t_AudioConfigBase(*mixerConfig, false /*isInput*/));
75     request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_DeviceDescriptorBase(device));
76     request.flags = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
77 
78     status_t status = af->openOutput(request, &response);
79     if (status == OK) {
80         *output = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.output));
81         *halConfig = VALUE_OR_RETURN_STATUS(
82                 aidl2legacy_AudioConfig_audio_config_t(response.config, false /*isInput*/));
83         *latencyMs = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(response.latencyMs));
84 
85         audio_config_base_t config = {.sample_rate = halConfig->sample_rate,
86             .channel_mask = halConfig->channel_mask,
87             .format = halConfig->format,
88         };
89         mAudioPolicyService->registerOutput(*output, config, flags);
90     }
91     return status;
92 }
93 
openDuplicateOutput(audio_io_handle_t output1,audio_io_handle_t output2)94 audio_io_handle_t AudioPolicyService::AudioPolicyClient::openDuplicateOutput(
95                                                                 audio_io_handle_t output1,
96                                                                 audio_io_handle_t output2)
97 {
98     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
99     if (af == 0) {
100         ALOGW("%s: could not get AudioFlinger", __func__);
101         return 0;
102     }
103     return af->openDuplicateOutput(output1, output2);
104 }
105 
closeOutput(audio_io_handle_t output)106 status_t AudioPolicyService::AudioPolicyClient::closeOutput(audio_io_handle_t output)
107 {
108     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
109     if (af == 0) {
110         return PERMISSION_DENIED;
111     }
112     mAudioPolicyService->unregisterOutput(output);
113     return af->closeOutput(output);
114 }
115 
suspendOutput(audio_io_handle_t output)116 status_t AudioPolicyService::AudioPolicyClient::suspendOutput(audio_io_handle_t output)
117 {
118     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
119     if (af == 0) {
120         ALOGW("%s: could not get AudioFlinger", __func__);
121         return PERMISSION_DENIED;
122     }
123 
124     return af->suspendOutput(output);
125 }
126 
restoreOutput(audio_io_handle_t output)127 status_t AudioPolicyService::AudioPolicyClient::restoreOutput(audio_io_handle_t output)
128 {
129     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
130     if (af == 0) {
131         ALOGW("%s: could not get AudioFlinger", __func__);
132         return PERMISSION_DENIED;
133     }
134 
135     return af->restoreOutput(output);
136 }
137 
openInput(audio_module_handle_t module,audio_io_handle_t * input,audio_config_t * config,audio_devices_t * device,const String8 & address,audio_source_t source,audio_input_flags_t flags)138 status_t AudioPolicyService::AudioPolicyClient::openInput(audio_module_handle_t module,
139                                                           audio_io_handle_t *input,
140                                                           audio_config_t *config,
141                                                           audio_devices_t *device,
142                                                           const String8& address,
143                                                           audio_source_t source,
144                                                           audio_input_flags_t flags)
145 {
146     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
147     if (af == 0) {
148         ALOGW("%s: could not get AudioFlinger", __func__);
149         return PERMISSION_DENIED;
150     }
151 
152     AudioDeviceTypeAddr deviceTypeAddr(*device, address.c_str());
153 
154     media::OpenInputRequest request;
155     request.module = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_module_handle_t_int32_t(module));
156     request.input = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
157     request.config = VALUE_OR_RETURN_STATUS(
158             legacy2aidl_audio_config_t_AudioConfig(*config, true /*isInput*/));
159     request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(deviceTypeAddr));
160     request.source = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_source_t_AudioSource(source));
161     request.flags = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
162 
163     media::OpenInputResponse response;
164     status_t status = af->openInput(request, &response);
165     if (status == OK) {
166         *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_module_handle_t(response.input));
167         *config = VALUE_OR_RETURN_STATUS(
168                 aidl2legacy_AudioConfig_audio_config_t(response.config, true /*isInput*/));
169     }
170     return status;
171 }
172 
closeInput(audio_io_handle_t input)173 status_t AudioPolicyService::AudioPolicyClient::closeInput(audio_io_handle_t input)
174 {
175     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
176     if (af == 0) {
177         return PERMISSION_DENIED;
178     }
179 
180     return af->closeInput(input);
181 }
182 
setStreamVolume(audio_stream_type_t stream,float volume,audio_io_handle_t output,int delay_ms)183 status_t AudioPolicyService::AudioPolicyClient::setStreamVolume(audio_stream_type_t stream,
184                      float volume, audio_io_handle_t output,
185                      int delay_ms)
186 {
187     return mAudioPolicyService->setStreamVolume(stream, volume, output,
188                                                delay_ms);
189 }
190 
setParameters(audio_io_handle_t io_handle,const String8 & keyValuePairs,int delay_ms)191 void AudioPolicyService::AudioPolicyClient::setParameters(audio_io_handle_t io_handle,
192                    const String8& keyValuePairs,
193                    int delay_ms)
194 {
195     mAudioPolicyService->setParameters(io_handle, keyValuePairs.c_str(), delay_ms);
196 }
197 
getParameters(audio_io_handle_t io_handle,const String8 & keys)198 String8 AudioPolicyService::AudioPolicyClient::getParameters(audio_io_handle_t io_handle,
199                       const String8& keys)
200 {
201     String8 result = AudioSystem::getParameters(io_handle, keys);
202     return result;
203 }
204 
setVoiceVolume(float volume,int delay_ms)205 status_t AudioPolicyService::AudioPolicyClient::setVoiceVolume(float volume, int delay_ms)
206 {
207     return mAudioPolicyService->setVoiceVolume(volume, delay_ms);
208 }
209 
moveEffects(audio_session_t session,audio_io_handle_t src_output,audio_io_handle_t dst_output)210 status_t AudioPolicyService::AudioPolicyClient::moveEffects(audio_session_t session,
211                         audio_io_handle_t src_output,
212                         audio_io_handle_t dst_output)
213 {
214     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
215     if (af == 0) {
216         return PERMISSION_DENIED;
217     }
218 
219     return af->moveEffects(session, src_output, dst_output);
220 }
221 
setEffectSuspended(int effectId,audio_session_t sessionId,bool suspended)222 void AudioPolicyService::AudioPolicyClient::setEffectSuspended(int effectId,
223                                 audio_session_t sessionId,
224                                 bool suspended)
225 {
226     mAudioPolicyService->setEffectSuspended(effectId, sessionId, suspended);
227 }
228 
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle,int delayMs)229 status_t AudioPolicyService::AudioPolicyClient::createAudioPatch(const struct audio_patch *patch,
230                                                                   audio_patch_handle_t *handle,
231                                                                   int delayMs)
232 {
233     return mAudioPolicyService->clientCreateAudioPatch(patch, handle, delayMs);
234 }
235 
releaseAudioPatch(audio_patch_handle_t handle,int delayMs)236 status_t AudioPolicyService::AudioPolicyClient::releaseAudioPatch(audio_patch_handle_t handle,
237                                                                   int delayMs)
238 {
239     return mAudioPolicyService->clientReleaseAudioPatch(handle, delayMs);
240 }
241 
setAudioPortConfig(const struct audio_port_config * config,int delayMs)242 status_t AudioPolicyService::AudioPolicyClient::setAudioPortConfig(
243                                                         const struct audio_port_config *config,
244                                                         int delayMs)
245 {
246     return mAudioPolicyService->clientSetAudioPortConfig(config, delayMs);
247 }
248 
onAudioPortListUpdate()249 void AudioPolicyService::AudioPolicyClient::onAudioPortListUpdate()
250 {
251     mAudioPolicyService->onAudioPortListUpdate();
252 }
253 
onAudioPatchListUpdate()254 void AudioPolicyService::AudioPolicyClient::onAudioPatchListUpdate()
255 {
256     mAudioPolicyService->onAudioPatchListUpdate();
257 }
258 
onDynamicPolicyMixStateUpdate(String8 regId,int32_t state)259 void AudioPolicyService::AudioPolicyClient::onDynamicPolicyMixStateUpdate(
260         String8 regId, int32_t state)
261 {
262     mAudioPolicyService->onDynamicPolicyMixStateUpdate(regId, state);
263 }
264 
onRecordingConfigurationUpdate(int event,const record_client_info_t * clientInfo,const audio_config_base_t * clientConfig,std::vector<effect_descriptor_t> clientEffects,const audio_config_base_t * deviceConfig,std::vector<effect_descriptor_t> effects,audio_patch_handle_t patchHandle,audio_source_t source)265 void AudioPolicyService::AudioPolicyClient::onRecordingConfigurationUpdate(
266                                                     int event,
267                                                     const record_client_info_t *clientInfo,
268                                                     const audio_config_base_t *clientConfig,
269                                                     std::vector<effect_descriptor_t> clientEffects,
270                                                     const audio_config_base_t *deviceConfig,
271                                                     std::vector<effect_descriptor_t> effects,
272                                                     audio_patch_handle_t patchHandle,
273                                                     audio_source_t source)
274 {
275     mAudioPolicyService->onRecordingConfigurationUpdate(event, clientInfo,
276             clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
277 }
278 
onAudioVolumeGroupChanged(volume_group_t group,int flags)279 void AudioPolicyService::AudioPolicyClient::onAudioVolumeGroupChanged(volume_group_t group,
280                                                                       int flags)
281 {
282     mAudioPolicyService->onAudioVolumeGroupChanged(group, flags);
283 }
284 
onRoutingUpdated()285 void AudioPolicyService::AudioPolicyClient::onRoutingUpdated()
286 {
287     mAudioPolicyService->onRoutingUpdated();
288 }
289 
onVolumeRangeInitRequest()290 void AudioPolicyService::AudioPolicyClient::onVolumeRangeInitRequest()
291 {
292     mAudioPolicyService->onVolumeRangeInitRequest();
293 }
294 
newAudioUniqueId(audio_unique_id_use_t use)295 audio_unique_id_t AudioPolicyService::AudioPolicyClient::newAudioUniqueId(audio_unique_id_use_t use)
296 {
297     return AudioSystem::newAudioUniqueId(use);
298 }
299 
setSoundTriggerCaptureState(bool active)300 void AudioPolicyService::AudioPolicyClient::setSoundTriggerCaptureState(bool active)
301 {
302     mAudioPolicyService->mCaptureStateNotifier.setCaptureState(active);
303 }
304 
getAudioPort(struct audio_port_v7 * port)305 status_t AudioPolicyService::AudioPolicyClient::getAudioPort(struct audio_port_v7 *port)
306 {
307     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
308     if (af == 0) {
309         ALOGW("%s: could not get AudioFlinger", __func__);
310         return PERMISSION_DENIED;
311     }
312     return af->getAudioPort(port);
313 }
314 
updateSecondaryOutputs(const TrackSecondaryOutputsMap & trackSecondaryOutputs)315 status_t AudioPolicyService::AudioPolicyClient::updateSecondaryOutputs(
316         const TrackSecondaryOutputsMap& trackSecondaryOutputs) {
317     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
318     if (af == nullptr) {
319         ALOGW("%s: could not get AudioFlinger", __func__);
320         return PERMISSION_DENIED;
321     }
322     return af->updateSecondaryOutputs(trackSecondaryOutputs);
323 }
324 
setDeviceConnectedState(const struct audio_port_v7 * port,media::DeviceConnectedState state)325 status_t AudioPolicyService::AudioPolicyClient::setDeviceConnectedState(
326         const struct audio_port_v7 *port, media::DeviceConnectedState state) {
327     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
328     if (af == nullptr) {
329         ALOGW("%s: could not get AudioFlinger", __func__);
330         return PERMISSION_DENIED;
331     }
332     return af->setDeviceConnectedState(port, state);
333 }
334 
invalidateTracks(const std::vector<audio_port_handle_t> & portIds)335 status_t AudioPolicyService::AudioPolicyClient::invalidateTracks(
336         const std::vector<audio_port_handle_t>& portIds) {
337     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
338     if (af == 0) {
339         return PERMISSION_DENIED;
340     }
341 
342     return af->invalidateTracks(portIds);
343 }
344 
getAudioMixPort(const struct audio_port_v7 * devicePort,struct audio_port_v7 * port)345 status_t AudioPolicyService::AudioPolicyClient::getAudioMixPort(
346         const struct audio_port_v7 *devicePort,
347         struct audio_port_v7 *port) {
348     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
349     if (af == 0) {
350         return PERMISSION_DENIED;
351     }
352     return af->getAudioMixPort(devicePort, port);
353 }
354 
setTracksInternalMute(const std::vector<media::TrackInternalMuteInfo> & tracksInternalMute)355 status_t AudioPolicyService::AudioPolicyClient::setTracksInternalMute(
356         const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) {
357     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
358     if (af == nullptr) {
359         return PERMISSION_DENIED;
360     }
361     return af->setTracksInternalMute(tracksInternalMute);
362 }
363 
364 } // namespace android
365