1 /* 2 * Copyright (C) 2013-2016 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 ATRACE_TAG ATRACE_TAG_AUDIO 19 /*#define LOG_NDEBUG 0*/ 20 /*#define VERY_VERY_VERBOSE_LOGGING*/ 21 #ifdef VERY_VERY_VERBOSE_LOGGING 22 #define ALOGVV ALOGV 23 #else 24 #define ALOGVV(a...) do { } while(0) 25 #endif 26 27 #include <errno.h> 28 #include <pthread.h> 29 #include <stdint.h> 30 #include <sys/time.h> 31 #include <stdlib.h> 32 #include <math.h> 33 #include <dlfcn.h> 34 #include <sys/resource.h> 35 #include <sys/prctl.h> 36 37 #include <cutils/log.h> 38 #include <cutils/trace.h> 39 #include <cutils/str_parms.h> 40 #include <cutils/properties.h> 41 #include <cutils/atomic.h> 42 #include <cutils/sched_policy.h> 43 44 #include <hardware/audio_effect.h> 45 #include <hardware/audio_alsaops.h> 46 #include <system/thread_defs.h> 47 #include <audio_effects/effect_aec.h> 48 #include <audio_effects/effect_ns.h> 49 #include "audio_hw.h" 50 #include "audio_extn.h" 51 #include "platform_api.h" 52 #include <platform.h> 53 #include "voice_extn.h" 54 55 #include "sound/compress_params.h" 56 57 /* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB. 58 * COMPRESS_OFFLOAD_FRAGMENT_SIZE * COMPRESS_OFFLOAD_NUM_FRAGMENTS must be less than 8MB. */ 59 #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024) 60 // 2 buffers causes problems with high bitrate files 61 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3 62 /* ToDo: Check and update a proper value in msec */ 63 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 64 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 65 66 #define PROXY_OPEN_RETRY_COUNT 100 67 #define PROXY_OPEN_WAIT_TIME 20 68 69 #define MIN_CHANNEL_COUNT 1 70 #define DEFAULT_CHANNEL_COUNT 2 71 72 #ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT 73 #define MAX_CHANNEL_COUNT 1 74 #else 75 #define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT)) 76 #define XSTR(x) STR(x) 77 #define STR(x) #x 78 #endif 79 80 #define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) 81 82 static unsigned int configured_low_latency_capture_period_size = 83 LOW_LATENCY_CAPTURE_PERIOD_SIZE; 84 85 /* This constant enables extended precision handling. 86 * TODO The flag is off until more testing is done. 87 */ 88 static const bool k_enable_extended_precision = false; 89 90 struct pcm_config pcm_config_deep_buffer = { 91 .channels = DEFAULT_CHANNEL_COUNT, 92 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 93 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, 94 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, 95 .format = PCM_FORMAT_S16_LE, 96 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 97 .stop_threshold = INT_MAX, 98 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 99 }; 100 101 struct pcm_config pcm_config_low_latency = { 102 .channels = DEFAULT_CHANNEL_COUNT, 103 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 104 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, 105 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, 106 .format = PCM_FORMAT_S16_LE, 107 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 108 .stop_threshold = INT_MAX, 109 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 110 }; 111 112 static int af_period_multiplier = 4; 113 struct pcm_config pcm_config_rt = { 114 .channels = DEFAULT_CHANNEL_COUNT, 115 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 116 .period_size = ULL_PERIOD_SIZE, //1 ms 117 .period_count = 512, //=> buffer size is 512ms 118 .format = PCM_FORMAT_S16_LE, 119 .start_threshold = ULL_PERIOD_SIZE*8, //8ms 120 .stop_threshold = INT_MAX, 121 .silence_threshold = 0, 122 .silence_size = 0, 123 .avail_min = ULL_PERIOD_SIZE, //1 ms 124 }; 125 126 struct pcm_config pcm_config_hdmi_multi = { 127 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ 128 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ 129 .period_size = HDMI_MULTI_PERIOD_SIZE, 130 .period_count = HDMI_MULTI_PERIOD_COUNT, 131 .format = PCM_FORMAT_S16_LE, 132 .start_threshold = 0, 133 .stop_threshold = INT_MAX, 134 .avail_min = 0, 135 }; 136 137 struct pcm_config pcm_config_audio_capture = { 138 .channels = DEFAULT_CHANNEL_COUNT, 139 .period_count = AUDIO_CAPTURE_PERIOD_COUNT, 140 .format = PCM_FORMAT_S16_LE, 141 .stop_threshold = INT_MAX, 142 .avail_min = 0, 143 }; 144 145 struct pcm_config pcm_config_audio_capture_rt = { 146 .channels = DEFAULT_CHANNEL_COUNT, 147 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 148 .period_size = ULL_PERIOD_SIZE, 149 .period_count = 512, 150 .format = PCM_FORMAT_S16_LE, 151 .start_threshold = 0, 152 .stop_threshold = INT_MAX, 153 .silence_threshold = 0, 154 .silence_size = 0, 155 .avail_min = ULL_PERIOD_SIZE, //1 ms 156 }; 157 158 #define AFE_PROXY_CHANNEL_COUNT 2 159 #define AFE_PROXY_SAMPLING_RATE 48000 160 161 #define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768 162 #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4 163 164 struct pcm_config pcm_config_afe_proxy_playback = { 165 .channels = AFE_PROXY_CHANNEL_COUNT, 166 .rate = AFE_PROXY_SAMPLING_RATE, 167 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE, 168 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT, 169 .format = PCM_FORMAT_S16_LE, 170 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE, 171 .stop_threshold = INT_MAX, 172 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE, 173 }; 174 175 #define AFE_PROXY_RECORD_PERIOD_SIZE 768 176 #define AFE_PROXY_RECORD_PERIOD_COUNT 4 177 178 struct pcm_config pcm_config_afe_proxy_record = { 179 .channels = AFE_PROXY_CHANNEL_COUNT, 180 .rate = AFE_PROXY_SAMPLING_RATE, 181 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE, 182 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT, 183 .format = PCM_FORMAT_S16_LE, 184 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE, 185 .stop_threshold = INT_MAX, 186 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE, 187 }; 188 189 const char * const use_case_table[AUDIO_USECASE_MAX] = { 190 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", 191 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", 192 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", 193 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", 194 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback", 195 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback", 196 197 [USECASE_AUDIO_RECORD] = "audio-record", 198 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", 199 200 [USECASE_AUDIO_HFP_SCO] = "hfp-sco", 201 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb", 202 203 [USECASE_VOICE_CALL] = "voice-call", 204 [USECASE_VOICE2_CALL] = "voice2-call", 205 [USECASE_VOLTE_CALL] = "volte-call", 206 [USECASE_QCHAT_CALL] = "qchat-call", 207 [USECASE_VOWLAN_CALL] = "vowlan-call", 208 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call", 209 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call", 210 211 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib", 212 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record", 213 214 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback", 215 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record", 216 }; 217 218 219 #define STRING_TO_ENUM(string) { #string, string } 220 221 struct string_to_enum { 222 const char *name; 223 uint32_t value; 224 }; 225 226 static const struct string_to_enum out_channels_name_to_enum_table[] = { 227 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 228 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 229 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 230 }; 231 232 static int set_voice_volume_l(struct audio_device *adev, float volume); 233 static struct audio_device *adev = NULL; 234 static pthread_mutex_t adev_init_lock; 235 static unsigned int audio_device_ref_count; 236 //cache last MBDRC cal step level 237 static int last_known_cal_step = -1 ; 238 239 static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id, 240 int flags __unused) 241 { 242 int dir = 0; 243 switch (uc_id) { 244 case USECASE_AUDIO_RECORD_LOW_LATENCY: 245 dir = 1; 246 case USECASE_AUDIO_PLAYBACK_ULL: 247 break; 248 default: 249 return false; 250 } 251 252 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ? 253 PCM_PLAYBACK : PCM_CAPTURE); 254 if (adev->adm_is_noirq_avail) 255 return adev->adm_is_noirq_avail(adev->adm_data, 256 adev->snd_card, dev_id, dir); 257 return false; 258 } 259 260 static void register_out_stream(struct stream_out *out) 261 { 262 struct audio_device *adev = out->dev; 263 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) 264 return; 265 266 if (!adev->adm_register_output_stream) 267 return; 268 269 adev->adm_register_output_stream(adev->adm_data, 270 out->handle, 271 out->flags); 272 273 if (!adev->adm_set_config) 274 return; 275 276 if (out->realtime) { 277 adev->adm_set_config(adev->adm_data, 278 out->handle, 279 out->pcm, &out->config); 280 } 281 } 282 283 static void register_in_stream(struct stream_in *in) 284 { 285 struct audio_device *adev = in->dev; 286 if (!adev->adm_register_input_stream) 287 return; 288 289 adev->adm_register_input_stream(adev->adm_data, 290 in->capture_handle, 291 in->flags); 292 293 if (!adev->adm_set_config) 294 return; 295 296 if (in->realtime) { 297 adev->adm_set_config(adev->adm_data, 298 in->capture_handle, 299 in->pcm, 300 &in->config); 301 } 302 } 303 304 static void request_out_focus(struct stream_out *out, long ns) 305 { 306 struct audio_device *adev = out->dev; 307 308 if (out->routing_change) { 309 out->routing_change = false; 310 if (adev->adm_on_routing_change) 311 adev->adm_on_routing_change(adev->adm_data, out->handle); 312 } 313 314 if (adev->adm_request_focus_v2) { 315 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns); 316 } else if (adev->adm_request_focus) { 317 adev->adm_request_focus(adev->adm_data, out->handle); 318 } 319 } 320 321 static void request_in_focus(struct stream_in *in, long ns) 322 { 323 struct audio_device *adev = in->dev; 324 325 if (in->routing_change) { 326 in->routing_change = false; 327 if (adev->adm_on_routing_change) 328 adev->adm_on_routing_change(adev->adm_data, in->capture_handle); 329 } 330 331 if (adev->adm_request_focus_v2) { 332 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns); 333 } else if (adev->adm_request_focus) { 334 adev->adm_request_focus(adev->adm_data, in->capture_handle); 335 } 336 } 337 338 static void release_out_focus(struct stream_out *out, long ns __unused) 339 { 340 struct audio_device *adev = out->dev; 341 342 if (adev->adm_abandon_focus) 343 adev->adm_abandon_focus(adev->adm_data, out->handle); 344 } 345 346 static void release_in_focus(struct stream_in *in, long ns __unused) 347 { 348 struct audio_device *adev = in->dev; 349 if (adev->adm_abandon_focus) 350 adev->adm_abandon_focus(adev->adm_data, in->capture_handle); 351 } 352 353 // Time string format similar to logcat, buffer_length must be >= 19 chars. 354 static void ns2string(int64_t ns, char *buffer, int buffer_length) 355 { 356 const int one_second = 1000000000; 357 const time_t sec = ns / one_second; 358 struct tm tm; 359 localtime_r(&sec, &tm); 360 snprintf(buffer, buffer_length, "%02d-%02d %02d:%02d:%02d.%03d", 361 tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range 362 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, 363 (int)(ns % one_second / 1000000)); 364 } 365 366 // Convert timespec to nsec. 367 static int64_t ts2ns(const struct timespec *ts) 368 { 369 return ts->tv_sec * 1000000000LL + ts->tv_nsec; 370 } 371 372 // Log errors: consecutive errors with the same code will 373 // be aggregated if they occur within one second. 374 // A mutual exclusion lock must be held before calling. 375 static void log_error_l(struct error_log *log, int code) { 376 ++log->errors; 377 378 struct timespec now_ts = { 0, 0 }; 379 (void)clock_gettime(CLOCK_REALTIME, &now_ts); 380 const int64_t now = ts2ns(&now_ts); 381 382 // Within 1 second, cluster the same error codes together. 383 const int one_second = 1000000000; 384 if (code == log->entries[log->idx].code && 385 now - log->entries[log->idx].last_time < one_second) { 386 log->entries[log->idx].count++; 387 log->entries[log->idx].last_time = now; 388 return; 389 } 390 391 // Add new error entry. 392 if (++log->idx >= ARRAY_SIZE(log->entries)) { 393 log->idx = 0; 394 } 395 log->entries[log->idx].count = 1; 396 log->entries[log->idx].code = code; 397 log->entries[log->idx].first_time = now; 398 log->entries[log->idx].last_time = now; 399 } 400 401 // Dump information in the error log. A mutual exclusion lock 402 // should be held, but if that cannot be obtained, one should 403 // make a copy of the error log before calling -- the call is 404 // still safe, but there might be some misinterpreted data. 405 static void log_dump_l(const struct error_log *log, int fd) 406 { 407 dprintf(fd, " Errors: %u\n", log->errors); 408 if (log->errors == 0) 409 return; 410 411 dprintf(fd, " Index Code Freq First time Last time\n"); 412 for (size_t i = 0; i < ARRAY_SIZE(log->entries); ++i) { 413 if (log->entries[i].count != 0) { 414 char first_time[32]; 415 char last_time[32]; 416 ns2string(log->entries[i].first_time, first_time, sizeof(first_time)); 417 ns2string(log->entries[i].last_time, last_time, sizeof(last_time)); 418 dprintf(fd, " %c%4zu %4d %5d %s %s\n", 419 i == log->idx ? '*' : ' ', // mark head position 420 i, log->entries[i].code, log->entries[i].count, 421 first_time, last_time); 422 } 423 } 424 } 425 426 static int parse_snd_card_status(struct str_parms * parms, int * card, 427 card_status_t * status) 428 { 429 char value[32]={0}; 430 char state[32]={0}; 431 432 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value)); 433 434 if (ret < 0) 435 return -1; 436 437 // sscanf should be okay as value is of max length 32. 438 // same as sizeof state. 439 if (sscanf(value, "%d,%s", card, state) < 2) 440 return -1; 441 442 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE : 443 CARD_STATUS_OFFLINE; 444 return 0; 445 } 446 447 __attribute__ ((visibility ("default"))) 448 bool audio_hw_send_gain_dep_calibration(int level) { 449 bool ret_val = false; 450 ALOGV("%s: enter ... ", __func__); 451 452 pthread_mutex_lock(&adev_init_lock); 453 454 if (adev != NULL && adev->platform != NULL) { 455 pthread_mutex_lock(&adev->lock); 456 ret_val = platform_send_gain_dep_cal(adev->platform, level); 457 pthread_mutex_unlock(&adev->lock); 458 459 // if cal set fails, cache level info 460 // if cal set succeds, reset known last cal set 461 if (!ret_val) 462 last_known_cal_step = level; 463 else if (last_known_cal_step != -1) 464 last_known_cal_step = -1; 465 } else { 466 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform"); 467 } 468 469 pthread_mutex_unlock(&adev_init_lock); 470 471 ALOGV("%s: exit with ret_val %d ", __func__, ret_val); 472 return ret_val; 473 } 474 475 __attribute__ ((visibility ("default"))) 476 int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl, 477 int table_size) { 478 int ret_val = 0; 479 ALOGV("%s: enter ... ", __func__); 480 481 pthread_mutex_lock(&adev_init_lock); 482 if (adev == NULL) { 483 ALOGW("%s: adev is NULL .... ", __func__); 484 goto done; 485 } 486 487 pthread_mutex_lock(&adev->lock); 488 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size); 489 pthread_mutex_unlock(&adev->lock); 490 done: 491 pthread_mutex_unlock(&adev_init_lock); 492 ALOGV("%s: exit ... ", __func__); 493 return ret_val; 494 } 495 496 static bool is_supported_format(audio_format_t format) 497 { 498 switch (format) { 499 case AUDIO_FORMAT_MP3: 500 case AUDIO_FORMAT_AAC_LC: 501 case AUDIO_FORMAT_AAC_HE_V1: 502 case AUDIO_FORMAT_AAC_HE_V2: 503 return true; 504 default: 505 break; 506 } 507 return false; 508 } 509 510 static inline bool is_mmap_usecase(audio_usecase_t uc_id) 511 { 512 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) || 513 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY); 514 } 515 516 static int get_snd_codec_id(audio_format_t format) 517 { 518 int id = 0; 519 520 switch (format & AUDIO_FORMAT_MAIN_MASK) { 521 case AUDIO_FORMAT_MP3: 522 id = SND_AUDIOCODEC_MP3; 523 break; 524 case AUDIO_FORMAT_AAC: 525 id = SND_AUDIOCODEC_AAC; 526 break; 527 default: 528 ALOGE("%s: Unsupported audio format", __func__); 529 } 530 531 return id; 532 } 533 534 int enable_audio_route(struct audio_device *adev, 535 struct audio_usecase *usecase) 536 { 537 snd_device_t snd_device; 538 char mixer_path[50]; 539 540 if (usecase == NULL) 541 return -EINVAL; 542 543 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 544 545 if (usecase->type == PCM_CAPTURE) 546 snd_device = usecase->in_snd_device; 547 else 548 snd_device = usecase->out_snd_device; 549 550 strcpy(mixer_path, use_case_table[usecase->id]); 551 platform_add_backend_name(adev->platform, mixer_path, snd_device); 552 ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__, usecase->id, mixer_path); 553 audio_route_apply_and_update_path(adev->audio_route, mixer_path); 554 555 ALOGV("%s: exit", __func__); 556 return 0; 557 } 558 559 int disable_audio_route(struct audio_device *adev, 560 struct audio_usecase *usecase) 561 { 562 snd_device_t snd_device; 563 char mixer_path[50]; 564 565 if (usecase == NULL) 566 return -EINVAL; 567 568 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 569 if (usecase->type == PCM_CAPTURE) 570 snd_device = usecase->in_snd_device; 571 else 572 snd_device = usecase->out_snd_device; 573 strcpy(mixer_path, use_case_table[usecase->id]); 574 platform_add_backend_name(adev->platform, mixer_path, snd_device); 575 ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path); 576 audio_route_reset_and_update_path(adev->audio_route, mixer_path); 577 578 ALOGV("%s: exit", __func__); 579 return 0; 580 } 581 582 int enable_snd_device(struct audio_device *adev, 583 snd_device_t snd_device) 584 { 585 int i, num_devices = 0; 586 snd_device_t new_snd_devices[2]; 587 int ret_val = -EINVAL; 588 if (snd_device < SND_DEVICE_MIN || 589 snd_device >= SND_DEVICE_MAX) { 590 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 591 goto on_error; 592 } 593 594 platform_send_audio_calibration(adev->platform, snd_device); 595 596 if (adev->snd_dev_ref_cnt[snd_device] >= 1) { 597 ALOGV("%s: snd_device(%d: %s) is already active", 598 __func__, snd_device, platform_get_snd_device_name(snd_device)); 599 goto on_success; 600 } 601 602 /* due to the possibility of calibration overwrite between listen 603 and audio, notify sound trigger hal before audio calibration is sent */ 604 audio_extn_sound_trigger_update_device_status(snd_device, 605 ST_EVENT_SND_DEVICE_BUSY); 606 607 if (audio_extn_spkr_prot_is_enabled()) 608 audio_extn_spkr_prot_calib_cancel(adev); 609 610 audio_extn_dsm_feedback_enable(adev, snd_device, true); 611 612 if ((snd_device == SND_DEVICE_OUT_SPEAKER || 613 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && 614 audio_extn_spkr_prot_is_enabled()) { 615 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) { 616 goto on_error; 617 } 618 if (audio_extn_spkr_prot_start_processing(snd_device)) { 619 ALOGE("%s: spkr_start_processing failed", __func__); 620 goto on_error; 621 } 622 } else if (platform_can_split_snd_device(snd_device, 623 &num_devices, 624 new_snd_devices) == 0) { 625 for (i = 0; i < num_devices; i++) { 626 enable_snd_device(adev, new_snd_devices[i]); 627 } 628 platform_set_speaker_gain_in_combo(adev, snd_device, true); 629 } else { 630 char device_name[DEVICE_NAME_MAX_SIZE] = {0}; 631 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { 632 ALOGE(" %s: Invalid sound device returned", __func__); 633 goto on_error; 634 } 635 636 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); 637 audio_route_apply_and_update_path(adev->audio_route, device_name); 638 } 639 on_success: 640 adev->snd_dev_ref_cnt[snd_device]++; 641 ret_val = 0; 642 on_error: 643 return ret_val; 644 } 645 646 int disable_snd_device(struct audio_device *adev, 647 snd_device_t snd_device) 648 { 649 int i, num_devices = 0; 650 snd_device_t new_snd_devices[2]; 651 652 if (snd_device < SND_DEVICE_MIN || 653 snd_device >= SND_DEVICE_MAX) { 654 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 655 return -EINVAL; 656 } 657 if (adev->snd_dev_ref_cnt[snd_device] <= 0) { 658 ALOGE("%s: device ref cnt is already 0", __func__); 659 return -EINVAL; 660 } 661 adev->snd_dev_ref_cnt[snd_device]--; 662 if (adev->snd_dev_ref_cnt[snd_device] == 0) { 663 audio_extn_dsm_feedback_enable(adev, snd_device, false); 664 if ((snd_device == SND_DEVICE_OUT_SPEAKER || 665 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && 666 audio_extn_spkr_prot_is_enabled()) { 667 audio_extn_spkr_prot_stop_processing(snd_device); 668 } else if (platform_can_split_snd_device(snd_device, 669 &num_devices, 670 new_snd_devices) == 0) { 671 for (i = 0; i < num_devices; i++) { 672 disable_snd_device(adev, new_snd_devices[i]); 673 } 674 platform_set_speaker_gain_in_combo(adev, snd_device, false); 675 } else { 676 char device_name[DEVICE_NAME_MAX_SIZE] = {0}; 677 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { 678 ALOGE(" %s: Invalid sound device returned", __func__); 679 return -EINVAL; 680 } 681 682 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); 683 audio_route_reset_and_update_path(adev->audio_route, device_name); 684 } 685 audio_extn_sound_trigger_update_device_status(snd_device, 686 ST_EVENT_SND_DEVICE_FREE); 687 } 688 689 return 0; 690 } 691 692 /* 693 legend: 694 uc - existing usecase 695 new_uc - new usecase 696 d1, d11, d2 - SND_DEVICE enums 697 a1, a2 - corresponding ANDROID device enums 698 B, B1, B2 - backend strings 699 700 case 1 701 uc->dev d1 (a1) B1 702 new_uc->dev d1 (a1), d2 (a2) B1, B2 703 704 resolution: disable and enable uc->dev on d1 705 706 case 2 707 uc->dev d1 (a1) B1 708 new_uc->dev d11 (a1) B1 709 710 resolution: need to switch uc since d1 and d11 are related 711 (e.g. speaker and voice-speaker) 712 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary 713 714 case 3 715 uc->dev d1 (a1) B1 716 new_uc->dev d2 (a2) B2 717 718 resolution: no need to switch uc 719 720 case 4 721 uc->dev d1 (a1) B 722 new_uc->dev d2 (a2) B 723 724 resolution: disable enable uc-dev on d2 since backends match 725 we cannot enable two streams on two different devices if they 726 share the same backend. e.g. if offload is on speaker device using 727 QUAD_MI2S backend and a low-latency stream is started on voice-handset 728 using the same backend, offload must also be switched to voice-handset. 729 730 case 5 731 uc->dev d1 (a1) B 732 new_uc->dev d1 (a1), d2 (a2) B 733 734 resolution: disable enable uc-dev on d2 since backends match 735 we cannot enable two streams on two different devices if they 736 share the same backend. 737 738 case 6 739 uc->dev d1 a1 B1 740 new_uc->dev d2 a1 B2 741 742 resolution: no need to switch 743 744 case 7 745 746 uc->dev d1 (a1), d2 (a2) B1, B2 747 new_uc->dev d1 B1 748 749 resolution: no need to switch 750 751 */ 752 static snd_device_t derive_playback_snd_device(struct audio_usecase *uc, 753 struct audio_usecase *new_uc, 754 snd_device_t new_snd_device) 755 { 756 audio_devices_t a1 = uc->stream.out->devices; 757 audio_devices_t a2 = new_uc->stream.out->devices; 758 759 snd_device_t d1 = uc->out_snd_device; 760 snd_device_t d2 = new_snd_device; 761 762 // Treat as a special case when a1 and a2 are not disjoint 763 if ((a1 != a2) && (a1 & a2)) { 764 snd_device_t d3[2]; 765 int num_devices = 0; 766 int ret = platform_can_split_snd_device(popcount(a1) > 1 ? d1 : d2, 767 &num_devices, 768 d3); 769 if (ret < 0) { 770 if (ret != -ENOSYS) { 771 ALOGW("%s failed to split snd_device %d", 772 __func__, 773 popcount(a1) > 1 ? d1 : d2); 774 } 775 goto end; 776 } 777 778 // NB: case 7 is hypothetical and isn't a practical usecase yet. 779 // But if it does happen, we need to give priority to d2 if 780 // the combo devices active on the existing usecase share a backend. 781 // This is because we cannot have a usecase active on a combo device 782 // and a new usecase requests one device in this combo pair. 783 if (platform_check_backends_match(d3[0], d3[1])) { 784 return d2; // case 5 785 } else { 786 return d1; // case 1 787 } 788 } else { 789 if (platform_check_backends_match(d1, d2)) { 790 return d2; // case 2, 4 791 } else { 792 return d1; // case 6, 3 793 } 794 } 795 796 end: 797 return d2; // return whatever was calculated before. 798 } 799 800 static void check_and_route_playback_usecases(struct audio_device *adev, 801 struct audio_usecase *uc_info, 802 snd_device_t snd_device) 803 { 804 struct listnode *node; 805 struct audio_usecase *usecase; 806 bool switch_device[AUDIO_USECASE_MAX]; 807 int i, num_uc_to_switch = 0; 808 809 /* 810 * This function is to make sure that all the usecases that are active on 811 * the hardware codec backend are always routed to any one device that is 812 * handled by the hardware codec. 813 * For example, if low-latency and deep-buffer usecases are currently active 814 * on speaker and out_set_parameters(headset) is received on low-latency 815 * output, then we have to make sure deep-buffer is also switched to headset, 816 * because of the limitation that both the devices cannot be enabled 817 * at the same time as they share the same backend. 818 */ 819 /* Disable all the usecases on the shared backend other than the 820 specified usecase */ 821 for (i = 0; i < AUDIO_USECASE_MAX; i++) 822 switch_device[i] = false; 823 824 list_for_each(node, &adev->usecase_list) { 825 usecase = node_to_item(node, struct audio_usecase, list); 826 if (usecase->type != PCM_CAPTURE && 827 usecase != uc_info && 828 usecase->out_snd_device != snd_device && 829 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND && 830 platform_check_backends_match(snd_device, usecase->out_snd_device)) { 831 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 832 __func__, use_case_table[usecase->id], 833 platform_get_snd_device_name(usecase->out_snd_device)); 834 disable_audio_route(adev, usecase); 835 switch_device[usecase->id] = true; 836 num_uc_to_switch++; 837 } 838 } 839 840 if (num_uc_to_switch) { 841 list_for_each(node, &adev->usecase_list) { 842 usecase = node_to_item(node, struct audio_usecase, list); 843 if (switch_device[usecase->id]) { 844 disable_snd_device(adev, usecase->out_snd_device); 845 } 846 } 847 848 snd_device_t d_device; 849 list_for_each(node, &adev->usecase_list) { 850 usecase = node_to_item(node, struct audio_usecase, list); 851 if (switch_device[usecase->id]) { 852 d_device = derive_playback_snd_device(usecase, uc_info, 853 snd_device); 854 enable_snd_device(adev, d_device); 855 /* Update the out_snd_device before enabling the audio route */ 856 usecase->out_snd_device = d_device; 857 } 858 } 859 860 /* Re-route all the usecases on the shared backend other than the 861 specified usecase to new snd devices */ 862 list_for_each(node, &adev->usecase_list) { 863 usecase = node_to_item(node, struct audio_usecase, list); 864 if (switch_device[usecase->id] ) { 865 enable_audio_route(adev, usecase); 866 } 867 } 868 } 869 } 870 871 static void check_and_route_capture_usecases(struct audio_device *adev, 872 struct audio_usecase *uc_info, 873 snd_device_t snd_device) 874 { 875 struct listnode *node; 876 struct audio_usecase *usecase; 877 bool switch_device[AUDIO_USECASE_MAX]; 878 int i, num_uc_to_switch = 0; 879 880 platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device); 881 882 /* 883 * This function is to make sure that all the active capture usecases 884 * are always routed to the same input sound device. 885 * For example, if audio-record and voice-call usecases are currently 886 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) 887 * is received for voice call then we have to make sure that audio-record 888 * usecase is also switched to earpiece i.e. voice-dmic-ef, 889 * because of the limitation that two devices cannot be enabled 890 * at the same time if they share the same backend. 891 */ 892 for (i = 0; i < AUDIO_USECASE_MAX; i++) 893 switch_device[i] = false; 894 895 list_for_each(node, &adev->usecase_list) { 896 usecase = node_to_item(node, struct audio_usecase, list); 897 if (usecase->type != PCM_PLAYBACK && 898 usecase != uc_info && 899 usecase->in_snd_device != snd_device && 900 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) { 901 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 902 __func__, use_case_table[usecase->id], 903 platform_get_snd_device_name(usecase->in_snd_device)); 904 disable_audio_route(adev, usecase); 905 switch_device[usecase->id] = true; 906 num_uc_to_switch++; 907 } 908 } 909 910 if (num_uc_to_switch) { 911 list_for_each(node, &adev->usecase_list) { 912 usecase = node_to_item(node, struct audio_usecase, list); 913 if (switch_device[usecase->id]) { 914 disable_snd_device(adev, usecase->in_snd_device); 915 } 916 } 917 918 list_for_each(node, &adev->usecase_list) { 919 usecase = node_to_item(node, struct audio_usecase, list); 920 if (switch_device[usecase->id]) { 921 enable_snd_device(adev, snd_device); 922 } 923 } 924 925 /* Re-route all the usecases on the shared backend other than the 926 specified usecase to new snd devices */ 927 list_for_each(node, &adev->usecase_list) { 928 usecase = node_to_item(node, struct audio_usecase, list); 929 /* Update the in_snd_device only before enabling the audio route */ 930 if (switch_device[usecase->id] ) { 931 usecase->in_snd_device = snd_device; 932 enable_audio_route(adev, usecase); 933 } 934 } 935 } 936 } 937 938 /* must be called with hw device mutex locked */ 939 static int read_hdmi_channel_masks(struct stream_out *out) 940 { 941 int ret = 0; 942 int channels = platform_edid_get_max_channels(out->dev->platform); 943 944 switch (channels) { 945 /* 946 * Do not handle stereo output in Multi-channel cases 947 * Stereo case is handled in normal playback path 948 */ 949 case 6: 950 ALOGV("%s: HDMI supports 5.1", __func__); 951 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 952 break; 953 case 8: 954 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); 955 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 956 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; 957 break; 958 default: 959 ALOGE("HDMI does not support multi channel playback"); 960 ret = -ENOSYS; 961 break; 962 } 963 return ret; 964 } 965 966 static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev) 967 { 968 struct audio_usecase *usecase; 969 struct listnode *node; 970 971 list_for_each(node, &adev->usecase_list) { 972 usecase = node_to_item(node, struct audio_usecase, list); 973 if (usecase->type == VOICE_CALL) { 974 ALOGV("%s: usecase id %d", __func__, usecase->id); 975 return usecase->id; 976 } 977 } 978 return USECASE_INVALID; 979 } 980 981 struct audio_usecase *get_usecase_from_list(struct audio_device *adev, 982 audio_usecase_t uc_id) 983 { 984 struct audio_usecase *usecase; 985 struct listnode *node; 986 987 list_for_each(node, &adev->usecase_list) { 988 usecase = node_to_item(node, struct audio_usecase, list); 989 if (usecase->id == uc_id) 990 return usecase; 991 } 992 return NULL; 993 } 994 995 int select_devices(struct audio_device *adev, 996 audio_usecase_t uc_id) 997 { 998 snd_device_t out_snd_device = SND_DEVICE_NONE; 999 snd_device_t in_snd_device = SND_DEVICE_NONE; 1000 struct audio_usecase *usecase = NULL; 1001 struct audio_usecase *vc_usecase = NULL; 1002 struct audio_usecase *hfp_usecase = NULL; 1003 audio_usecase_t hfp_ucid; 1004 struct listnode *node; 1005 int status = 0; 1006 1007 usecase = get_usecase_from_list(adev, uc_id); 1008 if (usecase == NULL) { 1009 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); 1010 return -EINVAL; 1011 } 1012 1013 if ((usecase->type == VOICE_CALL) || 1014 (usecase->type == PCM_HFP_CALL)) { 1015 out_snd_device = platform_get_output_snd_device(adev->platform, 1016 usecase->stream.out->devices); 1017 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices); 1018 usecase->devices = usecase->stream.out->devices; 1019 } else { 1020 /* 1021 * If the voice call is active, use the sound devices of voice call usecase 1022 * so that it would not result any device switch. All the usecases will 1023 * be switched to new device when select_devices() is called for voice call 1024 * usecase. This is to avoid switching devices for voice call when 1025 * check_and_route_playback_usecases() is called below. 1026 */ 1027 if (voice_is_in_call(adev)) { 1028 vc_usecase = get_usecase_from_list(adev, 1029 get_voice_usecase_id_from_list(adev)); 1030 if ((vc_usecase != NULL) && 1031 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || 1032 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) { 1033 in_snd_device = vc_usecase->in_snd_device; 1034 out_snd_device = vc_usecase->out_snd_device; 1035 } 1036 } else if (audio_extn_hfp_is_active(adev)) { 1037 hfp_ucid = audio_extn_hfp_get_usecase(); 1038 hfp_usecase = get_usecase_from_list(adev, hfp_ucid); 1039 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 1040 in_snd_device = hfp_usecase->in_snd_device; 1041 out_snd_device = hfp_usecase->out_snd_device; 1042 } 1043 } 1044 if (usecase->type == PCM_PLAYBACK) { 1045 usecase->devices = usecase->stream.out->devices; 1046 in_snd_device = SND_DEVICE_NONE; 1047 if (out_snd_device == SND_DEVICE_NONE) { 1048 out_snd_device = platform_get_output_snd_device(adev->platform, 1049 usecase->stream.out->devices); 1050 if (usecase->stream.out == adev->primary_output && 1051 adev->active_input && 1052 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 1053 adev->mode == AUDIO_MODE_IN_COMMUNICATION) && 1054 out_snd_device != usecase->out_snd_device) { 1055 select_devices(adev, adev->active_input->usecase); 1056 } 1057 } 1058 } else if (usecase->type == PCM_CAPTURE) { 1059 usecase->devices = usecase->stream.in->device; 1060 out_snd_device = SND_DEVICE_NONE; 1061 if (in_snd_device == SND_DEVICE_NONE) { 1062 audio_devices_t out_device = AUDIO_DEVICE_NONE; 1063 if (adev->active_input && 1064 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 1065 adev->mode == AUDIO_MODE_IN_COMMUNICATION)) { 1066 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE); 1067 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) { 1068 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX; 1069 } else if (adev->primary_output) { 1070 out_device = adev->primary_output->devices; 1071 } 1072 } 1073 in_snd_device = platform_get_input_snd_device(adev->platform, out_device); 1074 } 1075 } 1076 } 1077 1078 if (out_snd_device == usecase->out_snd_device && 1079 in_snd_device == usecase->in_snd_device) { 1080 return 0; 1081 } 1082 1083 if (out_snd_device != SND_DEVICE_NONE && 1084 out_snd_device != adev->last_logged_snd_device[uc_id][0]) { 1085 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", 1086 __func__, 1087 use_case_table[uc_id], 1088 adev->last_logged_snd_device[uc_id][0], 1089 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]), 1090 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ? 1091 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) : 1092 -1, 1093 out_snd_device, 1094 platform_get_snd_device_name(out_snd_device), 1095 platform_get_snd_device_acdb_id(out_snd_device)); 1096 adev->last_logged_snd_device[uc_id][0] = out_snd_device; 1097 } 1098 if (in_snd_device != SND_DEVICE_NONE && 1099 in_snd_device != adev->last_logged_snd_device[uc_id][1]) { 1100 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", 1101 __func__, 1102 use_case_table[uc_id], 1103 adev->last_logged_snd_device[uc_id][1], 1104 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]), 1105 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ? 1106 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) : 1107 -1, 1108 in_snd_device, 1109 platform_get_snd_device_name(in_snd_device), 1110 platform_get_snd_device_acdb_id(in_snd_device)); 1111 adev->last_logged_snd_device[uc_id][1] = in_snd_device; 1112 } 1113 1114 /* 1115 * Limitation: While in call, to do a device switch we need to disable 1116 * and enable both RX and TX devices though one of them is same as current 1117 * device. 1118 */ 1119 if ((usecase->type == VOICE_CALL) && 1120 (usecase->in_snd_device != SND_DEVICE_NONE) && 1121 (usecase->out_snd_device != SND_DEVICE_NONE)) { 1122 status = platform_switch_voice_call_device_pre(adev->platform); 1123 /* Disable sidetone only if voice call already exists */ 1124 if (voice_is_call_state_active(adev)) 1125 voice_set_sidetone(adev, usecase->out_snd_device, false); 1126 } 1127 1128 /* Disable current sound devices */ 1129 if (usecase->out_snd_device != SND_DEVICE_NONE) { 1130 disable_audio_route(adev, usecase); 1131 disable_snd_device(adev, usecase->out_snd_device); 1132 } 1133 1134 if (usecase->in_snd_device != SND_DEVICE_NONE) { 1135 disable_audio_route(adev, usecase); 1136 disable_snd_device(adev, usecase->in_snd_device); 1137 } 1138 1139 /* Applicable only on the targets that has external modem. 1140 * New device information should be sent to modem before enabling 1141 * the devices to reduce in-call device switch time. 1142 */ 1143 if ((usecase->type == VOICE_CALL) && 1144 (usecase->in_snd_device != SND_DEVICE_NONE) && 1145 (usecase->out_snd_device != SND_DEVICE_NONE)) { 1146 status = platform_switch_voice_call_enable_device_config(adev->platform, 1147 out_snd_device, 1148 in_snd_device); 1149 } 1150 1151 /* Enable new sound devices */ 1152 if (out_snd_device != SND_DEVICE_NONE) { 1153 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) 1154 check_and_route_playback_usecases(adev, usecase, out_snd_device); 1155 enable_snd_device(adev, out_snd_device); 1156 } 1157 1158 if (in_snd_device != SND_DEVICE_NONE) { 1159 check_and_route_capture_usecases(adev, usecase, in_snd_device); 1160 enable_snd_device(adev, in_snd_device); 1161 } 1162 1163 if (usecase->type == VOICE_CALL) 1164 status = platform_switch_voice_call_device_post(adev->platform, 1165 out_snd_device, 1166 in_snd_device); 1167 1168 usecase->in_snd_device = in_snd_device; 1169 usecase->out_snd_device = out_snd_device; 1170 1171 enable_audio_route(adev, usecase); 1172 1173 /* Applicable only on the targets that has external modem. 1174 * Enable device command should be sent to modem only after 1175 * enabling voice call mixer controls 1176 */ 1177 if (usecase->type == VOICE_CALL) { 1178 status = platform_switch_voice_call_usecase_route_post(adev->platform, 1179 out_snd_device, 1180 in_snd_device); 1181 /* Enable sidetone only if voice call already exists */ 1182 if (voice_is_call_state_active(adev)) 1183 voice_set_sidetone(adev, out_snd_device, true); 1184 } 1185 1186 return status; 1187 } 1188 1189 static int stop_input_stream(struct stream_in *in) 1190 { 1191 int i, ret = 0; 1192 struct audio_usecase *uc_info; 1193 struct audio_device *adev = in->dev; 1194 1195 adev->active_input = NULL; 1196 1197 ALOGV("%s: enter: usecase(%d: %s)", __func__, 1198 in->usecase, use_case_table[in->usecase]); 1199 uc_info = get_usecase_from_list(adev, in->usecase); 1200 if (uc_info == NULL) { 1201 ALOGE("%s: Could not find the usecase (%d) in the list", 1202 __func__, in->usecase); 1203 return -EINVAL; 1204 } 1205 1206 /* 1. Disable stream specific mixer controls */ 1207 disable_audio_route(adev, uc_info); 1208 1209 /* 2. Disable the tx device */ 1210 disable_snd_device(adev, uc_info->in_snd_device); 1211 1212 list_remove(&uc_info->list); 1213 free(uc_info); 1214 1215 ALOGV("%s: exit: status(%d)", __func__, ret); 1216 return ret; 1217 } 1218 1219 int start_input_stream(struct stream_in *in) 1220 { 1221 /* 1. Enable output device and stream routing controls */ 1222 int ret = 0; 1223 struct audio_usecase *uc_info; 1224 struct audio_device *adev = in->dev; 1225 1226 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); 1227 1228 if (in->card_status == CARD_STATUS_OFFLINE || 1229 adev->card_status == CARD_STATUS_OFFLINE) { 1230 ALOGW("in->card_status or adev->card_status offline, try again"); 1231 ret = -EAGAIN; 1232 goto error_config; 1233 } 1234 1235 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); 1236 if (in->pcm_device_id < 0) { 1237 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 1238 __func__, in->usecase); 1239 ret = -EINVAL; 1240 goto error_config; 1241 } 1242 1243 adev->active_input = in; 1244 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 1245 uc_info->id = in->usecase; 1246 uc_info->type = PCM_CAPTURE; 1247 uc_info->stream.in = in; 1248 uc_info->devices = in->device; 1249 uc_info->in_snd_device = SND_DEVICE_NONE; 1250 uc_info->out_snd_device = SND_DEVICE_NONE; 1251 1252 list_add_tail(&adev->usecase_list, &uc_info->list); 1253 1254 audio_extn_perf_lock_acquire(); 1255 1256 select_devices(adev, in->usecase); 1257 1258 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 1259 __func__, adev->snd_card, in->pcm_device_id, in->config.channels); 1260 1261 unsigned int flags = PCM_IN | PCM_MONOTONIC; 1262 unsigned int pcm_open_retry_count = 0; 1263 1264 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) { 1265 flags |= PCM_MMAP | PCM_NOIRQ; 1266 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; 1267 } else if (in->realtime) { 1268 flags |= PCM_MMAP | PCM_NOIRQ; 1269 } 1270 1271 while (1) { 1272 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, 1273 flags, &in->config); 1274 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { 1275 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); 1276 if (in->pcm != NULL) { 1277 pcm_close(in->pcm); 1278 in->pcm = NULL; 1279 } 1280 if (pcm_open_retry_count-- == 0) { 1281 ret = -EIO; 1282 goto error_open; 1283 } 1284 usleep(PROXY_OPEN_WAIT_TIME * 1000); 1285 continue; 1286 } 1287 break; 1288 } 1289 1290 ALOGV("%s: pcm_prepare", __func__); 1291 ret = pcm_prepare(in->pcm); 1292 if (ret < 0) { 1293 ALOGE("%s: pcm_prepare returned %d", __func__, ret); 1294 pcm_close(in->pcm); 1295 in->pcm = NULL; 1296 goto error_open; 1297 } 1298 if (in->realtime) { 1299 ret = pcm_start(in->pcm); 1300 if (ret < 0) { 1301 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); 1302 pcm_close(in->pcm); 1303 in->pcm = NULL; 1304 goto error_open; 1305 } 1306 } 1307 register_in_stream(in); 1308 audio_extn_perf_lock_release(); 1309 ALOGV("%s: exit", __func__); 1310 1311 return ret; 1312 1313 error_open: 1314 stop_input_stream(in); 1315 audio_extn_perf_lock_release(); 1316 1317 error_config: 1318 adev->active_input = NULL; 1319 ALOGW("%s: exit: status(%d)", __func__, ret); 1320 1321 return ret; 1322 } 1323 1324 void lock_input_stream(struct stream_in *in) 1325 { 1326 pthread_mutex_lock(&in->pre_lock); 1327 pthread_mutex_lock(&in->lock); 1328 pthread_mutex_unlock(&in->pre_lock); 1329 } 1330 1331 void lock_output_stream(struct stream_out *out) 1332 { 1333 pthread_mutex_lock(&out->pre_lock); 1334 pthread_mutex_lock(&out->lock); 1335 pthread_mutex_unlock(&out->pre_lock); 1336 } 1337 1338 /* must be called with out->lock locked */ 1339 static int send_offload_cmd_l(struct stream_out* out, int command) 1340 { 1341 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); 1342 1343 ALOGVV("%s %d", __func__, command); 1344 1345 cmd->cmd = command; 1346 list_add_tail(&out->offload_cmd_list, &cmd->node); 1347 pthread_cond_signal(&out->offload_cond); 1348 return 0; 1349 } 1350 1351 /* must be called iwth out->lock locked */ 1352 static void stop_compressed_output_l(struct stream_out *out) 1353 { 1354 out->offload_state = OFFLOAD_STATE_IDLE; 1355 out->playback_started = 0; 1356 out->send_new_metadata = 1; 1357 if (out->compr != NULL) { 1358 compress_stop(out->compr); 1359 while (out->offload_thread_blocked) { 1360 pthread_cond_wait(&out->cond, &out->lock); 1361 } 1362 } 1363 } 1364 1365 static void *offload_thread_loop(void *context) 1366 { 1367 struct stream_out *out = (struct stream_out *) context; 1368 struct listnode *item; 1369 1370 out->offload_state = OFFLOAD_STATE_IDLE; 1371 out->playback_started = 0; 1372 1373 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); 1374 set_sched_policy(0, SP_FOREGROUND); 1375 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); 1376 1377 ALOGV("%s", __func__); 1378 lock_output_stream(out); 1379 for (;;) { 1380 struct offload_cmd *cmd = NULL; 1381 stream_callback_event_t event; 1382 bool send_callback = false; 1383 1384 ALOGVV("%s offload_cmd_list %d out->offload_state %d", 1385 __func__, list_empty(&out->offload_cmd_list), 1386 out->offload_state); 1387 if (list_empty(&out->offload_cmd_list)) { 1388 ALOGV("%s SLEEPING", __func__); 1389 pthread_cond_wait(&out->offload_cond, &out->lock); 1390 ALOGV("%s RUNNING", __func__); 1391 continue; 1392 } 1393 1394 item = list_head(&out->offload_cmd_list); 1395 cmd = node_to_item(item, struct offload_cmd, node); 1396 list_remove(item); 1397 1398 ALOGVV("%s STATE %d CMD %d out->compr %p", 1399 __func__, out->offload_state, cmd->cmd, out->compr); 1400 1401 if (cmd->cmd == OFFLOAD_CMD_EXIT) { 1402 free(cmd); 1403 break; 1404 } 1405 1406 if (out->compr == NULL) { 1407 ALOGE("%s: Compress handle is NULL", __func__); 1408 free(cmd); 1409 pthread_cond_signal(&out->cond); 1410 continue; 1411 } 1412 out->offload_thread_blocked = true; 1413 pthread_mutex_unlock(&out->lock); 1414 send_callback = false; 1415 switch(cmd->cmd) { 1416 case OFFLOAD_CMD_WAIT_FOR_BUFFER: 1417 compress_wait(out->compr, -1); 1418 send_callback = true; 1419 event = STREAM_CBK_EVENT_WRITE_READY; 1420 break; 1421 case OFFLOAD_CMD_PARTIAL_DRAIN: 1422 compress_next_track(out->compr); 1423 compress_partial_drain(out->compr); 1424 send_callback = true; 1425 event = STREAM_CBK_EVENT_DRAIN_READY; 1426 /* Resend the metadata for next iteration */ 1427 out->send_new_metadata = 1; 1428 break; 1429 case OFFLOAD_CMD_DRAIN: 1430 compress_drain(out->compr); 1431 send_callback = true; 1432 event = STREAM_CBK_EVENT_DRAIN_READY; 1433 break; 1434 case OFFLOAD_CMD_ERROR: 1435 send_callback = true; 1436 event = STREAM_CBK_EVENT_ERROR; 1437 break; 1438 default: 1439 ALOGE("%s unknown command received: %d", __func__, cmd->cmd); 1440 break; 1441 } 1442 lock_output_stream(out); 1443 out->offload_thread_blocked = false; 1444 pthread_cond_signal(&out->cond); 1445 if (send_callback) { 1446 ALOGVV("%s: sending offload_callback event %d", __func__, event); 1447 out->offload_callback(event, NULL, out->offload_cookie); 1448 } 1449 free(cmd); 1450 } 1451 1452 pthread_cond_signal(&out->cond); 1453 while (!list_empty(&out->offload_cmd_list)) { 1454 item = list_head(&out->offload_cmd_list); 1455 list_remove(item); 1456 free(node_to_item(item, struct offload_cmd, node)); 1457 } 1458 pthread_mutex_unlock(&out->lock); 1459 1460 return NULL; 1461 } 1462 1463 static int create_offload_callback_thread(struct stream_out *out) 1464 { 1465 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); 1466 list_init(&out->offload_cmd_list); 1467 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, 1468 offload_thread_loop, out); 1469 return 0; 1470 } 1471 1472 static int destroy_offload_callback_thread(struct stream_out *out) 1473 { 1474 lock_output_stream(out); 1475 stop_compressed_output_l(out); 1476 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); 1477 1478 pthread_mutex_unlock(&out->lock); 1479 pthread_join(out->offload_thread, (void **) NULL); 1480 pthread_cond_destroy(&out->offload_cond); 1481 1482 return 0; 1483 } 1484 1485 static bool allow_hdmi_channel_config(struct audio_device *adev) 1486 { 1487 struct listnode *node; 1488 struct audio_usecase *usecase; 1489 bool ret = true; 1490 1491 list_for_each(node, &adev->usecase_list) { 1492 usecase = node_to_item(node, struct audio_usecase, list); 1493 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1494 /* 1495 * If voice call is already existing, do not proceed further to avoid 1496 * disabling/enabling both RX and TX devices, CSD calls, etc. 1497 * Once the voice call done, the HDMI channels can be configured to 1498 * max channels of remaining use cases. 1499 */ 1500 if (usecase->id == USECASE_VOICE_CALL) { 1501 ALOGV("%s: voice call is active, no change in HDMI channels", 1502 __func__); 1503 ret = false; 1504 break; 1505 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) { 1506 ALOGV("%s: multi channel playback is active, " 1507 "no change in HDMI channels", __func__); 1508 ret = false; 1509 break; 1510 } 1511 } 1512 } 1513 return ret; 1514 } 1515 1516 static int check_and_set_hdmi_channels(struct audio_device *adev, 1517 unsigned int channels) 1518 { 1519 struct listnode *node; 1520 struct audio_usecase *usecase; 1521 1522 /* Check if change in HDMI channel config is allowed */ 1523 if (!allow_hdmi_channel_config(adev)) 1524 return 0; 1525 1526 if (channels == adev->cur_hdmi_channels) { 1527 ALOGV("%s: Requested channels are same as current", __func__); 1528 return 0; 1529 } 1530 1531 platform_set_hdmi_channels(adev->platform, channels); 1532 adev->cur_hdmi_channels = channels; 1533 1534 /* 1535 * Deroute all the playback streams routed to HDMI so that 1536 * the back end is deactivated. Note that backend will not 1537 * be deactivated if any one stream is connected to it. 1538 */ 1539 list_for_each(node, &adev->usecase_list) { 1540 usecase = node_to_item(node, struct audio_usecase, list); 1541 if (usecase->type == PCM_PLAYBACK && 1542 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1543 disable_audio_route(adev, usecase); 1544 } 1545 } 1546 1547 /* 1548 * Enable all the streams disabled above. Now the HDMI backend 1549 * will be activated with new channel configuration 1550 */ 1551 list_for_each(node, &adev->usecase_list) { 1552 usecase = node_to_item(node, struct audio_usecase, list); 1553 if (usecase->type == PCM_PLAYBACK && 1554 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1555 enable_audio_route(adev, usecase); 1556 } 1557 } 1558 1559 return 0; 1560 } 1561 1562 static int stop_output_stream(struct stream_out *out) 1563 { 1564 int i, ret = 0; 1565 struct audio_usecase *uc_info; 1566 struct audio_device *adev = out->dev; 1567 1568 ALOGV("%s: enter: usecase(%d: %s)", __func__, 1569 out->usecase, use_case_table[out->usecase]); 1570 uc_info = get_usecase_from_list(adev, out->usecase); 1571 if (uc_info == NULL) { 1572 ALOGE("%s: Could not find the usecase (%d) in the list", 1573 __func__, out->usecase); 1574 return -EINVAL; 1575 } 1576 1577 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1578 if (adev->visualizer_stop_output != NULL) 1579 adev->visualizer_stop_output(out->handle, out->pcm_device_id); 1580 if (adev->offload_effects_stop_output != NULL) 1581 adev->offload_effects_stop_output(out->handle, out->pcm_device_id); 1582 } 1583 1584 /* 1. Get and set stream specific mixer controls */ 1585 disable_audio_route(adev, uc_info); 1586 1587 /* 2. Disable the rx device */ 1588 disable_snd_device(adev, uc_info->out_snd_device); 1589 1590 list_remove(&uc_info->list); 1591 free(uc_info); 1592 1593 audio_extn_extspk_update(adev->extspk); 1594 1595 /* Must be called after removing the usecase from list */ 1596 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) 1597 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS); 1598 1599 ALOGV("%s: exit: status(%d)", __func__, ret); 1600 return ret; 1601 } 1602 1603 int start_output_stream(struct stream_out *out) 1604 { 1605 int ret = 0; 1606 struct audio_usecase *uc_info; 1607 struct audio_device *adev = out->dev; 1608 1609 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)", 1610 __func__, out->usecase, use_case_table[out->usecase], out->devices); 1611 1612 if (out->card_status == CARD_STATUS_OFFLINE || 1613 adev->card_status == CARD_STATUS_OFFLINE) { 1614 ALOGW("out->card_status or adev->card_status offline, try again"); 1615 ret = -EAGAIN; 1616 goto error_config; 1617 } 1618 1619 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); 1620 if (out->pcm_device_id < 0) { 1621 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 1622 __func__, out->pcm_device_id, out->usecase); 1623 ret = -EINVAL; 1624 goto error_config; 1625 } 1626 1627 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 1628 uc_info->id = out->usecase; 1629 uc_info->type = PCM_PLAYBACK; 1630 uc_info->stream.out = out; 1631 uc_info->devices = out->devices; 1632 uc_info->in_snd_device = SND_DEVICE_NONE; 1633 uc_info->out_snd_device = SND_DEVICE_NONE; 1634 1635 /* This must be called before adding this usecase to the list */ 1636 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) 1637 check_and_set_hdmi_channels(adev, out->config.channels); 1638 1639 list_add_tail(&adev->usecase_list, &uc_info->list); 1640 1641 audio_extn_perf_lock_acquire(); 1642 1643 select_devices(adev, out->usecase); 1644 1645 audio_extn_extspk_update(adev->extspk); 1646 1647 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)", 1648 __func__, adev->snd_card, out->pcm_device_id, out->config.format); 1649 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1650 unsigned int flags = PCM_OUT; 1651 unsigned int pcm_open_retry_count = 0; 1652 1653 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) { 1654 flags |= PCM_MMAP | PCM_NOIRQ; 1655 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; 1656 } else if (out->realtime) { 1657 flags |= PCM_MMAP | PCM_NOIRQ; 1658 } else 1659 flags |= PCM_MONOTONIC; 1660 1661 while (1) { 1662 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id, 1663 flags, &out->config); 1664 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { 1665 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); 1666 if (out->pcm != NULL) { 1667 pcm_close(out->pcm); 1668 out->pcm = NULL; 1669 } 1670 if (pcm_open_retry_count-- == 0) { 1671 ret = -EIO; 1672 goto error_open; 1673 } 1674 usleep(PROXY_OPEN_WAIT_TIME * 1000); 1675 continue; 1676 } 1677 break; 1678 } 1679 ALOGV("%s: pcm_prepare", __func__); 1680 if (pcm_is_ready(out->pcm)) { 1681 ret = pcm_prepare(out->pcm); 1682 if (ret < 0) { 1683 ALOGE("%s: pcm_prepare returned %d", __func__, ret); 1684 pcm_close(out->pcm); 1685 out->pcm = NULL; 1686 goto error_open; 1687 } 1688 } 1689 } else { 1690 out->pcm = NULL; 1691 out->compr = compress_open(adev->snd_card, out->pcm_device_id, 1692 COMPRESS_IN, &out->compr_config); 1693 if (out->compr && !is_compress_ready(out->compr)) { 1694 ALOGE("%s: %s", __func__, compress_get_error(out->compr)); 1695 compress_close(out->compr); 1696 out->compr = NULL; 1697 ret = -EIO; 1698 goto error_open; 1699 } 1700 if (out->offload_callback) 1701 compress_nonblock(out->compr, out->non_blocking); 1702 1703 if (adev->visualizer_start_output != NULL) 1704 adev->visualizer_start_output(out->handle, out->pcm_device_id); 1705 if (adev->offload_effects_start_output != NULL) 1706 adev->offload_effects_start_output(out->handle, out->pcm_device_id); 1707 } 1708 ret = 0; 1709 if (out->realtime) { 1710 ret = pcm_start(out->pcm); 1711 if (ret < 0) { 1712 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); 1713 pcm_close(out->pcm); 1714 out->pcm = NULL; 1715 goto error_open; 1716 } 1717 } 1718 register_out_stream(out); 1719 audio_extn_perf_lock_release(); 1720 ALOGV("%s: exit", __func__); 1721 return ret; 1722 error_open: 1723 audio_extn_perf_lock_release(); 1724 stop_output_stream(out); 1725 error_config: 1726 return ret; 1727 } 1728 1729 static int check_input_parameters(uint32_t sample_rate, 1730 audio_format_t format, 1731 int channel_count) 1732 { 1733 if ((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT)) { 1734 ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format); 1735 return -EINVAL; 1736 } 1737 1738 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > MAX_CHANNEL_COUNT)) { 1739 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__, 1740 channel_count, MIN_CHANNEL_COUNT, MAX_CHANNEL_COUNT); 1741 return -EINVAL; 1742 } 1743 1744 switch (sample_rate) { 1745 case 8000: 1746 case 11025: 1747 case 12000: 1748 case 16000: 1749 case 22050: 1750 case 24000: 1751 case 32000: 1752 case 44100: 1753 case 48000: 1754 break; 1755 default: 1756 ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate); 1757 return -EINVAL; 1758 } 1759 1760 return 0; 1761 } 1762 1763 static size_t get_input_buffer_size(uint32_t sample_rate, 1764 audio_format_t format, 1765 int channel_count, 1766 bool is_low_latency) 1767 { 1768 size_t size = 0; 1769 1770 if (check_input_parameters(sample_rate, format, channel_count) != 0) 1771 return 0; 1772 1773 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000; 1774 if (is_low_latency) 1775 size = configured_low_latency_capture_period_size; 1776 1777 size *= channel_count * audio_bytes_per_sample(format); 1778 1779 /* make sure the size is multiple of 32 bytes 1780 * At 48 kHz mono 16-bit PCM: 1781 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15) 1782 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10) 1783 */ 1784 size += 0x1f; 1785 size &= ~0x1f; 1786 1787 return size; 1788 } 1789 1790 static uint32_t out_get_sample_rate(const struct audio_stream *stream) 1791 { 1792 struct stream_out *out = (struct stream_out *)stream; 1793 1794 return out->sample_rate; 1795 } 1796 1797 static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) 1798 { 1799 return -ENOSYS; 1800 } 1801 1802 static size_t out_get_buffer_size(const struct audio_stream *stream) 1803 { 1804 struct stream_out *out = (struct stream_out *)stream; 1805 1806 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1807 return out->compr_config.fragment_size; 1808 } 1809 return out->config.period_size * out->af_period_multiplier * 1810 audio_stream_out_frame_size((const struct audio_stream_out *)stream); 1811 } 1812 1813 static uint32_t out_get_channels(const struct audio_stream *stream) 1814 { 1815 struct stream_out *out = (struct stream_out *)stream; 1816 1817 return out->channel_mask; 1818 } 1819 1820 static audio_format_t out_get_format(const struct audio_stream *stream) 1821 { 1822 struct stream_out *out = (struct stream_out *)stream; 1823 1824 return out->format; 1825 } 1826 1827 static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) 1828 { 1829 return -ENOSYS; 1830 } 1831 1832 static int out_standby(struct audio_stream *stream) 1833 { 1834 struct stream_out *out = (struct stream_out *)stream; 1835 struct audio_device *adev = out->dev; 1836 1837 ALOGV("%s: enter: usecase(%d: %s)", __func__, 1838 out->usecase, use_case_table[out->usecase]); 1839 1840 lock_output_stream(out); 1841 if (!out->standby) { 1842 if (adev->adm_deregister_stream) 1843 adev->adm_deregister_stream(adev->adm_data, out->handle); 1844 1845 pthread_mutex_lock(&adev->lock); 1846 out->standby = true; 1847 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1848 if (out->pcm) { 1849 pcm_close(out->pcm); 1850 out->pcm = NULL; 1851 } 1852 } else { 1853 stop_compressed_output_l(out); 1854 out->gapless_mdata.encoder_delay = 0; 1855 out->gapless_mdata.encoder_padding = 0; 1856 if (out->compr != NULL) { 1857 compress_close(out->compr); 1858 out->compr = NULL; 1859 } 1860 } 1861 stop_output_stream(out); 1862 pthread_mutex_unlock(&adev->lock); 1863 } 1864 pthread_mutex_unlock(&out->lock); 1865 ALOGV("%s: exit", __func__); 1866 return 0; 1867 } 1868 1869 static int out_on_error(struct audio_stream *stream) 1870 { 1871 struct stream_out *out = (struct stream_out *)stream; 1872 struct audio_device *adev = out->dev; 1873 bool do_standby = false; 1874 1875 lock_output_stream(out); 1876 if (!out->standby) { 1877 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1878 stop_compressed_output_l(out); 1879 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR); 1880 } else 1881 do_standby = true; 1882 } 1883 pthread_mutex_unlock(&out->lock); 1884 1885 if (do_standby) 1886 return out_standby(&out->stream.common); 1887 1888 return 0; 1889 } 1890 1891 static int out_dump(const struct audio_stream *stream, int fd) 1892 { 1893 struct stream_out *out = (struct stream_out *)stream; 1894 1895 // We try to get the lock for consistency, 1896 // but it isn't necessary for these variables. 1897 // If we're not in standby, we may be blocked on a write. 1898 const bool locked = (pthread_mutex_trylock(&out->lock) == 0); 1899 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no"); 1900 dprintf(fd, " Frames written: %lld\n", (long long)out->written); 1901 1902 if (locked) { 1903 log_dump_l(&out->error_log, fd); 1904 pthread_mutex_unlock(&out->lock); 1905 } else { 1906 // We don't have the lock here, copy for safety. 1907 struct error_log log = out->error_log; 1908 log_dump_l(&log, fd); 1909 } 1910 return 0; 1911 } 1912 1913 static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) 1914 { 1915 int ret = 0; 1916 char value[32]; 1917 struct compr_gapless_mdata tmp_mdata; 1918 1919 if (!out || !parms) { 1920 return -EINVAL; 1921 } 1922 1923 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); 1924 if (ret >= 0) { 1925 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check? 1926 } else { 1927 return -EINVAL; 1928 } 1929 1930 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); 1931 if (ret >= 0) { 1932 tmp_mdata.encoder_padding = atoi(value); 1933 } else { 1934 return -EINVAL; 1935 } 1936 1937 out->gapless_mdata = tmp_mdata; 1938 out->send_new_metadata = 1; 1939 ALOGV("%s new encoder delay %u and padding %u", __func__, 1940 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); 1941 1942 return 0; 1943 } 1944 1945 static bool output_drives_call(struct audio_device *adev, struct stream_out *out) 1946 { 1947 return out == adev->primary_output || out == adev->voice_tx_output; 1948 } 1949 1950 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 1951 { 1952 struct stream_out *out = (struct stream_out *)stream; 1953 struct audio_device *adev = out->dev; 1954 struct audio_usecase *usecase; 1955 struct listnode *node; 1956 struct str_parms *parms; 1957 char value[32]; 1958 int ret, val = 0; 1959 bool select_new_device = false; 1960 int status = 0; 1961 1962 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", 1963 __func__, out->usecase, use_case_table[out->usecase], kvpairs); 1964 parms = str_parms_create_str(kvpairs); 1965 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1966 if (ret >= 0) { 1967 val = atoi(value); 1968 lock_output_stream(out); 1969 pthread_mutex_lock(&adev->lock); 1970 1971 /* 1972 * When HDMI cable is unplugged the music playback is paused and 1973 * the policy manager sends routing=0. But the audioflinger 1974 * continues to write data until standby time (3sec). 1975 * As the HDMI core is turned off, the write gets blocked. 1976 * Avoid this by routing audio to speaker until standby. 1977 */ 1978 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && 1979 val == AUDIO_DEVICE_NONE) { 1980 val = AUDIO_DEVICE_OUT_SPEAKER; 1981 } 1982 1983 /* 1984 * select_devices() call below switches all the usecases on the same 1985 * backend to the new device. Refer to check_and_route_playback_usecases() in 1986 * the select_devices(). But how do we undo this? 1987 * 1988 * For example, music playback is active on headset (deep-buffer usecase) 1989 * and if we go to ringtones and select a ringtone, low-latency usecase 1990 * will be started on headset+speaker. As we can't enable headset+speaker 1991 * and headset devices at the same time, select_devices() switches the music 1992 * playback to headset+speaker while starting low-lateny usecase for ringtone. 1993 * So when the ringtone playback is completed, how do we undo the same? 1994 * 1995 * We are relying on the out_set_parameters() call on deep-buffer output, 1996 * once the ringtone playback is ended. 1997 * NOTE: We should not check if the current devices are same as new devices. 1998 * Because select_devices() must be called to switch back the music 1999 * playback to headset. 2000 */ 2001 audio_devices_t new_dev = val; 2002 if (new_dev != AUDIO_DEVICE_NONE) { 2003 bool same_dev = out->devices == new_dev; 2004 out->devices = new_dev; 2005 2006 if (output_drives_call(adev, out)) { 2007 if (!voice_is_in_call(adev)) { 2008 if (adev->mode == AUDIO_MODE_IN_CALL) { 2009 adev->current_call_output = out; 2010 ret = voice_start_call(adev); 2011 } 2012 } else { 2013 adev->current_call_output = out; 2014 voice_update_devices_for_all_voice_usecases(adev); 2015 } 2016 } 2017 2018 if (!out->standby) { 2019 if (!same_dev) { 2020 ALOGV("update routing change"); 2021 out->routing_change = true; 2022 } 2023 select_devices(adev, out->usecase); 2024 } 2025 2026 } 2027 2028 pthread_mutex_unlock(&adev->lock); 2029 pthread_mutex_unlock(&out->lock); 2030 2031 /*handles device and call state changes*/ 2032 audio_extn_extspk_update(adev->extspk); 2033 } 2034 2035 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2036 parse_compress_metadata(out, parms); 2037 } 2038 2039 str_parms_destroy(parms); 2040 ALOGV("%s: exit: code(%d)", __func__, status); 2041 return status; 2042 } 2043 2044 static char* out_get_parameters(const struct audio_stream *stream, const char *keys) 2045 { 2046 struct stream_out *out = (struct stream_out *)stream; 2047 struct str_parms *query = str_parms_create_str(keys); 2048 char *str; 2049 char value[256]; 2050 struct str_parms *reply = str_parms_create(); 2051 size_t i, j; 2052 int ret; 2053 bool first = true; 2054 ALOGV("%s: enter: keys - %s", __func__, keys); 2055 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); 2056 if (ret >= 0) { 2057 value[0] = '\0'; 2058 i = 0; 2059 while (out->supported_channel_masks[i] != 0) { 2060 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { 2061 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { 2062 if (!first) { 2063 strcat(value, "|"); 2064 } 2065 strcat(value, out_channels_name_to_enum_table[j].name); 2066 first = false; 2067 break; 2068 } 2069 } 2070 i++; 2071 } 2072 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 2073 str = str_parms_to_str(reply); 2074 } else { 2075 str = strdup(keys); 2076 } 2077 str_parms_destroy(query); 2078 str_parms_destroy(reply); 2079 ALOGV("%s: exit: returns - %s", __func__, str); 2080 return str; 2081 } 2082 2083 static uint32_t out_get_latency(const struct audio_stream_out *stream) 2084 { 2085 uint32_t hw_delay, period_ms; 2086 struct stream_out *out = (struct stream_out *)stream; 2087 2088 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) 2089 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; 2090 else if (out->realtime) { 2091 // since the buffer won't be filled up faster than realtime, 2092 // return a smaller number 2093 period_ms = (out->af_period_multiplier * out->config.period_size * 2094 1000) / (out->config.rate); 2095 hw_delay = platform_render_latency(out->usecase)/1000; 2096 return period_ms + hw_delay; 2097 } 2098 2099 return (out->config.period_count * out->config.period_size * 1000) / 2100 (out->config.rate); 2101 } 2102 2103 static int out_set_volume(struct audio_stream_out *stream, float left, 2104 float right) 2105 { 2106 struct stream_out *out = (struct stream_out *)stream; 2107 int volume[2]; 2108 2109 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { 2110 /* only take left channel into account: the API is for stereo anyway */ 2111 out->muted = (left == 0.0f); 2112 return 0; 2113 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2114 const char *mixer_ctl_name = "Compress Playback Volume"; 2115 struct audio_device *adev = out->dev; 2116 struct mixer_ctl *ctl; 2117 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 2118 if (!ctl) { 2119 /* try with the control based on device id */ 2120 int pcm_device_id = platform_get_pcm_device_id(out->usecase, 2121 PCM_PLAYBACK); 2122 char ctl_name[128] = {0}; 2123 snprintf(ctl_name, sizeof(ctl_name), 2124 "Compress Playback %d Volume", pcm_device_id); 2125 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name); 2126 if (!ctl) { 2127 ALOGE("%s: Could not get volume ctl mixer cmd", __func__); 2128 return -EINVAL; 2129 } 2130 } 2131 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); 2132 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); 2133 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0])); 2134 return 0; 2135 } 2136 2137 return -ENOSYS; 2138 } 2139 2140 // note: this call is safe only if the stream_cb is 2141 // removed first in close_output_stream (as is done now). 2142 static void out_snd_mon_cb(void * stream, struct str_parms * parms) 2143 { 2144 if (!stream || !parms) 2145 return; 2146 2147 struct stream_out *out = (struct stream_out *)stream; 2148 struct audio_device *adev = out->dev; 2149 2150 card_status_t status; 2151 int card; 2152 if (parse_snd_card_status(parms, &card, &status) < 0) 2153 return; 2154 2155 pthread_mutex_lock(&adev->lock); 2156 bool valid_cb = (card == adev->snd_card); 2157 pthread_mutex_unlock(&adev->lock); 2158 2159 if (!valid_cb) 2160 return; 2161 2162 lock_output_stream(out); 2163 if (out->card_status != status) 2164 out->card_status = status; 2165 pthread_mutex_unlock(&out->lock); 2166 2167 ALOGW("out_snd_mon_cb for card %d usecase %s, status %s", card, 2168 use_case_table[out->usecase], 2169 status == CARD_STATUS_OFFLINE ? "offline" : "online"); 2170 2171 if (status == CARD_STATUS_OFFLINE) 2172 out_on_error(stream); 2173 2174 return; 2175 } 2176 2177 #ifdef NO_AUDIO_OUT 2178 static ssize_t out_write_for_no_output(struct audio_stream_out *stream, 2179 const void *buffer, size_t bytes) 2180 { 2181 struct stream_out *out = (struct stream_out *)stream; 2182 2183 /* No Output device supported other than BT for playback. 2184 * Sleep for the amount of buffer duration 2185 */ 2186 lock_output_stream(out); 2187 usleep(bytes * 1000000 / audio_stream_out_frame_size(&out->stream.common) / 2188 out_get_sample_rate(&out->stream.common)); 2189 pthread_mutex_unlock(&out->lock); 2190 return bytes; 2191 } 2192 #endif 2193 2194 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, 2195 size_t bytes) 2196 { 2197 struct stream_out *out = (struct stream_out *)stream; 2198 struct audio_device *adev = out->dev; 2199 ssize_t ret = 0; 2200 int error_code = ERROR_CODE_STANDBY; 2201 2202 lock_output_stream(out); 2203 if (out->standby) { 2204 out->standby = false; 2205 pthread_mutex_lock(&adev->lock); 2206 ret = start_output_stream(out); 2207 pthread_mutex_unlock(&adev->lock); 2208 /* ToDo: If use case is compress offload should return 0 */ 2209 if (ret != 0) { 2210 out->standby = true; 2211 goto exit; 2212 } 2213 2214 if (last_known_cal_step != -1) { 2215 ALOGD("%s: retry previous failed cal level set", __func__); 2216 audio_hw_send_gain_dep_calibration(last_known_cal_step); 2217 } 2218 } 2219 2220 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2221 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes); 2222 if (out->send_new_metadata) { 2223 ALOGVV("send new gapless metadata"); 2224 compress_set_gapless_metadata(out->compr, &out->gapless_mdata); 2225 out->send_new_metadata = 0; 2226 } 2227 unsigned int avail; 2228 struct timespec tstamp; 2229 ret = compress_get_hpointer(out->compr, &avail, &tstamp); 2230 /* Do not limit write size if the available frames count is unknown */ 2231 if (ret != 0) { 2232 avail = bytes; 2233 } 2234 if (avail == 0) { 2235 ret = 0; 2236 } else { 2237 if (avail > bytes) { 2238 avail = bytes; 2239 } 2240 ret = compress_write(out->compr, buffer, avail); 2241 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %zd", 2242 __func__, avail, ret); 2243 } 2244 2245 if (ret >= 0 && ret < (ssize_t)bytes) { 2246 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); 2247 } 2248 if (ret > 0 && !out->playback_started) { 2249 compress_start(out->compr); 2250 out->playback_started = 1; 2251 out->offload_state = OFFLOAD_STATE_PLAYING; 2252 } 2253 if (ret < 0) { 2254 log_error_l(&out->error_log, ERROR_CODE_WRITE); 2255 } 2256 pthread_mutex_unlock(&out->lock); 2257 return ret; 2258 } else { 2259 error_code = ERROR_CODE_WRITE; 2260 if (out->pcm) { 2261 if (out->muted) 2262 memset((void *)buffer, 0, bytes); 2263 2264 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); 2265 2266 long ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/ 2267 out->config.rate; 2268 request_out_focus(out, ns); 2269 2270 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime; 2271 if (use_mmap) 2272 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes); 2273 else 2274 ret = pcm_write(out->pcm, (void *)buffer, bytes); 2275 2276 release_out_focus(out, ns); 2277 } else { 2278 LOG_ALWAYS_FATAL("out->pcm is NULL after starting output stream"); 2279 } 2280 } 2281 2282 exit: 2283 // For PCM we always consume the buffer and return #bytes regardless of ret. 2284 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2285 out->written += bytes / (out->config.channels * sizeof(short)); 2286 } 2287 long long sleeptime_us = 0; 2288 if (ret != 0) { 2289 log_error_l(&out->error_log, error_code); 2290 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2291 ALOGE_IF(out->pcm != NULL, 2292 "%s: error %zd - %s", __func__, ret, pcm_get_error(out->pcm)); 2293 sleeptime_us = bytes * 1000000LL / audio_stream_out_frame_size(stream) / 2294 out_get_sample_rate(&out->stream.common); 2295 // usleep not guaranteed for values over 1 second but we don't limit here. 2296 } 2297 } 2298 2299 pthread_mutex_unlock(&out->lock); 2300 2301 if (ret != 0) { 2302 out_on_error(&out->stream.common); 2303 if (sleeptime_us != 0) 2304 usleep(sleeptime_us); 2305 } 2306 return bytes; 2307 } 2308 2309 static int out_get_render_position(const struct audio_stream_out *stream, 2310 uint32_t *dsp_frames) 2311 { 2312 struct stream_out *out = (struct stream_out *)stream; 2313 *dsp_frames = 0; 2314 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { 2315 lock_output_stream(out); 2316 if (out->compr != NULL) { 2317 unsigned long frames = 0; 2318 // TODO: check return value 2319 compress_get_tstamp(out->compr, &frames, &out->sample_rate); 2320 *dsp_frames = (uint32_t)frames; 2321 ALOGVV("%s rendered frames %d sample_rate %d", 2322 __func__, *dsp_frames, out->sample_rate); 2323 } 2324 pthread_mutex_unlock(&out->lock); 2325 return 0; 2326 } else 2327 return -EINVAL; 2328 } 2329 2330 static int out_add_audio_effect(const struct audio_stream *stream __unused, 2331 effect_handle_t effect __unused) 2332 { 2333 return 0; 2334 } 2335 2336 static int out_remove_audio_effect(const struct audio_stream *stream __unused, 2337 effect_handle_t effect __unused) 2338 { 2339 return 0; 2340 } 2341 2342 static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused, 2343 int64_t *timestamp __unused) 2344 { 2345 return -EINVAL; 2346 } 2347 2348 static int out_get_presentation_position(const struct audio_stream_out *stream, 2349 uint64_t *frames, struct timespec *timestamp) 2350 { 2351 struct stream_out *out = (struct stream_out *)stream; 2352 int ret = -EINVAL; 2353 unsigned long dsp_frames; 2354 2355 lock_output_stream(out); 2356 2357 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2358 if (out->compr != NULL) { 2359 // TODO: check return value 2360 compress_get_tstamp(out->compr, &dsp_frames, 2361 &out->sample_rate); 2362 ALOGVV("%s rendered frames %ld sample_rate %d", 2363 __func__, dsp_frames, out->sample_rate); 2364 *frames = dsp_frames; 2365 ret = 0; 2366 /* this is the best we can do */ 2367 clock_gettime(CLOCK_MONOTONIC, timestamp); 2368 } 2369 } else { 2370 if (out->pcm) { 2371 unsigned int avail; 2372 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { 2373 size_t kernel_buffer_size = out->config.period_size * out->config.period_count; 2374 int64_t signed_frames = out->written - kernel_buffer_size + avail; 2375 // This adjustment accounts for buffering after app processor. 2376 // It is based on estimated DSP latency per use case, rather than exact. 2377 signed_frames -= 2378 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL); 2379 2380 // It would be unusual for this value to be negative, but check just in case ... 2381 if (signed_frames >= 0) { 2382 *frames = signed_frames; 2383 ret = 0; 2384 } 2385 } 2386 } 2387 } 2388 2389 pthread_mutex_unlock(&out->lock); 2390 2391 return ret; 2392 } 2393 2394 static int out_set_callback(struct audio_stream_out *stream, 2395 stream_callback_t callback, void *cookie) 2396 { 2397 struct stream_out *out = (struct stream_out *)stream; 2398 2399 ALOGV("%s", __func__); 2400 lock_output_stream(out); 2401 out->offload_callback = callback; 2402 out->offload_cookie = cookie; 2403 pthread_mutex_unlock(&out->lock); 2404 return 0; 2405 } 2406 2407 static int out_pause(struct audio_stream_out* stream) 2408 { 2409 struct stream_out *out = (struct stream_out *)stream; 2410 int status = -ENOSYS; 2411 ALOGV("%s", __func__); 2412 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2413 lock_output_stream(out); 2414 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { 2415 status = compress_pause(out->compr); 2416 out->offload_state = OFFLOAD_STATE_PAUSED; 2417 } 2418 pthread_mutex_unlock(&out->lock); 2419 } 2420 return status; 2421 } 2422 2423 static int out_resume(struct audio_stream_out* stream) 2424 { 2425 struct stream_out *out = (struct stream_out *)stream; 2426 int status = -ENOSYS; 2427 ALOGV("%s", __func__); 2428 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2429 status = 0; 2430 lock_output_stream(out); 2431 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { 2432 status = compress_resume(out->compr); 2433 out->offload_state = OFFLOAD_STATE_PLAYING; 2434 } 2435 pthread_mutex_unlock(&out->lock); 2436 } 2437 return status; 2438 } 2439 2440 static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) 2441 { 2442 struct stream_out *out = (struct stream_out *)stream; 2443 int status = -ENOSYS; 2444 ALOGV("%s", __func__); 2445 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2446 lock_output_stream(out); 2447 if (type == AUDIO_DRAIN_EARLY_NOTIFY) 2448 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); 2449 else 2450 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); 2451 pthread_mutex_unlock(&out->lock); 2452 } 2453 return status; 2454 } 2455 2456 static int out_flush(struct audio_stream_out* stream) 2457 { 2458 struct stream_out *out = (struct stream_out *)stream; 2459 ALOGV("%s", __func__); 2460 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2461 lock_output_stream(out); 2462 stop_compressed_output_l(out); 2463 pthread_mutex_unlock(&out->lock); 2464 return 0; 2465 } 2466 return -ENOSYS; 2467 } 2468 2469 /** audio_stream_in implementation **/ 2470 static uint32_t in_get_sample_rate(const struct audio_stream *stream) 2471 { 2472 struct stream_in *in = (struct stream_in *)stream; 2473 2474 return in->config.rate; 2475 } 2476 2477 static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) 2478 { 2479 return -ENOSYS; 2480 } 2481 2482 static size_t in_get_buffer_size(const struct audio_stream *stream) 2483 { 2484 struct stream_in *in = (struct stream_in *)stream; 2485 2486 return in->config.period_size * in->af_period_multiplier * 2487 audio_stream_in_frame_size((const struct audio_stream_in *)stream); 2488 } 2489 2490 static uint32_t in_get_channels(const struct audio_stream *stream) 2491 { 2492 struct stream_in *in = (struct stream_in *)stream; 2493 2494 return in->channel_mask; 2495 } 2496 2497 static audio_format_t in_get_format(const struct audio_stream *stream) 2498 { 2499 struct stream_in *in = (struct stream_in *)stream; 2500 return in->format; 2501 } 2502 2503 static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) 2504 { 2505 return -ENOSYS; 2506 } 2507 2508 static int in_standby(struct audio_stream *stream) 2509 { 2510 struct stream_in *in = (struct stream_in *)stream; 2511 struct audio_device *adev = in->dev; 2512 int status = 0; 2513 ALOGV("%s: enter", __func__); 2514 2515 lock_input_stream(in); 2516 2517 if (!in->standby && in->is_st_session) { 2518 ALOGV("%s: sound trigger pcm stop lab", __func__); 2519 audio_extn_sound_trigger_stop_lab(in); 2520 in->standby = true; 2521 } 2522 2523 if (!in->standby) { 2524 if (adev->adm_deregister_stream) 2525 adev->adm_deregister_stream(adev->adm_data, in->capture_handle); 2526 2527 pthread_mutex_lock(&adev->lock); 2528 in->standby = true; 2529 if (in->pcm) { 2530 pcm_close(in->pcm); 2531 in->pcm = NULL; 2532 } 2533 adev->enable_voicerx = false; 2534 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE ); 2535 status = stop_input_stream(in); 2536 pthread_mutex_unlock(&adev->lock); 2537 } 2538 pthread_mutex_unlock(&in->lock); 2539 ALOGV("%s: exit: status(%d)", __func__, status); 2540 return status; 2541 } 2542 2543 static int in_dump(const struct audio_stream *stream __unused, int fd __unused) 2544 { 2545 return 0; 2546 } 2547 2548 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 2549 { 2550 struct stream_in *in = (struct stream_in *)stream; 2551 struct audio_device *adev = in->dev; 2552 struct str_parms *parms; 2553 char *str; 2554 char value[32]; 2555 int ret, val = 0; 2556 int status = 0; 2557 2558 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); 2559 parms = str_parms_create_str(kvpairs); 2560 2561 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 2562 2563 lock_input_stream(in); 2564 2565 pthread_mutex_lock(&adev->lock); 2566 if (ret >= 0) { 2567 val = atoi(value); 2568 /* no audio source uses val == 0 */ 2569 if ((in->source != val) && (val != 0)) { 2570 in->source = val; 2571 } 2572 } 2573 2574 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 2575 2576 if (ret >= 0) { 2577 val = atoi(value); 2578 if (((int)in->device != val) && (val != 0)) { 2579 in->device = val; 2580 /* If recording is in progress, change the tx device to new device */ 2581 if (!in->standby) { 2582 ALOGV("update input routing change"); 2583 in->routing_change = true; 2584 select_devices(adev, in->usecase); 2585 } 2586 } 2587 } 2588 2589 pthread_mutex_unlock(&adev->lock); 2590 pthread_mutex_unlock(&in->lock); 2591 2592 str_parms_destroy(parms); 2593 ALOGV("%s: exit: status(%d)", __func__, status); 2594 return status; 2595 } 2596 2597 static char* in_get_parameters(const struct audio_stream *stream __unused, 2598 const char *keys __unused) 2599 { 2600 return strdup(""); 2601 } 2602 2603 static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused) 2604 { 2605 return 0; 2606 } 2607 2608 static void in_snd_mon_cb(void * stream, struct str_parms * parms) 2609 { 2610 if (!stream || !parms) 2611 return; 2612 2613 struct stream_in *in = (struct stream_in *)stream; 2614 struct audio_device *adev = in->dev; 2615 2616 card_status_t status; 2617 int card; 2618 if (parse_snd_card_status(parms, &card, &status) < 0) 2619 return; 2620 2621 pthread_mutex_lock(&adev->lock); 2622 bool valid_cb = (card == adev->snd_card); 2623 pthread_mutex_unlock(&adev->lock); 2624 2625 if (!valid_cb) 2626 return; 2627 2628 lock_input_stream(in); 2629 if (in->card_status != status) 2630 in->card_status = status; 2631 pthread_mutex_unlock(&in->lock); 2632 2633 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card, 2634 use_case_table[in->usecase], 2635 status == CARD_STATUS_OFFLINE ? "offline" : "online"); 2636 2637 // a better solution would be to report error back to AF and let 2638 // it put the stream to standby 2639 if (status == CARD_STATUS_OFFLINE) 2640 in_standby(&in->stream.common); 2641 2642 return; 2643 } 2644 2645 static ssize_t in_read(struct audio_stream_in *stream, void *buffer, 2646 size_t bytes) 2647 { 2648 struct stream_in *in = (struct stream_in *)stream; 2649 struct audio_device *adev = in->dev; 2650 int i, ret = -1; 2651 int *int_buf_stream = NULL; 2652 2653 lock_input_stream(in); 2654 2655 if (in->is_st_session) { 2656 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes); 2657 /* Read from sound trigger HAL */ 2658 audio_extn_sound_trigger_read(in, buffer, bytes); 2659 pthread_mutex_unlock(&in->lock); 2660 return bytes; 2661 } 2662 2663 if (in->standby) { 2664 pthread_mutex_lock(&adev->lock); 2665 ret = start_input_stream(in); 2666 pthread_mutex_unlock(&adev->lock); 2667 if (ret != 0) { 2668 goto exit; 2669 } 2670 in->standby = 0; 2671 } 2672 2673 //what's the duration requested by the client? 2674 long ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/ 2675 in->config.rate; 2676 request_in_focus(in, ns); 2677 2678 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime; 2679 if (in->pcm) { 2680 if (use_mmap) { 2681 ret = pcm_mmap_read(in->pcm, buffer, bytes); 2682 } else { 2683 ret = pcm_read(in->pcm, buffer, bytes); 2684 } 2685 if (ret < 0) { 2686 ALOGE("Failed to read w/err %s", strerror(errno)); 2687 ret = -errno; 2688 } 2689 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) { 2690 if (bytes % 4 == 0) { 2691 /* data from DSP comes in 24_8 format, convert it to 8_24 */ 2692 int_buf_stream = buffer; 2693 for (size_t itt=0; itt < bytes/4 ; itt++) { 2694 int_buf_stream[itt] >>= 8; 2695 } 2696 } else { 2697 ALOGE("%s: !!! something wrong !!! ... data not 32 bit aligned ", __func__); 2698 ret = -EINVAL; 2699 goto exit; 2700 } 2701 } 2702 } 2703 2704 release_in_focus(in, ns); 2705 2706 /* 2707 * Instead of writing zeroes here, we could trust the hardware 2708 * to always provide zeroes when muted. 2709 * No need to acquire adev->lock to read mic_muted here as we don't change its state. 2710 */ 2711 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) 2712 memset(buffer, 0, bytes); 2713 2714 exit: 2715 pthread_mutex_unlock(&in->lock); 2716 2717 if (ret != 0) { 2718 in_standby(&in->stream.common); 2719 ALOGV("%s: read failed - sleeping for buffer duration", __func__); 2720 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) / 2721 in_get_sample_rate(&in->stream.common)); 2722 memset(buffer, 0, bytes); // clear return data 2723 } 2724 if (bytes > 0) { 2725 in->frames_read += bytes / audio_stream_in_frame_size(stream); 2726 } 2727 return bytes; 2728 } 2729 2730 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused) 2731 { 2732 return 0; 2733 } 2734 2735 static int in_get_capture_position(const struct audio_stream_in *stream, 2736 int64_t *frames, int64_t *time) 2737 { 2738 if (stream == NULL || frames == NULL || time == NULL) { 2739 return -EINVAL; 2740 } 2741 struct stream_in *in = (struct stream_in *)stream; 2742 int ret = -ENOSYS; 2743 2744 lock_input_stream(in); 2745 if (in->pcm) { 2746 struct timespec timestamp; 2747 unsigned int avail; 2748 if (pcm_get_htimestamp(in->pcm, &avail, ×tamp) == 0) { 2749 *frames = in->frames_read + avail; 2750 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec; 2751 ret = 0; 2752 } 2753 } 2754 pthread_mutex_unlock(&in->lock); 2755 return ret; 2756 } 2757 2758 static int add_remove_audio_effect(const struct audio_stream *stream, 2759 effect_handle_t effect, 2760 bool enable) 2761 { 2762 struct stream_in *in = (struct stream_in *)stream; 2763 struct audio_device *adev = in->dev; 2764 int status = 0; 2765 effect_descriptor_t desc; 2766 2767 status = (*effect)->get_descriptor(effect, &desc); 2768 if (status != 0) 2769 return status; 2770 2771 lock_input_stream(in); 2772 pthread_mutex_lock(&in->dev->lock); 2773 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 2774 in->source == AUDIO_SOURCE_VOICE_RECOGNITION || 2775 adev->mode == AUDIO_MODE_IN_COMMUNICATION) && 2776 in->enable_aec != enable && 2777 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { 2778 in->enable_aec = enable; 2779 if (!enable) 2780 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE); 2781 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 2782 adev->mode == AUDIO_MODE_IN_COMMUNICATION) { 2783 adev->enable_voicerx = enable; 2784 struct audio_usecase *usecase; 2785 struct listnode *node; 2786 list_for_each(node, &adev->usecase_list) { 2787 usecase = node_to_item(node, struct audio_usecase, list); 2788 if (usecase->type == PCM_PLAYBACK) { 2789 select_devices(adev, usecase->id); 2790 break; 2791 } 2792 } 2793 } 2794 if (!in->standby) 2795 select_devices(in->dev, in->usecase); 2796 } 2797 if (in->enable_ns != enable && 2798 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) { 2799 in->enable_ns = enable; 2800 if (!in->standby) 2801 select_devices(in->dev, in->usecase); 2802 } 2803 pthread_mutex_unlock(&in->dev->lock); 2804 pthread_mutex_unlock(&in->lock); 2805 2806 return 0; 2807 } 2808 2809 static int in_add_audio_effect(const struct audio_stream *stream, 2810 effect_handle_t effect) 2811 { 2812 ALOGV("%s: effect %p", __func__, effect); 2813 return add_remove_audio_effect(stream, effect, true); 2814 } 2815 2816 static int in_remove_audio_effect(const struct audio_stream *stream, 2817 effect_handle_t effect) 2818 { 2819 ALOGV("%s: effect %p", __func__, effect); 2820 return add_remove_audio_effect(stream, effect, false); 2821 } 2822 2823 static int adev_open_output_stream(struct audio_hw_device *dev, 2824 audio_io_handle_t handle, 2825 audio_devices_t devices, 2826 audio_output_flags_t flags, 2827 struct audio_config *config, 2828 struct audio_stream_out **stream_out, 2829 const char *address __unused) 2830 { 2831 struct audio_device *adev = (struct audio_device *)dev; 2832 struct stream_out *out; 2833 int i, ret; 2834 2835 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", 2836 __func__, config->sample_rate, config->channel_mask, devices, flags); 2837 *stream_out = NULL; 2838 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 2839 2840 if (devices == AUDIO_DEVICE_NONE) 2841 devices = AUDIO_DEVICE_OUT_SPEAKER; 2842 2843 out->flags = flags; 2844 out->devices = devices; 2845 out->dev = adev; 2846 out->format = config->format; 2847 out->sample_rate = config->sample_rate; 2848 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 2849 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; 2850 out->handle = handle; 2851 2852 /* Init use case and pcm_config */ 2853 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && 2854 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && 2855 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 2856 pthread_mutex_lock(&adev->lock); 2857 ret = read_hdmi_channel_masks(out); 2858 pthread_mutex_unlock(&adev->lock); 2859 if (ret != 0) 2860 goto error_open; 2861 2862 if (config->sample_rate == 0) 2863 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 2864 if (config->channel_mask == 0) 2865 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; 2866 if (config->format == AUDIO_FORMAT_DEFAULT) 2867 config->format = AUDIO_FORMAT_PCM_16_BIT; 2868 2869 out->channel_mask = config->channel_mask; 2870 out->sample_rate = config->sample_rate; 2871 out->format = config->format; 2872 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH; 2873 out->config = pcm_config_hdmi_multi; 2874 out->config.rate = config->sample_rate; 2875 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask); 2876 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2); 2877 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 2878 pthread_mutex_lock(&adev->lock); 2879 bool offline = (adev->card_status == CARD_STATUS_OFFLINE); 2880 pthread_mutex_unlock(&adev->lock); 2881 2882 // reject offload during card offline to allow 2883 // fallback to s/w paths 2884 if (offline) { 2885 ret = -ENODEV; 2886 goto error_open; 2887 } 2888 2889 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || 2890 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { 2891 ALOGE("%s: Unsupported Offload information", __func__); 2892 ret = -EINVAL; 2893 goto error_open; 2894 } 2895 if (!is_supported_format(config->offload_info.format)) { 2896 ALOGE("%s: Unsupported audio format", __func__); 2897 ret = -EINVAL; 2898 goto error_open; 2899 } 2900 2901 out->compr_config.codec = (struct snd_codec *) 2902 calloc(1, sizeof(struct snd_codec)); 2903 2904 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; 2905 if (config->offload_info.channel_mask) 2906 out->channel_mask = config->offload_info.channel_mask; 2907 else if (config->channel_mask) 2908 out->channel_mask = config->channel_mask; 2909 out->format = config->offload_info.format; 2910 out->sample_rate = config->offload_info.sample_rate; 2911 2912 out->stream.set_callback = out_set_callback; 2913 out->stream.pause = out_pause; 2914 out->stream.resume = out_resume; 2915 out->stream.drain = out_drain; 2916 out->stream.flush = out_flush; 2917 2918 out->compr_config.codec->id = 2919 get_snd_codec_id(config->offload_info.format); 2920 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; 2921 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; 2922 out->compr_config.codec->sample_rate = config->offload_info.sample_rate; 2923 out->compr_config.codec->bit_rate = 2924 config->offload_info.bit_rate; 2925 out->compr_config.codec->ch_in = 2926 audio_channel_count_from_out_mask(config->channel_mask); 2927 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; 2928 2929 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) 2930 out->non_blocking = 1; 2931 2932 out->send_new_metadata = 1; 2933 create_offload_callback_thread(out); 2934 ALOGV("%s: offloaded output offload_info version %04x bit rate %d", 2935 __func__, config->offload_info.version, 2936 config->offload_info.bit_rate); 2937 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) { 2938 if (config->sample_rate == 0) 2939 config->sample_rate = AFE_PROXY_SAMPLING_RATE; 2940 if (config->sample_rate != 48000 && config->sample_rate != 16000 && 2941 config->sample_rate != 8000) { 2942 config->sample_rate = AFE_PROXY_SAMPLING_RATE; 2943 ret = -EINVAL; 2944 goto error_open; 2945 } 2946 out->sample_rate = config->sample_rate; 2947 out->config.rate = config->sample_rate; 2948 if (config->format == AUDIO_FORMAT_DEFAULT) 2949 config->format = AUDIO_FORMAT_PCM_16_BIT; 2950 if (config->format != AUDIO_FORMAT_PCM_16_BIT) { 2951 config->format = AUDIO_FORMAT_PCM_16_BIT; 2952 ret = -EINVAL; 2953 goto error_open; 2954 } 2955 out->format = config->format; 2956 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY; 2957 out->config = pcm_config_afe_proxy_playback; 2958 adev->voice_tx_output = out; 2959 } else { 2960 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 2961 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; 2962 out->config = pcm_config_deep_buffer; 2963 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) { 2964 out->usecase = USECASE_AUDIO_PLAYBACK_TTS; 2965 out->config = pcm_config_deep_buffer; 2966 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) { 2967 out->usecase = USECASE_AUDIO_PLAYBACK_ULL; 2968 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, out->flags); 2969 out->usecase = USECASE_AUDIO_PLAYBACK_ULL; 2970 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency; 2971 } else { 2972 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; 2973 out->config = pcm_config_low_latency; 2974 } 2975 if (config->format != audio_format_from_pcm_format(out->config.format)) { 2976 if (k_enable_extended_precision 2977 && pcm_params_format_test(adev->use_case_table[out->usecase], 2978 pcm_format_from_audio_format(config->format))) { 2979 out->config.format = pcm_format_from_audio_format(config->format); 2980 /* out->format already set to config->format */ 2981 } else { 2982 /* deny the externally proposed config format 2983 * and use the one specified in audio_hw layer configuration. 2984 * Note: out->format is returned by out->stream.common.get_format() 2985 * and is used to set config->format in the code several lines below. 2986 */ 2987 out->format = audio_format_from_pcm_format(out->config.format); 2988 } 2989 } 2990 out->sample_rate = out->config.rate; 2991 } 2992 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n", 2993 __func__, use_case_table[out->usecase], config->format, out->config.format); 2994 2995 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { 2996 if (adev->primary_output == NULL) 2997 adev->primary_output = out; 2998 else { 2999 ALOGE("%s: Primary output is already opened", __func__); 3000 ret = -EEXIST; 3001 goto error_open; 3002 } 3003 } 3004 3005 /* Check if this usecase is already existing */ 3006 pthread_mutex_lock(&adev->lock); 3007 if (get_usecase_from_list(adev, out->usecase) != NULL) { 3008 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); 3009 pthread_mutex_unlock(&adev->lock); 3010 ret = -EEXIST; 3011 goto error_open; 3012 } 3013 pthread_mutex_unlock(&adev->lock); 3014 3015 out->stream.common.get_sample_rate = out_get_sample_rate; 3016 out->stream.common.set_sample_rate = out_set_sample_rate; 3017 out->stream.common.get_buffer_size = out_get_buffer_size; 3018 out->stream.common.get_channels = out_get_channels; 3019 out->stream.common.get_format = out_get_format; 3020 out->stream.common.set_format = out_set_format; 3021 out->stream.common.standby = out_standby; 3022 out->stream.common.dump = out_dump; 3023 out->stream.common.set_parameters = out_set_parameters; 3024 out->stream.common.get_parameters = out_get_parameters; 3025 out->stream.common.add_audio_effect = out_add_audio_effect; 3026 out->stream.common.remove_audio_effect = out_remove_audio_effect; 3027 out->stream.get_latency = out_get_latency; 3028 out->stream.set_volume = out_set_volume; 3029 #ifdef NO_AUDIO_OUT 3030 out->stream.write = out_write_for_no_output; 3031 #else 3032 out->stream.write = out_write; 3033 #endif 3034 out->stream.get_render_position = out_get_render_position; 3035 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 3036 out->stream.get_presentation_position = out_get_presentation_position; 3037 3038 out->af_period_multiplier = out->realtime ? af_period_multiplier : 1; 3039 out->standby = 1; 3040 /* out->muted = false; by calloc() */ 3041 /* out->written = 0; by calloc() */ 3042 3043 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); 3044 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL); 3045 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); 3046 3047 config->format = out->stream.common.get_format(&out->stream.common); 3048 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 3049 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 3050 3051 3052 /* 3053 By locking output stream before registering, we allow the callback 3054 to update stream's state only after stream's initial state is set to 3055 adev state. 3056 */ 3057 lock_output_stream(out); 3058 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb); 3059 pthread_mutex_lock(&adev->lock); 3060 out->card_status = adev->card_status; 3061 pthread_mutex_unlock(&adev->lock); 3062 pthread_mutex_unlock(&out->lock); 3063 3064 *stream_out = &out->stream; 3065 ALOGV("%s: exit", __func__); 3066 return 0; 3067 3068 error_open: 3069 free(out); 3070 *stream_out = NULL; 3071 ALOGW("%s: exit: ret %d", __func__, ret); 3072 return ret; 3073 } 3074 3075 static void adev_close_output_stream(struct audio_hw_device *dev __unused, 3076 struct audio_stream_out *stream) 3077 { 3078 struct stream_out *out = (struct stream_out *)stream; 3079 struct audio_device *adev = out->dev; 3080 3081 ALOGV("%s: enter", __func__); 3082 3083 // must deregister from sndmonitor first to prevent races 3084 // between the callback and close_stream 3085 audio_extn_snd_mon_unregister_listener(out); 3086 out_standby(&stream->common); 3087 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3088 destroy_offload_callback_thread(out); 3089 3090 if (out->compr_config.codec != NULL) 3091 free(out->compr_config.codec); 3092 } 3093 3094 if (adev->voice_tx_output == out) 3095 adev->voice_tx_output = NULL; 3096 3097 pthread_cond_destroy(&out->cond); 3098 pthread_mutex_destroy(&out->lock); 3099 free(stream); 3100 ALOGV("%s: exit", __func__); 3101 } 3102 3103 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 3104 { 3105 struct audio_device *adev = (struct audio_device *)dev; 3106 struct str_parms *parms; 3107 char *str; 3108 char value[32]; 3109 int val; 3110 int ret; 3111 int status = 0; 3112 3113 ALOGV("%s: enter: %s", __func__, kvpairs); 3114 3115 pthread_mutex_lock(&adev->lock); 3116 3117 parms = str_parms_create_str(kvpairs); 3118 status = voice_set_parameters(adev, parms); 3119 if (status != 0) { 3120 goto done; 3121 } 3122 3123 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 3124 if (ret >= 0) { 3125 /* When set to false, HAL should disable EC and NS */ 3126 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 3127 adev->bluetooth_nrec = true; 3128 else 3129 adev->bluetooth_nrec = false; 3130 } 3131 3132 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 3133 if (ret >= 0) { 3134 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 3135 adev->screen_off = false; 3136 else 3137 adev->screen_off = true; 3138 } 3139 3140 ret = str_parms_get_int(parms, "rotation", &val); 3141 if (ret >= 0) { 3142 bool reverse_speakers = false; 3143 switch(val) { 3144 // FIXME: note that the code below assumes that the speakers are in the correct placement 3145 // relative to the user when the device is rotated 90deg from its default rotation. This 3146 // assumption is device-specific, not platform-specific like this code. 3147 case 270: 3148 reverse_speakers = true; 3149 break; 3150 case 0: 3151 case 90: 3152 case 180: 3153 break; 3154 default: 3155 ALOGE("%s: unexpected rotation of %d", __func__, val); 3156 status = -EINVAL; 3157 } 3158 if (status == 0) { 3159 platform_swap_lr_channels(adev, reverse_speakers); 3160 } 3161 } 3162 3163 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value)); 3164 if (ret >= 0) { 3165 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON); 3166 } 3167 3168 audio_extn_hfp_set_parameters(adev, parms); 3169 done: 3170 str_parms_destroy(parms); 3171 pthread_mutex_unlock(&adev->lock); 3172 ALOGV("%s: exit with code(%d)", __func__, status); 3173 return status; 3174 } 3175 3176 static char* adev_get_parameters(const struct audio_hw_device *dev, 3177 const char *keys) 3178 { 3179 struct audio_device *adev = (struct audio_device *)dev; 3180 struct str_parms *reply = str_parms_create(); 3181 struct str_parms *query = str_parms_create_str(keys); 3182 char *str; 3183 3184 pthread_mutex_lock(&adev->lock); 3185 3186 voice_get_parameters(adev, query, reply); 3187 str = str_parms_to_str(reply); 3188 str_parms_destroy(query); 3189 str_parms_destroy(reply); 3190 3191 pthread_mutex_unlock(&adev->lock); 3192 ALOGV("%s: exit: returns - %s", __func__, str); 3193 return str; 3194 } 3195 3196 static int adev_init_check(const struct audio_hw_device *dev __unused) 3197 { 3198 return 0; 3199 } 3200 3201 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 3202 { 3203 int ret; 3204 struct audio_device *adev = (struct audio_device *)dev; 3205 3206 audio_extn_extspk_set_voice_vol(adev->extspk, volume); 3207 3208 pthread_mutex_lock(&adev->lock); 3209 ret = voice_set_volume(adev, volume); 3210 pthread_mutex_unlock(&adev->lock); 3211 3212 return ret; 3213 } 3214 3215 static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused) 3216 { 3217 return -ENOSYS; 3218 } 3219 3220 static int adev_get_master_volume(struct audio_hw_device *dev __unused, 3221 float *volume __unused) 3222 { 3223 return -ENOSYS; 3224 } 3225 3226 static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused) 3227 { 3228 return -ENOSYS; 3229 } 3230 3231 static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused) 3232 { 3233 return -ENOSYS; 3234 } 3235 3236 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 3237 { 3238 struct audio_device *adev = (struct audio_device *)dev; 3239 3240 pthread_mutex_lock(&adev->lock); 3241 if (adev->mode != mode) { 3242 ALOGD("%s: mode %d", __func__, (int)mode); 3243 adev->mode = mode; 3244 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) && 3245 voice_is_in_call(adev)) { 3246 voice_stop_call(adev); 3247 adev->current_call_output = NULL; 3248 } 3249 } 3250 pthread_mutex_unlock(&adev->lock); 3251 3252 audio_extn_extspk_set_mode(adev->extspk, mode); 3253 3254 return 0; 3255 } 3256 3257 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 3258 { 3259 int ret; 3260 struct audio_device *adev = (struct audio_device *)dev; 3261 3262 ALOGD("%s: state %d", __func__, (int)state); 3263 pthread_mutex_lock(&adev->lock); 3264 ret = voice_set_mic_mute(adev, state); 3265 adev->mic_muted = state; 3266 pthread_mutex_unlock(&adev->lock); 3267 3268 return ret; 3269 } 3270 3271 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 3272 { 3273 *state = voice_get_mic_mute((struct audio_device *)dev); 3274 return 0; 3275 } 3276 3277 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused, 3278 const struct audio_config *config) 3279 { 3280 int channel_count = audio_channel_count_from_in_mask(config->channel_mask); 3281 3282 return get_input_buffer_size(config->sample_rate, config->format, channel_count, 3283 false /* is_low_latency: since we don't know, be conservative */); 3284 } 3285 3286 static int adev_open_input_stream(struct audio_hw_device *dev, 3287 audio_io_handle_t handle, 3288 audio_devices_t devices, 3289 struct audio_config *config, 3290 struct audio_stream_in **stream_in, 3291 audio_input_flags_t flags, 3292 const char *address __unused, 3293 audio_source_t source ) 3294 { 3295 struct audio_device *adev = (struct audio_device *)dev; 3296 struct stream_in *in; 3297 int ret = 0, buffer_size, frame_size; 3298 int channel_count = audio_channel_count_from_in_mask(config->channel_mask); 3299 bool is_low_latency = false; 3300 3301 ALOGV("%s: enter", __func__); 3302 *stream_in = NULL; 3303 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) 3304 return -EINVAL; 3305 3306 in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 3307 3308 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); 3309 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL); 3310 3311 in->stream.common.get_sample_rate = in_get_sample_rate; 3312 in->stream.common.set_sample_rate = in_set_sample_rate; 3313 in->stream.common.get_buffer_size = in_get_buffer_size; 3314 in->stream.common.get_channels = in_get_channels; 3315 in->stream.common.get_format = in_get_format; 3316 in->stream.common.set_format = in_set_format; 3317 in->stream.common.standby = in_standby; 3318 in->stream.common.dump = in_dump; 3319 in->stream.common.set_parameters = in_set_parameters; 3320 in->stream.common.get_parameters = in_get_parameters; 3321 in->stream.common.add_audio_effect = in_add_audio_effect; 3322 in->stream.common.remove_audio_effect = in_remove_audio_effect; 3323 in->stream.set_gain = in_set_gain; 3324 in->stream.read = in_read; 3325 in->stream.get_input_frames_lost = in_get_input_frames_lost; 3326 in->stream.get_capture_position = in_get_capture_position; 3327 3328 in->device = devices; 3329 in->source = source; 3330 in->dev = adev; 3331 in->standby = 1; 3332 in->channel_mask = config->channel_mask; 3333 in->capture_handle = handle; 3334 in->flags = flags; 3335 3336 // restrict 24 bit capture for unprocessed source only 3337 // for other sources if 24 bit requested reject 24 and set 16 bit capture only 3338 if (config->format == AUDIO_FORMAT_DEFAULT) { 3339 config->format = AUDIO_FORMAT_PCM_16_BIT; 3340 } else if (config->format == AUDIO_FORMAT_PCM_FLOAT || 3341 config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED || 3342 config->format == AUDIO_FORMAT_PCM_8_24_BIT) { 3343 bool ret_error = false; 3344 /* 24 bit is restricted to UNPROCESSED source only,also format supported 3345 from HAL is 8_24 3346 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than 3347 8_24 return error indicating supported format is 8_24 3348 *> In case of any other source requesting 24 bit or float return error 3349 indicating format supported is 16 bit only. 3350 3351 on error flinger will retry with supported format passed 3352 */ 3353 if (source != AUDIO_SOURCE_UNPROCESSED) { 3354 config->format = AUDIO_FORMAT_PCM_16_BIT; 3355 ret_error = true; 3356 } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) { 3357 config->format = AUDIO_FORMAT_PCM_8_24_BIT; 3358 ret_error = true; 3359 } 3360 3361 if (ret_error) { 3362 ret = -EINVAL; 3363 goto err_open; 3364 } 3365 } 3366 3367 in->format = config->format; 3368 3369 /* Update config params with the requested sample rate and channels */ 3370 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) { 3371 if (config->sample_rate == 0) 3372 config->sample_rate = AFE_PROXY_SAMPLING_RATE; 3373 if (config->sample_rate != 48000 && config->sample_rate != 16000 && 3374 config->sample_rate != 8000) { 3375 config->sample_rate = AFE_PROXY_SAMPLING_RATE; 3376 ret = -EINVAL; 3377 goto err_open; 3378 } 3379 3380 if (config->format != AUDIO_FORMAT_PCM_16_BIT) { 3381 config->format = AUDIO_FORMAT_PCM_16_BIT; 3382 ret = -EINVAL; 3383 goto err_open; 3384 } 3385 3386 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY; 3387 in->config = pcm_config_afe_proxy_record; 3388 } else { 3389 in->usecase = USECASE_AUDIO_RECORD; 3390 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE && 3391 (flags & AUDIO_INPUT_FLAG_FAST) != 0) { 3392 is_low_latency = true; 3393 #if LOW_LATENCY_CAPTURE_USE_CASE 3394 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY; 3395 #endif 3396 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags); 3397 } 3398 3399 in->config = in->realtime ? pcm_config_audio_capture_rt : 3400 pcm_config_audio_capture; 3401 3402 if (config->format == AUDIO_FORMAT_PCM_8_24_BIT) 3403 in->config.format = PCM_FORMAT_S24_LE; 3404 3405 if (!in->realtime) { 3406 frame_size = audio_stream_in_frame_size(&in->stream); 3407 buffer_size = get_input_buffer_size(config->sample_rate, 3408 config->format, 3409 channel_count, 3410 is_low_latency); 3411 in->config.period_size = buffer_size / frame_size; 3412 } // period size is left untouched for rt mode playback 3413 } 3414 3415 in->config.channels = channel_count; 3416 if (in->realtime) { 3417 in->af_period_multiplier = af_period_multiplier; 3418 } else { 3419 in->config.rate = config->sample_rate; 3420 in->af_period_multiplier = 1; 3421 } 3422 3423 /* This stream could be for sound trigger lab, 3424 get sound trigger pcm if present */ 3425 audio_extn_sound_trigger_check_and_get_session(in); 3426 3427 lock_input_stream(in); 3428 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb); 3429 pthread_mutex_lock(&adev->lock); 3430 in->card_status = adev->card_status; 3431 pthread_mutex_unlock(&adev->lock); 3432 pthread_mutex_unlock(&in->lock); 3433 3434 *stream_in = &in->stream; 3435 ALOGV("%s: exit", __func__); 3436 return 0; 3437 3438 err_open: 3439 free(in); 3440 *stream_in = NULL; 3441 return ret; 3442 } 3443 3444 static void adev_close_input_stream(struct audio_hw_device *dev __unused, 3445 struct audio_stream_in *stream) 3446 { 3447 ALOGV("%s", __func__); 3448 3449 // must deregister from sndmonitor first to prevent races 3450 // between the callback and close_stream 3451 audio_extn_snd_mon_unregister_listener(stream); 3452 in_standby(&stream->common); 3453 free(stream); 3454 3455 return; 3456 } 3457 3458 static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused) 3459 { 3460 return 0; 3461 } 3462 3463 /* verifies input and output devices and their capabilities. 3464 * 3465 * This verification is required when enabling extended bit-depth or 3466 * sampling rates, as not all qcom products support it. 3467 * 3468 * Suitable for calling only on initialization such as adev_open(). 3469 * It fills the audio_device use_case_table[] array. 3470 * 3471 * Has a side-effect that it needs to configure audio routing / devices 3472 * in order to power up the devices and read the device parameters. 3473 * It does not acquire any hw device lock. Should restore the devices 3474 * back to "normal state" upon completion. 3475 */ 3476 static int adev_verify_devices(struct audio_device *adev) 3477 { 3478 /* enumeration is a bit difficult because one really wants to pull 3479 * the use_case, device id, etc from the hidden pcm_device_table[]. 3480 * In this case there are the following use cases and device ids. 3481 * 3482 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0}, 3483 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15}, 3484 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1}, 3485 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9}, 3486 * [USECASE_AUDIO_RECORD] = {0, 0}, 3487 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15}, 3488 * [USECASE_VOICE_CALL] = {2, 2}, 3489 * 3490 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted. 3491 * USECASE_VOICE_CALL omitted, but possible for either input or output. 3492 */ 3493 3494 /* should be the usecases enabled in adev_open_input_stream() */ 3495 static const int test_in_usecases[] = { 3496 USECASE_AUDIO_RECORD, 3497 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */ 3498 }; 3499 /* should be the usecases enabled in adev_open_output_stream()*/ 3500 static const int test_out_usecases[] = { 3501 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER, 3502 USECASE_AUDIO_PLAYBACK_LOW_LATENCY, 3503 }; 3504 static const usecase_type_t usecase_type_by_dir[] = { 3505 PCM_PLAYBACK, 3506 PCM_CAPTURE, 3507 }; 3508 static const unsigned flags_by_dir[] = { 3509 PCM_OUT, 3510 PCM_IN, 3511 }; 3512 3513 size_t i; 3514 unsigned dir; 3515 const unsigned card_id = adev->snd_card; 3516 char info[512]; /* for possible debug info */ 3517 3518 for (dir = 0; dir < 2; ++dir) { 3519 const usecase_type_t usecase_type = usecase_type_by_dir[dir]; 3520 const unsigned flags_dir = flags_by_dir[dir]; 3521 const size_t testsize = 3522 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases); 3523 const int *testcases = 3524 dir ? test_in_usecases : test_out_usecases; 3525 const audio_devices_t audio_device = 3526 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER; 3527 3528 for (i = 0; i < testsize; ++i) { 3529 const audio_usecase_t audio_usecase = testcases[i]; 3530 int device_id; 3531 snd_device_t snd_device; 3532 struct pcm_params **pparams; 3533 struct stream_out out; 3534 struct stream_in in; 3535 struct audio_usecase uc_info; 3536 int retval; 3537 3538 pparams = &adev->use_case_table[audio_usecase]; 3539 pcm_params_free(*pparams); /* can accept null input */ 3540 *pparams = NULL; 3541 3542 /* find the device ID for the use case (signed, for error) */ 3543 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type); 3544 if (device_id < 0) 3545 continue; 3546 3547 /* prepare structures for device probing */ 3548 memset(&uc_info, 0, sizeof(uc_info)); 3549 uc_info.id = audio_usecase; 3550 uc_info.type = usecase_type; 3551 if (dir) { 3552 adev->active_input = ∈ 3553 memset(&in, 0, sizeof(in)); 3554 in.device = audio_device; 3555 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION; 3556 uc_info.stream.in = ∈ 3557 } else { 3558 adev->active_input = NULL; 3559 } 3560 memset(&out, 0, sizeof(out)); 3561 out.devices = audio_device; /* only field needed in select_devices */ 3562 uc_info.stream.out = &out; 3563 uc_info.devices = audio_device; 3564 uc_info.in_snd_device = SND_DEVICE_NONE; 3565 uc_info.out_snd_device = SND_DEVICE_NONE; 3566 list_add_tail(&adev->usecase_list, &uc_info.list); 3567 3568 /* select device - similar to start_(in/out)put_stream() */ 3569 retval = select_devices(adev, audio_usecase); 3570 if (retval >= 0) { 3571 *pparams = pcm_params_get(card_id, device_id, flags_dir); 3572 #if LOG_NDEBUG == 0 3573 if (*pparams) { 3574 ALOGV("%s: (%s) card %d device %d", __func__, 3575 dir ? "input" : "output", card_id, device_id); 3576 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info)); 3577 } else { 3578 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id); 3579 } 3580 #endif 3581 } 3582 3583 /* deselect device - similar to stop_(in/out)put_stream() */ 3584 /* 1. Get and set stream specific mixer controls */ 3585 retval = disable_audio_route(adev, &uc_info); 3586 /* 2. Disable the rx device */ 3587 retval = disable_snd_device(adev, 3588 dir ? uc_info.in_snd_device : uc_info.out_snd_device); 3589 list_remove(&uc_info.list); 3590 } 3591 } 3592 adev->active_input = NULL; /* restore adev state */ 3593 return 0; 3594 } 3595 3596 static int adev_close(hw_device_t *device) 3597 { 3598 size_t i; 3599 struct audio_device *adev = (struct audio_device *)device; 3600 3601 if (!adev) 3602 return 0; 3603 3604 audio_extn_snd_mon_unregister_listener(adev); 3605 pthread_mutex_lock(&adev_init_lock); 3606 3607 if ((--audio_device_ref_count) == 0) { 3608 audio_route_free(adev->audio_route); 3609 free(adev->snd_dev_ref_cnt); 3610 platform_deinit(adev->platform); 3611 audio_extn_extspk_deinit(adev->extspk); 3612 audio_extn_sound_trigger_deinit(adev); 3613 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) { 3614 pcm_params_free(adev->use_case_table[i]); 3615 } 3616 if (adev->adm_deinit) 3617 adev->adm_deinit(adev->adm_data); 3618 free(device); 3619 } 3620 3621 pthread_mutex_unlock(&adev_init_lock); 3622 3623 return 0; 3624 } 3625 3626 /* This returns 1 if the input parameter looks at all plausible as a low latency period size, 3627 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work, 3628 * just that it _might_ work. 3629 */ 3630 static int period_size_is_plausible_for_low_latency(int period_size) 3631 { 3632 switch (period_size) { 3633 case 48: 3634 case 96: 3635 case 144: 3636 case 160: 3637 case 192: 3638 case 240: 3639 case 320: 3640 case 480: 3641 return 1; 3642 default: 3643 return 0; 3644 } 3645 } 3646 3647 static void adev_snd_mon_cb(void * stream __unused, struct str_parms * parms) 3648 { 3649 int card; 3650 card_status_t status; 3651 3652 if (!parms) 3653 return; 3654 3655 if (parse_snd_card_status(parms, &card, &status) < 0) 3656 return; 3657 3658 pthread_mutex_lock(&adev->lock); 3659 bool valid_cb = (card == adev->snd_card); 3660 if (valid_cb) { 3661 if (adev->card_status != status) { 3662 adev->card_status = status; 3663 platform_snd_card_update(adev->platform, status); 3664 } 3665 } 3666 pthread_mutex_unlock(&adev->lock); 3667 return; 3668 } 3669 3670 static int adev_open(const hw_module_t *module, const char *name, 3671 hw_device_t **device) 3672 { 3673 int i, ret; 3674 3675 ALOGD("%s: enter", __func__); 3676 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; 3677 pthread_mutex_lock(&adev_init_lock); 3678 if (audio_device_ref_count != 0) { 3679 *device = &adev->device.common; 3680 audio_device_ref_count++; 3681 ALOGV("%s: returning existing instance of adev", __func__); 3682 ALOGV("%s: exit", __func__); 3683 pthread_mutex_unlock(&adev_init_lock); 3684 return 0; 3685 } 3686 adev = calloc(1, sizeof(struct audio_device)); 3687 3688 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); 3689 3690 adev->device.common.tag = HARDWARE_DEVICE_TAG; 3691 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 3692 adev->device.common.module = (struct hw_module_t *)module; 3693 adev->device.common.close = adev_close; 3694 3695 adev->device.init_check = adev_init_check; 3696 adev->device.set_voice_volume = adev_set_voice_volume; 3697 adev->device.set_master_volume = adev_set_master_volume; 3698 adev->device.get_master_volume = adev_get_master_volume; 3699 adev->device.set_master_mute = adev_set_master_mute; 3700 adev->device.get_master_mute = adev_get_master_mute; 3701 adev->device.set_mode = adev_set_mode; 3702 adev->device.set_mic_mute = adev_set_mic_mute; 3703 adev->device.get_mic_mute = adev_get_mic_mute; 3704 adev->device.set_parameters = adev_set_parameters; 3705 adev->device.get_parameters = adev_get_parameters; 3706 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 3707 adev->device.open_output_stream = adev_open_output_stream; 3708 adev->device.close_output_stream = adev_close_output_stream; 3709 adev->device.open_input_stream = adev_open_input_stream; 3710 adev->device.close_input_stream = adev_close_input_stream; 3711 adev->device.dump = adev_dump; 3712 3713 /* Set the default route before the PCM stream is opened */ 3714 pthread_mutex_lock(&adev->lock); 3715 adev->mode = AUDIO_MODE_NORMAL; 3716 adev->active_input = NULL; 3717 adev->primary_output = NULL; 3718 adev->bluetooth_nrec = true; 3719 adev->acdb_settings = TTY_MODE_OFF; 3720 /* adev->cur_hdmi_channels = 0; by calloc() */ 3721 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); 3722 voice_init(adev); 3723 list_init(&adev->usecase_list); 3724 pthread_mutex_unlock(&adev->lock); 3725 3726 /* Loads platform specific libraries dynamically */ 3727 adev->platform = platform_init(adev); 3728 if (!adev->platform) { 3729 free(adev->snd_dev_ref_cnt); 3730 free(adev); 3731 ALOGE("%s: Failed to init platform data, aborting.", __func__); 3732 *device = NULL; 3733 pthread_mutex_unlock(&adev_init_lock); 3734 return -EINVAL; 3735 } 3736 adev->extspk = audio_extn_extspk_init(adev); 3737 audio_extn_sound_trigger_init(adev); 3738 3739 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); 3740 if (adev->visualizer_lib == NULL) { 3741 ALOGW("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); 3742 } else { 3743 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); 3744 adev->visualizer_start_output = 3745 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, 3746 "visualizer_hal_start_output"); 3747 adev->visualizer_stop_output = 3748 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, 3749 "visualizer_hal_stop_output"); 3750 } 3751 3752 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW); 3753 if (adev->offload_effects_lib == NULL) { 3754 ALOGW("%s: DLOPEN failed for %s", __func__, 3755 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); 3756 } else { 3757 ALOGV("%s: DLOPEN successful for %s", __func__, 3758 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); 3759 adev->offload_effects_start_output = 3760 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, 3761 "offload_effects_bundle_hal_start_output"); 3762 adev->offload_effects_stop_output = 3763 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, 3764 "offload_effects_bundle_hal_stop_output"); 3765 } 3766 3767 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW); 3768 if (adev->adm_lib == NULL) { 3769 ALOGW("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH); 3770 } else { 3771 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH); 3772 adev->adm_init = (adm_init_t) 3773 dlsym(adev->adm_lib, "adm_init"); 3774 adev->adm_deinit = (adm_deinit_t) 3775 dlsym(adev->adm_lib, "adm_deinit"); 3776 adev->adm_register_input_stream = (adm_register_input_stream_t) 3777 dlsym(adev->adm_lib, "adm_register_input_stream"); 3778 adev->adm_register_output_stream = (adm_register_output_stream_t) 3779 dlsym(adev->adm_lib, "adm_register_output_stream"); 3780 adev->adm_deregister_stream = (adm_deregister_stream_t) 3781 dlsym(adev->adm_lib, "adm_deregister_stream"); 3782 adev->adm_request_focus = (adm_request_focus_t) 3783 dlsym(adev->adm_lib, "adm_request_focus"); 3784 adev->adm_abandon_focus = (adm_abandon_focus_t) 3785 dlsym(adev->adm_lib, "adm_abandon_focus"); 3786 adev->adm_set_config = (adm_set_config_t) 3787 dlsym(adev->adm_lib, "adm_set_config"); 3788 adev->adm_request_focus_v2 = (adm_request_focus_v2_t) 3789 dlsym(adev->adm_lib, "adm_request_focus_v2"); 3790 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t) 3791 dlsym(adev->adm_lib, "adm_is_noirq_avail"); 3792 adev->adm_on_routing_change = (adm_on_routing_change_t) 3793 dlsym(adev->adm_lib, "adm_on_routing_change"); 3794 } 3795 3796 adev->bt_wb_speech_enabled = false; 3797 adev->enable_voicerx = false; 3798 3799 *device = &adev->device.common; 3800 3801 if (k_enable_extended_precision) 3802 adev_verify_devices(adev); 3803 3804 char value[PROPERTY_VALUE_MAX]; 3805 int trial; 3806 if (property_get("audio_hal.period_size", value, NULL) > 0) { 3807 trial = atoi(value); 3808 if (period_size_is_plausible_for_low_latency(trial)) { 3809 pcm_config_low_latency.period_size = trial; 3810 pcm_config_low_latency.start_threshold = trial / 4; 3811 pcm_config_low_latency.avail_min = trial / 4; 3812 configured_low_latency_capture_period_size = trial; 3813 } 3814 } 3815 if (property_get("audio_hal.in_period_size", value, NULL) > 0) { 3816 trial = atoi(value); 3817 if (period_size_is_plausible_for_low_latency(trial)) { 3818 configured_low_latency_capture_period_size = trial; 3819 } 3820 } 3821 3822 audio_device_ref_count++; 3823 3824 if (property_get("audio_hal.period_multiplier", value, NULL) > 0) { 3825 af_period_multiplier = atoi(value); 3826 if (af_period_multiplier < 0) { 3827 af_period_multiplier = 2; 3828 } else if (af_period_multiplier > 4) { 3829 af_period_multiplier = 4; 3830 } 3831 ALOGV("new period_multiplier = %d", af_period_multiplier); 3832 } 3833 3834 pthread_mutex_unlock(&adev_init_lock); 3835 3836 if (adev->adm_init) 3837 adev->adm_data = adev->adm_init(); 3838 3839 audio_extn_perf_lock_init(); 3840 audio_extn_snd_mon_init(); 3841 pthread_mutex_lock(&adev->lock); 3842 audio_extn_snd_mon_register_listener(NULL, adev_snd_mon_cb); 3843 adev->card_status = CARD_STATUS_ONLINE; 3844 pthread_mutex_unlock(&adev->lock); 3845 3846 ALOGD("%s: exit", __func__); 3847 return 0; 3848 } 3849 3850 static struct hw_module_methods_t hal_module_methods = { 3851 .open = adev_open, 3852 }; 3853 3854 struct audio_module HAL_MODULE_INFO_SYM = { 3855 .common = { 3856 .tag = HARDWARE_MODULE_TAG, 3857 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 3858 .hal_api_version = HARDWARE_HAL_API_VERSION, 3859 .id = AUDIO_HARDWARE_MODULE_ID, 3860 .name = "QCOM Audio HAL", 3861 .author = "Code Aurora Forum", 3862 .methods = &hal_module_methods, 3863 }, 3864 }; 3865