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