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 #include <limits.h> 37 38 #include <log/log.h> 39 #include <cutils/trace.h> 40 #include <cutils/str_parms.h> 41 #include <cutils/properties.h> 42 #include <cutils/atomic.h> 43 #include <utils/Timers.h> // systemTime 44 45 #include <hardware/audio_effect.h> 46 #include <hardware/audio_alsaops.h> 47 #include <processgroup/sched_policy.h> 48 #include <system/thread_defs.h> 49 #include <tinyalsa/asoundlib.h> 50 #include <audio_effects/effect_aec.h> 51 #include <audio_effects/effect_ns.h> 52 #include <audio_utils/clock.h> 53 #include "audio_hw.h" 54 #include "audio_extn.h" 55 #include "audio_perf.h" 56 #include "platform_api.h" 57 #include <platform.h> 58 #include "voice_extn.h" 59 60 #include "sound/compress_params.h" 61 #include "audio_extn/tfa_98xx.h" 62 #include "audio_extn/maxxaudio.h" 63 #include "audio_extn/audiozoom.h" 64 65 /* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB. 66 * COMPRESS_OFFLOAD_FRAGMENT_SIZE * COMPRESS_OFFLOAD_NUM_FRAGMENTS must be less than 8MB. */ 67 #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024) 68 // 2 buffers causes problems with high bitrate files 69 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3 70 /* ToDo: Check and update a proper value in msec */ 71 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 72 /* treat as unsigned Q1.13 */ 73 #define APP_TYPE_GAIN_DEFAULT 0x2000 74 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 75 #define PCM_PLAYBACK_VOLUME_MAX 0x2000 76 #define INVALID_OUT_VOLUME -1 77 78 /* treat as unsigned Q1.13 */ 79 #define VOIP_PLAYBACK_VOLUME_MAX 0x2000 80 81 #define RECORD_GAIN_MIN 0.0f 82 #define RECORD_GAIN_MAX 1.0f 83 #define RECORD_VOLUME_CTL_MAX 0x2000 84 85 #define PROXY_OPEN_RETRY_COUNT 100 86 #define PROXY_OPEN_WAIT_TIME 20 87 88 #define MIN_CHANNEL_COUNT 1 89 #define DEFAULT_CHANNEL_COUNT 2 90 91 #ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT 92 #define MAX_CHANNEL_COUNT 1 93 #else 94 #define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT)) 95 #define XSTR(x) STR(x) 96 #define STR(x) #x 97 #endif 98 #define MAX_HIFI_CHANNEL_COUNT 8 99 100 #define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) 101 102 static unsigned int configured_low_latency_capture_period_size = 103 LOW_LATENCY_CAPTURE_PERIOD_SIZE; 104 105 106 #define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) 107 #define MMAP_PERIOD_COUNT_MIN 32 108 #define MMAP_PERIOD_COUNT_MAX 512 109 #define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX) 110 #define MMAP_MIN_SIZE_FRAMES_MAX 64 * 1024 111 112 /* This constant enables extended precision handling. 113 * TODO The flag is off until more testing is done. 114 */ 115 static const bool k_enable_extended_precision = false; 116 117 struct pcm_config pcm_config_deep_buffer = { 118 .channels = DEFAULT_CHANNEL_COUNT, 119 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 120 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, 121 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, 122 .format = PCM_FORMAT_S16_LE, 123 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 124 .stop_threshold = INT_MAX, 125 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 126 }; 127 128 struct pcm_config pcm_config_low_latency = { 129 .channels = DEFAULT_CHANNEL_COUNT, 130 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 131 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, 132 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, 133 .format = PCM_FORMAT_S16_LE, 134 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 135 .stop_threshold = INT_MAX, 136 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 137 }; 138 139 struct pcm_config pcm_config_haptics_audio = { 140 .channels = 1, 141 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 142 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, 143 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, 144 .format = PCM_FORMAT_S16_LE, 145 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 146 .stop_threshold = INT_MAX, 147 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 148 }; 149 150 struct pcm_config pcm_config_haptics = { 151 .channels = 1, 152 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 153 .period_count = 2, 154 .format = PCM_FORMAT_S16_LE, 155 .stop_threshold = INT_MAX, 156 .avail_min = 0, 157 }; 158 159 static int af_period_multiplier = 4; 160 struct pcm_config pcm_config_rt = { 161 .channels = DEFAULT_CHANNEL_COUNT, 162 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 163 .period_size = ULL_PERIOD_SIZE, //1 ms 164 .period_count = 512, //=> buffer size is 512ms 165 .format = PCM_FORMAT_S16_LE, 166 .start_threshold = ULL_PERIOD_SIZE*8, //8ms 167 .stop_threshold = INT_MAX, 168 .silence_threshold = 0, 169 .silence_size = 0, 170 .avail_min = ULL_PERIOD_SIZE, //1 ms 171 }; 172 173 struct pcm_config pcm_config_hdmi_multi = { 174 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ 175 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ 176 .period_size = HDMI_MULTI_PERIOD_SIZE, 177 .period_count = HDMI_MULTI_PERIOD_COUNT, 178 .format = PCM_FORMAT_S16_LE, 179 .start_threshold = 0, 180 .stop_threshold = INT_MAX, 181 .avail_min = 0, 182 }; 183 184 struct pcm_config pcm_config_mmap_playback = { 185 .channels = DEFAULT_CHANNEL_COUNT, 186 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 187 .period_size = MMAP_PERIOD_SIZE, 188 .period_count = MMAP_PERIOD_COUNT_DEFAULT, 189 .format = PCM_FORMAT_S16_LE, 190 .start_threshold = MMAP_PERIOD_SIZE*8, 191 .stop_threshold = INT32_MAX, 192 .silence_threshold = 0, 193 .silence_size = 0, 194 .avail_min = MMAP_PERIOD_SIZE, //1 ms 195 }; 196 197 struct pcm_config pcm_config_hifi = { 198 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ 199 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ 200 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */ 201 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, 202 .format = PCM_FORMAT_S24_3LE, 203 .start_threshold = 0, 204 .stop_threshold = INT_MAX, 205 .avail_min = 0, 206 }; 207 208 struct pcm_config pcm_config_audio_capture = { 209 .channels = DEFAULT_CHANNEL_COUNT, 210 .period_count = AUDIO_CAPTURE_PERIOD_COUNT, 211 .format = PCM_FORMAT_S16_LE, 212 .stop_threshold = INT_MAX, 213 .avail_min = 0, 214 }; 215 216 struct pcm_config pcm_config_audio_capture_rt = { 217 .channels = DEFAULT_CHANNEL_COUNT, 218 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 219 .period_size = ULL_PERIOD_SIZE, 220 .period_count = 512, 221 .format = PCM_FORMAT_S16_LE, 222 .start_threshold = 0, 223 .stop_threshold = INT_MAX, 224 .silence_threshold = 0, 225 .silence_size = 0, 226 .avail_min = ULL_PERIOD_SIZE, //1 ms 227 }; 228 229 struct pcm_config pcm_config_mmap_capture = { 230 .channels = DEFAULT_CHANNEL_COUNT, 231 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 232 .period_size = MMAP_PERIOD_SIZE, 233 .period_count = MMAP_PERIOD_COUNT_DEFAULT, 234 .format = PCM_FORMAT_S16_LE, 235 .start_threshold = 0, 236 .stop_threshold = INT_MAX, 237 .silence_threshold = 0, 238 .silence_size = 0, 239 .avail_min = MMAP_PERIOD_SIZE, //1 ms 240 }; 241 242 struct pcm_config pcm_config_voip = { 243 .channels = 1, 244 .period_count = 2, 245 .format = PCM_FORMAT_S16_LE, 246 .stop_threshold = INT_MAX, 247 .avail_min = 0, 248 }; 249 250 #define AFE_PROXY_CHANNEL_COUNT 2 251 #define AFE_PROXY_SAMPLING_RATE 48000 252 253 #define AFE_PROXY_PLAYBACK_PERIOD_SIZE 256 254 #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4 255 256 struct pcm_config pcm_config_afe_proxy_playback = { 257 .channels = AFE_PROXY_CHANNEL_COUNT, 258 .rate = AFE_PROXY_SAMPLING_RATE, 259 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE, 260 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT, 261 .format = PCM_FORMAT_S16_LE, 262 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE, 263 .stop_threshold = INT_MAX, 264 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE, 265 }; 266 267 #define AFE_PROXY_RECORD_PERIOD_SIZE 256 268 #define AFE_PROXY_RECORD_PERIOD_COUNT 4 269 270 struct pcm_config pcm_config_afe_proxy_record = { 271 .channels = AFE_PROXY_CHANNEL_COUNT, 272 .rate = AFE_PROXY_SAMPLING_RATE, 273 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE, 274 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT, 275 .format = PCM_FORMAT_S16_LE, 276 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE, 277 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT, 278 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE, 279 }; 280 281 const char * const use_case_table[AUDIO_USECASE_MAX] = { 282 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", 283 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", 284 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback", 285 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback", 286 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", 287 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback", 288 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback", 289 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback", 290 291 [USECASE_AUDIO_RECORD] = "audio-record", 292 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", 293 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record", 294 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record", 295 296 [USECASE_AUDIO_HFP_SCO] = "hfp-sco", 297 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb", 298 299 [USECASE_VOICE_CALL] = "voice-call", 300 [USECASE_VOICE2_CALL] = "voice2-call", 301 [USECASE_VOLTE_CALL] = "volte-call", 302 [USECASE_QCHAT_CALL] = "qchat-call", 303 [USECASE_VOWLAN_CALL] = "vowlan-call", 304 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call", 305 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call", 306 307 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib", 308 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record", 309 310 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback", 311 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record", 312 313 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink", 314 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink", 315 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink", 316 317 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip", 318 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip", 319 320 [USECASE_INCALL_MUSIC_UPLINK] = "incall-music-uplink", 321 [USECASE_INCALL_MUSIC_UPLINK2] = "incall-music-uplink2", 322 323 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback", 324 }; 325 326 327 #define STRING_TO_ENUM(string) { #string, string } 328 329 struct string_to_enum { 330 const char *name; 331 uint32_t value; 332 }; 333 334 static const struct string_to_enum channels_name_to_enum_table[] = { 335 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 336 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 337 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 338 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), 339 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), 340 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), 341 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1), 342 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2), 343 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3), 344 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4), 345 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5), 346 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6), 347 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7), 348 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8), 349 }; 350 351 struct in_effect_list { 352 struct listnode list; 353 effect_handle_t handle; 354 }; 355 356 static int set_voice_volume_l(struct audio_device *adev, float volume); 357 static struct audio_device *adev = NULL; 358 static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER; 359 static unsigned int audio_device_ref_count; 360 //cache last MBDRC cal step level 361 static int last_known_cal_step = -1 ; 362 363 static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore); 364 static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right); 365 static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right); 366 367 static int in_set_microphone_direction(const struct audio_stream_in *stream, 368 audio_microphone_direction_t dir); 369 static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom); 370 may_use_noirq_mode(struct audio_device * adev,audio_usecase_t uc_id,int flags __unused)371 static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id, 372 int flags __unused) 373 { 374 int dir = 0; 375 switch (uc_id) { 376 case USECASE_AUDIO_RECORD_LOW_LATENCY: 377 dir = 1; 378 case USECASE_AUDIO_PLAYBACK_ULL: 379 break; 380 default: 381 return false; 382 } 383 384 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ? 385 PCM_PLAYBACK : PCM_CAPTURE); 386 if (adev->adm_is_noirq_avail) 387 return adev->adm_is_noirq_avail(adev->adm_data, 388 adev->snd_card, dev_id, dir); 389 return false; 390 } 391 register_out_stream(struct stream_out * out)392 static void register_out_stream(struct stream_out *out) 393 { 394 struct audio_device *adev = out->dev; 395 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) 396 return; 397 398 if (!adev->adm_register_output_stream) 399 return; 400 401 adev->adm_register_output_stream(adev->adm_data, 402 out->handle, 403 out->flags); 404 405 if (!adev->adm_set_config) 406 return; 407 408 if (out->realtime) { 409 adev->adm_set_config(adev->adm_data, 410 out->handle, 411 out->pcm, &out->config); 412 } 413 } 414 register_in_stream(struct stream_in * in)415 static void register_in_stream(struct stream_in *in) 416 { 417 struct audio_device *adev = in->dev; 418 if (!adev->adm_register_input_stream) 419 return; 420 421 adev->adm_register_input_stream(adev->adm_data, 422 in->capture_handle, 423 in->flags); 424 425 if (!adev->adm_set_config) 426 return; 427 428 if (in->realtime) { 429 adev->adm_set_config(adev->adm_data, 430 in->capture_handle, 431 in->pcm, 432 &in->config); 433 } 434 } 435 request_out_focus(struct stream_out * out,long ns)436 static void request_out_focus(struct stream_out *out, long ns) 437 { 438 struct audio_device *adev = out->dev; 439 440 if (adev->adm_request_focus_v2) { 441 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns); 442 } else if (adev->adm_request_focus) { 443 adev->adm_request_focus(adev->adm_data, out->handle); 444 } 445 } 446 request_in_focus(struct stream_in * in,long ns)447 static void request_in_focus(struct stream_in *in, long ns) 448 { 449 struct audio_device *adev = in->dev; 450 451 if (adev->adm_request_focus_v2) { 452 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns); 453 } else if (adev->adm_request_focus) { 454 adev->adm_request_focus(adev->adm_data, in->capture_handle); 455 } 456 } 457 release_out_focus(struct stream_out * out,long ns __unused)458 static void release_out_focus(struct stream_out *out, long ns __unused) 459 { 460 struct audio_device *adev = out->dev; 461 462 if (adev->adm_abandon_focus) 463 adev->adm_abandon_focus(adev->adm_data, out->handle); 464 } 465 release_in_focus(struct stream_in * in,long ns __unused)466 static void release_in_focus(struct stream_in *in, long ns __unused) 467 { 468 struct audio_device *adev = in->dev; 469 if (adev->adm_abandon_focus) 470 adev->adm_abandon_focus(adev->adm_data, in->capture_handle); 471 } 472 parse_snd_card_status(struct str_parms * parms,int * card,card_status_t * status)473 static int parse_snd_card_status(struct str_parms * parms, int * card, 474 card_status_t * status) 475 { 476 char value[32]={0}; 477 char state[32]={0}; 478 479 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value)); 480 481 if (ret < 0) 482 return -1; 483 484 // sscanf should be okay as value is of max length 32. 485 // same as sizeof state. 486 if (sscanf(value, "%d,%s", card, state) < 2) 487 return -1; 488 489 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE : 490 CARD_STATUS_OFFLINE; 491 return 0; 492 } 493 494 // always call with adev lock held send_gain_dep_calibration_l()495 void send_gain_dep_calibration_l() { 496 if (last_known_cal_step >= 0) 497 platform_send_gain_dep_cal(adev->platform, last_known_cal_step); 498 } 499 500 __attribute__ ((visibility ("default"))) audio_hw_send_gain_dep_calibration(int level)501 bool audio_hw_send_gain_dep_calibration(int level) { 502 bool ret_val = false; 503 ALOGV("%s: enter ... ", __func__); 504 505 pthread_mutex_lock(&adev_init_lock); 506 507 if (adev != NULL && adev->platform != NULL) { 508 pthread_mutex_lock(&adev->lock); 509 last_known_cal_step = level; 510 send_gain_dep_calibration_l(); 511 pthread_mutex_unlock(&adev->lock); 512 } else { 513 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform"); 514 } 515 516 pthread_mutex_unlock(&adev_init_lock); 517 518 ALOGV("%s: exit with ret_val %d ", __func__, ret_val); 519 return ret_val; 520 } 521 522 #ifdef MAXXAUDIO_QDSP_ENABLED audio_hw_send_ma_parameter(int stream_type,float vol,bool active)523 bool audio_hw_send_ma_parameter(int stream_type, float vol, bool active) 524 { 525 bool ret = false; 526 ALOGV("%s: enter ...", __func__); 527 528 pthread_mutex_lock(&adev_init_lock); 529 530 if (adev != NULL && adev->platform != NULL) { 531 pthread_mutex_lock(&adev->lock); 532 ret = audio_extn_ma_set_state(adev, stream_type, vol, active); 533 pthread_mutex_unlock(&adev->lock); 534 } 535 536 pthread_mutex_unlock(&adev_init_lock); 537 538 ALOGV("%s: exit with ret %d", __func__, ret); 539 return ret; 540 } 541 #else 542 #define audio_hw_send_ma_parameter(stream_type, vol, active) (0) 543 #endif 544 545 __attribute__ ((visibility ("default"))) audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table * mapping_tbl,int table_size)546 int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl, 547 int table_size) { 548 int ret_val = 0; 549 ALOGV("%s: enter ... ", __func__); 550 551 pthread_mutex_lock(&adev_init_lock); 552 if (adev == NULL) { 553 ALOGW("%s: adev is NULL .... ", __func__); 554 goto done; 555 } 556 557 pthread_mutex_lock(&adev->lock); 558 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size); 559 pthread_mutex_unlock(&adev->lock); 560 done: 561 pthread_mutex_unlock(&adev_init_lock); 562 ALOGV("%s: exit ... ", __func__); 563 return ret_val; 564 } 565 is_supported_format(audio_format_t format)566 static bool is_supported_format(audio_format_t format) 567 { 568 switch (format) { 569 case AUDIO_FORMAT_MP3: 570 case AUDIO_FORMAT_AAC_LC: 571 case AUDIO_FORMAT_AAC_HE_V1: 572 case AUDIO_FORMAT_AAC_HE_V2: 573 return true; 574 default: 575 break; 576 } 577 return false; 578 } 579 is_supported_24bits_audiosource(audio_source_t source)580 static bool is_supported_24bits_audiosource(audio_source_t source) 581 { 582 switch (source) { 583 case AUDIO_SOURCE_UNPROCESSED: 584 #ifdef ENABLED_24BITS_CAMCORDER 585 case AUDIO_SOURCE_CAMCORDER: 586 #endif 587 return true; 588 default: 589 break; 590 } 591 return false; 592 } 593 is_mmap_usecase(audio_usecase_t uc_id)594 static inline bool is_mmap_usecase(audio_usecase_t uc_id) 595 { 596 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) || 597 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY); 598 } 599 get_snd_codec_id(audio_format_t format)600 static int get_snd_codec_id(audio_format_t format) 601 { 602 int id = 0; 603 604 switch (format & AUDIO_FORMAT_MAIN_MASK) { 605 case AUDIO_FORMAT_MP3: 606 id = SND_AUDIOCODEC_MP3; 607 break; 608 case AUDIO_FORMAT_AAC: 609 id = SND_AUDIOCODEC_AAC; 610 break; 611 default: 612 ALOGE("%s: Unsupported audio format", __func__); 613 } 614 615 return id; 616 } 617 audio_ssr_status(struct audio_device * adev)618 static int audio_ssr_status(struct audio_device *adev) 619 { 620 int ret = 0; 621 struct mixer_ctl *ctl; 622 const char *mixer_ctl_name = "Audio SSR Status"; 623 624 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 625 ret = mixer_ctl_get_value(ctl, 0); 626 ALOGD("%s: value: %d", __func__, ret); 627 return ret; 628 } 629 stream_app_type_cfg_init(struct stream_app_type_cfg * cfg)630 static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg) 631 { 632 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT; 633 } 634 is_btsco_device(snd_device_t out_snd_device,snd_device_t in_snd_device)635 static bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device) 636 { 637 return out_snd_device == SND_DEVICE_OUT_BT_SCO || 638 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB || 639 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC || 640 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB || 641 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC || 642 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC; 643 644 } 645 is_a2dp_device(snd_device_t out_snd_device)646 static bool is_a2dp_device(snd_device_t out_snd_device) 647 { 648 return out_snd_device == SND_DEVICE_OUT_BT_A2DP; 649 } 650 enable_audio_route(struct audio_device * adev,struct audio_usecase * usecase)651 int enable_audio_route(struct audio_device *adev, 652 struct audio_usecase *usecase) 653 { 654 snd_device_t snd_device; 655 char mixer_path[MIXER_PATH_MAX_LENGTH]; 656 657 if (usecase == NULL) 658 return -EINVAL; 659 660 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 661 662 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY); 663 664 if (usecase->type == PCM_CAPTURE) { 665 struct stream_in *in = usecase->stream.in; 666 struct audio_usecase *uinfo; 667 snd_device = usecase->in_snd_device; 668 669 if (in) { 670 if (in->enable_aec || in->enable_ec_port) { 671 audio_devices_t out_device = AUDIO_DEVICE_OUT_SPEAKER; 672 struct listnode *node; 673 struct audio_usecase *voip_usecase = get_usecase_from_list(adev, 674 USECASE_AUDIO_PLAYBACK_VOIP); 675 if (voip_usecase) { 676 out_device = voip_usecase->stream.out->devices; 677 } else if (adev->primary_output && 678 !adev->primary_output->standby) { 679 out_device = adev->primary_output->devices; 680 } else { 681 list_for_each(node, &adev->usecase_list) { 682 uinfo = node_to_item(node, struct audio_usecase, list); 683 if (uinfo->type != PCM_CAPTURE) { 684 out_device = uinfo->stream.out->devices; 685 break; 686 } 687 } 688 } 689 platform_set_echo_reference(adev, true, out_device); 690 in->ec_opened = true; 691 } 692 } 693 } else 694 snd_device = usecase->out_snd_device; 695 audio_extn_utils_send_app_type_cfg(adev, usecase); 696 audio_extn_ma_set_device(usecase); 697 audio_extn_utils_send_audio_calibration(adev, usecase); 698 699 // we shouldn't truncate mixer_path 700 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)) 701 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__); 702 // this also appends to mixer_path 703 platform_add_backend_name(adev->platform, mixer_path, snd_device); 704 705 ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__, usecase->id, mixer_path); 706 audio_route_apply_and_update_path(adev->audio_route, mixer_path); 707 708 ALOGV("%s: exit", __func__); 709 return 0; 710 } 711 disable_audio_route(struct audio_device * adev,struct audio_usecase * usecase)712 int disable_audio_route(struct audio_device *adev, 713 struct audio_usecase *usecase) 714 { 715 snd_device_t snd_device; 716 char mixer_path[MIXER_PATH_MAX_LENGTH]; 717 718 if (usecase == NULL) 719 return -EINVAL; 720 721 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 722 if (usecase->type == PCM_CAPTURE) 723 snd_device = usecase->in_snd_device; 724 else 725 snd_device = usecase->out_snd_device; 726 727 // we shouldn't truncate mixer_path 728 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)) 729 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__); 730 // this also appends to mixer_path 731 platform_add_backend_name(adev->platform, mixer_path, snd_device); 732 ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path); 733 734 audio_route_reset_and_update_path(adev->audio_route, mixer_path); 735 if (usecase->type == PCM_CAPTURE) { 736 struct stream_in *in = usecase->stream.in; 737 if (in && in->ec_opened) { 738 platform_set_echo_reference(in->dev, false, AUDIO_DEVICE_NONE); 739 in->ec_opened = false; 740 } 741 } 742 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE); 743 744 ALOGV("%s: exit", __func__); 745 return 0; 746 } 747 enable_snd_device(struct audio_device * adev,snd_device_t snd_device)748 int enable_snd_device(struct audio_device *adev, 749 snd_device_t snd_device) 750 { 751 int i, num_devices = 0; 752 snd_device_t new_snd_devices[2]; 753 int ret_val = -EINVAL; 754 if (snd_device < SND_DEVICE_MIN || 755 snd_device >= SND_DEVICE_MAX) { 756 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 757 goto on_error; 758 } 759 760 platform_send_audio_calibration(adev->platform, snd_device); 761 762 if (adev->snd_dev_ref_cnt[snd_device] >= 1) { 763 ALOGV("%s: snd_device(%d: %s) is already active", 764 __func__, snd_device, platform_get_snd_device_name(snd_device)); 765 goto on_success; 766 } 767 768 /* due to the possibility of calibration overwrite between listen 769 and audio, notify sound trigger hal before audio calibration is sent */ 770 audio_extn_sound_trigger_update_device_status(snd_device, 771 ST_EVENT_SND_DEVICE_BUSY); 772 773 if (audio_extn_spkr_prot_is_enabled()) 774 audio_extn_spkr_prot_calib_cancel(adev); 775 776 audio_extn_dsm_feedback_enable(adev, snd_device, true); 777 778 if ((snd_device == SND_DEVICE_OUT_SPEAKER || 779 snd_device == SND_DEVICE_OUT_SPEAKER_SAFE || 780 snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE || 781 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && 782 audio_extn_spkr_prot_is_enabled()) { 783 if (platform_get_snd_device_acdb_id(snd_device) < 0) { 784 goto on_error; 785 } 786 if (audio_extn_spkr_prot_start_processing(snd_device)) { 787 ALOGE("%s: spkr_start_processing failed", __func__); 788 goto on_error; 789 } 790 } else if (platform_can_split_snd_device(snd_device, 791 &num_devices, 792 new_snd_devices) == 0) { 793 for (i = 0; i < num_devices; i++) { 794 enable_snd_device(adev, new_snd_devices[i]); 795 } 796 platform_set_speaker_gain_in_combo(adev, snd_device, true); 797 } else { 798 char device_name[DEVICE_NAME_MAX_SIZE] = {0}; 799 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { 800 ALOGE(" %s: Invalid sound device returned", __func__); 801 goto on_error; 802 } 803 804 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); 805 806 if (is_a2dp_device(snd_device)) { 807 if (audio_extn_a2dp_start_playback() < 0) { 808 ALOGE("%s: failed to configure A2DP control path", __func__); 809 goto on_error; 810 } else { 811 adev->a2dp_started = true; 812 } 813 } 814 815 audio_route_apply_and_update_path(adev->audio_route, device_name); 816 } 817 on_success: 818 adev->snd_dev_ref_cnt[snd_device]++; 819 ret_val = 0; 820 on_error: 821 return ret_val; 822 } 823 disable_snd_device(struct audio_device * adev,snd_device_t snd_device)824 int disable_snd_device(struct audio_device *adev, 825 snd_device_t snd_device) 826 { 827 int i, num_devices = 0; 828 snd_device_t new_snd_devices[2]; 829 830 if (snd_device < SND_DEVICE_MIN || 831 snd_device >= SND_DEVICE_MAX) { 832 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 833 return -EINVAL; 834 } 835 if (adev->snd_dev_ref_cnt[snd_device] <= 0) { 836 ALOGE("%s: device ref cnt is already 0", __func__); 837 return -EINVAL; 838 } 839 audio_extn_tfa_98xx_disable_speaker(snd_device); 840 841 adev->snd_dev_ref_cnt[snd_device]--; 842 if (adev->snd_dev_ref_cnt[snd_device] == 0) { 843 audio_extn_dsm_feedback_enable(adev, snd_device, false); 844 845 if (is_a2dp_device(snd_device)) { 846 audio_extn_a2dp_stop_playback(); 847 adev->a2dp_started = false; 848 } 849 if ((snd_device == SND_DEVICE_OUT_SPEAKER || 850 snd_device == SND_DEVICE_OUT_SPEAKER_SAFE || 851 snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE || 852 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && 853 audio_extn_spkr_prot_is_enabled()) { 854 audio_extn_spkr_prot_stop_processing(snd_device); 855 856 // FIXME b/65363602: bullhead is the only Nexus with audio_extn_spkr_prot_is_enabled() 857 // and does not use speaker swap. As this code causes a problem with device enable ref 858 // counting we remove it for now. 859 // when speaker device is disabled, reset swap. 860 // will be renabled on usecase start 861 // platform_set_swap_channels(adev, false); 862 863 } else if (platform_can_split_snd_device(snd_device, 864 &num_devices, 865 new_snd_devices) == 0) { 866 for (i = 0; i < num_devices; i++) { 867 disable_snd_device(adev, new_snd_devices[i]); 868 } 869 platform_set_speaker_gain_in_combo(adev, snd_device, false); 870 } else { 871 char device_name[DEVICE_NAME_MAX_SIZE] = {0}; 872 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { 873 ALOGE(" %s: Invalid sound device returned", __func__); 874 return -EINVAL; 875 } 876 877 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); 878 audio_route_reset_and_update_path(adev->audio_route, device_name); 879 } 880 audio_extn_sound_trigger_update_device_status(snd_device, 881 ST_EVENT_SND_DEVICE_FREE); 882 } 883 884 return 0; 885 } 886 887 #ifdef DYNAMIC_ECNS_ENABLED send_effect_enable_disable_mixer_ctl(struct audio_device * adev,struct stream_in * in,struct audio_effect_config effect_config,unsigned int param_value)888 static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev, 889 struct stream_in *in, 890 struct audio_effect_config effect_config, 891 unsigned int param_value) 892 { 893 char mixer_ctl_name[] = "Audio Effect"; 894 long set_values[6]; 895 896 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 897 if (!ctl) { 898 ALOGE("%s: Could not get mixer ctl - %s", 899 __func__, mixer_ctl_name); 900 return -EINVAL; 901 } 902 903 set_values[0] = 1; //0:Rx 1:Tx 904 set_values[1] = in->app_type_cfg.app_type; 905 set_values[2] = (long)effect_config.module_id; 906 set_values[3] = (long)effect_config.instance_id; 907 set_values[4] = (long)effect_config.param_id; 908 set_values[5] = param_value; 909 910 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values)); 911 912 return 0; 913 914 } 915 update_effect_param_ecns(struct audio_usecase * usecase,unsigned int module_id,int effect_type,unsigned int * param_value)916 static int update_effect_param_ecns(struct audio_usecase *usecase, 917 unsigned int module_id, int effect_type, 918 unsigned int *param_value) 919 { 920 int ret = 0; 921 struct audio_effect_config other_effect_config; 922 struct stream_in *in = NULL; 923 924 if (!usecase) 925 return -EINVAL; 926 927 in = usecase->stream.in; 928 929 /* Get the effect config data of the other effect */ 930 ret = platform_get_effect_config_data(usecase->in_snd_device, 931 &other_effect_config, 932 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC); 933 if (ret < 0) { 934 ALOGE("%s Failed to get effect params %d", __func__, ret); 935 return ret; 936 } 937 938 if (module_id == other_effect_config.module_id) { 939 //Same module id for AEC/NS. Values need to be combined 940 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) || 941 ((effect_type == EFFECT_NS) && (in->enable_aec))) 942 *param_value |= other_effect_config.param_value; 943 } 944 945 return ret; 946 } 947 enable_disable_effect(struct audio_device * adev,struct stream_in * in,int effect_type,bool enable)948 static int enable_disable_effect(struct audio_device *adev, struct stream_in *in, 949 int effect_type, bool enable) 950 { 951 struct audio_effect_config effect_config; 952 struct audio_usecase *usecase = NULL; 953 int ret = 0; 954 unsigned int param_value = 0; 955 956 if (!in) { 957 ALOGE("%s: Invalid input stream", __func__); 958 return -EINVAL; 959 } 960 961 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable); 962 963 usecase = get_usecase_from_list(adev, in->usecase); 964 965 ret = platform_get_effect_config_data(usecase->in_snd_device, 966 &effect_config, effect_type); 967 if (ret < 0) { 968 ALOGE("%s Failed to get module id %d", __func__, ret); 969 return ret; 970 } 971 ALOGV("%s: module %d app_type %d usecase->id:%d usecase->in_snd_device:%d", 972 __func__, effect_config.module_id, in->app_type_cfg.app_type, 973 usecase->id, usecase->in_snd_device); 974 975 if (enable) 976 param_value = effect_config.param_value; 977 978 /*Special handling for AEC & NS effects Param values need to be 979 updated if module ids are same*/ 980 981 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) { 982 ret = update_effect_param_ecns(usecase, effect_config.module_id, 983 effect_type, ¶m_value); 984 if (ret < 0) 985 return ret; 986 } 987 988 ret = send_effect_enable_disable_mixer_ctl(adev, in, 989 effect_config, param_value); 990 991 return ret; 992 } 993 check_and_enable_effect(struct audio_device * adev)994 static int check_and_enable_effect(struct audio_device *adev) 995 { 996 int ret = 0; 997 998 struct listnode *node; 999 struct stream_in *in = NULL; 1000 1001 list_for_each(node, &adev->usecase_list) 1002 { 1003 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); 1004 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) { 1005 in = usecase->stream.in; 1006 1007 if (in->standby) 1008 continue; 1009 1010 if (in->enable_aec) { 1011 ret = enable_disable_effect(adev, in, EFFECT_AEC, true); 1012 } 1013 1014 if (in->enable_ns && 1015 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 1016 ret = enable_disable_effect(adev, in, EFFECT_NS, true); 1017 } 1018 } 1019 } 1020 1021 return ret; 1022 } 1023 #else 1024 #define enable_disable_effect(w, x, y, z) -ENOSYS 1025 #define check_and_enable_effect(x) -ENOSYS 1026 #endif 1027 1028 /* 1029 legend: 1030 uc - existing usecase 1031 new_uc - new usecase 1032 d1, d11, d2 - SND_DEVICE enums 1033 a1, a2 - corresponding ANDROID device enums 1034 B, B1, B2 - backend strings 1035 1036 case 1 1037 uc->dev d1 (a1) B1 1038 new_uc->dev d1 (a1), d2 (a2) B1, B2 1039 1040 resolution: disable and enable uc->dev on d1 1041 1042 case 2 1043 uc->dev d1 (a1) B1 1044 new_uc->dev d11 (a1) B1 1045 1046 resolution: need to switch uc since d1 and d11 are related 1047 (e.g. speaker and voice-speaker) 1048 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary 1049 1050 case 3 1051 uc->dev d1 (a1) B1 1052 new_uc->dev d2 (a2) B2 1053 1054 resolution: no need to switch uc 1055 1056 case 4 1057 uc->dev d1 (a1) B 1058 new_uc->dev d2 (a2) B 1059 1060 resolution: disable enable uc-dev on d2 since backends match 1061 we cannot enable two streams on two different devices if they 1062 share the same backend. e.g. if offload is on speaker device using 1063 QUAD_MI2S backend and a low-latency stream is started on voice-handset 1064 using the same backend, offload must also be switched to voice-handset. 1065 1066 case 5 1067 uc->dev d1 (a1) B 1068 new_uc->dev d1 (a1), d2 (a2) B 1069 1070 resolution: disable enable uc-dev on d2 since backends match 1071 we cannot enable two streams on two different devices if they 1072 share the same backend. 1073 1074 case 6 1075 uc->dev d1 a1 B1 1076 new_uc->dev d2 a1 B2 1077 1078 resolution: no need to switch 1079 1080 case 7 1081 1082 uc->dev d1 (a1), d2 (a2) B1, B2 1083 new_uc->dev d1 B1 1084 1085 resolution: no need to switch 1086 1087 */ derive_playback_snd_device(struct audio_usecase * uc,struct audio_usecase * new_uc,snd_device_t new_snd_device)1088 static snd_device_t derive_playback_snd_device(struct audio_usecase *uc, 1089 struct audio_usecase *new_uc, 1090 snd_device_t new_snd_device) 1091 { 1092 audio_devices_t a1 = uc->stream.out->devices; 1093 audio_devices_t a2 = new_uc->stream.out->devices; 1094 1095 snd_device_t d1 = uc->out_snd_device; 1096 snd_device_t d2 = new_snd_device; 1097 1098 // Treat as a special case when a1 and a2 are not disjoint 1099 if ((a1 != a2) && (a1 & a2)) { 1100 snd_device_t d3[2]; 1101 int num_devices = 0; 1102 int ret = platform_can_split_snd_device(popcount(a1) > 1 ? d1 : d2, 1103 &num_devices, 1104 d3); 1105 if (ret < 0) { 1106 if (ret != -ENOSYS) { 1107 ALOGW("%s failed to split snd_device %d", 1108 __func__, 1109 popcount(a1) > 1 ? d1 : d2); 1110 } 1111 goto end; 1112 } 1113 1114 // NB: case 7 is hypothetical and isn't a practical usecase yet. 1115 // But if it does happen, we need to give priority to d2 if 1116 // the combo devices active on the existing usecase share a backend. 1117 // This is because we cannot have a usecase active on a combo device 1118 // and a new usecase requests one device in this combo pair. 1119 if (platform_check_backends_match(d3[0], d3[1])) { 1120 return d2; // case 5 1121 } else { 1122 return d1; // case 1 1123 } 1124 } else { 1125 if (platform_check_backends_match(d1, d2)) { 1126 return d2; // case 2, 4 1127 } else { 1128 return d1; // case 6, 3 1129 } 1130 } 1131 1132 end: 1133 return d2; // return whatever was calculated before. 1134 } 1135 check_and_route_playback_usecases(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device)1136 static void check_and_route_playback_usecases(struct audio_device *adev, 1137 struct audio_usecase *uc_info, 1138 snd_device_t snd_device) 1139 { 1140 struct listnode *node; 1141 struct audio_usecase *usecase; 1142 bool switch_device[AUDIO_USECASE_MAX]; 1143 int i, num_uc_to_switch = 0; 1144 1145 bool force_routing = platform_check_and_set_playback_backend_cfg(adev, 1146 uc_info, 1147 snd_device); 1148 1149 /* For a2dp device reconfigure all active sessions 1150 * with new AFE encoder format based on a2dp state 1151 */ 1152 if ((SND_DEVICE_OUT_BT_A2DP == snd_device || 1153 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device || 1154 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) && 1155 audio_extn_a2dp_is_force_device_switch()) { 1156 force_routing = true; 1157 } 1158 1159 /* 1160 * This function is to make sure that all the usecases that are active on 1161 * the hardware codec backend are always routed to any one device that is 1162 * handled by the hardware codec. 1163 * For example, if low-latency and deep-buffer usecases are currently active 1164 * on speaker and out_set_parameters(headset) is received on low-latency 1165 * output, then we have to make sure deep-buffer is also switched to headset, 1166 * because of the limitation that both the devices cannot be enabled 1167 * at the same time as they share the same backend. 1168 */ 1169 /* Disable all the usecases on the shared backend other than the 1170 specified usecase */ 1171 for (i = 0; i < AUDIO_USECASE_MAX; i++) 1172 switch_device[i] = false; 1173 1174 list_for_each(node, &adev->usecase_list) { 1175 usecase = node_to_item(node, struct audio_usecase, list); 1176 if (usecase->type == PCM_CAPTURE || usecase == uc_info) 1177 continue; 1178 1179 if (force_routing || 1180 (usecase->out_snd_device != snd_device && 1181 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND || 1182 usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) && 1183 platform_check_backends_match(snd_device, usecase->out_snd_device))) { 1184 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 1185 __func__, use_case_table[usecase->id], 1186 platform_get_snd_device_name(usecase->out_snd_device)); 1187 disable_audio_route(adev, usecase); 1188 switch_device[usecase->id] = true; 1189 num_uc_to_switch++; 1190 } 1191 } 1192 1193 if (num_uc_to_switch) { 1194 list_for_each(node, &adev->usecase_list) { 1195 usecase = node_to_item(node, struct audio_usecase, list); 1196 if (switch_device[usecase->id]) { 1197 disable_snd_device(adev, usecase->out_snd_device); 1198 } 1199 } 1200 1201 snd_device_t d_device; 1202 list_for_each(node, &adev->usecase_list) { 1203 usecase = node_to_item(node, struct audio_usecase, list); 1204 if (switch_device[usecase->id]) { 1205 d_device = derive_playback_snd_device(usecase, uc_info, 1206 snd_device); 1207 enable_snd_device(adev, d_device); 1208 /* Update the out_snd_device before enabling the audio route */ 1209 usecase->out_snd_device = d_device; 1210 } 1211 } 1212 1213 /* Re-route all the usecases on the shared backend other than the 1214 specified usecase to new snd devices */ 1215 list_for_each(node, &adev->usecase_list) { 1216 usecase = node_to_item(node, struct audio_usecase, list); 1217 if (switch_device[usecase->id] ) { 1218 enable_audio_route(adev, usecase); 1219 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) { 1220 struct stream_out *out = usecase->stream.out; 1221 audio_extn_utils_send_app_type_gain(out->dev, 1222 out->app_type_cfg.app_type, 1223 &out->app_type_cfg.gain[0]); 1224 } 1225 } 1226 } 1227 } 1228 } 1229 check_and_route_capture_usecases(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device)1230 static void check_and_route_capture_usecases(struct audio_device *adev, 1231 struct audio_usecase *uc_info, 1232 snd_device_t snd_device) 1233 { 1234 struct listnode *node; 1235 struct audio_usecase *usecase; 1236 bool switch_device[AUDIO_USECASE_MAX]; 1237 int i, num_uc_to_switch = 0; 1238 1239 platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device); 1240 1241 /* 1242 * This function is to make sure that all the active capture usecases 1243 * are always routed to the same input sound device. 1244 * For example, if audio-record and voice-call usecases are currently 1245 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) 1246 * is received for voice call then we have to make sure that audio-record 1247 * usecase is also switched to earpiece i.e. voice-dmic-ef, 1248 * because of the limitation that two devices cannot be enabled 1249 * at the same time if they share the same backend. 1250 */ 1251 for (i = 0; i < AUDIO_USECASE_MAX; i++) 1252 switch_device[i] = false; 1253 1254 list_for_each(node, &adev->usecase_list) { 1255 usecase = node_to_item(node, struct audio_usecase, list); 1256 if (usecase->type != PCM_PLAYBACK && 1257 usecase != uc_info && 1258 usecase->in_snd_device != snd_device && 1259 ((uc_info->type == VOICE_CALL && 1260 usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL) || 1261 platform_check_backends_match(snd_device,\ 1262 usecase->in_snd_device)) && 1263 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) { 1264 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 1265 __func__, use_case_table[usecase->id], 1266 platform_get_snd_device_name(usecase->in_snd_device)); 1267 disable_audio_route(adev, usecase); 1268 switch_device[usecase->id] = true; 1269 num_uc_to_switch++; 1270 } 1271 } 1272 1273 if (num_uc_to_switch) { 1274 list_for_each(node, &adev->usecase_list) { 1275 usecase = node_to_item(node, struct audio_usecase, list); 1276 if (switch_device[usecase->id]) { 1277 disable_snd_device(adev, usecase->in_snd_device); 1278 } 1279 } 1280 1281 list_for_each(node, &adev->usecase_list) { 1282 usecase = node_to_item(node, struct audio_usecase, list); 1283 if (switch_device[usecase->id]) { 1284 enable_snd_device(adev, snd_device); 1285 } 1286 } 1287 1288 /* Re-route all the usecases on the shared backend other than the 1289 specified usecase to new snd devices */ 1290 list_for_each(node, &adev->usecase_list) { 1291 usecase = node_to_item(node, struct audio_usecase, list); 1292 /* Update the in_snd_device only before enabling the audio route */ 1293 if (switch_device[usecase->id] ) { 1294 usecase->in_snd_device = snd_device; 1295 enable_audio_route(adev, usecase); 1296 } 1297 } 1298 } 1299 } 1300 1301 /* must be called with hw device mutex locked */ read_hdmi_channel_masks(struct stream_out * out)1302 static int read_hdmi_channel_masks(struct stream_out *out) 1303 { 1304 int ret = 0; 1305 int channels = platform_edid_get_max_channels(out->dev->platform); 1306 1307 switch (channels) { 1308 /* 1309 * Do not handle stereo output in Multi-channel cases 1310 * Stereo case is handled in normal playback path 1311 */ 1312 case 6: 1313 ALOGV("%s: HDMI supports 5.1", __func__); 1314 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 1315 break; 1316 case 8: 1317 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); 1318 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 1319 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; 1320 break; 1321 default: 1322 ALOGE("HDMI does not support multi channel playback"); 1323 ret = -ENOSYS; 1324 break; 1325 } 1326 return ret; 1327 } 1328 read_usb_sup_sample_rates(bool is_playback,uint32_t * supported_sample_rates,uint32_t max_rates)1329 static ssize_t read_usb_sup_sample_rates(bool is_playback, 1330 uint32_t *supported_sample_rates, 1331 uint32_t max_rates) 1332 { 1333 ssize_t count = audio_extn_usb_sup_sample_rates(is_playback, 1334 supported_sample_rates, 1335 max_rates); 1336 #if !LOG_NDEBUG 1337 for (ssize_t i=0; i<count; i++) { 1338 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C", 1339 supported_sample_rates[i]); 1340 } 1341 #endif 1342 return count; 1343 } 1344 read_usb_sup_channel_masks(bool is_playback,audio_channel_mask_t * supported_channel_masks,uint32_t max_masks)1345 static int read_usb_sup_channel_masks(bool is_playback, 1346 audio_channel_mask_t *supported_channel_masks, 1347 uint32_t max_masks) 1348 { 1349 int channels = audio_extn_usb_get_max_channels(is_playback); 1350 int channel_count; 1351 uint32_t num_masks = 0; 1352 if (channels > MAX_HIFI_CHANNEL_COUNT) { 1353 channels = MAX_HIFI_CHANNEL_COUNT; 1354 } 1355 if (is_playback) { 1356 // start from 2 channels as framework currently doesn't support mono. 1357 if (channels >= FCC_2) { 1358 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2); 1359 } 1360 for (channel_count = FCC_2; 1361 channel_count <= channels && num_masks < max_masks; 1362 ++channel_count) { 1363 supported_channel_masks[num_masks++] = 1364 audio_channel_mask_for_index_assignment_from_count(channel_count); 1365 } 1366 } else { 1367 // For capture we report all supported channel masks from 1 channel up. 1368 channel_count = MIN_CHANNEL_COUNT; 1369 // audio_channel_in_mask_from_count() does the right conversion to either positional or 1370 // indexed mask 1371 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) { 1372 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE; 1373 if (channel_count <= FCC_2) { 1374 mask = audio_channel_in_mask_from_count(channel_count); 1375 supported_channel_masks[num_masks++] = mask; 1376 } 1377 const audio_channel_mask_t index_mask = 1378 audio_channel_mask_for_index_assignment_from_count(channel_count); 1379 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added. 1380 supported_channel_masks[num_masks++] = index_mask; 1381 } 1382 } 1383 } 1384 #ifdef NDEBUG 1385 for (size_t i = 0; i < num_masks; ++i) { 1386 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__, 1387 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks); 1388 } 1389 #endif 1390 return num_masks; 1391 } 1392 read_usb_sup_formats(bool is_playback __unused,audio_format_t * supported_formats,uint32_t max_formats __unused)1393 static int read_usb_sup_formats(bool is_playback __unused, 1394 audio_format_t *supported_formats, 1395 uint32_t max_formats __unused) 1396 { 1397 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback); 1398 switch (bitwidth) { 1399 case 24: 1400 // XXX : usb.c returns 24 for s24 and s24_le? 1401 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED; 1402 break; 1403 case 32: 1404 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT; 1405 break; 1406 case 16: 1407 default : 1408 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT; 1409 break; 1410 } 1411 ALOGV("%s: %s supported format %d", __func__, 1412 is_playback ? "P" : "C", bitwidth); 1413 return 1; 1414 } 1415 read_usb_sup_params_and_compare(bool is_playback,audio_format_t * format,audio_format_t * supported_formats,uint32_t max_formats,audio_channel_mask_t * mask,audio_channel_mask_t * supported_channel_masks,uint32_t max_masks,uint32_t * rate,uint32_t * supported_sample_rates,uint32_t max_rates)1416 static int read_usb_sup_params_and_compare(bool is_playback, 1417 audio_format_t *format, 1418 audio_format_t *supported_formats, 1419 uint32_t max_formats, 1420 audio_channel_mask_t *mask, 1421 audio_channel_mask_t *supported_channel_masks, 1422 uint32_t max_masks, 1423 uint32_t *rate, 1424 uint32_t *supported_sample_rates, 1425 uint32_t max_rates) { 1426 int ret = 0; 1427 int num_formats; 1428 int num_masks; 1429 int num_rates; 1430 int i; 1431 1432 num_formats = read_usb_sup_formats(is_playback, supported_formats, 1433 max_formats); 1434 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks, 1435 max_masks); 1436 1437 num_rates = read_usb_sup_sample_rates(is_playback, 1438 supported_sample_rates, max_rates); 1439 1440 #define LUT(table, len, what, dflt) \ 1441 for (i=0; i<len && (table[i] != what); i++); \ 1442 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; } 1443 1444 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT); 1445 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE); 1446 LUT(supported_sample_rates, num_rates, *rate, 0); 1447 1448 #undef LUT 1449 return ret < 0 ? -EINVAL : 0; // HACK TBD 1450 } 1451 is_usb_ready(struct audio_device * adev,bool is_playback)1452 static bool is_usb_ready(struct audio_device *adev, bool is_playback) 1453 { 1454 // Check if usb is ready. 1455 // The usb device may have been removed quickly after insertion and hence 1456 // no longer available. This will show up as empty channel masks, or rates. 1457 1458 pthread_mutex_lock(&adev->lock); 1459 uint32_t supported_sample_rate; 1460 1461 // we consider usb ready if we can fetch at least one sample rate. 1462 const bool ready = read_usb_sup_sample_rates( 1463 is_playback, &supported_sample_rate, 1 /* max_rates */) > 0; 1464 pthread_mutex_unlock(&adev->lock); 1465 return ready; 1466 } 1467 get_voice_usecase_id_from_list(struct audio_device * adev)1468 static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev) 1469 { 1470 struct audio_usecase *usecase; 1471 struct listnode *node; 1472 1473 list_for_each(node, &adev->usecase_list) { 1474 usecase = node_to_item(node, struct audio_usecase, list); 1475 if (usecase->type == VOICE_CALL) { 1476 ALOGV("%s: usecase id %d", __func__, usecase->id); 1477 return usecase->id; 1478 } 1479 } 1480 return USECASE_INVALID; 1481 } 1482 get_usecase_from_list(struct audio_device * adev,audio_usecase_t uc_id)1483 struct audio_usecase *get_usecase_from_list(struct audio_device *adev, 1484 audio_usecase_t uc_id) 1485 { 1486 struct audio_usecase *usecase; 1487 struct listnode *node; 1488 1489 list_for_each(node, &adev->usecase_list) { 1490 usecase = node_to_item(node, struct audio_usecase, list); 1491 if (usecase->id == uc_id) 1492 return usecase; 1493 } 1494 return NULL; 1495 } 1496 force_device_switch(struct audio_usecase * usecase)1497 static bool force_device_switch(struct audio_usecase *usecase) 1498 { 1499 if (usecase->type == PCM_CAPTURE || usecase->stream.out == NULL) { 1500 return false; 1501 } 1502 1503 // Force all A2DP output devices to reconfigure for proper AFE encode format 1504 // Also handle a case where in earlier A2DP start failed as A2DP stream was 1505 // in suspended state, hence try to trigger a retry when we again get a routing request. 1506 if ((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && 1507 audio_extn_a2dp_is_force_device_switch()) { 1508 ALOGD("%s: Force A2DP device switch to update new encoder config", __func__); 1509 return true; 1510 } 1511 1512 return false; 1513 } 1514 adev_get_active_input(const struct audio_device * adev)1515 struct stream_in *adev_get_active_input(const struct audio_device *adev) 1516 { 1517 struct listnode *node; 1518 struct stream_in *last_active_in = NULL; 1519 1520 /* Get last added active input. 1521 * TODO: We may use a priority mechanism to pick highest priority active source */ 1522 list_for_each(node, &adev->usecase_list) 1523 { 1524 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); 1525 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) { 1526 last_active_in = usecase->stream.in; 1527 } 1528 } 1529 1530 return last_active_in; 1531 } 1532 get_voice_communication_input(const struct audio_device * adev)1533 struct stream_in *get_voice_communication_input(const struct audio_device *adev) 1534 { 1535 struct listnode *node; 1536 1537 /* First check active inputs with voice communication source and then 1538 * any input if audio mode is in communication */ 1539 list_for_each(node, &adev->usecase_list) 1540 { 1541 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); 1542 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL && 1543 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 1544 return usecase->stream.in; 1545 } 1546 } 1547 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) { 1548 return adev_get_active_input(adev); 1549 } 1550 return NULL; 1551 } 1552 1553 /* 1554 * Aligned with policy.h 1555 */ source_priority(int inputSource)1556 static inline int source_priority(int inputSource) 1557 { 1558 switch (inputSource) { 1559 case AUDIO_SOURCE_VOICE_COMMUNICATION: 1560 return 9; 1561 case AUDIO_SOURCE_CAMCORDER: 1562 return 8; 1563 case AUDIO_SOURCE_VOICE_PERFORMANCE: 1564 return 7; 1565 case AUDIO_SOURCE_UNPROCESSED: 1566 return 6; 1567 case AUDIO_SOURCE_MIC: 1568 return 5; 1569 case AUDIO_SOURCE_ECHO_REFERENCE: 1570 return 4; 1571 case AUDIO_SOURCE_FM_TUNER: 1572 return 3; 1573 case AUDIO_SOURCE_VOICE_RECOGNITION: 1574 return 2; 1575 case AUDIO_SOURCE_HOTWORD: 1576 return 1; 1577 default: 1578 break; 1579 } 1580 return 0; 1581 } 1582 get_priority_input(struct audio_device * adev)1583 static struct stream_in *get_priority_input(struct audio_device *adev) 1584 { 1585 struct listnode *node; 1586 struct audio_usecase *usecase; 1587 int last_priority = 0, priority; 1588 struct stream_in *priority_in = NULL; 1589 struct stream_in *in; 1590 1591 list_for_each(node, &adev->usecase_list) { 1592 usecase = node_to_item(node, struct audio_usecase, list); 1593 if (usecase->type == PCM_CAPTURE) { 1594 in = usecase->stream.in; 1595 if (!in) 1596 continue; 1597 priority = source_priority(in->source); 1598 1599 if (priority > last_priority) { 1600 last_priority = priority; 1601 priority_in = in; 1602 } 1603 } 1604 } 1605 return priority_in; 1606 } 1607 select_devices_with_force_switch(struct audio_device * adev,audio_usecase_t uc_id,bool force_switch)1608 int select_devices_with_force_switch(struct audio_device *adev, 1609 audio_usecase_t uc_id, 1610 bool force_switch) 1611 { 1612 snd_device_t out_snd_device = SND_DEVICE_NONE; 1613 snd_device_t in_snd_device = SND_DEVICE_NONE; 1614 struct audio_usecase *usecase = NULL; 1615 struct audio_usecase *vc_usecase = NULL; 1616 struct audio_usecase *hfp_usecase = NULL; 1617 audio_usecase_t hfp_ucid; 1618 struct listnode *node; 1619 int status = 0; 1620 struct audio_usecase *voip_usecase = get_usecase_from_list(adev, 1621 USECASE_AUDIO_PLAYBACK_VOIP); 1622 1623 usecase = get_usecase_from_list(adev, uc_id); 1624 if (usecase == NULL) { 1625 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); 1626 return -EINVAL; 1627 } 1628 1629 if ((usecase->type == VOICE_CALL) || 1630 (usecase->type == PCM_HFP_CALL)) { 1631 out_snd_device = platform_get_output_snd_device(adev->platform, 1632 usecase->stream.out->devices); 1633 in_snd_device = platform_get_input_snd_device(adev->platform, 1634 NULL, 1635 usecase->stream.out->devices); 1636 usecase->devices = usecase->stream.out->devices; 1637 } else { 1638 /* 1639 * If the voice call is active, use the sound devices of voice call usecase 1640 * so that it would not result any device switch. All the usecases will 1641 * be switched to new device when select_devices() is called for voice call 1642 * usecase. This is to avoid switching devices for voice call when 1643 * check_and_route_playback_usecases() is called below. 1644 */ 1645 if (voice_is_in_call(adev)) { 1646 vc_usecase = get_usecase_from_list(adev, 1647 get_voice_usecase_id_from_list(adev)); 1648 if ((vc_usecase != NULL) && 1649 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || 1650 (vc_usecase->devices == AUDIO_DEVICE_OUT_HEARING_AID) || 1651 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) { 1652 in_snd_device = vc_usecase->in_snd_device; 1653 out_snd_device = vc_usecase->out_snd_device; 1654 } 1655 } else if (audio_extn_hfp_is_active(adev)) { 1656 hfp_ucid = audio_extn_hfp_get_usecase(); 1657 hfp_usecase = get_usecase_from_list(adev, hfp_ucid); 1658 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 1659 in_snd_device = hfp_usecase->in_snd_device; 1660 out_snd_device = hfp_usecase->out_snd_device; 1661 } 1662 } 1663 if (usecase->type == PCM_PLAYBACK) { 1664 usecase->devices = usecase->stream.out->devices; 1665 in_snd_device = SND_DEVICE_NONE; 1666 if (out_snd_device == SND_DEVICE_NONE) { 1667 struct stream_out *voip_out = adev->primary_output; 1668 struct stream_in *voip_in = get_voice_communication_input(adev); 1669 1670 out_snd_device = platform_get_output_snd_device(adev->platform, 1671 usecase->stream.out->devices); 1672 1673 if (voip_usecase) 1674 voip_out = voip_usecase->stream.out; 1675 1676 if (usecase->stream.out == voip_out && voip_in != NULL) { 1677 select_devices(adev, voip_in->usecase); 1678 } 1679 } 1680 } else if (usecase->type == PCM_CAPTURE) { 1681 usecase->devices = usecase->stream.in->device; 1682 out_snd_device = SND_DEVICE_NONE; 1683 if (in_snd_device == SND_DEVICE_NONE) { 1684 audio_devices_t out_device = AUDIO_DEVICE_NONE; 1685 struct stream_in *voip_in = get_voice_communication_input(adev); 1686 struct stream_in *priority_in = NULL; 1687 1688 if (voip_in != NULL) { 1689 struct audio_usecase *voip_usecase = get_usecase_from_list(adev, 1690 USECASE_AUDIO_PLAYBACK_VOIP); 1691 1692 usecase->stream.in->enable_ec_port = false; 1693 1694 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) { 1695 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX; 1696 } else if (voip_usecase) { 1697 out_device = voip_usecase->stream.out->devices; 1698 } else if (adev->primary_output && 1699 !adev->primary_output->standby) { 1700 out_device = adev->primary_output->devices; 1701 } else { 1702 /* forcing speaker o/p device to get matching i/p pair 1703 in case o/p is not routed from same primary HAL */ 1704 out_device = AUDIO_DEVICE_OUT_SPEAKER; 1705 } 1706 priority_in = voip_in; 1707 } else { 1708 /* get the input with the highest priority source*/ 1709 priority_in = get_priority_input(adev); 1710 1711 if (!priority_in) 1712 priority_in = usecase->stream.in; 1713 } 1714 1715 in_snd_device = platform_get_input_snd_device(adev->platform, 1716 priority_in, 1717 out_device); 1718 } 1719 } 1720 } 1721 1722 if (out_snd_device == usecase->out_snd_device && 1723 in_snd_device == usecase->in_snd_device) { 1724 if (!force_device_switch(usecase) && !force_switch) 1725 return 0; 1726 } 1727 1728 if (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_is_ready()) { 1729 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route"); 1730 return 0; 1731 } 1732 1733 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP || 1734 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) && 1735 (!audio_extn_a2dp_is_ready())) { 1736 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__); 1737 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) 1738 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE; 1739 else 1740 out_snd_device = SND_DEVICE_OUT_SPEAKER; 1741 } 1742 1743 if (usecase->id == USECASE_INCALL_MUSIC_UPLINK || 1744 usecase->id == USECASE_INCALL_MUSIC_UPLINK2) { 1745 out_snd_device = SND_DEVICE_OUT_VOICE_MUSIC_TX; 1746 } 1747 1748 if (out_snd_device != SND_DEVICE_NONE && 1749 out_snd_device != adev->last_logged_snd_device[uc_id][0]) { 1750 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", 1751 __func__, 1752 use_case_table[uc_id], 1753 adev->last_logged_snd_device[uc_id][0], 1754 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]), 1755 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ? 1756 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) : 1757 -1, 1758 out_snd_device, 1759 platform_get_snd_device_name(out_snd_device), 1760 platform_get_snd_device_acdb_id(out_snd_device)); 1761 adev->last_logged_snd_device[uc_id][0] = out_snd_device; 1762 } 1763 if (in_snd_device != SND_DEVICE_NONE && 1764 in_snd_device != adev->last_logged_snd_device[uc_id][1]) { 1765 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", 1766 __func__, 1767 use_case_table[uc_id], 1768 adev->last_logged_snd_device[uc_id][1], 1769 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]), 1770 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ? 1771 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) : 1772 -1, 1773 in_snd_device, 1774 platform_get_snd_device_name(in_snd_device), 1775 platform_get_snd_device_acdb_id(in_snd_device)); 1776 adev->last_logged_snd_device[uc_id][1] = in_snd_device; 1777 } 1778 1779 /* 1780 * Limitation: While in call, to do a device switch we need to disable 1781 * and enable both RX and TX devices though one of them is same as current 1782 * device. 1783 */ 1784 if ((usecase->type == VOICE_CALL) && 1785 (usecase->in_snd_device != SND_DEVICE_NONE) && 1786 (usecase->out_snd_device != SND_DEVICE_NONE)) { 1787 status = platform_switch_voice_call_device_pre(adev->platform); 1788 /* Disable sidetone only if voice call already exists */ 1789 if (voice_is_call_state_active(adev)) 1790 voice_set_sidetone(adev, usecase->out_snd_device, false); 1791 } 1792 1793 /* Disable current sound devices */ 1794 if (usecase->out_snd_device != SND_DEVICE_NONE) { 1795 disable_audio_route(adev, usecase); 1796 disable_snd_device(adev, usecase->out_snd_device); 1797 } 1798 1799 if (usecase->in_snd_device != SND_DEVICE_NONE) { 1800 disable_audio_route(adev, usecase); 1801 disable_snd_device(adev, usecase->in_snd_device); 1802 } 1803 1804 /* Applicable only on the targets that has external modem. 1805 * New device information should be sent to modem before enabling 1806 * the devices to reduce in-call device switch time. 1807 */ 1808 if ((usecase->type == VOICE_CALL) && 1809 (usecase->in_snd_device != SND_DEVICE_NONE) && 1810 (usecase->out_snd_device != SND_DEVICE_NONE)) { 1811 status = platform_switch_voice_call_enable_device_config(adev->platform, 1812 out_snd_device, 1813 in_snd_device); 1814 } 1815 1816 /* Enable new sound devices */ 1817 if (out_snd_device != SND_DEVICE_NONE) { 1818 if ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || 1819 (usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) || 1820 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) 1821 check_and_route_playback_usecases(adev, usecase, out_snd_device); 1822 enable_snd_device(adev, out_snd_device); 1823 } 1824 1825 if (in_snd_device != SND_DEVICE_NONE) { 1826 check_and_route_capture_usecases(adev, usecase, in_snd_device); 1827 enable_snd_device(adev, in_snd_device); 1828 } 1829 1830 if (usecase->type == VOICE_CALL) 1831 status = platform_switch_voice_call_device_post(adev->platform, 1832 out_snd_device, 1833 in_snd_device); 1834 1835 usecase->in_snd_device = in_snd_device; 1836 usecase->out_snd_device = out_snd_device; 1837 1838 audio_extn_tfa_98xx_set_mode(); 1839 1840 enable_audio_route(adev, usecase); 1841 1842 /* If input stream is already running the effect needs to be 1843 applied on the new input device that's being enabled here. */ 1844 if (in_snd_device != SND_DEVICE_NONE) 1845 check_and_enable_effect(adev); 1846 1847 /* Applicable only on the targets that has external modem. 1848 * Enable device command should be sent to modem only after 1849 * enabling voice call mixer controls 1850 */ 1851 if (usecase->type == VOICE_CALL) { 1852 status = platform_switch_voice_call_usecase_route_post(adev->platform, 1853 out_snd_device, 1854 in_snd_device); 1855 /* Enable sidetone only if voice call already exists */ 1856 if (voice_is_call_state_active(adev)) 1857 voice_set_sidetone(adev, out_snd_device, true); 1858 } 1859 1860 if (usecase->type != PCM_CAPTURE && voip_usecase) { 1861 struct stream_out *voip_out = voip_usecase->stream.out; 1862 audio_extn_utils_send_app_type_gain(adev, 1863 voip_out->app_type_cfg.app_type, 1864 &voip_out->app_type_cfg.gain[0]); 1865 } 1866 return status; 1867 } 1868 select_devices(struct audio_device * adev,audio_usecase_t uc_id)1869 int select_devices(struct audio_device *adev, 1870 audio_usecase_t uc_id) 1871 { 1872 return select_devices_with_force_switch(adev, uc_id, false); 1873 } 1874 stop_input_stream(struct stream_in * in)1875 static int stop_input_stream(struct stream_in *in) 1876 { 1877 int i, ret = 0; 1878 struct audio_usecase *uc_info; 1879 struct audio_device *adev = in->dev; 1880 struct stream_in *priority_in = NULL; 1881 1882 ALOGV("%s: enter: usecase(%d: %s)", __func__, 1883 in->usecase, use_case_table[in->usecase]); 1884 1885 uc_info = get_usecase_from_list(adev, in->usecase); 1886 if (uc_info == NULL) { 1887 ALOGE("%s: Could not find the usecase (%d) in the list", 1888 __func__, in->usecase); 1889 return -EINVAL; 1890 } 1891 1892 priority_in = get_priority_input(adev); 1893 1894 /* Close in-call recording streams */ 1895 voice_check_and_stop_incall_rec_usecase(adev, in); 1896 1897 /* 1. Disable stream specific mixer controls */ 1898 disable_audio_route(adev, uc_info); 1899 1900 /* 2. Disable the tx device */ 1901 disable_snd_device(adev, uc_info->in_snd_device); 1902 1903 list_remove(&uc_info->list); 1904 free(uc_info); 1905 1906 if (priority_in == in) { 1907 priority_in = get_priority_input(adev); 1908 if (priority_in) 1909 select_devices(adev, priority_in->usecase); 1910 } 1911 1912 ALOGV("%s: exit: status(%d)", __func__, ret); 1913 return ret; 1914 } 1915 start_input_stream(struct stream_in * in)1916 int start_input_stream(struct stream_in *in) 1917 { 1918 /* 1. Enable output device and stream routing controls */ 1919 int ret = 0; 1920 struct audio_usecase *uc_info; 1921 struct audio_device *adev = in->dev; 1922 1923 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); 1924 1925 if (audio_extn_tfa_98xx_is_supported() && !audio_ssr_status(adev)) 1926 return -EIO; 1927 1928 if (in->card_status == CARD_STATUS_OFFLINE || 1929 adev->card_status == CARD_STATUS_OFFLINE) { 1930 ALOGW("in->card_status or adev->card_status offline, try again"); 1931 ret = -EAGAIN; 1932 goto error_config; 1933 } 1934 1935 /* Check if source matches incall recording usecase criteria */ 1936 ret = voice_check_and_set_incall_rec_usecase(adev, in); 1937 if (ret) 1938 goto error_config; 1939 else 1940 ALOGV("%s: usecase(%d)", __func__, in->usecase); 1941 1942 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); 1943 if (in->pcm_device_id < 0) { 1944 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 1945 __func__, in->usecase); 1946 ret = -EINVAL; 1947 goto error_config; 1948 } 1949 1950 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 1951 uc_info->id = in->usecase; 1952 uc_info->type = PCM_CAPTURE; 1953 uc_info->stream.in = in; 1954 uc_info->devices = in->device; 1955 uc_info->in_snd_device = SND_DEVICE_NONE; 1956 uc_info->out_snd_device = SND_DEVICE_NONE; 1957 1958 list_add_tail(&adev->usecase_list, &uc_info->list); 1959 1960 audio_streaming_hint_start(); 1961 audio_extn_perf_lock_acquire(); 1962 1963 select_devices(adev, in->usecase); 1964 1965 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { 1966 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { 1967 ALOGE("%s: pcm stream not ready", __func__); 1968 goto error_open; 1969 } 1970 ret = pcm_start(in->pcm); 1971 if (ret < 0) { 1972 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); 1973 goto error_open; 1974 } 1975 } else { 1976 unsigned int flags = PCM_IN | PCM_MONOTONIC; 1977 unsigned int pcm_open_retry_count = 0; 1978 1979 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) { 1980 flags |= PCM_MMAP | PCM_NOIRQ; 1981 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; 1982 } else if (in->realtime) { 1983 flags |= PCM_MMAP | PCM_NOIRQ; 1984 } 1985 1986 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 1987 __func__, adev->snd_card, in->pcm_device_id, in->config.channels); 1988 1989 while (1) { 1990 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, 1991 flags, &in->config); 1992 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { 1993 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); 1994 if (in->pcm != NULL) { 1995 pcm_close(in->pcm); 1996 in->pcm = NULL; 1997 } 1998 if (pcm_open_retry_count-- == 0) { 1999 ret = -EIO; 2000 goto error_open; 2001 } 2002 usleep(PROXY_OPEN_WAIT_TIME * 1000); 2003 continue; 2004 } 2005 break; 2006 } 2007 2008 ALOGV("%s: pcm_prepare", __func__); 2009 ret = pcm_prepare(in->pcm); 2010 if (ret < 0) { 2011 ALOGE("%s: pcm_prepare returned %d", __func__, ret); 2012 pcm_close(in->pcm); 2013 in->pcm = NULL; 2014 goto error_open; 2015 } 2016 if (in->realtime) { 2017 ret = pcm_start(in->pcm); 2018 if (ret < 0) { 2019 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); 2020 pcm_close(in->pcm); 2021 in->pcm = NULL; 2022 goto error_open; 2023 } 2024 } 2025 } 2026 register_in_stream(in); 2027 check_and_enable_effect(adev); 2028 audio_extn_audiozoom_set_microphone_direction(in, in->zoom); 2029 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction); 2030 audio_streaming_hint_end(); 2031 audio_extn_perf_lock_release(); 2032 ALOGV("%s: exit", __func__); 2033 2034 return 0; 2035 2036 error_open: 2037 stop_input_stream(in); 2038 audio_streaming_hint_end(); 2039 audio_extn_perf_lock_release(); 2040 2041 error_config: 2042 ALOGW("%s: exit: status(%d)", __func__, ret); 2043 return ret; 2044 } 2045 lock_input_stream(struct stream_in * in)2046 void lock_input_stream(struct stream_in *in) 2047 { 2048 pthread_mutex_lock(&in->pre_lock); 2049 pthread_mutex_lock(&in->lock); 2050 pthread_mutex_unlock(&in->pre_lock); 2051 } 2052 lock_output_stream(struct stream_out * out)2053 void lock_output_stream(struct stream_out *out) 2054 { 2055 pthread_mutex_lock(&out->pre_lock); 2056 pthread_mutex_lock(&out->lock); 2057 pthread_mutex_unlock(&out->pre_lock); 2058 } 2059 2060 /* must be called with out->lock locked */ send_offload_cmd_l(struct stream_out * out,int command)2061 static int send_offload_cmd_l(struct stream_out* out, int command) 2062 { 2063 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); 2064 2065 ALOGVV("%s %d", __func__, command); 2066 2067 cmd->cmd = command; 2068 list_add_tail(&out->offload_cmd_list, &cmd->node); 2069 pthread_cond_signal(&out->offload_cond); 2070 return 0; 2071 } 2072 2073 /* must be called iwth out->lock locked */ stop_compressed_output_l(struct stream_out * out)2074 static void stop_compressed_output_l(struct stream_out *out) 2075 { 2076 out->offload_state = OFFLOAD_STATE_IDLE; 2077 out->playback_started = 0; 2078 out->send_new_metadata = 1; 2079 if (out->compr != NULL) { 2080 compress_stop(out->compr); 2081 while (out->offload_thread_blocked) { 2082 pthread_cond_wait(&out->cond, &out->lock); 2083 } 2084 } 2085 } 2086 offload_thread_loop(void * context)2087 static void *offload_thread_loop(void *context) 2088 { 2089 struct stream_out *out = (struct stream_out *) context; 2090 struct listnode *item; 2091 2092 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); 2093 set_sched_policy(0, SP_FOREGROUND); 2094 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); 2095 2096 ALOGV("%s", __func__); 2097 2098 lock_output_stream(out); 2099 out->offload_state = OFFLOAD_STATE_IDLE; 2100 out->playback_started = 0; 2101 for (;;) { 2102 struct offload_cmd *cmd = NULL; 2103 stream_callback_event_t event; 2104 bool send_callback = false; 2105 2106 ALOGVV("%s offload_cmd_list %d out->offload_state %d", 2107 __func__, list_empty(&out->offload_cmd_list), 2108 out->offload_state); 2109 if (list_empty(&out->offload_cmd_list)) { 2110 ALOGV("%s SLEEPING", __func__); 2111 pthread_cond_wait(&out->offload_cond, &out->lock); 2112 ALOGV("%s RUNNING", __func__); 2113 continue; 2114 } 2115 2116 item = list_head(&out->offload_cmd_list); 2117 cmd = node_to_item(item, struct offload_cmd, node); 2118 list_remove(item); 2119 2120 ALOGVV("%s STATE %d CMD %d out->compr %p", 2121 __func__, out->offload_state, cmd->cmd, out->compr); 2122 2123 if (cmd->cmd == OFFLOAD_CMD_EXIT) { 2124 free(cmd); 2125 break; 2126 } 2127 2128 if (out->compr == NULL) { 2129 ALOGE("%s: Compress handle is NULL", __func__); 2130 free(cmd); 2131 pthread_cond_signal(&out->cond); 2132 continue; 2133 } 2134 out->offload_thread_blocked = true; 2135 pthread_mutex_unlock(&out->lock); 2136 send_callback = false; 2137 switch (cmd->cmd) { 2138 case OFFLOAD_CMD_WAIT_FOR_BUFFER: 2139 compress_wait(out->compr, -1); 2140 send_callback = true; 2141 event = STREAM_CBK_EVENT_WRITE_READY; 2142 break; 2143 case OFFLOAD_CMD_PARTIAL_DRAIN: 2144 compress_next_track(out->compr); 2145 compress_partial_drain(out->compr); 2146 send_callback = true; 2147 event = STREAM_CBK_EVENT_DRAIN_READY; 2148 /* Resend the metadata for next iteration */ 2149 out->send_new_metadata = 1; 2150 break; 2151 case OFFLOAD_CMD_DRAIN: 2152 compress_drain(out->compr); 2153 send_callback = true; 2154 event = STREAM_CBK_EVENT_DRAIN_READY; 2155 break; 2156 case OFFLOAD_CMD_ERROR: 2157 send_callback = true; 2158 event = STREAM_CBK_EVENT_ERROR; 2159 break; 2160 default: 2161 ALOGE("%s unknown command received: %d", __func__, cmd->cmd); 2162 break; 2163 } 2164 lock_output_stream(out); 2165 out->offload_thread_blocked = false; 2166 pthread_cond_signal(&out->cond); 2167 if (send_callback) { 2168 ALOGVV("%s: sending offload_callback event %d", __func__, event); 2169 out->offload_callback(event, NULL, out->offload_cookie); 2170 } 2171 free(cmd); 2172 } 2173 2174 pthread_cond_signal(&out->cond); 2175 while (!list_empty(&out->offload_cmd_list)) { 2176 item = list_head(&out->offload_cmd_list); 2177 list_remove(item); 2178 free(node_to_item(item, struct offload_cmd, node)); 2179 } 2180 pthread_mutex_unlock(&out->lock); 2181 2182 return NULL; 2183 } 2184 create_offload_callback_thread(struct stream_out * out)2185 static int create_offload_callback_thread(struct stream_out *out) 2186 { 2187 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); 2188 list_init(&out->offload_cmd_list); 2189 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, 2190 offload_thread_loop, out); 2191 return 0; 2192 } 2193 destroy_offload_callback_thread(struct stream_out * out)2194 static int destroy_offload_callback_thread(struct stream_out *out) 2195 { 2196 lock_output_stream(out); 2197 stop_compressed_output_l(out); 2198 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); 2199 2200 pthread_mutex_unlock(&out->lock); 2201 pthread_join(out->offload_thread, (void **) NULL); 2202 pthread_cond_destroy(&out->offload_cond); 2203 2204 return 0; 2205 } 2206 allow_hdmi_channel_config(struct audio_device * adev)2207 static bool allow_hdmi_channel_config(struct audio_device *adev) 2208 { 2209 struct listnode *node; 2210 struct audio_usecase *usecase; 2211 bool ret = true; 2212 2213 list_for_each(node, &adev->usecase_list) { 2214 usecase = node_to_item(node, struct audio_usecase, list); 2215 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 2216 /* 2217 * If voice call is already existing, do not proceed further to avoid 2218 * disabling/enabling both RX and TX devices, CSD calls, etc. 2219 * Once the voice call done, the HDMI channels can be configured to 2220 * max channels of remaining use cases. 2221 */ 2222 if (usecase->id == USECASE_VOICE_CALL) { 2223 ALOGV("%s: voice call is active, no change in HDMI channels", 2224 __func__); 2225 ret = false; 2226 break; 2227 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_HIFI) { 2228 ALOGV("%s: hifi playback is active, " 2229 "no change in HDMI channels", __func__); 2230 ret = false; 2231 break; 2232 } 2233 } 2234 } 2235 return ret; 2236 } 2237 check_and_set_hdmi_channels(struct audio_device * adev,unsigned int channels)2238 static int check_and_set_hdmi_channels(struct audio_device *adev, 2239 unsigned int channels) 2240 { 2241 struct listnode *node; 2242 struct audio_usecase *usecase; 2243 2244 /* Check if change in HDMI channel config is allowed */ 2245 if (!allow_hdmi_channel_config(adev)) 2246 return 0; 2247 2248 if (channels == adev->cur_hdmi_channels) { 2249 ALOGV("%s: Requested channels are same as current", __func__); 2250 return 0; 2251 } 2252 2253 platform_set_hdmi_channels(adev->platform, channels); 2254 adev->cur_hdmi_channels = channels; 2255 2256 /* 2257 * Deroute all the playback streams routed to HDMI so that 2258 * the back end is deactivated. Note that backend will not 2259 * be deactivated if any one stream is connected to it. 2260 */ 2261 list_for_each(node, &adev->usecase_list) { 2262 usecase = node_to_item(node, struct audio_usecase, list); 2263 if (usecase->type == PCM_PLAYBACK && 2264 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 2265 disable_audio_route(adev, usecase); 2266 } 2267 } 2268 2269 /* 2270 * Enable all the streams disabled above. Now the HDMI backend 2271 * will be activated with new channel configuration 2272 */ 2273 list_for_each(node, &adev->usecase_list) { 2274 usecase = node_to_item(node, struct audio_usecase, list); 2275 if (usecase->type == PCM_PLAYBACK && 2276 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 2277 enable_audio_route(adev, usecase); 2278 } 2279 } 2280 2281 return 0; 2282 } 2283 check_and_set_usb_service_interval(struct audio_device * adev,struct audio_usecase * uc_info,bool min)2284 static int check_and_set_usb_service_interval(struct audio_device *adev, 2285 struct audio_usecase *uc_info, 2286 bool min) 2287 { 2288 struct listnode *node; 2289 struct audio_usecase *usecase; 2290 bool switch_usecases = false; 2291 bool reconfig = false; 2292 2293 if ((uc_info->id != USECASE_AUDIO_PLAYBACK_MMAP) && 2294 (uc_info->id != USECASE_AUDIO_PLAYBACK_ULL)) 2295 return -1; 2296 2297 /* set if the valid usecase do not already exist */ 2298 list_for_each(node, &adev->usecase_list) { 2299 usecase = node_to_item(node, struct audio_usecase, list); 2300 if (usecase->type == PCM_PLAYBACK && 2301 (audio_is_usb_out_device(usecase->devices & AUDIO_DEVICE_OUT_ALL_USB))) { 2302 switch (usecase->id) { 2303 case USECASE_AUDIO_PLAYBACK_MMAP: 2304 case USECASE_AUDIO_PLAYBACK_ULL: 2305 // cannot reconfig while mmap/ull is present. 2306 return -1; 2307 default: 2308 switch_usecases = true; 2309 break; 2310 } 2311 } 2312 if (switch_usecases) 2313 break; 2314 } 2315 /* 2316 * client can try to set service interval in start_output_stream 2317 * to min or to 0 (i.e reset) in stop_output_stream . 2318 */ 2319 unsigned long service_interval = 2320 audio_extn_usb_find_service_interval(min, true /*playback*/); 2321 int ret = platform_set_usb_service_interval(adev->platform, 2322 true /*playback*/, 2323 service_interval, 2324 &reconfig); 2325 /* no change or not supported or no active usecases */ 2326 if (ret || !reconfig || !switch_usecases) 2327 return -1; 2328 return 0; 2329 #undef VALID_USECASE 2330 } 2331 stop_output_stream(struct stream_out * out)2332 static int stop_output_stream(struct stream_out *out) 2333 { 2334 int i, ret = 0; 2335 struct audio_usecase *uc_info; 2336 struct audio_device *adev = out->dev; 2337 2338 ALOGV("%s: enter: usecase(%d: %s)", __func__, 2339 out->usecase, use_case_table[out->usecase]); 2340 uc_info = get_usecase_from_list(adev, out->usecase); 2341 if (uc_info == NULL) { 2342 ALOGE("%s: Could not find the usecase (%d) in the list", 2343 __func__, out->usecase); 2344 return -EINVAL; 2345 } 2346 2347 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2348 if (adev->visualizer_stop_output != NULL) 2349 adev->visualizer_stop_output(out->handle, out->pcm_device_id); 2350 if (adev->offload_effects_stop_output != NULL) 2351 adev->offload_effects_stop_output(out->handle, out->pcm_device_id); 2352 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || 2353 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { 2354 audio_low_latency_hint_end(); 2355 } 2356 2357 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK || 2358 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) { 2359 voice_set_device_mute_flag(adev, false); 2360 } 2361 2362 /* 1. Get and set stream specific mixer controls */ 2363 disable_audio_route(adev, uc_info); 2364 2365 /* 2. Disable the rx device */ 2366 disable_snd_device(adev, uc_info->out_snd_device); 2367 2368 list_remove(&uc_info->list); 2369 2370 audio_extn_extspk_update(adev->extspk); 2371 2372 /* Must be called after removing the usecase from list */ 2373 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) 2374 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS); 2375 else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) { 2376 ret = check_and_set_usb_service_interval(adev, uc_info, false /*min*/); 2377 if (ret == 0) { 2378 /* default service interval was successfully updated, 2379 reopen USB backend with new service interval */ 2380 check_and_route_playback_usecases(adev, uc_info, uc_info->out_snd_device); 2381 } 2382 ret = 0; 2383 } 2384 /* 1) media + voip output routing to handset must route media back to 2385 speaker when voip stops. 2386 2) trigger voip input to reroute when voip output changes to 2387 hearing aid. */ 2388 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP || 2389 out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { 2390 struct listnode *node; 2391 struct audio_usecase *usecase; 2392 list_for_each(node, &adev->usecase_list) { 2393 usecase = node_to_item(node, struct audio_usecase, list); 2394 if ((usecase->type == PCM_CAPTURE && 2395 usecase->id != USECASE_AUDIO_RECORD_VOIP) 2396 || usecase == uc_info) 2397 continue; 2398 2399 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)", 2400 __func__, usecase->id, use_case_table[usecase->id], 2401 out->usecase, use_case_table[out->usecase]); 2402 select_devices(adev, usecase->id); 2403 } 2404 } 2405 2406 free(uc_info); 2407 ALOGV("%s: exit: status(%d)", __func__, ret); 2408 return ret; 2409 } 2410 pcm_open_prepare_helper(unsigned int snd_card,unsigned int pcm_device_id,unsigned int flags,unsigned int pcm_open_retry_count,struct pcm_config * config)2411 struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id, 2412 unsigned int flags, unsigned int pcm_open_retry_count, 2413 struct pcm_config *config) 2414 { 2415 struct pcm* pcm = NULL; 2416 2417 while (1) { 2418 pcm = pcm_open(snd_card, pcm_device_id, flags, config); 2419 if (pcm == NULL || !pcm_is_ready(pcm)) { 2420 ALOGE("%s: %s", __func__, pcm_get_error(pcm)); 2421 if (pcm != NULL) { 2422 pcm_close(pcm); 2423 pcm = NULL; 2424 } 2425 if (pcm_open_retry_count-- == 0) 2426 return NULL; 2427 2428 usleep(PROXY_OPEN_WAIT_TIME * 1000); 2429 continue; 2430 } 2431 break; 2432 } 2433 2434 if (pcm_is_ready(pcm)) { 2435 int ret = pcm_prepare(pcm); 2436 if (ret < 0) { 2437 ALOGE("%s: pcm_prepare returned %d", __func__, ret); 2438 pcm_close(pcm); 2439 pcm = NULL; 2440 } 2441 } 2442 2443 return pcm; 2444 } 2445 start_output_stream(struct stream_out * out)2446 int start_output_stream(struct stream_out *out) 2447 { 2448 int ret = 0; 2449 struct audio_usecase *uc_info; 2450 struct audio_device *adev = out->dev; 2451 bool a2dp_combo = false; 2452 2453 ALOGV("%s: enter: usecase(%d: %s) %s devices(%#x)", 2454 __func__, out->usecase, use_case_table[out->usecase], 2455 out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS ? "(with haptics)" : "", 2456 out->devices); 2457 2458 if (out->card_status == CARD_STATUS_OFFLINE || 2459 adev->card_status == CARD_STATUS_OFFLINE) { 2460 ALOGW("out->card_status or adev->card_status offline, try again"); 2461 ret = -EAGAIN; 2462 goto error_config; 2463 } 2464 2465 //Update incall music usecase to reflect correct voice session 2466 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) { 2467 ret = voice_extn_check_and_set_incall_music_usecase(adev, out); 2468 if (ret != 0) { 2469 ALOGE("%s: Incall music delivery usecase cannot be set error:%d", 2470 __func__, ret); 2471 goto error_config; 2472 } 2473 } 2474 2475 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) { 2476 if (out->devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { 2477 a2dp_combo = true; 2478 } else if (!audio_extn_a2dp_is_ready() && 2479 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { 2480 ALOGE("%s: A2DP profile is not ready, return error", __func__); 2481 ret = -EAGAIN; 2482 goto error_config; 2483 } 2484 } 2485 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); 2486 if (out->pcm_device_id < 0) { 2487 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 2488 __func__, out->pcm_device_id, out->usecase); 2489 ret = -EINVAL; 2490 goto error_config; 2491 } 2492 2493 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 2494 uc_info->id = out->usecase; 2495 uc_info->type = PCM_PLAYBACK; 2496 uc_info->stream.out = out; 2497 uc_info->devices = out->devices; 2498 uc_info->in_snd_device = SND_DEVICE_NONE; 2499 uc_info->out_snd_device = SND_DEVICE_NONE; 2500 2501 /* This must be called before adding this usecase to the list */ 2502 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) 2503 check_and_set_hdmi_channels(adev, out->config.channels); 2504 else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) { 2505 check_and_set_usb_service_interval(adev, uc_info, true /*min*/); 2506 /* USB backend is not reopened immediately. 2507 This is eventually done as part of select_devices */ 2508 } 2509 2510 list_add_tail(&adev->usecase_list, &uc_info->list); 2511 2512 audio_streaming_hint_start(); 2513 audio_extn_perf_lock_acquire(); 2514 2515 if (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || 2516 audio_extn_a2dp_is_ready()) { 2517 select_devices(adev, out->usecase); 2518 } 2519 2520 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && 2521 (!audio_extn_a2dp_is_ready() || !adev->a2dp_started)) { 2522 if (a2dp_combo) { 2523 audio_devices_t dev = out->devices; 2524 if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE) 2525 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE; 2526 else 2527 out->devices = AUDIO_DEVICE_OUT_SPEAKER; 2528 select_devices(adev, out->usecase); 2529 out->devices = dev; 2530 } else if (!audio_extn_a2dp_is_ready()) { 2531 check_a2dp_restore_l(adev, out, false); 2532 } else { 2533 ALOGE("%s: A2DP is not started, return error", __func__); 2534 ret = -EINVAL; 2535 goto error_open; 2536 } 2537 } 2538 2539 audio_extn_extspk_update(adev->extspk); 2540 2541 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK || 2542 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) { 2543 voice_set_device_mute_flag(adev, true); 2544 } 2545 2546 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)", 2547 __func__, adev->snd_card, out->pcm_device_id, out->config.format); 2548 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2549 out->pcm = NULL; 2550 out->compr = compress_open(adev->snd_card, out->pcm_device_id, 2551 COMPRESS_IN, &out->compr_config); 2552 if (out->compr && !is_compress_ready(out->compr)) { 2553 ALOGE("%s: %s", __func__, compress_get_error(out->compr)); 2554 compress_close(out->compr); 2555 out->compr = NULL; 2556 ret = -EIO; 2557 goto error_open; 2558 } 2559 if (out->offload_callback) 2560 compress_nonblock(out->compr, out->non_blocking); 2561 2562 if (adev->visualizer_start_output != NULL) { 2563 int capture_device_id = 2564 platform_get_pcm_device_id(USECASE_AUDIO_RECORD_AFE_PROXY, 2565 PCM_CAPTURE); 2566 adev->visualizer_start_output(out->handle, out->pcm_device_id, 2567 adev->snd_card, capture_device_id); 2568 } 2569 if (adev->offload_effects_start_output != NULL) 2570 adev->offload_effects_start_output(out->handle, out->pcm_device_id); 2571 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { 2572 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { 2573 ALOGE("%s: pcm stream not ready", __func__); 2574 goto error_open; 2575 } 2576 ret = pcm_start(out->pcm); 2577 if (ret < 0) { 2578 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); 2579 goto error_open; 2580 } 2581 } else { 2582 unsigned int flags = PCM_OUT | PCM_MONOTONIC; 2583 unsigned int pcm_open_retry_count = 0; 2584 2585 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) { 2586 flags |= PCM_MMAP | PCM_NOIRQ; 2587 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; 2588 } else if (out->realtime) { 2589 flags |= PCM_MMAP | PCM_NOIRQ; 2590 } 2591 2592 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id, 2593 flags, pcm_open_retry_count, 2594 &(out->config)); 2595 if (out->pcm == NULL) { 2596 ret = -EIO; 2597 goto error_open; 2598 } 2599 2600 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) { 2601 if (adev->haptic_pcm != NULL) { 2602 pcm_close(adev->haptic_pcm); 2603 adev->haptic_pcm = NULL; 2604 } 2605 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card, 2606 adev->haptic_pcm_device_id, 2607 flags, pcm_open_retry_count, 2608 &(adev->haptics_config)); 2609 // failure to open haptics pcm shouldnt stop audio, 2610 // so do not close audio pcm in case of error 2611 } 2612 2613 if (out->realtime) { 2614 ret = pcm_start(out->pcm); 2615 if (ret < 0) { 2616 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); 2617 pcm_close(out->pcm); 2618 out->pcm = NULL; 2619 goto error_open; 2620 } 2621 } 2622 if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY 2623 || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER 2624 || out->usecase == USECASE_AUDIO_PLAYBACK_ULL)) { 2625 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r); 2626 } 2627 } 2628 2629 register_out_stream(out); 2630 audio_streaming_hint_end(); 2631 audio_extn_perf_lock_release(); 2632 audio_extn_tfa_98xx_enable_speaker(); 2633 2634 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || 2635 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { 2636 audio_low_latency_hint_start(); 2637 } 2638 2639 // consider a scenario where on pause lower layers are tear down. 2640 // so on resume, swap mixer control need to be sent only when 2641 // backend is active, hence rather than sending from enable device 2642 // sending it from start of stream 2643 2644 platform_set_swap_channels(adev, true); 2645 2646 ALOGV("%s: exit", __func__); 2647 return 0; 2648 error_open: 2649 if (adev->haptic_pcm) { 2650 pcm_close(adev->haptic_pcm); 2651 adev->haptic_pcm = NULL; 2652 } 2653 audio_streaming_hint_end(); 2654 audio_extn_perf_lock_release(); 2655 stop_output_stream(out); 2656 error_config: 2657 return ret; 2658 } 2659 check_input_parameters(uint32_t sample_rate,audio_format_t format,int channel_count,bool is_usb_hifi)2660 static int check_input_parameters(uint32_t sample_rate, 2661 audio_format_t format, 2662 int channel_count, bool is_usb_hifi) 2663 { 2664 if ((format != AUDIO_FORMAT_PCM_16_BIT) && 2665 (format != AUDIO_FORMAT_PCM_8_24_BIT) && 2666 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && 2667 !(is_usb_hifi && (format == AUDIO_FORMAT_PCM_32_BIT))) { 2668 ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format); 2669 return -EINVAL; 2670 } 2671 2672 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT; 2673 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) { 2674 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__, 2675 channel_count, MIN_CHANNEL_COUNT, max_channel_count); 2676 return -EINVAL; 2677 } 2678 2679 switch (sample_rate) { 2680 case 8000: 2681 case 11025: 2682 case 12000: 2683 case 16000: 2684 case 22050: 2685 case 24000: 2686 case 32000: 2687 case 44100: 2688 case 48000: 2689 case 96000: 2690 break; 2691 default: 2692 ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate); 2693 return -EINVAL; 2694 } 2695 2696 return 0; 2697 } 2698 2699 /** Add a value in a list if not already present. 2700 * @return true if value was successfully inserted or already present, 2701 * false if the list is full and does not contain the value. 2702 */ register_uint(uint32_t value,uint32_t * list,size_t list_length)2703 static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) { 2704 for (size_t i = 0; i < list_length; i++) { 2705 if (list[i] == value) return true; // value is already present 2706 if (list[i] == 0) { // no values in this slot 2707 list[i] = value; 2708 return true; // value inserted 2709 } 2710 } 2711 return false; // could not insert value 2712 } 2713 2714 /** Add channel_mask in supported_channel_masks if not already present. 2715 * @return true if channel_mask was successfully inserted or already present, 2716 * false if supported_channel_masks is full and does not contain channel_mask. 2717 */ register_channel_mask(audio_channel_mask_t channel_mask,audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS])2718 static void register_channel_mask(audio_channel_mask_t channel_mask, 2719 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) { 2720 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS), 2721 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask); 2722 } 2723 2724 /** Add format in supported_formats if not already present. 2725 * @return true if format was successfully inserted or already present, 2726 * false if supported_formats is full and does not contain format. 2727 */ register_format(audio_format_t format,audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS])2728 static void register_format(audio_format_t format, 2729 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) { 2730 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS), 2731 "%s: stream can not declare supporting its format %x", __func__, format); 2732 } 2733 /** Add sample_rate in supported_sample_rates if not already present. 2734 * @return true if sample_rate was successfully inserted or already present, 2735 * false if supported_sample_rates is full and does not contain sample_rate. 2736 */ register_sample_rate(uint32_t sample_rate,uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES])2737 static void register_sample_rate(uint32_t sample_rate, 2738 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) { 2739 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES), 2740 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate); 2741 } 2742 get_stream_buffer_size(size_t duration_ms,uint32_t sample_rate,audio_format_t format,int channel_count,bool is_low_latency)2743 static size_t get_stream_buffer_size(size_t duration_ms, 2744 uint32_t sample_rate, 2745 audio_format_t format, 2746 int channel_count, 2747 bool is_low_latency) 2748 { 2749 // Compute target frames based on time or period size. 2750 size_t target_frames = is_low_latency 2751 ? configured_low_latency_capture_period_size // record only 2752 : (sample_rate * duration_ms) / 1000; 2753 2754 // Round up to a multiple of 16 frames in case sizing for the MixerThread. 2755 if (!is_low_latency) { // low latency flag set for record only 2756 target_frames = (target_frames + 0xf) & ~0xf; 2757 } 2758 2759 // Buffer size is the target frames multiplied by the frame size in bytes. 2760 const size_t frame_size = channel_count * audio_bytes_per_sample(format); 2761 const size_t buffer_size = target_frames * frame_size; 2762 2763 return buffer_size; 2764 } 2765 out_get_sample_rate(const struct audio_stream * stream)2766 static uint32_t out_get_sample_rate(const struct audio_stream *stream) 2767 { 2768 struct stream_out *out = (struct stream_out *)stream; 2769 2770 return out->sample_rate; 2771 } 2772 out_set_sample_rate(struct audio_stream * stream __unused,uint32_t rate __unused)2773 static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) 2774 { 2775 return -ENOSYS; 2776 } 2777 out_get_buffer_size(const struct audio_stream * stream)2778 static size_t out_get_buffer_size(const struct audio_stream *stream) 2779 { 2780 struct stream_out *out = (struct stream_out *)stream; 2781 2782 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2783 return out->compr_config.fragment_size; 2784 } 2785 return out->config.period_size * out->af_period_multiplier * 2786 audio_stream_out_frame_size((const struct audio_stream_out *)stream); 2787 } 2788 out_get_channels(const struct audio_stream * stream)2789 static uint32_t out_get_channels(const struct audio_stream *stream) 2790 { 2791 struct stream_out *out = (struct stream_out *)stream; 2792 2793 return out->channel_mask; 2794 } 2795 out_get_format(const struct audio_stream * stream)2796 static audio_format_t out_get_format(const struct audio_stream *stream) 2797 { 2798 struct stream_out *out = (struct stream_out *)stream; 2799 2800 return out->format; 2801 } 2802 out_set_format(struct audio_stream * stream __unused,audio_format_t format __unused)2803 static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) 2804 { 2805 return -ENOSYS; 2806 } 2807 2808 /* must be called with out->lock locked */ out_standby_l(struct audio_stream * stream)2809 static int out_standby_l(struct audio_stream *stream) 2810 { 2811 struct stream_out *out = (struct stream_out *)stream; 2812 struct audio_device *adev = out->dev; 2813 bool do_stop = true; 2814 2815 if (!out->standby) { 2816 if (adev->adm_deregister_stream) 2817 adev->adm_deregister_stream(adev->adm_data, out->handle); 2818 pthread_mutex_lock(&adev->lock); 2819 out->standby = true; 2820 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2821 if (out->pcm) { 2822 pcm_close(out->pcm); 2823 out->pcm = NULL; 2824 2825 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) { 2826 if (adev->haptic_pcm) { 2827 pcm_close(adev->haptic_pcm); 2828 adev->haptic_pcm = NULL; 2829 } 2830 2831 if (adev->haptic_buffer != NULL) { 2832 free(adev->haptic_buffer); 2833 adev->haptic_buffer = NULL; 2834 adev->haptic_buffer_size = 0; 2835 } 2836 } 2837 } 2838 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { 2839 do_stop = out->playback_started; 2840 out->playback_started = false; 2841 2842 if (out->mmap_shared_memory_fd >= 0) { 2843 ALOGV("%s: closing mmap_shared_memory_fd = %d", 2844 __func__, out->mmap_shared_memory_fd); 2845 close(out->mmap_shared_memory_fd); 2846 out->mmap_shared_memory_fd = -1; 2847 } 2848 2849 } 2850 } else { 2851 stop_compressed_output_l(out); 2852 out->gapless_mdata.encoder_delay = 0; 2853 out->gapless_mdata.encoder_padding = 0; 2854 if (out->compr != NULL) { 2855 compress_close(out->compr); 2856 out->compr = NULL; 2857 } 2858 } 2859 if (do_stop) { 2860 stop_output_stream(out); 2861 } 2862 pthread_mutex_unlock(&adev->lock); 2863 } 2864 return 0; 2865 } 2866 out_standby(struct audio_stream * stream)2867 static int out_standby(struct audio_stream *stream) 2868 { 2869 struct stream_out *out = (struct stream_out *)stream; 2870 2871 ALOGV("%s: enter: usecase(%d: %s)", __func__, 2872 out->usecase, use_case_table[out->usecase]); 2873 2874 lock_output_stream(out); 2875 out_standby_l(stream); 2876 pthread_mutex_unlock(&out->lock); 2877 ALOGV("%s: exit", __func__); 2878 return 0; 2879 } 2880 out_on_error(struct audio_stream * stream)2881 static int out_on_error(struct audio_stream *stream) 2882 { 2883 struct stream_out *out = (struct stream_out *)stream; 2884 struct audio_device *adev = out->dev; 2885 bool do_standby = false; 2886 2887 lock_output_stream(out); 2888 if (!out->standby) { 2889 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 2890 stop_compressed_output_l(out); 2891 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR); 2892 } else 2893 do_standby = true; 2894 } 2895 pthread_mutex_unlock(&out->lock); 2896 2897 if (do_standby) 2898 return out_standby(&out->stream.common); 2899 2900 return 0; 2901 } 2902 out_dump(const struct audio_stream * stream,int fd)2903 static int out_dump(const struct audio_stream *stream, int fd) 2904 { 2905 struct stream_out *out = (struct stream_out *)stream; 2906 2907 // We try to get the lock for consistency, 2908 // but it isn't necessary for these variables. 2909 // If we're not in standby, we may be blocked on a write. 2910 const bool locked = (pthread_mutex_trylock(&out->lock) == 0); 2911 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no"); 2912 dprintf(fd, " Frames written: %lld\n", (long long)out->written); 2913 2914 char buffer[256]; // for statistics formatting 2915 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer)); 2916 dprintf(fd, " Fifo frame underruns: %s\n", buffer); 2917 2918 if (out->start_latency_ms.n > 0) { 2919 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer)); 2920 dprintf(fd, " Start latency ms: %s\n", buffer); 2921 } 2922 2923 if (locked) { 2924 pthread_mutex_unlock(&out->lock); 2925 } 2926 2927 // dump error info 2928 (void)error_log_dump( 2929 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); 2930 2931 return 0; 2932 } 2933 parse_compress_metadata(struct stream_out * out,struct str_parms * parms)2934 static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) 2935 { 2936 int ret = 0; 2937 char value[32]; 2938 struct compr_gapless_mdata tmp_mdata; 2939 2940 if (!out || !parms) { 2941 return -EINVAL; 2942 } 2943 2944 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); 2945 if (ret >= 0) { 2946 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check? 2947 } else { 2948 return -EINVAL; 2949 } 2950 2951 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); 2952 if (ret >= 0) { 2953 tmp_mdata.encoder_padding = atoi(value); 2954 } else { 2955 return -EINVAL; 2956 } 2957 2958 out->gapless_mdata = tmp_mdata; 2959 out->send_new_metadata = 1; 2960 ALOGV("%s new encoder delay %u and padding %u", __func__, 2961 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); 2962 2963 return 0; 2964 } 2965 output_drives_call(struct audio_device * adev,struct stream_out * out)2966 static bool output_drives_call(struct audio_device *adev, struct stream_out *out) 2967 { 2968 return out == adev->primary_output || out == adev->voice_tx_output; 2969 } 2970 get_alive_usb_card(struct str_parms * parms)2971 static int get_alive_usb_card(struct str_parms* parms) { 2972 int card; 2973 if ((str_parms_get_int(parms, "card", &card) >= 0) && 2974 !audio_extn_usb_alive(card)) { 2975 return card; 2976 } 2977 return -ENODEV; 2978 } 2979 out_set_parameters(struct audio_stream * stream,const char * kvpairs)2980 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 2981 { 2982 struct stream_out *out = (struct stream_out *)stream; 2983 struct audio_device *adev = out->dev; 2984 struct audio_usecase *usecase; 2985 struct listnode *node; 2986 struct str_parms *parms; 2987 char value[32]; 2988 int ret, val = 0; 2989 bool select_new_device = false; 2990 int status = 0; 2991 bool bypass_a2dp = false; 2992 bool forced_speaker_fallback = false; 2993 2994 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", 2995 __func__, out->usecase, use_case_table[out->usecase], kvpairs); 2996 parms = str_parms_create_str(kvpairs); 2997 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 2998 if (ret >= 0) { 2999 val = atoi(value); 3000 3001 lock_output_stream(out); 3002 3003 if (val == AUDIO_DEVICE_NONE && 3004 audio_is_usb_out_device(out->devices)) { 3005 val = AUDIO_DEVICE_OUT_SPEAKER; 3006 forced_speaker_fallback = true; 3007 } 3008 3009 pthread_mutex_lock(&adev->lock); 3010 3011 /* 3012 * When HDMI cable is unplugged the music playback is paused and 3013 * the policy manager sends routing=0. But the audioflinger 3014 * continues to write data until standby time (3sec). 3015 * As the HDMI core is turned off, the write gets blocked. 3016 * Avoid this by routing audio to speaker until standby. 3017 */ 3018 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && 3019 val == AUDIO_DEVICE_NONE) { 3020 val = AUDIO_DEVICE_OUT_SPEAKER; 3021 forced_speaker_fallback = true; 3022 } 3023 3024 /* 3025 * When A2DP is disconnected the 3026 * music playback is paused and the policy manager sends routing=0 3027 * But the audioflingercontinues to write data until standby time 3028 * (3sec). As BT is turned off, the write gets blocked. 3029 * Avoid this by routing audio to speaker until standby. 3030 */ 3031 if ((out->devices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) && 3032 (val == AUDIO_DEVICE_NONE) && 3033 !audio_extn_a2dp_is_ready() && 3034 !adev->bt_sco_on) { 3035 val = AUDIO_DEVICE_OUT_SPEAKER; 3036 forced_speaker_fallback = true; 3037 } 3038 3039 /* To avoid a2dp to sco overlapping / BT device improper state 3040 * check with BT lib about a2dp streaming support before routing 3041 */ 3042 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { 3043 if (!audio_extn_a2dp_is_ready()) { 3044 if (val & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { 3045 //combo usecase just by pass a2dp 3046 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__); 3047 bypass_a2dp = true; 3048 } else { 3049 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__); 3050 /* update device to a2dp and don't route as BT returned error 3051 * However it is still possible a2dp routing called because 3052 * of current active device disconnection (like wired headset) 3053 */ 3054 out->devices = val; 3055 pthread_mutex_unlock(&out->lock); 3056 pthread_mutex_unlock(&adev->lock); 3057 status = -ENOSYS; 3058 goto routing_fail; 3059 } 3060 } 3061 } 3062 3063 audio_devices_t new_dev = val; 3064 3065 // Workaround: If routing to an non existing usb device, fail gracefully 3066 // The routing request will otherwise block during 10 second 3067 int card; 3068 if (audio_is_usb_out_device(new_dev) && 3069 (card = get_alive_usb_card(parms)) >= 0) { 3070 3071 ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card); 3072 pthread_mutex_unlock(&adev->lock); 3073 pthread_mutex_unlock(&out->lock); 3074 status = -ENOSYS; 3075 goto routing_fail; 3076 } 3077 3078 /* 3079 * select_devices() call below switches all the usecases on the same 3080 * backend to the new device. Refer to check_and_route_playback_usecases() in 3081 * the select_devices(). But how do we undo this? 3082 * 3083 * For example, music playback is active on headset (deep-buffer usecase) 3084 * and if we go to ringtones and select a ringtone, low-latency usecase 3085 * will be started on headset+speaker. As we can't enable headset+speaker 3086 * and headset devices at the same time, select_devices() switches the music 3087 * playback to headset+speaker while starting low-lateny usecase for ringtone. 3088 * So when the ringtone playback is completed, how do we undo the same? 3089 * 3090 * We are relying on the out_set_parameters() call on deep-buffer output, 3091 * once the ringtone playback is ended. 3092 * NOTE: We should not check if the current devices are same as new devices. 3093 * Because select_devices() must be called to switch back the music 3094 * playback to headset. 3095 */ 3096 if (new_dev != AUDIO_DEVICE_NONE) { 3097 bool same_dev = out->devices == new_dev; 3098 out->devices = new_dev; 3099 3100 if (output_drives_call(adev, out)) { 3101 if (!voice_is_call_state_active(adev)) { 3102 if (adev->mode == AUDIO_MODE_IN_CALL) { 3103 adev->current_call_output = out; 3104 ret = voice_start_call(adev); 3105 } 3106 } else { 3107 adev->current_call_output = out; 3108 voice_update_devices_for_all_voice_usecases(adev); 3109 } 3110 } 3111 3112 if (!out->standby) { 3113 int volume_delay_us = 0; 3114 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 3115 pthread_mutex_lock(&out->compr_mute_lock); 3116 if (out->a2dp_compress_mute && 3117 (!(new_dev & AUDIO_DEVICE_OUT_ALL_A2DP) || 3118 audio_extn_a2dp_is_ready())) { 3119 out->a2dp_compress_mute = false; 3120 } 3121 float volume_l = out->volume_l; 3122 float volume_r = out->volume_r; 3123 if (out->a2dp_compress_mute || forced_speaker_fallback) { 3124 volume_l = 0.0; 3125 volume_r = 0.0; 3126 } 3127 if (volume_l != out->applied_volume_l || volume_r != out->applied_volume_r) 3128 volume_delay_us = COMPRESS_OFFLOAD_PLAYBACK_LATENCY * 2000; 3129 3130 out_set_compr_volume(&out->stream, volume_l, volume_r); 3131 pthread_mutex_unlock(&out->compr_mute_lock); 3132 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || 3133 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER || 3134 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) { 3135 float volume_l = out->volume_l; 3136 float volume_r = out->volume_r; 3137 if (forced_speaker_fallback) { 3138 volume_l = 0.0; 3139 volume_r = 0.0; 3140 } 3141 if (volume_l != out->applied_volume_l || volume_r != out->applied_volume_r) 3142 volume_delay_us = (int)platform_render_latency(out) * 2; 3143 3144 out_set_pcm_volume(&out->stream, volume_l, volume_r); 3145 } 3146 if (volume_delay_us > 0) 3147 usleep(volume_delay_us * 2); 3148 3149 if (!same_dev) { 3150 ALOGV("update routing change"); 3151 // inform adm before actual routing to prevent glitches. 3152 if (adev->adm_on_routing_change) { 3153 adev->adm_on_routing_change(adev->adm_data, 3154 out->handle); 3155 } 3156 } 3157 if (!bypass_a2dp) { 3158 select_devices(adev, out->usecase); 3159 } else { 3160 if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE) 3161 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE; 3162 else 3163 out->devices = AUDIO_DEVICE_OUT_SPEAKER; 3164 select_devices(adev, out->usecase); 3165 out->devices = new_dev; 3166 } 3167 audio_extn_tfa_98xx_update(); 3168 3169 // on device switch force swap, lower functions will make sure 3170 // to check if swap is allowed or not. 3171 3172 if (!same_dev) 3173 platform_set_swap_channels(adev, true); 3174 3175 3176 } 3177 3178 } 3179 3180 pthread_mutex_unlock(&adev->lock); 3181 pthread_mutex_unlock(&out->lock); 3182 3183 /*handles device and call state changes*/ 3184 audio_extn_extspk_update(adev->extspk); 3185 } 3186 routing_fail: 3187 3188 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3189 parse_compress_metadata(out, parms); 3190 } 3191 3192 str_parms_destroy(parms); 3193 ALOGV("%s: exit: code(%d)", __func__, status); 3194 return status; 3195 } 3196 stream_get_parameter_channels(struct str_parms * query,struct str_parms * reply,audio_channel_mask_t * supported_channel_masks)3197 static bool stream_get_parameter_channels(struct str_parms *query, 3198 struct str_parms *reply, 3199 audio_channel_mask_t *supported_channel_masks) { 3200 int ret = -1; 3201 char value[ARRAY_SIZE(channels_name_to_enum_table) * 32 /* max channel name size */]; 3202 bool first = true; 3203 size_t i, j; 3204 3205 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { 3206 ret = 0; 3207 value[0] = '\0'; 3208 i = 0; 3209 while (supported_channel_masks[i] != 0) { 3210 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) { 3211 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) { 3212 if (!first) { 3213 strcat(value, "|"); 3214 } 3215 strcat(value, channels_name_to_enum_table[j].name); 3216 first = false; 3217 break; 3218 } 3219 } 3220 i++; 3221 } 3222 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 3223 } 3224 return ret >= 0; 3225 } 3226 stream_get_parameter_formats(struct str_parms * query,struct str_parms * reply,audio_format_t * supported_formats)3227 static bool stream_get_parameter_formats(struct str_parms *query, 3228 struct str_parms *reply, 3229 audio_format_t *supported_formats) { 3230 int ret = -1; 3231 char value[256]; 3232 int i; 3233 3234 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { 3235 ret = 0; 3236 value[0] = '\0'; 3237 switch (supported_formats[0]) { 3238 case AUDIO_FORMAT_PCM_16_BIT: 3239 strcat(value, "AUDIO_FORMAT_PCM_16_BIT"); 3240 break; 3241 case AUDIO_FORMAT_PCM_24_BIT_PACKED: 3242 strcat(value, "AUDIO_FORMAT_PCM_24_BIT_PACKED"); 3243 break; 3244 case AUDIO_FORMAT_PCM_32_BIT: 3245 strcat(value, "AUDIO_FORMAT_PCM_32_BIT"); 3246 break; 3247 default: 3248 ALOGE("%s: unsupported format %#x", __func__, 3249 supported_formats[0]); 3250 break; 3251 } 3252 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value); 3253 } 3254 return ret >= 0; 3255 } 3256 stream_get_parameter_rates(struct str_parms * query,struct str_parms * reply,uint32_t * supported_sample_rates)3257 static bool stream_get_parameter_rates(struct str_parms *query, 3258 struct str_parms *reply, 3259 uint32_t *supported_sample_rates) { 3260 3261 int i; 3262 char value[256]; 3263 int ret = -1; 3264 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { 3265 ret = 0; 3266 value[0] = '\0'; 3267 i=0; 3268 int cursor = 0; 3269 while (supported_sample_rates[i]) { 3270 int avail = sizeof(value) - cursor; 3271 ret = snprintf(value + cursor, avail, "%s%d", 3272 cursor > 0 ? "|" : "", 3273 supported_sample_rates[i]); 3274 if (ret < 0 || ret >= avail) { 3275 // if cursor is at the last element of the array 3276 // overwrite with \0 is duplicate work as 3277 // snprintf already put a \0 in place. 3278 // else 3279 // we had space to write the '|' at value[cursor] 3280 // (which will be overwritten) or no space to fill 3281 // the first element (=> cursor == 0) 3282 value[cursor] = '\0'; 3283 break; 3284 } 3285 cursor += ret; 3286 ++i; 3287 } 3288 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, 3289 value); 3290 } 3291 return ret >= 0; 3292 } 3293 out_get_parameters(const struct audio_stream * stream,const char * keys)3294 static char* out_get_parameters(const struct audio_stream *stream, const char *keys) 3295 { 3296 struct stream_out *out = (struct stream_out *)stream; 3297 struct str_parms *query = str_parms_create_str(keys); 3298 char *str; 3299 struct str_parms *reply = str_parms_create(); 3300 bool replied = false; 3301 ALOGV("%s: enter: keys - %s", __func__, keys); 3302 3303 replied |= stream_get_parameter_channels(query, reply, 3304 &out->supported_channel_masks[0]); 3305 replied |= stream_get_parameter_formats(query, reply, 3306 &out->supported_formats[0]); 3307 replied |= stream_get_parameter_rates(query, reply, 3308 &out->supported_sample_rates[0]); 3309 if (replied) { 3310 str = str_parms_to_str(reply); 3311 } else { 3312 str = strdup(""); 3313 } 3314 str_parms_destroy(query); 3315 str_parms_destroy(reply); 3316 ALOGV("%s: exit: returns - %s", __func__, str); 3317 return str; 3318 } 3319 out_get_latency(const struct audio_stream_out * stream)3320 static uint32_t out_get_latency(const struct audio_stream_out *stream) 3321 { 3322 uint32_t hw_delay, period_ms; 3323 struct stream_out *out = (struct stream_out *)stream; 3324 uint32_t latency; 3325 3326 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) 3327 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; 3328 else if ((out->realtime) || 3329 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) { 3330 // since the buffer won't be filled up faster than realtime, 3331 // return a smaller number 3332 period_ms = (out->af_period_multiplier * out->config.period_size * 3333 1000) / (out->config.rate); 3334 hw_delay = platform_render_latency(out)/1000; 3335 return period_ms + hw_delay; 3336 } 3337 3338 latency = (out->config.period_count * out->config.period_size * 1000) / 3339 (out->config.rate); 3340 3341 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) 3342 latency += audio_extn_a2dp_get_encoder_latency(); 3343 3344 return latency; 3345 } 3346 out_set_compr_volume(struct audio_stream_out * stream,float left,float right)3347 static int out_set_compr_volume(struct audio_stream_out *stream, float left, 3348 float right) 3349 { 3350 struct stream_out *out = (struct stream_out *)stream; 3351 int volume[2]; 3352 char mixer_ctl_name[128]; 3353 struct audio_device *adev = out->dev; 3354 struct mixer_ctl *ctl; 3355 int pcm_device_id = platform_get_pcm_device_id(out->usecase, 3356 PCM_PLAYBACK); 3357 3358 if (left == out->applied_volume_l && right == out->applied_volume_r) 3359 return 0; 3360 3361 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), 3362 "Compress Playback %d Volume", pcm_device_id); 3363 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 3364 if (!ctl) { 3365 ALOGE("%s: Could not get ctl for mixer cmd - %s", 3366 __func__, mixer_ctl_name); 3367 return -EINVAL; 3368 } 3369 ALOGV("%s: ctl for mixer cmd - %s, left %f, right %f", 3370 __func__, mixer_ctl_name, left, right); 3371 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); 3372 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); 3373 mixer_ctl_set_array(ctl, volume, sizeof(volume) / sizeof(volume[0])); 3374 3375 out->applied_volume_l = left; 3376 out->applied_volume_r = right; 3377 return 0; 3378 } 3379 out_set_pcm_volume(struct audio_stream_out * stream,float left,float right)3380 static int out_set_pcm_volume(struct audio_stream_out *stream, float left, 3381 float right) 3382 { 3383 struct stream_out *out = (struct stream_out *)stream; 3384 3385 if (left == out->applied_volume_l && right == out->applied_volume_r) 3386 return 0; 3387 3388 /* Volume control for pcm playback */ 3389 if (left != right) { 3390 return -EINVAL; 3391 } else { 3392 char mixer_ctl_name[128]; 3393 struct audio_device *adev = out->dev; 3394 struct mixer_ctl *ctl; 3395 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); 3396 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id); 3397 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 3398 if (!ctl) { 3399 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name); 3400 return -EINVAL; 3401 } 3402 3403 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX); 3404 int ret = mixer_ctl_set_value(ctl, 0, volume); 3405 if (ret < 0) { 3406 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret); 3407 return -EINVAL; 3408 } 3409 3410 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left); 3411 3412 out->applied_volume_l = left; 3413 out->applied_volume_r = right; 3414 return 0; 3415 } 3416 } 3417 out_set_volume(struct audio_stream_out * stream,float left,float right)3418 static int out_set_volume(struct audio_stream_out *stream, float left, 3419 float right) 3420 { 3421 struct stream_out *out = (struct stream_out *)stream; 3422 int ret = 0; 3423 3424 if (out->usecase == USECASE_AUDIO_PLAYBACK_HIFI) { 3425 /* only take left channel into account: the API is for stereo anyway */ 3426 out->muted = (left == 0.0f); 3427 return 0; 3428 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3429 pthread_mutex_lock(&out->compr_mute_lock); 3430 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute); 3431 if (!out->a2dp_compress_mute) 3432 ret = out_set_compr_volume(stream, left, right); 3433 out->volume_l = left; 3434 out->volume_r = right; 3435 pthread_mutex_unlock(&out->compr_mute_lock); 3436 return ret; 3437 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) { 3438 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX); 3439 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX); 3440 if (!out->standby) { 3441 // if in standby, cached volume will be sent after stream is opened 3442 audio_extn_utils_send_app_type_gain(out->dev, 3443 out->app_type_cfg.app_type, 3444 &out->app_type_cfg.gain[0]); 3445 } 3446 return 0; 3447 } 3448 3449 return -ENOSYS; 3450 } 3451 3452 // note: this call is safe only if the stream_cb is 3453 // removed first in close_output_stream (as is done now). out_snd_mon_cb(void * stream,struct str_parms * parms)3454 static void out_snd_mon_cb(void * stream, struct str_parms * parms) 3455 { 3456 if (!stream || !parms) 3457 return; 3458 3459 struct stream_out *out = (struct stream_out *)stream; 3460 struct audio_device *adev = out->dev; 3461 3462 card_status_t status; 3463 int card; 3464 if (parse_snd_card_status(parms, &card, &status) < 0) 3465 return; 3466 3467 pthread_mutex_lock(&adev->lock); 3468 bool valid_cb = (card == adev->snd_card); 3469 pthread_mutex_unlock(&adev->lock); 3470 3471 if (!valid_cb) 3472 return; 3473 3474 lock_output_stream(out); 3475 if (out->card_status != status) 3476 out->card_status = status; 3477 pthread_mutex_unlock(&out->lock); 3478 3479 ALOGW("out_snd_mon_cb for card %d usecase %s, status %s", card, 3480 use_case_table[out->usecase], 3481 status == CARD_STATUS_OFFLINE ? "offline" : "online"); 3482 3483 if (status == CARD_STATUS_OFFLINE) 3484 out_on_error(stream); 3485 3486 return; 3487 } 3488 3489 #ifdef NO_AUDIO_OUT out_write_for_no_output(struct audio_stream_out * stream,const void * buffer __unused,size_t bytes)3490 static ssize_t out_write_for_no_output(struct audio_stream_out *stream, 3491 const void *buffer __unused, size_t bytes) 3492 { 3493 struct stream_out *out = (struct stream_out *)stream; 3494 3495 /* No Output device supported other than BT for playback. 3496 * Sleep for the amount of buffer duration 3497 */ 3498 lock_output_stream(out); 3499 usleep(bytes * 1000000 / audio_stream_out_frame_size( 3500 (const struct audio_stream_out *)&out->stream) / 3501 out_get_sample_rate(&out->stream.common)); 3502 pthread_mutex_unlock(&out->lock); 3503 return bytes; 3504 } 3505 #endif 3506 out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)3507 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, 3508 size_t bytes) 3509 { 3510 struct stream_out *out = (struct stream_out *)stream; 3511 struct audio_device *adev = out->dev; 3512 ssize_t ret = 0; 3513 int error_code = ERROR_CODE_STANDBY; 3514 3515 lock_output_stream(out); 3516 // this is always nonzero 3517 const size_t frame_size = audio_stream_out_frame_size(stream); 3518 const size_t frames = bytes / frame_size; 3519 3520 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { 3521 error_code = ERROR_CODE_WRITE; 3522 goto exit; 3523 } 3524 3525 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && 3526 (audio_extn_a2dp_is_suspended())) { 3527 if (!(out->devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE))) { 3528 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { 3529 ret = -EIO; 3530 goto exit; 3531 } 3532 } 3533 } 3534 3535 const bool was_in_standby = out->standby; 3536 if (out->standby) { 3537 out->standby = false; 3538 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC); 3539 3540 pthread_mutex_lock(&adev->lock); 3541 ret = start_output_stream(out); 3542 3543 /* ToDo: If use case is compress offload should return 0 */ 3544 if (ret != 0) { 3545 out->standby = true; 3546 pthread_mutex_unlock(&adev->lock); 3547 goto exit; 3548 } 3549 3550 // after standby always force set last known cal step 3551 // dont change level anywhere except at the audio_hw_send_gain_dep_calibration 3552 ALOGD("%s: retry previous failed cal level set", __func__); 3553 send_gain_dep_calibration_l(); 3554 pthread_mutex_unlock(&adev->lock); 3555 3556 // log startup time in ms. 3557 simple_stats_log( 3558 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6); 3559 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid. 3560 } 3561 3562 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3563 ALOGVV("%s: writing buffer (%zu bytes) to compress device", __func__, bytes); 3564 if (out->send_new_metadata) { 3565 ALOGVV("send new gapless metadata"); 3566 compress_set_gapless_metadata(out->compr, &out->gapless_mdata); 3567 out->send_new_metadata = 0; 3568 } 3569 unsigned int avail; 3570 struct timespec tstamp; 3571 ret = compress_get_hpointer(out->compr, &avail, &tstamp); 3572 /* Do not limit write size if the available frames count is unknown */ 3573 if (ret != 0) { 3574 avail = bytes; 3575 } 3576 if (avail == 0) { 3577 ret = 0; 3578 } else { 3579 // check for compressed format underrun, essentially an empty buffer check 3580 // for a lack of better measurement. 3581 if (!was_in_standby && avail == out->kernel_buffer_size) { 3582 ALOGW("%s: compressed buffer empty (underrun)", __func__); 3583 simple_stats_log(&out->fifo_underruns, 1.); // Note: log one frame for compressed. 3584 } 3585 3586 if (avail > bytes) { 3587 avail = bytes; 3588 } 3589 ret = compress_write(out->compr, buffer, avail); 3590 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %zd", 3591 __func__, avail, ret); 3592 } 3593 3594 if (ret >= 0 && ret < (ssize_t)bytes) { 3595 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); 3596 } 3597 if (ret > 0 && !out->playback_started) { 3598 compress_start(out->compr); 3599 out->playback_started = 1; 3600 out->offload_state = OFFLOAD_STATE_PLAYING; 3601 } 3602 if (ret < 0) { 3603 error_log_log(out->error_log, ERROR_CODE_WRITE, audio_utils_get_real_time_ns()); 3604 } else { 3605 out->written += ret; // accumulate bytes written for offload. 3606 } 3607 pthread_mutex_unlock(&out->lock); 3608 // TODO: consider logging offload pcm 3609 return ret; 3610 } else { 3611 error_code = ERROR_CODE_WRITE; 3612 if (out->pcm) { 3613 size_t bytes_to_write = bytes; 3614 3615 if (out->muted) 3616 memset((void *)buffer, 0, bytes); 3617 // FIXME: this can be removed once audio flinger mixer supports mono output 3618 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP || 3619 out->usecase == USECASE_INCALL_MUSIC_UPLINK || 3620 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) { 3621 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask); 3622 int16_t *src = (int16_t *)buffer; 3623 int16_t *dst = (int16_t *)buffer; 3624 3625 LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 || 3626 out->format != AUDIO_FORMAT_PCM_16_BIT, 3627 "out_write called for VOIP use case with wrong properties"); 3628 3629 for (size_t i = 0; i < frames ; i++, dst++, src += 2) { 3630 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); 3631 } 3632 bytes_to_write /= 2; 3633 } 3634 3635 // Note: since out_get_presentation_position() is called alternating with out_write() 3636 // by AudioFlinger, we can check underruns using the prior timestamp read. 3637 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp(). 3638 if (out->last_fifo_valid) { 3639 // compute drain to see if there is an underrun. 3640 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call 3641 const int64_t frames_by_time = 3642 (current_ns - out->last_fifo_time_ns) * out->config.rate / NANOS_PER_SECOND; 3643 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining; 3644 3645 if (underrun > 0) { 3646 simple_stats_log(&out->fifo_underruns, underrun); 3647 3648 ALOGW("%s: underrun(%lld) " 3649 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)", 3650 __func__, 3651 (long long)out->fifo_underruns.n, 3652 (long long)frames_by_time, 3653 (long long)out->last_fifo_frames_remaining); 3654 } 3655 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale. 3656 } 3657 3658 long ns = (frames * (int64_t) NANOS_PER_SECOND) / out->config.rate; 3659 request_out_focus(out, ns); 3660 3661 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime; 3662 if (use_mmap) { 3663 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write); 3664 } else { 3665 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) { 3666 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask); 3667 size_t bytes_per_sample = audio_bytes_per_sample(out->format); 3668 size_t frame_size = channel_count * bytes_per_sample; 3669 size_t frame_count = bytes_to_write / frame_size; 3670 3671 bool force_haptic_path = 3672 property_get_bool("vendor.audio.test_haptic", false); 3673 3674 // extract Haptics data from Audio buffer 3675 bool alloc_haptic_buffer = false; 3676 int haptic_channel_count = adev->haptics_config.channels; 3677 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count; 3678 size_t audio_frame_size = frame_size - haptic_frame_size; 3679 size_t total_haptic_buffer_size = frame_count * haptic_frame_size; 3680 3681 if (adev->haptic_buffer == NULL) { 3682 alloc_haptic_buffer = true; 3683 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) { 3684 free(adev->haptic_buffer); 3685 adev->haptic_buffer_size = 0; 3686 alloc_haptic_buffer = true; 3687 } 3688 3689 if (alloc_haptic_buffer) { 3690 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size); 3691 adev->haptic_buffer_size = total_haptic_buffer_size; 3692 } 3693 3694 size_t src_index = 0, aud_index = 0, hap_index = 0; 3695 uint8_t *audio_buffer = (uint8_t *)buffer; 3696 uint8_t *haptic_buffer = adev->haptic_buffer; 3697 3698 // This is required for testing only. This works for stereo data only. 3699 // One channel is fed to audio stream and other to haptic stream for testing. 3700 if (force_haptic_path) { 3701 audio_frame_size = haptic_frame_size = bytes_per_sample; 3702 } 3703 3704 for (size_t i = 0; i < frame_count; i++) { 3705 for (size_t j = 0; j < audio_frame_size; j++) 3706 audio_buffer[aud_index++] = audio_buffer[src_index++]; 3707 3708 for (size_t j = 0; j < haptic_frame_size; j++) 3709 haptic_buffer[hap_index++] = audio_buffer[src_index++]; 3710 } 3711 3712 // This is required for testing only. 3713 // Discard haptic channel data. 3714 if (force_haptic_path) { 3715 src_index += haptic_frame_size; 3716 } 3717 3718 // write to audio pipeline 3719 ret = pcm_write(out->pcm, 3720 (void *)audio_buffer, 3721 frame_count * audio_frame_size); 3722 3723 // write to haptics pipeline 3724 if (adev->haptic_pcm) 3725 ret = pcm_write(adev->haptic_pcm, 3726 (void *)adev->haptic_buffer, 3727 frame_count * haptic_frame_size); 3728 3729 } else { 3730 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write); 3731 } 3732 } 3733 release_out_focus(out, ns); 3734 } else { 3735 LOG_ALWAYS_FATAL("out->pcm is NULL after starting output stream"); 3736 } 3737 } 3738 3739 exit: 3740 // For PCM we always consume the buffer and return #bytes regardless of ret. 3741 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3742 out->written += frames; 3743 } 3744 long long sleeptime_us = 0; 3745 3746 if (ret != 0) { 3747 error_log_log(out->error_log, error_code, audio_utils_get_real_time_ns()); 3748 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3749 ALOGE_IF(out->pcm != NULL, 3750 "%s: error %zd - %s", __func__, ret, pcm_get_error(out->pcm)); 3751 sleeptime_us = frames * 1000000LL / out_get_sample_rate(&out->stream.common); 3752 // usleep not guaranteed for values over 1 second but we don't limit here. 3753 } 3754 } 3755 3756 pthread_mutex_unlock(&out->lock); 3757 3758 if (ret != 0) { 3759 out_on_error(&out->stream.common); 3760 if (sleeptime_us != 0) 3761 usleep(sleeptime_us); 3762 } 3763 return bytes; 3764 } 3765 out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)3766 static int out_get_render_position(const struct audio_stream_out *stream, 3767 uint32_t *dsp_frames) 3768 { 3769 struct stream_out *out = (struct stream_out *)stream; 3770 *dsp_frames = 0; 3771 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { 3772 lock_output_stream(out); 3773 if (out->compr != NULL) { 3774 unsigned long frames = 0; 3775 // TODO: check return value 3776 compress_get_tstamp(out->compr, &frames, &out->sample_rate); 3777 *dsp_frames = (uint32_t)frames; 3778 ALOGVV("%s rendered frames %d sample_rate %d", 3779 __func__, *dsp_frames, out->sample_rate); 3780 } 3781 pthread_mutex_unlock(&out->lock); 3782 return 0; 3783 } else 3784 return -ENODATA; 3785 } 3786 out_add_audio_effect(const struct audio_stream * stream __unused,effect_handle_t effect __unused)3787 static int out_add_audio_effect(const struct audio_stream *stream __unused, 3788 effect_handle_t effect __unused) 3789 { 3790 return 0; 3791 } 3792 out_remove_audio_effect(const struct audio_stream * stream __unused,effect_handle_t effect __unused)3793 static int out_remove_audio_effect(const struct audio_stream *stream __unused, 3794 effect_handle_t effect __unused) 3795 { 3796 return 0; 3797 } 3798 out_get_next_write_timestamp(const struct audio_stream_out * stream __unused,int64_t * timestamp __unused)3799 static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused, 3800 int64_t *timestamp __unused) 3801 { 3802 return -ENOSYS; 3803 } 3804 out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)3805 static int out_get_presentation_position(const struct audio_stream_out *stream, 3806 uint64_t *frames, struct timespec *timestamp) 3807 { 3808 struct stream_out *out = (struct stream_out *)stream; 3809 int ret = -ENODATA; 3810 unsigned long dsp_frames; 3811 3812 lock_output_stream(out); 3813 3814 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3815 if (out->compr != NULL) { 3816 // TODO: check return value 3817 compress_get_tstamp(out->compr, &dsp_frames, 3818 &out->sample_rate); 3819 // Adjustment accounts for A2DP encoder latency with offload usecases 3820 // Note: Encoder latency is returned in ms. 3821 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { 3822 unsigned long offset = 3823 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); 3824 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0; 3825 } 3826 ALOGVV("%s rendered frames %ld sample_rate %d", 3827 __func__, dsp_frames, out->sample_rate); 3828 *frames = dsp_frames; 3829 ret = 0; 3830 /* this is the best we can do */ 3831 clock_gettime(CLOCK_MONOTONIC, timestamp); 3832 } 3833 } else { 3834 if (out->pcm) { 3835 unsigned int avail; 3836 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { 3837 3838 // pcm_get_htimestamp() computes the available frames by comparing 3839 // the alsa driver hw_ptr and the appl_ptr levels. 3840 // In underrun, the hw_ptr may keep running and report an excessively 3841 // large number available number. 3842 if (avail > out->kernel_buffer_size) { 3843 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!", 3844 __func__, avail, out->kernel_buffer_size); 3845 avail = out->kernel_buffer_size; 3846 out->last_fifo_frames_remaining = 0; 3847 } else { 3848 out->last_fifo_frames_remaining = out->kernel_buffer_size - avail; 3849 } 3850 out->last_fifo_valid = true; 3851 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp); 3852 3853 int64_t signed_frames = out->written - out->last_fifo_frames_remaining; 3854 3855 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu", 3856 __func__, (long long)signed_frames, avail, out->kernel_buffer_size); 3857 3858 // This adjustment accounts for buffering after app processor. 3859 // It is based on estimated DSP latency per use case, rather than exact. 3860 signed_frames -= 3861 (platform_render_latency(out) * out->sample_rate / 1000000LL); 3862 3863 // Adjustment accounts for A2DP encoder latency with non-offload usecases 3864 // Note: Encoder latency is returned in ms, while platform_render_latency in us. 3865 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { 3866 signed_frames -= 3867 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); 3868 } 3869 3870 // It would be unusual for this value to be negative, but check just in case ... 3871 if (signed_frames >= 0) { 3872 *frames = signed_frames; 3873 ret = 0; 3874 } 3875 } 3876 } 3877 } 3878 3879 pthread_mutex_unlock(&out->lock); 3880 3881 return ret; 3882 } 3883 out_set_callback(struct audio_stream_out * stream,stream_callback_t callback,void * cookie)3884 static int out_set_callback(struct audio_stream_out *stream, 3885 stream_callback_t callback, void *cookie) 3886 { 3887 struct stream_out *out = (struct stream_out *)stream; 3888 3889 ALOGV("%s", __func__); 3890 lock_output_stream(out); 3891 out->offload_callback = callback; 3892 out->offload_cookie = cookie; 3893 pthread_mutex_unlock(&out->lock); 3894 return 0; 3895 } 3896 out_pause(struct audio_stream_out * stream)3897 static int out_pause(struct audio_stream_out* stream) 3898 { 3899 struct stream_out *out = (struct stream_out *)stream; 3900 int status = -ENOSYS; 3901 ALOGV("%s", __func__); 3902 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3903 status = -ENODATA; 3904 lock_output_stream(out); 3905 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { 3906 status = compress_pause(out->compr); 3907 out->offload_state = OFFLOAD_STATE_PAUSED; 3908 } 3909 pthread_mutex_unlock(&out->lock); 3910 } 3911 return status; 3912 } 3913 out_resume(struct audio_stream_out * stream)3914 static int out_resume(struct audio_stream_out* stream) 3915 { 3916 struct stream_out *out = (struct stream_out *)stream; 3917 int status = -ENOSYS; 3918 ALOGV("%s", __func__); 3919 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3920 status = -ENODATA; 3921 lock_output_stream(out); 3922 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { 3923 status = compress_resume(out->compr); 3924 out->offload_state = OFFLOAD_STATE_PLAYING; 3925 } 3926 pthread_mutex_unlock(&out->lock); 3927 } 3928 return status; 3929 } 3930 out_drain(struct audio_stream_out * stream,audio_drain_type_t type)3931 static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) 3932 { 3933 struct stream_out *out = (struct stream_out *)stream; 3934 int status = -ENOSYS; 3935 ALOGV("%s", __func__); 3936 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3937 lock_output_stream(out); 3938 if (type == AUDIO_DRAIN_EARLY_NOTIFY) 3939 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); 3940 else 3941 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); 3942 pthread_mutex_unlock(&out->lock); 3943 } 3944 return status; 3945 } 3946 out_flush(struct audio_stream_out * stream)3947 static int out_flush(struct audio_stream_out* stream) 3948 { 3949 struct stream_out *out = (struct stream_out *)stream; 3950 ALOGV("%s", __func__); 3951 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 3952 lock_output_stream(out); 3953 stop_compressed_output_l(out); 3954 pthread_mutex_unlock(&out->lock); 3955 return 0; 3956 } 3957 return -ENOSYS; 3958 } 3959 out_stop(const struct audio_stream_out * stream)3960 static int out_stop(const struct audio_stream_out* stream) 3961 { 3962 struct stream_out *out = (struct stream_out *)stream; 3963 struct audio_device *adev = out->dev; 3964 int ret = -ENOSYS; 3965 3966 ALOGV("%s", __func__); 3967 pthread_mutex_lock(&adev->lock); 3968 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && 3969 out->playback_started && out->pcm != NULL) { 3970 pcm_stop(out->pcm); 3971 ret = stop_output_stream(out); 3972 out->playback_started = false; 3973 } 3974 pthread_mutex_unlock(&adev->lock); 3975 return ret; 3976 } 3977 out_start(const struct audio_stream_out * stream)3978 static int out_start(const struct audio_stream_out* stream) 3979 { 3980 struct stream_out *out = (struct stream_out *)stream; 3981 struct audio_device *adev = out->dev; 3982 int ret = -ENOSYS; 3983 3984 ALOGV("%s", __func__); 3985 pthread_mutex_lock(&adev->lock); 3986 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && 3987 !out->playback_started && out->pcm != NULL) { 3988 ret = start_output_stream(out); 3989 if (ret == 0) { 3990 out->playback_started = true; 3991 } 3992 } 3993 pthread_mutex_unlock(&adev->lock); 3994 return ret; 3995 } 3996 3997 /* 3998 * Modify config->period_count based on min_size_frames 3999 */ adjust_mmap_period_count(struct pcm_config * config,int32_t min_size_frames)4000 static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames) 4001 { 4002 int periodCountRequested = (min_size_frames + config->period_size - 1) 4003 / config->period_size; 4004 int periodCount = MMAP_PERIOD_COUNT_MIN; 4005 4006 ALOGV("%s original config.period_size = %d config.period_count = %d", 4007 __func__, config->period_size, config->period_count); 4008 4009 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) { 4010 periodCount *= 2; 4011 } 4012 config->period_count = periodCount; 4013 4014 ALOGV("%s requested config.period_count = %d", __func__, config->period_count); 4015 } 4016 4017 // Read offset for the positional timestamp from a persistent vendor property. 4018 // This is to workaround apparent inaccuracies in the timing information that 4019 // is used by the AAudio timing model. The inaccuracies can cause glitches. get_mmap_out_time_offset()4020 static int64_t get_mmap_out_time_offset() { 4021 const int32_t kDefaultOffsetMicros = 0; 4022 int32_t mmap_time_offset_micros = property_get_int32( 4023 "persist.audio.out_mmap_delay_micros", kDefaultOffsetMicros); 4024 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros); 4025 return mmap_time_offset_micros * (int64_t)1000; 4026 } 4027 out_create_mmap_buffer(const struct audio_stream_out * stream,int32_t min_size_frames,struct audio_mmap_buffer_info * info)4028 static int out_create_mmap_buffer(const struct audio_stream_out *stream, 4029 int32_t min_size_frames, 4030 struct audio_mmap_buffer_info *info) 4031 { 4032 struct stream_out *out = (struct stream_out *)stream; 4033 struct audio_device *adev = out->dev; 4034 int ret = 0; 4035 unsigned int offset1; 4036 unsigned int frames1; 4037 const char *step = ""; 4038 uint32_t mmap_size; 4039 uint32_t buffer_size; 4040 4041 ALOGV("%s", __func__); 4042 lock_output_stream(out); 4043 pthread_mutex_lock(&adev->lock); 4044 4045 if (info == NULL || min_size_frames <= 0 || min_size_frames > MMAP_MIN_SIZE_FRAMES_MAX) { 4046 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames); 4047 ret = -EINVAL; 4048 goto exit; 4049 } 4050 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) { 4051 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby); 4052 ret = -ENOSYS; 4053 goto exit; 4054 } 4055 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); 4056 if (out->pcm_device_id < 0) { 4057 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 4058 __func__, out->pcm_device_id, out->usecase); 4059 ret = -EINVAL; 4060 goto exit; 4061 } 4062 4063 adjust_mmap_period_count(&out->config, min_size_frames); 4064 4065 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 4066 __func__, adev->snd_card, out->pcm_device_id, out->config.channels); 4067 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id, 4068 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config); 4069 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { 4070 step = "open"; 4071 ret = -ENODEV; 4072 goto exit; 4073 } 4074 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1); 4075 if (ret < 0) { 4076 step = "begin"; 4077 goto exit; 4078 } 4079 info->buffer_size_frames = pcm_get_buffer_size(out->pcm); 4080 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames); 4081 info->burst_size_frames = out->config.period_size; 4082 ret = platform_get_mmap_data_fd(adev->platform, 4083 out->pcm_device_id, 0 /*playback*/, 4084 &info->shared_memory_fd, 4085 &mmap_size); 4086 if (ret < 0) { 4087 // Fall back to non exclusive mode 4088 info->shared_memory_fd = pcm_get_poll_fd(out->pcm); 4089 } else { 4090 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later 4091 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd); 4092 4093 if (mmap_size < buffer_size) { 4094 step = "mmap"; 4095 goto exit; 4096 } 4097 // FIXME: indicate exclusive mode support by returning a negative buffer size 4098 info->buffer_size_frames *= -1; 4099 } 4100 memset(info->shared_memory_address, 0, buffer_size); 4101 4102 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE); 4103 if (ret < 0) { 4104 step = "commit"; 4105 goto exit; 4106 } 4107 4108 out->mmap_time_offset_nanos = get_mmap_out_time_offset(); 4109 4110 out->standby = false; 4111 ret = 0; 4112 4113 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d", 4114 __func__, info->shared_memory_address, info->buffer_size_frames); 4115 4116 exit: 4117 if (ret != 0) { 4118 if (out->pcm == NULL) { 4119 ALOGE("%s: %s - %d", __func__, step, ret); 4120 } else { 4121 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm)); 4122 pcm_close(out->pcm); 4123 out->pcm = NULL; 4124 } 4125 } 4126 pthread_mutex_unlock(&adev->lock); 4127 pthread_mutex_unlock(&out->lock); 4128 return ret; 4129 } 4130 out_get_mmap_position(const struct audio_stream_out * stream,struct audio_mmap_position * position)4131 static int out_get_mmap_position(const struct audio_stream_out *stream, 4132 struct audio_mmap_position *position) 4133 { 4134 int ret = 0; 4135 struct stream_out *out = (struct stream_out *)stream; 4136 ALOGVV("%s", __func__); 4137 if (position == NULL) { 4138 return -EINVAL; 4139 } 4140 lock_output_stream(out); 4141 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || 4142 out->pcm == NULL) { 4143 ret = -ENOSYS; 4144 goto exit; 4145 } 4146 4147 struct timespec ts = { 0, 0 }; 4148 ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts); 4149 if (ret < 0) { 4150 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); 4151 goto exit; 4152 } 4153 position->time_nanoseconds = audio_utils_ns_from_timespec(&ts) 4154 + out->mmap_time_offset_nanos; 4155 4156 exit: 4157 pthread_mutex_unlock(&out->lock); 4158 return ret; 4159 } 4160 4161 4162 /** audio_stream_in implementation **/ in_get_sample_rate(const struct audio_stream * stream)4163 static uint32_t in_get_sample_rate(const struct audio_stream *stream) 4164 { 4165 struct stream_in *in = (struct stream_in *)stream; 4166 4167 return in->config.rate; 4168 } 4169 in_set_sample_rate(struct audio_stream * stream __unused,uint32_t rate __unused)4170 static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) 4171 { 4172 return -ENOSYS; 4173 } 4174 in_get_buffer_size(const struct audio_stream * stream)4175 static size_t in_get_buffer_size(const struct audio_stream *stream) 4176 { 4177 struct stream_in *in = (struct stream_in *)stream; 4178 return in->config.period_size * in->af_period_multiplier * 4179 audio_stream_in_frame_size((const struct audio_stream_in *)stream); 4180 } 4181 in_get_channels(const struct audio_stream * stream)4182 static uint32_t in_get_channels(const struct audio_stream *stream) 4183 { 4184 struct stream_in *in = (struct stream_in *)stream; 4185 4186 return in->channel_mask; 4187 } 4188 in_get_format(const struct audio_stream * stream)4189 static audio_format_t in_get_format(const struct audio_stream *stream) 4190 { 4191 struct stream_in *in = (struct stream_in *)stream; 4192 return in->format; 4193 } 4194 in_set_format(struct audio_stream * stream __unused,audio_format_t format __unused)4195 static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) 4196 { 4197 return -ENOSYS; 4198 } 4199 in_standby(struct audio_stream * stream)4200 static int in_standby(struct audio_stream *stream) 4201 { 4202 struct stream_in *in = (struct stream_in *)stream; 4203 struct audio_device *adev = in->dev; 4204 int status = 0; 4205 bool do_stop = true; 4206 4207 ALOGV("%s: enter", __func__); 4208 4209 lock_input_stream(in); 4210 4211 if (!in->standby && (in->flags & AUDIO_INPUT_FLAG_HW_HOTWORD)) { 4212 ALOGV("%s: sound trigger pcm stop lab", __func__); 4213 audio_extn_sound_trigger_stop_lab(in); 4214 in->standby = true; 4215 } 4216 4217 if (!in->standby) { 4218 if (adev->adm_deregister_stream) 4219 adev->adm_deregister_stream(adev->adm_data, in->capture_handle); 4220 4221 pthread_mutex_lock(&adev->lock); 4222 in->standby = true; 4223 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { 4224 do_stop = in->capture_started; 4225 in->capture_started = false; 4226 4227 if (in->mmap_shared_memory_fd >= 0) { 4228 ALOGV("%s: closing mmap_shared_memory_fd = %d", 4229 __func__, in->mmap_shared_memory_fd); 4230 close(in->mmap_shared_memory_fd); 4231 in->mmap_shared_memory_fd = -1; 4232 } 4233 4234 } 4235 if (in->pcm) { 4236 pcm_close(in->pcm); 4237 in->pcm = NULL; 4238 } 4239 4240 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) 4241 adev->enable_voicerx = false; 4242 4243 if (do_stop) { 4244 status = stop_input_stream(in); 4245 } 4246 4247 pthread_mutex_unlock(&adev->lock); 4248 } 4249 pthread_mutex_unlock(&in->lock); 4250 ALOGV("%s: exit: status(%d)", __func__, status); 4251 return status; 4252 } 4253 in_dump(const struct audio_stream * stream,int fd)4254 static int in_dump(const struct audio_stream *stream, int fd) 4255 { 4256 struct stream_in *in = (struct stream_in *)stream; 4257 4258 // We try to get the lock for consistency, 4259 // but it isn't necessary for these variables. 4260 // If we're not in standby, we may be blocked on a read. 4261 const bool locked = (pthread_mutex_trylock(&in->lock) == 0); 4262 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no"); 4263 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read); 4264 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted); 4265 4266 char buffer[256]; // for statistics formatting 4267 if (in->start_latency_ms.n > 0) { 4268 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer)); 4269 dprintf(fd, " Start latency ms: %s\n", buffer); 4270 } 4271 4272 if (locked) { 4273 pthread_mutex_unlock(&in->lock); 4274 } 4275 4276 // dump error info 4277 (void)error_log_dump( 4278 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); 4279 return 0; 4280 } 4281 in_set_parameters(struct audio_stream * stream,const char * kvpairs)4282 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 4283 { 4284 struct stream_in *in = (struct stream_in *)stream; 4285 struct audio_device *adev = in->dev; 4286 struct str_parms *parms; 4287 char *str; 4288 char value[32]; 4289 int ret, val = 0; 4290 int status = 0; 4291 4292 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); 4293 parms = str_parms_create_str(kvpairs); 4294 4295 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 4296 4297 lock_input_stream(in); 4298 4299 pthread_mutex_lock(&adev->lock); 4300 if (ret >= 0) { 4301 val = atoi(value); 4302 /* no audio source uses val == 0 */ 4303 if ((in->source != val) && (val != 0)) { 4304 in->source = val; 4305 } 4306 } 4307 4308 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 4309 4310 if (ret >= 0) { 4311 val = atoi(value); 4312 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) { 4313 4314 // Workaround: If routing to an non existing usb device, fail gracefully 4315 // The routing request will otherwise block during 10 second 4316 int card; 4317 if (audio_is_usb_in_device(val) && 4318 (card = get_alive_usb_card(parms)) >= 0) { 4319 4320 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card); 4321 status = -ENOSYS; 4322 } else { 4323 4324 in->device = val; 4325 /* If recording is in progress, change the tx device to new device */ 4326 if (!in->standby) { 4327 ALOGV("update input routing change"); 4328 // inform adm before actual routing to prevent glitches. 4329 if (adev->adm_on_routing_change) { 4330 adev->adm_on_routing_change(adev->adm_data, 4331 in->capture_handle); 4332 } 4333 select_devices(adev, in->usecase); 4334 } 4335 } 4336 } 4337 } 4338 4339 pthread_mutex_unlock(&adev->lock); 4340 pthread_mutex_unlock(&in->lock); 4341 4342 str_parms_destroy(parms); 4343 ALOGV("%s: exit: status(%d)", __func__, status); 4344 return status; 4345 } 4346 in_get_parameters(const struct audio_stream * stream,const char * keys)4347 static char* in_get_parameters(const struct audio_stream *stream, 4348 const char *keys) 4349 { 4350 struct stream_in *in = (struct stream_in *)stream; 4351 struct str_parms *query = str_parms_create_str(keys); 4352 char *str; 4353 struct str_parms *reply = str_parms_create(); 4354 bool replied = false; 4355 4356 ALOGV("%s: enter: keys - %s", __func__, keys); 4357 replied |= stream_get_parameter_channels(query, reply, 4358 &in->supported_channel_masks[0]); 4359 replied |= stream_get_parameter_formats(query, reply, 4360 &in->supported_formats[0]); 4361 replied |= stream_get_parameter_rates(query, reply, 4362 &in->supported_sample_rates[0]); 4363 if (replied) { 4364 str = str_parms_to_str(reply); 4365 } else { 4366 str = strdup(""); 4367 } 4368 str_parms_destroy(query); 4369 str_parms_destroy(reply); 4370 ALOGV("%s: exit: returns - %s", __func__, str); 4371 return str; 4372 } 4373 in_set_gain(struct audio_stream_in * stream,float gain)4374 static int in_set_gain(struct audio_stream_in *stream, float gain) 4375 { 4376 struct stream_in *in = (struct stream_in *)stream; 4377 char mixer_ctl_name[128]; 4378 struct mixer_ctl *ctl; 4379 int ctl_value; 4380 4381 ALOGV("%s: gain %f", __func__, gain); 4382 4383 if (stream == NULL) 4384 return -EINVAL; 4385 4386 /* in_set_gain() only used to silence MMAP capture for now */ 4387 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) 4388 return -ENOSYS; 4389 4390 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id); 4391 4392 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name); 4393 if (!ctl) { 4394 ALOGW("%s: Could not get ctl for mixer cmd - %s", 4395 __func__, mixer_ctl_name); 4396 return -ENOSYS; 4397 } 4398 4399 if (gain < RECORD_GAIN_MIN) 4400 gain = RECORD_GAIN_MIN; 4401 else if (gain > RECORD_GAIN_MAX) 4402 gain = RECORD_GAIN_MAX; 4403 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain); 4404 4405 mixer_ctl_set_value(ctl, 0, ctl_value); 4406 return 0; 4407 } 4408 in_snd_mon_cb(void * stream,struct str_parms * parms)4409 static void in_snd_mon_cb(void * stream, struct str_parms * parms) 4410 { 4411 if (!stream || !parms) 4412 return; 4413 4414 struct stream_in *in = (struct stream_in *)stream; 4415 struct audio_device *adev = in->dev; 4416 4417 card_status_t status; 4418 int card; 4419 if (parse_snd_card_status(parms, &card, &status) < 0) 4420 return; 4421 4422 pthread_mutex_lock(&adev->lock); 4423 bool valid_cb = (card == adev->snd_card); 4424 pthread_mutex_unlock(&adev->lock); 4425 4426 if (!valid_cb) 4427 return; 4428 4429 lock_input_stream(in); 4430 if (in->card_status != status) 4431 in->card_status = status; 4432 pthread_mutex_unlock(&in->lock); 4433 4434 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card, 4435 use_case_table[in->usecase], 4436 status == CARD_STATUS_OFFLINE ? "offline" : "online"); 4437 4438 // a better solution would be to report error back to AF and let 4439 // it put the stream to standby 4440 if (status == CARD_STATUS_OFFLINE) 4441 in_standby(&in->stream.common); 4442 4443 return; 4444 } 4445 in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)4446 static ssize_t in_read(struct audio_stream_in *stream, void *buffer, 4447 size_t bytes) 4448 { 4449 struct stream_in *in = (struct stream_in *)stream; 4450 struct audio_device *adev = in->dev; 4451 int i, ret = -1; 4452 int *int_buf_stream = NULL; 4453 int error_code = ERROR_CODE_STANDBY; // initial errors are considered coming out of standby. 4454 4455 lock_input_stream(in); 4456 const size_t frame_size = audio_stream_in_frame_size(stream); 4457 const size_t frames = bytes / frame_size; 4458 4459 if (in->flags & AUDIO_INPUT_FLAG_HW_HOTWORD) { 4460 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes); 4461 /* Read from sound trigger HAL */ 4462 audio_extn_sound_trigger_read(in, buffer, bytes); 4463 pthread_mutex_unlock(&in->lock); 4464 return bytes; 4465 } 4466 4467 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { 4468 ret = -ENOSYS; 4469 goto exit; 4470 } 4471 4472 if (in->standby) { 4473 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC); 4474 4475 pthread_mutex_lock(&adev->lock); 4476 ret = start_input_stream(in); 4477 pthread_mutex_unlock(&adev->lock); 4478 if (ret != 0) { 4479 goto exit; 4480 } 4481 in->standby = 0; 4482 4483 // log startup time in ms. 4484 simple_stats_log( 4485 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6); 4486 } 4487 4488 // errors that occur here are read errors. 4489 error_code = ERROR_CODE_READ; 4490 4491 //what's the duration requested by the client? 4492 long ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/ 4493 in->config.rate; 4494 request_in_focus(in, ns); 4495 4496 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime; 4497 if (in->pcm) { 4498 if (use_mmap) { 4499 ret = pcm_mmap_read(in->pcm, buffer, bytes); 4500 } else { 4501 ret = pcm_read(in->pcm, buffer, bytes); 4502 } 4503 if (ret < 0) { 4504 ALOGE("Failed to read w/err %s", strerror(errno)); 4505 ret = -errno; 4506 } 4507 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) { 4508 if (bytes % 4 == 0) { 4509 /* data from DSP comes in 24_8 format, convert it to 8_24 */ 4510 int_buf_stream = buffer; 4511 for (size_t itt=0; itt < bytes/4 ; itt++) { 4512 int_buf_stream[itt] >>= 8; 4513 } 4514 } else { 4515 ALOGE("%s: !!! something wrong !!! ... data not 32 bit aligned ", __func__); 4516 ret = -EINVAL; 4517 goto exit; 4518 } 4519 } 4520 } 4521 4522 release_in_focus(in, ns); 4523 4524 /* 4525 * Instead of writing zeroes here, we could trust the hardware 4526 * to always provide zeroes when muted. 4527 * No need to acquire adev->lock to read mic_muted here as we don't change its state. 4528 */ 4529 if (ret == 0 && adev->mic_muted && 4530 !voice_is_in_call_rec_stream(in) && 4531 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) { 4532 memset(buffer, 0, bytes); 4533 in->frames_muted += frames; 4534 } 4535 4536 exit: 4537 pthread_mutex_unlock(&in->lock); 4538 4539 if (ret != 0) { 4540 error_log_log(in->error_log, error_code, audio_utils_get_real_time_ns()); 4541 in_standby(&in->stream.common); 4542 ALOGV("%s: read failed - sleeping for buffer duration", __func__); 4543 usleep(frames * 1000000LL / in_get_sample_rate(&in->stream.common)); 4544 memset(buffer, 0, bytes); // clear return data 4545 in->frames_muted += frames; 4546 } 4547 if (bytes > 0) { 4548 in->frames_read += frames; 4549 } 4550 return bytes; 4551 } 4552 in_get_input_frames_lost(struct audio_stream_in * stream __unused)4553 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused) 4554 { 4555 return 0; 4556 } 4557 in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)4558 static int in_get_capture_position(const struct audio_stream_in *stream, 4559 int64_t *frames, int64_t *time) 4560 { 4561 if (stream == NULL || frames == NULL || time == NULL) { 4562 return -EINVAL; 4563 } 4564 struct stream_in *in = (struct stream_in *)stream; 4565 int ret = -ENOSYS; 4566 4567 lock_input_stream(in); 4568 // note: ST sessions do not close the alsa pcm driver synchronously 4569 // on standby. Therefore, we may return an error even though the 4570 // pcm stream is still opened. 4571 if (in->standby) { 4572 ALOGE_IF(in->pcm != NULL && !(in->flags & AUDIO_INPUT_FLAG_HW_HOTWORD), 4573 "%s stream in standby but pcm not NULL for non ST session", __func__); 4574 goto exit; 4575 } 4576 if (in->pcm) { 4577 struct timespec timestamp; 4578 unsigned int avail; 4579 if (pcm_get_htimestamp(in->pcm, &avail, ×tamp) == 0) { 4580 *frames = in->frames_read + avail; 4581 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec 4582 - platform_capture_latency(in) * 1000LL; 4583 ret = 0; 4584 } 4585 } 4586 exit: 4587 pthread_mutex_unlock(&in->lock); 4588 return ret; 4589 } 4590 in_update_effect_list(bool add,effect_handle_t effect,struct listnode * head)4591 static int in_update_effect_list(bool add, effect_handle_t effect, 4592 struct listnode *head) 4593 { 4594 struct listnode *node; 4595 struct in_effect_list *elist = NULL; 4596 struct in_effect_list *target = NULL; 4597 int ret = 0; 4598 4599 if (!head) 4600 return ret; 4601 4602 list_for_each(node, head) { 4603 elist = node_to_item(node, struct in_effect_list, list); 4604 if (elist->handle == effect) { 4605 target = elist; 4606 break; 4607 } 4608 } 4609 4610 if (add) { 4611 if (target) { 4612 ALOGD("effect %p already exist", effect); 4613 return ret; 4614 } 4615 4616 target = (struct in_effect_list *) 4617 calloc(1, sizeof(struct in_effect_list)); 4618 4619 if (!target) { 4620 ALOGE("%s:fail to allocate memory", __func__); 4621 return -ENOMEM; 4622 } 4623 4624 target->handle = effect; 4625 list_add_tail(head, &target->list); 4626 } else { 4627 if (target) { 4628 list_remove(&target->list); 4629 free(target); 4630 } 4631 } 4632 4633 return ret; 4634 } 4635 add_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect,bool enable)4636 static int add_remove_audio_effect(const struct audio_stream *stream, 4637 effect_handle_t effect, 4638 bool enable) 4639 { 4640 struct stream_in *in = (struct stream_in *)stream; 4641 struct audio_device *adev = in->dev; 4642 int status = 0; 4643 effect_descriptor_t desc; 4644 4645 status = (*effect)->get_descriptor(effect, &desc); 4646 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable); 4647 4648 if (status != 0) 4649 return status; 4650 4651 lock_input_stream(in); 4652 pthread_mutex_lock(&in->dev->lock); 4653 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 4654 in->source == AUDIO_SOURCE_VOICE_RECOGNITION || 4655 adev->mode == AUDIO_MODE_IN_COMMUNICATION) && 4656 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { 4657 4658 in_update_effect_list(enable, effect, &in->aec_list); 4659 enable = !list_empty(&in->aec_list); 4660 if (enable == in->enable_aec) 4661 goto exit; 4662 4663 in->enable_aec = enable; 4664 ALOGD("AEC enable %d", enable); 4665 4666 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || 4667 adev->mode == AUDIO_MODE_IN_COMMUNICATION) { 4668 adev->enable_voicerx = enable; 4669 struct audio_usecase *usecase; 4670 struct listnode *node; 4671 list_for_each(node, &adev->usecase_list) { 4672 usecase = node_to_item(node, struct audio_usecase, list); 4673 if (usecase->type == PCM_PLAYBACK) 4674 select_devices(adev, usecase->id); 4675 } 4676 } 4677 if (!in->standby 4678 && enable_disable_effect(in->dev, in, EFFECT_AEC, enable) == -ENOSYS) 4679 select_devices(in->dev, in->usecase); 4680 } 4681 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) { 4682 4683 in_update_effect_list(enable, effect, &in->ns_list); 4684 enable = !list_empty(&in->ns_list); 4685 if (enable == in->enable_ns) 4686 goto exit; 4687 4688 in->enable_ns = enable; 4689 ALOGD("NS enable %d", enable); 4690 if (!in->standby) { 4691 if (in->source != AUDIO_SOURCE_VOICE_COMMUNICATION 4692 || enable_disable_effect(in->dev, in, EFFECT_NS, enable) == -ENOSYS) 4693 select_devices(in->dev, in->usecase); 4694 } 4695 } 4696 exit: 4697 pthread_mutex_unlock(&in->dev->lock); 4698 pthread_mutex_unlock(&in->lock); 4699 4700 return 0; 4701 } 4702 in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)4703 static int in_add_audio_effect(const struct audio_stream *stream, 4704 effect_handle_t effect) 4705 { 4706 ALOGV("%s: effect %p", __func__, effect); 4707 return add_remove_audio_effect(stream, effect, true); 4708 } 4709 in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)4710 static int in_remove_audio_effect(const struct audio_stream *stream, 4711 effect_handle_t effect) 4712 { 4713 ALOGV("%s: effect %p", __func__, effect); 4714 return add_remove_audio_effect(stream, effect, false); 4715 } 4716 in_stop(const struct audio_stream_in * stream)4717 static int in_stop(const struct audio_stream_in* stream) 4718 { 4719 struct stream_in *in = (struct stream_in *)stream; 4720 struct audio_device *adev = in->dev; 4721 4722 int ret = -ENOSYS; 4723 ALOGV("%s", __func__); 4724 pthread_mutex_lock(&adev->lock); 4725 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && 4726 in->capture_started && in->pcm != NULL) { 4727 pcm_stop(in->pcm); 4728 ret = stop_input_stream(in); 4729 in->capture_started = false; 4730 } 4731 pthread_mutex_unlock(&adev->lock); 4732 return ret; 4733 } 4734 in_start(const struct audio_stream_in * stream)4735 static int in_start(const struct audio_stream_in* stream) 4736 { 4737 struct stream_in *in = (struct stream_in *)stream; 4738 struct audio_device *adev = in->dev; 4739 int ret = -ENOSYS; 4740 4741 ALOGV("%s in %p", __func__, in); 4742 pthread_mutex_lock(&adev->lock); 4743 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && 4744 !in->capture_started && in->pcm != NULL) { 4745 if (!in->capture_started) { 4746 ret = start_input_stream(in); 4747 if (ret == 0) { 4748 in->capture_started = true; 4749 } 4750 } 4751 } 4752 pthread_mutex_unlock(&adev->lock); 4753 return ret; 4754 } 4755 4756 // Read offset for the positional timestamp from a persistent vendor property. 4757 // This is to workaround apparent inaccuracies in the timing information that 4758 // is used by the AAudio timing model. The inaccuracies can cause glitches. in_get_mmap_time_offset()4759 static int64_t in_get_mmap_time_offset() { 4760 const int32_t kDefaultOffsetMicros = 0; 4761 int32_t mmap_time_offset_micros = property_get_int32( 4762 "persist.audio.in_mmap_delay_micros", kDefaultOffsetMicros); 4763 ALOGI("in_get_mmap_time_offset set to %d micros", mmap_time_offset_micros); 4764 return mmap_time_offset_micros * (int64_t)1000; 4765 } 4766 in_create_mmap_buffer(const struct audio_stream_in * stream,int32_t min_size_frames,struct audio_mmap_buffer_info * info)4767 static int in_create_mmap_buffer(const struct audio_stream_in *stream, 4768 int32_t min_size_frames, 4769 struct audio_mmap_buffer_info *info) 4770 { 4771 struct stream_in *in = (struct stream_in *)stream; 4772 struct audio_device *adev = in->dev; 4773 int ret = 0; 4774 unsigned int offset1; 4775 unsigned int frames1; 4776 const char *step = ""; 4777 uint32_t mmap_size; 4778 uint32_t buffer_size; 4779 4780 lock_input_stream(in); 4781 pthread_mutex_lock(&adev->lock); 4782 ALOGV("%s in %p", __func__, in); 4783 4784 if (info == NULL || min_size_frames <= 0 || min_size_frames > MMAP_MIN_SIZE_FRAMES_MAX) { 4785 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames); 4786 ret = -EINVAL; 4787 goto exit; 4788 } 4789 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) { 4790 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby); 4791 ALOGV("%s in %p", __func__, in); 4792 ret = -ENOSYS; 4793 goto exit; 4794 } 4795 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); 4796 if (in->pcm_device_id < 0) { 4797 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 4798 __func__, in->pcm_device_id, in->usecase); 4799 ret = -EINVAL; 4800 goto exit; 4801 } 4802 4803 adjust_mmap_period_count(&in->config, min_size_frames); 4804 4805 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 4806 __func__, adev->snd_card, in->pcm_device_id, in->config.channels); 4807 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, 4808 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config); 4809 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { 4810 step = "open"; 4811 ret = -ENODEV; 4812 goto exit; 4813 } 4814 4815 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1); 4816 if (ret < 0) { 4817 step = "begin"; 4818 goto exit; 4819 } 4820 info->buffer_size_frames = pcm_get_buffer_size(in->pcm); 4821 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames); 4822 info->burst_size_frames = in->config.period_size; 4823 ret = platform_get_mmap_data_fd(adev->platform, 4824 in->pcm_device_id, 1 /*capture*/, 4825 &info->shared_memory_fd, 4826 &mmap_size); 4827 if (ret < 0) { 4828 // Fall back to non exclusive mode 4829 info->shared_memory_fd = pcm_get_poll_fd(in->pcm); 4830 } else { 4831 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later 4832 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd); 4833 4834 if (mmap_size < buffer_size) { 4835 step = "mmap"; 4836 goto exit; 4837 } 4838 // FIXME: indicate exclusive mode support by returning a negative buffer size 4839 info->buffer_size_frames *= -1; 4840 } 4841 4842 memset(info->shared_memory_address, 0, buffer_size); 4843 4844 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE); 4845 if (ret < 0) { 4846 step = "commit"; 4847 goto exit; 4848 } 4849 4850 in->mmap_time_offset_nanos = in_get_mmap_time_offset(); 4851 4852 in->standby = false; 4853 ret = 0; 4854 4855 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d", 4856 __func__, info->shared_memory_address, info->buffer_size_frames); 4857 4858 exit: 4859 if (ret != 0) { 4860 if (in->pcm == NULL) { 4861 ALOGE("%s: %s - %d", __func__, step, ret); 4862 } else { 4863 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm)); 4864 pcm_close(in->pcm); 4865 in->pcm = NULL; 4866 } 4867 } 4868 pthread_mutex_unlock(&adev->lock); 4869 pthread_mutex_unlock(&in->lock); 4870 return ret; 4871 } 4872 in_get_mmap_position(const struct audio_stream_in * stream,struct audio_mmap_position * position)4873 static int in_get_mmap_position(const struct audio_stream_in *stream, 4874 struct audio_mmap_position *position) 4875 { 4876 int ret = 0; 4877 struct stream_in *in = (struct stream_in *)stream; 4878 ALOGVV("%s", __func__); 4879 if (position == NULL) { 4880 return -EINVAL; 4881 } 4882 lock_input_stream(in); 4883 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || 4884 in->pcm == NULL) { 4885 ret = -ENOSYS; 4886 goto exit; 4887 } 4888 struct timespec ts = { 0, 0 }; 4889 ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts); 4890 if (ret < 0) { 4891 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); 4892 goto exit; 4893 } 4894 position->time_nanoseconds = audio_utils_ns_from_timespec(&ts) 4895 + in->mmap_time_offset_nanos; 4896 4897 exit: 4898 pthread_mutex_unlock(&in->lock); 4899 return ret; 4900 } 4901 in_get_active_microphones(const struct audio_stream_in * stream,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)4902 static int in_get_active_microphones(const struct audio_stream_in *stream, 4903 struct audio_microphone_characteristic_t *mic_array, 4904 size_t *mic_count) { 4905 struct stream_in *in = (struct stream_in *)stream; 4906 struct audio_device *adev = in->dev; 4907 ALOGVV("%s", __func__); 4908 4909 lock_input_stream(in); 4910 pthread_mutex_lock(&adev->lock); 4911 int ret = platform_get_active_microphones(adev->platform, 4912 audio_channel_count_from_in_mask(in->channel_mask), 4913 in->usecase, mic_array, mic_count); 4914 pthread_mutex_unlock(&adev->lock); 4915 pthread_mutex_unlock(&in->lock); 4916 4917 return ret; 4918 } 4919 adev_get_microphones(const struct audio_hw_device * dev,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)4920 static int adev_get_microphones(const struct audio_hw_device *dev, 4921 struct audio_microphone_characteristic_t *mic_array, 4922 size_t *mic_count) { 4923 struct audio_device *adev = (struct audio_device *)dev; 4924 ALOGVV("%s", __func__); 4925 4926 pthread_mutex_lock(&adev->lock); 4927 int ret = platform_get_microphones(adev->platform, mic_array, mic_count); 4928 pthread_mutex_unlock(&adev->lock); 4929 4930 return ret; 4931 } 4932 in_set_microphone_direction(const struct audio_stream_in * stream,audio_microphone_direction_t dir)4933 static int in_set_microphone_direction(const struct audio_stream_in *stream, 4934 audio_microphone_direction_t dir) { 4935 struct stream_in *in = (struct stream_in *)stream; 4936 4937 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir); 4938 4939 in->direction = dir; 4940 4941 if (in->standby) 4942 return 0; 4943 4944 return audio_extn_audiozoom_set_microphone_direction(in, dir); 4945 } 4946 in_set_microphone_field_dimension(const struct audio_stream_in * stream,float zoom)4947 static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) { 4948 struct stream_in *in = (struct stream_in *)stream; 4949 4950 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom); 4951 4952 if (zoom > 1.0 || zoom < -1.0) 4953 return -EINVAL; 4954 4955 in->zoom = zoom; 4956 4957 if (in->standby) 4958 return 0; 4959 4960 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom); 4961 } 4962 in_update_sink_metadata(struct audio_stream_in * stream,const struct sink_metadata * sink_metadata)4963 static void in_update_sink_metadata(struct audio_stream_in *stream, 4964 const struct sink_metadata *sink_metadata) { 4965 4966 if (stream == NULL 4967 || sink_metadata == NULL 4968 || sink_metadata->tracks == NULL) { 4969 return; 4970 } 4971 4972 int error = 0; 4973 struct stream_in *in = (struct stream_in *)stream; 4974 struct audio_device *adev = in->dev; 4975 audio_devices_t device = AUDIO_DEVICE_NONE; 4976 4977 if (sink_metadata->track_count != 0) 4978 device = sink_metadata->tracks->dest_device; 4979 4980 lock_input_stream(in); 4981 pthread_mutex_lock(&adev->lock); 4982 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, device); 4983 4984 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY 4985 && device != AUDIO_DEVICE_NONE 4986 && adev->voice_tx_output != NULL) { 4987 /* Use the rx device from afe-proxy record to route voice call because 4988 there is no routing if tx device is on primary hal and rx device 4989 is on other hal during voice call. */ 4990 adev->voice_tx_output->devices = device; 4991 4992 if (!voice_is_call_state_active(adev)) { 4993 if (adev->mode == AUDIO_MODE_IN_CALL) { 4994 adev->current_call_output = adev->voice_tx_output; 4995 error = voice_start_call(adev); 4996 if (error != 0) 4997 ALOGE("%s: start voice call failed %d", __func__, error); 4998 } 4999 } else { 5000 adev->current_call_output = adev->voice_tx_output; 5001 voice_update_devices_for_all_voice_usecases(adev); 5002 } 5003 } 5004 5005 pthread_mutex_unlock(&adev->lock); 5006 pthread_mutex_unlock(&in->lock); 5007 } 5008 check_and_set_gapless_mode(struct audio_device * adev)5009 static int check_and_set_gapless_mode(struct audio_device *adev) 5010 { 5011 bool gapless_enabled = false; 5012 const char *mixer_ctl_name = "Compress Gapless Playback"; 5013 struct mixer_ctl *ctl; 5014 5015 ALOGV("%s:", __func__); 5016 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false); 5017 5018 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 5019 if (!ctl) { 5020 ALOGE("%s: Could not get ctl for mixer cmd - %s", 5021 __func__, mixer_ctl_name); 5022 return -EINVAL; 5023 } 5024 5025 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) { 5026 ALOGE("%s: Could not set gapless mode %d", 5027 __func__, gapless_enabled); 5028 return -EINVAL; 5029 } 5030 return 0; 5031 } 5032 adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address __unused)5033 static int adev_open_output_stream(struct audio_hw_device *dev, 5034 audio_io_handle_t handle, 5035 audio_devices_t devices, 5036 audio_output_flags_t flags, 5037 struct audio_config *config, 5038 struct audio_stream_out **stream_out, 5039 const char *address __unused) 5040 { 5041 struct audio_device *adev = (struct audio_device *)dev; 5042 struct stream_out *out; 5043 int i, ret = 0; 5044 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL; 5045 bool is_usb_dev = audio_is_usb_out_device(devices) && 5046 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY); 5047 bool force_haptic_path = 5048 property_get_bool("vendor.audio.test_haptic", false); 5049 5050 if (is_usb_dev && !is_usb_ready(adev, true /* is_playback */)) { 5051 return -ENOSYS; 5052 } 5053 5054 ALOGV("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", 5055 __func__, config->format, config->sample_rate, config->channel_mask, devices, flags); 5056 5057 *stream_out = NULL; 5058 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 5059 5060 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL); 5061 5062 if (devices == AUDIO_DEVICE_NONE) 5063 devices = AUDIO_DEVICE_OUT_SPEAKER; 5064 5065 out->flags = flags; 5066 out->devices = devices; 5067 out->dev = adev; 5068 out->handle = handle; 5069 out->a2dp_compress_mute = false; 5070 out->mmap_shared_memory_fd = -1; // not open 5071 5072 /* Init use case and pcm_config */ 5073 if ((is_hdmi || is_usb_dev) && 5074 (audio_is_linear_pcm(config->format) || config->format == AUDIO_FORMAT_DEFAULT) && 5075 (flags == AUDIO_OUTPUT_FLAG_NONE || 5076 (flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0)) { 5077 audio_format_t req_format = config->format; 5078 audio_channel_mask_t req_channel_mask = config->channel_mask; 5079 uint32_t req_sample_rate = config->sample_rate; 5080 5081 pthread_mutex_lock(&adev->lock); 5082 if (is_hdmi) { 5083 ret = read_hdmi_channel_masks(out); 5084 if (config->sample_rate == 0) 5085 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 5086 if (config->channel_mask == AUDIO_CHANNEL_NONE) 5087 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; 5088 if (config->format == AUDIO_FORMAT_DEFAULT) 5089 config->format = AUDIO_FORMAT_PCM_16_BIT; 5090 } else if (is_usb_dev) { 5091 ret = read_usb_sup_params_and_compare(true /*is_playback*/, 5092 &config->format, 5093 &out->supported_formats[0], 5094 MAX_SUPPORTED_FORMATS, 5095 &config->channel_mask, 5096 &out->supported_channel_masks[0], 5097 MAX_SUPPORTED_CHANNEL_MASKS, 5098 &config->sample_rate, 5099 &out->supported_sample_rates[0], 5100 MAX_SUPPORTED_SAMPLE_RATES); 5101 ALOGV("plugged dev USB ret %d", ret); 5102 } 5103 pthread_mutex_unlock(&adev->lock); 5104 if (ret != 0) { 5105 // For MMAP NO IRQ, allow conversions in ADSP 5106 if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) 5107 goto error_open; 5108 5109 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate) 5110 config->sample_rate = req_sample_rate; 5111 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask) 5112 config->channel_mask = req_channel_mask; 5113 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format) 5114 config->format = req_format; 5115 } 5116 5117 out->sample_rate = config->sample_rate; 5118 out->channel_mask = config->channel_mask; 5119 out->format = config->format; 5120 if (is_hdmi) { 5121 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; 5122 out->config = pcm_config_hdmi_multi; 5123 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { 5124 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; 5125 out->config = pcm_config_mmap_playback; 5126 out->stream.start = out_start; 5127 out->stream.stop = out_stop; 5128 out->stream.create_mmap_buffer = out_create_mmap_buffer; 5129 out->stream.get_mmap_position = out_get_mmap_position; 5130 } else { 5131 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; 5132 out->config = pcm_config_hifi; 5133 } 5134 5135 out->config.rate = out->sample_rate; 5136 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask); 5137 if (is_hdmi) { 5138 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 5139 audio_bytes_per_sample(out->format)); 5140 } 5141 out->config.format = pcm_format_from_audio_format(out->format); 5142 } else if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 5143 pthread_mutex_lock(&adev->lock); 5144 bool offline = (adev->card_status == CARD_STATUS_OFFLINE); 5145 pthread_mutex_unlock(&adev->lock); 5146 5147 // reject offload during card offline to allow 5148 // fallback to s/w paths 5149 if (offline) { 5150 ret = -ENODEV; 5151 goto error_open; 5152 } 5153 5154 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || 5155 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { 5156 ALOGE("%s: Unsupported Offload information", __func__); 5157 ret = -EINVAL; 5158 goto error_open; 5159 } 5160 if (!is_supported_format(config->offload_info.format)) { 5161 ALOGE("%s: Unsupported audio format", __func__); 5162 ret = -EINVAL; 5163 goto error_open; 5164 } 5165 out->sample_rate = config->offload_info.sample_rate; 5166 if (config->offload_info.channel_mask != AUDIO_CHANNEL_NONE) 5167 out->channel_mask = config->offload_info.channel_mask; 5168 else if (config->channel_mask != AUDIO_CHANNEL_NONE) 5169 out->channel_mask = config->channel_mask; 5170 else 5171 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 5172 5173 out->format = config->offload_info.format; 5174 5175 out->compr_config.codec = (struct snd_codec *) 5176 calloc(1, sizeof(struct snd_codec)); 5177 5178 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; 5179 5180 out->stream.set_callback = out_set_callback; 5181 out->stream.pause = out_pause; 5182 out->stream.resume = out_resume; 5183 out->stream.drain = out_drain; 5184 out->stream.flush = out_flush; 5185 5186 out->compr_config.codec->id = 5187 get_snd_codec_id(config->offload_info.format); 5188 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; 5189 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; 5190 out->compr_config.codec->sample_rate = out->sample_rate; 5191 out->compr_config.codec->bit_rate = 5192 config->offload_info.bit_rate; 5193 out->compr_config.codec->ch_in = 5194 audio_channel_count_from_out_mask(out->channel_mask); 5195 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; 5196 5197 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) 5198 out->non_blocking = 1; 5199 5200 out->send_new_metadata = 1; 5201 5202 check_and_set_gapless_mode(adev); 5203 5204 create_offload_callback_thread(out); 5205 ALOGV("%s: offloaded output offload_info version %04x bit rate %d", 5206 __func__, config->offload_info.version, 5207 config->offload_info.bit_rate); 5208 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) { 5209 switch (config->sample_rate) { 5210 case 0: 5211 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 5212 break; 5213 case 8000: 5214 case 16000: 5215 case 48000: 5216 out->sample_rate = config->sample_rate; 5217 break; 5218 default: 5219 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__, 5220 config->sample_rate); 5221 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 5222 ret = -EINVAL; 5223 goto error_open; 5224 } 5225 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it 5226 switch (config->channel_mask) { 5227 case AUDIO_CHANNEL_NONE: 5228 case AUDIO_CHANNEL_OUT_STEREO: 5229 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 5230 break; 5231 default: 5232 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__, 5233 config->channel_mask); 5234 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 5235 ret = -EINVAL; 5236 goto error_open; 5237 } 5238 switch (config->format) { 5239 case AUDIO_FORMAT_DEFAULT: 5240 case AUDIO_FORMAT_PCM_16_BIT: 5241 out->format = AUDIO_FORMAT_PCM_16_BIT; 5242 break; 5243 default: 5244 ALOGE("%s: Unsupported format %#x for Incall Music", __func__, 5245 config->format); 5246 config->format = AUDIO_FORMAT_PCM_16_BIT; 5247 ret = -EINVAL; 5248 goto error_open; 5249 } 5250 5251 voice_extn_check_and_set_incall_music_usecase(adev, out); 5252 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) { 5253 switch (config->sample_rate) { 5254 case 0: 5255 out->sample_rate = AFE_PROXY_SAMPLING_RATE; 5256 break; 5257 case 8000: 5258 case 16000: 5259 case 48000: 5260 out->sample_rate = config->sample_rate; 5261 break; 5262 default: 5263 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__, 5264 config->sample_rate); 5265 config->sample_rate = AFE_PROXY_SAMPLING_RATE; 5266 ret = -EINVAL; 5267 break; 5268 } 5269 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it 5270 switch (config->channel_mask) { 5271 case AUDIO_CHANNEL_NONE: 5272 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 5273 break; 5274 case AUDIO_CHANNEL_OUT_STEREO: 5275 out->channel_mask = config->channel_mask; 5276 break; 5277 default: 5278 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__, 5279 config->channel_mask); 5280 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 5281 ret = -EINVAL; 5282 break; 5283 } 5284 switch (config->format) { 5285 case AUDIO_FORMAT_DEFAULT: 5286 out->format = AUDIO_FORMAT_PCM_16_BIT; 5287 break; 5288 case AUDIO_FORMAT_PCM_16_BIT: 5289 out->format = config->format; 5290 break; 5291 default: 5292 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__, 5293 config->format); 5294 config->format = AUDIO_FORMAT_PCM_16_BIT; 5295 ret = -EINVAL; 5296 break; 5297 } 5298 if (ret != 0) 5299 goto error_open; 5300 5301 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY; 5302 out->config = pcm_config_afe_proxy_playback; 5303 out->config.rate = out->sample_rate; 5304 out->config.channels = 5305 audio_channel_count_from_out_mask(out->channel_mask); 5306 out->config.format = pcm_format_from_audio_format(out->format); 5307 adev->voice_tx_output = out; 5308 } else if (flags == AUDIO_OUTPUT_FLAG_VOIP_RX) { 5309 switch (config->sample_rate) { 5310 case 0: 5311 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 5312 break; 5313 case 8000: 5314 case 16000: 5315 case 32000: 5316 case 48000: 5317 out->sample_rate = config->sample_rate; 5318 break; 5319 default: 5320 ALOGE("%s: Unsupported sampling rate %d for Voip RX", __func__, 5321 config->sample_rate); 5322 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 5323 ret = -EINVAL; 5324 break; 5325 } 5326 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it 5327 switch (config->channel_mask) { 5328 case AUDIO_CHANNEL_NONE: 5329 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 5330 break; 5331 case AUDIO_CHANNEL_OUT_STEREO: 5332 out->channel_mask = config->channel_mask; 5333 break; 5334 default: 5335 ALOGE("%s: Unsupported channel mask %#x for Voip RX", __func__, 5336 config->channel_mask); 5337 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 5338 ret = -EINVAL; 5339 break; 5340 } 5341 switch (config->format) { 5342 case AUDIO_FORMAT_DEFAULT: 5343 out->format = AUDIO_FORMAT_PCM_16_BIT; 5344 break; 5345 case AUDIO_FORMAT_PCM_16_BIT: 5346 out->format = config->format; 5347 break; 5348 default: 5349 ALOGE("%s: Unsupported format %#x for Voip RX", __func__, 5350 config->format); 5351 config->format = AUDIO_FORMAT_PCM_16_BIT; 5352 ret = -EINVAL; 5353 break; 5354 } 5355 if (ret != 0) 5356 goto error_open; 5357 5358 uint32_t buffer_size, frame_size; 5359 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP; 5360 out->config = pcm_config_voip; 5361 out->config.rate = out->sample_rate; 5362 out->config.format = pcm_format_from_audio_format(out->format); 5363 buffer_size = get_stream_buffer_size(VOIP_PLAYBACK_PERIOD_DURATION_MSEC, 5364 out->sample_rate, 5365 out->format, 5366 out->config.channels, 5367 false /*is_low_latency*/); 5368 frame_size = audio_bytes_per_sample(out->format) * out->config.channels; 5369 out->config.period_size = buffer_size / frame_size; 5370 out->config.period_count = VOIP_PLAYBACK_PERIOD_COUNT; 5371 out->af_period_multiplier = 1; 5372 } else { 5373 if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 5374 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; 5375 out->config = pcm_config_deep_buffer; 5376 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) { 5377 out->usecase = USECASE_AUDIO_PLAYBACK_TTS; 5378 out->config = pcm_config_deep_buffer; 5379 } else if (flags & AUDIO_OUTPUT_FLAG_RAW) { 5380 out->usecase = USECASE_AUDIO_PLAYBACK_ULL; 5381 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, out->flags); 5382 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency; 5383 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { 5384 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; 5385 out->config = pcm_config_mmap_playback; 5386 out->stream.start = out_start; 5387 out->stream.stop = out_stop; 5388 out->stream.create_mmap_buffer = out_create_mmap_buffer; 5389 out->stream.get_mmap_position = out_get_mmap_position; 5390 } else { 5391 if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) { 5392 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS; 5393 adev->haptic_pcm_device_id = platform_get_haptics_pcm_device_id(); 5394 if (adev->haptic_pcm_device_id < 0) { 5395 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)", 5396 __func__, adev->haptic_pcm_device_id, out->usecase); 5397 ret = -ENOSYS; 5398 goto error_open; 5399 } 5400 out->config = pcm_config_haptics_audio; 5401 if (force_haptic_path) 5402 adev->haptics_config = pcm_config_haptics_audio; 5403 else 5404 adev->haptics_config = pcm_config_haptics; 5405 } else { 5406 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; 5407 out->config = pcm_config_low_latency; 5408 } 5409 } 5410 5411 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || 5412 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER || 5413 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) { 5414 out->volume_l = 1.0; 5415 out->volume_r = 1.0; 5416 } 5417 5418 if (config->sample_rate == 0) { 5419 out->sample_rate = out->config.rate; 5420 } else { 5421 out->sample_rate = config->sample_rate; 5422 } 5423 5424 if (config->channel_mask == AUDIO_CHANNEL_NONE) { 5425 out->channel_mask = audio_channel_out_mask_from_count(out->config.channels); 5426 } else { 5427 out->channel_mask = config->channel_mask; 5428 } 5429 5430 if (config->format == AUDIO_FORMAT_DEFAULT) 5431 out->format = audio_format_from_pcm_format(out->config.format); 5432 else if (!audio_is_linear_pcm(config->format)) { 5433 config->format = AUDIO_FORMAT_PCM_16_BIT; 5434 ret = -EINVAL; 5435 goto error_open; 5436 } else { 5437 out->format = config->format; 5438 } 5439 5440 out->config.rate = out->sample_rate; 5441 5442 if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) { 5443 out->config.channels = 5444 audio_channel_count_from_out_mask(out->channel_mask & 5445 ~AUDIO_CHANNEL_HAPTIC_ALL); 5446 5447 if (force_haptic_path) { 5448 out->config.channels = 1; 5449 adev->haptics_config.channels = 1; 5450 } else { 5451 adev->haptics_config.channels = 5452 audio_channel_count_from_out_mask(out->channel_mask & 5453 AUDIO_CHANNEL_HAPTIC_ALL); 5454 } 5455 } else { 5456 out->config.channels = 5457 audio_channel_count_from_out_mask(out->channel_mask); 5458 } 5459 5460 if (out->format != audio_format_from_pcm_format(out->config.format)) { 5461 out->config.format = pcm_format_from_audio_format(out->format); 5462 } 5463 } 5464 5465 if ((config->sample_rate != 0 && config->sample_rate != out->sample_rate) || 5466 (config->format != AUDIO_FORMAT_DEFAULT && config->format != out->format) || 5467 (config->channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != out->channel_mask)) { 5468 ALOGI("%s: Unsupported output config. sample_rate:%u format:%#x channel_mask:%#x", 5469 __func__, config->sample_rate, config->format, config->channel_mask); 5470 config->sample_rate = out->sample_rate; 5471 config->format = out->format; 5472 config->channel_mask = out->channel_mask; 5473 ret = -EINVAL; 5474 goto error_open; 5475 } 5476 5477 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n", 5478 __func__, use_case_table[out->usecase], config->format, out->config.format); 5479 5480 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { 5481 if (adev->primary_output == NULL) 5482 adev->primary_output = out; 5483 else { 5484 ALOGE("%s: Primary output is already opened", __func__); 5485 ret = -EEXIST; 5486 goto error_open; 5487 } 5488 } 5489 5490 /* Check if this usecase is already existing */ 5491 pthread_mutex_lock(&adev->lock); 5492 if (get_usecase_from_list(adev, out->usecase) != NULL) { 5493 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); 5494 pthread_mutex_unlock(&adev->lock); 5495 ret = -EEXIST; 5496 goto error_open; 5497 } 5498 pthread_mutex_unlock(&adev->lock); 5499 5500 out->applied_volume_l = INVALID_OUT_VOLUME; 5501 out->applied_volume_r = INVALID_OUT_VOLUME; 5502 5503 out->stream.common.get_sample_rate = out_get_sample_rate; 5504 out->stream.common.set_sample_rate = out_set_sample_rate; 5505 out->stream.common.get_buffer_size = out_get_buffer_size; 5506 out->stream.common.get_channels = out_get_channels; 5507 out->stream.common.get_format = out_get_format; 5508 out->stream.common.set_format = out_set_format; 5509 out->stream.common.standby = out_standby; 5510 out->stream.common.dump = out_dump; 5511 out->stream.common.set_parameters = out_set_parameters; 5512 out->stream.common.get_parameters = out_get_parameters; 5513 out->stream.common.add_audio_effect = out_add_audio_effect; 5514 out->stream.common.remove_audio_effect = out_remove_audio_effect; 5515 out->stream.get_latency = out_get_latency; 5516 out->stream.set_volume = out_set_volume; 5517 #ifdef NO_AUDIO_OUT 5518 out->stream.write = out_write_for_no_output; 5519 #else 5520 out->stream.write = out_write; 5521 #endif 5522 out->stream.get_render_position = out_get_render_position; 5523 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 5524 out->stream.get_presentation_position = out_get_presentation_position; 5525 5526 if (out->realtime) 5527 out->af_period_multiplier = af_period_multiplier; 5528 else 5529 out->af_period_multiplier = 1; 5530 5531 out->kernel_buffer_size = out->config.period_size * out->config.period_count; 5532 5533 out->standby = 1; 5534 /* out->muted = false; by calloc() */ 5535 /* out->written = 0; by calloc() */ 5536 5537 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); 5538 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL); 5539 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); 5540 5541 config->format = out->stream.common.get_format(&out->stream.common); 5542 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 5543 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 5544 5545 register_format(out->format, out->supported_formats); 5546 register_channel_mask(out->channel_mask, out->supported_channel_masks); 5547 register_sample_rate(out->sample_rate, out->supported_sample_rates); 5548 5549 out->error_log = error_log_create( 5550 ERROR_LOG_ENTRIES, 5551 1000000000 /* aggregate consecutive identical errors within one second in ns */); 5552 5553 /* 5554 By locking output stream before registering, we allow the callback 5555 to update stream's state only after stream's initial state is set to 5556 adev state. 5557 */ 5558 lock_output_stream(out); 5559 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb); 5560 pthread_mutex_lock(&adev->lock); 5561 out->card_status = adev->card_status; 5562 pthread_mutex_unlock(&adev->lock); 5563 pthread_mutex_unlock(&out->lock); 5564 5565 stream_app_type_cfg_init(&out->app_type_cfg); 5566 5567 *stream_out = &out->stream; 5568 5569 ALOGV("%s: exit", __func__); 5570 return 0; 5571 5572 error_open: 5573 free(out); 5574 *stream_out = NULL; 5575 ALOGW("%s: exit: ret %d", __func__, ret); 5576 return ret; 5577 } 5578 adev_close_output_stream(struct audio_hw_device * dev __unused,struct audio_stream_out * stream)5579 static void adev_close_output_stream(struct audio_hw_device *dev __unused, 5580 struct audio_stream_out *stream) 5581 { 5582 struct stream_out *out = (struct stream_out *)stream; 5583 struct audio_device *adev = out->dev; 5584 5585 ALOGV("%s: enter", __func__); 5586 5587 // must deregister from sndmonitor first to prevent races 5588 // between the callback and close_stream 5589 audio_extn_snd_mon_unregister_listener(out); 5590 out_standby(&stream->common); 5591 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 5592 destroy_offload_callback_thread(out); 5593 5594 if (out->compr_config.codec != NULL) 5595 free(out->compr_config.codec); 5596 } 5597 5598 out->a2dp_compress_mute = false; 5599 5600 if (adev->voice_tx_output == out) 5601 adev->voice_tx_output = NULL; 5602 5603 error_log_destroy(out->error_log); 5604 out->error_log = NULL; 5605 5606 pthread_cond_destroy(&out->cond); 5607 pthread_mutex_destroy(&out->pre_lock); 5608 pthread_mutex_destroy(&out->lock); 5609 free(stream); 5610 ALOGV("%s: exit", __func__); 5611 } 5612 adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)5613 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 5614 { 5615 struct audio_device *adev = (struct audio_device *)dev; 5616 struct str_parms *parms; 5617 char *str; 5618 char value[32]; 5619 int val; 5620 int ret; 5621 int status = 0; 5622 bool a2dp_reconfig = false; 5623 5624 ALOGV("%s: enter: %s", __func__, kvpairs); 5625 5626 pthread_mutex_lock(&adev->lock); 5627 5628 parms = str_parms_create_str(kvpairs); 5629 status = voice_set_parameters(adev, parms); 5630 if (status != 0) { 5631 goto done; 5632 } 5633 5634 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 5635 if (ret >= 0) { 5636 /* When set to false, HAL should disable EC and NS */ 5637 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 5638 adev->bluetooth_nrec = true; 5639 else 5640 adev->bluetooth_nrec = false; 5641 } 5642 5643 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 5644 if (ret >= 0) { 5645 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 5646 adev->screen_off = false; 5647 else 5648 adev->screen_off = true; 5649 } 5650 5651 ret = str_parms_get_int(parms, "rotation", &val); 5652 if (ret >= 0) { 5653 bool reverse_speakers = false; 5654 int camera_rotation = CAMERA_ROTATION_LANDSCAPE; 5655 switch (val) { 5656 // FIXME: note that the code below assumes that the speakers are in the correct placement 5657 // relative to the user when the device is rotated 90deg from its default rotation. This 5658 // assumption is device-specific, not platform-specific like this code. 5659 case 270: 5660 reverse_speakers = true; 5661 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE; 5662 break; 5663 case 0: 5664 case 180: 5665 camera_rotation = CAMERA_ROTATION_PORTRAIT; 5666 break; 5667 case 90: 5668 camera_rotation = CAMERA_ROTATION_LANDSCAPE; 5669 break; 5670 default: 5671 ALOGE("%s: unexpected rotation of %d", __func__, val); 5672 status = -EINVAL; 5673 } 5674 if (status == 0) { 5675 // check and set swap 5676 // - check if orientation changed and speaker active 5677 // - set rotation and cache the rotation value 5678 adev->camera_orientation = 5679 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation; 5680 #ifndef MAXXAUDIO_QDSP_ENABLED 5681 platform_check_and_set_swap_lr_channels(adev, reverse_speakers); 5682 #endif 5683 } 5684 } 5685 5686 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value)); 5687 if (ret >= 0) { 5688 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON); 5689 } 5690 5691 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value)); 5692 if (ret >= 0) { 5693 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 5694 adev->bt_sco_on = true; 5695 else 5696 adev->bt_sco_on = false; 5697 } 5698 5699 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value)); 5700 if (ret >= 0) { 5701 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); 5702 if (audio_is_usb_out_device(device)) { 5703 ret = str_parms_get_str(parms, "card", value, sizeof(value)); 5704 if (ret >= 0) { 5705 const int card = atoi(value); 5706 audio_extn_usb_add_device(device, card); 5707 } 5708 } else if (audio_is_usb_in_device(device)) { 5709 ret = str_parms_get_str(parms, "card", value, sizeof(value)); 5710 if (ret >= 0) { 5711 const int card = atoi(value); 5712 audio_extn_usb_add_device(device, card); 5713 } 5714 } 5715 } 5716 5717 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value)); 5718 if (ret >= 0) { 5719 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); 5720 if (audio_is_usb_out_device(device)) { 5721 ret = str_parms_get_str(parms, "card", value, sizeof(value)); 5722 if (ret >= 0) { 5723 const int card = atoi(value); 5724 audio_extn_usb_remove_device(device, card); 5725 } 5726 } else if (audio_is_usb_in_device(device)) { 5727 ret = str_parms_get_str(parms, "card", value, sizeof(value)); 5728 if (ret >= 0) { 5729 const int card = atoi(value); 5730 audio_extn_usb_remove_device(device, card); 5731 } 5732 } 5733 } 5734 5735 audio_extn_hfp_set_parameters(adev, parms); 5736 audio_extn_ma_set_parameters(adev, parms); 5737 5738 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig); 5739 if (status >= 0 && a2dp_reconfig) { 5740 struct audio_usecase *usecase; 5741 struct listnode *node; 5742 list_for_each(node, &adev->usecase_list) { 5743 usecase = node_to_item(node, struct audio_usecase, list); 5744 if ((usecase->type == PCM_PLAYBACK) && 5745 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { 5746 ALOGD("%s: reconfigure A2DP... forcing device switch", __func__); 5747 5748 pthread_mutex_unlock(&adev->lock); 5749 lock_output_stream(usecase->stream.out); 5750 pthread_mutex_lock(&adev->lock); 5751 audio_extn_a2dp_set_handoff_mode(true); 5752 // force device switch to reconfigure encoder 5753 select_devices(adev, usecase->id); 5754 audio_extn_a2dp_set_handoff_mode(false); 5755 pthread_mutex_unlock(&usecase->stream.out->lock); 5756 break; 5757 } 5758 } 5759 } 5760 5761 //FIXME: to be replaced by proper video capture properties API 5762 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value)); 5763 if (ret >= 0) { 5764 int camera_facing = CAMERA_FACING_BACK; 5765 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0) 5766 camera_facing = CAMERA_FACING_FRONT; 5767 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0) 5768 camera_facing = CAMERA_FACING_BACK; 5769 else { 5770 ALOGW("%s: invalid camera facing value: %s", __func__, value); 5771 goto done; 5772 } 5773 adev->camera_orientation = 5774 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing; 5775 struct audio_usecase *usecase; 5776 struct listnode *node; 5777 list_for_each(node, &adev->usecase_list) { 5778 usecase = node_to_item(node, struct audio_usecase, list); 5779 struct stream_in *in = usecase->stream.in; 5780 if (usecase->type == PCM_CAPTURE && in != NULL && 5781 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) { 5782 select_devices(adev, in->usecase); 5783 } 5784 } 5785 } 5786 5787 done: 5788 str_parms_destroy(parms); 5789 pthread_mutex_unlock(&adev->lock); 5790 ALOGV("%s: exit with code(%d)", __func__, status); 5791 return status; 5792 } 5793 adev_get_parameters(const struct audio_hw_device * dev,const char * keys)5794 static char* adev_get_parameters(const struct audio_hw_device *dev, 5795 const char *keys) 5796 { 5797 struct audio_device *adev = (struct audio_device *)dev; 5798 struct str_parms *reply = str_parms_create(); 5799 struct str_parms *query = str_parms_create_str(keys); 5800 char *str; 5801 5802 pthread_mutex_lock(&adev->lock); 5803 5804 voice_get_parameters(adev, query, reply); 5805 audio_extn_a2dp_get_parameters(query, reply); 5806 5807 str = str_parms_to_str(reply); 5808 str_parms_destroy(query); 5809 str_parms_destroy(reply); 5810 5811 pthread_mutex_unlock(&adev->lock); 5812 ALOGV("%s: exit: returns - %s", __func__, str); 5813 return str; 5814 } 5815 adev_init_check(const struct audio_hw_device * dev __unused)5816 static int adev_init_check(const struct audio_hw_device *dev __unused) 5817 { 5818 return 0; 5819 } 5820 adev_set_voice_volume(struct audio_hw_device * dev,float volume)5821 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 5822 { 5823 int ret; 5824 struct audio_device *adev = (struct audio_device *)dev; 5825 5826 audio_extn_extspk_set_voice_vol(adev->extspk, volume); 5827 5828 pthread_mutex_lock(&adev->lock); 5829 ret = voice_set_volume(adev, volume); 5830 pthread_mutex_unlock(&adev->lock); 5831 5832 return ret; 5833 } 5834 adev_set_master_volume(struct audio_hw_device * dev __unused,float volume __unused)5835 static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused) 5836 { 5837 return -ENOSYS; 5838 } 5839 adev_get_master_volume(struct audio_hw_device * dev __unused,float * volume __unused)5840 static int adev_get_master_volume(struct audio_hw_device *dev __unused, 5841 float *volume __unused) 5842 { 5843 return -ENOSYS; 5844 } 5845 adev_set_master_mute(struct audio_hw_device * dev __unused,bool muted __unused)5846 static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused) 5847 { 5848 return -ENOSYS; 5849 } 5850 adev_get_master_mute(struct audio_hw_device * dev __unused,bool * muted __unused)5851 static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused) 5852 { 5853 return -ENOSYS; 5854 } 5855 adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)5856 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 5857 { 5858 struct audio_device *adev = (struct audio_device *)dev; 5859 5860 pthread_mutex_lock(&adev->lock); 5861 if (adev->mode != mode) { 5862 ALOGD("%s: mode %d", __func__, (int)mode); 5863 adev->mode = mode; 5864 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) && 5865 voice_is_in_call(adev)) { 5866 voice_stop_call(adev); 5867 adev->current_call_output = NULL; 5868 5869 /* 5870 * After stopping the call, it must check if any active capture 5871 * activity device needs to be re-selected. 5872 */ 5873 struct audio_usecase *usecase; 5874 struct listnode *node; 5875 list_for_each(node, &adev->usecase_list) { 5876 usecase = node_to_item(node, struct audio_usecase, list); 5877 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) { 5878 select_devices_with_force_switch(adev, usecase->id, true); 5879 } 5880 } 5881 } 5882 } 5883 pthread_mutex_unlock(&adev->lock); 5884 5885 audio_extn_extspk_set_mode(adev->extspk, mode); 5886 5887 return 0; 5888 } 5889 adev_set_mic_mute(struct audio_hw_device * dev,bool state)5890 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 5891 { 5892 int ret; 5893 struct audio_device *adev = (struct audio_device *)dev; 5894 5895 ALOGD("%s: state %d", __func__, (int)state); 5896 pthread_mutex_lock(&adev->lock); 5897 if (audio_extn_tfa_98xx_is_supported() && adev->enable_hfp) { 5898 ret = audio_extn_hfp_set_mic_mute(adev, state); 5899 } else { 5900 ret = voice_set_mic_mute(adev, state); 5901 } 5902 adev->mic_muted = state; 5903 pthread_mutex_unlock(&adev->lock); 5904 5905 return ret; 5906 } 5907 adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)5908 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 5909 { 5910 *state = voice_get_mic_mute((struct audio_device *)dev); 5911 return 0; 5912 } 5913 adev_get_input_buffer_size(const struct audio_hw_device * dev __unused,const struct audio_config * config)5914 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused, 5915 const struct audio_config *config) 5916 { 5917 int channel_count = audio_channel_count_from_in_mask(config->channel_mask); 5918 5919 /* Don't know if USB HIFI in this context so use true to be conservative */ 5920 if (check_input_parameters(config->sample_rate, config->format, channel_count, 5921 true /*is_usb_hifi */) != 0) 5922 return 0; 5923 5924 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, 5925 config->sample_rate, config->format, 5926 channel_count, 5927 false /* is_low_latency: since we don't know, be conservative */); 5928 } 5929 adev_input_allow_hifi_record(struct audio_device * adev,audio_devices_t devices,audio_input_flags_t flags,audio_source_t source)5930 static bool adev_input_allow_hifi_record(struct audio_device *adev, 5931 audio_devices_t devices, 5932 audio_input_flags_t flags, 5933 audio_source_t source) { 5934 const bool allowed = true; 5935 5936 if (!audio_is_usb_in_device(devices)) 5937 return !allowed; 5938 5939 switch (flags) { 5940 case AUDIO_INPUT_FLAG_NONE: 5941 case AUDIO_INPUT_FLAG_FAST: // just fast, not fast|raw || fast|mmap 5942 break; 5943 default: 5944 return !allowed; 5945 } 5946 5947 switch (source) { 5948 case AUDIO_SOURCE_DEFAULT: 5949 case AUDIO_SOURCE_MIC: 5950 case AUDIO_SOURCE_UNPROCESSED: 5951 break; 5952 default: 5953 return !allowed; 5954 } 5955 5956 switch (adev->mode) { 5957 case 0: 5958 break; 5959 default: 5960 return !allowed; 5961 } 5962 5963 return allowed; 5964 } 5965 adev_open_input_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags,const char * address __unused,audio_source_t source)5966 static int adev_open_input_stream(struct audio_hw_device *dev, 5967 audio_io_handle_t handle, 5968 audio_devices_t devices, 5969 struct audio_config *config, 5970 struct audio_stream_in **stream_in, 5971 audio_input_flags_t flags, 5972 const char *address __unused, 5973 audio_source_t source ) 5974 { 5975 struct audio_device *adev = (struct audio_device *)dev; 5976 struct stream_in *in; 5977 int ret = 0, buffer_size, frame_size; 5978 int channel_count; 5979 bool is_low_latency = false; 5980 bool is_usb_dev = audio_is_usb_in_device(devices); 5981 bool may_use_hifi_record = adev_input_allow_hifi_record(adev, 5982 devices, 5983 flags, 5984 source); 5985 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d," 5986 " sample_rate %u, channel_mask %#x, format %#x", 5987 __func__, flags, is_usb_dev, may_use_hifi_record, 5988 config->sample_rate, config->channel_mask, config->format); 5989 *stream_in = NULL; 5990 5991 if (is_usb_dev && !is_usb_ready(adev, false /* is_playback */)) { 5992 return -ENOSYS; 5993 } 5994 5995 if (!(is_usb_dev && may_use_hifi_record)) { 5996 if (config->sample_rate == 0) 5997 config->sample_rate = DEFAULT_INPUT_SAMPLING_RATE; 5998 if (config->channel_mask == AUDIO_CHANNEL_NONE) 5999 config->channel_mask = AUDIO_CHANNEL_IN_MONO; 6000 if (config->format == AUDIO_FORMAT_DEFAULT) 6001 config->format = AUDIO_FORMAT_PCM_16_BIT; 6002 6003 channel_count = audio_channel_count_from_in_mask(config->channel_mask); 6004 6005 if (check_input_parameters(config->sample_rate, config->format, channel_count, false) != 0) 6006 return -EINVAL; 6007 } 6008 6009 if (audio_extn_tfa_98xx_is_supported() && 6010 (audio_extn_hfp_is_active(adev) || voice_is_in_call(adev))) 6011 return -EINVAL; 6012 6013 in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 6014 6015 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); 6016 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL); 6017 6018 in->stream.common.get_sample_rate = in_get_sample_rate; 6019 in->stream.common.set_sample_rate = in_set_sample_rate; 6020 in->stream.common.get_buffer_size = in_get_buffer_size; 6021 in->stream.common.get_channels = in_get_channels; 6022 in->stream.common.get_format = in_get_format; 6023 in->stream.common.set_format = in_set_format; 6024 in->stream.common.standby = in_standby; 6025 in->stream.common.dump = in_dump; 6026 in->stream.common.set_parameters = in_set_parameters; 6027 in->stream.common.get_parameters = in_get_parameters; 6028 in->stream.common.add_audio_effect = in_add_audio_effect; 6029 in->stream.common.remove_audio_effect = in_remove_audio_effect; 6030 in->stream.set_gain = in_set_gain; 6031 in->stream.read = in_read; 6032 in->stream.get_input_frames_lost = in_get_input_frames_lost; 6033 in->stream.get_capture_position = in_get_capture_position; 6034 in->stream.get_active_microphones = in_get_active_microphones; 6035 in->stream.set_microphone_direction = in_set_microphone_direction; 6036 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension; 6037 in->stream.update_sink_metadata = in_update_sink_metadata; 6038 6039 in->device = devices; 6040 in->source = source; 6041 in->dev = adev; 6042 in->standby = 1; 6043 in->capture_handle = handle; 6044 in->flags = flags; 6045 in->direction = MIC_DIRECTION_UNSPECIFIED; 6046 in->zoom = 0; 6047 in->mmap_shared_memory_fd = -1; // not open 6048 list_init(&in->aec_list); 6049 list_init(&in->ns_list); 6050 6051 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask); 6052 if (source == AUDIO_SOURCE_VOICE_UPLINK || 6053 source == AUDIO_SOURCE_VOICE_DOWNLINK) { 6054 /* Force channel config requested to mono if incall 6055 record is being requested for only uplink/downlink */ 6056 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) { 6057 config->channel_mask = AUDIO_CHANNEL_IN_MONO; 6058 ret = -EINVAL; 6059 goto err_open; 6060 } 6061 } 6062 6063 if (is_usb_dev && may_use_hifi_record) { 6064 /* HiFi record selects an appropriate format, channel, rate combo 6065 depending on sink capabilities*/ 6066 ret = read_usb_sup_params_and_compare(false /*is_playback*/, 6067 &config->format, 6068 &in->supported_formats[0], 6069 MAX_SUPPORTED_FORMATS, 6070 &config->channel_mask, 6071 &in->supported_channel_masks[0], 6072 MAX_SUPPORTED_CHANNEL_MASKS, 6073 &config->sample_rate, 6074 &in->supported_sample_rates[0], 6075 MAX_SUPPORTED_SAMPLE_RATES); 6076 if (ret != 0) { 6077 ret = -EINVAL; 6078 goto err_open; 6079 } 6080 channel_count = audio_channel_count_from_in_mask(config->channel_mask); 6081 } else if (config->format == AUDIO_FORMAT_DEFAULT) { 6082 config->format = AUDIO_FORMAT_PCM_16_BIT; 6083 } else if (config->format == AUDIO_FORMAT_PCM_FLOAT || 6084 config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED || 6085 config->format == AUDIO_FORMAT_PCM_8_24_BIT) { 6086 bool ret_error = false; 6087 /* 24 bit is restricted to UNPROCESSED source only,also format supported 6088 from HAL is 8_24 6089 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than 6090 8_24 return error indicating supported format is 8_24 6091 *> In case of any other source requesting 24 bit or float return error 6092 indicating format supported is 16 bit only. 6093 6094 on error flinger will retry with supported format passed 6095 */ 6096 if (!is_supported_24bits_audiosource(source)) { 6097 config->format = AUDIO_FORMAT_PCM_16_BIT; 6098 ret_error = true; 6099 } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) { 6100 config->format = AUDIO_FORMAT_PCM_8_24_BIT; 6101 ret_error = true; 6102 } 6103 6104 if (ret_error) { 6105 ret = -EINVAL; 6106 goto err_open; 6107 } 6108 } 6109 6110 in->format = config->format; 6111 in->channel_mask = config->channel_mask; 6112 6113 /* Update config params with the requested sample rate and channels */ 6114 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) { 6115 if (config->sample_rate == 0) 6116 config->sample_rate = AFE_PROXY_SAMPLING_RATE; 6117 if (config->sample_rate != 48000 && config->sample_rate != 16000 && 6118 config->sample_rate != 8000) { 6119 config->sample_rate = AFE_PROXY_SAMPLING_RATE; 6120 ret = -EINVAL; 6121 goto err_open; 6122 } 6123 6124 if (config->format != AUDIO_FORMAT_PCM_16_BIT) { 6125 config->format = AUDIO_FORMAT_PCM_16_BIT; 6126 ret = -EINVAL; 6127 goto err_open; 6128 } 6129 6130 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY; 6131 in->config = pcm_config_afe_proxy_record; 6132 in->af_period_multiplier = 1; 6133 } else if (is_usb_dev && may_use_hifi_record) { 6134 in->usecase = USECASE_AUDIO_RECORD_HIFI; 6135 in->config = pcm_config_audio_capture; 6136 frame_size = audio_stream_in_frame_size(&in->stream); 6137 buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, 6138 config->sample_rate, 6139 config->format, 6140 channel_count, 6141 false /*is_low_latency*/); 6142 in->config.period_size = buffer_size / frame_size; 6143 in->config.rate = config->sample_rate; 6144 in->af_period_multiplier = 1; 6145 in->config.format = pcm_format_from_audio_format(config->format); 6146 } else { 6147 in->usecase = USECASE_AUDIO_RECORD; 6148 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE && 6149 (in->flags & AUDIO_INPUT_FLAG_FAST) != 0) { 6150 is_low_latency = true; 6151 #if LOW_LATENCY_CAPTURE_USE_CASE 6152 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY; 6153 #endif 6154 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags); 6155 if (!in->realtime) { 6156 in->config = pcm_config_audio_capture; 6157 frame_size = audio_stream_in_frame_size(&in->stream); 6158 buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, 6159 config->sample_rate, 6160 config->format, 6161 channel_count, 6162 is_low_latency); 6163 in->config.period_size = buffer_size / frame_size; 6164 in->config.rate = config->sample_rate; 6165 in->af_period_multiplier = 1; 6166 } else { 6167 // period size is left untouched for rt mode playback 6168 in->config = pcm_config_audio_capture_rt; 6169 in->af_period_multiplier = af_period_multiplier; 6170 } 6171 } else if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) && 6172 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) { 6173 // FIXME: Add support for multichannel capture over USB using MMAP 6174 in->usecase = USECASE_AUDIO_RECORD_MMAP; 6175 in->config = pcm_config_mmap_capture; 6176 in->stream.start = in_start; 6177 in->stream.stop = in_stop; 6178 in->stream.create_mmap_buffer = in_create_mmap_buffer; 6179 in->stream.get_mmap_position = in_get_mmap_position; 6180 in->af_period_multiplier = 1; 6181 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__); 6182 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION && 6183 in->flags & AUDIO_INPUT_FLAG_VOIP_TX && 6184 (config->sample_rate == 8000 || 6185 config->sample_rate == 16000 || 6186 config->sample_rate == 32000 || 6187 config->sample_rate == 48000) && 6188 channel_count == 1) { 6189 in->usecase = USECASE_AUDIO_RECORD_VOIP; 6190 in->config = pcm_config_audio_capture; 6191 frame_size = audio_stream_in_frame_size(&in->stream); 6192 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC, 6193 config->sample_rate, 6194 config->format, 6195 channel_count, false /*is_low_latency*/); 6196 in->config.period_size = buffer_size / frame_size; 6197 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT; 6198 in->config.rate = config->sample_rate; 6199 in->af_period_multiplier = 1; 6200 } else { 6201 in->config = pcm_config_audio_capture; 6202 frame_size = audio_stream_in_frame_size(&in->stream); 6203 buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, 6204 config->sample_rate, 6205 config->format, 6206 channel_count, 6207 is_low_latency); 6208 in->config.period_size = buffer_size / frame_size; 6209 in->config.rate = config->sample_rate; 6210 in->af_period_multiplier = 1; 6211 } 6212 if (config->format == AUDIO_FORMAT_PCM_8_24_BIT) 6213 in->config.format = PCM_FORMAT_S24_LE; 6214 } 6215 6216 in->config.channels = channel_count; 6217 in->sample_rate = in->config.rate; 6218 6219 6220 register_format(in->format, in->supported_formats); 6221 register_channel_mask(in->channel_mask, in->supported_channel_masks); 6222 register_sample_rate(in->sample_rate, in->supported_sample_rates); 6223 6224 in->error_log = error_log_create( 6225 ERROR_LOG_ENTRIES, 6226 NANOS_PER_SECOND /* aggregate consecutive identical errors within one second */); 6227 6228 /* This stream could be for sound trigger lab, 6229 get sound trigger pcm if present */ 6230 audio_extn_sound_trigger_check_and_get_session(in); 6231 6232 if (in->is_st_session) 6233 in->flags |= AUDIO_INPUT_FLAG_HW_HOTWORD; 6234 6235 lock_input_stream(in); 6236 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb); 6237 pthread_mutex_lock(&adev->lock); 6238 in->card_status = adev->card_status; 6239 pthread_mutex_unlock(&adev->lock); 6240 pthread_mutex_unlock(&in->lock); 6241 6242 stream_app_type_cfg_init(&in->app_type_cfg); 6243 6244 *stream_in = &in->stream; 6245 ALOGV("%s: exit", __func__); 6246 return 0; 6247 6248 err_open: 6249 free(in); 6250 *stream_in = NULL; 6251 return ret; 6252 } 6253 adev_close_input_stream(struct audio_hw_device * dev __unused,struct audio_stream_in * stream)6254 static void adev_close_input_stream(struct audio_hw_device *dev __unused, 6255 struct audio_stream_in *stream) 6256 { 6257 struct stream_in *in = (struct stream_in *)stream; 6258 ALOGV("%s", __func__); 6259 6260 // must deregister from sndmonitor first to prevent races 6261 // between the callback and close_stream 6262 audio_extn_snd_mon_unregister_listener(stream); 6263 in_standby(&stream->common); 6264 6265 error_log_destroy(in->error_log); 6266 in->error_log = NULL; 6267 6268 pthread_mutex_destroy(&in->pre_lock); 6269 pthread_mutex_destroy(&in->lock); 6270 6271 free(stream); 6272 6273 return; 6274 } 6275 adev_dump(const audio_hw_device_t * device __unused,int fd __unused)6276 static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused) 6277 { 6278 return 0; 6279 } 6280 6281 /* verifies input and output devices and their capabilities. 6282 * 6283 * This verification is required when enabling extended bit-depth or 6284 * sampling rates, as not all qcom products support it. 6285 * 6286 * Suitable for calling only on initialization such as adev_open(). 6287 * It fills the audio_device use_case_table[] array. 6288 * 6289 * Has a side-effect that it needs to configure audio routing / devices 6290 * in order to power up the devices and read the device parameters. 6291 * It does not acquire any hw device lock. Should restore the devices 6292 * back to "normal state" upon completion. 6293 */ adev_verify_devices(struct audio_device * adev)6294 static int adev_verify_devices(struct audio_device *adev) 6295 { 6296 /* enumeration is a bit difficult because one really wants to pull 6297 * the use_case, device id, etc from the hidden pcm_device_table[]. 6298 * In this case there are the following use cases and device ids. 6299 * 6300 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0}, 6301 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15}, 6302 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1}, 6303 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9}, 6304 * [USECASE_AUDIO_RECORD] = {0, 0}, 6305 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15}, 6306 * [USECASE_VOICE_CALL] = {2, 2}, 6307 * 6308 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted. 6309 * USECASE_VOICE_CALL omitted, but possible for either input or output. 6310 */ 6311 6312 /* should be the usecases enabled in adev_open_input_stream() */ 6313 static const int test_in_usecases[] = { 6314 USECASE_AUDIO_RECORD, 6315 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */ 6316 }; 6317 /* should be the usecases enabled in adev_open_output_stream()*/ 6318 static const int test_out_usecases[] = { 6319 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER, 6320 USECASE_AUDIO_PLAYBACK_LOW_LATENCY, 6321 }; 6322 static const usecase_type_t usecase_type_by_dir[] = { 6323 PCM_PLAYBACK, 6324 PCM_CAPTURE, 6325 }; 6326 static const unsigned flags_by_dir[] = { 6327 PCM_OUT, 6328 PCM_IN, 6329 }; 6330 6331 size_t i; 6332 unsigned dir; 6333 const unsigned card_id = adev->snd_card; 6334 char info[512]; /* for possible debug info */ 6335 6336 for (dir = 0; dir < 2; ++dir) { 6337 const usecase_type_t usecase_type = usecase_type_by_dir[dir]; 6338 const unsigned flags_dir = flags_by_dir[dir]; 6339 const size_t testsize = 6340 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases); 6341 const int *testcases = 6342 dir ? test_in_usecases : test_out_usecases; 6343 const audio_devices_t audio_device = 6344 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER; 6345 6346 for (i = 0; i < testsize; ++i) { 6347 const audio_usecase_t audio_usecase = testcases[i]; 6348 int device_id; 6349 snd_device_t snd_device; 6350 struct pcm_params **pparams; 6351 struct stream_out out; 6352 struct stream_in in; 6353 struct audio_usecase uc_info; 6354 int retval; 6355 6356 pparams = &adev->use_case_table[audio_usecase]; 6357 pcm_params_free(*pparams); /* can accept null input */ 6358 *pparams = NULL; 6359 6360 /* find the device ID for the use case (signed, for error) */ 6361 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type); 6362 if (device_id < 0) 6363 continue; 6364 6365 /* prepare structures for device probing */ 6366 memset(&uc_info, 0, sizeof(uc_info)); 6367 uc_info.id = audio_usecase; 6368 uc_info.type = usecase_type; 6369 if (dir) { 6370 memset(&in, 0, sizeof(in)); 6371 in.device = audio_device; 6372 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION; 6373 uc_info.stream.in = ∈ 6374 } 6375 memset(&out, 0, sizeof(out)); 6376 out.devices = audio_device; /* only field needed in select_devices */ 6377 uc_info.stream.out = &out; 6378 uc_info.devices = audio_device; 6379 uc_info.in_snd_device = SND_DEVICE_NONE; 6380 uc_info.out_snd_device = SND_DEVICE_NONE; 6381 list_add_tail(&adev->usecase_list, &uc_info.list); 6382 6383 /* select device - similar to start_(in/out)put_stream() */ 6384 retval = select_devices(adev, audio_usecase); 6385 if (retval >= 0) { 6386 *pparams = pcm_params_get(card_id, device_id, flags_dir); 6387 #if LOG_NDEBUG == 0 6388 if (*pparams) { 6389 ALOGV("%s: (%s) card %d device %d", __func__, 6390 dir ? "input" : "output", card_id, device_id); 6391 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info)); 6392 } else { 6393 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id); 6394 } 6395 #endif 6396 } 6397 6398 /* deselect device - similar to stop_(in/out)put_stream() */ 6399 /* 1. Get and set stream specific mixer controls */ 6400 retval = disable_audio_route(adev, &uc_info); 6401 /* 2. Disable the rx device */ 6402 retval = disable_snd_device(adev, 6403 dir ? uc_info.in_snd_device : uc_info.out_snd_device); 6404 list_remove(&uc_info.list); 6405 } 6406 } 6407 return 0; 6408 } 6409 adev_close(hw_device_t * device)6410 static int adev_close(hw_device_t *device) 6411 { 6412 size_t i; 6413 6414 pthread_mutex_lock(&adev_init_lock); 6415 if (!device || ((struct audio_device *)device != adev)) 6416 goto done; 6417 6418 if ((--audio_device_ref_count) == 0) { 6419 audio_extn_snd_mon_unregister_listener(adev); 6420 audio_extn_tfa_98xx_deinit(); 6421 audio_extn_ma_deinit(); 6422 audio_route_free(adev->audio_route); 6423 free(adev->snd_dev_ref_cnt); 6424 platform_deinit(adev->platform); 6425 audio_extn_extspk_deinit(adev->extspk); 6426 audio_extn_sound_trigger_deinit(adev); 6427 audio_extn_snd_mon_deinit(); 6428 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) { 6429 pcm_params_free(adev->use_case_table[i]); 6430 } 6431 if (adev->adm_deinit) 6432 adev->adm_deinit(adev->adm_data); 6433 pthread_mutex_destroy(&adev->lock); 6434 free(device); 6435 adev = NULL; 6436 } 6437 6438 done: 6439 pthread_mutex_unlock(&adev_init_lock); 6440 return 0; 6441 } 6442 6443 /* This returns 1 if the input parameter looks at all plausible as a low latency period size, 6444 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work, 6445 * just that it _might_ work. 6446 */ period_size_is_plausible_for_low_latency(int period_size)6447 static int period_size_is_plausible_for_low_latency(int period_size) 6448 { 6449 switch (period_size) { 6450 case 48: 6451 case 96: 6452 case 144: 6453 case 160: 6454 case 192: 6455 case 240: 6456 case 320: 6457 case 480: 6458 return 1; 6459 default: 6460 return 0; 6461 } 6462 } 6463 adev_snd_mon_cb(void * stream __unused,struct str_parms * parms)6464 static void adev_snd_mon_cb(void * stream __unused, struct str_parms * parms) 6465 { 6466 int card; 6467 card_status_t status; 6468 6469 if (!parms) 6470 return; 6471 6472 if (parse_snd_card_status(parms, &card, &status) < 0) 6473 return; 6474 6475 pthread_mutex_lock(&adev->lock); 6476 bool valid_cb = (card == adev->snd_card); 6477 if (valid_cb) { 6478 if (adev->card_status != status) { 6479 adev->card_status = status; 6480 platform_snd_card_update(adev->platform, status); 6481 } 6482 } 6483 pthread_mutex_unlock(&adev->lock); 6484 return; 6485 } 6486 6487 /* out and adev lock held */ check_a2dp_restore_l(struct audio_device * adev,struct stream_out * out,bool restore)6488 static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore) 6489 { 6490 struct audio_usecase *uc_info; 6491 float left_p; 6492 float right_p; 6493 audio_devices_t devices; 6494 6495 uc_info = get_usecase_from_list(adev, out->usecase); 6496 if (uc_info == NULL) { 6497 ALOGE("%s: Could not find the usecase (%d) in the list", 6498 __func__, out->usecase); 6499 return -EINVAL; 6500 } 6501 6502 ALOGD("%s: enter: usecase(%d: %s)", __func__, 6503 out->usecase, use_case_table[out->usecase]); 6504 6505 if (restore) { 6506 // restore A2DP device for active usecases and unmute if required 6507 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && 6508 !is_a2dp_device(uc_info->out_snd_device)) { 6509 ALOGD("%s: restoring A2DP and unmuting stream", __func__); 6510 select_devices(adev, uc_info->id); 6511 pthread_mutex_lock(&out->compr_mute_lock); 6512 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && 6513 (out->a2dp_compress_mute)) { 6514 out->a2dp_compress_mute = false; 6515 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r); 6516 } 6517 pthread_mutex_unlock(&out->compr_mute_lock); 6518 } 6519 } else { 6520 // mute compress stream if suspended 6521 pthread_mutex_lock(&out->compr_mute_lock); 6522 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && 6523 (!out->a2dp_compress_mute)) { 6524 if (!out->standby) { 6525 ALOGD("%s: selecting speaker and muting stream", __func__); 6526 devices = out->devices; 6527 out->devices = AUDIO_DEVICE_OUT_SPEAKER; 6528 left_p = out->volume_l; 6529 right_p = out->volume_r; 6530 if (out->offload_state == OFFLOAD_STATE_PLAYING) 6531 compress_pause(out->compr); 6532 out_set_compr_volume(&out->stream, 0.0f, 0.0f); 6533 out->a2dp_compress_mute = true; 6534 select_devices(adev, out->usecase); 6535 if (out->offload_state == OFFLOAD_STATE_PLAYING) 6536 compress_resume(out->compr); 6537 out->devices = devices; 6538 out->volume_l = left_p; 6539 out->volume_r = right_p; 6540 } 6541 } 6542 pthread_mutex_unlock(&out->compr_mute_lock); 6543 } 6544 ALOGV("%s: exit", __func__); 6545 return 0; 6546 } 6547 check_a2dp_restore(struct audio_device * adev,struct stream_out * out,bool restore)6548 int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore) 6549 { 6550 int ret = 0; 6551 6552 lock_output_stream(out); 6553 pthread_mutex_lock(&adev->lock); 6554 6555 ret = check_a2dp_restore_l(adev, out, restore); 6556 6557 pthread_mutex_unlock(&adev->lock); 6558 pthread_mutex_unlock(&out->lock); 6559 return ret; 6560 } 6561 adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)6562 static int adev_open(const hw_module_t *module, const char *name, 6563 hw_device_t **device) 6564 { 6565 int i, ret; 6566 6567 ALOGD("%s: enter", __func__); 6568 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; 6569 pthread_mutex_lock(&adev_init_lock); 6570 if (audio_device_ref_count != 0) { 6571 *device = &adev->device.common; 6572 audio_device_ref_count++; 6573 ALOGV("%s: returning existing instance of adev", __func__); 6574 ALOGV("%s: exit", __func__); 6575 pthread_mutex_unlock(&adev_init_lock); 6576 return 0; 6577 } 6578 adev = calloc(1, sizeof(struct audio_device)); 6579 6580 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); 6581 6582 adev->device.common.tag = HARDWARE_DEVICE_TAG; 6583 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 6584 adev->device.common.module = (struct hw_module_t *)module; 6585 adev->device.common.close = adev_close; 6586 6587 adev->device.init_check = adev_init_check; 6588 adev->device.set_voice_volume = adev_set_voice_volume; 6589 adev->device.set_master_volume = adev_set_master_volume; 6590 adev->device.get_master_volume = adev_get_master_volume; 6591 adev->device.set_master_mute = adev_set_master_mute; 6592 adev->device.get_master_mute = adev_get_master_mute; 6593 adev->device.set_mode = adev_set_mode; 6594 adev->device.set_mic_mute = adev_set_mic_mute; 6595 adev->device.get_mic_mute = adev_get_mic_mute; 6596 adev->device.set_parameters = adev_set_parameters; 6597 adev->device.get_parameters = adev_get_parameters; 6598 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 6599 adev->device.open_output_stream = adev_open_output_stream; 6600 adev->device.close_output_stream = adev_close_output_stream; 6601 adev->device.open_input_stream = adev_open_input_stream; 6602 6603 adev->device.close_input_stream = adev_close_input_stream; 6604 adev->device.dump = adev_dump; 6605 adev->device.get_microphones = adev_get_microphones; 6606 6607 /* Set the default route before the PCM stream is opened */ 6608 pthread_mutex_lock(&adev->lock); 6609 adev->mode = AUDIO_MODE_NORMAL; 6610 adev->primary_output = NULL; 6611 adev->bluetooth_nrec = true; 6612 adev->acdb_settings = TTY_MODE_OFF; 6613 adev->a2dp_started = false; 6614 /* adev->cur_hdmi_channels = 0; by calloc() */ 6615 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); 6616 voice_init(adev); 6617 list_init(&adev->usecase_list); 6618 pthread_mutex_unlock(&adev->lock); 6619 6620 /* Loads platform specific libraries dynamically */ 6621 adev->platform = platform_init(adev); 6622 if (!adev->platform) { 6623 free(adev->snd_dev_ref_cnt); 6624 free(adev); 6625 ALOGE("%s: Failed to init platform data, aborting.", __func__); 6626 *device = NULL; 6627 pthread_mutex_unlock(&adev_init_lock); 6628 return -EINVAL; 6629 } 6630 adev->extspk = audio_extn_extspk_init(adev); 6631 6632 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); 6633 if (adev->visualizer_lib == NULL) { 6634 ALOGW("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); 6635 } else { 6636 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); 6637 adev->visualizer_start_output = 6638 (int (*)(audio_io_handle_t, int, int, int))dlsym(adev->visualizer_lib, 6639 "visualizer_hal_start_output"); 6640 adev->visualizer_stop_output = 6641 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, 6642 "visualizer_hal_stop_output"); 6643 } 6644 6645 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW); 6646 if (adev->offload_effects_lib == NULL) { 6647 ALOGW("%s: DLOPEN failed for %s", __func__, 6648 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); 6649 } else { 6650 ALOGV("%s: DLOPEN successful for %s", __func__, 6651 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); 6652 adev->offload_effects_start_output = 6653 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, 6654 "offload_effects_bundle_hal_start_output"); 6655 adev->offload_effects_stop_output = 6656 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, 6657 "offload_effects_bundle_hal_stop_output"); 6658 } 6659 6660 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW); 6661 if (adev->adm_lib == NULL) { 6662 ALOGW("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH); 6663 } else { 6664 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH); 6665 adev->adm_init = (adm_init_t) 6666 dlsym(adev->adm_lib, "adm_init"); 6667 adev->adm_deinit = (adm_deinit_t) 6668 dlsym(adev->adm_lib, "adm_deinit"); 6669 adev->adm_register_input_stream = (adm_register_input_stream_t) 6670 dlsym(adev->adm_lib, "adm_register_input_stream"); 6671 adev->adm_register_output_stream = (adm_register_output_stream_t) 6672 dlsym(adev->adm_lib, "adm_register_output_stream"); 6673 adev->adm_deregister_stream = (adm_deregister_stream_t) 6674 dlsym(adev->adm_lib, "adm_deregister_stream"); 6675 adev->adm_request_focus = (adm_request_focus_t) 6676 dlsym(adev->adm_lib, "adm_request_focus"); 6677 adev->adm_abandon_focus = (adm_abandon_focus_t) 6678 dlsym(adev->adm_lib, "adm_abandon_focus"); 6679 adev->adm_set_config = (adm_set_config_t) 6680 dlsym(adev->adm_lib, "adm_set_config"); 6681 adev->adm_request_focus_v2 = (adm_request_focus_v2_t) 6682 dlsym(adev->adm_lib, "adm_request_focus_v2"); 6683 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t) 6684 dlsym(adev->adm_lib, "adm_is_noirq_avail"); 6685 adev->adm_on_routing_change = (adm_on_routing_change_t) 6686 dlsym(adev->adm_lib, "adm_on_routing_change"); 6687 } 6688 6689 adev->bt_wb_speech_enabled = false; 6690 adev->enable_voicerx = false; 6691 6692 *device = &adev->device.common; 6693 6694 if (k_enable_extended_precision) 6695 adev_verify_devices(adev); 6696 6697 char value[PROPERTY_VALUE_MAX]; 6698 int trial; 6699 if ((property_get("vendor.audio_hal.period_size", value, NULL) > 0) || 6700 (property_get("audio_hal.period_size", value, NULL) > 0)) { 6701 trial = atoi(value); 6702 if (period_size_is_plausible_for_low_latency(trial)) { 6703 pcm_config_low_latency.period_size = trial; 6704 pcm_config_low_latency.start_threshold = trial / 4; 6705 pcm_config_low_latency.avail_min = trial / 4; 6706 configured_low_latency_capture_period_size = trial; 6707 } 6708 } 6709 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) || 6710 (property_get("audio_hal.in_period_size", value, NULL) > 0)) { 6711 trial = atoi(value); 6712 if (period_size_is_plausible_for_low_latency(trial)) { 6713 configured_low_latency_capture_period_size = trial; 6714 } 6715 } 6716 6717 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false); 6718 6719 adev->camera_orientation = CAMERA_DEFAULT; 6720 6721 // commented as full set of app type cfg is sent from platform 6722 // audio_extn_utils_send_default_app_type_cfg(adev->platform, adev->mixer); 6723 audio_device_ref_count++; 6724 6725 if ((property_get("vendor.audio_hal.period_multiplier", value, NULL) > 0) || 6726 (property_get("audio_hal.period_multiplier", value, NULL) > 0)) { 6727 af_period_multiplier = atoi(value); 6728 if (af_period_multiplier < 0) { 6729 af_period_multiplier = 2; 6730 } else if (af_period_multiplier > 4) { 6731 af_period_multiplier = 4; 6732 } 6733 ALOGV("new period_multiplier = %d", af_period_multiplier); 6734 } 6735 6736 audio_extn_tfa_98xx_init(adev); 6737 audio_extn_ma_init(adev->platform); 6738 audio_extn_audiozoom_init(); 6739 6740 pthread_mutex_unlock(&adev_init_lock); 6741 6742 if (adev->adm_init) 6743 adev->adm_data = adev->adm_init(); 6744 6745 audio_extn_perf_lock_init(); 6746 audio_extn_snd_mon_init(); 6747 pthread_mutex_lock(&adev->lock); 6748 audio_extn_snd_mon_register_listener(NULL, adev_snd_mon_cb); 6749 adev->card_status = CARD_STATUS_ONLINE; 6750 pthread_mutex_unlock(&adev->lock); 6751 audio_extn_sound_trigger_init(adev);/* dependent on snd_mon_init() */ 6752 6753 ALOGD("%s: exit", __func__); 6754 return 0; 6755 } 6756 6757 static struct hw_module_methods_t hal_module_methods = { 6758 .open = adev_open, 6759 }; 6760 6761 struct audio_module HAL_MODULE_INFO_SYM = { 6762 .common = { 6763 .tag = HARDWARE_MODULE_TAG, 6764 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 6765 .hal_api_version = HARDWARE_HAL_API_VERSION, 6766 .id = AUDIO_HARDWARE_MODULE_ID, 6767 .name = "QCOM Audio HAL", 6768 .author = "Code Aurora Forum", 6769 .methods = &hal_module_methods, 6770 }, 6771 }; 6772