blob: 1e40bc3626c0c00c7a9c64fc58068a813b906f93 [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>
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);
Eric Laurentbb6c9a02014-09-25 14:11:47 -070087 mPhoneState = state;
Eric Laurentdce54a12014-03-10 12:19:46 -070088 return NO_ERROR;
89}
90
Eric Laurentbb6c9a02014-09-25 14:11:47 -070091audio_mode_t AudioPolicyService::getPhoneState()
92{
93 Mutex::Autolock _l(mLock);
94 return mPhoneState;
95}
96
Eric Laurentdce54a12014-03-10 12:19:46 -070097status_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 Mutex::Autolock _l(mLock);
bryant_liuba2b4392014-06-11 16:49:30 +0800154
155 // create audio processors according to stream
156 status_t status = mAudioPolicyEffects->addOutputSessionEffects(output, stream, session);
157 if (status != NO_ERROR && status != ALREADY_EXISTS) {
158 ALOGW("Failed to add effects on session %d", session);
159 }
160
Eric Laurentdce54a12014-03-10 12:19:46 -0700161 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
162}
163
164status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
165 audio_stream_type_t stream,
166 int session)
167{
168 if (mpAudioPolicy == NULL) {
169 return NO_INIT;
170 }
171 ALOGV("stopOutput()");
172 mOutputCommandThread->stopOutputCommand(output, stream, session);
173 return NO_ERROR;
174}
175
176status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
177 audio_stream_type_t stream,
178 int session)
179{
180 ALOGV("doStopOutput from tid %d", gettid());
181 Mutex::Autolock _l(mLock);
bryant_liuba2b4392014-06-11 16:49:30 +0800182
183 // release audio processors from the stream
184 status_t status = mAudioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
185 if (status != NO_ERROR && status != ALREADY_EXISTS) {
186 ALOGW("Failed to release effects on session %d", session);
187 }
188
Eric Laurentdce54a12014-03-10 12:19:46 -0700189 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
190}
191
192void AudioPolicyService::releaseOutput(audio_io_handle_t output)
193{
194 if (mpAudioPolicy == NULL) {
195 return;
196 }
197 ALOGV("releaseOutput()");
198 mOutputCommandThread->releaseOutputCommand(output);
199}
200
201void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
202{
203 ALOGV("doReleaseOutput from tid %d", gettid());
204 Mutex::Autolock _l(mLock);
205 mpAudioPolicy->release_output(mpAudioPolicy, output);
206}
207
208audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
209 uint32_t samplingRate,
210 audio_format_t format,
211 audio_channel_mask_t channelMask,
Glenn Kastenb3b16602014-07-16 08:36:31 -0700212 int audioSession,
213 audio_input_flags_t flags __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700214{
215 if (mpAudioPolicy == NULL) {
216 return 0;
217 }
218 // already checked by client, but double-check in case the client wrapper is bypassed
219 if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
220 return 0;
221 }
222
223 if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
224 return 0;
225 }
226
227 Mutex::Autolock _l(mLock);
228 // the audio_in_acoustics_t parameter is ignored by get_input()
229 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
230 format, channelMask, (audio_in_acoustics_t) 0);
231
232 if (input == 0) {
233 return input;
234 }
bryant_liuba2b4392014-06-11 16:49:30 +0800235
Eric Laurentdce54a12014-03-10 12:19:46 -0700236 // create audio pre processors according to input source
bryant_liuba2b4392014-06-11 16:49:30 +0800237 status_t status = mAudioPolicyEffects->addInputEffects(input, inputSource, audioSession);
238 if (status != NO_ERROR && status != ALREADY_EXISTS) {
239 ALOGW("Failed to add effects on input %d", input);
Eric Laurentdce54a12014-03-10 12:19:46 -0700240 }
241
Eric Laurentdce54a12014-03-10 12:19:46 -0700242 return input;
243}
244
Eric Laurent4dc68062014-07-28 17:26:49 -0700245status_t AudioPolicyService::startInput(audio_io_handle_t input,
246 audio_session_t session __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700247{
248 if (mpAudioPolicy == NULL) {
249 return NO_INIT;
250 }
251 Mutex::Autolock _l(mLock);
252
253 return mpAudioPolicy->start_input(mpAudioPolicy, input);
254}
255
Eric Laurent4dc68062014-07-28 17:26:49 -0700256status_t AudioPolicyService::stopInput(audio_io_handle_t input,
257 audio_session_t session __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700258{
259 if (mpAudioPolicy == NULL) {
260 return NO_INIT;
261 }
262 Mutex::Autolock _l(mLock);
263
264 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
265}
266
Eric Laurent4dc68062014-07-28 17:26:49 -0700267void AudioPolicyService::releaseInput(audio_io_handle_t input,
268 audio_session_t session __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700269{
270 if (mpAudioPolicy == NULL) {
271 return;
272 }
273 Mutex::Autolock _l(mLock);
274 mpAudioPolicy->release_input(mpAudioPolicy, input);
275
bryant_liuba2b4392014-06-11 16:49:30 +0800276 // release audio processors from the input
277 status_t status = mAudioPolicyEffects->releaseInputEffects(input);
278 if(status != NO_ERROR) {
279 ALOGW("Failed to release effects on input %d", input);
Eric Laurentdce54a12014-03-10 12:19:46 -0700280 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700281}
282
283status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
284 int indexMin,
285 int indexMax)
286{
287 if (mpAudioPolicy == NULL) {
288 return NO_INIT;
289 }
290 if (!settingsAllowed()) {
291 return PERMISSION_DENIED;
292 }
293 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
294 return BAD_VALUE;
295 }
296 Mutex::Autolock _l(mLock);
297 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
298 return NO_ERROR;
299}
300
301status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
302 int index,
303 audio_devices_t device)
304{
305 if (mpAudioPolicy == NULL) {
306 return NO_INIT;
307 }
308 if (!settingsAllowed()) {
309 return PERMISSION_DENIED;
310 }
311 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
312 return BAD_VALUE;
313 }
314 Mutex::Autolock _l(mLock);
315 if (mpAudioPolicy->set_stream_volume_index_for_device) {
316 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
317 stream,
318 index,
319 device);
320 } else {
321 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
322 }
323}
324
325status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
326 int *index,
327 audio_devices_t device)
328{
329 if (mpAudioPolicy == NULL) {
330 return NO_INIT;
331 }
332 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
333 return BAD_VALUE;
334 }
335 Mutex::Autolock _l(mLock);
336 if (mpAudioPolicy->get_stream_volume_index_for_device) {
337 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
338 stream,
339 index,
340 device);
341 } else {
342 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
343 }
344}
345
346uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
347{
348 if (mpAudioPolicy == NULL) {
349 return 0;
350 }
351 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
352}
353
354//audio policy: use audio_device_t appropriately
355
356audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
357{
358 if (mpAudioPolicy == NULL) {
359 return (audio_devices_t)0;
360 }
361 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
362}
363
364audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
365{
366 // FIXME change return type to status_t, and return NO_INIT here
367 if (mpAudioPolicy == NULL) {
368 return 0;
369 }
370 Mutex::Autolock _l(mLock);
371 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
372}
373
374status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
375 audio_io_handle_t io,
376 uint32_t strategy,
377 int session,
378 int id)
379{
380 if (mpAudioPolicy == NULL) {
381 return NO_INIT;
382 }
383 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
384}
385
386status_t AudioPolicyService::unregisterEffect(int id)
387{
388 if (mpAudioPolicy == NULL) {
389 return NO_INIT;
390 }
391 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
392}
393
394status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
395{
396 if (mpAudioPolicy == NULL) {
397 return NO_INIT;
398 }
399 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
400}
401
402bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
403{
404 if (mpAudioPolicy == NULL) {
405 return 0;
406 }
407 Mutex::Autolock _l(mLock);
408 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
409}
410
411bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
412{
413 if (mpAudioPolicy == NULL) {
414 return 0;
415 }
416 Mutex::Autolock _l(mLock);
417 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
418}
419
420bool AudioPolicyService::isSourceActive(audio_source_t source) const
421{
422 if (mpAudioPolicy == NULL) {
423 return false;
424 }
425 if (mpAudioPolicy->is_source_active == 0) {
426 return false;
427 }
428 Mutex::Autolock _l(mLock);
429 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
430}
431
432status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
433 effect_descriptor_t *descriptors,
434 uint32_t *count)
435{
Eric Laurentdce54a12014-03-10 12:19:46 -0700436 if (mpAudioPolicy == NULL) {
437 *count = 0;
438 return NO_INIT;
439 }
440 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700441
bryant_liuba2b4392014-06-11 16:49:30 +0800442 return mAudioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurentdce54a12014-03-10 12:19:46 -0700443}
444
445bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
446{
447 if (mpAudioPolicy == NULL) {
448 ALOGV("mpAudioPolicy == NULL");
449 return false;
450 }
451
452 if (mpAudioPolicy->is_offload_supported == NULL) {
453 ALOGV("HAL does not implement is_offload_supported");
454 return false;
455 }
456
457 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
458}
459
Eric Laurent8670c312014-04-01 10:34:16 -0700460status_t AudioPolicyService::listAudioPorts(audio_port_role_t role __unused,
461 audio_port_type_t type __unused,
462 unsigned int *num_ports,
463 struct audio_port *ports __unused,
464 unsigned int *generation __unused)
465{
466 *num_ports = 0;
467 return INVALID_OPERATION;
468}
469
470status_t AudioPolicyService::getAudioPort(struct audio_port *port __unused)
471{
472 return INVALID_OPERATION;
473}
474
475status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch __unused,
476 audio_patch_handle_t *handle __unused)
477{
478 return INVALID_OPERATION;
479}
480
481status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle __unused)
482{
483 return INVALID_OPERATION;
484}
485
486status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
487 struct audio_patch *patches __unused,
488 unsigned int *generation __unused)
489{
490 *num_patches = 0;
491 return INVALID_OPERATION;
492}
493
494status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config __unused)
495{
496 return INVALID_OPERATION;
497}
Eric Laurentdce54a12014-03-10 12:19:46 -0700498
Eric Laurent7c5b60a2014-07-15 10:38:16 -0700499audio_io_handle_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr __unused,
500 uint32_t samplingRate,
501 audio_format_t format,
502 audio_channel_mask_t channelMask,
503 audio_output_flags_t flags,
504 const audio_offload_info_t *offloadInfo)
505{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700506 audio_stream_type_t stream = audio_attributes_to_stream_type(attr);
507
Eric Laurent7c5b60a2014-07-15 10:38:16 -0700508 return getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
509}
510
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700511status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
512 audio_io_handle_t *ioHandle,
513 audio_devices_t *device)
514{
515 return INVALID_OPERATION;
516}
517
518status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
519{
520 return INVALID_OPERATION;
521}
Eric Laurent7c5b60a2014-07-15 10:38:16 -0700522
Eric Laurentdce54a12014-03-10 12:19:46 -0700523}; // namespace android