1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2006-2010 Nokia Corporation 6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel (at) holtmann.org> 7 * 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 * 23 */ 24 25 #ifdef HAVE_CONFIG_H 26 #include <config.h> 27 #endif 28 29 #include <stdlib.h> 30 #include <errno.h> 31 32 #include <dbus/dbus.h> 33 #include <glib.h> 34 35 #include <bluetooth/bluetooth.h> 36 #include <bluetooth/sdp.h> 37 #include <bluetooth/sdp_lib.h> 38 39 #include "log.h" 40 #include "device.h" 41 #include "manager.h" 42 #include "avdtp.h" 43 #include "sink.h" 44 #include "source.h" 45 #include "unix.h" 46 #include "media.h" 47 #include "transport.h" 48 #include "a2dp.h" 49 #include "sdpd.h" 50 51 /* The duration that streams without users are allowed to stay in 52 * STREAMING state. */ 53 #define SUSPEND_TIMEOUT 5 54 #define RECONFIGURE_TIMEOUT 500 55 56 #ifndef MIN 57 # define MIN(x, y) ((x) < (y) ? (x) : (y)) 58 #endif 59 60 #ifndef MAX 61 # define MAX(x, y) ((x) > (y) ? (x) : (y)) 62 #endif 63 64 struct a2dp_sep { 65 struct a2dp_server *server; 66 struct media_endpoint *endpoint; 67 uint8_t type; 68 uint8_t codec; 69 struct avdtp_local_sep *lsep; 70 struct avdtp *session; 71 struct avdtp_stream *stream; 72 guint suspend_timer; 73 gboolean delay_reporting; 74 gboolean locked; 75 gboolean suspending; 76 gboolean starting; 77 }; 78 79 struct a2dp_setup_cb { 80 struct a2dp_setup *setup; 81 a2dp_select_cb_t select_cb; 82 a2dp_config_cb_t config_cb; 83 a2dp_stream_cb_t resume_cb; 84 a2dp_stream_cb_t suspend_cb; 85 guint source_id; 86 void *user_data; 87 unsigned int id; 88 }; 89 90 struct a2dp_setup { 91 struct audio_device *dev; 92 struct avdtp *session; 93 struct a2dp_sep *sep; 94 struct avdtp_remote_sep *rsep; 95 struct avdtp_stream *stream; 96 struct avdtp_error *err; 97 avdtp_set_configuration_cb setconf_cb; 98 GSList *caps; 99 gboolean reconfigure; 100 gboolean start; 101 GSList *cb; 102 int ref; 103 }; 104 105 static DBusConnection *connection = NULL; 106 107 struct a2dp_server { 108 bdaddr_t src; 109 GSList *sinks; 110 GSList *sources; 111 uint32_t source_record_id; 112 uint32_t sink_record_id; 113 uint16_t version; 114 gboolean sink_enabled; 115 gboolean source_enabled; 116 }; 117 118 static GSList *servers = NULL; 119 static GSList *setups = NULL; 120 static unsigned int cb_id = 0; 121 122 static struct a2dp_setup *setup_ref(struct a2dp_setup *setup) 123 { 124 setup->ref++; 125 126 DBG("%p: ref=%d", setup, setup->ref); 127 128 return setup; 129 } 130 131 static struct audio_device *a2dp_get_dev(struct avdtp *session) 132 { 133 bdaddr_t src, dst; 134 135 avdtp_get_peers(session, &src, &dst); 136 137 return manager_find_device(NULL, &src, &dst, NULL, FALSE); 138 } 139 140 static struct a2dp_setup *setup_new(struct avdtp *session) 141 { 142 struct audio_device *dev; 143 struct a2dp_setup *setup; 144 145 dev = a2dp_get_dev(session); 146 if (!dev) { 147 error("Unable to create setup"); 148 return NULL; 149 } 150 151 setup = g_new0(struct a2dp_setup, 1); 152 setup->session = avdtp_ref(session); 153 setup->dev = a2dp_get_dev(session); 154 setups = g_slist_append(setups, setup); 155 156 return setup; 157 } 158 159 static void setup_free(struct a2dp_setup *s) 160 { 161 DBG("%p", s); 162 163 setups = g_slist_remove(setups, s); 164 if (s->session) 165 avdtp_unref(s->session); 166 g_slist_foreach(s->cb, (GFunc) g_free, NULL); 167 g_slist_free(s->cb); 168 g_slist_foreach(s->caps, (GFunc) g_free, NULL); 169 g_slist_free(s->caps); 170 g_free(s); 171 } 172 173 static void setup_unref(struct a2dp_setup *setup) 174 { 175 setup->ref--; 176 177 DBG("%p: ref=%d", setup, setup->ref); 178 179 if (setup->ref > 0) 180 return; 181 182 setup_free(setup); 183 } 184 185 static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup) 186 { 187 struct a2dp_setup_cb *cb; 188 189 cb = g_new0(struct a2dp_setup_cb, 1); 190 cb->setup = setup; 191 cb->id = ++cb_id; 192 193 setup->cb = g_slist_append(setup->cb, cb); 194 return cb; 195 } 196 197 static void setup_cb_free(struct a2dp_setup_cb *cb) 198 { 199 struct a2dp_setup *setup = cb->setup; 200 201 if (cb->source_id) 202 g_source_remove(cb->source_id); 203 204 setup->cb = g_slist_remove(setup->cb, cb); 205 setup_unref(cb->setup); 206 g_free(cb); 207 } 208 209 static void finalize_setup_errno(struct a2dp_setup *s, int err, 210 GSourceFunc cb1, ...) 211 { 212 GSourceFunc finalize; 213 va_list args; 214 struct avdtp_error avdtp_err; 215 216 if (err < 0) { 217 avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err); 218 s->err = &avdtp_err; 219 } 220 221 va_start(args, cb1); 222 finalize = cb1; 223 setup_ref(s); 224 while (finalize != NULL) { 225 finalize(s); 226 finalize = va_arg(args, GSourceFunc); 227 } 228 setup_unref(s); 229 va_end(args); 230 } 231 232 static gboolean finalize_config(gpointer data) 233 { 234 struct a2dp_setup *s = data; 235 GSList *l; 236 struct avdtp_stream *stream = s->err ? NULL : s->stream; 237 238 for (l = s->cb; l != NULL; ) { 239 struct a2dp_setup_cb *cb = l->data; 240 241 l = l->next; 242 243 if (!cb->config_cb) 244 continue; 245 246 cb->config_cb(s->session, s->sep, stream, s->err, 247 cb->user_data); 248 setup_cb_free(cb); 249 } 250 251 return FALSE; 252 } 253 254 static gboolean finalize_resume(gpointer data) 255 { 256 struct a2dp_setup *s = data; 257 GSList *l; 258 259 for (l = s->cb; l != NULL; ) { 260 struct a2dp_setup_cb *cb = l->data; 261 262 l = l->next; 263 264 if (!cb->resume_cb) 265 continue; 266 267 cb->resume_cb(s->session, s->err, cb->user_data); 268 setup_cb_free(cb); 269 } 270 271 return FALSE; 272 } 273 274 static gboolean finalize_suspend(gpointer data) 275 { 276 struct a2dp_setup *s = data; 277 GSList *l; 278 279 for (l = s->cb; l != NULL; ) { 280 struct a2dp_setup_cb *cb = l->data; 281 282 l = l->next; 283 284 if (!cb->suspend_cb) 285 continue; 286 287 cb->suspend_cb(s->session, s->err, cb->user_data); 288 setup_cb_free(cb); 289 } 290 291 return FALSE; 292 } 293 294 static void finalize_select(struct a2dp_setup *s) 295 { 296 GSList *l; 297 298 for (l = s->cb; l != NULL; ) { 299 struct a2dp_setup_cb *cb = l->data; 300 301 l = l->next; 302 303 if (!cb->select_cb) 304 continue; 305 306 cb->select_cb(s->session, s->sep, s->caps, cb->user_data); 307 setup_cb_free(cb); 308 } 309 } 310 311 static struct a2dp_setup *find_setup_by_session(struct avdtp *session) 312 { 313 GSList *l; 314 315 for (l = setups; l != NULL; l = l->next) { 316 struct a2dp_setup *setup = l->data; 317 318 if (setup->session == session) 319 return setup; 320 } 321 322 return NULL; 323 } 324 325 static struct a2dp_setup *a2dp_setup_get(struct avdtp *session) 326 { 327 struct a2dp_setup *setup; 328 329 setup = find_setup_by_session(session); 330 if (!setup) { 331 setup = setup_new(session); 332 if (!setup) 333 return NULL; 334 } 335 336 return setup_ref(setup); 337 } 338 339 static struct a2dp_setup *find_setup_by_dev(struct audio_device *dev) 340 { 341 GSList *l; 342 343 for (l = setups; l != NULL; l = l->next) { 344 struct a2dp_setup *setup = l->data; 345 346 if (setup->dev == dev) 347 return setup; 348 } 349 350 return NULL; 351 } 352 353 static void stream_state_changed(struct avdtp_stream *stream, 354 avdtp_state_t old_state, 355 avdtp_state_t new_state, 356 struct avdtp_error *err, 357 void *user_data) 358 { 359 struct a2dp_sep *sep = user_data; 360 361 if (new_state != AVDTP_STATE_IDLE) 362 return; 363 364 if (sep->suspend_timer) { 365 g_source_remove(sep->suspend_timer); 366 sep->suspend_timer = 0; 367 } 368 369 if (sep->session) { 370 avdtp_unref(sep->session); 371 sep->session = NULL; 372 } 373 374 if (sep->endpoint) 375 media_endpoint_clear_configuration(sep->endpoint); 376 377 sep->stream = NULL; 378 379 } 380 381 static gboolean auto_config(gpointer data) 382 { 383 struct a2dp_setup *setup = data; 384 struct avdtp_error *err = NULL; 385 386 /* Check if configuration was aborted */ 387 if (setup->sep->stream == NULL) 388 return FALSE; 389 390 if (setup->err != NULL) { 391 err = setup->err; 392 goto done; 393 } 394 395 avdtp_stream_add_cb(setup->session, setup->stream, 396 stream_state_changed, setup->sep); 397 398 if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE) 399 sink_new_stream(setup->dev, setup->session, setup->stream); 400 else 401 source_new_stream(setup->dev, setup->session, setup->stream); 402 403 done: 404 if (setup->setconf_cb) 405 setup->setconf_cb(setup->session, setup->stream, setup->err); 406 407 finalize_config(setup); 408 409 if (err) 410 g_free(err); 411 412 setup_unref(setup); 413 414 return FALSE; 415 } 416 417 static gboolean sbc_setconf_ind(struct avdtp *session, 418 struct avdtp_local_sep *sep, 419 struct avdtp_stream *stream, 420 GSList *caps, 421 avdtp_set_configuration_cb cb, 422 void *user_data) 423 { 424 struct a2dp_sep *a2dp_sep = user_data; 425 struct a2dp_setup *setup; 426 427 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 428 DBG("Sink %p: Set_Configuration_Ind", sep); 429 else 430 DBG("Source %p: Set_Configuration_Ind", sep); 431 432 setup = a2dp_setup_get(session); 433 if (!setup) 434 return FALSE; 435 436 a2dp_sep->stream = stream; 437 setup->sep = a2dp_sep; 438 setup->stream = stream; 439 setup->setconf_cb = cb; 440 441 /* Check valid settings */ 442 for (; caps != NULL; caps = g_slist_next(caps)) { 443 struct avdtp_service_capability *cap = caps->data; 444 struct avdtp_media_codec_capability *codec_cap; 445 struct sbc_codec_cap *sbc_cap; 446 447 if (cap->category == AVDTP_DELAY_REPORTING && 448 !a2dp_sep->delay_reporting) { 449 setup->err = g_new(struct avdtp_error, 1); 450 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING, 451 AVDTP_UNSUPPORTED_CONFIGURATION); 452 goto done; 453 } 454 455 if (cap->category != AVDTP_MEDIA_CODEC) 456 continue; 457 458 if (cap->length < sizeof(struct sbc_codec_cap)) 459 continue; 460 461 codec_cap = (void *) cap->data; 462 463 if (codec_cap->media_codec_type != A2DP_CODEC_SBC) 464 continue; 465 466 sbc_cap = (void *) codec_cap; 467 468 if (sbc_cap->min_bitpool < MIN_BITPOOL || 469 sbc_cap->max_bitpool > MAX_BITPOOL) { 470 setup->err = g_new(struct avdtp_error, 1); 471 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC, 472 AVDTP_UNSUPPORTED_CONFIGURATION); 473 goto done; 474 } 475 } 476 477 done: 478 g_idle_add(auto_config, setup); 479 return TRUE; 480 } 481 482 static gboolean sbc_getcap_ind(struct avdtp *session, struct avdtp_local_sep *sep, 483 gboolean get_all, GSList **caps, uint8_t *err, 484 void *user_data) 485 { 486 struct a2dp_sep *a2dp_sep = user_data; 487 struct avdtp_service_capability *media_transport, *media_codec; 488 struct sbc_codec_cap sbc_cap; 489 490 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 491 DBG("Sink %p: Get_Capability_Ind", sep); 492 else 493 DBG("Source %p: Get_Capability_Ind", sep); 494 495 *caps = NULL; 496 497 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, 498 NULL, 0); 499 500 *caps = g_slist_append(*caps, media_transport); 501 502 memset(&sbc_cap, 0, sizeof(struct sbc_codec_cap)); 503 504 sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO; 505 sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC; 506 507 #ifdef ANDROID 508 sbc_cap.frequency = SBC_SAMPLING_FREQ_44100; 509 #else 510 sbc_cap.frequency = ( SBC_SAMPLING_FREQ_48000 | 511 SBC_SAMPLING_FREQ_44100 | 512 SBC_SAMPLING_FREQ_32000 | 513 SBC_SAMPLING_FREQ_16000 ); 514 #endif 515 516 sbc_cap.channel_mode = ( SBC_CHANNEL_MODE_JOINT_STEREO | 517 SBC_CHANNEL_MODE_STEREO | 518 SBC_CHANNEL_MODE_DUAL_CHANNEL | 519 SBC_CHANNEL_MODE_MONO ); 520 521 sbc_cap.block_length = ( SBC_BLOCK_LENGTH_16 | 522 SBC_BLOCK_LENGTH_12 | 523 SBC_BLOCK_LENGTH_8 | 524 SBC_BLOCK_LENGTH_4 ); 525 526 sbc_cap.subbands = ( SBC_SUBBANDS_8 | SBC_SUBBANDS_4 ); 527 528 sbc_cap.allocation_method = ( SBC_ALLOCATION_LOUDNESS | 529 SBC_ALLOCATION_SNR ); 530 531 sbc_cap.min_bitpool = MIN_BITPOOL; 532 sbc_cap.max_bitpool = MAX_BITPOOL; 533 534 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap, 535 sizeof(sbc_cap)); 536 537 *caps = g_slist_append(*caps, media_codec); 538 539 if (get_all) { 540 struct avdtp_service_capability *delay_reporting; 541 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING, 542 NULL, 0); 543 *caps = g_slist_append(*caps, delay_reporting); 544 } 545 546 return TRUE; 547 } 548 549 static gboolean mpeg_setconf_ind(struct avdtp *session, 550 struct avdtp_local_sep *sep, 551 struct avdtp_stream *stream, 552 GSList *caps, 553 avdtp_set_configuration_cb cb, 554 void *user_data) 555 { 556 struct a2dp_sep *a2dp_sep = user_data; 557 struct a2dp_setup *setup; 558 559 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 560 DBG("Sink %p: Set_Configuration_Ind", sep); 561 else 562 DBG("Source %p: Set_Configuration_Ind", sep); 563 564 setup = a2dp_setup_get(session); 565 if (!setup) 566 return FALSE; 567 568 a2dp_sep->stream = stream; 569 setup->sep = a2dp_sep; 570 setup->stream = stream; 571 setup->setconf_cb = cb; 572 573 for (; caps != NULL; caps = g_slist_next(caps)) { 574 struct avdtp_service_capability *cap = caps->data; 575 576 if (cap->category == AVDTP_DELAY_REPORTING && 577 !a2dp_sep->delay_reporting) { 578 setup->err = g_new(struct avdtp_error, 1); 579 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING, 580 AVDTP_UNSUPPORTED_CONFIGURATION); 581 goto done; 582 } 583 } 584 585 done: 586 g_idle_add(auto_config, setup); 587 return TRUE; 588 } 589 590 static gboolean mpeg_getcap_ind(struct avdtp *session, 591 struct avdtp_local_sep *sep, 592 gboolean get_all, 593 GSList **caps, uint8_t *err, void *user_data) 594 { 595 struct a2dp_sep *a2dp_sep = user_data; 596 struct avdtp_service_capability *media_transport, *media_codec; 597 struct mpeg_codec_cap mpeg_cap; 598 599 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 600 DBG("Sink %p: Get_Capability_Ind", sep); 601 else 602 DBG("Source %p: Get_Capability_Ind", sep); 603 604 *caps = NULL; 605 606 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, 607 NULL, 0); 608 609 *caps = g_slist_append(*caps, media_transport); 610 611 memset(&mpeg_cap, 0, sizeof(struct mpeg_codec_cap)); 612 613 mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO; 614 mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12; 615 616 mpeg_cap.frequency = ( MPEG_SAMPLING_FREQ_48000 | 617 MPEG_SAMPLING_FREQ_44100 | 618 MPEG_SAMPLING_FREQ_32000 | 619 MPEG_SAMPLING_FREQ_24000 | 620 MPEG_SAMPLING_FREQ_22050 | 621 MPEG_SAMPLING_FREQ_16000 ); 622 623 mpeg_cap.channel_mode = ( MPEG_CHANNEL_MODE_JOINT_STEREO | 624 MPEG_CHANNEL_MODE_STEREO | 625 MPEG_CHANNEL_MODE_DUAL_CHANNEL | 626 MPEG_CHANNEL_MODE_MONO ); 627 628 mpeg_cap.layer = ( MPEG_LAYER_MP3 | MPEG_LAYER_MP2 | MPEG_LAYER_MP1 ); 629 630 mpeg_cap.bitrate = 0xFFFF; 631 632 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap, 633 sizeof(mpeg_cap)); 634 635 *caps = g_slist_append(*caps, media_codec); 636 637 if (get_all) { 638 struct avdtp_service_capability *delay_reporting; 639 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING, 640 NULL, 0); 641 *caps = g_slist_append(*caps, delay_reporting); 642 } 643 644 return TRUE; 645 } 646 647 static void endpoint_setconf_cb(struct media_endpoint *endpoint, void *ret, 648 int size, void *user_data) 649 { 650 struct a2dp_setup *setup = user_data; 651 652 if (ret == NULL) { 653 setup->err = g_new(struct avdtp_error, 1); 654 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC, 655 AVDTP_UNSUPPORTED_CONFIGURATION); 656 } 657 658 auto_config(setup); 659 } 660 661 static gboolean endpoint_setconf_ind(struct avdtp *session, 662 struct avdtp_local_sep *sep, 663 struct avdtp_stream *stream, 664 GSList *caps, 665 avdtp_set_configuration_cb cb, 666 void *user_data) 667 { 668 struct a2dp_sep *a2dp_sep = user_data; 669 struct a2dp_setup *setup; 670 671 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 672 DBG("Sink %p: Set_Configuration_Ind", sep); 673 else 674 DBG("Source %p: Set_Configuration_Ind", sep); 675 676 setup = a2dp_setup_get(session); 677 if (!session) 678 return FALSE; 679 680 a2dp_sep->stream = stream; 681 setup->sep = a2dp_sep; 682 setup->stream = stream; 683 setup->setconf_cb = cb; 684 685 for (; caps != NULL; caps = g_slist_next(caps)) { 686 struct avdtp_service_capability *cap = caps->data; 687 struct avdtp_media_codec_capability *codec; 688 gboolean ret; 689 690 if (cap->category == AVDTP_DELAY_REPORTING && 691 !a2dp_sep->delay_reporting) { 692 setup->err = g_new(struct avdtp_error, 1); 693 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING, 694 AVDTP_UNSUPPORTED_CONFIGURATION); 695 goto done; 696 } 697 698 if (cap->category != AVDTP_MEDIA_CODEC) 699 continue; 700 701 codec = (struct avdtp_media_codec_capability *) cap->data; 702 703 if (codec->media_codec_type != a2dp_sep->codec) { 704 setup->err = g_new(struct avdtp_error, 1); 705 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC, 706 AVDTP_UNSUPPORTED_CONFIGURATION); 707 goto done; 708 } 709 710 ret = media_endpoint_set_configuration(a2dp_sep->endpoint, 711 setup->dev, codec->data, 712 cap->length - sizeof(*codec), 713 endpoint_setconf_cb, setup); 714 if (ret) 715 return TRUE; 716 717 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC, 718 AVDTP_UNSUPPORTED_CONFIGURATION); 719 break; 720 } 721 722 done: 723 g_idle_add(auto_config, setup); 724 return TRUE; 725 } 726 727 static gboolean endpoint_getcap_ind(struct avdtp *session, 728 struct avdtp_local_sep *sep, 729 gboolean get_all, GSList **caps, 730 uint8_t *err, void *user_data) 731 { 732 struct a2dp_sep *a2dp_sep = user_data; 733 struct avdtp_service_capability *media_transport, *media_codec; 734 struct avdtp_media_codec_capability *codec_caps; 735 uint8_t *capabilities; 736 size_t length; 737 738 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 739 DBG("Sink %p: Get_Capability_Ind", sep); 740 else 741 DBG("Source %p: Get_Capability_Ind", sep); 742 743 *caps = NULL; 744 745 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, 746 NULL, 0); 747 748 *caps = g_slist_append(*caps, media_transport); 749 750 length = media_endpoint_get_capabilities(a2dp_sep->endpoint, 751 &capabilities); 752 753 codec_caps = g_malloc0(sizeof(*codec_caps) + length); 754 codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO; 755 codec_caps->media_codec_type = a2dp_sep->codec; 756 memcpy(codec_caps->data, capabilities, length); 757 758 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps, 759 sizeof(*codec_caps) + length); 760 761 *caps = g_slist_append(*caps, media_codec); 762 g_free(codec_caps); 763 764 if (get_all) { 765 struct avdtp_service_capability *delay_reporting; 766 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING, 767 NULL, 0); 768 *caps = g_slist_append(*caps, delay_reporting); 769 } 770 771 return TRUE; 772 } 773 774 static void endpoint_open_cb(struct media_endpoint *endpoint, void *ret, 775 int size, void *user_data) 776 { 777 struct a2dp_setup *setup = user_data; 778 int err; 779 780 if (ret == NULL) { 781 setup->stream = NULL; 782 finalize_setup_errno(setup, -EPERM, finalize_config, NULL); 783 return; 784 } 785 786 err = avdtp_open(setup->session, setup->stream); 787 if (err == 0) 788 return; 789 790 error("Error on avdtp_open %s (%d)", strerror(-err), -err); 791 setup->stream = NULL; 792 finalize_setup_errno(setup, err, finalize_config, NULL); 793 } 794 795 static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 796 struct avdtp_stream *stream, 797 struct avdtp_error *err, void *user_data) 798 { 799 struct a2dp_sep *a2dp_sep = user_data; 800 struct a2dp_setup *setup; 801 struct audio_device *dev; 802 int ret; 803 804 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 805 DBG("Sink %p: Set_Configuration_Cfm", sep); 806 else 807 DBG("Source %p: Set_Configuration_Cfm", sep); 808 809 setup = find_setup_by_session(session); 810 811 if (err) { 812 if (setup) { 813 setup->err = err; 814 finalize_config(setup); 815 } 816 return; 817 } 818 819 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep); 820 a2dp_sep->stream = stream; 821 822 if (!setup) 823 return; 824 825 dev = a2dp_get_dev(session); 826 827 /* Notify D-Bus interface of the new stream */ 828 if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE) 829 sink_new_stream(dev, session, setup->stream); 830 else 831 source_new_stream(dev, session, setup->stream); 832 833 /* Notify Endpoint */ 834 if (a2dp_sep->endpoint) { 835 struct avdtp_service_capability *service; 836 struct avdtp_media_codec_capability *codec; 837 838 service = avdtp_stream_get_codec(stream); 839 codec = (struct avdtp_media_codec_capability *) service->data; 840 841 if (media_endpoint_set_configuration(a2dp_sep->endpoint, dev, 842 codec->data, service->length - 843 sizeof(*codec), 844 endpoint_open_cb, setup) == 845 TRUE) 846 return; 847 848 setup->stream = NULL; 849 finalize_setup_errno(setup, -EPERM, finalize_config, NULL); 850 return; 851 } 852 853 ret = avdtp_open(session, stream); 854 if (ret < 0) { 855 error("Error on avdtp_open %s (%d)", strerror(-ret), -ret); 856 setup->stream = NULL; 857 finalize_setup_errno(setup, ret, finalize_config, NULL); 858 } 859 } 860 861 static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep, 862 uint8_t *err, void *user_data) 863 { 864 struct a2dp_sep *a2dp_sep = user_data; 865 866 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 867 DBG("Sink %p: Get_Configuration_Ind", sep); 868 else 869 DBG("Source %p: Get_Configuration_Ind", sep); 870 return TRUE; 871 } 872 873 static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 874 struct avdtp_stream *stream, struct avdtp_error *err, 875 void *user_data) 876 { 877 struct a2dp_sep *a2dp_sep = user_data; 878 879 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 880 DBG("Sink %p: Set_Configuration_Cfm", sep); 881 else 882 DBG("Source %p: Set_Configuration_Cfm", sep); 883 } 884 885 static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep, 886 struct avdtp_stream *stream, uint8_t *err, 887 void *user_data) 888 { 889 struct a2dp_sep *a2dp_sep = user_data; 890 891 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 892 DBG("Sink %p: Open_Ind", sep); 893 else 894 DBG("Source %p: Open_Ind", sep); 895 return TRUE; 896 } 897 898 static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 899 struct avdtp_stream *stream, struct avdtp_error *err, 900 void *user_data) 901 { 902 struct a2dp_sep *a2dp_sep = user_data; 903 struct a2dp_setup *setup; 904 905 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 906 DBG("Sink %p: Open_Cfm", sep); 907 else 908 DBG("Source %p: Open_Cfm", sep); 909 910 setup = find_setup_by_session(session); 911 if (!setup) 912 return; 913 914 if (setup->reconfigure) 915 setup->reconfigure = FALSE; 916 917 if (err) { 918 setup->stream = NULL; 919 setup->err = err; 920 } 921 922 finalize_config(setup); 923 } 924 925 static gboolean suspend_timeout(struct a2dp_sep *sep) 926 { 927 if (avdtp_suspend(sep->session, sep->stream) == 0) 928 sep->suspending = TRUE; 929 930 sep->suspend_timer = 0; 931 932 avdtp_unref(sep->session); 933 sep->session = NULL; 934 935 return FALSE; 936 } 937 938 static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep, 939 struct avdtp_stream *stream, uint8_t *err, 940 void *user_data) 941 { 942 struct a2dp_sep *a2dp_sep = user_data; 943 struct a2dp_setup *setup; 944 945 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 946 DBG("Sink %p: Start_Ind", sep); 947 else 948 DBG("Source %p: Start_Ind", sep); 949 950 setup = find_setup_by_session(session); 951 if (setup) 952 finalize_resume(setup); 953 954 if (!a2dp_sep->locked) { 955 a2dp_sep->session = avdtp_ref(session); 956 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT, 957 (GSourceFunc) suspend_timeout, 958 a2dp_sep); 959 } 960 961 return TRUE; 962 } 963 964 static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 965 struct avdtp_stream *stream, struct avdtp_error *err, 966 void *user_data) 967 { 968 struct a2dp_sep *a2dp_sep = user_data; 969 struct a2dp_setup *setup; 970 971 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 972 DBG("Sink %p: Start_Cfm", sep); 973 else 974 DBG("Source %p: Start_Cfm", sep); 975 976 setup = find_setup_by_session(session); 977 if (!setup) 978 return; 979 980 if (err) { 981 setup->stream = NULL; 982 setup->err = err; 983 } 984 985 finalize_resume(setup); 986 } 987 988 static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep, 989 struct avdtp_stream *stream, uint8_t *err, 990 void *user_data) 991 { 992 struct a2dp_sep *a2dp_sep = user_data; 993 994 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 995 DBG("Sink %p: Suspend_Ind", sep); 996 else 997 DBG("Source %p: Suspend_Ind", sep); 998 999 if (a2dp_sep->suspend_timer) { 1000 g_source_remove(a2dp_sep->suspend_timer); 1001 a2dp_sep->suspend_timer = 0; 1002 avdtp_unref(a2dp_sep->session); 1003 a2dp_sep->session = NULL; 1004 } 1005 1006 return TRUE; 1007 } 1008 1009 static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 1010 struct avdtp_stream *stream, struct avdtp_error *err, 1011 void *user_data) 1012 { 1013 struct a2dp_sep *a2dp_sep = user_data; 1014 struct a2dp_setup *setup; 1015 gboolean start; 1016 int perr; 1017 1018 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1019 DBG("Sink %p: Suspend_Cfm", sep); 1020 else 1021 DBG("Source %p: Suspend_Cfm", sep); 1022 1023 a2dp_sep->suspending = FALSE; 1024 1025 setup = find_setup_by_session(session); 1026 if (!setup) 1027 return; 1028 1029 start = setup->start; 1030 setup->start = FALSE; 1031 1032 if (err) { 1033 setup->stream = NULL; 1034 setup->err = err; 1035 } 1036 1037 finalize_suspend(setup); 1038 1039 if (!start) 1040 return; 1041 1042 if (err) { 1043 finalize_resume(setup); 1044 return; 1045 } 1046 1047 perr = avdtp_start(session, a2dp_sep->stream); 1048 if (perr < 0) { 1049 error("Error on avdtp_start %s (%d)", strerror(-perr), -perr); 1050 finalize_setup_errno(setup, -EIO, finalize_suspend, NULL); 1051 } 1052 } 1053 1054 static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep, 1055 struct avdtp_stream *stream, uint8_t *err, 1056 void *user_data) 1057 { 1058 struct a2dp_sep *a2dp_sep = user_data; 1059 struct a2dp_setup *setup; 1060 1061 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1062 DBG("Sink %p: Close_Ind", sep); 1063 else 1064 DBG("Source %p: Close_Ind", sep); 1065 1066 setup = find_setup_by_session(session); 1067 if (!setup) 1068 return TRUE; 1069 1070 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend, 1071 finalize_resume, NULL); 1072 1073 return TRUE; 1074 } 1075 1076 static gboolean a2dp_reconfigure(gpointer data) 1077 { 1078 struct a2dp_setup *setup = data; 1079 struct a2dp_sep *sep = setup->sep; 1080 int posix_err; 1081 struct avdtp_media_codec_capability *rsep_codec; 1082 struct avdtp_service_capability *cap; 1083 1084 if (setup->rsep) { 1085 cap = avdtp_get_codec(setup->rsep); 1086 rsep_codec = (struct avdtp_media_codec_capability *) cap->data; 1087 } 1088 1089 if (!setup->rsep || sep->codec != rsep_codec->media_codec_type) 1090 setup->rsep = avdtp_find_remote_sep(setup->session, sep->lsep); 1091 1092 posix_err = avdtp_set_configuration(setup->session, setup->rsep, 1093 sep->lsep, 1094 setup->caps, 1095 &setup->stream); 1096 if (posix_err < 0) { 1097 error("avdtp_set_configuration: %s", strerror(-posix_err)); 1098 goto failed; 1099 } 1100 1101 return FALSE; 1102 1103 failed: 1104 finalize_setup_errno(setup, posix_err, finalize_config, NULL); 1105 return FALSE; 1106 } 1107 1108 static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 1109 struct avdtp_stream *stream, struct avdtp_error *err, 1110 void *user_data) 1111 { 1112 struct a2dp_sep *a2dp_sep = user_data; 1113 struct a2dp_setup *setup; 1114 1115 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1116 DBG("Sink %p: Close_Cfm", sep); 1117 else 1118 DBG("Source %p: Close_Cfm", sep); 1119 1120 setup = find_setup_by_session(session); 1121 if (!setup) 1122 return; 1123 1124 if (err) { 1125 setup->stream = NULL; 1126 setup->err = err; 1127 finalize_config(setup); 1128 return; 1129 } 1130 1131 if (!setup->rsep) 1132 setup->rsep = avdtp_stream_get_remote_sep(stream); 1133 1134 if (setup->reconfigure) 1135 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup); 1136 } 1137 1138 static gboolean abort_ind(struct avdtp *session, struct avdtp_local_sep *sep, 1139 struct avdtp_stream *stream, uint8_t *err, 1140 void *user_data) 1141 { 1142 struct a2dp_sep *a2dp_sep = user_data; 1143 1144 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1145 DBG("Sink %p: Abort_Ind", sep); 1146 else 1147 DBG("Source %p: Abort_Ind", sep); 1148 1149 a2dp_sep->stream = NULL; 1150 1151 return TRUE; 1152 } 1153 1154 static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 1155 struct avdtp_stream *stream, struct avdtp_error *err, 1156 void *user_data) 1157 { 1158 struct a2dp_sep *a2dp_sep = user_data; 1159 struct a2dp_setup *setup; 1160 1161 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1162 DBG("Sink %p: Abort_Cfm", sep); 1163 else 1164 DBG("Source %p: Abort_Cfm", sep); 1165 1166 setup = find_setup_by_session(session); 1167 if (!setup) 1168 return; 1169 1170 setup_unref(setup); 1171 } 1172 1173 static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep, 1174 uint8_t *err, void *user_data) 1175 { 1176 struct a2dp_sep *a2dp_sep = user_data; 1177 1178 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1179 DBG("Sink %p: ReConfigure_Ind", sep); 1180 else 1181 DBG("Source %p: ReConfigure_Ind", sep); 1182 1183 return TRUE; 1184 } 1185 1186 static gboolean delayreport_ind(struct avdtp *session, 1187 struct avdtp_local_sep *sep, 1188 uint8_t rseid, uint16_t delay, 1189 uint8_t *err, void *user_data) 1190 { 1191 struct a2dp_sep *a2dp_sep = user_data; 1192 struct audio_device *dev = a2dp_get_dev(session); 1193 1194 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1195 DBG("Sink %p: DelayReport_Ind", sep); 1196 else 1197 DBG("Source %p: DelayReport_Ind", sep); 1198 1199 unix_delay_report(dev, rseid, delay); 1200 1201 return TRUE; 1202 } 1203 1204 static gboolean endpoint_delayreport_ind(struct avdtp *session, 1205 struct avdtp_local_sep *sep, 1206 uint8_t rseid, uint16_t delay, 1207 uint8_t *err, void *user_data) 1208 { 1209 struct a2dp_sep *a2dp_sep = user_data; 1210 struct media_transport *transport; 1211 1212 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1213 DBG("Sink %p: DelayReport_Ind", sep); 1214 else 1215 DBG("Source %p: DelayReport_Ind", sep); 1216 1217 transport = media_endpoint_get_transport(a2dp_sep->endpoint); 1218 if (transport == NULL) 1219 return FALSE; 1220 1221 media_transport_update_delay(transport, delay); 1222 1223 return TRUE; 1224 } 1225 1226 static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 1227 struct avdtp_stream *stream, struct avdtp_error *err, 1228 void *user_data) 1229 { 1230 struct a2dp_sep *a2dp_sep = user_data; 1231 struct a2dp_setup *setup; 1232 1233 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1234 DBG("Sink %p: ReConfigure_Cfm", sep); 1235 else 1236 DBG("Source %p: ReConfigure_Cfm", sep); 1237 1238 setup = find_setup_by_session(session); 1239 if (!setup) 1240 return; 1241 1242 if (err) { 1243 setup->stream = NULL; 1244 setup->err = err; 1245 } 1246 1247 finalize_config(setup); 1248 } 1249 1250 static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep, 1251 struct avdtp_stream *stream, 1252 struct avdtp_error *err, void *user_data) 1253 { 1254 struct a2dp_sep *a2dp_sep = user_data; 1255 1256 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) 1257 DBG("Sink %p: DelayReport_Cfm", sep); 1258 else 1259 DBG("Source %p: DelayReport_Cfm", sep); 1260 } 1261 1262 static struct avdtp_sep_cfm cfm = { 1263 .set_configuration = setconf_cfm, 1264 .get_configuration = getconf_cfm, 1265 .open = open_cfm, 1266 .start = start_cfm, 1267 .suspend = suspend_cfm, 1268 .close = close_cfm, 1269 .abort = abort_cfm, 1270 .reconfigure = reconf_cfm, 1271 .delay_report = delay_report_cfm, 1272 }; 1273 1274 static struct avdtp_sep_ind sbc_ind = { 1275 .get_capability = sbc_getcap_ind, 1276 .set_configuration = sbc_setconf_ind, 1277 .get_configuration = getconf_ind, 1278 .open = open_ind, 1279 .start = start_ind, 1280 .suspend = suspend_ind, 1281 .close = close_ind, 1282 .abort = abort_ind, 1283 .reconfigure = reconf_ind, 1284 .delayreport = delayreport_ind, 1285 }; 1286 1287 static struct avdtp_sep_ind mpeg_ind = { 1288 .get_capability = mpeg_getcap_ind, 1289 .set_configuration = mpeg_setconf_ind, 1290 .get_configuration = getconf_ind, 1291 .open = open_ind, 1292 .start = start_ind, 1293 .suspend = suspend_ind, 1294 .close = close_ind, 1295 .abort = abort_ind, 1296 .reconfigure = reconf_ind, 1297 .delayreport = delayreport_ind, 1298 }; 1299 1300 static struct avdtp_sep_ind endpoint_ind = { 1301 .get_capability = endpoint_getcap_ind, 1302 .set_configuration = endpoint_setconf_ind, 1303 .get_configuration = getconf_ind, 1304 .open = open_ind, 1305 .start = start_ind, 1306 .suspend = suspend_ind, 1307 .close = close_ind, 1308 .abort = abort_ind, 1309 .reconfigure = reconf_ind, 1310 .delayreport = endpoint_delayreport_ind, 1311 }; 1312 1313 static sdp_record_t *a2dp_record(uint8_t type, uint16_t avdtp_ver) 1314 { 1315 sdp_list_t *svclass_id, *pfseq, *apseq, *root; 1316 uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid; 1317 sdp_profile_desc_t profile[1]; 1318 sdp_list_t *aproto, *proto[2]; 1319 sdp_record_t *record; 1320 sdp_data_t *psm, *version, *features; 1321 uint16_t lp = AVDTP_UUID; 1322 uint16_t a2dp_ver = 0x0102, feat = 0x000f; 1323 1324 #ifdef ANDROID 1325 feat = 0x0001; 1326 #endif 1327 record = sdp_record_alloc(); 1328 if (!record) 1329 return NULL; 1330 1331 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP); 1332 root = sdp_list_append(0, &root_uuid); 1333 sdp_set_browse_groups(record, root); 1334 1335 if (type == AVDTP_SEP_TYPE_SOURCE) 1336 sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID); 1337 else 1338 sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID); 1339 svclass_id = sdp_list_append(0, &a2dp_uuid); 1340 sdp_set_service_classes(record, svclass_id); 1341 1342 sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID); 1343 profile[0].version = a2dp_ver; 1344 pfseq = sdp_list_append(0, &profile[0]); 1345 sdp_set_profile_descs(record, pfseq); 1346 1347 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID); 1348 proto[0] = sdp_list_append(0, &l2cap_uuid); 1349 psm = sdp_data_alloc(SDP_UINT16, &lp); 1350 proto[0] = sdp_list_append(proto[0], psm); 1351 apseq = sdp_list_append(0, proto[0]); 1352 1353 sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID); 1354 proto[1] = sdp_list_append(0, &avdtp_uuid); 1355 version = sdp_data_alloc(SDP_UINT16, &avdtp_ver); 1356 proto[1] = sdp_list_append(proto[1], version); 1357 apseq = sdp_list_append(apseq, proto[1]); 1358 1359 aproto = sdp_list_append(0, apseq); 1360 sdp_set_access_protos(record, aproto); 1361 1362 features = sdp_data_alloc(SDP_UINT16, &feat); 1363 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features); 1364 1365 if (type == AVDTP_SEP_TYPE_SOURCE) 1366 sdp_set_info_attr(record, "Audio Source", 0, 0); 1367 else 1368 sdp_set_info_attr(record, "Audio Sink", 0, 0); 1369 1370 free(psm); 1371 free(version); 1372 sdp_list_free(proto[0], 0); 1373 sdp_list_free(proto[1], 0); 1374 sdp_list_free(apseq, 0); 1375 sdp_list_free(pfseq, 0); 1376 sdp_list_free(aproto, 0); 1377 sdp_list_free(root, 0); 1378 sdp_list_free(svclass_id, 0); 1379 1380 return record; 1381 } 1382 1383 static struct a2dp_server *find_server(GSList *list, const bdaddr_t *src) 1384 { 1385 1386 for (; list; list = list->next) { 1387 struct a2dp_server *server = list->data; 1388 1389 if (bacmp(&server->src, src) == 0) 1390 return server; 1391 } 1392 1393 return NULL; 1394 } 1395 1396 int a2dp_register(DBusConnection *conn, const bdaddr_t *src, GKeyFile *config) 1397 { 1398 int sbc_srcs = 1, sbc_sinks = 1; 1399 int mpeg12_srcs = 0, mpeg12_sinks = 0; 1400 gboolean source = TRUE, sink = FALSE, socket = TRUE; 1401 gboolean delay_reporting = FALSE; 1402 char *str; 1403 GError *err = NULL; 1404 int i; 1405 struct a2dp_server *server; 1406 1407 if (!config) 1408 goto proceed; 1409 1410 str = g_key_file_get_string(config, "General", "Enable", &err); 1411 1412 if (err) { 1413 DBG("audio.conf: %s", err->message); 1414 g_clear_error(&err); 1415 } else { 1416 if (strstr(str, "Sink")) 1417 source = TRUE; 1418 if (strstr(str, "Source")) 1419 sink = TRUE; 1420 g_free(str); 1421 } 1422 1423 str = g_key_file_get_string(config, "General", "Disable", &err); 1424 1425 if (err) { 1426 DBG("audio.conf: %s", err->message); 1427 g_clear_error(&err); 1428 } else { 1429 if (strstr(str, "Sink")) 1430 source = FALSE; 1431 if (strstr(str, "Source")) 1432 sink = FALSE; 1433 if (strstr(str, "Socket")) 1434 socket = FALSE; 1435 g_free(str); 1436 } 1437 1438 /* Don't register any local sep if Socket is disabled */ 1439 if (socket == FALSE) { 1440 sbc_srcs = 0; 1441 sbc_sinks = 0; 1442 mpeg12_srcs = 0; 1443 mpeg12_sinks = 0; 1444 goto proceed; 1445 } 1446 1447 str = g_key_file_get_string(config, "A2DP", "SBCSources", &err); 1448 if (err) { 1449 DBG("audio.conf: %s", err->message); 1450 g_clear_error(&err); 1451 } else { 1452 sbc_srcs = atoi(str); 1453 g_free(str); 1454 } 1455 1456 str = g_key_file_get_string(config, "A2DP", "MPEG12Sources", &err); 1457 if (err) { 1458 DBG("audio.conf: %s", err->message); 1459 g_clear_error(&err); 1460 } else { 1461 mpeg12_srcs = atoi(str); 1462 g_free(str); 1463 } 1464 1465 str = g_key_file_get_string(config, "A2DP", "SBCSinks", &err); 1466 if (err) { 1467 DBG("audio.conf: %s", err->message); 1468 g_clear_error(&err); 1469 } else { 1470 sbc_sinks = atoi(str); 1471 g_free(str); 1472 } 1473 1474 str = g_key_file_get_string(config, "A2DP", "MPEG12Sinks", &err); 1475 if (err) { 1476 DBG("audio.conf: %s", err->message); 1477 g_clear_error(&err); 1478 } else { 1479 mpeg12_sinks = atoi(str); 1480 g_free(str); 1481 } 1482 1483 proceed: 1484 if (!connection) 1485 connection = dbus_connection_ref(conn); 1486 1487 server = find_server(servers, src); 1488 if (!server) { 1489 int av_err; 1490 1491 server = g_new0(struct a2dp_server, 1); 1492 if (!server) 1493 return -ENOMEM; 1494 1495 av_err = avdtp_init(src, config, &server->version); 1496 if (av_err < 0) { 1497 g_free(server); 1498 return av_err; 1499 } 1500 1501 bacpy(&server->src, src); 1502 servers = g_slist_append(servers, server); 1503 } 1504 1505 if (config) 1506 delay_reporting = g_key_file_get_boolean(config, "A2DP", 1507 "DelayReporting", NULL); 1508 1509 if (delay_reporting) 1510 server->version = 0x0103; 1511 else 1512 server->version = 0x0102; 1513 1514 server->source_enabled = source; 1515 if (source) { 1516 for (i = 0; i < sbc_srcs; i++) 1517 a2dp_add_sep(src, AVDTP_SEP_TYPE_SOURCE, 1518 A2DP_CODEC_SBC, delay_reporting, NULL, NULL); 1519 1520 for (i = 0; i < mpeg12_srcs; i++) 1521 a2dp_add_sep(src, AVDTP_SEP_TYPE_SOURCE, 1522 A2DP_CODEC_MPEG12, delay_reporting, 1523 NULL, NULL); 1524 } 1525 server->sink_enabled = sink; 1526 if (sink) { 1527 for (i = 0; i < sbc_sinks; i++) 1528 a2dp_add_sep(src, AVDTP_SEP_TYPE_SINK, 1529 A2DP_CODEC_SBC, delay_reporting, NULL, NULL); 1530 1531 for (i = 0; i < mpeg12_sinks; i++) 1532 a2dp_add_sep(src, AVDTP_SEP_TYPE_SINK, 1533 A2DP_CODEC_MPEG12, delay_reporting, 1534 NULL, NULL); 1535 } 1536 1537 return 0; 1538 } 1539 1540 static void a2dp_unregister_sep(struct a2dp_sep *sep) 1541 { 1542 if (sep->endpoint) { 1543 media_endpoint_release(sep->endpoint); 1544 sep->endpoint = NULL; 1545 } 1546 1547 avdtp_unregister_sep(sep->lsep); 1548 g_free(sep); 1549 } 1550 1551 void a2dp_unregister(const bdaddr_t *src) 1552 { 1553 struct a2dp_server *server; 1554 1555 server = find_server(servers, src); 1556 if (!server) 1557 return; 1558 1559 g_slist_foreach(server->sinks, (GFunc) a2dp_remove_sep, NULL); 1560 g_slist_free(server->sinks); 1561 1562 g_slist_foreach(server->sources, (GFunc) a2dp_remove_sep, NULL); 1563 g_slist_free(server->sources); 1564 1565 avdtp_exit(src); 1566 1567 servers = g_slist_remove(servers, server); 1568 g_free(server); 1569 1570 if (servers) 1571 return; 1572 1573 dbus_connection_unref(connection); 1574 connection = NULL; 1575 } 1576 1577 struct a2dp_sep *a2dp_add_sep(const bdaddr_t *src, uint8_t type, 1578 uint8_t codec, gboolean delay_reporting, 1579 struct media_endpoint *endpoint, int *err) 1580 { 1581 struct a2dp_server *server; 1582 struct a2dp_sep *sep; 1583 GSList **l; 1584 uint32_t *record_id; 1585 sdp_record_t *record; 1586 struct avdtp_sep_ind *ind; 1587 1588 server = find_server(servers, src); 1589 if (server == NULL) { 1590 if (err) 1591 *err = -EINVAL; 1592 return NULL; 1593 } 1594 1595 if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) { 1596 if (err) 1597 *err = -EPROTONOSUPPORT; 1598 return NULL; 1599 } 1600 1601 if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) { 1602 if (err) 1603 *err = -EPROTONOSUPPORT; 1604 return NULL; 1605 } 1606 1607 sep = g_new0(struct a2dp_sep, 1); 1608 1609 if (endpoint) { 1610 ind = &endpoint_ind; 1611 goto proceed; 1612 } 1613 1614 ind = (codec == A2DP_CODEC_MPEG12) ? &mpeg_ind : &sbc_ind; 1615 1616 proceed: 1617 sep->lsep = avdtp_register_sep(&server->src, type, 1618 AVDTP_MEDIA_TYPE_AUDIO, codec, 1619 delay_reporting, ind, &cfm, sep); 1620 if (sep->lsep == NULL) { 1621 g_free(sep); 1622 if (err) 1623 *err = -EINVAL; 1624 return NULL; 1625 } 1626 1627 sep->server = server; 1628 sep->endpoint = endpoint; 1629 sep->codec = codec; 1630 sep->type = type; 1631 sep->delay_reporting = delay_reporting; 1632 1633 if (type == AVDTP_SEP_TYPE_SOURCE) { 1634 l = &server->sources; 1635 record_id = &server->source_record_id; 1636 } else { 1637 l = &server->sinks; 1638 record_id = &server->sink_record_id; 1639 } 1640 1641 if (*record_id != 0) 1642 goto add; 1643 1644 record = a2dp_record(type, server->version); 1645 if (!record) { 1646 error("Unable to allocate new service record"); 1647 avdtp_unregister_sep(sep->lsep); 1648 g_free(sep); 1649 if (err) 1650 *err = -EINVAL; 1651 return NULL; 1652 } 1653 1654 if (add_record_to_server(&server->src, record) < 0) { 1655 error("Unable to register A2DP service record");\ 1656 sdp_record_free(record); 1657 avdtp_unregister_sep(sep->lsep); 1658 g_free(sep); 1659 if (err) 1660 *err = -EINVAL; 1661 return NULL; 1662 } 1663 *record_id = record->handle; 1664 1665 add: 1666 *l = g_slist_append(*l, sep); 1667 1668 if (err) 1669 *err = 0; 1670 return sep; 1671 } 1672 1673 void a2dp_remove_sep(struct a2dp_sep *sep) 1674 { 1675 struct a2dp_server *server = sep->server; 1676 1677 if (sep->type == AVDTP_SEP_TYPE_SOURCE) { 1678 if (g_slist_find(server->sources, sep) == NULL) 1679 return; 1680 server->sources = g_slist_remove(server->sources, sep); 1681 if (server->sources == NULL && server->source_record_id) { 1682 remove_record_from_server(server->source_record_id); 1683 server->source_record_id = 0; 1684 } 1685 } else { 1686 if (g_slist_find(server->sinks, sep) == NULL) 1687 return; 1688 server->sinks = g_slist_remove(server->sinks, sep); 1689 if (server->sinks == NULL && server->sink_record_id) { 1690 remove_record_from_server(server->sink_record_id); 1691 server->sink_record_id = 0; 1692 } 1693 } 1694 1695 if (sep->locked) 1696 return; 1697 1698 a2dp_unregister_sep(sep); 1699 } 1700 1701 struct a2dp_sep *a2dp_get(struct avdtp *session, 1702 struct avdtp_remote_sep *rsep) 1703 { 1704 GSList *l; 1705 struct a2dp_server *server; 1706 struct avdtp_service_capability *cap; 1707 struct avdtp_media_codec_capability *codec_cap = NULL; 1708 bdaddr_t src; 1709 1710 avdtp_get_peers(session, &src, NULL); 1711 server = find_server(servers, &src); 1712 if (!server) 1713 return NULL; 1714 1715 cap = avdtp_get_codec(rsep); 1716 codec_cap = (void *) cap->data; 1717 1718 if (avdtp_get_type(rsep) == AVDTP_SEP_TYPE_SINK) 1719 l = server->sources; 1720 else 1721 l = server->sinks; 1722 1723 for (; l != NULL; l = l->next) { 1724 struct a2dp_sep *sep = l->data; 1725 1726 if (sep->locked) 1727 continue; 1728 1729 if (sep->codec != codec_cap->media_codec_type) 1730 continue; 1731 1732 if (!sep->stream || avdtp_has_stream(session, sep->stream)) 1733 return sep; 1734 } 1735 1736 return NULL; 1737 } 1738 1739 static uint8_t default_bitpool(uint8_t freq, uint8_t mode) 1740 { 1741 switch (freq) { 1742 case SBC_SAMPLING_FREQ_16000: 1743 case SBC_SAMPLING_FREQ_32000: 1744 return 53; 1745 case SBC_SAMPLING_FREQ_44100: 1746 switch (mode) { 1747 case SBC_CHANNEL_MODE_MONO: 1748 case SBC_CHANNEL_MODE_DUAL_CHANNEL: 1749 return 31; 1750 case SBC_CHANNEL_MODE_STEREO: 1751 case SBC_CHANNEL_MODE_JOINT_STEREO: 1752 return 53; 1753 default: 1754 error("Invalid channel mode %u", mode); 1755 return 53; 1756 } 1757 case SBC_SAMPLING_FREQ_48000: 1758 switch (mode) { 1759 case SBC_CHANNEL_MODE_MONO: 1760 case SBC_CHANNEL_MODE_DUAL_CHANNEL: 1761 return 29; 1762 case SBC_CHANNEL_MODE_STEREO: 1763 case SBC_CHANNEL_MODE_JOINT_STEREO: 1764 return 51; 1765 default: 1766 error("Invalid channel mode %u", mode); 1767 return 51; 1768 } 1769 default: 1770 error("Invalid sampling freq %u", freq); 1771 return 53; 1772 } 1773 } 1774 1775 static gboolean select_sbc_params(struct sbc_codec_cap *cap, 1776 struct sbc_codec_cap *supported) 1777 { 1778 unsigned int max_bitpool, min_bitpool; 1779 1780 memset(cap, 0, sizeof(struct sbc_codec_cap)); 1781 1782 cap->cap.media_type = AVDTP_MEDIA_TYPE_AUDIO; 1783 cap->cap.media_codec_type = A2DP_CODEC_SBC; 1784 1785 if (supported->frequency & SBC_SAMPLING_FREQ_44100) 1786 cap->frequency = SBC_SAMPLING_FREQ_44100; 1787 else if (supported->frequency & SBC_SAMPLING_FREQ_48000) 1788 cap->frequency = SBC_SAMPLING_FREQ_48000; 1789 else if (supported->frequency & SBC_SAMPLING_FREQ_32000) 1790 cap->frequency = SBC_SAMPLING_FREQ_32000; 1791 else if (supported->frequency & SBC_SAMPLING_FREQ_16000) 1792 cap->frequency = SBC_SAMPLING_FREQ_16000; 1793 else { 1794 error("No supported frequencies"); 1795 return FALSE; 1796 } 1797 1798 if (supported->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO) 1799 cap->channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO; 1800 else if (supported->channel_mode & SBC_CHANNEL_MODE_STEREO) 1801 cap->channel_mode = SBC_CHANNEL_MODE_STEREO; 1802 else if (supported->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL) 1803 cap->channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL; 1804 else if (supported->channel_mode & SBC_CHANNEL_MODE_MONO) 1805 cap->channel_mode = SBC_CHANNEL_MODE_MONO; 1806 else { 1807 error("No supported channel modes"); 1808 return FALSE; 1809 } 1810 1811 if (supported->block_length & SBC_BLOCK_LENGTH_16) 1812 cap->block_length = SBC_BLOCK_LENGTH_16; 1813 else if (supported->block_length & SBC_BLOCK_LENGTH_12) 1814 cap->block_length = SBC_BLOCK_LENGTH_12; 1815 else if (supported->block_length & SBC_BLOCK_LENGTH_8) 1816 cap->block_length = SBC_BLOCK_LENGTH_8; 1817 else if (supported->block_length & SBC_BLOCK_LENGTH_4) 1818 cap->block_length = SBC_BLOCK_LENGTH_4; 1819 else { 1820 error("No supported block lengths"); 1821 return FALSE; 1822 } 1823 1824 if (supported->subbands & SBC_SUBBANDS_8) 1825 cap->subbands = SBC_SUBBANDS_8; 1826 else if (supported->subbands & SBC_SUBBANDS_4) 1827 cap->subbands = SBC_SUBBANDS_4; 1828 else { 1829 error("No supported subbands"); 1830 return FALSE; 1831 } 1832 1833 if (supported->allocation_method & SBC_ALLOCATION_LOUDNESS) 1834 cap->allocation_method = SBC_ALLOCATION_LOUDNESS; 1835 else if (supported->allocation_method & SBC_ALLOCATION_SNR) 1836 cap->allocation_method = SBC_ALLOCATION_SNR; 1837 1838 min_bitpool = MAX(MIN_BITPOOL, supported->min_bitpool); 1839 max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode), 1840 supported->max_bitpool); 1841 1842 cap->min_bitpool = min_bitpool; 1843 cap->max_bitpool = max_bitpool; 1844 1845 return TRUE; 1846 } 1847 1848 static gboolean select_capabilities(struct avdtp *session, 1849 struct avdtp_remote_sep *rsep, 1850 GSList **caps) 1851 { 1852 struct avdtp_service_capability *media_transport, *media_codec; 1853 struct sbc_codec_cap sbc_cap; 1854 1855 media_codec = avdtp_get_codec(rsep); 1856 if (!media_codec) 1857 return FALSE; 1858 1859 select_sbc_params(&sbc_cap, (struct sbc_codec_cap *) media_codec->data); 1860 1861 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, 1862 NULL, 0); 1863 1864 *caps = g_slist_append(*caps, media_transport); 1865 1866 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap, 1867 sizeof(sbc_cap)); 1868 1869 *caps = g_slist_append(*caps, media_codec); 1870 1871 if (avdtp_get_delay_reporting(rsep)) { 1872 struct avdtp_service_capability *delay_reporting; 1873 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING, 1874 NULL, 0); 1875 *caps = g_slist_append(*caps, delay_reporting); 1876 } 1877 1878 return TRUE; 1879 } 1880 1881 static void select_cb(struct media_endpoint *endpoint, void *ret, int size, 1882 void *user_data) 1883 { 1884 struct a2dp_setup *setup = user_data; 1885 struct avdtp_service_capability *media_transport, *media_codec; 1886 struct avdtp_media_codec_capability *cap; 1887 1888 if (size < 0) { 1889 DBG("Endpoint replied an invalid configuration"); 1890 goto done; 1891 } 1892 1893 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, 1894 NULL, 0); 1895 1896 setup->caps = g_slist_append(setup->caps, media_transport); 1897 1898 cap = g_malloc0(sizeof(*cap) + size); 1899 cap->media_type = AVDTP_MEDIA_TYPE_AUDIO; 1900 cap->media_codec_type = setup->sep->codec; 1901 memcpy(cap->data, ret, size); 1902 1903 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap, 1904 sizeof(*cap) + size); 1905 1906 setup->caps = g_slist_append(setup->caps, media_codec); 1907 g_free(cap); 1908 1909 done: 1910 finalize_select(setup); 1911 } 1912 1913 static gboolean auto_select(gpointer data) 1914 { 1915 struct a2dp_setup *setup = data; 1916 1917 finalize_select(setup); 1918 1919 return FALSE; 1920 } 1921 1922 static struct a2dp_sep *a2dp_find_sep(struct avdtp *session, GSList *list, 1923 const char *sender) 1924 { 1925 for (; list; list = list->next) { 1926 struct a2dp_sep *sep = list->data; 1927 1928 /* Use sender's endpoint if available */ 1929 if (sender) { 1930 const char *name; 1931 1932 if (sep->endpoint == NULL) 1933 continue; 1934 1935 name = media_endpoint_get_sender(sep->endpoint); 1936 if (g_strcmp0(sender, name) != 0) 1937 continue; 1938 } 1939 1940 if (avdtp_find_remote_sep(session, sep->lsep) == NULL) 1941 continue; 1942 1943 return sep; 1944 } 1945 1946 return NULL; 1947 } 1948 1949 static struct a2dp_sep *a2dp_select_sep(struct avdtp *session, uint8_t type, 1950 const char *sender) 1951 { 1952 struct a2dp_server *server; 1953 struct a2dp_sep *sep; 1954 GSList *l; 1955 bdaddr_t src; 1956 1957 avdtp_get_peers(session, &src, NULL); 1958 server = find_server(servers, &src); 1959 if (!server) 1960 return NULL; 1961 1962 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks; 1963 1964 /* Check sender's seps first */ 1965 sep = a2dp_find_sep(session, l, sender); 1966 if (sep != NULL) 1967 return sep; 1968 1969 return a2dp_find_sep(session, l, NULL); 1970 } 1971 1972 unsigned int a2dp_select_capabilities(struct avdtp *session, 1973 uint8_t type, const char *sender, 1974 a2dp_select_cb_t cb, 1975 void *user_data) 1976 { 1977 struct a2dp_setup *setup; 1978 struct a2dp_setup_cb *cb_data; 1979 struct a2dp_sep *sep; 1980 struct avdtp_service_capability *service; 1981 struct avdtp_media_codec_capability *codec; 1982 1983 sep = a2dp_select_sep(session, type, sender); 1984 if (!sep) { 1985 error("Unable to select SEP"); 1986 return 0; 1987 } 1988 1989 setup = a2dp_setup_get(session); 1990 if (!setup) 1991 return 0; 1992 1993 cb_data = setup_cb_new(setup); 1994 cb_data->select_cb = cb; 1995 cb_data->user_data = user_data; 1996 1997 setup->sep = sep; 1998 setup->rsep = avdtp_find_remote_sep(session, sep->lsep); 1999 2000 if (setup->rsep == NULL) { 2001 error("Could not find remote sep"); 2002 goto fail; 2003 } 2004 2005 /* FIXME: Remove auto select when it is not longer possible to register 2006 endpoint in the configuration file */ 2007 if (sep->endpoint == NULL) { 2008 if (!select_capabilities(session, setup->rsep, 2009 &setup->caps)) { 2010 error("Unable to auto select remote SEP capabilities"); 2011 goto fail; 2012 } 2013 2014 g_idle_add(auto_select, setup); 2015 2016 return cb_data->id; 2017 } 2018 2019 service = avdtp_get_codec(setup->rsep); 2020 codec = (struct avdtp_media_codec_capability *) service->data; 2021 2022 if (media_endpoint_select_configuration(sep->endpoint, codec->data, 2023 service->length - sizeof(*codec), 2024 select_cb, setup) == 2025 TRUE) 2026 return cb_data->id; 2027 2028 fail: 2029 setup_cb_free(cb_data); 2030 return 0; 2031 2032 } 2033 2034 unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep, 2035 a2dp_config_cb_t cb, GSList *caps, 2036 void *user_data) 2037 { 2038 struct a2dp_setup_cb *cb_data; 2039 GSList *l; 2040 struct a2dp_server *server; 2041 struct a2dp_setup *setup; 2042 struct a2dp_sep *tmp; 2043 struct avdtp_service_capability *cap; 2044 struct avdtp_media_codec_capability *codec_cap = NULL; 2045 int posix_err; 2046 bdaddr_t src; 2047 2048 avdtp_get_peers(session, &src, NULL); 2049 server = find_server(servers, &src); 2050 if (!server) 2051 return 0; 2052 2053 for (l = caps; l != NULL; l = l->next) { 2054 cap = l->data; 2055 2056 if (cap->category != AVDTP_MEDIA_CODEC) 2057 continue; 2058 2059 codec_cap = (void *) cap->data; 2060 break; 2061 } 2062 2063 if (!codec_cap) 2064 return 0; 2065 2066 if (sep->codec != codec_cap->media_codec_type) 2067 return 0; 2068 2069 DBG("a2dp_config: selected SEP %p", sep->lsep); 2070 2071 setup = a2dp_setup_get(session); 2072 if (!setup) 2073 return 0; 2074 2075 cb_data = setup_cb_new(setup); 2076 cb_data->config_cb = cb; 2077 cb_data->user_data = user_data; 2078 2079 setup->sep = sep; 2080 setup->stream = sep->stream; 2081 2082 /* Copy given caps if they are different than current caps */ 2083 if (setup->caps != caps) { 2084 g_slist_foreach(setup->caps, (GFunc) g_free, NULL); 2085 g_slist_free(setup->caps); 2086 setup->caps = g_slist_copy(caps); 2087 } 2088 2089 switch (avdtp_sep_get_state(sep->lsep)) { 2090 case AVDTP_STATE_IDLE: 2091 if (sep->type == AVDTP_SEP_TYPE_SOURCE) 2092 l = server->sources; 2093 else 2094 l = server->sinks; 2095 2096 for (; l != NULL; l = l->next) { 2097 tmp = l->data; 2098 if (avdtp_has_stream(session, tmp->stream)) 2099 break; 2100 } 2101 2102 if (l != NULL) { 2103 if (a2dp_sep_get_lock(tmp)) 2104 goto failed; 2105 setup->reconfigure = TRUE; 2106 if (avdtp_close(session, tmp->stream, FALSE) < 0) { 2107 error("avdtp_close failed"); 2108 goto failed; 2109 } 2110 break; 2111 } 2112 2113 setup->rsep = avdtp_find_remote_sep(session, sep->lsep); 2114 if (setup->rsep == NULL) { 2115 error("No matching ACP and INT SEPs found"); 2116 goto failed; 2117 } 2118 2119 posix_err = avdtp_set_configuration(session, setup->rsep, 2120 sep->lsep, caps, 2121 &setup->stream); 2122 if (posix_err < 0) { 2123 error("avdtp_set_configuration: %s", 2124 strerror(-posix_err)); 2125 goto failed; 2126 } 2127 break; 2128 case AVDTP_STATE_OPEN: 2129 case AVDTP_STATE_STREAMING: 2130 if (avdtp_stream_has_capabilities(setup->stream, caps)) { 2131 DBG("Configuration match: resuming"); 2132 cb_data->source_id = g_idle_add(finalize_config, 2133 setup); 2134 } else if (!setup->reconfigure) { 2135 setup->reconfigure = TRUE; 2136 if (avdtp_close(session, sep->stream, FALSE) < 0) { 2137 error("avdtp_close failed"); 2138 goto failed; 2139 } 2140 } 2141 break; 2142 default: 2143 error("SEP in bad state for requesting a new stream"); 2144 goto failed; 2145 } 2146 2147 return cb_data->id; 2148 2149 failed: 2150 setup_cb_free(cb_data); 2151 return 0; 2152 } 2153 2154 unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep, 2155 a2dp_stream_cb_t cb, void *user_data) 2156 { 2157 struct a2dp_setup_cb *cb_data; 2158 struct a2dp_setup *setup; 2159 2160 setup = a2dp_setup_get(session); 2161 if (!setup) 2162 return 0; 2163 2164 cb_data = setup_cb_new(setup); 2165 cb_data->resume_cb = cb; 2166 cb_data->user_data = user_data; 2167 2168 setup->sep = sep; 2169 setup->stream = sep->stream; 2170 2171 switch (avdtp_sep_get_state(sep->lsep)) { 2172 case AVDTP_STATE_IDLE: 2173 goto failed; 2174 break; 2175 case AVDTP_STATE_OPEN: 2176 if (avdtp_start(session, sep->stream) < 0) { 2177 error("avdtp_start failed"); 2178 goto failed; 2179 } 2180 break; 2181 case AVDTP_STATE_STREAMING: 2182 if (!sep->suspending && sep->suspend_timer) { 2183 g_source_remove(sep->suspend_timer); 2184 sep->suspend_timer = 0; 2185 avdtp_unref(sep->session); 2186 sep->session = NULL; 2187 } 2188 if (sep->suspending) 2189 setup->start = TRUE; 2190 else 2191 cb_data->source_id = g_idle_add(finalize_resume, 2192 setup); 2193 break; 2194 default: 2195 error("SEP in bad state for resume"); 2196 goto failed; 2197 } 2198 2199 return cb_data->id; 2200 2201 failed: 2202 setup_cb_free(cb_data); 2203 return 0; 2204 } 2205 2206 unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep, 2207 a2dp_stream_cb_t cb, void *user_data) 2208 { 2209 struct a2dp_setup_cb *cb_data; 2210 struct a2dp_setup *setup; 2211 2212 setup = a2dp_setup_get(session); 2213 if (!setup) 2214 return 0; 2215 2216 cb_data = setup_cb_new(setup); 2217 cb_data->suspend_cb = cb; 2218 cb_data->user_data = user_data; 2219 2220 setup->sep = sep; 2221 setup->stream = sep->stream; 2222 2223 switch (avdtp_sep_get_state(sep->lsep)) { 2224 case AVDTP_STATE_IDLE: 2225 error("a2dp_suspend: no stream to suspend"); 2226 goto failed; 2227 break; 2228 case AVDTP_STATE_OPEN: 2229 cb_data->source_id = g_idle_add(finalize_suspend, setup); 2230 break; 2231 case AVDTP_STATE_STREAMING: 2232 if (avdtp_suspend(session, sep->stream) < 0) { 2233 error("avdtp_suspend failed"); 2234 goto failed; 2235 } 2236 sep->suspending = TRUE; 2237 break; 2238 default: 2239 error("SEP in bad state for suspend"); 2240 goto failed; 2241 } 2242 2243 return cb_data->id; 2244 2245 failed: 2246 setup_cb_free(cb_data); 2247 return 0; 2248 } 2249 2250 gboolean a2dp_cancel(struct audio_device *dev, unsigned int id) 2251 { 2252 struct a2dp_setup *setup; 2253 GSList *l; 2254 2255 setup = find_setup_by_dev(dev); 2256 if (!setup) 2257 return FALSE; 2258 2259 for (l = setup->cb; l != NULL; l = g_slist_next(l)) { 2260 struct a2dp_setup_cb *cb = l->data; 2261 2262 if (cb->id != id) 2263 continue; 2264 2265 setup_ref(setup); 2266 setup_cb_free(cb); 2267 2268 if (!setup->cb) { 2269 DBG("aborting setup %p", setup); 2270 avdtp_abort(setup->session, setup->stream); 2271 return TRUE; 2272 } 2273 2274 setup_unref(setup); 2275 return TRUE; 2276 } 2277 2278 return FALSE; 2279 } 2280 2281 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session) 2282 { 2283 if (sep->locked) 2284 return FALSE; 2285 2286 DBG("SEP %p locked", sep->lsep); 2287 sep->locked = TRUE; 2288 2289 return TRUE; 2290 } 2291 2292 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session) 2293 { 2294 struct a2dp_server *server = sep->server; 2295 avdtp_state_t state; 2296 GSList *l; 2297 2298 state = avdtp_sep_get_state(sep->lsep); 2299 2300 sep->locked = FALSE; 2301 2302 DBG("SEP %p unlocked", sep->lsep); 2303 2304 if (sep->type == AVDTP_SEP_TYPE_SOURCE) 2305 l = server->sources; 2306 else 2307 l = server->sinks; 2308 2309 /* Unregister sep if it was removed */ 2310 if (g_slist_find(l, sep) == NULL) { 2311 a2dp_unregister_sep(sep); 2312 return TRUE; 2313 } 2314 2315 if (!sep->stream || state == AVDTP_STATE_IDLE) 2316 return TRUE; 2317 2318 switch (state) { 2319 case AVDTP_STATE_OPEN: 2320 /* Set timer here */ 2321 break; 2322 case AVDTP_STATE_STREAMING: 2323 if (avdtp_suspend(session, sep->stream) == 0) 2324 sep->suspending = TRUE; 2325 break; 2326 default: 2327 break; 2328 } 2329 2330 return TRUE; 2331 } 2332 2333 gboolean a2dp_sep_get_lock(struct a2dp_sep *sep) 2334 { 2335 return sep->locked; 2336 } 2337 2338 static int stream_cmp(gconstpointer data, gconstpointer user_data) 2339 { 2340 const struct a2dp_sep *sep = data; 2341 const struct avdtp_stream *stream = user_data; 2342 2343 return (sep->stream != stream); 2344 } 2345 2346 struct a2dp_sep *a2dp_get_sep(struct avdtp *session, 2347 struct avdtp_stream *stream) 2348 { 2349 struct a2dp_server *server; 2350 bdaddr_t src, dst; 2351 GSList *l; 2352 2353 avdtp_get_peers(session, &src, &dst); 2354 2355 for (l = servers; l; l = l->next) { 2356 server = l->data; 2357 2358 if (bacmp(&src, &server->src) == 0) 2359 break; 2360 } 2361 2362 if (!l) 2363 return NULL; 2364 2365 l = g_slist_find_custom(server->sources, stream, stream_cmp); 2366 if (l) 2367 return l->data; 2368 2369 l = g_slist_find_custom(server->sinks, stream, stream_cmp); 2370 if (l) 2371 return l->data; 2372 2373 return NULL; 2374 } 2375 2376 struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep) 2377 { 2378 return sep->stream; 2379 } 2380