3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2007 Nokia Corporation
6 * Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
32 #include <dbus/dbus.h>
35 #include <bluetooth/bluetooth.h>
36 #include <bluetooth/sdp.h>
37 #include <bluetooth/sdp_lib.h>
47 /* The duration that streams without users are allowed to stay in
49 #define SUSPEND_TIMEOUT 5000
50 #define RECONFIGURE_TIMEOUT 500
53 # define MIN(x, y) ((x) < (y) ? (x) : (y))
57 # define MAX(x, y) ((x) > (y) ? (x) : (y))
63 struct avdtp_local_sep *sep;
64 struct avdtp *session;
65 struct avdtp_stream *stream;
72 struct a2dp_setup_cb {
73 a2dp_config_cb_t config_cb;
74 a2dp_stream_cb_t resume_cb;
75 a2dp_stream_cb_t suspend_cb;
81 struct avdtp *session;
83 struct avdtp_stream *stream;
84 struct avdtp_error *err;
93 static DBusConnection *connection = NULL;
95 static GSList *sinks = NULL;
96 static GSList *sources = NULL;
98 static uint32_t source_record_id = 0;
99 static uint32_t sink_record_id = 0;
101 static GSList *setups = NULL;
102 static unsigned int cb_id = 0;
104 static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
108 debug("setup_ref(%p): ref=%d", setup, setup->ref);
113 static void setup_free(struct a2dp_setup *s)
115 debug("setup_free(%p)", s);
116 setups = g_slist_remove(setups, s);
118 avdtp_unref(s->session);
119 g_slist_foreach(s->cb, (GFunc) g_free, NULL);
124 static void setup_unref(struct a2dp_setup *setup)
128 debug("setup_unref(%p): ref=%d", setup, setup->ref);
134 static struct audio_device *a2dp_get_dev(struct avdtp *session)
138 avdtp_get_peers(session, NULL, &addr);
140 return manager_device_connected(&addr, A2DP_SOURCE_UUID);
143 static gboolean finalize_config(struct a2dp_setup *s)
148 for (l = s->cb; l != NULL; l = l->next) {
149 struct a2dp_setup_cb *cb = l->data;
152 cb->config_cb(s->session, s->sep, s->stream, s->err,
154 cb->config_cb = NULL;
163 static gboolean finalize_config_errno(struct a2dp_setup *s, int err)
165 struct avdtp_error avdtp_err;
167 avdtp_error_init(&avdtp_err, AVDTP_ERROR_ERRNO, -err);
168 s->err = err ? &avdtp_err : NULL;
170 return finalize_config(s);
173 static gboolean finalize_resume(struct a2dp_setup *s)
178 for (l = s->cb; l != NULL; l = l->next) {
179 struct a2dp_setup_cb *cb = l->data;
181 if (cb && cb->resume_cb) {
182 cb->resume_cb(s->session, s->err, cb->user_data);
183 cb->resume_cb = NULL;
192 static gboolean finalize_resume_errno(struct a2dp_setup *s, int err)
194 struct avdtp_error avdtp_err;
196 avdtp_error_init(&avdtp_err, AVDTP_ERROR_ERRNO, -err);
197 s->err = err ? &avdtp_err : NULL;
199 return finalize_resume(s);
202 static gboolean finalize_suspend(struct a2dp_setup *s)
207 for (l = s->cb; l != NULL; l = l->next) {
208 struct a2dp_setup_cb *cb = l->data;
210 if (cb->suspend_cb) {
211 cb->suspend_cb(s->session, s->err, cb->user_data);
212 cb->suspend_cb = NULL;
221 static gboolean finalize_suspend_errno(struct a2dp_setup *s, int err)
223 struct avdtp_error avdtp_err;
225 avdtp_error_init(&avdtp_err, AVDTP_ERROR_ERRNO, -err);
226 s->err = err ? &avdtp_err : NULL;
228 return finalize_suspend(s);
231 static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
235 for (l = setups; l != NULL; l = l->next) {
236 struct a2dp_setup *setup = l->data;
238 if (setup->session == session)
245 static struct a2dp_setup *find_setup_by_dev(struct audio_device *dev)
249 for (l = setups; l != NULL; l = l->next) {
250 struct a2dp_setup *setup = l->data;
251 struct audio_device *setup_dev = a2dp_get_dev(setup->session);
253 if (setup_dev == dev)
260 static void stream_state_changed(struct avdtp_stream *stream,
261 avdtp_state_t old_state,
262 avdtp_state_t new_state,
263 struct avdtp_error *err,
266 struct a2dp_sep *sep = user_data;
268 if (new_state != AVDTP_STATE_IDLE)
271 if (sep->suspend_timer) {
272 g_source_remove(sep->suspend_timer);
273 sep->suspend_timer = 0;
277 avdtp_unref(sep->session);
285 static gboolean sbc_setconf_ind(struct avdtp *session,
286 struct avdtp_local_sep *sep,
287 struct avdtp_stream *stream,
288 GSList *caps, uint8_t *err,
289 uint8_t *category, void *user_data)
291 struct a2dp_sep *a2dp_sep = user_data;
292 struct audio_device *dev;
293 struct avdtp_service_capability *cap;
294 struct avdtp_media_codec_capability *codec_cap;
295 struct sbc_codec_cap *sbc_cap;
297 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
298 debug("Sink %p: Set_Configuration_Ind", sep);
300 debug("Source %p: Set_Configuration_Ind", sep);
302 dev = a2dp_get_dev(session);
304 *err = AVDTP_UNSUPPORTED_CONFIGURATION;
309 /* Check bipool range */
310 for (codec_cap = NULL; caps; caps = g_slist_next(caps)) {
312 if (cap->category == AVDTP_MEDIA_CODEC) {
313 codec_cap = (void *) cap->data;
314 if (codec_cap->media_codec_type == A2DP_CODEC_SBC) {
315 sbc_cap = (void *) codec_cap;
316 if (sbc_cap->min_bitpool < MIN_BITPOOL ||
317 sbc_cap->max_bitpool > MAX_BITPOOL) {
318 *err = AVDTP_UNSUPPORTED_CONFIGURATION;
319 *category = AVDTP_MEDIA_CODEC;
327 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
328 a2dp_sep->stream = stream;
330 if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE)
331 sink_new_stream(dev, session, stream);
336 static gboolean sbc_getcap_ind(struct avdtp *session, struct avdtp_local_sep *sep,
337 GSList **caps, uint8_t *err, void *user_data)
339 struct a2dp_sep *a2dp_sep = user_data;
340 struct avdtp_service_capability *media_transport, *media_codec;
341 struct sbc_codec_cap sbc_cap;
343 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
344 debug("Sink %p: Get_Capability_Ind", sep);
346 debug("Source %p: Get_Capability_Ind", sep);
350 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
353 *caps = g_slist_append(*caps, media_transport);
355 memset(&sbc_cap, 0, sizeof(struct sbc_codec_cap));
357 sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
358 sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC;
361 sbc_cap.frequency = SBC_SAMPLING_FREQ_44100;
363 sbc_cap.frequency = ( SBC_SAMPLING_FREQ_48000 |
364 SBC_SAMPLING_FREQ_44100 |
365 SBC_SAMPLING_FREQ_32000 |
366 SBC_SAMPLING_FREQ_16000 );
369 sbc_cap.channel_mode = ( SBC_CHANNEL_MODE_JOINT_STEREO |
370 SBC_CHANNEL_MODE_STEREO |
371 SBC_CHANNEL_MODE_DUAL_CHANNEL |
372 SBC_CHANNEL_MODE_MONO );
374 sbc_cap.block_length = ( SBC_BLOCK_LENGTH_16 |
375 SBC_BLOCK_LENGTH_12 |
377 SBC_BLOCK_LENGTH_4 );
379 sbc_cap.subbands = ( SBC_SUBBANDS_8 | SBC_SUBBANDS_4 );
381 sbc_cap.allocation_method = ( SBC_ALLOCATION_LOUDNESS |
382 SBC_ALLOCATION_SNR );
384 sbc_cap.min_bitpool = MIN_BITPOOL;
385 sbc_cap.max_bitpool = MAX_BITPOOL;
387 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
390 *caps = g_slist_append(*caps, media_codec);
395 static gboolean mpeg_setconf_ind(struct avdtp *session,
396 struct avdtp_local_sep *sep,
397 struct avdtp_stream *stream,
398 GSList *caps, uint8_t *err,
399 uint8_t *category, void *user_data)
401 struct a2dp_sep *a2dp_sep = user_data;
402 struct audio_device *dev;
404 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
405 debug("Sink %p: Set_Configuration_Ind", sep);
407 debug("Source %p: Set_Configuration_Ind", sep);
409 dev = a2dp_get_dev(session);
411 *err = AVDTP_UNSUPPORTED_CONFIGURATION;
416 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
417 a2dp_sep->stream = stream;
419 if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE)
420 sink_new_stream(dev, session, stream);
425 static gboolean mpeg_getcap_ind(struct avdtp *session,
426 struct avdtp_local_sep *sep,
427 GSList **caps, uint8_t *err, void *user_data)
429 struct a2dp_sep *a2dp_sep = user_data;
430 struct avdtp_service_capability *media_transport, *media_codec;
431 struct mpeg_codec_cap mpeg_cap;
433 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
434 debug("Sink %p: Get_Capability_Ind", sep);
436 debug("Source %p: Get_Capability_Ind", sep);
440 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
443 *caps = g_slist_append(*caps, media_transport);
445 memset(&mpeg_cap, 0, sizeof(struct mpeg_codec_cap));
447 mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
448 mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12;
450 mpeg_cap.frequency = ( MPEG_SAMPLING_FREQ_48000 |
451 MPEG_SAMPLING_FREQ_44100 |
452 MPEG_SAMPLING_FREQ_32000 |
453 MPEG_SAMPLING_FREQ_24000 |
454 MPEG_SAMPLING_FREQ_22050 |
455 MPEG_SAMPLING_FREQ_16000 );
457 mpeg_cap.channel_mode = ( MPEG_CHANNEL_MODE_JOINT_STEREO |
458 MPEG_CHANNEL_MODE_STEREO |
459 MPEG_CHANNEL_MODE_DUAL_CHANNEL |
460 MPEG_CHANNEL_MODE_MONO );
462 mpeg_cap.layer = ( MPEG_LAYER_MP3 | MPEG_LAYER_MP2 | MPEG_LAYER_MP1 );
464 mpeg_cap.bitrate = 0xFFFF;
466 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap,
469 *caps = g_slist_append(*caps, media_codec);
474 static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
475 struct avdtp_stream *stream,
476 struct avdtp_error *err, void *user_data)
478 struct a2dp_sep *a2dp_sep = user_data;
479 struct a2dp_setup *setup;
480 struct audio_device *dev;
483 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
484 debug("Sink %p: Set_Configuration_Cfm", sep);
486 debug("Source %p: Set_Configuration_Cfm", sep);
488 setup = find_setup_by_session(session);
493 finalize_config(setup);
498 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
499 a2dp_sep->stream = stream;
504 dev = a2dp_get_dev(session);
506 /* Notify sink.c of the new stream */
507 sink_new_stream(dev, session, setup->stream);
509 ret = avdtp_open(session, stream);
511 error("Error on avdtp_open %s (%d)", strerror(-ret),
513 setup->stream = NULL;
514 finalize_config_errno(setup, ret);
518 static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
519 uint8_t *err, void *user_data)
521 struct a2dp_sep *a2dp_sep = user_data;
523 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
524 debug("Sink %p: Get_Configuration_Ind");
526 debug("Source %p: Get_Configuration_Ind");
530 static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
531 struct avdtp_stream *stream, struct avdtp_error *err,
534 struct a2dp_sep *a2dp_sep = user_data;
536 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
537 debug("Sink %p: Set_Configuration_Cfm", sep);
539 debug("Source %p: Set_Configuration_Cfm", sep);
542 static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
543 struct avdtp_stream *stream, uint8_t *err,
546 struct a2dp_sep *a2dp_sep = user_data;
548 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
549 debug("Sink %p: Open_Ind", sep);
551 debug("Source %p: Open_Ind", sep);
555 static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
556 struct avdtp_stream *stream, struct avdtp_error *err,
559 struct a2dp_sep *a2dp_sep = user_data;
560 struct a2dp_setup *setup;
562 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
563 debug("Sink %p: Open_Cfm", sep);
565 debug("Source %p: Open_Cfm", sep);
567 setup = find_setup_by_session(session);
571 if (setup->canceled) {
573 avdtp_close(session, stream);
578 if (setup->reconfigure)
579 setup->reconfigure = FALSE;
582 setup->stream = NULL;
584 finalize_config(setup);
587 finalize_config_errno(setup, 0);
590 static gboolean suspend_timeout(struct a2dp_sep *sep)
592 if (avdtp_suspend(sep->session, sep->stream) == 0)
593 sep->suspending = TRUE;
595 sep->suspend_timer = 0;
597 avdtp_unref(sep->session);
603 static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
604 struct avdtp_stream *stream, uint8_t *err,
607 struct a2dp_sep *a2dp_sep = user_data;
609 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
610 debug("Sink %p: Start_Ind", sep);
612 debug("Source %p: Start_Ind", sep);
614 if (!a2dp_sep->locked) {
615 a2dp_sep->session = avdtp_ref(session);
616 a2dp_sep->suspend_timer = g_timeout_add(SUSPEND_TIMEOUT,
617 (GSourceFunc) suspend_timeout,
624 static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
625 struct avdtp_stream *stream, struct avdtp_error *err,
628 struct a2dp_sep *a2dp_sep = user_data;
629 struct a2dp_setup *setup;
631 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
632 debug("Sink %p: Start_Cfm", sep);
634 debug("Source %p: Start_Cfm", sep);
636 setup = find_setup_by_session(session);
640 if (setup->canceled) {
642 avdtp_close(session, stream);
648 setup->stream = NULL;
650 finalize_resume(setup);
653 finalize_resume_errno(setup, 0);
656 static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
657 struct avdtp_stream *stream, uint8_t *err,
660 struct a2dp_sep *a2dp_sep = user_data;
662 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
663 debug("Sink %p: Suspend_Ind", sep);
665 debug("Source %p: Suspend_Ind", sep);
667 if (a2dp_sep->suspend_timer) {
668 g_source_remove(a2dp_sep->suspend_timer);
669 a2dp_sep->suspend_timer = 0;
670 avdtp_unref(a2dp_sep->session);
671 a2dp_sep->session = NULL;
677 static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
678 struct avdtp_stream *stream, struct avdtp_error *err,
681 struct a2dp_sep *a2dp_sep = user_data;
682 struct a2dp_setup *setup;
685 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
686 debug("Sink %p: Suspend_Cfm", sep);
688 debug("Source %p: Suspend_Cfm", sep);
690 a2dp_sep->suspending = FALSE;
692 setup = find_setup_by_session(session);
696 start = setup->start;
697 setup->start = FALSE;
700 setup->stream = NULL;
702 finalize_suspend(setup);
705 finalize_suspend_errno(setup, 0);
712 finalize_suspend(setup);
713 } else if (avdtp_start(session, a2dp_sep->stream) < 0) {
714 struct avdtp_error start_err;
715 error("avdtp_start failed");
716 avdtp_error_init(&start_err, AVDTP_ERROR_ERRNO, EIO);
718 finalize_suspend(setup);
722 static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
723 struct avdtp_stream *stream, uint8_t *err,
726 struct a2dp_sep *a2dp_sep = user_data;
728 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
729 debug("Sink %p: Close_Ind", sep);
731 debug("Source %p: Close_Ind", sep);
736 static gboolean a2dp_reconfigure(gpointer data)
738 struct a2dp_setup *setup = data;
739 struct avdtp_local_sep *lsep;
740 struct avdtp_remote_sep *rsep;
741 struct avdtp_service_capability *cap;
742 struct avdtp_media_codec_capability *codec_cap = NULL;
746 for (l = setup->client_caps; l != NULL; l = l->next) {
749 if (cap->category != AVDTP_MEDIA_CODEC)
752 codec_cap = (void *) cap->data;
756 posix_err = avdtp_get_seps(setup->session, AVDTP_SEP_TYPE_SINK,
757 codec_cap->media_type,
758 codec_cap->media_codec_type,
761 error("No matching ACP and INT SEPs found");
762 finalize_config_errno(setup, posix_err);
765 posix_err = avdtp_set_configuration(setup->session, rsep, lsep,
769 error("avdtp_set_configuration: %s",
770 strerror(-posix_err));
771 finalize_config_errno(setup, posix_err);
777 static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
778 struct avdtp_stream *stream, struct avdtp_error *err,
781 struct a2dp_sep *a2dp_sep = user_data;
782 struct a2dp_setup *setup;
784 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
785 debug("Sink %p: Close_Cfm", sep);
787 debug("Source %p: Close_Cfm", sep);
789 setup = find_setup_by_session(session);
793 if (setup->canceled) {
799 setup->stream = NULL;
801 finalize_config(setup);
805 if (setup->reconfigure)
806 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
809 static gboolean abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
810 struct avdtp_stream *stream, uint8_t *err,
813 struct a2dp_sep *a2dp_sep = user_data;
815 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
816 debug("Sink %p: Abort_Ind", sep);
818 debug("Source %p: Abort_Ind", sep);
820 a2dp_sep->stream = NULL;
825 static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
826 struct avdtp_stream *stream, struct avdtp_error *err,
829 struct a2dp_sep *a2dp_sep = user_data;
830 struct a2dp_setup *setup;
832 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
833 debug("Sink %p: Abort_Cfm", sep);
835 debug("Source %p: Abort_Cfm", sep);
837 setup = find_setup_by_session(session);
844 static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
845 uint8_t *err, void *user_data)
847 struct a2dp_sep *a2dp_sep = user_data;
849 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
850 debug("Sink %p: ReConfigure_Ind", sep);
852 debug("Source %p: ReConfigure_Ind", sep);
856 static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
857 struct avdtp_stream *stream, struct avdtp_error *err,
860 struct a2dp_sep *a2dp_sep = user_data;
861 struct a2dp_setup *setup;
863 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
864 debug("Sink %p: ReConfigure_Cfm", sep);
866 debug("Source %p: ReConfigure_Cfm", sep);
868 setup = find_setup_by_session(session);
872 if (setup->canceled) {
874 avdtp_close(session, stream);
880 setup->stream = NULL;
882 finalize_config(setup);
885 finalize_config_errno(setup, 0);
888 static struct avdtp_sep_cfm cfm = {
889 .set_configuration = setconf_cfm,
890 .get_configuration = getconf_cfm,
893 .suspend = suspend_cfm,
896 .reconfigure = reconf_cfm
899 static struct avdtp_sep_ind sbc_ind = {
900 .get_capability = sbc_getcap_ind,
901 .set_configuration = sbc_setconf_ind,
902 .get_configuration = getconf_ind,
905 .suspend = suspend_ind,
908 .reconfigure = reconf_ind
911 static struct avdtp_sep_ind mpeg_ind = {
912 .get_capability = mpeg_getcap_ind,
913 .set_configuration = mpeg_setconf_ind,
914 .get_configuration = getconf_ind,
917 .suspend = suspend_ind,
920 .reconfigure = reconf_ind
923 static sdp_record_t *a2dp_source_record()
925 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
926 uuid_t root_uuid, l2cap, avdtp, a2src;
927 sdp_profile_desc_t profile[1];
928 sdp_list_t *aproto, *proto[2];
929 sdp_record_t *record;
930 sdp_data_t *psm, *version, *features;
931 uint16_t lp = AVDTP_UUID, ver = 0x0100, feat = 0x000F;
933 record = sdp_record_alloc();
937 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
938 root = sdp_list_append(0, &root_uuid);
939 sdp_set_browse_groups(record, root);
941 sdp_uuid16_create(&a2src, AUDIO_SOURCE_SVCLASS_ID);
942 svclass_id = sdp_list_append(0, &a2src);
943 sdp_set_service_classes(record, svclass_id);
945 sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
946 profile[0].version = 0x0100;
947 pfseq = sdp_list_append(0, &profile[0]);
948 sdp_set_profile_descs(record, pfseq);
950 sdp_uuid16_create(&l2cap, L2CAP_UUID);
951 proto[0] = sdp_list_append(0, &l2cap);
952 psm = sdp_data_alloc(SDP_UINT16, &lp);
953 proto[0] = sdp_list_append(proto[0], psm);
954 apseq = sdp_list_append(0, proto[0]);
956 sdp_uuid16_create(&avdtp, AVDTP_UUID);
957 proto[1] = sdp_list_append(0, &avdtp);
958 version = sdp_data_alloc(SDP_UINT16, &ver);
959 proto[1] = sdp_list_append(proto[1], version);
960 apseq = sdp_list_append(apseq, proto[1]);
962 aproto = sdp_list_append(0, apseq);
963 sdp_set_access_protos(record, aproto);
965 features = sdp_data_alloc(SDP_UINT16, &feat);
966 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
968 sdp_set_info_attr(record, "Audio Source", 0, 0);
972 sdp_list_free(proto[0], 0);
973 sdp_list_free(proto[1], 0);
974 sdp_list_free(apseq, 0);
975 sdp_list_free(pfseq, 0);
976 sdp_list_free(aproto, 0);
977 sdp_list_free(root, 0);
978 sdp_list_free(svclass_id, 0);
983 static sdp_record_t *a2dp_sink_record()
988 static struct a2dp_sep *a2dp_add_sep(DBusConnection *conn, uint8_t type,
991 struct a2dp_sep *sep;
993 sdp_record_t *(*create_record)(void);
995 sdp_record_t *record;
996 struct avdtp_sep_ind *ind;
998 sep = g_new0(struct a2dp_sep, 1);
1000 ind = (codec == A2DP_CODEC_MPEG12) ? &mpeg_ind : &sbc_ind;
1001 sep->sep = avdtp_register_sep(type, AVDTP_MEDIA_TYPE_AUDIO, codec,
1003 if (sep->sep == NULL) {
1011 if (type == AVDTP_SEP_TYPE_SOURCE) {
1013 create_record = a2dp_source_record;
1014 record_id = &source_record_id;
1017 create_record = a2dp_sink_record;
1018 record_id = &sink_record_id;
1021 if (*record_id != 0)
1024 record = create_record();
1026 error("Unable to allocate new service record");
1027 avdtp_unregister_sep(sep->sep);
1032 if (add_record_to_server(BDADDR_ANY, record) < 0) {
1033 error("Unable to register A2DP service record");\
1034 sdp_record_free(record);
1035 avdtp_unregister_sep(sep->sep);
1039 *record_id = record->handle;
1042 *l = g_slist_append(*l, sep);
1047 int a2dp_init(DBusConnection *conn, GKeyFile *config)
1049 int sbc_srcs = 1, sbc_sinks = 0;
1050 int mpeg12_srcs = 0, mpeg12_sinks = 0;
1051 gboolean source = TRUE, sink = TRUE;
1059 str = g_key_file_get_string(config, "General", "Disable", &err);
1062 debug("audio.conf: %s", err->message);
1066 if (strstr(str, "Sink"))
1068 if (strstr(str, "Source"))
1073 str = g_key_file_get_string(config, "A2DP", "SBCSources", &err);
1075 debug("audio.conf: %s", err->message);
1079 sbc_srcs = atoi(str);
1083 str = g_key_file_get_string(config, "A2DP", "MPEG12Sources", &err);
1085 debug("audio.conf: %s", err->message);
1089 mpeg12_srcs = atoi(str);
1093 str = g_key_file_get_string(config, "A2DP", "SBCSinks", &err);
1095 debug("audio.conf: %s", err->message);
1099 sbc_sinks = atoi(str);
1103 str = g_key_file_get_string(config, "A2DP", "MPEG12Sinks", &err);
1105 debug("audio.conf: %s", err->message);
1109 mpeg12_sinks = atoi(str);
1114 connection = dbus_connection_ref(conn);
1119 for (i = 0; i < sbc_srcs; i++)
1120 a2dp_add_sep(conn, AVDTP_SEP_TYPE_SOURCE,
1123 for (i = 0; i < mpeg12_srcs; i++)
1124 a2dp_add_sep(conn, AVDTP_SEP_TYPE_SOURCE,
1129 for (i = 0; i < sbc_sinks; i++)
1130 a2dp_add_sep(conn, AVDTP_SEP_TYPE_SINK,
1133 for (i = 0; i < mpeg12_sinks; i++)
1134 a2dp_add_sep(conn, AVDTP_SEP_TYPE_SINK,
1141 static void a2dp_unregister_sep(struct a2dp_sep *sep)
1143 avdtp_unregister_sep(sep->sep);
1149 g_slist_foreach(sinks, (GFunc) a2dp_unregister_sep, NULL);
1150 g_slist_free(sinks);
1153 g_slist_foreach(sources, (GFunc) a2dp_unregister_sep, NULL);
1154 g_slist_free(sources);
1157 if (source_record_id) {
1158 remove_record_from_server(source_record_id);
1159 source_record_id = 0;
1162 if (sink_record_id) {
1163 remove_record_from_server(sink_record_id);
1167 dbus_connection_unref(connection);
1170 gboolean a2dp_source_cancel(struct audio_device *dev, unsigned int id)
1172 struct a2dp_setup_cb *cb_data;
1173 struct a2dp_setup *setup;
1176 setup = find_setup_by_dev(dev);
1180 for (cb_data = NULL, l = setup->cb; l != NULL; l = g_slist_next(l)) {
1181 struct a2dp_setup_cb *cb = l->data;
1192 setup->cb = g_slist_remove(setup->cb, cb_data);
1196 setup->canceled = TRUE;
1203 unsigned int a2dp_source_config(struct avdtp *session, a2dp_config_cb_t cb,
1204 GSList *caps, void *user_data)
1206 struct a2dp_setup_cb *cb_data;
1208 struct a2dp_setup *setup;
1209 struct a2dp_sep *sep = NULL, *tmp;
1210 struct avdtp_local_sep *lsep;
1211 struct avdtp_remote_sep *rsep;
1212 struct avdtp_service_capability *cap;
1213 struct avdtp_media_codec_capability *codec_cap = NULL;
1216 for (l = caps; l != NULL; l = l->next) {
1219 if (cap->category != AVDTP_MEDIA_CODEC)
1222 codec_cap = (void *) cap->data;
1229 for (l = sources; l != NULL; l = l->next) {
1235 if (tmp->codec != codec_cap->media_codec_type)
1238 if (!tmp->stream || avdtp_has_stream(session, tmp->stream)) {
1245 error("a2dp_source_cfg: no available SEP found");
1249 debug("a2dp_source_config: selected SEP %p", sep->sep);
1251 cb_data = g_new0(struct a2dp_setup_cb, 1);
1252 cb_data->config_cb = cb;
1253 cb_data->user_data = user_data;
1254 cb_data->id = ++cb_id;
1256 setup = find_setup_by_session(session);
1258 setup = g_new0(struct a2dp_setup, 1);
1259 setup->session = avdtp_ref(session);
1260 setups = g_slist_append(setups, setup);
1264 setup->cb = g_slist_append(setup->cb, cb_data);
1266 setup->stream = sep->stream;
1267 setup->client_caps = caps;
1269 switch (avdtp_sep_get_state(sep->sep)) {
1270 case AVDTP_STATE_IDLE:
1271 for (l = sources; l != NULL; l = l->next) {
1274 if (avdtp_has_stream(session, tmp->stream))
1279 setup->reconfigure = TRUE;
1280 if (avdtp_close(session, tmp->stream) < 0) {
1281 error("avdtp_close failed");
1287 if (avdtp_get_seps(session, AVDTP_SEP_TYPE_SINK,
1288 codec_cap->media_type,
1289 codec_cap->media_codec_type,
1290 &lsep, &rsep) < 0) {
1291 error("No matching ACP and INT SEPs found");
1295 posix_err = avdtp_set_configuration(session, rsep, lsep,
1296 caps, &setup->stream);
1297 if (posix_err < 0) {
1298 error("avdtp_set_configuration: %s",
1299 strerror(-posix_err));
1303 case AVDTP_STATE_OPEN:
1304 case AVDTP_STATE_STREAMING:
1305 if (avdtp_stream_has_capabilities(setup->stream, caps))
1306 g_idle_add((GSourceFunc) finalize_config, setup);
1307 else if (!setup->reconfigure) {
1308 setup->reconfigure = TRUE;
1309 if (avdtp_close(session, sep->stream) < 0) {
1310 error("avdtp_close failed");
1316 error("SEP in bad state for requesting a new stream");
1328 unsigned int a2dp_source_resume(struct avdtp *session, struct a2dp_sep *sep,
1329 a2dp_stream_cb_t cb, void *user_data)
1331 struct a2dp_setup_cb *cb_data;
1332 struct a2dp_setup *setup;
1334 cb_data = g_new0(struct a2dp_setup_cb, 1);
1335 cb_data->resume_cb = cb;
1336 cb_data->user_data = user_data;
1337 cb_data->id = ++cb_id;
1339 setup = find_setup_by_session(session);
1341 setup = g_new0(struct a2dp_setup, 1);
1342 setup->session = avdtp_ref(session);
1343 setups = g_slist_append(setups, setup);
1347 setup->cb = g_slist_append(setup->cb, cb_data);
1349 setup->stream = sep->stream;
1351 switch (avdtp_sep_get_state(sep->sep)) {
1352 case AVDTP_STATE_IDLE:
1355 case AVDTP_STATE_OPEN:
1356 if (avdtp_start(session, sep->stream) < 0) {
1357 error("avdtp_start failed");
1361 case AVDTP_STATE_STREAMING:
1362 if (!sep->suspending && sep->suspend_timer) {
1363 g_source_remove(sep->suspend_timer);
1364 sep->suspend_timer = 0;
1365 avdtp_unref(sep->session);
1366 sep->session = NULL;
1368 if (sep->suspending)
1369 setup->start = TRUE;
1371 g_idle_add((GSourceFunc) finalize_resume, setup);
1374 error("SEP in bad state");
1386 unsigned int a2dp_source_suspend(struct avdtp *session, struct a2dp_sep *sep,
1387 a2dp_stream_cb_t cb, void *user_data)
1389 struct a2dp_setup_cb *cb_data;
1390 struct a2dp_setup *setup;
1392 cb_data = g_new0(struct a2dp_setup_cb, 1);
1393 cb_data->suspend_cb = cb;
1394 cb_data->user_data = user_data;
1395 cb_data->id = ++cb_id;
1397 setup = find_setup_by_session(session);
1399 setup = g_new0(struct a2dp_setup, 1);
1400 setup->session = avdtp_ref(session);
1401 setups = g_slist_append(setups, setup);
1405 setup->cb = g_slist_append(setup->cb, cb_data);
1407 setup->stream = sep->stream;
1409 switch (avdtp_sep_get_state(sep->sep)) {
1410 case AVDTP_STATE_IDLE:
1411 error("a2dp_source_suspend: no stream to suspend");
1414 case AVDTP_STATE_OPEN:
1415 g_idle_add((GSourceFunc) finalize_suspend, setup);
1417 case AVDTP_STATE_STREAMING:
1418 if (avdtp_suspend(session, sep->stream) < 0) {
1419 error("avdtp_suspend failed");
1424 error("SEP in bad state for resume");
1436 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
1441 debug("SEP %p locked", sep->sep);
1447 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
1449 avdtp_state_t state;
1451 state = avdtp_sep_get_state(sep->sep);
1453 sep->locked = FALSE;
1455 debug("SEP %p unlocked", sep->sep);
1457 if (!sep->stream || state == AVDTP_STATE_IDLE)
1461 case AVDTP_STATE_OPEN:
1462 /* Set timer here */
1464 case AVDTP_STATE_STREAMING:
1465 if (avdtp_suspend(session, sep->stream) == 0)
1466 sep->suspending = TRUE;