audio_hw.c revision 33d330678f797b8796f72114bad42957b9ca204f
1/* 2 * Copyright (C) 2013 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 "audio_hw_primary" 18/*#define LOG_NDEBUG 0*/ 19#define LOG_NDDEBUG 0 20 21#include <errno.h> 22#include <pthread.h> 23#include <stdint.h> 24#include <sys/time.h> 25#include <stdlib.h> 26#include <math.h> 27 28#include <cutils/log.h> 29#include <cutils/str_parms.h> 30#include <cutils/properties.h> 31 32#include <hardware/audio_effect.h> 33#include <audio_effects/effect_aec.h> 34#include <audio_effects/effect_ns.h> 35#include "audio_hw.h" 36#include "platform_api.h" 37#include <platform.h> 38 39#define MIXER_XML_PATH "/system/etc/mixer_paths.xml" 40 41struct pcm_config pcm_config_deep_buffer = { 42 .channels = 2, 43 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 44 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, 45 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, 46 .format = PCM_FORMAT_S16_LE, 47 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 48 .stop_threshold = INT_MAX, 49 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 50}; 51 52struct pcm_config pcm_config_low_latency = { 53 .channels = 2, 54 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 55 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, 56 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, 57 .format = PCM_FORMAT_S16_LE, 58 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 59 .stop_threshold = INT_MAX, 60 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 61}; 62 63struct pcm_config pcm_config_hdmi_multi = { 64 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ 65 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ 66 .period_size = HDMI_MULTI_PERIOD_SIZE, 67 .period_count = HDMI_MULTI_PERIOD_COUNT, 68 .format = PCM_FORMAT_S16_LE, 69 .start_threshold = 0, 70 .stop_threshold = INT_MAX, 71 .avail_min = 0, 72}; 73 74struct pcm_config pcm_config_audio_capture = { 75 .channels = 2, 76 .period_count = AUDIO_CAPTURE_PERIOD_COUNT, 77 .format = PCM_FORMAT_S16_LE, 78}; 79 80struct pcm_config pcm_config_voice_call = { 81 .channels = 1, 82 .rate = 8000, 83 .period_size = 160, 84 .period_count = 2, 85 .format = PCM_FORMAT_S16_LE, 86}; 87 88static const char * const use_case_table[AUDIO_USECASE_MAX] = { 89 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", 90 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", 91 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", 92 [USECASE_AUDIO_RECORD] = "audio-record", 93 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", 94 [USECASE_VOICE_CALL] = "voice-call", 95}; 96 97 98#define STRING_TO_ENUM(string) { #string, string } 99 100struct string_to_enum { 101 const char *name; 102 uint32_t value; 103}; 104 105static const struct string_to_enum out_channels_name_to_enum_table[] = { 106 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 107 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 108 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 109}; 110 111 112static int enable_audio_route(struct audio_device *adev, 113 struct audio_usecase *usecase, 114 bool update_mixer) 115{ 116 snd_device_t snd_device; 117 char mixer_path[50]; 118 119 if (usecase == NULL) 120 return -EINVAL; 121 122 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 123 124 if (usecase->type == PCM_CAPTURE) 125 snd_device = usecase->in_snd_device; 126 else 127 snd_device = usecase->out_snd_device; 128 129 strcpy(mixer_path, use_case_table[usecase->id]); 130 platform_add_backend_name(mixer_path, snd_device); 131 ALOGD("%s: apply mixer path: %s", __func__, mixer_path); 132 audio_route_apply_path(adev->audio_route, mixer_path); 133 if (update_mixer) 134 audio_route_update_mixer(adev->audio_route); 135 136 ALOGV("%s: exit", __func__); 137 return 0; 138} 139 140static int disable_audio_route(struct audio_device *adev, 141 struct audio_usecase *usecase, 142 bool update_mixer) 143{ 144 snd_device_t snd_device; 145 char mixer_path[50]; 146 147 if (usecase == NULL) 148 return -EINVAL; 149 150 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 151 if (usecase->type == PCM_CAPTURE) 152 snd_device = usecase->in_snd_device; 153 else 154 snd_device = usecase->out_snd_device; 155 strcpy(mixer_path, use_case_table[usecase->id]); 156 platform_add_backend_name(mixer_path, snd_device); 157 ALOGD("%s: reset mixer path: %s", __func__, mixer_path); 158 audio_route_reset_path(adev->audio_route, mixer_path); 159 if (update_mixer) 160 audio_route_update_mixer(adev->audio_route); 161 162 ALOGV("%s: exit", __func__); 163 return 0; 164} 165 166static int enable_snd_device(struct audio_device *adev, 167 snd_device_t snd_device, 168 bool update_mixer) 169{ 170 if (snd_device < SND_DEVICE_MIN || 171 snd_device >= SND_DEVICE_MAX) { 172 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 173 return -EINVAL; 174 } 175 176 adev->snd_dev_ref_cnt[snd_device]++; 177 if (adev->snd_dev_ref_cnt[snd_device] > 1) { 178 ALOGD("%s: snd_device(%d: %s) is already active", 179 __func__, snd_device, platform_get_snd_device_name(snd_device)); 180 return 0; 181 } 182 183 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) { 184 adev->snd_dev_ref_cnt[snd_device]--; 185 return -EINVAL; 186 } 187 188 ALOGD("%s: snd_device(%d: %s)", __func__, 189 snd_device, platform_get_snd_device_name(snd_device)); 190 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device)); 191 if (update_mixer) 192 audio_route_update_mixer(adev->audio_route); 193 194 return 0; 195} 196 197static int disable_snd_device(struct audio_device *adev, 198 snd_device_t snd_device, 199 bool update_mixer) 200{ 201 if (snd_device < SND_DEVICE_MIN || 202 snd_device >= SND_DEVICE_MAX) { 203 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 204 return -EINVAL; 205 } 206 if (adev->snd_dev_ref_cnt[snd_device] <= 0) { 207 ALOGE("%s: device ref cnt is already 0", __func__); 208 return -EINVAL; 209 } 210 adev->snd_dev_ref_cnt[snd_device]--; 211 if (adev->snd_dev_ref_cnt[snd_device] == 0) { 212 ALOGD("%s: snd_device(%d: %s)", __func__, 213 snd_device, platform_get_snd_device_name(snd_device)); 214 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device)); 215 if (update_mixer) 216 audio_route_update_mixer(adev->audio_route); 217 } 218 return 0; 219} 220 221static void check_usecases_codec_backend(struct audio_device *adev, 222 struct audio_usecase *uc_info, 223 snd_device_t snd_device) 224{ 225 struct listnode *node; 226 struct audio_usecase *usecase; 227 bool switch_device[AUDIO_USECASE_MAX]; 228 int i, num_uc_to_switch = 0; 229 230 /* 231 * This function is to make sure that all the usecases that are active on 232 * the hardware codec backend are always routed to any one device that is 233 * handled by the hardware codec. 234 * For example, if low-latency and deep-buffer usecases are currently active 235 * on speaker and out_set_parameters(headset) is received on low-latency 236 * output, then we have to make sure deep-buffer is also switched to headset, 237 * because of the limitation that both the devices cannot be enabled 238 * at the same time as they share the same backend. 239 */ 240 /* Disable all the usecases on the shared backend other than the 241 specified usecase */ 242 for (i = 0; i < AUDIO_USECASE_MAX; i++) 243 switch_device[i] = false; 244 245 list_for_each(node, &adev->usecase_list) { 246 usecase = node_to_item(node, struct audio_usecase, list); 247 if (usecase->type != PCM_CAPTURE && 248 usecase != uc_info && 249 usecase->out_snd_device != snd_device && 250 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 251 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 252 __func__, use_case_table[usecase->id], 253 platform_get_snd_device_name(usecase->out_snd_device)); 254 disable_audio_route(adev, usecase, false); 255 switch_device[usecase->id] = true; 256 num_uc_to_switch++; 257 } 258 } 259 260 if (num_uc_to_switch) { 261 /* Make sure all the streams are de-routed before disabling the device */ 262 audio_route_update_mixer(adev->audio_route); 263 264 list_for_each(node, &adev->usecase_list) { 265 usecase = node_to_item(node, struct audio_usecase, list); 266 if (switch_device[usecase->id]) { 267 disable_snd_device(adev, usecase->out_snd_device, false); 268 enable_snd_device(adev, snd_device, false); 269 } 270 } 271 272 /* Make sure new snd device is enabled before re-routing the streams */ 273 audio_route_update_mixer(adev->audio_route); 274 275 /* Re-route all the usecases on the shared backend other than the 276 specified usecase to new snd devices */ 277 list_for_each(node, &adev->usecase_list) { 278 usecase = node_to_item(node, struct audio_usecase, list); 279 /* Update the out_snd_device only before enabling the audio route */ 280 if (switch_device[usecase->id] ) { 281 usecase->out_snd_device = snd_device; 282 enable_audio_route(adev, usecase, false); 283 } 284 } 285 286 audio_route_update_mixer(adev->audio_route); 287 } 288} 289 290static void check_and_route_capture_usecases(struct audio_device *adev, 291 struct audio_usecase *uc_info, 292 snd_device_t snd_device) 293{ 294 struct listnode *node; 295 struct audio_usecase *usecase; 296 bool switch_device[AUDIO_USECASE_MAX]; 297 int i, num_uc_to_switch = 0; 298 299 /* 300 * This function is to make sure that all the active capture usecases 301 * are always routed to the same input sound device. 302 * For example, if audio-record and voice-call usecases are currently 303 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) 304 * is received for voice call then we have to make sure that audio-record 305 * usecase is also switched to earpiece i.e. voice-dmic-ef, 306 * because of the limitation that two devices cannot be enabled 307 * at the same time if they share the same backend. 308 */ 309 for (i = 0; i < AUDIO_USECASE_MAX; i++) 310 switch_device[i] = false; 311 312 list_for_each(node, &adev->usecase_list) { 313 usecase = node_to_item(node, struct audio_usecase, list); 314 if (usecase->type != PCM_PLAYBACK && 315 usecase != uc_info && 316 usecase->in_snd_device != snd_device) { 317 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 318 __func__, use_case_table[usecase->id], 319 device_table[usecase->in_snd_device]); 320 disable_audio_route(adev, usecase, false); 321 switch_device[usecase->id] = true; 322 num_uc_to_switch++; 323 } 324 } 325 326 if (num_uc_to_switch) { 327 /* Make sure all the streams are de-routed before disabling the device */ 328 audio_route_update_mixer(adev->audio_route); 329 330 list_for_each(node, &adev->usecase_list) { 331 usecase = node_to_item(node, struct audio_usecase, list); 332 if (switch_device[usecase->id]) { 333 disable_snd_device(adev, usecase->in_snd_device, false); 334 enable_snd_device(adev, snd_device, false); 335 } 336 } 337 338 /* Make sure new snd device is enabled before re-routing the streams */ 339 audio_route_update_mixer(adev->audio_route); 340 341 /* Re-route all the usecases on the shared backend other than the 342 specified usecase to new snd devices */ 343 list_for_each(node, &adev->usecase_list) { 344 usecase = node_to_item(node, struct audio_usecase, list); 345 /* Update the in_snd_device only before enabling the audio route */ 346 if (switch_device[usecase->id] ) { 347 usecase->in_snd_device = snd_device; 348 enable_audio_route(adev, usecase, false); 349 } 350 } 351 352 audio_route_update_mixer(adev->audio_route); 353 } 354} 355 356 357/* must be called with hw device mutex locked */ 358static int read_hdmi_channel_masks(struct stream_out *out) 359{ 360 int ret = 0; 361 int channels = platform_edid_get_max_channels(); 362 363 switch (channels) { 364 /* 365 * Do not handle stereo output in Multi-channel cases 366 * Stereo case is handled in normal playback path 367 */ 368 case 6: 369 ALOGV("%s: HDMI supports 5.1", __func__); 370 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 371 break; 372 case 8: 373 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); 374 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 375 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; 376 break; 377 default: 378 ALOGE("HDMI does not support multi channel playback"); 379 ret = -ENOSYS; 380 break; 381 } 382 return ret; 383} 384 385static struct audio_usecase *get_usecase_from_list(struct audio_device *adev, 386 audio_usecase_t uc_id) 387{ 388 struct audio_usecase *usecase; 389 struct listnode *node; 390 391 list_for_each(node, &adev->usecase_list) { 392 usecase = node_to_item(node, struct audio_usecase, list); 393 if (usecase->id == uc_id) 394 return usecase; 395 } 396 return NULL; 397} 398 399static int select_devices(struct audio_device *adev, 400 audio_usecase_t uc_id) 401{ 402 snd_device_t out_snd_device = SND_DEVICE_NONE; 403 snd_device_t in_snd_device = SND_DEVICE_NONE; 404 struct audio_usecase *usecase = NULL; 405 struct audio_usecase *vc_usecase = NULL; 406 struct listnode *node; 407 int status = 0; 408 409 usecase = get_usecase_from_list(adev, uc_id); 410 if (usecase == NULL) { 411 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); 412 return -EINVAL; 413 } 414 415 if (usecase->type == VOICE_CALL) { 416 out_snd_device = platform_get_output_snd_device(adev->platform, 417 usecase->stream.out->devices); 418 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices); 419 usecase->devices = usecase->stream.out->devices; 420 } else { 421 /* 422 * If the voice call is active, use the sound devices of voice call usecase 423 * so that it would not result any device switch. All the usecases will 424 * be switched to new device when select_devices() is called for voice call 425 * usecase. This is to avoid switching devices for voice call when 426 * check_usecases_codec_backend() is called below. 427 */ 428 if (adev->in_call) { 429 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL); 430 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 431 in_snd_device = vc_usecase->in_snd_device; 432 out_snd_device = vc_usecase->out_snd_device; 433 } 434 } 435 if (usecase->type == PCM_PLAYBACK) { 436 usecase->devices = usecase->stream.out->devices; 437 in_snd_device = SND_DEVICE_NONE; 438 if (out_snd_device == SND_DEVICE_NONE) { 439 out_snd_device = platform_get_output_snd_device(adev->platform, 440 usecase->stream.out->devices); 441 if (usecase->stream.out == adev->primary_output && 442 adev->active_input && 443 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 444 select_devices(adev, adev->active_input->usecase); 445 } 446 } 447 } else if (usecase->type == PCM_CAPTURE) { 448 usecase->devices = usecase->stream.in->device; 449 out_snd_device = SND_DEVICE_NONE; 450 if (in_snd_device == SND_DEVICE_NONE) { 451 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && 452 adev->primary_output && !adev->primary_output->standby) { 453 in_snd_device = platform_get_input_snd_device(adev->platform, 454 adev->primary_output->devices); 455 } else { 456 in_snd_device = platform_get_input_snd_device(adev->platform, 457 AUDIO_DEVICE_NONE); 458 } 459 } 460 } 461 } 462 463 if (out_snd_device == usecase->out_snd_device && 464 in_snd_device == usecase->in_snd_device) { 465 return 0; 466 } 467 468 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__, 469 out_snd_device, platform_get_snd_device_name(out_snd_device), 470 in_snd_device, platform_get_snd_device_name(in_snd_device)); 471 472 /* 473 * Limitation: While in call, to do a device switch we need to disable 474 * and enable both RX and TX devices though one of them is same as current 475 * device. 476 */ 477 if (usecase->type == VOICE_CALL) { 478 status = platform_switch_voice_call_device_pre(adev->platform); 479 } 480 481 /* Disable current sound devices */ 482 if (usecase->out_snd_device != SND_DEVICE_NONE) { 483 disable_audio_route(adev, usecase, true); 484 disable_snd_device(adev, usecase->out_snd_device, false); 485 } 486 487 if (usecase->in_snd_device != SND_DEVICE_NONE) { 488 disable_audio_route(adev, usecase, true); 489 disable_snd_device(adev, usecase->in_snd_device, false); 490 } 491 492 /* Enable new sound devices */ 493 if (out_snd_device != SND_DEVICE_NONE) { 494 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) 495 check_usecases_codec_backend(adev, usecase, out_snd_device); 496 enable_snd_device(adev, out_snd_device, false); 497 } 498 499 if (in_snd_device != SND_DEVICE_NONE) { 500 check_and_route_capture_usecases(adev, usecase, in_snd_device); 501 enable_snd_device(adev, in_snd_device, false); 502 } 503 504 if (usecase->type == VOICE_CALL) 505 status = platform_switch_voice_call_device_post(adev->platform, 506 out_snd_device, 507 in_snd_device); 508 509 audio_route_update_mixer(adev->audio_route); 510 511 usecase->in_snd_device = in_snd_device; 512 usecase->out_snd_device = out_snd_device; 513 514 enable_audio_route(adev, usecase, true); 515 516 return status; 517} 518 519static int stop_input_stream(struct stream_in *in) 520{ 521 int i, ret = 0; 522 struct audio_usecase *uc_info; 523 struct audio_device *adev = in->dev; 524 525 adev->active_input = NULL; 526 527 ALOGD("%s: enter: usecase(%d: %s)", __func__, 528 in->usecase, use_case_table[in->usecase]); 529 uc_info = get_usecase_from_list(adev, in->usecase); 530 if (uc_info == NULL) { 531 ALOGE("%s: Could not find the usecase (%d) in the list", 532 __func__, in->usecase); 533 return -EINVAL; 534 } 535 536 /* 1. Disable stream specific mixer controls */ 537 disable_audio_route(adev, uc_info, true); 538 539 /* 2. Disable the tx device */ 540 disable_snd_device(adev, uc_info->in_snd_device, true); 541 542 list_remove(&uc_info->list); 543 free(uc_info); 544 545 ALOGD("%s: exit: status(%d)", __func__, ret); 546 return ret; 547} 548 549int start_input_stream(struct stream_in *in) 550{ 551 /* 1. Enable output device and stream routing controls */ 552 int ret = 0; 553 struct audio_usecase *uc_info; 554 struct audio_device *adev = in->dev; 555 556 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase); 557 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); 558 if (in->pcm_device_id < 0) { 559 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 560 __func__, in->usecase); 561 ret = -EINVAL; 562 goto error_config; 563 } 564 565 adev->active_input = in; 566 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 567 uc_info->id = in->usecase; 568 uc_info->type = PCM_CAPTURE; 569 uc_info->stream.in = in; 570 uc_info->devices = in->device; 571 uc_info->in_snd_device = SND_DEVICE_NONE; 572 uc_info->out_snd_device = SND_DEVICE_NONE; 573 574 list_add_tail(&adev->usecase_list, &uc_info->list); 575 select_devices(adev, in->usecase); 576 577 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 578 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels); 579 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id, 580 PCM_IN, &in->config); 581 if (in->pcm && !pcm_is_ready(in->pcm)) { 582 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); 583 pcm_close(in->pcm); 584 in->pcm = NULL; 585 ret = -EIO; 586 goto error_open; 587 } 588 ALOGD("%s: exit", __func__); 589 return ret; 590 591error_open: 592 stop_input_stream(in); 593 594error_config: 595 adev->active_input = NULL; 596 ALOGD("%s: exit: status(%d)", __func__, ret); 597 598 return ret; 599} 600 601static int stop_output_stream(struct stream_out *out) 602{ 603 int i, ret = 0; 604 struct audio_usecase *uc_info; 605 struct audio_device *adev = out->dev; 606 607 ALOGD("%s: enter: usecase(%d: %s)", __func__, 608 out->usecase, use_case_table[out->usecase]); 609 uc_info = get_usecase_from_list(adev, out->usecase); 610 if (uc_info == NULL) { 611 ALOGE("%s: Could not find the usecase (%d) in the list", 612 __func__, out->usecase); 613 return -EINVAL; 614 } 615 616 /* 1. Get and set stream specific mixer controls */ 617 disable_audio_route(adev, uc_info, true); 618 619 /* 2. Disable the rx device */ 620 disable_snd_device(adev, uc_info->out_snd_device, true); 621 622 list_remove(&uc_info->list); 623 free(uc_info); 624 625 ALOGD("%s: exit: status(%d)", __func__, ret); 626 return ret; 627} 628 629int start_output_stream(struct stream_out *out) 630{ 631 int ret = 0; 632 struct audio_usecase *uc_info; 633 struct audio_device *adev = out->dev; 634 635 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)", 636 __func__, out->usecase, use_case_table[out->usecase], out->devices); 637 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); 638 if (out->pcm_device_id < 0) { 639 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 640 __func__, out->pcm_device_id, out->usecase); 641 ret = -EINVAL; 642 goto error_config; 643 } 644 645 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 646 uc_info->id = out->usecase; 647 uc_info->type = PCM_PLAYBACK; 648 uc_info->stream.out = out; 649 uc_info->devices = out->devices; 650 uc_info->in_snd_device = SND_DEVICE_NONE; 651 uc_info->out_snd_device = SND_DEVICE_NONE; 652 653 list_add_tail(&adev->usecase_list, &uc_info->list); 654 655 select_devices(adev, out->usecase); 656 657 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", 658 __func__, 0, out->pcm_device_id); 659 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id, 660 PCM_OUT, &out->config); 661 if (out->pcm && !pcm_is_ready(out->pcm)) { 662 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); 663 pcm_close(out->pcm); 664 out->pcm = NULL; 665 ret = -EIO; 666 goto error_pcm_open; 667 } 668 ALOGD("%s: exit", __func__); 669 return 0; 670error_pcm_open: 671 stop_output_stream(out); 672error_config: 673 return ret; 674} 675 676static int stop_voice_call(struct audio_device *adev) 677{ 678 int i, ret = 0; 679 struct audio_usecase *uc_info; 680 681 ALOGD("%s: enter", __func__); 682 adev->in_call = false; 683 684 ret = platform_stop_voice_call(adev->platform); 685 686 /* 1. Close the PCM devices */ 687 if (adev->voice_call_rx) { 688 pcm_close(adev->voice_call_rx); 689 adev->voice_call_rx = NULL; 690 } 691 if (adev->voice_call_tx) { 692 pcm_close(adev->voice_call_tx); 693 adev->voice_call_tx = NULL; 694 } 695 696 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL); 697 if (uc_info == NULL) { 698 ALOGE("%s: Could not find the usecase (%d) in the list", 699 __func__, USECASE_VOICE_CALL); 700 return -EINVAL; 701 } 702 703 /* 2. Get and set stream specific mixer controls */ 704 disable_audio_route(adev, uc_info, true); 705 706 /* 3. Disable the rx and tx devices */ 707 disable_snd_device(adev, uc_info->out_snd_device, false); 708 disable_snd_device(adev, uc_info->in_snd_device, true); 709 710 list_remove(&uc_info->list); 711 free(uc_info); 712 713 ALOGD("%s: exit: status(%d)", __func__, ret); 714 return ret; 715} 716 717static int start_voice_call(struct audio_device *adev) 718{ 719 int i, ret = 0; 720 struct audio_usecase *uc_info; 721 int pcm_dev_rx_id, pcm_dev_tx_id; 722 723 ALOGD("%s: enter", __func__); 724 725 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 726 uc_info->id = USECASE_VOICE_CALL; 727 uc_info->type = VOICE_CALL; 728 uc_info->stream.out = adev->primary_output; 729 uc_info->devices = adev->primary_output->devices; 730 uc_info->in_snd_device = SND_DEVICE_NONE; 731 uc_info->out_snd_device = SND_DEVICE_NONE; 732 733 list_add_tail(&adev->usecase_list, &uc_info->list); 734 735 select_devices(adev, USECASE_VOICE_CALL); 736 737 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); 738 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); 739 740 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) { 741 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", 742 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); 743 ret = -EIO; 744 goto error_start_voice; 745 } 746 747 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)", 748 __func__, SOUND_CARD, pcm_dev_rx_id); 749 adev->voice_call_rx = pcm_open(SOUND_CARD, 750 pcm_dev_rx_id, 751 PCM_OUT, &pcm_config_voice_call); 752 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) { 753 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx)); 754 ret = -EIO; 755 goto error_start_voice; 756 } 757 758 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)", 759 __func__, SOUND_CARD, pcm_dev_tx_id); 760 adev->voice_call_tx = pcm_open(SOUND_CARD, 761 pcm_dev_tx_id, 762 PCM_IN, &pcm_config_voice_call); 763 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) { 764 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx)); 765 ret = -EIO; 766 goto error_start_voice; 767 } 768 pcm_start(adev->voice_call_rx); 769 pcm_start(adev->voice_call_tx); 770 771 ret = platform_start_voice_call(adev->platform); 772 if (ret < 0) { 773 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret); 774 goto error_start_voice; 775 } 776 777 adev->in_call = true; 778 return 0; 779 780error_start_voice: 781 stop_voice_call(adev); 782 783 ALOGD("%s: exit: status(%d)", __func__, ret); 784 return ret; 785} 786 787static int check_input_parameters(uint32_t sample_rate, 788 audio_format_t format, 789 int channel_count) 790{ 791 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; 792 793 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; 794 795 switch (sample_rate) { 796 case 8000: 797 case 11025: 798 case 12000: 799 case 16000: 800 case 22050: 801 case 24000: 802 case 32000: 803 case 44100: 804 case 48000: 805 break; 806 default: 807 return -EINVAL; 808 } 809 810 return 0; 811} 812 813static size_t get_input_buffer_size(uint32_t sample_rate, 814 audio_format_t format, 815 int channel_count) 816{ 817 size_t size = 0; 818 819 if (check_input_parameters(sample_rate, format, channel_count) != 0) 820 return 0; 821 822 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000; 823 /* ToDo: should use frame_size computed based on the format and 824 channel_count here. */ 825 size *= sizeof(short) * channel_count; 826 827 /* make sure the size is multiple of 64 */ 828 size += 0x3f; 829 size &= ~0x3f; 830 831 return size; 832} 833 834static uint32_t out_get_sample_rate(const struct audio_stream *stream) 835{ 836 struct stream_out *out = (struct stream_out *)stream; 837 838 return out->config.rate; 839} 840 841static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 842{ 843 return -ENOSYS; 844} 845 846static size_t out_get_buffer_size(const struct audio_stream *stream) 847{ 848 struct stream_out *out = (struct stream_out *)stream; 849 850 return out->config.period_size * audio_stream_frame_size(stream); 851} 852 853static uint32_t out_get_channels(const struct audio_stream *stream) 854{ 855 struct stream_out *out = (struct stream_out *)stream; 856 857 return out->channel_mask; 858} 859 860static audio_format_t out_get_format(const struct audio_stream *stream) 861{ 862 return AUDIO_FORMAT_PCM_16_BIT; 863} 864 865static int out_set_format(struct audio_stream *stream, audio_format_t format) 866{ 867 return -ENOSYS; 868} 869 870static int out_standby(struct audio_stream *stream) 871{ 872 struct stream_out *out = (struct stream_out *)stream; 873 struct audio_device *adev = out->dev; 874 ALOGD("%s: enter: usecase(%d: %s)", __func__, 875 out->usecase, use_case_table[out->usecase]); 876 pthread_mutex_lock(&out->lock); 877 878 if (!out->standby) { 879 out->standby = true; 880 if (out->pcm) { 881 pcm_close(out->pcm); 882 out->pcm = NULL; 883 } 884 pthread_mutex_lock(&adev->lock); 885 stop_output_stream(out); 886 pthread_mutex_unlock(&adev->lock); 887 } 888 pthread_mutex_unlock(&out->lock); 889 ALOGD("%s: exit", __func__); 890 return 0; 891} 892 893static int out_dump(const struct audio_stream *stream, int fd) 894{ 895 return 0; 896} 897 898static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 899{ 900 struct stream_out *out = (struct stream_out *)stream; 901 struct audio_device *adev = out->dev; 902 struct audio_usecase *usecase; 903 struct listnode *node; 904 struct str_parms *parms; 905 char value[32]; 906 int ret, val = 0; 907 bool select_new_device = false; 908 909 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", 910 __func__, out->usecase, use_case_table[out->usecase], kvpairs); 911 parms = str_parms_create_str(kvpairs); 912 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 913 if (ret >= 0) { 914 val = atoi(value); 915 pthread_mutex_lock(&out->lock); 916 pthread_mutex_lock(&adev->lock); 917 918 /* 919 * When HDMI cable is unplugged the music playback is paused and 920 * the policy manager sends routing=0. But the audioflinger 921 * continues to write data until standby time (3sec). 922 * As the HDMI core is turned off, the write gets blocked. 923 * Avoid this by routing audio to speaker until standby. 924 */ 925 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && 926 val == AUDIO_DEVICE_NONE) { 927 val = AUDIO_DEVICE_OUT_SPEAKER; 928 } 929 930 /* 931 * select_devices() call below switches all the usecases on the same 932 * backend to the new device. Refer to check_usecases_codec_backend() in 933 * the select_devices(). But how do we undo this? 934 * 935 * For example, music playback is active on headset (deep-buffer usecase) 936 * and if we go to ringtones and select a ringtone, low-latency usecase 937 * will be started on headset+speaker. As we can't enable headset+speaker 938 * and headset devices at the same time, select_devices() switches the music 939 * playback to headset+speaker while starting low-lateny usecase for ringtone. 940 * So when the ringtone playback is completed, how do we undo the same? 941 * 942 * We are relying on the out_set_parameters() call on deep-buffer output, 943 * once the ringtone playback is ended. 944 * NOTE: We should not check if the current devices are same as new devices. 945 * Because select_devices() must be called to switch back the music 946 * playback to headset. 947 */ 948 if (val != 0) { 949 out->devices = val; 950 951 if (!out->standby) 952 select_devices(adev, out->usecase); 953 954 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call && 955 (out == adev->primary_output)) { 956 start_voice_call(adev); 957 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call && 958 (out == adev->primary_output)) { 959 select_devices(adev, USECASE_VOICE_CALL); 960 } 961 } 962 963 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call && 964 (out == adev->primary_output)) { 965 stop_voice_call(adev); 966 } 967 968 pthread_mutex_unlock(&adev->lock); 969 pthread_mutex_unlock(&out->lock); 970 } 971 str_parms_destroy(parms); 972 ALOGD("%s: exit: code(%d)", __func__, ret); 973 return ret; 974} 975 976static char* out_get_parameters(const struct audio_stream *stream, const char *keys) 977{ 978 struct stream_out *out = (struct stream_out *)stream; 979 struct str_parms *query = str_parms_create_str(keys); 980 char *str; 981 char value[256]; 982 struct str_parms *reply = str_parms_create(); 983 size_t i, j; 984 int ret; 985 bool first = true; 986 ALOGD("%s: enter: keys - %s", __func__, keys); 987 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); 988 if (ret >= 0) { 989 value[0] = '\0'; 990 i = 0; 991 while (out->supported_channel_masks[i] != 0) { 992 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { 993 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { 994 if (!first) { 995 strcat(value, "|"); 996 } 997 strcat(value, out_channels_name_to_enum_table[j].name); 998 first = false; 999 break; 1000 } 1001 } 1002 i++; 1003 } 1004 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 1005 str = str_parms_to_str(reply); 1006 } else { 1007 str = strdup(keys); 1008 } 1009 str_parms_destroy(query); 1010 str_parms_destroy(reply); 1011 ALOGD("%s: exit: returns - %s", __func__, str); 1012 return str; 1013} 1014 1015static uint32_t out_get_latency(const struct audio_stream_out *stream) 1016{ 1017 struct stream_out *out = (struct stream_out *)stream; 1018 1019 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate); 1020} 1021 1022static int out_set_volume(struct audio_stream_out *stream, float left, 1023 float right) 1024{ 1025 struct stream_out *out = (struct stream_out *)stream; 1026 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { 1027 /* only take left channel into account: the API is for stereo anyway */ 1028 out->muted = (left == 0.0f); 1029 return 0; 1030 } 1031 return -ENOSYS; 1032} 1033 1034static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, 1035 size_t bytes) 1036{ 1037 struct stream_out *out = (struct stream_out *)stream; 1038 struct audio_device *adev = out->dev; 1039 int i, ret = -1; 1040 1041 pthread_mutex_lock(&out->lock); 1042 if (out->standby) { 1043 out->standby = false; 1044 pthread_mutex_lock(&adev->lock); 1045 ret = start_output_stream(out); 1046 pthread_mutex_unlock(&adev->lock); 1047 if (ret != 0) { 1048 out->standby = true; 1049 goto exit; 1050 } 1051 } 1052 1053 if (out->pcm) { 1054 if (out->muted) 1055 memset((void *)buffer, 0, bytes); 1056 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); 1057 ret = pcm_write(out->pcm, (void *)buffer, bytes); 1058 } 1059 1060exit: 1061 pthread_mutex_unlock(&out->lock); 1062 1063 if (ret != 0) { 1064 if (out->pcm) 1065 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm)); 1066 out_standby(&out->stream.common); 1067 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) / 1068 out_get_sample_rate(&out->stream.common)); 1069 } 1070 return bytes; 1071} 1072 1073static int out_get_render_position(const struct audio_stream_out *stream, 1074 uint32_t *dsp_frames) 1075{ 1076 return -EINVAL; 1077} 1078 1079static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1080{ 1081 return 0; 1082} 1083 1084static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1085{ 1086 return 0; 1087} 1088 1089static int out_get_next_write_timestamp(const struct audio_stream_out *stream, 1090 int64_t *timestamp) 1091{ 1092 return -EINVAL; 1093} 1094 1095/** audio_stream_in implementation **/ 1096static uint32_t in_get_sample_rate(const struct audio_stream *stream) 1097{ 1098 struct stream_in *in = (struct stream_in *)stream; 1099 1100 return in->config.rate; 1101} 1102 1103static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1104{ 1105 return -ENOSYS; 1106} 1107 1108static size_t in_get_buffer_size(const struct audio_stream *stream) 1109{ 1110 struct stream_in *in = (struct stream_in *)stream; 1111 1112 return in->config.period_size * audio_stream_frame_size(stream); 1113} 1114 1115static uint32_t in_get_channels(const struct audio_stream *stream) 1116{ 1117 struct stream_in *in = (struct stream_in *)stream; 1118 1119 return in->channel_mask; 1120} 1121 1122static audio_format_t in_get_format(const struct audio_stream *stream) 1123{ 1124 return AUDIO_FORMAT_PCM_16_BIT; 1125} 1126 1127static int in_set_format(struct audio_stream *stream, audio_format_t format) 1128{ 1129 return -ENOSYS; 1130} 1131 1132static int in_standby(struct audio_stream *stream) 1133{ 1134 struct stream_in *in = (struct stream_in *)stream; 1135 struct audio_device *adev = in->dev; 1136 int status = 0; 1137 ALOGD("%s: enter", __func__); 1138 pthread_mutex_lock(&in->lock); 1139 if (!in->standby) { 1140 in->standby = true; 1141 if (in->pcm) { 1142 pcm_close(in->pcm); 1143 in->pcm = NULL; 1144 } 1145 pthread_mutex_lock(&adev->lock); 1146 status = stop_input_stream(in); 1147 pthread_mutex_unlock(&adev->lock); 1148 } 1149 pthread_mutex_unlock(&in->lock); 1150 ALOGD("%s: exit: status(%d)", __func__, status); 1151 return status; 1152} 1153 1154static int in_dump(const struct audio_stream *stream, int fd) 1155{ 1156 return 0; 1157} 1158 1159static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 1160{ 1161 struct stream_in *in = (struct stream_in *)stream; 1162 struct audio_device *adev = in->dev; 1163 struct str_parms *parms; 1164 char *str; 1165 char value[32]; 1166 int ret, val = 0; 1167 1168 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs); 1169 parms = str_parms_create_str(kvpairs); 1170 1171 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 1172 1173 pthread_mutex_lock(&in->lock); 1174 pthread_mutex_lock(&adev->lock); 1175 if (ret >= 0) { 1176 val = atoi(value); 1177 /* no audio source uses val == 0 */ 1178 if ((in->source != val) && (val != 0)) { 1179 in->source = val; 1180 } 1181 } 1182 1183 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1184 if (ret >= 0) { 1185 val = atoi(value); 1186 if ((in->device != val) && (val != 0)) { 1187 in->device = val; 1188 /* If recording is in progress, change the tx device to new device */ 1189 if (!in->standby) 1190 ret = select_devices(adev, in->usecase); 1191 } 1192 } 1193 1194 pthread_mutex_unlock(&adev->lock); 1195 pthread_mutex_unlock(&in->lock); 1196 1197 str_parms_destroy(parms); 1198 ALOGD("%s: exit: status(%d)", __func__, ret); 1199 return ret; 1200} 1201 1202static char* in_get_parameters(const struct audio_stream *stream, 1203 const char *keys) 1204{ 1205 return strdup(""); 1206} 1207 1208static int in_set_gain(struct audio_stream_in *stream, float gain) 1209{ 1210 return 0; 1211} 1212 1213static ssize_t in_read(struct audio_stream_in *stream, void *buffer, 1214 size_t bytes) 1215{ 1216 struct stream_in *in = (struct stream_in *)stream; 1217 struct audio_device *adev = in->dev; 1218 int i, ret = -1; 1219 1220 pthread_mutex_lock(&in->lock); 1221 if (in->standby) { 1222 pthread_mutex_lock(&adev->lock); 1223 ret = start_input_stream(in); 1224 pthread_mutex_unlock(&adev->lock); 1225 if (ret != 0) { 1226 goto exit; 1227 } 1228 in->standby = 0; 1229 } 1230 1231 if (in->pcm) { 1232 ret = pcm_read(in->pcm, buffer, bytes); 1233 } 1234 1235 /* 1236 * Instead of writing zeroes here, we could trust the hardware 1237 * to always provide zeroes when muted. 1238 */ 1239 if (ret == 0 && adev->mic_mute) 1240 memset(buffer, 0, bytes); 1241 1242exit: 1243 pthread_mutex_unlock(&in->lock); 1244 1245 if (ret != 0) { 1246 in_standby(&in->stream.common); 1247 ALOGV("%s: read failed - sleeping for buffer duration", __func__); 1248 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) / 1249 in_get_sample_rate(&in->stream.common)); 1250 } 1251 return bytes; 1252} 1253 1254static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 1255{ 1256 return 0; 1257} 1258 1259static int add_remove_audio_effect(const struct audio_stream *stream, 1260 effect_handle_t effect, 1261 bool enable) 1262{ 1263 struct stream_in *in = (struct stream_in *)stream; 1264 int status = 0; 1265 effect_descriptor_t desc; 1266 1267 status = (*effect)->get_descriptor(effect, &desc); 1268 if (status != 0) 1269 return status; 1270 1271 pthread_mutex_lock(&in->lock); 1272 pthread_mutex_lock(&in->dev->lock); 1273 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && 1274 in->enable_aec != enable && 1275 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { 1276 in->enable_aec = enable; 1277 if (!in->standby) 1278 select_devices(in->dev, in->usecase); 1279 } 1280 pthread_mutex_unlock(&in->dev->lock); 1281 pthread_mutex_unlock(&in->lock); 1282 1283 return 0; 1284} 1285 1286static int in_add_audio_effect(const struct audio_stream *stream, 1287 effect_handle_t effect) 1288{ 1289 ALOGD("%s: effect %p", __func__, effect); 1290 return add_remove_audio_effect(stream, effect, true); 1291} 1292 1293static int in_remove_audio_effect(const struct audio_stream *stream, 1294 effect_handle_t effect) 1295{ 1296 ALOGD("%s: effect %p", __func__, effect); 1297 return add_remove_audio_effect(stream, effect, false); 1298} 1299 1300static int adev_open_output_stream(struct audio_hw_device *dev, 1301 audio_io_handle_t handle, 1302 audio_devices_t devices, 1303 audio_output_flags_t flags, 1304 struct audio_config *config, 1305 struct audio_stream_out **stream_out) 1306{ 1307 struct audio_device *adev = (struct audio_device *)dev; 1308 struct stream_out *out; 1309 int i, ret; 1310 1311 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", 1312 __func__, config->sample_rate, config->channel_mask, devices, flags); 1313 *stream_out = NULL; 1314 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 1315 1316 if (devices == AUDIO_DEVICE_NONE) 1317 devices = AUDIO_DEVICE_OUT_SPEAKER; 1318 1319 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; 1320 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 1321 out->flags = flags; 1322 out->devices = devices; 1323 1324 /* Init use case and pcm_config */ 1325 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && 1326 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1327 pthread_mutex_lock(&adev->lock); 1328 ret = read_hdmi_channel_masks(out); 1329 pthread_mutex_unlock(&adev->lock); 1330 if (ret != 0) { 1331 /* If HDMI does not support multi channel playback, set the default */ 1332 out->config.channels = popcount(out->channel_mask); 1333 platform_set_hdmi_channels(adev->platform, out->config.channels); 1334 goto error_open; 1335 } 1336 1337 if (config->sample_rate == 0) 1338 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 1339 if (config->channel_mask == 0) 1340 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; 1341 1342 out->channel_mask = config->channel_mask; 1343 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH; 1344 out->config = pcm_config_hdmi_multi; 1345 out->config.rate = config->sample_rate; 1346 out->config.channels = popcount(out->channel_mask); 1347 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2); 1348 platform_set_hdmi_channels(adev->platform, out->config.channels); 1349 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 1350 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; 1351 out->config = pcm_config_deep_buffer; 1352 } else { 1353 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; 1354 out->config = pcm_config_low_latency; 1355 } 1356 1357 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { 1358 if(adev->primary_output == NULL) 1359 adev->primary_output = out; 1360 else { 1361 ALOGE("%s: Primary output is already opened", __func__); 1362 ret = -EEXIST; 1363 goto error_open; 1364 } 1365 } 1366 1367 /* Check if this usecase is already existing */ 1368 pthread_mutex_lock(&adev->lock); 1369 if (get_usecase_from_list(adev, out->usecase) != NULL) { 1370 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); 1371 pthread_mutex_unlock(&adev->lock); 1372 ret = -EEXIST; 1373 goto error_open; 1374 } 1375 pthread_mutex_unlock(&adev->lock); 1376 1377 out->stream.common.get_sample_rate = out_get_sample_rate; 1378 out->stream.common.set_sample_rate = out_set_sample_rate; 1379 out->stream.common.get_buffer_size = out_get_buffer_size; 1380 out->stream.common.get_channels = out_get_channels; 1381 out->stream.common.get_format = out_get_format; 1382 out->stream.common.set_format = out_set_format; 1383 out->stream.common.standby = out_standby; 1384 out->stream.common.dump = out_dump; 1385 out->stream.common.set_parameters = out_set_parameters; 1386 out->stream.common.get_parameters = out_get_parameters; 1387 out->stream.common.add_audio_effect = out_add_audio_effect; 1388 out->stream.common.remove_audio_effect = out_remove_audio_effect; 1389 out->stream.get_latency = out_get_latency; 1390 out->stream.set_volume = out_set_volume; 1391 out->stream.write = out_write; 1392 out->stream.get_render_position = out_get_render_position; 1393 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 1394 1395 out->dev = adev; 1396 out->standby = 1; 1397 /* out->muted = false; by calloc() */ 1398 1399 config->format = out->stream.common.get_format(&out->stream.common); 1400 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 1401 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 1402 1403 *stream_out = &out->stream; 1404 ALOGD("%s: exit", __func__); 1405 return 0; 1406 1407error_open: 1408 free(out); 1409 *stream_out = NULL; 1410 ALOGD("%s: exit: ret %d", __func__, ret); 1411 return ret; 1412} 1413 1414static void adev_close_output_stream(struct audio_hw_device *dev, 1415 struct audio_stream_out *stream) 1416{ 1417 ALOGD("%s: enter", __func__); 1418 out_standby(&stream->common); 1419 free(stream); 1420 ALOGD("%s: exit", __func__); 1421} 1422 1423static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 1424{ 1425 struct audio_device *adev = (struct audio_device *)dev; 1426 struct str_parms *parms; 1427 char *str; 1428 char value[32]; 1429 int val; 1430 int ret; 1431 1432 ALOGD("%s: enter: %s", __func__, kvpairs); 1433 1434 parms = str_parms_create_str(kvpairs); 1435 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 1436 if (ret >= 0) { 1437 int tty_mode; 1438 1439 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 1440 tty_mode = TTY_MODE_OFF; 1441 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 1442 tty_mode = TTY_MODE_VCO; 1443 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 1444 tty_mode = TTY_MODE_HCO; 1445 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 1446 tty_mode = TTY_MODE_FULL; 1447 else 1448 return -EINVAL; 1449 1450 pthread_mutex_lock(&adev->lock); 1451 if (tty_mode != adev->tty_mode) { 1452 adev->tty_mode = tty_mode; 1453 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode; 1454 if (adev->in_call) 1455 select_devices(adev, USECASE_VOICE_CALL); 1456 } 1457 pthread_mutex_unlock(&adev->lock); 1458 } 1459 1460 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 1461 if (ret >= 0) { 1462 /* When set to false, HAL should disable EC and NS 1463 * But it is currently not supported. 1464 */ 1465 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 1466 adev->bluetooth_nrec = true; 1467 else 1468 adev->bluetooth_nrec = false; 1469 } 1470 1471 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 1472 if (ret >= 0) { 1473 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 1474 adev->screen_off = false; 1475 else 1476 adev->screen_off = true; 1477 } 1478 1479 ret = str_parms_get_int(parms, "rotation", &val); 1480 if (ret >= 0) { 1481 bool reverse_speakers = false; 1482 switch(val) { 1483 // FIXME: note that the code below assumes that the speakers are in the correct placement 1484 // relative to the user when the device is rotated 90deg from its default rotation. This 1485 // assumption is device-specific, not platform-specific like this code. 1486 case 270: 1487 reverse_speakers = true; 1488 break; 1489 case 0: 1490 case 90: 1491 case 180: 1492 break; 1493 default: 1494 ALOGE("%s: unexpected rotation of %d", __func__, val); 1495 } 1496 pthread_mutex_lock(&adev->lock); 1497 if (adev->speaker_lr_swap != reverse_speakers) { 1498 adev->speaker_lr_swap = reverse_speakers; 1499 // only update the selected device if there is active pcm playback 1500 struct audio_usecase *usecase; 1501 struct listnode *node; 1502 list_for_each(node, &adev->usecase_list) { 1503 usecase = node_to_item(node, struct audio_usecase, list); 1504 if (usecase->type == PCM_PLAYBACK) { 1505 select_devices(adev, usecase->id); 1506 break; 1507 } 1508 } 1509 } 1510 pthread_mutex_unlock(&adev->lock); 1511 } 1512 1513 str_parms_destroy(parms); 1514 ALOGD("%s: exit with code(%d)", __func__, ret); 1515 return ret; 1516} 1517 1518static char* adev_get_parameters(const struct audio_hw_device *dev, 1519 const char *keys) 1520{ 1521 return strdup(""); 1522} 1523 1524static int adev_init_check(const struct audio_hw_device *dev) 1525{ 1526 return 0; 1527} 1528 1529static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 1530{ 1531 struct audio_device *adev = (struct audio_device *)dev; 1532 int vol, err = 0; 1533 1534 pthread_mutex_lock(&adev->lock); 1535 adev->voice_volume = volume; 1536 if (adev->mode == AUDIO_MODE_IN_CALL) { 1537 if (volume < 0.0) { 1538 volume = 0.0; 1539 } else if (volume > 1.0) { 1540 volume = 1.0; 1541 } 1542 1543 vol = lrint(volume * 100.0); 1544 1545 // Voice volume levels from android are mapped to driver volume levels as follows. 1546 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0 1547 // So adjust the volume to get the correct volume index in driver 1548 vol = 100 - vol; 1549 1550 err = platform_set_voice_volume(adev->platform, vol); 1551 } 1552 pthread_mutex_unlock(&adev->lock); 1553 return err; 1554} 1555 1556static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 1557{ 1558 return -ENOSYS; 1559} 1560 1561static int adev_get_master_volume(struct audio_hw_device *dev, 1562 float *volume) 1563{ 1564 return -ENOSYS; 1565} 1566 1567static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) 1568{ 1569 return -ENOSYS; 1570} 1571 1572static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) 1573{ 1574 return -ENOSYS; 1575} 1576 1577static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 1578{ 1579 struct audio_device *adev = (struct audio_device *)dev; 1580 1581 pthread_mutex_lock(&adev->lock); 1582 if (adev->mode != mode) { 1583 adev->mode = mode; 1584 } 1585 pthread_mutex_unlock(&adev->lock); 1586 return 0; 1587} 1588 1589static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 1590{ 1591 struct audio_device *adev = (struct audio_device *)dev; 1592 int err = 0; 1593 1594 pthread_mutex_lock(&adev->lock); 1595 adev->mic_mute = state; 1596 1597 err = platform_set_mic_mute(adev->platform, state); 1598 pthread_mutex_unlock(&adev->lock); 1599 return err; 1600} 1601 1602static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 1603{ 1604 struct audio_device *adev = (struct audio_device *)dev; 1605 1606 *state = adev->mic_mute; 1607 1608 return 0; 1609} 1610 1611static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 1612 const struct audio_config *config) 1613{ 1614 int channel_count = popcount(config->channel_mask); 1615 1616 return get_input_buffer_size(config->sample_rate, config->format, channel_count); 1617} 1618 1619static int adev_open_input_stream(struct audio_hw_device *dev, 1620 audio_io_handle_t handle, 1621 audio_devices_t devices, 1622 struct audio_config *config, 1623 struct audio_stream_in **stream_in) 1624{ 1625 struct audio_device *adev = (struct audio_device *)dev; 1626 struct stream_in *in; 1627 int ret, buffer_size, frame_size; 1628 int channel_count = popcount(config->channel_mask); 1629 1630 ALOGD("%s: enter", __func__); 1631 *stream_in = NULL; 1632 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) 1633 return -EINVAL; 1634 1635 in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 1636 1637 in->stream.common.get_sample_rate = in_get_sample_rate; 1638 in->stream.common.set_sample_rate = in_set_sample_rate; 1639 in->stream.common.get_buffer_size = in_get_buffer_size; 1640 in->stream.common.get_channels = in_get_channels; 1641 in->stream.common.get_format = in_get_format; 1642 in->stream.common.set_format = in_set_format; 1643 in->stream.common.standby = in_standby; 1644 in->stream.common.dump = in_dump; 1645 in->stream.common.set_parameters = in_set_parameters; 1646 in->stream.common.get_parameters = in_get_parameters; 1647 in->stream.common.add_audio_effect = in_add_audio_effect; 1648 in->stream.common.remove_audio_effect = in_remove_audio_effect; 1649 in->stream.set_gain = in_set_gain; 1650 in->stream.read = in_read; 1651 in->stream.get_input_frames_lost = in_get_input_frames_lost; 1652 1653 in->device = devices; 1654 in->source = AUDIO_SOURCE_DEFAULT; 1655 in->dev = adev; 1656 in->standby = 1; 1657 in->channel_mask = config->channel_mask; 1658 1659 /* Update config params with the requested sample rate and channels */ 1660 in->usecase = USECASE_AUDIO_RECORD; 1661 in->config = pcm_config_audio_capture; 1662 in->config.channels = channel_count; 1663 in->config.rate = config->sample_rate; 1664 1665 frame_size = audio_stream_frame_size((struct audio_stream *)in); 1666 buffer_size = get_input_buffer_size(config->sample_rate, 1667 config->format, 1668 channel_count); 1669 in->config.period_size = buffer_size / frame_size; 1670 1671 *stream_in = &in->stream; 1672 ALOGD("%s: exit", __func__); 1673 return 0; 1674 1675err_open: 1676 free(in); 1677 *stream_in = NULL; 1678 return ret; 1679} 1680 1681static void adev_close_input_stream(struct audio_hw_device *dev, 1682 struct audio_stream_in *stream) 1683{ 1684 ALOGD("%s", __func__); 1685 1686 in_standby(&stream->common); 1687 free(stream); 1688 1689 return; 1690} 1691 1692static int adev_dump(const audio_hw_device_t *device, int fd) 1693{ 1694 return 0; 1695} 1696 1697static int adev_close(hw_device_t *device) 1698{ 1699 struct audio_device *adev = (struct audio_device *)device; 1700 audio_route_free(adev->audio_route); 1701 free(adev->snd_dev_ref_cnt); 1702 platform_deinit(adev->platform); 1703 free(device); 1704 return 0; 1705} 1706 1707static int adev_open(const hw_module_t *module, const char *name, 1708 hw_device_t **device) 1709{ 1710 struct audio_device *adev; 1711 int i, ret; 1712 1713 ALOGD("%s: enter", __func__); 1714 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; 1715 1716 adev = calloc(1, sizeof(struct audio_device)); 1717 1718 adev->mixer = mixer_open(MIXER_CARD); 1719 if (!adev->mixer) { 1720 ALOGE("Unable to open the mixer, aborting."); 1721 return -ENOSYS; 1722 } 1723 1724 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH); 1725 if (!adev->audio_route) { 1726 free(adev); 1727 ALOGE("%s: Failed to init audio route controls, aborting.", __func__); 1728 *device = NULL; 1729 return -EINVAL; 1730 } 1731 1732 adev->device.common.tag = HARDWARE_DEVICE_TAG; 1733 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 1734 adev->device.common.module = (struct hw_module_t *)module; 1735 adev->device.common.close = adev_close; 1736 1737 adev->device.init_check = adev_init_check; 1738 adev->device.set_voice_volume = adev_set_voice_volume; 1739 adev->device.set_master_volume = adev_set_master_volume; 1740 adev->device.get_master_volume = adev_get_master_volume; 1741 adev->device.set_master_mute = adev_set_master_mute; 1742 adev->device.get_master_mute = adev_get_master_mute; 1743 adev->device.set_mode = adev_set_mode; 1744 adev->device.set_mic_mute = adev_set_mic_mute; 1745 adev->device.get_mic_mute = adev_get_mic_mute; 1746 adev->device.set_parameters = adev_set_parameters; 1747 adev->device.get_parameters = adev_get_parameters; 1748 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 1749 adev->device.open_output_stream = adev_open_output_stream; 1750 adev->device.close_output_stream = adev_close_output_stream; 1751 adev->device.open_input_stream = adev_open_input_stream; 1752 adev->device.close_input_stream = adev_close_input_stream; 1753 adev->device.dump = adev_dump; 1754 1755 /* Set the default route before the PCM stream is opened */ 1756 pthread_mutex_lock(&adev->lock); 1757 adev->mode = AUDIO_MODE_NORMAL; 1758 adev->active_input = NULL; 1759 adev->primary_output = NULL; 1760 adev->out_device = AUDIO_DEVICE_NONE; 1761 adev->voice_call_rx = NULL; 1762 adev->voice_call_tx = NULL; 1763 adev->voice_volume = 1.0f; 1764 adev->tty_mode = TTY_MODE_OFF; 1765 adev->bluetooth_nrec = true; 1766 adev->in_call = false; 1767 adev->acdb_settings = TTY_MODE_OFF; 1768 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); 1769 list_init(&adev->usecase_list); 1770 pthread_mutex_unlock(&adev->lock); 1771 1772 /* Loads platform specific libraries dynamically */ 1773 adev->platform = platform_init(adev); 1774 if (!adev->platform) { 1775 free(adev->snd_dev_ref_cnt); 1776 free(adev); 1777 ALOGE("%s: Failed to init platform data, aborting.", __func__); 1778 *device = NULL; 1779 return -EINVAL; 1780 } 1781 *device = &adev->device.common; 1782 1783 ALOGD("%s: exit", __func__); 1784 return 0; 1785} 1786 1787static struct hw_module_methods_t hal_module_methods = { 1788 .open = adev_open, 1789}; 1790 1791struct audio_module HAL_MODULE_INFO_SYM = { 1792 .common = { 1793 .tag = HARDWARE_MODULE_TAG, 1794 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 1795 .hal_api_version = HARDWARE_HAL_API_VERSION, 1796 .id = AUDIO_HARDWARE_MODULE_ID, 1797 .name = "QCOM Audio HAL", 1798 .author = "Code Aurora Forum", 1799 .methods = &hal_module_methods, 1800 }, 1801}; 1802