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