AudioPolicyInterfaceImplLegacy.cpp revision 4dc680607181e6a76f4e91a39366c4f5dfb7b03e
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                                    audio_input_flags_t flags __unused)
207{
208    if (mpAudioPolicy == NULL) {
209        return 0;
210    }
211    // already checked by client, but double-check in case the client wrapper is bypassed
212    if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
213        return 0;
214    }
215
216    if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
217        return 0;
218    }
219
220    Mutex::Autolock _l(mLock);
221    // the audio_in_acoustics_t parameter is ignored by get_input()
222    audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
223                                                   format, channelMask, (audio_in_acoustics_t) 0);
224
225    if (input == 0) {
226        return input;
227    }
228
229    // create audio pre processors according to input source
230    status_t status = mAudioPolicyEffects->addInputEffects(input, inputSource, audioSession);
231    if (status != NO_ERROR && status != ALREADY_EXISTS) {
232        ALOGW("Failed to add effects on input %d", input);
233    }
234
235    return input;
236}
237
238status_t AudioPolicyService::startInput(audio_io_handle_t input,
239                                        audio_session_t session __unused)
240{
241    if (mpAudioPolicy == NULL) {
242        return NO_INIT;
243    }
244    Mutex::Autolock _l(mLock);
245
246    return mpAudioPolicy->start_input(mpAudioPolicy, input);
247}
248
249status_t AudioPolicyService::stopInput(audio_io_handle_t input,
250                                       audio_session_t session __unused)
251{
252    if (mpAudioPolicy == NULL) {
253        return NO_INIT;
254    }
255    Mutex::Autolock _l(mLock);
256
257    return mpAudioPolicy->stop_input(mpAudioPolicy, input);
258}
259
260void AudioPolicyService::releaseInput(audio_io_handle_t input,
261                                      audio_session_t session __unused)
262{
263    if (mpAudioPolicy == NULL) {
264        return;
265    }
266    Mutex::Autolock _l(mLock);
267    mpAudioPolicy->release_input(mpAudioPolicy, input);
268
269    // release audio processors from the input
270    status_t status = mAudioPolicyEffects->releaseInputEffects(input);
271    if(status != NO_ERROR) {
272        ALOGW("Failed to release effects on input %d", input);
273    }
274}
275
276status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
277                                            int indexMin,
278                                            int indexMax)
279{
280    if (mpAudioPolicy == NULL) {
281        return NO_INIT;
282    }
283    if (!settingsAllowed()) {
284        return PERMISSION_DENIED;
285    }
286    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
287        return BAD_VALUE;
288    }
289    Mutex::Autolock _l(mLock);
290    mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
291    return NO_ERROR;
292}
293
294status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
295                                                  int index,
296                                                  audio_devices_t device)
297{
298    if (mpAudioPolicy == NULL) {
299        return NO_INIT;
300    }
301    if (!settingsAllowed()) {
302        return PERMISSION_DENIED;
303    }
304    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
305        return BAD_VALUE;
306    }
307    Mutex::Autolock _l(mLock);
308    if (mpAudioPolicy->set_stream_volume_index_for_device) {
309        return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
310                                                                stream,
311                                                                index,
312                                                                device);
313    } else {
314        return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
315    }
316}
317
318status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
319                                                  int *index,
320                                                  audio_devices_t device)
321{
322    if (mpAudioPolicy == NULL) {
323        return NO_INIT;
324    }
325    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
326        return BAD_VALUE;
327    }
328    Mutex::Autolock _l(mLock);
329    if (mpAudioPolicy->get_stream_volume_index_for_device) {
330        return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
331                                                                stream,
332                                                                index,
333                                                                device);
334    } else {
335        return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
336    }
337}
338
339uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
340{
341    if (mpAudioPolicy == NULL) {
342        return 0;
343    }
344    return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, 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 (mpAudioPolicy == NULL) {
352        return (audio_devices_t)0;
353    }
354    return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, 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 (mpAudioPolicy == NULL) {
361        return 0;
362    }
363    Mutex::Autolock _l(mLock);
364    return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, 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 (mpAudioPolicy == NULL) {
374        return NO_INIT;
375    }
376    return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
377}
378
379status_t AudioPolicyService::unregisterEffect(int id)
380{
381    if (mpAudioPolicy == NULL) {
382        return NO_INIT;
383    }
384    return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
385}
386
387status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
388{
389    if (mpAudioPolicy == NULL) {
390        return NO_INIT;
391    }
392    return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
393}
394
395bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
396{
397    if (mpAudioPolicy == NULL) {
398        return 0;
399    }
400    Mutex::Autolock _l(mLock);
401    return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
402}
403
404bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
405{
406    if (mpAudioPolicy == NULL) {
407        return 0;
408    }
409    Mutex::Autolock _l(mLock);
410    return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
411}
412
413bool AudioPolicyService::isSourceActive(audio_source_t source) const
414{
415    if (mpAudioPolicy == NULL) {
416        return false;
417    }
418    if (mpAudioPolicy->is_source_active == 0) {
419        return false;
420    }
421    Mutex::Autolock _l(mLock);
422    return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
423}
424
425status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
426                                                       effect_descriptor_t *descriptors,
427                                                       uint32_t *count)
428{
429    if (mpAudioPolicy == NULL) {
430        *count = 0;
431        return NO_INIT;
432    }
433    Mutex::Autolock _l(mLock);
434
435    return mAudioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
436}
437
438bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
439{
440    if (mpAudioPolicy == NULL) {
441        ALOGV("mpAudioPolicy == NULL");
442        return false;
443    }
444
445    if (mpAudioPolicy->is_offload_supported == NULL) {
446        ALOGV("HAL does not implement is_offload_supported");
447        return false;
448    }
449
450    return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
451}
452
453status_t AudioPolicyService::listAudioPorts(audio_port_role_t role __unused,
454                                            audio_port_type_t type __unused,
455                                            unsigned int *num_ports,
456                                            struct audio_port *ports __unused,
457                                            unsigned int *generation __unused)
458{
459    *num_ports = 0;
460    return INVALID_OPERATION;
461}
462
463status_t AudioPolicyService::getAudioPort(struct audio_port *port __unused)
464{
465    return INVALID_OPERATION;
466}
467
468status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch __unused,
469        audio_patch_handle_t *handle __unused)
470{
471    return INVALID_OPERATION;
472}
473
474status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle __unused)
475{
476    return INVALID_OPERATION;
477}
478
479status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
480        struct audio_patch *patches __unused,
481        unsigned int *generation __unused)
482{
483    *num_patches = 0;
484    return INVALID_OPERATION;
485}
486
487status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config __unused)
488{
489    return INVALID_OPERATION;
490}
491
492audio_io_handle_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr __unused,
493                                    uint32_t samplingRate,
494                                    audio_format_t format,
495                                    audio_channel_mask_t channelMask,
496                                    audio_output_flags_t flags,
497                                    const audio_offload_info_t *offloadInfo)
498{
499    //FIXME: temporary to fix build with USE_LEGACY_AUDIO_POLICY
500    audio_stream_type_t stream = AUDIO_STREAM_MUSIC;
501    return getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
502}
503
504
505}; // namespace android
506