audio_hw.c revision 03f09436bb527c26750659d702713ee16bbe75bf
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 VERY_VERY_VERBOSE_LOGGING*/ 20#ifdef VERY_VERY_VERBOSE_LOGGING 21#define ALOGVV ALOGV 22#else 23#define ALOGVV(a...) do { } while(0) 24#endif 25 26#include <errno.h> 27#include <pthread.h> 28#include <stdint.h> 29#include <sys/time.h> 30#include <stdlib.h> 31#include <math.h> 32#include <dlfcn.h> 33#include <sys/resource.h> 34#include <sys/prctl.h> 35 36#include <cutils/log.h> 37#include <cutils/str_parms.h> 38#include <cutils/properties.h> 39#include <cutils/atomic.h> 40#include <cutils/sched_policy.h> 41 42#include <hardware/audio_effect.h> 43#include <system/thread_defs.h> 44#include <audio_effects/effect_aec.h> 45#include <audio_effects/effect_ns.h> 46#include "audio_hw.h" 47#include "platform_api.h" 48#include <platform.h> 49 50#include "sound/compress_params.h" 51 52#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024) 53#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4 54/* ToDo: Check and update a proper value in msec */ 55#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 56#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 57 58struct pcm_config pcm_config_deep_buffer = { 59 .channels = 2, 60 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 61 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, 62 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, 63 .format = PCM_FORMAT_S16_LE, 64 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 65 .stop_threshold = INT_MAX, 66 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 67}; 68 69struct pcm_config pcm_config_low_latency = { 70 .channels = 2, 71 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 72 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, 73 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, 74 .format = PCM_FORMAT_S16_LE, 75 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 76 .stop_threshold = INT_MAX, 77 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 78}; 79 80struct pcm_config pcm_config_hdmi_multi = { 81 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ 82 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ 83 .period_size = HDMI_MULTI_PERIOD_SIZE, 84 .period_count = HDMI_MULTI_PERIOD_COUNT, 85 .format = PCM_FORMAT_S16_LE, 86 .start_threshold = 0, 87 .stop_threshold = INT_MAX, 88 .avail_min = 0, 89}; 90 91struct pcm_config pcm_config_audio_capture = { 92 .channels = 2, 93 .period_count = AUDIO_CAPTURE_PERIOD_COUNT, 94 .format = PCM_FORMAT_S16_LE, 95}; 96 97struct pcm_config pcm_config_voice_call = { 98 .channels = 1, 99 .rate = 8000, 100 .period_size = 160, 101 .period_count = 2, 102 .format = PCM_FORMAT_S16_LE, 103}; 104 105static const char * const use_case_table[AUDIO_USECASE_MAX] = { 106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", 107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", 108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", 109 [USECASE_AUDIO_RECORD] = "audio-record", 110 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", 111 [USECASE_VOICE_CALL] = "voice-call", 112 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", 113}; 114 115 116#define STRING_TO_ENUM(string) { #string, string } 117 118struct string_to_enum { 119 const char *name; 120 uint32_t value; 121}; 122 123static const struct string_to_enum out_channels_name_to_enum_table[] = { 124 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 125 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 126 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 127}; 128 129static int set_voice_volume_l(struct audio_device *adev, float volume); 130 131static bool is_supported_format(audio_format_t format) 132{ 133 if (format == AUDIO_FORMAT_MP3 || 134 format == AUDIO_FORMAT_AAC) 135 return true; 136 137 return false; 138} 139 140static int get_snd_codec_id(audio_format_t format) 141{ 142 int id = 0; 143 144 switch (format) { 145 case AUDIO_FORMAT_MP3: 146 id = SND_AUDIOCODEC_MP3; 147 break; 148 case AUDIO_FORMAT_AAC: 149 id = SND_AUDIOCODEC_AAC; 150 break; 151 default: 152 ALOGE("%s: Unsupported audio format", __func__); 153 } 154 155 return id; 156} 157 158static int enable_audio_route(struct audio_device *adev, 159 struct audio_usecase *usecase, 160 bool update_mixer) 161{ 162 snd_device_t snd_device; 163 char mixer_path[50]; 164 165 if (usecase == NULL) 166 return -EINVAL; 167 168 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 169 170 if (usecase->type == PCM_CAPTURE) 171 snd_device = usecase->in_snd_device; 172 else 173 snd_device = usecase->out_snd_device; 174 175 strcpy(mixer_path, use_case_table[usecase->id]); 176 platform_add_backend_name(mixer_path, snd_device); 177 ALOGV("%s: apply mixer path: %s", __func__, mixer_path); 178 audio_route_apply_path(adev->audio_route, mixer_path); 179 if (update_mixer) 180 audio_route_update_mixer(adev->audio_route); 181 182 ALOGV("%s: exit", __func__); 183 return 0; 184} 185 186static int disable_audio_route(struct audio_device *adev, 187 struct audio_usecase *usecase, 188 bool update_mixer) 189{ 190 snd_device_t snd_device; 191 char mixer_path[50]; 192 193 if (usecase == NULL) 194 return -EINVAL; 195 196 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 197 if (usecase->type == PCM_CAPTURE) 198 snd_device = usecase->in_snd_device; 199 else 200 snd_device = usecase->out_snd_device; 201 strcpy(mixer_path, use_case_table[usecase->id]); 202 platform_add_backend_name(mixer_path, snd_device); 203 ALOGV("%s: reset mixer path: %s", __func__, mixer_path); 204 audio_route_reset_path(adev->audio_route, mixer_path); 205 if (update_mixer) 206 audio_route_update_mixer(adev->audio_route); 207 208 ALOGV("%s: exit", __func__); 209 return 0; 210} 211 212static int enable_snd_device(struct audio_device *adev, 213 snd_device_t snd_device, 214 bool update_mixer) 215{ 216 if (snd_device < SND_DEVICE_MIN || 217 snd_device >= SND_DEVICE_MAX) { 218 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 219 return -EINVAL; 220 } 221 222 adev->snd_dev_ref_cnt[snd_device]++; 223 if (adev->snd_dev_ref_cnt[snd_device] > 1) { 224 ALOGV("%s: snd_device(%d: %s) is already active", 225 __func__, snd_device, platform_get_snd_device_name(snd_device)); 226 return 0; 227 } 228 229 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) { 230 adev->snd_dev_ref_cnt[snd_device]--; 231 return -EINVAL; 232 } 233 234 ALOGV("%s: snd_device(%d: %s)", __func__, 235 snd_device, platform_get_snd_device_name(snd_device)); 236 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device)); 237 if (update_mixer) 238 audio_route_update_mixer(adev->audio_route); 239 240 return 0; 241} 242 243static int disable_snd_device(struct audio_device *adev, 244 snd_device_t snd_device, 245 bool update_mixer) 246{ 247 if (snd_device < SND_DEVICE_MIN || 248 snd_device >= SND_DEVICE_MAX) { 249 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 250 return -EINVAL; 251 } 252 if (adev->snd_dev_ref_cnt[snd_device] <= 0) { 253 ALOGE("%s: device ref cnt is already 0", __func__); 254 return -EINVAL; 255 } 256 adev->snd_dev_ref_cnt[snd_device]--; 257 if (adev->snd_dev_ref_cnt[snd_device] == 0) { 258 ALOGV("%s: snd_device(%d: %s)", __func__, 259 snd_device, platform_get_snd_device_name(snd_device)); 260 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device)); 261 if (update_mixer) 262 audio_route_update_mixer(adev->audio_route); 263 } 264 return 0; 265} 266 267static void check_usecases_codec_backend(struct audio_device *adev, 268 struct audio_usecase *uc_info, 269 snd_device_t snd_device) 270{ 271 struct listnode *node; 272 struct audio_usecase *usecase; 273 bool switch_device[AUDIO_USECASE_MAX]; 274 int i, num_uc_to_switch = 0; 275 276 /* 277 * This function is to make sure that all the usecases that are active on 278 * the hardware codec backend are always routed to any one device that is 279 * handled by the hardware codec. 280 * For example, if low-latency and deep-buffer usecases are currently active 281 * on speaker and out_set_parameters(headset) is received on low-latency 282 * output, then we have to make sure deep-buffer is also switched to headset, 283 * because of the limitation that both the devices cannot be enabled 284 * at the same time as they share the same backend. 285 */ 286 /* Disable all the usecases on the shared backend other than the 287 specified usecase */ 288 for (i = 0; i < AUDIO_USECASE_MAX; i++) 289 switch_device[i] = false; 290 291 list_for_each(node, &adev->usecase_list) { 292 usecase = node_to_item(node, struct audio_usecase, list); 293 if (usecase->type != PCM_CAPTURE && 294 usecase != uc_info && 295 usecase->out_snd_device != snd_device && 296 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 297 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 298 __func__, use_case_table[usecase->id], 299 platform_get_snd_device_name(usecase->out_snd_device)); 300 disable_audio_route(adev, usecase, false); 301 switch_device[usecase->id] = true; 302 num_uc_to_switch++; 303 } 304 } 305 306 if (num_uc_to_switch) { 307 /* Make sure all the streams are de-routed before disabling the device */ 308 audio_route_update_mixer(adev->audio_route); 309 310 list_for_each(node, &adev->usecase_list) { 311 usecase = node_to_item(node, struct audio_usecase, list); 312 if (switch_device[usecase->id]) { 313 disable_snd_device(adev, usecase->out_snd_device, false); 314 } 315 } 316 317 list_for_each(node, &adev->usecase_list) { 318 usecase = node_to_item(node, struct audio_usecase, list); 319 if (switch_device[usecase->id]) { 320 enable_snd_device(adev, snd_device, false); 321 } 322 } 323 324 /* Make sure new snd device is enabled before re-routing the streams */ 325 audio_route_update_mixer(adev->audio_route); 326 327 /* Re-route all the usecases on the shared backend other than the 328 specified usecase to new snd devices */ 329 list_for_each(node, &adev->usecase_list) { 330 usecase = node_to_item(node, struct audio_usecase, list); 331 /* Update the out_snd_device only before enabling the audio route */ 332 if (switch_device[usecase->id] ) { 333 usecase->out_snd_device = snd_device; 334 enable_audio_route(adev, usecase, false); 335 } 336 } 337 338 audio_route_update_mixer(adev->audio_route); 339 } 340} 341 342static void check_and_route_capture_usecases(struct audio_device *adev, 343 struct audio_usecase *uc_info, 344 snd_device_t snd_device) 345{ 346 struct listnode *node; 347 struct audio_usecase *usecase; 348 bool switch_device[AUDIO_USECASE_MAX]; 349 int i, num_uc_to_switch = 0; 350 351 /* 352 * This function is to make sure that all the active capture usecases 353 * are always routed to the same input sound device. 354 * For example, if audio-record and voice-call usecases are currently 355 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) 356 * is received for voice call then we have to make sure that audio-record 357 * usecase is also switched to earpiece i.e. voice-dmic-ef, 358 * because of the limitation that two devices cannot be enabled 359 * at the same time if they share the same backend. 360 */ 361 for (i = 0; i < AUDIO_USECASE_MAX; i++) 362 switch_device[i] = false; 363 364 list_for_each(node, &adev->usecase_list) { 365 usecase = node_to_item(node, struct audio_usecase, list); 366 if (usecase->type != PCM_PLAYBACK && 367 usecase != uc_info && 368 usecase->in_snd_device != snd_device) { 369 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 370 __func__, use_case_table[usecase->id], 371 platform_get_snd_device_name(usecase->in_snd_device)); 372 disable_audio_route(adev, usecase, false); 373 switch_device[usecase->id] = true; 374 num_uc_to_switch++; 375 } 376 } 377 378 if (num_uc_to_switch) { 379 /* Make sure all the streams are de-routed before disabling the device */ 380 audio_route_update_mixer(adev->audio_route); 381 382 list_for_each(node, &adev->usecase_list) { 383 usecase = node_to_item(node, struct audio_usecase, list); 384 if (switch_device[usecase->id]) { 385 disable_snd_device(adev, usecase->in_snd_device, false); 386 enable_snd_device(adev, snd_device, false); 387 } 388 } 389 390 /* Make sure new snd device is enabled before re-routing the streams */ 391 audio_route_update_mixer(adev->audio_route); 392 393 /* Re-route all the usecases on the shared backend other than the 394 specified usecase to new snd devices */ 395 list_for_each(node, &adev->usecase_list) { 396 usecase = node_to_item(node, struct audio_usecase, list); 397 /* Update the in_snd_device only before enabling the audio route */ 398 if (switch_device[usecase->id] ) { 399 usecase->in_snd_device = snd_device; 400 enable_audio_route(adev, usecase, false); 401 } 402 } 403 404 audio_route_update_mixer(adev->audio_route); 405 } 406} 407 408 409/* must be called with hw device mutex locked */ 410static int read_hdmi_channel_masks(struct stream_out *out) 411{ 412 int ret = 0; 413 int channels = platform_edid_get_max_channels(out->dev->platform); 414 415 switch (channels) { 416 /* 417 * Do not handle stereo output in Multi-channel cases 418 * Stereo case is handled in normal playback path 419 */ 420 case 6: 421 ALOGV("%s: HDMI supports 5.1", __func__); 422 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 423 break; 424 case 8: 425 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); 426 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 427 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; 428 break; 429 default: 430 ALOGE("HDMI does not support multi channel playback"); 431 ret = -ENOSYS; 432 break; 433 } 434 return ret; 435} 436 437static struct audio_usecase *get_usecase_from_list(struct audio_device *adev, 438 audio_usecase_t uc_id) 439{ 440 struct audio_usecase *usecase; 441 struct listnode *node; 442 443 list_for_each(node, &adev->usecase_list) { 444 usecase = node_to_item(node, struct audio_usecase, list); 445 if (usecase->id == uc_id) 446 return usecase; 447 } 448 return NULL; 449} 450 451static int select_devices(struct audio_device *adev, 452 audio_usecase_t uc_id) 453{ 454 snd_device_t out_snd_device = SND_DEVICE_NONE; 455 snd_device_t in_snd_device = SND_DEVICE_NONE; 456 struct audio_usecase *usecase = NULL; 457 struct audio_usecase *vc_usecase = NULL; 458 struct listnode *node; 459 int status = 0; 460 461 usecase = get_usecase_from_list(adev, uc_id); 462 if (usecase == NULL) { 463 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); 464 return -EINVAL; 465 } 466 467 if (usecase->type == VOICE_CALL) { 468 out_snd_device = platform_get_output_snd_device(adev->platform, 469 usecase->stream.out->devices); 470 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices); 471 usecase->devices = usecase->stream.out->devices; 472 } else { 473 /* 474 * If the voice call is active, use the sound devices of voice call usecase 475 * so that it would not result any device switch. All the usecases will 476 * be switched to new device when select_devices() is called for voice call 477 * usecase. This is to avoid switching devices for voice call when 478 * check_usecases_codec_backend() is called below. 479 */ 480 if (adev->in_call) { 481 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL); 482 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 483 in_snd_device = vc_usecase->in_snd_device; 484 out_snd_device = vc_usecase->out_snd_device; 485 } 486 } 487 if (usecase->type == PCM_PLAYBACK) { 488 usecase->devices = usecase->stream.out->devices; 489 in_snd_device = SND_DEVICE_NONE; 490 if (out_snd_device == SND_DEVICE_NONE) { 491 out_snd_device = platform_get_output_snd_device(adev->platform, 492 usecase->stream.out->devices); 493 if (usecase->stream.out == adev->primary_output && 494 adev->active_input && 495 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 496 select_devices(adev, adev->active_input->usecase); 497 } 498 } 499 } else if (usecase->type == PCM_CAPTURE) { 500 usecase->devices = usecase->stream.in->device; 501 out_snd_device = SND_DEVICE_NONE; 502 if (in_snd_device == SND_DEVICE_NONE) { 503 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && 504 adev->primary_output && !adev->primary_output->standby) { 505 in_snd_device = platform_get_input_snd_device(adev->platform, 506 adev->primary_output->devices); 507 } else { 508 in_snd_device = platform_get_input_snd_device(adev->platform, 509 AUDIO_DEVICE_NONE); 510 } 511 } 512 } 513 } 514 515 if (out_snd_device == usecase->out_snd_device && 516 in_snd_device == usecase->in_snd_device) { 517 return 0; 518 } 519 520 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__, 521 out_snd_device, platform_get_snd_device_name(out_snd_device), 522 in_snd_device, platform_get_snd_device_name(in_snd_device)); 523 524 /* 525 * Limitation: While in call, to do a device switch we need to disable 526 * and enable both RX and TX devices though one of them is same as current 527 * device. 528 */ 529 if (usecase->type == VOICE_CALL) { 530 status = platform_switch_voice_call_device_pre(adev->platform); 531 } 532 533 /* Disable current sound devices */ 534 if (usecase->out_snd_device != SND_DEVICE_NONE) { 535 disable_audio_route(adev, usecase, true); 536 disable_snd_device(adev, usecase->out_snd_device, false); 537 } 538 539 if (usecase->in_snd_device != SND_DEVICE_NONE) { 540 disable_audio_route(adev, usecase, true); 541 disable_snd_device(adev, usecase->in_snd_device, false); 542 } 543 544 /* Enable new sound devices */ 545 if (out_snd_device != SND_DEVICE_NONE) { 546 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) 547 check_usecases_codec_backend(adev, usecase, out_snd_device); 548 enable_snd_device(adev, out_snd_device, false); 549 } 550 551 if (in_snd_device != SND_DEVICE_NONE) { 552 check_and_route_capture_usecases(adev, usecase, in_snd_device); 553 enable_snd_device(adev, in_snd_device, false); 554 } 555 556 if (usecase->type == VOICE_CALL) 557 status = platform_switch_voice_call_device_post(adev->platform, 558 out_snd_device, 559 in_snd_device); 560 561 audio_route_update_mixer(adev->audio_route); 562 563 usecase->in_snd_device = in_snd_device; 564 usecase->out_snd_device = out_snd_device; 565 566 enable_audio_route(adev, usecase, true); 567 568 return status; 569} 570 571static int stop_input_stream(struct stream_in *in) 572{ 573 int i, ret = 0; 574 struct audio_usecase *uc_info; 575 struct audio_device *adev = in->dev; 576 577 adev->active_input = NULL; 578 579 ALOGV("%s: enter: usecase(%d: %s)", __func__, 580 in->usecase, use_case_table[in->usecase]); 581 uc_info = get_usecase_from_list(adev, in->usecase); 582 if (uc_info == NULL) { 583 ALOGE("%s: Could not find the usecase (%d) in the list", 584 __func__, in->usecase); 585 return -EINVAL; 586 } 587 588 /* 1. Disable stream specific mixer controls */ 589 disable_audio_route(adev, uc_info, true); 590 591 /* 2. Disable the tx device */ 592 disable_snd_device(adev, uc_info->in_snd_device, true); 593 594 list_remove(&uc_info->list); 595 free(uc_info); 596 597 ALOGV("%s: exit: status(%d)", __func__, ret); 598 return ret; 599} 600 601int start_input_stream(struct stream_in *in) 602{ 603 /* 1. Enable output device and stream routing controls */ 604 int ret = 0; 605 struct audio_usecase *uc_info; 606 struct audio_device *adev = in->dev; 607 608 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); 609 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); 610 if (in->pcm_device_id < 0) { 611 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 612 __func__, in->usecase); 613 ret = -EINVAL; 614 goto error_config; 615 } 616 617 adev->active_input = in; 618 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 619 uc_info->id = in->usecase; 620 uc_info->type = PCM_CAPTURE; 621 uc_info->stream.in = in; 622 uc_info->devices = in->device; 623 uc_info->in_snd_device = SND_DEVICE_NONE; 624 uc_info->out_snd_device = SND_DEVICE_NONE; 625 626 list_add_tail(&adev->usecase_list, &uc_info->list); 627 select_devices(adev, in->usecase); 628 629 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 630 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels); 631 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id, 632 PCM_IN, &in->config); 633 if (in->pcm && !pcm_is_ready(in->pcm)) { 634 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); 635 pcm_close(in->pcm); 636 in->pcm = NULL; 637 ret = -EIO; 638 goto error_open; 639 } 640 ALOGV("%s: exit", __func__); 641 return ret; 642 643error_open: 644 stop_input_stream(in); 645 646error_config: 647 adev->active_input = NULL; 648 ALOGD("%s: exit: status(%d)", __func__, ret); 649 650 return ret; 651} 652 653/* must be called with out->lock locked */ 654static int send_offload_cmd_l(struct stream_out* out, int command) 655{ 656 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); 657 658 ALOGVV("%s %d", __func__, command); 659 660 cmd->cmd = command; 661 list_add_tail(&out->offload_cmd_list, &cmd->node); 662 pthread_cond_signal(&out->offload_cond); 663 return 0; 664} 665 666/* must be called iwth out->lock locked */ 667static void stop_compressed_output_l(struct stream_out *out) 668{ 669 out->offload_state = OFFLOAD_STATE_IDLE; 670 out->playback_started = 0; 671 out->send_new_metadata = 1; 672 if (out->compr != NULL) { 673 compress_stop(out->compr); 674 while (out->offload_thread_blocked) { 675 pthread_cond_wait(&out->cond, &out->lock); 676 } 677 } 678} 679 680static void *offload_thread_loop(void *context) 681{ 682 struct stream_out *out = (struct stream_out *) context; 683 struct listnode *item; 684 685 out->offload_state = OFFLOAD_STATE_IDLE; 686 out->playback_started = 0; 687 688 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); 689 set_sched_policy(0, SP_FOREGROUND); 690 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); 691 692 ALOGV("%s", __func__); 693 pthread_mutex_lock(&out->lock); 694 for (;;) { 695 struct offload_cmd *cmd = NULL; 696 stream_callback_event_t event; 697 bool send_callback = false; 698 699 ALOGVV("%s offload_cmd_list %d out->offload_state %d", 700 __func__, list_empty(&out->offload_cmd_list), 701 out->offload_state); 702 if (list_empty(&out->offload_cmd_list)) { 703 ALOGV("%s SLEEPING", __func__); 704 pthread_cond_wait(&out->offload_cond, &out->lock); 705 ALOGV("%s RUNNING", __func__); 706 continue; 707 } 708 709 item = list_head(&out->offload_cmd_list); 710 cmd = node_to_item(item, struct offload_cmd, node); 711 list_remove(item); 712 713 ALOGVV("%s STATE %d CMD %d out->compr %p", 714 __func__, out->offload_state, cmd->cmd, out->compr); 715 716 if (cmd->cmd == OFFLOAD_CMD_EXIT) { 717 free(cmd); 718 break; 719 } 720 721 if (out->compr == NULL) { 722 ALOGE("%s: Compress handle is NULL", __func__); 723 pthread_cond_signal(&out->cond); 724 continue; 725 } 726 out->offload_thread_blocked = true; 727 pthread_mutex_unlock(&out->lock); 728 send_callback = false; 729 switch(cmd->cmd) { 730 case OFFLOAD_CMD_WAIT_FOR_BUFFER: 731 compress_wait(out->compr, -1); 732 send_callback = true; 733 event = STREAM_CBK_EVENT_WRITE_READY; 734 break; 735 case OFFLOAD_CMD_PARTIAL_DRAIN: 736 compress_next_track(out->compr); 737 compress_partial_drain(out->compr); 738 send_callback = true; 739 event = STREAM_CBK_EVENT_DRAIN_READY; 740 break; 741 case OFFLOAD_CMD_DRAIN: 742 compress_drain(out->compr); 743 send_callback = true; 744 event = STREAM_CBK_EVENT_DRAIN_READY; 745 break; 746 default: 747 ALOGE("%s unknown command received: %d", __func__, cmd->cmd); 748 break; 749 } 750 pthread_mutex_lock(&out->lock); 751 out->offload_thread_blocked = false; 752 pthread_cond_signal(&out->cond); 753 if (send_callback) { 754 out->offload_callback(event, NULL, out->offload_cookie); 755 } 756 free(cmd); 757 } 758 759 pthread_cond_signal(&out->cond); 760 while (!list_empty(&out->offload_cmd_list)) { 761 item = list_head(&out->offload_cmd_list); 762 list_remove(item); 763 free(node_to_item(item, struct offload_cmd, node)); 764 } 765 pthread_mutex_unlock(&out->lock); 766 767 return NULL; 768} 769 770static int create_offload_callback_thread(struct stream_out *out) 771{ 772 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); 773 list_init(&out->offload_cmd_list); 774 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, 775 offload_thread_loop, out); 776 return 0; 777} 778 779static int destroy_offload_callback_thread(struct stream_out *out) 780{ 781 pthread_mutex_lock(&out->lock); 782 stop_compressed_output_l(out); 783 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); 784 785 pthread_mutex_unlock(&out->lock); 786 pthread_join(out->offload_thread, (void **) NULL); 787 pthread_cond_destroy(&out->offload_cond); 788 789 return 0; 790} 791 792static bool allow_hdmi_channel_config(struct audio_device *adev) 793{ 794 struct listnode *node; 795 struct audio_usecase *usecase; 796 bool ret = true; 797 798 list_for_each(node, &adev->usecase_list) { 799 usecase = node_to_item(node, struct audio_usecase, list); 800 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 801 /* 802 * If voice call is already existing, do not proceed further to avoid 803 * disabling/enabling both RX and TX devices, CSD calls, etc. 804 * Once the voice call done, the HDMI channels can be configured to 805 * max channels of remaining use cases. 806 */ 807 if (usecase->id == USECASE_VOICE_CALL) { 808 ALOGD("%s: voice call is active, no change in HDMI channels", 809 __func__); 810 ret = false; 811 break; 812 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) { 813 ALOGD("%s: multi channel playback is active, " 814 "no change in HDMI channels", __func__); 815 ret = false; 816 break; 817 } 818 } 819 } 820 return ret; 821} 822 823static int check_and_set_hdmi_channels(struct audio_device *adev, 824 unsigned int channels) 825{ 826 struct listnode *node; 827 struct audio_usecase *usecase; 828 829 /* Check if change in HDMI channel config is allowed */ 830 if (!allow_hdmi_channel_config(adev)) 831 return 0; 832 833 if (channels == adev->cur_hdmi_channels) { 834 ALOGD("%s: Requested channels are same as current", __func__); 835 return 0; 836 } 837 838 platform_set_hdmi_channels(adev->platform, channels); 839 adev->cur_hdmi_channels = channels; 840 841 /* 842 * Deroute all the playback streams routed to HDMI so that 843 * the back end is deactivated. Note that backend will not 844 * be deactivated if any one stream is connected to it. 845 */ 846 list_for_each(node, &adev->usecase_list) { 847 usecase = node_to_item(node, struct audio_usecase, list); 848 if (usecase->type == PCM_PLAYBACK && 849 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 850 disable_audio_route(adev, usecase, true); 851 } 852 } 853 854 /* 855 * Enable all the streams disabled above. Now the HDMI backend 856 * will be activated with new channel configuration 857 */ 858 list_for_each(node, &adev->usecase_list) { 859 usecase = node_to_item(node, struct audio_usecase, list); 860 if (usecase->type == PCM_PLAYBACK && 861 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 862 enable_audio_route(adev, usecase, true); 863 } 864 } 865 866 return 0; 867} 868 869static int stop_output_stream(struct stream_out *out) 870{ 871 int i, ret = 0; 872 struct audio_usecase *uc_info; 873 struct audio_device *adev = out->dev; 874 875 ALOGV("%s: enter: usecase(%d: %s)", __func__, 876 out->usecase, use_case_table[out->usecase]); 877 uc_info = get_usecase_from_list(adev, out->usecase); 878 if (uc_info == NULL) { 879 ALOGE("%s: Could not find the usecase (%d) in the list", 880 __func__, out->usecase); 881 return -EINVAL; 882 } 883 884 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD && 885 adev->visualizer_stop_output != NULL) 886 adev->visualizer_stop_output(out->handle); 887 888 /* 1. Get and set stream specific mixer controls */ 889 disable_audio_route(adev, uc_info, true); 890 891 /* 2. Disable the rx device */ 892 disable_snd_device(adev, uc_info->out_snd_device, true); 893 894 list_remove(&uc_info->list); 895 free(uc_info); 896 897 /* Must be called after removing the usecase from list */ 898 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) 899 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS); 900 901 ALOGV("%s: exit: status(%d)", __func__, ret); 902 return ret; 903} 904 905int start_output_stream(struct stream_out *out) 906{ 907 int ret = 0; 908 struct audio_usecase *uc_info; 909 struct audio_device *adev = out->dev; 910 911 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)", 912 __func__, out->usecase, use_case_table[out->usecase], out->devices); 913 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); 914 if (out->pcm_device_id < 0) { 915 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 916 __func__, out->pcm_device_id, out->usecase); 917 ret = -EINVAL; 918 goto error_config; 919 } 920 921 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 922 uc_info->id = out->usecase; 923 uc_info->type = PCM_PLAYBACK; 924 uc_info->stream.out = out; 925 uc_info->devices = out->devices; 926 uc_info->in_snd_device = SND_DEVICE_NONE; 927 uc_info->out_snd_device = SND_DEVICE_NONE; 928 929 /* This must be called before adding this usecase to the list */ 930 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) 931 check_and_set_hdmi_channels(adev, out->config.channels); 932 933 list_add_tail(&adev->usecase_list, &uc_info->list); 934 935 select_devices(adev, out->usecase); 936 937 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", 938 __func__, 0, out->pcm_device_id); 939 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 940 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id, 941 PCM_OUT | PCM_MONOTONIC, &out->config); 942 if (out->pcm && !pcm_is_ready(out->pcm)) { 943 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); 944 pcm_close(out->pcm); 945 out->pcm = NULL; 946 ret = -EIO; 947 goto error_open; 948 } 949 } else { 950 out->pcm = NULL; 951 out->compr = compress_open(SOUND_CARD, out->pcm_device_id, 952 COMPRESS_IN, &out->compr_config); 953 if (out->compr && !is_compress_ready(out->compr)) { 954 ALOGE("%s: %s", __func__, compress_get_error(out->compr)); 955 compress_close(out->compr); 956 out->compr = NULL; 957 ret = -EIO; 958 goto error_open; 959 } 960 if (out->offload_callback) 961 compress_nonblock(out->compr, out->non_blocking); 962 963 if (adev->visualizer_start_output != NULL) 964 adev->visualizer_start_output(out->handle); 965 } 966 ALOGV("%s: exit", __func__); 967 return 0; 968error_open: 969 stop_output_stream(out); 970error_config: 971 return ret; 972} 973 974static int stop_voice_call(struct audio_device *adev) 975{ 976 int i, ret = 0; 977 struct audio_usecase *uc_info; 978 979 ALOGV("%s: enter", __func__); 980 adev->in_call = false; 981 982 ret = platform_stop_voice_call(adev->platform); 983 984 /* 1. Close the PCM devices */ 985 if (adev->voice_call_rx) { 986 pcm_close(adev->voice_call_rx); 987 adev->voice_call_rx = NULL; 988 } 989 if (adev->voice_call_tx) { 990 pcm_close(adev->voice_call_tx); 991 adev->voice_call_tx = NULL; 992 } 993 994 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL); 995 if (uc_info == NULL) { 996 ALOGE("%s: Could not find the usecase (%d) in the list", 997 __func__, USECASE_VOICE_CALL); 998 return -EINVAL; 999 } 1000 1001 /* 2. Get and set stream specific mixer controls */ 1002 disable_audio_route(adev, uc_info, true); 1003 1004 /* 3. Disable the rx and tx devices */ 1005 disable_snd_device(adev, uc_info->out_snd_device, false); 1006 disable_snd_device(adev, uc_info->in_snd_device, true); 1007 1008 list_remove(&uc_info->list); 1009 free(uc_info); 1010 1011 ALOGV("%s: exit: status(%d)", __func__, ret); 1012 return ret; 1013} 1014 1015static int start_voice_call(struct audio_device *adev) 1016{ 1017 int i, ret = 0; 1018 struct audio_usecase *uc_info; 1019 int pcm_dev_rx_id, pcm_dev_tx_id; 1020 1021 ALOGV("%s: enter", __func__); 1022 1023 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 1024 uc_info->id = USECASE_VOICE_CALL; 1025 uc_info->type = VOICE_CALL; 1026 uc_info->stream.out = adev->primary_output; 1027 uc_info->devices = adev->primary_output->devices; 1028 uc_info->in_snd_device = SND_DEVICE_NONE; 1029 uc_info->out_snd_device = SND_DEVICE_NONE; 1030 1031 list_add_tail(&adev->usecase_list, &uc_info->list); 1032 1033 select_devices(adev, USECASE_VOICE_CALL); 1034 1035 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); 1036 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); 1037 1038 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) { 1039 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", 1040 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); 1041 ret = -EIO; 1042 goto error_start_voice; 1043 } 1044 1045 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)", 1046 __func__, SOUND_CARD, pcm_dev_rx_id); 1047 adev->voice_call_rx = pcm_open(SOUND_CARD, 1048 pcm_dev_rx_id, 1049 PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call); 1050 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) { 1051 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx)); 1052 ret = -EIO; 1053 goto error_start_voice; 1054 } 1055 1056 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)", 1057 __func__, SOUND_CARD, pcm_dev_tx_id); 1058 adev->voice_call_tx = pcm_open(SOUND_CARD, 1059 pcm_dev_tx_id, 1060 PCM_IN, &pcm_config_voice_call); 1061 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) { 1062 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx)); 1063 ret = -EIO; 1064 goto error_start_voice; 1065 } 1066 1067 /* set cached volume */ 1068 set_voice_volume_l(adev, adev->voice_volume); 1069 1070 pcm_start(adev->voice_call_rx); 1071 pcm_start(adev->voice_call_tx); 1072 1073 ret = platform_start_voice_call(adev->platform); 1074 if (ret < 0) { 1075 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret); 1076 goto error_start_voice; 1077 } 1078 adev->in_call = true; 1079 return 0; 1080 1081error_start_voice: 1082 stop_voice_call(adev); 1083 1084 ALOGD("%s: exit: status(%d)", __func__, ret); 1085 return ret; 1086} 1087 1088static int check_input_parameters(uint32_t sample_rate, 1089 audio_format_t format, 1090 int channel_count) 1091{ 1092 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; 1093 1094 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; 1095 1096 switch (sample_rate) { 1097 case 8000: 1098 case 11025: 1099 case 12000: 1100 case 16000: 1101 case 22050: 1102 case 24000: 1103 case 32000: 1104 case 44100: 1105 case 48000: 1106 break; 1107 default: 1108 return -EINVAL; 1109 } 1110 1111 return 0; 1112} 1113 1114static size_t get_input_buffer_size(uint32_t sample_rate, 1115 audio_format_t format, 1116 int channel_count) 1117{ 1118 size_t size = 0; 1119 1120 if (check_input_parameters(sample_rate, format, channel_count) != 0) 1121 return 0; 1122 1123 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000; 1124 /* ToDo: should use frame_size computed based on the format and 1125 channel_count here. */ 1126 size *= sizeof(short) * channel_count; 1127 1128 /* make sure the size is multiple of 64 */ 1129 size += 0x3f; 1130 size &= ~0x3f; 1131 1132 return size; 1133} 1134 1135static uint32_t out_get_sample_rate(const struct audio_stream *stream) 1136{ 1137 struct stream_out *out = (struct stream_out *)stream; 1138 1139 return out->sample_rate; 1140} 1141 1142static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1143{ 1144 return -ENOSYS; 1145} 1146 1147static size_t out_get_buffer_size(const struct audio_stream *stream) 1148{ 1149 struct stream_out *out = (struct stream_out *)stream; 1150 1151 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1152 return out->compr_config.fragment_size; 1153 } 1154 1155 return out->config.period_size * audio_stream_frame_size(stream); 1156} 1157 1158static uint32_t out_get_channels(const struct audio_stream *stream) 1159{ 1160 struct stream_out *out = (struct stream_out *)stream; 1161 1162 return out->channel_mask; 1163} 1164 1165static audio_format_t out_get_format(const struct audio_stream *stream) 1166{ 1167 struct stream_out *out = (struct stream_out *)stream; 1168 1169 return out->format; 1170} 1171 1172static int out_set_format(struct audio_stream *stream, audio_format_t format) 1173{ 1174 return -ENOSYS; 1175} 1176 1177static int out_standby(struct audio_stream *stream) 1178{ 1179 struct stream_out *out = (struct stream_out *)stream; 1180 struct audio_device *adev = out->dev; 1181 1182 ALOGV("%s: enter: usecase(%d: %s)", __func__, 1183 out->usecase, use_case_table[out->usecase]); 1184 1185 pthread_mutex_lock(&out->lock); 1186 if (!out->standby) { 1187 pthread_mutex_lock(&adev->lock); 1188 out->standby = true; 1189 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1190 if (out->pcm) { 1191 pcm_close(out->pcm); 1192 out->pcm = NULL; 1193 } 1194 } else { 1195 stop_compressed_output_l(out); 1196 out->gapless_mdata.encoder_delay = 0; 1197 out->gapless_mdata.encoder_padding = 0; 1198 if (out->compr != NULL) { 1199 compress_close(out->compr); 1200 out->compr = NULL; 1201 } 1202 } 1203 stop_output_stream(out); 1204 pthread_mutex_unlock(&adev->lock); 1205 } 1206 pthread_mutex_unlock(&out->lock); 1207 ALOGV("%s: exit", __func__); 1208 return 0; 1209} 1210 1211static int out_dump(const struct audio_stream *stream, int fd) 1212{ 1213 return 0; 1214} 1215 1216static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) 1217{ 1218 int ret = 0; 1219 char value[32]; 1220 struct compr_gapless_mdata tmp_mdata; 1221 1222 if (!out || !parms) { 1223 return -EINVAL; 1224 } 1225 1226 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); 1227 if (ret >= 0) { 1228 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check? 1229 } else { 1230 return -EINVAL; 1231 } 1232 1233 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); 1234 if (ret >= 0) { 1235 tmp_mdata.encoder_padding = atoi(value); 1236 } else { 1237 return -EINVAL; 1238 } 1239 1240 out->gapless_mdata = tmp_mdata; 1241 out->send_new_metadata = 1; 1242 ALOGV("%s new encoder delay %u and padding %u", __func__, 1243 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); 1244 1245 return 0; 1246} 1247 1248 1249static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 1250{ 1251 struct stream_out *out = (struct stream_out *)stream; 1252 struct audio_device *adev = out->dev; 1253 struct audio_usecase *usecase; 1254 struct listnode *node; 1255 struct str_parms *parms; 1256 char value[32]; 1257 int ret, val = 0; 1258 bool select_new_device = false; 1259 int status = 0; 1260 1261 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", 1262 __func__, out->usecase, use_case_table[out->usecase], kvpairs); 1263 parms = str_parms_create_str(kvpairs); 1264 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1265 if (ret >= 0) { 1266 val = atoi(value); 1267 pthread_mutex_lock(&out->lock); 1268 pthread_mutex_lock(&adev->lock); 1269 1270 /* 1271 * When HDMI cable is unplugged the music playback is paused and 1272 * the policy manager sends routing=0. But the audioflinger 1273 * continues to write data until standby time (3sec). 1274 * As the HDMI core is turned off, the write gets blocked. 1275 * Avoid this by routing audio to speaker until standby. 1276 */ 1277 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && 1278 val == AUDIO_DEVICE_NONE) { 1279 val = AUDIO_DEVICE_OUT_SPEAKER; 1280 } 1281 1282 /* 1283 * select_devices() call below switches all the usecases on the same 1284 * backend to the new device. Refer to check_usecases_codec_backend() in 1285 * the select_devices(). But how do we undo this? 1286 * 1287 * For example, music playback is active on headset (deep-buffer usecase) 1288 * and if we go to ringtones and select a ringtone, low-latency usecase 1289 * will be started on headset+speaker. As we can't enable headset+speaker 1290 * and headset devices at the same time, select_devices() switches the music 1291 * playback to headset+speaker while starting low-lateny usecase for ringtone. 1292 * So when the ringtone playback is completed, how do we undo the same? 1293 * 1294 * We are relying on the out_set_parameters() call on deep-buffer output, 1295 * once the ringtone playback is ended. 1296 * NOTE: We should not check if the current devices are same as new devices. 1297 * Because select_devices() must be called to switch back the music 1298 * playback to headset. 1299 */ 1300 if (val != 0) { 1301 out->devices = val; 1302 1303 if (!out->standby) 1304 select_devices(adev, out->usecase); 1305 1306 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call && 1307 (out == adev->primary_output)) { 1308 start_voice_call(adev); 1309 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call && 1310 (out == adev->primary_output)) { 1311 select_devices(adev, USECASE_VOICE_CALL); 1312 } 1313 } 1314 1315 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call && 1316 (out == adev->primary_output)) { 1317 stop_voice_call(adev); 1318 } 1319 1320 pthread_mutex_unlock(&adev->lock); 1321 pthread_mutex_unlock(&out->lock); 1322 } 1323 1324 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1325 parse_compress_metadata(out, parms); 1326 } 1327 1328 str_parms_destroy(parms); 1329 ALOGV("%s: exit: code(%d)", __func__, status); 1330 return status; 1331} 1332 1333static char* out_get_parameters(const struct audio_stream *stream, const char *keys) 1334{ 1335 struct stream_out *out = (struct stream_out *)stream; 1336 struct str_parms *query = str_parms_create_str(keys); 1337 char *str; 1338 char value[256]; 1339 struct str_parms *reply = str_parms_create(); 1340 size_t i, j; 1341 int ret; 1342 bool first = true; 1343 ALOGV("%s: enter: keys - %s", __func__, keys); 1344 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); 1345 if (ret >= 0) { 1346 value[0] = '\0'; 1347 i = 0; 1348 while (out->supported_channel_masks[i] != 0) { 1349 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { 1350 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { 1351 if (!first) { 1352 strcat(value, "|"); 1353 } 1354 strcat(value, out_channels_name_to_enum_table[j].name); 1355 first = false; 1356 break; 1357 } 1358 } 1359 i++; 1360 } 1361 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 1362 str = str_parms_to_str(reply); 1363 } else { 1364 str = strdup(keys); 1365 } 1366 str_parms_destroy(query); 1367 str_parms_destroy(reply); 1368 ALOGV("%s: exit: returns - %s", __func__, str); 1369 return str; 1370} 1371 1372static uint32_t out_get_latency(const struct audio_stream_out *stream) 1373{ 1374 struct stream_out *out = (struct stream_out *)stream; 1375 1376 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) 1377 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; 1378 1379 return (out->config.period_count * out->config.period_size * 1000) / 1380 (out->config.rate); 1381} 1382 1383static int out_set_volume(struct audio_stream_out *stream, float left, 1384 float right) 1385{ 1386 struct stream_out *out = (struct stream_out *)stream; 1387 int volume[2]; 1388 1389 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { 1390 /* only take left channel into account: the API is for stereo anyway */ 1391 out->muted = (left == 0.0f); 1392 return 0; 1393 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1394 const char *mixer_ctl_name = "Compress Playback Volume"; 1395 struct audio_device *adev = out->dev; 1396 struct mixer_ctl *ctl; 1397 1398 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 1399 if (!ctl) { 1400 ALOGE("%s: Could not get ctl for mixer cmd - %s", 1401 __func__, mixer_ctl_name); 1402 return -EINVAL; 1403 } 1404 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); 1405 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); 1406 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0])); 1407 return 0; 1408 } 1409 1410 return -ENOSYS; 1411} 1412 1413static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, 1414 size_t bytes) 1415{ 1416 struct stream_out *out = (struct stream_out *)stream; 1417 struct audio_device *adev = out->dev; 1418 ssize_t ret = 0; 1419 1420 pthread_mutex_lock(&out->lock); 1421 if (out->standby) { 1422 out->standby = false; 1423 pthread_mutex_lock(&adev->lock); 1424 ret = start_output_stream(out); 1425 pthread_mutex_unlock(&adev->lock); 1426 /* ToDo: If use case is compress offload should return 0 */ 1427 if (ret != 0) { 1428 out->standby = true; 1429 goto exit; 1430 } 1431 } 1432 1433 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1434 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes); 1435 if (out->send_new_metadata) { 1436 ALOGVV("send new gapless metadata"); 1437 compress_set_gapless_metadata(out->compr, &out->gapless_mdata); 1438 out->send_new_metadata = 0; 1439 } 1440 1441 ret = compress_write(out->compr, buffer, bytes); 1442 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret); 1443 if (ret >= 0 && ret < (ssize_t)bytes) { 1444 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); 1445 } 1446 if (!out->playback_started) { 1447 compress_start(out->compr); 1448 out->playback_started = 1; 1449 out->offload_state = OFFLOAD_STATE_PLAYING; 1450 } 1451 pthread_mutex_unlock(&out->lock); 1452 return ret; 1453 } else { 1454 if (out->pcm) { 1455 if (out->muted) 1456 memset((void *)buffer, 0, bytes); 1457 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); 1458 ret = pcm_write(out->pcm, (void *)buffer, bytes); 1459 if (ret == 0) 1460 out->written += bytes / (out->config.channels * sizeof(short)); 1461 } 1462 } 1463 1464exit: 1465 pthread_mutex_unlock(&out->lock); 1466 1467 if (ret != 0) { 1468 if (out->pcm) 1469 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm)); 1470 out_standby(&out->stream.common); 1471 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) / 1472 out_get_sample_rate(&out->stream.common)); 1473 } 1474 return bytes; 1475} 1476 1477static int out_get_render_position(const struct audio_stream_out *stream, 1478 uint32_t *dsp_frames) 1479{ 1480 struct stream_out *out = (struct stream_out *)stream; 1481 *dsp_frames = 0; 1482 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { 1483 pthread_mutex_lock(&out->lock); 1484 if (out->compr != NULL) { 1485 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames, 1486 &out->sample_rate); 1487 ALOGVV("%s rendered frames %d sample_rate %d", 1488 __func__, *dsp_frames, out->sample_rate); 1489 } 1490 pthread_mutex_unlock(&out->lock); 1491 return 0; 1492 } else 1493 return -EINVAL; 1494} 1495 1496static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1497{ 1498 return 0; 1499} 1500 1501static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1502{ 1503 return 0; 1504} 1505 1506static int out_get_next_write_timestamp(const struct audio_stream_out *stream, 1507 int64_t *timestamp) 1508{ 1509 return -EINVAL; 1510} 1511 1512static int out_get_presentation_position(const struct audio_stream_out *stream, 1513 uint64_t *frames, struct timespec *timestamp) 1514{ 1515 struct stream_out *out = (struct stream_out *)stream; 1516 int ret = -1; 1517 unsigned long dsp_frames; 1518 1519 pthread_mutex_lock(&out->lock); 1520 1521 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1522 if (out->compr != NULL) { 1523 compress_get_tstamp(out->compr, &dsp_frames, 1524 &out->sample_rate); 1525 ALOGVV("%s rendered frames %ld sample_rate %d", 1526 __func__, dsp_frames, out->sample_rate); 1527 *frames = dsp_frames; 1528 ret = 0; 1529 /* this is the best we can do */ 1530 clock_gettime(CLOCK_MONOTONIC, timestamp); 1531 } 1532 } else { 1533 if (out->pcm) { 1534 size_t avail; 1535 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { 1536 size_t kernel_buffer_size = out->config.period_size * out->config.period_count; 1537 int64_t signed_frames = out->written - kernel_buffer_size + avail; 1538 // This adjustment accounts for buffering after app processor. 1539 // It is based on estimated DSP latency per use case, rather than exact. 1540 signed_frames -= 1541 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL); 1542 1543 // It would be unusual for this value to be negative, but check just in case ... 1544 if (signed_frames >= 0) { 1545 *frames = signed_frames; 1546 ret = 0; 1547 } 1548 } 1549 } 1550 } 1551 1552 pthread_mutex_unlock(&out->lock); 1553 1554 return ret; 1555} 1556 1557static int out_set_callback(struct audio_stream_out *stream, 1558 stream_callback_t callback, void *cookie) 1559{ 1560 struct stream_out *out = (struct stream_out *)stream; 1561 1562 ALOGV("%s", __func__); 1563 pthread_mutex_lock(&out->lock); 1564 out->offload_callback = callback; 1565 out->offload_cookie = cookie; 1566 pthread_mutex_unlock(&out->lock); 1567 return 0; 1568} 1569 1570static int out_pause(struct audio_stream_out* stream) 1571{ 1572 struct stream_out *out = (struct stream_out *)stream; 1573 int status = -ENOSYS; 1574 ALOGV("%s", __func__); 1575 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1576 pthread_mutex_lock(&out->lock); 1577 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { 1578 status = compress_pause(out->compr); 1579 out->offload_state = OFFLOAD_STATE_PAUSED; 1580 } 1581 pthread_mutex_unlock(&out->lock); 1582 } 1583 return status; 1584} 1585 1586static int out_resume(struct audio_stream_out* stream) 1587{ 1588 struct stream_out *out = (struct stream_out *)stream; 1589 int status = -ENOSYS; 1590 ALOGV("%s", __func__); 1591 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1592 status = 0; 1593 pthread_mutex_lock(&out->lock); 1594 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { 1595 status = compress_resume(out->compr); 1596 out->offload_state = OFFLOAD_STATE_PLAYING; 1597 } 1598 pthread_mutex_unlock(&out->lock); 1599 } 1600 return status; 1601} 1602 1603static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) 1604{ 1605 struct stream_out *out = (struct stream_out *)stream; 1606 int status = -ENOSYS; 1607 ALOGV("%s", __func__); 1608 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1609 pthread_mutex_lock(&out->lock); 1610 if (type == AUDIO_DRAIN_EARLY_NOTIFY) 1611 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); 1612 else 1613 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); 1614 pthread_mutex_unlock(&out->lock); 1615 } 1616 return status; 1617} 1618 1619static int out_flush(struct audio_stream_out* stream) 1620{ 1621 struct stream_out *out = (struct stream_out *)stream; 1622 ALOGV("%s", __func__); 1623 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1624 pthread_mutex_lock(&out->lock); 1625 stop_compressed_output_l(out); 1626 pthread_mutex_unlock(&out->lock); 1627 return 0; 1628 } 1629 return -ENOSYS; 1630} 1631 1632/** audio_stream_in implementation **/ 1633static uint32_t in_get_sample_rate(const struct audio_stream *stream) 1634{ 1635 struct stream_in *in = (struct stream_in *)stream; 1636 1637 return in->config.rate; 1638} 1639 1640static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1641{ 1642 return -ENOSYS; 1643} 1644 1645static size_t in_get_buffer_size(const struct audio_stream *stream) 1646{ 1647 struct stream_in *in = (struct stream_in *)stream; 1648 1649 return in->config.period_size * audio_stream_frame_size(stream); 1650} 1651 1652static uint32_t in_get_channels(const struct audio_stream *stream) 1653{ 1654 struct stream_in *in = (struct stream_in *)stream; 1655 1656 return in->channel_mask; 1657} 1658 1659static audio_format_t in_get_format(const struct audio_stream *stream) 1660{ 1661 return AUDIO_FORMAT_PCM_16_BIT; 1662} 1663 1664static int in_set_format(struct audio_stream *stream, audio_format_t format) 1665{ 1666 return -ENOSYS; 1667} 1668 1669static int in_standby(struct audio_stream *stream) 1670{ 1671 struct stream_in *in = (struct stream_in *)stream; 1672 struct audio_device *adev = in->dev; 1673 int status = 0; 1674 ALOGV("%s: enter", __func__); 1675 pthread_mutex_lock(&in->lock); 1676 if (!in->standby) { 1677 pthread_mutex_lock(&adev->lock); 1678 in->standby = true; 1679 if (in->pcm) { 1680 pcm_close(in->pcm); 1681 in->pcm = NULL; 1682 } 1683 status = stop_input_stream(in); 1684 pthread_mutex_unlock(&adev->lock); 1685 } 1686 pthread_mutex_unlock(&in->lock); 1687 ALOGV("%s: exit: status(%d)", __func__, status); 1688 return status; 1689} 1690 1691static int in_dump(const struct audio_stream *stream, int fd) 1692{ 1693 return 0; 1694} 1695 1696static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 1697{ 1698 struct stream_in *in = (struct stream_in *)stream; 1699 struct audio_device *adev = in->dev; 1700 struct str_parms *parms; 1701 char *str; 1702 char value[32]; 1703 int ret, val = 0; 1704 int status = 0; 1705 1706 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); 1707 parms = str_parms_create_str(kvpairs); 1708 1709 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 1710 1711 pthread_mutex_lock(&in->lock); 1712 pthread_mutex_lock(&adev->lock); 1713 if (ret >= 0) { 1714 val = atoi(value); 1715 /* no audio source uses val == 0 */ 1716 if ((in->source != val) && (val != 0)) { 1717 in->source = val; 1718 } 1719 } 1720 1721 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1722 1723 if (ret >= 0) { 1724 val = atoi(value); 1725 if ((in->device != val) && (val != 0)) { 1726 in->device = val; 1727 /* If recording is in progress, change the tx device to new device */ 1728 if (!in->standby) 1729 status = select_devices(adev, in->usecase); 1730 } 1731 } 1732 1733 pthread_mutex_unlock(&adev->lock); 1734 pthread_mutex_unlock(&in->lock); 1735 1736 str_parms_destroy(parms); 1737 ALOGV("%s: exit: status(%d)", __func__, status); 1738 return status; 1739} 1740 1741static char* in_get_parameters(const struct audio_stream *stream, 1742 const char *keys) 1743{ 1744 return strdup(""); 1745} 1746 1747static int in_set_gain(struct audio_stream_in *stream, float gain) 1748{ 1749 return 0; 1750} 1751 1752static ssize_t in_read(struct audio_stream_in *stream, void *buffer, 1753 size_t bytes) 1754{ 1755 struct stream_in *in = (struct stream_in *)stream; 1756 struct audio_device *adev = in->dev; 1757 int i, ret = -1; 1758 1759 pthread_mutex_lock(&in->lock); 1760 if (in->standby) { 1761 pthread_mutex_lock(&adev->lock); 1762 ret = start_input_stream(in); 1763 pthread_mutex_unlock(&adev->lock); 1764 if (ret != 0) { 1765 goto exit; 1766 } 1767 in->standby = 0; 1768 } 1769 1770 if (in->pcm) { 1771 ret = pcm_read(in->pcm, buffer, bytes); 1772 } 1773 1774 /* 1775 * Instead of writing zeroes here, we could trust the hardware 1776 * to always provide zeroes when muted. 1777 */ 1778 if (ret == 0 && adev->mic_mute) 1779 memset(buffer, 0, bytes); 1780 1781exit: 1782 pthread_mutex_unlock(&in->lock); 1783 1784 if (ret != 0) { 1785 in_standby(&in->stream.common); 1786 ALOGV("%s: read failed - sleeping for buffer duration", __func__); 1787 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) / 1788 in_get_sample_rate(&in->stream.common)); 1789 } 1790 return bytes; 1791} 1792 1793static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 1794{ 1795 return 0; 1796} 1797 1798static int add_remove_audio_effect(const struct audio_stream *stream, 1799 effect_handle_t effect, 1800 bool enable) 1801{ 1802 struct stream_in *in = (struct stream_in *)stream; 1803 int status = 0; 1804 effect_descriptor_t desc; 1805 1806 status = (*effect)->get_descriptor(effect, &desc); 1807 if (status != 0) 1808 return status; 1809 1810 pthread_mutex_lock(&in->lock); 1811 pthread_mutex_lock(&in->dev->lock); 1812 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && 1813 in->enable_aec != enable && 1814 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { 1815 in->enable_aec = enable; 1816 if (!in->standby) 1817 select_devices(in->dev, in->usecase); 1818 } 1819 pthread_mutex_unlock(&in->dev->lock); 1820 pthread_mutex_unlock(&in->lock); 1821 1822 return 0; 1823} 1824 1825static int in_add_audio_effect(const struct audio_stream *stream, 1826 effect_handle_t effect) 1827{ 1828 ALOGV("%s: effect %p", __func__, effect); 1829 return add_remove_audio_effect(stream, effect, true); 1830} 1831 1832static int in_remove_audio_effect(const struct audio_stream *stream, 1833 effect_handle_t effect) 1834{ 1835 ALOGV("%s: effect %p", __func__, effect); 1836 return add_remove_audio_effect(stream, effect, false); 1837} 1838 1839static int adev_open_output_stream(struct audio_hw_device *dev, 1840 audio_io_handle_t handle, 1841 audio_devices_t devices, 1842 audio_output_flags_t flags, 1843 struct audio_config *config, 1844 struct audio_stream_out **stream_out) 1845{ 1846 struct audio_device *adev = (struct audio_device *)dev; 1847 struct stream_out *out; 1848 int i, ret; 1849 1850 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", 1851 __func__, config->sample_rate, config->channel_mask, devices, flags); 1852 *stream_out = NULL; 1853 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 1854 1855 if (devices == AUDIO_DEVICE_NONE) 1856 devices = AUDIO_DEVICE_OUT_SPEAKER; 1857 1858 out->flags = flags; 1859 out->devices = devices; 1860 out->dev = adev; 1861 out->format = config->format; 1862 out->sample_rate = config->sample_rate; 1863 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 1864 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; 1865 out->handle = handle; 1866 1867 /* Init use case and pcm_config */ 1868 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && 1869 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && 1870 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1871 pthread_mutex_lock(&adev->lock); 1872 ret = read_hdmi_channel_masks(out); 1873 pthread_mutex_unlock(&adev->lock); 1874 if (ret != 0) 1875 goto error_open; 1876 1877 if (config->sample_rate == 0) 1878 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 1879 if (config->channel_mask == 0) 1880 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; 1881 1882 out->channel_mask = config->channel_mask; 1883 out->sample_rate = config->sample_rate; 1884 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH; 1885 out->config = pcm_config_hdmi_multi; 1886 out->config.rate = config->sample_rate; 1887 out->config.channels = popcount(out->channel_mask); 1888 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2); 1889 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 1890 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; 1891 out->config = pcm_config_deep_buffer; 1892 out->sample_rate = out->config.rate; 1893 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 1894 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || 1895 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { 1896 ALOGE("%s: Unsupported Offload information", __func__); 1897 ret = -EINVAL; 1898 goto error_open; 1899 } 1900 if (!is_supported_format(config->offload_info.format)) { 1901 ALOGE("%s: Unsupported audio format", __func__); 1902 ret = -EINVAL; 1903 goto error_open; 1904 } 1905 1906 out->compr_config.codec = (struct snd_codec *) 1907 calloc(1, sizeof(struct snd_codec)); 1908 1909 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; 1910 if (config->offload_info.channel_mask) 1911 out->channel_mask = config->offload_info.channel_mask; 1912 else if (config->channel_mask) 1913 out->channel_mask = config->channel_mask; 1914 out->format = config->offload_info.format; 1915 out->sample_rate = config->offload_info.sample_rate; 1916 1917 out->stream.set_callback = out_set_callback; 1918 out->stream.pause = out_pause; 1919 out->stream.resume = out_resume; 1920 out->stream.drain = out_drain; 1921 out->stream.flush = out_flush; 1922 1923 out->compr_config.codec->id = 1924 get_snd_codec_id(config->offload_info.format); 1925 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; 1926 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; 1927 out->compr_config.codec->sample_rate = 1928 compress_get_alsa_rate(config->offload_info.sample_rate); 1929 out->compr_config.codec->bit_rate = 1930 config->offload_info.bit_rate; 1931 out->compr_config.codec->ch_in = 1932 popcount(config->channel_mask); 1933 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; 1934 1935 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) 1936 out->non_blocking = 1; 1937 1938 out->send_new_metadata = 1; 1939 create_offload_callback_thread(out); 1940 ALOGV("%s: offloaded output offload_info version %04x bit rate %d", 1941 __func__, config->offload_info.version, 1942 config->offload_info.bit_rate); 1943 } else { 1944 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; 1945 out->config = pcm_config_low_latency; 1946 out->sample_rate = out->config.rate; 1947 } 1948 1949 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { 1950 if(adev->primary_output == NULL) 1951 adev->primary_output = out; 1952 else { 1953 ALOGE("%s: Primary output is already opened", __func__); 1954 ret = -EEXIST; 1955 goto error_open; 1956 } 1957 } 1958 1959 /* Check if this usecase is already existing */ 1960 pthread_mutex_lock(&adev->lock); 1961 if (get_usecase_from_list(adev, out->usecase) != NULL) { 1962 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); 1963 pthread_mutex_unlock(&adev->lock); 1964 ret = -EEXIST; 1965 goto error_open; 1966 } 1967 pthread_mutex_unlock(&adev->lock); 1968 1969 out->stream.common.get_sample_rate = out_get_sample_rate; 1970 out->stream.common.set_sample_rate = out_set_sample_rate; 1971 out->stream.common.get_buffer_size = out_get_buffer_size; 1972 out->stream.common.get_channels = out_get_channels; 1973 out->stream.common.get_format = out_get_format; 1974 out->stream.common.set_format = out_set_format; 1975 out->stream.common.standby = out_standby; 1976 out->stream.common.dump = out_dump; 1977 out->stream.common.set_parameters = out_set_parameters; 1978 out->stream.common.get_parameters = out_get_parameters; 1979 out->stream.common.add_audio_effect = out_add_audio_effect; 1980 out->stream.common.remove_audio_effect = out_remove_audio_effect; 1981 out->stream.get_latency = out_get_latency; 1982 out->stream.set_volume = out_set_volume; 1983 out->stream.write = out_write; 1984 out->stream.get_render_position = out_get_render_position; 1985 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 1986 out->stream.get_presentation_position = out_get_presentation_position; 1987 1988 out->standby = 1; 1989 /* out->muted = false; by calloc() */ 1990 /* out->written = 0; by calloc() */ 1991 1992 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); 1993 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); 1994 1995 config->format = out->stream.common.get_format(&out->stream.common); 1996 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 1997 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 1998 1999 *stream_out = &out->stream; 2000 ALOGV("%s: exit", __func__); 2001 return 0; 2002 2003error_open: 2004 free(out); 2005 *stream_out = NULL; 2006 ALOGD("%s: exit: ret %d", __func__, ret); 2007 return ret; 2008} 2009 2010static void adev_close_output_stream(struct audio_hw_device *dev, 2011 struct audio_stream_out *stream) 2012{ 2013 struct stream_out *out = (struct stream_out *)stream; 2014 struct audio_device *adev = out->dev; 2015 2016 ALOGV("%s: enter", __func__); 2017 out_standby(&stream->common); 2018 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2019 destroy_offload_callback_thread(out); 2020 2021 if (out->compr_config.codec != NULL) 2022 free(out->compr_config.codec); 2023 } 2024 pthread_cond_destroy(&out->cond); 2025 pthread_mutex_destroy(&out->lock); 2026 free(stream); 2027 ALOGV("%s: exit", __func__); 2028} 2029 2030static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 2031{ 2032 struct audio_device *adev = (struct audio_device *)dev; 2033 struct str_parms *parms; 2034 char *str; 2035 char value[32]; 2036 int val; 2037 int ret; 2038 int status = 0; 2039 2040 ALOGV("%s: enter: %s", __func__, kvpairs); 2041 2042 parms = str_parms_create_str(kvpairs); 2043 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 2044 if (ret >= 0) { 2045 int tty_mode; 2046 2047 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 2048 tty_mode = TTY_MODE_OFF; 2049 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 2050 tty_mode = TTY_MODE_VCO; 2051 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 2052 tty_mode = TTY_MODE_HCO; 2053 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 2054 tty_mode = TTY_MODE_FULL; 2055 else 2056 return -EINVAL; 2057 2058 pthread_mutex_lock(&adev->lock); 2059 if (tty_mode != adev->tty_mode) { 2060 adev->tty_mode = tty_mode; 2061 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode; 2062 if (adev->in_call) 2063 select_devices(adev, USECASE_VOICE_CALL); 2064 } 2065 pthread_mutex_unlock(&adev->lock); 2066 } 2067 2068 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 2069 if (ret >= 0) { 2070 /* When set to false, HAL should disable EC and NS 2071 * But it is currently not supported. 2072 */ 2073 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 2074 adev->bluetooth_nrec = true; 2075 else 2076 adev->bluetooth_nrec = false; 2077 } 2078 2079 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 2080 if (ret >= 0) { 2081 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 2082 adev->screen_off = false; 2083 else 2084 adev->screen_off = true; 2085 } 2086 2087 ret = str_parms_get_int(parms, "rotation", &val); 2088 if (ret >= 0) { 2089 bool reverse_speakers = false; 2090 switch(val) { 2091 // FIXME: note that the code below assumes that the speakers are in the correct placement 2092 // relative to the user when the device is rotated 90deg from its default rotation. This 2093 // assumption is device-specific, not platform-specific like this code. 2094 case 270: 2095 reverse_speakers = true; 2096 break; 2097 case 0: 2098 case 90: 2099 case 180: 2100 break; 2101 default: 2102 ALOGE("%s: unexpected rotation of %d", __func__, val); 2103 status = -EINVAL; 2104 } 2105 if (status == 0) { 2106 pthread_mutex_lock(&adev->lock); 2107 if (adev->speaker_lr_swap != reverse_speakers) { 2108 adev->speaker_lr_swap = reverse_speakers; 2109 // only update the selected device if there is active pcm playback 2110 struct audio_usecase *usecase; 2111 struct listnode *node; 2112 list_for_each(node, &adev->usecase_list) { 2113 usecase = node_to_item(node, struct audio_usecase, list); 2114 if (usecase->type == PCM_PLAYBACK) { 2115 select_devices(adev, usecase->id); 2116 break; 2117 } 2118 } 2119 } 2120 pthread_mutex_unlock(&adev->lock); 2121 } 2122 } 2123 2124 str_parms_destroy(parms); 2125 ALOGV("%s: exit with code(%d)", __func__, status); 2126 return status; 2127} 2128 2129static char* adev_get_parameters(const struct audio_hw_device *dev, 2130 const char *keys) 2131{ 2132 return strdup(""); 2133} 2134 2135static int adev_init_check(const struct audio_hw_device *dev) 2136{ 2137 return 0; 2138} 2139 2140/* always called with adev lock held */ 2141static int set_voice_volume_l(struct audio_device *adev, float volume) 2142{ 2143 int vol, err = 0; 2144 2145 if (adev->mode == AUDIO_MODE_IN_CALL) { 2146 if (volume < 0.0) { 2147 volume = 0.0; 2148 } else if (volume > 1.0) { 2149 volume = 1.0; 2150 } 2151 2152 vol = lrint(volume * 100.0); 2153 2154 // Voice volume levels from android are mapped to driver volume levels as follows. 2155 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0 2156 // So adjust the volume to get the correct volume index in driver 2157 vol = 100 - vol; 2158 2159 err = platform_set_voice_volume(adev->platform, vol); 2160 } 2161 return err; 2162} 2163 2164static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 2165{ 2166 int ret; 2167 struct audio_device *adev = (struct audio_device *)dev; 2168 pthread_mutex_lock(&adev->lock); 2169 /* cache volume */ 2170 adev->voice_volume = volume; 2171 ret = set_voice_volume_l(adev, adev->voice_volume); 2172 pthread_mutex_unlock(&adev->lock); 2173 return ret; 2174} 2175 2176static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 2177{ 2178 return -ENOSYS; 2179} 2180 2181static int adev_get_master_volume(struct audio_hw_device *dev, 2182 float *volume) 2183{ 2184 return -ENOSYS; 2185} 2186 2187static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) 2188{ 2189 return -ENOSYS; 2190} 2191 2192static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) 2193{ 2194 return -ENOSYS; 2195} 2196 2197static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 2198{ 2199 struct audio_device *adev = (struct audio_device *)dev; 2200 2201 pthread_mutex_lock(&adev->lock); 2202 if (adev->mode != mode) { 2203 adev->mode = mode; 2204 } 2205 pthread_mutex_unlock(&adev->lock); 2206 return 0; 2207} 2208 2209static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 2210{ 2211 struct audio_device *adev = (struct audio_device *)dev; 2212 int err = 0; 2213 2214 pthread_mutex_lock(&adev->lock); 2215 adev->mic_mute = state; 2216 2217 err = platform_set_mic_mute(adev->platform, state); 2218 pthread_mutex_unlock(&adev->lock); 2219 return err; 2220} 2221 2222static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 2223{ 2224 struct audio_device *adev = (struct audio_device *)dev; 2225 2226 *state = adev->mic_mute; 2227 2228 return 0; 2229} 2230 2231static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 2232 const struct audio_config *config) 2233{ 2234 int channel_count = popcount(config->channel_mask); 2235 2236 return get_input_buffer_size(config->sample_rate, config->format, channel_count); 2237} 2238 2239static int adev_open_input_stream(struct audio_hw_device *dev, 2240 audio_io_handle_t handle, 2241 audio_devices_t devices, 2242 struct audio_config *config, 2243 struct audio_stream_in **stream_in) 2244{ 2245 struct audio_device *adev = (struct audio_device *)dev; 2246 struct stream_in *in; 2247 int ret, buffer_size, frame_size; 2248 int channel_count = popcount(config->channel_mask); 2249 2250 ALOGV("%s: enter", __func__); 2251 *stream_in = NULL; 2252 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) 2253 return -EINVAL; 2254 2255 in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 2256 2257 in->stream.common.get_sample_rate = in_get_sample_rate; 2258 in->stream.common.set_sample_rate = in_set_sample_rate; 2259 in->stream.common.get_buffer_size = in_get_buffer_size; 2260 in->stream.common.get_channels = in_get_channels; 2261 in->stream.common.get_format = in_get_format; 2262 in->stream.common.set_format = in_set_format; 2263 in->stream.common.standby = in_standby; 2264 in->stream.common.dump = in_dump; 2265 in->stream.common.set_parameters = in_set_parameters; 2266 in->stream.common.get_parameters = in_get_parameters; 2267 in->stream.common.add_audio_effect = in_add_audio_effect; 2268 in->stream.common.remove_audio_effect = in_remove_audio_effect; 2269 in->stream.set_gain = in_set_gain; 2270 in->stream.read = in_read; 2271 in->stream.get_input_frames_lost = in_get_input_frames_lost; 2272 2273 in->device = devices; 2274 in->source = AUDIO_SOURCE_DEFAULT; 2275 in->dev = adev; 2276 in->standby = 1; 2277 in->channel_mask = config->channel_mask; 2278 2279 /* Update config params with the requested sample rate and channels */ 2280 in->usecase = USECASE_AUDIO_RECORD; 2281 in->config = pcm_config_audio_capture; 2282 in->config.channels = channel_count; 2283 in->config.rate = config->sample_rate; 2284 2285 frame_size = audio_stream_frame_size((struct audio_stream *)in); 2286 buffer_size = get_input_buffer_size(config->sample_rate, 2287 config->format, 2288 channel_count); 2289 in->config.period_size = buffer_size / frame_size; 2290 2291 *stream_in = &in->stream; 2292 ALOGV("%s: exit", __func__); 2293 return 0; 2294 2295err_open: 2296 free(in); 2297 *stream_in = NULL; 2298 return ret; 2299} 2300 2301static void adev_close_input_stream(struct audio_hw_device *dev, 2302 struct audio_stream_in *stream) 2303{ 2304 ALOGV("%s", __func__); 2305 2306 in_standby(&stream->common); 2307 free(stream); 2308 2309 return; 2310} 2311 2312static int adev_dump(const audio_hw_device_t *device, int fd) 2313{ 2314 return 0; 2315} 2316 2317static int adev_close(hw_device_t *device) 2318{ 2319 struct audio_device *adev = (struct audio_device *)device; 2320 audio_route_free(adev->audio_route); 2321 free(adev->snd_dev_ref_cnt); 2322 platform_deinit(adev->platform); 2323 free(device); 2324 return 0; 2325} 2326 2327static int adev_open(const hw_module_t *module, const char *name, 2328 hw_device_t **device) 2329{ 2330 struct audio_device *adev; 2331 int i, ret; 2332 2333 ALOGD("%s: enter", __func__); 2334 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; 2335 2336 adev = calloc(1, sizeof(struct audio_device)); 2337 2338 adev->device.common.tag = HARDWARE_DEVICE_TAG; 2339 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 2340 adev->device.common.module = (struct hw_module_t *)module; 2341 adev->device.common.close = adev_close; 2342 2343 adev->device.init_check = adev_init_check; 2344 adev->device.set_voice_volume = adev_set_voice_volume; 2345 adev->device.set_master_volume = adev_set_master_volume; 2346 adev->device.get_master_volume = adev_get_master_volume; 2347 adev->device.set_master_mute = adev_set_master_mute; 2348 adev->device.get_master_mute = adev_get_master_mute; 2349 adev->device.set_mode = adev_set_mode; 2350 adev->device.set_mic_mute = adev_set_mic_mute; 2351 adev->device.get_mic_mute = adev_get_mic_mute; 2352 adev->device.set_parameters = adev_set_parameters; 2353 adev->device.get_parameters = adev_get_parameters; 2354 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 2355 adev->device.open_output_stream = adev_open_output_stream; 2356 adev->device.close_output_stream = adev_close_output_stream; 2357 adev->device.open_input_stream = adev_open_input_stream; 2358 adev->device.close_input_stream = adev_close_input_stream; 2359 adev->device.dump = adev_dump; 2360 2361 /* Set the default route before the PCM stream is opened */ 2362 pthread_mutex_lock(&adev->lock); 2363 adev->mode = AUDIO_MODE_NORMAL; 2364 adev->active_input = NULL; 2365 adev->primary_output = NULL; 2366 adev->voice_call_rx = NULL; 2367 adev->voice_call_tx = NULL; 2368 adev->voice_volume = 1.0f; 2369 adev->tty_mode = TTY_MODE_OFF; 2370 adev->bluetooth_nrec = true; 2371 adev->in_call = false; 2372 adev->acdb_settings = TTY_MODE_OFF; 2373 /* adev->cur_hdmi_channels = 0; by calloc() */ 2374 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); 2375 list_init(&adev->usecase_list); 2376 pthread_mutex_unlock(&adev->lock); 2377 2378 /* Loads platform specific libraries dynamically */ 2379 adev->platform = platform_init(adev); 2380 if (!adev->platform) { 2381 free(adev->snd_dev_ref_cnt); 2382 free(adev); 2383 ALOGE("%s: Failed to init platform data, aborting.", __func__); 2384 *device = NULL; 2385 return -EINVAL; 2386 } 2387 2388 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) { 2389 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); 2390 if (adev->visualizer_lib == NULL) { 2391 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); 2392 } else { 2393 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); 2394 adev->visualizer_start_output = 2395 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib, 2396 "visualizer_hal_start_output"); 2397 adev->visualizer_stop_output = 2398 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib, 2399 "visualizer_hal_stop_output"); 2400 } 2401 } 2402 2403 *device = &adev->device.common; 2404 2405 ALOGV("%s: exit", __func__); 2406 return 0; 2407} 2408 2409static struct hw_module_methods_t hal_module_methods = { 2410 .open = adev_open, 2411}; 2412 2413struct audio_module HAL_MODULE_INFO_SYM = { 2414 .common = { 2415 .tag = HARDWARE_MODULE_TAG, 2416 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 2417 .hal_api_version = HARDWARE_HAL_API_VERSION, 2418 .id = AUDIO_HARDWARE_MODULE_ID, 2419 .name = "QCOM Audio HAL", 2420 .author = "Code Aurora Forum", 2421 .methods = &hal_module_methods, 2422 }, 2423}; 2424