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