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