AudioPolicyInterfaceImpl.cpp revision 6a8ab05f0598f4ebdd5ef82e93cf32fde0598189
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 "AudioPolicyIntefaceImpl"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
21#include "AudioPolicyService.h"
22#include "ServiceUtilities.h"
23
24namespace android {
25
26
27// ----------------------------------------------------------------------------
28
29status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
30                                                  audio_policy_dev_state_t state,
31                                                  const char *device_address)
32{
33    if (mAudioPolicyManager == NULL) {
34        return NO_INIT;
35    }
36    if (!settingsAllowed()) {
37        return PERMISSION_DENIED;
38    }
39    if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
40        return BAD_VALUE;
41    }
42    if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
43            state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
44        return BAD_VALUE;
45    }
46
47    ALOGV("setDeviceConnectionState()");
48    Mutex::Autolock _l(mLock);
49    return mAudioPolicyManager->setDeviceConnectionState(device,
50                                                      state, device_address);
51}
52
53audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
54                                                              audio_devices_t device,
55                                                              const char *device_address)
56{
57    if (mAudioPolicyManager == NULL) {
58        return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
59    }
60    return mAudioPolicyManager->getDeviceConnectionState(device,
61                                                      device_address);
62}
63
64status_t AudioPolicyService::setPhoneState(audio_mode_t state)
65{
66    if (mAudioPolicyManager == NULL) {
67        return NO_INIT;
68    }
69    if (!settingsAllowed()) {
70        return PERMISSION_DENIED;
71    }
72    if (uint32_t(state) >= AUDIO_MODE_CNT) {
73        return BAD_VALUE;
74    }
75
76    ALOGV("setPhoneState()");
77
78    // TODO: check if it is more appropriate to do it in platform specific policy manager
79    AudioSystem::setMode(state);
80
81    Mutex::Autolock _l(mLock);
82    mAudioPolicyManager->setPhoneState(state);
83    return NO_ERROR;
84}
85
86status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
87                                         audio_policy_forced_cfg_t config)
88{
89    if (mAudioPolicyManager == NULL) {
90        return NO_INIT;
91    }
92    if (!settingsAllowed()) {
93        return PERMISSION_DENIED;
94    }
95    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
96        return BAD_VALUE;
97    }
98    if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
99        return BAD_VALUE;
100    }
101    ALOGV("setForceUse()");
102    Mutex::Autolock _l(mLock);
103    mAudioPolicyManager->setForceUse(usage, config);
104    return NO_ERROR;
105}
106
107audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
108{
109    if (mAudioPolicyManager == NULL) {
110        return AUDIO_POLICY_FORCE_NONE;
111    }
112    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
113        return AUDIO_POLICY_FORCE_NONE;
114    }
115    return mAudioPolicyManager->getForceUse(usage);
116}
117
118audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
119                                    uint32_t samplingRate,
120                                    audio_format_t format,
121                                    audio_channel_mask_t channelMask,
122                                    audio_output_flags_t flags,
123                                    const audio_offload_info_t *offloadInfo)
124{
125    if (mAudioPolicyManager == NULL) {
126        return 0;
127    }
128    ALOGV("getOutput()");
129    Mutex::Autolock _l(mLock);
130    return mAudioPolicyManager->getOutput(stream, samplingRate,
131                                    format, channelMask, flags, offloadInfo);
132}
133
134audio_io_handle_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
135                                    uint32_t samplingRate,
136                                    audio_format_t format,
137                                    audio_channel_mask_t channelMask,
138                                    audio_output_flags_t flags,
139                                    const audio_offload_info_t *offloadInfo)
140{
141    if (mAudioPolicyManager == NULL) {
142        return 0;
143    }
144    ALOGV("getOutput()");
145    Mutex::Autolock _l(mLock);
146    return mAudioPolicyManager->getOutputForAttr(attr, samplingRate,
147                                    format, channelMask, flags, offloadInfo);
148}
149
150status_t AudioPolicyService::startOutput(audio_io_handle_t output,
151                                         audio_stream_type_t stream,
152                                         int session)
153{
154    if (mAudioPolicyManager == NULL) {
155        return NO_INIT;
156    }
157    ALOGV("startOutput()");
158    Mutex::Autolock _l(mLock);
159
160    // create audio processors according to stream
161    status_t status = mAudioPolicyEffects->addOutputSessionEffects(output, stream, session);
162    if (status != NO_ERROR && status != ALREADY_EXISTS) {
163        ALOGW("Failed to add effects on session %d", session);
164    }
165
166    return mAudioPolicyManager->startOutput(output, stream, session);
167}
168
169status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
170                                        audio_stream_type_t stream,
171                                        int session)
172{
173    if (mAudioPolicyManager == NULL) {
174        return NO_INIT;
175    }
176    ALOGV("stopOutput()");
177    mOutputCommandThread->stopOutputCommand(output, stream, session);
178    return NO_ERROR;
179}
180
181status_t  AudioPolicyService::doStopOutput(audio_io_handle_t output,
182                                      audio_stream_type_t stream,
183                                      int session)
184{
185    ALOGV("doStopOutput from tid %d", gettid());
186    Mutex::Autolock _l(mLock);
187
188    // release audio processors from the stream
189    status_t status = mAudioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
190    if (status != NO_ERROR && status != ALREADY_EXISTS) {
191        ALOGW("Failed to release effects on session %d", session);
192    }
193
194    return mAudioPolicyManager->stopOutput(output, stream, session);
195}
196
197void AudioPolicyService::releaseOutput(audio_io_handle_t output)
198{
199    if (mAudioPolicyManager == NULL) {
200        return;
201    }
202    ALOGV("releaseOutput()");
203    mOutputCommandThread->releaseOutputCommand(output);
204}
205
206void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
207{
208    ALOGV("doReleaseOutput from tid %d", gettid());
209    Mutex::Autolock _l(mLock);
210    mAudioPolicyManager->releaseOutput(output);
211}
212
213audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
214                                    uint32_t samplingRate,
215                                    audio_format_t format,
216                                    audio_channel_mask_t channelMask,
217                                    int audioSession,
218                                    audio_input_flags_t flags)
219{
220    if (mAudioPolicyManager == NULL) {
221        return 0;
222    }
223    // already checked by client, but double-check in case the client wrapper is bypassed
224    if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
225        return 0;
226    }
227
228    if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
229        return 0;
230    }
231
232    Mutex::Autolock _l(mLock);
233    // the audio_in_acoustics_t parameter is ignored by get_input()
234    audio_io_handle_t input = mAudioPolicyManager->getInput(inputSource, samplingRate,
235                                                   format, channelMask, (audio_in_acoustics_t) 0,
236                                                   flags);
237
238    if (input == 0) {
239        return input;
240    }
241
242    // create audio pre processors according to input source
243    status_t status = mAudioPolicyEffects->addInputEffects(input, inputSource, audioSession);
244    if (status != NO_ERROR && status != ALREADY_EXISTS) {
245        ALOGW("Failed to add effects on input %d", input);
246    }
247
248    return input;
249}
250
251status_t AudioPolicyService::startInput(audio_io_handle_t input)
252{
253    if (mAudioPolicyManager == NULL) {
254        return NO_INIT;
255    }
256    Mutex::Autolock _l(mLock);
257
258    return mAudioPolicyManager->startInput(input);
259}
260
261status_t AudioPolicyService::stopInput(audio_io_handle_t input)
262{
263    if (mAudioPolicyManager == NULL) {
264        return NO_INIT;
265    }
266    Mutex::Autolock _l(mLock);
267
268    return mAudioPolicyManager->stopInput(input);
269}
270
271void AudioPolicyService::releaseInput(audio_io_handle_t input)
272{
273    if (mAudioPolicyManager == NULL) {
274        return;
275    }
276    Mutex::Autolock _l(mLock);
277    mAudioPolicyManager->releaseInput(input);
278
279    // release audio processors from the input
280    status_t status = mAudioPolicyEffects->releaseInputEffects(input);
281    if(status != NO_ERROR) {
282        ALOGW("Failed to release effects on input %d", input);
283    }
284}
285
286status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
287                                            int indexMin,
288                                            int indexMax)
289{
290    if (mAudioPolicyManager == NULL) {
291        return NO_INIT;
292    }
293    if (!settingsAllowed()) {
294        return PERMISSION_DENIED;
295    }
296    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
297        return BAD_VALUE;
298    }
299    Mutex::Autolock _l(mLock);
300    mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
301    return NO_ERROR;
302}
303
304status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
305                                                  int index,
306                                                  audio_devices_t device)
307{
308    if (mAudioPolicyManager == NULL) {
309        return NO_INIT;
310    }
311    if (!settingsAllowed()) {
312        return PERMISSION_DENIED;
313    }
314    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
315        return BAD_VALUE;
316    }
317    Mutex::Autolock _l(mLock);
318    return mAudioPolicyManager->setStreamVolumeIndex(stream,
319                                                    index,
320                                                    device);
321}
322
323status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
324                                                  int *index,
325                                                  audio_devices_t device)
326{
327    if (mAudioPolicyManager == NULL) {
328        return NO_INIT;
329    }
330    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
331        return BAD_VALUE;
332    }
333    Mutex::Autolock _l(mLock);
334    return mAudioPolicyManager->getStreamVolumeIndex(stream,
335                                                    index,
336                                                    device);
337}
338
339uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
340{
341    if (mAudioPolicyManager == NULL) {
342        return 0;
343    }
344    return mAudioPolicyManager->getStrategyForStream(stream);
345}
346
347//audio policy: use audio_device_t appropriately
348
349audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
350{
351    if (mAudioPolicyManager == NULL) {
352        return (audio_devices_t)0;
353    }
354    return mAudioPolicyManager->getDevicesForStream(stream);
355}
356
357audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
358{
359    // FIXME change return type to status_t, and return NO_INIT here
360    if (mAudioPolicyManager == NULL) {
361        return 0;
362    }
363    Mutex::Autolock _l(mLock);
364    return mAudioPolicyManager->getOutputForEffect(desc);
365}
366
367status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
368                                audio_io_handle_t io,
369                                uint32_t strategy,
370                                int session,
371                                int id)
372{
373    if (mAudioPolicyManager == NULL) {
374        return NO_INIT;
375    }
376    return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
377}
378
379status_t AudioPolicyService::unregisterEffect(int id)
380{
381    if (mAudioPolicyManager == NULL) {
382        return NO_INIT;
383    }
384    return mAudioPolicyManager->unregisterEffect(id);
385}
386
387status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
388{
389    if (mAudioPolicyManager == NULL) {
390        return NO_INIT;
391    }
392    return mAudioPolicyManager->setEffectEnabled(id, enabled);
393}
394
395bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
396{
397    if (mAudioPolicyManager == NULL) {
398        return 0;
399    }
400    Mutex::Autolock _l(mLock);
401    return mAudioPolicyManager->isStreamActive(stream, inPastMs);
402}
403
404bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
405{
406    if (mAudioPolicyManager == NULL) {
407        return 0;
408    }
409    Mutex::Autolock _l(mLock);
410    return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
411}
412
413bool AudioPolicyService::isSourceActive(audio_source_t source) const
414{
415    if (mAudioPolicyManager == NULL) {
416        return false;
417    }
418    Mutex::Autolock _l(mLock);
419    return mAudioPolicyManager->isSourceActive(source);
420}
421
422status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
423                                                       effect_descriptor_t *descriptors,
424                                                       uint32_t *count)
425{
426    if (mAudioPolicyManager == NULL) {
427        *count = 0;
428        return NO_INIT;
429    }
430    Mutex::Autolock _l(mLock);
431
432    return mAudioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
433}
434
435bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
436{
437    if (mAudioPolicyManager == NULL) {
438        ALOGV("mAudioPolicyManager == NULL");
439        return false;
440    }
441
442    return mAudioPolicyManager->isOffloadSupported(info);
443}
444
445status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
446                                            audio_port_type_t type,
447                                            unsigned int *num_ports,
448                                            struct audio_port *ports,
449                                            unsigned int *generation)
450{
451    Mutex::Autolock _l(mLock);
452    if(!modifyAudioRoutingAllowed()) {
453        return PERMISSION_DENIED;
454    }
455    if (mAudioPolicyManager == NULL) {
456        return NO_INIT;
457    }
458
459    return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
460}
461
462status_t AudioPolicyService::getAudioPort(struct audio_port *port)
463{
464    Mutex::Autolock _l(mLock);
465    if(!modifyAudioRoutingAllowed()) {
466        return PERMISSION_DENIED;
467    }
468    if (mAudioPolicyManager == NULL) {
469        return NO_INIT;
470    }
471
472    return mAudioPolicyManager->getAudioPort(port);
473}
474
475status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
476        audio_patch_handle_t *handle)
477{
478    Mutex::Autolock _l(mLock);
479    if(!modifyAudioRoutingAllowed()) {
480        return PERMISSION_DENIED;
481    }
482    if (mAudioPolicyManager == NULL) {
483        return NO_INIT;
484    }
485    return mAudioPolicyManager->createAudioPatch(patch, handle,
486                                                  IPCThreadState::self()->getCallingUid());
487}
488
489status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
490{
491    Mutex::Autolock _l(mLock);
492    if(!modifyAudioRoutingAllowed()) {
493        return PERMISSION_DENIED;
494    }
495    if (mAudioPolicyManager == NULL) {
496        return NO_INIT;
497    }
498
499    return mAudioPolicyManager->releaseAudioPatch(handle,
500                                                     IPCThreadState::self()->getCallingUid());
501}
502
503status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
504        struct audio_patch *patches,
505        unsigned int *generation)
506{
507    Mutex::Autolock _l(mLock);
508    if(!modifyAudioRoutingAllowed()) {
509        return PERMISSION_DENIED;
510    }
511    if (mAudioPolicyManager == NULL) {
512        return NO_INIT;
513    }
514
515    return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
516}
517
518status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
519{
520    Mutex::Autolock _l(mLock);
521    if(!modifyAudioRoutingAllowed()) {
522        return PERMISSION_DENIED;
523    }
524    if (mAudioPolicyManager == NULL) {
525        return NO_INIT;
526    }
527
528    return mAudioPolicyManager->setAudioPortConfig(config);
529}
530
531}; // namespace android
532