1 /****************************************************************************** 2 * 3 * Copyright (C) 2009-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /***************************************************************************** 20 * 21 * Filename: audio_a2dp_hw.c 22 * 23 * Description: Implements hal for bluedroid a2dp audio device 24 * 25 *****************************************************************************/ 26 27 #include <errno.h> 28 #include <inttypes.h> 29 #include <pthread.h> 30 #include <stdint.h> 31 #include <sys/time.h> 32 #include <sys/socket.h> 33 #include <sys/un.h> 34 #include <sys/poll.h> 35 #include <sys/errno.h> 36 #include <sys/stat.h> 37 #include <unistd.h> 38 #include <fcntl.h> 39 #include <cutils/str_parms.h> 40 #include <cutils/sockets.h> 41 42 #include <system/audio.h> 43 #include <hardware/audio.h> 44 45 #include <hardware/hardware.h> 46 #include "audio_a2dp_hw.h" 47 #include "bt_utils.h" 48 49 #define LOG_TAG "audio_a2dp_hw" 50 /* #define LOG_NDEBUG 0 */ 51 #include <log/log.h> 52 53 /***************************************************************************** 54 ** Constants & Macros 55 ******************************************************************************/ 56 57 #define CTRL_CHAN_RETRY_COUNT 3 58 #define USEC_PER_SEC 1000000L 59 60 #define CASE_RETURN_STR(const) case const: return #const; 61 62 #define FNLOG() ALOGV("%s", __FUNCTION__); 63 #define DEBUG(fmt, ...) ALOGV("%s: " fmt,__FUNCTION__, ## __VA_ARGS__) 64 #define INFO(fmt, ...) ALOGI("%s: " fmt,__FUNCTION__, ## __VA_ARGS__) 65 #define ERROR(fmt, ...) ALOGE("%s: " fmt,__FUNCTION__, ## __VA_ARGS__) 66 67 #define ASSERTC(cond, msg, val) if (!(cond)) {ERROR("### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, val);} 68 69 /***************************************************************************** 70 ** Local type definitions 71 ******************************************************************************/ 72 73 typedef enum { 74 AUDIO_A2DP_STATE_STARTING, 75 AUDIO_A2DP_STATE_STARTED, 76 AUDIO_A2DP_STATE_STOPPING, 77 AUDIO_A2DP_STATE_STOPPED, 78 AUDIO_A2DP_STATE_SUSPENDED, /* need explicit set param call to resume (suspend=false) */ 79 AUDIO_A2DP_STATE_STANDBY /* allows write to autoresume */ 80 } a2dp_state_t; 81 82 struct a2dp_stream_in; 83 struct a2dp_stream_out; 84 85 struct a2dp_audio_device { 86 struct audio_hw_device device; 87 struct a2dp_stream_in *input; 88 struct a2dp_stream_out *output; 89 }; 90 91 struct a2dp_config { 92 uint32_t rate; 93 uint32_t channel_flags; 94 int format; 95 }; 96 97 /* move ctrl_fd outside output stream and keep open until HAL unloaded ? */ 98 99 struct a2dp_stream_common { 100 pthread_mutex_t lock; 101 int ctrl_fd; 102 int audio_fd; 103 size_t buffer_sz; 104 struct a2dp_config cfg; 105 a2dp_state_t state; 106 }; 107 108 struct a2dp_stream_out { 109 struct audio_stream_out stream; 110 struct a2dp_stream_common common; 111 }; 112 113 struct a2dp_stream_in { 114 struct audio_stream_in stream; 115 struct a2dp_stream_common common; 116 }; 117 118 /***************************************************************************** 119 ** Static variables 120 ******************************************************************************/ 121 122 /***************************************************************************** 123 ** Static functions 124 ******************************************************************************/ 125 126 static size_t out_get_buffer_size(const struct audio_stream *stream); 127 128 /***************************************************************************** 129 ** Externs 130 ******************************************************************************/ 131 132 /***************************************************************************** 133 ** Functions 134 ******************************************************************************/ 135 136 /***************************************************************************** 137 ** Miscellaneous helper functions 138 ******************************************************************************/ 139 140 static const char* dump_a2dp_ctrl_event(char event) 141 { 142 switch(event) 143 { 144 CASE_RETURN_STR(A2DP_CTRL_CMD_NONE) 145 CASE_RETURN_STR(A2DP_CTRL_CMD_CHECK_READY) 146 CASE_RETURN_STR(A2DP_CTRL_CMD_START) 147 CASE_RETURN_STR(A2DP_CTRL_CMD_STOP) 148 CASE_RETURN_STR(A2DP_CTRL_CMD_SUSPEND) 149 default: 150 return "UNKNOWN MSG ID"; 151 } 152 } 153 154 /* logs timestamp with microsec precision 155 pprev is optional in case a dedicated diff is required */ 156 static void ts_log(char *tag, int val, struct timespec *pprev_opt) 157 { 158 struct timespec now; 159 static struct timespec prev = {0,0}; 160 unsigned long long now_us; 161 unsigned long long diff_us; 162 UNUSED(tag); 163 UNUSED(val); 164 165 clock_gettime(CLOCK_MONOTONIC, &now); 166 167 now_us = now.tv_sec*USEC_PER_SEC + now.tv_nsec/1000; 168 169 if (pprev_opt) 170 { 171 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000; 172 *pprev_opt = now; 173 DEBUG("[%s] ts %08lld, *diff %08lld, val %d", tag, now_us, diff_us, val); 174 } 175 else 176 { 177 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000; 178 prev = now; 179 DEBUG("[%s] ts %08lld, diff %08lld, val %d", tag, now_us, diff_us, val); 180 } 181 } 182 183 static int calc_audiotime(struct a2dp_config cfg, int bytes) 184 { 185 int chan_count = popcount(cfg.channel_flags); 186 187 ASSERTC(cfg.format == AUDIO_FORMAT_PCM_16_BIT, 188 "unsupported sample sz", cfg.format); 189 190 return bytes*(1000000/(chan_count*2))/cfg.rate; 191 } 192 193 /***************************************************************************** 194 ** 195 ** bluedroid stack adaptation 196 ** 197 *****************************************************************************/ 198 199 static int skt_connect(char *path, size_t buffer_sz) 200 { 201 int ret; 202 int skt_fd; 203 struct sockaddr_un remote; 204 int len; 205 206 INFO("connect to %s (sz %zu)", path, buffer_sz); 207 208 skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0); 209 210 if(socket_local_client_connect(skt_fd, path, 211 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0) 212 { 213 ERROR("failed to connect (%s)", strerror(errno)); 214 close(skt_fd); 215 return -1; 216 } 217 218 len = buffer_sz; 219 ret = setsockopt(skt_fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, (int)sizeof(len)); 220 221 /* only issue warning if failed */ 222 if (ret < 0) 223 ERROR("setsockopt failed (%s)", strerror(errno)); 224 225 ret = setsockopt(skt_fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, (int)sizeof(len)); 226 227 /* only issue warning if failed */ 228 if (ret < 0) 229 ERROR("setsockopt failed (%s)", strerror(errno)); 230 231 INFO("connected to stack fd = %d", skt_fd); 232 233 return skt_fd; 234 } 235 236 static int skt_read(int fd, void *p, size_t len) 237 { 238 int read; 239 struct pollfd pfd; 240 struct timespec ts; 241 242 FNLOG(); 243 244 ts_log("skt_read recv", len, NULL); 245 246 if ((read = recv(fd, p, len, MSG_NOSIGNAL)) == -1) 247 { 248 ERROR("write failed with errno=%d\n", errno); 249 return -1; 250 } 251 252 return read; 253 } 254 255 static int skt_write(int fd, const void *p, size_t len) 256 { 257 int sent; 258 struct pollfd pfd; 259 260 FNLOG(); 261 262 pfd.fd = fd; 263 pfd.events = POLLOUT; 264 265 /* poll for 500 ms */ 266 267 /* send time out */ 268 if (poll(&pfd, 1, 500) == 0) 269 return 0; 270 271 ts_log("skt_write", len, NULL); 272 273 if ((sent = send(fd, p, len, MSG_NOSIGNAL)) == -1) 274 { 275 ERROR("write failed with errno=%d\n", errno); 276 return -1; 277 } 278 279 return sent; 280 } 281 282 static int skt_disconnect(int fd) 283 { 284 INFO("fd %d", fd); 285 286 if (fd != AUDIO_SKT_DISCONNECTED) 287 { 288 shutdown(fd, SHUT_RDWR); 289 close(fd); 290 } 291 return 0; 292 } 293 294 295 296 /***************************************************************************** 297 ** 298 ** AUDIO CONTROL PATH 299 ** 300 *****************************************************************************/ 301 302 static int a2dp_ctrl_receive(struct a2dp_stream_common *common, void* buffer, int length) 303 { 304 int ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL); 305 if (ret < 0) 306 { 307 ERROR("ack failed (%s)", strerror(errno)); 308 if (errno == EINTR) 309 { 310 /* retry again */ 311 ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL); 312 if (ret < 0) 313 { 314 ERROR("ack failed (%s)", strerror(errno)); 315 skt_disconnect(common->ctrl_fd); 316 common->ctrl_fd = AUDIO_SKT_DISCONNECTED; 317 return -1; 318 } 319 } 320 else 321 { 322 skt_disconnect(common->ctrl_fd); 323 common->ctrl_fd = AUDIO_SKT_DISCONNECTED; 324 return -1; 325 326 } 327 } 328 return ret; 329 } 330 331 static int a2dp_command(struct a2dp_stream_common *common, char cmd) 332 { 333 char ack; 334 335 DEBUG("A2DP COMMAND %s", dump_a2dp_ctrl_event(cmd)); 336 337 /* send command */ 338 if (send(common->ctrl_fd, &cmd, 1, MSG_NOSIGNAL) == -1) 339 { 340 ERROR("cmd failed (%s)", strerror(errno)); 341 skt_disconnect(common->ctrl_fd); 342 common->ctrl_fd = AUDIO_SKT_DISCONNECTED; 343 return -1; 344 } 345 346 /* wait for ack byte */ 347 if (a2dp_ctrl_receive(common, &ack, 1) < 0) 348 return -1; 349 350 DEBUG("A2DP COMMAND %s DONE STATUS %d", dump_a2dp_ctrl_event(cmd), ack); 351 352 if (ack != A2DP_CTRL_ACK_SUCCESS) 353 return -1; 354 355 return 0; 356 } 357 358 static int check_a2dp_ready(struct a2dp_stream_common *common) 359 { 360 if (a2dp_command(common, A2DP_CTRL_CMD_CHECK_READY) < 0) 361 { 362 ERROR("check a2dp ready failed"); 363 return -1; 364 } 365 return 0; 366 } 367 368 static int a2dp_read_audio_config(struct a2dp_stream_common *common) 369 { 370 char cmd = A2DP_CTRL_GET_AUDIO_CONFIG; 371 uint32_t sample_rate; 372 uint8_t channel_count; 373 374 if (a2dp_command(common, A2DP_CTRL_GET_AUDIO_CONFIG) < 0) 375 { 376 ERROR("check a2dp ready failed"); 377 return -1; 378 } 379 380 if (a2dp_ctrl_receive(common, &sample_rate, 4) < 0) 381 return -1; 382 if (a2dp_ctrl_receive(common, &channel_count, 1) < 0) 383 return -1; 384 385 common->cfg.channel_flags = (channel_count == 1 ? AUDIO_CHANNEL_IN_MONO : AUDIO_CHANNEL_IN_STEREO); 386 common->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT; 387 common->cfg.rate = sample_rate; 388 389 INFO("got config %d %d", common->cfg.format, common->cfg.rate); 390 391 return 0; 392 } 393 394 static void a2dp_open_ctrl_path(struct a2dp_stream_common *common) 395 { 396 int i; 397 398 /* retry logic to catch any timing variations on control channel */ 399 for (i = 0; i < CTRL_CHAN_RETRY_COUNT; i++) 400 { 401 /* connect control channel if not already connected */ 402 if ((common->ctrl_fd = skt_connect(A2DP_CTRL_PATH, common->buffer_sz)) > 0) 403 { 404 /* success, now check if stack is ready */ 405 if (check_a2dp_ready(common) == 0) 406 break; 407 408 ERROR("error : a2dp not ready, wait 250 ms and retry"); 409 usleep(250000); 410 skt_disconnect(common->ctrl_fd); 411 common->ctrl_fd = AUDIO_SKT_DISCONNECTED; 412 } 413 414 /* ctrl channel not ready, wait a bit */ 415 usleep(250000); 416 } 417 } 418 419 /***************************************************************************** 420 ** 421 ** AUDIO DATA PATH 422 ** 423 *****************************************************************************/ 424 425 static void a2dp_stream_common_init(struct a2dp_stream_common *common) 426 { 427 pthread_mutexattr_t lock_attr; 428 429 FNLOG(); 430 431 pthread_mutexattr_init(&lock_attr); 432 pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE); 433 pthread_mutex_init(&common->lock, &lock_attr); 434 435 common->ctrl_fd = AUDIO_SKT_DISCONNECTED; 436 common->audio_fd = AUDIO_SKT_DISCONNECTED; 437 common->state = AUDIO_A2DP_STATE_STOPPED; 438 439 /* manages max capacity of socket pipe */ 440 common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ; 441 } 442 443 static int start_audio_datapath(struct a2dp_stream_common *common) 444 { 445 int oldstate = common->state; 446 447 INFO("state %d", common->state); 448 449 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) { 450 INFO("AUDIO_SKT_DISCONNECTED"); 451 return -1; 452 } 453 454 common->state = AUDIO_A2DP_STATE_STARTING; 455 456 if (a2dp_command(common, A2DP_CTRL_CMD_START) < 0) 457 { 458 ERROR("audiopath start failed"); 459 460 common->state = oldstate; 461 return -1; 462 } 463 464 /* connect socket if not yet connected */ 465 if (common->audio_fd == AUDIO_SKT_DISCONNECTED) 466 { 467 common->audio_fd = skt_connect(A2DP_DATA_PATH, common->buffer_sz); 468 if (common->audio_fd < 0) 469 { 470 common->state = oldstate; 471 return -1; 472 } 473 474 common->state = AUDIO_A2DP_STATE_STARTED; 475 } 476 477 return 0; 478 } 479 480 481 static int stop_audio_datapath(struct a2dp_stream_common *common) 482 { 483 int oldstate = common->state; 484 485 INFO("state %d", common->state); 486 487 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) 488 return -1; 489 490 /* prevent any stray output writes from autostarting the stream 491 while stopping audiopath */ 492 common->state = AUDIO_A2DP_STATE_STOPPING; 493 494 if (a2dp_command(common, A2DP_CTRL_CMD_STOP) < 0) 495 { 496 ERROR("audiopath stop failed"); 497 common->state = oldstate; 498 return -1; 499 } 500 501 common->state = AUDIO_A2DP_STATE_STOPPED; 502 503 /* disconnect audio path */ 504 skt_disconnect(common->audio_fd); 505 common->audio_fd = AUDIO_SKT_DISCONNECTED; 506 507 return 0; 508 } 509 510 static int suspend_audio_datapath(struct a2dp_stream_common *common, bool standby) 511 { 512 INFO("state %d", common->state); 513 514 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) 515 return -1; 516 517 if (common->state == AUDIO_A2DP_STATE_STOPPING) 518 return -1; 519 520 if (a2dp_command(common, A2DP_CTRL_CMD_SUSPEND) < 0) 521 return -1; 522 523 if (standby) 524 common->state = AUDIO_A2DP_STATE_STANDBY; 525 else 526 common->state = AUDIO_A2DP_STATE_SUSPENDED; 527 528 /* disconnect audio path */ 529 skt_disconnect(common->audio_fd); 530 531 common->audio_fd = AUDIO_SKT_DISCONNECTED; 532 533 return 0; 534 } 535 536 537 /***************************************************************************** 538 ** 539 ** audio output callbacks 540 ** 541 *****************************************************************************/ 542 543 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, 544 size_t bytes) 545 { 546 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 547 int sent; 548 549 DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd); 550 551 if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED) 552 { 553 DEBUG("stream suspended"); 554 return -1; 555 } 556 557 /* only allow autostarting if we are in stopped or standby */ 558 if ((out->common.state == AUDIO_A2DP_STATE_STOPPED) || 559 (out->common.state == AUDIO_A2DP_STATE_STANDBY)) 560 { 561 pthread_mutex_lock(&out->common.lock); 562 563 if (start_audio_datapath(&out->common) < 0) 564 { 565 /* emulate time this write represents to avoid very fast write 566 failures during transition periods or remote suspend */ 567 568 int us_delay = calc_audiotime(out->common.cfg, bytes); 569 570 DEBUG("emulate a2dp write delay (%d us)", us_delay); 571 572 usleep(us_delay); 573 pthread_mutex_unlock(&out->common.lock); 574 return -1; 575 } 576 577 pthread_mutex_unlock(&out->common.lock); 578 } 579 else if (out->common.state != AUDIO_A2DP_STATE_STARTED) 580 { 581 ERROR("stream not in stopped or standby"); 582 return -1; 583 } 584 585 sent = skt_write(out->common.audio_fd, buffer, bytes); 586 587 if (sent == -1) 588 { 589 skt_disconnect(out->common.audio_fd); 590 out->common.audio_fd = AUDIO_SKT_DISCONNECTED; 591 out->common.state = AUDIO_A2DP_STATE_STOPPED; 592 } 593 594 DEBUG("wrote %d bytes out of %zu bytes", sent, bytes); 595 return sent; 596 } 597 598 599 static uint32_t out_get_sample_rate(const struct audio_stream *stream) 600 { 601 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 602 603 DEBUG("rate %" PRIu32,out->common.cfg.rate); 604 605 return out->common.cfg.rate; 606 } 607 608 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 609 { 610 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 611 612 DEBUG("out_set_sample_rate : %" PRIu32, rate); 613 614 if (rate != AUDIO_STREAM_DEFAULT_RATE) 615 { 616 ERROR("only rate %d supported", AUDIO_STREAM_DEFAULT_RATE); 617 return -1; 618 } 619 620 out->common.cfg.rate = rate; 621 622 return 0; 623 } 624 625 static size_t out_get_buffer_size(const struct audio_stream *stream) 626 { 627 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 628 629 DEBUG("buffer_size : %zu", out->common.buffer_sz); 630 631 return out->common.buffer_sz; 632 } 633 634 static uint32_t out_get_channels(const struct audio_stream *stream) 635 { 636 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 637 638 DEBUG("channels 0x%" PRIx32, out->common.cfg.channel_flags); 639 640 return out->common.cfg.channel_flags; 641 } 642 643 static audio_format_t out_get_format(const struct audio_stream *stream) 644 { 645 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 646 DEBUG("format 0x%x", out->common.cfg.format); 647 return out->common.cfg.format; 648 } 649 650 static int out_set_format(struct audio_stream *stream, audio_format_t format) 651 { 652 UNUSED(format); 653 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 654 DEBUG("setting format not yet supported (0x%x)", format); 655 return -ENOSYS; 656 } 657 658 static int out_standby(struct audio_stream *stream) 659 { 660 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 661 int retVal = 0; 662 663 FNLOG(); 664 665 pthread_mutex_lock(&out->common.lock); 666 667 if (out->common.state == AUDIO_A2DP_STATE_STARTED) 668 retVal = suspend_audio_datapath(&out->common, true); 669 else 670 retVal = 0; 671 pthread_mutex_unlock(&out->common.lock); 672 673 return retVal; 674 } 675 676 static int out_dump(const struct audio_stream *stream, int fd) 677 { 678 UNUSED(fd); 679 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 680 FNLOG(); 681 return 0; 682 } 683 684 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 685 { 686 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 687 struct str_parms *parms; 688 char keyval[16]; 689 int retval; 690 int status = 0; 691 692 INFO("state %d", out->common.state); 693 694 pthread_mutex_lock(&out->common.lock); 695 696 parms = str_parms_create_str(kvpairs); 697 698 /* dump params */ 699 str_parms_dump(parms); 700 701 retval = str_parms_get_str(parms, "closing", keyval, sizeof(keyval)); 702 703 if (retval >= 0) 704 { 705 if (strcmp(keyval, "true") == 0) 706 { 707 DEBUG("stream closing, disallow any writes"); 708 out->common.state = AUDIO_A2DP_STATE_STOPPING; 709 } 710 } 711 712 retval = str_parms_get_str(parms, "A2dpSuspended", keyval, sizeof(keyval)); 713 714 if (retval >= 0) 715 { 716 if (strcmp(keyval, "true") == 0) 717 { 718 if (out->common.state == AUDIO_A2DP_STATE_STARTED) 719 status = suspend_audio_datapath(&out->common, false); 720 } 721 else 722 { 723 /* Do not start the streaming automatically. If the phone was streaming 724 * prior to being suspended, the next out_write shall trigger the 725 * AVDTP start procedure */ 726 if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED) 727 out->common.state = AUDIO_A2DP_STATE_STANDBY; 728 /* Irrespective of the state, return 0 */ 729 } 730 } 731 732 pthread_mutex_unlock(&out->common.lock); 733 str_parms_destroy(parms); 734 735 return status; 736 } 737 738 static char * out_get_parameters(const struct audio_stream *stream, const char *keys) 739 { 740 UNUSED(keys); 741 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 742 743 FNLOG(); 744 745 /* add populating param here */ 746 747 return strdup(""); 748 } 749 750 static uint32_t out_get_latency(const struct audio_stream_out *stream) 751 { 752 int latency_us; 753 754 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 755 756 FNLOG(); 757 758 latency_us = ((out->common.buffer_sz * 1000 ) / 759 audio_stream_out_frame_size(&out->stream) / 760 out->common.cfg.rate) * 1000; 761 762 763 return (latency_us / 1000) + 200; 764 } 765 766 static int out_set_volume(struct audio_stream_out *stream, float left, 767 float right) 768 { 769 UNUSED(stream); 770 UNUSED(left); 771 UNUSED(right); 772 773 FNLOG(); 774 775 /* volume controlled in audioflinger mixer (digital) */ 776 777 return -ENOSYS; 778 } 779 780 781 782 static int out_get_render_position(const struct audio_stream_out *stream, 783 uint32_t *dsp_frames) 784 { 785 UNUSED(stream); 786 UNUSED(dsp_frames); 787 788 FNLOG(); 789 return -EINVAL; 790 } 791 792 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 793 { 794 UNUSED(stream); 795 UNUSED(effect); 796 797 FNLOG(); 798 return 0; 799 } 800 801 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 802 { 803 UNUSED(stream); 804 UNUSED(effect); 805 806 FNLOG(); 807 return 0; 808 } 809 810 /* 811 * AUDIO INPUT STREAM 812 */ 813 814 static uint32_t in_get_sample_rate(const struct audio_stream *stream) 815 { 816 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream; 817 818 FNLOG(); 819 return in->common.cfg.rate; 820 } 821 822 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 823 { 824 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream; 825 826 FNLOG(); 827 828 if (in->common.cfg.rate > 0 && in->common.cfg.rate == rate) 829 return 0; 830 else 831 return -1; 832 } 833 834 static size_t in_get_buffer_size(const struct audio_stream *stream) 835 { 836 UNUSED(stream); 837 838 FNLOG(); 839 return 320; 840 } 841 842 static uint32_t in_get_channels(const struct audio_stream *stream) 843 { 844 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream; 845 846 FNLOG(); 847 return in->common.cfg.channel_flags; 848 } 849 850 static audio_format_t in_get_format(const struct audio_stream *stream) 851 { 852 UNUSED(stream); 853 854 FNLOG(); 855 return AUDIO_FORMAT_PCM_16_BIT; 856 } 857 858 static int in_set_format(struct audio_stream *stream, audio_format_t format) 859 { 860 UNUSED(stream); 861 UNUSED(format); 862 863 FNLOG(); 864 if (format == AUDIO_FORMAT_PCM_16_BIT) 865 return 0; 866 else 867 return -1; 868 } 869 870 static int in_standby(struct audio_stream *stream) 871 { 872 UNUSED(stream); 873 874 FNLOG(); 875 return 0; 876 } 877 878 static int in_dump(const struct audio_stream *stream, int fd) 879 { 880 UNUSED(stream); 881 UNUSED(fd); 882 883 FNLOG(); 884 return 0; 885 } 886 887 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 888 { 889 UNUSED(stream); 890 UNUSED(kvpairs); 891 892 FNLOG(); 893 return 0; 894 } 895 896 static char * in_get_parameters(const struct audio_stream *stream, 897 const char *keys) 898 { 899 UNUSED(stream); 900 UNUSED(keys); 901 902 FNLOG(); 903 return strdup(""); 904 } 905 906 static int in_set_gain(struct audio_stream_in *stream, float gain) 907 { 908 UNUSED(stream); 909 UNUSED(gain); 910 911 FNLOG(); 912 return 0; 913 } 914 915 static ssize_t in_read(struct audio_stream_in *stream, void* buffer, 916 size_t bytes) 917 { 918 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream; 919 int read; 920 921 DEBUG("read %zu bytes, state: %d", bytes, in->common.state); 922 923 if (in->common.state == AUDIO_A2DP_STATE_SUSPENDED) 924 { 925 DEBUG("stream suspended"); 926 return -1; 927 } 928 929 /* only allow autostarting if we are in stopped or standby */ 930 if ((in->common.state == AUDIO_A2DP_STATE_STOPPED) || 931 (in->common.state == AUDIO_A2DP_STATE_STANDBY)) 932 { 933 pthread_mutex_lock(&in->common.lock); 934 935 if (start_audio_datapath(&in->common) < 0) 936 { 937 /* emulate time this write represents to avoid very fast write 938 failures during transition periods or remote suspend */ 939 940 int us_delay = calc_audiotime(in->common.cfg, bytes); 941 942 DEBUG("emulate a2dp read delay (%d us)", us_delay); 943 944 usleep(us_delay); 945 pthread_mutex_unlock(&in->common.lock); 946 return -1; 947 } 948 949 pthread_mutex_unlock(&in->common.lock); 950 } 951 else if (in->common.state != AUDIO_A2DP_STATE_STARTED) 952 { 953 ERROR("stream not in stopped or standby"); 954 return -1; 955 } 956 957 read = skt_read(in->common.audio_fd, buffer, bytes); 958 959 if (read == -1) 960 { 961 skt_disconnect(in->common.audio_fd); 962 in->common.audio_fd = AUDIO_SKT_DISCONNECTED; 963 in->common.state = AUDIO_A2DP_STATE_STOPPED; 964 } else if (read == 0) { 965 DEBUG("read time out - return zeros"); 966 memset(buffer, 0, bytes); 967 read = bytes; 968 } 969 970 DEBUG("read %d bytes out of %zu bytes", read, bytes); 971 return read; 972 } 973 974 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 975 { 976 UNUSED(stream); 977 978 FNLOG(); 979 return 0; 980 } 981 982 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 983 { 984 UNUSED(stream); 985 UNUSED(effect); 986 987 FNLOG(); 988 return 0; 989 } 990 991 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 992 { 993 UNUSED(stream); 994 UNUSED(effect); 995 996 FNLOG(); 997 998 return 0; 999 } 1000 1001 static int adev_open_output_stream(struct audio_hw_device *dev, 1002 audio_io_handle_t handle, 1003 audio_devices_t devices, 1004 audio_output_flags_t flags, 1005 struct audio_config *config, 1006 struct audio_stream_out **stream_out, 1007 const char *address __unused) 1008 1009 { 1010 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev; 1011 struct a2dp_stream_out *out; 1012 int ret = 0; 1013 int i; 1014 UNUSED(handle); 1015 UNUSED(devices); 1016 UNUSED(flags); 1017 1018 INFO("opening output"); 1019 1020 out = (struct a2dp_stream_out *)calloc(1, sizeof(struct a2dp_stream_out)); 1021 1022 if (!out) 1023 return -ENOMEM; 1024 1025 out->stream.common.get_sample_rate = out_get_sample_rate; 1026 out->stream.common.set_sample_rate = out_set_sample_rate; 1027 out->stream.common.get_buffer_size = out_get_buffer_size; 1028 out->stream.common.get_channels = out_get_channels; 1029 out->stream.common.get_format = out_get_format; 1030 out->stream.common.set_format = out_set_format; 1031 out->stream.common.standby = out_standby; 1032 out->stream.common.dump = out_dump; 1033 out->stream.common.set_parameters = out_set_parameters; 1034 out->stream.common.get_parameters = out_get_parameters; 1035 out->stream.common.add_audio_effect = out_add_audio_effect; 1036 out->stream.common.remove_audio_effect = out_remove_audio_effect; 1037 out->stream.get_latency = out_get_latency; 1038 out->stream.set_volume = out_set_volume; 1039 out->stream.write = out_write; 1040 out->stream.get_render_position = out_get_render_position; 1041 1042 /* initialize a2dp specifics */ 1043 a2dp_stream_common_init(&out->common); 1044 1045 out->common.cfg.channel_flags = AUDIO_STREAM_DEFAULT_CHANNEL_FLAG; 1046 out->common.cfg.format = AUDIO_STREAM_DEFAULT_FORMAT; 1047 out->common.cfg.rate = AUDIO_STREAM_DEFAULT_RATE; 1048 1049 /* set output config values */ 1050 if (config) 1051 { 1052 config->format = out_get_format((const struct audio_stream *)&out->stream); 1053 config->sample_rate = out_get_sample_rate((const struct audio_stream *)&out->stream); 1054 config->channel_mask = out_get_channels((const struct audio_stream *)&out->stream); 1055 } 1056 *stream_out = &out->stream; 1057 a2dp_dev->output = out; 1058 1059 a2dp_open_ctrl_path(&out->common); 1060 if (out->common.ctrl_fd == AUDIO_SKT_DISCONNECTED) 1061 { 1062 ERROR("ctrl socket failed to connect (%s)", strerror(errno)); 1063 ret = -1; 1064 goto err_open; 1065 } 1066 1067 DEBUG("success"); 1068 return 0; 1069 1070 err_open: 1071 free(out); 1072 *stream_out = NULL; 1073 a2dp_dev->output = NULL; 1074 ERROR("failed"); 1075 return ret; 1076 } 1077 1078 static void adev_close_output_stream(struct audio_hw_device *dev, 1079 struct audio_stream_out *stream) 1080 { 1081 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev; 1082 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream; 1083 1084 INFO("closing output (state %d)", out->common.state); 1085 1086 if ((out->common.state == AUDIO_A2DP_STATE_STARTED) || (out->common.state == AUDIO_A2DP_STATE_STOPPING)) 1087 stop_audio_datapath(&out->common); 1088 1089 skt_disconnect(out->common.ctrl_fd); 1090 free(stream); 1091 a2dp_dev->output = NULL; 1092 1093 DEBUG("done"); 1094 } 1095 1096 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 1097 { 1098 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev; 1099 struct a2dp_stream_out *out = a2dp_dev->output; 1100 int retval = 0; 1101 1102 if (out == NULL) 1103 return retval; 1104 1105 INFO("state %d", out->common.state); 1106 1107 retval = out->stream.common.set_parameters((struct audio_stream *)out, kvpairs); 1108 1109 return retval; 1110 } 1111 1112 static char * adev_get_parameters(const struct audio_hw_device *dev, 1113 const char *keys) 1114 { 1115 struct str_parms *parms; 1116 UNUSED(dev); 1117 1118 FNLOG(); 1119 1120 parms = str_parms_create_str(keys); 1121 1122 str_parms_dump(parms); 1123 1124 str_parms_destroy(parms); 1125 1126 return strdup(""); 1127 } 1128 1129 static int adev_init_check(const struct audio_hw_device *dev) 1130 { 1131 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device*)dev; 1132 1133 FNLOG(); 1134 1135 return 0; 1136 } 1137 1138 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 1139 { 1140 UNUSED(dev); 1141 UNUSED(volume); 1142 1143 FNLOG(); 1144 1145 return -ENOSYS; 1146 } 1147 1148 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 1149 { 1150 UNUSED(dev); 1151 UNUSED(volume); 1152 1153 FNLOG(); 1154 1155 return -ENOSYS; 1156 } 1157 1158 static int adev_set_mode(struct audio_hw_device *dev, int mode) 1159 { 1160 UNUSED(dev); 1161 UNUSED(mode); 1162 1163 FNLOG(); 1164 1165 return 0; 1166 } 1167 1168 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 1169 { 1170 UNUSED(dev); 1171 UNUSED(state); 1172 1173 FNLOG(); 1174 1175 return -ENOSYS; 1176 } 1177 1178 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 1179 { 1180 UNUSED(dev); 1181 UNUSED(state); 1182 1183 FNLOG(); 1184 1185 return -ENOSYS; 1186 } 1187 1188 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 1189 const struct audio_config *config) 1190 { 1191 UNUSED(dev); 1192 UNUSED(config); 1193 1194 FNLOG(); 1195 1196 return 320; 1197 } 1198 1199 static int adev_open_input_stream(struct audio_hw_device *dev, 1200 audio_io_handle_t handle, 1201 audio_devices_t devices, 1202 struct audio_config *config, 1203 struct audio_stream_in **stream_in, 1204 audio_input_flags_t flags __unused, 1205 const char *address __unused, 1206 audio_source_t source __unused) 1207 { 1208 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev; 1209 struct a2dp_stream_in *in; 1210 int ret; 1211 UNUSED(handle); 1212 UNUSED(devices); 1213 UNUSED(config); 1214 1215 FNLOG(); 1216 1217 in = (struct a2dp_stream_in *)calloc(1, sizeof(struct a2dp_stream_in)); 1218 1219 if (!in) 1220 return -ENOMEM; 1221 1222 in->stream.common.get_sample_rate = in_get_sample_rate; 1223 in->stream.common.set_sample_rate = in_set_sample_rate; 1224 in->stream.common.get_buffer_size = in_get_buffer_size; 1225 in->stream.common.get_channels = in_get_channels; 1226 in->stream.common.get_format = in_get_format; 1227 in->stream.common.set_format = in_set_format; 1228 in->stream.common.standby = in_standby; 1229 in->stream.common.dump = in_dump; 1230 in->stream.common.set_parameters = in_set_parameters; 1231 in->stream.common.get_parameters = in_get_parameters; 1232 in->stream.common.add_audio_effect = in_add_audio_effect; 1233 in->stream.common.remove_audio_effect = in_remove_audio_effect; 1234 in->stream.set_gain = in_set_gain; 1235 in->stream.read = in_read; 1236 in->stream.get_input_frames_lost = in_get_input_frames_lost; 1237 1238 /* initialize a2dp specifics */ 1239 a2dp_stream_common_init(&in->common); 1240 1241 *stream_in = &in->stream; 1242 a2dp_dev->input = in; 1243 1244 a2dp_open_ctrl_path(&in->common); 1245 if (in->common.ctrl_fd == AUDIO_SKT_DISCONNECTED) 1246 { 1247 ERROR("ctrl socket failed to connect (%s)", strerror(errno)); 1248 ret = -1; 1249 goto err_open; 1250 } 1251 1252 if (a2dp_read_audio_config(&in->common) < 0) { 1253 ERROR("a2dp_read_audio_config failed (%s)", strerror(errno)); 1254 ret = -1; 1255 goto err_open; 1256 } 1257 1258 DEBUG("success"); 1259 return 0; 1260 1261 err_open: 1262 free(in); 1263 *stream_in = NULL; 1264 a2dp_dev->input = NULL; 1265 ERROR("failed"); 1266 return ret; 1267 } 1268 1269 static void adev_close_input_stream(struct audio_hw_device *dev, 1270 struct audio_stream_in *stream) 1271 { 1272 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev; 1273 struct a2dp_stream_in* in = (struct a2dp_stream_in *)stream; 1274 a2dp_state_t state = in->common.state; 1275 1276 INFO("closing input (state %d)", state); 1277 1278 if ((state == AUDIO_A2DP_STATE_STARTED) || (state == AUDIO_A2DP_STATE_STOPPING)) 1279 stop_audio_datapath(&in->common); 1280 1281 skt_disconnect(in->common.ctrl_fd); 1282 free(stream); 1283 a2dp_dev->input = NULL; 1284 1285 DEBUG("done"); 1286 } 1287 1288 static int adev_dump(const audio_hw_device_t *device, int fd) 1289 { 1290 UNUSED(device); 1291 UNUSED(fd); 1292 1293 FNLOG(); 1294 1295 return 0; 1296 } 1297 1298 static int adev_close(hw_device_t *device) 1299 { 1300 FNLOG(); 1301 1302 free(device); 1303 return 0; 1304 } 1305 1306 static int adev_open(const hw_module_t* module, const char* name, 1307 hw_device_t** device) 1308 { 1309 struct a2dp_audio_device *adev; 1310 int ret; 1311 1312 INFO(" adev_open in A2dp_hw module"); 1313 FNLOG(); 1314 1315 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) 1316 { 1317 ERROR("interface %s not matching [%s]", name, AUDIO_HARDWARE_INTERFACE); 1318 return -EINVAL; 1319 } 1320 1321 adev = calloc(1, sizeof(struct a2dp_audio_device)); 1322 1323 if (!adev) 1324 return -ENOMEM; 1325 1326 adev->device.common.tag = HARDWARE_DEVICE_TAG; 1327 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 1328 adev->device.common.module = (struct hw_module_t *) module; 1329 adev->device.common.close = adev_close; 1330 1331 adev->device.init_check = adev_init_check; 1332 adev->device.set_voice_volume = adev_set_voice_volume; 1333 adev->device.set_master_volume = adev_set_master_volume; 1334 adev->device.set_mode = adev_set_mode; 1335 adev->device.set_mic_mute = adev_set_mic_mute; 1336 adev->device.get_mic_mute = adev_get_mic_mute; 1337 adev->device.set_parameters = adev_set_parameters; 1338 adev->device.get_parameters = adev_get_parameters; 1339 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 1340 adev->device.open_output_stream = adev_open_output_stream; 1341 adev->device.close_output_stream = adev_close_output_stream; 1342 adev->device.open_input_stream = adev_open_input_stream; 1343 adev->device.close_input_stream = adev_close_input_stream; 1344 adev->device.dump = adev_dump; 1345 1346 adev->output = NULL; 1347 1348 1349 *device = &adev->device.common; 1350 1351 return 0; 1352 } 1353 1354 static struct hw_module_methods_t hal_module_methods = { 1355 .open = adev_open, 1356 }; 1357 1358 struct audio_module HAL_MODULE_INFO_SYM = { 1359 .common = { 1360 .tag = HARDWARE_MODULE_TAG, 1361 .version_major = 1, 1362 .version_minor = 0, 1363 .id = AUDIO_HARDWARE_MODULE_ID, 1364 .name = "A2DP Audio HW HAL", 1365 .author = "The Android Open Source Project", 1366 .methods = &hal_module_methods, 1367 }, 1368 }; 1369 1370