audio_policy_hal.cpp revision 9038c346d76d6f9e33be34b7760f39804553d993
1/* 2 * Copyright (C) 2011 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 "legacy_audio_policy_hal" 18//#define LOG_NDEBUG 0 19 20#include <stdint.h> 21 22#include <hardware/hardware.h> 23#include <system/audio.h> 24#include <system/audio_policy.h> 25#include <hardware/audio_policy.h> 26 27#include <hardware_legacy/AudioPolicyInterface.h> 28#include <hardware_legacy/AudioSystemLegacy.h> 29 30#include "AudioPolicyCompatClient.h" 31 32namespace android_audio_legacy { 33 34extern "C" { 35 36struct legacy_ap_module { 37 struct audio_policy_module module; 38}; 39 40struct legacy_ap_device { 41 struct audio_policy_device device; 42}; 43 44struct legacy_audio_policy { 45 struct audio_policy policy; 46 47 void *service; 48 struct audio_policy_service_ops *aps_ops; 49 AudioPolicyCompatClient *service_client; 50 AudioPolicyInterface *apm; 51}; 52 53static inline struct legacy_audio_policy * to_lap(struct audio_policy *pol) 54{ 55 return reinterpret_cast<struct legacy_audio_policy *>(pol); 56} 57 58static inline const struct legacy_audio_policy * to_clap(const struct audio_policy *pol) 59{ 60 return reinterpret_cast<const struct legacy_audio_policy *>(pol); 61} 62 63 64static int ap_set_device_connection_state(struct audio_policy *pol, 65 audio_devices_t device, 66 audio_policy_dev_state_t state, 67 const char *device_address) 68{ 69 struct legacy_audio_policy *lap = to_lap(pol); 70 return lap->apm->setDeviceConnectionState( 71 (AudioSystem::audio_devices)device, 72 (AudioSystem::device_connection_state)state, 73 device_address); 74} 75 76static audio_policy_dev_state_t ap_get_device_connection_state( 77 const struct audio_policy *pol, 78 audio_devices_t device, 79 const char *device_address) 80{ 81 const struct legacy_audio_policy *lap = to_clap(pol); 82 return (audio_policy_dev_state_t)lap->apm->getDeviceConnectionState( 83 (AudioSystem::audio_devices)device, 84 device_address); 85} 86 87static void ap_set_phone_state(struct audio_policy *pol, audio_mode_t state) 88{ 89 struct legacy_audio_policy *lap = to_lap(pol); 90 // as this is the legacy API, don't change it to use audio_mode_t instead of int 91 lap->apm->setPhoneState((int) state); 92} 93 94 /* indicate a change in ringer mode */ 95static void ap_set_ringer_mode(struct audio_policy *pol, uint32_t mode, 96 uint32_t mask) 97{ 98 struct legacy_audio_policy *lap = to_lap(pol); 99 lap->apm->setRingerMode(mode, mask); 100} 101 102 /* force using a specific device category for the specified usage */ 103static void ap_set_force_use(struct audio_policy *pol, 104 audio_policy_force_use_t usage, 105 audio_policy_forced_cfg_t config) 106{ 107 struct legacy_audio_policy *lap = to_lap(pol); 108 lap->apm->setForceUse((AudioSystem::force_use)usage, 109 (AudioSystem::forced_config)config); 110} 111 112 /* retreive current device category forced for a given usage */ 113static audio_policy_forced_cfg_t ap_get_force_use( 114 const struct audio_policy *pol, 115 audio_policy_force_use_t usage) 116{ 117 const struct legacy_audio_policy *lap = to_clap(pol); 118 return (audio_policy_forced_cfg_t)lap->apm->getForceUse( 119 (AudioSystem::force_use)usage); 120} 121 122/* if can_mute is true, then audio streams that are marked ENFORCED_AUDIBLE 123 * can still be muted. */ 124static void ap_set_can_mute_enforced_audible(struct audio_policy *pol, 125 bool can_mute) 126{ 127 struct legacy_audio_policy *lap = to_lap(pol); 128 lap->apm->setSystemProperty("ro.camera.sound.forced", can_mute ? "0" : "1"); 129} 130 131static int ap_init_check(const struct audio_policy *pol) 132{ 133 const struct legacy_audio_policy *lap = to_clap(pol); 134 return lap->apm->initCheck(); 135} 136 137static audio_io_handle_t ap_get_output(struct audio_policy *pol, 138 audio_stream_type_t stream, 139 uint32_t sampling_rate, 140 uint32_t format, 141 uint32_t channels, 142 audio_policy_output_flags_t flags) 143{ 144 struct legacy_audio_policy *lap = to_lap(pol); 145 146 ALOGV("%s: tid %d", __func__, gettid()); 147 return lap->apm->getOutput((AudioSystem::stream_type)stream, 148 sampling_rate, format, channels, 149 (AudioSystem::output_flags)flags); 150} 151 152static int ap_start_output(struct audio_policy *pol, audio_io_handle_t output, 153 audio_stream_type_t stream, int session) 154{ 155 struct legacy_audio_policy *lap = to_lap(pol); 156 return lap->apm->startOutput(output, (AudioSystem::stream_type)stream, 157 session); 158} 159 160static int ap_stop_output(struct audio_policy *pol, audio_io_handle_t output, 161 audio_stream_type_t stream, int session) 162{ 163 struct legacy_audio_policy *lap = to_lap(pol); 164 return lap->apm->stopOutput(output, (AudioSystem::stream_type)stream, 165 session); 166} 167 168static void ap_release_output(struct audio_policy *pol, 169 audio_io_handle_t output) 170{ 171 struct legacy_audio_policy *lap = to_lap(pol); 172 lap->apm->releaseOutput(output); 173} 174 175static audio_io_handle_t ap_get_input(struct audio_policy *pol, int inputSource, 176 uint32_t sampling_rate, 177 uint32_t format, 178 uint32_t channels, 179 audio_in_acoustics_t acoustics) 180{ 181 struct legacy_audio_policy *lap = to_lap(pol); 182 return lap->apm->getInput(inputSource, sampling_rate, format, channels, 183 (AudioSystem::audio_in_acoustics)acoustics); 184} 185 186static int ap_start_input(struct audio_policy *pol, audio_io_handle_t input) 187{ 188 struct legacy_audio_policy *lap = to_lap(pol); 189 return lap->apm->startInput(input); 190} 191 192static int ap_stop_input(struct audio_policy *pol, audio_io_handle_t input) 193{ 194 struct legacy_audio_policy *lap = to_lap(pol); 195 return lap->apm->stopInput(input); 196} 197 198static void ap_release_input(struct audio_policy *pol, audio_io_handle_t input) 199{ 200 struct legacy_audio_policy *lap = to_lap(pol); 201 lap->apm->releaseInput(input); 202} 203 204static void ap_init_stream_volume(struct audio_policy *pol, 205 audio_stream_type_t stream, int index_min, 206 int index_max) 207{ 208 struct legacy_audio_policy *lap = to_lap(pol); 209 lap->apm->initStreamVolume((AudioSystem::stream_type)stream, index_min, 210 index_max); 211} 212 213static int ap_set_stream_volume_index(struct audio_policy *pol, 214 audio_stream_type_t stream, 215 int index) 216{ 217 struct legacy_audio_policy *lap = to_lap(pol); 218 return lap->apm->setStreamVolumeIndex((AudioSystem::stream_type)stream, 219 index); 220} 221 222static int ap_get_stream_volume_index(const struct audio_policy *pol, 223 audio_stream_type_t stream, 224 int *index) 225{ 226 const struct legacy_audio_policy *lap = to_clap(pol); 227 return lap->apm->getStreamVolumeIndex((AudioSystem::stream_type)stream, 228 index); 229} 230 231static uint32_t ap_get_strategy_for_stream(const struct audio_policy *pol, 232 audio_stream_type_t stream) 233{ 234 const struct legacy_audio_policy *lap = to_clap(pol); 235 return lap->apm->getStrategyForStream((AudioSystem::stream_type)stream); 236} 237 238static uint32_t ap_get_devices_for_stream(const struct audio_policy *pol, 239 audio_stream_type_t stream) 240{ 241 const struct legacy_audio_policy *lap = to_clap(pol); 242 return lap->apm->getDevicesForStream((AudioSystem::stream_type)stream); 243} 244 245static audio_io_handle_t ap_get_output_for_effect(struct audio_policy *pol, 246 struct effect_descriptor_s *desc) 247{ 248 struct legacy_audio_policy *lap = to_lap(pol); 249 return lap->apm->getOutputForEffect(desc); 250} 251 252static int ap_register_effect(struct audio_policy *pol, 253 struct effect_descriptor_s *desc, 254 audio_io_handle_t io, 255 uint32_t strategy, 256 int session, 257 int id) 258{ 259 struct legacy_audio_policy *lap = to_lap(pol); 260 return lap->apm->registerEffect(desc, io, strategy, session, id); 261} 262 263static int ap_unregister_effect(struct audio_policy *pol, int id) 264{ 265 struct legacy_audio_policy *lap = to_lap(pol); 266 return lap->apm->unregisterEffect(id); 267} 268 269static int ap_set_effect_enabled(struct audio_policy *pol, int id, bool enabled) 270{ 271 struct legacy_audio_policy *lap = to_lap(pol); 272 return lap->apm->setEffectEnabled(id, enabled); 273} 274 275static bool ap_is_stream_active(const struct audio_policy *pol, audio_stream_type_t stream, 276 uint32_t in_past_ms) 277{ 278 const struct legacy_audio_policy *lap = to_clap(pol); 279 return lap->apm->isStreamActive((int) stream, in_past_ms); 280} 281 282static int ap_dump(const struct audio_policy *pol, int fd) 283{ 284 const struct legacy_audio_policy *lap = to_clap(pol); 285 return lap->apm->dump(fd); 286} 287 288static int create_legacy_ap(const struct audio_policy_device *device, 289 struct audio_policy_service_ops *aps_ops, 290 void *service, 291 struct audio_policy **ap) 292{ 293 struct legacy_audio_policy *lap; 294 int ret; 295 296 if (!service || !aps_ops) 297 return -EINVAL; 298 299 lap = (struct legacy_audio_policy *)calloc(1, sizeof(*lap)); 300 if (!lap) 301 return -ENOMEM; 302 303 lap->policy.set_device_connection_state = ap_set_device_connection_state; 304 lap->policy.get_device_connection_state = ap_get_device_connection_state; 305 lap->policy.set_phone_state = ap_set_phone_state; 306 lap->policy.set_ringer_mode = ap_set_ringer_mode; 307 lap->policy.set_force_use = ap_set_force_use; 308 lap->policy.get_force_use = ap_get_force_use; 309 lap->policy.set_can_mute_enforced_audible = 310 ap_set_can_mute_enforced_audible; 311 lap->policy.init_check = ap_init_check; 312 lap->policy.get_output = ap_get_output; 313 lap->policy.start_output = ap_start_output; 314 lap->policy.stop_output = ap_stop_output; 315 lap->policy.release_output = ap_release_output; 316 lap->policy.get_input = ap_get_input; 317 lap->policy.start_input = ap_start_input; 318 lap->policy.stop_input = ap_stop_input; 319 lap->policy.release_input = ap_release_input; 320 lap->policy.init_stream_volume = ap_init_stream_volume; 321 lap->policy.set_stream_volume_index = ap_set_stream_volume_index; 322 lap->policy.get_stream_volume_index = ap_get_stream_volume_index; 323 lap->policy.get_strategy_for_stream = ap_get_strategy_for_stream; 324 lap->policy.get_devices_for_stream = ap_get_devices_for_stream; 325 lap->policy.get_output_for_effect = ap_get_output_for_effect; 326 lap->policy.register_effect = ap_register_effect; 327 lap->policy.unregister_effect = ap_unregister_effect; 328 lap->policy.set_effect_enabled = ap_set_effect_enabled; 329 lap->policy.is_stream_active = ap_is_stream_active; 330 lap->policy.dump = ap_dump; 331 332 lap->service = service; 333 lap->aps_ops = aps_ops; 334 lap->service_client = 335 new AudioPolicyCompatClient(aps_ops, service); 336 if (!lap->service_client) { 337 ret = -ENOMEM; 338 goto err_new_compat_client; 339 } 340 341 lap->apm = createAudioPolicyManager(lap->service_client); 342 if (!lap->apm) { 343 ret = -ENOMEM; 344 goto err_create_apm; 345 } 346 347 *ap = &lap->policy; 348 return 0; 349 350err_create_apm: 351 delete lap->service_client; 352err_new_compat_client: 353 free(lap); 354 *ap = NULL; 355 return ret; 356} 357 358static int destroy_legacy_ap(const struct audio_policy_device *ap_dev, 359 struct audio_policy *ap) 360{ 361 struct legacy_audio_policy *lap = to_lap(ap); 362 363 if (!lap) 364 return 0; 365 366 if (lap->apm) 367 destroyAudioPolicyManager(lap->apm); 368 if (lap->service_client) 369 delete lap->service_client; 370 free(lap); 371 return 0; 372} 373 374static int legacy_ap_dev_close(hw_device_t* device) 375{ 376 if (device) 377 free(device); 378 return 0; 379} 380 381static int legacy_ap_dev_open(const hw_module_t* module, const char* name, 382 hw_device_t** device) 383{ 384 struct legacy_ap_device *dev; 385 386 if (strcmp(name, AUDIO_POLICY_INTERFACE) != 0) 387 return -EINVAL; 388 389 dev = (struct legacy_ap_device *)calloc(1, sizeof(*dev)); 390 if (!dev) 391 return -ENOMEM; 392 393 dev->device.common.tag = HARDWARE_DEVICE_TAG; 394 dev->device.common.version = 0; 395 dev->device.common.module = const_cast<hw_module_t*>(module); 396 dev->device.common.close = legacy_ap_dev_close; 397 dev->device.create_audio_policy = create_legacy_ap; 398 dev->device.destroy_audio_policy = destroy_legacy_ap; 399 400 *device = &dev->device.common; 401 402 return 0; 403} 404 405static struct hw_module_methods_t legacy_ap_module_methods = { 406 open: legacy_ap_dev_open 407}; 408 409struct legacy_ap_module HAL_MODULE_INFO_SYM = { 410 module: { 411 common: { 412 tag: HARDWARE_MODULE_TAG, 413 version_major: 1, 414 version_minor: 0, 415 id: AUDIO_POLICY_HARDWARE_MODULE_ID, 416 name: "LEGACY Audio Policy HAL", 417 author: "The Android Open Source Project", 418 methods: &legacy_ap_module_methods, 419 dso : NULL, 420 reserved : {0}, 421 }, 422 }, 423}; 424 425}; // extern "C" 426 427}; // namespace android_audio_legacy 428