audio_hw.c revision 5191a856311c5bd5a1b48810032ccdbc35a7fdc7
1/* 2 * Copyright (C) 2013 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/*#define VERY_VERY_VERBOSE_LOGGING*/ 20#ifdef VERY_VERY_VERBOSE_LOGGING 21#define ALOGVV ALOGV 22#else 23#define ALOGVV(a...) do { } while(0) 24#endif 25 26#include <errno.h> 27#include <pthread.h> 28#include <stdint.h> 29#include <sys/time.h> 30#include <stdlib.h> 31#include <math.h> 32#include <sys/resource.h> 33#include <sys/prctl.h> 34 35#include <cutils/log.h> 36#include <cutils/str_parms.h> 37#include <cutils/properties.h> 38#include <cutils/atomic.h> 39#include <cutils/sched_policy.h> 40 41#include <hardware/audio_effect.h> 42#include <system/thread_defs.h> 43#include <audio_effects/effect_aec.h> 44#include <audio_effects/effect_ns.h> 45#include "audio_hw.h" 46#include "platform_api.h" 47#include <platform.h> 48 49#include "sound/compress_params.h" 50 51#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024) 52#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4 53/* ToDo: Check and update a proper value in msec */ 54#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 55#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 56 57struct pcm_config pcm_config_deep_buffer = { 58 .channels = 2, 59 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 60 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, 61 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, 62 .format = PCM_FORMAT_S16_LE, 63 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 64 .stop_threshold = INT_MAX, 65 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, 66}; 67 68struct pcm_config pcm_config_low_latency = { 69 .channels = 2, 70 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, 71 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, 72 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, 73 .format = PCM_FORMAT_S16_LE, 74 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 75 .stop_threshold = INT_MAX, 76 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, 77}; 78 79struct pcm_config pcm_config_hdmi_multi = { 80 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ 81 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ 82 .period_size = HDMI_MULTI_PERIOD_SIZE, 83 .period_count = HDMI_MULTI_PERIOD_COUNT, 84 .format = PCM_FORMAT_S16_LE, 85 .start_threshold = 0, 86 .stop_threshold = INT_MAX, 87 .avail_min = 0, 88}; 89 90struct pcm_config pcm_config_audio_capture = { 91 .channels = 2, 92 .period_count = AUDIO_CAPTURE_PERIOD_COUNT, 93 .format = PCM_FORMAT_S16_LE, 94}; 95 96struct pcm_config pcm_config_voice_call = { 97 .channels = 1, 98 .rate = 8000, 99 .period_size = 160, 100 .period_count = 2, 101 .format = PCM_FORMAT_S16_LE, 102}; 103 104static const char * const use_case_table[AUDIO_USECASE_MAX] = { 105 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", 106 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", 107 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", 108 [USECASE_AUDIO_RECORD] = "audio-record", 109 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", 110 [USECASE_VOICE_CALL] = "voice-call", 111 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", 112}; 113 114 115#define STRING_TO_ENUM(string) { #string, string } 116 117struct string_to_enum { 118 const char *name; 119 uint32_t value; 120}; 121 122static const struct string_to_enum out_channels_name_to_enum_table[] = { 123 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 124 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 125 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 126}; 127 128static int set_voice_volume_l(struct audio_device *adev, float volume); 129 130static bool is_supported_format(audio_format_t format) 131{ 132 if (format == AUDIO_FORMAT_MP3 /*|| 133 format == AUDIO_FORMAT_AAC */) 134 return true; 135 136 return false; 137} 138 139static int get_snd_codec_id(audio_format_t format) 140{ 141 int id = 0; 142 143 switch (format) { 144 case AUDIO_FORMAT_MP3: 145 id = SND_AUDIOCODEC_MP3; 146 break; 147 case AUDIO_FORMAT_AAC: 148 id = SND_AUDIOCODEC_AAC; 149 break; 150 default: 151 ALOGE("%s: Unsupported audio format", __func__); 152 } 153 154 return id; 155} 156 157static int enable_audio_route(struct audio_device *adev, 158 struct audio_usecase *usecase, 159 bool update_mixer) 160{ 161 snd_device_t snd_device; 162 char mixer_path[50]; 163 164 if (usecase == NULL) 165 return -EINVAL; 166 167 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 168 169 if (usecase->type == PCM_CAPTURE) 170 snd_device = usecase->in_snd_device; 171 else 172 snd_device = usecase->out_snd_device; 173 174 strcpy(mixer_path, use_case_table[usecase->id]); 175 platform_add_backend_name(mixer_path, snd_device); 176 ALOGV("%s: apply mixer path: %s", __func__, mixer_path); 177 audio_route_apply_path(adev->audio_route, mixer_path); 178 if (update_mixer) 179 audio_route_update_mixer(adev->audio_route); 180 181 ALOGV("%s: exit", __func__); 182 return 0; 183} 184 185static int disable_audio_route(struct audio_device *adev, 186 struct audio_usecase *usecase, 187 bool update_mixer) 188{ 189 snd_device_t snd_device; 190 char mixer_path[50]; 191 192 if (usecase == NULL) 193 return -EINVAL; 194 195 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); 196 if (usecase->type == PCM_CAPTURE) 197 snd_device = usecase->in_snd_device; 198 else 199 snd_device = usecase->out_snd_device; 200 strcpy(mixer_path, use_case_table[usecase->id]); 201 platform_add_backend_name(mixer_path, snd_device); 202 ALOGV("%s: reset mixer path: %s", __func__, mixer_path); 203 audio_route_reset_path(adev->audio_route, mixer_path); 204 if (update_mixer) 205 audio_route_update_mixer(adev->audio_route); 206 207 ALOGV("%s: exit", __func__); 208 return 0; 209} 210 211static int enable_snd_device(struct audio_device *adev, 212 snd_device_t snd_device, 213 bool update_mixer) 214{ 215 if (snd_device < SND_DEVICE_MIN || 216 snd_device >= SND_DEVICE_MAX) { 217 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 218 return -EINVAL; 219 } 220 221 adev->snd_dev_ref_cnt[snd_device]++; 222 if (adev->snd_dev_ref_cnt[snd_device] > 1) { 223 ALOGV("%s: snd_device(%d: %s) is already active", 224 __func__, snd_device, platform_get_snd_device_name(snd_device)); 225 return 0; 226 } 227 228 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) { 229 adev->snd_dev_ref_cnt[snd_device]--; 230 return -EINVAL; 231 } 232 233 ALOGV("%s: snd_device(%d: %s)", __func__, 234 snd_device, platform_get_snd_device_name(snd_device)); 235 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device)); 236 if (update_mixer) 237 audio_route_update_mixer(adev->audio_route); 238 239 return 0; 240} 241 242static int disable_snd_device(struct audio_device *adev, 243 snd_device_t snd_device, 244 bool update_mixer) 245{ 246 if (snd_device < SND_DEVICE_MIN || 247 snd_device >= SND_DEVICE_MAX) { 248 ALOGE("%s: Invalid sound device %d", __func__, snd_device); 249 return -EINVAL; 250 } 251 if (adev->snd_dev_ref_cnt[snd_device] <= 0) { 252 ALOGE("%s: device ref cnt is already 0", __func__); 253 return -EINVAL; 254 } 255 adev->snd_dev_ref_cnt[snd_device]--; 256 if (adev->snd_dev_ref_cnt[snd_device] == 0) { 257 ALOGV("%s: snd_device(%d: %s)", __func__, 258 snd_device, platform_get_snd_device_name(snd_device)); 259 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device)); 260 if (update_mixer) 261 audio_route_update_mixer(adev->audio_route); 262 } 263 return 0; 264} 265 266static void check_usecases_codec_backend(struct audio_device *adev, 267 struct audio_usecase *uc_info, 268 snd_device_t snd_device) 269{ 270 struct listnode *node; 271 struct audio_usecase *usecase; 272 bool switch_device[AUDIO_USECASE_MAX]; 273 int i, num_uc_to_switch = 0; 274 275 /* 276 * This function is to make sure that all the usecases that are active on 277 * the hardware codec backend are always routed to any one device that is 278 * handled by the hardware codec. 279 * For example, if low-latency and deep-buffer usecases are currently active 280 * on speaker and out_set_parameters(headset) is received on low-latency 281 * output, then we have to make sure deep-buffer is also switched to headset, 282 * because of the limitation that both the devices cannot be enabled 283 * at the same time as they share the same backend. 284 */ 285 /* Disable all the usecases on the shared backend other than the 286 specified usecase */ 287 for (i = 0; i < AUDIO_USECASE_MAX; i++) 288 switch_device[i] = false; 289 290 list_for_each(node, &adev->usecase_list) { 291 usecase = node_to_item(node, struct audio_usecase, list); 292 if (usecase->type != PCM_CAPTURE && 293 usecase != uc_info && 294 usecase->out_snd_device != snd_device && 295 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 296 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 297 __func__, use_case_table[usecase->id], 298 platform_get_snd_device_name(usecase->out_snd_device)); 299 disable_audio_route(adev, usecase, false); 300 switch_device[usecase->id] = true; 301 num_uc_to_switch++; 302 } 303 } 304 305 if (num_uc_to_switch) { 306 /* Make sure all the streams are de-routed before disabling the device */ 307 audio_route_update_mixer(adev->audio_route); 308 309 list_for_each(node, &adev->usecase_list) { 310 usecase = node_to_item(node, struct audio_usecase, list); 311 if (switch_device[usecase->id]) { 312 disable_snd_device(adev, usecase->out_snd_device, false); 313 enable_snd_device(adev, snd_device, false); 314 } 315 } 316 317 /* Make sure new snd device is enabled before re-routing the streams */ 318 audio_route_update_mixer(adev->audio_route); 319 320 /* Re-route all the usecases on the shared backend other than the 321 specified usecase to new snd devices */ 322 list_for_each(node, &adev->usecase_list) { 323 usecase = node_to_item(node, struct audio_usecase, list); 324 /* Update the out_snd_device only before enabling the audio route */ 325 if (switch_device[usecase->id] ) { 326 usecase->out_snd_device = snd_device; 327 enable_audio_route(adev, usecase, false); 328 } 329 } 330 331 audio_route_update_mixer(adev->audio_route); 332 } 333} 334 335static void check_and_route_capture_usecases(struct audio_device *adev, 336 struct audio_usecase *uc_info, 337 snd_device_t snd_device) 338{ 339 struct listnode *node; 340 struct audio_usecase *usecase; 341 bool switch_device[AUDIO_USECASE_MAX]; 342 int i, num_uc_to_switch = 0; 343 344 /* 345 * This function is to make sure that all the active capture usecases 346 * are always routed to the same input sound device. 347 * For example, if audio-record and voice-call usecases are currently 348 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) 349 * is received for voice call then we have to make sure that audio-record 350 * usecase is also switched to earpiece i.e. voice-dmic-ef, 351 * because of the limitation that two devices cannot be enabled 352 * at the same time if they share the same backend. 353 */ 354 for (i = 0; i < AUDIO_USECASE_MAX; i++) 355 switch_device[i] = false; 356 357 list_for_each(node, &adev->usecase_list) { 358 usecase = node_to_item(node, struct audio_usecase, list); 359 if (usecase->type != PCM_PLAYBACK && 360 usecase != uc_info && 361 usecase->in_snd_device != snd_device) { 362 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", 363 __func__, use_case_table[usecase->id], 364 platform_get_snd_device_name(usecase->in_snd_device)); 365 disable_audio_route(adev, usecase, false); 366 switch_device[usecase->id] = true; 367 num_uc_to_switch++; 368 } 369 } 370 371 if (num_uc_to_switch) { 372 /* Make sure all the streams are de-routed before disabling the device */ 373 audio_route_update_mixer(adev->audio_route); 374 375 list_for_each(node, &adev->usecase_list) { 376 usecase = node_to_item(node, struct audio_usecase, list); 377 if (switch_device[usecase->id]) { 378 disable_snd_device(adev, usecase->in_snd_device, false); 379 enable_snd_device(adev, snd_device, false); 380 } 381 } 382 383 /* Make sure new snd device is enabled before re-routing the streams */ 384 audio_route_update_mixer(adev->audio_route); 385 386 /* Re-route all the usecases on the shared backend other than the 387 specified usecase to new snd devices */ 388 list_for_each(node, &adev->usecase_list) { 389 usecase = node_to_item(node, struct audio_usecase, list); 390 /* Update the in_snd_device only before enabling the audio route */ 391 if (switch_device[usecase->id] ) { 392 usecase->in_snd_device = snd_device; 393 enable_audio_route(adev, usecase, false); 394 } 395 } 396 397 audio_route_update_mixer(adev->audio_route); 398 } 399} 400 401 402/* must be called with hw device mutex locked */ 403static int read_hdmi_channel_masks(struct stream_out *out) 404{ 405 int ret = 0; 406 int channels = platform_edid_get_max_channels(out->dev->platform); 407 408 switch (channels) { 409 /* 410 * Do not handle stereo output in Multi-channel cases 411 * Stereo case is handled in normal playback path 412 */ 413 case 6: 414 ALOGV("%s: HDMI supports 5.1", __func__); 415 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 416 break; 417 case 8: 418 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); 419 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 420 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; 421 break; 422 default: 423 ALOGE("HDMI does not support multi channel playback"); 424 ret = -ENOSYS; 425 break; 426 } 427 return ret; 428} 429 430static struct audio_usecase *get_usecase_from_list(struct audio_device *adev, 431 audio_usecase_t uc_id) 432{ 433 struct audio_usecase *usecase; 434 struct listnode *node; 435 436 list_for_each(node, &adev->usecase_list) { 437 usecase = node_to_item(node, struct audio_usecase, list); 438 if (usecase->id == uc_id) 439 return usecase; 440 } 441 return NULL; 442} 443 444static int select_devices(struct audio_device *adev, 445 audio_usecase_t uc_id) 446{ 447 snd_device_t out_snd_device = SND_DEVICE_NONE; 448 snd_device_t in_snd_device = SND_DEVICE_NONE; 449 struct audio_usecase *usecase = NULL; 450 struct audio_usecase *vc_usecase = NULL; 451 struct listnode *node; 452 int status = 0; 453 454 usecase = get_usecase_from_list(adev, uc_id); 455 if (usecase == NULL) { 456 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); 457 return -EINVAL; 458 } 459 460 if (usecase->type == VOICE_CALL) { 461 out_snd_device = platform_get_output_snd_device(adev->platform, 462 usecase->stream.out->devices); 463 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices); 464 usecase->devices = usecase->stream.out->devices; 465 } else { 466 /* 467 * If the voice call is active, use the sound devices of voice call usecase 468 * so that it would not result any device switch. All the usecases will 469 * be switched to new device when select_devices() is called for voice call 470 * usecase. This is to avoid switching devices for voice call when 471 * check_usecases_codec_backend() is called below. 472 */ 473 if (adev->in_call) { 474 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL); 475 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { 476 in_snd_device = vc_usecase->in_snd_device; 477 out_snd_device = vc_usecase->out_snd_device; 478 } 479 } 480 if (usecase->type == PCM_PLAYBACK) { 481 usecase->devices = usecase->stream.out->devices; 482 in_snd_device = SND_DEVICE_NONE; 483 if (out_snd_device == SND_DEVICE_NONE) { 484 out_snd_device = platform_get_output_snd_device(adev->platform, 485 usecase->stream.out->devices); 486 if (usecase->stream.out == adev->primary_output && 487 adev->active_input && 488 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 489 select_devices(adev, adev->active_input->usecase); 490 } 491 } 492 } else if (usecase->type == PCM_CAPTURE) { 493 usecase->devices = usecase->stream.in->device; 494 out_snd_device = SND_DEVICE_NONE; 495 if (in_snd_device == SND_DEVICE_NONE) { 496 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && 497 adev->primary_output && !adev->primary_output->standby) { 498 in_snd_device = platform_get_input_snd_device(adev->platform, 499 adev->primary_output->devices); 500 } else { 501 in_snd_device = platform_get_input_snd_device(adev->platform, 502 AUDIO_DEVICE_NONE); 503 } 504 } 505 } 506 } 507 508 if (out_snd_device == usecase->out_snd_device && 509 in_snd_device == usecase->in_snd_device) { 510 return 0; 511 } 512 513 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__, 514 out_snd_device, platform_get_snd_device_name(out_snd_device), 515 in_snd_device, platform_get_snd_device_name(in_snd_device)); 516 517 /* 518 * Limitation: While in call, to do a device switch we need to disable 519 * and enable both RX and TX devices though one of them is same as current 520 * device. 521 */ 522 if (usecase->type == VOICE_CALL) { 523 status = platform_switch_voice_call_device_pre(adev->platform); 524 } 525 526 /* Disable current sound devices */ 527 if (usecase->out_snd_device != SND_DEVICE_NONE) { 528 disable_audio_route(adev, usecase, true); 529 disable_snd_device(adev, usecase->out_snd_device, false); 530 } 531 532 if (usecase->in_snd_device != SND_DEVICE_NONE) { 533 disable_audio_route(adev, usecase, true); 534 disable_snd_device(adev, usecase->in_snd_device, false); 535 } 536 537 /* Enable new sound devices */ 538 if (out_snd_device != SND_DEVICE_NONE) { 539 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) 540 check_usecases_codec_backend(adev, usecase, out_snd_device); 541 enable_snd_device(adev, out_snd_device, false); 542 } 543 544 if (in_snd_device != SND_DEVICE_NONE) { 545 check_and_route_capture_usecases(adev, usecase, in_snd_device); 546 enable_snd_device(adev, in_snd_device, false); 547 } 548 549 if (usecase->type == VOICE_CALL) 550 status = platform_switch_voice_call_device_post(adev->platform, 551 out_snd_device, 552 in_snd_device); 553 554 audio_route_update_mixer(adev->audio_route); 555 556 usecase->in_snd_device = in_snd_device; 557 usecase->out_snd_device = out_snd_device; 558 559 enable_audio_route(adev, usecase, true); 560 561 return status; 562} 563 564static int stop_input_stream(struct stream_in *in) 565{ 566 int i, ret = 0; 567 struct audio_usecase *uc_info; 568 struct audio_device *adev = in->dev; 569 570 adev->active_input = NULL; 571 572 ALOGV("%s: enter: usecase(%d: %s)", __func__, 573 in->usecase, use_case_table[in->usecase]); 574 uc_info = get_usecase_from_list(adev, in->usecase); 575 if (uc_info == NULL) { 576 ALOGE("%s: Could not find the usecase (%d) in the list", 577 __func__, in->usecase); 578 return -EINVAL; 579 } 580 581 /* 1. Disable stream specific mixer controls */ 582 disable_audio_route(adev, uc_info, true); 583 584 /* 2. Disable the tx device */ 585 disable_snd_device(adev, uc_info->in_snd_device, true); 586 587 list_remove(&uc_info->list); 588 free(uc_info); 589 590 ALOGV("%s: exit: status(%d)", __func__, ret); 591 return ret; 592} 593 594int start_input_stream(struct stream_in *in) 595{ 596 /* 1. Enable output device and stream routing controls */ 597 int ret = 0; 598 struct audio_usecase *uc_info; 599 struct audio_device *adev = in->dev; 600 601 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); 602 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); 603 if (in->pcm_device_id < 0) { 604 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 605 __func__, in->usecase); 606 ret = -EINVAL; 607 goto error_config; 608 } 609 610 adev->active_input = in; 611 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 612 uc_info->id = in->usecase; 613 uc_info->type = PCM_CAPTURE; 614 uc_info->stream.in = in; 615 uc_info->devices = in->device; 616 uc_info->in_snd_device = SND_DEVICE_NONE; 617 uc_info->out_snd_device = SND_DEVICE_NONE; 618 619 list_add_tail(&adev->usecase_list, &uc_info->list); 620 select_devices(adev, in->usecase); 621 622 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 623 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels); 624 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id, 625 PCM_IN, &in->config); 626 if (in->pcm && !pcm_is_ready(in->pcm)) { 627 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); 628 pcm_close(in->pcm); 629 in->pcm = NULL; 630 ret = -EIO; 631 goto error_open; 632 } 633 ALOGV("%s: exit", __func__); 634 return ret; 635 636error_open: 637 stop_input_stream(in); 638 639error_config: 640 adev->active_input = NULL; 641 ALOGD("%s: exit: status(%d)", __func__, ret); 642 643 return ret; 644} 645 646/* must be called with out->lock locked */ 647static int send_offload_cmd_l(struct stream_out* out, int command) 648{ 649 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); 650 651 ALOGVV("%s %d", __func__, command); 652 653 cmd->cmd = command; 654 list_add_tail(&out->offload_cmd_list, &cmd->node); 655 pthread_cond_signal(&out->offload_cond); 656 return 0; 657} 658 659/* must be called iwth out->lock locked */ 660static void stop_compressed_output_l(struct stream_out *out) 661{ 662 out->offload_state = OFFLOAD_STATE_IDLE; 663 out->playback_started = 0; 664 if (out->compr != NULL) { 665 compress_stop(out->compr); 666 while (out->offload_thread_blocked) { 667 pthread_cond_wait(&out->cond, &out->lock); 668 } 669 } 670} 671 672static void *offload_thread_loop(void *context) 673{ 674 struct stream_out *out = (struct stream_out *) context; 675 struct listnode *item; 676 677 out->offload_state = OFFLOAD_STATE_IDLE; 678 out->playback_started = 0; 679 680 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); 681 set_sched_policy(0, SP_FOREGROUND); 682 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); 683 684 ALOGV("%s", __func__); 685 pthread_mutex_lock(&out->lock); 686 for (;;) { 687 struct offload_cmd *cmd = NULL; 688 stream_callback_event_t event; 689 bool send_callback = false; 690 691 ALOGVV("%s offload_cmd_list %d out->offload_state %d", 692 __func__, list_empty(&out->offload_cmd_list), 693 out->offload_state); 694 if (list_empty(&out->offload_cmd_list)) { 695 ALOGV("%s SLEEPING", __func__); 696 pthread_cond_wait(&out->offload_cond, &out->lock); 697 ALOGV("%s RUNNING", __func__); 698 continue; 699 } 700 701 item = list_head(&out->offload_cmd_list); 702 cmd = node_to_item(item, struct offload_cmd, node); 703 list_remove(item); 704 705 ALOGVV("%s STATE %d CMD %d out->compr %p", 706 __func__, out->offload_state, cmd->cmd, out->compr); 707 708 if (cmd->cmd == OFFLOAD_CMD_EXIT) { 709 free(cmd); 710 break; 711 } 712 713 if (out->compr == NULL) { 714 ALOGE("%s: Compress handle is NULL", __func__); 715 pthread_cond_signal(&out->cond); 716 continue; 717 } 718 out->offload_thread_blocked = true; 719 pthread_mutex_unlock(&out->lock); 720 send_callback = false; 721 switch(cmd->cmd) { 722 case OFFLOAD_CMD_WAIT_FOR_BUFFER: 723 compress_wait(out->compr, -1); 724 send_callback = true; 725 event = STREAM_CBK_EVENT_WRITE_READY; 726 break; 727 case OFFLOAD_CMD_PARTIAL_DRAIN: 728 compress_drain(out->compr); 729//FIXME compress_partial_drain(out->compr); 730 send_callback = true; 731 event = STREAM_CBK_EVENT_DRAIN_READY; 732 break; 733 case OFFLOAD_CMD_DRAIN: 734 compress_drain(out->compr); 735 send_callback = true; 736 event = STREAM_CBK_EVENT_DRAIN_READY; 737 break; 738 default: 739 ALOGE("%s unknown command received: %d", __func__, cmd->cmd); 740 break; 741 } 742 pthread_mutex_lock(&out->lock); 743 out->offload_thread_blocked = false; 744 pthread_cond_signal(&out->cond); 745 if (send_callback) { 746 if (event == STREAM_CBK_EVENT_DRAIN_READY) 747 stop_compressed_output_l(out); 748 out->offload_callback(event, NULL, out->offload_cookie); 749 } 750 free(cmd); 751 } 752 753 pthread_cond_signal(&out->cond); 754 while (!list_empty(&out->offload_cmd_list)) { 755 item = list_head(&out->offload_cmd_list); 756 list_remove(item); 757 free(node_to_item(item, struct offload_cmd, node)); 758 } 759 pthread_mutex_unlock(&out->lock); 760 761 return NULL; 762} 763 764static int create_offload_callback_thread(struct stream_out *out) 765{ 766 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); 767 list_init(&out->offload_cmd_list); 768 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, 769 offload_thread_loop, out); 770 return 0; 771} 772 773static int destroy_offload_callback_thread(struct stream_out *out) 774{ 775 pthread_mutex_lock(&out->lock); 776 stop_compressed_output_l(out); 777 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); 778 779 pthread_mutex_unlock(&out->lock); 780 pthread_join(out->offload_thread, (void **) NULL); 781 pthread_cond_destroy(&out->offload_cond); 782 783 return 0; 784} 785 786static int stop_output_stream(struct stream_out *out) 787{ 788 int i, ret = 0; 789 struct audio_usecase *uc_info; 790 struct audio_device *adev = out->dev; 791 792 ALOGV("%s: enter: usecase(%d: %s)", __func__, 793 out->usecase, use_case_table[out->usecase]); 794 uc_info = get_usecase_from_list(adev, out->usecase); 795 if (uc_info == NULL) { 796 ALOGE("%s: Could not find the usecase (%d) in the list", 797 __func__, out->usecase); 798 return -EINVAL; 799 } 800 801 /* 1. Get and set stream specific mixer controls */ 802 disable_audio_route(adev, uc_info, true); 803 804 /* 2. Disable the rx device */ 805 disable_snd_device(adev, uc_info->out_snd_device, true); 806 807 list_remove(&uc_info->list); 808 free(uc_info); 809 810 ALOGV("%s: exit: status(%d)", __func__, ret); 811 return ret; 812} 813 814int start_output_stream(struct stream_out *out) 815{ 816 int ret = 0; 817 struct audio_usecase *uc_info; 818 struct audio_device *adev = out->dev; 819 820 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)", 821 __func__, out->usecase, use_case_table[out->usecase], out->devices); 822 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); 823 if (out->pcm_device_id < 0) { 824 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 825 __func__, out->pcm_device_id, out->usecase); 826 ret = -EINVAL; 827 goto error_config; 828 } 829 830 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 831 uc_info->id = out->usecase; 832 uc_info->type = PCM_PLAYBACK; 833 uc_info->stream.out = out; 834 uc_info->devices = out->devices; 835 uc_info->in_snd_device = SND_DEVICE_NONE; 836 uc_info->out_snd_device = SND_DEVICE_NONE; 837 838 list_add_tail(&adev->usecase_list, &uc_info->list); 839 840 select_devices(adev, out->usecase); 841 842 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", 843 __func__, 0, out->pcm_device_id); 844 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 845 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id, 846 PCM_OUT | PCM_MONOTONIC, &out->config); 847 if (out->pcm && !pcm_is_ready(out->pcm)) { 848 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); 849 pcm_close(out->pcm); 850 out->pcm = NULL; 851 ret = -EIO; 852 goto error_open; 853 } 854 } else { 855 out->pcm = NULL; 856 out->compr = compress_open(SOUND_CARD, out->pcm_device_id, 857 COMPRESS_IN, &out->compr_config); 858 if (out->compr && !is_compress_ready(out->compr)) { 859 ALOGE("%s: %s", __func__, compress_get_error(out->compr)); 860 compress_close(out->compr); 861 out->compr = NULL; 862 ret = -EIO; 863 goto error_open; 864 } 865 if (out->offload_callback) 866 compress_nonblock(out->compr, out->non_blocking); 867 } 868 ALOGV("%s: exit", __func__); 869 return 0; 870error_open: 871 stop_output_stream(out); 872error_config: 873 return ret; 874} 875 876static int stop_voice_call(struct audio_device *adev) 877{ 878 int i, ret = 0; 879 struct audio_usecase *uc_info; 880 881 ALOGV("%s: enter", __func__); 882 adev->in_call = false; 883 884 ret = platform_stop_voice_call(adev->platform); 885 886 /* 1. Close the PCM devices */ 887 if (adev->voice_call_rx) { 888 pcm_close(adev->voice_call_rx); 889 adev->voice_call_rx = NULL; 890 } 891 if (adev->voice_call_tx) { 892 pcm_close(adev->voice_call_tx); 893 adev->voice_call_tx = NULL; 894 } 895 896 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL); 897 if (uc_info == NULL) { 898 ALOGE("%s: Could not find the usecase (%d) in the list", 899 __func__, USECASE_VOICE_CALL); 900 return -EINVAL; 901 } 902 903 /* 2. Get and set stream specific mixer controls */ 904 disable_audio_route(adev, uc_info, true); 905 906 /* 3. Disable the rx and tx devices */ 907 disable_snd_device(adev, uc_info->out_snd_device, false); 908 disable_snd_device(adev, uc_info->in_snd_device, true); 909 910 list_remove(&uc_info->list); 911 free(uc_info); 912 913 ALOGV("%s: exit: status(%d)", __func__, ret); 914 return ret; 915} 916 917static int start_voice_call(struct audio_device *adev) 918{ 919 int i, ret = 0; 920 struct audio_usecase *uc_info; 921 int pcm_dev_rx_id, pcm_dev_tx_id; 922 923 ALOGV("%s: enter", __func__); 924 925 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 926 uc_info->id = USECASE_VOICE_CALL; 927 uc_info->type = VOICE_CALL; 928 uc_info->stream.out = adev->primary_output; 929 uc_info->devices = adev->primary_output->devices; 930 uc_info->in_snd_device = SND_DEVICE_NONE; 931 uc_info->out_snd_device = SND_DEVICE_NONE; 932 933 list_add_tail(&adev->usecase_list, &uc_info->list); 934 935 select_devices(adev, USECASE_VOICE_CALL); 936 937 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); 938 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); 939 940 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) { 941 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", 942 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); 943 ret = -EIO; 944 goto error_start_voice; 945 } 946 947 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)", 948 __func__, SOUND_CARD, pcm_dev_rx_id); 949 adev->voice_call_rx = pcm_open(SOUND_CARD, 950 pcm_dev_rx_id, 951 PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call); 952 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) { 953 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx)); 954 ret = -EIO; 955 goto error_start_voice; 956 } 957 958 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)", 959 __func__, SOUND_CARD, pcm_dev_tx_id); 960 adev->voice_call_tx = pcm_open(SOUND_CARD, 961 pcm_dev_tx_id, 962 PCM_IN, &pcm_config_voice_call); 963 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) { 964 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx)); 965 ret = -EIO; 966 goto error_start_voice; 967 } 968 969 /* set cached volume */ 970 set_voice_volume_l(adev, adev->voice_volume); 971 972 pcm_start(adev->voice_call_rx); 973 pcm_start(adev->voice_call_tx); 974 975 ret = platform_start_voice_call(adev->platform); 976 if (ret < 0) { 977 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret); 978 goto error_start_voice; 979 } 980 adev->in_call = true; 981 return 0; 982 983error_start_voice: 984 stop_voice_call(adev); 985 986 ALOGD("%s: exit: status(%d)", __func__, ret); 987 return ret; 988} 989 990static int check_input_parameters(uint32_t sample_rate, 991 audio_format_t format, 992 int channel_count) 993{ 994 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; 995 996 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; 997 998 switch (sample_rate) { 999 case 8000: 1000 case 11025: 1001 case 12000: 1002 case 16000: 1003 case 22050: 1004 case 24000: 1005 case 32000: 1006 case 44100: 1007 case 48000: 1008 break; 1009 default: 1010 return -EINVAL; 1011 } 1012 1013 return 0; 1014} 1015 1016static size_t get_input_buffer_size(uint32_t sample_rate, 1017 audio_format_t format, 1018 int channel_count) 1019{ 1020 size_t size = 0; 1021 1022 if (check_input_parameters(sample_rate, format, channel_count) != 0) 1023 return 0; 1024 1025 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000; 1026 /* ToDo: should use frame_size computed based on the format and 1027 channel_count here. */ 1028 size *= sizeof(short) * channel_count; 1029 1030 /* make sure the size is multiple of 64 */ 1031 size += 0x3f; 1032 size &= ~0x3f; 1033 1034 return size; 1035} 1036 1037static uint32_t out_get_sample_rate(const struct audio_stream *stream) 1038{ 1039 struct stream_out *out = (struct stream_out *)stream; 1040 1041 return out->sample_rate; 1042} 1043 1044static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1045{ 1046 return -ENOSYS; 1047} 1048 1049static size_t out_get_buffer_size(const struct audio_stream *stream) 1050{ 1051 struct stream_out *out = (struct stream_out *)stream; 1052 1053 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1054 return out->compr_config.fragment_size; 1055 } 1056 1057 return out->config.period_size * audio_stream_frame_size(stream); 1058} 1059 1060static uint32_t out_get_channels(const struct audio_stream *stream) 1061{ 1062 struct stream_out *out = (struct stream_out *)stream; 1063 1064 return out->channel_mask; 1065} 1066 1067static audio_format_t out_get_format(const struct audio_stream *stream) 1068{ 1069 struct stream_out *out = (struct stream_out *)stream; 1070 1071 return out->format; 1072} 1073 1074static int out_set_format(struct audio_stream *stream, audio_format_t format) 1075{ 1076 return -ENOSYS; 1077} 1078 1079static int out_standby(struct audio_stream *stream) 1080{ 1081 struct stream_out *out = (struct stream_out *)stream; 1082 struct audio_device *adev = out->dev; 1083 1084 ALOGV("%s: enter: usecase(%d: %s)", __func__, 1085 out->usecase, use_case_table[out->usecase]); 1086 1087 pthread_mutex_lock(&out->lock); 1088 if (!out->standby) { 1089 out->standby = true; 1090 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1091 if (out->pcm) { 1092 pcm_close(out->pcm); 1093 out->pcm = NULL; 1094 } 1095 } else { 1096 stop_compressed_output_l(out); 1097 if (out->compr != NULL) { 1098 compress_close(out->compr); 1099 out->compr = NULL; 1100 } 1101 } 1102 pthread_mutex_lock(&adev->lock); 1103 stop_output_stream(out); 1104 pthread_mutex_unlock(&adev->lock); 1105 } 1106 pthread_mutex_unlock(&out->lock); 1107 ALOGV("%s: exit", __func__); 1108 return 0; 1109} 1110 1111static int out_dump(const struct audio_stream *stream, int fd) 1112{ 1113 return 0; 1114} 1115 1116static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 1117{ 1118 struct stream_out *out = (struct stream_out *)stream; 1119 struct audio_device *adev = out->dev; 1120 struct audio_usecase *usecase; 1121 struct listnode *node; 1122 struct str_parms *parms; 1123 char value[32]; 1124 int ret, val = 0; 1125 bool select_new_device = false; 1126 1127 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", 1128 __func__, out->usecase, use_case_table[out->usecase], kvpairs); 1129 parms = str_parms_create_str(kvpairs); 1130 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1131 if (ret >= 0) { 1132 val = atoi(value); 1133 pthread_mutex_lock(&out->lock); 1134 pthread_mutex_lock(&adev->lock); 1135 1136 /* 1137 * When HDMI cable is unplugged the music playback is paused and 1138 * the policy manager sends routing=0. But the audioflinger 1139 * continues to write data until standby time (3sec). 1140 * As the HDMI core is turned off, the write gets blocked. 1141 * Avoid this by routing audio to speaker until standby. 1142 */ 1143 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && 1144 val == AUDIO_DEVICE_NONE) { 1145 val = AUDIO_DEVICE_OUT_SPEAKER; 1146 } 1147 1148 /* 1149 * select_devices() call below switches all the usecases on the same 1150 * backend to the new device. Refer to check_usecases_codec_backend() in 1151 * the select_devices(). But how do we undo this? 1152 * 1153 * For example, music playback is active on headset (deep-buffer usecase) 1154 * and if we go to ringtones and select a ringtone, low-latency usecase 1155 * will be started on headset+speaker. As we can't enable headset+speaker 1156 * and headset devices at the same time, select_devices() switches the music 1157 * playback to headset+speaker while starting low-lateny usecase for ringtone. 1158 * So when the ringtone playback is completed, how do we undo the same? 1159 * 1160 * We are relying on the out_set_parameters() call on deep-buffer output, 1161 * once the ringtone playback is ended. 1162 * NOTE: We should not check if the current devices are same as new devices. 1163 * Because select_devices() must be called to switch back the music 1164 * playback to headset. 1165 */ 1166 if (val != 0) { 1167 out->devices = val; 1168 1169 if (!out->standby) 1170 select_devices(adev, out->usecase); 1171 1172 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call && 1173 (out == adev->primary_output)) { 1174 start_voice_call(adev); 1175 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call && 1176 (out == adev->primary_output)) { 1177 select_devices(adev, USECASE_VOICE_CALL); 1178 } 1179 } 1180 1181 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call && 1182 (out == adev->primary_output)) { 1183 stop_voice_call(adev); 1184 } 1185 1186 pthread_mutex_unlock(&adev->lock); 1187 pthread_mutex_unlock(&out->lock); 1188 } 1189 str_parms_destroy(parms); 1190 ALOGV("%s: exit: code(%d)", __func__, ret); 1191 return ret; 1192} 1193 1194static char* out_get_parameters(const struct audio_stream *stream, const char *keys) 1195{ 1196 struct stream_out *out = (struct stream_out *)stream; 1197 struct str_parms *query = str_parms_create_str(keys); 1198 char *str; 1199 char value[256]; 1200 struct str_parms *reply = str_parms_create(); 1201 size_t i, j; 1202 int ret; 1203 bool first = true; 1204 ALOGV("%s: enter: keys - %s", __func__, keys); 1205 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); 1206 if (ret >= 0) { 1207 value[0] = '\0'; 1208 i = 0; 1209 while (out->supported_channel_masks[i] != 0) { 1210 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { 1211 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { 1212 if (!first) { 1213 strcat(value, "|"); 1214 } 1215 strcat(value, out_channels_name_to_enum_table[j].name); 1216 first = false; 1217 break; 1218 } 1219 } 1220 i++; 1221 } 1222 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 1223 str = str_parms_to_str(reply); 1224 } else { 1225 str = strdup(keys); 1226 } 1227 str_parms_destroy(query); 1228 str_parms_destroy(reply); 1229 ALOGV("%s: exit: returns - %s", __func__, str); 1230 return str; 1231} 1232 1233static uint32_t out_get_latency(const struct audio_stream_out *stream) 1234{ 1235 struct stream_out *out = (struct stream_out *)stream; 1236 1237 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) 1238 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; 1239 1240 return (out->config.period_count * out->config.period_size * 1000) / 1241 (out->config.rate); 1242} 1243 1244static int out_set_volume(struct audio_stream_out *stream, float left, 1245 float right) 1246{ 1247 struct stream_out *out = (struct stream_out *)stream; 1248 int volume[2]; 1249 1250 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { 1251 /* only take left channel into account: the API is for stereo anyway */ 1252 out->muted = (left == 0.0f); 1253 return 0; 1254 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1255 const char *mixer_ctl_name = "Compress Playback Volume"; 1256 struct audio_device *adev = out->dev; 1257 struct mixer_ctl *ctl; 1258 1259 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); 1260 if (!ctl) { 1261 ALOGE("%s: Could not get ctl for mixer cmd - %s", 1262 __func__, mixer_ctl_name); 1263 return -EINVAL; 1264 } 1265 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); 1266 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); 1267 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0])); 1268 return 0; 1269 } 1270 1271 return -ENOSYS; 1272} 1273 1274static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, 1275 size_t bytes) 1276{ 1277 struct stream_out *out = (struct stream_out *)stream; 1278 struct audio_device *adev = out->dev; 1279 ssize_t ret = 0; 1280 1281 pthread_mutex_lock(&out->lock); 1282 if (out->standby) { 1283 out->standby = false; 1284 pthread_mutex_lock(&adev->lock); 1285 ret = start_output_stream(out); 1286 pthread_mutex_unlock(&adev->lock); 1287 /* ToDo: If use case is compress offload should return 0 */ 1288 if (ret != 0) { 1289 out->standby = true; 1290 goto exit; 1291 } 1292 } 1293 1294 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1295 ret = compress_write(out->compr, buffer, bytes); 1296 ALOGVV("%s: writing buffer (%d bytes) to pcm device returned %d", __func__, bytes, ret); 1297 if (ret >= 0 && ret < (ssize_t)bytes) { 1298 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); 1299 } 1300 if (!out->playback_started) { 1301 compress_start(out->compr); 1302 out->playback_started = 1; 1303 out->offload_state = OFFLOAD_STATE_PLAYING; 1304 } 1305 pthread_mutex_unlock(&out->lock); 1306 return ret; 1307 } else { 1308 if (out->pcm) { 1309 if (out->muted) 1310 memset((void *)buffer, 0, bytes); 1311 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); 1312 ret = pcm_write(out->pcm, (void *)buffer, bytes); 1313 if (ret == 0) 1314 out->written += bytes / (out->config.channels * sizeof(short)); 1315 } 1316 } 1317 1318exit: 1319 pthread_mutex_unlock(&out->lock); 1320 1321 if (ret != 0) { 1322 if (out->pcm) 1323 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm)); 1324 out_standby(&out->stream.common); 1325 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) / 1326 out_get_sample_rate(&out->stream.common)); 1327 } 1328 return bytes; 1329} 1330 1331static int out_get_render_position(const struct audio_stream_out *stream, 1332 uint32_t *dsp_frames) 1333{ 1334 struct stream_out *out = (struct stream_out *)stream; 1335 *dsp_frames = 0; 1336 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { 1337 pthread_mutex_lock(&out->lock); 1338 if (out->compr != NULL) { 1339 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames, 1340 &out->sample_rate); 1341 ALOGVV("%s rendered frames %d sample_rate %d", 1342 __func__, *dsp_frames, out->sample_rate); 1343 } 1344 pthread_mutex_unlock(&out->lock); 1345 return 0; 1346 } else 1347 return -EINVAL; 1348} 1349 1350static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1351{ 1352 return 0; 1353} 1354 1355static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1356{ 1357 return 0; 1358} 1359 1360static int out_get_next_write_timestamp(const struct audio_stream_out *stream, 1361 int64_t *timestamp) 1362{ 1363 return -EINVAL; 1364} 1365 1366static int out_get_presentation_position(const struct audio_stream_out *stream, 1367 uint64_t *frames, struct timespec *timestamp) 1368{ 1369 struct stream_out *out = (struct stream_out *)stream; 1370 int ret = -1; 1371 1372 pthread_mutex_lock(&out->lock); 1373 1374 if (out->pcm) { 1375 size_t avail; 1376 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { 1377 size_t kernel_buffer_size = out->config.period_size * out->config.period_count; 1378 // FIXME This calculation is incorrect if there is buffering after app processor 1379 int64_t signed_frames = out->written - kernel_buffer_size + avail; 1380 // It would be unusual for this value to be negative, but check just in case ... 1381 if (signed_frames >= 0) { 1382 *frames = signed_frames; 1383 ret = 0; 1384 } 1385 } 1386 } 1387 1388 pthread_mutex_unlock(&out->lock); 1389 1390 return ret; 1391} 1392 1393static int out_set_callback(struct audio_stream_out *stream, 1394 stream_callback_t callback, void *cookie) 1395{ 1396 struct stream_out *out = (struct stream_out *)stream; 1397 1398 ALOGV("%s", __func__); 1399 pthread_mutex_lock(&out->lock); 1400 out->offload_callback = callback; 1401 out->offload_cookie = cookie; 1402 pthread_mutex_unlock(&out->lock); 1403 return 0; 1404} 1405 1406static int out_pause(struct audio_stream_out* stream) 1407{ 1408 struct stream_out *out = (struct stream_out *)stream; 1409 int status = -ENOSYS; 1410 ALOGV("%s", __func__); 1411 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1412 pthread_mutex_lock(&out->lock); 1413 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { 1414 status = compress_pause(out->compr); 1415 out->offload_state = OFFLOAD_STATE_PAUSED; 1416 } 1417 pthread_mutex_unlock(&out->lock); 1418 } 1419 return status; 1420} 1421 1422static int out_resume(struct audio_stream_out* stream) 1423{ 1424 struct stream_out *out = (struct stream_out *)stream; 1425 int status = -ENOSYS; 1426 ALOGV("%s", __func__); 1427 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1428 status = 0; 1429 pthread_mutex_lock(&out->lock); 1430 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { 1431 status = compress_resume(out->compr); 1432 out->offload_state = OFFLOAD_STATE_PLAYING; 1433 } 1434 pthread_mutex_unlock(&out->lock); 1435 } 1436 return status; 1437} 1438 1439static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) 1440{ 1441 struct stream_out *out = (struct stream_out *)stream; 1442 int status = -ENOSYS; 1443 ALOGV("%s", __func__); 1444 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1445 pthread_mutex_lock(&out->lock); 1446 if (type == AUDIO_DRAIN_EARLY_NOTIFY) 1447 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); 1448 else 1449 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); 1450 pthread_mutex_unlock(&out->lock); 1451 } 1452 return status; 1453} 1454 1455static int out_flush(struct audio_stream_out* stream) 1456{ 1457 struct stream_out *out = (struct stream_out *)stream; 1458 ALOGV("%s", __func__); 1459 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1460 pthread_mutex_lock(&out->lock); 1461 stop_compressed_output_l(out); 1462 pthread_mutex_unlock(&out->lock); 1463 return 0; 1464 } 1465 return -ENOSYS; 1466} 1467 1468/** audio_stream_in implementation **/ 1469static uint32_t in_get_sample_rate(const struct audio_stream *stream) 1470{ 1471 struct stream_in *in = (struct stream_in *)stream; 1472 1473 return in->config.rate; 1474} 1475 1476static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1477{ 1478 return -ENOSYS; 1479} 1480 1481static size_t in_get_buffer_size(const struct audio_stream *stream) 1482{ 1483 struct stream_in *in = (struct stream_in *)stream; 1484 1485 return in->config.period_size * audio_stream_frame_size(stream); 1486} 1487 1488static uint32_t in_get_channels(const struct audio_stream *stream) 1489{ 1490 struct stream_in *in = (struct stream_in *)stream; 1491 1492 return in->channel_mask; 1493} 1494 1495static audio_format_t in_get_format(const struct audio_stream *stream) 1496{ 1497 return AUDIO_FORMAT_PCM_16_BIT; 1498} 1499 1500static int in_set_format(struct audio_stream *stream, audio_format_t format) 1501{ 1502 return -ENOSYS; 1503} 1504 1505static int in_standby(struct audio_stream *stream) 1506{ 1507 struct stream_in *in = (struct stream_in *)stream; 1508 struct audio_device *adev = in->dev; 1509 int status = 0; 1510 ALOGV("%s: enter", __func__); 1511 pthread_mutex_lock(&in->lock); 1512 if (!in->standby) { 1513 in->standby = true; 1514 if (in->pcm) { 1515 pcm_close(in->pcm); 1516 in->pcm = NULL; 1517 } 1518 pthread_mutex_lock(&adev->lock); 1519 status = stop_input_stream(in); 1520 pthread_mutex_unlock(&adev->lock); 1521 } 1522 pthread_mutex_unlock(&in->lock); 1523 ALOGV("%s: exit: status(%d)", __func__, status); 1524 return status; 1525} 1526 1527static int in_dump(const struct audio_stream *stream, int fd) 1528{ 1529 return 0; 1530} 1531 1532static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 1533{ 1534 struct stream_in *in = (struct stream_in *)stream; 1535 struct audio_device *adev = in->dev; 1536 struct str_parms *parms; 1537 char *str; 1538 char value[32]; 1539 int ret, val = 0; 1540 1541 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); 1542 parms = str_parms_create_str(kvpairs); 1543 1544 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 1545 1546 pthread_mutex_lock(&in->lock); 1547 pthread_mutex_lock(&adev->lock); 1548 if (ret >= 0) { 1549 val = atoi(value); 1550 /* no audio source uses val == 0 */ 1551 if ((in->source != val) && (val != 0)) { 1552 in->source = val; 1553 } 1554 } 1555 1556 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1557 if (ret >= 0) { 1558 val = atoi(value); 1559 if ((in->device != val) && (val != 0)) { 1560 in->device = val; 1561 /* If recording is in progress, change the tx device to new device */ 1562 if (!in->standby) 1563 ret = select_devices(adev, in->usecase); 1564 } 1565 } 1566 1567 pthread_mutex_unlock(&adev->lock); 1568 pthread_mutex_unlock(&in->lock); 1569 1570 str_parms_destroy(parms); 1571 ALOGV("%s: exit: status(%d)", __func__, ret); 1572 return ret; 1573} 1574 1575static char* in_get_parameters(const struct audio_stream *stream, 1576 const char *keys) 1577{ 1578 return strdup(""); 1579} 1580 1581static int in_set_gain(struct audio_stream_in *stream, float gain) 1582{ 1583 return 0; 1584} 1585 1586static ssize_t in_read(struct audio_stream_in *stream, void *buffer, 1587 size_t bytes) 1588{ 1589 struct stream_in *in = (struct stream_in *)stream; 1590 struct audio_device *adev = in->dev; 1591 int i, ret = -1; 1592 1593 pthread_mutex_lock(&in->lock); 1594 if (in->standby) { 1595 pthread_mutex_lock(&adev->lock); 1596 ret = start_input_stream(in); 1597 pthread_mutex_unlock(&adev->lock); 1598 if (ret != 0) { 1599 goto exit; 1600 } 1601 in->standby = 0; 1602 } 1603 1604 if (in->pcm) { 1605 ret = pcm_read(in->pcm, buffer, bytes); 1606 } 1607 1608 /* 1609 * Instead of writing zeroes here, we could trust the hardware 1610 * to always provide zeroes when muted. 1611 */ 1612 if (ret == 0 && adev->mic_mute) 1613 memset(buffer, 0, bytes); 1614 1615exit: 1616 pthread_mutex_unlock(&in->lock); 1617 1618 if (ret != 0) { 1619 in_standby(&in->stream.common); 1620 ALOGV("%s: read failed - sleeping for buffer duration", __func__); 1621 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) / 1622 in_get_sample_rate(&in->stream.common)); 1623 } 1624 return bytes; 1625} 1626 1627static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 1628{ 1629 return 0; 1630} 1631 1632static int add_remove_audio_effect(const struct audio_stream *stream, 1633 effect_handle_t effect, 1634 bool enable) 1635{ 1636 struct stream_in *in = (struct stream_in *)stream; 1637 int status = 0; 1638 effect_descriptor_t desc; 1639 1640 status = (*effect)->get_descriptor(effect, &desc); 1641 if (status != 0) 1642 return status; 1643 1644 pthread_mutex_lock(&in->lock); 1645 pthread_mutex_lock(&in->dev->lock); 1646 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && 1647 in->enable_aec != enable && 1648 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { 1649 in->enable_aec = enable; 1650 if (!in->standby) 1651 select_devices(in->dev, in->usecase); 1652 } 1653 pthread_mutex_unlock(&in->dev->lock); 1654 pthread_mutex_unlock(&in->lock); 1655 1656 return 0; 1657} 1658 1659static int in_add_audio_effect(const struct audio_stream *stream, 1660 effect_handle_t effect) 1661{ 1662 ALOGV("%s: effect %p", __func__, effect); 1663 return add_remove_audio_effect(stream, effect, true); 1664} 1665 1666static int in_remove_audio_effect(const struct audio_stream *stream, 1667 effect_handle_t effect) 1668{ 1669 ALOGV("%s: effect %p", __func__, effect); 1670 return add_remove_audio_effect(stream, effect, false); 1671} 1672 1673static int adev_open_output_stream(struct audio_hw_device *dev, 1674 audio_io_handle_t handle, 1675 audio_devices_t devices, 1676 audio_output_flags_t flags, 1677 struct audio_config *config, 1678 struct audio_stream_out **stream_out) 1679{ 1680 struct audio_device *adev = (struct audio_device *)dev; 1681 struct stream_out *out; 1682 int i, ret; 1683 1684 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", 1685 __func__, config->sample_rate, config->channel_mask, devices, flags); 1686 *stream_out = NULL; 1687 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 1688 1689 if (devices == AUDIO_DEVICE_NONE) 1690 devices = AUDIO_DEVICE_OUT_SPEAKER; 1691 1692 out->flags = flags; 1693 out->devices = devices; 1694 out->dev = adev; 1695 out->format = config->format; 1696 out->sample_rate = config->sample_rate; 1697 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 1698 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; 1699 1700 /* Init use case and pcm_config */ 1701 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && 1702 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1703 pthread_mutex_lock(&adev->lock); 1704 ret = read_hdmi_channel_masks(out); 1705 pthread_mutex_unlock(&adev->lock); 1706 if (ret != 0) { 1707 /* If HDMI does not support multi channel playback, set the default */ 1708 out->config.channels = popcount(out->channel_mask); 1709 platform_set_hdmi_channels(adev->platform, out->config.channels); 1710 goto error_open; 1711 } 1712 1713 if (config->sample_rate == 0) 1714 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 1715 if (config->channel_mask == 0) 1716 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; 1717 1718 out->channel_mask = config->channel_mask; 1719 out->sample_rate = config->sample_rate; 1720 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH; 1721 out->config = pcm_config_hdmi_multi; 1722 out->config.rate = config->sample_rate; 1723 out->config.channels = popcount(out->channel_mask); 1724 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2); 1725 platform_set_hdmi_channels(adev->platform, out->config.channels); 1726 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 1727 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; 1728 out->config = pcm_config_deep_buffer; 1729 out->sample_rate = out->config.rate; 1730 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 1731 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || 1732 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { 1733 ALOGE("%s: Unsupported Offload information", __func__); 1734 ret = -EINVAL; 1735 goto error_open; 1736 } 1737 if (!is_supported_format(config->offload_info.format)) { 1738 ALOGE("%s: Unsupported audio format", __func__); 1739 ret = -EINVAL; 1740 goto error_open; 1741 } 1742 1743 out->compr_config.codec = (struct snd_codec *) 1744 calloc(1, sizeof(struct snd_codec)); 1745 1746 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; 1747 if (config->offload_info.channel_mask) 1748 out->channel_mask = config->offload_info.channel_mask; 1749 else if (config->channel_mask) 1750 out->channel_mask = config->channel_mask; 1751 out->format = config->offload_info.format; 1752 out->sample_rate = config->offload_info.sample_rate; 1753 1754 out->stream.set_callback = out_set_callback; 1755 out->stream.pause = out_pause; 1756 out->stream.resume = out_resume; 1757 out->stream.drain = out_drain; 1758 out->stream.flush = out_flush; 1759 1760 out->compr_config.codec->id = 1761 get_snd_codec_id(config->offload_info.format); 1762 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; 1763 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; 1764 out->compr_config.codec->sample_rate = 1765 compress_get_alsa_rate(config->offload_info.sample_rate); 1766 out->compr_config.codec->bit_rate = 1767 config->offload_info.bit_rate; 1768 out->compr_config.codec->ch_in = 1769 popcount(config->channel_mask); 1770 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; 1771 1772 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) 1773 out->non_blocking = 1; 1774 create_offload_callback_thread(out); 1775 ALOGV("%s: offloaded output offload_info version %04x bit rate %d", 1776 __func__, config->offload_info.version, 1777 config->offload_info.bit_rate); 1778 } else { 1779 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; 1780 out->config = pcm_config_low_latency; 1781 out->sample_rate = out->config.rate; 1782 } 1783 1784 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { 1785 if(adev->primary_output == NULL) 1786 adev->primary_output = out; 1787 else { 1788 ALOGE("%s: Primary output is already opened", __func__); 1789 ret = -EEXIST; 1790 goto error_open; 1791 } 1792 } 1793 1794 /* Check if this usecase is already existing */ 1795 pthread_mutex_lock(&adev->lock); 1796 if (get_usecase_from_list(adev, out->usecase) != NULL) { 1797 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); 1798 pthread_mutex_unlock(&adev->lock); 1799 ret = -EEXIST; 1800 goto error_open; 1801 } 1802 pthread_mutex_unlock(&adev->lock); 1803 1804 out->stream.common.get_sample_rate = out_get_sample_rate; 1805 out->stream.common.set_sample_rate = out_set_sample_rate; 1806 out->stream.common.get_buffer_size = out_get_buffer_size; 1807 out->stream.common.get_channels = out_get_channels; 1808 out->stream.common.get_format = out_get_format; 1809 out->stream.common.set_format = out_set_format; 1810 out->stream.common.standby = out_standby; 1811 out->stream.common.dump = out_dump; 1812 out->stream.common.set_parameters = out_set_parameters; 1813 out->stream.common.get_parameters = out_get_parameters; 1814 out->stream.common.add_audio_effect = out_add_audio_effect; 1815 out->stream.common.remove_audio_effect = out_remove_audio_effect; 1816 out->stream.get_latency = out_get_latency; 1817 out->stream.set_volume = out_set_volume; 1818 out->stream.write = out_write; 1819 out->stream.get_render_position = out_get_render_position; 1820 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 1821 out->stream.get_presentation_position = out_get_presentation_position; 1822 1823 out->standby = 1; 1824 /* out->muted = false; by calloc() */ 1825 /* out->written = 0; by calloc() */ 1826 1827 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); 1828 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); 1829 1830 config->format = out->stream.common.get_format(&out->stream.common); 1831 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 1832 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 1833 1834 *stream_out = &out->stream; 1835 ALOGV("%s: exit", __func__); 1836 return 0; 1837 1838error_open: 1839 free(out); 1840 *stream_out = NULL; 1841 ALOGD("%s: exit: ret %d", __func__, ret); 1842 return ret; 1843} 1844 1845static void adev_close_output_stream(struct audio_hw_device *dev, 1846 struct audio_stream_out *stream) 1847{ 1848 struct stream_out *out = (struct stream_out *)stream; 1849 struct audio_device *adev = out->dev; 1850 1851 ALOGV("%s: enter", __func__); 1852 out_standby(&stream->common); 1853 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { 1854 destroy_offload_callback_thread(out); 1855 1856 if (out->compr_config.codec != NULL) 1857 free(out->compr_config.codec); 1858 } 1859 pthread_cond_destroy(&out->cond); 1860 pthread_mutex_destroy(&out->lock); 1861 free(stream); 1862 ALOGV("%s: exit", __func__); 1863} 1864 1865static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 1866{ 1867 struct audio_device *adev = (struct audio_device *)dev; 1868 struct str_parms *parms; 1869 char *str; 1870 char value[32]; 1871 int val; 1872 int ret; 1873 1874 ALOGV("%s: enter: %s", __func__, kvpairs); 1875 1876 parms = str_parms_create_str(kvpairs); 1877 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 1878 if (ret >= 0) { 1879 int tty_mode; 1880 1881 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 1882 tty_mode = TTY_MODE_OFF; 1883 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 1884 tty_mode = TTY_MODE_VCO; 1885 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 1886 tty_mode = TTY_MODE_HCO; 1887 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 1888 tty_mode = TTY_MODE_FULL; 1889 else 1890 return -EINVAL; 1891 1892 pthread_mutex_lock(&adev->lock); 1893 if (tty_mode != adev->tty_mode) { 1894 adev->tty_mode = tty_mode; 1895 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode; 1896 if (adev->in_call) 1897 select_devices(adev, USECASE_VOICE_CALL); 1898 } 1899 pthread_mutex_unlock(&adev->lock); 1900 } 1901 1902 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 1903 if (ret >= 0) { 1904 /* When set to false, HAL should disable EC and NS 1905 * But it is currently not supported. 1906 */ 1907 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 1908 adev->bluetooth_nrec = true; 1909 else 1910 adev->bluetooth_nrec = false; 1911 } 1912 1913 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 1914 if (ret >= 0) { 1915 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 1916 adev->screen_off = false; 1917 else 1918 adev->screen_off = true; 1919 } 1920 1921 ret = str_parms_get_int(parms, "rotation", &val); 1922 if (ret >= 0) { 1923 bool reverse_speakers = false; 1924 switch(val) { 1925 // FIXME: note that the code below assumes that the speakers are in the correct placement 1926 // relative to the user when the device is rotated 90deg from its default rotation. This 1927 // assumption is device-specific, not platform-specific like this code. 1928 case 270: 1929 reverse_speakers = true; 1930 break; 1931 case 0: 1932 case 90: 1933 case 180: 1934 break; 1935 default: 1936 ALOGE("%s: unexpected rotation of %d", __func__, val); 1937 } 1938 pthread_mutex_lock(&adev->lock); 1939 if (adev->speaker_lr_swap != reverse_speakers) { 1940 adev->speaker_lr_swap = reverse_speakers; 1941 // only update the selected device if there is active pcm playback 1942 struct audio_usecase *usecase; 1943 struct listnode *node; 1944 list_for_each(node, &adev->usecase_list) { 1945 usecase = node_to_item(node, struct audio_usecase, list); 1946 if (usecase->type == PCM_PLAYBACK) { 1947 select_devices(adev, usecase->id); 1948 break; 1949 } 1950 } 1951 } 1952 pthread_mutex_unlock(&adev->lock); 1953 } 1954 1955 str_parms_destroy(parms); 1956 ALOGV("%s: exit with code(%d)", __func__, ret); 1957 return ret; 1958} 1959 1960static char* adev_get_parameters(const struct audio_hw_device *dev, 1961 const char *keys) 1962{ 1963 return strdup(""); 1964} 1965 1966static int adev_init_check(const struct audio_hw_device *dev) 1967{ 1968 return 0; 1969} 1970 1971/* always called with adev lock held */ 1972static int set_voice_volume_l(struct audio_device *adev, float volume) 1973{ 1974 int vol, err = 0; 1975 1976 if (adev->mode == AUDIO_MODE_IN_CALL) { 1977 if (volume < 0.0) { 1978 volume = 0.0; 1979 } else if (volume > 1.0) { 1980 volume = 1.0; 1981 } 1982 1983 vol = lrint(volume * 100.0); 1984 1985 // Voice volume levels from android are mapped to driver volume levels as follows. 1986 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0 1987 // So adjust the volume to get the correct volume index in driver 1988 vol = 100 - vol; 1989 1990 err = platform_set_voice_volume(adev->platform, vol); 1991 } 1992 return err; 1993} 1994 1995static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 1996{ 1997 int ret; 1998 struct audio_device *adev = (struct audio_device *)dev; 1999 pthread_mutex_lock(&adev->lock); 2000 /* cache volume */ 2001 adev->voice_volume = volume; 2002 ret = set_voice_volume_l(adev, adev->voice_volume); 2003 pthread_mutex_unlock(&adev->lock); 2004 return ret; 2005} 2006 2007static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 2008{ 2009 return -ENOSYS; 2010} 2011 2012static int adev_get_master_volume(struct audio_hw_device *dev, 2013 float *volume) 2014{ 2015 return -ENOSYS; 2016} 2017 2018static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) 2019{ 2020 return -ENOSYS; 2021} 2022 2023static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) 2024{ 2025 return -ENOSYS; 2026} 2027 2028static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 2029{ 2030 struct audio_device *adev = (struct audio_device *)dev; 2031 2032 pthread_mutex_lock(&adev->lock); 2033 if (adev->mode != mode) { 2034 adev->mode = mode; 2035 } 2036 pthread_mutex_unlock(&adev->lock); 2037 return 0; 2038} 2039 2040static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 2041{ 2042 struct audio_device *adev = (struct audio_device *)dev; 2043 int err = 0; 2044 2045 pthread_mutex_lock(&adev->lock); 2046 adev->mic_mute = state; 2047 2048 err = platform_set_mic_mute(adev->platform, state); 2049 pthread_mutex_unlock(&adev->lock); 2050 return err; 2051} 2052 2053static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 2054{ 2055 struct audio_device *adev = (struct audio_device *)dev; 2056 2057 *state = adev->mic_mute; 2058 2059 return 0; 2060} 2061 2062static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 2063 const struct audio_config *config) 2064{ 2065 int channel_count = popcount(config->channel_mask); 2066 2067 return get_input_buffer_size(config->sample_rate, config->format, channel_count); 2068} 2069 2070static int adev_open_input_stream(struct audio_hw_device *dev, 2071 audio_io_handle_t handle, 2072 audio_devices_t devices, 2073 struct audio_config *config, 2074 struct audio_stream_in **stream_in) 2075{ 2076 struct audio_device *adev = (struct audio_device *)dev; 2077 struct stream_in *in; 2078 int ret, buffer_size, frame_size; 2079 int channel_count = popcount(config->channel_mask); 2080 2081 ALOGV("%s: enter", __func__); 2082 *stream_in = NULL; 2083 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) 2084 return -EINVAL; 2085 2086 in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 2087 2088 in->stream.common.get_sample_rate = in_get_sample_rate; 2089 in->stream.common.set_sample_rate = in_set_sample_rate; 2090 in->stream.common.get_buffer_size = in_get_buffer_size; 2091 in->stream.common.get_channels = in_get_channels; 2092 in->stream.common.get_format = in_get_format; 2093 in->stream.common.set_format = in_set_format; 2094 in->stream.common.standby = in_standby; 2095 in->stream.common.dump = in_dump; 2096 in->stream.common.set_parameters = in_set_parameters; 2097 in->stream.common.get_parameters = in_get_parameters; 2098 in->stream.common.add_audio_effect = in_add_audio_effect; 2099 in->stream.common.remove_audio_effect = in_remove_audio_effect; 2100 in->stream.set_gain = in_set_gain; 2101 in->stream.read = in_read; 2102 in->stream.get_input_frames_lost = in_get_input_frames_lost; 2103 2104 in->device = devices; 2105 in->source = AUDIO_SOURCE_DEFAULT; 2106 in->dev = adev; 2107 in->standby = 1; 2108 in->channel_mask = config->channel_mask; 2109 2110 /* Update config params with the requested sample rate and channels */ 2111 in->usecase = USECASE_AUDIO_RECORD; 2112 in->config = pcm_config_audio_capture; 2113 in->config.channels = channel_count; 2114 in->config.rate = config->sample_rate; 2115 2116 frame_size = audio_stream_frame_size((struct audio_stream *)in); 2117 buffer_size = get_input_buffer_size(config->sample_rate, 2118 config->format, 2119 channel_count); 2120 in->config.period_size = buffer_size / frame_size; 2121 2122 *stream_in = &in->stream; 2123 ALOGV("%s: exit", __func__); 2124 return 0; 2125 2126err_open: 2127 free(in); 2128 *stream_in = NULL; 2129 return ret; 2130} 2131 2132static void adev_close_input_stream(struct audio_hw_device *dev, 2133 struct audio_stream_in *stream) 2134{ 2135 ALOGV("%s", __func__); 2136 2137 in_standby(&stream->common); 2138 free(stream); 2139 2140 return; 2141} 2142 2143static int adev_dump(const audio_hw_device_t *device, int fd) 2144{ 2145 return 0; 2146} 2147 2148static int adev_close(hw_device_t *device) 2149{ 2150 struct audio_device *adev = (struct audio_device *)device; 2151 audio_route_free(adev->audio_route); 2152 free(adev->snd_dev_ref_cnt); 2153 platform_deinit(adev->platform); 2154 free(device); 2155 return 0; 2156} 2157 2158static int adev_open(const hw_module_t *module, const char *name, 2159 hw_device_t **device) 2160{ 2161 struct audio_device *adev; 2162 int i, ret; 2163 2164 ALOGD("%s: enter", __func__); 2165 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; 2166 2167 adev = calloc(1, sizeof(struct audio_device)); 2168 2169 adev->device.common.tag = HARDWARE_DEVICE_TAG; 2170 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 2171 adev->device.common.module = (struct hw_module_t *)module; 2172 adev->device.common.close = adev_close; 2173 2174 adev->device.init_check = adev_init_check; 2175 adev->device.set_voice_volume = adev_set_voice_volume; 2176 adev->device.set_master_volume = adev_set_master_volume; 2177 adev->device.get_master_volume = adev_get_master_volume; 2178 adev->device.set_master_mute = adev_set_master_mute; 2179 adev->device.get_master_mute = adev_get_master_mute; 2180 adev->device.set_mode = adev_set_mode; 2181 adev->device.set_mic_mute = adev_set_mic_mute; 2182 adev->device.get_mic_mute = adev_get_mic_mute; 2183 adev->device.set_parameters = adev_set_parameters; 2184 adev->device.get_parameters = adev_get_parameters; 2185 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 2186 adev->device.open_output_stream = adev_open_output_stream; 2187 adev->device.close_output_stream = adev_close_output_stream; 2188 adev->device.open_input_stream = adev_open_input_stream; 2189 adev->device.close_input_stream = adev_close_input_stream; 2190 adev->device.dump = adev_dump; 2191 2192 /* Set the default route before the PCM stream is opened */ 2193 pthread_mutex_lock(&adev->lock); 2194 adev->mode = AUDIO_MODE_NORMAL; 2195 adev->active_input = NULL; 2196 adev->primary_output = NULL; 2197 adev->out_device = AUDIO_DEVICE_NONE; 2198 adev->voice_call_rx = NULL; 2199 adev->voice_call_tx = NULL; 2200 adev->voice_volume = 1.0f; 2201 adev->tty_mode = TTY_MODE_OFF; 2202 adev->bluetooth_nrec = true; 2203 adev->in_call = false; 2204 adev->acdb_settings = TTY_MODE_OFF; 2205 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); 2206 list_init(&adev->usecase_list); 2207 pthread_mutex_unlock(&adev->lock); 2208 2209 /* Loads platform specific libraries dynamically */ 2210 adev->platform = platform_init(adev); 2211 if (!adev->platform) { 2212 free(adev->snd_dev_ref_cnt); 2213 free(adev); 2214 ALOGE("%s: Failed to init platform data, aborting.", __func__); 2215 *device = NULL; 2216 return -EINVAL; 2217 } 2218 *device = &adev->device.common; 2219 2220 ALOGV("%s: exit", __func__); 2221 return 0; 2222} 2223 2224static struct hw_module_methods_t hal_module_methods = { 2225 .open = adev_open, 2226}; 2227 2228struct audio_module HAL_MODULE_INFO_SYM = { 2229 .common = { 2230 .tag = HARDWARE_MODULE_TAG, 2231 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 2232 .hal_api_version = HARDWARE_HAL_API_VERSION, 2233 .id = AUDIO_HARDWARE_MODULE_ID, 2234 .name = "QCOM Audio HAL", 2235 .author = "Code Aurora Forum", 2236 .methods = &hal_module_methods, 2237 }, 2238}; 2239