Home | History | Annotate | Download | only in audio
      1 /*
      2  *
      3  *  BlueZ - Bluetooth protocol stack for Linux
      4  *
      5  *  Copyright (C) 2006-2007  Nokia Corporation
      6  *  Copyright (C) 2004-2009  Marcel Holtmann <marcel (at) holtmann.org>
      7  *  Copyright (C) 2009-2010  Motorola Inc.
      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 #include <bluetooth/sdp.h>
     34 
     35 #include <glib.h>
     36 #include <dbus/dbus.h>
     37 #include <gdbus.h>
     38 
     39 #include "logging.h"
     40 
     41 #include "device.h"
     42 #include "avdtp.h"
     43 #include "a2dp.h"
     44 #include "error.h"
     45 #include "sink.h"
     46 #include "dbus-common.h"
     47 #include "../src/adapter.h"
     48 #include "../src/device.h"
     49 
     50 #define STREAM_SETUP_RETRY_TIMER 2
     51 
     52 struct pending_request {
     53 	DBusConnection *conn;
     54 	DBusMessage *msg;
     55 	unsigned int id;
     56 };
     57 
     58 struct sink {
     59 	struct audio_device *dev;
     60 	struct avdtp *session;
     61 	struct avdtp_stream *stream;
     62 	unsigned int cb_id;
     63 	guint dc_id;
     64 	guint retry_id;
     65 	avdtp_session_state_t session_state;
     66 	avdtp_state_t stream_state;
     67 	sink_state_t state;
     68 	struct pending_request *connect;
     69 	struct pending_request *disconnect;
     70 	DBusConnection *conn;
     71 };
     72 
     73 struct sink_state_callback {
     74 	sink_state_cb cb;
     75 	void *user_data;
     76 	unsigned int id;
     77 };
     78 
     79 static GSList *sink_callbacks = NULL;
     80 
     81 static unsigned int avdtp_callback_id = 0;
     82 
     83 static const char *state2str(sink_state_t state)
     84 {
     85 	switch (state) {
     86 	case SINK_STATE_DISCONNECTED:
     87 		return "disconnected";
     88 	case SINK_STATE_CONNECTING:
     89 		return "connecting";
     90 	case SINK_STATE_CONNECTED:
     91 		return "connected";
     92 	case SINK_STATE_PLAYING:
     93 		return "playing";
     94 	default:
     95 		error("Invalid sink state %d", state);
     96 		return NULL;
     97 	}
     98 }
     99 
    100 static void sink_set_state(struct audio_device *dev, sink_state_t new_state)
    101 {
    102 	struct sink *sink = dev->sink;
    103 	const char *state_str;
    104 	sink_state_t old_state = sink->state;
    105 	GSList *l;
    106 
    107 	sink->state = new_state;
    108 
    109 	state_str = state2str(new_state);
    110 	if (state_str)
    111 		emit_property_changed(dev->conn, dev->path,
    112 					AUDIO_SINK_INTERFACE, "State",
    113 					DBUS_TYPE_STRING, &state_str);
    114 
    115 	for (l = sink_callbacks; l != NULL; l = l->next) {
    116 		struct sink_state_callback *cb = l->data;
    117 		cb->cb(dev, old_state, new_state, cb->user_data);
    118 	}
    119 }
    120 
    121 static void avdtp_state_callback(struct audio_device *dev,
    122 					struct avdtp *session,
    123 					avdtp_session_state_t old_state,
    124 					avdtp_session_state_t new_state,
    125 					void *user_data)
    126 {
    127 	struct sink *sink = dev->sink;
    128 
    129 	if (sink == NULL)
    130 		return;
    131 
    132 	switch (new_state) {
    133 	case AVDTP_SESSION_STATE_DISCONNECTED:
    134 		if (sink->state != SINK_STATE_CONNECTING) {
    135 			gboolean value = FALSE;
    136 			g_dbus_emit_signal(dev->conn, dev->path,
    137 					AUDIO_SINK_INTERFACE, "Disconnected",
    138 					DBUS_TYPE_INVALID);
    139 			emit_property_changed(dev->conn, dev->path,
    140 					AUDIO_SINK_INTERFACE, "Connected",
    141 					DBUS_TYPE_BOOLEAN, &value);
    142 			if (sink->dc_id) {
    143 				device_remove_disconnect_watch(dev->btd_dev,
    144 								sink->dc_id);
    145 				sink->dc_id = 0;
    146 			}
    147 		}
    148 		sink_set_state(dev, SINK_STATE_DISCONNECTED);
    149 		break;
    150 	case AVDTP_SESSION_STATE_CONNECTING:
    151 		sink_set_state(dev, SINK_STATE_CONNECTING);
    152 		break;
    153 	case AVDTP_SESSION_STATE_CONNECTED:
    154 		break;
    155 	}
    156 
    157 	sink->session_state = new_state;
    158 }
    159 
    160 static void pending_request_free(struct audio_device *dev,
    161 					struct pending_request *pending)
    162 {
    163 	if (pending->conn)
    164 		dbus_connection_unref(pending->conn);
    165 	if (pending->msg)
    166 		dbus_message_unref(pending->msg);
    167 	if (pending->id)
    168 		a2dp_cancel(dev, pending->id);
    169 
    170 	g_free(pending);
    171 }
    172 
    173 static void disconnect_cb(struct btd_device *btd_dev, gboolean removal,
    174 				void *user_data)
    175 {
    176 	struct audio_device *device = user_data;
    177 	struct sink *sink = device->sink;
    178 
    179 	debug("Sink: disconnect %s", device->path);
    180 
    181 	avdtp_close(sink->session, sink->stream);
    182 }
    183 
    184 static void stream_state_changed(struct avdtp_stream *stream,
    185 					avdtp_state_t old_state,
    186 					avdtp_state_t new_state,
    187 					struct avdtp_error *err,
    188 					void *user_data)
    189 {
    190 	struct audio_device *dev = user_data;
    191 	struct sink *sink = dev->sink;
    192 	gboolean value;
    193 
    194 	if (err)
    195 		return;
    196 
    197 	switch (new_state) {
    198 	case AVDTP_STATE_IDLE:
    199 		if (sink->disconnect) {
    200 			DBusMessage *reply;
    201 			struct pending_request *p;
    202 
    203 			p = sink->disconnect;
    204 			sink->disconnect = NULL;
    205 
    206 			reply = dbus_message_new_method_return(p->msg);
    207 			g_dbus_send_message(p->conn, reply);
    208 			pending_request_free(dev, p);
    209 		}
    210 
    211 		if (sink->dc_id) {
    212 			device_remove_disconnect_watch(dev->btd_dev,
    213 							sink->dc_id);
    214 			sink->dc_id = 0;
    215 		}
    216 
    217 		if (sink->session) {
    218 			avdtp_unref(sink->session);
    219 			sink->session = NULL;
    220 		}
    221 		sink->stream = NULL;
    222 		sink->cb_id = 0;
    223 		break;
    224 	case AVDTP_STATE_OPEN:
    225 		if (old_state == AVDTP_STATE_CONFIGURED &&
    226 				sink->state == SINK_STATE_CONNECTING) {
    227 			value = TRUE;
    228 			g_dbus_emit_signal(dev->conn, dev->path,
    229 						AUDIO_SINK_INTERFACE,
    230 						"Connected",
    231 						DBUS_TYPE_INVALID);
    232 			emit_property_changed(dev->conn, dev->path,
    233 						AUDIO_SINK_INTERFACE,
    234 						"Connected",
    235 						DBUS_TYPE_BOOLEAN, &value);
    236 			sink->dc_id = device_add_disconnect_watch(dev->btd_dev,
    237 								disconnect_cb,
    238 								dev, NULL);
    239 		} else if (old_state == AVDTP_STATE_STREAMING) {
    240 			value = FALSE;
    241 			g_dbus_emit_signal(dev->conn, dev->path,
    242 						AUDIO_SINK_INTERFACE,
    243 						"Stopped",
    244 						DBUS_TYPE_INVALID);
    245 			emit_property_changed(dev->conn, dev->path,
    246 						AUDIO_SINK_INTERFACE,
    247 						"Playing",
    248 						DBUS_TYPE_BOOLEAN, &value);
    249 		}
    250 		sink_set_state(dev, SINK_STATE_CONNECTED);
    251 		break;
    252 	case AVDTP_STATE_STREAMING:
    253 		value = TRUE;
    254 		g_dbus_emit_signal(dev->conn, dev->path, AUDIO_SINK_INTERFACE,
    255 					"Playing", DBUS_TYPE_INVALID);
    256 		emit_property_changed(dev->conn, dev->path,
    257 					AUDIO_SINK_INTERFACE, "Playing",
    258 					DBUS_TYPE_BOOLEAN, &value);
    259 		sink_set_state(dev, SINK_STATE_PLAYING);
    260 		break;
    261 	case AVDTP_STATE_CONFIGURED:
    262 	case AVDTP_STATE_CLOSING:
    263 	case AVDTP_STATE_ABORTING:
    264 	default:
    265 		break;
    266 	}
    267 
    268 	sink->stream_state = new_state;
    269 }
    270 
    271 static DBusHandlerResult error_failed(DBusConnection *conn,
    272 					DBusMessage *msg, const char * desc)
    273 {
    274 	return error_common_reply(conn, msg, ERROR_INTERFACE ".Failed", desc);
    275 }
    276 
    277 static gboolean stream_setup_retry(gpointer user_data)
    278 {
    279 	struct sink *sink = user_data;
    280 	struct pending_request *pending = sink->connect;
    281 
    282 	sink->retry_id = 0;
    283 
    284 	if (sink->stream_state >= AVDTP_STATE_OPEN) {
    285 		debug("Stream successfully created, after XCASE connect:connect");
    286 		if (pending->msg) {
    287 			DBusMessage *reply;
    288 			reply = dbus_message_new_method_return(pending->msg);
    289 			g_dbus_send_message(pending->conn, reply);
    290 		}
    291 	} else {
    292 		debug("Stream setup failed, after XCASE connect:connect");
    293 		if (pending->msg)
    294 			error_failed(pending->conn, pending->msg, "Stream setup failed");
    295 	}
    296 
    297 	sink->connect = NULL;
    298 	pending_request_free(sink->dev, pending);
    299 
    300 	return FALSE;
    301 }
    302 
    303 static void stream_setup_complete(struct avdtp *session, struct a2dp_sep *sep,
    304 					struct avdtp_stream *stream,
    305 					struct avdtp_error *err, void *user_data)
    306 {
    307 	struct sink *sink = user_data;
    308 	struct pending_request *pending;
    309 
    310 	pending = sink->connect;
    311 
    312 	pending->id = 0;
    313 
    314 	if (stream) {
    315 		debug("Stream successfully created");
    316 
    317 		if (pending->msg) {
    318 			DBusMessage *reply;
    319 			reply = dbus_message_new_method_return(pending->msg);
    320 			g_dbus_send_message(pending->conn, reply);
    321 		}
    322 
    323 		sink->connect = NULL;
    324 		pending_request_free(sink->dev, pending);
    325 
    326 		return;
    327 	}
    328 
    329 	avdtp_unref(sink->session);
    330 	sink->session = NULL;
    331 	if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
    332 			&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
    333 		debug("connect:connect XCASE detected");
    334 		sink->retry_id = g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
    335 							stream_setup_retry,
    336 							sink);
    337 	} else {
    338 		if (pending->msg)
    339 			error_failed(pending->conn, pending->msg, "Stream setup failed");
    340 		sink->connect = NULL;
    341 		pending_request_free(sink->dev, pending);
    342 		debug("Stream setup failed : %s", avdtp_strerror(err));
    343 	}
    344 }
    345 
    346 static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
    347 {
    348 	switch (freq) {
    349 	case SBC_SAMPLING_FREQ_16000:
    350 	case SBC_SAMPLING_FREQ_32000:
    351 		return 53;
    352 	case SBC_SAMPLING_FREQ_44100:
    353 		switch (mode) {
    354 		case SBC_CHANNEL_MODE_MONO:
    355 		case SBC_CHANNEL_MODE_DUAL_CHANNEL:
    356 			return 31;
    357 		case SBC_CHANNEL_MODE_STEREO:
    358 		case SBC_CHANNEL_MODE_JOINT_STEREO:
    359 			return 53;
    360 		default:
    361 			error("Invalid channel mode %u", mode);
    362 			return 53;
    363 		}
    364 	case SBC_SAMPLING_FREQ_48000:
    365 		switch (mode) {
    366 		case SBC_CHANNEL_MODE_MONO:
    367 		case SBC_CHANNEL_MODE_DUAL_CHANNEL:
    368 			return 29;
    369 		case SBC_CHANNEL_MODE_STEREO:
    370 		case SBC_CHANNEL_MODE_JOINT_STEREO:
    371 			return 51;
    372 		default:
    373 			error("Invalid channel mode %u", mode);
    374 			return 51;
    375 		}
    376 	default:
    377 		error("Invalid sampling freq %u", freq);
    378 		return 53;
    379 	}
    380 }
    381 
    382 static gboolean select_sbc_params(struct sbc_codec_cap *cap,
    383 					struct sbc_codec_cap *supported)
    384 {
    385 	unsigned int max_bitpool, min_bitpool;
    386 
    387 	memset(cap, 0, sizeof(struct sbc_codec_cap));
    388 
    389 	cap->cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
    390 	cap->cap.media_codec_type = A2DP_CODEC_SBC;
    391 
    392 	if (supported->frequency & SBC_SAMPLING_FREQ_44100)
    393 		cap->frequency = SBC_SAMPLING_FREQ_44100;
    394 	else if (supported->frequency & SBC_SAMPLING_FREQ_48000)
    395 		cap->frequency = SBC_SAMPLING_FREQ_48000;
    396 	else if (supported->frequency & SBC_SAMPLING_FREQ_32000)
    397 		cap->frequency = SBC_SAMPLING_FREQ_32000;
    398 	else if (supported->frequency & SBC_SAMPLING_FREQ_16000)
    399 		cap->frequency = SBC_SAMPLING_FREQ_16000;
    400 	else {
    401 		error("No supported frequencies");
    402 		return FALSE;
    403 	}
    404 
    405 	if (supported->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
    406 		cap->channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
    407 	else if (supported->channel_mode & SBC_CHANNEL_MODE_STEREO)
    408 		cap->channel_mode = SBC_CHANNEL_MODE_STEREO;
    409 	else if (supported->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
    410 		cap->channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
    411 	else if (supported->channel_mode & SBC_CHANNEL_MODE_MONO)
    412 		cap->channel_mode = SBC_CHANNEL_MODE_MONO;
    413 	else {
    414 		error("No supported channel modes");
    415 		return FALSE;
    416 	}
    417 
    418 	if (supported->block_length & SBC_BLOCK_LENGTH_16)
    419 		cap->block_length = SBC_BLOCK_LENGTH_16;
    420 	else if (supported->block_length & SBC_BLOCK_LENGTH_12)
    421 		cap->block_length = SBC_BLOCK_LENGTH_12;
    422 	else if (supported->block_length & SBC_BLOCK_LENGTH_8)
    423 		cap->block_length = SBC_BLOCK_LENGTH_8;
    424 	else if (supported->block_length & SBC_BLOCK_LENGTH_4)
    425 		cap->block_length = SBC_BLOCK_LENGTH_4;
    426 	else {
    427 		error("No supported block lengths");
    428 		return FALSE;
    429 	}
    430 
    431 	if (supported->subbands & SBC_SUBBANDS_8)
    432 		cap->subbands = SBC_SUBBANDS_8;
    433 	else if (supported->subbands & SBC_SUBBANDS_4)
    434 		cap->subbands = SBC_SUBBANDS_4;
    435 	else {
    436 		error("No supported subbands");
    437 		return FALSE;
    438 	}
    439 
    440 	if (supported->allocation_method & SBC_ALLOCATION_LOUDNESS)
    441 		cap->allocation_method = SBC_ALLOCATION_LOUDNESS;
    442 	else if (supported->allocation_method & SBC_ALLOCATION_SNR)
    443 		cap->allocation_method = SBC_ALLOCATION_SNR;
    444 
    445 	min_bitpool = MAX(MIN_BITPOOL, supported->min_bitpool);
    446 	max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode),
    447 							supported->max_bitpool);
    448 
    449 	cap->min_bitpool = min_bitpool;
    450 	cap->max_bitpool = max_bitpool;
    451 
    452 	return TRUE;
    453 }
    454 
    455 static gboolean select_capabilities(struct avdtp *session,
    456 					struct avdtp_remote_sep *rsep,
    457 					GSList **caps)
    458 {
    459 	struct avdtp_service_capability *media_transport, *media_codec;
    460 	struct sbc_codec_cap sbc_cap;
    461 
    462 	media_codec = avdtp_get_codec(rsep);
    463 	if (!media_codec)
    464 		return FALSE;
    465 
    466 	select_sbc_params(&sbc_cap, (struct sbc_codec_cap *) media_codec->data);
    467 
    468 	media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
    469 						NULL, 0);
    470 
    471 	*caps = g_slist_append(*caps, media_transport);
    472 
    473 	media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
    474 						sizeof(sbc_cap));
    475 
    476 	*caps = g_slist_append(*caps, media_codec);
    477 
    478 
    479 	return TRUE;
    480 }
    481 
    482 static void discovery_complete(struct avdtp *session, GSList *seps, struct avdtp_error *err,
    483 				void *user_data)
    484 {
    485 	struct sink *sink = user_data;
    486 	struct pending_request *pending;
    487 	struct avdtp_local_sep *lsep;
    488 	struct avdtp_remote_sep *rsep;
    489 	struct a2dp_sep *sep;
    490 	GSList *caps = NULL;
    491 	int id;
    492 
    493 	pending = sink->connect;
    494 
    495 	if (err) {
    496 		avdtp_unref(sink->session);
    497 		sink->session = NULL;
    498 		if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
    499 				&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
    500 			debug("connect:connect XCASE detected");
    501 			sink->retry_id =
    502 				g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
    503 							stream_setup_retry,
    504 							sink);
    505 		} else
    506 			goto failed;
    507 		return;
    508 	}
    509 
    510 	debug("Discovery complete");
    511 
    512 	if (avdtp_get_seps(session, AVDTP_SEP_TYPE_SINK, AVDTP_MEDIA_TYPE_AUDIO,
    513 				A2DP_CODEC_SBC, &lsep, &rsep) < 0) {
    514 		error("No matching ACP and INT SEPs found");
    515 		goto failed;
    516 	}
    517 
    518 	if (!select_capabilities(session, rsep, &caps)) {
    519 		error("Unable to select remote SEP capabilities");
    520 		goto failed;
    521 	}
    522 
    523 	sep = a2dp_get(session, rsep);
    524 	if (!sep) {
    525 		error("Unable to get a local source SEP");
    526 		goto failed;
    527 	}
    528 
    529 	id = a2dp_config(sink->session, sep, stream_setup_complete, caps, sink);
    530 	if (id == 0)
    531 		goto failed;
    532 
    533 	pending->id = id;
    534 	return;
    535 
    536 failed:
    537 	if (pending->msg)
    538 		error_failed(pending->conn, pending->msg, "Stream setup failed");
    539 	pending_request_free(sink->dev, pending);
    540 	sink->connect = NULL;
    541 	avdtp_unref(sink->session);
    542 	sink->session = NULL;
    543 }
    544 
    545 gboolean sink_setup_stream(struct sink *sink, struct avdtp *session)
    546 {
    547 	if (sink->connect || sink->disconnect)
    548 		return FALSE;
    549 
    550 	if (session && !sink->session)
    551 		sink->session = avdtp_ref(session);
    552 
    553 	if (!sink->session)
    554 		return FALSE;
    555 
    556 	avdtp_set_auto_disconnect(sink->session, FALSE);
    557 
    558 	if (avdtp_discover(sink->session, discovery_complete, sink) < 0)
    559 		return FALSE;
    560 
    561 	sink->connect = g_new0(struct pending_request, 1);
    562 
    563 	return TRUE;
    564 }
    565 
    566 static DBusMessage *sink_connect(DBusConnection *conn,
    567 				DBusMessage *msg, void *data)
    568 {
    569 	struct audio_device *dev = data;
    570 	struct sink *sink = dev->sink;
    571 	struct pending_request *pending;
    572 
    573 	if (!sink->session)
    574 		sink->session = avdtp_get(&dev->src, &dev->dst);
    575 
    576 	if (!sink->session)
    577 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    578 						"Unable to get a session");
    579 
    580 	if (sink->connect || sink->disconnect)
    581 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    582 						"%s", strerror(EBUSY));
    583 
    584 	if (sink->stream_state >= AVDTP_STATE_OPEN)
    585 		return g_dbus_create_error(msg, ERROR_INTERFACE
    586 						".AlreadyConnected",
    587 						"Device Already Connected");
    588 
    589 	if (!sink_setup_stream(sink, NULL))
    590 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    591 						"Failed to create a stream");
    592 
    593 	dev->auto_connect = FALSE;
    594 
    595 	pending = sink->connect;
    596 
    597 	pending->conn = dbus_connection_ref(conn);
    598 	pending->msg = dbus_message_ref(msg);
    599 
    600 	debug("stream creation in progress");
    601 
    602 	return NULL;
    603 }
    604 
    605 static DBusMessage *sink_disconnect(DBusConnection *conn,
    606 					DBusMessage *msg, void *data)
    607 {
    608 	struct audio_device *device = data;
    609 	struct sink *sink = device->sink;
    610 	struct pending_request *pending;
    611 	int err;
    612 
    613 	if (!sink->session)
    614 		return g_dbus_create_error(msg, ERROR_INTERFACE
    615 						".NotConnected",
    616 						"Device not Connected");
    617 
    618 	if (sink->connect || sink->disconnect)
    619 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    620 						"%s", strerror(EBUSY));
    621 
    622 	if (sink->stream_state < AVDTP_STATE_OPEN) {
    623 		DBusMessage *reply = dbus_message_new_method_return(msg);
    624 		if (!reply)
    625 			return NULL;
    626 		avdtp_unref(sink->session);
    627 		sink->session = NULL;
    628 		return reply;
    629 	}
    630 
    631 	err = avdtp_close(sink->session, sink->stream);
    632 	if (err < 0)
    633 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    634 						"%s", strerror(-err));
    635 
    636 	pending = g_new0(struct pending_request, 1);
    637 	pending->conn = dbus_connection_ref(conn);
    638 	pending->msg = dbus_message_ref(msg);
    639 	sink->disconnect = pending;
    640 
    641 	return NULL;
    642 }
    643 
    644 static DBusMessage *sink_suspend(DBusConnection *conn,
    645 					DBusMessage *msg, void *data)
    646 {
    647 	struct audio_device *device = data;
    648 	struct sink *sink = device->sink;
    649 	struct pending_request *pending;
    650 	int err;
    651 
    652 	if (!sink->session)
    653 		return g_dbus_create_error(msg, ERROR_INTERFACE
    654 						".NotConnected",
    655 						"Device not Connected");
    656 
    657 	if (sink->connect || sink->disconnect)
    658 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    659 						"%s", strerror(EBUSY));
    660 
    661 	if (sink->state < AVDTP_STATE_OPEN) {
    662 		DBusMessage *reply = dbus_message_new_method_return(msg);
    663 		if (!reply)
    664 			return NULL;
    665 		avdtp_unref(sink->session);
    666 		sink->session = NULL;
    667 		return reply;
    668 	}
    669 
    670 	err = avdtp_suspend(sink->session, sink->stream);
    671 	if (err < 0)
    672 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    673 						"%s", strerror(-err));
    674 
    675 	return NULL;
    676 }
    677 
    678 static DBusMessage *sink_resume(DBusConnection *conn,
    679 					DBusMessage *msg, void *data)
    680 {
    681 	struct audio_device *device = data;
    682 	struct sink *sink = device->sink;
    683 	struct pending_request *pending;
    684 	int err;
    685 
    686 	if (!sink->session)
    687 		return g_dbus_create_error(msg, ERROR_INTERFACE
    688 						".NotConnected",
    689 						"Device not Connected");
    690 
    691 	if (sink->connect || sink->disconnect)
    692 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    693 						"%s", strerror(EBUSY));
    694 
    695 	if (sink->state < AVDTP_STATE_OPEN) {
    696 		DBusMessage *reply = dbus_message_new_method_return(msg);
    697 		if (!reply)
    698 			return NULL;
    699 		avdtp_unref(sink->session);
    700 		sink->session = NULL;
    701 		return reply;
    702 	}
    703 
    704 	err = avdtp_start(sink->session, sink->stream);
    705 	if (err < 0)
    706 		return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
    707 						"%s", strerror(-err));
    708 
    709 	return NULL;
    710 }
    711 
    712 static DBusMessage *sink_is_connected(DBusConnection *conn,
    713 					DBusMessage *msg,
    714 					void *data)
    715 {
    716 	struct audio_device *device = data;
    717 	struct sink *sink = device->sink;
    718 	DBusMessage *reply;
    719 	dbus_bool_t connected;
    720 
    721 	reply = dbus_message_new_method_return(msg);
    722 	if (!reply)
    723 		return NULL;
    724 
    725 	connected = (sink->stream_state >= AVDTP_STATE_CONFIGURED);
    726 
    727 	dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &connected,
    728 					DBUS_TYPE_INVALID);
    729 
    730 	return reply;
    731 }
    732 
    733 static DBusMessage *sink_get_properties(DBusConnection *conn,
    734 					DBusMessage *msg, void *data)
    735 {
    736 	struct audio_device *device = data;
    737 	struct sink *sink = device->sink;
    738 	DBusMessage *reply;
    739 	DBusMessageIter iter;
    740 	DBusMessageIter dict;
    741 	const char *state;
    742 	gboolean value;
    743 
    744 	reply = dbus_message_new_method_return(msg);
    745 	if (!reply)
    746 		return NULL;
    747 
    748 	dbus_message_iter_init_append(reply, &iter);
    749 
    750 	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
    751 			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
    752 			DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
    753 			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
    754 
    755 	/* Playing */
    756 	value = (sink->stream_state == AVDTP_STATE_STREAMING);
    757 	dict_append_entry(&dict, "Playing", DBUS_TYPE_BOOLEAN, &value);
    758 
    759 	/* Connected */
    760 	value = (sink->stream_state >= AVDTP_STATE_CONFIGURED);
    761 	dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &value);
    762 
    763 	/* State */
    764 	state = state2str(sink->state);
    765 	if (state)
    766 		dict_append_entry(&dict, "State", DBUS_TYPE_STRING, &state);
    767 
    768 	dbus_message_iter_close_container(&iter, &dict);
    769 
    770 	return reply;
    771 }
    772 
    773 static GDBusMethodTable sink_methods[] = {
    774 	{ "Connect",		"",	"",	sink_connect,
    775 						G_DBUS_METHOD_FLAG_ASYNC },
    776 	{ "Disconnect",		"",	"",	sink_disconnect,
    777 						G_DBUS_METHOD_FLAG_ASYNC },
    778 	{ "Suspend",        "", "", sink_suspend,
    779                         G_DBUS_METHOD_FLAG_ASYNC },
    780 	{ "Resume",         "", "", sink_resume,
    781                         G_DBUS_METHOD_FLAG_ASYNC },
    782 	{ "IsConnected",	"",	"b",	sink_is_connected,
    783 						G_DBUS_METHOD_FLAG_DEPRECATED },
    784 	{ "GetProperties",	"",	"a{sv}",sink_get_properties },
    785 	{ NULL, NULL, NULL, NULL }
    786 };
    787 
    788 static GDBusSignalTable sink_signals[] = {
    789 	{ "Connected",			"",	G_DBUS_SIGNAL_FLAG_DEPRECATED },
    790 	{ "Disconnected",		"",	G_DBUS_SIGNAL_FLAG_DEPRECATED },
    791 	{ "Playing",			"",	G_DBUS_SIGNAL_FLAG_DEPRECATED },
    792 	{ "Stopped",			"",	G_DBUS_SIGNAL_FLAG_DEPRECATED },
    793 	{ "PropertyChanged",		"sv"	},
    794 	{ NULL, NULL }
    795 };
    796 
    797 static void sink_free(struct audio_device *dev)
    798 {
    799 	struct sink *sink = dev->sink;
    800 
    801 	if (sink->cb_id)
    802 		avdtp_stream_remove_cb(sink->session, sink->stream,
    803 					sink->cb_id);
    804 
    805 	if (sink->dc_id)
    806 		device_remove_disconnect_watch(dev->btd_dev, sink->dc_id);
    807 
    808 	if (sink->session)
    809 		avdtp_unref(sink->session);
    810 
    811 	if (sink->connect)
    812 		pending_request_free(dev, sink->connect);
    813 
    814 	if (sink->disconnect)
    815 		pending_request_free(dev, sink->disconnect);
    816 
    817 	if (sink->retry_id)
    818 		g_source_remove(sink->retry_id);
    819 
    820 	g_free(sink);
    821 	dev->sink = NULL;
    822 }
    823 
    824 static void path_unregister(void *data)
    825 {
    826 	struct audio_device *dev = data;
    827 
    828 	debug("Unregistered interface %s on path %s",
    829 		AUDIO_SINK_INTERFACE, dev->path);
    830 
    831 	sink_free(dev);
    832 }
    833 
    834 void sink_unregister(struct audio_device *dev)
    835 {
    836 	g_dbus_unregister_interface(dev->conn, dev->path,
    837 		AUDIO_SINK_INTERFACE);
    838 }
    839 
    840 struct sink *sink_init(struct audio_device *dev)
    841 {
    842 	struct sink *sink;
    843 
    844 	if (!g_dbus_register_interface(dev->conn, dev->path,
    845 					AUDIO_SINK_INTERFACE,
    846 					sink_methods, sink_signals, NULL,
    847 					dev, path_unregister))
    848 		return NULL;
    849 
    850 	debug("Registered interface %s on path %s",
    851 		AUDIO_SINK_INTERFACE, dev->path);
    852 
    853 	if (avdtp_callback_id == 0)
    854 		avdtp_callback_id = avdtp_add_state_cb(avdtp_state_callback,
    855 									NULL);
    856 
    857 	sink = g_new0(struct sink, 1);
    858 
    859 	sink->dev = dev;
    860 
    861 	return sink;
    862 }
    863 
    864 gboolean sink_is_active(struct audio_device *dev)
    865 {
    866 	struct sink *sink = dev->sink;
    867 
    868 	if (sink->session)
    869 		return TRUE;
    870 
    871 	return FALSE;
    872 }
    873 
    874 avdtp_state_t sink_get_state(struct audio_device *dev)
    875 {
    876 	struct sink *sink = dev->sink;
    877 
    878 	return sink->stream_state;
    879 }
    880 
    881 gboolean sink_new_stream(struct audio_device *dev, struct avdtp *session,
    882 				struct avdtp_stream *stream)
    883 {
    884 	struct sink *sink = dev->sink;
    885 
    886 	if (sink->stream)
    887 		return FALSE;
    888 
    889 	if (!sink->session)
    890 		sink->session = avdtp_ref(session);
    891 
    892 	sink->stream = stream;
    893 
    894 	sink->cb_id = avdtp_stream_add_cb(session, stream,
    895 						stream_state_changed, dev);
    896 
    897 	return TRUE;
    898 }
    899 
    900 gboolean sink_shutdown(struct sink *sink)
    901 {
    902 	if (!sink->stream)
    903 		return FALSE;
    904 
    905 	if (avdtp_close(sink->session, sink->stream) < 0)
    906 		return FALSE;
    907 
    908 	return TRUE;
    909 }
    910 
    911 unsigned int sink_add_state_cb(sink_state_cb cb, void *user_data)
    912 {
    913 	struct sink_state_callback *state_cb;
    914 	static unsigned int id = 0;
    915 
    916 	state_cb = g_new(struct sink_state_callback, 1);
    917 	state_cb->cb = cb;
    918 	state_cb->user_data = user_data;
    919 	state_cb->id = ++id;
    920 
    921 	sink_callbacks = g_slist_append(sink_callbacks, state_cb);
    922 
    923 	return state_cb->id;
    924 }
    925 
    926 gboolean sink_remove_state_cb(unsigned int id)
    927 {
    928 	GSList *l;
    929 
    930 	for (l = sink_callbacks; l != NULL; l = l->next) {
    931 		struct sink_state_callback *cb = l->data;
    932 		if (cb && cb->id == id) {
    933 			sink_callbacks = g_slist_remove(sink_callbacks, cb);
    934 			g_free(cb);
    935 			return TRUE;
    936 		}
    937 	}
    938 
    939 	return FALSE;
    940 }
    941