3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2006-2010 Nokia Corporation
6
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
7
* Copyright (C) 2011 BMW Car IT GmbH. All rights reserved.
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33
#include <dbus/dbus.h>
36
#include <bluetooth/bluetooth.h>
37
#include <bluetooth/sdp.h>
38
#include <bluetooth/sdp_lib.h>
50
/* The duration that streams without users are allowed to stay in
52
#define SUSPEND_TIMEOUT 5
53
#define RECONFIGURE_TIMEOUT 500
56
# define MIN(x, y) ((x) < (y) ? (x) : (y))
60
# define MAX(x, y) ((x) > (y) ? (x) : (y))
64
struct a2dp_server *server;
65
struct a2dp_endpoint *endpoint;
68
struct avdtp_local_sep *lsep;
69
struct avdtp *session;
70
struct avdtp_stream *stream;
72
gboolean delay_reporting;
77
GDestroyNotify destroy;
80
struct a2dp_setup_cb {
81
struct a2dp_setup *setup;
82
a2dp_select_cb_t select_cb;
83
a2dp_config_cb_t config_cb;
84
a2dp_stream_cb_t resume_cb;
85
a2dp_stream_cb_t suspend_cb;
92
struct audio_device *dev;
93
struct avdtp *session;
95
struct avdtp_remote_sep *rsep;
96
struct avdtp_stream *stream;
97
struct avdtp_error *err;
98
avdtp_set_configuration_cb setconf_cb;
100
gboolean reconfigure;
106
static DBusConnection *connection = NULL;
112
uint32_t source_record_id;
113
uint32_t sink_record_id;
115
gboolean sink_enabled;
116
gboolean source_enabled;
119
static GSList *servers = NULL;
120
static GSList *setups = NULL;
121
static unsigned int cb_id = 0;
123
static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
127
DBG("%p: ref=%d", setup, setup->ref);
132
static struct audio_device *a2dp_get_dev(struct avdtp *session)
136
avdtp_get_peers(session, &src, &dst);
138
return manager_find_device(NULL, &src, &dst, NULL, FALSE);
141
static struct a2dp_setup *setup_new(struct avdtp *session)
143
struct audio_device *dev;
144
struct a2dp_setup *setup;
146
dev = a2dp_get_dev(session);
148
error("Unable to create setup");
152
setup = g_new0(struct a2dp_setup, 1);
153
setup->session = avdtp_ref(session);
154
setup->dev = a2dp_get_dev(session);
155
setups = g_slist_append(setups, setup);
160
static void setup_free(struct a2dp_setup *s)
164
setups = g_slist_remove(setups, s);
166
avdtp_unref(s->session);
167
g_slist_free_full(s->cb, g_free);
168
g_slist_free_full(s->caps, g_free);
172
static void setup_unref(struct a2dp_setup *setup)
176
DBG("%p: ref=%d", setup, setup->ref);
184
static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup)
186
struct a2dp_setup_cb *cb;
188
cb = g_new0(struct a2dp_setup_cb, 1);
192
setup->cb = g_slist_append(setup->cb, cb);
196
static void setup_cb_free(struct a2dp_setup_cb *cb)
198
struct a2dp_setup *setup = cb->setup;
201
g_source_remove(cb->source_id);
203
setup->cb = g_slist_remove(setup->cb, cb);
204
setup_unref(cb->setup);
208
static void finalize_setup_errno(struct a2dp_setup *s, int err,
209
GSourceFunc cb1, ...)
211
GSourceFunc finalize;
213
struct avdtp_error avdtp_err;
216
avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err);
223
while (finalize != NULL) {
225
finalize = va_arg(args, GSourceFunc);
231
static gboolean finalize_config(gpointer data)
233
struct a2dp_setup *s = data;
235
struct avdtp_stream *stream = s->err ? NULL : s->stream;
237
for (l = s->cb; l != NULL; ) {
238
struct a2dp_setup_cb *cb = l->data;
245
cb->config_cb(s->session, s->sep, stream, s->err,
253
static gboolean finalize_resume(gpointer data)
255
struct a2dp_setup *s = data;
258
for (l = s->cb; l != NULL; ) {
259
struct a2dp_setup_cb *cb = l->data;
266
cb->resume_cb(s->session, s->err, cb->user_data);
273
static gboolean finalize_suspend(gpointer data)
275
struct a2dp_setup *s = data;
278
for (l = s->cb; l != NULL; ) {
279
struct a2dp_setup_cb *cb = l->data;
286
cb->suspend_cb(s->session, s->err, cb->user_data);
293
static void finalize_select(struct a2dp_setup *s)
297
for (l = s->cb; l != NULL; ) {
298
struct a2dp_setup_cb *cb = l->data;
305
cb->select_cb(s->session, s->sep, s->caps, cb->user_data);
310
static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
314
for (l = setups; l != NULL; l = l->next) {
315
struct a2dp_setup *setup = l->data;
317
if (setup->session == session)
324
static struct a2dp_setup *a2dp_setup_get(struct avdtp *session)
326
struct a2dp_setup *setup;
328
setup = find_setup_by_session(session);
330
setup = setup_new(session);
335
return setup_ref(setup);
338
static struct a2dp_setup *find_setup_by_dev(struct audio_device *dev)
342
for (l = setups; l != NULL; l = l->next) {
343
struct a2dp_setup *setup = l->data;
345
if (setup->dev == dev)
352
static void stream_state_changed(struct avdtp_stream *stream,
353
avdtp_state_t old_state,
354
avdtp_state_t new_state,
355
struct avdtp_error *err,
358
struct a2dp_sep *sep = user_data;
360
if (new_state != AVDTP_STATE_IDLE)
363
if (sep->suspend_timer) {
364
g_source_remove(sep->suspend_timer);
365
sep->suspend_timer = 0;
369
avdtp_unref(sep->session);
375
if (sep->endpoint && sep->endpoint->clear_configuration)
376
sep->endpoint->clear_configuration(sep, sep->user_data);
379
static gboolean auto_config(gpointer data)
381
struct a2dp_setup *setup = data;
382
struct avdtp_error *err = NULL;
384
/* Check if configuration was aborted */
385
if (setup->sep->stream == NULL)
388
if (setup->err != NULL) {
393
avdtp_stream_add_cb(setup->session, setup->stream,
394
stream_state_changed, setup->sep);
396
if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE)
397
sink_new_stream(setup->dev, setup->session, setup->stream);
399
source_new_stream(setup->dev, setup->session, setup->stream);
402
if (setup->setconf_cb)
403
setup->setconf_cb(setup->session, setup->stream, setup->err);
405
finalize_config(setup);
415
static gboolean sbc_setconf_ind(struct avdtp *session,
416
struct avdtp_local_sep *sep,
417
struct avdtp_stream *stream,
419
avdtp_set_configuration_cb cb,
422
struct a2dp_sep *a2dp_sep = user_data;
423
struct a2dp_setup *setup;
425
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
426
DBG("Sink %p: Set_Configuration_Ind", sep);
428
DBG("Source %p: Set_Configuration_Ind", sep);
430
setup = a2dp_setup_get(session);
434
a2dp_sep->stream = stream;
435
setup->sep = a2dp_sep;
436
setup->stream = stream;
437
setup->setconf_cb = cb;
439
/* Check valid settings */
440
for (; caps != NULL; caps = g_slist_next(caps)) {
441
struct avdtp_service_capability *cap = caps->data;
442
struct avdtp_media_codec_capability *codec_cap;
443
struct sbc_codec_cap *sbc_cap;
445
if (cap->category == AVDTP_DELAY_REPORTING &&
446
!a2dp_sep->delay_reporting) {
447
setup->err = g_new(struct avdtp_error, 1);
448
avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
449
AVDTP_UNSUPPORTED_CONFIGURATION);
453
if (cap->category != AVDTP_MEDIA_CODEC)
456
if (cap->length < sizeof(struct sbc_codec_cap))
459
codec_cap = (void *) cap->data;
461
if (codec_cap->media_codec_type != A2DP_CODEC_SBC)
464
sbc_cap = (void *) codec_cap;
466
if (sbc_cap->min_bitpool < MIN_BITPOOL ||
467
sbc_cap->max_bitpool > MAX_BITPOOL) {
468
setup->err = g_new(struct avdtp_error, 1);
469
avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
470
AVDTP_UNSUPPORTED_CONFIGURATION);
476
g_idle_add(auto_config, setup);
480
static gboolean sbc_getcap_ind(struct avdtp *session, struct avdtp_local_sep *sep,
481
gboolean get_all, GSList **caps, uint8_t *err,
484
struct a2dp_sep *a2dp_sep = user_data;
485
struct avdtp_service_capability *media_transport, *media_codec;
486
struct sbc_codec_cap sbc_cap;
488
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
489
DBG("Sink %p: Get_Capability_Ind", sep);
491
DBG("Source %p: Get_Capability_Ind", sep);
495
media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
498
*caps = g_slist_append(*caps, media_transport);
500
memset(&sbc_cap, 0, sizeof(struct sbc_codec_cap));
502
sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
503
sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC;
505
sbc_cap.frequency = ( SBC_SAMPLING_FREQ_48000 |
506
SBC_SAMPLING_FREQ_44100 |
507
SBC_SAMPLING_FREQ_32000 |
508
SBC_SAMPLING_FREQ_16000 );
510
sbc_cap.channel_mode = ( SBC_CHANNEL_MODE_JOINT_STEREO |
511
SBC_CHANNEL_MODE_STEREO |
512
SBC_CHANNEL_MODE_DUAL_CHANNEL |
513
SBC_CHANNEL_MODE_MONO );
515
sbc_cap.block_length = ( SBC_BLOCK_LENGTH_16 |
516
SBC_BLOCK_LENGTH_12 |
518
SBC_BLOCK_LENGTH_4 );
520
sbc_cap.subbands = ( SBC_SUBBANDS_8 | SBC_SUBBANDS_4 );
522
sbc_cap.allocation_method = ( SBC_ALLOCATION_LOUDNESS |
523
SBC_ALLOCATION_SNR );
525
sbc_cap.min_bitpool = MIN_BITPOOL;
526
sbc_cap.max_bitpool = MAX_BITPOOL;
528
media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
531
*caps = g_slist_append(*caps, media_codec);
534
struct avdtp_service_capability *delay_reporting;
535
delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
537
*caps = g_slist_append(*caps, delay_reporting);
543
static gboolean mpeg_setconf_ind(struct avdtp *session,
544
struct avdtp_local_sep *sep,
545
struct avdtp_stream *stream,
547
avdtp_set_configuration_cb cb,
550
struct a2dp_sep *a2dp_sep = user_data;
551
struct a2dp_setup *setup;
553
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
554
DBG("Sink %p: Set_Configuration_Ind", sep);
556
DBG("Source %p: Set_Configuration_Ind", sep);
558
setup = a2dp_setup_get(session);
562
a2dp_sep->stream = stream;
563
setup->sep = a2dp_sep;
564
setup->stream = stream;
565
setup->setconf_cb = cb;
567
for (; caps != NULL; caps = g_slist_next(caps)) {
568
struct avdtp_service_capability *cap = caps->data;
570
if (cap->category == AVDTP_DELAY_REPORTING &&
571
!a2dp_sep->delay_reporting) {
572
setup->err = g_new(struct avdtp_error, 1);
573
avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
574
AVDTP_UNSUPPORTED_CONFIGURATION);
580
g_idle_add(auto_config, setup);
584
static gboolean mpeg_getcap_ind(struct avdtp *session,
585
struct avdtp_local_sep *sep,
587
GSList **caps, uint8_t *err, void *user_data)
589
struct a2dp_sep *a2dp_sep = user_data;
590
struct avdtp_service_capability *media_transport, *media_codec;
591
struct mpeg_codec_cap mpeg_cap;
593
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
594
DBG("Sink %p: Get_Capability_Ind", sep);
596
DBG("Source %p: Get_Capability_Ind", sep);
600
media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
603
*caps = g_slist_append(*caps, media_transport);
605
memset(&mpeg_cap, 0, sizeof(struct mpeg_codec_cap));
607
mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
608
mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12;
610
mpeg_cap.frequency = ( MPEG_SAMPLING_FREQ_48000 |
611
MPEG_SAMPLING_FREQ_44100 |
612
MPEG_SAMPLING_FREQ_32000 |
613
MPEG_SAMPLING_FREQ_24000 |
614
MPEG_SAMPLING_FREQ_22050 |
615
MPEG_SAMPLING_FREQ_16000 );
617
mpeg_cap.channel_mode = ( MPEG_CHANNEL_MODE_JOINT_STEREO |
618
MPEG_CHANNEL_MODE_STEREO |
619
MPEG_CHANNEL_MODE_DUAL_CHANNEL |
620
MPEG_CHANNEL_MODE_MONO );
622
mpeg_cap.layer = ( MPEG_LAYER_MP3 | MPEG_LAYER_MP2 | MPEG_LAYER_MP1 );
624
mpeg_cap.bitrate = 0xFFFF;
626
media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap,
629
*caps = g_slist_append(*caps, media_codec);
632
struct avdtp_service_capability *delay_reporting;
633
delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
635
*caps = g_slist_append(*caps, delay_reporting);
642
static void endpoint_setconf_cb(struct a2dp_setup *setup, gboolean ret)
645
setup->err = g_new(struct avdtp_error, 1);
646
avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
647
AVDTP_UNSUPPORTED_CONFIGURATION);
653
static gboolean endpoint_setconf_ind(struct avdtp *session,
654
struct avdtp_local_sep *sep,
655
struct avdtp_stream *stream,
657
avdtp_set_configuration_cb cb,
660
struct a2dp_sep *a2dp_sep = user_data;
661
struct a2dp_setup *setup;
663
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
664
DBG("Sink %p: Set_Configuration_Ind", sep);
666
DBG("Source %p: Set_Configuration_Ind", sep);
668
setup = a2dp_setup_get(session);
672
a2dp_sep->stream = stream;
673
setup->sep = a2dp_sep;
674
setup->stream = stream;
675
setup->setconf_cb = cb;
677
for (; caps != NULL; caps = g_slist_next(caps)) {
678
struct avdtp_service_capability *cap = caps->data;
679
struct avdtp_media_codec_capability *codec;
682
if (cap->category == AVDTP_DELAY_REPORTING &&
683
!a2dp_sep->delay_reporting) {
684
setup->err = g_new(struct avdtp_error, 1);
685
avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
686
AVDTP_UNSUPPORTED_CONFIGURATION);
690
if (cap->category != AVDTP_MEDIA_CODEC)
693
codec = (struct avdtp_media_codec_capability *) cap->data;
695
if (codec->media_codec_type != a2dp_sep->codec) {
696
setup->err = g_new(struct avdtp_error, 1);
697
avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
698
AVDTP_UNSUPPORTED_CONFIGURATION);
702
ret = a2dp_sep->endpoint->set_configuration(a2dp_sep,
703
setup->dev, codec->data,
704
cap->length - sizeof(*codec),
707
a2dp_sep->user_data);
711
setup->err = g_new(struct avdtp_error, 1);
712
avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
713
AVDTP_UNSUPPORTED_CONFIGURATION);
718
g_idle_add(auto_config, setup);
722
static gboolean endpoint_getcap_ind(struct avdtp *session,
723
struct avdtp_local_sep *sep,
724
gboolean get_all, GSList **caps,
725
uint8_t *err, void *user_data)
727
struct a2dp_sep *a2dp_sep = user_data;
728
struct avdtp_service_capability *media_transport, *media_codec;
729
struct avdtp_media_codec_capability *codec_caps;
730
uint8_t *capabilities;
733
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
734
DBG("Sink %p: Get_Capability_Ind", sep);
736
DBG("Source %p: Get_Capability_Ind", sep);
740
media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
743
*caps = g_slist_append(*caps, media_transport);
745
length = a2dp_sep->endpoint->get_capabilities(a2dp_sep, &capabilities,
746
a2dp_sep->user_data);
748
codec_caps = g_malloc0(sizeof(*codec_caps) + length);
749
codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO;
750
codec_caps->media_codec_type = a2dp_sep->codec;
751
memcpy(codec_caps->data, capabilities, length);
753
media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps,
754
sizeof(*codec_caps) + length);
756
*caps = g_slist_append(*caps, media_codec);
760
struct avdtp_service_capability *delay_reporting;
761
delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
763
*caps = g_slist_append(*caps, delay_reporting);
769
static void endpoint_open_cb(struct a2dp_setup *setup, gboolean ret)
774
setup->stream = NULL;
775
finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
779
err = avdtp_open(setup->session, setup->stream);
783
error("Error on avdtp_open %s (%d)", strerror(-err), -err);
784
setup->stream = NULL;
785
finalize_setup_errno(setup, err, finalize_config, NULL);
788
static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
789
struct avdtp_stream *stream,
790
struct avdtp_error *err, void *user_data)
792
struct a2dp_sep *a2dp_sep = user_data;
793
struct a2dp_setup *setup;
794
struct audio_device *dev;
797
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
798
DBG("Sink %p: Set_Configuration_Cfm", sep);
800
DBG("Source %p: Set_Configuration_Cfm", sep);
802
setup = find_setup_by_session(session);
807
finalize_config(setup);
812
avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
813
a2dp_sep->stream = stream;
818
dev = a2dp_get_dev(session);
820
/* Notify D-Bus interface of the new stream */
821
if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE)
822
sink_new_stream(dev, session, setup->stream);
824
source_new_stream(dev, session, setup->stream);
826
/* Notify Endpoint */
827
if (a2dp_sep->endpoint) {
828
struct avdtp_service_capability *service;
829
struct avdtp_media_codec_capability *codec;
832
service = avdtp_stream_get_codec(stream);
833
codec = (struct avdtp_media_codec_capability *) service->data;
835
err = a2dp_sep->endpoint->set_configuration(a2dp_sep, dev,
836
codec->data, service->length -
840
a2dp_sep->user_data);
844
setup->stream = NULL;
845
finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
849
ret = avdtp_open(session, stream);
851
error("Error on avdtp_open %s (%d)", strerror(-ret), -ret);
852
setup->stream = NULL;
853
finalize_setup_errno(setup, ret, finalize_config, NULL);
857
static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
858
uint8_t *err, void *user_data)
860
struct a2dp_sep *a2dp_sep = user_data;
862
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
863
DBG("Sink %p: Get_Configuration_Ind", sep);
865
DBG("Source %p: Get_Configuration_Ind", sep);
869
static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
870
struct avdtp_stream *stream, struct avdtp_error *err,
873
struct a2dp_sep *a2dp_sep = user_data;
875
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
876
DBG("Sink %p: Set_Configuration_Cfm", sep);
878
DBG("Source %p: Set_Configuration_Cfm", sep);
881
static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
882
struct avdtp_stream *stream, uint8_t *err,
885
struct a2dp_sep *a2dp_sep = user_data;
886
struct a2dp_setup *setup;
888
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
889
DBG("Sink %p: Open_Ind", sep);
891
DBG("Source %p: Open_Ind", sep);
893
setup = find_setup_by_session(session);
897
if (setup->reconfigure)
898
setup->reconfigure = FALSE;
900
finalize_config(setup);
905
static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
906
struct avdtp_stream *stream, struct avdtp_error *err,
909
struct a2dp_sep *a2dp_sep = user_data;
910
struct a2dp_setup *setup;
912
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
913
DBG("Sink %p: Open_Cfm", sep);
915
DBG("Source %p: Open_Cfm", sep);
917
setup = find_setup_by_session(session);
921
if (setup->reconfigure)
922
setup->reconfigure = FALSE;
925
setup->stream = NULL;
929
finalize_config(setup);
932
static gboolean suspend_timeout(struct a2dp_sep *sep)
934
if (avdtp_suspend(sep->session, sep->stream) == 0)
935
sep->suspending = TRUE;
937
sep->suspend_timer = 0;
939
avdtp_unref(sep->session);
945
static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
946
struct avdtp_stream *stream, uint8_t *err,
949
struct a2dp_sep *a2dp_sep = user_data;
950
struct a2dp_setup *setup;
952
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
953
DBG("Sink %p: Start_Ind", sep);
955
DBG("Source %p: Start_Ind", sep);
957
if (!a2dp_sep->locked) {
958
a2dp_sep->session = avdtp_ref(session);
959
a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
960
(GSourceFunc) suspend_timeout,
964
if (!a2dp_sep->starting)
967
a2dp_sep->starting = FALSE;
969
setup = find_setup_by_session(session);
971
finalize_resume(setup);
976
static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
977
struct avdtp_stream *stream, struct avdtp_error *err,
980
struct a2dp_sep *a2dp_sep = user_data;
981
struct a2dp_setup *setup;
983
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
984
DBG("Sink %p: Start_Cfm", sep);
986
DBG("Source %p: Start_Cfm", sep);
988
a2dp_sep->starting = FALSE;
990
setup = find_setup_by_session(session);
995
setup->stream = NULL;
999
finalize_resume(setup);
1002
static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1003
struct avdtp_stream *stream, uint8_t *err,
1006
struct a2dp_sep *a2dp_sep = user_data;
1007
struct a2dp_setup *setup;
1011
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1012
DBG("Sink %p: Suspend_Ind", sep);
1014
DBG("Source %p: Suspend_Ind", sep);
1016
if (a2dp_sep->suspend_timer) {
1017
g_source_remove(a2dp_sep->suspend_timer);
1018
a2dp_sep->suspend_timer = 0;
1019
avdtp_unref(a2dp_sep->session);
1020
a2dp_sep->session = NULL;
1023
if (!a2dp_sep->suspending)
1026
a2dp_sep->suspending = FALSE;
1028
setup = find_setup_by_session(session);
1032
start = setup->start;
1033
setup->start = FALSE;
1035
finalize_suspend(setup);
1040
start_err = avdtp_start(session, a2dp_sep->stream);
1041
if (start_err < 0 && start_err != -EINPROGRESS) {
1042
error("avdtp_start: %s (%d)", strerror(-start_err),
1044
finalize_setup_errno(setup, start_err, finalize_resume);
1050
static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1051
struct avdtp_stream *stream, struct avdtp_error *err,
1054
struct a2dp_sep *a2dp_sep = user_data;
1055
struct a2dp_setup *setup;
1059
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1060
DBG("Sink %p: Suspend_Cfm", sep);
1062
DBG("Source %p: Suspend_Cfm", sep);
1064
a2dp_sep->suspending = FALSE;
1066
setup = find_setup_by_session(session);
1070
start = setup->start;
1071
setup->start = FALSE;
1074
setup->stream = NULL;
1078
finalize_suspend(setup);
1084
finalize_resume(setup);
1088
start_err = avdtp_start(session, a2dp_sep->stream);
1089
if (start_err < 0 && start_err != -EINPROGRESS) {
1090
error("avdtp_start: %s (%d)", strerror(-start_err),
1092
finalize_setup_errno(setup, start_err, finalize_suspend, NULL);
1096
static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1097
struct avdtp_stream *stream, uint8_t *err,
1100
struct a2dp_sep *a2dp_sep = user_data;
1101
struct a2dp_setup *setup;
1103
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1104
DBG("Sink %p: Close_Ind", sep);
1106
DBG("Source %p: Close_Ind", sep);
1108
setup = find_setup_by_session(session);
1112
finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1113
finalize_resume, NULL);
1118
static gboolean a2dp_reconfigure(gpointer data)
1120
struct a2dp_setup *setup = data;
1121
struct a2dp_sep *sep = setup->sep;
1123
struct avdtp_media_codec_capability *rsep_codec;
1124
struct avdtp_service_capability *cap;
1127
cap = avdtp_get_codec(setup->rsep);
1128
rsep_codec = (struct avdtp_media_codec_capability *) cap->data;
1131
if (!setup->rsep || sep->codec != rsep_codec->media_codec_type)
1132
setup->rsep = avdtp_find_remote_sep(setup->session, sep->lsep);
1134
posix_err = avdtp_set_configuration(setup->session, setup->rsep,
1138
if (posix_err < 0) {
1139
error("avdtp_set_configuration: %s", strerror(-posix_err));
1146
finalize_setup_errno(setup, posix_err, finalize_config, NULL);
1150
static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1151
struct avdtp_stream *stream, struct avdtp_error *err,
1154
struct a2dp_sep *a2dp_sep = user_data;
1155
struct a2dp_setup *setup;
1157
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1158
DBG("Sink %p: Close_Cfm", sep);
1160
DBG("Source %p: Close_Cfm", sep);
1162
setup = find_setup_by_session(session);
1167
setup->stream = NULL;
1169
finalize_config(setup);
1174
setup->rsep = avdtp_stream_get_remote_sep(stream);
1176
if (setup->reconfigure)
1177
g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
1180
static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1181
struct avdtp_stream *stream, uint8_t *err,
1184
struct a2dp_sep *a2dp_sep = user_data;
1185
struct a2dp_setup *setup;
1187
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1188
DBG("Sink %p: Abort_Ind", sep);
1190
DBG("Source %p: Abort_Ind", sep);
1192
a2dp_sep->stream = NULL;
1194
setup = find_setup_by_session(session);
1198
finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1205
static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1206
struct avdtp_stream *stream, struct avdtp_error *err,
1209
struct a2dp_sep *a2dp_sep = user_data;
1210
struct a2dp_setup *setup;
1212
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1213
DBG("Sink %p: Abort_Cfm", sep);
1215
DBG("Source %p: Abort_Cfm", sep);
1217
setup = find_setup_by_session(session);
1224
static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1225
uint8_t *err, void *user_data)
1227
struct a2dp_sep *a2dp_sep = user_data;
1229
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1230
DBG("Sink %p: ReConfigure_Ind", sep);
1232
DBG("Source %p: ReConfigure_Ind", sep);
1237
static gboolean delayreport_ind(struct avdtp *session,
1238
struct avdtp_local_sep *sep,
1239
uint8_t rseid, uint16_t delay,
1240
uint8_t *err, void *user_data)
1242
struct a2dp_sep *a2dp_sep = user_data;
1243
struct audio_device *dev = a2dp_get_dev(session);
1245
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1246
DBG("Sink %p: DelayReport_Ind", sep);
1248
DBG("Source %p: DelayReport_Ind", sep);
1250
unix_delay_report(dev, rseid, delay);
1255
static gboolean endpoint_delayreport_ind(struct avdtp *session,
1256
struct avdtp_local_sep *sep,
1257
uint8_t rseid, uint16_t delay,
1258
uint8_t *err, void *user_data)
1260
struct a2dp_sep *a2dp_sep = user_data;
1262
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1263
DBG("Sink %p: DelayReport_Ind", sep);
1265
DBG("Source %p: DelayReport_Ind", sep);
1267
if (a2dp_sep->endpoint == NULL ||
1268
a2dp_sep->endpoint->set_delay == NULL)
1271
a2dp_sep->endpoint->set_delay(a2dp_sep, delay, a2dp_sep->user_data);
1276
static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1277
struct avdtp_stream *stream, struct avdtp_error *err,
1280
struct a2dp_sep *a2dp_sep = user_data;
1281
struct a2dp_setup *setup;
1283
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1284
DBG("Sink %p: ReConfigure_Cfm", sep);
1286
DBG("Source %p: ReConfigure_Cfm", sep);
1288
setup = find_setup_by_session(session);
1293
setup->stream = NULL;
1297
finalize_config(setup);
1300
static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1301
struct avdtp_stream *stream,
1302
struct avdtp_error *err, void *user_data)
1304
struct a2dp_sep *a2dp_sep = user_data;
1306
if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1307
DBG("Sink %p: DelayReport_Cfm", sep);
1309
DBG("Source %p: DelayReport_Cfm", sep);
1312
static struct avdtp_sep_cfm cfm = {
1313
.set_configuration = setconf_cfm,
1314
.get_configuration = getconf_cfm,
1317
.suspend = suspend_cfm,
1320
.reconfigure = reconf_cfm,
1321
.delay_report = delay_report_cfm,
1324
static struct avdtp_sep_ind sbc_ind = {
1325
.get_capability = sbc_getcap_ind,
1326
.set_configuration = sbc_setconf_ind,
1327
.get_configuration = getconf_ind,
1330
.suspend = suspend_ind,
1333
.reconfigure = reconf_ind,
1334
.delayreport = delayreport_ind,
1337
static struct avdtp_sep_ind mpeg_ind = {
1338
.get_capability = mpeg_getcap_ind,
1339
.set_configuration = mpeg_setconf_ind,
1340
.get_configuration = getconf_ind,
1343
.suspend = suspend_ind,
1346
.reconfigure = reconf_ind,
1347
.delayreport = delayreport_ind,
1350
static struct avdtp_sep_ind endpoint_ind = {
1351
.get_capability = endpoint_getcap_ind,
1352
.set_configuration = endpoint_setconf_ind,
1353
.get_configuration = getconf_ind,
1356
.suspend = suspend_ind,
1359
.reconfigure = reconf_ind,
1360
.delayreport = endpoint_delayreport_ind,
1363
static sdp_record_t *a2dp_record(uint8_t type, uint16_t avdtp_ver)
1365
sdp_list_t *svclass_id, *pfseq, *apseq, *root;
1366
uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
1367
sdp_profile_desc_t profile[1];
1368
sdp_list_t *aproto, *proto[2];
1369
sdp_record_t *record;
1370
sdp_data_t *psm, *version, *features;
1371
uint16_t lp = AVDTP_UUID;
1372
uint16_t a2dp_ver = 0x0102, feat = 0x000f;
1374
record = sdp_record_alloc();
1378
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1379
root = sdp_list_append(0, &root_uuid);
1380
sdp_set_browse_groups(record, root);
1382
if (type == AVDTP_SEP_TYPE_SOURCE)
1383
sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
1385
sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID);
1386
svclass_id = sdp_list_append(0, &a2dp_uuid);
1387
sdp_set_service_classes(record, svclass_id);
1389
sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
1390
profile[0].version = a2dp_ver;
1391
pfseq = sdp_list_append(0, &profile[0]);
1392
sdp_set_profile_descs(record, pfseq);
1394
sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1395
proto[0] = sdp_list_append(0, &l2cap_uuid);
1396
psm = sdp_data_alloc(SDP_UINT16, &lp);
1397
proto[0] = sdp_list_append(proto[0], psm);
1398
apseq = sdp_list_append(0, proto[0]);
1400
sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
1401
proto[1] = sdp_list_append(0, &avdtp_uuid);
1402
version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
1403
proto[1] = sdp_list_append(proto[1], version);
1404
apseq = sdp_list_append(apseq, proto[1]);
1406
aproto = sdp_list_append(0, apseq);
1407
sdp_set_access_protos(record, aproto);
1409
features = sdp_data_alloc(SDP_UINT16, &feat);
1410
sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
1412
if (type == AVDTP_SEP_TYPE_SOURCE)
1413
sdp_set_info_attr(record, "Audio Source", 0, 0);
1415
sdp_set_info_attr(record, "Audio Sink", 0, 0);
1419
sdp_list_free(proto[0], 0);
1420
sdp_list_free(proto[1], 0);
1421
sdp_list_free(apseq, 0);
1422
sdp_list_free(pfseq, 0);
1423
sdp_list_free(aproto, 0);
1424
sdp_list_free(root, 0);
1425
sdp_list_free(svclass_id, 0);
1430
static struct a2dp_server *find_server(GSList *list, const bdaddr_t *src)
1433
for (; list; list = list->next) {
1434
struct a2dp_server *server = list->data;
1436
if (bacmp(&server->src, src) == 0)
1443
int a2dp_register(DBusConnection *conn, const bdaddr_t *src, GKeyFile *config)
1445
int sbc_srcs = 0, sbc_sinks = 0;
1446
int mpeg12_srcs = 0, mpeg12_sinks = 0;
1447
gboolean source = TRUE, sink = TRUE, socket = FALSE;
1448
gboolean delay_reporting = FALSE;
1452
struct a2dp_server *server;
1457
str = g_key_file_get_string(config, "General", "Enable", &err);
1460
DBG("audio.conf: %s", err->message);
1461
g_clear_error(&err);
1463
if (strstr(str, "Sink"))
1465
if (strstr(str, "Source"))
1467
if (strstr(str, "Socket"))
1472
str = g_key_file_get_string(config, "General", "Disable", &err);
1475
DBG("audio.conf: %s", err->message);
1476
g_clear_error(&err);
1478
if (strstr(str, "Sink"))
1480
if (strstr(str, "Source"))
1482
if (strstr(str, "Socket"))
1487
/* Don't register any local sep if Socket is disabled */
1488
if (socket == FALSE)
1491
str = g_key_file_get_string(config, "A2DP", "SBCSources", &err);
1493
DBG("audio.conf: %s", err->message);
1494
g_clear_error(&err);
1497
sbc_srcs = atoi(str);
1501
str = g_key_file_get_string(config, "A2DP", "MPEG12Sources", &err);
1503
DBG("audio.conf: %s", err->message);
1504
g_clear_error(&err);
1506
mpeg12_srcs = atoi(str);
1510
str = g_key_file_get_string(config, "A2DP", "SBCSinks", &err);
1512
DBG("audio.conf: %s", err->message);
1513
g_clear_error(&err);
1516
sbc_sinks = atoi(str);
1520
str = g_key_file_get_string(config, "A2DP", "MPEG12Sinks", &err);
1522
DBG("audio.conf: %s", err->message);
1523
g_clear_error(&err);
1525
mpeg12_sinks = atoi(str);
1531
connection = dbus_connection_ref(conn);
1533
server = find_server(servers, src);
1537
server = g_new0(struct a2dp_server, 1);
1541
av_err = avdtp_init(src, config, &server->version);
1547
bacpy(&server->src, src);
1548
servers = g_slist_append(servers, server);
1552
delay_reporting = g_key_file_get_boolean(config, "A2DP",
1553
"DelayReporting", NULL);
1555
if (delay_reporting)
1556
server->version = 0x0103;
1558
server->version = 0x0102;
1560
server->source_enabled = source;
1562
for (i = 0; i < sbc_srcs; i++)
1563
a2dp_add_sep(src, AVDTP_SEP_TYPE_SOURCE,
1564
A2DP_CODEC_SBC, delay_reporting,
1565
NULL, NULL, NULL, NULL);
1567
for (i = 0; i < mpeg12_srcs; i++)
1568
a2dp_add_sep(src, AVDTP_SEP_TYPE_SOURCE,
1569
A2DP_CODEC_MPEG12, delay_reporting,
1570
NULL, NULL, NULL, NULL);
1572
server->sink_enabled = sink;
1574
for (i = 0; i < sbc_sinks; i++)
1575
a2dp_add_sep(src, AVDTP_SEP_TYPE_SINK,
1576
A2DP_CODEC_SBC, delay_reporting,
1577
NULL, NULL, NULL, NULL);
1579
for (i = 0; i < mpeg12_sinks; i++)
1580
a2dp_add_sep(src, AVDTP_SEP_TYPE_SINK,
1581
A2DP_CODEC_MPEG12, delay_reporting,
1582
NULL, NULL, NULL, NULL);
1588
static void a2dp_unregister_sep(struct a2dp_sep *sep)
1591
sep->destroy(sep->user_data);
1592
sep->endpoint = NULL;
1595
avdtp_unregister_sep(sep->lsep);
1599
void a2dp_unregister(const bdaddr_t *src)
1601
struct a2dp_server *server;
1603
server = find_server(servers, src);
1607
g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
1608
g_slist_free_full(server->sources,
1609
(GDestroyNotify) a2dp_unregister_sep);
1613
servers = g_slist_remove(servers, server);
1615
if (server->source_record_id)
1616
remove_record_from_server(server->source_record_id);
1618
if (server->sink_record_id)
1619
remove_record_from_server(server->sink_record_id);
1626
dbus_connection_unref(connection);
1630
struct a2dp_sep *a2dp_add_sep(const bdaddr_t *src, uint8_t type,
1631
uint8_t codec, gboolean delay_reporting,
1632
struct a2dp_endpoint *endpoint,
1633
void *user_data, GDestroyNotify destroy,
1636
struct a2dp_server *server;
1637
struct a2dp_sep *sep;
1639
uint32_t *record_id;
1640
sdp_record_t *record;
1641
struct avdtp_sep_ind *ind;
1643
server = find_server(servers, src);
1644
if (server == NULL) {
1646
*err = -EPROTONOSUPPORT;
1650
if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
1652
*err = -EPROTONOSUPPORT;
1656
if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
1658
*err = -EPROTONOSUPPORT;
1662
sep = g_new0(struct a2dp_sep, 1);
1665
ind = &endpoint_ind;
1669
ind = (codec == A2DP_CODEC_MPEG12) ? &mpeg_ind : &sbc_ind;
1672
sep->lsep = avdtp_register_sep(&server->src, type,
1673
AVDTP_MEDIA_TYPE_AUDIO, codec,
1674
delay_reporting, ind, &cfm, sep);
1675
if (sep->lsep == NULL) {
1682
sep->server = server;
1683
sep->endpoint = endpoint;
1686
sep->delay_reporting = delay_reporting;
1687
sep->user_data = user_data;
1688
sep->destroy = destroy;
1690
if (type == AVDTP_SEP_TYPE_SOURCE) {
1691
l = &server->sources;
1692
record_id = &server->source_record_id;
1695
record_id = &server->sink_record_id;
1698
if (*record_id != 0)
1701
record = a2dp_record(type, server->version);
1703
error("Unable to allocate new service record");
1704
avdtp_unregister_sep(sep->lsep);
1711
if (add_record_to_server(&server->src, record) < 0) {
1712
error("Unable to register A2DP service record");\
1713
sdp_record_free(record);
1714
avdtp_unregister_sep(sep->lsep);
1720
*record_id = record->handle;
1723
*l = g_slist_append(*l, sep);
1730
void a2dp_remove_sep(struct a2dp_sep *sep)
1732
struct a2dp_server *server = sep->server;
1734
if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
1735
if (g_slist_find(server->sources, sep) == NULL)
1737
server->sources = g_slist_remove(server->sources, sep);
1738
if (server->sources == NULL && server->source_record_id) {
1739
remove_record_from_server(server->source_record_id);
1740
server->source_record_id = 0;
1743
if (g_slist_find(server->sinks, sep) == NULL)
1745
server->sinks = g_slist_remove(server->sinks, sep);
1746
if (server->sinks == NULL && server->sink_record_id) {
1747
remove_record_from_server(server->sink_record_id);
1748
server->sink_record_id = 0;
1755
a2dp_unregister_sep(sep);
1758
struct a2dp_sep *a2dp_get(struct avdtp *session,
1759
struct avdtp_remote_sep *rsep)
1762
struct a2dp_server *server;
1763
struct avdtp_service_capability *cap;
1764
struct avdtp_media_codec_capability *codec_cap = NULL;
1767
avdtp_get_peers(session, &src, NULL);
1768
server = find_server(servers, &src);
1772
cap = avdtp_get_codec(rsep);
1773
codec_cap = (void *) cap->data;
1775
if (avdtp_get_type(rsep) == AVDTP_SEP_TYPE_SINK)
1776
l = server->sources;
1780
for (; l != NULL; l = l->next) {
1781
struct a2dp_sep *sep = l->data;
1786
if (sep->codec != codec_cap->media_codec_type)
1789
if (!sep->stream || avdtp_has_stream(session, sep->stream))
1796
static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
1799
case SBC_SAMPLING_FREQ_16000:
1800
case SBC_SAMPLING_FREQ_32000:
1802
case SBC_SAMPLING_FREQ_44100:
1804
case SBC_CHANNEL_MODE_MONO:
1805
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1807
case SBC_CHANNEL_MODE_STEREO:
1808
case SBC_CHANNEL_MODE_JOINT_STEREO:
1811
error("Invalid channel mode %u", mode);
1814
case SBC_SAMPLING_FREQ_48000:
1816
case SBC_CHANNEL_MODE_MONO:
1817
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1819
case SBC_CHANNEL_MODE_STEREO:
1820
case SBC_CHANNEL_MODE_JOINT_STEREO:
1823
error("Invalid channel mode %u", mode);
1827
error("Invalid sampling freq %u", freq);
1832
static gboolean select_sbc_params(struct sbc_codec_cap *cap,
1833
struct sbc_codec_cap *supported)
1835
unsigned int max_bitpool, min_bitpool;
1837
memset(cap, 0, sizeof(struct sbc_codec_cap));
1839
cap->cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
1840
cap->cap.media_codec_type = A2DP_CODEC_SBC;
1842
if (supported->frequency & SBC_SAMPLING_FREQ_44100)
1843
cap->frequency = SBC_SAMPLING_FREQ_44100;
1844
else if (supported->frequency & SBC_SAMPLING_FREQ_48000)
1845
cap->frequency = SBC_SAMPLING_FREQ_48000;
1846
else if (supported->frequency & SBC_SAMPLING_FREQ_32000)
1847
cap->frequency = SBC_SAMPLING_FREQ_32000;
1848
else if (supported->frequency & SBC_SAMPLING_FREQ_16000)
1849
cap->frequency = SBC_SAMPLING_FREQ_16000;
1851
error("No supported frequencies");
1855
if (supported->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
1856
cap->channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
1857
else if (supported->channel_mode & SBC_CHANNEL_MODE_STEREO)
1858
cap->channel_mode = SBC_CHANNEL_MODE_STEREO;
1859
else if (supported->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
1860
cap->channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
1861
else if (supported->channel_mode & SBC_CHANNEL_MODE_MONO)
1862
cap->channel_mode = SBC_CHANNEL_MODE_MONO;
1864
error("No supported channel modes");
1868
if (supported->block_length & SBC_BLOCK_LENGTH_16)
1869
cap->block_length = SBC_BLOCK_LENGTH_16;
1870
else if (supported->block_length & SBC_BLOCK_LENGTH_12)
1871
cap->block_length = SBC_BLOCK_LENGTH_12;
1872
else if (supported->block_length & SBC_BLOCK_LENGTH_8)
1873
cap->block_length = SBC_BLOCK_LENGTH_8;
1874
else if (supported->block_length & SBC_BLOCK_LENGTH_4)
1875
cap->block_length = SBC_BLOCK_LENGTH_4;
1877
error("No supported block lengths");
1881
if (supported->subbands & SBC_SUBBANDS_8)
1882
cap->subbands = SBC_SUBBANDS_8;
1883
else if (supported->subbands & SBC_SUBBANDS_4)
1884
cap->subbands = SBC_SUBBANDS_4;
1886
error("No supported subbands");
1890
if (supported->allocation_method & SBC_ALLOCATION_LOUDNESS)
1891
cap->allocation_method = SBC_ALLOCATION_LOUDNESS;
1892
else if (supported->allocation_method & SBC_ALLOCATION_SNR)
1893
cap->allocation_method = SBC_ALLOCATION_SNR;
1895
min_bitpool = MAX(MIN_BITPOOL, supported->min_bitpool);
1896
max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode),
1897
supported->max_bitpool);
1899
cap->min_bitpool = min_bitpool;
1900
cap->max_bitpool = max_bitpool;
1905
static gboolean select_capabilities(struct avdtp *session,
1906
struct avdtp_remote_sep *rsep,
1909
struct avdtp_service_capability *media_transport, *media_codec;
1910
struct sbc_codec_cap sbc_cap;
1912
media_codec = avdtp_get_codec(rsep);
1916
select_sbc_params(&sbc_cap, (struct sbc_codec_cap *) media_codec->data);
1918
media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1921
*caps = g_slist_append(*caps, media_transport);
1923
media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
1926
*caps = g_slist_append(*caps, media_codec);
1928
if (avdtp_get_delay_reporting(rsep)) {
1929
struct avdtp_service_capability *delay_reporting;
1930
delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
1932
*caps = g_slist_append(*caps, delay_reporting);
1938
static void select_cb(struct a2dp_setup *setup, void *ret, int size)
1940
struct avdtp_service_capability *media_transport, *media_codec;
1941
struct avdtp_media_codec_capability *cap;
1944
DBG("Endpoint replied an invalid configuration");
1948
media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1951
setup->caps = g_slist_append(setup->caps, media_transport);
1953
cap = g_malloc0(sizeof(*cap) + size);
1954
cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
1955
cap->media_codec_type = setup->sep->codec;
1956
memcpy(cap->data, ret, size);
1958
media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
1959
sizeof(*cap) + size);
1961
setup->caps = g_slist_append(setup->caps, media_codec);
1965
finalize_select(setup);
1968
static gboolean auto_select(gpointer data)
1970
struct a2dp_setup *setup = data;
1972
finalize_select(setup);
1977
static struct a2dp_sep *a2dp_find_sep(struct avdtp *session, GSList *list,
1980
for (; list; list = list->next) {
1981
struct a2dp_sep *sep = list->data;
1983
/* Use sender's endpoint if available */
1987
if (sep->endpoint == NULL)
1990
name = sep->endpoint->get_name(sep, sep->user_data);
1991
if (g_strcmp0(sender, name) != 0)
1995
if (avdtp_find_remote_sep(session, sep->lsep) == NULL)
2004
static struct a2dp_sep *a2dp_select_sep(struct avdtp *session, uint8_t type,
2007
struct a2dp_server *server;
2008
struct a2dp_sep *sep;
2012
avdtp_get_peers(session, &src, NULL);
2013
server = find_server(servers, &src);
2017
l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
2019
/* Check sender's seps first */
2020
sep = a2dp_find_sep(session, l, sender);
2024
return a2dp_find_sep(session, l, NULL);
2027
unsigned int a2dp_select_capabilities(struct avdtp *session,
2028
uint8_t type, const char *sender,
2029
a2dp_select_cb_t cb,
2032
struct a2dp_setup *setup;
2033
struct a2dp_setup_cb *cb_data;
2034
struct a2dp_sep *sep;
2035
struct avdtp_service_capability *service;
2036
struct avdtp_media_codec_capability *codec;
2039
sep = a2dp_select_sep(session, type, sender);
2041
error("Unable to select SEP");
2045
setup = a2dp_setup_get(session);
2049
cb_data = setup_cb_new(setup);
2050
cb_data->select_cb = cb;
2051
cb_data->user_data = user_data;
2054
setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
2056
if (setup->rsep == NULL) {
2057
error("Could not find remote sep");
2061
/* FIXME: Remove auto select when it is not longer possible to register
2062
endpoint in the configuration file */
2063
if (sep->endpoint == NULL) {
2064
if (!select_capabilities(session, setup->rsep,
2066
error("Unable to auto select remote SEP capabilities");
2070
g_idle_add(auto_select, setup);
2075
service = avdtp_get_codec(setup->rsep);
2076
codec = (struct avdtp_media_codec_capability *) service->data;
2078
err = sep->endpoint->select_configuration(sep, codec->data,
2079
service->length - sizeof(*codec),
2081
select_cb, sep->user_data);
2086
setup_cb_free(cb_data);
2091
unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
2092
a2dp_config_cb_t cb, GSList *caps,
2095
struct a2dp_setup_cb *cb_data;
2097
struct a2dp_server *server;
2098
struct a2dp_setup *setup;
2099
struct a2dp_sep *tmp;
2100
struct avdtp_service_capability *cap;
2101
struct avdtp_media_codec_capability *codec_cap = NULL;
2105
avdtp_get_peers(session, &src, NULL);
2106
server = find_server(servers, &src);
2110
for (l = caps; l != NULL; l = l->next) {
2113
if (cap->category != AVDTP_MEDIA_CODEC)
2116
codec_cap = (void *) cap->data;
2123
if (sep->codec != codec_cap->media_codec_type)
2126
DBG("a2dp_config: selected SEP %p", sep->lsep);
2128
setup = a2dp_setup_get(session);
2132
cb_data = setup_cb_new(setup);
2133
cb_data->config_cb = cb;
2134
cb_data->user_data = user_data;
2137
setup->stream = sep->stream;
2139
/* Copy given caps if they are different than current caps */
2140
if (setup->caps != caps) {
2141
g_slist_free_full(setup->caps, g_free);
2142
setup->caps = g_slist_copy(caps);
2145
switch (avdtp_sep_get_state(sep->lsep)) {
2146
case AVDTP_STATE_IDLE:
2147
if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2148
l = server->sources;
2152
for (; l != NULL; l = l->next) {
2154
if (avdtp_has_stream(session, tmp->stream))
2159
if (a2dp_sep_get_lock(tmp))
2161
setup->reconfigure = TRUE;
2162
if (avdtp_close(session, tmp->stream, FALSE) < 0) {
2163
error("avdtp_close failed");
2169
setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
2170
if (setup->rsep == NULL) {
2171
error("No matching ACP and INT SEPs found");
2175
posix_err = avdtp_set_configuration(session, setup->rsep,
2178
if (posix_err < 0) {
2179
error("avdtp_set_configuration: %s",
2180
strerror(-posix_err));
2184
case AVDTP_STATE_OPEN:
2185
case AVDTP_STATE_STREAMING:
2186
if (avdtp_stream_has_capabilities(setup->stream, caps)) {
2187
DBG("Configuration match: resuming");
2188
cb_data->source_id = g_idle_add(finalize_config,
2190
} else if (!setup->reconfigure) {
2191
setup->reconfigure = TRUE;
2192
if (avdtp_close(session, sep->stream, FALSE) < 0) {
2193
error("avdtp_close failed");
2199
error("SEP in bad state for requesting a new stream");
2206
setup_cb_free(cb_data);
2210
unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
2211
a2dp_stream_cb_t cb, void *user_data)
2213
struct a2dp_setup_cb *cb_data;
2214
struct a2dp_setup *setup;
2216
setup = a2dp_setup_get(session);
2220
cb_data = setup_cb_new(setup);
2221
cb_data->resume_cb = cb;
2222
cb_data->user_data = user_data;
2225
setup->stream = sep->stream;
2227
switch (avdtp_sep_get_state(sep->lsep)) {
2228
case AVDTP_STATE_IDLE:
2231
case AVDTP_STATE_OPEN:
2232
if (avdtp_start(session, sep->stream) < 0) {
2233
error("avdtp_start failed");
2236
sep->starting = TRUE;
2238
case AVDTP_STATE_STREAMING:
2239
if (!sep->suspending && sep->suspend_timer) {
2240
g_source_remove(sep->suspend_timer);
2241
sep->suspend_timer = 0;
2242
avdtp_unref(sep->session);
2243
sep->session = NULL;
2245
if (sep->suspending)
2246
setup->start = TRUE;
2248
cb_data->source_id = g_idle_add(finalize_resume,
2252
error("SEP in bad state for resume");
2259
setup_cb_free(cb_data);
2263
unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
2264
a2dp_stream_cb_t cb, void *user_data)
2266
struct a2dp_setup_cb *cb_data;
2267
struct a2dp_setup *setup;
2269
setup = a2dp_setup_get(session);
2273
cb_data = setup_cb_new(setup);
2274
cb_data->suspend_cb = cb;
2275
cb_data->user_data = user_data;
2278
setup->stream = sep->stream;
2280
switch (avdtp_sep_get_state(sep->lsep)) {
2281
case AVDTP_STATE_IDLE:
2282
error("a2dp_suspend: no stream to suspend");
2285
case AVDTP_STATE_OPEN:
2286
cb_data->source_id = g_idle_add(finalize_suspend, setup);
2288
case AVDTP_STATE_STREAMING:
2289
if (avdtp_suspend(session, sep->stream) < 0) {
2290
error("avdtp_suspend failed");
2293
sep->suspending = TRUE;
2296
error("SEP in bad state for suspend");
2303
setup_cb_free(cb_data);
2307
gboolean a2dp_cancel(struct audio_device *dev, unsigned int id)
2309
struct a2dp_setup *setup;
2312
setup = find_setup_by_dev(dev);
2316
for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
2317
struct a2dp_setup_cb *cb = l->data;
2326
DBG("aborting setup %p", setup);
2327
avdtp_abort(setup->session, setup->stream);
2338
gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
2343
DBG("SEP %p locked", sep->lsep);
2349
gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
2351
struct a2dp_server *server = sep->server;
2352
avdtp_state_t state;
2355
state = avdtp_sep_get_state(sep->lsep);
2357
sep->locked = FALSE;
2359
DBG("SEP %p unlocked", sep->lsep);
2361
if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2362
l = server->sources;
2366
/* Unregister sep if it was removed */
2367
if (g_slist_find(l, sep) == NULL) {
2368
a2dp_unregister_sep(sep);
2372
if (!sep->stream || state == AVDTP_STATE_IDLE)
2376
case AVDTP_STATE_OPEN:
2377
/* Set timer here */
2379
case AVDTP_STATE_STREAMING:
2380
if (avdtp_suspend(session, sep->stream) == 0)
2381
sep->suspending = TRUE;
2390
gboolean a2dp_sep_get_lock(struct a2dp_sep *sep)
2395
static int stream_cmp(gconstpointer data, gconstpointer user_data)
2397
const struct a2dp_sep *sep = data;
2398
const struct avdtp_stream *stream = user_data;
2400
return (sep->stream != stream);
2403
struct a2dp_sep *a2dp_get_sep(struct avdtp *session,
2404
struct avdtp_stream *stream)
2406
struct a2dp_server *server;
2410
avdtp_get_peers(session, &src, &dst);
2412
for (l = servers; l; l = l->next) {
2415
if (bacmp(&src, &server->src) == 0)
2422
l = g_slist_find_custom(server->sources, stream, stream_cmp);
2426
l = g_slist_find_custom(server->sinks, stream, stream_cmp);
2433
struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)