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 48 #include "sound/compress_params.h" 49 50 /* TODO: the following PCM device profiles could be read from a config file */ 51 struct pcm_device_profile pcm_device_playback_hs = { 52 .config = { 53 .channels = PLAYBACK_DEFAULT_CHANNEL_COUNT, 54 .rate = PLAYBACK_DEFAULT_SAMPLING_RATE, 55 .period_size = PLAYBACK_PERIOD_SIZE, 56 .period_count = PLAYBACK_PERIOD_COUNT, 57 .format = PCM_FORMAT_S16_LE, 58 .start_threshold = PLAYBACK_START_THRESHOLD, 59 .stop_threshold = PLAYBACK_STOP_THRESHOLD, 60 .silence_threshold = 0, 61 .avail_min = PLAYBACK_AVAILABLE_MIN, 62 }, 63 .card = SOUND_CARD, 64 .id = 0, 65 .type = PCM_PLAYBACK, 66 .devices = AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE, 67 }; 68 69 struct pcm_device_profile pcm_device_capture = { 70 .config = { 71 .channels = CAPTURE_DEFAULT_CHANNEL_COUNT, 72 .rate = CAPTURE_DEFAULT_SAMPLING_RATE, 73 .period_size = CAPTURE_PERIOD_SIZE, 74 .period_count = CAPTURE_PERIOD_COUNT, 75 .format = PCM_FORMAT_S16_LE, 76 .start_threshold = CAPTURE_START_THRESHOLD, 77 .stop_threshold = 0, 78 .silence_threshold = 0, 79 .avail_min = 0, 80 }, 81 .card = SOUND_CARD, 82 .id = 0, 83 .type = PCM_CAPTURE, 84 .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC, 85 }; 86 87 struct pcm_device_profile pcm_device_capture_loopback_aec = { 88 .config = { 89 .channels = CAPTURE_DEFAULT_CHANNEL_COUNT, 90 .rate = CAPTURE_DEFAULT_SAMPLING_RATE, 91 .period_size = CAPTURE_PERIOD_SIZE, 92 .period_count = CAPTURE_PERIOD_COUNT, 93 .format = PCM_FORMAT_S16_LE, 94 .start_threshold = CAPTURE_START_THRESHOLD, 95 .stop_threshold = 0, 96 .silence_threshold = 0, 97 .avail_min = 0, 98 }, 99 .card = SOUND_CARD, 100 .id = 1, 101 .type = PCM_CAPTURE, 102 .devices = SND_DEVICE_IN_LOOPBACK_AEC, 103 }; 104 105 struct pcm_device_profile pcm_device_playback_spk = { 106 .config = { 107 .channels = PLAYBACK_DEFAULT_CHANNEL_COUNT, 108 .rate = PLAYBACK_DEFAULT_SAMPLING_RATE, 109 .period_size = PLAYBACK_PERIOD_SIZE, 110 .period_count = PLAYBACK_PERIOD_COUNT, 111 .format = PCM_FORMAT_S16_LE, 112 .start_threshold = PLAYBACK_START_THRESHOLD, 113 .stop_threshold = INT_MAX, 114 .silence_threshold = 0, 115 .avail_min = PLAYBACK_AVAILABLE_MIN, 116 }, 117 .card = SOUND_CARD, 118 .id = 1, 119 .type = PCM_PLAYBACK, 120 .devices = AUDIO_DEVICE_OUT_SPEAKER, 121 }; 122 123 struct pcm_device_profile pcm_device_playback_sco = { 124 .config = { 125 .channels = SCO_DEFAULT_CHANNEL_COUNT, 126 .rate = SCO_DEFAULT_SAMPLING_RATE, 127 .period_size = SCO_PERIOD_SIZE, 128 .period_count = SCO_PERIOD_COUNT, 129 .format = PCM_FORMAT_S16_LE, 130 .start_threshold = SCO_START_THRESHOLD, 131 .stop_threshold = SCO_STOP_THRESHOLD, 132 .silence_threshold = 0, 133 .avail_min = SCO_AVAILABLE_MIN, 134 }, 135 .card = SOUND_CARD, 136 .id = 2, 137 .type = PCM_PLAYBACK, 138 .devices = 139 AUDIO_DEVICE_OUT_BLUETOOTH_SCO|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET| 140 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, 141 }; 142 143 struct pcm_device_profile pcm_device_capture_sco = { 144 .config = { 145 .channels = SCO_DEFAULT_CHANNEL_COUNT, 146 .rate = SCO_DEFAULT_SAMPLING_RATE, 147 .period_size = SCO_PERIOD_SIZE, 148 .period_count = SCO_PERIOD_COUNT, 149 .format = PCM_FORMAT_S16_LE, 150 .start_threshold = CAPTURE_START_THRESHOLD, 151 .stop_threshold = 0, 152 .silence_threshold = 0, 153 .avail_min = 0, 154 }, 155 .card = SOUND_CARD, 156 .id = 2, 157 .type = PCM_CAPTURE, 158 .devices = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, 159 }; 160 161 struct pcm_device_profile pcm_device_hotword_streaming = { 162 .config = { 163 .channels = 1, 164 .rate = 16000, 165 .period_size = CAPTURE_PERIOD_SIZE, 166 .period_count = CAPTURE_PERIOD_COUNT, 167 .format = PCM_FORMAT_S16_LE, 168 .start_threshold = CAPTURE_START_THRESHOLD, 169 .stop_threshold = 0, 170 .silence_threshold = 0, 171 .avail_min = 0, 172 }, 173 .card = SOUND_CARD, 174 .id = 0, 175 .type = PCM_HOTWORD_STREAMING, 176 .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC 177 }; 178 179 struct pcm_device_profile *pcm_devices[] = { 180 &pcm_device_playback_hs, 181 &pcm_device_capture, 182 &pcm_device_playback_spk, 183 &pcm_device_playback_sco, 184 &pcm_device_capture_sco, 185 &pcm_device_capture_loopback_aec, 186 &pcm_device_hotword_streaming, 187 NULL, 188 }; 189 190 static const char * const use_case_table[AUDIO_USECASE_MAX] = { 191 [USECASE_AUDIO_PLAYBACK] = "playback", 192 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "playback multi-channel", 193 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", 194 [USECASE_AUDIO_CAPTURE] = "capture", 195 [USECASE_AUDIO_CAPTURE_HOTWORD] = "capture-hotword", 196 [USECASE_VOICE_CALL] = "voice-call", 197 }; 198 199 200 #define STRING_TO_ENUM(string) { #string, string } 201 202 static unsigned int audio_device_ref_count; 203 204 struct pcm_config pcm_config_deep_buffer = { 205 .channels = 2, 206 .rate = DEEP_BUFFER_OUTPUT_SAMPLING_RATE, 207 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, 208 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, 209 .format = PCM_FORMAT_S16_LE, 210 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 211 .stop_threshold = INT_MAX, 212 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 213 }; 214 215 struct string_to_enum { 216 const char *name; 217 uint32_t value; 218 }; 219 220 static const struct string_to_enum out_channels_name_to_enum_table[] = { 221 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 222 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 223 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 224 }; 225 226 static void dummybuf_thread_close(struct audio_device *adev); 227 228 static bool is_supported_format(audio_format_t format) 229 { 230 if (format == AUDIO_FORMAT_MP3 || 231 ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)) 232 return true; 233 234 return false; 235 } 236 237 static int get_snd_codec_id(audio_format_t format) 238 { 239 int id = 0; 240 241 switch (format & AUDIO_FORMAT_MAIN_MASK) { 242 case AUDIO_FORMAT_MP3: 243 id = SND_AUDIOCODEC_MP3; 244 break; 245 case AUDIO_FORMAT_AAC: 246 id = SND_AUDIOCODEC_AAC; 247 break; 248 default: 249 ALOGE("%s: Unsupported audio format", __func__); 250 } 251 252 return id; 253 } 254 255 /* Array to store sound devices */ 256 static const char * const device_table[SND_DEVICE_MAX] = { 257 [SND_DEVICE_NONE] = "none", 258 /* Playback sound devices */ 259 [SND_DEVICE_OUT_HANDSET] = "handset", 260 [SND_DEVICE_OUT_SPEAKER] = "speaker", 261 [SND_DEVICE_OUT_HEADPHONES] = "headphones", 262 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones", 263 [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset", 264 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker", 265 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones", 266 [SND_DEVICE_OUT_HDMI] = "hdmi", 267 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi", 268 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset", 269 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones", 270 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones", 271 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset", 272 273 /* Capture sound devices */ 274 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic", 275 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic", 276 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic", 277 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic", 278 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic", 279 [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic", 280 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic", 281 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic", 282 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic", 283 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic", 284 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic", 285 [SND_DEVICE_IN_VOICE_DMIC_1] = "voice-dmic-1", 286 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_1] = "voice-speaker-dmic-1", 287 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic", 288 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic", 289 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic", 290 [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = "voice-rec-headset-mic", 291 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic", 292 [SND_DEVICE_IN_VOICE_REC_DMIC_1] = "voice-rec-dmic-1", 293 [SND_DEVICE_IN_VOICE_REC_DMIC_NS_1] = "voice-rec-dmic-ns-1", 294 [SND_DEVICE_IN_LOOPBACK_AEC] = "loopback-aec", 295 }; 296 297 struct mixer_card *adev_get_mixer_for_card(struct audio_device *adev, int card) 298 { 299 struct mixer_card *mixer_card; 300 struct listnode *node; 301 302 list_for_each(node, &adev->mixer_list) { 303 mixer_card = node_to_item(node, struct mixer_card, adev_list_node); 304 if (mixer_card->card == card) 305 return mixer_card; 306 } 307 return NULL; 308 } 309 310 struct mixer_card *uc_get_mixer_for_card(struct audio_usecase *usecase, int card) 311 { 312 struct mixer_card *mixer_card; 313 struct listnode *node; 314 315 list_for_each(node, &usecase->mixer_list) { 316 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]); 317 if (mixer_card->card == card) 318 return mixer_card; 319 } 320 return NULL; 321 } 322 323 void free_mixer_list(struct audio_device *adev) 324 { 325 struct mixer_card *mixer_card; 326 struct listnode *node; 327 struct listnode *next; 328 329 list_for_each_safe(node, next, &adev->mixer_list) { 330 mixer_card = node_to_item(node, struct mixer_card, adev_list_node); 331 list_remove(node); 332 audio_route_free(mixer_card->audio_route); 333 free(mixer_card); 334 } 335 } 336 337 int mixer_init(struct audio_device *adev) 338 { 339 int i; 340 int card; 341 int retry_num; 342 struct mixer *mixer; 343 struct audio_route *audio_route; 344 char mixer_path[PATH_MAX]; 345 struct mixer_card *mixer_card; 346 struct listnode *node; 347 348 list_init(&adev->mixer_list); 349 350 for (i = 0; pcm_devices[i] != NULL; i++) { 351 card = pcm_devices[i]->card; 352 if (adev_get_mixer_for_card(adev, card) == NULL) { 353 retry_num = 0; 354 do { 355 mixer = mixer_open(card); 356 if (mixer == NULL) { 357 if (++retry_num > RETRY_NUMBER) { 358 ALOGE("%s unable to open the mixer for--card %d, aborting.", 359 __func__, card); 360 goto error; 361 } 362 usleep(RETRY_US); 363 } 364 } while (mixer == NULL); 365 366 sprintf(mixer_path, "/system/etc/mixer_paths_%d.xml", card); 367 audio_route = audio_route_init(card, mixer_path); 368 if (!audio_route) { 369 ALOGE("%s: Failed to init audio route controls for card %d, aborting.", 370 __func__, card); 371 goto error; 372 } 373 mixer_card = calloc(1, sizeof(struct mixer_card)); 374 mixer_card->card = card; 375 mixer_card->mixer = mixer; 376 mixer_card->audio_route = audio_route; 377 list_add_tail(&adev->mixer_list, &mixer_card->adev_list_node); 378 } 379 } 380 381 return 0; 382 383 error: 384 free_mixer_list(adev); 385 return -ENODEV; 386 } 387 388 const char *get_snd_device_name(snd_device_t snd_device) 389 { 390 const char *name = NULL; 391 392 if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) 393 name = device_table[snd_device]; 394 395 ALOGE_IF(name == NULL, "%s: invalid snd device %d", __func__, snd_device); 396 397 return name; 398 } 399 400 const char *get_snd_device_display_name(snd_device_t snd_device) 401 { 402 const char *name = get_snd_device_name(snd_device); 403 404 if (name == NULL) 405 name = "SND DEVICE NOT FOUND"; 406 407 return name; 408 } 409 410 struct pcm_device_profile *get_pcm_device(usecase_type_t uc_type, audio_devices_t devices) 411 { 412 int i; 413 414 devices &= ~AUDIO_DEVICE_BIT_IN; 415 for (i = 0; pcm_devices[i] != NULL; i++) { 416 if ((pcm_devices[i]->type == uc_type) && 417 (devices & pcm_devices[i]->devices)) 418 break; 419 } 420 return pcm_devices[i]; 421 } 422 423 static struct audio_usecase *get_usecase_from_id(struct audio_device *adev, 424 audio_usecase_t uc_id) 425 { 426 struct audio_usecase *usecase; 427 struct listnode *node; 428 429 list_for_each(node, &adev->usecase_list) { 430 usecase = node_to_item(node, struct audio_usecase, adev_list_node); 431 if (usecase->id == uc_id) 432 return usecase; 433 } 434 return NULL; 435 } 436 437 static struct audio_usecase *get_usecase_from_type(struct audio_device *adev, 438 usecase_type_t type) 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, adev_list_node); 445 if (usecase->type & type) 446 return usecase; 447 } 448 return NULL; 449 } 450 451 /* always called with adev lock held */ 452 static int set_voice_volume_l(struct audio_device *adev, float volume) 453 { 454 int err = 0; 455 (void)volume; 456 457 if (adev->mode == AUDIO_MODE_IN_CALL) { 458 /* TODO */ 459 } 460 return err; 461 } 462 463 464 snd_device_t get_output_snd_device(struct audio_device *adev, audio_devices_t devices) 465 { 466 467 audio_mode_t mode = adev->mode; 468 snd_device_t snd_device = SND_DEVICE_NONE; 469 470 ALOGV("%s: enter: output devices(%#x), mode(%d)", __func__, devices, mode); 471 if (devices == AUDIO_DEVICE_NONE || 472 devices & AUDIO_DEVICE_BIT_IN) { 473 ALOGV("%s: Invalid output devices (%#x)", __func__, devices); 474 goto exit; 475 } 476 477 if (mode == AUDIO_MODE_IN_CALL) { 478 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || 479 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 480 if (adev->tty_mode == TTY_MODE_FULL) 481 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES; 482 else if (adev->tty_mode == TTY_MODE_VCO) 483 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES; 484 else if (adev->tty_mode == TTY_MODE_HCO) 485 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET; 486 else 487 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES; 488 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) { 489 snd_device = SND_DEVICE_OUT_BT_SCO; 490 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) { 491 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER; 492 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) { 493 snd_device = SND_DEVICE_OUT_HANDSET; 494 } 495 if (snd_device != SND_DEVICE_NONE) { 496 goto exit; 497 } 498 } 499 500 if (popcount(devices) == 2) { 501 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE | 502 AUDIO_DEVICE_OUT_SPEAKER)) { 503 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES; 504 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET | 505 AUDIO_DEVICE_OUT_SPEAKER)) { 506 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES; 507 } else { 508 ALOGE("%s: Invalid combo device(%#x)", __func__, devices); 509 goto exit; 510 } 511 if (snd_device != SND_DEVICE_NONE) { 512 goto exit; 513 } 514 } 515 516 if (popcount(devices) != 1) { 517 ALOGE("%s: Invalid output devices(%#x)", __func__, devices); 518 goto exit; 519 } 520 521 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || 522 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 523 snd_device = SND_DEVICE_OUT_HEADPHONES; 524 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) { 525 snd_device = SND_DEVICE_OUT_SPEAKER; 526 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) { 527 snd_device = SND_DEVICE_OUT_BT_SCO; 528 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) { 529 snd_device = SND_DEVICE_OUT_HANDSET; 530 } else { 531 ALOGE("%s: Unknown device(s) %#x", __func__, devices); 532 } 533 exit: 534 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]); 535 return snd_device; 536 } 537 538 snd_device_t get_input_snd_device(struct audio_device *adev, audio_devices_t out_device) 539 { 540 audio_source_t source; 541 audio_mode_t mode = adev->mode; 542 audio_devices_t in_device; 543 audio_channel_mask_t channel_mask; 544 snd_device_t snd_device = SND_DEVICE_NONE; 545 struct stream_in *active_input = NULL; 546 struct audio_usecase *usecase; 547 548 usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL); 549 if (usecase != NULL) { 550 active_input = (struct stream_in *)usecase->stream; 551 } 552 source = (active_input == NULL) ? 553 AUDIO_SOURCE_DEFAULT : active_input->source; 554 555 in_device = ((active_input == NULL) ? 556 AUDIO_DEVICE_NONE : active_input->devices) 557 & ~AUDIO_DEVICE_BIT_IN; 558 channel_mask = (active_input == NULL) ? 559 AUDIO_CHANNEL_IN_MONO : active_input->main_channels; 560 561 ALOGV("%s: enter: out_device(%#x) in_device(%#x)", 562 __func__, out_device, in_device); 563 if (mode == AUDIO_MODE_IN_CALL) { 564 if (out_device == AUDIO_DEVICE_NONE) { 565 ALOGE("%s: No output device set for voice call", __func__); 566 goto exit; 567 } 568 if (adev->tty_mode != TTY_MODE_OFF) { 569 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || 570 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 571 switch (adev->tty_mode) { 572 case TTY_MODE_FULL: 573 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC; 574 break; 575 case TTY_MODE_VCO: 576 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC; 577 break; 578 case TTY_MODE_HCO: 579 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC; 580 break; 581 default: 582 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode); 583 } 584 goto exit; 585 } 586 } 587 if (out_device & AUDIO_DEVICE_OUT_EARPIECE || 588 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { 589 snd_device = SND_DEVICE_IN_HANDSET_MIC; 590 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 591 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC; 592 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) { 593 snd_device = SND_DEVICE_IN_BT_SCO_MIC ; 594 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) { 595 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC; 596 } 597 } else if (source == AUDIO_SOURCE_CAMCORDER) { 598 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC || 599 in_device & AUDIO_DEVICE_IN_BACK_MIC) { 600 snd_device = SND_DEVICE_IN_CAMCORDER_MIC; 601 } 602 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) { 603 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { 604 if (adev->dualmic_config == DUALMIC_CONFIG_1) { 605 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) 606 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_1; 607 else if (adev->ns_in_voice_rec) 608 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_NS_1; 609 } 610 611 if (snd_device == SND_DEVICE_NONE) { 612 snd_device = SND_DEVICE_IN_VOICE_REC_MIC; 613 } 614 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) { 615 snd_device = SND_DEVICE_IN_VOICE_REC_HEADSET_MIC; 616 } 617 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION || source == AUDIO_SOURCE_MIC) { 618 if (out_device & AUDIO_DEVICE_OUT_SPEAKER) 619 in_device = AUDIO_DEVICE_IN_BACK_MIC; 620 if (active_input) { 621 if (active_input->enable_aec) { 622 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) { 623 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC; 624 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { 625 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { 626 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC; 627 } else { 628 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC; 629 } 630 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) { 631 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC; 632 } 633 } 634 /* TODO: set echo reference */ 635 } 636 } else if (source == AUDIO_SOURCE_DEFAULT) { 637 goto exit; 638 } 639 640 641 if (snd_device != SND_DEVICE_NONE) { 642 goto exit; 643 } 644 645 if (in_device != AUDIO_DEVICE_NONE && 646 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) && 647 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) { 648 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { 649 snd_device = SND_DEVICE_IN_HANDSET_MIC; 650 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) { 651 snd_device = SND_DEVICE_IN_SPEAKER_MIC; 652 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) { 653 snd_device = SND_DEVICE_IN_HEADSET_MIC; 654 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { 655 snd_device = SND_DEVICE_IN_BT_SCO_MIC ; 656 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) { 657 snd_device = SND_DEVICE_IN_HDMI_MIC; 658 } else { 659 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device); 660 ALOGW("%s: Using default handset-mic", __func__); 661 snd_device = SND_DEVICE_IN_HANDSET_MIC; 662 } 663 } else { 664 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) { 665 snd_device = SND_DEVICE_IN_HANDSET_MIC; 666 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 667 snd_device = SND_DEVICE_IN_HEADSET_MIC; 668 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) { 669 snd_device = SND_DEVICE_IN_SPEAKER_MIC; 670 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { 671 snd_device = SND_DEVICE_IN_HANDSET_MIC; 672 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) { 673 snd_device = SND_DEVICE_IN_BT_SCO_MIC; 674 } else { 675 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device); 676 ALOGW("%s: Using default handset-mic", __func__); 677 snd_device = SND_DEVICE_IN_HANDSET_MIC; 678 } 679 } 680 exit: 681 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]); 682 return snd_device; 683 } 684 685 int set_hdmi_channels(struct audio_device *adev, int channel_count) 686 { 687 struct mixer_ctl *ctl; 688 const char *mixer_ctl_name = ""; 689 (void)adev; 690 (void)channel_count; 691 /* TODO */ 692 693 return 0; 694 } 695 696 int edid_get_max_channels(struct audio_device *adev) 697 { 698 int max_channels = 2; 699 struct mixer_ctl *ctl; 700 (void)adev; 701 702 /* TODO */ 703 return max_channels; 704 } 705 706 /* Delay in Us */ 707 int64_t render_latency(audio_usecase_t usecase) 708 { 709 (void)usecase; 710 /* TODO */ 711 return 0; 712 } 713 714 static int enable_snd_device(struct audio_device *adev, 715 struct audio_usecase *uc_info, 716 snd_device_t snd_device, 717 bool update_mixer) 718 { 719 struct mixer_card *mixer_card; 720 struct listnode *node; 721 const char *snd_device_name = get_snd_device_name(snd_device); 722 723 if (snd_device_name == NULL) 724 return -EINVAL; 725 726 adev->snd_dev_ref_cnt[snd_device]++; 727 if (adev->snd_dev_ref_cnt[snd_device] > 1) { 728 ALOGV("%s: snd_device(%d: %s) is already active", 729 __func__, snd_device, snd_device_name); 730 return 0; 731 } 732 733 ALOGV("%s: snd_device(%d: %s)", __func__, 734 snd_device, snd_device_name); 735 736 list_for_each(node, &uc_info->mixer_list) { 737 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]); 738 audio_route_apply_path(mixer_card->audio_route, snd_device_name); 739 if (update_mixer) 740 audio_route_update_mixer(mixer_card->audio_route); 741 } 742 743 return 0; 744 } 745 746 static int disable_snd_device(struct audio_device *adev, 747 struct audio_usecase *uc_info, 748 snd_device_t snd_device, 749 bool update_mixer) 750 { 751 struct mixer_card *mixer_card; 752 struct listnode *node; 753 const char *snd_device_name = get_snd_device_name(snd_device); 754 755 if (snd_device_name == NULL) 756 return -EINVAL; 757 758 if (adev->snd_dev_ref_cnt[snd_device] <= 0) { 759 ALOGE("%s: device ref cnt is already 0", __func__); 760 return -EINVAL; 761 } 762 adev->snd_dev_ref_cnt[snd_device]--; 763 if (adev->snd_dev_ref_cnt[snd_device] == 0) { 764 ALOGV("%s: snd_device(%d: %s)", __func__, 765 snd_device, snd_device_name); 766 list_for_each(node, &uc_info->mixer_list) { 767 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]); 768 audio_route_reset_path(mixer_card->audio_route, snd_device_name); 769 if (update_mixer) 770 audio_route_update_mixer(mixer_card->audio_route); 771 } 772 } 773 return 0; 774 } 775 776 static int select_devices(struct audio_device *adev, 777 audio_usecase_t uc_id) 778 { 779 snd_device_t out_snd_device = SND_DEVICE_NONE; 780 snd_device_t in_snd_device = SND_DEVICE_NONE; 781 struct audio_usecase *usecase = NULL; 782 struct audio_usecase *vc_usecase = NULL; 783 struct listnode *node; 784 struct stream_in *active_input = NULL; 785 struct stream_out *active_out; 786 struct mixer_card *mixer_card; 787 788 ALOGV("%s: usecase(%d)", __func__, uc_id); 789 790 if (uc_id == USECASE_AUDIO_CAPTURE_HOTWORD) 791 return 0; 792 793 usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL); 794 if (usecase != NULL) { 795 active_input = (struct stream_in *)usecase->stream; 796 } 797 798 usecase = get_usecase_from_id(adev, uc_id); 799 if (usecase == NULL) { 800 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); 801 return -EINVAL; 802 } 803 active_out = (struct stream_out *)usecase->stream; 804 805 if (usecase->type == VOICE_CALL) { 806 out_snd_device = get_output_snd_device(adev, active_out->devices); 807 in_snd_device = get_input_snd_device(adev, active_out->devices); 808 usecase->devices = active_out->devices; 809 } else { 810 /* 811 * If the voice call is active, use the sound devices of voice call usecase 812 * so that it would not result any device switch. All the usecases will 813 * be switched to new device when select_devices() is called for voice call 814 * usecase. 815 */ 816 if (adev->in_call) { 817 vc_usecase = get_usecase_from_id(adev, USECASE_VOICE_CALL); 818 if (usecase == NULL) { 819 ALOGE("%s: Could not find the voice call usecase", __func__); 820 } else { 821 in_snd_device = vc_usecase->in_snd_device; 822 out_snd_device = vc_usecase->out_snd_device; 823 } 824 } 825 if (usecase->type == PCM_PLAYBACK) { 826 usecase->devices = active_out->devices; 827 in_snd_device = SND_DEVICE_NONE; 828 if (out_snd_device == SND_DEVICE_NONE) { 829 out_snd_device = get_output_snd_device(adev, active_out->devices); 830 if (active_out == adev->primary_output && 831 active_input && 832 active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 833 select_devices(adev, active_input->usecase); 834 } 835 } 836 } else if (usecase->type == PCM_CAPTURE) { 837 usecase->devices = ((struct stream_in *)usecase->stream)->devices; 838 out_snd_device = SND_DEVICE_NONE; 839 if (in_snd_device == SND_DEVICE_NONE) { 840 if (active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && 841 adev->primary_output && !adev->primary_output->standby) { 842 in_snd_device = get_input_snd_device(adev, adev->primary_output->devices); 843 } else { 844 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE); 845 } 846 } 847 } 848 } 849 850 if (out_snd_device == usecase->out_snd_device && 851 in_snd_device == usecase->in_snd_device) { 852 return 0; 853 } 854 855 ALOGV("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__, 856 out_snd_device, get_snd_device_display_name(out_snd_device), 857 in_snd_device, get_snd_device_display_name(in_snd_device)); 858 859 860 /* Disable current sound devices */ 861 if (usecase->out_snd_device != SND_DEVICE_NONE) { 862 pthread_mutex_lock(&adev->tfa9895_lock); 863 disable_snd_device(adev, usecase, usecase->out_snd_device, false); 864 pthread_mutex_unlock(&adev->tfa9895_lock); 865 } 866 867 if (usecase->in_snd_device != SND_DEVICE_NONE) { 868 disable_snd_device(adev, usecase, usecase->in_snd_device, false); 869 } 870 871 /* Enable new sound devices */ 872 if (out_snd_device != SND_DEVICE_NONE) { 873 enable_snd_device(adev, usecase, out_snd_device, false); 874 } 875 876 if (in_snd_device != SND_DEVICE_NONE) { 877 enable_snd_device(adev, usecase, in_snd_device, false); 878 } 879 880 list_for_each(node, &usecase->mixer_list) { 881 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]); 882 audio_route_update_mixer(mixer_card->audio_route); 883 } 884 885 usecase->in_snd_device = in_snd_device; 886 usecase->out_snd_device = out_snd_device; 887 888 if (out_snd_device != SND_DEVICE_NONE) 889 if (usecase->devices & (AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) 890 if (adev->htc_acoustic_set_rt5506_amp != NULL) 891 adev->htc_acoustic_set_rt5506_amp(adev->mode, usecase->devices); 892 return 0; 893 } 894 895 896 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames); 897 static int do_in_standby_l(struct stream_in *in); 898 899 #ifdef PREPROCESSING_ENABLED 900 static void get_capture_reference_delay(struct stream_in *in, 901 size_t frames, 902 struct echo_reference_buffer *buffer) 903 { 904 ALOGVV("%s: enter:)", __func__); 905 906 /* read frames available in kernel driver buffer */ 907 unsigned int kernel_frames; 908 struct timespec tstamp; 909 long buf_delay; 910 long kernel_delay; 911 long delay_ns; 912 struct pcm_device *ref_device; 913 long rsmp_delay = 0; 914 915 ref_device = node_to_item(list_tail(&in->pcm_dev_list), 916 struct pcm_device, stream_list_node); 917 918 if (pcm_get_htimestamp(ref_device->pcm, &kernel_frames, &tstamp) < 0) { 919 buffer->time_stamp.tv_sec = 0; 920 buffer->time_stamp.tv_nsec = 0; 921 buffer->delay_ns = 0; 922 ALOGW("read get_capture_reference_delay(): pcm_htimestamp error"); 923 return; 924 } 925 926 /* adjust render time stamp with delay added by current driver buffer. 927 * Add the duration of current frame as we want the render time of the last 928 * sample being written. */ 929 930 kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / ref_device->pcm_profile->config.rate); 931 932 buffer->time_stamp = tstamp; 933 buffer->delay_ns = kernel_delay; 934 935 ALOGVV("get_capture_reference_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames: [%5d]," 936 " delay_ns: [%d] , frames:[%zd]", 937 buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, kernel_frames, buffer->delay_ns, frames); 938 } 939 940 static void get_capture_delay(struct stream_in *in, 941 size_t frames, 942 struct echo_reference_buffer *buffer) 943 { 944 ALOGVV("%s: enter:)", __func__); 945 /* read frames available in kernel driver buffer */ 946 unsigned int kernel_frames; 947 struct timespec tstamp; 948 long buf_delay; 949 long rsmp_delay; 950 long kernel_delay; 951 long delay_ns; 952 struct pcm_device *pcm_device; 953 954 pcm_device = node_to_item(list_head(&in->pcm_dev_list), 955 struct pcm_device, stream_list_node); 956 957 if (pcm_get_htimestamp(pcm_device->pcm, &kernel_frames, &tstamp) < 0) { 958 buffer->time_stamp.tv_sec = 0; 959 buffer->time_stamp.tv_nsec = 0; 960 buffer->delay_ns = 0; 961 ALOGW("read get_capture_delay(): pcm_htimestamp error"); 962 return; 963 } 964 965 /* read frames available in audio HAL input buffer 966 * add number of frames being read as we want the capture time of first sample 967 * in current buffer */ 968 /* frames in in->read_buf are at driver sampling rate while frames in in->proc_buf are 969 * at requested sampling rate */ 970 buf_delay = (long)(((int64_t)(in->read_buf_frames) * 1000000000) / in->config.rate + 971 ((int64_t)(in->proc_buf_frames) * 1000000000) / in->requested_rate ); 972 973 /* add delay introduced by resampler */ 974 rsmp_delay = 0; 975 if (in->resampler) { 976 rsmp_delay = in->resampler->delay_ns(in->resampler); 977 } 978 979 kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / in->config.rate); 980 981 delay_ns = kernel_delay + buf_delay + rsmp_delay; 982 983 buffer->time_stamp = tstamp; 984 buffer->delay_ns = delay_ns; 985 ALOGVV("get_capture_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames:[%5d]," 986 " delay_ns: [%d], kernel_delay:[%ld], buf_delay:[%ld], rsmp_delay:[%ld], " 987 "in->read_buf_frames:[%zd], in->proc_buf_frames:[%zd], frames:[%zd]", 988 buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, kernel_frames, 989 buffer->delay_ns, kernel_delay, buf_delay, rsmp_delay, 990 in->read_buf_frames, in->proc_buf_frames, frames); 991 } 992 993 static int32_t update_echo_reference(struct stream_in *in, size_t frames) 994 { 995 ALOGVV("%s: enter:), in->config.channels(%d)", __func__,in->config.channels); 996 struct echo_reference_buffer b; 997 b.delay_ns = 0; 998 struct pcm_device *pcm_device; 999 1000 pcm_device = node_to_item(list_head(&in->pcm_dev_list), 1001 struct pcm_device, stream_list_node); 1002 1003 ALOGVV("update_echo_reference, in->config.channels(%d), frames = [%zd], in->ref_buf_frames = [%zd], " 1004 "b.frame_count = [%zd]", 1005 in->config.channels, frames, in->ref_buf_frames, frames - in->ref_buf_frames); 1006 if (in->ref_buf_frames < frames) { 1007 if (in->ref_buf_size < frames) { 1008 in->ref_buf_size = frames; 1009 in->ref_buf = (int16_t *)realloc(in->ref_buf, pcm_frames_to_bytes(pcm_device->pcm, frames)); 1010 ALOG_ASSERT((in->ref_buf != NULL), 1011 "update_echo_reference() failed to reallocate ref_buf"); 1012 ALOGVV("update_echo_reference(): ref_buf %p extended to %d bytes", 1013 in->ref_buf, pcm_frames_to_bytes(pcm_device->pcm, frames)); 1014 } 1015 b.frame_count = frames - in->ref_buf_frames; 1016 b.raw = (void *)(in->ref_buf + in->ref_buf_frames * in->config.channels); 1017 1018 get_capture_delay(in, frames, &b); 1019 1020 if (in->echo_reference->read(in->echo_reference, &b) == 0) 1021 { 1022 in->ref_buf_frames += b.frame_count; 1023 ALOGVV("update_echo_reference(): in->ref_buf_frames:[%zd], " 1024 "in->ref_buf_size:[%zd], frames:[%zd], b.frame_count:[%zd]", 1025 in->ref_buf_frames, in->ref_buf_size, frames, b.frame_count); 1026 } 1027 } else 1028 ALOGW("update_echo_reference(): NOT enough frames to read ref buffer"); 1029 return b.delay_ns; 1030 } 1031 1032 static int set_preprocessor_param(effect_handle_t handle, 1033 effect_param_t *param) 1034 { 1035 uint32_t size = sizeof(int); 1036 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + 1037 param->vsize; 1038 1039 int status = (*handle)->command(handle, 1040 EFFECT_CMD_SET_PARAM, 1041 sizeof (effect_param_t) + psize, 1042 param, 1043 &size, 1044 ¶m->status); 1045 if (status == 0) 1046 status = param->status; 1047 1048 return status; 1049 } 1050 1051 static int set_preprocessor_echo_delay(effect_handle_t handle, 1052 int32_t delay_us) 1053 { 1054 struct { 1055 effect_param_t param; 1056 uint32_t data_0; 1057 int32_t data_1; 1058 } buf; 1059 memset(&buf, 0, sizeof(buf)); 1060 1061 buf.param.psize = sizeof(uint32_t); 1062 buf.param.vsize = sizeof(uint32_t); 1063 buf.data_0 = AEC_PARAM_ECHO_DELAY; 1064 buf.data_1 = delay_us; 1065 1066 return set_preprocessor_param(handle, &buf.param); 1067 } 1068 1069 static void push_echo_reference(struct stream_in *in, size_t frames) 1070 { 1071 ALOGVV("%s: enter:)", __func__); 1072 /* read frames from echo reference buffer and update echo delay 1073 * in->ref_buf_frames is updated with frames available in in->ref_buf */ 1074 1075 int32_t delay_us = update_echo_reference(in, frames)/1000; 1076 int32_t size_in_bytes = 0; 1077 int i; 1078 audio_buffer_t buf; 1079 1080 if (in->ref_buf_frames < frames) 1081 frames = in->ref_buf_frames; 1082 1083 buf.frameCount = frames; 1084 buf.raw = in->ref_buf; 1085 1086 for (i = 0; i < in->num_preprocessors; i++) { 1087 if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL) 1088 continue; 1089 ALOGVV("%s: effect_itfe)->process_reverse() BEGIN i=(%d) ", __func__, i); 1090 (*in->preprocessors[i].effect_itfe)->process_reverse(in->preprocessors[i].effect_itfe, 1091 &buf, 1092 NULL); 1093 ALOGVV("%s: effect_itfe)->process_reverse() END i=(%d) ", __func__, i); 1094 set_preprocessor_echo_delay(in->preprocessors[i].effect_itfe, delay_us); 1095 } 1096 1097 in->ref_buf_frames -= buf.frameCount; 1098 ALOGVV("%s: in->ref_buf_frames(%zd), in->config.channels(%d) ", 1099 __func__, in->ref_buf_frames, in->config.channels); 1100 if (in->ref_buf_frames) { 1101 memcpy(in->ref_buf, 1102 in->ref_buf + buf.frameCount * in->config.channels, 1103 in->ref_buf_frames * in->config.channels * sizeof(int16_t)); 1104 } 1105 } 1106 1107 static void put_echo_reference(struct audio_device *adev, 1108 struct echo_reference_itfe *reference) 1109 { 1110 ALOGV("%s: enter:)", __func__); 1111 int32_t prev_generation = adev->echo_reference_generation; 1112 struct stream_out *out = adev->primary_output; 1113 1114 if (adev->echo_reference != NULL && 1115 reference == adev->echo_reference) { 1116 /* echo reference is taken from the low latency output stream used 1117 * for voice use cases */ 1118 adev->echo_reference = NULL; 1119 android_atomic_inc(&adev->echo_reference_generation); 1120 if (out != NULL && out->usecase == USECASE_AUDIO_PLAYBACK) { 1121 // if the primary output is in standby or did not pick the echo reference yet 1122 // we can safely get rid of it here. 1123 // otherwise, out_write() or out_standby() will detect the change in echo reference 1124 // generation and release the echo reference owned by the stream. 1125 if ((out->echo_reference_generation != prev_generation) || out->standby) 1126 release_echo_reference(reference); 1127 } else { 1128 release_echo_reference(reference); 1129 } 1130 ALOGV("release_echo_reference"); 1131 } 1132 } 1133 1134 static struct echo_reference_itfe *get_echo_reference(struct audio_device *adev, 1135 audio_format_t format __unused, 1136 uint32_t channel_count, 1137 uint32_t sampling_rate) 1138 { 1139 ALOGV("%s: enter:)", __func__); 1140 put_echo_reference(adev, adev->echo_reference); 1141 /* echo reference is taken from the low latency output stream used 1142 * for voice use cases */ 1143 if (adev->primary_output!= NULL && adev->primary_output->usecase == USECASE_AUDIO_PLAYBACK && 1144 !adev->primary_output->standby) { 1145 struct audio_stream *stream = 1146 &adev->primary_output->stream.common; 1147 uint32_t wr_channel_count = audio_channel_count_from_out_mask(stream->get_channels(stream)); 1148 uint32_t wr_sampling_rate = stream->get_sample_rate(stream); 1149 ALOGV("Calling create_echo_reference"); 1150 int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT, 1151 channel_count, 1152 sampling_rate, 1153 AUDIO_FORMAT_PCM_16_BIT, 1154 wr_channel_count, 1155 wr_sampling_rate, 1156 &adev->echo_reference); 1157 if (status == 0) 1158 android_atomic_inc(&adev->echo_reference_generation); 1159 } 1160 return adev->echo_reference; 1161 } 1162 1163 #ifdef HW_AEC_LOOPBACK 1164 static int get_hw_echo_reference(struct stream_in *in) 1165 { 1166 struct pcm_device_profile *ref_pcm_profile; 1167 struct pcm_device *ref_device; 1168 struct audio_device *adev = in->dev; 1169 1170 in->hw_echo_reference = false; 1171 1172 if (adev->primary_output!= NULL && 1173 !adev->primary_output->standby && 1174 adev->primary_output->usecase == USECASE_AUDIO_PLAYBACK && 1175 adev->primary_output->devices == AUDIO_DEVICE_OUT_SPEAKER) { 1176 struct audio_stream *stream = &adev->primary_output->stream.common; 1177 1178 ref_pcm_profile = get_pcm_device(PCM_CAPTURE, pcm_device_capture_loopback_aec.devices); 1179 if (ref_pcm_profile == NULL) { 1180 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 1181 __func__, pcm_device_capture_loopback_aec.devices); 1182 return -EINVAL; 1183 } 1184 1185 if (in->input_flags & AUDIO_INPUT_FLAG_FAST) { 1186 ALOGV("%s: change capture period size to low latency size %d", 1187 __func__, CAPTURE_PERIOD_SIZE_LOW_LATENCY); 1188 ref_pcm_profile->config.period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY; 1189 } 1190 1191 ref_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device)); 1192 ref_device->pcm_profile = ref_pcm_profile; 1193 1194 ALOGV("%s: ref_device rate:%d, ch:%d", __func__, ref_pcm_profile->config.rate, ref_pcm_profile->config.channels); 1195 ref_device->pcm = pcm_open(ref_device->pcm_profile->card, ref_device->pcm_profile->id, PCM_IN | PCM_MONOTONIC, &ref_device->pcm_profile->config); 1196 1197 if (ref_device->pcm && !pcm_is_ready(ref_device->pcm)) { 1198 ALOGE("%s: %s", __func__, pcm_get_error(ref_device->pcm)); 1199 pcm_close(ref_device->pcm); 1200 ref_device->pcm = NULL; 1201 return -EIO; 1202 } 1203 list_add_tail(&in->pcm_dev_list, &ref_device->stream_list_node); 1204 1205 in->hw_echo_reference = true; 1206 1207 ALOGV("%s: hw_echo_reference is true", __func__); 1208 } 1209 1210 return 0; 1211 } 1212 #endif 1213 1214 static int get_playback_delay(struct stream_out *out, 1215 size_t frames, 1216 struct echo_reference_buffer *buffer) 1217 { 1218 unsigned int kernel_frames; 1219 int status; 1220 int primary_pcm = 0; 1221 struct pcm_device *pcm_device; 1222 1223 pcm_device = node_to_item(list_head(&out->pcm_dev_list), 1224 struct pcm_device, stream_list_node); 1225 1226 status = pcm_get_htimestamp(pcm_device->pcm, &kernel_frames, &buffer->time_stamp); 1227 if (status < 0) { 1228 buffer->time_stamp.tv_sec = 0; 1229 buffer->time_stamp.tv_nsec = 0; 1230 buffer->delay_ns = 0; 1231 ALOGV("get_playback_delay(): pcm_get_htimestamp error," 1232 "setting playbackTimestamp to 0"); 1233 return status; 1234 } 1235 1236 kernel_frames = pcm_get_buffer_size(pcm_device->pcm) - kernel_frames; 1237 1238 /* adjust render time stamp with delay added by current driver buffer. 1239 * Add the duration of current frame as we want the render time of the last 1240 * sample being written. */ 1241 buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/ 1242 out->config.rate); 1243 ALOGVV("get_playback_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames: [%5u], delay_ns: [%d],", 1244 buffer->time_stamp.tv_sec, buffer->time_stamp.tv_nsec, kernel_frames, buffer->delay_ns); 1245 1246 return 0; 1247 } 1248 1249 #define GET_COMMAND_STATUS(status, fct_status, cmd_status) \ 1250 do { \ 1251 if (fct_status != 0) \ 1252 status = fct_status; \ 1253 else if (cmd_status != 0) \ 1254 status = cmd_status; \ 1255 } while(0) 1256 1257 static int in_configure_reverse(struct stream_in *in) 1258 { 1259 int32_t cmd_status; 1260 uint32_t size = sizeof(int); 1261 effect_config_t config; 1262 int32_t status = 0; 1263 int32_t fct_status = 0; 1264 int i; 1265 ALOGV("%s: enter: in->num_preprocessors(%d)", __func__, in->num_preprocessors); 1266 if (in->num_preprocessors > 0) { 1267 config.inputCfg.channels = in->main_channels; 1268 config.outputCfg.channels = in->main_channels; 1269 config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 1270 config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 1271 config.inputCfg.samplingRate = in->requested_rate; 1272 config.outputCfg.samplingRate = in->requested_rate; 1273 config.inputCfg.mask = 1274 ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT ); 1275 config.outputCfg.mask = 1276 ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT ); 1277 1278 for (i = 0; i < in->num_preprocessors; i++) 1279 { 1280 if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL) 1281 continue; 1282 fct_status = (*(in->preprocessors[i].effect_itfe))->command( 1283 in->preprocessors[i].effect_itfe, 1284 EFFECT_CMD_SET_CONFIG_REVERSE, 1285 sizeof(effect_config_t), 1286 &config, 1287 &size, 1288 &cmd_status); 1289 ALOGV("%s: calling EFFECT_CMD_SET_CONFIG_REVERSE",__func__); 1290 GET_COMMAND_STATUS(status, fct_status, cmd_status); 1291 } 1292 } 1293 return status; 1294 } 1295 1296 #define MAX_NUM_CHANNEL_CONFIGS 10 1297 1298 static void in_read_audio_effect_channel_configs(struct stream_in *in __unused, 1299 struct effect_info_s *effect_info) 1300 { 1301 /* size and format of the cmd are defined in hardware/audio_effect.h */ 1302 effect_handle_t effect = effect_info->effect_itfe; 1303 uint32_t cmd_size = 2 * sizeof(uint32_t); 1304 uint32_t cmd[] = { EFFECT_FEATURE_AUX_CHANNELS, MAX_NUM_CHANNEL_CONFIGS }; 1305 /* reply = status + number of configs (n) + n x channel_config_t */ 1306 uint32_t reply_size = 1307 2 * sizeof(uint32_t) + (MAX_NUM_CHANNEL_CONFIGS * sizeof(channel_config_t)); 1308 int32_t reply[reply_size]; 1309 int32_t cmd_status; 1310 1311 ALOG_ASSERT((effect_info->num_channel_configs == 0), 1312 "in_read_audio_effect_channel_configs() num_channel_configs not cleared"); 1313 ALOG_ASSERT((effect_info->channel_configs == NULL), 1314 "in_read_audio_effect_channel_configs() channel_configs not cleared"); 1315 1316 /* if this command is not supported, then the effect is supposed to return -EINVAL. 1317 * This error will be interpreted as if the effect supports the main_channels but does not 1318 * support any aux_channels */ 1319 cmd_status = (*effect)->command(effect, 1320 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, 1321 cmd_size, 1322 (void*)&cmd, 1323 &reply_size, 1324 (void*)&reply); 1325 1326 if (cmd_status != 0) { 1327 ALOGV("in_read_audio_effect_channel_configs(): " 1328 "fx->command returned %d", cmd_status); 1329 return; 1330 } 1331 1332 if (reply[0] != 0) { 1333 ALOGW("in_read_audio_effect_channel_configs(): " 1334 "command EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS error %d num configs %d", 1335 reply[0], (reply[0] == -ENOMEM) ? reply[1] : MAX_NUM_CHANNEL_CONFIGS); 1336 return; 1337 } 1338 1339 /* the feature is not supported */ 1340 ALOGV("in_read_audio_effect_channel_configs()(): " 1341 "Feature supported and adding %d channel configs to the list", reply[1]); 1342 effect_info->num_channel_configs = reply[1]; 1343 effect_info->channel_configs = 1344 (channel_config_t *) malloc(sizeof(channel_config_t) * reply[1]); /* n x configs */ 1345 memcpy(effect_info->channel_configs, (reply + 2), sizeof(channel_config_t) * reply[1]); 1346 } 1347 1348 1349 #define NUM_IN_AUX_CNL_CONFIGS 2 1350 channel_config_t in_aux_cnl_configs[NUM_IN_AUX_CNL_CONFIGS] = { 1351 { AUDIO_CHANNEL_IN_FRONT , AUDIO_CHANNEL_IN_BACK}, 1352 { AUDIO_CHANNEL_IN_STEREO , AUDIO_CHANNEL_IN_RIGHT} 1353 }; 1354 static uint32_t in_get_aux_channels(struct stream_in *in) 1355 { 1356 int i; 1357 channel_config_t new_chcfg = {0, 0}; 1358 1359 if (in->num_preprocessors == 0) 1360 return 0; 1361 1362 /* do not enable dual mic configurations when capturing from other microphones than 1363 * main or sub */ 1364 if (!(in->devices & (AUDIO_DEVICE_IN_BUILTIN_MIC | AUDIO_DEVICE_IN_BACK_MIC))) 1365 return 0; 1366 1367 /* retain most complex aux channels configuration compatible with requested main channels and 1368 * supported by audio driver and all pre processors */ 1369 for (i = 0; i < NUM_IN_AUX_CNL_CONFIGS; i++) { 1370 channel_config_t *cur_chcfg = &in_aux_cnl_configs[i]; 1371 if (cur_chcfg->main_channels == in->main_channels) { 1372 size_t match_cnt; 1373 size_t idx_preproc; 1374 for (idx_preproc = 0, match_cnt = 0; 1375 /* no need to continue if at least one preprocessor doesn't match */ 1376 idx_preproc < (size_t)in->num_preprocessors && match_cnt == idx_preproc; 1377 idx_preproc++) { 1378 struct effect_info_s *effect_info = &in->preprocessors[idx_preproc]; 1379 size_t idx_chcfg; 1380 1381 for (idx_chcfg = 0; idx_chcfg < effect_info->num_channel_configs; idx_chcfg++) { 1382 if (memcmp(effect_info->channel_configs + idx_chcfg, 1383 cur_chcfg, 1384 sizeof(channel_config_t)) == 0) { 1385 match_cnt++; 1386 break; 1387 } 1388 } 1389 } 1390 /* if all preprocessors match, we have a candidate */ 1391 if (match_cnt == (size_t)in->num_preprocessors) { 1392 /* retain most complex aux channels configuration */ 1393 if (audio_channel_count_from_in_mask(cur_chcfg->aux_channels) > audio_channel_count_from_in_mask(new_chcfg.aux_channels)) { 1394 new_chcfg = *cur_chcfg; 1395 } 1396 } 1397 } 1398 } 1399 1400 ALOGV("in_get_aux_channels(): return %04x", new_chcfg.aux_channels); 1401 1402 return new_chcfg.aux_channels; 1403 } 1404 1405 static int in_configure_effect_channels(effect_handle_t effect, 1406 channel_config_t *channel_config) 1407 { 1408 int status = 0; 1409 int fct_status; 1410 int32_t cmd_status; 1411 uint32_t reply_size; 1412 effect_config_t config; 1413 uint32_t cmd[(sizeof(uint32_t) + sizeof(channel_config_t) - 1) / sizeof(uint32_t) + 1]; 1414 1415 ALOGV("in_configure_effect_channels(): configure effect with channels: [%04x][%04x]", 1416 channel_config->main_channels, 1417 channel_config->aux_channels); 1418 1419 config.inputCfg.mask = EFFECT_CONFIG_CHANNELS; 1420 config.outputCfg.mask = EFFECT_CONFIG_CHANNELS; 1421 reply_size = sizeof(effect_config_t); 1422 fct_status = (*effect)->command(effect, 1423 EFFECT_CMD_GET_CONFIG, 1424 0, 1425 NULL, 1426 &reply_size, 1427 &config); 1428 if (fct_status != 0) { 1429 ALOGE("in_configure_effect_channels(): EFFECT_CMD_GET_CONFIG failed"); 1430 return fct_status; 1431 } 1432 1433 config.inputCfg.channels = channel_config->main_channels | channel_config->aux_channels; 1434 config.outputCfg.channels = config.inputCfg.channels; 1435 reply_size = sizeof(uint32_t); 1436 fct_status = (*effect)->command(effect, 1437 EFFECT_CMD_SET_CONFIG, 1438 sizeof(effect_config_t), 1439 &config, 1440 &reply_size, 1441 &cmd_status); 1442 GET_COMMAND_STATUS(status, fct_status, cmd_status); 1443 1444 cmd[0] = EFFECT_FEATURE_AUX_CHANNELS; 1445 memcpy(cmd + 1, channel_config, sizeof(channel_config_t)); 1446 reply_size = sizeof(uint32_t); 1447 fct_status = (*effect)->command(effect, 1448 EFFECT_CMD_SET_FEATURE_CONFIG, 1449 sizeof(cmd), //sizeof(uint32_t) + sizeof(channel_config_t), 1450 cmd, 1451 &reply_size, 1452 &cmd_status); 1453 GET_COMMAND_STATUS(status, fct_status, cmd_status); 1454 1455 /* some implementations need to be re-enabled after a config change */ 1456 reply_size = sizeof(uint32_t); 1457 fct_status = (*effect)->command(effect, 1458 EFFECT_CMD_ENABLE, 1459 0, 1460 NULL, 1461 &reply_size, 1462 &cmd_status); 1463 GET_COMMAND_STATUS(status, fct_status, cmd_status); 1464 1465 return status; 1466 } 1467 1468 static int in_reconfigure_channels(struct stream_in *in, 1469 effect_handle_t effect, 1470 channel_config_t *channel_config, 1471 bool config_changed) { 1472 1473 int status = 0; 1474 1475 ALOGV("in_reconfigure_channels(): config_changed %d effect %p", 1476 config_changed, effect); 1477 1478 /* if config changed, reconfigure all previously added effects */ 1479 if (config_changed) { 1480 int i; 1481 ALOGV("%s: config_changed (%d)", __func__, config_changed); 1482 for (i = 0; i < in->num_preprocessors; i++) 1483 { 1484 int cur_status = in_configure_effect_channels(in->preprocessors[i].effect_itfe, 1485 channel_config); 1486 ALOGV("%s: in_configure_effect_channels i=(%d), [main_channel,aux_channel]=[%d|%d], status=%d", 1487 __func__, i, channel_config->main_channels, channel_config->aux_channels, cur_status); 1488 if (cur_status != 0) { 1489 ALOGV("in_reconfigure_channels(): error %d configuring effect " 1490 "%d with channels: [%04x][%04x]", 1491 cur_status, 1492 i, 1493 channel_config->main_channels, 1494 channel_config->aux_channels); 1495 status = cur_status; 1496 } 1497 } 1498 } else if (effect != NULL && channel_config->aux_channels) { 1499 /* if aux channels config did not change but aux channels are present, 1500 * we still need to configure the effect being added */ 1501 status = in_configure_effect_channels(effect, channel_config); 1502 } 1503 return status; 1504 } 1505 1506 static void in_update_aux_channels(struct stream_in *in, 1507 effect_handle_t effect) 1508 { 1509 uint32_t aux_channels; 1510 channel_config_t channel_config; 1511 int status; 1512 1513 aux_channels = in_get_aux_channels(in); 1514 1515 channel_config.main_channels = in->main_channels; 1516 channel_config.aux_channels = aux_channels; 1517 status = in_reconfigure_channels(in, 1518 effect, 1519 &channel_config, 1520 (aux_channels != in->aux_channels)); 1521 1522 if (status != 0) { 1523 ALOGV("in_update_aux_channels(): in_reconfigure_channels error %d", status); 1524 /* resetting aux channels configuration */ 1525 aux_channels = 0; 1526 channel_config.aux_channels = 0; 1527 in_reconfigure_channels(in, effect, &channel_config, true); 1528 } 1529 ALOGV("%s: aux_channels=%d, in->aux_channels_changed=%d", __func__, aux_channels, in->aux_channels_changed); 1530 if (in->aux_channels != aux_channels) { 1531 in->aux_channels_changed = true; 1532 in->aux_channels = aux_channels; 1533 do_in_standby_l(in); 1534 } 1535 } 1536 #endif 1537 1538 /* This function reads PCM data and: 1539 * - resample if needed 1540 * - process if pre-processors are attached 1541 * - discard unwanted channels 1542 */ 1543 static ssize_t read_and_process_frames(struct stream_in *in, void* buffer, ssize_t frames) 1544 { 1545 ssize_t frames_wr = 0; 1546 audio_buffer_t in_buf; 1547 audio_buffer_t out_buf; 1548 size_t src_channels = in->config.channels; 1549 size_t dst_channels = audio_channel_count_from_in_mask(in->main_channels); 1550 int i; 1551 void *proc_buf_out; 1552 struct pcm_device *pcm_device; 1553 bool has_additional_channels = (dst_channels != src_channels) ? true : false; 1554 #ifdef PREPROCESSING_ENABLED 1555 bool has_processing = (in->num_preprocessors != 0) ? true : false; 1556 #endif 1557 1558 /* Additional channels might be added on top of main_channels: 1559 * - aux_channels (by processing effects) 1560 * - extra channels due to HW limitations 1561 * In case of additional channels, we cannot work inplace 1562 */ 1563 if (has_additional_channels) 1564 proc_buf_out = in->proc_buf_out; 1565 else 1566 proc_buf_out = buffer; 1567 1568 if (list_empty(&in->pcm_dev_list)) { 1569 ALOGE("%s: pcm device list empty", __func__); 1570 return -EINVAL; 1571 } 1572 1573 pcm_device = node_to_item(list_head(&in->pcm_dev_list), 1574 struct pcm_device, stream_list_node); 1575 1576 #ifdef PREPROCESSING_ENABLED 1577 if (has_processing) { 1578 /* since all the processing below is done in frames and using the config.channels 1579 * as the number of channels, no changes is required in case aux_channels are present */ 1580 while (frames_wr < frames) { 1581 /* first reload enough frames at the end of process input buffer */ 1582 if (in->proc_buf_frames < (size_t)frames) { 1583 ssize_t frames_rd; 1584 if (in->proc_buf_size < (size_t)frames) { 1585 size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, frames); 1586 in->proc_buf_size = (size_t)frames; 1587 in->proc_buf_in = (int16_t *)realloc(in->proc_buf_in, size_in_bytes); 1588 ALOG_ASSERT((in->proc_buf_in != NULL), 1589 "process_frames() failed to reallocate proc_buf_in"); 1590 if (has_additional_channels) { 1591 in->proc_buf_out = (int16_t *)realloc(in->proc_buf_out, size_in_bytes); 1592 ALOG_ASSERT((in->proc_buf_out != NULL), 1593 "process_frames() failed to reallocate proc_buf_out"); 1594 proc_buf_out = in->proc_buf_out; 1595 } 1596 } 1597 frames_rd = read_frames(in, 1598 in->proc_buf_in + 1599 in->proc_buf_frames * in->config.channels, 1600 frames - in->proc_buf_frames); 1601 if (frames_rd < 0) { 1602 /* Return error code */ 1603 frames_wr = frames_rd; 1604 break; 1605 } 1606 in->proc_buf_frames += frames_rd; 1607 } 1608 1609 if (in->echo_reference != NULL) { 1610 push_echo_reference(in, in->proc_buf_frames); 1611 } 1612 1613 /* in_buf.frameCount and out_buf.frameCount indicate respectively 1614 * the maximum number of frames to be consumed and produced by process() */ 1615 in_buf.frameCount = in->proc_buf_frames; 1616 in_buf.s16 = in->proc_buf_in; 1617 out_buf.frameCount = frames - frames_wr; 1618 out_buf.s16 = (int16_t *)proc_buf_out + frames_wr * in->config.channels; 1619 1620 /* FIXME: this works because of current pre processing library implementation that 1621 * does the actual process only when the last enabled effect process is called. 1622 * The generic solution is to have an output buffer for each effect and pass it as 1623 * input to the next. 1624 */ 1625 for (i = 0; i < in->num_preprocessors; i++) { 1626 (*in->preprocessors[i].effect_itfe)->process(in->preprocessors[i].effect_itfe, 1627 &in_buf, 1628 &out_buf); 1629 } 1630 1631 /* process() has updated the number of frames consumed and produced in 1632 * in_buf.frameCount and out_buf.frameCount respectively 1633 * move remaining frames to the beginning of in->proc_buf_in */ 1634 in->proc_buf_frames -= in_buf.frameCount; 1635 1636 if (in->proc_buf_frames) { 1637 memcpy(in->proc_buf_in, 1638 in->proc_buf_in + in_buf.frameCount * in->config.channels, 1639 in->proc_buf_frames * in->config.channels * sizeof(int16_t)); 1640 } 1641 1642 /* if not enough frames were passed to process(), read more and retry. */ 1643 if (out_buf.frameCount == 0) { 1644 ALOGW("No frames produced by preproc"); 1645 continue; 1646 } 1647 1648 if ((frames_wr + (ssize_t)out_buf.frameCount) <= frames) { 1649 frames_wr += out_buf.frameCount; 1650 } else { 1651 /* The effect does not comply to the API. In theory, we should never end up here! */ 1652 ALOGE("preprocessing produced too many frames: %d + %zd > %d !", 1653 (unsigned int)frames_wr, out_buf.frameCount, (unsigned int)frames); 1654 frames_wr = frames; 1655 } 1656 } 1657 } 1658 else 1659 #endif //PREPROCESSING_ENABLED 1660 { 1661 /* No processing effects attached */ 1662 if (has_additional_channels) { 1663 /* With additional channels, we cannot use original buffer */ 1664 if (in->proc_buf_size < (size_t)frames) { 1665 size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, frames); 1666 in->proc_buf_size = (size_t)frames; 1667 in->proc_buf_out = (int16_t *)realloc(in->proc_buf_out, size_in_bytes); 1668 ALOG_ASSERT((in->proc_buf_out != NULL), 1669 "process_frames() failed to reallocate proc_buf_out"); 1670 proc_buf_out = in->proc_buf_out; 1671 } 1672 } 1673 frames_wr = read_frames(in, proc_buf_out, frames); 1674 } 1675 1676 /* Remove all additional channels that have been added on top of main_channels: 1677 * - aux_channels 1678 * - extra channels from HW due to HW limitations 1679 * Assumption is made that the channels are interleaved and that the main 1680 * channels are first. */ 1681 1682 if (has_additional_channels) 1683 { 1684 int16_t* src_buffer = (int16_t *)proc_buf_out; 1685 int16_t* dst_buffer = (int16_t *)buffer; 1686 1687 if (dst_channels == 1) { 1688 for (i = frames_wr; i > 0; i--) 1689 { 1690 *dst_buffer++ = *src_buffer; 1691 src_buffer += src_channels; 1692 } 1693 } else { 1694 for (i = frames_wr; i > 0; i--) 1695 { 1696 memcpy(dst_buffer, src_buffer, dst_channels*sizeof(int16_t)); 1697 dst_buffer += dst_channels; 1698 src_buffer += src_channels; 1699 } 1700 } 1701 } 1702 1703 return frames_wr; 1704 } 1705 1706 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider, 1707 struct resampler_buffer* buffer) 1708 { 1709 struct stream_in *in; 1710 struct pcm_device *pcm_device; 1711 1712 if (buffer_provider == NULL || buffer == NULL) 1713 return -EINVAL; 1714 1715 in = (struct stream_in *)((char *)buffer_provider - 1716 offsetof(struct stream_in, buf_provider)); 1717 1718 if (list_empty(&in->pcm_dev_list)) { 1719 buffer->raw = NULL; 1720 buffer->frame_count = 0; 1721 in->read_status = -ENODEV; 1722 return -ENODEV; 1723 } 1724 1725 pcm_device = node_to_item(list_head(&in->pcm_dev_list), 1726 struct pcm_device, stream_list_node); 1727 1728 if (in->read_buf_frames == 0) { 1729 size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, in->config.period_size); 1730 if (in->read_buf_size < in->config.period_size) { 1731 in->read_buf_size = in->config.period_size; 1732 in->read_buf = (int16_t *) realloc(in->read_buf, size_in_bytes); 1733 ALOG_ASSERT((in->read_buf != NULL), 1734 "get_next_buffer() failed to reallocate read_buf"); 1735 } 1736 1737 in->read_status = pcm_read(pcm_device->pcm, (void*)in->read_buf, size_in_bytes); 1738 1739 if (in->read_status != 0) { 1740 ALOGE("get_next_buffer() pcm_read error %d", in->read_status); 1741 buffer->raw = NULL; 1742 buffer->frame_count = 0; 1743 return in->read_status; 1744 } 1745 in->read_buf_frames = in->config.period_size; 1746 1747 #ifdef PREPROCESSING_ENABLED 1748 #ifdef HW_AEC_LOOPBACK 1749 if (in->hw_echo_reference) { 1750 struct pcm_device *temp_device = NULL; 1751 struct pcm_device *ref_device = NULL; 1752 struct listnode *node = NULL; 1753 struct echo_reference_buffer b; 1754 size_t size_hw_ref_bytes; 1755 size_t size_hw_ref_frames; 1756 int read_status = 0; 1757 1758 ref_device = node_to_item(list_tail(&in->pcm_dev_list), 1759 struct pcm_device, stream_list_node); 1760 list_for_each(node, &in->pcm_dev_list) { 1761 temp_device = node_to_item(node, struct pcm_device, stream_list_node); 1762 if (temp_device->pcm_profile->id == 1) { 1763 ref_device = temp_device; 1764 break; 1765 } 1766 } 1767 if (ref_device) { 1768 size_hw_ref_bytes = pcm_frames_to_bytes(ref_device->pcm, ref_device->pcm_profile->config.period_size); 1769 size_hw_ref_frames = ref_device->pcm_profile->config.period_size; 1770 if (in->hw_ref_buf_size < size_hw_ref_frames) { 1771 in->hw_ref_buf_size = size_hw_ref_frames; 1772 in->hw_ref_buf = (int16_t *) realloc(in->hw_ref_buf, size_hw_ref_bytes); 1773 ALOG_ASSERT((in->hw_ref_buf != NULL), 1774 "get_next_buffer() failed to reallocate hw_ref_buf"); 1775 ALOGV("get_next_buffer(): hw_ref_buf %p extended to %zd bytes", 1776 in->hw_ref_buf, size_hw_ref_bytes); 1777 } 1778 1779 read_status = pcm_read(ref_device->pcm, (void*)in->hw_ref_buf, size_hw_ref_bytes); 1780 if (read_status != 0) { 1781 ALOGE("process_frames() pcm_read error for HW reference %d", read_status); 1782 b.raw = NULL; 1783 b.frame_count = 0; 1784 } 1785 else { 1786 get_capture_reference_delay(in, size_hw_ref_frames, &b); 1787 b.raw = (void *)in->hw_ref_buf; 1788 b.frame_count = size_hw_ref_frames; 1789 if (b.delay_ns != 0) 1790 b.delay_ns = -b.delay_ns; // as this is capture delay, it needs to be subtracted from the microphone delay 1791 in->echo_reference->write(in->echo_reference, &b); 1792 } 1793 } 1794 } 1795 #endif // HW_AEC_LOOPBACK 1796 #endif // PREPROCESSING_ENABLED 1797 } 1798 1799 buffer->frame_count = (buffer->frame_count > in->read_buf_frames) ? 1800 in->read_buf_frames : buffer->frame_count; 1801 buffer->i16 = in->read_buf + (in->config.period_size - in->read_buf_frames) * 1802 in->config.channels; 1803 return in->read_status; 1804 } 1805 1806 static void release_buffer(struct resampler_buffer_provider *buffer_provider, 1807 struct resampler_buffer* buffer) 1808 { 1809 struct stream_in *in; 1810 1811 if (buffer_provider == NULL || buffer == NULL) 1812 return; 1813 1814 in = (struct stream_in *)((char *)buffer_provider - 1815 offsetof(struct stream_in, buf_provider)); 1816 1817 in->read_buf_frames -= buffer->frame_count; 1818 } 1819 1820 /* read_frames() reads frames from kernel driver, down samples to capture rate 1821 * if necessary and output the number of frames requested to the buffer specified */ 1822 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames) 1823 { 1824 ssize_t frames_wr = 0; 1825 1826 struct pcm_device *pcm_device; 1827 1828 if (list_empty(&in->pcm_dev_list)) { 1829 ALOGE("%s: pcm device list empty", __func__); 1830 return -EINVAL; 1831 } 1832 1833 pcm_device = node_to_item(list_head(&in->pcm_dev_list), 1834 struct pcm_device, stream_list_node); 1835 1836 while (frames_wr < frames) { 1837 size_t frames_rd = frames - frames_wr; 1838 ALOGVV("%s: frames_rd: %zd, frames_wr: %zd, in->config.channels: %d", 1839 __func__,frames_rd,frames_wr,in->config.channels); 1840 if (in->resampler != NULL) { 1841 in->resampler->resample_from_provider(in->resampler, 1842 (int16_t *)((char *)buffer + 1843 pcm_frames_to_bytes(pcm_device->pcm, frames_wr)), 1844 &frames_rd); 1845 } else { 1846 struct resampler_buffer buf = { 1847 { raw : NULL, }, 1848 frame_count : frames_rd, 1849 }; 1850 get_next_buffer(&in->buf_provider, &buf); 1851 if (buf.raw != NULL) { 1852 memcpy((char *)buffer + 1853 pcm_frames_to_bytes(pcm_device->pcm, frames_wr), 1854 buf.raw, 1855 pcm_frames_to_bytes(pcm_device->pcm, buf.frame_count)); 1856 frames_rd = buf.frame_count; 1857 } 1858 release_buffer(&in->buf_provider, &buf); 1859 } 1860 /* in->read_status is updated by getNextBuffer() also called by 1861 * in->resampler->resample_from_provider() */ 1862 if (in->read_status != 0) 1863 return in->read_status; 1864 1865 frames_wr += frames_rd; 1866 } 1867 return frames_wr; 1868 } 1869 1870 static int in_release_pcm_devices(struct stream_in *in) 1871 { 1872 struct pcm_device *pcm_device; 1873 struct listnode *node; 1874 struct listnode *next; 1875 1876 list_for_each_safe(node, next, &in->pcm_dev_list) { 1877 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 1878 list_remove(node); 1879 free(pcm_device); 1880 } 1881 1882 return 0; 1883 } 1884 1885 static int stop_input_stream(struct stream_in *in) 1886 { 1887 struct audio_usecase *uc_info; 1888 struct audio_device *adev = in->dev; 1889 1890 adev->active_input = NULL; 1891 ALOGV("%s: enter: usecase(%d: %s)", __func__, 1892 in->usecase, use_case_table[in->usecase]); 1893 uc_info = get_usecase_from_id(adev, in->usecase); 1894 if (uc_info == NULL) { 1895 ALOGE("%s: Could not find the usecase (%d) in the list", 1896 __func__, in->usecase); 1897 return -EINVAL; 1898 } 1899 1900 /* Disable the tx device */ 1901 disable_snd_device(adev, uc_info, uc_info->in_snd_device, true); 1902 1903 list_remove(&uc_info->adev_list_node); 1904 free(uc_info); 1905 1906 if (list_empty(&in->pcm_dev_list)) { 1907 ALOGE("%s: pcm device list empty", __func__); 1908 return -EINVAL; 1909 } 1910 1911 in_release_pcm_devices(in); 1912 list_init(&in->pcm_dev_list); 1913 1914 #ifdef HW_AEC_LOOPBACK 1915 if (in->hw_echo_reference) 1916 { 1917 in->hw_echo_reference = false; 1918 } 1919 #endif 1920 1921 ALOGV("%s: exit", __func__); 1922 return 0; 1923 } 1924 1925 int start_input_stream(struct stream_in *in) 1926 { 1927 /* Enable output device and stream routing controls */ 1928 int ret = 0; 1929 bool recreate_resampler = false; 1930 struct audio_usecase *uc_info; 1931 struct audio_device *adev = in->dev; 1932 struct pcm_device_profile *pcm_profile; 1933 struct pcm_device *pcm_device; 1934 1935 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); 1936 adev->active_input = in; 1937 pcm_profile = get_pcm_device(in->usecase == USECASE_AUDIO_CAPTURE_HOTWORD 1938 ? PCM_HOTWORD_STREAMING : PCM_CAPTURE, in->devices); 1939 if (pcm_profile == NULL) { 1940 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 1941 __func__, in->usecase); 1942 ret = -EINVAL; 1943 goto error_config; 1944 } 1945 1946 if (in->input_flags & AUDIO_INPUT_FLAG_FAST) { 1947 ALOGV("%s: change capture period size to low latency size %d", 1948 __func__, CAPTURE_PERIOD_SIZE_LOW_LATENCY); 1949 pcm_profile->config.period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY; 1950 } 1951 1952 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 1953 uc_info->id = in->usecase; 1954 uc_info->type = PCM_CAPTURE; 1955 uc_info->stream = (struct audio_stream *)in; 1956 uc_info->devices = in->devices; 1957 uc_info->in_snd_device = SND_DEVICE_NONE; 1958 uc_info->out_snd_device = SND_DEVICE_NONE; 1959 1960 pcm_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device)); 1961 pcm_device->pcm_profile = pcm_profile; 1962 list_init(&in->pcm_dev_list); 1963 list_add_tail(&in->pcm_dev_list, &pcm_device->stream_list_node); 1964 1965 list_init(&uc_info->mixer_list); 1966 list_add_tail(&uc_info->mixer_list, 1967 &adev_get_mixer_for_card(adev, 1968 pcm_device->pcm_profile->card)->uc_list_node[uc_info->id]); 1969 1970 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node); 1971 1972 select_devices(adev, in->usecase); 1973 1974 /* Config should be updated as profile can be changed between different calls 1975 * to this function: 1976 * - Trigger resampler creation 1977 * - Config needs to be updated */ 1978 if (in->config.rate != pcm_profile->config.rate) { 1979 recreate_resampler = true; 1980 } 1981 in->config = pcm_profile->config; 1982 1983 #ifdef PREPROCESSING_ENABLED 1984 if (in->aux_channels_changed) { 1985 in->config.channels = audio_channel_count_from_in_mask(in->main_channels | in->aux_channels); 1986 recreate_resampler = true; 1987 } 1988 #endif 1989 1990 if (in->requested_rate != in->config.rate) { 1991 recreate_resampler = true; 1992 } 1993 1994 if (recreate_resampler) { 1995 if (in->resampler) { 1996 release_resampler(in->resampler); 1997 in->resampler = NULL; 1998 } 1999 in->buf_provider.get_next_buffer = get_next_buffer; 2000 in->buf_provider.release_buffer = release_buffer; 2001 ret = create_resampler(in->config.rate, 2002 in->requested_rate, 2003 in->config.channels, 2004 RESAMPLER_QUALITY_DEFAULT, 2005 &in->buf_provider, 2006 &in->resampler); 2007 } 2008 2009 #ifdef PREPROCESSING_ENABLED 2010 if (in->enable_aec && in->echo_reference == NULL) { 2011 in->echo_reference = get_echo_reference(adev, 2012 AUDIO_FORMAT_PCM_16_BIT, 2013 audio_channel_count_from_in_mask(in->main_channels), 2014 in->requested_rate 2015 ); 2016 } 2017 2018 #ifdef HW_AEC_LOOPBACK 2019 if (in->enable_aec) { 2020 ret = get_hw_echo_reference(in); 2021 if (ret!=0) 2022 goto error_open; 2023 2024 /* force ref buffer reallocation */ 2025 in->hw_ref_buf_size = 0; 2026 } 2027 #endif 2028 #endif 2029 2030 /* Open the PCM device. 2031 * The HW is limited to support only the default pcm_profile settings. 2032 * As such a change in aux_channels will not have an effect. 2033 */ 2034 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d, smp rate %d format %d, \ 2035 period_size %d", __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, 2036 pcm_device->pcm_profile->config.channels,pcm_device->pcm_profile->config.rate, 2037 pcm_device->pcm_profile->config.format, pcm_device->pcm_profile->config.period_size); 2038 2039 if (pcm_profile->type == PCM_HOTWORD_STREAMING) { 2040 if (!adev->sound_trigger_open_for_streaming) { 2041 ALOGE("%s: No handle to sound trigger HAL", __func__); 2042 ret = -EIO; 2043 goto error_open; 2044 } 2045 pcm_device->pcm = NULL; 2046 pcm_device->sound_trigger_handle = adev->sound_trigger_open_for_streaming(); 2047 if (pcm_device->sound_trigger_handle <= 0) { 2048 ALOGE("%s: Failed to open DSP for streaming", __func__); 2049 ret = -EIO; 2050 goto error_open; 2051 } 2052 ALOGV("Opened DSP successfully"); 2053 } else { 2054 pcm_device->sound_trigger_handle = 0; 2055 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, 2056 PCM_IN | PCM_MONOTONIC, &pcm_device->pcm_profile->config); 2057 2058 if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) { 2059 ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm)); 2060 pcm_close(pcm_device->pcm); 2061 pcm_device->pcm = NULL; 2062 ret = -EIO; 2063 goto error_open; 2064 } 2065 } 2066 2067 /* force read and proc buffer reallocation in case of frame size or 2068 * channel count change */ 2069 in->proc_buf_frames = 0; 2070 in->proc_buf_size = 0; 2071 in->read_buf_size = 0; 2072 in->read_buf_frames = 0; 2073 2074 /* if no supported sample rate is available, use the resampler */ 2075 if (in->resampler) { 2076 in->resampler->reset(in->resampler); 2077 } 2078 2079 ALOGV("%s: exit", __func__); 2080 return ret; 2081 2082 error_open: 2083 if (in->resampler) { 2084 release_resampler(in->resampler); 2085 in->resampler = NULL; 2086 } 2087 stop_input_stream(in); 2088 2089 error_config: 2090 ALOGD("%s: exit: status(%d)", __func__, ret); 2091 adev->active_input = NULL; 2092 return ret; 2093 } 2094 2095 /* must be called with out->lock locked */ 2096 static int send_offload_cmd_l(struct stream_out* out, int command) 2097 { 2098 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); 2099 2100 ALOGVV("%s %d", __func__, command); 2101 2102 cmd->cmd = command; 2103 list_add_tail(&out->offload_cmd_list, &cmd->node); 2104 pthread_cond_signal(&out->offload_cond); 2105 return 0; 2106 } 2107 2108 /* must be called iwth out->lock locked */ 2109 static void stop_compressed_output_l(struct stream_out *out) 2110 { 2111 out->offload_state = OFFLOAD_STATE_IDLE; 2112 out->playback_started = 0; 2113 out->send_new_metadata = 1; 2114 if (out->compr != NULL) { 2115 compress_stop(out->compr); 2116 while (out->offload_thread_blocked) { 2117 pthread_cond_wait(&out->cond, &out->lock); 2118 } 2119 } 2120 } 2121 2122 static void *offload_thread_loop(void *context) 2123 { 2124 struct stream_out *out = (struct stream_out *) context; 2125 struct listnode *item; 2126 2127 out->offload_state = OFFLOAD_STATE_IDLE; 2128 out->playback_started = 0; 2129 2130 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); 2131 set_sched_policy(0, SP_FOREGROUND); 2132 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); 2133 2134 ALOGV("%s", __func__); 2135 pthread_mutex_lock(&out->lock); 2136 for (;;) { 2137 struct offload_cmd *cmd = NULL; 2138 stream_callback_event_t event; 2139 bool send_callback = false; 2140 2141 ALOGVV("%s offload_cmd_list %d out->offload_state %d", 2142 __func__, list_empty(&out->offload_cmd_list), 2143 out->offload_state); 2144 if (list_empty(&out->offload_cmd_list)) { 2145 ALOGV("%s SLEEPING", __func__); 2146 pthread_cond_wait(&out->offload_cond, &out->lock); 2147 ALOGV("%s RUNNING", __func__); 2148 continue; 2149 } 2150 2151 item = list_head(&out->offload_cmd_list); 2152 cmd = node_to_item(item, struct offload_cmd, node); 2153 list_remove(item); 2154 2155 ALOGVV("%s STATE %d CMD %d out->compr %p", 2156 __func__, out->offload_state, cmd->cmd, out->compr); 2157 2158 if (cmd->cmd == OFFLOAD_CMD_EXIT) { 2159 free(cmd); 2160 break; 2161 } 2162 2163 if (out->compr == NULL) { 2164 ALOGE("%s: Compress handle is NULL", __func__); 2165 pthread_cond_signal(&out->cond); 2166 continue; 2167 } 2168 out->offload_thread_blocked = true; 2169 pthread_mutex_unlock(&out->lock); 2170 send_callback = false; 2171 switch(cmd->cmd) { 2172 case OFFLOAD_CMD_WAIT_FOR_BUFFER: 2173 compress_wait(out->compr, -1); 2174 send_callback = true; 2175 event = STREAM_CBK_EVENT_WRITE_READY; 2176 break; 2177 case OFFLOAD_CMD_PARTIAL_DRAIN: 2178 compress_next_track(out->compr); 2179 compress_partial_drain(out->compr); 2180 send_callback = true; 2181 event = STREAM_CBK_EVENT_DRAIN_READY; 2182 break; 2183 case OFFLOAD_CMD_DRAIN: 2184 compress_drain(out->compr); 2185 send_callback = true; 2186 event = STREAM_CBK_EVENT_DRAIN_READY; 2187 break; 2188 default: 2189 ALOGE("%s unknown command received: %d", __func__, cmd->cmd); 2190 break; 2191 } 2192 pthread_mutex_lock(&out->lock); 2193 out->offload_thread_blocked = false; 2194 pthread_cond_signal(&out->cond); 2195 if (send_callback) { 2196 out->offload_callback(event, NULL, out->offload_cookie); 2197 } 2198 free(cmd); 2199 } 2200 2201 pthread_cond_signal(&out->cond); 2202 while (!list_empty(&out->offload_cmd_list)) { 2203 item = list_head(&out->offload_cmd_list); 2204 list_remove(item); 2205 free(node_to_item(item, struct offload_cmd, node)); 2206 } 2207 pthread_mutex_unlock(&out->lock); 2208 2209 return NULL; 2210 } 2211 2212 static int create_offload_callback_thread(struct stream_out *out) 2213 { 2214 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); 2215 list_init(&out->offload_cmd_list); 2216 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, 2217 offload_thread_loop, out); 2218 return 0; 2219 } 2220 2221 static int destroy_offload_callback_thread(struct stream_out *out) 2222 { 2223 pthread_mutex_lock(&out->lock); 2224 stop_compressed_output_l(out); 2225 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); 2226 2227 pthread_mutex_unlock(&out->lock); 2228 pthread_join(out->offload_thread, (void **) NULL); 2229 pthread_cond_destroy(&out->offload_cond); 2230 2231 return 0; 2232 } 2233 2234 static int uc_release_pcm_devices(struct audio_usecase *usecase) 2235 { 2236 struct stream_out *out = (struct stream_out *)usecase->stream; 2237 struct pcm_device *pcm_device; 2238 struct listnode *node; 2239 struct listnode *next; 2240 2241 list_for_each_safe(node, next, &out->pcm_dev_list) { 2242 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 2243 list_remove(node); 2244 free(pcm_device); 2245 } 2246 list_init(&usecase->mixer_list); 2247 2248 return 0; 2249 } 2250 2251 static int uc_select_pcm_devices(struct audio_usecase *usecase) 2252 2253 { 2254 struct stream_out *out = (struct stream_out *)usecase->stream; 2255 struct pcm_device *pcm_device; 2256 struct pcm_device_profile *pcm_profile; 2257 struct mixer_card *mixer_card; 2258 audio_devices_t devices = usecase->devices; 2259 2260 list_init(&usecase->mixer_list); 2261 list_init(&out->pcm_dev_list); 2262 2263 while ((pcm_profile = get_pcm_device(usecase->type, devices)) != NULL) { 2264 pcm_device = calloc(1, sizeof(struct pcm_device)); 2265 pcm_device->pcm_profile = pcm_profile; 2266 list_add_tail(&out->pcm_dev_list, &pcm_device->stream_list_node); 2267 mixer_card = uc_get_mixer_for_card(usecase, pcm_profile->card); 2268 if (mixer_card == NULL) { 2269 mixer_card = adev_get_mixer_for_card(out->dev, pcm_profile->card); 2270 list_add_tail(&usecase->mixer_list, &mixer_card->uc_list_node[usecase->id]); 2271 } 2272 devices &= ~pcm_profile->devices; 2273 } 2274 2275 return 0; 2276 } 2277 2278 static int out_close_pcm_devices(struct stream_out *out) 2279 { 2280 struct pcm_device *pcm_device; 2281 struct listnode *node; 2282 struct audio_device *adev = out->dev; 2283 2284 list_for_each(node, &out->pcm_dev_list) { 2285 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 2286 if (pcm_device->sound_trigger_handle > 0) { 2287 adev->sound_trigger_close_for_streaming(pcm_device->sound_trigger_handle); 2288 pcm_device->sound_trigger_handle = 0; 2289 } 2290 if (pcm_device->pcm) { 2291 pcm_close(pcm_device->pcm); 2292 pcm_device->pcm = NULL; 2293 } 2294 if (pcm_device->resampler) { 2295 release_resampler(pcm_device->resampler); 2296 pcm_device->resampler = NULL; 2297 } 2298 if (pcm_device->res_buffer) { 2299 free(pcm_device->res_buffer); 2300 pcm_device->res_buffer = NULL; 2301 } 2302 } 2303 2304 return 0; 2305 } 2306 2307 static int out_open_pcm_devices(struct stream_out *out) 2308 { 2309 struct pcm_device *pcm_device; 2310 struct listnode *node; 2311 int ret = 0; 2312 2313 list_for_each(node, &out->pcm_dev_list) { 2314 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 2315 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", 2316 __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->id); 2317 2318 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, 2319 PCM_OUT | PCM_MONOTONIC, &pcm_device->pcm_profile->config); 2320 2321 if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) { 2322 ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm)); 2323 pcm_device->pcm = NULL; 2324 ret = -EIO; 2325 goto error_open; 2326 } 2327 /* 2328 * If the stream rate differs from the PCM rate, we need to 2329 * create a resampler. 2330 */ 2331 if (out->sample_rate != pcm_device->pcm_profile->config.rate) { 2332 ALOGV("%s: create_resampler(), pcm_device_card(%d), pcm_device_id(%d), \ 2333 out_rate(%d), device_rate(%d)",__func__, 2334 pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, 2335 out->sample_rate, pcm_device->pcm_profile->config.rate); 2336 ret = create_resampler(out->sample_rate, 2337 pcm_device->pcm_profile->config.rate, 2338 audio_channel_count_from_out_mask(out->channel_mask), 2339 RESAMPLER_QUALITY_DEFAULT, 2340 NULL, 2341 &pcm_device->resampler); 2342 pcm_device->res_byte_count = 0; 2343 pcm_device->res_buffer = NULL; 2344 } 2345 } 2346 return ret; 2347 2348 error_open: 2349 out_close_pcm_devices(out); 2350 return ret; 2351 } 2352 2353 static int stop_output_stream(struct stream_out *out) 2354 { 2355 int i, ret = 0; 2356 struct audio_usecase *uc_info; 2357 struct audio_device *adev = out->dev; 2358 2359 ALOGV("%s: enter: usecase(%d: %s)", __func__, 2360 out->usecase, use_case_table[out->usecase]); 2361 uc_info = get_usecase_from_id(adev, out->usecase); 2362 if (uc_info == NULL) { 2363 ALOGE("%s: Could not find the usecase (%d) in the list", 2364 __func__, out->usecase); 2365 return -EINVAL; 2366 } 2367 2368 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD && 2369 adev->offload_fx_stop_output != NULL) 2370 adev->offload_fx_stop_output(out->handle); 2371 2372 disable_snd_device(adev, uc_info, uc_info->out_snd_device, true); 2373 2374 uc_release_pcm_devices(uc_info); 2375 list_remove(&uc_info->adev_list_node); 2376 free(uc_info); 2377 2378 ALOGV("%s: exit: status(%d)", __func__, ret); 2379 return ret; 2380 } 2381 2382 int start_output_stream(struct stream_out *out) 2383 { 2384 int ret = 0; 2385 struct audio_usecase *uc_info; 2386 struct audio_device *adev = out->dev; 2387 2388 ALOGV("%s: enter: usecase(%d: %s) devices(%#x) channels(%d)", 2389 __func__, out->usecase, use_case_table[out->usecase], out->devices, out->config.channels); 2390 2391 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 2392 uc_info->id = out->usecase; 2393 uc_info->type = PCM_PLAYBACK; 2394 uc_info->stream = (struct audio_stream *)out; 2395 uc_info->devices = out->devices; 2396 uc_info->in_snd_device = SND_DEVICE_NONE; 2397 uc_info->out_snd_device = SND_DEVICE_NONE; 2398 uc_select_pcm_devices(uc_info); 2399 2400 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node); 2401 2402 select_devices(adev, out->usecase); 2403 2404 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2405 out->compr = NULL; 2406 ret = out_open_pcm_devices(out); 2407 if (ret != 0) 2408 goto error_open; 2409 #ifdef PREPROCESSING_ENABLED 2410 out->echo_reference = NULL; 2411 out->echo_reference_generation = adev->echo_reference_generation; 2412 if (adev->echo_reference != NULL) 2413 out->echo_reference = adev->echo_reference; 2414 #endif 2415 } else { 2416 out->compr = compress_open(COMPRESS_CARD, COMPRESS_DEVICE, 2417 COMPRESS_IN, &out->compr_config); 2418 if (out->compr && !is_compress_ready(out->compr)) { 2419 ALOGE("%s: %s", __func__, compress_get_error(out->compr)); 2420 compress_close(out->compr); 2421 out->compr = NULL; 2422 ret = -EIO; 2423 goto error_open; 2424 } 2425 if (out->offload_callback) 2426 compress_nonblock(out->compr, out->non_blocking); 2427 2428 if (adev->offload_fx_start_output != NULL) 2429 adev->offload_fx_start_output(out->handle); 2430 } 2431 ALOGV("%s: exit", __func__); 2432 return 0; 2433 error_open: 2434 stop_output_stream(out); 2435 error_config: 2436 return ret; 2437 } 2438 2439 static int stop_voice_call(struct audio_device *adev) 2440 { 2441 struct audio_usecase *uc_info; 2442 2443 ALOGV("%s: enter", __func__); 2444 adev->in_call = false; 2445 2446 /* TODO: implement voice call stop */ 2447 2448 uc_info = get_usecase_from_id(adev, USECASE_VOICE_CALL); 2449 if (uc_info == NULL) { 2450 ALOGE("%s: Could not find the usecase (%d) in the list", 2451 __func__, USECASE_VOICE_CALL); 2452 return -EINVAL; 2453 } 2454 2455 disable_snd_device(adev, uc_info, uc_info->out_snd_device, false); 2456 disable_snd_device(adev, uc_info, uc_info->in_snd_device, true); 2457 2458 uc_release_pcm_devices(uc_info); 2459 list_remove(&uc_info->adev_list_node); 2460 free(uc_info); 2461 2462 ALOGV("%s: exit", __func__); 2463 return 0; 2464 } 2465 2466 /* always called with adev lock held */ 2467 static int start_voice_call(struct audio_device *adev) 2468 { 2469 struct audio_usecase *uc_info; 2470 2471 ALOGV("%s: enter", __func__); 2472 2473 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 2474 uc_info->id = USECASE_VOICE_CALL; 2475 uc_info->type = VOICE_CALL; 2476 uc_info->stream = (struct audio_stream *)adev->primary_output; 2477 uc_info->devices = adev->primary_output->devices; 2478 uc_info->in_snd_device = SND_DEVICE_NONE; 2479 uc_info->out_snd_device = SND_DEVICE_NONE; 2480 2481 uc_select_pcm_devices(uc_info); 2482 2483 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node); 2484 2485 select_devices(adev, USECASE_VOICE_CALL); 2486 2487 2488 /* TODO: implement voice call start */ 2489 2490 /* set cached volume */ 2491 set_voice_volume_l(adev, adev->voice_volume); 2492 2493 adev->in_call = true; 2494 ALOGV("%s: exit", __func__); 2495 return 0; 2496 } 2497 2498 static int check_input_parameters(uint32_t sample_rate, 2499 audio_format_t format, 2500 int channel_count) 2501 { 2502 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; 2503 2504 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; 2505 2506 switch (sample_rate) { 2507 case 8000: 2508 case 11025: 2509 case 12000: 2510 case 16000: 2511 case 22050: 2512 case 24000: 2513 case 32000: 2514 case 44100: 2515 case 48000: 2516 break; 2517 default: 2518 return -EINVAL; 2519 } 2520 2521 return 0; 2522 } 2523 2524 static size_t get_input_buffer_size(uint32_t sample_rate, 2525 audio_format_t format, 2526 int channel_count, 2527 audio_devices_t devices) 2528 { 2529 size_t size = 0; 2530 struct pcm_device_profile *pcm_profile; 2531 2532 if (check_input_parameters(sample_rate, format, channel_count) != 0) 2533 return 0; 2534 2535 pcm_profile = get_pcm_device(PCM_CAPTURE, devices); 2536 if (pcm_profile == NULL) 2537 return 0; 2538 2539 /* 2540 * take resampling into account and return the closest majoring 2541 * multiple of 16 frames, as audioflinger expects audio buffers to 2542 * be a multiple of 16 frames 2543 */ 2544 size = (pcm_profile->config.period_size * sample_rate) / pcm_profile->config.rate; 2545 size = ((size + 15) / 16) * 16; 2546 2547 return (size * channel_count * audio_bytes_per_sample(format)); 2548 2549 } 2550 2551 static uint32_t out_get_sample_rate(const struct audio_stream *stream) 2552 { 2553 struct stream_out *out = (struct stream_out *)stream; 2554 2555 return out->sample_rate; 2556 } 2557 2558 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 2559 { 2560 (void)stream; 2561 (void)rate; 2562 return -ENOSYS; 2563 } 2564 2565 static size_t out_get_buffer_size(const struct audio_stream *stream) 2566 { 2567 struct stream_out *out = (struct stream_out *)stream; 2568 2569 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2570 return out->compr_config.fragment_size; 2571 } 2572 2573 return out->config.period_size * 2574 audio_stream_out_frame_size((const struct audio_stream_out *)stream); 2575 } 2576 2577 static uint32_t out_get_channels(const struct audio_stream *stream) 2578 { 2579 struct stream_out *out = (struct stream_out *)stream; 2580 2581 return out->channel_mask; 2582 } 2583 2584 static audio_format_t out_get_format(const struct audio_stream *stream) 2585 { 2586 struct stream_out *out = (struct stream_out *)stream; 2587 2588 return out->format; 2589 } 2590 2591 static int out_set_format(struct audio_stream *stream, audio_format_t format) 2592 { 2593 (void)stream; 2594 (void)format; 2595 return -ENOSYS; 2596 } 2597 2598 static int do_out_standby_l(struct stream_out *out) 2599 { 2600 struct audio_device *adev = out->dev; 2601 int status = 0; 2602 2603 out->standby = true; 2604 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2605 out_close_pcm_devices(out); 2606 #ifdef PREPROCESSING_ENABLED 2607 /* stop writing to echo reference */ 2608 if (out->echo_reference != NULL) { 2609 out->echo_reference->write(out->echo_reference, NULL); 2610 if (out->echo_reference_generation != adev->echo_reference_generation) { 2611 ALOGV("%s: release_echo_reference %p", __func__, out->echo_reference); 2612 release_echo_reference(out->echo_reference); 2613 out->echo_reference_generation = adev->echo_reference_generation; 2614 } 2615 out->echo_reference = NULL; 2616 } 2617 #endif 2618 } else { 2619 stop_compressed_output_l(out); 2620 out->gapless_mdata.encoder_delay = 0; 2621 out->gapless_mdata.encoder_padding = 0; 2622 if (out->compr != NULL) { 2623 compress_close(out->compr); 2624 out->compr = NULL; 2625 } 2626 } 2627 status = stop_output_stream(out); 2628 2629 return status; 2630 } 2631 2632 static int out_standby(struct audio_stream *stream) 2633 { 2634 struct stream_out *out = (struct stream_out *)stream; 2635 struct audio_device *adev = out->dev; 2636 2637 ALOGV("%s: enter: usecase(%d: %s)", __func__, 2638 out->usecase, use_case_table[out->usecase]); 2639 pthread_mutex_lock(&out->lock); 2640 if (!out->standby) { 2641 pthread_mutex_lock(&adev->lock); 2642 do_out_standby_l(out); 2643 pthread_mutex_unlock(&adev->lock); 2644 } 2645 pthread_mutex_unlock(&out->lock); 2646 ALOGV("%s: exit", __func__); 2647 return 0; 2648 } 2649 2650 static int out_dump(const struct audio_stream *stream, int fd) 2651 { 2652 (void)stream; 2653 (void)fd; 2654 2655 return 0; 2656 } 2657 2658 static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) 2659 { 2660 int ret = 0; 2661 char value[32]; 2662 struct compr_gapless_mdata tmp_mdata; 2663 2664 if (!out || !parms) { 2665 return -EINVAL; 2666 } 2667 2668 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); 2669 if (ret >= 0) { 2670 tmp_mdata.encoder_delay = atoi(value); /* what is a good limit check? */ 2671 } else { 2672 return -EINVAL; 2673 } 2674 2675 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); 2676 if (ret >= 0) { 2677 tmp_mdata.encoder_padding = atoi(value); 2678 } else { 2679 return -EINVAL; 2680 } 2681 2682 out->gapless_mdata = tmp_mdata; 2683 out->send_new_metadata = 1; 2684 ALOGV("%s new encoder delay %u and padding %u", __func__, 2685 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); 2686 2687 return 0; 2688 } 2689 2690 2691 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 2692 { 2693 struct stream_out *out = (struct stream_out *)stream; 2694 struct audio_device *adev = out->dev; 2695 struct audio_usecase *usecase; 2696 struct listnode *node; 2697 struct str_parms *parms; 2698 char value[32]; 2699 int ret, val = 0; 2700 struct audio_usecase *uc_info; 2701 bool do_standby = false; 2702 struct pcm_device *pcm_device; 2703 struct pcm_device_profile *pcm_profile; 2704 #ifdef PREPROCESSING_ENABLED 2705 struct stream_in *in = NULL; /* if non-NULL, then force input to standby */ 2706 #endif 2707 2708 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s out->devices(%d) adev->mode(%d)", 2709 __func__, out->usecase, use_case_table[out->usecase], kvpairs, out->devices, adev->mode); 2710 parms = str_parms_create_str(kvpairs); 2711 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 2712 if (ret >= 0) { 2713 val = atoi(value); 2714 pthread_mutex_lock(&adev->lock_inputs); 2715 pthread_mutex_lock(&out->lock); 2716 pthread_mutex_lock(&adev->lock); 2717 #ifdef PREPROCESSING_ENABLED 2718 if (((int)out->devices != val) && (val != 0) && (!out->standby) && 2719 (out->usecase == USECASE_AUDIO_PLAYBACK)) { 2720 /* reset active input: 2721 * - to attach the echo reference 2722 * - because a change in output device may change mic settings */ 2723 if (adev->active_input && (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 2724 adev->active_input->source == AUDIO_SOURCE_MIC)) { 2725 in = adev->active_input; 2726 } 2727 } 2728 #endif 2729 if (val != 0) { 2730 out->devices = val; 2731 2732 if (!out->standby) { 2733 uc_info = get_usecase_from_id(adev, out->usecase); 2734 if (uc_info == NULL) { 2735 ALOGE("%s: Could not find the usecase (%d) in the list", 2736 __func__, out->usecase); 2737 } else { 2738 list_for_each(node, &out->pcm_dev_list) { 2739 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 2740 if ((pcm_device->pcm_profile->devices & val) == 0) 2741 do_standby = true; 2742 val &= ~pcm_device->pcm_profile->devices; 2743 } 2744 if (val != 0) 2745 do_standby = true; 2746 } 2747 if (do_standby) 2748 do_out_standby_l(out); 2749 else 2750 select_devices(adev, out->usecase); 2751 } 2752 2753 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call && 2754 (out == adev->primary_output)) { 2755 start_voice_call(adev); 2756 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call && 2757 (out == adev->primary_output)) { 2758 select_devices(adev, USECASE_VOICE_CALL); 2759 } 2760 } 2761 2762 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call && 2763 (out == adev->primary_output)) { 2764 stop_voice_call(adev); 2765 } 2766 pthread_mutex_unlock(&adev->lock); 2767 pthread_mutex_unlock(&out->lock); 2768 #ifdef PREPROCESSING_ENABLED 2769 if (in) { 2770 /* The lock on adev->lock_inputs prevents input stream from being closed */ 2771 pthread_mutex_lock(&in->lock); 2772 pthread_mutex_lock(&adev->lock); 2773 LOG_ALWAYS_FATAL_IF(in != adev->active_input); 2774 do_in_standby_l(in); 2775 pthread_mutex_unlock(&adev->lock); 2776 pthread_mutex_unlock(&in->lock); 2777 } 2778 #endif 2779 pthread_mutex_unlock(&adev->lock_inputs); 2780 } 2781 2782 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2783 parse_compress_metadata(out, parms); 2784 } 2785 2786 str_parms_destroy(parms); 2787 ALOGV("%s: exit: code(%d)", __func__, ret); 2788 return ret; 2789 } 2790 2791 static char* out_get_parameters(const struct audio_stream *stream, const char *keys) 2792 { 2793 struct stream_out *out = (struct stream_out *)stream; 2794 struct str_parms *query = str_parms_create_str(keys); 2795 char *str; 2796 char value[256]; 2797 struct str_parms *reply = str_parms_create(); 2798 size_t i, j; 2799 int ret; 2800 bool first = true; 2801 ALOGV("%s: enter: keys - %s", __func__, keys); 2802 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); 2803 if (ret >= 0) { 2804 value[0] = '\0'; 2805 i = 0; 2806 while (out->supported_channel_masks[i] != 0) { 2807 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { 2808 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { 2809 if (!first) { 2810 strcat(value, "|"); 2811 } 2812 strcat(value, out_channels_name_to_enum_table[j].name); 2813 first = false; 2814 break; 2815 } 2816 } 2817 i++; 2818 } 2819 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 2820 str = str_parms_to_str(reply); 2821 } else { 2822 str = strdup(keys); 2823 } 2824 str_parms_destroy(query); 2825 str_parms_destroy(reply); 2826 ALOGV("%s: exit: returns - %s", __func__, str); 2827 return str; 2828 } 2829 2830 static uint32_t out_get_latency(const struct audio_stream_out *stream) 2831 { 2832 struct stream_out *out = (struct stream_out *)stream; 2833 2834 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) 2835 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; 2836 2837 return (out->config.period_count * out->config.period_size * 1000) / 2838 (out->config.rate); 2839 } 2840 2841 static int out_set_volume(struct audio_stream_out *stream, float left, 2842 float right) 2843 { 2844 struct stream_out *out = (struct stream_out *)stream; 2845 int volume[2]; 2846 2847 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { 2848 /* only take left channel into account: the API is for stereo anyway */ 2849 out->muted = (left == 0.0f); 2850 return 0; 2851 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2852 (void)right; 2853 /* TODO */ 2854 return 0; 2855 } 2856 2857 return -ENOSYS; 2858 } 2859 2860 static void *tfa9895_config_thread(void *context) 2861 { 2862 ALOGD("%s: enter", __func__); 2863 pthread_detach(pthread_self()); 2864 struct audio_device *adev = (struct audio_device *)context; 2865 pthread_mutex_lock(&adev->tfa9895_lock); 2866 adev->tfa9895_init = 2867 adev->htc_acoustic_set_amp_mode(adev->mode, AUDIO_DEVICE_OUT_SPEAKER, 0, 0, false); 2868 if (!adev->tfa9895_init) { 2869 ALOGE("set_amp_mode failed, need to re-config again"); 2870 adev->tfa9895_mode_change |= 0x1; 2871 } 2872 ALOGI("@@##tfa9895_config_thread Done!! tfa9895_mode_change=%d", adev->tfa9895_mode_change); 2873 pthread_mutex_unlock(&adev->tfa9895_lock); 2874 dummybuf_thread_close(adev); 2875 return NULL; 2876 } 2877 2878 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, 2879 size_t bytes) 2880 { 2881 struct stream_out *out = (struct stream_out *)stream; 2882 struct audio_device *adev = out->dev; 2883 ssize_t ret = 0; 2884 struct pcm_device *pcm_device; 2885 struct listnode *node; 2886 size_t frame_size = audio_stream_out_frame_size(stream); 2887 size_t frames_wr = 0, frames_rq = 0; 2888 unsigned char *data = NULL; 2889 #ifdef PREPROCESSING_ENABLED 2890 size_t in_frames = bytes / frame_size; 2891 size_t out_frames = in_frames; 2892 struct stream_in *in = NULL; 2893 #endif 2894 2895 pthread_mutex_lock(&out->lock); 2896 if (out->standby) { 2897 #ifdef PREPROCESSING_ENABLED 2898 pthread_mutex_unlock(&out->lock); 2899 /* Prevent input stream from being closed */ 2900 pthread_mutex_lock(&adev->lock_inputs); 2901 pthread_mutex_lock(&out->lock); 2902 if (!out->standby) { 2903 pthread_mutex_unlock(&adev->lock_inputs); 2904 goto false_alarm; 2905 } 2906 #endif 2907 pthread_mutex_lock(&adev->lock); 2908 ret = start_output_stream(out); 2909 /* ToDo: If use case is compress offload should return 0 */ 2910 if (ret != 0) { 2911 pthread_mutex_unlock(&adev->lock); 2912 #ifdef PREPROCESSING_ENABLED 2913 pthread_mutex_unlock(&adev->lock_inputs); 2914 #endif 2915 goto exit; 2916 } 2917 out->standby = false; 2918 2919 #ifdef PREPROCESSING_ENABLED 2920 /* A change in output device may change the microphone selection */ 2921 if (adev->active_input && 2922 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 2923 adev->active_input->source == AUDIO_SOURCE_MIC)) { 2924 in = adev->active_input; 2925 ALOGV("%s: enter:) force_input_standby true", __func__); 2926 } 2927 #endif 2928 pthread_mutex_unlock(&adev->lock); 2929 #ifdef PREPROCESSING_ENABLED 2930 if (!in) { 2931 /* Leave mutex locked iff in != NULL */ 2932 pthread_mutex_unlock(&adev->lock_inputs); 2933 } 2934 #endif 2935 } 2936 false_alarm: 2937 2938 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2939 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes); 2940 if (out->send_new_metadata) { 2941 ALOGVV("send new gapless metadata"); 2942 compress_set_gapless_metadata(out->compr, &out->gapless_mdata); 2943 out->send_new_metadata = 0; 2944 } 2945 2946 ret = compress_write(out->compr, buffer, bytes); 2947 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret); 2948 if (ret >= 0 && ret < (ssize_t)bytes) { 2949 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); 2950 } 2951 if (!out->playback_started) { 2952 compress_start(out->compr); 2953 out->playback_started = 1; 2954 out->offload_state = OFFLOAD_STATE_PLAYING; 2955 } 2956 pthread_mutex_unlock(&out->lock); 2957 #ifdef PREPROCESSING_ENABLED 2958 if (in) { 2959 /* This mutex was left locked iff in != NULL */ 2960 pthread_mutex_unlock(&adev->lock_inputs); 2961 } 2962 #endif 2963 return ret; 2964 } else { 2965 #ifdef PREPROCESSING_ENABLED 2966 if (android_atomic_acquire_load(&adev->echo_reference_generation) 2967 != out->echo_reference_generation) { 2968 pthread_mutex_lock(&adev->lock); 2969 if (out->echo_reference != NULL) { 2970 ALOGV("%s: release_echo_reference %p", __func__, out->echo_reference); 2971 release_echo_reference(out->echo_reference); 2972 } 2973 // note that adev->echo_reference_generation here can be different from the one 2974 // tested above but it doesn't matter as we now have the adev mutex and it is consistent 2975 // with what has been set by get_echo_reference() or put_echo_reference() 2976 out->echo_reference_generation = adev->echo_reference_generation; 2977 out->echo_reference = adev->echo_reference; 2978 ALOGV("%s: update echo reference generation %d", __func__, 2979 out->echo_reference_generation); 2980 pthread_mutex_unlock(&adev->lock); 2981 } 2982 #endif 2983 2984 if (out->muted) 2985 memset((void *)buffer, 0, bytes); 2986 list_for_each(node, &out->pcm_dev_list) { 2987 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 2988 if (pcm_device->resampler) { 2989 if (bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size 2990 > pcm_device->res_byte_count) { 2991 pcm_device->res_byte_count = 2992 bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size; 2993 pcm_device->res_buffer = 2994 realloc(pcm_device->res_buffer, pcm_device->res_byte_count); 2995 ALOGV("%s: resampler res_byte_count = %zu", __func__, 2996 pcm_device->res_byte_count); 2997 } 2998 frames_rq = bytes / frame_size; 2999 frames_wr = pcm_device->res_byte_count / frame_size; 3000 ALOGVV("%s: resampler request frames = %d frame_size = %d", 3001 __func__, frames_rq, frame_size); 3002 pcm_device->resampler->resample_from_input(pcm_device->resampler, 3003 (int16_t *)buffer, &frames_rq, (int16_t *)pcm_device->res_buffer, &frames_wr); 3004 ALOGVV("%s: resampler output frames_= %d", __func__, frames_wr); 3005 } 3006 if (pcm_device->pcm) { 3007 #ifdef PREPROCESSING_ENABLED 3008 if (out->echo_reference != NULL && pcm_device->pcm_profile->devices != SND_DEVICE_OUT_SPEAKER) { 3009 struct echo_reference_buffer b; 3010 b.raw = (void *)buffer; 3011 b.frame_count = in_frames; 3012 3013 get_playback_delay(out, out_frames, &b); 3014 out->echo_reference->write(out->echo_reference, &b); 3015 } 3016 #endif 3017 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) { 3018 pthread_mutex_lock(&adev->tfa9895_lock); 3019 if (adev->tfa9895_mode_change == 0x1) { 3020 data = (unsigned char *) 3021 calloc(pcm_frames_to_bytes(pcm_device->pcm, out->config.period_size), 3022 sizeof(unsigned char)); 3023 if (data) { 3024 int i; 3025 for (i = out->config.period_count; i > 0; i--) 3026 pcm_write(pcm_device->pcm, (void *)data, 3027 pcm_frames_to_bytes(pcm_device->pcm, out->config.period_size)); 3028 /* TODO: Hold on 100 ms and wait i2s signal ready 3029 before giving dsp related i2c commands */ 3030 usleep(100000); 3031 adev->tfa9895_mode_change &= ~0x1; 3032 ALOGD("@@##checking - 2: tfa9895_config_thread: " 3033 "adev->tfa9895_mode_change=%d", adev->tfa9895_mode_change); 3034 adev->tfa9895_init = 3035 adev->htc_acoustic_set_amp_mode(adev->mode, AUDIO_DEVICE_OUT_SPEAKER, 3036 0, 0, false); 3037 free(data); 3038 } 3039 } 3040 pthread_mutex_unlock(&adev->tfa9895_lock); 3041 } 3042 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); 3043 if (pcm_device->resampler && pcm_device->res_buffer) 3044 pcm_device->status = 3045 pcm_write(pcm_device->pcm, (void *)pcm_device->res_buffer, 3046 frames_wr * frame_size); 3047 else 3048 pcm_device->status = pcm_write(pcm_device->pcm, (void *)buffer, bytes); 3049 if (pcm_device->status != 0) 3050 ret = pcm_device->status; 3051 } 3052 } 3053 if (ret == 0) 3054 out->written += bytes / (out->config.channels * sizeof(short)); 3055 } 3056 3057 exit: 3058 pthread_mutex_unlock(&out->lock); 3059 3060 if (ret != 0) { 3061 list_for_each(node, &out->pcm_dev_list) { 3062 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 3063 if (pcm_device->pcm && pcm_device->status != 0) 3064 ALOGE("%s: error %zd - %s", __func__, ret, pcm_get_error(pcm_device->pcm)); 3065 } 3066 out_standby(&out->stream.common); 3067 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / 3068 out_get_sample_rate(&out->stream.common)); 3069 } 3070 3071 #ifdef PREPROCESSING_ENABLED 3072 if (in) { 3073 /* The lock on adev->lock_inputs prevents input stream from being closed */ 3074 pthread_mutex_lock(&in->lock); 3075 pthread_mutex_lock(&adev->lock); 3076 LOG_ALWAYS_FATAL_IF(in != adev->active_input); 3077 do_in_standby_l(in); 3078 pthread_mutex_unlock(&adev->lock); 3079 pthread_mutex_unlock(&in->lock); 3080 /* This mutex was left locked iff in != NULL */ 3081 pthread_mutex_unlock(&adev->lock_inputs); 3082 } 3083 #endif 3084 3085 return bytes; 3086 } 3087 3088 static int out_get_render_position(const struct audio_stream_out *stream, 3089 uint32_t *dsp_frames) 3090 { 3091 struct stream_out *out = (struct stream_out *)stream; 3092 *dsp_frames = 0; 3093 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { 3094 pthread_mutex_lock(&out->lock); 3095 if (out->compr != NULL) { 3096 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames, 3097 &out->sample_rate); 3098 ALOGVV("%s rendered frames %d sample_rate %d", 3099 __func__, *dsp_frames, out->sample_rate); 3100 } 3101 pthread_mutex_unlock(&out->lock); 3102 return 0; 3103 } else 3104 return -EINVAL; 3105 } 3106 3107 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 3108 { 3109 (void)stream; 3110 (void)effect; 3111 return 0; 3112 } 3113 3114 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 3115 { 3116 (void)stream; 3117 (void)effect; 3118 return 0; 3119 } 3120 3121 static int out_get_next_write_timestamp(const struct audio_stream_out *stream, 3122 int64_t *timestamp) 3123 { 3124 (void)stream; 3125 (void)timestamp; 3126 return -EINVAL; 3127 } 3128 3129 static int out_get_presentation_position(const struct audio_stream_out *stream, 3130 uint64_t *frames, struct timespec *timestamp) 3131 { 3132 struct stream_out *out = (struct stream_out *)stream; 3133 int ret = -1; 3134 unsigned long dsp_frames; 3135 3136 pthread_mutex_lock(&out->lock); 3137 3138 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3139 if (out->compr != NULL) { 3140 compress_get_tstamp(out->compr, &dsp_frames, 3141 &out->sample_rate); 3142 ALOGVV("%s rendered frames %ld sample_rate %d", 3143 __func__, dsp_frames, out->sample_rate); 3144 *frames = dsp_frames; 3145 ret = 0; 3146 /* this is the best we can do */ 3147 clock_gettime(CLOCK_MONOTONIC, timestamp); 3148 } 3149 } else { 3150 /* FIXME: which device to read from? */ 3151 if (!list_empty(&out->pcm_dev_list)) { 3152 unsigned int avail; 3153 struct pcm_device *pcm_device = node_to_item(list_head(&out->pcm_dev_list), 3154 struct pcm_device, stream_list_node); 3155 3156 if (pcm_get_htimestamp(pcm_device->pcm, &avail, timestamp) == 0) { 3157 size_t kernel_buffer_size = out->config.period_size * out->config.period_count; 3158 int64_t signed_frames = out->written - kernel_buffer_size + avail; 3159 /* This adjustment accounts for buffering after app processor. 3160 It is based on estimated DSP latency per use case, rather than exact. */ 3161 signed_frames -= 3162 (render_latency(out->usecase) * out->sample_rate / 1000000LL); 3163 3164 /* It would be unusual for this value to be negative, but check just in case ... */ 3165 if (signed_frames >= 0) { 3166 *frames = signed_frames; 3167 ret = 0; 3168 } 3169 } 3170 } 3171 } 3172 3173 pthread_mutex_unlock(&out->lock); 3174 3175 return ret; 3176 } 3177 3178 static int out_set_callback(struct audio_stream_out *stream, 3179 stream_callback_t callback, void *cookie) 3180 { 3181 struct stream_out *out = (struct stream_out *)stream; 3182 3183 ALOGV("%s", __func__); 3184 pthread_mutex_lock(&out->lock); 3185 out->offload_callback = callback; 3186 out->offload_cookie = cookie; 3187 pthread_mutex_unlock(&out->lock); 3188 return 0; 3189 } 3190 3191 static int out_pause(struct audio_stream_out* stream) 3192 { 3193 struct stream_out *out = (struct stream_out *)stream; 3194 int status = -ENOSYS; 3195 ALOGV("%s", __func__); 3196 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3197 pthread_mutex_lock(&out->lock); 3198 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { 3199 status = compress_pause(out->compr); 3200 out->offload_state = OFFLOAD_STATE_PAUSED; 3201 } 3202 pthread_mutex_unlock(&out->lock); 3203 } 3204 return status; 3205 } 3206 3207 static int out_resume(struct audio_stream_out* stream) 3208 { 3209 struct stream_out *out = (struct stream_out *)stream; 3210 int status = -ENOSYS; 3211 ALOGV("%s", __func__); 3212 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3213 status = 0; 3214 pthread_mutex_lock(&out->lock); 3215 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { 3216 status = compress_resume(out->compr); 3217 out->offload_state = OFFLOAD_STATE_PLAYING; 3218 } 3219 pthread_mutex_unlock(&out->lock); 3220 } 3221 return status; 3222 } 3223 3224 static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) 3225 { 3226 struct stream_out *out = (struct stream_out *)stream; 3227 int status = -ENOSYS; 3228 ALOGV("%s", __func__); 3229 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3230 pthread_mutex_lock(&out->lock); 3231 if (type == AUDIO_DRAIN_EARLY_NOTIFY) 3232 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); 3233 else 3234 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); 3235 pthread_mutex_unlock(&out->lock); 3236 } 3237 return status; 3238 } 3239 3240 static int out_flush(struct audio_stream_out* stream) 3241 { 3242 struct stream_out *out = (struct stream_out *)stream; 3243 ALOGV("%s", __func__); 3244 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3245 pthread_mutex_lock(&out->lock); 3246 stop_compressed_output_l(out); 3247 pthread_mutex_unlock(&out->lock); 3248 return 0; 3249 } 3250 return -ENOSYS; 3251 } 3252 3253 /** audio_stream_in implementation **/ 3254 static uint32_t in_get_sample_rate(const struct audio_stream *stream) 3255 { 3256 struct stream_in *in = (struct stream_in *)stream; 3257 3258 return in->requested_rate; 3259 } 3260 3261 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 3262 { 3263 (void)stream; 3264 (void)rate; 3265 return -ENOSYS; 3266 } 3267 3268 static uint32_t in_get_channels(const struct audio_stream *stream) 3269 { 3270 struct stream_in *in = (struct stream_in *)stream; 3271 3272 return in->main_channels; 3273 } 3274 3275 static audio_format_t in_get_format(const struct audio_stream *stream) 3276 { 3277 (void)stream; 3278 return AUDIO_FORMAT_PCM_16_BIT; 3279 } 3280 3281 static int in_set_format(struct audio_stream *stream, audio_format_t format) 3282 { 3283 (void)stream; 3284 (void)format; 3285 3286 return -ENOSYS; 3287 } 3288 3289 static size_t in_get_buffer_size(const struct audio_stream *stream) 3290 { 3291 struct stream_in *in = (struct stream_in *)stream; 3292 3293 return get_input_buffer_size(in->requested_rate, 3294 in_get_format(stream), 3295 audio_channel_count_from_in_mask(in->main_channels), 3296 in->devices); 3297 } 3298 3299 static int in_close_pcm_devices(struct stream_in *in) 3300 { 3301 struct pcm_device *pcm_device; 3302 struct listnode *node; 3303 struct audio_device *adev = in->dev; 3304 3305 list_for_each(node, &in->pcm_dev_list) { 3306 pcm_device = node_to_item(node, struct pcm_device, stream_list_node); 3307 if (pcm_device) { 3308 if (pcm_device->pcm) 3309 pcm_close(pcm_device->pcm); 3310 pcm_device->pcm = NULL; 3311 if (pcm_device->sound_trigger_handle > 0) 3312 adev->sound_trigger_close_for_streaming(pcm_device->sound_trigger_handle); 3313 pcm_device->sound_trigger_handle = 0; 3314 } 3315 } 3316 return 0; 3317 } 3318 3319 3320 /* must be called with stream and hw device mutex locked */ 3321 static int do_in_standby_l(struct stream_in *in) 3322 { 3323 int status = 0; 3324 3325 #ifdef PREPROCESSING_ENABLED 3326 struct audio_device *adev = in->dev; 3327 #endif 3328 if (!in->standby) { 3329 3330 in_close_pcm_devices(in); 3331 3332 #ifdef PREPROCESSING_ENABLED 3333 if (in->echo_reference != NULL) { 3334 /* stop reading from echo reference */ 3335 in->echo_reference->read(in->echo_reference, NULL); 3336 put_echo_reference(adev, in->echo_reference); 3337 in->echo_reference = NULL; 3338 } 3339 #ifdef HW_AEC_LOOPBACK 3340 if (in->hw_echo_reference) 3341 { 3342 if (in->hw_ref_buf) { 3343 free(in->hw_ref_buf); 3344 in->hw_ref_buf = NULL; 3345 } 3346 } 3347 #endif // HW_AEC_LOOPBACK 3348 #endif // PREPROCESSING_ENABLED 3349 3350 status = stop_input_stream(in); 3351 3352 if (in->read_buf) { 3353 free(in->read_buf); 3354 in->read_buf = NULL; 3355 } 3356 3357 in->standby = 1; 3358 } 3359 return 0; 3360 } 3361 3362 // called with adev->lock_inputs locked 3363 static int in_standby_l(struct stream_in *in) 3364 { 3365 struct audio_device *adev = in->dev; 3366 int status = 0; 3367 pthread_mutex_lock(&in->lock); 3368 if (!in->standby) { 3369 pthread_mutex_lock(&adev->lock); 3370 status = do_in_standby_l(in); 3371 pthread_mutex_unlock(&adev->lock); 3372 } 3373 pthread_mutex_unlock(&in->lock); 3374 return status; 3375 } 3376 3377 static int in_standby(struct audio_stream *stream) 3378 { 3379 struct stream_in *in = (struct stream_in *)stream; 3380 struct audio_device *adev = in->dev; 3381 int status; 3382 ALOGV("%s: enter", __func__); 3383 pthread_mutex_lock(&adev->lock_inputs); 3384 status = in_standby_l(in); 3385 pthread_mutex_unlock(&adev->lock_inputs); 3386 ALOGV("%s: exit: status(%d)", __func__, status); 3387 return status; 3388 } 3389 3390 static int in_dump(const struct audio_stream *stream, int fd) 3391 { 3392 (void)stream; 3393 (void)fd; 3394 3395 return 0; 3396 } 3397 3398 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 3399 { 3400 struct stream_in *in = (struct stream_in *)stream; 3401 struct audio_device *adev = in->dev; 3402 struct str_parms *parms; 3403 char *str; 3404 char value[32]; 3405 int ret, val = 0; 3406 struct audio_usecase *uc_info; 3407 bool do_standby = false; 3408 struct listnode *node; 3409 struct pcm_device *pcm_device; 3410 struct pcm_device_profile *pcm_profile; 3411 3412 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); 3413 parms = str_parms_create_str(kvpairs); 3414 3415 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 3416 3417 pthread_mutex_lock(&adev->lock_inputs); 3418 pthread_mutex_lock(&in->lock); 3419 pthread_mutex_lock(&adev->lock); 3420 if (ret >= 0) { 3421 val = atoi(value); 3422 /* no audio source uses val == 0 */ 3423 if (((int)in->source != val) && (val != 0)) { 3424 in->source = val; 3425 } 3426 } 3427 3428 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 3429 if (ret >= 0) { 3430 val = atoi(value); 3431 if (((int)in->devices != val) && (val != 0)) { 3432 in->devices = val; 3433 /* If recording is in progress, change the tx device to new device */ 3434 if (!in->standby) { 3435 uc_info = get_usecase_from_id(adev, in->usecase); 3436 if (uc_info == NULL) { 3437 ALOGE("%s: Could not find the usecase (%d) in the list", 3438 __func__, in->usecase); 3439 } else { 3440 if (list_empty(&in->pcm_dev_list)) 3441 ALOGE("%s: pcm device list empty", __func__); 3442 else { 3443 pcm_device = node_to_item(list_head(&in->pcm_dev_list), 3444 struct pcm_device, stream_list_node); 3445 if ((pcm_device->pcm_profile->devices & val & ~AUDIO_DEVICE_BIT_IN) == 0) { 3446 do_standby = true; 3447 } 3448 } 3449 } 3450 if (do_standby) { 3451 ret = do_in_standby_l(in); 3452 } else 3453 ret = select_devices(adev, in->usecase); 3454 } 3455 } 3456 } 3457 pthread_mutex_unlock(&adev->lock); 3458 pthread_mutex_unlock(&in->lock); 3459 pthread_mutex_unlock(&adev->lock_inputs); 3460 str_parms_destroy(parms); 3461 ALOGV("%s: exit: status(%d)", __func__, ret); 3462 return ret; 3463 } 3464 3465 static char* in_get_parameters(const struct audio_stream *stream, 3466 const char *keys) 3467 { 3468 (void)stream; 3469 (void)keys; 3470 3471 return strdup(""); 3472 } 3473 3474 static int in_set_gain(struct audio_stream_in *stream, float gain) 3475 { 3476 (void)stream; 3477 (void)gain; 3478 3479 return 0; 3480 } 3481 3482 static ssize_t read_bytes_from_dsp(struct stream_in *in, void* buffer, 3483 size_t bytes) 3484 { 3485 struct pcm_device *pcm_device; 3486 struct audio_device *adev = in->dev; 3487 3488 pcm_device = node_to_item(list_head(&in->pcm_dev_list), 3489 struct pcm_device, stream_list_node); 3490 3491 if (pcm_device->sound_trigger_handle > 0) 3492 return adev->sound_trigger_read_samples(pcm_device->sound_trigger_handle, buffer, bytes); 3493 else 3494 return 0; 3495 } 3496 3497 static ssize_t in_read(struct audio_stream_in *stream, void *buffer, 3498 size_t bytes) 3499 { 3500 struct stream_in *in = (struct stream_in *)stream; 3501 struct audio_device *adev = in->dev; 3502 ssize_t frames = -1; 3503 int ret = -1; 3504 int read_and_process_successful = false; 3505 3506 size_t frames_rq = bytes / audio_stream_in_frame_size(stream); 3507 3508 /* no need to acquire adev->lock_inputs because API contract prevents a close */ 3509 pthread_mutex_lock(&in->lock); 3510 if (in->standby) { 3511 pthread_mutex_unlock(&in->lock); 3512 pthread_mutex_lock(&adev->lock_inputs); 3513 pthread_mutex_lock(&in->lock); 3514 if (!in->standby) { 3515 pthread_mutex_unlock(&adev->lock_inputs); 3516 goto false_alarm; 3517 } 3518 pthread_mutex_lock(&adev->lock); 3519 ret = start_input_stream(in); 3520 pthread_mutex_unlock(&adev->lock); 3521 pthread_mutex_unlock(&adev->lock_inputs); 3522 if (ret != 0) { 3523 goto exit; 3524 } 3525 in->standby = 0; 3526 } 3527 false_alarm: 3528 3529 if (!list_empty(&in->pcm_dev_list)) { 3530 if (in->usecase == USECASE_AUDIO_CAPTURE_HOTWORD) { 3531 bytes = read_bytes_from_dsp(in, buffer, bytes); 3532 if (bytes > 0) 3533 read_and_process_successful = true; 3534 } else { 3535 /* 3536 * Read PCM and: 3537 * - resample if needed 3538 * - process if pre-processors are attached 3539 * - discard unwanted channels 3540 */ 3541 frames = read_and_process_frames(in, buffer, frames_rq); 3542 if (frames >= 0) 3543 read_and_process_successful = true; 3544 } 3545 } 3546 3547 /* 3548 * Instead of writing zeroes here, we could trust the hardware 3549 * to always provide zeroes when muted. 3550 */ 3551 if (read_and_process_successful == true && adev->mic_mute) 3552 memset(buffer, 0, bytes); 3553 3554 exit: 3555 pthread_mutex_unlock(&in->lock); 3556 3557 if (read_and_process_successful == false) { 3558 in_standby(&in->stream.common); 3559 ALOGV("%s: read failed - sleeping for buffer duration", __func__); 3560 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) / 3561 in->requested_rate); 3562 } 3563 return bytes; 3564 } 3565 3566 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 3567 { 3568 (void)stream; 3569 3570 return 0; 3571 } 3572 3573 static int add_remove_audio_effect(const struct audio_stream *stream, 3574 effect_handle_t effect, 3575 bool enable) 3576 { 3577 struct stream_in *in = (struct stream_in *)stream; 3578 struct audio_device *adev = in->dev; 3579 int status = 0; 3580 effect_descriptor_t desc; 3581 #ifdef PREPROCESSING_ENABLED 3582 int i; 3583 #endif 3584 status = (*effect)->get_descriptor(effect, &desc); 3585 if (status != 0) 3586 return status; 3587 3588 ALOGI("add_remove_audio_effect(), effect type: %08x, enable: %d ", desc.type.timeLow, enable); 3589 3590 pthread_mutex_lock(&adev->lock_inputs); 3591 pthread_mutex_lock(&in->lock); 3592 pthread_mutex_lock(&in->dev->lock); 3593 #ifndef PREPROCESSING_ENABLED 3594 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && 3595 in->enable_aec != enable && 3596 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { 3597 in->enable_aec = enable; 3598 if (!in->standby) 3599 select_devices(in->dev, in->usecase); 3600 } 3601 #else 3602 if ( (in->num_preprocessors > MAX_PREPROCESSORS) && (enable == true) ) { 3603 status = -ENOSYS; 3604 goto exit; 3605 } 3606 if ( enable == true ) { 3607 in->preprocessors[in->num_preprocessors].effect_itfe = effect; 3608 /* add the supported channel of the effect in the channel_configs */ 3609 in_read_audio_effect_channel_configs(in, &in->preprocessors[in->num_preprocessors]); 3610 in->num_preprocessors ++; 3611 /* check compatibility between main channel supported and possible auxiliary channels */ 3612 in_update_aux_channels(in, effect);//wesley crash 3613 in->aux_channels_changed = true; 3614 } else { 3615 /* if ( enable == false ) */ 3616 if (in->num_preprocessors <= 0) { 3617 status = -ENOSYS; 3618 goto exit; 3619 } 3620 status = -EINVAL; 3621 for (i=0; i < in->num_preprocessors; i++) { 3622 if (status == 0) { /* status == 0 means an effect was removed from a previous slot */ 3623 in->preprocessors[i - 1].effect_itfe = in->preprocessors[i].effect_itfe; 3624 in->preprocessors[i - 1].channel_configs = in->preprocessors[i].channel_configs; 3625 in->preprocessors[i - 1].num_channel_configs = 3626 in->preprocessors[i].num_channel_configs; 3627 ALOGV("add_remove_audio_effect moving fx from %d to %d", i, i-1); 3628 continue; 3629 } 3630 if ( in->preprocessors[i].effect_itfe == effect ) { 3631 ALOGV("add_remove_audio_effect found fx at index %d", i); 3632 free(in->preprocessors[i].channel_configs); 3633 status = 0; 3634 } 3635 } 3636 if (status != 0) 3637 goto exit; 3638 in->num_preprocessors--; 3639 /* if we remove one effect, at least the last proproc should be reset */ 3640 in->preprocessors[in->num_preprocessors].num_channel_configs = 0; 3641 in->preprocessors[in->num_preprocessors].effect_itfe = NULL; 3642 in->preprocessors[in->num_preprocessors].channel_configs = NULL; 3643 in->aux_channels_changed = false; 3644 ALOGV("%s: enable(%d), in->aux_channels_changed(%d)", __func__, enable, in->aux_channels_changed); 3645 } 3646 ALOGI("%s: num_preprocessors = %d", __func__, in->num_preprocessors); 3647 3648 if ( memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) { 3649 in->enable_aec = enable; 3650 ALOGV("add_remove_audio_effect(), FX_IID_AEC, enable: %d", enable); 3651 if (!in->standby) { 3652 select_devices(in->dev, in->usecase); 3653 do_in_standby_l(in); 3654 } 3655 if (in->enable_aec == true) { 3656 in_configure_reverse(in); 3657 } 3658 } 3659 exit: 3660 #endif 3661 ALOGW_IF(status != 0, "add_remove_audio_effect() error %d", status); 3662 pthread_mutex_unlock(&in->dev->lock); 3663 pthread_mutex_unlock(&in->lock); 3664 pthread_mutex_unlock(&adev->lock_inputs); 3665 return status; 3666 } 3667 3668 static int in_add_audio_effect(const struct audio_stream *stream, 3669 effect_handle_t effect) 3670 { 3671 ALOGV("%s: effect %p", __func__, effect); 3672 return add_remove_audio_effect(stream, effect, true); 3673 } 3674 3675 static int in_remove_audio_effect(const struct audio_stream *stream, 3676 effect_handle_t effect) 3677 { 3678 ALOGV("%s: effect %p", __func__, effect); 3679 return add_remove_audio_effect(stream, effect, false); 3680 } 3681 3682 static int adev_open_output_stream(struct audio_hw_device *dev, 3683 audio_io_handle_t handle, 3684 audio_devices_t devices, 3685 audio_output_flags_t flags, 3686 struct audio_config *config, 3687 struct audio_stream_out **stream_out, 3688 const char *address __unused) 3689 { 3690 struct audio_device *adev = (struct audio_device *)dev; 3691 struct stream_out *out; 3692 int i, ret; 3693 struct pcm_device_profile *pcm_profile; 3694 3695 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", 3696 __func__, config->sample_rate, config->channel_mask, devices, flags); 3697 *stream_out = NULL; 3698 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 3699 3700 if (devices == AUDIO_DEVICE_NONE) 3701 devices = AUDIO_DEVICE_OUT_SPEAKER; 3702 3703 out->flags = flags; 3704 out->devices = devices; 3705 out->dev = adev; 3706 out->format = config->format; 3707 out->sample_rate = config->sample_rate; 3708 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 3709 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; 3710 out->handle = handle; 3711 3712 pcm_profile = get_pcm_device(PCM_PLAYBACK, devices); 3713 if (pcm_profile == NULL) { 3714 ret = -EINVAL; 3715 goto error_open; 3716 } 3717 out->config = pcm_profile->config; 3718 3719 /* Init use case and pcm_config */ 3720 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 3721 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || 3722 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { 3723 ALOGE("%s: Unsupported Offload information", __func__); 3724 ret = -EINVAL; 3725 goto error_open; 3726 } 3727 if (!is_supported_format(config->offload_info.format)) { 3728 ALOGE("%s: Unsupported audio format", __func__); 3729 ret = -EINVAL; 3730 goto error_open; 3731 } 3732 3733 out->compr_config.codec = (struct snd_codec *) 3734 calloc(1, sizeof(struct snd_codec)); 3735 3736 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; 3737 if (config->offload_info.channel_mask) 3738 out->channel_mask = config->offload_info.channel_mask; 3739 else if (config->channel_mask) 3740 out->channel_mask = config->channel_mask; 3741 out->format = config->offload_info.format; 3742 out->sample_rate = config->offload_info.sample_rate; 3743 3744 out->stream.set_callback = out_set_callback; 3745 out->stream.pause = out_pause; 3746 out->stream.resume = out_resume; 3747 out->stream.drain = out_drain; 3748 out->stream.flush = out_flush; 3749 3750 out->compr_config.codec->id = 3751 get_snd_codec_id(config->offload_info.format); 3752 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; 3753 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; 3754 out->compr_config.codec->sample_rate = 3755 compress_get_alsa_rate(config->offload_info.sample_rate); 3756 out->compr_config.codec->bit_rate = 3757 config->offload_info.bit_rate; 3758 out->compr_config.codec->ch_in = 3759 audio_channel_count_from_out_mask(config->channel_mask); 3760 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; 3761 3762 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) 3763 out->non_blocking = 1; 3764 3765 out->send_new_metadata = 1; 3766 create_offload_callback_thread(out); 3767 ALOGV("%s: offloaded output offload_info version %04x bit rate %d", 3768 __func__, config->offload_info.version, 3769 config->offload_info.bit_rate); 3770 } else if (out->flags & (AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) { 3771 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; 3772 out->config = pcm_config_deep_buffer; 3773 out->sample_rate = out->config.rate; 3774 ALOGD("%s: use AUDIO_PLAYBACK_DEEP_BUFFER",__func__); 3775 } else { 3776 out->usecase = USECASE_AUDIO_PLAYBACK; 3777 out->sample_rate = out->config.rate; 3778 } 3779 3780 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { 3781 if (adev->primary_output == NULL) 3782 adev->primary_output = out; 3783 else { 3784 ALOGE("%s: Primary output is already opened", __func__); 3785 ret = -EEXIST; 3786 goto error_open; 3787 } 3788 } 3789 3790 /* Check if this usecase is already existing */ 3791 pthread_mutex_lock(&adev->lock); 3792 if (get_usecase_from_id(adev, out->usecase) != NULL) { 3793 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); 3794 pthread_mutex_unlock(&adev->lock); 3795 ret = -EEXIST; 3796 goto error_open; 3797 } 3798 pthread_mutex_unlock(&adev->lock); 3799 3800 out->stream.common.get_sample_rate = out_get_sample_rate; 3801 out->stream.common.set_sample_rate = out_set_sample_rate; 3802 out->stream.common.get_buffer_size = out_get_buffer_size; 3803 out->stream.common.get_channels = out_get_channels; 3804 out->stream.common.get_format = out_get_format; 3805 out->stream.common.set_format = out_set_format; 3806 out->stream.common.standby = out_standby; 3807 out->stream.common.dump = out_dump; 3808 out->stream.common.set_parameters = out_set_parameters; 3809 out->stream.common.get_parameters = out_get_parameters; 3810 out->stream.common.add_audio_effect = out_add_audio_effect; 3811 out->stream.common.remove_audio_effect = out_remove_audio_effect; 3812 out->stream.get_latency = out_get_latency; 3813 out->stream.set_volume = out_set_volume; 3814 out->stream.write = out_write; 3815 out->stream.get_render_position = out_get_render_position; 3816 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 3817 out->stream.get_presentation_position = out_get_presentation_position; 3818 3819 out->standby = 1; 3820 /* out->muted = false; by calloc() */ 3821 /* out->written = 0; by calloc() */ 3822 3823 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); 3824 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); 3825 3826 config->format = out->stream.common.get_format(&out->stream.common); 3827 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 3828 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 3829 3830 *stream_out = &out->stream; 3831 ALOGV("%s: exit", __func__); 3832 return 0; 3833 3834 error_open: 3835 free(out); 3836 *stream_out = NULL; 3837 ALOGD("%s: exit: ret %d", __func__, ret); 3838 return ret; 3839 } 3840 3841 static void adev_close_output_stream(struct audio_hw_device *dev, 3842 struct audio_stream_out *stream) 3843 { 3844 struct stream_out *out = (struct stream_out *)stream; 3845 struct audio_device *adev = out->dev; 3846 (void)dev; 3847 3848 ALOGV("%s: enter", __func__); 3849 out_standby(&stream->common); 3850 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3851 destroy_offload_callback_thread(out); 3852 3853 if (out->compr_config.codec != NULL) 3854 free(out->compr_config.codec); 3855 } 3856 pthread_cond_destroy(&out->cond); 3857 pthread_mutex_destroy(&out->lock); 3858 free(stream); 3859 ALOGV("%s: exit", __func__); 3860 } 3861 3862 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 3863 { 3864 struct audio_device *adev = (struct audio_device *)dev; 3865 struct str_parms *parms; 3866 char *str; 3867 char value[32]; 3868 int val; 3869 int ret; 3870 3871 ALOGV("%s: enter: %s", __func__, kvpairs); 3872 3873 parms = str_parms_create_str(kvpairs); 3874 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 3875 if (ret >= 0) { 3876 int tty_mode; 3877 3878 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 3879 tty_mode = TTY_MODE_OFF; 3880 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 3881 tty_mode = TTY_MODE_VCO; 3882 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 3883 tty_mode = TTY_MODE_HCO; 3884 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 3885 tty_mode = TTY_MODE_FULL; 3886 else 3887 return -EINVAL; 3888 3889 pthread_mutex_lock(&adev->lock); 3890 if (tty_mode != adev->tty_mode) { 3891 adev->tty_mode = tty_mode; 3892 if (adev->in_call) 3893 select_devices(adev, USECASE_VOICE_CALL); 3894 } 3895 pthread_mutex_unlock(&adev->lock); 3896 } 3897 3898 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 3899 if (ret >= 0) { 3900 /* When set to false, HAL should disable EC and NS 3901 * But it is currently not supported. 3902 */ 3903 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 3904 adev->bluetooth_nrec = true; 3905 else 3906 adev->bluetooth_nrec = false; 3907 } 3908 3909 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 3910 if (ret >= 0) { 3911 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 3912 adev->screen_off = false; 3913 else 3914 adev->screen_off = true; 3915 } 3916 3917 ret = str_parms_get_int(parms, "rotation", &val); 3918 if (ret >= 0) { 3919 bool reverse_speakers = false; 3920 switch(val) { 3921 /* FIXME: note that the code below assumes that the speakers are in the correct placement 3922 relative to the user when the device is rotated 90deg from its default rotation. This 3923 assumption is device-specific, not platform-specific like this code. */ 3924 case 270: 3925 reverse_speakers = true; 3926 break; 3927 case 0: 3928 case 90: 3929 case 180: 3930 break; 3931 default: 3932 ALOGE("%s: unexpected rotation of %d", __func__, val); 3933 } 3934 pthread_mutex_lock(&adev->lock); 3935 if (adev->speaker_lr_swap != reverse_speakers) { 3936 adev->speaker_lr_swap = reverse_speakers; 3937 /* only update the selected device if there is active pcm playback */ 3938 struct audio_usecase *usecase; 3939 struct listnode *node; 3940 list_for_each(node, &adev->usecase_list) { 3941 usecase = node_to_item(node, struct audio_usecase, adev_list_node); 3942 if (usecase->type == PCM_PLAYBACK) { 3943 select_devices(adev, usecase->id); 3944 if (adev->htc_acoustic_spk_reverse) 3945 adev->htc_acoustic_spk_reverse(adev->speaker_lr_swap); 3946 break; 3947 } 3948 } 3949 } 3950 pthread_mutex_unlock(&adev->lock); 3951 } 3952 3953 str_parms_destroy(parms); 3954 ALOGV("%s: exit with code(%d)", __func__, ret); 3955 return ret; 3956 } 3957 3958 static char* adev_get_parameters(const struct audio_hw_device *dev, 3959 const char *keys) 3960 { 3961 (void)dev; 3962 (void)keys; 3963 3964 return strdup(""); 3965 } 3966 3967 static int adev_init_check(const struct audio_hw_device *dev) 3968 { 3969 (void)dev; 3970 3971 return 0; 3972 } 3973 3974 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 3975 { 3976 int ret = 0; 3977 struct audio_device *adev = (struct audio_device *)dev; 3978 pthread_mutex_lock(&adev->lock); 3979 /* cache volume */ 3980 adev->voice_volume = volume; 3981 ret = set_voice_volume_l(adev, adev->voice_volume); 3982 pthread_mutex_unlock(&adev->lock); 3983 return ret; 3984 } 3985 3986 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 3987 { 3988 (void)dev; 3989 (void)volume; 3990 3991 return -ENOSYS; 3992 } 3993 3994 static int adev_get_master_volume(struct audio_hw_device *dev, 3995 float *volume) 3996 { 3997 (void)dev; 3998 (void)volume; 3999 4000 return -ENOSYS; 4001 } 4002 4003 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) 4004 { 4005 (void)dev; 4006 (void)muted; 4007 4008 return -ENOSYS; 4009 } 4010 4011 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) 4012 { 4013 (void)dev; 4014 (void)muted; 4015 4016 return -ENOSYS; 4017 } 4018 4019 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 4020 { 4021 struct audio_device *adev = (struct audio_device *)dev; 4022 4023 pthread_mutex_lock(&adev->lock); 4024 if (adev->mode != mode) { 4025 ALOGI("%s mode = %d", __func__, mode); 4026 adev->mode = mode; 4027 pthread_mutex_lock(&adev->tfa9895_lock); 4028 adev->tfa9895_mode_change |= 0x1; 4029 pthread_mutex_unlock(&adev->tfa9895_lock); 4030 } 4031 pthread_mutex_unlock(&adev->lock); 4032 return 0; 4033 } 4034 4035 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 4036 { 4037 struct audio_device *adev = (struct audio_device *)dev; 4038 int err = 0; 4039 4040 pthread_mutex_lock(&adev->lock); 4041 adev->mic_mute = state; 4042 4043 if (adev->mode == AUDIO_MODE_IN_CALL) { 4044 /* TODO */ 4045 } 4046 4047 pthread_mutex_unlock(&adev->lock); 4048 return err; 4049 } 4050 4051 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 4052 { 4053 struct audio_device *adev = (struct audio_device *)dev; 4054 4055 *state = adev->mic_mute; 4056 4057 return 0; 4058 } 4059 4060 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 4061 const struct audio_config *config) 4062 { 4063 (void)dev; 4064 4065 /* NOTE: we default to built in mic which may cause a mismatch between what we 4066 * report here and the actual buffer size 4067 */ 4068 return get_input_buffer_size(config->sample_rate, 4069 config->format, 4070 audio_channel_count_from_in_mask(config->channel_mask), 4071 AUDIO_DEVICE_IN_BUILTIN_MIC); 4072 } 4073 4074 static int adev_open_input_stream(struct audio_hw_device *dev, 4075 audio_io_handle_t handle __unused, 4076 audio_devices_t devices, 4077 struct audio_config *config, 4078 struct audio_stream_in **stream_in, 4079 audio_input_flags_t flags, 4080 const char *address __unused, 4081 audio_source_t source) 4082 { 4083 struct audio_device *adev = (struct audio_device *)dev; 4084 struct stream_in *in; 4085 struct pcm_device_profile *pcm_profile; 4086 4087 ALOGV("%s: enter", __func__); 4088 4089 *stream_in = NULL; 4090 if (check_input_parameters(config->sample_rate, config->format, 4091 audio_channel_count_from_in_mask(config->channel_mask)) != 0) 4092 return -EINVAL; 4093 4094 if (source == AUDIO_SOURCE_HOTWORD) { 4095 pcm_profile = get_pcm_device(PCM_HOTWORD_STREAMING, devices); 4096 } else { 4097 pcm_profile = get_pcm_device(PCM_CAPTURE, devices); 4098 } 4099 if (pcm_profile == NULL) 4100 return -EINVAL; 4101 4102 in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 4103 4104 in->stream.common.get_sample_rate = in_get_sample_rate; 4105 in->stream.common.set_sample_rate = in_set_sample_rate; 4106 in->stream.common.get_buffer_size = in_get_buffer_size; 4107 in->stream.common.get_channels = in_get_channels; 4108 in->stream.common.get_format = in_get_format; 4109 in->stream.common.set_format = in_set_format; 4110 in->stream.common.standby = in_standby; 4111 in->stream.common.dump = in_dump; 4112 in->stream.common.set_parameters = in_set_parameters; 4113 in->stream.common.get_parameters = in_get_parameters; 4114 in->stream.common.add_audio_effect = in_add_audio_effect; 4115 in->stream.common.remove_audio_effect = in_remove_audio_effect; 4116 in->stream.set_gain = in_set_gain; 4117 in->stream.read = in_read; 4118 in->stream.get_input_frames_lost = in_get_input_frames_lost; 4119 4120 in->devices = devices; 4121 in->source = source; 4122 in->dev = adev; 4123 in->standby = 1; 4124 in->main_channels = config->channel_mask; 4125 in->requested_rate = config->sample_rate; 4126 if (config->sample_rate != CAPTURE_DEFAULT_SAMPLING_RATE) 4127 flags = flags & ~AUDIO_INPUT_FLAG_FAST; 4128 in->input_flags = flags; 4129 /* HW codec is limited to default channels. No need to update with 4130 * requested channels */ 4131 in->config = pcm_profile->config; 4132 4133 /* Update config params with the requested sample rate and channels */ 4134 if (source == AUDIO_SOURCE_HOTWORD) { 4135 in->usecase = USECASE_AUDIO_CAPTURE_HOTWORD; 4136 } else { 4137 in->usecase = USECASE_AUDIO_CAPTURE; 4138 } 4139 4140 *stream_in = &in->stream; 4141 ALOGV("%s: exit", __func__); 4142 return 0; 4143 } 4144 4145 static void adev_close_input_stream(struct audio_hw_device *dev, 4146 struct audio_stream_in *stream) 4147 { 4148 struct audio_device *adev = (struct audio_device *)dev; 4149 struct stream_in *in = (struct stream_in*)stream; 4150 ALOGV("%s", __func__); 4151 4152 /* prevent concurrent out_set_parameters, or out_write from standby */ 4153 pthread_mutex_lock(&adev->lock_inputs); 4154 4155 #ifdef PREPROCESSING_ENABLED 4156 int i; 4157 4158 for (i=0; i<in->num_preprocessors; i++) { 4159 free(in->preprocessors[i].channel_configs); 4160 } 4161 4162 if (in->read_buf) { 4163 free(in->read_buf); 4164 in->read_buf = NULL; 4165 } 4166 4167 if (in->proc_buf_in) { 4168 free(in->proc_buf_in); 4169 in->proc_buf_in = NULL; 4170 } 4171 4172 if (in->proc_buf_out) { 4173 free(in->proc_buf_out); 4174 in->proc_buf_out = NULL; 4175 } 4176 4177 if (in->ref_buf) { 4178 free(in->ref_buf); 4179 in->ref_buf = NULL; 4180 } 4181 4182 if (in->resampler) { 4183 release_resampler(in->resampler); 4184 in->resampler = NULL; 4185 } 4186 #endif 4187 4188 in_standby_l(in); 4189 free(stream); 4190 4191 pthread_mutex_unlock(&adev->lock_inputs); 4192 4193 return; 4194 } 4195 4196 static int adev_dump(const audio_hw_device_t *device, int fd) 4197 { 4198 (void)device; 4199 (void)fd; 4200 4201 return 0; 4202 } 4203 4204 static int adev_close(hw_device_t *device) 4205 { 4206 struct audio_device *adev = (struct audio_device *)device; 4207 audio_device_ref_count--; 4208 free(adev->snd_dev_ref_cnt); 4209 free_mixer_list(adev); 4210 free(device); 4211 return 0; 4212 } 4213 4214 static void *dummybuf_thread(void *context) 4215 { 4216 ALOGD("%s: enter", __func__); 4217 pthread_detach(pthread_self()); 4218 struct audio_device *adev = (struct audio_device *)context; 4219 struct pcm_config config; 4220 const char *mixer_ctl_name = "Headphone Jack Switch"; 4221 struct mixer *mixer = NULL; 4222 struct mixer_ctl *ctl; 4223 unsigned char *data = NULL; 4224 struct pcm *pcm = NULL; 4225 struct pcm_device_profile *profile = NULL; 4226 4227 memset(&config, 0, sizeof(struct pcm_config)); 4228 if (adev->dummybuf_thread_devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { 4229 profile = &pcm_device_playback_hs; 4230 mixer = mixer_open(profile->card); 4231 } 4232 else 4233 profile = &pcm_device_playback_spk; 4234 4235 memcpy(&config, &profile->config, sizeof(struct pcm_config)); 4236 /* Use large value for stop_threshold so that automatic 4237 trigger for stop is avoided, when this thread fails to write data */ 4238 config.stop_threshold = INT_MAX/2; 4239 pthread_mutex_lock(&adev->dummybuf_thread_lock); 4240 pcm = pcm_open(profile->card, profile->id, 4241 (PCM_OUT | PCM_MONOTONIC), &config); 4242 if (pcm != NULL && !pcm_is_ready(pcm)) { 4243 ALOGE("pcm_open: card=%d, id=%d is not ready", profile->card, profile->id); 4244 pcm_close(pcm); 4245 pcm = NULL; 4246 } else { 4247 ALOGD("pcm_open: card=%d, id=%d", profile->card, profile->id); 4248 } 4249 4250 if (mixer) { 4251 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name); 4252 if (ctl != NULL) 4253 mixer_ctl_set_value(ctl, 0, 1); 4254 else { 4255 ALOGE("Invalid mixer control: name(%s): skip dummy thread", mixer_ctl_name); 4256 adev->dummybuf_thread_active = 1; 4257 mixer_close(mixer); 4258 if (pcm) { 4259 pcm_close(pcm); 4260 pcm = NULL; 4261 } 4262 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4263 return NULL; 4264 } 4265 } 4266 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4267 4268 while (1) { 4269 pthread_mutex_lock(&adev->dummybuf_thread_lock); 4270 if (pcm) { 4271 if (data == NULL) 4272 data = (unsigned char *)calloc(DEEP_BUFFER_OUTPUT_PERIOD_SIZE * 8, 4273 sizeof(unsigned char)); 4274 if (data) { 4275 pcm_write(pcm, (void *)data, DEEP_BUFFER_OUTPUT_PERIOD_SIZE * 8); 4276 adev->dummybuf_thread_active = 1; 4277 } else { 4278 ALOGD("%s: cant open a buffer, retry to open it", __func__); 4279 } 4280 } else { 4281 ALOGD("%s: cant open a output deep stream, retry to open it", __func__); 4282 pcm = pcm_open(profile->card, profile->id, 4283 (PCM_OUT | PCM_MONOTONIC), &config); 4284 if (pcm != NULL && !pcm_is_ready(pcm)) { 4285 ALOGE("pcm_open: card=%d, id=%d is not ready", profile->card, profile->id); 4286 pcm_close(pcm); 4287 pcm = NULL; 4288 } else { 4289 ALOGD("pcm_open: card=%d, id=%d", profile->card, profile->id); 4290 } 4291 } 4292 4293 if (adev->dummybuf_thread_cancel || adev->dummybuf_thread_timeout-- <= 0) { 4294 adev->dummybuf_thread_active = 0; 4295 adev->dummybuf_thread_cancel = 0; 4296 if (pcm) { 4297 ALOGD("pcm_close ++ card=%d, id=%d", profile->card, profile->id); 4298 pcm_close(pcm); 4299 if (mixer) { 4300 mixer_ctl_set_value(ctl, 0, 0); 4301 mixer_close(mixer); 4302 } 4303 ALOGD("pcm_close -- card=%d, id=%d", profile->card, profile->id); 4304 pcm = NULL; 4305 } 4306 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4307 4308 break; 4309 } 4310 4311 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4312 usleep(3000); 4313 } 4314 4315 if (data) 4316 free(data); 4317 4318 return NULL; 4319 } 4320 4321 static void dummybuf_thread_open(struct audio_device *adev) 4322 { 4323 adev->dummybuf_thread_timeout = 6000; /* in 18 sec */ 4324 adev->dummybuf_thread_cancel = 0; 4325 adev->dummybuf_thread_active = 0; 4326 pthread_mutex_init(&adev->dummybuf_thread_lock, (const pthread_mutexattr_t *) NULL); 4327 if (!adev->dummybuf_thread) 4328 pthread_create(&adev->dummybuf_thread, (const pthread_attr_t *) NULL, dummybuf_thread, adev); 4329 } 4330 4331 static void dummybuf_thread_close(struct audio_device *adev) 4332 { 4333 ALOGD("%s: enter", __func__); 4334 int retry_cnt = 20; 4335 4336 if (adev->dummybuf_thread == 0) 4337 return; 4338 4339 pthread_mutex_lock(&adev->dummybuf_thread_lock); 4340 adev->dummybuf_thread_cancel = 1; 4341 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4342 4343 while (retry_cnt > 0) { 4344 pthread_mutex_lock(&adev->dummybuf_thread_lock); 4345 if (adev->dummybuf_thread_active == 0) { 4346 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4347 break; 4348 } 4349 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4350 retry_cnt--; 4351 usleep(1000); 4352 } 4353 4354 pthread_join(adev->dummybuf_thread, (void **) NULL); 4355 pthread_mutex_destroy(&adev->dummybuf_thread_lock); 4356 adev->dummybuf_thread = 0; 4357 } 4358 4359 static int adev_open(const hw_module_t *module, const char *name, 4360 hw_device_t **device) 4361 { 4362 struct audio_device *adev; 4363 int i, ret, retry_count; 4364 4365 ALOGD("%s: enter", __func__); 4366 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; 4367 4368 adev = calloc(1, sizeof(struct audio_device)); 4369 4370 adev->device.common.tag = HARDWARE_DEVICE_TAG; 4371 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 4372 adev->device.common.module = (struct hw_module_t *)module; 4373 adev->device.common.close = adev_close; 4374 4375 adev->device.init_check = adev_init_check; 4376 adev->device.set_voice_volume = adev_set_voice_volume; 4377 adev->device.set_master_volume = adev_set_master_volume; 4378 adev->device.get_master_volume = adev_get_master_volume; 4379 adev->device.set_master_mute = adev_set_master_mute; 4380 adev->device.get_master_mute = adev_get_master_mute; 4381 adev->device.set_mode = adev_set_mode; 4382 adev->device.set_mic_mute = adev_set_mic_mute; 4383 adev->device.get_mic_mute = adev_get_mic_mute; 4384 adev->device.set_parameters = adev_set_parameters; 4385 adev->device.get_parameters = adev_get_parameters; 4386 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 4387 adev->device.open_output_stream = adev_open_output_stream; 4388 adev->device.close_output_stream = adev_close_output_stream; 4389 adev->device.open_input_stream = adev_open_input_stream; 4390 adev->device.close_input_stream = adev_close_input_stream; 4391 adev->device.dump = adev_dump; 4392 4393 /* Set the default route before the PCM stream is opened */ 4394 adev->mode = AUDIO_MODE_NORMAL; 4395 adev->active_input = NULL; 4396 adev->primary_output = NULL; 4397 adev->voice_volume = 1.0f; 4398 adev->tty_mode = TTY_MODE_OFF; 4399 adev->bluetooth_nrec = true; 4400 adev->in_call = false; 4401 /* adev->cur_hdmi_channels = 0; by calloc() */ 4402 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); 4403 4404 adev->dualmic_config = DUALMIC_CONFIG_NONE; 4405 adev->ns_in_voice_rec = false; 4406 4407 list_init(&adev->usecase_list); 4408 4409 if (mixer_init(adev) != 0) { 4410 free(adev->snd_dev_ref_cnt); 4411 free(adev); 4412 ALOGE("%s: Failed to init, aborting.", __func__); 4413 *device = NULL; 4414 return -EINVAL; 4415 } 4416 4417 if (access(OFFLOAD_FX_LIBRARY_PATH, R_OK) == 0) { 4418 adev->offload_fx_lib = dlopen(OFFLOAD_FX_LIBRARY_PATH, RTLD_NOW); 4419 if (adev->offload_fx_lib == NULL) { 4420 ALOGE("%s: DLOPEN failed for %s", __func__, OFFLOAD_FX_LIBRARY_PATH); 4421 } else { 4422 ALOGV("%s: DLOPEN successful for %s", __func__, OFFLOAD_FX_LIBRARY_PATH); 4423 adev->offload_fx_start_output = 4424 (int (*)(audio_io_handle_t))dlsym(adev->offload_fx_lib, 4425 "visualizer_hal_start_output"); 4426 adev->offload_fx_stop_output = 4427 (int (*)(audio_io_handle_t))dlsym(adev->offload_fx_lib, 4428 "visualizer_hal_stop_output"); 4429 } 4430 } 4431 4432 if (access(HTC_ACOUSTIC_LIBRARY_PATH, R_OK) == 0) { 4433 adev->htc_acoustic_lib = dlopen(HTC_ACOUSTIC_LIBRARY_PATH, RTLD_NOW); 4434 if (adev->htc_acoustic_lib == NULL) { 4435 ALOGE("%s: DLOPEN failed for %s", __func__, HTC_ACOUSTIC_LIBRARY_PATH); 4436 } else { 4437 ALOGV("%s: DLOPEN successful for %s", __func__, HTC_ACOUSTIC_LIBRARY_PATH); 4438 adev->htc_acoustic_init_rt5506 = 4439 (int (*)())dlsym(adev->htc_acoustic_lib, 4440 "init_rt5506"); 4441 adev->htc_acoustic_set_rt5506_amp = 4442 (int (*)(int, int))dlsym(adev->htc_acoustic_lib, 4443 "set_rt5506_amp"); 4444 adev->htc_acoustic_set_amp_mode = 4445 (int (*)(int , int, int, int, bool))dlsym(adev->htc_acoustic_lib, 4446 "set_amp_mode"); 4447 adev->htc_acoustic_spk_reverse = 4448 (int (*)(bool))dlsym(adev->htc_acoustic_lib, 4449 "spk_reverse"); 4450 if (adev->htc_acoustic_spk_reverse) 4451 adev->htc_acoustic_spk_reverse(adev->speaker_lr_swap); 4452 } 4453 } 4454 4455 if (access(SOUND_TRIGGER_HAL_LIBRARY_PATH, R_OK) == 0) { 4456 adev->sound_trigger_lib = dlopen(SOUND_TRIGGER_HAL_LIBRARY_PATH, RTLD_NOW); 4457 if (adev->sound_trigger_lib == NULL) { 4458 ALOGE("%s: DLOPEN failed for %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH); 4459 } else { 4460 ALOGV("%s: DLOPEN successful for %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH); 4461 adev->sound_trigger_open_for_streaming = 4462 (int (*)(void))dlsym(adev->sound_trigger_lib, 4463 "sound_trigger_open_for_streaming"); 4464 adev->sound_trigger_read_samples = 4465 (int (*)(audio_io_handle_t, int))dlsym(adev->sound_trigger_lib, 4466 "sound_trigger_read_samples"); 4467 adev->sound_trigger_close_for_streaming = 4468 (int (*)(int))dlsym(adev->sound_trigger_lib, 4469 "sound_trigger_close_for_streaming"); 4470 if (!adev->sound_trigger_open_for_streaming || 4471 !adev->sound_trigger_read_samples || 4472 !adev->sound_trigger_close_for_streaming) { 4473 4474 ALOGE("%s: Error grabbing functions in %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH); 4475 adev->sound_trigger_open_for_streaming = 0; 4476 adev->sound_trigger_read_samples = 0; 4477 adev->sound_trigger_close_for_streaming = 0; 4478 } 4479 } 4480 } 4481 4482 4483 *device = &adev->device.common; 4484 4485 if (adev->htc_acoustic_init_rt5506 != NULL) 4486 adev->htc_acoustic_init_rt5506(); 4487 4488 if (audio_device_ref_count == 0) { 4489 /* For HS GPIO initial config */ 4490 adev->dummybuf_thread_devices = AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 4491 dummybuf_thread_open(adev); 4492 retry_count = RETRY_NUMBER; 4493 while (retry_count-- > 0) { 4494 pthread_mutex_lock(&adev->dummybuf_thread_lock); 4495 if (adev->dummybuf_thread_active != 0) { 4496 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4497 break; 4498 } 4499 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4500 usleep(10000); 4501 } 4502 dummybuf_thread_close(adev); 4503 4504 /* For NXP DSP config */ 4505 if (adev->htc_acoustic_set_amp_mode) { 4506 pthread_t th; 4507 adev->dummybuf_thread_devices = AUDIO_DEVICE_OUT_SPEAKER; 4508 dummybuf_thread_open(adev); 4509 pthread_mutex_lock(&adev->dummybuf_thread_lock); 4510 retry_count = RETRY_NUMBER; 4511 while (retry_count-- > 0) { 4512 if (adev->dummybuf_thread_active) { 4513 break; 4514 } 4515 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4516 usleep(10000); 4517 pthread_mutex_lock(&adev->dummybuf_thread_lock); 4518 } 4519 if (adev->dummybuf_thread_active) { 4520 usleep(10000); /* tfa9895 spk amp need more than 1ms i2s signal before giving dsp related i2c commands*/ 4521 if (pthread_create(&th, NULL, tfa9895_config_thread, (void* )adev) != 0) { 4522 ALOGE("@@##THREAD_FADE_IN_UPPER_SPEAKER thread create fail"); 4523 } 4524 } 4525 pthread_mutex_unlock(&adev->dummybuf_thread_lock); 4526 /* Then, dummybuf_thread_close() is called by tfa9895_config_thread() */ 4527 } 4528 } 4529 audio_device_ref_count++; 4530 4531 ALOGV("%s: exit", __func__); 4532 return 0; 4533 } 4534 4535 static struct hw_module_methods_t hal_module_methods = { 4536 .open = adev_open, 4537 }; 4538 4539 struct audio_module HAL_MODULE_INFO_SYM = { 4540 .common = { 4541 .tag = HARDWARE_MODULE_TAG, 4542 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 4543 .hal_api_version = HARDWARE_HAL_API_VERSION, 4544 .id = AUDIO_HARDWARE_MODULE_ID, 4545 .name = "NVIDIA Tegra Audio HAL", 4546 .author = "The Android Open Source Project", 4547 .methods = &hal_module_methods, 4548 }, 4549 }; 4550