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 /* Mixer control names */ 43 #define MIXER_DL2_LEFT_EQUALIZER "DL2 Left Equalizer" 44 #define MIXER_DL2_RIGHT_EQUALIZER "DL2 Right Equalizer" 45 #define MIXER_DL1_MEDIA_PLAYBACK_VOLUME "DL1 Media Playback Volume" 46 #define MIXER_DL1_VOICE_PLAYBACK_VOLUME "DL1 Voice Playback Volume" 47 #define MIXER_DL2_MEDIA_PLAYBACK_VOLUME "DL2 Media Playback Volume" 48 #define MIXER_DL2_VOICE_PLAYBACK_VOLUME "DL2 Voice Playback Volume" 49 #define MIXER_SDT_DL_VOLUME "SDT DL Volume" 50 #define MIXER_SDT_UL_VOLUME "SDT UL Volume" 51 52 #define MIXER_HEADSET_PLAYBACK_VOLUME "Headset Playback Volume" 53 #define MIXER_HANDSFREE_PLAYBACK_VOLUME "Handsfree Playback Volume" 54 #define MIXER_EARPHONE_PLAYBACK_VOLUME "Earphone Playback Volume" 55 #define MIXER_BT_UL_VOLUME "BT UL Volume" 56 57 #define MIXER_DL1_EQUALIZER "DL1 Equalizer" 58 #define MIXER_DL1_MIXER_MULTIMEDIA "DL1 Mixer Multimedia" 59 #define MIXER_DL1_MIXER_VOICE "DL1 Mixer Voice" 60 #define MIXER_DL2_MIXER_MULTIMEDIA "DL2 Mixer Multimedia" 61 #define MIXER_DL2_MIXER_VOICE "DL2 Mixer Voice" 62 #define MIXER_SIDETONE_MIXER_PLAYBACK "Sidetone Mixer Playback" 63 #define MIXER_SIDETONE_MIXER_CAPTURE "Sidetone Mixer Capture" 64 #define MIXER_DL2_MONO_MIXER "DL2 Mono Mixer" 65 #define MIXER_DL1_PDM_SWITCH "DL1 PDM Switch" 66 #define MIXER_DL1_BT_VX_SWITCH "DL1 BT_VX Switch" 67 #define MIXER_VOICE_CAPTURE_MIXER_CAPTURE "Voice Capture Mixer Capture" 68 69 #define MIXER_HS_LEFT_PLAYBACK "HS Left Playback" 70 #define MIXER_HS_RIGHT_PLAYBACK "HS Right Playback" 71 #define MIXER_HF_LEFT_PLAYBACK "HF Left Playback" 72 #define MIXER_HF_RIGHT_PLAYBACK "HF Right Playback" 73 #define MIXER_EARPHONE_ENABLE_SWITCH "Earphone Enable Switch" 74 75 #define MIXER_ANALOG_LEFT_CAPTURE_ROUTE "Analog Left Capture Route" 76 #define MIXER_ANALOG_RIGHT_CAPTURE_ROUTE "Analog Right Capture Route" 77 #define MIXER_CAPTURE_PREAMPLIFIER_VOLUME "Capture Preamplifier Volume" 78 #define MIXER_CAPTURE_VOLUME "Capture Volume" 79 #define MIXER_AMIC_UL_VOLUME "AMIC UL Volume" 80 #define MIXER_AUDUL_VOICE_UL_VOLUME "AUDUL Voice UL Volume" 81 #define MIXER_MUX_VX0 "MUX_VX0" 82 #define MIXER_MUX_VX1 "MUX_VX1" 83 #define MIXER_MUX_UL10 "MUX_UL10" 84 #define MIXER_MUX_UL11 "MUX_UL11" 85 86 /* Mixer control gain and route values */ 87 #define MIXER_ABE_GAIN_0DB 120 88 #define MIXER_PLAYBACK_HS_DAC "HS DAC" 89 #define MIXER_PLAYBACK_HF_DAC "HF DAC" 90 #define MIXER_MAIN_MIC "Main Mic" 91 #define MIXER_SUB_MIC "Sub Mic" 92 #define MIXER_HS_MIC "Headset Mic" 93 #define MIXER_AMIC0 "AMic0" 94 #define MIXER_AMIC1 "AMic1" 95 #define MIXER_BT_LEFT "BT Left" 96 #define MIXER_BT_RIGHT "BT Right" 97 #define MIXER_450HZ_HIGH_PASS "450Hz High-pass" 98 #define MIXER_FLAT_RESPONSE "Flat response" 99 #define MIXER_4KHZ_LPF_0DB "4Khz LPF 0dB" 100 101 102 /* ALSA cards for OMAP4 */ 103 #define CARD_OMAP4_ABE 0 104 #define CARD_OMAP4_HDMI 1 105 #define CARD_TUNA_DEFAULT CARD_OMAP4_ABE 106 107 /* ALSA ports for OMAP4 */ 108 #define PORT_MM 0 109 #define PORT_MM2_UL 1 110 #define PORT_VX 2 111 #define PORT_TONES 3 112 #define PORT_VIBRA 4 113 #define PORT_MODEM 5 114 #define PORT_MM_LP 6 115 #define PORT_SPDIF 9 116 #define PORT_HDMI 0 117 118 /* constraint imposed by ABE: all period sizes must be multiples of 24 */ 119 #define ABE_BASE_FRAME_COUNT 24 120 /* number of base blocks in a short period (low latency) */ 121 #define SHORT_PERIOD_MULTIPLIER 44 /* 22 ms */ 122 /* number of frames per short period (low latency) */ 123 #define SHORT_PERIOD_SIZE (ABE_BASE_FRAME_COUNT * SHORT_PERIOD_MULTIPLIER) 124 /* number of short periods in a long period (low power) */ 125 #define LONG_PERIOD_MULTIPLIER 14 /* 308 ms */ 126 /* number of frames per long period (low power) */ 127 #define LONG_PERIOD_SIZE (SHORT_PERIOD_SIZE * LONG_PERIOD_MULTIPLIER) 128 /* number of periods for low power playback */ 129 #define PLAYBACK_LONG_PERIOD_COUNT 2 130 /* number of pseudo periods for low latency playback */ 131 #define PLAYBACK_SHORT_PERIOD_COUNT 4 132 /* number of periods for capture */ 133 #define CAPTURE_PERIOD_COUNT 2 134 /* minimum sleep time in out_write() when write threshold is not reached */ 135 #define MIN_WRITE_SLEEP_US 5000 136 137 #define RESAMPLER_BUFFER_FRAMES (SHORT_PERIOD_SIZE * 2) 138 #define RESAMPLER_BUFFER_SIZE (4 * RESAMPLER_BUFFER_FRAMES) 139 140 #define DEFAULT_OUT_SAMPLING_RATE 44100 141 142 /* sampling rate when using MM low power port */ 143 #define MM_LOW_POWER_SAMPLING_RATE 44100 144 /* sampling rate when using MM full power port */ 145 #define MM_FULL_POWER_SAMPLING_RATE 48000 146 /* sampling rate when using VX port for narrow band */ 147 #define VX_NB_SAMPLING_RATE 8000 148 /* sampling rate when using VX port for wide band */ 149 #define VX_WB_SAMPLING_RATE 16000 150 151 /* conversions from dB to ABE and codec gains */ 152 #define DB_TO_ABE_GAIN(x) ((x) + MIXER_ABE_GAIN_0DB) 153 #define DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(x) (((x) + 6) / 6) 154 #define DB_TO_CAPTURE_VOLUME(x) (((x) - 6) / 6) 155 #define DB_TO_HEADSET_VOLUME(x) (((x) + 30) / 2) 156 #define DB_TO_SPEAKER_VOLUME(x) (((x) + 52) / 2) 157 #define DB_TO_EARPIECE_VOLUME(x) (((x) + 24) / 2) 158 159 /* conversions from codec and ABE gains to dB */ 160 #define DB_FROM_SPEAKER_VOLUME(x) ((x) * 2 - 52) 161 162 /* use-case specific mic volumes, all in dB */ 163 #define CAPTURE_MAIN_MIC_VOLUME 16 164 #define CAPTURE_SUB_MIC_VOLUME 18 165 #define CAPTURE_HEADSET_MIC_VOLUME 12 166 167 #define VOICE_RECOGNITION_MAIN_MIC_VOLUME 5 168 #define VOICE_RECOGNITION_SUB_MIC_VOLUME 18 169 #define VOICE_RECOGNITION_HEADSET_MIC_VOLUME 14 170 171 #define CAMCORDER_MAIN_MIC_VOLUME 13 172 #define CAMCORDER_SUB_MIC_VOLUME 10 173 #define CAMCORDER_HEADSET_MIC_VOLUME 12 174 175 #define VOIP_MAIN_MIC_VOLUME 13 176 #define VOIP_SUB_MIC_VOLUME 20 177 #define VOIP_HEADSET_MIC_VOLUME 12 178 179 #define VOICE_CALL_MAIN_MIC_VOLUME 0 180 #define VOICE_CALL_SUB_MIC_VOLUME_MAGURO -4 181 #define VOICE_CALL_SUB_MIC_VOLUME_TORO -2 182 #define VOICE_CALL_HEADSET_MIC_VOLUME 8 183 184 /* use-case specific output volumes */ 185 #define NORMAL_SPEAKER_VOLUME_TORO 4 186 #define NORMAL_SPEAKER_VOLUME_MAGURO 2 187 #define NORMAL_HEADSET_VOLUME_TORO -12 188 #define NORMAL_HEADSET_VOLUME_MAGURO -12 189 #define NORMAL_HEADPHONE_VOLUME_TORO -6 /* allow louder output for headphones */ 190 #define NORMAL_HEADPHONE_VOLUME_MAGURO -6 191 #define NORMAL_EARPIECE_VOLUME_TORO -2 192 #define NORMAL_EARPIECE_VOLUME_MAGURO -2 193 194 #define VOICE_CALL_SPEAKER_VOLUME_TORO 9 195 #define VOICE_CALL_SPEAKER_VOLUME_MAGURO 6 196 #define VOICE_CALL_HEADSET_VOLUME_TORO -6 197 #define VOICE_CALL_HEADSET_VOLUME_MAGURO 0 198 #define VOICE_CALL_EARPIECE_VOLUME_TORO 2 199 #define VOICE_CALL_EARPIECE_VOLUME_MAGURO 6 200 201 #define VOIP_SPEAKER_VOLUME_TORO 9 202 #define VOIP_SPEAKER_VOLUME_MAGURO 7 203 #define VOIP_HEADSET_VOLUME_TORO -6 204 #define VOIP_HEADSET_VOLUME_MAGURO -6 205 #define VOIP_EARPIECE_VOLUME_TORO 6 206 #define VOIP_EARPIECE_VOLUME_MAGURO 6 207 208 #define HEADPHONE_VOLUME_TTY -2 209 #define RINGTONE_HEADSET_VOLUME_OFFSET -14 210 211 /* product-specific defines */ 212 #define PRODUCT_DEVICE_PROPERTY "ro.product.device" 213 #define PRODUCT_NAME_PROPERTY "ro.product.name" 214 #define PRODUCT_DEVICE_TORO "toro" 215 #define PRODUCT_NAME_YAKJU "yakju" 216 217 enum tty_modes { 218 TTY_MODE_OFF, 219 TTY_MODE_VCO, 220 TTY_MODE_HCO, 221 TTY_MODE_FULL 222 }; 223 224 struct pcm_config pcm_config_mm = { 225 .channels = 2, 226 .rate = MM_FULL_POWER_SAMPLING_RATE, 227 .period_size = LONG_PERIOD_SIZE, 228 .period_count = PLAYBACK_LONG_PERIOD_COUNT, 229 .format = PCM_FORMAT_S16_LE, 230 }; 231 232 struct pcm_config pcm_config_mm_ul = { 233 .channels = 2, 234 .rate = MM_FULL_POWER_SAMPLING_RATE, 235 .period_size = SHORT_PERIOD_SIZE, 236 .period_count = CAPTURE_PERIOD_COUNT, 237 .format = PCM_FORMAT_S16_LE, 238 }; 239 240 struct pcm_config pcm_config_vx = { 241 .channels = 2, 242 .rate = VX_NB_SAMPLING_RATE, 243 .period_size = 160, 244 .period_count = 2, 245 .format = PCM_FORMAT_S16_LE, 246 }; 247 248 #define MIN(x, y) ((x) > (y) ? (y) : (x)) 249 250 struct route_setting 251 { 252 char *ctl_name; 253 int intval; 254 char *strval; 255 }; 256 257 /* These are values that never change */ 258 struct route_setting defaults[] = { 259 /* general */ 260 { 261 .ctl_name = MIXER_DL2_LEFT_EQUALIZER, 262 .strval = MIXER_450HZ_HIGH_PASS, 263 }, 264 { 265 .ctl_name = MIXER_DL2_RIGHT_EQUALIZER, 266 .strval = MIXER_450HZ_HIGH_PASS, 267 }, 268 { 269 .ctl_name = MIXER_DL1_MEDIA_PLAYBACK_VOLUME, 270 .intval = MIXER_ABE_GAIN_0DB, 271 }, 272 { 273 .ctl_name = MIXER_DL2_MEDIA_PLAYBACK_VOLUME, 274 .intval = MIXER_ABE_GAIN_0DB, 275 }, 276 { 277 .ctl_name = MIXER_DL1_VOICE_PLAYBACK_VOLUME, 278 .intval = MIXER_ABE_GAIN_0DB, 279 }, 280 { 281 .ctl_name = MIXER_DL2_VOICE_PLAYBACK_VOLUME, 282 .intval = MIXER_ABE_GAIN_0DB, 283 }, 284 { 285 .ctl_name = MIXER_SDT_DL_VOLUME, 286 .intval = MIXER_ABE_GAIN_0DB, 287 }, 288 { 289 .ctl_name = MIXER_AUDUL_VOICE_UL_VOLUME, 290 .intval = MIXER_ABE_GAIN_0DB, 291 }, 292 { 293 .ctl_name = MIXER_CAPTURE_PREAMPLIFIER_VOLUME, 294 .intval = DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(0), 295 }, 296 { 297 .ctl_name = MIXER_CAPTURE_VOLUME, 298 .intval = DB_TO_CAPTURE_VOLUME(30), 299 }, 300 { 301 .ctl_name = MIXER_SDT_UL_VOLUME, 302 .intval = MIXER_ABE_GAIN_0DB - 17, 303 }, 304 { 305 .ctl_name = MIXER_SIDETONE_MIXER_CAPTURE, 306 .intval = 0, 307 }, 308 309 /* headset */ 310 { 311 .ctl_name = MIXER_SIDETONE_MIXER_PLAYBACK, 312 .intval = 1, 313 }, 314 { 315 .ctl_name = MIXER_DL1_PDM_SWITCH, 316 .intval = 1, 317 }, 318 319 /* bt */ 320 { 321 .ctl_name = MIXER_BT_UL_VOLUME, 322 .intval = MIXER_ABE_GAIN_0DB, 323 }, 324 { 325 .ctl_name = NULL, 326 }, 327 }; 328 329 struct route_setting hf_output[] = { 330 { 331 .ctl_name = MIXER_HF_LEFT_PLAYBACK, 332 .strval = MIXER_PLAYBACK_HF_DAC, 333 }, 334 { 335 .ctl_name = MIXER_HF_RIGHT_PLAYBACK, 336 .strval = MIXER_PLAYBACK_HF_DAC, 337 }, 338 { 339 .ctl_name = NULL, 340 }, 341 }; 342 343 struct route_setting hs_output[] = { 344 { 345 .ctl_name = MIXER_HS_LEFT_PLAYBACK, 346 .strval = MIXER_PLAYBACK_HS_DAC, 347 }, 348 { 349 .ctl_name = MIXER_HS_RIGHT_PLAYBACK, 350 .strval = MIXER_PLAYBACK_HS_DAC, 351 }, 352 { 353 .ctl_name = NULL, 354 }, 355 }; 356 357 /* MM UL front-end paths */ 358 struct route_setting mm_ul2_bt[] = { 359 { 360 .ctl_name = MIXER_MUX_UL10, 361 .strval = MIXER_BT_LEFT, 362 }, 363 { 364 .ctl_name = MIXER_MUX_UL11, 365 .strval = MIXER_BT_LEFT, 366 }, 367 { 368 .ctl_name = NULL, 369 }, 370 }; 371 372 struct route_setting mm_ul2_amic_left[] = { 373 { 374 .ctl_name = MIXER_MUX_UL10, 375 .strval = MIXER_AMIC0, 376 }, 377 { 378 .ctl_name = MIXER_MUX_UL11, 379 .strval = MIXER_AMIC0, 380 }, 381 { 382 .ctl_name = NULL, 383 }, 384 }; 385 386 struct route_setting mm_ul2_amic_right[] = { 387 { 388 .ctl_name = MIXER_MUX_UL10, 389 .strval = MIXER_AMIC1, 390 }, 391 { 392 .ctl_name = MIXER_MUX_UL11, 393 .strval = MIXER_AMIC1, 394 }, 395 { 396 .ctl_name = NULL, 397 }, 398 }; 399 400 /* VX UL front-end paths */ 401 struct route_setting vx_ul_amic_left[] = { 402 { 403 .ctl_name = MIXER_MUX_VX0, 404 .strval = MIXER_AMIC0, 405 }, 406 { 407 .ctl_name = MIXER_MUX_VX1, 408 .strval = MIXER_AMIC0, 409 }, 410 { 411 .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE, 412 .intval = 1, 413 }, 414 { 415 .ctl_name = NULL, 416 }, 417 }; 418 419 struct route_setting vx_ul_amic_right[] = { 420 { 421 .ctl_name = MIXER_MUX_VX0, 422 .strval = MIXER_AMIC1, 423 }, 424 { 425 .ctl_name = MIXER_MUX_VX1, 426 .strval = MIXER_AMIC1, 427 }, 428 { 429 .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE, 430 .intval = 1, 431 }, 432 { 433 .ctl_name = NULL, 434 }, 435 }; 436 437 struct route_setting vx_ul_bt[] = { 438 { 439 .ctl_name = MIXER_MUX_VX0, 440 .strval = MIXER_BT_LEFT, 441 }, 442 { 443 .ctl_name = MIXER_MUX_VX1, 444 .strval = MIXER_BT_LEFT, 445 }, 446 { 447 .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE, 448 .intval = 1, 449 }, 450 { 451 .ctl_name = NULL, 452 }, 453 }; 454 455 struct mixer_ctls 456 { 457 struct mixer_ctl *dl1_eq; 458 struct mixer_ctl *mm_dl2_volume; 459 struct mixer_ctl *vx_dl2_volume; 460 struct mixer_ctl *mm_dl1; 461 struct mixer_ctl *mm_dl2; 462 struct mixer_ctl *vx_dl1; 463 struct mixer_ctl *vx_dl2; 464 struct mixer_ctl *earpiece_enable; 465 struct mixer_ctl *dl2_mono; 466 struct mixer_ctl *dl1_headset; 467 struct mixer_ctl *dl1_bt; 468 struct mixer_ctl *left_capture; 469 struct mixer_ctl *right_capture; 470 struct mixer_ctl *amic_ul_volume; 471 struct mixer_ctl *voice_ul_volume; 472 struct mixer_ctl *sidetone_capture; 473 struct mixer_ctl *headset_volume; 474 struct mixer_ctl *speaker_volume; 475 struct mixer_ctl *earpiece_volume; 476 }; 477 478 struct tuna_audio_device { 479 struct audio_hw_device hw_device; 480 481 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 482 struct mixer *mixer; 483 struct mixer_ctls mixer_ctls; 484 int mode; 485 int devices; 486 struct pcm *pcm_modem_dl; 487 struct pcm *pcm_modem_ul; 488 int in_call; 489 float voice_volume; 490 struct tuna_stream_in *active_input; 491 struct tuna_stream_out *active_output; 492 bool mic_mute; 493 int tty_mode; 494 struct echo_reference_itfe *echo_reference; 495 bool bluetooth_nrec; 496 bool device_is_toro; 497 int wb_amr; 498 bool low_power; 499 500 /* RIL */ 501 struct ril_handle ril; 502 }; 503 504 struct tuna_stream_out { 505 struct audio_stream_out stream; 506 507 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 508 struct pcm_config config; 509 struct pcm *pcm; 510 struct resampler_itfe *resampler; 511 char *buffer; 512 int standby; 513 struct echo_reference_itfe *echo_reference; 514 struct tuna_audio_device *dev; 515 int write_threshold; 516 bool low_power; 517 }; 518 519 #define MAX_PREPROCESSORS 3 /* maximum one AGC + one NS + one AEC per input stream */ 520 521 struct tuna_stream_in { 522 struct audio_stream_in stream; 523 524 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 525 struct pcm_config config; 526 struct pcm *pcm; 527 int device; 528 struct resampler_itfe *resampler; 529 struct resampler_buffer_provider buf_provider; 530 int16_t *buffer; 531 size_t frames_in; 532 unsigned int requested_rate; 533 int standby; 534 int source; 535 struct echo_reference_itfe *echo_reference; 536 bool need_echo_reference; 537 effect_handle_t preprocessors[MAX_PREPROCESSORS]; 538 int num_preprocessors; 539 int16_t *proc_buf; 540 size_t proc_buf_size; 541 size_t proc_frames_in; 542 int16_t *ref_buf; 543 size_t ref_buf_size; 544 size_t ref_frames_in; 545 int read_status; 546 547 struct tuna_audio_device *dev; 548 }; 549 550 /** 551 * NOTE: when multiple mutexes have to be acquired, always respect the following order: 552 * hw device > in stream > out stream 553 */ 554 555 556 static void select_output_device(struct tuna_audio_device *adev); 557 static void select_input_device(struct tuna_audio_device *adev); 558 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume); 559 static int do_input_standby(struct tuna_stream_in *in); 560 static int do_output_standby(struct tuna_stream_out *out); 561 562 /* Returns true on devices that are toro, false otherwise */ 563 static int is_device_toro(void) 564 { 565 char property[PROPERTY_VALUE_MAX]; 566 567 property_get(PRODUCT_DEVICE_PROPERTY, property, PRODUCT_DEVICE_TORO); 568 569 /* return true if the property matches the given value */ 570 return strcmp(property, PRODUCT_DEVICE_TORO) == 0; 571 } 572 573 /* The enable flag when 0 makes the assumption that enums are disabled by 574 * "Off" and integers/booleans by 0 */ 575 static int set_route_by_array(struct mixer *mixer, struct route_setting *route, 576 int enable) 577 { 578 struct mixer_ctl *ctl; 579 unsigned int i, j; 580 581 /* Go through the route array and set each value */ 582 i = 0; 583 while (route[i].ctl_name) { 584 ctl = mixer_get_ctl_by_name(mixer, route[i].ctl_name); 585 if (!ctl) 586 return -EINVAL; 587 588 if (route[i].strval) { 589 if (enable) 590 mixer_ctl_set_enum_by_string(ctl, route[i].strval); 591 else 592 mixer_ctl_set_enum_by_string(ctl, "Off"); 593 } else { 594 /* This ensures multiple (i.e. stereo) values are set jointly */ 595 for (j = 0; j < mixer_ctl_get_num_values(ctl); j++) { 596 if (enable) 597 mixer_ctl_set_value(ctl, j, route[i].intval); 598 else 599 mixer_ctl_set_value(ctl, j, 0); 600 } 601 } 602 i++; 603 } 604 605 return 0; 606 } 607 608 static int start_call(struct tuna_audio_device *adev) 609 { 610 LOGE("Opening modem PCMs"); 611 612 pcm_config_vx.rate = adev->wb_amr ? VX_WB_SAMPLING_RATE : VX_NB_SAMPLING_RATE; 613 614 /* Open modem PCM channels */ 615 if (adev->pcm_modem_dl == NULL) { 616 adev->pcm_modem_dl = pcm_open(0, PORT_MODEM, PCM_OUT, &pcm_config_vx); 617 if (!pcm_is_ready(adev->pcm_modem_dl)) { 618 LOGE("cannot open PCM modem DL stream: %s", pcm_get_error(adev->pcm_modem_dl)); 619 goto err_open_dl; 620 } 621 } 622 623 if (adev->pcm_modem_ul == NULL) { 624 adev->pcm_modem_ul = pcm_open(0, PORT_MODEM, PCM_IN, &pcm_config_vx); 625 if (!pcm_is_ready(adev->pcm_modem_ul)) { 626 LOGE("cannot open PCM modem UL stream: %s", pcm_get_error(adev->pcm_modem_ul)); 627 goto err_open_ul; 628 } 629 } 630 631 pcm_start(adev->pcm_modem_dl); 632 pcm_start(adev->pcm_modem_ul); 633 634 return 0; 635 636 err_open_ul: 637 pcm_close(adev->pcm_modem_ul); 638 adev->pcm_modem_ul = NULL; 639 err_open_dl: 640 pcm_close(adev->pcm_modem_dl); 641 adev->pcm_modem_dl = NULL; 642 643 return -ENOMEM; 644 } 645 646 static void end_call(struct tuna_audio_device *adev) 647 { 648 LOGE("Closing modem PCMs"); 649 650 pcm_stop(adev->pcm_modem_dl); 651 pcm_stop(adev->pcm_modem_ul); 652 pcm_close(adev->pcm_modem_dl); 653 pcm_close(adev->pcm_modem_ul); 654 adev->pcm_modem_dl = NULL; 655 adev->pcm_modem_ul = NULL; 656 } 657 658 static void set_eq_filter(struct tuna_audio_device *adev) 659 { 660 /* DL1_EQ can't be used for bt */ 661 int dl1_eq_applicable = adev->devices & (AUDIO_DEVICE_OUT_WIRED_HEADSET | 662 AUDIO_DEVICE_OUT_WIRED_HEADPHONE | AUDIO_DEVICE_OUT_EARPIECE); 663 664 /* 4Khz LPF is used only in NB-AMR voicecall */ 665 if ((adev->mode == AUDIO_MODE_IN_CALL) && dl1_eq_applicable && 666 (adev->tty_mode == TTY_MODE_OFF) && !adev->wb_amr) 667 mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_4KHZ_LPF_0DB); 668 else 669 mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_FLAT_RESPONSE); 670 } 671 672 void audio_set_wb_amr_callback(void *data, int enable) 673 { 674 struct tuna_audio_device *adev = (struct tuna_audio_device *)data; 675 676 pthread_mutex_lock(&adev->lock); 677 if (adev->wb_amr != enable) { 678 adev->wb_amr = enable; 679 680 /* reopen the modem PCMs at the new rate */ 681 if (adev->in_call) { 682 end_call(adev); 683 set_eq_filter(adev); 684 start_call(adev); 685 } 686 } 687 pthread_mutex_unlock(&adev->lock); 688 } 689 690 static void set_incall_device(struct tuna_audio_device *adev) 691 { 692 int device_type; 693 694 switch(adev->devices & AUDIO_DEVICE_OUT_ALL) { 695 case AUDIO_DEVICE_OUT_EARPIECE: 696 device_type = SOUND_AUDIO_PATH_HANDSET; 697 break; 698 case AUDIO_DEVICE_OUT_SPEAKER: 699 case AUDIO_DEVICE_OUT_AUX_DIGITAL: 700 case AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET: 701 device_type = SOUND_AUDIO_PATH_SPEAKER; 702 break; 703 case AUDIO_DEVICE_OUT_WIRED_HEADSET: 704 device_type = SOUND_AUDIO_PATH_HEADSET; 705 break; 706 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: 707 device_type = SOUND_AUDIO_PATH_HEADPHONE; 708 break; 709 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: 710 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 711 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 712 if (adev->bluetooth_nrec) 713 device_type = SOUND_AUDIO_PATH_BLUETOOTH; 714 else 715 device_type = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR; 716 break; 717 default: 718 device_type = SOUND_AUDIO_PATH_HANDSET; 719 break; 720 } 721 722 /* if output device isn't supported, open modem side to handset by default */ 723 ril_set_call_audio_path(&adev->ril, device_type); 724 } 725 726 static void set_input_volumes(struct tuna_audio_device *adev, int main_mic_on, 727 int headset_mic_on, int sub_mic_on) 728 { 729 unsigned int channel; 730 int volume = MIXER_ABE_GAIN_0DB; 731 732 if (adev->mode == AUDIO_MODE_IN_CALL) { 733 int sub_mic_volume = is_device_toro() ? VOICE_CALL_SUB_MIC_VOLUME_TORO : 734 VOICE_CALL_SUB_MIC_VOLUME_MAGURO; 735 /* special case: don't look at input source for IN_CALL state */ 736 volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_CALL_MAIN_MIC_VOLUME : 737 (headset_mic_on ? VOICE_CALL_HEADSET_MIC_VOLUME : 738 (sub_mic_on ? sub_mic_volume : 0))); 739 } else if (adev->active_input) { 740 /* determine input volume by use case */ 741 switch (adev->active_input->source) { 742 case AUDIO_SOURCE_MIC: /* general capture */ 743 volume = DB_TO_ABE_GAIN(main_mic_on ? CAPTURE_MAIN_MIC_VOLUME : 744 (headset_mic_on ? CAPTURE_HEADSET_MIC_VOLUME : 745 (sub_mic_on ? CAPTURE_SUB_MIC_VOLUME : 0))); 746 break; 747 748 case AUDIO_SOURCE_CAMCORDER: 749 volume = DB_TO_ABE_GAIN(main_mic_on ? CAMCORDER_MAIN_MIC_VOLUME : 750 (headset_mic_on ? CAMCORDER_HEADSET_MIC_VOLUME : 751 (sub_mic_on ? CAMCORDER_SUB_MIC_VOLUME : 0))); 752 break; 753 754 case AUDIO_SOURCE_VOICE_RECOGNITION: 755 volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_RECOGNITION_MAIN_MIC_VOLUME : 756 (headset_mic_on ? VOICE_RECOGNITION_HEADSET_MIC_VOLUME : 757 (sub_mic_on ? VOICE_RECOGNITION_SUB_MIC_VOLUME : 0))); 758 break; 759 760 case AUDIO_SOURCE_VOICE_COMMUNICATION: /* VoIP */ 761 volume = DB_TO_ABE_GAIN(main_mic_on ? VOIP_MAIN_MIC_VOLUME : 762 (headset_mic_on ? VOIP_HEADSET_MIC_VOLUME : 763 (sub_mic_on ? VOIP_SUB_MIC_VOLUME : 0))); 764 break; 765 766 default: 767 /* nothing to do */ 768 break; 769 } 770 } 771 772 for (channel = 0; channel < 2; channel++) 773 mixer_ctl_set_value(adev->mixer_ctls.amic_ul_volume, channel, volume); 774 } 775 776 static void set_output_volumes(struct tuna_audio_device *adev, bool tty_volume) 777 { 778 unsigned int channel; 779 int speaker_volume; 780 int headset_volume; 781 int earpiece_volume; 782 bool toro = adev->device_is_toro; 783 int headphone_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 784 int speaker_on = adev->devices & AUDIO_DEVICE_OUT_SPEAKER; 785 int speaker_volume_overrange = MIXER_ABE_GAIN_0DB; 786 int speaker_max_db = 787 DB_FROM_SPEAKER_VOLUME(mixer_ctl_get_range_max(adev->mixer_ctls.speaker_volume)); 788 struct mixer_ctl *mixer_ctl_overrange = adev->mixer_ctls.mm_dl2_volume; 789 790 if (adev->mode == AUDIO_MODE_IN_CALL) { 791 /* Voice call */ 792 speaker_volume = toro ? VOICE_CALL_SPEAKER_VOLUME_TORO : 793 VOICE_CALL_SPEAKER_VOLUME_MAGURO; 794 headset_volume = toro ? VOICE_CALL_HEADSET_VOLUME_TORO : 795 VOICE_CALL_HEADSET_VOLUME_MAGURO; 796 earpiece_volume = toro ? VOICE_CALL_EARPIECE_VOLUME_TORO : 797 VOICE_CALL_EARPIECE_VOLUME_MAGURO; 798 mixer_ctl_overrange = adev->mixer_ctls.vx_dl2_volume; 799 } else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) { 800 /* VoIP */ 801 speaker_volume = toro ? VOIP_SPEAKER_VOLUME_TORO : 802 VOIP_SPEAKER_VOLUME_MAGURO; 803 headset_volume = toro ? VOIP_HEADSET_VOLUME_TORO : 804 VOIP_HEADSET_VOLUME_MAGURO; 805 earpiece_volume = toro ? VOIP_EARPIECE_VOLUME_TORO : 806 VOIP_EARPIECE_VOLUME_MAGURO; 807 } else { 808 /* Media */ 809 speaker_volume = toro ? NORMAL_SPEAKER_VOLUME_TORO : 810 NORMAL_SPEAKER_VOLUME_MAGURO; 811 if (headphone_on) 812 headset_volume = toro ? NORMAL_HEADPHONE_VOLUME_TORO : 813 NORMAL_HEADPHONE_VOLUME_MAGURO; 814 else 815 headset_volume = toro ? NORMAL_HEADSET_VOLUME_TORO : 816 NORMAL_HEADSET_VOLUME_MAGURO; 817 earpiece_volume = toro ? NORMAL_EARPIECE_VOLUME_TORO : 818 NORMAL_EARPIECE_VOLUME_MAGURO; 819 } 820 if (tty_volume) 821 headset_volume = HEADPHONE_VOLUME_TTY; 822 else if (adev->mode == AUDIO_MODE_RINGTONE) 823 headset_volume += RINGTONE_HEADSET_VOLUME_OFFSET; 824 825 /* If we have run out of range in the codec (analog) speaker volume, 826 we have to apply the remainder of the dB increase to the DL2 827 media/voice mixer volume, which is a digital gain */ 828 if (speaker_volume > speaker_max_db) { 829 speaker_volume_overrange += (speaker_volume - speaker_max_db); 830 speaker_volume = speaker_max_db; 831 } 832 833 for (channel = 0; channel < 2; channel++) { 834 mixer_ctl_set_value(adev->mixer_ctls.speaker_volume, channel, 835 DB_TO_SPEAKER_VOLUME(speaker_volume)); 836 mixer_ctl_set_value(adev->mixer_ctls.headset_volume, channel, 837 DB_TO_HEADSET_VOLUME(headset_volume)); 838 } 839 if (speaker_on) 840 mixer_ctl_set_value(mixer_ctl_overrange, 0, speaker_volume_overrange); 841 else 842 mixer_ctl_set_value(mixer_ctl_overrange, 0, MIXER_ABE_GAIN_0DB); 843 mixer_ctl_set_value(adev->mixer_ctls.earpiece_volume, 0, 844 DB_TO_EARPIECE_VOLUME(earpiece_volume)); 845 } 846 847 static void force_all_standby(struct tuna_audio_device *adev) 848 { 849 struct tuna_stream_in *in; 850 struct tuna_stream_out *out; 851 852 if (adev->active_output) { 853 out = adev->active_output; 854 pthread_mutex_lock(&out->lock); 855 do_output_standby(out); 856 pthread_mutex_unlock(&out->lock); 857 } 858 859 if (adev->active_input) { 860 in = adev->active_input; 861 pthread_mutex_lock(&in->lock); 862 do_input_standby(in); 863 pthread_mutex_unlock(&in->lock); 864 } 865 } 866 867 static void select_mode(struct tuna_audio_device *adev) 868 { 869 if (adev->mode == AUDIO_MODE_IN_CALL) { 870 LOGE("Entering IN_CALL state, in_call=%d", adev->in_call); 871 if (!adev->in_call) { 872 force_all_standby(adev); 873 /* force earpiece route for in call state if speaker is the 874 only currently selected route. This prevents having to tear 875 down the modem PCMs to change route from speaker to earpiece 876 after the ringtone is played, but doesn't cause a route 877 change if a headset or bt device is already connected. If 878 speaker is not the only thing active, just remove it from 879 the route. We'll assume it'll never be used initally during 880 a call. This works because we're sure that the audio policy 881 manager will update the output device after the audio mode 882 change, even if the device selection did not change. */ 883 if ((adev->devices & AUDIO_DEVICE_OUT_ALL) == AUDIO_DEVICE_OUT_SPEAKER) 884 adev->devices = AUDIO_DEVICE_OUT_EARPIECE | 885 AUDIO_DEVICE_IN_BUILTIN_MIC; 886 else 887 adev->devices &= ~AUDIO_DEVICE_OUT_SPEAKER; 888 select_output_device(adev); 889 start_call(adev); 890 ril_set_call_clock_sync(&adev->ril, SOUND_CLOCK_START); 891 adev_set_voice_volume(&adev->hw_device, adev->voice_volume); 892 adev->in_call = 1; 893 } 894 } else { 895 LOGE("Leaving IN_CALL state, in_call=%d, mode=%d", 896 adev->in_call, adev->mode); 897 if (adev->in_call) { 898 adev->in_call = 0; 899 end_call(adev); 900 force_all_standby(adev); 901 select_output_device(adev); 902 select_input_device(adev); 903 } 904 } 905 } 906 907 static void select_output_device(struct tuna_audio_device *adev) 908 { 909 int headset_on; 910 int headphone_on; 911 int speaker_on; 912 int earpiece_on; 913 int bt_on; 914 int dl1_on; 915 int sidetone_capture_on = 0; 916 bool tty_volume = false; 917 unsigned int channel; 918 919 /* Mute VX_UL to avoid pop noises in the tx path 920 * during call before switch changes. 921 */ 922 if (adev->mode == AUDIO_MODE_IN_CALL) { 923 for (channel = 0; channel < 2; channel++) 924 mixer_ctl_set_value(adev->mixer_ctls.voice_ul_volume, 925 channel, 0); 926 } 927 928 headset_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADSET; 929 headphone_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 930 speaker_on = adev->devices & AUDIO_DEVICE_OUT_SPEAKER; 931 earpiece_on = adev->devices & AUDIO_DEVICE_OUT_EARPIECE; 932 bt_on = adev->devices & AUDIO_DEVICE_OUT_ALL_SCO; 933 934 /* force rx path according to TTY mode when in call */ 935 if (adev->mode == AUDIO_MODE_IN_CALL && !bt_on) { 936 switch(adev->tty_mode) { 937 case TTY_MODE_FULL: 938 case TTY_MODE_VCO: 939 /* rx path to headphones */ 940 headphone_on = 1; 941 headset_on = 0; 942 speaker_on = 0; 943 earpiece_on = 0; 944 tty_volume = true; 945 break; 946 case TTY_MODE_HCO: 947 /* rx path to device speaker */ 948 headphone_on = 0; 949 headset_on = 0; 950 speaker_on = 1; 951 earpiece_on = 0; 952 break; 953 case TTY_MODE_OFF: 954 default: 955 /* force speaker on when in call and HDMI or S/PDIF is selected 956 * as voice DL audio cannot be routed there by ABE */ 957 if (adev->devices & 958 (AUDIO_DEVICE_OUT_AUX_DIGITAL | 959 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) 960 speaker_on = 1; 961 break; 962 } 963 } 964 965 dl1_on = headset_on | headphone_on | earpiece_on | bt_on; 966 967 /* Select front end */ 968 mixer_ctl_set_value(adev->mixer_ctls.mm_dl2, 0, speaker_on); 969 mixer_ctl_set_value(adev->mixer_ctls.vx_dl2, 0, 970 speaker_on && (adev->mode == AUDIO_MODE_IN_CALL)); 971 mixer_ctl_set_value(adev->mixer_ctls.mm_dl1, 0, dl1_on); 972 mixer_ctl_set_value(adev->mixer_ctls.vx_dl1, 0, 973 dl1_on && (adev->mode == AUDIO_MODE_IN_CALL)); 974 /* Select back end */ 975 mixer_ctl_set_value(adev->mixer_ctls.dl1_headset, 0, 976 headset_on | headphone_on | earpiece_on); 977 mixer_ctl_set_value(adev->mixer_ctls.dl1_bt, 0, bt_on); 978 mixer_ctl_set_value(adev->mixer_ctls.dl2_mono, 0, 979 (adev->mode != AUDIO_MODE_IN_CALL) && speaker_on); 980 mixer_ctl_set_value(adev->mixer_ctls.earpiece_enable, 0, earpiece_on); 981 982 /* select output stage */ 983 set_route_by_array(adev->mixer, hs_output, headset_on | headphone_on); 984 set_route_by_array(adev->mixer, hf_output, speaker_on); 985 986 set_eq_filter(adev); 987 set_output_volumes(adev, tty_volume); 988 989 /* Special case: select input path if in a call, otherwise 990 in_set_parameters is used to update the input route 991 todo: use sub mic for handsfree case */ 992 if (adev->mode == AUDIO_MODE_IN_CALL) { 993 if (bt_on) 994 set_route_by_array(adev->mixer, vx_ul_bt, bt_on); 995 else { 996 /* force tx path according to TTY mode when in call */ 997 switch(adev->tty_mode) { 998 case TTY_MODE_FULL: 999 case TTY_MODE_HCO: 1000 /* tx path from headset mic */ 1001 headphone_on = 0; 1002 headset_on = 1; 1003 speaker_on = 0; 1004 earpiece_on = 0; 1005 break; 1006 case TTY_MODE_VCO: 1007 /* tx path from device sub mic */ 1008 headphone_on = 0; 1009 headset_on = 0; 1010 speaker_on = 1; 1011 earpiece_on = 0; 1012 break; 1013 case TTY_MODE_OFF: 1014 default: 1015 break; 1016 } 1017 1018 if (headset_on || headphone_on || earpiece_on) 1019 set_route_by_array(adev->mixer, vx_ul_amic_left, 1); 1020 else if (speaker_on) 1021 set_route_by_array(adev->mixer, vx_ul_amic_right, 1); 1022 else 1023 set_route_by_array(adev->mixer, vx_ul_amic_left, 0); 1024 1025 mixer_ctl_set_enum_by_string(adev->mixer_ctls.left_capture, 1026 (earpiece_on || headphone_on) ? MIXER_MAIN_MIC : 1027 (headset_on ? MIXER_HS_MIC : "Off")); 1028 mixer_ctl_set_enum_by_string(adev->mixer_ctls.right_capture, 1029 speaker_on ? MIXER_SUB_MIC : "Off"); 1030 1031 set_input_volumes(adev, earpiece_on || headphone_on, 1032 headset_on, speaker_on); 1033 1034 /* enable sidetone mixer capture if needed */ 1035 sidetone_capture_on = earpiece_on && adev->device_is_toro; 1036 } 1037 1038 set_incall_device(adev); 1039 1040 /* Unmute VX_UL after the switch */ 1041 for (channel = 0; channel < 2; channel++) { 1042 mixer_ctl_set_value(adev->mixer_ctls.voice_ul_volume, 1043 channel, MIXER_ABE_GAIN_0DB); 1044 } 1045 } 1046 1047 mixer_ctl_set_value(adev->mixer_ctls.sidetone_capture, 0, sidetone_capture_on); 1048 } 1049 1050 static void select_input_device(struct tuna_audio_device *adev) 1051 { 1052 int headset_on = 0; 1053 int main_mic_on = 0; 1054 int sub_mic_on = 0; 1055 int bt_on = adev->devices & AUDIO_DEVICE_IN_ALL_SCO; 1056 1057 if (!bt_on) { 1058 if ((adev->mode != AUDIO_MODE_IN_CALL) && (adev->active_input != 0)) { 1059 /* sub mic is used for camcorder or VoIP on speaker phone */ 1060 sub_mic_on = (adev->active_input->source == AUDIO_SOURCE_CAMCORDER) || 1061 ((adev->devices & AUDIO_DEVICE_OUT_SPEAKER) && 1062 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION)); 1063 } 1064 if (!sub_mic_on) { 1065 headset_on = adev->devices & AUDIO_DEVICE_IN_WIRED_HEADSET; 1066 main_mic_on = adev->devices & AUDIO_DEVICE_IN_BUILTIN_MIC; 1067 } 1068 } 1069 1070 /* TODO: check how capture is possible during voice calls or if 1071 * both use cases are mutually exclusive. 1072 */ 1073 if (bt_on) 1074 set_route_by_array(adev->mixer, mm_ul2_bt, 1); 1075 else { 1076 /* Select front end */ 1077 if (main_mic_on || headset_on) 1078 set_route_by_array(adev->mixer, mm_ul2_amic_left, 1); 1079 else if (sub_mic_on) 1080 set_route_by_array(adev->mixer, mm_ul2_amic_right, 1); 1081 else 1082 set_route_by_array(adev->mixer, mm_ul2_amic_left, 0); 1083 1084 /* Select back end */ 1085 mixer_ctl_set_enum_by_string(adev->mixer_ctls.right_capture, 1086 sub_mic_on ? MIXER_SUB_MIC : "Off"); 1087 mixer_ctl_set_enum_by_string(adev->mixer_ctls.left_capture, 1088 main_mic_on ? MIXER_MAIN_MIC : 1089 (headset_on ? MIXER_HS_MIC : "Off")); 1090 } 1091 1092 set_input_volumes(adev, main_mic_on, headset_on, sub_mic_on); 1093 } 1094 1095 /* must be called with hw device and output stream mutexes locked */ 1096 static int start_output_stream(struct tuna_stream_out *out) 1097 { 1098 struct tuna_audio_device *adev = out->dev; 1099 unsigned int card = CARD_TUNA_DEFAULT; 1100 unsigned int port = PORT_MM; 1101 1102 adev->active_output = out; 1103 1104 if (adev->mode != AUDIO_MODE_IN_CALL) { 1105 /* FIXME: only works if only one output can be active at a time */ 1106 select_output_device(adev); 1107 } 1108 /* S/PDIF takes priority over HDMI audio. In the case of multiple 1109 * devices, this will cause use of S/PDIF or HDMI only */ 1110 out->config.rate = MM_FULL_POWER_SAMPLING_RATE; 1111 if (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) 1112 port = PORT_SPDIF; 1113 else if(adev->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1114 card = CARD_OMAP4_HDMI; 1115 port = PORT_HDMI; 1116 out->config.rate = MM_LOW_POWER_SAMPLING_RATE; 1117 } 1118 /* default to low power: will be corrected in out_write if necessary before first write to 1119 * tinyalsa. 1120 */ 1121 out->write_threshold = PLAYBACK_LONG_PERIOD_COUNT * LONG_PERIOD_SIZE; 1122 out->config.start_threshold = SHORT_PERIOD_SIZE * 2; 1123 out->config.avail_min = LONG_PERIOD_SIZE; 1124 out->low_power = 1; 1125 1126 out->pcm = pcm_open(card, port, PCM_OUT | PCM_MMAP | PCM_NOIRQ, &out->config); 1127 1128 if (!pcm_is_ready(out->pcm)) { 1129 LOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm)); 1130 pcm_close(out->pcm); 1131 adev->active_output = NULL; 1132 return -ENOMEM; 1133 } 1134 1135 if (adev->echo_reference != NULL) 1136 out->echo_reference = adev->echo_reference; 1137 1138 out->resampler->reset(out->resampler); 1139 1140 return 0; 1141 } 1142 1143 static int check_input_parameters(uint32_t sample_rate, int format, int channel_count) 1144 { 1145 if (format != AUDIO_FORMAT_PCM_16_BIT) 1146 return -EINVAL; 1147 1148 if ((channel_count < 1) || (channel_count > 2)) 1149 return -EINVAL; 1150 1151 switch(sample_rate) { 1152 case 8000: 1153 case 11025: 1154 case 16000: 1155 case 22050: 1156 case 24000: 1157 case 32000: 1158 case 44100: 1159 case 48000: 1160 break; 1161 default: 1162 return -EINVAL; 1163 } 1164 1165 return 0; 1166 } 1167 1168 static size_t get_input_buffer_size(uint32_t sample_rate, int format, int channel_count) 1169 { 1170 size_t size; 1171 size_t device_rate; 1172 1173 if (check_input_parameters(sample_rate, format, channel_count) != 0) 1174 return 0; 1175 1176 /* take resampling into account and return the closest majoring 1177 multiple of 16 frames, as audioflinger expects audio buffers to 1178 be a multiple of 16 frames */ 1179 size = (pcm_config_mm_ul.period_size * sample_rate) / pcm_config_mm_ul.rate; 1180 size = ((size + 15) / 16) * 16; 1181 1182 return size * channel_count * sizeof(short); 1183 } 1184 1185 static void add_echo_reference(struct tuna_stream_out *out, 1186 struct echo_reference_itfe *reference) 1187 { 1188 pthread_mutex_lock(&out->lock); 1189 out->echo_reference = reference; 1190 pthread_mutex_unlock(&out->lock); 1191 } 1192 1193 static void remove_echo_reference(struct tuna_stream_out *out, 1194 struct echo_reference_itfe *reference) 1195 { 1196 pthread_mutex_lock(&out->lock); 1197 if (out->echo_reference == reference) { 1198 /* stop writing to echo reference */ 1199 reference->write(reference, NULL); 1200 out->echo_reference = NULL; 1201 } 1202 pthread_mutex_unlock(&out->lock); 1203 } 1204 1205 static void put_echo_reference(struct tuna_audio_device *adev, 1206 struct echo_reference_itfe *reference) 1207 { 1208 if (adev->echo_reference != NULL && 1209 reference == adev->echo_reference) { 1210 if (adev->active_output != NULL) 1211 remove_echo_reference(adev->active_output, reference); 1212 release_echo_reference(reference); 1213 adev->echo_reference = NULL; 1214 } 1215 } 1216 1217 static struct echo_reference_itfe *get_echo_reference(struct tuna_audio_device *adev, 1218 audio_format_t format, 1219 uint32_t channel_count, 1220 uint32_t sampling_rate) 1221 { 1222 put_echo_reference(adev, adev->echo_reference); 1223 if (adev->active_output != NULL) { 1224 struct audio_stream *stream = &adev->active_output->stream.common; 1225 uint32_t wr_channel_count = popcount(stream->get_channels(stream)); 1226 uint32_t wr_sampling_rate = stream->get_sample_rate(stream); 1227 1228 int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT, 1229 channel_count, 1230 sampling_rate, 1231 AUDIO_FORMAT_PCM_16_BIT, 1232 wr_channel_count, 1233 wr_sampling_rate, 1234 &adev->echo_reference); 1235 if (status == 0) 1236 add_echo_reference(adev->active_output, adev->echo_reference); 1237 } 1238 return adev->echo_reference; 1239 } 1240 1241 static int get_playback_delay(struct tuna_stream_out *out, 1242 size_t frames, 1243 struct echo_reference_buffer *buffer) 1244 { 1245 size_t kernel_frames; 1246 int status; 1247 1248 status = pcm_get_htimestamp(out->pcm, &kernel_frames, &buffer->time_stamp); 1249 if (status < 0) { 1250 buffer->time_stamp.tv_sec = 0; 1251 buffer->time_stamp.tv_nsec = 0; 1252 buffer->delay_ns = 0; 1253 LOGV("get_playback_delay(): pcm_get_htimestamp error," 1254 "setting playbackTimestamp to 0"); 1255 return status; 1256 } 1257 1258 kernel_frames = pcm_get_buffer_size(out->pcm) - kernel_frames; 1259 1260 /* adjust render time stamp with delay added by current driver buffer. 1261 * Add the duration of current frame as we want the render time of the last 1262 * sample being written. */ 1263 buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/ 1264 MM_FULL_POWER_SAMPLING_RATE); 1265 1266 return 0; 1267 } 1268 1269 static uint32_t out_get_sample_rate(const struct audio_stream *stream) 1270 { 1271 return DEFAULT_OUT_SAMPLING_RATE; 1272 } 1273 1274 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1275 { 1276 return 0; 1277 } 1278 1279 static size_t out_get_buffer_size(const struct audio_stream *stream) 1280 { 1281 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1282 1283 /* take resampling into account and return the closest majoring 1284 multiple of 16 frames, as audioflinger expects audio buffers to 1285 be a multiple of 16 frames */ 1286 size_t size = (SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) / out->config.rate; 1287 size = ((size + 15) / 16) * 16; 1288 return size * audio_stream_frame_size((struct audio_stream *)stream); 1289 } 1290 1291 static uint32_t out_get_channels(const struct audio_stream *stream) 1292 { 1293 return AUDIO_CHANNEL_OUT_STEREO; 1294 } 1295 1296 static int out_get_format(const struct audio_stream *stream) 1297 { 1298 return AUDIO_FORMAT_PCM_16_BIT; 1299 } 1300 1301 static int out_set_format(struct audio_stream *stream, int format) 1302 { 1303 return 0; 1304 } 1305 1306 /* must be called with hw device and output stream mutexes locked */ 1307 static int do_output_standby(struct tuna_stream_out *out) 1308 { 1309 struct tuna_audio_device *adev = out->dev; 1310 1311 if (!out->standby) { 1312 pcm_close(out->pcm); 1313 out->pcm = NULL; 1314 1315 adev->active_output = 0; 1316 1317 /* if in call, don't turn off the output stage. This will 1318 be done when the call is ended */ 1319 if (adev->mode != AUDIO_MODE_IN_CALL) { 1320 /* FIXME: only works if only one output can be active at a time */ 1321 set_route_by_array(adev->mixer, hs_output, 0); 1322 set_route_by_array(adev->mixer, hf_output, 0); 1323 } 1324 1325 /* stop writing to echo reference */ 1326 if (out->echo_reference != NULL) { 1327 out->echo_reference->write(out->echo_reference, NULL); 1328 out->echo_reference = NULL; 1329 } 1330 1331 out->standby = 1; 1332 } 1333 return 0; 1334 } 1335 1336 static int out_standby(struct audio_stream *stream) 1337 { 1338 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1339 int status; 1340 1341 pthread_mutex_lock(&out->dev->lock); 1342 pthread_mutex_lock(&out->lock); 1343 status = do_output_standby(out); 1344 pthread_mutex_unlock(&out->lock); 1345 pthread_mutex_unlock(&out->dev->lock); 1346 return status; 1347 } 1348 1349 static int out_dump(const struct audio_stream *stream, int fd) 1350 { 1351 return 0; 1352 } 1353 1354 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 1355 { 1356 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1357 struct tuna_audio_device *adev = out->dev; 1358 struct tuna_stream_in *in; 1359 struct str_parms *parms; 1360 char *str; 1361 char value[32]; 1362 int ret, val = 0; 1363 bool force_input_standby = false; 1364 1365 parms = str_parms_create_str(kvpairs); 1366 1367 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1368 if (ret >= 0) { 1369 val = atoi(value); 1370 pthread_mutex_lock(&adev->lock); 1371 pthread_mutex_lock(&out->lock); 1372 if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) { 1373 if (out == adev->active_output) { 1374 /* a change in output device may change the microphone selection */ 1375 if (adev->active_input && 1376 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 1377 force_input_standby = true; 1378 } 1379 /* force standby if moving to/from HDMI */ 1380 if (((val & AUDIO_DEVICE_OUT_AUX_DIGITAL) ^ 1381 (adev->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) || 1382 ((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^ 1383 (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET))) 1384 do_output_standby(out); 1385 } 1386 adev->devices &= ~AUDIO_DEVICE_OUT_ALL; 1387 adev->devices |= val; 1388 select_output_device(adev); 1389 } 1390 pthread_mutex_unlock(&out->lock); 1391 if (force_input_standby) { 1392 in = adev->active_input; 1393 pthread_mutex_lock(&in->lock); 1394 do_input_standby(in); 1395 pthread_mutex_unlock(&in->lock); 1396 } 1397 pthread_mutex_unlock(&adev->lock); 1398 } 1399 1400 str_parms_destroy(parms); 1401 return ret; 1402 } 1403 1404 static char * out_get_parameters(const struct audio_stream *stream, const char *keys) 1405 { 1406 return strdup(""); 1407 } 1408 1409 static uint32_t out_get_latency(const struct audio_stream_out *stream) 1410 { 1411 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1412 1413 return (SHORT_PERIOD_SIZE * PLAYBACK_SHORT_PERIOD_COUNT * 1000) / out->config.rate; 1414 } 1415 1416 static int out_set_volume(struct audio_stream_out *stream, float left, 1417 float right) 1418 { 1419 return -ENOSYS; 1420 } 1421 1422 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, 1423 size_t bytes) 1424 { 1425 int ret; 1426 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 1427 struct tuna_audio_device *adev = out->dev; 1428 size_t frame_size = audio_stream_frame_size(&out->stream.common); 1429 size_t in_frames = bytes / frame_size; 1430 size_t out_frames = RESAMPLER_BUFFER_SIZE / frame_size; 1431 bool force_input_standby = false; 1432 struct tuna_stream_in *in; 1433 bool low_power; 1434 int kernel_frames; 1435 void *buf; 1436 1437 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 1438 * on the output stream mutex - e.g. executing select_mode() while holding the hw device 1439 * mutex 1440 */ 1441 pthread_mutex_lock(&adev->lock); 1442 pthread_mutex_lock(&out->lock); 1443 if (out->standby) { 1444 ret = start_output_stream(out); 1445 if (ret != 0) { 1446 pthread_mutex_unlock(&adev->lock); 1447 goto exit; 1448 } 1449 out->standby = 0; 1450 /* a change in output device may change the microphone selection */ 1451 if (adev->active_input && 1452 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) 1453 force_input_standby = true; 1454 } 1455 low_power = adev->low_power && !adev->active_input; 1456 pthread_mutex_unlock(&adev->lock); 1457 1458 if (low_power != out->low_power) { 1459 if (low_power) { 1460 out->write_threshold = LONG_PERIOD_SIZE * PLAYBACK_LONG_PERIOD_COUNT; 1461 out->config.avail_min = LONG_PERIOD_SIZE; 1462 } else { 1463 out->write_threshold = SHORT_PERIOD_SIZE * PLAYBACK_SHORT_PERIOD_COUNT; 1464 out->config.avail_min = SHORT_PERIOD_SIZE; 1465 } 1466 pcm_set_avail_min(out->pcm, out->config.avail_min); 1467 out->low_power = low_power; 1468 } 1469 1470 /* only use resampler if required */ 1471 if (out->config.rate != DEFAULT_OUT_SAMPLING_RATE) { 1472 out->resampler->resample_from_input(out->resampler, 1473 (int16_t *)buffer, 1474 &in_frames, 1475 (int16_t *)out->buffer, 1476 &out_frames); 1477 buf = out->buffer; 1478 } else { 1479 out_frames = in_frames; 1480 buf = (void *)buffer; 1481 } 1482 if (out->echo_reference != NULL) { 1483 struct echo_reference_buffer b; 1484 b.raw = (void *)buffer; 1485 b.frame_count = in_frames; 1486 1487 get_playback_delay(out, out_frames, &b); 1488 out->echo_reference->write(out->echo_reference, &b); 1489 } 1490 1491 /* do not allow more than out->write_threshold frames in kernel pcm driver buffer */ 1492 do { 1493 struct timespec time_stamp; 1494 1495 if (pcm_get_htimestamp(out->pcm, (unsigned int *)&kernel_frames, &time_stamp) < 0) 1496 break; 1497 kernel_frames = pcm_get_buffer_size(out->pcm) - kernel_frames; 1498 1499 if (kernel_frames > out->write_threshold) { 1500 unsigned long time = (unsigned long) 1501 (((int64_t)(kernel_frames - out->write_threshold) * 1000000) / 1502 MM_FULL_POWER_SAMPLING_RATE); 1503 if (time < MIN_WRITE_SLEEP_US) 1504 time = MIN_WRITE_SLEEP_US; 1505 usleep(time); 1506 } 1507 } while (kernel_frames > out->write_threshold); 1508 1509 ret = pcm_mmap_write(out->pcm, (void *)buf, out_frames * frame_size); 1510 1511 exit: 1512 pthread_mutex_unlock(&out->lock); 1513 1514 if (ret != 0) { 1515 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / 1516 out_get_sample_rate(&stream->common)); 1517 } 1518 1519 if (force_input_standby) { 1520 pthread_mutex_lock(&adev->lock); 1521 if (adev->active_input) { 1522 in = adev->active_input; 1523 pthread_mutex_lock(&in->lock); 1524 do_input_standby(in); 1525 pthread_mutex_unlock(&in->lock); 1526 } 1527 pthread_mutex_unlock(&adev->lock); 1528 } 1529 1530 return bytes; 1531 } 1532 1533 static int out_get_render_position(const struct audio_stream_out *stream, 1534 uint32_t *dsp_frames) 1535 { 1536 return -EINVAL; 1537 } 1538 1539 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1540 { 1541 return 0; 1542 } 1543 1544 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1545 { 1546 return 0; 1547 } 1548 1549 /** audio_stream_in implementation **/ 1550 1551 /* must be called with hw device and input stream mutexes locked */ 1552 static int start_input_stream(struct tuna_stream_in *in) 1553 { 1554 int ret = 0; 1555 struct tuna_audio_device *adev = in->dev; 1556 1557 adev->active_input = in; 1558 1559 if (adev->mode != AUDIO_MODE_IN_CALL) { 1560 adev->devices &= ~AUDIO_DEVICE_IN_ALL; 1561 adev->devices |= in->device; 1562 select_input_device(adev); 1563 } 1564 1565 if (in->need_echo_reference && in->echo_reference == NULL) 1566 in->echo_reference = get_echo_reference(adev, 1567 AUDIO_FORMAT_PCM_16_BIT, 1568 in->config.channels, 1569 in->requested_rate); 1570 1571 /* this assumes routing is done previously */ 1572 in->pcm = pcm_open(0, PORT_MM2_UL, PCM_IN, &in->config); 1573 if (!pcm_is_ready(in->pcm)) { 1574 LOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm)); 1575 pcm_close(in->pcm); 1576 adev->active_input = NULL; 1577 return -ENOMEM; 1578 } 1579 1580 /* if no supported sample rate is available, use the resampler */ 1581 if (in->resampler) { 1582 in->resampler->reset(in->resampler); 1583 in->frames_in = 0; 1584 } 1585 return 0; 1586 } 1587 1588 static uint32_t in_get_sample_rate(const struct audio_stream *stream) 1589 { 1590 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 1591 1592 return in->requested_rate; 1593 } 1594 1595 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1596 { 1597 return 0; 1598 } 1599 1600 static size_t in_get_buffer_size(const struct audio_stream *stream) 1601 { 1602 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 1603 1604 return get_input_buffer_size(in->requested_rate, 1605 AUDIO_FORMAT_PCM_16_BIT, 1606 in->config.channels); 1607 } 1608 1609 static uint32_t in_get_channels(const struct audio_stream *stream) 1610 { 1611 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 1612 1613 if (in->config.channels == 1) { 1614 return AUDIO_CHANNEL_IN_MONO; 1615 } else { 1616 return AUDIO_CHANNEL_IN_STEREO; 1617 } 1618 } 1619 1620 static int in_get_format(const struct audio_stream *stream) 1621 { 1622 return AUDIO_FORMAT_PCM_16_BIT; 1623 } 1624 1625 static int in_set_format(struct audio_stream *stream, int format) 1626 { 1627 return 0; 1628 } 1629 1630 /* must be called with hw device and input stream mutexes locked */ 1631 static int do_input_standby(struct tuna_stream_in *in) 1632 { 1633 struct tuna_audio_device *adev = in->dev; 1634 1635 if (!in->standby) { 1636 pcm_close(in->pcm); 1637 in->pcm = NULL; 1638 1639 adev->active_input = 0; 1640 if (adev->mode != AUDIO_MODE_IN_CALL) { 1641 adev->devices &= ~AUDIO_DEVICE_IN_ALL; 1642 select_input_device(adev); 1643 } 1644 1645 if (in->echo_reference != NULL) { 1646 /* stop reading from echo reference */ 1647 in->echo_reference->read(in->echo_reference, NULL); 1648 put_echo_reference(adev, in->echo_reference); 1649 in->echo_reference = NULL; 1650 } 1651 1652 in->standby = 1; 1653 } 1654 return 0; 1655 } 1656 1657 static int in_standby(struct audio_stream *stream) 1658 { 1659 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 1660 int status; 1661 1662 pthread_mutex_lock(&in->dev->lock); 1663 pthread_mutex_lock(&in->lock); 1664 status = do_input_standby(in); 1665 pthread_mutex_unlock(&in->lock); 1666 pthread_mutex_unlock(&in->dev->lock); 1667 return status; 1668 } 1669 1670 static int in_dump(const struct audio_stream *stream, int fd) 1671 { 1672 return 0; 1673 } 1674 1675 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 1676 { 1677 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 1678 struct tuna_audio_device *adev = in->dev; 1679 struct str_parms *parms; 1680 char *str; 1681 char value[32]; 1682 int ret, val = 0; 1683 bool do_standby = false; 1684 1685 parms = str_parms_create_str(kvpairs); 1686 1687 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 1688 1689 pthread_mutex_lock(&adev->lock); 1690 pthread_mutex_lock(&in->lock); 1691 if (ret >= 0) { 1692 val = atoi(value); 1693 /* no audio source uses val == 0 */ 1694 if ((in->source != val) && (val != 0)) { 1695 in->source = val; 1696 do_standby = true; 1697 } 1698 } 1699 1700 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1701 if (ret >= 0) { 1702 val = atoi(value); 1703 if ((in->device != val) && (val != 0)) { 1704 in->device = val; 1705 do_standby = true; 1706 } 1707 } 1708 1709 if (do_standby) 1710 do_input_standby(in); 1711 pthread_mutex_unlock(&in->lock); 1712 pthread_mutex_unlock(&adev->lock); 1713 1714 str_parms_destroy(parms); 1715 return ret; 1716 } 1717 1718 static char * in_get_parameters(const struct audio_stream *stream, 1719 const char *keys) 1720 { 1721 return strdup(""); 1722 } 1723 1724 static int in_set_gain(struct audio_stream_in *stream, float gain) 1725 { 1726 return 0; 1727 } 1728 1729 static void get_capture_delay(struct tuna_stream_in *in, 1730 size_t frames, 1731 struct echo_reference_buffer *buffer) 1732 { 1733 1734 /* read frames available in kernel driver buffer */ 1735 size_t kernel_frames; 1736 struct timespec tstamp; 1737 long buf_delay; 1738 long rsmp_delay; 1739 long kernel_delay; 1740 long delay_ns; 1741 1742 if (pcm_get_htimestamp(in->pcm, &kernel_frames, &tstamp) < 0) { 1743 buffer->time_stamp.tv_sec = 0; 1744 buffer->time_stamp.tv_nsec = 0; 1745 buffer->delay_ns = 0; 1746 LOGW("read get_capture_delay(): pcm_htimestamp error"); 1747 return; 1748 } 1749 1750 /* read frames available in audio HAL input buffer 1751 * add number of frames being read as we want the capture time of first sample 1752 * in current buffer */ 1753 buf_delay = (long)(((int64_t)(in->frames_in + in->proc_frames_in) * 1000000000) 1754 / in->config.rate); 1755 /* add delay introduced by resampler */ 1756 rsmp_delay = 0; 1757 if (in->resampler) { 1758 rsmp_delay = in->resampler->delay_ns(in->resampler); 1759 } 1760 1761 kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / in->config.rate); 1762 1763 delay_ns = kernel_delay + buf_delay + rsmp_delay; 1764 1765 buffer->time_stamp = tstamp; 1766 buffer->delay_ns = delay_ns; 1767 LOGV("get_capture_delay time_stamp = [%ld].[%ld], delay_ns: [%d]," 1768 " kernel_delay:[%ld], buf_delay:[%ld], rsmp_delay:[%ld], kernel_frames:[%d], " 1769 "in->frames_in:[%d], in->proc_frames_in:[%d], frames:[%d]", 1770 buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, buffer->delay_ns, 1771 kernel_delay, buf_delay, rsmp_delay, kernel_frames, 1772 in->frames_in, in->proc_frames_in, frames); 1773 1774 } 1775 1776 static int32_t update_echo_reference(struct tuna_stream_in *in, size_t frames) 1777 { 1778 struct echo_reference_buffer b; 1779 b.delay_ns = 0; 1780 1781 LOGV("update_echo_reference, frames = [%d], in->ref_frames_in = [%d], " 1782 "b.frame_count = [%d]", 1783 frames, in->ref_frames_in, frames - in->ref_frames_in); 1784 if (in->ref_frames_in < frames) { 1785 if (in->ref_buf_size < frames) { 1786 in->ref_buf_size = frames; 1787 in->ref_buf = (int16_t *)realloc(in->ref_buf, 1788 in->ref_buf_size * 1789 in->config.channels * sizeof(int16_t)); 1790 } 1791 1792 b.frame_count = frames - in->ref_frames_in; 1793 b.raw = (void *)(in->ref_buf + in->ref_frames_in * in->config.channels); 1794 1795 get_capture_delay(in, frames, &b); 1796 1797 if (in->echo_reference->read(in->echo_reference, &b) == 0) 1798 { 1799 in->ref_frames_in += b.frame_count; 1800 LOGV("update_echo_reference: in->ref_frames_in:[%d], " 1801 "in->ref_buf_size:[%d], frames:[%d], b.frame_count:[%d]", 1802 in->ref_frames_in, in->ref_buf_size, frames, b.frame_count); 1803 } 1804 } else 1805 LOGW("update_echo_reference: NOT enough frames to read ref buffer"); 1806 return b.delay_ns; 1807 } 1808 1809 static int set_preprocessor_param(effect_handle_t handle, 1810 effect_param_t *param) 1811 { 1812 uint32_t size = sizeof(int); 1813 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + 1814 param->vsize; 1815 1816 int status = (*handle)->command(handle, 1817 EFFECT_CMD_SET_PARAM, 1818 sizeof (effect_param_t) + psize, 1819 param, 1820 &size, 1821 ¶m->status); 1822 if (status == 0) 1823 status = param->status; 1824 1825 return status; 1826 } 1827 1828 static int set_preprocessor_echo_delay(effect_handle_t handle, 1829 int32_t delay_us) 1830 { 1831 uint32_t buf[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; 1832 effect_param_t *param = (effect_param_t *)buf; 1833 1834 param->psize = sizeof(uint32_t); 1835 param->vsize = sizeof(uint32_t); 1836 *(uint32_t *)param->data = AEC_PARAM_ECHO_DELAY; 1837 *((int32_t *)param->data + 1) = delay_us; 1838 1839 return set_preprocessor_param(handle, param); 1840 } 1841 1842 static void push_echo_reference(struct tuna_stream_in *in, size_t frames) 1843 { 1844 /* read frames from echo reference buffer and update echo delay 1845 * in->ref_frames_in is updated with frames available in in->ref_buf */ 1846 int32_t delay_us = update_echo_reference(in, frames)/1000; 1847 int i; 1848 audio_buffer_t buf; 1849 1850 if (in->ref_frames_in < frames) 1851 frames = in->ref_frames_in; 1852 1853 buf.frameCount = frames; 1854 buf.raw = in->ref_buf; 1855 1856 for (i = 0; i < in->num_preprocessors; i++) { 1857 if ((*in->preprocessors[i])->process_reverse == NULL) 1858 continue; 1859 1860 (*in->preprocessors[i])->process_reverse(in->preprocessors[i], 1861 &buf, 1862 NULL); 1863 set_preprocessor_echo_delay(in->preprocessors[i], delay_us); 1864 } 1865 1866 in->ref_frames_in -= buf.frameCount; 1867 if (in->ref_frames_in) { 1868 memcpy(in->ref_buf, 1869 in->ref_buf + buf.frameCount * in->config.channels, 1870 in->ref_frames_in * in->config.channels * sizeof(int16_t)); 1871 } 1872 } 1873 1874 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider, 1875 struct resampler_buffer* buffer) 1876 { 1877 struct tuna_stream_in *in; 1878 1879 if (buffer_provider == NULL || buffer == NULL) 1880 return -EINVAL; 1881 1882 in = (struct tuna_stream_in *)((char *)buffer_provider - 1883 offsetof(struct tuna_stream_in, buf_provider)); 1884 1885 if (in->pcm == NULL) { 1886 buffer->raw = NULL; 1887 buffer->frame_count = 0; 1888 in->read_status = -ENODEV; 1889 return -ENODEV; 1890 } 1891 1892 if (in->frames_in == 0) { 1893 in->read_status = pcm_read(in->pcm, 1894 (void*)in->buffer, 1895 in->config.period_size * 1896 audio_stream_frame_size(&in->stream.common)); 1897 if (in->read_status != 0) { 1898 LOGE("get_next_buffer() pcm_read error %d", in->read_status); 1899 buffer->raw = NULL; 1900 buffer->frame_count = 0; 1901 return in->read_status; 1902 } 1903 in->frames_in = in->config.period_size; 1904 } 1905 1906 buffer->frame_count = (buffer->frame_count > in->frames_in) ? 1907 in->frames_in : buffer->frame_count; 1908 buffer->i16 = in->buffer + (in->config.period_size - in->frames_in) * 1909 in->config.channels; 1910 1911 return in->read_status; 1912 1913 } 1914 1915 static void release_buffer(struct resampler_buffer_provider *buffer_provider, 1916 struct resampler_buffer* buffer) 1917 { 1918 struct tuna_stream_in *in; 1919 1920 if (buffer_provider == NULL || buffer == NULL) 1921 return; 1922 1923 in = (struct tuna_stream_in *)((char *)buffer_provider - 1924 offsetof(struct tuna_stream_in, buf_provider)); 1925 1926 in->frames_in -= buffer->frame_count; 1927 } 1928 1929 /* read_frames() reads frames from kernel driver, down samples to capture rate 1930 * if necessary and output the number of frames requested to the buffer specified */ 1931 static ssize_t read_frames(struct tuna_stream_in *in, void *buffer, ssize_t frames) 1932 { 1933 ssize_t frames_wr = 0; 1934 1935 while (frames_wr < frames) { 1936 size_t frames_rd = frames - frames_wr; 1937 if (in->resampler != NULL) { 1938 in->resampler->resample_from_provider(in->resampler, 1939 (int16_t *)((char *)buffer + 1940 frames_wr * audio_stream_frame_size(&in->stream.common)), 1941 &frames_rd); 1942 } else { 1943 struct resampler_buffer buf = { 1944 { raw : NULL, }, 1945 frame_count : frames_rd, 1946 }; 1947 get_next_buffer(&in->buf_provider, &buf); 1948 if (buf.raw != NULL) { 1949 memcpy((char *)buffer + 1950 frames_wr * audio_stream_frame_size(&in->stream.common), 1951 buf.raw, 1952 buf.frame_count * audio_stream_frame_size(&in->stream.common)); 1953 frames_rd = buf.frame_count; 1954 } 1955 release_buffer(&in->buf_provider, &buf); 1956 } 1957 /* in->read_status is updated by getNextBuffer() also called by 1958 * in->resampler->resample_from_provider() */ 1959 if (in->read_status != 0) 1960 return in->read_status; 1961 1962 frames_wr += frames_rd; 1963 } 1964 return frames_wr; 1965 } 1966 1967 /* process_frames() reads frames from kernel driver (via read_frames()), 1968 * calls the active audio pre processings and output the number of frames requested 1969 * to the buffer specified */ 1970 static ssize_t process_frames(struct tuna_stream_in *in, void* buffer, ssize_t frames) 1971 { 1972 ssize_t frames_wr = 0; 1973 audio_buffer_t in_buf; 1974 audio_buffer_t out_buf; 1975 int i; 1976 1977 while (frames_wr < frames) { 1978 /* first reload enough frames at the end of process input buffer */ 1979 if (in->proc_frames_in < (size_t)frames) { 1980 ssize_t frames_rd; 1981 1982 if (in->proc_buf_size < (size_t)frames) { 1983 in->proc_buf_size = (size_t)frames; 1984 in->proc_buf = (int16_t *)realloc(in->proc_buf, 1985 in->proc_buf_size * 1986 in->config.channels * sizeof(int16_t)); 1987 LOGV("process_frames(): in->proc_buf %p size extended to %d frames", 1988 in->proc_buf, in->proc_buf_size); 1989 } 1990 frames_rd = read_frames(in, 1991 in->proc_buf + 1992 in->proc_frames_in * in->config.channels, 1993 frames - in->proc_frames_in); 1994 if (frames_rd < 0) { 1995 frames_wr = frames_rd; 1996 break; 1997 } 1998 in->proc_frames_in += frames_rd; 1999 } 2000 2001 if (in->echo_reference != NULL) 2002 push_echo_reference(in, in->proc_frames_in); 2003 2004 /* in_buf.frameCount and out_buf.frameCount indicate respectively 2005 * the maximum number of frames to be consumed and produced by process() */ 2006 in_buf.frameCount = in->proc_frames_in; 2007 in_buf.s16 = in->proc_buf; 2008 out_buf.frameCount = frames - frames_wr; 2009 out_buf.s16 = (int16_t *)buffer + frames_wr * in->config.channels; 2010 2011 for (i = 0; i < in->num_preprocessors; i++) 2012 (*in->preprocessors[i])->process(in->preprocessors[i], 2013 &in_buf, 2014 &out_buf); 2015 2016 /* process() has updated the number of frames consumed and produced in 2017 * in_buf.frameCount and out_buf.frameCount respectively 2018 * move remaining frames to the beginning of in->proc_buf */ 2019 in->proc_frames_in -= in_buf.frameCount; 2020 if (in->proc_frames_in) { 2021 memcpy(in->proc_buf, 2022 in->proc_buf + in_buf.frameCount * in->config.channels, 2023 in->proc_frames_in * in->config.channels * sizeof(int16_t)); 2024 } 2025 2026 /* if not enough frames were passed to process(), read more and retry. */ 2027 if (out_buf.frameCount == 0) 2028 continue; 2029 2030 frames_wr += out_buf.frameCount; 2031 } 2032 return frames_wr; 2033 } 2034 2035 static ssize_t in_read(struct audio_stream_in *stream, void* buffer, 2036 size_t bytes) 2037 { 2038 int ret = 0; 2039 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2040 struct tuna_audio_device *adev = in->dev; 2041 size_t frames_rq = bytes / audio_stream_frame_size(&stream->common); 2042 2043 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 2044 * on the input stream mutex - e.g. executing select_mode() while holding the hw device 2045 * mutex 2046 */ 2047 pthread_mutex_lock(&adev->lock); 2048 pthread_mutex_lock(&in->lock); 2049 if (in->standby) { 2050 ret = start_input_stream(in); 2051 if (ret == 0) 2052 in->standby = 0; 2053 } 2054 pthread_mutex_unlock(&adev->lock); 2055 2056 if (ret < 0) 2057 goto exit; 2058 2059 if (in->num_preprocessors != 0) 2060 ret = process_frames(in, buffer, frames_rq); 2061 else if (in->resampler != NULL) 2062 ret = read_frames(in, buffer, frames_rq); 2063 else 2064 ret = pcm_read(in->pcm, buffer, bytes); 2065 2066 if (ret > 0) 2067 ret = 0; 2068 2069 if (ret == 0 && adev->mic_mute) 2070 memset(buffer, 0, bytes); 2071 2072 exit: 2073 if (ret < 0) 2074 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / 2075 in_get_sample_rate(&stream->common)); 2076 2077 pthread_mutex_unlock(&in->lock); 2078 return bytes; 2079 } 2080 2081 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 2082 { 2083 return 0; 2084 } 2085 2086 static int in_add_audio_effect(const struct audio_stream *stream, 2087 effect_handle_t effect) 2088 { 2089 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2090 int status; 2091 effect_descriptor_t desc; 2092 2093 pthread_mutex_lock(&in->dev->lock); 2094 pthread_mutex_lock(&in->lock); 2095 if (in->num_preprocessors >= MAX_PREPROCESSORS) { 2096 status = -ENOSYS; 2097 goto exit; 2098 } 2099 2100 status = (*effect)->get_descriptor(effect, &desc); 2101 if (status != 0) 2102 goto exit; 2103 2104 in->preprocessors[in->num_preprocessors++] = effect; 2105 2106 if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) { 2107 in->need_echo_reference = true; 2108 do_input_standby(in); 2109 } 2110 2111 exit: 2112 2113 pthread_mutex_unlock(&in->lock); 2114 pthread_mutex_unlock(&in->dev->lock); 2115 return status; 2116 } 2117 2118 static int in_remove_audio_effect(const struct audio_stream *stream, 2119 effect_handle_t effect) 2120 { 2121 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2122 int i; 2123 int status = -EINVAL; 2124 bool found = false; 2125 effect_descriptor_t desc; 2126 2127 pthread_mutex_lock(&in->dev->lock); 2128 pthread_mutex_lock(&in->lock); 2129 if (in->num_preprocessors <= 0) { 2130 status = -ENOSYS; 2131 goto exit; 2132 } 2133 2134 for (i = 0; i < in->num_preprocessors; i++) { 2135 if (found) { 2136 in->preprocessors[i - 1] = in->preprocessors[i]; 2137 continue; 2138 } 2139 if (in->preprocessors[i] == effect) { 2140 in->preprocessors[i] = NULL; 2141 status = 0; 2142 found = true; 2143 } 2144 } 2145 2146 if (status != 0) 2147 goto exit; 2148 2149 in->num_preprocessors--; 2150 2151 status = (*effect)->get_descriptor(effect, &desc); 2152 if (status != 0) 2153 goto exit; 2154 if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) { 2155 in->need_echo_reference = false; 2156 do_input_standby(in); 2157 } 2158 2159 exit: 2160 2161 pthread_mutex_unlock(&in->lock); 2162 pthread_mutex_unlock(&in->dev->lock); 2163 return status; 2164 } 2165 2166 2167 static int adev_open_output_stream(struct audio_hw_device *dev, 2168 uint32_t devices, int *format, 2169 uint32_t *channels, uint32_t *sample_rate, 2170 struct audio_stream_out **stream_out) 2171 { 2172 struct tuna_audio_device *ladev = (struct tuna_audio_device *)dev; 2173 struct tuna_stream_out *out; 2174 int ret; 2175 2176 out = (struct tuna_stream_out *)calloc(1, sizeof(struct tuna_stream_out)); 2177 if (!out) 2178 return -ENOMEM; 2179 2180 ret = create_resampler(DEFAULT_OUT_SAMPLING_RATE, 2181 MM_FULL_POWER_SAMPLING_RATE, 2182 2, 2183 RESAMPLER_QUALITY_DEFAULT, 2184 NULL, 2185 &out->resampler); 2186 if (ret != 0) 2187 goto err_open; 2188 out->buffer = malloc(RESAMPLER_BUFFER_SIZE); /* todo: allow for reallocing */ 2189 2190 out->stream.common.get_sample_rate = out_get_sample_rate; 2191 out->stream.common.set_sample_rate = out_set_sample_rate; 2192 out->stream.common.get_buffer_size = out_get_buffer_size; 2193 out->stream.common.get_channels = out_get_channels; 2194 out->stream.common.get_format = out_get_format; 2195 out->stream.common.set_format = out_set_format; 2196 out->stream.common.standby = out_standby; 2197 out->stream.common.dump = out_dump; 2198 out->stream.common.set_parameters = out_set_parameters; 2199 out->stream.common.get_parameters = out_get_parameters; 2200 out->stream.common.add_audio_effect = out_add_audio_effect; 2201 out->stream.common.remove_audio_effect = out_remove_audio_effect; 2202 out->stream.get_latency = out_get_latency; 2203 out->stream.set_volume = out_set_volume; 2204 out->stream.write = out_write; 2205 out->stream.get_render_position = out_get_render_position; 2206 2207 out->config = pcm_config_mm; 2208 2209 out->dev = ladev; 2210 out->standby = 1; 2211 2212 /* FIXME: when we support multiple output devices, we will want to 2213 * do the following: 2214 * adev->devices &= ~AUDIO_DEVICE_OUT_ALL; 2215 * adev->devices |= out->device; 2216 * select_output_device(adev); 2217 * This is because out_set_parameters() with a route is not 2218 * guaranteed to be called after an output stream is opened. */ 2219 2220 *format = out_get_format(&out->stream.common); 2221 *channels = out_get_channels(&out->stream.common); 2222 *sample_rate = out_get_sample_rate(&out->stream.common); 2223 2224 *stream_out = &out->stream; 2225 return 0; 2226 2227 err_open: 2228 free(out); 2229 *stream_out = NULL; 2230 return ret; 2231 } 2232 2233 static void adev_close_output_stream(struct audio_hw_device *dev, 2234 struct audio_stream_out *stream) 2235 { 2236 struct tuna_stream_out *out = (struct tuna_stream_out *)stream; 2237 2238 out_standby(&stream->common); 2239 if (out->buffer) 2240 free(out->buffer); 2241 if (out->resampler) 2242 release_resampler(out->resampler); 2243 free(stream); 2244 } 2245 2246 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 2247 { 2248 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 2249 struct str_parms *parms; 2250 char *str; 2251 char value[32]; 2252 int ret; 2253 2254 parms = str_parms_create_str(kvpairs); 2255 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 2256 if (ret >= 0) { 2257 int tty_mode; 2258 2259 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 2260 tty_mode = TTY_MODE_OFF; 2261 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 2262 tty_mode = TTY_MODE_VCO; 2263 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 2264 tty_mode = TTY_MODE_HCO; 2265 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 2266 tty_mode = TTY_MODE_FULL; 2267 else 2268 return -EINVAL; 2269 2270 pthread_mutex_lock(&adev->lock); 2271 if (tty_mode != adev->tty_mode) { 2272 adev->tty_mode = tty_mode; 2273 if (adev->mode == AUDIO_MODE_IN_CALL) 2274 select_output_device(adev); 2275 } 2276 pthread_mutex_unlock(&adev->lock); 2277 } 2278 2279 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 2280 if (ret >= 0) { 2281 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 2282 adev->bluetooth_nrec = true; 2283 else 2284 adev->bluetooth_nrec = false; 2285 } 2286 2287 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 2288 if (ret >= 0) { 2289 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 2290 adev->low_power = false; 2291 else 2292 adev->low_power = true; 2293 } 2294 2295 str_parms_destroy(parms); 2296 return ret; 2297 } 2298 2299 static char * adev_get_parameters(const struct audio_hw_device *dev, 2300 const char *keys) 2301 { 2302 return strdup(""); 2303 } 2304 2305 static int adev_init_check(const struct audio_hw_device *dev) 2306 { 2307 return 0; 2308 } 2309 2310 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 2311 { 2312 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 2313 2314 adev->voice_volume = volume; 2315 2316 if (adev->mode == AUDIO_MODE_IN_CALL) 2317 ril_set_call_volume(&adev->ril, SOUND_TYPE_VOICE, volume); 2318 2319 return 0; 2320 } 2321 2322 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 2323 { 2324 return -ENOSYS; 2325 } 2326 2327 static int adev_set_mode(struct audio_hw_device *dev, int mode) 2328 { 2329 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 2330 2331 pthread_mutex_lock(&adev->lock); 2332 if (adev->mode != mode) { 2333 adev->mode = mode; 2334 select_mode(adev); 2335 } 2336 pthread_mutex_unlock(&adev->lock); 2337 2338 return 0; 2339 } 2340 2341 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 2342 { 2343 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 2344 2345 adev->mic_mute = state; 2346 2347 return 0; 2348 } 2349 2350 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 2351 { 2352 struct tuna_audio_device *adev = (struct tuna_audio_device *)dev; 2353 2354 *state = adev->mic_mute; 2355 2356 return 0; 2357 } 2358 2359 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 2360 uint32_t sample_rate, int format, 2361 int channel_count) 2362 { 2363 size_t size; 2364 2365 if (check_input_parameters(sample_rate, format, channel_count) != 0) 2366 return 0; 2367 2368 return get_input_buffer_size(sample_rate, format, channel_count); 2369 } 2370 2371 static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices, 2372 int *format, uint32_t *channel_mask, 2373 uint32_t *sample_rate, 2374 audio_in_acoustics_t acoustics, 2375 struct audio_stream_in **stream_in) 2376 { 2377 struct tuna_audio_device *ladev = (struct tuna_audio_device *)dev; 2378 struct tuna_stream_in *in; 2379 int ret; 2380 int channel_count = popcount(*channel_mask); 2381 2382 if (check_input_parameters(*sample_rate, *format, channel_count) != 0) 2383 return -EINVAL; 2384 2385 in = (struct tuna_stream_in *)calloc(1, sizeof(struct tuna_stream_in)); 2386 if (!in) 2387 return -ENOMEM; 2388 2389 in->stream.common.get_sample_rate = in_get_sample_rate; 2390 in->stream.common.set_sample_rate = in_set_sample_rate; 2391 in->stream.common.get_buffer_size = in_get_buffer_size; 2392 in->stream.common.get_channels = in_get_channels; 2393 in->stream.common.get_format = in_get_format; 2394 in->stream.common.set_format = in_set_format; 2395 in->stream.common.standby = in_standby; 2396 in->stream.common.dump = in_dump; 2397 in->stream.common.set_parameters = in_set_parameters; 2398 in->stream.common.get_parameters = in_get_parameters; 2399 in->stream.common.add_audio_effect = in_add_audio_effect; 2400 in->stream.common.remove_audio_effect = in_remove_audio_effect; 2401 in->stream.set_gain = in_set_gain; 2402 in->stream.read = in_read; 2403 in->stream.get_input_frames_lost = in_get_input_frames_lost; 2404 2405 in->requested_rate = *sample_rate; 2406 2407 memcpy(&in->config, &pcm_config_mm_ul, sizeof(pcm_config_mm_ul)); 2408 in->config.channels = channel_count; 2409 2410 in->buffer = malloc(in->config.period_size * 2411 audio_stream_frame_size(&in->stream.common)); 2412 if (!in->buffer) { 2413 ret = -ENOMEM; 2414 goto err; 2415 } 2416 2417 if (in->requested_rate != in->config.rate) { 2418 in->buf_provider.get_next_buffer = get_next_buffer; 2419 in->buf_provider.release_buffer = release_buffer; 2420 2421 ret = create_resampler(in->config.rate, 2422 in->requested_rate, 2423 in->config.channels, 2424 RESAMPLER_QUALITY_DEFAULT, 2425 &in->buf_provider, 2426 &in->resampler); 2427 if (ret != 0) { 2428 ret = -EINVAL; 2429 goto err; 2430 } 2431 } 2432 2433 in->dev = ladev; 2434 in->standby = 1; 2435 in->device = devices; 2436 2437 *stream_in = &in->stream; 2438 return 0; 2439 2440 err: 2441 if (in->resampler) 2442 release_resampler(in->resampler); 2443 2444 free(in); 2445 *stream_in = NULL; 2446 return ret; 2447 } 2448 2449 static void adev_close_input_stream(struct audio_hw_device *dev, 2450 struct audio_stream_in *stream) 2451 { 2452 struct tuna_stream_in *in = (struct tuna_stream_in *)stream; 2453 2454 in_standby(&stream->common); 2455 2456 if (in->resampler) { 2457 free(in->buffer); 2458 release_resampler(in->resampler); 2459 } 2460 if (in->proc_buf) 2461 free(in->proc_buf); 2462 if (in->ref_buf) 2463 free(in->ref_buf); 2464 2465 free(stream); 2466 return; 2467 } 2468 2469 static int adev_dump(const audio_hw_device_t *device, int fd) 2470 { 2471 return 0; 2472 } 2473 2474 static int adev_close(hw_device_t *device) 2475 { 2476 struct tuna_audio_device *adev = (struct tuna_audio_device *)device; 2477 2478 /* RIL */ 2479 ril_close(&adev->ril); 2480 2481 mixer_close(adev->mixer); 2482 free(device); 2483 return 0; 2484 } 2485 2486 static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev) 2487 { 2488 return (/* OUT */ 2489 AUDIO_DEVICE_OUT_EARPIECE | 2490 AUDIO_DEVICE_OUT_SPEAKER | 2491 AUDIO_DEVICE_OUT_WIRED_HEADSET | 2492 AUDIO_DEVICE_OUT_WIRED_HEADPHONE | 2493 AUDIO_DEVICE_OUT_AUX_DIGITAL | 2494 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET | 2495 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET | 2496 AUDIO_DEVICE_OUT_ALL_SCO | 2497 AUDIO_DEVICE_OUT_DEFAULT | 2498 /* IN */ 2499 AUDIO_DEVICE_IN_COMMUNICATION | 2500 AUDIO_DEVICE_IN_AMBIENT | 2501 AUDIO_DEVICE_IN_BUILTIN_MIC | 2502 AUDIO_DEVICE_IN_WIRED_HEADSET | 2503 AUDIO_DEVICE_IN_AUX_DIGITAL | 2504 AUDIO_DEVICE_IN_BACK_MIC | 2505 AUDIO_DEVICE_IN_ALL_SCO | 2506 AUDIO_DEVICE_IN_DEFAULT); 2507 } 2508 2509 static int adev_open(const hw_module_t* module, const char* name, 2510 hw_device_t** device) 2511 { 2512 struct tuna_audio_device *adev; 2513 int ret; 2514 2515 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) 2516 return -EINVAL; 2517 2518 adev = calloc(1, sizeof(struct tuna_audio_device)); 2519 if (!adev) 2520 return -ENOMEM; 2521 2522 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; 2523 adev->hw_device.common.version = 0; 2524 adev->hw_device.common.module = (struct hw_module_t *) module; 2525 adev->hw_device.common.close = adev_close; 2526 2527 adev->hw_device.get_supported_devices = adev_get_supported_devices; 2528 adev->hw_device.init_check = adev_init_check; 2529 adev->hw_device.set_voice_volume = adev_set_voice_volume; 2530 adev->hw_device.set_master_volume = adev_set_master_volume; 2531 adev->hw_device.set_mode = adev_set_mode; 2532 adev->hw_device.set_mic_mute = adev_set_mic_mute; 2533 adev->hw_device.get_mic_mute = adev_get_mic_mute; 2534 adev->hw_device.set_parameters = adev_set_parameters; 2535 adev->hw_device.get_parameters = adev_get_parameters; 2536 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; 2537 adev->hw_device.open_output_stream = adev_open_output_stream; 2538 adev->hw_device.close_output_stream = adev_close_output_stream; 2539 adev->hw_device.open_input_stream = adev_open_input_stream; 2540 adev->hw_device.close_input_stream = adev_close_input_stream; 2541 adev->hw_device.dump = adev_dump; 2542 2543 adev->mixer = mixer_open(0); 2544 if (!adev->mixer) { 2545 free(adev); 2546 LOGE("Unable to open the mixer, aborting."); 2547 return -EINVAL; 2548 } 2549 2550 adev->mixer_ctls.dl1_eq = mixer_get_ctl_by_name(adev->mixer, 2551 MIXER_DL1_EQUALIZER); 2552 adev->mixer_ctls.mm_dl2_volume = mixer_get_ctl_by_name(adev->mixer, 2553 MIXER_DL2_MEDIA_PLAYBACK_VOLUME); 2554 adev->mixer_ctls.vx_dl2_volume = mixer_get_ctl_by_name(adev->mixer, 2555 MIXER_DL2_VOICE_PLAYBACK_VOLUME); 2556 adev->mixer_ctls.mm_dl1 = mixer_get_ctl_by_name(adev->mixer, 2557 MIXER_DL1_MIXER_MULTIMEDIA); 2558 adev->mixer_ctls.vx_dl1 = mixer_get_ctl_by_name(adev->mixer, 2559 MIXER_DL1_MIXER_VOICE); 2560 adev->mixer_ctls.mm_dl2 = mixer_get_ctl_by_name(adev->mixer, 2561 MIXER_DL2_MIXER_MULTIMEDIA); 2562 adev->mixer_ctls.vx_dl2 = mixer_get_ctl_by_name(adev->mixer, 2563 MIXER_DL2_MIXER_VOICE); 2564 adev->mixer_ctls.dl2_mono = mixer_get_ctl_by_name(adev->mixer, 2565 MIXER_DL2_MONO_MIXER); 2566 adev->mixer_ctls.dl1_headset = mixer_get_ctl_by_name(adev->mixer, 2567 MIXER_DL1_PDM_SWITCH); 2568 adev->mixer_ctls.dl1_bt = mixer_get_ctl_by_name(adev->mixer, 2569 MIXER_DL1_BT_VX_SWITCH); 2570 adev->mixer_ctls.earpiece_enable = mixer_get_ctl_by_name(adev->mixer, 2571 MIXER_EARPHONE_ENABLE_SWITCH); 2572 adev->mixer_ctls.left_capture = mixer_get_ctl_by_name(adev->mixer, 2573 MIXER_ANALOG_LEFT_CAPTURE_ROUTE); 2574 adev->mixer_ctls.right_capture = mixer_get_ctl_by_name(adev->mixer, 2575 MIXER_ANALOG_RIGHT_CAPTURE_ROUTE); 2576 adev->mixer_ctls.amic_ul_volume = mixer_get_ctl_by_name(adev->mixer, 2577 MIXER_AMIC_UL_VOLUME); 2578 adev->mixer_ctls.voice_ul_volume = mixer_get_ctl_by_name(adev->mixer, 2579 MIXER_AUDUL_VOICE_UL_VOLUME); 2580 adev->mixer_ctls.sidetone_capture = mixer_get_ctl_by_name(adev->mixer, 2581 MIXER_SIDETONE_MIXER_CAPTURE); 2582 adev->mixer_ctls.headset_volume = mixer_get_ctl_by_name(adev->mixer, 2583 MIXER_HEADSET_PLAYBACK_VOLUME); 2584 adev->mixer_ctls.speaker_volume = mixer_get_ctl_by_name(adev->mixer, 2585 MIXER_HANDSFREE_PLAYBACK_VOLUME); 2586 adev->mixer_ctls.earpiece_volume = mixer_get_ctl_by_name(adev->mixer, 2587 MIXER_EARPHONE_PLAYBACK_VOLUME); 2588 2589 if (!adev->mixer_ctls.dl1_eq || !adev->mixer_ctls.vx_dl2_volume || 2590 !adev->mixer_ctls.mm_dl2_volume || !adev->mixer_ctls.mm_dl1 || 2591 !adev->mixer_ctls.vx_dl1 || !adev->mixer_ctls.mm_dl2 || 2592 !adev->mixer_ctls.vx_dl2 || !adev->mixer_ctls.dl2_mono || 2593 !adev->mixer_ctls.dl1_headset || !adev->mixer_ctls.dl1_bt || 2594 !adev->mixer_ctls.earpiece_enable || !adev->mixer_ctls.left_capture || 2595 !adev->mixer_ctls.right_capture || !adev->mixer_ctls.amic_ul_volume || 2596 !adev->mixer_ctls.voice_ul_volume || !adev->mixer_ctls.sidetone_capture || 2597 !adev->mixer_ctls.headset_volume || !adev->mixer_ctls.speaker_volume || 2598 !adev->mixer_ctls.earpiece_volume) { 2599 mixer_close(adev->mixer); 2600 free(adev); 2601 LOGE("Unable to locate all mixer controls, aborting."); 2602 return -EINVAL; 2603 } 2604 2605 /* Set the default route before the PCM stream is opened */ 2606 pthread_mutex_lock(&adev->lock); 2607 set_route_by_array(adev->mixer, defaults, 1); 2608 adev->mode = AUDIO_MODE_NORMAL; 2609 adev->devices = AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_IN_BUILTIN_MIC; 2610 select_output_device(adev); 2611 2612 adev->pcm_modem_dl = NULL; 2613 adev->pcm_modem_ul = NULL; 2614 adev->voice_volume = 1.0f; 2615 adev->tty_mode = TTY_MODE_OFF; 2616 adev->device_is_toro = is_device_toro(); 2617 adev->bluetooth_nrec = true; 2618 adev->wb_amr = 0; 2619 2620 /* RIL */ 2621 ril_open(&adev->ril); 2622 pthread_mutex_unlock(&adev->lock); 2623 /* register callback for wideband AMR setting */ 2624 ril_register_set_wb_amr_callback(audio_set_wb_amr_callback, (void *)adev); 2625 2626 *device = &adev->hw_device.common; 2627 2628 return 0; 2629 } 2630 2631 static struct hw_module_methods_t hal_module_methods = { 2632 .open = adev_open, 2633 }; 2634 2635 struct audio_module HAL_MODULE_INFO_SYM = { 2636 .common = { 2637 .tag = HARDWARE_MODULE_TAG, 2638 .version_major = 1, 2639 .version_minor = 0, 2640 .id = AUDIO_HARDWARE_MODULE_ID, 2641 .name = "Tuna audio HW HAL", 2642 .author = "The Android Open Source Project", 2643 .methods = &hal_module_methods, 2644 }, 2645 }; 2646