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>
48 #include "a2dp-codecs.h"
51 /* The duration that streams without users are allowed to stay in
53 #define SUSPEND_TIMEOUT 5
54 #define RECONFIGURE_TIMEOUT 500
57 # define MIN(x, y) ((x) < (y) ? (x) : (y))
61 # define MAX(x, y) ((x) > (y) ? (x) : (y))
65 struct a2dp_server *server;
66 struct a2dp_endpoint *endpoint;
69 struct avdtp_local_sep *lsep;
70 struct avdtp *session;
71 struct avdtp_stream *stream;
73 gboolean delay_reporting;
78 GDestroyNotify destroy;
81 struct a2dp_setup_cb {
82 struct a2dp_setup *setup;
83 a2dp_select_cb_t select_cb;
84 a2dp_config_cb_t config_cb;
85 a2dp_stream_cb_t resume_cb;
86 a2dp_stream_cb_t suspend_cb;
93 struct audio_device *dev;
94 struct avdtp *session;
96 struct avdtp_remote_sep *rsep;
97 struct avdtp_stream *stream;
98 struct avdtp_error *err;
99 avdtp_set_configuration_cb setconf_cb;
101 gboolean reconfigure;
107 static DBusConnection *connection = NULL;
113 uint32_t source_record_id;
114 uint32_t sink_record_id;
116 gboolean sink_enabled;
117 gboolean source_enabled;
120 static GSList *servers = NULL;
121 static GSList *setups = NULL;
122 static unsigned int cb_id = 0;
124 static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
128 DBG("%p: ref=%d", setup, setup->ref);
133 static struct audio_device *a2dp_get_dev(struct avdtp *session)
137 avdtp_get_peers(session, &src, &dst);
139 return manager_find_device(NULL, &src, &dst, NULL, FALSE);
142 static struct a2dp_setup *setup_new(struct avdtp *session)
144 struct audio_device *dev;
145 struct a2dp_setup *setup;
147 dev = a2dp_get_dev(session);
149 error("Unable to create setup");
153 setup = g_new0(struct a2dp_setup, 1);
154 setup->session = avdtp_ref(session);
155 setup->dev = a2dp_get_dev(session);
156 setups = g_slist_append(setups, setup);
161 static void setup_free(struct a2dp_setup *s)
165 setups = g_slist_remove(setups, s);
167 avdtp_unref(s->session);
168 g_slist_free_full(s->cb, g_free);
169 g_slist_free_full(s->caps, g_free);
173 static void setup_unref(struct a2dp_setup *setup)
177 DBG("%p: ref=%d", setup, setup->ref);
185 static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup)
187 struct a2dp_setup_cb *cb;
189 cb = g_new0(struct a2dp_setup_cb, 1);
193 setup->cb = g_slist_append(setup->cb, cb);
197 static void setup_cb_free(struct a2dp_setup_cb *cb)
199 struct a2dp_setup *setup = cb->setup;
202 g_source_remove(cb->source_id);
204 setup->cb = g_slist_remove(setup->cb, cb);
205 setup_unref(cb->setup);
209 static void finalize_setup_errno(struct a2dp_setup *s, int err,
210 GSourceFunc cb1, ...)
212 GSourceFunc finalize;
214 struct avdtp_error avdtp_err;
217 avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err);
224 while (finalize != NULL) {
226 finalize = va_arg(args, GSourceFunc);
232 static gboolean finalize_config(gpointer data)
234 struct a2dp_setup *s = data;
236 struct avdtp_stream *stream = s->err ? NULL : s->stream;
238 for (l = s->cb; l != NULL; ) {
239 struct a2dp_setup_cb *cb = l->data;
246 cb->config_cb(s->session, s->sep, stream, s->err,
254 static gboolean finalize_resume(gpointer data)
256 struct a2dp_setup *s = data;
259 for (l = s->cb; l != NULL; ) {
260 struct a2dp_setup_cb *cb = l->data;
267 cb->resume_cb(s->session, s->err, cb->user_data);
274 static gboolean finalize_suspend(gpointer data)
276 struct a2dp_setup *s = data;
279 for (l = s->cb; l != NULL; ) {
280 struct a2dp_setup_cb *cb = l->data;
287 cb->suspend_cb(s->session, s->err, cb->user_data);
294 static void finalize_select(struct a2dp_setup *s)
298 for (l = s->cb; l != NULL; ) {
299 struct a2dp_setup_cb *cb = l->data;
306 cb->select_cb(s->session, s->sep, s->caps, cb->user_data);
311 static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
315 for (l = setups; l != NULL; l = l->next) {
316 struct a2dp_setup *setup = l->data;
318 if (setup->session == session)
325 static struct a2dp_setup *a2dp_setup_get(struct avdtp *session)
327 struct a2dp_setup *setup;
329 setup = find_setup_by_session(session);
331 setup = setup_new(session);
336 return setup_ref(setup);
339 static struct a2dp_setup *find_setup_by_dev(struct audio_device *dev)
343 for (l = setups; l != NULL; l = l->next) {
344 struct a2dp_setup *setup = l->data;
346 if (setup->dev == dev)
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,
359 struct a2dp_sep *sep = user_data;
361 if (new_state != AVDTP_STATE_IDLE)
364 if (sep->suspend_timer) {
365 g_source_remove(sep->suspend_timer);
366 sep->suspend_timer = 0;
370 avdtp_unref(sep->session);
376 if (sep->endpoint && sep->endpoint->clear_configuration)
377 sep->endpoint->clear_configuration(sep, sep->user_data);
380 static gboolean auto_config(gpointer data)
382 struct a2dp_setup *setup = data;
383 struct avdtp_error *err = NULL;
385 /* Check if configuration was aborted */
386 if (setup->sep->stream == NULL)
389 if (setup->err != NULL) {
394 avdtp_stream_add_cb(setup->session, setup->stream,
395 stream_state_changed, setup->sep);
397 if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE)
398 sink_new_stream(setup->dev, setup->session, setup->stream);
400 source_new_stream(setup->dev, setup->session, setup->stream);
403 if (setup->setconf_cb)
404 setup->setconf_cb(setup->session, setup->stream, setup->err);
406 finalize_config(setup);
416 static gboolean sbc_setconf_ind(struct avdtp *session,
417 struct avdtp_local_sep *sep,
418 struct avdtp_stream *stream,
420 avdtp_set_configuration_cb cb,
423 struct a2dp_sep *a2dp_sep = user_data;
424 struct a2dp_setup *setup;
426 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
427 DBG("Sink %p: Set_Configuration_Ind", sep);
429 DBG("Source %p: Set_Configuration_Ind", sep);
431 setup = a2dp_setup_get(session);
435 a2dp_sep->stream = stream;
436 setup->sep = a2dp_sep;
437 setup->stream = stream;
438 setup->setconf_cb = cb;
440 /* Check valid settings */
441 for (; caps != NULL; caps = g_slist_next(caps)) {
442 struct avdtp_service_capability *cap = caps->data;
443 struct avdtp_media_codec_capability *codec_cap;
444 struct sbc_codec_cap *sbc_cap;
446 if (cap->category == AVDTP_DELAY_REPORTING &&
447 !a2dp_sep->delay_reporting) {
448 setup->err = g_new(struct avdtp_error, 1);
449 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
450 AVDTP_UNSUPPORTED_CONFIGURATION);
454 if (cap->category != AVDTP_MEDIA_CODEC)
457 if (cap->length < sizeof(struct sbc_codec_cap))
460 codec_cap = (void *) cap->data;
462 if (codec_cap->media_codec_type != A2DP_CODEC_SBC)
465 sbc_cap = (void *) codec_cap;
467 if (sbc_cap->min_bitpool < MIN_BITPOOL ||
468 sbc_cap->max_bitpool > MAX_BITPOOL) {
469 setup->err = g_new(struct avdtp_error, 1);
470 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
471 AVDTP_UNSUPPORTED_CONFIGURATION);
477 g_idle_add(auto_config, setup);
481 static gboolean sbc_getcap_ind(struct avdtp *session, struct avdtp_local_sep *sep,
482 gboolean get_all, GSList **caps, uint8_t *err,
485 struct a2dp_sep *a2dp_sep = user_data;
486 struct avdtp_service_capability *media_transport, *media_codec;
487 struct sbc_codec_cap sbc_cap;
489 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
490 DBG("Sink %p: Get_Capability_Ind", sep);
492 DBG("Source %p: Get_Capability_Ind", sep);
496 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
499 *caps = g_slist_append(*caps, media_transport);
501 memset(&sbc_cap, 0, sizeof(struct sbc_codec_cap));
503 sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
504 sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC;
506 sbc_cap.frequency = ( SBC_SAMPLING_FREQ_48000 |
507 SBC_SAMPLING_FREQ_44100 |
508 SBC_SAMPLING_FREQ_32000 |
509 SBC_SAMPLING_FREQ_16000 );
511 sbc_cap.channel_mode = ( SBC_CHANNEL_MODE_JOINT_STEREO |
512 SBC_CHANNEL_MODE_STEREO |
513 SBC_CHANNEL_MODE_DUAL_CHANNEL |
514 SBC_CHANNEL_MODE_MONO );
516 sbc_cap.block_length = ( SBC_BLOCK_LENGTH_16 |
517 SBC_BLOCK_LENGTH_12 |
519 SBC_BLOCK_LENGTH_4 );
521 sbc_cap.subbands = ( SBC_SUBBANDS_8 | SBC_SUBBANDS_4 );
523 sbc_cap.allocation_method = ( SBC_ALLOCATION_LOUDNESS |
524 SBC_ALLOCATION_SNR );
526 sbc_cap.min_bitpool = MIN_BITPOOL;
527 sbc_cap.max_bitpool = MAX_BITPOOL;
529 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
532 *caps = g_slist_append(*caps, media_codec);
535 struct avdtp_service_capability *delay_reporting;
536 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
538 *caps = g_slist_append(*caps, delay_reporting);
544 static gboolean mpeg_setconf_ind(struct avdtp *session,
545 struct avdtp_local_sep *sep,
546 struct avdtp_stream *stream,
548 avdtp_set_configuration_cb cb,
551 struct a2dp_sep *a2dp_sep = user_data;
552 struct a2dp_setup *setup;
554 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
555 DBG("Sink %p: Set_Configuration_Ind", sep);
557 DBG("Source %p: Set_Configuration_Ind", sep);
559 setup = a2dp_setup_get(session);
563 a2dp_sep->stream = stream;
564 setup->sep = a2dp_sep;
565 setup->stream = stream;
566 setup->setconf_cb = cb;
568 for (; caps != NULL; caps = g_slist_next(caps)) {
569 struct avdtp_service_capability *cap = caps->data;
571 if (cap->category == AVDTP_DELAY_REPORTING &&
572 !a2dp_sep->delay_reporting) {
573 setup->err = g_new(struct avdtp_error, 1);
574 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
575 AVDTP_UNSUPPORTED_CONFIGURATION);
581 g_idle_add(auto_config, setup);
585 static gboolean mpeg_getcap_ind(struct avdtp *session,
586 struct avdtp_local_sep *sep,
588 GSList **caps, uint8_t *err, void *user_data)
590 struct a2dp_sep *a2dp_sep = user_data;
591 struct avdtp_service_capability *media_transport, *media_codec;
592 struct mpeg_codec_cap mpeg_cap;
594 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
595 DBG("Sink %p: Get_Capability_Ind", sep);
597 DBG("Source %p: Get_Capability_Ind", sep);
601 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
604 *caps = g_slist_append(*caps, media_transport);
606 memset(&mpeg_cap, 0, sizeof(struct mpeg_codec_cap));
608 mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
609 mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12;
611 mpeg_cap.frequency = ( MPEG_SAMPLING_FREQ_48000 |
612 MPEG_SAMPLING_FREQ_44100 |
613 MPEG_SAMPLING_FREQ_32000 |
614 MPEG_SAMPLING_FREQ_24000 |
615 MPEG_SAMPLING_FREQ_22050 |
616 MPEG_SAMPLING_FREQ_16000 );
618 mpeg_cap.channel_mode = ( MPEG_CHANNEL_MODE_JOINT_STEREO |
619 MPEG_CHANNEL_MODE_STEREO |
620 MPEG_CHANNEL_MODE_DUAL_CHANNEL |
621 MPEG_CHANNEL_MODE_MONO );
623 mpeg_cap.layer = ( MPEG_LAYER_MP3 | MPEG_LAYER_MP2 | MPEG_LAYER_MP1 );
625 mpeg_cap.bitrate = 0xFFFF;
627 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap,
630 *caps = g_slist_append(*caps, media_codec);
633 struct avdtp_service_capability *delay_reporting;
634 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
636 *caps = g_slist_append(*caps, delay_reporting);
643 static void endpoint_setconf_cb(struct a2dp_setup *setup, gboolean ret)
646 setup->err = g_new(struct avdtp_error, 1);
647 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
648 AVDTP_UNSUPPORTED_CONFIGURATION);
650 #ifdef __BT_SCMST_FEATURE__
651 if (avdtp_get_protection_cap(setup->stream)) {
652 DBG("Recived Set congiuration for SCMS-T \n");
653 sink_set_protection(TRUE);
655 DBG("SCMS-T not supported\n");
656 sink_set_protection(FALSE);
658 sink_stream_protected(setup->dev);
664 static gboolean endpoint_setconf_ind(struct avdtp *session,
665 struct avdtp_local_sep *sep,
666 struct avdtp_stream *stream,
668 avdtp_set_configuration_cb cb,
671 struct a2dp_sep *a2dp_sep = user_data;
672 struct a2dp_setup *setup;
674 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
675 DBG("Sink %p: Set_Configuration_Ind", sep);
677 DBG("Source %p: Set_Configuration_Ind", sep);
679 setup = a2dp_setup_get(session);
683 a2dp_sep->stream = stream;
684 setup->sep = a2dp_sep;
685 setup->stream = stream;
686 setup->setconf_cb = cb;
688 for (; caps != NULL; caps = g_slist_next(caps)) {
689 struct avdtp_service_capability *cap = caps->data;
690 struct avdtp_media_codec_capability *codec;
693 if (cap->category == AVDTP_DELAY_REPORTING &&
694 !a2dp_sep->delay_reporting) {
695 setup->err = g_new(struct avdtp_error, 1);
696 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
697 AVDTP_UNSUPPORTED_CONFIGURATION);
701 if (cap->category != AVDTP_MEDIA_CODEC)
704 codec = (struct avdtp_media_codec_capability *) cap->data;
706 if (codec->media_codec_type != a2dp_sep->codec) {
707 setup->err = g_new(struct avdtp_error, 1);
708 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
709 AVDTP_UNSUPPORTED_CONFIGURATION);
713 ret = a2dp_sep->endpoint->set_configuration(a2dp_sep,
714 setup->dev, codec->data,
715 cap->length - sizeof(*codec),
718 a2dp_sep->user_data);
722 setup->err = g_new(struct avdtp_error, 1);
723 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
724 AVDTP_UNSUPPORTED_CONFIGURATION);
729 g_idle_add(auto_config, setup);
733 static gboolean endpoint_getcap_ind(struct avdtp *session,
734 struct avdtp_local_sep *sep,
735 gboolean get_all, GSList **caps,
736 uint8_t *err, void *user_data)
738 struct a2dp_sep *a2dp_sep = user_data;
739 struct avdtp_service_capability *media_transport, *media_codec;
740 struct avdtp_media_codec_capability *codec_caps;
741 uint8_t *capabilities;
743 #ifdef __BT_SCMST_FEATURE__
744 struct avdtp_service_capability *media_content_protection;
745 struct avdtp_cp_cap content_protection;
750 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
751 DBG("Sink %p: Get_Capability_Ind", sep);
753 DBG("Source %p: Get_Capability_Ind", sep);
757 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
760 *caps = g_slist_append(*caps, media_transport);
762 length = a2dp_sep->endpoint->get_capabilities(a2dp_sep, &capabilities,
763 a2dp_sep->user_data);
765 codec_caps = g_malloc0(sizeof(*codec_caps) + length);
766 codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO;
767 codec_caps->media_codec_type = a2dp_sep->codec;
768 memcpy(codec_caps->data, capabilities, length);
770 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps,
771 sizeof(*codec_caps) + length);
773 *caps = g_slist_append(*caps, media_codec);
776 #ifdef __BT_SCMST_FEATURE__
777 cp_length = a2dp_sep->endpoint->get_contect_protection_capabilites(
779 a2dp_sep->user_data);
782 content_protection.cp_type_lsb = cp_caps[0] & 0xFF;
783 content_protection.cp_type_msb = cp_caps[1];
785 media_content_protection = avdtp_service_cap_new(
786 AVDTP_CONTENT_PROTECTION,
787 &content_protection, cp_length);
789 *caps = g_slist_append(*caps, media_content_protection);
793 struct avdtp_service_capability *delay_reporting;
794 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
796 *caps = g_slist_append(*caps, delay_reporting);
802 static void endpoint_open_cb(struct a2dp_setup *setup, gboolean ret)
807 setup->stream = NULL;
808 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
812 err = avdtp_open(setup->session, setup->stream);
816 error("Error on avdtp_open %s (%d)", strerror(-err), -err);
817 setup->stream = NULL;
818 finalize_setup_errno(setup, err, finalize_config, NULL);
821 static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
822 struct avdtp_stream *stream,
823 struct avdtp_error *err, void *user_data)
825 struct a2dp_sep *a2dp_sep = user_data;
826 struct a2dp_setup *setup;
827 struct audio_device *dev;
830 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
831 DBG("Sink %p: Set_Configuration_Cfm", sep);
833 DBG("Source %p: Set_Configuration_Cfm", sep);
835 setup = find_setup_by_session(session);
840 finalize_config(setup);
845 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
846 a2dp_sep->stream = stream;
851 dev = a2dp_get_dev(session);
853 /* Notify D-Bus interface of the new stream */
854 if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE)
855 sink_new_stream(dev, session, setup->stream);
857 source_new_stream(dev, session, setup->stream);
859 /* Notify Endpoint */
860 if (a2dp_sep->endpoint) {
861 struct avdtp_service_capability *service;
862 struct avdtp_media_codec_capability *codec;
865 service = avdtp_stream_get_codec(stream);
866 codec = (struct avdtp_media_codec_capability *) service->data;
868 err = a2dp_sep->endpoint->set_configuration(a2dp_sep, dev,
869 codec->data, service->length -
873 a2dp_sep->user_data);
877 setup->stream = NULL;
878 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
882 ret = avdtp_open(session, stream);
884 error("Error on avdtp_open %s (%d)", strerror(-ret), -ret);
885 setup->stream = NULL;
886 finalize_setup_errno(setup, ret, finalize_config, NULL);
890 static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
891 uint8_t *err, void *user_data)
893 struct a2dp_sep *a2dp_sep = user_data;
895 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
896 DBG("Sink %p: Get_Configuration_Ind", sep);
898 DBG("Source %p: Get_Configuration_Ind", sep);
902 static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
903 struct avdtp_stream *stream, struct avdtp_error *err,
906 struct a2dp_sep *a2dp_sep = user_data;
908 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
909 DBG("Sink %p: Set_Configuration_Cfm", sep);
911 DBG("Source %p: Set_Configuration_Cfm", sep);
914 static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
915 struct avdtp_stream *stream, uint8_t *err,
918 struct a2dp_sep *a2dp_sep = user_data;
919 struct a2dp_setup *setup;
921 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
922 DBG("Sink %p: Open_Ind", sep);
924 DBG("Source %p: Open_Ind", sep);
926 setup = find_setup_by_session(session);
930 if (setup->reconfigure)
931 setup->reconfigure = FALSE;
933 finalize_config(setup);
938 static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
939 struct avdtp_stream *stream, struct avdtp_error *err,
942 struct a2dp_sep *a2dp_sep = user_data;
943 struct a2dp_setup *setup;
945 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
946 DBG("Sink %p: Open_Cfm", sep);
948 DBG("Source %p: Open_Cfm", sep);
950 setup = find_setup_by_session(session);
954 if (setup->reconfigure)
955 setup->reconfigure = FALSE;
958 setup->stream = NULL;
962 finalize_config(setup);
965 static gboolean suspend_timeout(struct a2dp_sep *sep)
967 if (avdtp_suspend(sep->session, sep->stream) == 0)
968 sep->suspending = TRUE;
970 sep->suspend_timer = 0;
972 avdtp_unref(sep->session);
978 static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
979 struct avdtp_stream *stream, uint8_t *err,
982 struct a2dp_sep *a2dp_sep = user_data;
983 struct a2dp_setup *setup;
985 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
986 DBG("Sink %p: Start_Ind", sep);
988 DBG("Source %p: Start_Ind", sep);
990 if (!a2dp_sep->locked) {
991 a2dp_sep->session = avdtp_ref(session);
992 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
993 (GSourceFunc) suspend_timeout,
997 if (!a2dp_sep->starting)
1000 a2dp_sep->starting = FALSE;
1002 setup = find_setup_by_session(session);
1004 finalize_resume(setup);
1009 static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1010 struct avdtp_stream *stream, struct avdtp_error *err,
1013 struct a2dp_sep *a2dp_sep = user_data;
1014 struct a2dp_setup *setup;
1016 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1017 DBG("Sink %p: Start_Cfm", sep);
1019 DBG("Source %p: Start_Cfm", sep);
1021 a2dp_sep->starting = FALSE;
1023 setup = find_setup_by_session(session);
1028 setup->stream = NULL;
1032 finalize_resume(setup);
1035 static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1036 struct avdtp_stream *stream, uint8_t *err,
1039 struct a2dp_sep *a2dp_sep = user_data;
1040 struct a2dp_setup *setup;
1044 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1045 DBG("Sink %p: Suspend_Ind", sep);
1047 DBG("Source %p: Suspend_Ind", sep);
1049 if (a2dp_sep->suspend_timer) {
1050 g_source_remove(a2dp_sep->suspend_timer);
1051 a2dp_sep->suspend_timer = 0;
1052 avdtp_unref(a2dp_sep->session);
1053 a2dp_sep->session = NULL;
1056 if (!a2dp_sep->suspending)
1059 a2dp_sep->suspending = FALSE;
1061 setup = find_setup_by_session(session);
1065 start = setup->start;
1066 setup->start = FALSE;
1068 finalize_suspend(setup);
1073 start_err = avdtp_start(session, a2dp_sep->stream);
1074 if (start_err < 0 && start_err != -EINPROGRESS) {
1075 error("avdtp_start: %s (%d)", strerror(-start_err),
1077 #ifdef __TIZEN_PATCH__
1078 finalize_setup_errno(setup, start_err, finalize_resume, NULL);
1080 finalize_setup_errno(setup, start_err, finalize_resume);
1087 static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1088 struct avdtp_stream *stream, struct avdtp_error *err,
1091 struct a2dp_sep *a2dp_sep = user_data;
1092 struct a2dp_setup *setup;
1096 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1097 DBG("Sink %p: Suspend_Cfm", sep);
1099 DBG("Source %p: Suspend_Cfm", sep);
1101 a2dp_sep->suspending = FALSE;
1103 setup = find_setup_by_session(session);
1107 start = setup->start;
1108 setup->start = FALSE;
1111 setup->stream = NULL;
1115 finalize_suspend(setup);
1121 finalize_resume(setup);
1125 start_err = avdtp_start(session, a2dp_sep->stream);
1126 if (start_err < 0 && start_err != -EINPROGRESS) {
1127 error("avdtp_start: %s (%d)", strerror(-start_err),
1129 finalize_setup_errno(setup, start_err, finalize_suspend, NULL);
1133 static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1134 struct avdtp_stream *stream, uint8_t *err,
1137 struct a2dp_sep *a2dp_sep = user_data;
1138 struct a2dp_setup *setup;
1140 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1141 DBG("Sink %p: Close_Ind", sep);
1143 DBG("Source %p: Close_Ind", sep);
1145 setup = find_setup_by_session(session);
1149 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1150 finalize_resume, NULL);
1155 static gboolean a2dp_reconfigure(gpointer data)
1157 struct a2dp_setup *setup = data;
1158 struct a2dp_sep *sep = setup->sep;
1160 struct avdtp_media_codec_capability *rsep_codec;
1161 struct avdtp_service_capability *cap;
1164 cap = avdtp_get_codec(setup->rsep);
1165 rsep_codec = (struct avdtp_media_codec_capability *) cap->data;
1168 if (!setup->rsep || sep->codec != rsep_codec->media_codec_type)
1169 setup->rsep = avdtp_find_remote_sep(setup->session, sep->lsep);
1171 posix_err = avdtp_set_configuration(setup->session, setup->rsep,
1175 if (posix_err < 0) {
1176 error("avdtp_set_configuration: %s", strerror(-posix_err));
1183 finalize_setup_errno(setup, posix_err, finalize_config, NULL);
1187 static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1188 struct avdtp_stream *stream, struct avdtp_error *err,
1191 struct a2dp_sep *a2dp_sep = user_data;
1192 struct a2dp_setup *setup;
1194 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1195 DBG("Sink %p: Close_Cfm", sep);
1197 DBG("Source %p: Close_Cfm", sep);
1199 setup = find_setup_by_session(session);
1204 setup->stream = NULL;
1206 finalize_config(setup);
1211 setup->rsep = avdtp_stream_get_remote_sep(stream);
1213 if (setup->reconfigure)
1214 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
1217 static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1218 struct avdtp_stream *stream, uint8_t *err,
1221 struct a2dp_sep *a2dp_sep = user_data;
1222 struct a2dp_setup *setup;
1224 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1225 DBG("Sink %p: Abort_Ind", sep);
1227 DBG("Source %p: Abort_Ind", sep);
1229 a2dp_sep->stream = NULL;
1231 setup = find_setup_by_session(session);
1234 #ifdef __TIZEN_PATCH__
1235 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1237 finalize_config, NULL);
1239 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1247 static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1248 struct avdtp_stream *stream, struct avdtp_error *err,
1251 struct a2dp_sep *a2dp_sep = user_data;
1252 struct a2dp_setup *setup;
1254 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1255 DBG("Sink %p: Abort_Cfm", sep);
1257 DBG("Source %p: Abort_Cfm", sep);
1259 setup = find_setup_by_session(session);
1266 static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1267 uint8_t *err, void *user_data)
1269 struct a2dp_sep *a2dp_sep = user_data;
1271 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1272 DBG("Sink %p: ReConfigure_Ind", sep);
1274 DBG("Source %p: ReConfigure_Ind", sep);
1279 static gboolean delayreport_ind(struct avdtp *session,
1280 struct avdtp_local_sep *sep,
1281 uint8_t rseid, uint16_t delay,
1282 uint8_t *err, void *user_data)
1284 struct a2dp_sep *a2dp_sep = user_data;
1285 struct audio_device *dev = a2dp_get_dev(session);
1287 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1288 DBG("Sink %p: DelayReport_Ind", sep);
1290 DBG("Source %p: DelayReport_Ind", sep);
1292 unix_delay_report(dev, rseid, delay);
1297 static gboolean endpoint_delayreport_ind(struct avdtp *session,
1298 struct avdtp_local_sep *sep,
1299 uint8_t rseid, uint16_t delay,
1300 uint8_t *err, void *user_data)
1302 struct a2dp_sep *a2dp_sep = user_data;
1304 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1305 DBG("Sink %p: DelayReport_Ind", sep);
1307 DBG("Source %p: DelayReport_Ind", sep);
1309 if (a2dp_sep->endpoint == NULL ||
1310 a2dp_sep->endpoint->set_delay == NULL)
1313 a2dp_sep->endpoint->set_delay(a2dp_sep, delay, a2dp_sep->user_data);
1318 static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1319 struct avdtp_stream *stream, struct avdtp_error *err,
1322 struct a2dp_sep *a2dp_sep = user_data;
1323 struct a2dp_setup *setup;
1325 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1326 DBG("Sink %p: ReConfigure_Cfm", sep);
1328 DBG("Source %p: ReConfigure_Cfm", sep);
1330 setup = find_setup_by_session(session);
1335 setup->stream = NULL;
1339 finalize_config(setup);
1342 static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1343 struct avdtp_stream *stream,
1344 struct avdtp_error *err, void *user_data)
1346 struct a2dp_sep *a2dp_sep = user_data;
1348 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1349 DBG("Sink %p: DelayReport_Cfm", sep);
1351 DBG("Source %p: DelayReport_Cfm", sep);
1354 static struct avdtp_sep_cfm cfm = {
1355 .set_configuration = setconf_cfm,
1356 .get_configuration = getconf_cfm,
1359 .suspend = suspend_cfm,
1362 .reconfigure = reconf_cfm,
1363 .delay_report = delay_report_cfm,
1366 static struct avdtp_sep_ind sbc_ind = {
1367 .get_capability = sbc_getcap_ind,
1368 .set_configuration = sbc_setconf_ind,
1369 .get_configuration = getconf_ind,
1372 .suspend = suspend_ind,
1375 .reconfigure = reconf_ind,
1376 .delayreport = delayreport_ind,
1379 static struct avdtp_sep_ind mpeg_ind = {
1380 .get_capability = mpeg_getcap_ind,
1381 .set_configuration = mpeg_setconf_ind,
1382 .get_configuration = getconf_ind,
1385 .suspend = suspend_ind,
1388 .reconfigure = reconf_ind,
1389 .delayreport = delayreport_ind,
1392 static struct avdtp_sep_ind endpoint_ind = {
1393 .get_capability = endpoint_getcap_ind,
1394 .set_configuration = endpoint_setconf_ind,
1395 .get_configuration = getconf_ind,
1398 .suspend = suspend_ind,
1401 .reconfigure = reconf_ind,
1402 .delayreport = endpoint_delayreport_ind,
1405 static sdp_record_t *a2dp_record(uint8_t type, uint16_t avdtp_ver)
1407 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
1408 uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
1409 sdp_profile_desc_t profile[1];
1410 sdp_list_t *aproto, *proto[2];
1411 sdp_record_t *record;
1412 sdp_data_t *psm, *version, *features;
1413 uint16_t lp = AVDTP_UUID;
1414 #ifdef __TIZEN_PATCH__
1415 uint16_t a2dp_ver = 0x0102, feat = 0x0001;
1417 uint16_t a2dp_ver = 0x0102, feat = 0x000f;
1420 record = sdp_record_alloc();
1424 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1425 root = sdp_list_append(0, &root_uuid);
1426 sdp_set_browse_groups(record, root);
1428 if (type == AVDTP_SEP_TYPE_SOURCE)
1429 sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
1431 sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID);
1432 svclass_id = sdp_list_append(0, &a2dp_uuid);
1433 sdp_set_service_classes(record, svclass_id);
1435 sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
1436 profile[0].version = a2dp_ver;
1437 pfseq = sdp_list_append(0, &profile[0]);
1438 sdp_set_profile_descs(record, pfseq);
1440 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1441 proto[0] = sdp_list_append(0, &l2cap_uuid);
1442 psm = sdp_data_alloc(SDP_UINT16, &lp);
1443 proto[0] = sdp_list_append(proto[0], psm);
1444 apseq = sdp_list_append(0, proto[0]);
1446 sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
1447 proto[1] = sdp_list_append(0, &avdtp_uuid);
1448 version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
1449 proto[1] = sdp_list_append(proto[1], version);
1450 apseq = sdp_list_append(apseq, proto[1]);
1452 aproto = sdp_list_append(0, apseq);
1453 sdp_set_access_protos(record, aproto);
1455 features = sdp_data_alloc(SDP_UINT16, &feat);
1456 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
1458 if (type == AVDTP_SEP_TYPE_SOURCE)
1459 sdp_set_info_attr(record, "Audio Source", 0, 0);
1461 sdp_set_info_attr(record, "Audio Sink", 0, 0);
1465 sdp_list_free(proto[0], 0);
1466 sdp_list_free(proto[1], 0);
1467 sdp_list_free(apseq, 0);
1468 sdp_list_free(pfseq, 0);
1469 sdp_list_free(aproto, 0);
1470 sdp_list_free(root, 0);
1471 sdp_list_free(svclass_id, 0);
1476 static struct a2dp_server *find_server(GSList *list, const bdaddr_t *src)
1479 for (; list; list = list->next) {
1480 struct a2dp_server *server = list->data;
1482 if (bacmp(&server->src, src) == 0)
1489 int a2dp_register(DBusConnection *conn, const bdaddr_t *src, GKeyFile *config)
1491 int sbc_srcs = 0, sbc_sinks = 0;
1492 int mpeg12_srcs = 0, mpeg12_sinks = 0;
1493 gboolean source = TRUE, sink = FALSE, socket = FALSE;
1494 gboolean delay_reporting = FALSE;
1498 struct a2dp_server *server;
1503 str = g_key_file_get_string(config, "General", "Enable", &err);
1506 DBG("audio.conf: %s", err->message);
1507 g_clear_error(&err);
1509 if (strstr(str, "Sink"))
1511 if (strstr(str, "Source"))
1513 if (strstr(str, "Socket"))
1518 str = g_key_file_get_string(config, "General", "Disable", &err);
1521 DBG("audio.conf: %s", err->message);
1522 g_clear_error(&err);
1524 if (strstr(str, "Sink"))
1526 if (strstr(str, "Source"))
1528 if (strstr(str, "Socket"))
1533 /* Don't register any local sep if Socket is disabled */
1534 if (socket == FALSE)
1537 str = g_key_file_get_string(config, "A2DP", "SBCSources", &err);
1539 DBG("audio.conf: %s", err->message);
1540 g_clear_error(&err);
1543 sbc_srcs = atoi(str);
1547 str = g_key_file_get_string(config, "A2DP", "MPEG12Sources", &err);
1549 DBG("audio.conf: %s", err->message);
1550 g_clear_error(&err);
1552 mpeg12_srcs = atoi(str);
1556 str = g_key_file_get_string(config, "A2DP", "SBCSinks", &err);
1558 DBG("audio.conf: %s", err->message);
1559 g_clear_error(&err);
1562 sbc_sinks = atoi(str);
1566 str = g_key_file_get_string(config, "A2DP", "MPEG12Sinks", &err);
1568 DBG("audio.conf: %s", err->message);
1569 g_clear_error(&err);
1571 mpeg12_sinks = atoi(str);
1577 connection = dbus_connection_ref(conn);
1579 server = find_server(servers, src);
1583 server = g_new0(struct a2dp_server, 1);
1587 av_err = avdtp_init(src, config, &server->version);
1593 bacpy(&server->src, src);
1594 servers = g_slist_append(servers, server);
1598 delay_reporting = g_key_file_get_boolean(config, "A2DP",
1599 "DelayReporting", NULL);
1601 if (delay_reporting)
1602 server->version = 0x0103;
1604 server->version = 0x0102;
1606 server->source_enabled = source;
1608 for (i = 0; i < sbc_srcs; i++)
1609 a2dp_add_sep(src, AVDTP_SEP_TYPE_SOURCE,
1610 A2DP_CODEC_SBC, delay_reporting,
1611 NULL, NULL, NULL, NULL);
1613 for (i = 0; i < mpeg12_srcs; i++)
1614 a2dp_add_sep(src, AVDTP_SEP_TYPE_SOURCE,
1615 A2DP_CODEC_MPEG12, delay_reporting,
1616 NULL, NULL, NULL, NULL);
1618 server->sink_enabled = sink;
1620 for (i = 0; i < sbc_sinks; i++)
1621 a2dp_add_sep(src, AVDTP_SEP_TYPE_SINK,
1622 A2DP_CODEC_SBC, delay_reporting,
1623 NULL, NULL, NULL, NULL);
1625 for (i = 0; i < mpeg12_sinks; i++)
1626 a2dp_add_sep(src, AVDTP_SEP_TYPE_SINK,
1627 A2DP_CODEC_MPEG12, delay_reporting,
1628 NULL, NULL, NULL, NULL);
1634 static void a2dp_unregister_sep(struct a2dp_sep *sep)
1637 sep->destroy(sep->user_data);
1638 sep->endpoint = NULL;
1641 avdtp_unregister_sep(sep->lsep);
1645 void a2dp_unregister(const bdaddr_t *src)
1647 struct a2dp_server *server;
1649 server = find_server(servers, src);
1653 g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
1654 g_slist_free_full(server->sources,
1655 (GDestroyNotify) a2dp_unregister_sep);
1659 servers = g_slist_remove(servers, server);
1661 if (server->source_record_id)
1662 remove_record_from_server(server->source_record_id);
1664 if (server->sink_record_id)
1665 remove_record_from_server(server->sink_record_id);
1672 dbus_connection_unref(connection);
1676 struct a2dp_sep *a2dp_add_sep(const bdaddr_t *src, uint8_t type,
1677 uint8_t codec, gboolean delay_reporting,
1678 struct a2dp_endpoint *endpoint,
1679 void *user_data, GDestroyNotify destroy,
1682 struct a2dp_server *server;
1683 struct a2dp_sep *sep;
1685 uint32_t *record_id;
1686 sdp_record_t *record;
1687 struct avdtp_sep_ind *ind;
1689 server = find_server(servers, src);
1690 if (server == NULL) {
1692 *err = -EPROTONOSUPPORT;
1696 if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
1698 *err = -EPROTONOSUPPORT;
1702 if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
1704 *err = -EPROTONOSUPPORT;
1708 sep = g_new0(struct a2dp_sep, 1);
1711 ind = &endpoint_ind;
1715 ind = (codec == A2DP_CODEC_MPEG12) ? &mpeg_ind : &sbc_ind;
1718 sep->lsep = avdtp_register_sep(&server->src, type,
1719 AVDTP_MEDIA_TYPE_AUDIO, codec,
1720 delay_reporting, ind, &cfm, sep);
1721 if (sep->lsep == NULL) {
1728 sep->server = server;
1729 sep->endpoint = endpoint;
1732 sep->delay_reporting = delay_reporting;
1733 sep->user_data = user_data;
1734 sep->destroy = destroy;
1736 if (type == AVDTP_SEP_TYPE_SOURCE) {
1737 l = &server->sources;
1738 record_id = &server->source_record_id;
1741 record_id = &server->sink_record_id;
1744 if (*record_id != 0)
1747 record = a2dp_record(type, server->version);
1749 error("Unable to allocate new service record");
1750 avdtp_unregister_sep(sep->lsep);
1757 if (add_record_to_server(&server->src, record) < 0) {
1758 error("Unable to register A2DP service record");\
1759 sdp_record_free(record);
1760 avdtp_unregister_sep(sep->lsep);
1766 *record_id = record->handle;
1769 *l = g_slist_append(*l, sep);
1776 void a2dp_remove_sep(struct a2dp_sep *sep)
1778 struct a2dp_server *server = sep->server;
1780 if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
1781 if (g_slist_find(server->sources, sep) == NULL)
1783 server->sources = g_slist_remove(server->sources, sep);
1784 if (server->sources == NULL && server->source_record_id) {
1785 remove_record_from_server(server->source_record_id);
1786 server->source_record_id = 0;
1789 if (g_slist_find(server->sinks, sep) == NULL)
1791 server->sinks = g_slist_remove(server->sinks, sep);
1792 if (server->sinks == NULL && server->sink_record_id) {
1793 remove_record_from_server(server->sink_record_id);
1794 server->sink_record_id = 0;
1801 a2dp_unregister_sep(sep);
1804 struct a2dp_sep *a2dp_get(struct avdtp *session,
1805 struct avdtp_remote_sep *rsep)
1808 struct a2dp_server *server;
1809 struct avdtp_service_capability *cap;
1810 struct avdtp_media_codec_capability *codec_cap = NULL;
1813 avdtp_get_peers(session, &src, NULL);
1814 server = find_server(servers, &src);
1818 cap = avdtp_get_codec(rsep);
1819 codec_cap = (void *) cap->data;
1821 if (avdtp_get_type(rsep) == AVDTP_SEP_TYPE_SINK)
1822 l = server->sources;
1826 for (; l != NULL; l = l->next) {
1827 struct a2dp_sep *sep = l->data;
1832 if (sep->codec != codec_cap->media_codec_type)
1835 if (!sep->stream || avdtp_has_stream(session, sep->stream))
1842 static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
1845 case SBC_SAMPLING_FREQ_16000:
1846 case SBC_SAMPLING_FREQ_32000:
1848 case SBC_SAMPLING_FREQ_44100:
1850 case SBC_CHANNEL_MODE_MONO:
1851 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1853 case SBC_CHANNEL_MODE_STEREO:
1854 case SBC_CHANNEL_MODE_JOINT_STEREO:
1857 error("Invalid channel mode %u", mode);
1860 case SBC_SAMPLING_FREQ_48000:
1862 case SBC_CHANNEL_MODE_MONO:
1863 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1865 case SBC_CHANNEL_MODE_STEREO:
1866 case SBC_CHANNEL_MODE_JOINT_STEREO:
1869 error("Invalid channel mode %u", mode);
1873 error("Invalid sampling freq %u", freq);
1878 static gboolean select_sbc_params(struct sbc_codec_cap *cap,
1879 struct sbc_codec_cap *supported)
1881 unsigned int max_bitpool, min_bitpool;
1883 memset(cap, 0, sizeof(struct sbc_codec_cap));
1885 cap->cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
1886 cap->cap.media_codec_type = A2DP_CODEC_SBC;
1888 if (supported->frequency & SBC_SAMPLING_FREQ_44100)
1889 cap->frequency = SBC_SAMPLING_FREQ_44100;
1890 else if (supported->frequency & SBC_SAMPLING_FREQ_48000)
1891 cap->frequency = SBC_SAMPLING_FREQ_48000;
1892 else if (supported->frequency & SBC_SAMPLING_FREQ_32000)
1893 cap->frequency = SBC_SAMPLING_FREQ_32000;
1894 else if (supported->frequency & SBC_SAMPLING_FREQ_16000)
1895 cap->frequency = SBC_SAMPLING_FREQ_16000;
1897 error("No supported frequencies");
1901 if (supported->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
1902 cap->channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
1903 else if (supported->channel_mode & SBC_CHANNEL_MODE_STEREO)
1904 cap->channel_mode = SBC_CHANNEL_MODE_STEREO;
1905 else if (supported->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
1906 cap->channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
1907 else if (supported->channel_mode & SBC_CHANNEL_MODE_MONO)
1908 cap->channel_mode = SBC_CHANNEL_MODE_MONO;
1910 error("No supported channel modes");
1914 if (supported->block_length & SBC_BLOCK_LENGTH_16)
1915 cap->block_length = SBC_BLOCK_LENGTH_16;
1916 else if (supported->block_length & SBC_BLOCK_LENGTH_12)
1917 cap->block_length = SBC_BLOCK_LENGTH_12;
1918 else if (supported->block_length & SBC_BLOCK_LENGTH_8)
1919 cap->block_length = SBC_BLOCK_LENGTH_8;
1920 else if (supported->block_length & SBC_BLOCK_LENGTH_4)
1921 cap->block_length = SBC_BLOCK_LENGTH_4;
1923 error("No supported block lengths");
1927 if (supported->subbands & SBC_SUBBANDS_8)
1928 cap->subbands = SBC_SUBBANDS_8;
1929 else if (supported->subbands & SBC_SUBBANDS_4)
1930 cap->subbands = SBC_SUBBANDS_4;
1932 error("No supported subbands");
1936 if (supported->allocation_method & SBC_ALLOCATION_LOUDNESS)
1937 cap->allocation_method = SBC_ALLOCATION_LOUDNESS;
1938 else if (supported->allocation_method & SBC_ALLOCATION_SNR)
1939 cap->allocation_method = SBC_ALLOCATION_SNR;
1941 min_bitpool = MAX(MIN_BITPOOL, supported->min_bitpool);
1942 max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode),
1943 supported->max_bitpool);
1945 cap->min_bitpool = min_bitpool;
1946 cap->max_bitpool = max_bitpool;
1951 static gboolean select_capabilities(struct avdtp *session,
1952 struct avdtp_remote_sep *rsep,
1955 struct avdtp_service_capability *media_transport, *media_codec;
1956 struct sbc_codec_cap sbc_cap;
1958 media_codec = avdtp_get_codec(rsep);
1962 select_sbc_params(&sbc_cap, (struct sbc_codec_cap *) media_codec->data);
1964 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1967 *caps = g_slist_append(*caps, media_transport);
1969 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
1972 *caps = g_slist_append(*caps, media_codec);
1974 if (avdtp_get_delay_reporting(rsep)) {
1975 struct avdtp_service_capability *delay_reporting;
1976 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
1978 *caps = g_slist_append(*caps, delay_reporting);
1984 static void select_cb(struct a2dp_setup *setup, void *ret, int size)
1986 struct avdtp_service_capability *media_transport, *media_codec;
1987 struct avdtp_media_codec_capability *cap;
1989 #ifdef __BT_SCMST_FEATURE__
1990 struct a2dp_sep *a2dp_sep = setup->sep;
1991 struct avdtp_service_capability *media_content_protection;
1992 struct avdtp_service_capability *cp;
1994 struct avdtp_cp_cap content_protection;
1998 media_content_protection = avdtp_get_remote_sep_protection_cap(setup->rsep);
2002 DBG("Endpoint replied an invalid configuration");
2006 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
2009 setup->caps = g_slist_append(setup->caps, media_transport);
2011 cap = g_malloc0(sizeof(*cap) + size);
2012 cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
2013 cap->media_codec_type = setup->sep->codec;
2014 memcpy(cap->data, ret, size);
2016 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
2017 sizeof(*cap) + size);
2019 setup->caps = g_slist_append(setup->caps, media_codec);
2022 #ifdef __BT_SCMST_FEATURE__
2023 cp_length = a2dp_sep->endpoint->get_contect_protection_capabilites(
2025 a2dp_sep->user_data);
2026 if (cp_length > 0) {
2027 content_protection.cp_type_lsb = cp_caps[0] & 0xFF;
2028 content_protection.cp_type_msb = cp_caps[1];
2031 if (media_content_protection && cp_length > 0 &&
2032 (memcmp(media_content_protection->data, cp_caps,
2034 cp = avdtp_service_cap_new(AVDTP_CONTENT_PROTECTION,
2035 &content_protection, cp_length);
2036 setup->caps = g_slist_append(setup->caps, cp);
2038 DBG("Select SCMS-T - TRUE");
2039 sink_set_protection(TRUE);
2041 DBG("Select SCMS-T - FALSE");
2042 sink_set_protection(FALSE);
2047 finalize_select(setup);
2050 static gboolean auto_select(gpointer data)
2052 struct a2dp_setup *setup = data;
2054 finalize_select(setup);
2059 static gboolean check_vendor_codec(struct a2dp_sep *sep, uint8_t *cap,
2062 uint8_t *capabilities;
2064 a2dp_vendor_codec_t *local_codec;
2065 a2dp_vendor_codec_t *remote_codec;
2067 if (len < sizeof(a2dp_vendor_codec_t))
2070 remote_codec = (a2dp_vendor_codec_t *) cap;
2072 if (sep->endpoint == NULL)
2075 length = sep->endpoint->get_capabilities(sep,
2076 &capabilities, sep->user_data);
2078 if (length < sizeof(a2dp_vendor_codec_t))
2081 local_codec = (a2dp_vendor_codec_t *) capabilities;
2083 if (memcmp(remote_codec->vendor_id, local_codec->vendor_id,
2084 sizeof(local_codec->vendor_id)))
2087 if (memcmp(remote_codec->codec_id, local_codec->codec_id,
2088 sizeof(local_codec->codec_id)))
2091 DBG("vendor 0x%02x%02x%02x%02x codec 0x%02x%02x",
2092 remote_codec->vendor_id[0], remote_codec->vendor_id[1],
2093 remote_codec->vendor_id[2], remote_codec->vendor_id[3],
2094 remote_codec->codec_id[0], remote_codec->codec_id[1]);
2099 static struct a2dp_sep *a2dp_find_sep(struct avdtp *session, GSList *list,
2102 for (; list; list = list->next) {
2103 struct a2dp_sep *sep = list->data;
2104 struct avdtp_remote_sep *rsep;
2105 struct avdtp_media_codec_capability *cap;
2106 struct avdtp_service_capability *service;
2108 /* Use sender's endpoint if available */
2112 if (sep->endpoint == NULL)
2115 name = sep->endpoint->get_name(sep, sep->user_data);
2116 if (g_strcmp0(sender, name) != 0)
2120 rsep = avdtp_find_remote_sep(session, sep->lsep);
2124 service = avdtp_get_codec(rsep);
2125 cap = (struct avdtp_media_codec_capability *) service->data;
2127 if (cap->media_codec_type != A2DP_CODEC_VENDOR)
2130 if (check_vendor_codec(sep, cap->data,
2131 service->length - sizeof(*cap)))
2138 static struct a2dp_sep *a2dp_select_sep(struct avdtp *session, uint8_t type,
2141 struct a2dp_server *server;
2142 struct a2dp_sep *sep;
2146 avdtp_get_peers(session, &src, NULL);
2147 server = find_server(servers, &src);
2151 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
2153 /* Check sender's seps first */
2154 sep = a2dp_find_sep(session, l, sender);
2158 return a2dp_find_sep(session, l, NULL);
2161 unsigned int a2dp_select_capabilities(struct avdtp *session,
2162 uint8_t type, const char *sender,
2163 a2dp_select_cb_t cb,
2166 struct a2dp_setup *setup;
2167 struct a2dp_setup_cb *cb_data;
2168 struct a2dp_sep *sep;
2169 struct avdtp_service_capability *service;
2170 struct avdtp_media_codec_capability *codec;
2173 sep = a2dp_select_sep(session, type, sender);
2175 error("Unable to select SEP");
2179 setup = a2dp_setup_get(session);
2183 cb_data = setup_cb_new(setup);
2184 cb_data->select_cb = cb;
2185 cb_data->user_data = user_data;
2188 setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
2190 if (setup->rsep == NULL) {
2191 error("Could not find remote sep");
2195 /* FIXME: Remove auto select when it is not longer possible to register
2196 endpoint in the configuration file */
2197 if (sep->endpoint == NULL) {
2198 if (!select_capabilities(session, setup->rsep,
2200 error("Unable to auto select remote SEP capabilities");
2204 g_idle_add(auto_select, setup);
2209 service = avdtp_get_codec(setup->rsep);
2210 codec = (struct avdtp_media_codec_capability *) service->data;
2212 err = sep->endpoint->select_configuration(sep, codec->data,
2213 service->length - sizeof(*codec),
2215 select_cb, sep->user_data);
2220 setup_cb_free(cb_data);
2225 unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
2226 a2dp_config_cb_t cb, GSList *caps,
2229 struct a2dp_setup_cb *cb_data;
2231 struct a2dp_server *server;
2232 struct a2dp_setup *setup;
2233 struct a2dp_sep *tmp;
2234 struct avdtp_service_capability *cap;
2235 struct avdtp_media_codec_capability *codec_cap = NULL;
2239 avdtp_get_peers(session, &src, NULL);
2240 server = find_server(servers, &src);
2244 for (l = caps; l != NULL; l = l->next) {
2247 if (cap->category != AVDTP_MEDIA_CODEC)
2250 codec_cap = (void *) cap->data;
2257 if (sep->codec != codec_cap->media_codec_type)
2260 DBG("a2dp_config: selected SEP %p", sep->lsep);
2262 setup = a2dp_setup_get(session);
2266 cb_data = setup_cb_new(setup);
2267 cb_data->config_cb = cb;
2268 cb_data->user_data = user_data;
2271 setup->stream = sep->stream;
2273 /* Copy given caps if they are different than current caps */
2274 if (setup->caps != caps) {
2275 g_slist_free_full(setup->caps, g_free);
2276 setup->caps = g_slist_copy(caps);
2279 switch (avdtp_sep_get_state(sep->lsep)) {
2280 case AVDTP_STATE_IDLE:
2281 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2282 l = server->sources;
2286 for (; l != NULL; l = l->next) {
2288 if (avdtp_has_stream(session, tmp->stream))
2293 if (a2dp_sep_get_lock(tmp))
2295 setup->reconfigure = TRUE;
2296 if (avdtp_close(session, tmp->stream, FALSE) < 0) {
2297 error("avdtp_close failed");
2303 setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
2304 if (setup->rsep == NULL) {
2305 error("No matching ACP and INT SEPs found");
2309 posix_err = avdtp_set_configuration(session, setup->rsep,
2312 if (posix_err < 0) {
2313 error("avdtp_set_configuration: %s",
2314 strerror(-posix_err));
2318 case AVDTP_STATE_OPEN:
2319 case AVDTP_STATE_STREAMING:
2320 if (avdtp_stream_has_capabilities(setup->stream, caps)) {
2321 DBG("Configuration match: resuming");
2322 cb_data->source_id = g_idle_add(finalize_config,
2324 } else if (!setup->reconfigure) {
2325 setup->reconfigure = TRUE;
2326 if (avdtp_close(session, sep->stream, FALSE) < 0) {
2327 error("avdtp_close failed");
2333 error("SEP in bad state for requesting a new stream");
2340 setup_cb_free(cb_data);
2344 unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
2345 a2dp_stream_cb_t cb, void *user_data)
2347 struct a2dp_setup_cb *cb_data;
2348 struct a2dp_setup *setup;
2350 setup = a2dp_setup_get(session);
2354 cb_data = setup_cb_new(setup);
2355 cb_data->resume_cb = cb;
2356 cb_data->user_data = user_data;
2359 setup->stream = sep->stream;
2361 switch (avdtp_sep_get_state(sep->lsep)) {
2362 case AVDTP_STATE_IDLE:
2365 case AVDTP_STATE_OPEN:
2366 if (avdtp_start(session, sep->stream) < 0) {
2367 error("avdtp_start failed");
2370 sep->starting = TRUE;
2372 case AVDTP_STATE_STREAMING:
2373 if (!sep->suspending && sep->suspend_timer) {
2374 g_source_remove(sep->suspend_timer);
2375 sep->suspend_timer = 0;
2376 avdtp_unref(sep->session);
2377 sep->session = NULL;
2379 if (sep->suspending)
2380 setup->start = TRUE;
2382 cb_data->source_id = g_idle_add(finalize_resume,
2386 error("SEP in bad state for resume");
2393 setup_cb_free(cb_data);
2397 unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
2398 a2dp_stream_cb_t cb, void *user_data)
2400 struct a2dp_setup_cb *cb_data;
2401 struct a2dp_setup *setup;
2403 setup = a2dp_setup_get(session);
2407 cb_data = setup_cb_new(setup);
2408 cb_data->suspend_cb = cb;
2409 cb_data->user_data = user_data;
2412 setup->stream = sep->stream;
2414 switch (avdtp_sep_get_state(sep->lsep)) {
2415 case AVDTP_STATE_IDLE:
2416 error("a2dp_suspend: no stream to suspend");
2419 case AVDTP_STATE_OPEN:
2420 cb_data->source_id = g_idle_add(finalize_suspend, setup);
2422 case AVDTP_STATE_STREAMING:
2423 if (avdtp_suspend(session, sep->stream) < 0) {
2424 error("avdtp_suspend failed");
2427 sep->suspending = TRUE;
2430 error("SEP in bad state for suspend");
2437 setup_cb_free(cb_data);
2441 gboolean a2dp_cancel(struct audio_device *dev, unsigned int id)
2443 struct a2dp_setup *setup;
2446 setup = find_setup_by_dev(dev);
2450 for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
2451 struct a2dp_setup_cb *cb = l->data;
2460 DBG("aborting setup %p", setup);
2461 avdtp_abort(setup->session, setup->stream);
2472 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
2477 DBG("SEP %p locked", sep->lsep);
2483 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
2485 struct a2dp_server *server = sep->server;
2486 avdtp_state_t state;
2489 state = avdtp_sep_get_state(sep->lsep);
2491 sep->locked = FALSE;
2493 DBG("SEP %p unlocked", sep->lsep);
2495 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2496 l = server->sources;
2500 /* Unregister sep if it was removed */
2501 if (g_slist_find(l, sep) == NULL) {
2502 a2dp_unregister_sep(sep);
2506 if (!sep->stream || state == AVDTP_STATE_IDLE)
2510 case AVDTP_STATE_OPEN:
2511 /* Set timer here */
2513 case AVDTP_STATE_STREAMING:
2514 if (avdtp_suspend(session, sep->stream) == 0)
2515 sep->suspending = TRUE;
2524 gboolean a2dp_sep_get_lock(struct a2dp_sep *sep)
2529 static int stream_cmp(gconstpointer data, gconstpointer user_data)
2531 const struct a2dp_sep *sep = data;
2532 const struct avdtp_stream *stream = user_data;
2534 return (sep->stream != stream);
2537 struct a2dp_sep *a2dp_get_sep(struct avdtp *session,
2538 struct avdtp_stream *stream)
2540 struct a2dp_server *server;
2544 avdtp_get_peers(session, &src, &dst);
2546 for (l = servers; l; l = l->next) {
2549 if (bacmp(&src, &server->src) == 0)
2556 l = g_slist_find_custom(server->sources, stream, stream_cmp);
2560 l = g_slist_find_custom(server->sinks, stream, stream_cmp);
2567 struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)