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                                             uid_t uid __unused,
238                                             uint32_t samplingRate,
239                                             audio_format_t format,
240                                             audio_channel_mask_t channelMask,
241                                             audio_input_flags_t flags __unused,
242                                             audio_port_handle_t selectedDeviceId __unused)
243{
244    if (mpAudioPolicy == NULL) {
245        return NO_INIT;
246    }
247
248    audio_source_t inputSource = attr->source;
249
250    // already checked by client, but double-check in case the client wrapper is bypassed
251    if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD &&
252        inputSource != AUDIO_SOURCE_FM_TUNER) {
253        return BAD_VALUE;
254    }
255
256    if (inputSource == AUDIO_SOURCE_DEFAULT) {
257        inputSource = AUDIO_SOURCE_MIC;
258    }
259
260    if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
261        return BAD_VALUE;
262    }
263
264    sp<AudioPolicyEffects>audioPolicyEffects;
265    {
266        Mutex::Autolock _l(mLock);
267        // the audio_in_acoustics_t parameter is ignored by get_input()
268        *input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
269                                             format, channelMask, (audio_in_acoustics_t) 0);
270        audioPolicyEffects = mAudioPolicyEffects;
271    }
272    if (*input == AUDIO_IO_HANDLE_NONE) {
273        return INVALID_OPERATION;
274    }
275
276    if (audioPolicyEffects != 0) {
277        // create audio pre processors according to input source
278        status_t status = audioPolicyEffects->addInputEffects(*input, inputSource, session);
279        if (status != NO_ERROR && status != ALREADY_EXISTS) {
280            ALOGW("Failed to add effects on input %d", input);
281        }
282    }
283    return NO_ERROR;
284}
285
286status_t AudioPolicyService::startInput(audio_io_handle_t input,
287                                        audio_session_t session __unused)
288{
289    if (mpAudioPolicy == NULL) {
290        return NO_INIT;
291    }
292    Mutex::Autolock _l(mLock);
293
294    return mpAudioPolicy->start_input(mpAudioPolicy, input);
295}
296
297status_t AudioPolicyService::stopInput(audio_io_handle_t input,
298                                       audio_session_t session __unused)
299{
300    if (mpAudioPolicy == NULL) {
301        return NO_INIT;
302    }
303    Mutex::Autolock _l(mLock);
304
305    return mpAudioPolicy->stop_input(mpAudioPolicy, input);
306}
307
308void AudioPolicyService::releaseInput(audio_io_handle_t input,
309                                      audio_session_t session __unused)
310{
311    if (mpAudioPolicy == NULL) {
312        return;
313    }
314
315    sp<AudioPolicyEffects>audioPolicyEffects;
316    {
317        Mutex::Autolock _l(mLock);
318        mpAudioPolicy->release_input(mpAudioPolicy, input);
319        audioPolicyEffects = mAudioPolicyEffects;
320    }
321    if (audioPolicyEffects != 0) {
322        // release audio processors from the input
323        status_t status = audioPolicyEffects->releaseInputEffects(input);
324        if(status != NO_ERROR) {
325            ALOGW("Failed to release effects on input %d", input);
326        }
327    }
328}
329
330status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
331                                            int indexMin,
332                                            int indexMax)
333{
334    if (mpAudioPolicy == NULL) {
335        return NO_INIT;
336    }
337    if (!settingsAllowed()) {
338        return PERMISSION_DENIED;
339    }
340    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
341        return BAD_VALUE;
342    }
343    Mutex::Autolock _l(mLock);
344    mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
345    return NO_ERROR;
346}
347
348status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
349                                                  int index,
350                                                  audio_devices_t device)
351{
352    if (mpAudioPolicy == NULL) {
353        return NO_INIT;
354    }
355    if (!settingsAllowed()) {
356        return PERMISSION_DENIED;
357    }
358    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
359        return BAD_VALUE;
360    }
361    Mutex::Autolock _l(mLock);
362    if (mpAudioPolicy->set_stream_volume_index_for_device) {
363        return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
364                                                                stream,
365                                                                index,
366                                                                device);
367    } else {
368        return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
369    }
370}
371
372status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
373                                                  int *index,
374                                                  audio_devices_t device)
375{
376    if (mpAudioPolicy == NULL) {
377        return NO_INIT;
378    }
379    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
380        return BAD_VALUE;
381    }
382    Mutex::Autolock _l(mLock);
383    if (mpAudioPolicy->get_stream_volume_index_for_device) {
384        return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
385                                                                stream,
386                                                                index,
387                                                                device);
388    } else {
389        return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
390    }
391}
392
393uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
394{
395    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
396        return 0;
397    }
398    if (mpAudioPolicy == NULL) {
399        return 0;
400    }
401    return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
402}
403
404//audio policy: use audio_device_t appropriately
405
406audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
407{
408    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
409        return AUDIO_DEVICE_NONE;
410    }
411    if (mpAudioPolicy == NULL) {
412        return AUDIO_DEVICE_NONE;
413    }
414    return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
415}
416
417audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
418{
419    // FIXME change return type to status_t, and return NO_INIT here
420    if (mpAudioPolicy == NULL) {
421        return 0;
422    }
423    Mutex::Autolock _l(mLock);
424    return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
425}
426
427status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
428                                audio_io_handle_t io,
429                                uint32_t strategy,
430                                int session,
431                                int id)
432{
433    if (mpAudioPolicy == NULL) {
434        return NO_INIT;
435    }
436    return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
437}
438
439status_t AudioPolicyService::unregisterEffect(int id)
440{
441    if (mpAudioPolicy == NULL) {
442        return NO_INIT;
443    }
444    return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
445}
446
447status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
448{
449    if (mpAudioPolicy == NULL) {
450        return NO_INIT;
451    }
452    return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
453}
454
455bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
456{
457    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
458        return false;
459    }
460    if (mpAudioPolicy == NULL) {
461        return false;
462    }
463    Mutex::Autolock _l(mLock);
464    return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
465}
466
467bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
468{
469    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
470        return false;
471    }
472    if (mpAudioPolicy == NULL) {
473        return false;
474    }
475    Mutex::Autolock _l(mLock);
476    return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
477}
478
479bool AudioPolicyService::isSourceActive(audio_source_t source) const
480{
481    if (mpAudioPolicy == NULL) {
482        return false;
483    }
484    if (mpAudioPolicy->is_source_active == 0) {
485        return false;
486    }
487    Mutex::Autolock _l(mLock);
488    return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
489}
490
491status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
492                                                       effect_descriptor_t *descriptors,
493                                                       uint32_t *count)
494{
495    if (mpAudioPolicy == NULL) {
496        *count = 0;
497        return NO_INIT;
498    }
499    sp<AudioPolicyEffects>audioPolicyEffects;
500    {
501        Mutex::Autolock _l(mLock);
502        audioPolicyEffects = mAudioPolicyEffects;
503    }
504    if (audioPolicyEffects == 0) {
505        *count = 0;
506        return NO_INIT;
507    }
508    return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
509}
510
511bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
512{
513    if (mpAudioPolicy == NULL) {
514        ALOGV("mpAudioPolicy == NULL");
515        return false;
516    }
517
518    if (mpAudioPolicy->is_offload_supported == NULL) {
519        ALOGV("HAL does not implement is_offload_supported");
520        return false;
521    }
522
523    return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
524}
525
526status_t AudioPolicyService::listAudioPorts(audio_port_role_t role __unused,
527                                            audio_port_type_t type __unused,
528                                            unsigned int *num_ports,
529                                            struct audio_port *ports __unused,
530                                            unsigned int *generation __unused)
531{
532    *num_ports = 0;
533    return INVALID_OPERATION;
534}
535
536status_t AudioPolicyService::getAudioPort(struct audio_port *port __unused)
537{
538    return INVALID_OPERATION;
539}
540
541status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch __unused,
542        audio_patch_handle_t *handle __unused)
543{
544    return INVALID_OPERATION;
545}
546
547status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle __unused)
548{
549    return INVALID_OPERATION;
550}
551
552status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
553        struct audio_patch *patches __unused,
554        unsigned int *generation __unused)
555{
556    *num_patches = 0;
557    return INVALID_OPERATION;
558}
559
560status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config __unused)
561{
562    return INVALID_OPERATION;
563}
564
565status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
566                                              audio_io_handle_t *output,
567                                              audio_session_t session __unused,
568                                              audio_stream_type_t *stream,
569                                              uid_t uid __unused,
570                                              uint32_t samplingRate,
571                                              audio_format_t format,
572                                              audio_channel_mask_t channelMask,
573                                              audio_output_flags_t flags,
574                                              audio_port_handle_t selectedDeviceId __unused,
575                                              const audio_offload_info_t *offloadInfo)
576{
577    if (attr != NULL) {
578        *stream = audio_attributes_to_stream_type(attr);
579    } else {
580        if (*stream == AUDIO_STREAM_DEFAULT) {
581            return BAD_VALUE;
582        }
583    }
584    *output = getOutput(*stream, samplingRate, format, channelMask,
585                                          flags, offloadInfo);
586    if (*output == AUDIO_IO_HANDLE_NONE) {
587        return INVALID_OPERATION;
588    }
589    return NO_ERROR;
590}
591
592status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session __unused,
593                                       audio_io_handle_t *ioHandle __unused,
594                                       audio_devices_t *device __unused)
595{
596    return INVALID_OPERATION;
597}
598
599status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session __unused)
600{
601    return INVALID_OPERATION;
602}
603
604status_t AudioPolicyService::registerPolicyMixes(Vector<AudioMix> mixes __unused,
605                                                 bool registration __unused)
606{
607    return INVALID_OPERATION;
608}
609
610status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
611                                  const audio_attributes_t *attributes,
612                                  audio_io_handle_t *handle)
613{
614    return INVALID_OPERATION;
615}
616
617status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
618{
619    return INVALID_OPERATION;
620}
621
622}; // namespace android
623