blob: e1e81e1154d1cc12d329b6050679f8d34eafc4ba [file] [log] [blame]
Eric Laurentdce54a12014-03-10 12:19:46 -07001/*
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>
Eric Laurentf3806772014-10-07 09:19:31 -070027#include <media/AudioPolicyHelper.h>
Eric Laurentdce54a12014-03-10 12:19:46 -070028
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{
38 if (mpAudioPolicy == NULL) {
39 return NO_INIT;
40 }
41 if (!settingsAllowed()) {
42 return PERMISSION_DENIED;
43 }
44 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
45 return BAD_VALUE;
46 }
47 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
48 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
49 return BAD_VALUE;
50 }
51
52 ALOGV("setDeviceConnectionState()");
53 Mutex::Autolock _l(mLock);
54 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
55 state, device_address);
56}
57
58audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
59 audio_devices_t device,
60 const char *device_address)
61{
62 if (mpAudioPolicy == NULL) {
63 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
64 }
65 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
66 device_address);
67}
68
69status_t AudioPolicyService::setPhoneState(audio_mode_t state)
70{
71 if (mpAudioPolicy == NULL) {
72 return NO_INIT;
73 }
74 if (!settingsAllowed()) {
75 return PERMISSION_DENIED;
76 }
77 if (uint32_t(state) >= AUDIO_MODE_CNT) {
78 return BAD_VALUE;
79 }
80
81 ALOGV("setPhoneState()");
82
83 // TODO: check if it is more appropriate to do it in platform specific policy manager
84 AudioSystem::setMode(state);
85
86 Mutex::Autolock _l(mLock);
87 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Eric Laurentbb6c9a02014-09-25 14:11:47 -070088 mPhoneState = state;
Eric Laurentdce54a12014-03-10 12:19:46 -070089 return NO_ERROR;
90}
91
Eric Laurentbb6c9a02014-09-25 14:11:47 -070092audio_mode_t AudioPolicyService::getPhoneState()
93{
94 Mutex::Autolock _l(mLock);
95 return mPhoneState;
96}
97
Eric Laurentdce54a12014-03-10 12:19:46 -070098status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
99 audio_policy_forced_cfg_t config)
100{
101 if (mpAudioPolicy == NULL) {
102 return NO_INIT;
103 }
104 if (!settingsAllowed()) {
105 return PERMISSION_DENIED;
106 }
107 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
108 return BAD_VALUE;
109 }
110 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
111 return BAD_VALUE;
112 }
113 ALOGV("setForceUse()");
114 Mutex::Autolock _l(mLock);
115 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
116 return NO_ERROR;
117}
118
119audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
120{
121 if (mpAudioPolicy == NULL) {
122 return AUDIO_POLICY_FORCE_NONE;
123 }
124 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
125 return AUDIO_POLICY_FORCE_NONE;
126 }
127 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
128}
129
130audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
131 uint32_t samplingRate,
132 audio_format_t format,
133 audio_channel_mask_t channelMask,
134 audio_output_flags_t flags,
135 const audio_offload_info_t *offloadInfo)
136{
137 if (mpAudioPolicy == NULL) {
138 return 0;
139 }
140 ALOGV("getOutput()");
141 Mutex::Autolock _l(mLock);
142 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
143 format, channelMask, flags, offloadInfo);
144}
145
146status_t AudioPolicyService::startOutput(audio_io_handle_t output,
147 audio_stream_type_t stream,
148 int session)
149{
150 if (mpAudioPolicy == NULL) {
151 return NO_INIT;
152 }
153 ALOGV("startOutput()");
bryant_liuba2b4392014-06-11 16:49:30 +0800154 // create audio processors according to stream
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700155 sp<AudioPolicyEffects>audioPolicyEffects;
156 {
157 Mutex::Autolock _l(mLock);
158 audioPolicyEffects = mAudioPolicyEffects;
159 }
160 if (audioPolicyEffects != 0) {
161 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
162 if (status != NO_ERROR && status != ALREADY_EXISTS) {
163 ALOGW("Failed to add effects on session %d", session);
164 }
bryant_liuba2b4392014-06-11 16:49:30 +0800165 }
166
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700167 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700168 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
169}
170
171status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
172 audio_stream_type_t stream,
173 int session)
174{
175 if (mpAudioPolicy == NULL) {
176 return NO_INIT;
177 }
178 ALOGV("stopOutput()");
179 mOutputCommandThread->stopOutputCommand(output, stream, session);
180 return NO_ERROR;
181}
182
183status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
184 audio_stream_type_t stream,
185 int session)
186{
187 ALOGV("doStopOutput from tid %d", gettid());
bryant_liuba2b4392014-06-11 16:49:30 +0800188 // release audio processors from the stream
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700189 sp<AudioPolicyEffects>audioPolicyEffects;
190 {
191 Mutex::Autolock _l(mLock);
192 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800193 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700194 if (audioPolicyEffects != 0) {
195 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
196 if (status != NO_ERROR && status != ALREADY_EXISTS) {
197 ALOGW("Failed to release effects on session %d", session);
198 }
199 }
200 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700201 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
202}
203
204void AudioPolicyService::releaseOutput(audio_io_handle_t output)
205{
206 if (mpAudioPolicy == NULL) {
207 return;
208 }
209 ALOGV("releaseOutput()");
210 mOutputCommandThread->releaseOutputCommand(output);
211}
212
213void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
214{
215 ALOGV("doReleaseOutput from tid %d", gettid());
216 Mutex::Autolock _l(mLock);
217 mpAudioPolicy->release_output(mpAudioPolicy, output);
218}
219
220audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
221 uint32_t samplingRate,
222 audio_format_t format,
223 audio_channel_mask_t channelMask,
Glenn Kastenb3b16602014-07-16 08:36:31 -0700224 int audioSession,
225 audio_input_flags_t flags __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700226{
227 if (mpAudioPolicy == NULL) {
228 return 0;
229 }
230 // already checked by client, but double-check in case the client wrapper is bypassed
231 if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
232 return 0;
233 }
234
235 if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
236 return 0;
237 }
238
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700239 audio_io_handle_t input;
240 sp<AudioPolicyEffects>audioPolicyEffects;
241 {
242 Mutex::Autolock _l(mLock);
243 // the audio_in_acoustics_t parameter is ignored by get_input()
244 input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
245 format, channelMask, (audio_in_acoustics_t) 0);
246 audioPolicyEffects = mAudioPolicyEffects;
247 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700248 if (input == 0) {
249 return input;
250 }
bryant_liuba2b4392014-06-11 16:49:30 +0800251
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700252 if (audioPolicyEffects != 0) {
253 // create audio pre processors according to input source
254 status_t status = audioPolicyEffects->addInputEffects(input, inputSource, audioSession);
255 if (status != NO_ERROR && status != ALREADY_EXISTS) {
256 ALOGW("Failed to add effects on input %d", input);
257 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700258 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700259 return input;
260}
261
Eric Laurent4dc68062014-07-28 17:26:49 -0700262status_t AudioPolicyService::startInput(audio_io_handle_t input,
263 audio_session_t session __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700264{
265 if (mpAudioPolicy == NULL) {
266 return NO_INIT;
267 }
268 Mutex::Autolock _l(mLock);
269
270 return mpAudioPolicy->start_input(mpAudioPolicy, input);
271}
272
Eric Laurent4dc68062014-07-28 17:26:49 -0700273status_t AudioPolicyService::stopInput(audio_io_handle_t input,
274 audio_session_t session __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700275{
276 if (mpAudioPolicy == NULL) {
277 return NO_INIT;
278 }
279 Mutex::Autolock _l(mLock);
280
281 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
282}
283
Eric Laurent4dc68062014-07-28 17:26:49 -0700284void AudioPolicyService::releaseInput(audio_io_handle_t input,
285 audio_session_t session __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700286{
287 if (mpAudioPolicy == NULL) {
288 return;
289 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700290
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700291 sp<AudioPolicyEffects>audioPolicyEffects;
292 {
293 Mutex::Autolock _l(mLock);
294 mpAudioPolicy->release_input(mpAudioPolicy, input);
295 audioPolicyEffects = mAudioPolicyEffects;
296 }
297 if (audioPolicyEffects != 0) {
298 // release audio processors from the input
299 status_t status = audioPolicyEffects->releaseInputEffects(input);
300 if(status != NO_ERROR) {
301 ALOGW("Failed to release effects on input %d", input);
302 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700303 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700304}
305
306status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
307 int indexMin,
308 int indexMax)
309{
310 if (mpAudioPolicy == NULL) {
311 return NO_INIT;
312 }
313 if (!settingsAllowed()) {
314 return PERMISSION_DENIED;
315 }
316 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
317 return BAD_VALUE;
318 }
319 Mutex::Autolock _l(mLock);
320 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
321 return NO_ERROR;
322}
323
324status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
325 int index,
326 audio_devices_t device)
327{
328 if (mpAudioPolicy == NULL) {
329 return NO_INIT;
330 }
331 if (!settingsAllowed()) {
332 return PERMISSION_DENIED;
333 }
334 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
335 return BAD_VALUE;
336 }
337 Mutex::Autolock _l(mLock);
338 if (mpAudioPolicy->set_stream_volume_index_for_device) {
339 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
340 stream,
341 index,
342 device);
343 } else {
344 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
345 }
346}
347
348status_t AudioPolicyService::getStreamVolumeIndex(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 (uint32_t(stream) >= AUDIO_STREAM_CNT) {
356 return BAD_VALUE;
357 }
358 Mutex::Autolock _l(mLock);
359 if (mpAudioPolicy->get_stream_volume_index_for_device) {
360 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
361 stream,
362 index,
363 device);
364 } else {
365 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
366 }
367}
368
369uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
370{
371 if (mpAudioPolicy == NULL) {
372 return 0;
373 }
374 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
375}
376
377//audio policy: use audio_device_t appropriately
378
379audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
380{
381 if (mpAudioPolicy == NULL) {
382 return (audio_devices_t)0;
383 }
384 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
385}
386
387audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
388{
389 // FIXME change return type to status_t, and return NO_INIT here
390 if (mpAudioPolicy == NULL) {
391 return 0;
392 }
393 Mutex::Autolock _l(mLock);
394 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
395}
396
397status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
398 audio_io_handle_t io,
399 uint32_t strategy,
400 int session,
401 int id)
402{
403 if (mpAudioPolicy == NULL) {
404 return NO_INIT;
405 }
406 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
407}
408
409status_t AudioPolicyService::unregisterEffect(int id)
410{
411 if (mpAudioPolicy == NULL) {
412 return NO_INIT;
413 }
414 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
415}
416
417status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
418{
419 if (mpAudioPolicy == NULL) {
420 return NO_INIT;
421 }
422 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
423}
424
425bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
426{
427 if (mpAudioPolicy == NULL) {
428 return 0;
429 }
430 Mutex::Autolock _l(mLock);
431 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
432}
433
434bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
435{
436 if (mpAudioPolicy == NULL) {
437 return 0;
438 }
439 Mutex::Autolock _l(mLock);
440 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
441}
442
443bool AudioPolicyService::isSourceActive(audio_source_t source) const
444{
445 if (mpAudioPolicy == NULL) {
446 return false;
447 }
448 if (mpAudioPolicy->is_source_active == 0) {
449 return false;
450 }
451 Mutex::Autolock _l(mLock);
452 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
453}
454
455status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
456 effect_descriptor_t *descriptors,
457 uint32_t *count)
458{
Eric Laurentdce54a12014-03-10 12:19:46 -0700459 if (mpAudioPolicy == NULL) {
460 *count = 0;
461 return NO_INIT;
462 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700463 sp<AudioPolicyEffects>audioPolicyEffects;
464 {
465 Mutex::Autolock _l(mLock);
466 audioPolicyEffects = mAudioPolicyEffects;
467 }
468 if (audioPolicyEffects == 0) {
469 *count = 0;
470 return NO_INIT;
471 }
472 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurentdce54a12014-03-10 12:19:46 -0700473}
474
475bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
476{
477 if (mpAudioPolicy == NULL) {
478 ALOGV("mpAudioPolicy == NULL");
479 return false;
480 }
481
482 if (mpAudioPolicy->is_offload_supported == NULL) {
483 ALOGV("HAL does not implement is_offload_supported");
484 return false;
485 }
486
487 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
488}
489
Eric Laurent8670c312014-04-01 10:34:16 -0700490status_t AudioPolicyService::listAudioPorts(audio_port_role_t role __unused,
491 audio_port_type_t type __unused,
492 unsigned int *num_ports,
493 struct audio_port *ports __unused,
494 unsigned int *generation __unused)
495{
496 *num_ports = 0;
497 return INVALID_OPERATION;
498}
499
500status_t AudioPolicyService::getAudioPort(struct audio_port *port __unused)
501{
502 return INVALID_OPERATION;
503}
504
505status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch __unused,
506 audio_patch_handle_t *handle __unused)
507{
508 return INVALID_OPERATION;
509}
510
511status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle __unused)
512{
513 return INVALID_OPERATION;
514}
515
516status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
517 struct audio_patch *patches __unused,
518 unsigned int *generation __unused)
519{
520 *num_patches = 0;
521 return INVALID_OPERATION;
522}
523
524status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config __unused)
525{
526 return INVALID_OPERATION;
527}
Eric Laurentdce54a12014-03-10 12:19:46 -0700528
Eric Laurentf3806772014-10-07 09:19:31 -0700529audio_io_handle_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
Eric Laurent7c5b60a2014-07-15 10:38:16 -0700530 uint32_t samplingRate,
531 audio_format_t format,
532 audio_channel_mask_t channelMask,
533 audio_output_flags_t flags,
534 const audio_offload_info_t *offloadInfo)
535{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700536 audio_stream_type_t stream = audio_attributes_to_stream_type(attr);
537
Eric Laurent7c5b60a2014-07-15 10:38:16 -0700538 return getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
539}
540
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700541status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
542 audio_io_handle_t *ioHandle,
543 audio_devices_t *device)
544{
545 return INVALID_OPERATION;
546}
547
548status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
549{
550 return INVALID_OPERATION;
551}
Eric Laurent7c5b60a2014-07-15 10:38:16 -0700552
Eric Laurentdce54a12014-03-10 12:19:46 -0700553}; // namespace android