• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2007  Nokia Corporation
6  *  Copyright (C) 2004-2008  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <stdint.h>
30 #include <errno.h>
31 
32 #include <bluetooth/bluetooth.h>
33 
34 #include <glib.h>
35 #include <dbus/dbus.h>
36 #include <gdbus.h>
37 
38 #include "logging.h"
39 
40 #include "avdtp.h"
41 #include "device.h"
42 #include "a2dp.h"
43 #include "error.h"
44 #include "sink.h"
45 
46 #define STREAM_SETUP_RETRY_TIMER 2000
47 
48 struct pending_request {
49 	DBusConnection *conn;
50 	DBusMessage *msg;
51 	unsigned int id;
52 };
53 
54 struct sink {
55 	struct avdtp *session;
56 	struct avdtp_stream *stream;
57 	unsigned int cb_id;
58 	uint8_t state;
59 	struct pending_request *connect;
60 	struct pending_request *disconnect;
61 	DBusConnection *conn;
62 };
63 
pending_request_free(struct pending_request * pending)64 static void pending_request_free(struct pending_request *pending)
65 {
66 	if (pending->conn)
67 		dbus_connection_unref(pending->conn);
68 	if (pending->msg)
69 		dbus_message_unref(pending->msg);
70 	g_free(pending);
71 }
72 
stream_state_changed(struct avdtp_stream * stream,avdtp_state_t old_state,avdtp_state_t new_state,struct avdtp_error * err,void * user_data)73 static void stream_state_changed(struct avdtp_stream *stream,
74 					avdtp_state_t old_state,
75 					avdtp_state_t new_state,
76 					struct avdtp_error *err,
77 					void *user_data)
78 {
79 	struct audio_device *dev = user_data;
80 	struct sink *sink = dev->sink;
81 
82 	if (err)
83 		return;
84 
85 	switch (new_state) {
86 	case AVDTP_STATE_IDLE:
87 		g_dbus_emit_signal(dev->conn, dev->path,
88 						AUDIO_SINK_INTERFACE,
89 						"Disconnected",
90 						DBUS_TYPE_INVALID);
91 		if (sink->disconnect) {
92 			DBusMessage *reply;
93 			struct pending_request *p;
94 
95 			p = sink->disconnect;
96 			sink->disconnect = NULL;
97 
98 			reply = dbus_message_new_method_return(p->msg);
99 			dbus_connection_send(p->conn, reply, NULL);
100 			dbus_message_unref(reply);
101 			pending_request_free(p);
102 		}
103 
104 		if (sink->session) {
105 			avdtp_unref(sink->session);
106 			sink->session = NULL;
107 		}
108 		sink->stream = NULL;
109 		sink->cb_id = 0;
110 		break;
111 	case AVDTP_STATE_OPEN:
112 		if (old_state == AVDTP_STATE_CONFIGURED)
113 			g_dbus_emit_signal(dev->conn, dev->path,
114 							AUDIO_SINK_INTERFACE,
115 							"Connected",
116 							DBUS_TYPE_INVALID);
117 		else if (old_state == AVDTP_STATE_STREAMING)
118 			g_dbus_emit_signal(dev->conn, dev->path,
119 							AUDIO_SINK_INTERFACE,
120 							"Stopped",
121 							DBUS_TYPE_INVALID);
122 		break;
123 	case AVDTP_STATE_STREAMING:
124 		g_dbus_emit_signal(dev->conn, dev->path,
125 						AUDIO_SINK_INTERFACE,
126 						"Playing",
127 						DBUS_TYPE_INVALID);
128 		break;
129 	case AVDTP_STATE_CONFIGURED:
130 	case AVDTP_STATE_CLOSING:
131 	case AVDTP_STATE_ABORTING:
132 	default:
133 		break;
134 	}
135 
136 	sink->state = new_state;
137 }
138 
stream_setup_retry(gpointer user_data)139 static gboolean stream_setup_retry(gpointer user_data)
140 {
141 	struct sink *sink = user_data;
142 	struct pending_request *pending = sink->connect;
143 
144 	if (sink->state >= AVDTP_STATE_OPEN) {
145 		debug("Stream successfully created, after XCASE connect:connect");
146 		if (pending->msg) {
147 			DBusMessage *reply;
148 			reply = dbus_message_new_method_return(pending->msg);
149 			dbus_connection_send(pending->conn, reply, NULL);
150 			dbus_message_unref(reply);
151 		}
152 	} else {
153 		debug("Stream setup failed, after XCASE connect:connect");
154 		if (pending->msg)
155 			error_failed(pending->conn, pending->msg, "Stream setup failed");
156 	}
157 
158 	sink->connect = NULL;
159 	pending_request_free(pending);
160 
161 	return FALSE;
162 }
163 
stream_setup_complete(struct avdtp * session,struct a2dp_sep * sep,struct avdtp_stream * stream,struct avdtp_error * err,void * user_data)164 static void stream_setup_complete(struct avdtp *session, struct a2dp_sep *sep,
165 					struct avdtp_stream *stream,
166 					struct avdtp_error *err, void *user_data)
167 {
168 	struct sink *sink = user_data;
169 	struct pending_request *pending;
170 
171 	pending = sink->connect;
172 
173 	if (stream && !err) {
174 		debug("Stream successfully created");
175 
176 		if (pending->msg) {
177 			DBusMessage *reply;
178 			reply = dbus_message_new_method_return(pending->msg);
179 			dbus_connection_send(pending->conn, reply, NULL);
180 			dbus_message_unref(reply);
181 		}
182 		sink->connect = NULL;
183 		pending_request_free(pending);
184 		return;
185 	}
186 
187 	avdtp_unref(sink->session);
188 	sink->session = NULL;
189 	if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
190 			&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
191 		debug("connect:connect XCASE detected");
192 		g_timeout_add(STREAM_SETUP_RETRY_TIMER,
193 				stream_setup_retry, sink);
194 	} else {
195 		if (pending->msg)
196 			error_failed(pending->conn, pending->msg, "Stream setup failed");
197 		sink->connect = NULL;
198 		pending_request_free(pending);
199 		debug("Stream setup failed : %s", avdtp_strerror(err));
200 	}
201 }
202 
default_bitpool(uint8_t freq,uint8_t mode)203 static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
204 {
205 	switch (freq) {
206 	case SBC_SAMPLING_FREQ_16000:
207 	case SBC_SAMPLING_FREQ_32000:
208 		return 53;
209 	case SBC_SAMPLING_FREQ_44100:
210 		switch (mode) {
211 		case SBC_CHANNEL_MODE_MONO:
212 		case SBC_CHANNEL_MODE_DUAL_CHANNEL:
213 			return 31;
214 		case SBC_CHANNEL_MODE_STEREO:
215 		case SBC_CHANNEL_MODE_JOINT_STEREO:
216 			return 53;
217 		default:
218 			error("Invalid channel mode %u", mode);
219 			return 53;
220 		}
221 	case SBC_SAMPLING_FREQ_48000:
222 		switch (mode) {
223 		case SBC_CHANNEL_MODE_MONO:
224 		case SBC_CHANNEL_MODE_DUAL_CHANNEL:
225 			return 29;
226 		case SBC_CHANNEL_MODE_STEREO:
227 		case SBC_CHANNEL_MODE_JOINT_STEREO:
228 			return 51;
229 		default:
230 			error("Invalid channel mode %u", mode);
231 			return 51;
232 		}
233 	default:
234 		error("Invalid sampling freq %u", freq);
235 		return 53;
236 	}
237 }
238 
select_sbc_params(struct sbc_codec_cap * cap,struct sbc_codec_cap * supported)239 static gboolean select_sbc_params(struct sbc_codec_cap *cap,
240 					struct sbc_codec_cap *supported)
241 {
242 	unsigned int max_bitpool, min_bitpool;
243 
244 	memset(cap, 0, sizeof(struct sbc_codec_cap));
245 
246 	cap->cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
247 	cap->cap.media_codec_type = A2DP_CODEC_SBC;
248 
249 	if (supported->frequency & SBC_SAMPLING_FREQ_44100)
250 		cap->frequency = SBC_SAMPLING_FREQ_44100;
251 	else if (supported->frequency & SBC_SAMPLING_FREQ_48000)
252 		cap->frequency = SBC_SAMPLING_FREQ_48000;
253 	else if (supported->frequency & SBC_SAMPLING_FREQ_32000)
254 		cap->frequency = SBC_SAMPLING_FREQ_32000;
255 	else if (supported->frequency & SBC_SAMPLING_FREQ_16000)
256 		cap->frequency = SBC_SAMPLING_FREQ_16000;
257 	else {
258 		error("No supported frequencies");
259 		return FALSE;
260 	}
261 
262 	if (supported->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
263 		cap->channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
264 	else if (supported->channel_mode & SBC_CHANNEL_MODE_STEREO)
265 		cap->channel_mode = SBC_CHANNEL_MODE_STEREO;
266 	else if (supported->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
267 		cap->channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
268 	else if (supported->channel_mode & SBC_CHANNEL_MODE_MONO)
269 		cap->channel_mode = SBC_CHANNEL_MODE_MONO;
270 	else {
271 		error("No supported channel modes");
272 		return FALSE;
273 	}
274 
275 	if (supported->block_length & SBC_BLOCK_LENGTH_16)
276 		cap->block_length = SBC_BLOCK_LENGTH_16;
277 	else if (supported->block_length & SBC_BLOCK_LENGTH_12)
278 		cap->block_length = SBC_BLOCK_LENGTH_12;
279 	else if (supported->block_length & SBC_BLOCK_LENGTH_8)
280 		cap->block_length = SBC_BLOCK_LENGTH_8;
281 	else if (supported->block_length & SBC_BLOCK_LENGTH_4)
282 		cap->block_length = SBC_BLOCK_LENGTH_4;
283 	else {
284 		error("No supported block lengths");
285 		return FALSE;
286 	}
287 
288 	if (supported->subbands & SBC_SUBBANDS_8)
289 		cap->subbands = SBC_SUBBANDS_8;
290 	else if (supported->subbands & SBC_SUBBANDS_4)
291 		cap->subbands = SBC_SUBBANDS_4;
292 	else {
293 		error("No supported subbands");
294 		return FALSE;
295 	}
296 
297 	if (supported->allocation_method & SBC_ALLOCATION_LOUDNESS)
298 		cap->allocation_method = SBC_ALLOCATION_LOUDNESS;
299 	else if (supported->allocation_method & SBC_ALLOCATION_SNR)
300 		cap->allocation_method = SBC_ALLOCATION_SNR;
301 
302 	min_bitpool = MAX(MIN_BITPOOL, supported->min_bitpool);
303 	max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode),
304 							supported->max_bitpool);
305 
306 	cap->min_bitpool = min_bitpool;
307 	cap->max_bitpool = max_bitpool;
308 
309 	return TRUE;
310 }
311 
select_capabilities(struct avdtp * session,struct avdtp_remote_sep * rsep,GSList ** caps)312 static gboolean select_capabilities(struct avdtp *session,
313 					struct avdtp_remote_sep *rsep,
314 					GSList **caps)
315 {
316 	struct avdtp_service_capability *media_transport, *media_codec;
317 	struct sbc_codec_cap sbc_cap;
318 
319 	media_codec = avdtp_get_codec(rsep);
320 	if (!media_codec)
321 		return FALSE;
322 
323 	select_sbc_params(&sbc_cap, (struct sbc_codec_cap *) media_codec->data);
324 
325 	media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
326 						NULL, 0);
327 
328 	*caps = g_slist_append(*caps, media_transport);
329 
330 	media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
331 						sizeof(sbc_cap));
332 
333 	*caps = g_slist_append(*caps, media_codec);
334 
335 
336 	return TRUE;
337 }
338 
discovery_complete(struct avdtp * session,GSList * seps,struct avdtp_error * err,void * user_data)339 static void discovery_complete(struct avdtp *session, GSList *seps, struct avdtp_error *err,
340 				void *user_data)
341 {
342 	struct sink *sink = user_data;
343 	struct pending_request *pending;
344 	struct avdtp_local_sep *lsep;
345 	struct avdtp_remote_sep *rsep;
346 	GSList *caps = NULL;
347 	int id;
348 
349 	pending = sink->connect;
350 
351 	if (err) {
352 		avdtp_unref(sink->session);
353 		sink->session = NULL;
354 		if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
355 				&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
356 			debug("connect:connect XCASE detected");
357 			g_timeout_add(STREAM_SETUP_RETRY_TIMER,
358 					stream_setup_retry, sink);
359 		} else
360 			goto failed;
361 		return;
362 	}
363 
364 	debug("Discovery complete");
365 
366 	if (avdtp_get_seps(session, AVDTP_SEP_TYPE_SINK, AVDTP_MEDIA_TYPE_AUDIO,
367 				A2DP_CODEC_SBC, &lsep, &rsep) < 0) {
368 		error("No matching ACP and INT SEPs found");
369 		goto failed;
370 	}
371 
372 	if (!select_capabilities(session, rsep, &caps)) {
373 		error("Unable to select remote SEP capabilities");
374 		goto failed;
375 	}
376 
377 	id = a2dp_source_config(sink->session, stream_setup_complete,
378 				caps, sink);
379 	if (id == 0)
380 		goto failed;
381 
382 	if (pending)
383 		pending->id = id;
384 	return;
385 
386 failed:
387 	if (pending) {
388 		if (pending->msg)
389 			error_failed(pending->conn, pending->msg, "Stream setup failed");
390 		pending_request_free(pending);
391 	}
392 	sink->connect = NULL;
393 	avdtp_unref(sink->session);
394 	sink->session = NULL;
395 }
396 
sink_setup_stream(struct sink * sink,struct avdtp * session,DBusConnection * conn,DBusMessage * msg)397 gboolean sink_setup_stream(struct sink *sink, struct avdtp *session,
398 				DBusConnection *conn, DBusMessage *msg)
399 {
400 	struct pending_request *pending;
401 
402 	if (sink->connect || sink->disconnect)
403 		return FALSE;
404 
405 	if (session && !sink->session)
406 		sink->session = avdtp_ref(session);
407 
408 	pending = g_new0(struct pending_request, 1);
409 	if (conn && msg) {
410 		pending->conn = dbus_connection_ref(conn);
411 		pending->msg = dbus_message_ref(msg);
412 	}
413 	sink->connect = pending;
414 
415 	if (avdtp_discover(sink->session, discovery_complete, sink) < 0) {
416 		pending_request_free(pending);
417 		return FALSE;
418 	}
419 
420 	return TRUE;
421 }
422 
sink_connect(DBusConnection * conn,DBusMessage * msg,void * data)423 static DBusMessage *sink_connect(DBusConnection *conn,
424 				DBusMessage *msg, void *data)
425 {
426 	struct audio_device *dev = data;
427 	struct sink *sink = dev->sink;
428 
429 	if (!sink->session)
430 		sink->session = avdtp_get(&dev->src, &dev->dst);
431 
432 	if (!sink->session)
433 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
434 						"Unable to get a session");
435 
436 	if (sink->connect || sink->disconnect)
437 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
438 						"%s", strerror(EBUSY));
439 
440 	if (sink->state >= AVDTP_STATE_OPEN)
441 		return g_dbus_create_error(msg, ERROR_INTERFACE
442 						".AlreadyConnected",
443 						"Device Already Connected");
444 
445 	if (!sink_setup_stream(sink, NULL, conn, msg))
446 		return g_dbus_create_error(msg, ERROR_INTERFACE ".FAILED",
447 						"Failed to create a stream");
448 
449 	debug("stream creation in progress");
450 
451 	return NULL;
452 }
453 
sink_disconnect(DBusConnection * conn,DBusMessage * msg,void * data)454 static DBusMessage *sink_disconnect(DBusConnection *conn,
455 					DBusMessage *msg, void *data)
456 {
457 	struct audio_device *device = data;
458 	struct sink *sink = device->sink;
459 	struct pending_request *pending;
460 	int err;
461 
462 	if (!sink->session)
463 		return g_dbus_create_error(msg, ERROR_INTERFACE
464 						".NotConnected",
465 						"Device not Connected");
466 
467 	if (sink->connect || sink->disconnect)
468 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
469 						"%s", strerror(EBUSY));
470 
471 	if (sink->state < AVDTP_STATE_OPEN) {
472 		DBusMessage *reply = dbus_message_new_method_return(msg);
473 		if (!reply)
474 			return NULL;
475 		avdtp_unref(sink->session);
476 		sink->session = NULL;
477 		return reply;
478 	}
479 
480 	err = avdtp_close(sink->session, sink->stream);
481 	if (err < 0)
482 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
483 						"%s", strerror(-err));
484 
485 	pending = g_new0(struct pending_request, 1);
486 	pending->conn = dbus_connection_ref(conn);
487 	pending->msg = dbus_message_ref(msg);
488 	sink->disconnect = pending;
489 
490 	return NULL;
491 }
492 
sink_is_connected(DBusConnection * conn,DBusMessage * msg,void * data)493 static DBusMessage *sink_is_connected(DBusConnection *conn,
494 					DBusMessage *msg,
495 					void *data)
496 {
497 	struct audio_device *device = data;
498 	struct sink *sink = device->sink;
499 	DBusMessage *reply;
500 	dbus_bool_t connected;
501 
502 	reply = dbus_message_new_method_return(msg);
503 	if (!reply)
504 		return NULL;
505 
506 	connected = (sink->state >= AVDTP_STATE_CONFIGURED);
507 
508 	dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &connected,
509 					DBUS_TYPE_INVALID);
510 
511 	return reply;
512 }
513 
514 static GDBusMethodTable sink_methods[] = {
515 	{ "Connect",		"",	"",	sink_connect,
516 						G_DBUS_METHOD_FLAG_ASYNC },
517 	{ "Disconnect",		"",	"",	sink_disconnect,
518 						G_DBUS_METHOD_FLAG_ASYNC },
519 	{ "IsConnected",	"",	"b",	sink_is_connected },
520 	{ NULL, NULL, NULL, NULL }
521 };
522 
523 static GDBusSignalTable sink_signals[] = {
524 	{ "Connected",			""	},
525 	{ "Disconnected",		""	},
526 	{ "Playing",			""	},
527 	{ "Stopped",			""	},
528 	{ NULL, NULL }
529 };
530 
sink_init(struct audio_device * dev)531 struct sink *sink_init(struct audio_device *dev)
532 {
533 	if (!g_dbus_register_interface(dev->conn, dev->path,
534 					AUDIO_SINK_INTERFACE,
535 					sink_methods, sink_signals, NULL,
536 					dev, NULL))
537 		return NULL;
538 
539 	return g_new0(struct sink, 1);
540 }
541 
sink_free(struct audio_device * dev)542 void sink_free(struct audio_device *dev)
543 {
544 	struct sink *sink = dev->sink;
545 
546 	if (sink->cb_id)
547 		avdtp_stream_remove_cb(sink->session, sink->stream,
548 					sink->cb_id);
549 
550 	if (sink->session)
551 		avdtp_unref(sink->session);
552 
553 	if (sink->connect)
554 		pending_request_free(sink->connect);
555 
556 	if (sink->disconnect)
557 		pending_request_free(sink->disconnect);
558 
559 	g_free(sink);
560 	dev->sink = NULL;
561 }
562 
sink_is_active(struct audio_device * dev)563 gboolean sink_is_active(struct audio_device *dev)
564 {
565 	struct sink *sink = dev->sink;
566 
567 	if (sink->session)
568 		return TRUE;
569 
570 	return FALSE;
571 }
572 
sink_get_state(struct audio_device * dev)573 avdtp_state_t sink_get_state(struct audio_device *dev)
574 {
575 	struct sink *sink = dev->sink;
576 
577 	return sink->state;
578 }
579 
sink_new_stream(struct audio_device * dev,struct avdtp * session,struct avdtp_stream * stream)580 gboolean sink_new_stream(struct audio_device *dev, struct avdtp *session,
581 				struct avdtp_stream *stream)
582 {
583 	struct sink *sink = dev->sink;
584 
585 	if (sink->stream)
586 		return FALSE;
587 
588 	if (!sink->session)
589 		sink->session = avdtp_ref(session);
590 
591 	sink->stream = stream;
592 
593 	sink->cb_id = avdtp_stream_add_cb(session, stream,
594 						stream_state_changed, dev);
595 
596 	return TRUE;
597 }
598