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