1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "audio_hw_primary" 18 /*#define LOG_NDEBUG 0*/ 19 20 #include <errno.h> 21 #include <pthread.h> 22 #include <stdint.h> 23 #include <sys/time.h> 24 #include <stdlib.h> 25 26 #include <cutils/log.h> 27 #include <cutils/str_parms.h> 28 #include <cutils/properties.h> 29 30 #include <hardware/hardware.h> 31 #include <system/audio.h> 32 #include <hardware/audio.h> 33 34 #include <tinyalsa/asoundlib.h> 35 #include <audio_utils/resampler.h> 36 #include <audio_utils/echo_reference.h> 37 #include <hardware/audio_effect.h> 38 #include <audio_effects/effect_aec.h> 39 40 #include "ril_interface.h" 41 42 43 /* Mixer control names */ 44 #define MIXER_DL2_LEFT_EQUALIZER "DL2 Left Equalizer" 45 #define MIXER_DL2_RIGHT_EQUALIZER "DL2 Right Equalizer" 46 #define MIXER_DL1_MEDIA_PLAYBACK_VOLUME "DL1 Media Playback Volume" 47 #define MIXER_DL1_VOICE_PLAYBACK_VOLUME "DL1 Voice Playback Volume" 48 #define MIXER_DL1_TONES_PLAYBACK_VOLUME "DL1 Tones Playback Volume" 49 #define MIXER_DL2_MEDIA_PLAYBACK_VOLUME "DL2 Media Playback Volume" 50 #define MIXER_DL2_VOICE_PLAYBACK_VOLUME "DL2 Voice Playback Volume" 51 #define MIXER_DL2_TONES_PLAYBACK_VOLUME "DL2 Tones Playback Volume" 52 #define MIXER_SDT_DL_VOLUME "SDT DL Volume" 53 #define MIXER_SDT_UL_VOLUME "SDT UL Volume" 54 55 #define MIXER_HEADSET_PLAYBACK_VOLUME "Headset Playback Volume" 56 #define MIXER_HANDSFREE_PLAYBACK_VOLUME "Handsfree Playback Volume" 57 #define MIXER_EARPHONE_PLAYBACK_VOLUME "Earphone Playback Volume" 58 #define MIXER_BT_UL_VOLUME "BT UL Volume" 59 60 #define MIXER_DL1_EQUALIZER "DL1 Equalizer" 61 #define MIXER_DL1_MIXER_MULTIMEDIA "DL1 Mixer Multimedia" 62 #define MIXER_DL1_MIXER_VOICE "DL1 Mixer Voice" 63 #define MIXER_DL1_MIXER_TONES "DL1 Mixer Tones" 64 #define MIXER_DL2_MIXER_MULTIMEDIA "DL2 Mixer Multimedia" 65 #define MIXER_DL2_MIXER_VOICE "DL2 Mixer Voice" 66 #define MIXER_DL2_MIXER_TONES "DL2 Mixer Tones" 67 #define MIXER_SIDETONE_MIXER_PLAYBACK "Sidetone Mixer Playback" 68 #define MIXER_SIDETONE_MIXER_CAPTURE "Sidetone Mixer Capture" 69 #define MIXER_DL2_MONO_MIXER "DL2 Mono Mixer" 70 #define MIXER_DL1_PDM_SWITCH "DL1 PDM Switch" 71 #define MIXER_DL1_BT_VX_SWITCH "DL1 BT_VX Switch" 72 #define MIXER_VOICE_CAPTURE_MIXER_CAPTURE "Voice Capture Mixer Capture" 73 74 #define MIXER_HS_LEFT_PLAYBACK "HS Left Playback" 75 #define MIXER_HS_RIGHT_PLAYBACK "HS Right Playback" 76 #define MIXER_HF_LEFT_PLAYBACK "HF Left Playback" 77 #define MIXER_HF_RIGHT_PLAYBACK "HF Right Playback" 78 #define MIXER_EARPHONE_ENABLE_SWITCH "Earphone Enable Switch" 79 80 #define MIXER_ANALOG_LEFT_CAPTURE_ROUTE "Analog Left Capture Route" 81 #define MIXER_ANALOG_RIGHT_CAPTURE_ROUTE "Analog Right Capture Route" 82 #define MIXER_CAPTURE_PREAMPLIFIER_VOLUME "Capture Preamplifier Volume" 83 #define MIXER_CAPTURE_VOLUME "Capture Volume" 84 #define MIXER_AMIC_UL_VOLUME "AMIC UL Volume" 85 #define MIXER_AUDUL_VOICE_UL_VOLUME "AUDUL Voice UL Volume" 86 #define MIXER_MUX_VX0 "MUX_VX0" 87 #define MIXER_MUX_VX1 "MUX_VX1" 88 #define MIXER_MUX_UL10 "MUX_UL10" 89 #define MIXER_MUX_UL11 "MUX_UL11" 90 91 /* Mixer control gain and route values */ 92 #define MIXER_ABE_GAIN_0DB 120 93 #define MIXER_PLAYBACK_HS_DAC "HS DAC" 94 #define MIXER_PLAYBACK_HF_DAC "HF DAC" 95 #define MIXER_MAIN_MIC "Main Mic" 96 #define MIXER_SUB_MIC "Sub Mic" 97 #define MIXER_HS_MIC "Headset Mic" 98 #define MIXER_AMIC0 "AMic0" 99 #define MIXER_AMIC1 "AMic1" 100 #define MIXER_BT_LEFT "BT Left" 101 #define MIXER_BT_RIGHT "BT Right" 102 #define MIXER_450HZ_HIGH_PASS "450Hz High-pass" 103 #define MIXER_FLAT_RESPONSE "Flat response" 104 #define MIXER_4KHZ_LPF_0DB "4Khz LPF 0dB" 105 106 /* HDMI mixer controls */ 107 #define MIXER_MAXIMUM_LPCM_CHANNELS "Maximum LPCM channels" 108 109 110 /* ALSA cards for OMAP4 */ 111 #define CARD_OMAP4_ABE 0 112 #define CARD_OMAP4_HDMI 1 113 #define CARD_TUNA_DEFAULT CARD_OMAP4_ABE 114 115 /* ALSA ports for OMAP4 */ 116 #define PORT_MM 0 117 #define PORT_MM2_UL 1 118 #define PORT_VX 2 119 #define PORT_TONES 3 120 #define PORT_VIBRA 4 121 #define PORT_MODEM 5 122 #define PORT_MM_LP 6 123 #define PORT_SPDIF 9 124 #define PORT_HDMI 0 125 126 /* User serviceable */ 127 /* #define to use mmap no-irq mode for playback, #undef for non-mmap irq mode */ 128 #undef PLAYBACK_MMAP // was #define 129 /* short period (aka low latency) in milliseconds */ 130 #define SHORT_PERIOD_MS 3 // was 22 131 /* deep buffer short period (screen on) in milliseconds */ 132 #define DEEP_BUFFER_SHORT_PERIOD_MS 22 133 /* deep buffer long period (screen off) in milliseconds */ 134 #define DEEP_BUFFER_LONG_PERIOD_MS 308 135 136 /* Constraint imposed by ABE: for playback, all period sizes must be multiples of 24 frames 137 * = 500 us at 48 kHz. It seems to be either 48 or 96 for capture, or maybe it is because the 138 * limitation is actually a min number of bytes which translates to a different amount of frames 139 * according to the number of channels. 140 */ 141 #define ABE_BASE_FRAME_COUNT 24 142 143 /* Derived from MM_FULL_POWER_SAMPLING_RATE=48000 and ABE_BASE_FRAME_COUNT=24 */ 144 #define MULTIPLIER_FACTOR 2 145 146 /* number of base blocks in a short period (low latency) */ 147 #define SHORT_PERIOD_MULTIPLIER (SHORT_PERIOD_MS * MULTIPLIER_FACTOR) 148 /* number of frames per short period (low latency) */ 149 #define SHORT_PERIOD_SIZE (ABE_BASE_FRAME_COUNT * SHORT_PERIOD_MULTIPLIER) 150 151 /* number of base blocks in a short deep buffer period (screen on) */ 152 #define DEEP_BUFFER_SHORT_PERIOD_MULTIPLIER (DEEP_BUFFER_SHORT_PERIOD_MS * MULTIPLIER_FACTOR) 153 /* number of frames per short deep buffer period (screen on) */ 154 #define DEEP_BUFFER_SHORT_PERIOD_SIZE (ABE_BASE_FRAME_COUNT * DEEP_BUFFER_SHORT_PERIOD_MULTIPLIER) 155 /* number of periods for deep buffer playback (screen on) */ 156 #define PLAYBACK_DEEP_BUFFER_SHORT_PERIOD_COUNT 4 157 158 /* number of short deep buffer periods in a long period */ 159 #define DEEP_BUFFER_LONG_PERIOD_MULTIPLIER \ 160 (DEEP_BUFFER_LONG_PERIOD_MS / DEEP_BUFFER_SHORT_PERIOD_MS) 161 /* number of frames per long deep buffer period (screen off) */ 162 #define DEEP_BUFFER_LONG_PERIOD_SIZE \ 163 (DEEP_BUFFER_SHORT_PERIOD_SIZE * DEEP_BUFFER_LONG_PERIOD_MULTIPLIER) 164 /* number of periods for deep buffer playback (screen off) */ 165 #define PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT 2 166 167 /* number of frames per period for HDMI multichannel output */ 168 #define HDMI_MULTI_PERIOD_SIZE 1024 169 /* number of periods for HDMI multichannel output */ 170 #define HDMI_MULTI_PERIOD_COUNT 4 171 /* default number of channels for HDMI multichannel output */ 172 #define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6 173 174 /* Number of pseudo periods for low latency playback. 175 * These are called "pseudo" periods in that they are not known as periods by ALSA. 176 * Formerly, ALSA was configured in MMAP mode with 2 large periods, and this 177 * number was set to 4 (2 didn't work). 178 * The short periods size and count were only known by the audio HAL. 179 * Now for low latency, we are using non-MMAP mode and can set this to 2. 180 */ 181 #ifdef PLAYBACK_MMAP 182 #define PLAYBACK_SHORT_PERIOD_COUNT 4 183 /* If sample rate converter is required, then use triple-buffering to 184 * help mask the variance in cycle times. Otherwise use double-buffering. 185 */ 186 #elif DEFAULT_OUT_SAMPLING_RATE != MM_FULL_POWER_SAMPLING_RATE 187 #define PLAYBACK_SHORT_PERIOD_COUNT 3 188 #else 189 #define PLAYBACK_SHORT_PERIOD_COUNT 2 190 #endif 191 192 /* write function */ 193 #ifdef PLAYBACK_MMAP 194 #define PCM_WRITE pcm_mmap_write 195 #else 196 #define PCM_WRITE pcm_write 197 #endif 198 199 /* User serviceable */ 200 #define CAPTURE_PERIOD_MS 22 201 202 /* Number of frames per period for capture. This cannot be reduced below 96. 203 * Possibly related to the following rule in sound/soc/omap/omap-pcm.c: 204 * ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 384); 205 * (where 96 * 4 = 384) 206 * The only constraints I can find are periods_min = 2, period_bytes_min = 32. 207 * If you define RULES_DEBUG in sound/core/pcm_native.c, you can see which rule 208 * caused capture to fail. 209 * Decoupling playback and capture period size may have impacts on echo canceler behavior: 210 * to be verified. Currently 96 = 4 x 24 but it could be changed without noticing 211 * if we use separate defines. 212 */ 213 #define CAPTURE_PERIOD_SIZE (ABE_BASE_FRAME_COUNT * CAPTURE_PERIOD_MS * MULTIPLIER_FACTOR) 214 /* number of periods for capture */ 215 #define CAPTURE_PERIOD_COUNT 2 216 /* minimum sleep time in out_write() when write threshold is not reached */ 217 #define MIN_WRITE_SLEEP_US 5000 218 219 #define DEFAULT_OUT_SAMPLING_RATE 44100 // 48000 is possible but interacts poorly with HDMI 220 221 /* sampling rate when using MM low power port */ 222 #define MM_LOW_POWER_SAMPLING_RATE 44100 223 /* sampling rate when using MM full power port */ 224 #define MM_FULL_POWER_SAMPLING_RATE 48000 // affects MULTIPLIER_FACTOR 225 /* sampling rate when using VX port for narrow band */ 226 #define VX_NB_SAMPLING_RATE 8000 227 /* sampling rate when using VX port for wide band */ 228 #define VX_WB_SAMPLING_RATE 16000 229 230 /* conversions from dB to ABE and codec gains */ 231 #define DB_TO_ABE_GAIN(x) ((x) + MIXER_ABE_GAIN_0DB) 232 #define DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(x) (((x) + 6) / 6) 233 #define DB_TO_CAPTURE_VOLUME(x) (((x) - 6) / 6) 234 #define DB_TO_HEADSET_VOLUME(x) (((x) + 30) / 2) 235 #define DB_TO_SPEAKER_VOLUME(x) (((x) + 52) / 2) 236 #define DB_TO_EARPIECE_VOLUME(x) (((x) + 24) / 2) 237 238 /* conversions from codec and ABE gains to dB */ 239 #define DB_FROM_SPEAKER_VOLUME(x) ((x) * 2 - 52) 240 241 /* use-case specific mic volumes, all in dB */ 242 #define CAPTURE_MAIN_MIC_VOLUME 16 243 #define CAPTURE_SUB_MIC_VOLUME 18 244 #define CAPTURE_HEADSET_MIC_VOLUME 12 245 246 #define VOICE_RECOGNITION_MAIN_MIC_VOLUME 5 247 #define VOICE_RECOGNITION_SUB_MIC_VOLUME 18 248 #define VOICE_RECOGNITION_HEADSET_MIC_VOLUME 14 249 250 #define CAMCORDER_MAIN_MIC_VOLUME 13 251 #define CAMCORDER_SUB_MIC_VOLUME 10 252 #define CAMCORDER_HEADSET_MIC_VOLUME 12 253 254 #define VOIP_MAIN_MIC_VOLUME 13 255 #define VOIP_SUB_MIC_VOLUME 20 256 #define VOIP_HEADSET_MIC_VOLUME 12 257 258 #define VOICE_CALL_MAIN_MIC_VOLUME 0 259 #define VOICE_CALL_SUB_MIC_VOLUME_MAGURO -4 260 #define VOICE_CALL_SUB_MIC_VOLUME_TORO -2 261 #define VOICE_CALL_HEADSET_MIC_VOLUME 8 262 263 /* use-case specific output volumes */ 264 #define NORMAL_SPEAKER_VOLUME_TORO 6 265 #define NORMAL_SPEAKER_VOLUME_MAGURO 2 266 #define NORMAL_HEADSET_VOLUME_TORO -12 267 #define NORMAL_HEADSET_VOLUME_MAGURO -12 268 #define NORMAL_HEADPHONE_VOLUME_TORO -6 /* allow louder output for headphones */ 269 #define NORMAL_HEADPHONE_VOLUME_MAGURO -6 270 #define NORMAL_EARPIECE_VOLUME_TORO -2 271 #define NORMAL_EARPIECE_VOLUME_MAGURO -2 272 273 #define VOICE_CALL_SPEAKER_VOLUME_TORO 9 274 #define VOICE_CALL_SPEAKER_VOLUME_MAGURO 6 275 #define VOICE_CALL_HEADSET_VOLUME_TORO -6 276 #define VOICE_CALL_HEADSET_VOLUME_MAGURO 0 277 #define VOICE_CALL_EARPIECE_VOLUME_TORO 2 278 #define VOICE_CALL_EARPIECE_VOLUME_MAGURO 6 279 280 #define VOIP_SPEAKER_VOLUME_TORO 9 281 #define VOIP_SPEAKER_VOLUME_MAGURO 7 282 #define VOIP_HEADSET_VOLUME_TORO -6 283 #define VOIP_HEADSET_VOLUME_MAGURO -6 284 #define VOIP_EARPIECE_VOLUME_TORO 6 285 #define VOIP_EARPIECE_VOLUME_MAGURO 6 286 287 #define HEADPHONE_VOLUME_TTY -2 288 #define RINGTONE_HEADSET_VOLUME_OFFSET -14 289 290 /* product-specific defines */ 291 #define PRODUCT_DEVICE_PROPERTY "ro.product.device" 292 #define PRODUCT_NAME_PROPERTY "ro.product.name" 293 #define PRODUCT_DEVICE_TORO "toro" 294 #define PRODUCT_NAME_YAKJU "yakju" 295 296 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 297 298 enum tty_modes { 299 TTY_MODE_OFF, 300 TTY_MODE_VCO, 301 TTY_MODE_HCO, 302 TTY_MODE_FULL 303 }; 304 305 /* deep buffer */ 306 struct pcm_config pcm_config_mm = { 307 .channels = 2, 308 .rate = MM_FULL_POWER_SAMPLING_RATE, 309 .period_size = DEEP_BUFFER_LONG_PERIOD_SIZE, 310 .period_count = PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT, 311 .format = PCM_FORMAT_S16_LE, 312 .start_threshold = DEEP_BUFFER_SHORT_PERIOD_SIZE * 2, 313 .avail_min = DEEP_BUFFER_LONG_PERIOD_SIZE, 314 }; 315 316 /* low latency */ 317 struct pcm_config pcm_config_tones = { 318 .channels = 2, 319 .rate = MM_FULL_POWER_SAMPLING_RATE, 320 .period_size = SHORT_PERIOD_SIZE, 321 .period_count = PLAYBACK_SHORT_PERIOD_COUNT, 322 .format = PCM_FORMAT_S16_LE, 323 #ifdef PLAYBACK_MMAP 324 .start_threshold = SHORT_PERIOD_SIZE, 325 .avail_min = SHORT_PERIOD_SIZE, 326 #else 327 .start_threshold = 0, 328 .avail_min = 0, 329 #endif 330 }; 331 332 struct pcm_config pcm_config_hdmi_multi = { 333 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ 334 .rate = MM_FULL_POWER_SAMPLING_RATE, /* changed when the stream is opened */ 335 .period_size = HDMI_MULTI_PERIOD_SIZE, 336 .period_count = HDMI_MULTI_PERIOD_COUNT, 337 .format = PCM_FORMAT_S16_LE, 338 .start_threshold = 0, 339 .avail_min = 0, 340 }; 341 342 struct pcm_config pcm_config_mm_ul = { 343 .channels = 2, 344 .rate = MM_FULL_POWER_SAMPLING_RATE, 345 .period_size = CAPTURE_PERIOD_SIZE, 346 .period_count = CAPTURE_PERIOD_COUNT, 347 .format = PCM_FORMAT_S16_LE, 348 }; 349 350 struct pcm_config pcm_config_vx = { 351 .channels = 2, 352 .rate = VX_NB_SAMPLING_RATE, 353 .period_size = 160, 354 .period_count = 2, 355 .format = PCM_FORMAT_S16_LE, 356 }; 357 358 #define MIN(x, y) ((x) > (y) ? (y) : (x)) 359 360 struct route_setting 361 { 362 char *ctl_name; 363 int intval; 364 char *strval; 365 }; 366 367 /* These are values that never change */ 368 struct route_setting defaults[] = { 369 /* general */ 370 { 371 .ctl_name = MIXER_DL2_LEFT_EQUALIZER, 372 .strval = MIXER_450HZ_HIGH_PASS, 373 }, 374 { 375 .ctl_name = MIXER_DL2_RIGHT_EQUALIZER, 376 .strval = MIXER_450HZ_HIGH_PASS, 377 }, 378 { 379 .ctl_name = MIXER_DL1_MEDIA_PLAYBACK_VOLUME, 380 .intval = MIXER_ABE_GAIN_0DB, 381 }, 382 { 383 .ctl_name = MIXER_DL2_MEDIA_PLAYBACK_VOLUME, 384 .intval = MIXER_ABE_GAIN_0DB, 385 }, 386 { 387 .ctl_name = MIXER_DL1_VOICE_PLAYBACK_VOLUME, 388 .intval = MIXER_ABE_GAIN_0DB, 389 }, 390 { 391 .ctl_name = MIXER_DL2_VOICE_PLAYBACK_VOLUME, 392 .intval = MIXER_ABE_GAIN_0DB, 393 }, 394 { 395 .ctl_name = MIXER_DL1_TONES_PLAYBACK_VOLUME, 396 .intval = MIXER_ABE_GAIN_0DB, 397 }, 398 { 399 .ctl_name = MIXER_DL2_TONES_PLAYBACK_VOLUME, 400 .intval = MIXER_ABE_GAIN_0DB, 401 }, 402 { 403 .ctl_name = MIXER_SDT_DL_VOLUME, 404 .intval = MIXER_ABE_GAIN_0DB, 405 }, 406 { 407 .ctl_name = MIXER_AUDUL_VOICE_UL_VOLUME, 408 .intval = MIXER_ABE_GAIN_0DB, 409 }, 410 { 411 .ctl_name = MIXER_CAPTURE_PREAMPLIFIER_VOLUME, 412 .intval = DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(0), 413 }, 414 { 415 .ctl_name = MIXER_CAPTURE_VOLUME, 416 .intval = DB_TO_CAPTURE_VOLUME(30), 417 }, 418 { 419 .ctl_name = MIXER_SDT_UL_VOLUME, 420 .intval = MIXER_ABE_GAIN_0DB - 17, 421 }, 422 { 423 .ctl_name = MIXER_SIDETONE_MIXER_CAPTURE, 424 .intval = 0, 425 }, 426 427 /* headset */ 428 { 429 .ctl_name = MIXER_SIDETONE_MIXER_PLAYBACK, 430 .intval = 1, 431 }, 432 { 433 .ctl_name = MIXER_DL1_PDM_SWITCH, 434 .intval = 1, 435 }, 436 437 /* bt */ 438 { 439 .ctl_name = MIXER_BT_UL_VOLUME, 440 .intval = MIXER_ABE_GAIN_0DB, 441 }, 442 { 443 .ctl_name = NULL, 444 }, 445 }; 446 447 struct route_setting hf_output[] = { 448 { 449 .ctl_name = MIXER_HF_LEFT_PLAYBACK, 450 .strval = MIXER_PLAYBACK_HF_DAC, 451 }, 452 { 453 .ctl_name = MIXER_HF_RIGHT_PLAYBACK, 454 .strval = MIXER_PLAYBACK_HF_DAC, 455 }, 456 { 457 .ctl_name = NULL, 458 }, 459 }; 460 461 struct route_setting hs_output[] = { 462 { 463 .ctl_name = MIXER_HS_LEFT_PLAYBACK, 464 .strval = MIXER_PLAYBACK_HS_DAC, 465 }, 466 { 467 .ctl_name = MIXER_HS_RIGHT_PLAYBACK, 468 .strval = MIXER_PLAYBACK_HS_DAC, 469 }, 470 { 471 .ctl_name = NULL, 472 }, 473 }; 474 475 /* MM UL front-end paths */ 476 struct route_setting mm_ul2_bt[] = { 477 { 478 .ctl_name = MIXER_MUX_UL10, 479 .strval = MIXER_BT_LEFT, 480 }, 481 { 482 .ctl_name = MIXER_MUX_UL11, 483 .strval = MIXER_BT_LEFT, 484 }, 485 { 486 .ctl_name = NULL, 487 }, 488 }; 489 490 struct route_setting mm_ul2_amic_left[] = { 491 { 492 .ctl_name = MIXER_MUX_UL10, 493 .strval = MIXER_AMIC0, 494 }, 495 { 496 .ctl_name = MIXER_MUX_UL11, 497 .strval = MIXER_AMIC0, 498 }, 499 { 500 .ctl_name = NULL, 501 }, 502 }; 503 504 struct route_setting mm_ul2_amic_right[] = { 505 { 506 .ctl_name = MIXER_MUX_UL10, 507 .strval = MIXER_AMIC1, 508 }, 509 { 510 .ctl_name = MIXER_MUX_UL11, 511 .strval = MIXER_AMIC1, 512 }, 513 { 514 .ctl_name = NULL, 515 }, 516 }; 517 518 /* dual mic configuration with main mic on main channel and sub mic on aux channel. 519 * Used for handset mode (near talk) */ 520 struct route_setting mm_ul2_amic_dual_main_sub[] = { 521 { 522 .ctl_name = MIXER_MUX_UL10, 523 .strval = MIXER_AMIC0, 524 }, 525 { 526 .ctl_name = MIXER_MUX_UL11, 527 .strval = MIXER_AMIC1, 528 }, 529 { 530 .ctl_name = NULL, 531 }, 532 }; 533 534 /* dual mic configuration with sub mic on main channel and main mic on aux channel. 535 * Used for speakerphone mode (far talk) */ 536 struct route_setting mm_ul2_amic_dual_sub_main[] = { 537 { 538 .ctl_name = MIXER_MUX_UL10, 539 .strval = MIXER_AMIC1, 540 }, 541 { 542 .ctl_name = MIXER_MUX_UL11, 543 .strval = MIXER_AMIC0, 544 }, 545 { 546 .ctl_name = NULL, 547 }, 548 }; 549 550 /* VX UL front-end paths */ 551 struct route_setting vx_ul_amic_left[] = { 552 { 553 .ctl_name = MIXER_MUX_VX0, 554 .strval = MIXER_AMIC0, 555 }, 556 { 557 .ctl_name = MIXER_MUX_VX1, 558 .strval = MIXER_AMIC0, 559 }, 560 { 561 .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE, 562 .intval = 1, 563 }, 564 { 565 .ctl_name = NULL, 566 }, 567 }; 568 569 struct route_setting vx_ul_amic_right[] = { 570 { 571 .ctl_name = MIXER_MUX_VX0, 572 .strval = MIXER_AMIC1, 573 }, 574 { 575 .ctl_name = MIXER_MUX_VX1, 576 .strval = MIXER_AMIC1, 577 }, 578 { 579 .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE, 580 .intval = 1, 581 }, 582 { 583 .ctl_name = NULL, 584 }, 585 }; 586 587 struct route_setting vx_ul_bt[] = { 588 { 589 .ctl_name = MIXER_MUX_VX0, 590 .strval = MIXER_BT_LEFT, 591 }, 592 { 593 .ctl_name = MIXER_MUX_VX1, 594 .strval = MIXER_BT_LEFT, 595 }, 596 { 597 .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE, 598 .intval = 1, 599 }, 600 { 601 .ctl_name = NULL, 602 }, 603 }; 604 605 struct mixer_ctls 606 { 607 struct mixer_ctl *dl1_eq; 608 struct mixer_ctl *mm_dl1_volume; 609 struct mixer_ctl *tones_dl1_volume; 610 struct mixer_ctl *mm_dl2_volume; 611 struct mixer_ctl *vx_dl2_volume; 612 struct mixer_ctl *tones_dl2_volume; 613 struct mixer_ctl *mm_dl1; 614 struct mixer_ctl *mm_dl2; 615 struct mixer_ctl *vx_dl1; 616 struct mixer_ctl *vx_dl2; 617 struct mixer_ctl *tones_dl1; 618 struct mixer_ctl *tones_dl2; 619 struct mixer_ctl *earpiece_enable; 620 struct mixer_ctl *dl2_mono; 621 struct mixer_ctl *dl1_headset; 622 struct mixer_ctl *dl1_bt; 623 struct mixer_ctl *left_capture; 624 struct mixer_ctl *right_capture; 625 struct mixer_ctl *amic_ul_volume; 626 struct mixer_ctl *voice_ul_volume; 627 struct mixer_ctl *sidetone_capture; 628 struct mixer_ctl *headset_volume; 629 struct mixer_ctl *speaker_volume; 630 struct mixer_ctl *earpiece_volume; 631 }; 632 633 enum output_type { 634 OUTPUT_DEEP_BUF, // deep PCM buffers output stream 635 OUTPUT_LOW_LATENCY, // low latency output stream 636 OUTPUT_HDMI, 637 OUTPUT_TOTAL 638 }; 639 640 641 struct tuna_audio_device { 642 struct audio_hw_device hw_device; 643 644 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 645 struct mixer *mixer; 646 struct mixer_ctls mixer_ctls; 647 audio_mode_t mode; 648 int out_device; 649 int in_device; 650 struct pcm *pcm_modem_dl; 651 struct pcm *pcm_modem_ul; 652 int in_call; 653 float voice_volume; 654 struct tuna_stream_in *active_input; 655 struct tuna_stream_out *outputs[OUTPUT_TOTAL]; 656 bool mic_mute; 657 int tty_mode; 658 struct echo_reference_itfe *echo_reference; 659 bool bluetooth_nrec; 660 bool device_is_toro; 661 int wb_amr; 662 bool screen_off; 663 664 /* RIL */ 665 struct ril_handle ril; 666 }; 667 668 enum pcm_type { 669 PCM_NORMAL = 0, 670 PCM_SPDIF, 671 PCM_HDMI, 672 PCM_TOTAL, 673 }; 674 675 struct tuna_stream_out { 676 struct audio_stream_out stream; 677 678 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 679 struct pcm_config config[PCM_TOTAL]; 680 struct pcm *pcm[PCM_TOTAL]; 681 struct resampler_itfe *resampler; 682 char *buffer; 683 size_t buffer_frames; 684 int standby; 685 struct echo_reference_itfe *echo_reference; 686 int write_threshold; 687 bool use_long_periods; 688 audio_channel_mask_t channel_mask; 689 audio_channel_mask_t sup_channel_masks[3]; 690 691 /* FIXME: workaround for HDMI multi channel channel swap on first playback after opening 692 * the output stream: force reopening the pcm driver after writing a few periods. */ 693 int restart_periods_cnt; 694 695 struct tuna_audio_device *dev; 696 }; 697 698 #define MAX_PREPROCESSORS 3 /* maximum one AGC + one NS + one AEC per input stream */ 699 700 struct effect_info_s { 701 effect_handle_t effect_itfe; 702 size_t num_channel_configs; 703 channel_config_t* channel_configs; 704 }; 705 706 #define NUM_IN_AUX_CNL_CONFIGS 2 707 channel_config_t in_aux_cnl_configs[NUM_IN_AUX_CNL_CONFIGS] = { 708 { AUDIO_CHANNEL_IN_FRONT , AUDIO_CHANNEL_IN_BACK}, 709 { AUDIO_CHANNEL_IN_STEREO , AUDIO_CHANNEL_IN_RIGHT} 710 }; 711 712 713 struct tuna_stream_in { 714 struct audio_stream_in stream; 715 716 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 717 struct pcm_config config; 718 struct pcm *pcm; 719 int device; 720 struct resampler_itfe *resampler; 721 struct resampler_buffer_provider buf_provider; 722 unsigned int requested_rate; 723 int standby; 724 int source; 725 struct echo_reference_itfe *echo_reference; 726 bool need_echo_reference; 727 728 int16_t *read_buf; 729 size_t read_buf_size; 730 size_t read_buf_frames; 731 732 int16_t *proc_buf_in; 733 int16_t *proc_buf_out; 734 size_t proc_buf_size; 735 size_t proc_buf_frames; 736 737 int16_t *ref_buf; 738 size_t ref_buf_size; 739 size_t ref_buf_frames; 740 741 int read_status; 742 743 int num_preprocessors; 744 struct effect_info_s preprocessors[MAX_PREPROCESSORS]; 745 746 bool aux_channels_changed; 747 uint32_t main_channels; 748 uint32_t aux_channels; 749 struct tuna_audio_device *dev; 750 }; 751 752 753 #define STRING_TO_ENUM(string) { #string, string } 754 755 struct string_to_enum { 756 const char *name; 757 uint32_t value; 758 }; 759 760 const struct string_to_enum out_channels_name_to_enum_table[] = { 761 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 762 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 763 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 764 }; 765 766 767 /** 768 * NOTE: when multiple mutexes have to be acquired, always respect the following order: 769 * hw device > in stream > out stream 770 */ 771 772 773 static void select_output_device(struct tuna_audio_device *adev); 774 static void select_input_device(struct tuna_audio_device *adev); 775 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume); 776 static int do_input_standby(struct tuna_stream_in *in); 777 static int do_output_standby(struct tuna_stream_out *out); 778 static void in_update_aux_channels(struct tuna_stream_in *in, effect_handle_t effect); 779 780 /* Returns true on devices that are toro, false otherwise */ 781 static int is_device_toro(void) 782 { 783 char property[PROPERTY_VALUE_MAX]; 784 785 property_get(PRODUCT_DEVICE_PROPERTY, property, PRODUCT_DEVICE_TORO); 786 787 /* return true if the property matches the given value */ 788 return strcmp(property, PRODUCT_DEVICE_TORO) == 0; 789 } 790 791 /* The enable flag when 0 makes the assumption that enums are disabled by 792 * "Off" and integers/booleans by 0 */ 793 static int set_route_by_array(struct mixer *mixer, struct route_setting *route, 794 int enable) 795 { 796 struct mixer_ctl *ctl; 797 unsigned int i, j; 798 799 /* Go through the route array and set each value */ 800 i = 0; 801 while (route[i].ctl_name) { 802 ctl = mixer_get_ctl_by_name(mixer, route[i].ctl_name); 803 if (!ctl) 804 return -EINVAL; 805 806 if (route[i].strval) { 807 if (enable) 808 mixer_ctl_set_enum_by_string(ctl, route[i].strval); 809 else 810 mixer_ctl_set_enum_by_string(ctl, "Off"); 811 } else { 812 /* This ensures multiple (i.e. stereo) values are set jointly */ 813 for (j = 0; j < mixer_ctl_get_num_values(ctl); j++) { 814 if (enable) 815 mixer_ctl_set_value(ctl, j, route[i].intval); 816 else 817 mixer_ctl_set_value(ctl, j, 0); 818 } 819 } 820 i++; 821 } 822 823 return 0; 824 } 825 826 static int start_call(struct tuna_audio_device *adev) 827 { 828 ALOGE("Opening modem PCMs"); 829 830 pcm_config_vx.rate = adev->wb_amr ? VX_WB_SAMPLING_RATE : VX_NB_SAMPLING_RATE; 831 832 /* Open modem PCM channels */ 833 if (adev->pcm_modem_dl == NULL) { 834 adev->pcm_modem_dl = pcm_open(0, PORT_MODEM, PCM_OUT, &pcm_config_vx); 835 if (!pcm_is_ready(adev->pcm_modem_dl)) { 836 ALOGE("cannot open PCM modem DL stream: %s", pcm_get_error(adev->pcm_modem_dl)); 837 goto err_open_dl; 838 } 839 } 840 841 if (adev->pcm_modem_ul == NULL) { 842 adev->pcm_modem_ul = pcm_open(0, PORT_MODEM, PCM_IN, &pcm_config_vx); 843 if (!pcm_is_ready(adev->pcm_modem_ul)) { 844 ALOGE("cannot open PCM modem UL stream: %s", pcm_get_error(adev->pcm_modem_ul)); 845 goto err_open_ul; 846 } 847 } 848 849 pcm_start(adev->pcm_modem_dl); 850 pcm_start(adev->pcm_modem_ul); 851 852 return 0; 853 854 err_open_ul: 855 pcm_close(adev->pcm_modem_ul); 856 adev->pcm_modem_ul = NULL; 857 err_open_dl: 858 pcm_close(adev->pcm_modem_dl); 859 adev->pcm_modem_dl = NULL; 860 861 return -ENOMEM; 862 } 863 864 static void end_call(struct tuna_audio_device *adev) 865 { 866 ALOGE("Closing modem PCMs"); 867 868 pcm_stop(adev->pcm_modem_dl); 869 pcm_stop(adev->pcm_modem_ul); 870 pcm_close(adev->pcm_modem_dl); 871 pcm_close(adev->pcm_modem_ul); 872 adev->pcm_modem_dl = NULL; 873 adev->pcm_modem_ul = NULL; 874 } 875 876 static void set_eq_filter(struct tuna_audio_device *adev) 877 { 878 /* DL1_EQ can't be used for bt */ 879 int dl1_eq_applicable = adev->out_device & (AUDIO_DEVICE_OUT_WIRED_HEADSET | 880 AUDIO_DEVICE_OUT_WIRED_HEADPHONE | AUDIO_DEVICE_OUT_EARPIECE); 881 882 /* 4Khz LPF is used only in NB-AMR voicecall */ 883 if ((adev->mode == AUDIO_MODE_IN_CALL) && dl1_eq_applicable && 884 (adev->tty_mode == TTY_MODE_OFF) && !adev->wb_amr) 885 mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_4KHZ_LPF_0DB); 886 else 887 mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_FLAT_RESPONSE); 888 } 889 890 void audio_set_wb_amr_callback(void *data, int enable) 891 { 892 struct tuna_audio_device *adev = (struct tuna_audio_device *)data; 893 894 pthread_mutex_lock(&adev->lock); 895 if (adev->wb_amr != enable) { 896 adev->wb_amr = enable; 897 898 /* reopen the modem PCMs at the new rate */ 899 if (adev->in_call) { 900 end_call(adev); 901 set_eq_filter(adev); 902 start_call(adev); 903 } 904 } 905 pthread_mutex_unlock(&adev->lock); 906 } 907 908 static void set_incall_device(struct tuna_audio_device *adev) 909 { 910 int device_type; 911 912 switch(adev->out_device) { 913 case AUDIO_DEVICE_OUT_EARPIECE: 914 device_type = SOUND_AUDIO_PATH_HANDSET; 915 break; 916 case AUDIO_DEVICE_OUT_SPEAKER: 917 case AUDIO_DEVICE_OUT_AUX_DIGITAL: 918 case AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET: 919 device_type = SOUND_AUDIO_PATH_SPEAKER; 920 break; 921 case AUDIO_DEVICE_OUT_WIRED_HEADSET: 922 device_type = SOUND_AUDIO_PATH_HEADSET; 923 break; 924 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: 925 device_type = SOUND_AUDIO_PATH_HEADPHONE; 926 break; 927 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: 928 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 929 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 930 if (adev->bluetooth_nrec) 931 device_type = SOUND_AUDIO_PATH_BLUETOOTH; 932 else 933 device_type = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR; 934 break; 935 default: 936 device_type = SOUND_AUDIO_PATH_HANDSET; 937 break; 938 } 939 940 /* if output device isn't supported, open modem side to handset by default */ 941 ril_set_call_audio_path(&adev->ril, device_type); 942 } 943 944 static void set_input_volumes(struct tuna_audio_device *adev, int main_mic_on, 945 int headset_mic_on, int sub_mic_on) 946 { 947 unsigned int channel; 948 int volume = MIXER_ABE_GAIN_0DB; 949 950 if (adev->mode == AUDIO_MODE_IN_CALL) { 951 int sub_mic_volume = is_device_toro() ? VOICE_CALL_SUB_MIC_VOLUME_TORO : 952 VOICE_CALL_SUB_MIC_VOLUME_MAGURO; 953 /* special case: don't look at input source for IN_CALL state */ 954 volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_CALL_MAIN_MIC_VOLUME : 955 (headset_mic_on ? VOICE_CALL_HEADSET_MIC_VOLUME : 956 (sub_mic_on ? sub_mic_volume : 0))); 957 } else if (adev->active_input) { 958 /* determine input volume by use case */ 959 switch (adev->active_input->source) { 960 case AUDIO_SOURCE_MIC: /* general capture */ 961 volume = DB_TO_ABE_GAIN(main_mic_on ? CAPTURE_MAIN_MIC_VOLUME : 962 (headset_mic_on ? CAPTURE_HEADSET_MIC_VOLUME : 963 (sub_mic_on ? CAPTURE_SUB_MIC_VOLUME : 0))); 964 break; 965 966 case AUDIO_SOURCE_CAMCORDER: 967 volume = DB_TO_ABE_GAIN(main_mic_on ? CAMCORDER_MAIN_MIC_VOLUME : 968 (headset_mic_on ? CAMCORDER_HEADSET_MIC_VOLUME : 969 (sub_mic_on ? CAMCORDER_SUB_MIC_VOLUME : 0))); 970 break; 971 972 case AUDIO_SOURCE_VOICE_RECOGNITION: 973 volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_RECOGNITION_MAIN_MIC_VOLUME : 974 (headset_mic_on ? VOICE_RECOGNITION_HEADSET_MIC_VOLUME : 975 (sub_mic_on ? VOICE_RECOGNITION_SUB_MIC_VOLUME : 0))); 976 break; 977 978 case AUDIO_SOURCE_VOICE_COMMUNICATION: /* VoIP */ 979 volume = DB_TO_ABE_GAIN(main_mic_on ? VOIP_MAIN_MIC_VOLUME : 980 (headset_mic_on ? VOIP_HEADSET_MIC_VOLUME : 981 (sub_mic_on ? VOIP_SUB_MIC_VOLUME : 0))); 982 break; 983 984 default: 985 /* nothing to do */ 986 break; 987 } 988 } 989 990 for (channel = 0; channel < 2; channel++) 991 mixer_ctl_set_value(adev->mixer_ctls.amic_ul_volume, channel, volume); 992 } 993 994 static void set_output_volumes(struct tuna_audio_device *adev, bool tty_volume) 995 { 996 unsigned int channel; 997 int speaker_volume; 998 int headset_volume; 999 int earpiece_volume; 1000 bool toro = adev->device_is_toro; 1001 int headphone_on = adev->out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 1002 int speaker_on = adev->out_device & AUDIO_DEVICE_OUT_SPEAKER; 1003 int speaker_volume_overrange = MIXER_ABE_GAIN_0DB; 1004 int speaker_max_db = 1005 DB_FROM_SPEAKER_VOLUME(mixer_ctl_get_range_max(adev->mixer_ctls.speaker_volume)); 1006 int normal_speaker_volume = toro ? NORMAL_SPEAKER_VOLUME_TORO : 1007 NORMAL_SPEAKER_VOLUME_MAGURO; 1008 int normal_headphone_volume = toro ? NORMAL_HEADPHONE_VOLUME_TORO : 1009 NORMAL_HEADPHONE_VOLUME_MAGURO; 1010 int normal_headset_volume = toro ? NORMAL_HEADSET_VOLUME_TORO : 1011 NORMAL_HEADSET_VOLUME_MAGURO; 1012 int normal_earpiece_volume = toro ? NORMAL_EARPIECE_VOLUME_TORO : 1013 NORMAL_EARPIECE_VOLUME_MAGURO; 1014 int dl1_volume_correction = 0; 1015 int dl2_volume_correction = 0; 1016 1017 if (adev->mode == AUDIO_MODE_IN_CALL) { 1018 /* Voice call */ 1019 speaker_volume = toro ? VOICE_CALL_SPEAKER_VOLUME_TORO : 1020 VOICE_CALL_SPEAKER_VOLUME_MAGURO; 1021 headset_volume = toro ? VOICE_CALL_HEADSET_VOLUME_TORO : 1022 VOICE_CALL_HEADSET_VOLUME_MAGURO; 1023 earpiece_volume = toro ? VOICE_CALL_EARPIECE_VOLUME_TORO : 1024 VOICE_CALL_EARPIECE_VOLUME_MAGURO; 1025 } else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) { 1026 /* VoIP */ 1027 speaker_volume = toro ? VOIP_SPEAKER_VOLUME_TORO : 1028 VOIP_SPEAKER_VOLUME_MAGURO; 1029 headset_volume = toro ? VOIP_HEADSET_VOLUME_TORO : 1030 VOIP_HEADSET_VOLUME_MAGURO; 1031 earpiece_volume = toro ? VOIP_EARPIECE_VOLUME_TORO : 1032 VOIP_EARPIECE_VOLUME_MAGURO; 1033 } else { 1034 /* Media */ 1035 speaker_volume = normal_speaker_volume; 1036 if (headphone_on) 1037 headset_volume = normal_headphone_volume; 1038 else 1039 headset_volume = normal_headset_volume; 1040 earpiece_volume = normal_earpiece_volume; 1041 } 1042 1043 if (tty_volume) 1044 headset_volume = HEADPHONE_VOLUME_TTY; 1045 else if (adev->mode == AUDIO_MODE_RINGTONE) 1046 headset_volume += RINGTONE_HEADSET_VOLUME_OFFSET; 1047 1048 /* apply correction on digital volume to keep the overall volume consistent if the 1049 * analog volume is not driven by media use case 1050 */ 1051 if (headphone_on) 1052 dl1_volume_correction = normal_headphone_volume - headset_volume; 1053 else if (adev->out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) 1054 dl1_volume_correction = normal_headset_volume - headset_volume; 1055 else 1056 dl1_volume_correction = normal_earpiece_volume - earpiece_volume; 1057 1058 if (speaker_on) 1059 dl2_volume_correction = normal_speaker_volume - speaker_volume; 1060 1061 /* If we have run out of range in the codec (analog) speaker volume, 1062 we have to apply the remainder of the dB increase to the DL2 1063 media/voice mixer volume, which is a digital gain */ 1064 if (speaker_volume > speaker_max_db) { 1065 speaker_volume_overrange += (speaker_volume - speaker_max_db); 1066 speaker_volume = speaker_max_db; 1067 } 1068 1069 for (channel = 0; channel < 2; channel++) { 1070 mixer_ctl_set_value(adev->mixer_ctls.speaker_volume, channel, 1071 DB_TO_SPEAKER_VOLUME(speaker_volume)); 1072 mixer_ctl_set_value(adev->mixer_ctls.headset_volume, channel, 1073 DB_TO_HEADSET_VOLUME(headset_volume)); 1074 } 1075 1076 if (!speaker_on) 1077 speaker_volume_overrange = MIXER_ABE_GAIN_0DB; 1078 1079 if (adev->mode == AUDIO_MODE_IN_CALL) { 1080 mixer_ctl_set_value(adev->mixer_ctls.tones_dl1_volume, 0, 1081 MIXER_ABE_GAIN_0DB + dl1_volume_correction); 1082 mixer_ctl_set_value(adev->mixer_ctls.vx_dl2_volume, 0, 1083 speaker_volume_overrange); 1084 mixer_ctl_set_value(adev->mixer_ctls.tones_dl2_volume, 0, 1085 speaker_volume_overrange + dl2_volume_correction); 1086 } else if ((adev->mode == AUDIO_MODE_IN_COMMUNICATION) || 1087 (adev->mode == AUDIO_MODE_RINGTONE)) { 1088 mixer_ctl_set_value(adev->mixer_ctls.tones_dl1_volume, 0, 1089 MIXER_ABE_GAIN_0DB); 1090 mixer_ctl_set_value(adev->mixer_ctls.tones_dl2_volume, 0, 1091 speaker_volume_overrange); 1092 } else { 1093 mixer_ctl_set_value(adev->mixer_ctls.tones_dl1_volume, 0, 1094 MIXER_ABE_GAIN_0DB + dl1_volume_correction); 1095 mixer_ctl_set_value(adev->mixer_ctls.tones_dl2_volume, 0, 1096 speaker_volume_overrange + dl2_volume_correction); 1097 } 1098 1099 mixer_ctl_set_value(adev->mixer_ctls.mm_dl1_volume, 0, 1100 MIXER_ABE_GAIN_0DB + dl1_volume_correction); 1101 mixer_ctl_set_value(adev->mixer_ctls.mm_dl2_volume, 0, 1102 speaker_volume_overrange + dl2_volume_correction); 1103 1104 mixer_ctl_set_value(adev->mixer_ctls.earpiece_volume, 0, 1105 DB_TO_EARPIECE_VOLUME(earpiece_volume)); 1106 } 1107 1108 static void force_all_standby(struct tuna_audio_device *adev) 1109 { 1110 struct tuna_stream_in *in; 1111 struct tuna_stream_out *out; 1112 1113 /* only needed for low latency output streams as other streams are not used 1114 * for voice use cases */ 1115 if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL && 1116 !adev->outputs[OUTPUT_LOW_LATENCY]->standby) { 1117 out = adev->outputs[OUTPUT_LOW_LATENCY]; 1118 pthread_mutex_lock(&out->lock); 1119 do_output_standby(out); 1120 pthread_mutex_unlock(&out->lock); 1121 } 1122 1123 if (adev->active_input) { 1124 in = adev->active_input; 1125 pthread_mutex_lock(&in->lock); 1126 do_input_standby(in); 1127 pthread_mutex_unlock(&in->lock); 1128 } 1129 } 1130 1131 static void select_mode(struct tuna_audio_device *adev) 1132 { 1133 if (adev->mode == AUDIO_MODE_IN_CALL) { 1134 ALOGE("Entering IN_CALL state, in_call=%d", adev->in_call); 1135 if (!adev->in_call) { 1136 force_all_standby(adev); 1137 /* force earpiece route for in call state if speaker is the 1138 only currently selected route. This prevents having to tear 1139 down the modem PCMs to change route from speaker to earpiece 1140 after the ringtone is played, but doesn't cause a route 1141 change if a headset or bt device is already connected. If 1142 speaker is not the only thing active, just remove it from 1143 the route. We'll assume it'll never be used initially during 1144 a call. This works because we're sure that the audio policy 1145 manager will update the output device after the audio mode 1146 change, even if the device selection did not change. */ 1147 if (adev->out_device == AUDIO_DEVICE_OUT_SPEAKER) { 1148 adev->out_device = AUDIO_DEVICE_OUT_EARPIECE; 1149 adev->in_device = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN; 1150 } else 1151 adev->out_device &= ~AUDIO_DEVICE_OUT_SPEAKER; 1152 select_output_device(adev); 1153 start_call(adev); 1154 ril_set_call_volume(&adev->ril, SOUND_TYPE_VOICE, adev->voice_volume); 1155 adev->in_call = 1; 1156 } 1157 } else { 1158 ALOGE("Leaving IN_CALL state, in_call=%d, mode=%d", 1159 adev->in_call, adev->mode); 1160 if (adev->in_call) { 1161 adev->in_call = 0; 1162 end_call(adev); 1163 force_all_standby(adev); 1164 select_output_device(adev); 1165 select_input_device(adev); 1166 } 1167 } 1168 } 1169 1170 static void select_output_device(struct tuna_audio_device *adev) 1171 { 1172 int headset_on; 1173 int headphone_on; 1174 int speaker_on; 1175 int earpiece_on; 1176 int bt_on; 1177 int dl1_on; 1178 int sidetone_capture_on = 0; 1179 bool tty_volume = false; 1180 unsigned int channel; 1181 1182 /* Mute VX_UL to avoid pop noises in the tx path 1183 * during call before switch changes. 1184 */ 1185 if (adev->mode == AUDIO_MODE_IN_CALL) { 1186 for (channel = 0; channel < 2; channel++) 1187 mixer_ctl_set_value(adev->mixer_ctls.voice_ul_volume, 1188 channel, 0); 1189 } 1190 1191 headset_on = adev->out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET; 1192 headphone_on = adev->out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 1193 speaker_on = adev->out_device & AUDIO_DEVICE_OUT_SPEAKER; 1194 earpiece_on = adev->out_device & AUDIO_DEVICE_OUT_EARPIECE; 1195 bt_on = adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO; 1196 1197 /* force rx path according to TTY mode when in call */ 1198 if (adev->mode == AUDIO_MODE_IN_CALL && !bt_on) { 1199 switch(adev->tty_mode) { 1200 case TTY_MODE_FULL: 1201 case TTY_MODE_VCO: 1202 /* rx path to headphones */ 1203 headphone_on = 1; 1204 headset_on = 0; 1205 speaker_on = 0; 1206 earpiece_on = 0; 1207 tty_volume = true; 1208 break; 1209 case TTY_MODE_HCO: 1210 /* rx path to device speaker */ 1211 headphone_on = 0; 1212 headset_on = 0; 1213 speaker_on = 1; 1214 earpiece_on = 0; 1215 break; 1216 case TTY_MODE_OFF: 1217 default: 1218 /* force speaker on when in call and HDMI or S/PDIF is selected 1219 * as voice DL audio cannot be routed there by ABE */ 1220 if (adev->out_device & 1221 (AUDIO_DEVICE_OUT_AUX_DIGITAL | 1222 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) 1223 speaker_on = 1; 1224 break; 1225 } 1226 } 1227 1228 dl1_on = headset_on | headphone_on | earpiece_on | bt_on; 1229 1230 /* Select front end */ 1231 mixer_ctl_set_value(adev->mixer_ctls.mm_dl2, 0, speaker_on); 1232 mixer_ctl_set_value(adev->mixer_ctls.tones_dl2, 0, speaker_on); 1233 mixer_ctl_set_value(adev->mixer_ctls.vx_dl2, 0, 1234 speaker_on && (adev->mode == AUDIO_MODE_IN_CALL)); 1235 mixer_ctl_set_value(adev->mixer_ctls.mm_dl1, 0, dl1_on); 1236 mixer_ctl_set_value(adev->mixer_ctls.tones_dl1, 0, dl1_on); 1237 mixer_ctl_set_value(adev->mixer_ctls.vx_dl1, 0, 1238 dl1_on && (adev->mode == AUDIO_MODE_IN_CALL)); 1239 /* Select back end */ 1240 mixer_ctl_set_value(adev->mixer_ctls.dl1_headset, 0, 1241 headset_on | headphone_on | earpiece_on); 1242 mixer_ctl_set_value(adev->mixer_ctls.dl1_bt, 0, bt_on); 1243 mixer_ctl_set_value(adev->mixer_ctls.dl2_mono, 0, 1244 (adev->mode != AUDIO_MODE_IN_CALL) && speaker_on); 1245 mixer_ctl_set_value(adev->mixer_ctls.earpiece_enable, 0, earpiece_on); 1246 1247 /* select output stage */ 1248 set_route_by_array(adev->mixer, hs_output, headset_on | headphone_on); 1249 set_route_by_array(adev->mixer, hf_output, speaker_on); 1250 1251 set_eq_filter(adev); 1252 set_output_volumes(adev, tty_volume); 1253 1254 /* Special case: select input path if in a call, otherwise 1255 in_set_parameters is used to update the input route 1256 todo: use sub mic for handsfree case */ 1257 if (adev->mode == AUDIO_MODE_IN_CALL) { 1258 if (bt_on) 1259 set_route_by_array(adev->mixer, vx_ul_bt, bt_on); 1260 else { 1261 /* force tx path according to TTY mode when in call */ 1262 switch(adev->tty_mode) { 1263 case TTY_MODE_FULL: 1264 case TTY_MODE_HCO: 1265 /* tx path from headset mic */ 1266 headphone_on = 0; 1267 headset_on = 1; 1268 speaker_on = 0; 1269 earpiece_on = 0; 1270 break; 1271 case TTY_MODE_VCO: 1272 /* tx path from device sub mic */ 1273 headphone_on = 0; 1274 headset_on = 0; 1275 speaker_on = 1; 1276 earpiece_on = 0; 1277 break; 1278 case TTY_MODE_OFF: 1279 default: 1280 break; 1281 } 1282 1283 if (headset_on || headphone_on || earpiece_on) 1284 set_route_by_array(adev->mixer, vx_ul_amic_left, 1); 1285 else if (speaker_on) 1286 set_route_by_array(adev->mixer, vx_ul_amic_right, 1); 1287 else 1288 set_route_by_array(adev->mixer, vx_ul_amic_left, 0); 1289 1290 mixer_ctl_set_enum_by_string(adev->mixer_ctls.left_capture, 1291 (earpiece_on || headphone_on) ? MIXER_MAIN_MIC : 1292 (headset_on ? MIXER_HS_MIC : "Off")); 1293 mixer_ctl_set_enum_by_string(adev->mixer_ctls.right_capture, 1294 speaker_on ? MIXER_SUB_MIC : "Off"); 1295 1296 set_input_volumes(adev, earpiece_on || headphone_on, 1297 headset_on, speaker_on); 1298 1299 /* enable sidetone mixer capture if needed */ 1300 sidetone_capture_on = earpiece_on && adev->device_is_toro; 1301 } 1302 1303 set_incall_device(adev); 1304 1305 /* Unmute VX_UL after the switch */ 1306 for (channel = 0; channel < 2; channel++) { 1307 mixer_ctl_set_value(adev->mixer_ctls.voice_ul_volume, 1308 channel, MIXER_ABE_GAIN_0DB); 1309 } 1310 } 1311 1312 mixer_ctl_set_value(adev->mixer_ctls.sidetone_capture, 0, sidetone_capture_on); 1313 } 1314 1315 static void select_input_device(struct tuna_audio_device *adev) 1316 { 1317 int headset_on = 0; 1318 int main_mic_on = 0; 1319 int sub_mic_on = 0; 1320 int bt_on = adev->in_device & AUDIO_DEVICE_IN_ALL_SCO; 1321 1322 if (!bt_on) { 1323 if ((adev->mode != AUDIO_MODE_IN_CALL) && (adev->active_input != 0)) { 1324 /* sub mic is used for camcorder or VoIP on speaker phone */ 1325 sub_mic_on = (adev->active_input->source == AUDIO_SOURCE_CAMCORDER) || 1326 ((adev->out_device & AUDIO_DEVICE_OUT_SPEAKER) && 1327 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION)); 1328 } 1329 if (!sub_mic_on) { 1330 headset_on = adev->in_device & AUDIO_DEVICE_IN_WIRED_HEADSET; 1331 main_mic_on = adev->in_device & AUDIO_DEVICE_IN_BUILTIN_MIC; 1332 } 1333 } 1334 1335 /* TODO: check how capture is possible during voice calls or if 1336 * both use cases are mutually exclusive. 1337 */ 1338 if (bt_on) 1339 set_route_by_array(adev->mixer, mm_ul2_bt, 1); 1340 else { 1341 /* Select front end */ 1342 1343 1344 if ((adev->active_input != 0) && (adev->active_input->aux_channels)) { 1345 ALOGV("select input device(): multi-mic configuration main mic %s sub mic %s", 1346 main_mic_on ? "ON" : "OFF", sub_mic_on ? "ON" : "OFF"); 1347 if (main_mic_on) { 1348 set_route_by_array(adev->mixer, mm_ul2_amic_dual_main_sub, 1); 1349 sub_mic_on = 1; 1350 } 1351 else if (sub_mic_on) { 1352 set_route_by_array(adev->mixer, mm_ul2_amic_dual_sub_main, 1); 1353 main_mic_on = 1; 1354 } 1355 else { 1356 set_route_by_array(adev->mixer, mm_ul2_amic_dual_main_sub, 0); 1357 } 1358 } else { 1359 ALOGV("select input device(): single mic configuration"); 1360 if (main_mic_on || headset_on) 1361 set_route_by_array(adev->mixer, mm_ul2_amic_left, 1); 1362 else if (sub_mic_on) 1363 set_route_by_array(adev->mixer, mm_ul2_amic_right, 1); 1364 else 1365 set_route_by_array(adev->mixer, mm_ul2_amic_left, 0); 1366 } 1367 1368 1369 /* Select back end */ 1370 mixer_ctl_set_enum_by_string(adev->mixer_ctls.right_capture, 1371 sub_mic_on ? MIXER_SUB_MIC : "Off"); 1372 mixer_ctl_set_enum_by_string(adev->mixer_ctls.left_capture, 1373 main_mic_on ? MIXER_MAIN_MIC : 1374 (headset_on ? MIXER_HS_MIC : "Off")); 1375 } 1376 1377 set_input_volumes(adev, main_mic_on, headset_on, sub_mic_on); 1378 } 1379 1380 /* must be called with hw device and output stream mutexes locked */ 1381 static int start_output_stream_low_latency(struct tuna_stream_out *out) 1382 { 1383 struct tuna_audio_device *adev = out->dev; 1384 #ifdef PLAYBACK_MMAP 1385 unsigned int flags = PCM_OUT | PCM_MMAP | PCM_NOIRQ; 1386 #else 1387 unsigned int flags = PCM_OUT; 1388 #endif 1389 int i; 1390 bool success = true; 1391 1392 if (adev->mode != AUDIO_MODE_IN_CALL) { 1393 select_output_device(adev); 1394 } 1395 1396 /* default to low power: will be corrected in out_write if necessary before first write to 1397 * tinyalsa. 1398 */ 1399 1400 if (adev->out_device & ~(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET | AUDIO_DEVICE_OUT_AUX_DIGITAL)) { 1401 /* Something not a dock in use */ 1402 out->config[PCM_NORMAL] = pcm_config_tones; 1403 out->config[PCM_NORMAL].rate = MM_FULL_POWER_SAMPLING_RATE; 1404 out->pcm[PCM_NORMAL] = pcm_open(CARD_TUNA_DEFAULT, PORT_TONES, 1405 flags, &out->config[PCM_NORMAL]); 1406 } 1407 1408 if (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) { 1409 /* SPDIF output in use */ 1410 out->config[PCM_SPDIF] = pcm_config_tones; 1411 out->config[PCM_SPDIF].rate = MM_FULL_POWER_SAMPLING_RATE; 1412 out->pcm[PCM_SPDIF] = pcm_open(CARD_TUNA_DEFAULT, PORT_SPDIF, 1413 flags, &out->config[PCM_SPDIF]); 1414 } 1415 1416 /* priority is given to multichannel HDMI output */ 1417 if ((adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) && 1418 (adev->outputs[OUTPUT_HDMI] == NULL || adev->outputs[OUTPUT_HDMI]->standby)) { 1419 /* HDMI output in use */ 1420 out->config[PCM_HDMI] = pcm_config_tones; 1421 out->config[PCM_HDMI].rate = MM_LOW_POWER_SAMPLING_RATE; 1422 out->pcm[PCM_HDMI] = pcm_open(CARD_OMAP4_HDMI, PORT_HDMI, 1423 flags, &out->config[PCM_HDMI]); 1424 } 1425 1426 /* Close any PCMs that could not be opened properly and return an error */ 1427 for (i = 0; i < PCM_TOTAL; i++) { 1428 if (out->pcm[i] && !pcm_is_ready(out->pcm[i])) { 1429 ALOGE("cannot open pcm_out driver %d: %s", i, pcm_get_error(out->pcm[i])); 1430 pcm_close(out->pcm[i]); 1431 out->pcm[i] = NULL; 1432 success = false; 1433 } 1434 } 1435 1436 if (success) { 1437 out->buffer_frames = pcm_config_tones.period_size * 2; 1438 if (out->buffer == NULL) 1439 out->buffer = malloc(out->buffer_frames * audio_stream_frame_size(&out->stream.common)); 1440 1441 if (adev->echo_reference != NULL) 1442 out->echo_reference = adev->echo_reference; 1443 out->resampler->reset(out->resampler); 1444 1445 return 0; 1446 } 1447 1448 return -ENOMEM; 1449 } 1450 1451 /* must be called with hw device and output stream mutexes locked */ 1452 static int start_output_stream_deep_buffer(struct tuna_stream_out *out) 1453 { 1454 struct tuna_audio_device *adev = out->dev; 1455 1456 if (adev->mode != AUDIO_MODE_IN_CALL) { 1457 select_output_device(adev); 1458 } 1459 1460 out->write_threshold = PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT * DEEP_BUFFER_LONG_PERIOD_SIZE; 1461 out->use_long_periods = true; 1462 1463 out->config[PCM_NORMAL] = pcm_config_mm; 1464 out->config[PCM_NORMAL].rate = MM_FULL_POWER_SAMPLING_RATE; 1465 out->pcm[PCM_NORMAL] = pcm_open(CARD_TUNA_DEFAULT, PORT_MM, 1466 PCM_OUT | PCM_MMAP | PCM_NOIRQ, &out->config[PCM_NORMAL]); 1467 if (out->pcm[PCM_NORMAL] && !pcm_is_ready(out->pcm[PCM_NORMAL])) { 1468 ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm[PCM_NORMAL])); 1469 pcm_close(out->pcm[PCM_NORMAL]); 1470 out->pcm[PCM_NORMAL] = NULL; 1471 return -ENOMEM; 1472 } 1473 out->buffer_frames = DEEP_BUFFER_SHORT_PERIOD_SIZE * 2; 1474 if (out->buffer == NULL) 1475 out->buffer = malloc(out->buffer_frames * audio_stream_frame_size(&out->stream.common)); 1476 1477 return 0; 1478 } 1479 1480 static int start_output_stream_hdmi(struct tuna_stream_out *out) 1481 { 1482 struct tuna_audio_device *adev = out->dev; 1483 1484 /* force standby on low latency output stream to close HDMI driver in case it was in use */ 1485 if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL && 1486 !adev->outputs[OUTPUT_LOW_LATENCY]->standby) { 1487 struct tuna_stream_out *ll_out = adev->outputs[OUTPUT_LOW_LATENCY]; 1488 pthread_mutex_lock(&ll_out->lock); 1489 do_output_standby(ll_out); 1490 pthread_mutex_unlock(&ll_out->lock); 1491 } 1492 1493 out->pcm[PCM_HDMI] = pcm_open(CARD_OMAP4_HDMI, PORT_HDMI, PCM_OUT, &out->config[PCM_HDMI]); 1494 1495 if (out->pcm[PCM_HDMI] && !pcm_is_ready(out->pcm[PCM_HDMI])) { 1496 ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm[PCM_HDMI])); 1497 pcm_close(out->pcm[PCM_HDMI]); 1498 out->pcm[PCM_HDMI] = NULL; 1499 return -ENOMEM; 1500 } 1501 return 0; 1502 } 1503 1504 static int check_input_parameters(uint32_t sample_rate, audio_format_t format, int channel_count) 1505 { 1506 if (format != AUDIO_FORMAT_PCM_16_BIT) 1507 return -EINVAL; 1508 1509 if ((channel_count < 1) || (channel_count > 2)) 1510 return -EINVAL; 1511 1512 switch(sample_rate) { 1513 case 8000: 1514 case 11025: 1515 case 16000: 1516 case 22050: 1517 case 24000: 1518 case 32000: 1519 case 44100: 1520 case 48000: 1521 break; 1522 default: 1523 return -EINVAL; 1524 } 1525 1526 return 0; 1527 } 1528 1529 static size_t get_input_buffer_size(uint32_t sample_rate, audio_format_t format, int channel_count) 1530 { 1531 size_t size; 1532 size_t device_rate; 1533 1534 if (check_input_parameters(sample_rate, format, channel_count) != 0) 1535 return 0; 1536 1537 /* take resampling into account and return the closest majoring 1538 multiple of 16 frames, as audioflinger expects audio buffers to 1539 be a multiple of 16 frames */ 1540 size = (pcm_config_mm_ul.period_size * sample_rate) / pcm_config_mm_ul.rate; 1541 size = ((size + 15) / 16) * 16; 1542 1543 return size * channel_count * sizeof(short); 1544 } 1545 1546 static void add_echo_reference(struct tuna_stream_out *out, 1547 struct echo_reference_itfe *reference) 1548 { 1549 pthread_mutex_lock(&out->lock); 1550 out->echo_reference = reference; 1551 pthread_mutex_unlock(&out->lock); 1552 } 1553 1554 static void remove_echo_reference(struct tuna_stream_out *out, 1555 struct echo_reference_itfe *reference) 1556 { 1557 pthread_mutex_lock(&out->lock); 1558 if (out->echo_reference == reference) { 1559 /* stop writing to echo reference */ 1560 reference->write(reference, NULL); 1561 out->echo_reference = NULL; 1562 } 1563 pthread_mutex_unlock(&out->lock); 1564 } 1565 1566 static void put_echo_reference(struct tuna_audio_device *adev, 1567 struct echo_reference_itfe *reference) 1568 { 1569 if (adev->echo_reference != NULL && 1570 reference == adev->echo_reference) { 1571 /* echo reference is taken from the low latency output stream used 1572 * for voice use cases */ 1573 if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL && 1574 !adev->outputs[OUTPUT_LOW_LATENCY]->standby) 1575 remove_echo_reference(adev->outputs[OUTPUT_LOW_LATENCY], reference); 1576 release_echo_reference(reference); 1577 adev->echo_reference = NULL; 1578 } 1579 } 1580 1581 static struct echo_reference_itfe *get_echo_reference(struct tuna_audio_device *adev, 1582 audio_format_t format, 1583 uint32_t channel_count, 1584 uint32_t sampling_rate) 1585 { 1586 put_echo_reference(adev, adev->echo_reference); 1587 /* echo reference is taken from the low latency output stream used 1588 * for voice use cases */ 1589 if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL && 1590 !adev->outputs[OUTPUT_LOW_LATENCY]->standby) { 1591 struct audio_stream *stream = 1592 &adev->outputs[OUTPUT_LOW_LATENCY]->stream.common; 1593 uint32_t wr_channel_count = popcount(stream->get_channels(stream)); 1594 uint32_t wr_sampling_rate = stream->get_sample_rate(stream); 1595 1596 int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT, 1597 channel_count, 1598 sampling_rate, 1599 AUDIO_FORMAT_PCM_16_BIT, 1600 wr_channel_count, 1601 wr_sampling_rate, 1602 &adev->echo_reference); 1603 if (status == 0) 1604 add_echo_reference(adev->outputs[OUTPUT_LOW_LATENCY], 1605 adev->echo_reference); 1606 } 1607 return adev->echo_reference; 1608 } 1609 1610 static int get_playback_delay(struct tuna_stream_out *out, 1611 size_t frames, 1612 struct echo_reference_buffer *buffer) 1613 { 1614 size_t kernel_frames; 1615 int status; 1616 int primary_pcm = 0; 1617 1618 /* Find the first active PCM to act as primary */ 1619 while ((primary_pcm < PCM_TOTAL) && !out->pcm[primary_pcm]) 1620 primary_pcm++; 1621 1622 status = pcm_get_htimestamp(out->pcm[primary_pcm], &kernel_frames, &buffer->time_stamp); 1623 if (status < 0) { 1624 buffer->time_stamp.tv_sec = 0; 1625 buffer->time_stamp.tv_nsec = 0; 1626 buffer->delay_ns = 0; 1627 ALOGV("get_playback_delay(): pcm_get_htimestamp error," 1628 "setting playbackTimestamp to 0"); 1629 return status; 1630 } 1631 1632 kernel_frames = pcm_get_buffer_size(out->pcm[primary_pcm]) - kernel_frames; 1633 1634 /* adjust render time stamp with delay added by current driver buffer. 1635 * Add the duration of current frame as we want the render time of the last 1636 * sample being written. */ 1637 buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/ 1638 MM_FULL_POWER_SAMPLING_RATE); 1639 1640 return 0; 1641 } 1642 1643 static uint32_t out_get_sample_rate(const struct audio_stream *stream) 1644 { 1645 return DEFAULT_OUT_SAMPLING_RATE; 1646 } 1647 1648 static uint32_t out_get_sample_rate_hdmi(const struct audio_stream *stream) 1649 { 1650 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1651 1652 return out->config[PCM_HDMI].rate; 1653 } 1654 1655 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1656 { 1657 return 0; 1658 } 1659 1660 static size_t out_get_buffer_size_low_latency(const struct audio_stream *stream) 1661 { 1662 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1663 1664 /* take resampling into account and return the closest majoring 1665 multiple of 16 frames, as audioflinger expects audio buffers to 1666 be a multiple of 16 frames. Note: we use the default rate here 1667 from pcm_config_tones.rate. */ 1668 size_t size = (SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) / pcm_config_tones.rate; 1669 size = ((size + 15) / 16) * 16; 1670 return size * audio_stream_frame_size((struct audio_stream *)stream); 1671 } 1672 1673 static size_t out_get_buffer_size_deep_buffer(const struct audio_stream *stream) 1674 { 1675 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1676 1677 /* take resampling into account and return the closest majoring 1678 multiple of 16 frames, as audioflinger expects audio buffers to 1679 be a multiple of 16 frames. Note: we use the default rate here 1680 from pcm_config_mm.rate. */ 1681 size_t size = (DEEP_BUFFER_SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) / 1682 pcm_config_mm.rate; 1683 size = ((size + 15) / 16) * 16; 1684 return size * audio_stream_frame_size((struct audio_stream *)stream); 1685 } 1686 1687 static size_t out_get_buffer_size_hdmi(const struct audio_stream *stream) 1688 { 1689 return HDMI_MULTI_PERIOD_SIZE * audio_stream_frame_size((struct audio_stream *)stream); 1690 } 1691 1692 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) 1693 { 1694 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1695 1696 return out->channel_mask; 1697 } 1698 1699 static audio_format_t out_get_format(const struct audio_stream *stream) 1700 { 1701 return AUDIO_FORMAT_PCM_16_BIT; 1702 } 1703 1704 static int out_set_format(struct audio_stream *stream, audio_format_t format) 1705 { 1706 return 0; 1707 } 1708 1709 /* must be called with hw device and output stream mutexes locked */ 1710 static int do_output_standby(struct tuna_stream_out *out) 1711 { 1712 struct tuna_audio_device *adev = out->dev; 1713 int i; 1714 bool all_outputs_in_standby = true; 1715 1716 if (!out->standby) { 1717 out->standby = 1; 1718 1719 for (i = 0; i < PCM_TOTAL; i++) { 1720 if (out->pcm[i]) { 1721 pcm_close(out->pcm[i]); 1722 out->pcm[i] = NULL; 1723 } 1724 } 1725 1726 for (i = 0; i < OUTPUT_TOTAL; i++) { 1727 if (adev->outputs[i] != NULL && !adev->outputs[i]->standby) { 1728 all_outputs_in_standby = false; 1729 break; 1730 } 1731 } 1732 /* if in call, don't turn off the output stage. This will 1733 be done when the call is ended */ 1734 if (all_outputs_in_standby && adev->mode != AUDIO_MODE_IN_CALL) { 1735 set_route_by_array(adev->mixer, hs_output, 0); 1736 set_route_by_array(adev->mixer, hf_output, 0); 1737 } 1738 1739 /* force standby on low latency output stream so that it can reuse HDMI driver if 1740 * necessary when restarted */ 1741 if (out == adev->outputs[OUTPUT_HDMI]) { 1742 if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL && 1743 !adev->outputs[OUTPUT_LOW_LATENCY]->standby) { 1744 struct tuna_stream_out *ll_out = adev->outputs[OUTPUT_LOW_LATENCY]; 1745 pthread_mutex_lock(&ll_out->lock); 1746 do_output_standby(ll_out); 1747 pthread_mutex_unlock(&ll_out->lock); 1748 } 1749 } 1750 1751 /* stop writing to echo reference */ 1752 if (out->echo_reference != NULL) { 1753 out->echo_reference->write(out->echo_reference, NULL); 1754 out->echo_reference = NULL; 1755 } 1756 } 1757 return 0; 1758 } 1759 1760 static int out_standby(struct audio_stream *stream) 1761 { 1762 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1763 int status; 1764 1765 pthread_mutex_lock(&out->dev->lock); 1766 pthread_mutex_lock(&out->lock); 1767 status = do_output_standby(out); 1768 pthread_mutex_unlock(&out->lock); 1769 pthread_mutex_unlock(&out->dev->lock); 1770 return status; 1771 } 1772 1773 static int out_dump(const struct audio_stream *stream, int fd) 1774 { 1775 return 0; 1776 } 1777 1778 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 1779 { 1780 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1781 struct tuna_audio_device *adev = out->dev; 1782 struct tuna_stream_in *in; 1783 struct str_parms *parms; 1784 char *str; 1785 char value[32]; 1786 int ret, val = 0; 1787 bool force_input_standby = false; 1788 1789 parms = str_parms_create_str(kvpairs); 1790 1791 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1792 if (ret >= 0) { 1793 val = atoi(value); 1794 pthread_mutex_lock(&adev->lock); 1795 pthread_mutex_lock(&out->lock); 1796 if ((adev->out_device != val) && (val != 0)) { 1797 /* this is needed only when changing device on low latency output 1798 * as other output streams are not used for voice use cases nor 1799 * handle duplication to HDMI or SPDIF */ 1800 if (out == adev->outputs[OUTPUT_LOW_LATENCY] && !out->standby) { 1801 /* a change in output device may change the microphone selection */ 1802 if (adev->active_input && 1803 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 1804 force_input_standby = true; 1805 } 1806 /* force standby if moving to/from HDMI/SPDIF or if the output 1807 * device changes when in HDMI/SPDIF mode */ 1808 /* FIXME also force standby when in call as some audio path switches do not work 1809 * while in call and an output stream is active (e.g BT SCO => earpiece) */ 1810 1811 /* FIXME workaround for audio being dropped when switching path without forcing standby 1812 * (several hundred ms of audio can be lost: e.g beginning of a ringtone. We must understand 1813 * the root cause in audio HAL, driver or ABE. 1814 if (((val & AUDIO_DEVICE_OUT_AUX_DIGITAL) ^ 1815 (adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) || 1816 ((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^ 1817 (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) || 1818 (adev->out_device & (AUDIO_DEVICE_OUT_AUX_DIGITAL | 1819 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET))) 1820 */ 1821 if (((val & AUDIO_DEVICE_OUT_AUX_DIGITAL) ^ 1822 (adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) || 1823 ((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^ 1824 (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) || 1825 (adev->out_device & (AUDIO_DEVICE_OUT_AUX_DIGITAL | 1826 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) || 1827 ((val & AUDIO_DEVICE_OUT_SPEAKER) ^ 1828 (adev->out_device & AUDIO_DEVICE_OUT_SPEAKER)) || 1829 (adev->mode == AUDIO_MODE_IN_CALL)) 1830 do_output_standby(out); 1831 } 1832 if (out != adev->outputs[OUTPUT_HDMI]) { 1833 adev->out_device = val; 1834 select_output_device(adev); 1835 } 1836 } 1837 pthread_mutex_unlock(&out->lock); 1838 if (force_input_standby) { 1839 in = adev->active_input; 1840 pthread_mutex_lock(&in->lock); 1841 do_input_standby(in); 1842 pthread_mutex_unlock(&in->lock); 1843 } 1844 pthread_mutex_unlock(&adev->lock); 1845 } 1846 1847 str_parms_destroy(parms); 1848 return ret; 1849 } 1850 1851 static char * out_get_parameters(const struct audio_stream *stream, const char *keys) 1852 { 1853 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1854 1855 struct str_parms *query = str_parms_create_str(keys); 1856 char *str; 1857 char value[256]; 1858 struct str_parms *reply = str_parms_create(); 1859 size_t i, j; 1860 int ret; 1861 bool first = true; 1862 1863 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); 1864 if (ret >= 0) { 1865 value[0] = '\0'; 1866 i = 0; 1867 while (out->sup_channel_masks[i] != 0) { 1868 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { 1869 if (out_channels_name_to_enum_table[j].value == out->sup_channel_masks[i]) { 1870 if (!first) { 1871 strcat(value, "|"); 1872 } 1873 strcat(value, out_channels_name_to_enum_table[j].name); 1874 first = false; 1875 break; 1876 } 1877 } 1878 i++; 1879 } 1880 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 1881 str = str_parms_to_str(reply); 1882 } else { 1883 str = strdup(keys); 1884 } 1885 str_parms_destroy(query); 1886 str_parms_destroy(reply); 1887 return str; 1888 } 1889 1890 static uint32_t out_get_latency_low_latency(const struct audio_stream_out *stream) 1891 { 1892 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1893 1894 /* Note: we use the default rate here from pcm_config_mm.rate */ 1895 return (SHORT_PERIOD_SIZE * PLAYBACK_SHORT_PERIOD_COUNT * 1000) / pcm_config_tones.rate; 1896 } 1897 1898 static uint32_t out_get_latency_deep_buffer(const struct audio_stream_out *stream) 1899 { 1900 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1901 1902 /* Note: we use the default rate here from pcm_config_mm.rate */ 1903 return (DEEP_BUFFER_LONG_PERIOD_SIZE * PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT * 1000) / 1904 pcm_config_mm.rate; 1905 } 1906 1907 static uint32_t out_get_latency_hdmi(const struct audio_stream_out *stream) 1908 { 1909 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1910 1911 return (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_PERIOD_COUNT * 1000) / out->config[PCM_HDMI].rate; 1912 } 1913 1914 static int out_set_volume(struct audio_stream_out *stream, float left, 1915 float right) 1916 { 1917 return -ENOSYS; 1918 } 1919 1920 static ssize_t out_write_low_latency(struct audio_stream_out *stream, const void* buffer, 1921 size_t bytes) 1922 { 1923 int ret; 1924 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1925 struct tuna_audio_device *adev = out->dev; 1926 size_t frame_size = audio_stream_frame_size(&out->stream.common); 1927 size_t in_frames = bytes / frame_size; 1928 size_t out_frames = in_frames; 1929 bool force_input_standby = false; 1930 struct tuna_stream_in *in; 1931 int i; 1932 1933 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 1934 * on the output stream mutex - e.g. executing select_mode() while holding the hw device 1935 * mutex 1936 */ 1937 pthread_mutex_lock(&adev->lock); 1938 pthread_mutex_lock(&out->lock); 1939 if (out->standby) { 1940 ret = start_output_stream_low_latency(out); 1941 if (ret != 0) { 1942 pthread_mutex_unlock(&adev->lock); 1943 goto exit; 1944 } 1945 out->standby = 0; 1946 /* a change in output device may change the microphone selection */ 1947 if (adev->active_input && 1948 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) 1949 force_input_standby = true; 1950 } 1951 pthread_mutex_unlock(&adev->lock); 1952 1953 for (i = 0; i < PCM_TOTAL; i++) { 1954 /* only use resampler if required */ 1955 if (out->pcm[i] && (out->config[i].rate != DEFAULT_OUT_SAMPLING_RATE)) { 1956 out_frames = out->buffer_frames; 1957 out->resampler->resample_from_input(out->resampler, 1958 (int16_t *)buffer, 1959 &in_frames, 1960 (int16_t *)out->buffer, 1961 &out_frames); 1962 break; 1963 } 1964 } 1965 1966 if (out->echo_reference != NULL) { 1967 struct echo_reference_buffer b; 1968 b.raw = (void *)buffer; 1969 b.frame_count = in_frames; 1970 1971 get_playback_delay(out, out_frames, &b); 1972 out->echo_reference->write(out->echo_reference, &b); 1973 } 1974 1975 /* Write to all active PCMs */ 1976 for (i = 0; i < PCM_TOTAL; i++) { 1977 if (out->pcm[i]) { 1978 if (out->config[i].rate == DEFAULT_OUT_SAMPLING_RATE) { 1979 /* PCM uses native sample rate */ 1980 ret = PCM_WRITE(out->pcm[i], (void *)buffer, bytes); 1981 } else { 1982 /* PCM needs resampler */ 1983 ret = PCM_WRITE(out->pcm[i], (void *)out->buffer, out_frames * frame_size); 1984 } 1985 if (ret) 1986 break; 1987 } 1988 } 1989 1990 exit: 1991 pthread_mutex_unlock(&out->lock); 1992 1993 if (ret != 0) { 1994 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / 1995 out_get_sample_rate(&stream->common)); 1996 } 1997 1998 if (force_input_standby) { 1999 pthread_mutex_lock(&adev->lock); 2000 if (adev->active_input) { 2001 in = adev->active_input; 2002 pthread_mutex_lock(&in->lock); 2003 do_input_standby(in); 2004 pthread_mutex_unlock(&in->lock); 2005 } 2006 pthread_mutex_unlock(&adev->lock); 2007 } 2008 2009 return bytes; 2010 } 2011 2012 static ssize_t out_write_deep_buffer(struct audio_stream_out *stream, const void* buffer, 2013 size_t bytes) 2014 { 2015 int ret; 2016 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 2017 struct tuna_audio_device *adev = out->dev; 2018 size_t frame_size = audio_stream_frame_size(&out->stream.common); 2019 size_t in_frames = bytes / frame_size; 2020 size_t out_frames; 2021 bool use_long_periods; 2022 int kernel_frames; 2023 void *buf; 2024 2025 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 2026 * on the output stream mutex - e.g. executing select_mode() while holding the hw device 2027 * mutex 2028 */ 2029 pthread_mutex_lock(&adev->lock); 2030 pthread_mutex_lock(&out->lock); 2031 if (out->standby) { 2032 ret = start_output_stream_deep_buffer(out); 2033 if (ret != 0) { 2034 pthread_mutex_unlock(&adev->lock); 2035 goto exit; 2036 } 2037 out->standby = 0; 2038 } 2039 use_long_periods = adev->screen_off && !adev->active_input; 2040 pthread_mutex_unlock(&adev->lock); 2041 2042 if (use_long_periods != out->use_long_periods) { 2043 size_t period_size; 2044 size_t period_count; 2045 2046 if (use_long_periods) { 2047 period_size = DEEP_BUFFER_LONG_PERIOD_SIZE; 2048 period_count = PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT; 2049 } else { 2050 period_size = DEEP_BUFFER_SHORT_PERIOD_SIZE; 2051 period_count = PLAYBACK_DEEP_BUFFER_SHORT_PERIOD_COUNT; 2052 } 2053 out->write_threshold = period_size * period_count; 2054 pcm_set_avail_min(out->pcm[PCM_NORMAL], period_size); 2055 out->use_long_periods = use_long_periods; 2056 } 2057 2058 /* only use resampler if required */ 2059 if (out->config[PCM_NORMAL].rate != DEFAULT_OUT_SAMPLING_RATE) { 2060 out_frames = out->buffer_frames; 2061 out->resampler->resample_from_input(out->resampler, 2062 (int16_t *)buffer, 2063 &in_frames, 2064 (int16_t *)out->buffer, 2065 &out_frames); 2066 buf = (void *)out->buffer; 2067 } else { 2068 out_frames = in_frames; 2069 buf = (void *)buffer; 2070 } 2071 2072 /* do not allow more than out->write_threshold frames in kernel pcm driver buffer */ 2073 do { 2074 struct timespec time_stamp; 2075 2076 if (pcm_get_htimestamp(out->pcm[PCM_NORMAL], 2077 (unsigned int *)&kernel_frames, &time_stamp) < 0) 2078 break; 2079 kernel_frames = pcm_get_buffer_size(out->pcm[PCM_NORMAL]) - kernel_frames; 2080 2081 if (kernel_frames > out->write_threshold) { 2082 unsigned long time = (unsigned long) 2083 (((int64_t)(kernel_frames - out->write_threshold) * 1000000) / 2084 MM_FULL_POWER_SAMPLING_RATE); 2085 if (time < MIN_WRITE_SLEEP_US) 2086 time = MIN_WRITE_SLEEP_US; 2087 usleep(time); 2088 } 2089 } while (kernel_frames > out->write_threshold); 2090 2091 ret = pcm_mmap_write(out->pcm[PCM_NORMAL], buf, out_frames * frame_size); 2092 2093 exit: 2094 pthread_mutex_unlock(&out->lock); 2095 2096 if (ret != 0) { 2097 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / 2098 out_get_sample_rate(&stream->common)); 2099 } 2100 2101 return bytes; 2102 } 2103 2104 static ssize_t out_write_hdmi(struct audio_stream_out *stream, const void* buffer, 2105 size_t bytes) 2106 { 2107 int ret; 2108 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 2109 struct tuna_audio_device *adev = out->dev; 2110 size_t frame_size = audio_stream_frame_size(&out->stream.common); 2111 size_t in_frames = bytes / frame_size; 2112 2113 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 2114 * on the output stream mutex - e.g. executing select_mode() while holding the hw device 2115 * mutex 2116 */ 2117 pthread_mutex_lock(&adev->lock); 2118 pthread_mutex_lock(&out->lock); 2119 if (out->standby) { 2120 ret = start_output_stream_hdmi(out); 2121 if (ret != 0) { 2122 pthread_mutex_unlock(&adev->lock); 2123 goto exit; 2124 } 2125 out->standby = 0; 2126 } 2127 pthread_mutex_unlock(&adev->lock); 2128 2129 ret = pcm_write(out->pcm[PCM_HDMI], 2130 buffer, 2131 pcm_frames_to_bytes(out->pcm[PCM_HDMI], in_frames)); 2132 2133 exit: 2134 pthread_mutex_unlock(&out->lock); 2135 2136 if (ret != 0) { 2137 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / 2138 out_get_sample_rate_hdmi(&stream->common)); 2139 } 2140 /* FIXME: workaround for HDMI multi channel channel swap on first playback after opening 2141 * the output stream: force reopening the pcm driver after writing a few periods. */ 2142 if ((out->restart_periods_cnt > 0) && 2143 (--out->restart_periods_cnt == 0)) 2144 out_standby(&stream->common); 2145 2146 return bytes; 2147 } 2148 2149 static int out_get_render_position(const struct audio_stream_out *stream, 2150 uint32_t *dsp_frames) 2151 { 2152 return -EINVAL; 2153 } 2154 2155 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 2156 { 2157 return 0; 2158 } 2159 2160 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 2161 { 2162 return 0; 2163 } 2164 2165 /** audio_stream_in implementation **/ 2166 2167 /* must be called with hw device and input stream mutexes locked */ 2168 static int start_input_stream(struct tuna_stream_in *in) 2169 { 2170 int ret = 0; 2171 struct tuna_audio_device *adev = in->dev; 2172 2173 adev->active_input = in; 2174 2175 if (adev->mode != AUDIO_MODE_IN_CALL) { 2176 adev->in_device = in->device; 2177 select_input_device(adev); 2178 } 2179 2180 if (in->aux_channels_changed) 2181 { 2182 in->aux_channels_changed = false; 2183 in->config.channels = popcount(in->main_channels | in->aux_channels); 2184 2185 if (in->resampler) { 2186 /* release and recreate the resampler with the new number of channel of the input */ 2187 release_resampler(in->resampler); 2188 in->resampler = NULL; 2189 ret = create_resampler(in->config.rate, 2190 in->requested_rate, 2191 in->config.channels, 2192 RESAMPLER_QUALITY_DEFAULT, 2193 &in->buf_provider, 2194 &in->resampler); 2195 } 2196 ALOGV("start_input_stream(): New channel configuration, " 2197 "main_channels = [%04x], aux_channels = [%04x], config.channels = %d", 2198 in->main_channels, in->aux_channels, in->config.channels); 2199 } 2200 2201 if (in->need_echo_reference && in->echo_reference == NULL) 2202 in->echo_reference = get_echo_reference(adev, 2203 AUDIO_FORMAT_PCM_16_BIT, 2204 popcount(in->main_channels), 2205 in->requested_rate); 2206 2207 /* this assumes routing is done previously */ 2208 in->pcm = pcm_open(0, PORT_MM2_UL, PCM_IN, &in->config); 2209 if (!pcm_is_ready(in->pcm)) { 2210 ALOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm)); 2211 pcm_close(in->pcm); 2212 adev->active_input = NULL; 2213 return -ENOMEM; 2214 } 2215 2216 /* force read and proc buf reallocation case of frame size or channel count change */ 2217 in->read_buf_frames = 0; 2218 in->read_buf_size = 0; 2219 in->proc_buf_frames = 0; 2220 in->proc_buf_size = 0; 2221 /* if no supported sample rate is available, use the resampler */ 2222 if (in->resampler) { 2223 in->resampler->reset(in->resampler); 2224 } 2225 return 0; 2226 } 2227 2228 static uint32_t in_get_sample_rate(const struct audio_stream *stream) 2229 { 2230 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2231 2232 return in->requested_rate; 2233 } 2234 2235 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 2236 { 2237 return 0; 2238 } 2239 2240 static size_t in_get_buffer_size(const struct audio_stream *stream) 2241 { 2242 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2243 2244 return get_input_buffer_size(in->requested_rate, 2245 AUDIO_FORMAT_PCM_16_BIT, 2246 popcount(in->main_channels)); 2247 } 2248 2249 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) 2250 { 2251 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2252 2253 return in->main_channels; 2254 } 2255 2256 static audio_format_t in_get_format(const struct audio_stream *stream) 2257 { 2258 return AUDIO_FORMAT_PCM_16_BIT; 2259 } 2260 2261 static int in_set_format(struct audio_stream *stream, audio_format_t format) 2262 { 2263 return 0; 2264 } 2265 2266 /* must be called with hw device and input stream mutexes locked */ 2267 static int do_input_standby(struct tuna_stream_in *in) 2268 { 2269 struct tuna_audio_device *adev = in->dev; 2270 2271 if (!in->standby) { 2272 pcm_close(in->pcm); 2273 in->pcm = NULL; 2274 2275 adev->active_input = 0; 2276 if (adev->mode != AUDIO_MODE_IN_CALL) { 2277 adev->in_device = AUDIO_DEVICE_NONE; 2278 select_input_device(adev); 2279 } 2280 2281 if (in->echo_reference != NULL) { 2282 /* stop reading from echo reference */ 2283 in->echo_reference->read(in->echo_reference, NULL); 2284 put_echo_reference(adev, in->echo_reference); 2285 in->echo_reference = NULL; 2286 } 2287 2288 in->standby = 1; 2289 } 2290 return 0; 2291 } 2292 2293 static int in_standby(struct audio_stream *stream) 2294 { 2295 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2296 int status; 2297 2298 pthread_mutex_lock(&in->dev->lock); 2299 pthread_mutex_lock(&in->lock); 2300 status = do_input_standby(in); 2301 pthread_mutex_unlock(&in->lock); 2302 pthread_mutex_unlock(&in->dev->lock); 2303 return status; 2304 } 2305 2306 static int in_dump(const struct audio_stream *stream, int fd) 2307 { 2308 return 0; 2309 } 2310 2311 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 2312 { 2313 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2314 struct tuna_audio_device *adev = in->dev; 2315 struct str_parms *parms; 2316 char *str; 2317 char value[32]; 2318 int ret, val = 0; 2319 bool do_standby = false; 2320 2321 parms = str_parms_create_str(kvpairs); 2322 2323 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 2324 2325 pthread_mutex_lock(&adev->lock); 2326 pthread_mutex_lock(&in->lock); 2327 if (ret >= 0) { 2328 val = atoi(value); 2329 /* no audio source uses val == 0 */ 2330 if ((in->source != val) && (val != 0)) { 2331 in->source = val; 2332 do_standby = true; 2333 } 2334 } 2335 2336 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 2337 if (ret >= 0) { 2338 val = atoi(value) & ~AUDIO_DEVICE_BIT_IN; 2339 if ((in->device != val) && (val != 0)) { 2340 in->device = val; 2341 do_standby = true; 2342 /* make sure new device selection is incompatible with multi-mic pre processing 2343 * configuration */ 2344 in_update_aux_channels(in, NULL); 2345 } 2346 } 2347 2348 if (do_standby) 2349 do_input_standby(in); 2350 pthread_mutex_unlock(&in->lock); 2351 pthread_mutex_unlock(&adev->lock); 2352 2353 str_parms_destroy(parms); 2354 return ret; 2355 } 2356 2357 static char * in_get_parameters(const struct audio_stream *stream, 2358 const char *keys) 2359 { 2360 return strdup(""); 2361 } 2362 2363 static int in_set_gain(struct audio_stream_in *stream, float gain) 2364 { 2365 return 0; 2366 } 2367 2368 static void get_capture_delay(struct tuna_stream_in *in, 2369 size_t frames, 2370 struct echo_reference_buffer *buffer) 2371 { 2372 2373 /* read frames available in kernel driver buffer */ 2374 size_t kernel_frames; 2375 struct timespec tstamp; 2376 long buf_delay; 2377 long rsmp_delay; 2378 long kernel_delay; 2379 long delay_ns; 2380 2381 if (pcm_get_htimestamp(in->pcm, &kernel_frames, &tstamp) < 0) { 2382 buffer->time_stamp.tv_sec = 0; 2383 buffer->time_stamp.tv_nsec = 0; 2384 buffer->delay_ns = 0; 2385 ALOGW("read get_capture_delay(): pcm_htimestamp error"); 2386 return; 2387 } 2388 2389 /* read frames available in audio HAL input buffer 2390 * add number of frames being read as we want the capture time of first sample 2391 * in current buffer */ 2392 /* frames in in->buffer are at driver sampling rate while frames in in->proc_buf are 2393 * at requested sampling rate */ 2394 buf_delay = (long)(((int64_t)(in->read_buf_frames) * 1000000000) / in->config.rate + 2395 ((int64_t)(in->proc_buf_frames) * 1000000000) / 2396 in->requested_rate); 2397 2398 /* add delay introduced by resampler */ 2399 rsmp_delay = 0; 2400 if (in->resampler) { 2401 rsmp_delay = in->resampler->delay_ns(in->resampler); 2402 } 2403 2404 kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / in->config.rate); 2405 2406 delay_ns = kernel_delay + buf_delay + rsmp_delay; 2407 2408 buffer->time_stamp = tstamp; 2409 buffer->delay_ns = delay_ns; 2410 ALOGV("get_capture_delay time_stamp = [%ld].[%ld], delay_ns: [%d]," 2411 " kernel_delay:[%ld], buf_delay:[%ld], rsmp_delay:[%ld], kernel_frames:[%d], " 2412 "in->read_buf_frames:[%d], in->proc_buf_frames:[%d], frames:[%d]", 2413 buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, buffer->delay_ns, 2414 kernel_delay, buf_delay, rsmp_delay, kernel_frames, 2415 in->read_buf_frames, in->proc_buf_frames, frames); 2416 2417 } 2418 2419 static int32_t update_echo_reference(struct tuna_stream_in *in, size_t frames) 2420 { 2421 struct echo_reference_buffer b; 2422 b.delay_ns = 0; 2423 2424 ALOGV("update_echo_reference, frames = [%d], in->ref_buf_frames = [%d], " 2425 "b.frame_count = [%d]", 2426 frames, in->ref_buf_frames, frames - in->ref_buf_frames); 2427 if (in->ref_buf_frames < frames) { 2428 if (in->ref_buf_size < frames) { 2429 in->ref_buf_size = frames; 2430 in->ref_buf = (int16_t *)realloc(in->ref_buf, pcm_frames_to_bytes(in->pcm, frames)); 2431 ALOG_ASSERT((in->ref_buf != NULL), 2432 "update_echo_reference() failed to reallocate ref_buf"); 2433 ALOGV("update_echo_reference(): ref_buf %p extended to %d bytes", 2434 in->ref_buf, pcm_frames_to_bytes(in->pcm, frames)); 2435 } 2436 b.frame_count = frames - in->ref_buf_frames; 2437 b.raw = (void *)(in->ref_buf + in->ref_buf_frames * in->config.channels); 2438 2439 get_capture_delay(in, frames, &b); 2440 2441 if (in->echo_reference->read(in->echo_reference, &b) == 0) 2442 { 2443 in->ref_buf_frames += b.frame_count; 2444 ALOGV("update_echo_reference(): in->ref_buf_frames:[%d], " 2445 "in->ref_buf_size:[%d], frames:[%d], b.frame_count:[%d]", 2446 in->ref_buf_frames, in->ref_buf_size, frames, b.frame_count); 2447 } 2448 } else 2449 ALOGW("update_echo_reference(): NOT enough frames to read ref buffer"); 2450 return b.delay_ns; 2451 } 2452 2453 static int set_preprocessor_param(effect_handle_t handle, 2454 effect_param_t *param) 2455 { 2456 uint32_t size = sizeof(int); 2457 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + 2458 param->vsize; 2459 2460 int status = (*handle)->command(handle, 2461 EFFECT_CMD_SET_PARAM, 2462 sizeof (effect_param_t) + psize, 2463 param, 2464 &size, 2465 ¶m->status); 2466 if (status == 0) 2467 status = param->status; 2468 2469 return status; 2470 } 2471 2472 static int set_preprocessor_echo_delay(effect_handle_t handle, 2473 int32_t delay_us) 2474 { 2475 uint32_t buf[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; 2476 effect_param_t *param = (effect_param_t *)buf; 2477 2478 param->psize = sizeof(uint32_t); 2479 param->vsize = sizeof(uint32_t); 2480 *(uint32_t *)param->data = AEC_PARAM_ECHO_DELAY; 2481 *((int32_t *)param->data + 1) = delay_us; 2482 2483 return set_preprocessor_param(handle, param); 2484 } 2485 2486 static void push_echo_reference(struct tuna_stream_in *in, size_t frames) 2487 { 2488 /* read frames from echo reference buffer and update echo delay 2489 * in->ref_buf_frames is updated with frames available in in->ref_buf */ 2490 int32_t delay_us = update_echo_reference(in, frames)/1000; 2491 int i; 2492 audio_buffer_t buf; 2493 2494 if (in->ref_buf_frames < frames) 2495 frames = in->ref_buf_frames; 2496 2497 buf.frameCount = frames; 2498 buf.raw = in->ref_buf; 2499 2500 for (i = 0; i < in->num_preprocessors; i++) { 2501 if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL) 2502 continue; 2503 2504 (*in->preprocessors[i].effect_itfe)->process_reverse(in->preprocessors[i].effect_itfe, 2505 &buf, 2506 NULL); 2507 set_preprocessor_echo_delay(in->preprocessors[i].effect_itfe, delay_us); 2508 } 2509 2510 in->ref_buf_frames -= buf.frameCount; 2511 if (in->ref_buf_frames) { 2512 memcpy(in->ref_buf, 2513 in->ref_buf + buf.frameCount * in->config.channels, 2514 in->ref_buf_frames * in->config.channels * sizeof(int16_t)); 2515 } 2516 } 2517 2518 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider, 2519 struct resampler_buffer* buffer) 2520 { 2521 struct tuna_stream_in *in; 2522 2523 if (buffer_provider == NULL || buffer == NULL) 2524 return -EINVAL; 2525 2526 in = (struct tuna_stream_in *)((char *)buffer_provider - 2527 offsetof(struct tuna_stream_in, buf_provider)); 2528 2529 if (in->pcm == NULL) { 2530 buffer->raw = NULL; 2531 buffer->frame_count = 0; 2532 in->read_status = -ENODEV; 2533 return -ENODEV; 2534 } 2535 2536 if (in->read_buf_frames == 0) { 2537 size_t size_in_bytes = pcm_frames_to_bytes(in->pcm, in->config.period_size); 2538 if (in->read_buf_size < in->config.period_size) { 2539 in->read_buf_size = in->config.period_size; 2540 in->read_buf = (int16_t *) realloc(in->read_buf, size_in_bytes); 2541 ALOG_ASSERT((in->read_buf != NULL), 2542 "get_next_buffer() failed to reallocate read_buf"); 2543 ALOGV("get_next_buffer(): read_buf %p extended to %d bytes", 2544 in->read_buf, size_in_bytes); 2545 } 2546 2547 in->read_status = pcm_read(in->pcm, (void*)in->read_buf, size_in_bytes); 2548 2549 if (in->read_status != 0) { 2550 ALOGE("get_next_buffer() pcm_read error %d", in->read_status); 2551 buffer->raw = NULL; 2552 buffer->frame_count = 0; 2553 return in->read_status; 2554 } 2555 in->read_buf_frames = in->config.period_size; 2556 } 2557 2558 buffer->frame_count = (buffer->frame_count > in->read_buf_frames) ? 2559 in->read_buf_frames : buffer->frame_count; 2560 buffer->i16 = in->read_buf + (in->config.period_size - in->read_buf_frames) * 2561 in->config.channels; 2562 2563 return in->read_status; 2564 2565 } 2566 2567 static void release_buffer(struct resampler_buffer_provider *buffer_provider, 2568 struct resampler_buffer* buffer) 2569 { 2570 struct tuna_stream_in *in; 2571 2572 if (buffer_provider == NULL || buffer == NULL) 2573 return; 2574 2575 in = (struct tuna_stream_in *)((char *)buffer_provider - 2576 offsetof(struct tuna_stream_in, buf_provider)); 2577 2578 in->read_buf_frames -= buffer->frame_count; 2579 } 2580 2581 /* read_frames() reads frames from kernel driver, down samples to capture rate 2582 * if necessary and output the number of frames requested to the buffer specified */ 2583 static ssize_t read_frames(struct tuna_stream_in *in, void *buffer, ssize_t frames) 2584 { 2585 ssize_t frames_wr = 0; 2586 2587 while (frames_wr < frames) { 2588 size_t frames_rd = frames - frames_wr; 2589 if (in->resampler != NULL) { 2590 in->resampler->resample_from_provider(in->resampler, 2591 (int16_t *)((char *)buffer + 2592 pcm_frames_to_bytes(in->pcm ,frames_wr)), 2593 &frames_rd); 2594 2595 } else { 2596 struct resampler_buffer buf = { 2597 { raw : NULL, }, 2598 frame_count : frames_rd, 2599 }; 2600 get_next_buffer(&in->buf_provider, &buf); 2601 if (buf.raw != NULL) { 2602 memcpy((char *)buffer + 2603 pcm_frames_to_bytes(in->pcm, frames_wr), 2604 buf.raw, 2605 pcm_frames_to_bytes(in->pcm, buf.frame_count)); 2606 frames_rd = buf.frame_count; 2607 } 2608 release_buffer(&in->buf_provider, &buf); 2609 } 2610 /* in->read_status is updated by getNextBuffer() also called by 2611 * in->resampler->resample_from_provider() */ 2612 if (in->read_status != 0) 2613 return in->read_status; 2614 2615 frames_wr += frames_rd; 2616 } 2617 return frames_wr; 2618 } 2619 2620 /* process_frames() reads frames from kernel driver (via read_frames()), 2621 * calls the active audio pre processings and output the number of frames requested 2622 * to the buffer specified */ 2623 static ssize_t process_frames(struct tuna_stream_in *in, void* buffer, ssize_t frames) 2624 { 2625 ssize_t frames_wr = 0; 2626 audio_buffer_t in_buf; 2627 audio_buffer_t out_buf; 2628 int i; 2629 bool has_aux_channels = (~in->main_channels & in->aux_channels); 2630 void *proc_buf_out; 2631 2632 if (has_aux_channels) 2633 proc_buf_out = in->proc_buf_out; 2634 else 2635 proc_buf_out = buffer; 2636 2637 /* since all the processing below is done in frames and using the config.channels 2638 * as the number of channels, no changes is required in case aux_channels are present */ 2639 while (frames_wr < frames) { 2640 /* first reload enough frames at the end of process input buffer */ 2641 if (in->proc_buf_frames < (size_t)frames) { 2642 ssize_t frames_rd; 2643 2644 if (in->proc_buf_size < (size_t)frames) { 2645 size_t size_in_bytes = pcm_frames_to_bytes(in->pcm, frames); 2646 2647 in->proc_buf_size = (size_t)frames; 2648 in->proc_buf_in = (int16_t *)realloc(in->proc_buf_in, size_in_bytes); 2649 ALOG_ASSERT((in->proc_buf_in != NULL), 2650 "process_frames() failed to reallocate proc_buf_in"); 2651 if (has_aux_channels) { 2652 in->proc_buf_out = (int16_t *)realloc(in->proc_buf_out, size_in_bytes); 2653 ALOG_ASSERT((in->proc_buf_out != NULL), 2654 "process_frames() failed to reallocate proc_buf_out"); 2655 proc_buf_out = in->proc_buf_out; 2656 } 2657 ALOGV("process_frames(): proc_buf_in %p extended to %d bytes", 2658 in->proc_buf_in, size_in_bytes); 2659 } 2660 frames_rd = read_frames(in, 2661 in->proc_buf_in + 2662 in->proc_buf_frames * in->config.channels, 2663 frames - in->proc_buf_frames); 2664 if (frames_rd < 0) { 2665 frames_wr = frames_rd; 2666 break; 2667 } 2668 in->proc_buf_frames += frames_rd; 2669 } 2670 2671 if (in->echo_reference != NULL) 2672 push_echo_reference(in, in->proc_buf_frames); 2673 2674 /* in_buf.frameCount and out_buf.frameCount indicate respectively 2675 * the maximum number of frames to be consumed and produced by process() */ 2676 in_buf.frameCount = in->proc_buf_frames; 2677 in_buf.s16 = in->proc_buf_in; 2678 out_buf.frameCount = frames - frames_wr; 2679 out_buf.s16 = (int16_t *)proc_buf_out + frames_wr * in->config.channels; 2680 2681 /* FIXME: this works because of current pre processing library implementation that 2682 * does the actual process only when the last enabled effect process is called. 2683 * The generic solution is to have an output buffer for each effect and pass it as 2684 * input to the next. 2685 */ 2686 for (i = 0; i < in->num_preprocessors; i++) { 2687 (*in->preprocessors[i].effect_itfe)->process(in->preprocessors[i].effect_itfe, 2688 &in_buf, 2689 &out_buf); 2690 } 2691 2692 /* process() has updated the number of frames consumed and produced in 2693 * in_buf.frameCount and out_buf.frameCount respectively 2694 * move remaining frames to the beginning of in->proc_buf_in */ 2695 in->proc_buf_frames -= in_buf.frameCount; 2696 2697 if (in->proc_buf_frames) { 2698 memcpy(in->proc_buf_in, 2699 in->proc_buf_in + in_buf.frameCount * in->config.channels, 2700 in->proc_buf_frames * in->config.channels * sizeof(int16_t)); 2701 } 2702 2703 /* if not enough frames were passed to process(), read more and retry. */ 2704 if (out_buf.frameCount == 0) { 2705 ALOGW("No frames produced by preproc"); 2706 continue; 2707 } 2708 2709 if ((frames_wr + (ssize_t)out_buf.frameCount) <= frames) { 2710 frames_wr += out_buf.frameCount; 2711 } else { 2712 /* The effect does not comply to the API. In theory, we should never end up here! */ 2713 ALOGE("preprocessing produced too many frames: %d + %d > %d !", 2714 (unsigned int)frames_wr, out_buf.frameCount, (unsigned int)frames); 2715 frames_wr = frames; 2716 } 2717 } 2718 2719 /* Remove aux_channels that have been added on top of main_channels 2720 * Assumption is made that the channels are interleaved and that the main 2721 * channels are first. */ 2722 if (has_aux_channels) 2723 { 2724 size_t src_channels = in->config.channels; 2725 size_t dst_channels = popcount(in->main_channels); 2726 int16_t* src_buffer = (int16_t *)proc_buf_out; 2727 int16_t* dst_buffer = (int16_t *)buffer; 2728 2729 if (dst_channels == 1) { 2730 for (i = frames_wr; i > 0; i--) 2731 { 2732 *dst_buffer++ = *src_buffer; 2733 src_buffer += src_channels; 2734 } 2735 } else { 2736 for (i = frames_wr; i > 0; i--) 2737 { 2738 memcpy(dst_buffer, src_buffer, dst_channels*sizeof(int16_t)); 2739 dst_buffer += dst_channels; 2740 src_buffer += src_channels; 2741 } 2742 } 2743 } 2744 2745 return frames_wr; 2746 } 2747 2748 static ssize_t in_read(struct audio_stream_in *stream, void* buffer, 2749 2750 size_t bytes) 2751 { 2752 int ret = 0; 2753 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2754 struct tuna_audio_device *adev = in->dev; 2755 size_t frames_rq = bytes / audio_stream_frame_size(&stream->common); 2756 2757 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 2758 * on the input stream mutex - e.g. executing select_mode() while holding the hw device 2759 * mutex 2760 */ 2761 pthread_mutex_lock(&adev->lock); 2762 pthread_mutex_lock(&in->lock); 2763 if (in->standby) { 2764 ret = start_input_stream(in); 2765 if (ret == 0) 2766 in->standby = 0; 2767 } 2768 pthread_mutex_unlock(&adev->lock); 2769 2770 if (ret < 0) 2771 goto exit; 2772 2773 if (in->num_preprocessors != 0) 2774 ret = process_frames(in, buffer, frames_rq); 2775 else if (in->resampler != NULL) 2776 ret = read_frames(in, buffer, frames_rq); 2777 else 2778 ret = pcm_read(in->pcm, buffer, bytes); 2779 2780 if (ret > 0) 2781 ret = 0; 2782 2783 if (ret == 0 && adev->mic_mute) 2784 memset(buffer, 0, bytes); 2785 2786 exit: 2787 if (ret < 0) 2788 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / 2789 in_get_sample_rate(&stream->common)); 2790 2791 pthread_mutex_unlock(&in->lock); 2792 return bytes; 2793 } 2794 2795 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 2796 { 2797 return 0; 2798 } 2799 2800 #define GET_COMMAND_STATUS(status, fct_status, cmd_status) \ 2801 do { \ 2802 if (fct_status != 0) \ 2803 status = fct_status; \ 2804 else if (cmd_status != 0) \ 2805 status = cmd_status; \ 2806 } while(0) 2807 2808 static int in_configure_reverse(struct tuna_stream_in *in) 2809 { 2810 int32_t cmd_status; 2811 uint32_t size = sizeof(int); 2812 effect_config_t config; 2813 int32_t status = 0; 2814 int32_t fct_status = 0; 2815 int i; 2816 2817 if (in->num_preprocessors > 0) { 2818 config.inputCfg.channels = in->main_channels; 2819 config.outputCfg.channels = in->main_channels; 2820 config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 2821 config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 2822 config.inputCfg.samplingRate = in->requested_rate; 2823 config.outputCfg.samplingRate = in->requested_rate; 2824 config.inputCfg.mask = 2825 ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT ); 2826 config.outputCfg.mask = 2827 ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT ); 2828 2829 for (i = 0; i < in->num_preprocessors; i++) 2830 { 2831 if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL) 2832 continue; 2833 fct_status = (*(in->preprocessors[i].effect_itfe))->command( 2834 in->preprocessors[i].effect_itfe, 2835 EFFECT_CMD_SET_CONFIG_REVERSE, 2836 sizeof(effect_config_t), 2837 &config, 2838 &size, 2839 &cmd_status); 2840 GET_COMMAND_STATUS(status, fct_status, cmd_status); 2841 } 2842 } 2843 return status; 2844 } 2845 2846 #define MAX_NUM_CHANNEL_CONFIGS 10 2847 2848 static void in_read_audio_effect_channel_configs(struct tuna_stream_in *in, 2849 struct effect_info_s *effect_info) 2850 { 2851 /* size and format of the cmd are defined in hardware/audio_effect.h */ 2852 effect_handle_t effect = effect_info->effect_itfe; 2853 uint32_t cmd_size = 2 * sizeof(uint32_t); 2854 uint32_t cmd[] = { EFFECT_FEATURE_AUX_CHANNELS, MAX_NUM_CHANNEL_CONFIGS }; 2855 /* reply = status + number of configs (n) + n x channel_config_t */ 2856 uint32_t reply_size = 2857 2 * sizeof(uint32_t) + (MAX_NUM_CHANNEL_CONFIGS * sizeof(channel_config_t)); 2858 int32_t reply[reply_size]; 2859 int32_t cmd_status; 2860 2861 ALOG_ASSERT((effect_info->num_channel_configs == 0), 2862 "in_read_audio_effect_channel_configs() num_channel_configs not cleared"); 2863 ALOG_ASSERT((effect_info->channel_configs == NULL), 2864 "in_read_audio_effect_channel_configs() channel_configs not cleared"); 2865 2866 /* if this command is not supported, then the effect is supposed to return -EINVAL. 2867 * This error will be interpreted as if the effect supports the main_channels but does not 2868 * support any aux_channels */ 2869 cmd_status = (*effect)->command(effect, 2870 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, 2871 cmd_size, 2872 (void*)&cmd, 2873 &reply_size, 2874 (void*)&reply); 2875 2876 if (cmd_status != 0) { 2877 ALOGV("in_read_audio_effect_channel_configs(): " 2878 "fx->command returned %d", cmd_status); 2879 return; 2880 } 2881 2882 if (reply[0] != 0) { 2883 ALOGW("in_read_audio_effect_channel_configs(): " 2884 "command EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS error %d num configs %d", 2885 reply[0], (reply[0] == -ENOMEM) ? reply[1] : MAX_NUM_CHANNEL_CONFIGS); 2886 return; 2887 } 2888 2889 /* the feature is not supported */ 2890 ALOGV("in_read_audio_effect_channel_configs()(): " 2891 "Feature supported and adding %d channel configs to the list", reply[1]); 2892 effect_info->num_channel_configs = reply[1]; 2893 effect_info->channel_configs = 2894 (channel_config_t *) malloc(sizeof(channel_config_t) * reply[1]); /* n x configs */ 2895 memcpy(effect_info->channel_configs, (reply + 2), sizeof(channel_config_t) * reply[1]); 2896 } 2897 2898 2899 static uint32_t in_get_aux_channels(struct tuna_stream_in *in) 2900 { 2901 int i; 2902 channel_config_t new_chcfg = {0, 0}; 2903 2904 if (in->num_preprocessors == 0) 2905 return 0; 2906 2907 /* do not enable dual mic configurations when capturing from other microphones than 2908 * main or sub */ 2909 if (!(in->device & (AUDIO_DEVICE_IN_BUILTIN_MIC | AUDIO_DEVICE_IN_BACK_MIC))) 2910 return 0; 2911 2912 /* retain most complex aux channels configuration compatible with requested main channels and 2913 * supported by audio driver and all pre processors */ 2914 for (i = 0; i < NUM_IN_AUX_CNL_CONFIGS; i++) { 2915 channel_config_t *cur_chcfg = &in_aux_cnl_configs[i]; 2916 if (cur_chcfg->main_channels == in->main_channels) { 2917 size_t match_cnt; 2918 size_t idx_preproc; 2919 for (idx_preproc = 0, match_cnt = 0; 2920 /* no need to continue if at least one preprocessor doesn't match */ 2921 idx_preproc < (size_t)in->num_preprocessors && match_cnt == idx_preproc; 2922 idx_preproc++) { 2923 struct effect_info_s *effect_info = &in->preprocessors[idx_preproc]; 2924 size_t idx_chcfg; 2925 2926 for (idx_chcfg = 0; idx_chcfg < effect_info->num_channel_configs; idx_chcfg++) { 2927 if (memcmp(effect_info->channel_configs + idx_chcfg, 2928 cur_chcfg, 2929 sizeof(channel_config_t)) == 0) { 2930 match_cnt++; 2931 break; 2932 } 2933 } 2934 } 2935 /* if all preprocessors match, we have a candidate */ 2936 if (match_cnt == (size_t)in->num_preprocessors) { 2937 /* retain most complex aux channels configuration */ 2938 if (popcount(cur_chcfg->aux_channels) > popcount(new_chcfg.aux_channels)) { 2939 new_chcfg = *cur_chcfg; 2940 } 2941 } 2942 } 2943 } 2944 2945 ALOGV("in_get_aux_channels(): return %04x", new_chcfg.aux_channels); 2946 2947 return new_chcfg.aux_channels; 2948 } 2949 2950 static int in_configure_effect_channels(effect_handle_t effect, 2951 channel_config_t *channel_config) 2952 { 2953 int status = 0; 2954 int fct_status; 2955 int32_t cmd_status; 2956 uint32_t reply_size; 2957 effect_config_t config; 2958 uint32_t cmd[(sizeof(uint32_t) + sizeof(channel_config_t) - 1) / sizeof(uint32_t) + 1]; 2959 2960 ALOGV("in_configure_effect_channels(): configure effect with channels: [%04x][%04x]", 2961 channel_config->main_channels, 2962 channel_config->aux_channels); 2963 2964 config.inputCfg.mask = EFFECT_CONFIG_CHANNELS; 2965 config.outputCfg.mask = EFFECT_CONFIG_CHANNELS; 2966 reply_size = sizeof(effect_config_t); 2967 fct_status = (*effect)->command(effect, 2968 EFFECT_CMD_GET_CONFIG, 2969 0, 2970 NULL, 2971 &reply_size, 2972 &config); 2973 if (fct_status != 0) { 2974 ALOGE("in_configure_effect_channels(): EFFECT_CMD_GET_CONFIG failed"); 2975 return fct_status; 2976 } 2977 2978 config.inputCfg.channels = channel_config->main_channels | channel_config->aux_channels; 2979 config.outputCfg.channels = config.inputCfg.channels; 2980 reply_size = sizeof(uint32_t); 2981 fct_status = (*effect)->command(effect, 2982 EFFECT_CMD_SET_CONFIG, 2983 sizeof(effect_config_t), 2984 &config, 2985 &reply_size, 2986 &cmd_status); 2987 GET_COMMAND_STATUS(status, fct_status, cmd_status); 2988 2989 cmd[0] = EFFECT_FEATURE_AUX_CHANNELS; 2990 memcpy(cmd + 1, channel_config, sizeof(channel_config_t)); 2991 reply_size = sizeof(uint32_t); 2992 fct_status = (*effect)->command(effect, 2993 EFFECT_CMD_SET_FEATURE_CONFIG, 2994 sizeof(cmd), //sizeof(uint32_t) + sizeof(channel_config_t), 2995 cmd, 2996 &reply_size, 2997 &cmd_status); 2998 GET_COMMAND_STATUS(status, fct_status, cmd_status); 2999 3000 /* some implementations need to be re-enabled after a config change */ 3001 reply_size = sizeof(uint32_t); 3002 fct_status = (*effect)->command(effect, 3003 EFFECT_CMD_ENABLE, 3004 0, 3005 NULL, 3006 &reply_size, 3007 &cmd_status); 3008 GET_COMMAND_STATUS(status, fct_status, cmd_status); 3009 3010 return status; 3011 } 3012 3013 static int in_reconfigure_channels(struct tuna_stream_in *in, 3014 effect_handle_t effect, 3015 channel_config_t *channel_config, 3016 bool config_changed) { 3017 3018 int status = 0; 3019 3020 ALOGV("in_reconfigure_channels(): config_changed %d effect %p", 3021 config_changed, effect); 3022 3023 /* if config changed, reconfigure all previously added effects */ 3024 if (config_changed) { 3025 int i; 3026 for (i = 0; i < in->num_preprocessors; i++) 3027 { 3028 int cur_status = in_configure_effect_channels(in->preprocessors[i].effect_itfe, 3029 channel_config); 3030 if (cur_status != 0) { 3031 ALOGV("in_reconfigure_channels(): error %d configuring effect " 3032 "%d with channels: [%04x][%04x]", 3033 cur_status, 3034 i, 3035 channel_config->main_channels, 3036 channel_config->aux_channels); 3037 status = cur_status; 3038 } 3039 } 3040 } else if (effect != NULL && channel_config->aux_channels) { 3041 /* if aux channels config did not change but aux channels are present, 3042 * we still need to configure the effect being added */ 3043 status = in_configure_effect_channels(effect, channel_config); 3044 } 3045 return status; 3046 } 3047 3048 static void in_update_aux_channels(struct tuna_stream_in *in, 3049 effect_handle_t effect) 3050 { 3051 uint32_t aux_channels; 3052 channel_config_t channel_config; 3053 int status; 3054 3055 aux_channels = in_get_aux_channels(in); 3056 3057 channel_config.main_channels = in->main_channels; 3058 channel_config.aux_channels = aux_channels; 3059 status = in_reconfigure_channels(in, 3060 effect, 3061 &channel_config, 3062 (aux_channels != in->aux_channels)); 3063 3064 if (status != 0) { 3065 ALOGV("in_update_aux_channels(): in_reconfigure_channels error %d", status); 3066 /* resetting aux channels configuration */ 3067 aux_channels = 0; 3068 channel_config.aux_channels = 0; 3069 in_reconfigure_channels(in, effect, &channel_config, true); 3070 } 3071 if (in->aux_channels != aux_channels) { 3072 in->aux_channels_changed = true; 3073 in->aux_channels = aux_channels; 3074 do_input_standby(in); 3075 } 3076 } 3077 3078 static int in_add_audio_effect(const struct audio_stream *stream, 3079 effect_handle_t effect) 3080 { 3081 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 3082 int status; 3083 effect_descriptor_t desc; 3084 3085 pthread_mutex_lock(&in->dev->lock); 3086 pthread_mutex_lock(&in->lock); 3087 if (in->num_preprocessors >= MAX_PREPROCESSORS) { 3088 status = -ENOSYS; 3089 goto exit; 3090 } 3091 3092 status = (*effect)->get_descriptor(effect, &desc); 3093 if (status != 0) 3094 goto exit; 3095 3096 in->preprocessors[in->num_preprocessors].effect_itfe = effect; 3097 /* add the supported channel of the effect in the channel_configs */ 3098 in_read_audio_effect_channel_configs(in, &in->preprocessors[in->num_preprocessors]); 3099 3100 in->num_preprocessors++; 3101 3102 /* check compatibility between main channel supported and possible auxiliary channels */ 3103 in_update_aux_channels(in, effect); 3104 3105 ALOGV("in_add_audio_effect(), effect type: %08x", desc.type.timeLow); 3106 3107 if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) { 3108 in->need_echo_reference = true; 3109 do_input_standby(in); 3110 in_configure_reverse(in); 3111 } 3112 3113 exit: 3114 3115 ALOGW_IF(status != 0, "in_add_audio_effect() error %d", status); 3116 pthread_mutex_unlock(&in->lock); 3117 pthread_mutex_unlock(&in->dev->lock); 3118 return status; 3119 } 3120 3121 static int in_remove_audio_effect(const struct audio_stream *stream, 3122 effect_handle_t effect) 3123 { 3124 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 3125 int i; 3126 int status = -EINVAL; 3127 effect_descriptor_t desc; 3128 3129 pthread_mutex_lock(&in->dev->lock); 3130 pthread_mutex_lock(&in->lock); 3131 if (in->num_preprocessors <= 0) { 3132 status = -ENOSYS; 3133 goto exit; 3134 } 3135 3136 for (i = 0; i < in->num_preprocessors; i++) { 3137 if (status == 0) { /* status == 0 means an effect was removed from a previous slot */ 3138 in->preprocessors[i - 1].effect_itfe = in->preprocessors[i].effect_itfe; 3139 in->preprocessors[i - 1].channel_configs = in->preprocessors[i].channel_configs; 3140 in->preprocessors[i - 1].num_channel_configs = in->preprocessors[i].num_channel_configs; 3141 ALOGV("in_remove_audio_effect moving fx from %d to %d", i, i - 1); 3142 continue; 3143 } 3144 if (in->preprocessors[i].effect_itfe == effect) { 3145 ALOGV("in_remove_audio_effect found fx at index %d", i); 3146 free(in->preprocessors[i].channel_configs); 3147 status = 0; 3148 } 3149 } 3150 3151 if (status != 0) 3152 goto exit; 3153 3154 in->num_preprocessors--; 3155 /* if we remove one effect, at least the last preproc should be reset */ 3156 in->preprocessors[in->num_preprocessors].num_channel_configs = 0; 3157 in->preprocessors[in->num_preprocessors].effect_itfe = NULL; 3158 in->preprocessors[in->num_preprocessors].channel_configs = NULL; 3159 3160 3161 /* check compatibility between main channel supported and possible auxiliary channels */ 3162 in_update_aux_channels(in, NULL); 3163 3164 status = (*effect)->get_descriptor(effect, &desc); 3165 if (status != 0) 3166 goto exit; 3167 3168 ALOGV("in_remove_audio_effect(), effect type: %08x", desc.type.timeLow); 3169 3170 if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) { 3171 in->need_echo_reference = false; 3172 do_input_standby(in); 3173 } 3174 3175 exit: 3176 3177 ALOGW_IF(status != 0, "in_remove_audio_effect() error %d", status); 3178 pthread_mutex_unlock(&in->lock); 3179 pthread_mutex_unlock(&in->dev->lock); 3180 return status; 3181 } 3182 3183 static int out_read_hdmi_channel_masks(struct tuna_stream_out *out) { 3184 int max_channels = 0; 3185 struct mixer *mixer_hdmi; 3186 3187 mixer_hdmi = mixer_open(CARD_OMAP4_HDMI); 3188 if (mixer_hdmi) { 3189 struct mixer_ctl *ctl; 3190 3191 ctl = mixer_get_ctl_by_name(mixer_hdmi, MIXER_MAXIMUM_LPCM_CHANNELS); 3192 if (ctl) 3193 max_channels = mixer_ctl_get_value(ctl, 0); 3194 mixer_close(mixer_hdmi); 3195 } 3196 3197 ALOGV("out_read_hdmi_channel_masks() got %d max channels", max_channels); 3198 3199 if (max_channels != 6 && max_channels != 8) 3200 return -ENOSYS; 3201 3202 out->sup_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 3203 if (max_channels == 8) 3204 out->sup_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; 3205 3206 return 0; 3207 } 3208 3209 static int adev_open_output_stream(struct audio_hw_device *dev, 3210 audio_io_handle_t handle, 3211 audio_devices_t devices, 3212 audio_output_flags_t flags, 3213 struct audio_config *config, 3214 struct audio_stream_out **stream_out) 3215 { 3216 struct tuna_audio_device *ladev = (struct tuna_audio_device *)dev; 3217 struct tuna_stream_out *out; 3218 int ret; 3219 int output_type; 3220 3221 *stream_out = NULL; 3222 3223 out = (struct tuna_stream_out *)calloc(1, sizeof(struct tuna_stream_out)); 3224 if (!out) 3225 return -ENOMEM; 3226 3227 out->sup_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; 3228 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 3229 3230 if (flags & AUDIO_OUTPUT_FLAG_DIRECT && 3231 devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) { 3232 ALOGV("adev_open_output_stream() HDMI multichannel"); 3233 if (ladev->outputs[OUTPUT_HDMI] != NULL) { 3234 ret = -ENOSYS; 3235 goto err_open; 3236 } 3237 ret = out_read_hdmi_channel_masks(out); 3238 if (ret != 0) 3239 goto err_open; 3240 output_type = OUTPUT_HDMI; 3241 if (config->sample_rate == 0) 3242 config->sample_rate = MM_FULL_POWER_SAMPLING_RATE; 3243 if (config->channel_mask == 0) 3244 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; 3245 out->channel_mask = config->channel_mask; 3246 out->stream.common.get_buffer_size = out_get_buffer_size_hdmi; 3247 out->stream.common.get_sample_rate = out_get_sample_rate_hdmi; 3248 out->stream.get_latency = out_get_latency_hdmi; 3249 out->stream.write = out_write_hdmi; 3250 out->config[PCM_HDMI] = pcm_config_hdmi_multi; 3251 out->config[PCM_HDMI].rate = config->sample_rate; 3252 out->config[PCM_HDMI].channels = popcount(config->channel_mask); 3253 /* FIXME: workaround for channel swap on first playback after opening the output */ 3254 out->restart_periods_cnt = out->config[PCM_HDMI].period_count * 2; 3255 } else if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 3256 ALOGV("adev_open_output_stream() deep buffer"); 3257 if (ladev->outputs[OUTPUT_DEEP_BUF] != NULL) { 3258 ret = -ENOSYS; 3259 goto err_open; 3260 } 3261 output_type = OUTPUT_DEEP_BUF; 3262 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 3263 out->stream.common.get_buffer_size = out_get_buffer_size_deep_buffer; 3264 out->stream.common.get_sample_rate = out_get_sample_rate; 3265 out->stream.get_latency = out_get_latency_deep_buffer; 3266 out->stream.write = out_write_deep_buffer; 3267 } else { 3268 ALOGV("adev_open_output_stream() normal buffer"); 3269 if (ladev->outputs[OUTPUT_LOW_LATENCY] != NULL) { 3270 ret = -ENOSYS; 3271 goto err_open; 3272 } 3273 output_type = OUTPUT_LOW_LATENCY; 3274 out->stream.common.get_buffer_size = out_get_buffer_size_low_latency; 3275 out->stream.common.get_sample_rate = out_get_sample_rate; 3276 out->stream.get_latency = out_get_latency_low_latency; 3277 out->stream.write = out_write_low_latency; 3278 } 3279 3280 ret = create_resampler(DEFAULT_OUT_SAMPLING_RATE, 3281 MM_FULL_POWER_SAMPLING_RATE, 3282 2, 3283 RESAMPLER_QUALITY_DEFAULT, 3284 NULL, 3285 &out->resampler); 3286 if (ret != 0) 3287 goto err_open; 3288 3289 out->stream.common.set_sample_rate = out_set_sample_rate; 3290 out->stream.common.get_channels = out_get_channels; 3291 out->stream.common.get_format = out_get_format; 3292 out->stream.common.set_format = out_set_format; 3293 out->stream.common.standby = out_standby; 3294 out->stream.common.dump = out_dump; 3295 out->stream.common.set_parameters = out_set_parameters; 3296 out->stream.common.get_parameters = out_get_parameters; 3297 out->stream.common.add_audio_effect = out_add_audio_effect; 3298 out->stream.common.remove_audio_effect = out_remove_audio_effect; 3299 out->stream.set_volume = out_set_volume; 3300 out->stream.get_render_position = out_get_render_position; 3301 3302 out->dev = ladev; 3303 out->standby = 1; 3304 3305 /* FIXME: when we support multiple output devices, we will want to 3306 * do the following: 3307 * adev->out_device = out->device; 3308 * select_output_device(adev); 3309 * This is because out_set_parameters() with a route is not 3310 * guaranteed to be called after an output stream is opened. */ 3311 3312 config->format = out->stream.common.get_format(&out->stream.common); 3313 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 3314 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 3315 3316 *stream_out = &out->stream; 3317 ladev->outputs[output_type] = out; 3318 3319 return 0; 3320 3321 err_open: 3322 free(out); 3323 return ret; 3324 } 3325 3326 static void adev_close_output_stream(struct audio_hw_device *dev, 3327 struct audio_stream_out *stream) 3328 { 3329 struct tuna_audio_device *ladev = (struct tuna_audio_device *)dev; 3330 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 3331 int i; 3332 3333 out_standby(&stream->common); 3334 for (i = 0; i < OUTPUT_TOTAL; i++) { 3335 if (ladev->outputs[i] == out) { 3336 ladev->outputs[i] = NULL; 3337 break; 3338 } 3339 } 3340 3341 if (out->buffer) 3342 free(out->buffer); 3343 if (out->resampler) 3344 release_resampler(out->resampler); 3345 free(stream); 3346 } 3347 3348 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 3349 { 3350 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 3351 struct str_parms *parms; 3352 char *str; 3353 char value[32]; 3354 int ret; 3355 3356 parms = str_parms_create_str(kvpairs); 3357 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 3358 if (ret >= 0) { 3359 int tty_mode; 3360 3361 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 3362 tty_mode = TTY_MODE_OFF; 3363 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 3364 tty_mode = TTY_MODE_VCO; 3365 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 3366 tty_mode = TTY_MODE_HCO; 3367 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 3368 tty_mode = TTY_MODE_FULL; 3369 else 3370 return -EINVAL; 3371 3372 pthread_mutex_lock(&adev->lock); 3373 if (tty_mode != adev->tty_mode) { 3374 adev->tty_mode = tty_mode; 3375 if (adev->mode == AUDIO_MODE_IN_CALL) 3376 select_output_device(adev); 3377 } 3378 pthread_mutex_unlock(&adev->lock); 3379 } 3380 3381 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 3382 if (ret >= 0) { 3383 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 3384 adev->bluetooth_nrec = true; 3385 else 3386 adev->bluetooth_nrec = false; 3387 } 3388 3389 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 3390 if (ret >= 0) { 3391 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 3392 adev->screen_off = false; 3393 else 3394 adev->screen_off = true; 3395 } 3396 3397 str_parms_destroy(parms); 3398 return ret; 3399 } 3400 3401 static char * adev_get_parameters(const struct audio_hw_device *dev, 3402 const char *keys) 3403 { 3404 return strdup(""); 3405 } 3406 3407 static int adev_init_check(const struct audio_hw_device *dev) 3408 { 3409 return 0; 3410 } 3411 3412 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 3413 { 3414 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 3415 3416 pthread_mutex_lock(&adev->lock); 3417 adev->voice_volume = volume; 3418 3419 if (adev->mode == AUDIO_MODE_IN_CALL) 3420 ril_set_call_volume(&adev->ril, SOUND_TYPE_VOICE, volume); 3421 3422 pthread_mutex_unlock(&adev->lock); 3423 return 0; 3424 } 3425 3426 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 3427 { 3428 return -ENOSYS; 3429 } 3430 3431 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 3432 { 3433 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 3434 3435 pthread_mutex_lock(&adev->lock); 3436 if (adev->mode != mode) { 3437 adev->mode = mode; 3438 select_mode(adev); 3439 } 3440 pthread_mutex_unlock(&adev->lock); 3441 3442 return 0; 3443 } 3444 3445 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 3446 { 3447 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 3448 3449 adev->mic_mute = state; 3450 3451 return 0; 3452 } 3453 3454 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 3455 { 3456 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 3457 3458 *state = adev->mic_mute; 3459 3460 return 0; 3461 } 3462 3463 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 3464 const struct audio_config *config) 3465 { 3466 size_t size; 3467 int channel_count = popcount(config->channel_mask); 3468 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) 3469 return 0; 3470 3471 return get_input_buffer_size(config->sample_rate, config->format, channel_count); 3472 } 3473 3474 static int adev_open_input_stream(struct audio_hw_device *dev, 3475 audio_io_handle_t handle, 3476 audio_devices_t devices, 3477 struct audio_config *config, 3478 struct audio_stream_in **stream_in) 3479 { 3480 struct tuna_audio_device *ladev = (struct tuna_audio_device *)dev; 3481 struct tuna_stream_in *in; 3482 int ret; 3483 int channel_count = popcount(config->channel_mask); 3484 3485 *stream_in = NULL; 3486 3487 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) 3488 return -EINVAL; 3489 3490 in = (struct tuna_stream_in *)calloc(1, sizeof(struct tuna_stream_in)); 3491 if (!in) 3492 return -ENOMEM; 3493 3494 in->stream.common.get_sample_rate = in_get_sample_rate; 3495 in->stream.common.set_sample_rate = in_set_sample_rate; 3496 in->stream.common.get_buffer_size = in_get_buffer_size; 3497 in->stream.common.get_channels = in_get_channels; 3498 in->stream.common.get_format = in_get_format; 3499 in->stream.common.set_format = in_set_format; 3500 in->stream.common.standby = in_standby; 3501 in->stream.common.dump = in_dump; 3502 in->stream.common.set_parameters = in_set_parameters; 3503 in->stream.common.get_parameters = in_get_parameters; 3504 in->stream.common.add_audio_effect = in_add_audio_effect; 3505 in->stream.common.remove_audio_effect = in_remove_audio_effect; 3506 in->stream.set_gain = in_set_gain; 3507 in->stream.read = in_read; 3508 in->stream.get_input_frames_lost = in_get_input_frames_lost; 3509 3510 in->requested_rate = config->sample_rate; 3511 3512 memcpy(&in->config, &pcm_config_mm_ul, sizeof(pcm_config_mm_ul)); 3513 in->config.channels = channel_count; 3514 3515 in->main_channels = config->channel_mask; 3516 3517 /* initialisation of preprocessor structure array is implicit with the calloc. 3518 * same for in->aux_channels and in->aux_channels_changed */ 3519 3520 if (in->requested_rate != in->config.rate) { 3521 in->buf_provider.get_next_buffer = get_next_buffer; 3522 in->buf_provider.release_buffer = release_buffer; 3523 3524 ret = create_resampler(in->config.rate, 3525 in->requested_rate, 3526 in->config.channels, 3527 RESAMPLER_QUALITY_DEFAULT, 3528 &in->buf_provider, 3529 &in->resampler); 3530 if (ret != 0) { 3531 ret = -EINVAL; 3532 goto err; 3533 } 3534 } 3535 3536 in->dev = ladev; 3537 in->standby = 1; 3538 in->device = devices & ~AUDIO_DEVICE_BIT_IN; 3539 3540 *stream_in = &in->stream; 3541 return 0; 3542 3543 err: 3544 if (in->resampler) 3545 release_resampler(in->resampler); 3546 3547 free(in); 3548 return ret; 3549 } 3550 3551 static void adev_close_input_stream(struct audio_hw_device *dev, 3552 struct audio_stream_in *stream) 3553 { 3554 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 3555 int i; 3556 3557 in_standby(&stream->common); 3558 3559 for (i = 0; i < in->num_preprocessors; i++) { 3560 free(in->preprocessors[i].channel_configs); 3561 } 3562 3563 free(in->read_buf); 3564 if (in->resampler) { 3565 release_resampler(in->resampler); 3566 } 3567 if (in->proc_buf_in) 3568 free(in->proc_buf_in); 3569 if (in->proc_buf_out) 3570 free(in->proc_buf_out); 3571 if (in->ref_buf) 3572 free(in->ref_buf); 3573 3574 free(stream); 3575 return; 3576 } 3577 3578 static int adev_dump(const audio_hw_device_t *device, int fd) 3579 { 3580 return 0; 3581 } 3582 3583 static int adev_close(hw_device_t *device) 3584 { 3585 struct tuna_audio_device *adev = (struct tuna_audio_device *)device; 3586 3587 /* RIL */ 3588 ril_close(&adev->ril); 3589 3590 mixer_close(adev->mixer); 3591 free(device); 3592 return 0; 3593 } 3594 3595 static int adev_open(const hw_module_t* module, const char* name, 3596 hw_device_t** device) 3597 { 3598 struct tuna_audio_device *adev; 3599 int ret; 3600 3601 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) 3602 return -EINVAL; 3603 3604 adev = calloc(1, sizeof(struct tuna_audio_device)); 3605 if (!adev) 3606 return -ENOMEM; 3607 3608 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; 3609 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 3610 adev->hw_device.common.module = (struct hw_module_t *) module; 3611 adev->hw_device.common.close = adev_close; 3612 3613 adev->hw_device.init_check = adev_init_check; 3614 adev->hw_device.set_voice_volume = adev_set_voice_volume; 3615 adev->hw_device.set_master_volume = adev_set_master_volume; 3616 adev->hw_device.set_mode = adev_set_mode; 3617 adev->hw_device.set_mic_mute = adev_set_mic_mute; 3618 adev->hw_device.get_mic_mute = adev_get_mic_mute; 3619 adev->hw_device.set_parameters = adev_set_parameters; 3620 adev->hw_device.get_parameters = adev_get_parameters; 3621 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; 3622 adev->hw_device.open_output_stream = adev_open_output_stream; 3623 adev->hw_device.close_output_stream = adev_close_output_stream; 3624 adev->hw_device.open_input_stream = adev_open_input_stream; 3625 adev->hw_device.close_input_stream = adev_close_input_stream; 3626 adev->hw_device.dump = adev_dump; 3627 3628 adev->mixer = mixer_open(CARD_OMAP4_ABE); 3629 if (!adev->mixer) { 3630 free(adev); 3631 ALOGE("Unable to open the mixer, aborting."); 3632 return -EINVAL; 3633 } 3634 3635 adev->mixer_ctls.dl1_eq = mixer_get_ctl_by_name(adev->mixer, 3636 MIXER_DL1_EQUALIZER); 3637 adev->mixer_ctls.mm_dl1_volume = mixer_get_ctl_by_name(adev->mixer, 3638 MIXER_DL1_MEDIA_PLAYBACK_VOLUME); 3639 adev->mixer_ctls.tones_dl1_volume = mixer_get_ctl_by_name(adev->mixer, 3640 MIXER_DL1_TONES_PLAYBACK_VOLUME); 3641 adev->mixer_ctls.mm_dl2_volume = mixer_get_ctl_by_name(adev->mixer, 3642 MIXER_DL2_MEDIA_PLAYBACK_VOLUME); 3643 adev->mixer_ctls.vx_dl2_volume = mixer_get_ctl_by_name(adev->mixer, 3644 MIXER_DL2_VOICE_PLAYBACK_VOLUME); 3645 adev->mixer_ctls.tones_dl2_volume = mixer_get_ctl_by_name(adev->mixer, 3646 MIXER_DL2_TONES_PLAYBACK_VOLUME); 3647 adev->mixer_ctls.mm_dl1 = mixer_get_ctl_by_name(adev->mixer, 3648 MIXER_DL1_MIXER_MULTIMEDIA); 3649 adev->mixer_ctls.vx_dl1 = mixer_get_ctl_by_name(adev->mixer, 3650 MIXER_DL1_MIXER_VOICE); 3651 adev->mixer_ctls.tones_dl1 = mixer_get_ctl_by_name(adev->mixer, 3652 MIXER_DL1_MIXER_TONES); 3653 adev->mixer_ctls.mm_dl2 = mixer_get_ctl_by_name(adev->mixer, 3654 MIXER_DL2_MIXER_MULTIMEDIA); 3655 adev->mixer_ctls.vx_dl2 = mixer_get_ctl_by_name(adev->mixer, 3656 MIXER_DL2_MIXER_VOICE); 3657 adev->mixer_ctls.tones_dl2 = mixer_get_ctl_by_name(adev->mixer, 3658 MIXER_DL2_MIXER_TONES); 3659 adev->mixer_ctls.dl2_mono = mixer_get_ctl_by_name(adev->mixer, 3660 MIXER_DL2_MONO_MIXER); 3661 adev->mixer_ctls.dl1_headset = mixer_get_ctl_by_name(adev->mixer, 3662 MIXER_DL1_PDM_SWITCH); 3663 adev->mixer_ctls.dl1_bt = mixer_get_ctl_by_name(adev->mixer, 3664 MIXER_DL1_BT_VX_SWITCH); 3665 adev->mixer_ctls.earpiece_enable = mixer_get_ctl_by_name(adev->mixer, 3666 MIXER_EARPHONE_ENABLE_SWITCH); 3667 adev->mixer_ctls.left_capture = mixer_get_ctl_by_name(adev->mixer, 3668 MIXER_ANALOG_LEFT_CAPTURE_ROUTE); 3669 adev->mixer_ctls.right_capture = mixer_get_ctl_by_name(adev->mixer, 3670 MIXER_ANALOG_RIGHT_CAPTURE_ROUTE); 3671 adev->mixer_ctls.amic_ul_volume = mixer_get_ctl_by_name(adev->mixer, 3672 MIXER_AMIC_UL_VOLUME); 3673 adev->mixer_ctls.voice_ul_volume = mixer_get_ctl_by_name(adev->mixer, 3674 MIXER_AUDUL_VOICE_UL_VOLUME); 3675 adev->mixer_ctls.sidetone_capture = mixer_get_ctl_by_name(adev->mixer, 3676 MIXER_SIDETONE_MIXER_CAPTURE); 3677 adev->mixer_ctls.headset_volume = mixer_get_ctl_by_name(adev->mixer, 3678 MIXER_HEADSET_PLAYBACK_VOLUME); 3679 adev->mixer_ctls.speaker_volume = mixer_get_ctl_by_name(adev->mixer, 3680 MIXER_HANDSFREE_PLAYBACK_VOLUME); 3681 adev->mixer_ctls.earpiece_volume = mixer_get_ctl_by_name(adev->mixer, 3682 MIXER_EARPHONE_PLAYBACK_VOLUME); 3683 3684 if (!adev->mixer_ctls.dl1_eq || 3685 !adev->mixer_ctls.mm_dl1_volume || 3686 !adev->mixer_ctls.tones_dl1_volume || 3687 !adev->mixer_ctls.mm_dl2_volume || 3688 !adev->mixer_ctls.vx_dl2_volume || 3689 !adev->mixer_ctls.tones_dl2_volume || 3690 !adev->mixer_ctls.mm_dl1 || 3691 !adev->mixer_ctls.vx_dl1 || 3692 !adev->mixer_ctls.tones_dl1 || 3693 !adev->mixer_ctls.mm_dl2 || 3694 !adev->mixer_ctls.vx_dl2 || 3695 !adev->mixer_ctls.tones_dl2 || 3696 !adev->mixer_ctls.dl2_mono || 3697 !adev->mixer_ctls.dl1_headset || 3698 !adev->mixer_ctls.dl1_bt || 3699 !adev->mixer_ctls.earpiece_enable || 3700 !adev->mixer_ctls.left_capture || 3701 !adev->mixer_ctls.right_capture || 3702 !adev->mixer_ctls.amic_ul_volume || 3703 !adev->mixer_ctls.voice_ul_volume || 3704 !adev->mixer_ctls.sidetone_capture || 3705 !adev->mixer_ctls.headset_volume || 3706 !adev->mixer_ctls.speaker_volume || 3707 !adev->mixer_ctls.earpiece_volume) { 3708 mixer_close(adev->mixer); 3709 free(adev); 3710 ALOGE("Unable to locate all mixer controls, aborting."); 3711 return -EINVAL; 3712 } 3713 3714 /* Set the default route before the PCM stream is opened */ 3715 pthread_mutex_lock(&adev->lock); 3716 set_route_by_array(adev->mixer, defaults, 1); 3717 adev->mode = AUDIO_MODE_NORMAL; 3718 adev->out_device = AUDIO_DEVICE_OUT_SPEAKER; 3719 adev->in_device = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN; 3720 select_output_device(adev); 3721 3722 adev->pcm_modem_dl = NULL; 3723 adev->pcm_modem_ul = NULL; 3724 adev->voice_volume = 1.0f; 3725 adev->tty_mode = TTY_MODE_OFF; 3726 adev->device_is_toro = is_device_toro(); 3727 adev->bluetooth_nrec = true; 3728 adev->wb_amr = 0; 3729 3730 /* RIL */ 3731 ril_open(&adev->ril); 3732 pthread_mutex_unlock(&adev->lock); 3733 /* register callback for wideband AMR setting */ 3734 ril_register_set_wb_amr_callback(audio_set_wb_amr_callback, (void *)adev); 3735 3736 *device = &adev->hw_device.common; 3737 3738 return 0; 3739 } 3740 3741 static struct hw_module_methods_t hal_module_methods = { 3742 .open = adev_open, 3743 }; 3744 3745 struct audio_module HAL_MODULE_INFO_SYM = { 3746 .common = { 3747 .tag = HARDWARE_MODULE_TAG, 3748 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 3749 .hal_api_version = HARDWARE_HAL_API_VERSION, 3750 .id = AUDIO_HARDWARE_MODULE_ID, 3751 .name = "Tuna audio HW HAL", 3752 .author = "The Android Open Source Project", 3753 .methods = &hal_module_methods, 3754 }, 3755 }; 3756