blob: 5ef02e5811f88d148d1f74eb42f5e86308732e80 [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);
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);
bryant_liuba2b4392014-06-11 16:49:30 +0800147
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
Eric Laurentdce54a12014-03-10 12:19:46 -0700154 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);
bryant_liuba2b4392014-06-11 16:49:30 +0800175
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
Eric Laurentdce54a12014-03-10 12:19:46 -0700182 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,
Glenn Kastenb3b16602014-07-16 08:36:31 -0700205 int audioSession,
206 audio_input_flags_t flags __unused)
Eric Laurentdce54a12014-03-10 12:19:46 -0700207{
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 }
bryant_liuba2b4392014-06-11 16:49:30 +0800228
Eric Laurentdce54a12014-03-10 12:19:46 -0700229 // create audio pre processors according to input source
bryant_liuba2b4392014-06-11 16:49:30 +0800230 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);
Eric Laurentdce54a12014-03-10 12:19:46 -0700233 }
234
Eric Laurentdce54a12014-03-10 12:19:46 -0700235 return input;
236}
237
238status_t AudioPolicyService::startInput(audio_io_handle_t input)
239{
240 if (mpAudioPolicy == NULL) {
241 return NO_INIT;
242 }
243 Mutex::Autolock _l(mLock);
244
245 return mpAudioPolicy->start_input(mpAudioPolicy, input);
246}
247
248status_t AudioPolicyService::stopInput(audio_io_handle_t input)
249{
250 if (mpAudioPolicy == NULL) {
251 return NO_INIT;
252 }
253 Mutex::Autolock _l(mLock);
254
255 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
256}
257
258void AudioPolicyService::releaseInput(audio_io_handle_t input)
259{
260 if (mpAudioPolicy == NULL) {
261 return;
262 }
263 Mutex::Autolock _l(mLock);
264 mpAudioPolicy->release_input(mpAudioPolicy, input);
265
bryant_liuba2b4392014-06-11 16:49:30 +0800266 // release audio processors from the input
267 status_t status = mAudioPolicyEffects->releaseInputEffects(input);
268 if(status != NO_ERROR) {
269 ALOGW("Failed to release effects on input %d", input);
Eric Laurentdce54a12014-03-10 12:19:46 -0700270 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700271}
272
273status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
274 int indexMin,
275 int indexMax)
276{
277 if (mpAudioPolicy == NULL) {
278 return NO_INIT;
279 }
280 if (!settingsAllowed()) {
281 return PERMISSION_DENIED;
282 }
283 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
284 return BAD_VALUE;
285 }
286 Mutex::Autolock _l(mLock);
287 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
288 return NO_ERROR;
289}
290
291status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
292 int index,
293 audio_devices_t device)
294{
295 if (mpAudioPolicy == NULL) {
296 return NO_INIT;
297 }
298 if (!settingsAllowed()) {
299 return PERMISSION_DENIED;
300 }
301 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
302 return BAD_VALUE;
303 }
304 Mutex::Autolock _l(mLock);
305 if (mpAudioPolicy->set_stream_volume_index_for_device) {
306 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
307 stream,
308 index,
309 device);
310 } else {
311 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
312 }
313}
314
315status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
316 int *index,
317 audio_devices_t device)
318{
319 if (mpAudioPolicy == NULL) {
320 return NO_INIT;
321 }
322 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
323 return BAD_VALUE;
324 }
325 Mutex::Autolock _l(mLock);
326 if (mpAudioPolicy->get_stream_volume_index_for_device) {
327 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
328 stream,
329 index,
330 device);
331 } else {
332 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
333 }
334}
335
336uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
337{
338 if (mpAudioPolicy == NULL) {
339 return 0;
340 }
341 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
342}
343
344//audio policy: use audio_device_t appropriately
345
346audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
347{
348 if (mpAudioPolicy == NULL) {
349 return (audio_devices_t)0;
350 }
351 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
352}
353
354audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
355{
356 // FIXME change return type to status_t, and return NO_INIT here
357 if (mpAudioPolicy == NULL) {
358 return 0;
359 }
360 Mutex::Autolock _l(mLock);
361 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
362}
363
364status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
365 audio_io_handle_t io,
366 uint32_t strategy,
367 int session,
368 int id)
369{
370 if (mpAudioPolicy == NULL) {
371 return NO_INIT;
372 }
373 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
374}
375
376status_t AudioPolicyService::unregisterEffect(int id)
377{
378 if (mpAudioPolicy == NULL) {
379 return NO_INIT;
380 }
381 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
382}
383
384status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
385{
386 if (mpAudioPolicy == NULL) {
387 return NO_INIT;
388 }
389 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
390}
391
392bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
393{
394 if (mpAudioPolicy == NULL) {
395 return 0;
396 }
397 Mutex::Autolock _l(mLock);
398 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
399}
400
401bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
402{
403 if (mpAudioPolicy == NULL) {
404 return 0;
405 }
406 Mutex::Autolock _l(mLock);
407 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
408}
409
410bool AudioPolicyService::isSourceActive(audio_source_t source) const
411{
412 if (mpAudioPolicy == NULL) {
413 return false;
414 }
415 if (mpAudioPolicy->is_source_active == 0) {
416 return false;
417 }
418 Mutex::Autolock _l(mLock);
419 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
420}
421
422status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
423 effect_descriptor_t *descriptors,
424 uint32_t *count)
425{
Eric Laurentdce54a12014-03-10 12:19:46 -0700426 if (mpAudioPolicy == NULL) {
427 *count = 0;
428 return NO_INIT;
429 }
430 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700431
bryant_liuba2b4392014-06-11 16:49:30 +0800432 return mAudioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurentdce54a12014-03-10 12:19:46 -0700433}
434
435bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
436{
437 if (mpAudioPolicy == NULL) {
438 ALOGV("mpAudioPolicy == NULL");
439 return false;
440 }
441
442 if (mpAudioPolicy->is_offload_supported == NULL) {
443 ALOGV("HAL does not implement is_offload_supported");
444 return false;
445 }
446
447 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
448}
449
Eric Laurent8670c312014-04-01 10:34:16 -0700450status_t AudioPolicyService::listAudioPorts(audio_port_role_t role __unused,
451 audio_port_type_t type __unused,
452 unsigned int *num_ports,
453 struct audio_port *ports __unused,
454 unsigned int *generation __unused)
455{
456 *num_ports = 0;
457 return INVALID_OPERATION;
458}
459
460status_t AudioPolicyService::getAudioPort(struct audio_port *port __unused)
461{
462 return INVALID_OPERATION;
463}
464
465status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch __unused,
466 audio_patch_handle_t *handle __unused)
467{
468 return INVALID_OPERATION;
469}
470
471status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle __unused)
472{
473 return INVALID_OPERATION;
474}
475
476status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
477 struct audio_patch *patches __unused,
478 unsigned int *generation __unused)
479{
480 *num_patches = 0;
481 return INVALID_OPERATION;
482}
483
484status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config __unused)
485{
486 return INVALID_OPERATION;
487}
Eric Laurentdce54a12014-03-10 12:19:46 -0700488
Eric Laurent7c5b60a2014-07-15 10:38:16 -0700489audio_io_handle_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr __unused,
490 uint32_t samplingRate,
491 audio_format_t format,
492 audio_channel_mask_t channelMask,
493 audio_output_flags_t flags,
494 const audio_offload_info_t *offloadInfo)
495{
496 //FIXME: temporary to fix build with USE_LEGACY_AUDIO_POLICY
497 audio_stream_type_t stream = AUDIO_STREAM_MUSIC;
498 return getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
499}
500
501
Eric Laurentdce54a12014-03-10 12:19:46 -0700502}; // namespace android