audio_policy_hal.cpp revision 9746c4758b161e26eec92b1ef1ff1bf0ba0bd268
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 "qcom_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 qcom_ap_module { 37 struct audio_policy_module module; 38}; 39 40struct qcom_ap_device { 41 struct audio_policy_device device; 42}; 43 44struct qcom_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 qcom_audio_policy * to_qap(struct audio_policy *pol) 54{ 55 return reinterpret_cast<struct qcom_audio_policy *>(pol); 56} 57 58static inline const struct qcom_audio_policy * to_cqap(const struct audio_policy *pol) 59{ 60 return reinterpret_cast<const struct qcom_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 qcom_audio_policy *qap = to_qap(pol); 70 return qap->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 qcom_audio_policy *qap = to_cqap(pol); 82 return (audio_policy_dev_state_t)qap->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 qcom_audio_policy *qap = to_qap(pol); 90 qap->apm->setPhoneState(state); 91} 92 93 /* force using a specific device category for the specified usage */ 94static void ap_set_force_use(struct audio_policy *pol, 95 audio_policy_force_use_t usage, 96 audio_policy_forced_cfg_t config) 97{ 98 struct qcom_audio_policy *qap = to_qap(pol); 99 qap->apm->setForceUse((AudioSystem::force_use)usage, 100 (AudioSystem::forced_config)config); 101} 102 103 /* retreive current device category forced for a given usage */ 104static audio_policy_forced_cfg_t ap_get_force_use( 105 const struct audio_policy *pol, 106 audio_policy_force_use_t usage) 107{ 108 const struct qcom_audio_policy *qap = to_cqap(pol); 109 return (audio_policy_forced_cfg_t)qap->apm->getForceUse( 110 (AudioSystem::force_use)usage); 111} 112 113/* if can_mute is true, then audio streams that are marked ENFORCED_AUDIBLE 114 * can still be muted. */ 115static void ap_set_can_mute_enforced_audible(struct audio_policy *pol, 116 bool can_mute) 117{ 118 struct qcom_audio_policy *qap = to_qap(pol); 119 qap->apm->setSystemProperty("ro.camera.sound.forced", can_mute ? "0" : "1"); 120} 121 122static int ap_init_check(const struct audio_policy *pol) 123{ 124 const struct qcom_audio_policy *qap = to_cqap(pol); 125 return qap->apm->initCheck(); 126} 127 128#ifdef QCOM_TUNNEL_LPA_ENABLED 129static audio_io_handle_t ap_get_session(struct audio_policy *pol, 130 audio_stream_type_t stream, 131 audio_format_t format, 132 audio_policy_output_flags_t flags, 133 int sessionId, 134 uint32_t samplingRate, 135 uint32_t channels) 136{ 137 struct qcom_audio_policy *qap = to_qap(pol); 138 139 ALOGV("%s: tid %d", __func__, gettid()); 140 return qap->apm->getSession((AudioSystem::stream_type)stream, 141 format, (AudioSystem::output_flags)flags, 142 sessionId, 143 samplingRate, 144 channels); 145} 146 147static void ap_pause_session(struct audio_policy *pol, audio_io_handle_t output, 148 audio_stream_type_t stream) 149{ 150 struct qcom_audio_policy *qap = to_qap(pol); 151 qap->apm->pauseSession(output, (AudioSystem::stream_type)stream); 152} 153 154static void ap_resume_session(struct audio_policy *pol, audio_io_handle_t output, 155 audio_stream_type_t stream) 156{ 157 struct qcom_audio_policy *qap = to_qap(pol); 158 qap->apm->resumeSession(output, (AudioSystem::stream_type)stream); 159} 160 161static void ap_release_session(struct audio_policy *pol, audio_io_handle_t output) 162{ 163 struct qcom_audio_policy *qap = to_qap(pol); 164 qap->apm->releaseSession(output); 165} 166#endif 167 168static audio_io_handle_t ap_get_output(struct audio_policy *pol, 169 audio_stream_type_t stream, 170 uint32_t sampling_rate, 171 audio_format_t format, 172 uint32_t channels, 173 audio_output_flags_t flags) 174{ 175 struct qcom_audio_policy *qap = to_qap(pol); 176 177 ALOGV("%s: tid %d", __func__, gettid()); 178 return qap->apm->getOutput((AudioSystem::stream_type)stream, 179 sampling_rate, format, channels, 180 (AudioSystem::output_flags)flags); 181} 182 183static int ap_start_output(struct audio_policy *pol, audio_io_handle_t output, 184 audio_stream_type_t stream, int session) 185{ 186 struct qcom_audio_policy *qap = to_qap(pol); 187 return qap->apm->startOutput(output, (AudioSystem::stream_type)stream, 188 session); 189} 190 191static int ap_stop_output(struct audio_policy *pol, audio_io_handle_t output, 192 audio_stream_type_t stream, int session) 193{ 194 struct qcom_audio_policy *qap = to_qap(pol); 195 return qap->apm->stopOutput(output, (AudioSystem::stream_type)stream, 196 session); 197} 198 199static void ap_release_output(struct audio_policy *pol, 200 audio_io_handle_t output) 201{ 202 struct qcom_audio_policy *qap = to_qap(pol); 203 qap->apm->releaseOutput(output); 204} 205 206static audio_io_handle_t ap_get_input(struct audio_policy *pol, audio_source_t inputSource, 207 uint32_t sampling_rate, 208 audio_format_t format, 209 uint32_t channels, 210 audio_in_acoustics_t acoustics) 211{ 212 struct qcom_audio_policy *qap = to_qap(pol); 213 return qap->apm->getInput(inputSource, sampling_rate, format, channels, 214 (AudioSystem::audio_in_acoustics)acoustics); 215} 216 217static int ap_start_input(struct audio_policy *pol, audio_io_handle_t input) 218{ 219 struct qcom_audio_policy *qap = to_qap(pol); 220 return qap->apm->startInput(input); 221} 222 223static int ap_stop_input(struct audio_policy *pol, audio_io_handle_t input) 224{ 225 struct qcom_audio_policy *qap = to_qap(pol); 226 return qap->apm->stopInput(input); 227} 228 229static void ap_release_input(struct audio_policy *pol, audio_io_handle_t input) 230{ 231 struct qcom_audio_policy *qap = to_qap(pol); 232 qap->apm->releaseInput(input); 233} 234 235static void ap_init_stream_volume(struct audio_policy *pol, 236 audio_stream_type_t stream, int index_min, 237 int index_max) 238{ 239 struct qcom_audio_policy *qap = to_qap(pol); 240 qap->apm->initStreamVolume((AudioSystem::stream_type)stream, index_min, 241 index_max); 242} 243 244static int ap_set_stream_volume_index(struct audio_policy *pol, 245 audio_stream_type_t stream, 246 int index) 247{ 248 struct qcom_audio_policy *qap = to_qap(pol); 249 return qap->apm->setStreamVolumeIndex((AudioSystem::stream_type)stream, 250 index, 251 AUDIO_DEVICE_OUT_DEFAULT); 252} 253 254static int ap_get_stream_volume_index(const struct audio_policy *pol, 255 audio_stream_type_t stream, 256 int *index) 257{ 258 const struct qcom_audio_policy *qap = to_cqap(pol); 259 return qap->apm->getStreamVolumeIndex((AudioSystem::stream_type)stream, 260 index, 261 AUDIO_DEVICE_OUT_DEFAULT); 262} 263 264static uint32_t ap_get_strategy_for_stream(const struct audio_policy *pol, 265 audio_stream_type_t stream) 266{ 267 const struct qcom_audio_policy *qap = to_cqap(pol); 268 return qap->apm->getStrategyForStream((AudioSystem::stream_type)stream); 269} 270 271static int ap_set_stream_volume_index_for_device(struct audio_policy *pol, 272 audio_stream_type_t stream, 273 int index, 274 audio_devices_t device) 275{ 276 const struct qcom_audio_policy *qap = to_cqap(pol); 277 return qap->apm->setStreamVolumeIndex((AudioSystem::stream_type)stream, 278 index, 279 device); 280} 281 282static int ap_get_stream_volume_index_for_device(const struct audio_policy *pol, 283 audio_stream_type_t stream, 284 int *index, 285 audio_devices_t device) 286{ 287 const struct qcom_audio_policy *qap = to_cqap(pol); 288 return qap->apm->getStreamVolumeIndex((AudioSystem::stream_type)stream, 289 index, 290 AUDIO_DEVICE_OUT_DEFAULT); 291} 292 293static audio_devices_t ap_get_devices_for_stream(const struct audio_policy *pol, 294 audio_stream_type_t stream) 295{ 296 const struct qcom_audio_policy *qap = to_cqap(pol); 297 return qap->apm->getDevicesForStream((AudioSystem::stream_type)stream); 298} 299 300static audio_io_handle_t ap_get_output_for_effect(struct audio_policy *pol, 301 struct effect_descriptor_s *desc) 302{ 303 struct qcom_audio_policy *qap = to_qap(pol); 304 return qap->apm->getOutputForEffect(desc); 305} 306 307static int ap_register_effect(struct audio_policy *pol, 308 struct effect_descriptor_s *desc, 309 audio_io_handle_t io, 310 uint32_t strategy, 311 int session, 312 int id) 313{ 314 struct qcom_audio_policy *qap = to_qap(pol); 315 return qap->apm->registerEffect(desc, io, strategy, session, id); 316} 317 318static int ap_unregister_effect(struct audio_policy *pol, int id) 319{ 320 struct qcom_audio_policy *qap = to_qap(pol); 321 return qap->apm->unregisterEffect(id); 322} 323 324static int ap_set_effect_enabled(struct audio_policy *pol, int id, bool enabled) 325{ 326 struct qcom_audio_policy *qap = to_qap(pol); 327 return qap->apm->setEffectEnabled(id, enabled); 328} 329 330static bool ap_is_stream_active(const struct audio_policy *pol, 331 audio_stream_type_t stream, 332 uint32_t in_past_ms) 333{ 334 const struct qcom_audio_policy *qap = to_cqap(pol); 335 return qap->apm->isStreamActive(stream, in_past_ms); 336} 337 338static int ap_dump(const struct audio_policy *pol, int fd) 339{ 340 const struct qcom_audio_policy *qap = to_cqap(pol); 341 return qap->apm->dump(fd); 342} 343 344static int create_qcom_ap(const struct audio_policy_device *device, 345 struct audio_policy_service_ops *aps_ops, 346 void *service, 347 struct audio_policy **ap) 348{ 349 struct qcom_audio_policy *qap; 350 int ret; 351 352 if (!service || !aps_ops) 353 return -EINVAL; 354 355 qap = (struct qcom_audio_policy *)calloc(1, sizeof(*qap)); 356 if (!qap) 357 return -ENOMEM; 358 359 qap->policy.set_device_connection_state = ap_set_device_connection_state; 360 qap->policy.get_device_connection_state = ap_get_device_connection_state; 361 qap->policy.set_phone_state = ap_set_phone_state; 362 qap->policy.set_force_use = ap_set_force_use; 363 qap->policy.get_force_use = ap_get_force_use; 364 qap->policy.set_can_mute_enforced_audible = 365 ap_set_can_mute_enforced_audible; 366 qap->policy.init_check = ap_init_check; 367 qap->policy.get_output = ap_get_output; 368#ifdef QCOM_TUNNEL_LPA_ENABLED 369 qap->policy.get_session = ap_get_session; 370 qap->policy.pause_session = ap_pause_session; 371 qap->policy.resume_session = ap_resume_session; 372 qap->policy.release_session = ap_release_session; 373#endif 374 qap->policy.start_output = ap_start_output; 375 qap->policy.stop_output = ap_stop_output; 376 qap->policy.release_output = ap_release_output; 377 qap->policy.get_input = ap_get_input; 378 qap->policy.start_input = ap_start_input; 379 qap->policy.stop_input = ap_stop_input; 380 qap->policy.release_input = ap_release_input; 381 qap->policy.init_stream_volume = ap_init_stream_volume; 382 qap->policy.set_stream_volume_index = ap_set_stream_volume_index; 383 qap->policy.get_stream_volume_index = ap_get_stream_volume_index; 384 qap->policy.set_stream_volume_index_for_device = ap_set_stream_volume_index_for_device; 385 qap->policy.get_stream_volume_index_for_device = ap_get_stream_volume_index_for_device; 386 qap->policy.get_strategy_for_stream = ap_get_strategy_for_stream; 387 qap->policy.get_devices_for_stream = ap_get_devices_for_stream; 388 qap->policy.get_output_for_effect = ap_get_output_for_effect; 389 qap->policy.register_effect = ap_register_effect; 390 qap->policy.unregister_effect = ap_unregister_effect; 391 qap->policy.set_effect_enabled = ap_set_effect_enabled; 392 qap->policy.is_stream_active = ap_is_stream_active; 393 qap->policy.dump = ap_dump; 394 395 qap->service = service; 396 qap->aps_ops = aps_ops; 397 qap->service_client = 398 new AudioPolicyCompatClient(aps_ops, service); 399 if (!qap->service_client) { 400 ret = -ENOMEM; 401 goto err_new_compat_client; 402 } 403 404 qap->apm = createAudioPolicyManager(qap->service_client); 405 if (!qap->apm) { 406 ret = -ENOMEM; 407 goto err_create_apm; 408 } 409 410 *ap = &qap->policy; 411 return 0; 412 413err_create_apm: 414 delete qap->service_client; 415err_new_compat_client: 416 free(qap); 417 *ap = NULL; 418 return ret; 419} 420 421static int destroy_qcom_ap(const struct audio_policy_device *ap_dev, 422 struct audio_policy *ap) 423{ 424 struct qcom_audio_policy *qap = to_qap(ap); 425 426 if (!qap) 427 return 0; 428 429 if (qap->apm) 430 destroyAudioPolicyManager(qap->apm); 431 if (qap->service_client) 432 delete qap->service_client; 433 free(qap); 434 return 0; 435} 436 437static int qcom_ap_dev_close(hw_device_t* device) 438{ 439 if (device) 440 free(device); 441 return 0; 442} 443 444static int qcom_ap_dev_open(const hw_module_t* module, const char* name, 445 hw_device_t** device) 446{ 447 struct qcom_ap_device *dev; 448 449 if (strcmp(name, AUDIO_POLICY_INTERFACE) != 0) 450 return -EINVAL; 451 452 dev = (struct qcom_ap_device *)calloc(1, sizeof(*dev)); 453 if (!dev) 454 return -ENOMEM; 455 456 dev->device.common.tag = HARDWARE_DEVICE_TAG; 457 dev->device.common.version = 0; 458 dev->device.common.module = const_cast<hw_module_t*>(module); 459 dev->device.common.close = qcom_ap_dev_close; 460 dev->device.create_audio_policy = create_qcom_ap; 461 dev->device.destroy_audio_policy = destroy_qcom_ap; 462 463 *device = &dev->device.common; 464 465 return 0; 466} 467 468static struct hw_module_methods_t qcom_ap_module_methods = { 469 open: qcom_ap_dev_open 470}; 471 472struct qcom_ap_module HAL_MODULE_INFO_SYM = { 473 module: { 474 common: { 475 tag: HARDWARE_MODULE_TAG, 476 version_major: 1, 477 version_minor: 0, 478 id: AUDIO_POLICY_HARDWARE_MODULE_ID, 479 name: "QCOM Audio Policy HAL", 480 author: "Code Aurora Forum", 481 methods: &qcom_ap_module_methods, 482 dso : NULL, 483 reserved : {0}, 484 }, 485 }, 486}; 487 488}; // extern "C" 489 490}; // namespace android_audio_legacy 491