Home | History | Annotate | Download | only in audio
      1 /*
      2  *
      3  *  BlueZ - Bluetooth protocol stack for Linux
      4  *
      5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel (at) holtmann.org>
      6  *
      7  *
      8  *  This library is free software; you can redistribute it and/or
      9  *  modify it under the terms of the GNU Lesser General Public
     10  *  License as published by the Free Software Foundation; either
     11  *  version 2.1 of the License, or (at your option) any later version.
     12  *
     13  *  This library is distributed in the hope that it will be useful,
     14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  *  Lesser General Public License for more details.
     17  *
     18  *  You should have received a copy of the GNU Lesser General Public
     19  *  License along with this library; if not, write to the Free Software
     20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     21  *
     22  */
     23 
     24 #ifdef HAVE_CONFIG_H
     25 #include <config.h>
     26 #endif
     27 
     28 #include <unistd.h>
     29 #include <sys/un.h>
     30 #include <sys/socket.h>
     31 #include <fcntl.h>
     32 #include <pthread.h>
     33 
     34 #include <netinet/in.h>
     35 
     36 #include <bluetooth/bluetooth.h>
     37 
     38 #include <gst/rtp/gstrtpbuffer.h>
     39 
     40 #include <dbus/dbus.h>
     41 
     42 #include "ipc.h"
     43 #include "rtp.h"
     44 #include "a2dp-codecs.h"
     45 
     46 #include "gstpragma.h"
     47 #include "gstavdtpsink.h"
     48 
     49 GST_DEBUG_CATEGORY_STATIC(avdtp_sink_debug);
     50 #define GST_CAT_DEFAULT avdtp_sink_debug
     51 
     52 #define BUFFER_SIZE 2048
     53 #define TEMPLATE_MAX_BITPOOL 64
     54 #define CRC_PROTECTED 1
     55 #define CRC_UNPROTECTED 0
     56 
     57 #define DEFAULT_AUTOCONNECT TRUE
     58 
     59 #define GST_AVDTP_SINK_MUTEX_LOCK(s) G_STMT_START {	\
     60 		g_mutex_lock(s->sink_lock);		\
     61 	} G_STMT_END
     62 
     63 #define GST_AVDTP_SINK_MUTEX_UNLOCK(s) G_STMT_START {	\
     64 		g_mutex_unlock(s->sink_lock);		\
     65 	} G_STMT_END
     66 
     67 #ifndef DBUS_TYPE_UNIX_FD
     68 #define DBUS_TYPE_UNIX_FD -1
     69 #endif
     70 
     71 struct bluetooth_data {
     72 	struct bt_get_capabilities_rsp *caps; /* Bluetooth device caps */
     73 	guint link_mtu;
     74 
     75 	DBusConnection *conn;
     76 	guint8 codec; /* Bluetooth transport configuration */
     77 	gchar *uuid;
     78 	guint8 *config;
     79 	gint config_size;
     80 
     81 	gchar buffer[BUFFER_SIZE];	/* Codec transfer buffer */
     82 };
     83 
     84 #define IS_SBC(n) (strcmp((n), "audio/x-sbc") == 0)
     85 #define IS_MPEG_AUDIO(n) (strcmp((n), "audio/mpeg") == 0)
     86 
     87 enum {
     88 	PROP_0,
     89 	PROP_DEVICE,
     90 	PROP_AUTOCONNECT,
     91 	PROP_TRANSPORT
     92 };
     93 
     94 GST_BOILERPLATE(GstAvdtpSink, gst_avdtp_sink, GstBaseSink,
     95 			GST_TYPE_BASE_SINK);
     96 
     97 static const GstElementDetails avdtp_sink_details =
     98 	GST_ELEMENT_DETAILS("Bluetooth AVDTP sink",
     99 				"Sink/Audio",
    100 				"Plays audio to an A2DP device",
    101 				"Marcel Holtmann <marcel (at) holtmann.org>");
    102 
    103 static GstStaticPadTemplate avdtp_sink_factory =
    104 	GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
    105 		GST_STATIC_CAPS("application/x-rtp, "
    106 				"media = (string) \"audio\","
    107 				"payload = (int) "
    108 					GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
    109 				"clock-rate = (int) { 16000, 32000, "
    110 					"44100, 48000 }, "
    111 				"encoding-name = (string) \"SBC\"; "
    112 				"application/x-rtp, "
    113 				"media = (string) \"audio\", "
    114 				"payload = (int) "
    115 				GST_RTP_PAYLOAD_MPA_STRING ", "
    116 				"clock-rate = (int) 90000; "
    117 				"application/x-rtp, "
    118 				"media = (string) \"audio\", "
    119 				"payload = (int) "
    120 				GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
    121 				"clock-rate = (int) 90000, "
    122 				"encoding-name = (string) \"MPA\""
    123 				));
    124 
    125 static int gst_avdtp_sink_audioservice_send(GstAvdtpSink *self,
    126 					const bt_audio_msg_header_t *msg);
    127 static int gst_avdtp_sink_audioservice_expect(GstAvdtpSink *self,
    128 					bt_audio_msg_header_t *outmsg,
    129 					guint8 expected_name);
    130 
    131 
    132 static void gst_avdtp_sink_base_init(gpointer g_class)
    133 {
    134 	GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
    135 
    136 	gst_element_class_add_pad_template(element_class,
    137 		gst_static_pad_template_get(&avdtp_sink_factory));
    138 
    139 	gst_element_class_set_details(element_class, &avdtp_sink_details);
    140 }
    141 
    142 static void gst_avdtp_sink_transport_release(GstAvdtpSink *self)
    143 {
    144 	DBusMessage *msg;
    145 	const char *access_type = "w";
    146 
    147 	msg = dbus_message_new_method_call("org.bluez", self->transport,
    148 						"org.bluez.MediaTransport",
    149 						"Release");
    150 
    151 	dbus_message_append_args(msg, DBUS_TYPE_STRING, &access_type,
    152 					DBUS_TYPE_INVALID);
    153 
    154 	dbus_connection_send(self->data->conn, msg, NULL);
    155 
    156 	dbus_message_unref(msg);
    157 }
    158 
    159 static gboolean gst_avdtp_sink_stop(GstBaseSink *basesink)
    160 {
    161 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
    162 
    163 	GST_INFO_OBJECT(self, "stop");
    164 
    165 	if (self->watch_id != 0) {
    166 		g_source_remove(self->watch_id);
    167 		self->watch_id = 0;
    168 	}
    169 
    170 	if (self->server) {
    171 		bt_audio_service_close(g_io_channel_unix_get_fd(self->server));
    172 		g_io_channel_unref(self->server);
    173 		self->server = NULL;
    174 	}
    175 
    176 	if (self->stream) {
    177 		g_io_channel_shutdown(self->stream, TRUE, NULL);
    178 		g_io_channel_unref(self->stream);
    179 		self->stream = NULL;
    180 	}
    181 
    182 	if (self->data) {
    183 		if (self->transport)
    184 			gst_avdtp_sink_transport_release(self);
    185 		if (self->data->conn)
    186 			dbus_connection_unref(self->data->conn);
    187 		g_free(self->data);
    188 		self->data = NULL;
    189 	}
    190 
    191 	if (self->stream_caps) {
    192 		gst_caps_unref(self->stream_caps);
    193 		self->stream_caps = NULL;
    194 	}
    195 
    196 	if (self->dev_caps) {
    197 		gst_caps_unref(self->dev_caps);
    198 		self->dev_caps = NULL;
    199 	}
    200 
    201 	return TRUE;
    202 }
    203 
    204 static void gst_avdtp_sink_finalize(GObject *object)
    205 {
    206 	GstAvdtpSink *self = GST_AVDTP_SINK(object);
    207 
    208 	if (self->data)
    209 		gst_avdtp_sink_stop(GST_BASE_SINK(self));
    210 
    211 	if (self->device)
    212 		g_free(self->device);
    213 
    214 	if (self->transport)
    215 		g_free(self->transport);
    216 
    217 	g_mutex_free(self->sink_lock);
    218 
    219 	G_OBJECT_CLASS(parent_class)->finalize(object);
    220 }
    221 
    222 static void gst_avdtp_sink_set_property(GObject *object, guint prop_id,
    223 					const GValue *value, GParamSpec *pspec)
    224 {
    225 	GstAvdtpSink *sink = GST_AVDTP_SINK(object);
    226 
    227 	switch (prop_id) {
    228 	case PROP_DEVICE:
    229 		if (sink->device)
    230 			g_free(sink->device);
    231 		sink->device = g_value_dup_string(value);
    232 		break;
    233 
    234 	case PROP_AUTOCONNECT:
    235 		sink->autoconnect = g_value_get_boolean(value);
    236 		break;
    237 
    238 	case PROP_TRANSPORT:
    239 		if (sink->transport)
    240 			g_free(sink->transport);
    241 		sink->transport = g_value_dup_string(value);
    242 		break;
    243 
    244 	default:
    245 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    246 		break;
    247 	}
    248 }
    249 
    250 static void gst_avdtp_sink_get_property(GObject *object, guint prop_id,
    251 					GValue *value, GParamSpec *pspec)
    252 {
    253 	GstAvdtpSink *sink = GST_AVDTP_SINK(object);
    254 
    255 	switch (prop_id) {
    256 	case PROP_DEVICE:
    257 		g_value_set_string(value, sink->device);
    258 		break;
    259 
    260 	case PROP_AUTOCONNECT:
    261 		g_value_set_boolean(value, sink->autoconnect);
    262 		break;
    263 
    264 	case PROP_TRANSPORT:
    265 		g_value_set_string(value, sink->transport);
    266 		break;
    267 
    268 	default:
    269 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    270 		break;
    271 	}
    272 }
    273 
    274 static gint gst_avdtp_sink_bluetooth_recvmsg_fd(GstAvdtpSink *sink)
    275 {
    276 	int err, ret;
    277 
    278 	ret = bt_audio_service_get_data_fd(
    279 			g_io_channel_unix_get_fd(sink->server));
    280 
    281 	if (ret < 0) {
    282 		err = errno;
    283 		GST_ERROR_OBJECT(sink, "Unable to receive fd: %s (%d)",
    284 				strerror(err), err);
    285 		return -err;
    286 	}
    287 
    288 	sink->stream = g_io_channel_unix_new(ret);
    289 	g_io_channel_set_encoding(sink->stream, NULL, NULL);
    290 	GST_DEBUG_OBJECT(sink, "stream_fd=%d", ret);
    291 
    292 	return 0;
    293 }
    294 
    295 static codec_capabilities_t *gst_avdtp_find_caps(GstAvdtpSink *sink,
    296 						uint8_t codec_type)
    297 {
    298 	struct bt_get_capabilities_rsp *rsp = sink->data->caps;
    299 	codec_capabilities_t *codec = (void *) rsp->data;
    300 	int bytes_left = rsp->h.length - sizeof(*rsp);
    301 
    302 	while (bytes_left > 0) {
    303 		if ((codec->type == codec_type) &&
    304 				!(codec->lock & BT_WRITE_LOCK))
    305 			break;
    306 
    307 		bytes_left -= codec->length;
    308 		codec = (void *) codec + codec->length;
    309 	}
    310 
    311 	if (bytes_left <= 0)
    312 		return NULL;
    313 
    314 	return codec;
    315 }
    316 
    317 static gboolean gst_avdtp_sink_init_sbc_pkt_conf(GstAvdtpSink *sink,
    318 					GstCaps *caps,
    319 					sbc_capabilities_t *pkt)
    320 {
    321 	sbc_capabilities_t *cfg;
    322 	const GValue *value = NULL;
    323 	const char *pref, *name;
    324 	gint rate, subbands, blocks;
    325 	GstStructure *structure = gst_caps_get_structure(caps, 0);
    326 
    327 	cfg = (void *) gst_avdtp_find_caps(sink, BT_A2DP_SBC_SINK);
    328 	name = gst_structure_get_name(structure);
    329 
    330 	if (!(IS_SBC(name))) {
    331 		GST_ERROR_OBJECT(sink, "Unexpected format %s, "
    332 				"was expecting sbc", name);
    333 		return FALSE;
    334 	}
    335 
    336 	value = gst_structure_get_value(structure, "rate");
    337 	rate = g_value_get_int(value);
    338 	if (rate == 44100)
    339 		cfg->frequency = BT_SBC_SAMPLING_FREQ_44100;
    340 	else if (rate == 48000)
    341 		cfg->frequency = BT_SBC_SAMPLING_FREQ_48000;
    342 	else if (rate == 32000)
    343 		cfg->frequency = BT_SBC_SAMPLING_FREQ_32000;
    344 	else if (rate == 16000)
    345 		cfg->frequency = BT_SBC_SAMPLING_FREQ_16000;
    346 	else {
    347 		GST_ERROR_OBJECT(sink, "Invalid rate while setting caps");
    348 		return FALSE;
    349 	}
    350 
    351 	value = gst_structure_get_value(structure, "mode");
    352 	pref = g_value_get_string(value);
    353 	if (strcmp(pref, "mono") == 0)
    354 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
    355 	else if (strcmp(pref, "dual") == 0)
    356 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
    357 	else if (strcmp(pref, "stereo") == 0)
    358 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
    359 	else if (strcmp(pref, "joint") == 0)
    360 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
    361 	else {
    362 		GST_ERROR_OBJECT(sink, "Invalid mode %s", pref);
    363 		return FALSE;
    364 	}
    365 
    366 	value = gst_structure_get_value(structure, "allocation");
    367 	pref = g_value_get_string(value);
    368 	if (strcmp(pref, "loudness") == 0)
    369 		cfg->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
    370 	else if (strcmp(pref, "snr") == 0)
    371 		cfg->allocation_method = BT_A2DP_ALLOCATION_SNR;
    372 	else {
    373 		GST_ERROR_OBJECT(sink, "Invalid allocation: %s", pref);
    374 		return FALSE;
    375 	}
    376 
    377 	value = gst_structure_get_value(structure, "subbands");
    378 	subbands = g_value_get_int(value);
    379 	if (subbands == 8)
    380 		cfg->subbands = BT_A2DP_SUBBANDS_8;
    381 	else if (subbands == 4)
    382 		cfg->subbands = BT_A2DP_SUBBANDS_4;
    383 	else {
    384 		GST_ERROR_OBJECT(sink, "Invalid subbands %d", subbands);
    385 		return FALSE;
    386 	}
    387 
    388 	value = gst_structure_get_value(structure, "blocks");
    389 	blocks = g_value_get_int(value);
    390 	if (blocks == 16)
    391 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_16;
    392 	else if (blocks == 12)
    393 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_12;
    394 	else if (blocks == 8)
    395 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_8;
    396 	else if (blocks == 4)
    397 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_4;
    398 	else {
    399 		GST_ERROR_OBJECT(sink, "Invalid blocks %d", blocks);
    400 		return FALSE;
    401 	}
    402 
    403 	value = gst_structure_get_value(structure, "bitpool");
    404 	cfg->max_bitpool = cfg->min_bitpool = g_value_get_int(value);
    405 
    406 	memcpy(pkt, cfg, sizeof(*pkt));
    407 
    408 	return TRUE;
    409 }
    410 
    411 static gboolean gst_avdtp_sink_conf_recv_stream_fd(
    412 					GstAvdtpSink *self)
    413 {
    414 	struct bluetooth_data *data = self->data;
    415 	gint ret;
    416 	GError *gerr = NULL;
    417 	GIOStatus status;
    418 	GIOFlags flags;
    419 	int fd;
    420 
    421 	/* Proceed if stream was already acquired */
    422 	if (self->stream != NULL)
    423 		goto proceed;
    424 
    425 	ret = gst_avdtp_sink_bluetooth_recvmsg_fd(self);
    426 	if (ret < 0)
    427 		return FALSE;
    428 
    429 	if (!self->stream) {
    430 		GST_ERROR_OBJECT(self, "Error while configuring device: "
    431 				"could not acquire audio socket");
    432 		return FALSE;
    433 	}
    434 
    435 proceed:
    436 	/* set stream socket to nonblock */
    437 	GST_LOG_OBJECT(self, "setting stream socket to nonblock");
    438 	flags = g_io_channel_get_flags(self->stream);
    439 	flags |= G_IO_FLAG_NONBLOCK;
    440 	status = g_io_channel_set_flags(self->stream, flags, &gerr);
    441 	if (status != G_IO_STATUS_NORMAL) {
    442 		if (gerr)
    443 			GST_WARNING_OBJECT(self, "Error while "
    444 				"setting server socket to nonblock: "
    445 				"%s", gerr->message);
    446 		else
    447 			GST_WARNING_OBJECT(self, "Error while "
    448 					"setting server "
    449 					"socket to nonblock");
    450 	}
    451 
    452 	fd = g_io_channel_unix_get_fd(self->stream);
    453 
    454 	/* It is possible there is some outstanding
    455 	data in the pipe - we have to empty it */
    456 	GST_LOG_OBJECT(self, "emptying stream pipe");
    457 	while (1) {
    458 		ssize_t bread = read(fd, data->buffer, data->link_mtu);
    459 		if (bread <= 0)
    460 			break;
    461 	}
    462 
    463 	/* set stream socket to block */
    464 	GST_LOG_OBJECT(self, "setting stream socket to block");
    465 	flags = g_io_channel_get_flags(self->stream);
    466 	flags &= ~G_IO_FLAG_NONBLOCK;
    467 	status = g_io_channel_set_flags(self->stream, flags, &gerr);
    468 	if (status != G_IO_STATUS_NORMAL) {
    469 		if (gerr)
    470 			GST_WARNING_OBJECT(self, "Error while "
    471 				"setting server socket to block:"
    472 				"%s", gerr->message);
    473 		else
    474 			GST_WARNING_OBJECT(self, "Error while "
    475 				"setting server "
    476 				"socket to block");
    477 	}
    478 
    479 	memset(data->buffer, 0, sizeof(data->buffer));
    480 
    481 	return TRUE;
    482 }
    483 
    484 static gboolean server_callback(GIOChannel *chan,
    485 					GIOCondition cond, gpointer data)
    486 {
    487 	if (cond & G_IO_HUP || cond & G_IO_NVAL)
    488 		return FALSE;
    489 	else if (cond & G_IO_ERR)
    490 		GST_WARNING_OBJECT(GST_AVDTP_SINK(data),
    491 					"Untreated callback G_IO_ERR");
    492 
    493 	return TRUE;
    494 }
    495 
    496 static GstStructure *gst_avdtp_sink_parse_sbc_caps(
    497 			GstAvdtpSink *self, sbc_capabilities_t *sbc)
    498 {
    499 	GstStructure *structure;
    500 	GValue *value;
    501 	GValue *list;
    502 	gboolean mono, stereo;
    503 
    504 	if (sbc == NULL)
    505 		return NULL;
    506 
    507 	structure = gst_structure_empty_new("audio/x-sbc");
    508 	value = g_value_init(g_new0(GValue, 1), G_TYPE_STRING);
    509 
    510 	/* mode */
    511 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    512 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
    513 		g_value_set_static_string(value, "mono");
    514 		gst_value_list_prepend_value(list, value);
    515 	}
    516 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) {
    517 		g_value_set_static_string(value, "stereo");
    518 		gst_value_list_prepend_value(list, value);
    519 	}
    520 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) {
    521 		g_value_set_static_string(value, "dual");
    522 		gst_value_list_prepend_value(list, value);
    523 	}
    524 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO) {
    525 		g_value_set_static_string(value, "joint");
    526 		gst_value_list_prepend_value(list, value);
    527 	}
    528 	g_value_unset(value);
    529 	if (list) {
    530 		gst_structure_set_value(structure, "mode", list);
    531 		g_free(list);
    532 		list = NULL;
    533 	}
    534 
    535 	/* subbands */
    536 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    537 	value = g_value_init(value, G_TYPE_INT);
    538 	if (sbc->subbands & BT_A2DP_SUBBANDS_4) {
    539 		g_value_set_int(value, 4);
    540 		gst_value_list_prepend_value(list, value);
    541 	}
    542 	if (sbc->subbands & BT_A2DP_SUBBANDS_8) {
    543 		g_value_set_int(value, 8);
    544 		gst_value_list_prepend_value(list, value);
    545 	}
    546 	g_value_unset(value);
    547 	if (list) {
    548 		gst_structure_set_value(structure, "subbands", list);
    549 		g_free(list);
    550 		list = NULL;
    551 	}
    552 
    553 	/* blocks */
    554 	value = g_value_init(value, G_TYPE_INT);
    555 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    556 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_16) {
    557 		g_value_set_int(value, 16);
    558 		gst_value_list_prepend_value(list, value);
    559 	}
    560 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_12) {
    561 		g_value_set_int(value, 12);
    562 		gst_value_list_prepend_value(list, value);
    563 	}
    564 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_8) {
    565 		g_value_set_int(value, 8);
    566 		gst_value_list_prepend_value(list, value);
    567 	}
    568 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_4) {
    569 		g_value_set_int(value, 4);
    570 		gst_value_list_prepend_value(list, value);
    571 	}
    572 	g_value_unset(value);
    573 	if (list) {
    574 		gst_structure_set_value(structure, "blocks", list);
    575 		g_free(list);
    576 		list = NULL;
    577 	}
    578 
    579 	/* allocation */
    580 	g_value_init(value, G_TYPE_STRING);
    581 	list = g_value_init(g_new0(GValue,1), GST_TYPE_LIST);
    582 	if (sbc->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS) {
    583 		g_value_set_static_string(value, "loudness");
    584 		gst_value_list_prepend_value(list, value);
    585 	}
    586 	if (sbc->allocation_method & BT_A2DP_ALLOCATION_SNR) {
    587 		g_value_set_static_string(value, "snr");
    588 		gst_value_list_prepend_value(list, value);
    589 	}
    590 	g_value_unset(value);
    591 	if (list) {
    592 		gst_structure_set_value(structure, "allocation", list);
    593 		g_free(list);
    594 		list = NULL;
    595 	}
    596 
    597 	/* rate */
    598 	g_value_init(value, G_TYPE_INT);
    599 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    600 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_48000) {
    601 		g_value_set_int(value, 48000);
    602 		gst_value_list_prepend_value(list, value);
    603 	}
    604 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_44100) {
    605 		g_value_set_int(value, 44100);
    606 		gst_value_list_prepend_value(list, value);
    607 	}
    608 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_32000) {
    609 		g_value_set_int(value, 32000);
    610 		gst_value_list_prepend_value(list, value);
    611 	}
    612 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_16000) {
    613 		g_value_set_int(value, 16000);
    614 		gst_value_list_prepend_value(list, value);
    615 	}
    616 	g_value_unset(value);
    617 	if (list) {
    618 		gst_structure_set_value(structure, "rate", list);
    619 		g_free(list);
    620 		list = NULL;
    621 	}
    622 
    623 	/* bitpool */
    624 	value = g_value_init(value, GST_TYPE_INT_RANGE);
    625 	gst_value_set_int_range(value,
    626 			MIN(sbc->min_bitpool, TEMPLATE_MAX_BITPOOL),
    627 			MIN(sbc->max_bitpool, TEMPLATE_MAX_BITPOOL));
    628 	gst_structure_set_value(structure, "bitpool", value);
    629 	g_value_unset(value);
    630 
    631 	/* channels */
    632 	mono = FALSE;
    633 	stereo = FALSE;
    634 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
    635 		mono = TRUE;
    636 	if ((sbc->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) ||
    637 			(sbc->channel_mode &
    638 			BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) ||
    639 			(sbc->channel_mode &
    640 			BT_A2DP_CHANNEL_MODE_JOINT_STEREO))
    641 		stereo = TRUE;
    642 
    643 	if (mono && stereo) {
    644 		g_value_init(value, GST_TYPE_INT_RANGE);
    645 		gst_value_set_int_range(value, 1, 2);
    646 	} else {
    647 		g_value_init(value, G_TYPE_INT);
    648 		if (mono)
    649 			g_value_set_int(value, 1);
    650 		else if (stereo)
    651 			g_value_set_int(value, 2);
    652 		else {
    653 			GST_ERROR_OBJECT(self,
    654 				"Unexpected number of channels");
    655 			g_value_set_int(value, 0);
    656 		}
    657 	}
    658 
    659 	gst_structure_set_value(structure, "channels", value);
    660 	g_free(value);
    661 
    662 	return structure;
    663 }
    664 
    665 static GstStructure *gst_avdtp_sink_parse_mpeg_caps(
    666 			GstAvdtpSink *self, mpeg_capabilities_t *mpeg)
    667 {
    668 	GstStructure *structure;
    669 	GValue *value;
    670 	GValue *list;
    671 	gboolean valid_layer = FALSE;
    672 	gboolean mono, stereo;
    673 
    674 	if (!mpeg)
    675 		return NULL;
    676 
    677 	GST_LOG_OBJECT(self, "parsing mpeg caps");
    678 
    679 	structure = gst_structure_empty_new("audio/mpeg");
    680 	value = g_new0(GValue, 1);
    681 	g_value_init(value, G_TYPE_INT);
    682 
    683 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    684 	g_value_set_int(value, 1);
    685 	gst_value_list_prepend_value(list, value);
    686 	g_value_set_int(value, 2);
    687 	gst_value_list_prepend_value(list, value);
    688 	gst_structure_set_value(structure, "mpegversion", list);
    689 	g_free(list);
    690 
    691 	/* layer */
    692 	GST_LOG_OBJECT(self, "setting mpeg layer");
    693 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    694 	if (mpeg->layer & BT_MPEG_LAYER_1) {
    695 		g_value_set_int(value, 1);
    696 		gst_value_list_prepend_value(list, value);
    697 		valid_layer = TRUE;
    698 	}
    699 	if (mpeg->layer & BT_MPEG_LAYER_2) {
    700 		g_value_set_int(value, 2);
    701 		gst_value_list_prepend_value(list, value);
    702 		valid_layer = TRUE;
    703 	}
    704 	if (mpeg->layer & BT_MPEG_LAYER_3) {
    705 		g_value_set_int(value, 3);
    706 		gst_value_list_prepend_value(list, value);
    707 		valid_layer = TRUE;
    708 	}
    709 	if (list) {
    710 		gst_structure_set_value(structure, "layer", list);
    711 		g_free(list);
    712 		list = NULL;
    713 	}
    714 
    715 	if (!valid_layer) {
    716 		gst_structure_free(structure);
    717 		g_free(value);
    718 		return NULL;
    719 	}
    720 
    721 	/* rate */
    722 	GST_LOG_OBJECT(self, "setting mpeg rate");
    723 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    724 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_48000) {
    725 		g_value_set_int(value, 48000);
    726 		gst_value_list_prepend_value(list, value);
    727 	}
    728 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_44100) {
    729 		g_value_set_int(value, 44100);
    730 		gst_value_list_prepend_value(list, value);
    731 	}
    732 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_32000) {
    733 		g_value_set_int(value, 32000);
    734 		gst_value_list_prepend_value(list, value);
    735 	}
    736 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_24000) {
    737 		g_value_set_int(value, 24000);
    738 		gst_value_list_prepend_value(list, value);
    739 	}
    740 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_22050) {
    741 		g_value_set_int(value, 22050);
    742 		gst_value_list_prepend_value(list, value);
    743 	}
    744 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_16000) {
    745 		g_value_set_int(value, 16000);
    746 		gst_value_list_prepend_value(list, value);
    747 	}
    748 	g_value_unset(value);
    749 	if (list) {
    750 		gst_structure_set_value(structure, "rate", list);
    751 		g_free(list);
    752 		list = NULL;
    753 	}
    754 
    755 	/* channels */
    756 	GST_LOG_OBJECT(self, "setting mpeg channels");
    757 	mono = FALSE;
    758 	stereo = FALSE;
    759 	if (mpeg->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
    760 		mono = TRUE;
    761 	if ((mpeg->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) ||
    762 			(mpeg->channel_mode &
    763 			BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) ||
    764 			(mpeg->channel_mode &
    765 			BT_A2DP_CHANNEL_MODE_JOINT_STEREO))
    766 		stereo = TRUE;
    767 
    768 	if (mono && stereo) {
    769 		g_value_init(value, GST_TYPE_INT_RANGE);
    770 		gst_value_set_int_range(value, 1, 2);
    771 	} else {
    772 		g_value_init(value, G_TYPE_INT);
    773 		if (mono)
    774 			g_value_set_int(value, 1);
    775 		else if (stereo)
    776 			g_value_set_int(value, 2);
    777 		else {
    778 			GST_ERROR_OBJECT(self,
    779 				"Unexpected number of channels");
    780 			g_value_set_int(value, 0);
    781 		}
    782 	}
    783 	gst_structure_set_value(structure, "channels", value);
    784 	g_free(value);
    785 
    786 	return structure;
    787 }
    788 
    789 static GstStructure *gst_avdtp_sink_parse_sbc_raw(GstAvdtpSink *self)
    790 {
    791 	a2dp_sbc_t *sbc = (a2dp_sbc_t *) self->data->config;
    792 	GstStructure *structure;
    793 	GValue *value;
    794 	GValue *list;
    795 	gboolean mono, stereo;
    796 
    797 	structure = gst_structure_empty_new("audio/x-sbc");
    798 	value = g_value_init(g_new0(GValue, 1), G_TYPE_STRING);
    799 
    800 	/* mode */
    801 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    802 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
    803 		g_value_set_static_string(value, "mono");
    804 		gst_value_list_prepend_value(list, value);
    805 	}
    806 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) {
    807 		g_value_set_static_string(value, "stereo");
    808 		gst_value_list_prepend_value(list, value);
    809 	}
    810 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) {
    811 		g_value_set_static_string(value, "dual");
    812 		gst_value_list_prepend_value(list, value);
    813 	}
    814 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO) {
    815 		g_value_set_static_string(value, "joint");
    816 		gst_value_list_prepend_value(list, value);
    817 	}
    818 	g_value_unset(value);
    819 	if (list) {
    820 		gst_structure_set_value(structure, "mode", list);
    821 		g_free(list);
    822 		list = NULL;
    823 	}
    824 
    825 	/* subbands */
    826 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    827 	value = g_value_init(value, G_TYPE_INT);
    828 	if (sbc->subbands & BT_A2DP_SUBBANDS_4) {
    829 		g_value_set_int(value, 4);
    830 		gst_value_list_prepend_value(list, value);
    831 	}
    832 	if (sbc->subbands & BT_A2DP_SUBBANDS_8) {
    833 		g_value_set_int(value, 8);
    834 		gst_value_list_prepend_value(list, value);
    835 	}
    836 	g_value_unset(value);
    837 	if (list) {
    838 		gst_structure_set_value(structure, "subbands", list);
    839 		g_free(list);
    840 		list = NULL;
    841 	}
    842 
    843 	/* blocks */
    844 	value = g_value_init(value, G_TYPE_INT);
    845 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    846 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_16) {
    847 		g_value_set_int(value, 16);
    848 		gst_value_list_prepend_value(list, value);
    849 	}
    850 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_12) {
    851 		g_value_set_int(value, 12);
    852 		gst_value_list_prepend_value(list, value);
    853 	}
    854 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_8) {
    855 		g_value_set_int(value, 8);
    856 		gst_value_list_prepend_value(list, value);
    857 	}
    858 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_4) {
    859 		g_value_set_int(value, 4);
    860 		gst_value_list_prepend_value(list, value);
    861 	}
    862 	g_value_unset(value);
    863 	if (list) {
    864 		gst_structure_set_value(structure, "blocks", list);
    865 		g_free(list);
    866 		list = NULL;
    867 	}
    868 
    869 	/* allocation */
    870 	g_value_init(value, G_TYPE_STRING);
    871 	list = g_value_init(g_new0(GValue,1), GST_TYPE_LIST);
    872 	if (sbc->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS) {
    873 		g_value_set_static_string(value, "loudness");
    874 		gst_value_list_prepend_value(list, value);
    875 	}
    876 	if (sbc->allocation_method & BT_A2DP_ALLOCATION_SNR) {
    877 		g_value_set_static_string(value, "snr");
    878 		gst_value_list_prepend_value(list, value);
    879 	}
    880 	g_value_unset(value);
    881 	if (list) {
    882 		gst_structure_set_value(structure, "allocation", list);
    883 		g_free(list);
    884 		list = NULL;
    885 	}
    886 
    887 	/* rate */
    888 	g_value_init(value, G_TYPE_INT);
    889 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    890 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_48000) {
    891 		g_value_set_int(value, 48000);
    892 		gst_value_list_prepend_value(list, value);
    893 	}
    894 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_44100) {
    895 		g_value_set_int(value, 44100);
    896 		gst_value_list_prepend_value(list, value);
    897 	}
    898 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_32000) {
    899 		g_value_set_int(value, 32000);
    900 		gst_value_list_prepend_value(list, value);
    901 	}
    902 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_16000) {
    903 		g_value_set_int(value, 16000);
    904 		gst_value_list_prepend_value(list, value);
    905 	}
    906 	g_value_unset(value);
    907 	if (list) {
    908 		gst_structure_set_value(structure, "rate", list);
    909 		g_free(list);
    910 		list = NULL;
    911 	}
    912 
    913 	/* bitpool */
    914 	value = g_value_init(value, GST_TYPE_INT_RANGE);
    915 	gst_value_set_int_range(value,
    916 			MIN(sbc->min_bitpool, TEMPLATE_MAX_BITPOOL),
    917 			MIN(sbc->max_bitpool, TEMPLATE_MAX_BITPOOL));
    918 	gst_structure_set_value(structure, "bitpool", value);
    919 	g_value_unset(value);
    920 
    921 	/* channels */
    922 	mono = FALSE;
    923 	stereo = FALSE;
    924 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
    925 		mono = TRUE;
    926 	if ((sbc->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) ||
    927 			(sbc->channel_mode &
    928 			BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) ||
    929 			(sbc->channel_mode &
    930 			BT_A2DP_CHANNEL_MODE_JOINT_STEREO))
    931 		stereo = TRUE;
    932 
    933 	if (mono && stereo) {
    934 		g_value_init(value, GST_TYPE_INT_RANGE);
    935 		gst_value_set_int_range(value, 1, 2);
    936 	} else {
    937 		g_value_init(value, G_TYPE_INT);
    938 		if (mono)
    939 			g_value_set_int(value, 1);
    940 		else if (stereo)
    941 			g_value_set_int(value, 2);
    942 		else {
    943 			GST_ERROR_OBJECT(self,
    944 				"Unexpected number of channels");
    945 			g_value_set_int(value, 0);
    946 		}
    947 	}
    948 
    949 	gst_structure_set_value(structure, "channels", value);
    950 	g_free(value);
    951 
    952 	return structure;
    953 }
    954 
    955 static GstStructure *gst_avdtp_sink_parse_mpeg_raw(GstAvdtpSink *self)
    956 {
    957 	a2dp_mpeg_t *mpeg = (a2dp_mpeg_t *) self->data->config;
    958 	GstStructure *structure;
    959 	GValue *value;
    960 	GValue *list;
    961 	gboolean valid_layer = FALSE;
    962 	gboolean mono, stereo;
    963 
    964 	GST_LOG_OBJECT(self, "parsing mpeg caps");
    965 
    966 	structure = gst_structure_empty_new("audio/mpeg");
    967 	value = g_new0(GValue, 1);
    968 	g_value_init(value, G_TYPE_INT);
    969 
    970 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    971 	g_value_set_int(value, 1);
    972 	gst_value_list_prepend_value(list, value);
    973 	g_value_set_int(value, 2);
    974 	gst_value_list_prepend_value(list, value);
    975 	gst_structure_set_value(structure, "mpegversion", list);
    976 	g_free(list);
    977 
    978 	/* layer */
    979 	GST_LOG_OBJECT(self, "setting mpeg layer");
    980 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    981 	if (mpeg->layer & BT_MPEG_LAYER_1) {
    982 		g_value_set_int(value, 1);
    983 		gst_value_list_prepend_value(list, value);
    984 		valid_layer = TRUE;
    985 	}
    986 	if (mpeg->layer & BT_MPEG_LAYER_2) {
    987 		g_value_set_int(value, 2);
    988 		gst_value_list_prepend_value(list, value);
    989 		valid_layer = TRUE;
    990 	}
    991 	if (mpeg->layer & BT_MPEG_LAYER_3) {
    992 		g_value_set_int(value, 3);
    993 		gst_value_list_prepend_value(list, value);
    994 		valid_layer = TRUE;
    995 	}
    996 	if (list) {
    997 		gst_structure_set_value(structure, "layer", list);
    998 		g_free(list);
    999 		list = NULL;
   1000 	}
   1001 
   1002 	if (!valid_layer) {
   1003 		gst_structure_free(structure);
   1004 		g_free(value);
   1005 		return NULL;
   1006 	}
   1007 
   1008 	/* rate */
   1009 	GST_LOG_OBJECT(self, "setting mpeg rate");
   1010 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
   1011 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_48000) {
   1012 		g_value_set_int(value, 48000);
   1013 		gst_value_list_prepend_value(list, value);
   1014 	}
   1015 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_44100) {
   1016 		g_value_set_int(value, 44100);
   1017 		gst_value_list_prepend_value(list, value);
   1018 	}
   1019 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_32000) {
   1020 		g_value_set_int(value, 32000);
   1021 		gst_value_list_prepend_value(list, value);
   1022 	}
   1023 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_24000) {
   1024 		g_value_set_int(value, 24000);
   1025 		gst_value_list_prepend_value(list, value);
   1026 	}
   1027 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_22050) {
   1028 		g_value_set_int(value, 22050);
   1029 		gst_value_list_prepend_value(list, value);
   1030 	}
   1031 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_16000) {
   1032 		g_value_set_int(value, 16000);
   1033 		gst_value_list_prepend_value(list, value);
   1034 	}
   1035 	g_value_unset(value);
   1036 	if (list) {
   1037 		gst_structure_set_value(structure, "rate", list);
   1038 		g_free(list);
   1039 		list = NULL;
   1040 	}
   1041 
   1042 	/* channels */
   1043 	GST_LOG_OBJECT(self, "setting mpeg channels");
   1044 	mono = FALSE;
   1045 	stereo = FALSE;
   1046 	if (mpeg->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
   1047 		mono = TRUE;
   1048 	if ((mpeg->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) ||
   1049 			(mpeg->channel_mode &
   1050 			BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) ||
   1051 			(mpeg->channel_mode &
   1052 			BT_A2DP_CHANNEL_MODE_JOINT_STEREO))
   1053 		stereo = TRUE;
   1054 
   1055 	if (mono && stereo) {
   1056 		g_value_init(value, GST_TYPE_INT_RANGE);
   1057 		gst_value_set_int_range(value, 1, 2);
   1058 	} else {
   1059 		g_value_init(value, G_TYPE_INT);
   1060 		if (mono)
   1061 			g_value_set_int(value, 1);
   1062 		else if (stereo)
   1063 			g_value_set_int(value, 2);
   1064 		else {
   1065 			GST_ERROR_OBJECT(self,
   1066 				"Unexpected number of channels");
   1067 			g_value_set_int(value, 0);
   1068 		}
   1069 	}
   1070 	gst_structure_set_value(structure, "channels", value);
   1071 	g_free(value);
   1072 
   1073 	return structure;
   1074 }
   1075 
   1076 static gboolean gst_avdtp_sink_update_config(GstAvdtpSink *self)
   1077 {
   1078 	GstStructure *structure;
   1079 	gchar *tmp;
   1080 
   1081 	switch (self->data->codec) {
   1082 	case A2DP_CODEC_SBC:
   1083 		structure = gst_avdtp_sink_parse_sbc_raw(self);
   1084 		break;
   1085 	case A2DP_CODEC_MPEG12:
   1086 		structure = gst_avdtp_sink_parse_mpeg_raw(self);
   1087 		break;
   1088 	default:
   1089 		GST_ERROR_OBJECT(self, "Unsupported configuration");
   1090 		return FALSE;
   1091 	}
   1092 
   1093 	if (structure == NULL)
   1094 		return FALSE;
   1095 
   1096 	if (self->dev_caps != NULL)
   1097 		gst_caps_unref(self->dev_caps);
   1098 
   1099 	self->dev_caps = gst_caps_new_full(structure, NULL);
   1100 
   1101 	tmp = gst_caps_to_string(self->dev_caps);
   1102 	GST_DEBUG_OBJECT(self, "Transport configuration: %s", tmp);
   1103 	g_free(tmp);
   1104 
   1105 	return TRUE;
   1106 }
   1107 
   1108 static gboolean gst_avdtp_sink_update_caps(GstAvdtpSink *self)
   1109 {
   1110 	sbc_capabilities_t *sbc;
   1111 	mpeg_capabilities_t *mpeg;
   1112 	GstStructure *sbc_structure;
   1113 	GstStructure *mpeg_structure;
   1114 	gchar *tmp;
   1115 
   1116 	GST_LOG_OBJECT(self, "updating device caps");
   1117 
   1118 	if (self->data->config_size != 0 && self->data->config != NULL)
   1119 		return gst_avdtp_sink_update_config(self);
   1120 
   1121 	sbc = (void *) gst_avdtp_find_caps(self, BT_A2DP_SBC_SINK);
   1122 	mpeg = (void *) gst_avdtp_find_caps(self, BT_A2DP_MPEG12_SINK);
   1123 
   1124 	sbc_structure = gst_avdtp_sink_parse_sbc_caps(self, sbc);
   1125 	mpeg_structure = gst_avdtp_sink_parse_mpeg_caps(self, mpeg);
   1126 
   1127 	if (self->dev_caps != NULL)
   1128 		gst_caps_unref(self->dev_caps);
   1129 	self->dev_caps = gst_caps_new_full(sbc_structure, NULL);
   1130 	if (mpeg_structure != NULL)
   1131 		gst_caps_append_structure(self->dev_caps, mpeg_structure);
   1132 
   1133 	tmp = gst_caps_to_string(self->dev_caps);
   1134 	GST_DEBUG_OBJECT(self, "Device capabilities: %s", tmp);
   1135 	g_free(tmp);
   1136 
   1137 	return TRUE;
   1138 }
   1139 
   1140 static gboolean gst_avdtp_sink_get_capabilities(GstAvdtpSink *self)
   1141 {
   1142 	gchar *buf[BT_SUGGESTED_BUFFER_SIZE];
   1143 	struct bt_get_capabilities_req *req = (void *) buf;
   1144 	struct bt_get_capabilities_rsp *rsp = (void *) buf;
   1145 	int err;
   1146 
   1147 	memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
   1148 
   1149 	req->h.type = BT_REQUEST;
   1150 	req->h.name = BT_GET_CAPABILITIES;
   1151 	req->h.length = sizeof(*req);
   1152 
   1153 	if (self->device == NULL)
   1154 		return FALSE;
   1155 	strncpy(req->destination, self->device, 18);
   1156 	if (self->autoconnect)
   1157 		req->flags |= BT_FLAG_AUTOCONNECT;
   1158 
   1159 	err = gst_avdtp_sink_audioservice_send(self, &req->h);
   1160 	if (err < 0) {
   1161 		GST_ERROR_OBJECT(self, "Error while asking device caps");
   1162 		return FALSE;
   1163 	}
   1164 
   1165 	rsp->h.length = 0;
   1166 	err = gst_avdtp_sink_audioservice_expect(self,
   1167 					&rsp->h, BT_GET_CAPABILITIES);
   1168 	if (err < 0) {
   1169 		GST_ERROR_OBJECT(self, "Error while getting device caps");
   1170 		return FALSE;
   1171 	}
   1172 
   1173 	self->data->caps = g_malloc0(rsp->h.length);
   1174 	memcpy(self->data->caps, rsp, rsp->h.length);
   1175 	if (!gst_avdtp_sink_update_caps(self)) {
   1176 		GST_WARNING_OBJECT(self, "failed to update capabilities");
   1177 		return FALSE;
   1178 	}
   1179 
   1180 	return TRUE;
   1181 }
   1182 
   1183 static gint gst_avdtp_sink_get_channel_mode(const gchar *mode)
   1184 {
   1185 	if (strcmp(mode, "stereo") == 0)
   1186 		return BT_A2DP_CHANNEL_MODE_STEREO;
   1187 	else if (strcmp(mode, "joint-stereo") == 0)
   1188 		return BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
   1189 	else if (strcmp(mode, "dual-channel") == 0)
   1190 		return BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
   1191 	else if (strcmp(mode, "mono") == 0)
   1192 		return BT_A2DP_CHANNEL_MODE_MONO;
   1193 	else
   1194 		return -1;
   1195 }
   1196 
   1197 static void gst_avdtp_sink_tag(const GstTagList *taglist,
   1198 			const gchar *tag, gpointer user_data)
   1199 {
   1200 	gboolean crc;
   1201 	gchar *channel_mode = NULL;
   1202 	GstAvdtpSink *self = GST_AVDTP_SINK(user_data);
   1203 
   1204 	if (strcmp(tag, "has-crc") == 0) {
   1205 
   1206 		if (!gst_tag_list_get_boolean(taglist, tag, &crc)) {
   1207 			GST_WARNING_OBJECT(self, "failed to get crc tag");
   1208 			return;
   1209 		}
   1210 
   1211 		gst_avdtp_sink_set_crc(self, crc);
   1212 
   1213 	} else if (strcmp(tag, "channel-mode") == 0) {
   1214 
   1215 		if (!gst_tag_list_get_string(taglist, tag, &channel_mode)) {
   1216 			GST_WARNING_OBJECT(self,
   1217 				"failed to get channel-mode tag");
   1218 			return;
   1219 		}
   1220 
   1221 		self->channel_mode = gst_avdtp_sink_get_channel_mode(
   1222 					channel_mode);
   1223 		if (self->channel_mode == -1)
   1224 			GST_WARNING_OBJECT(self, "Received invalid channel "
   1225 					"mode: %s", channel_mode);
   1226 		g_free(channel_mode);
   1227 
   1228 	} else
   1229 		GST_DEBUG_OBJECT(self, "received unused tag: %s", tag);
   1230 }
   1231 
   1232 static gboolean gst_avdtp_sink_event(GstBaseSink *basesink,
   1233 			GstEvent *event)
   1234 {
   1235 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1236 	GstTagList *taglist = NULL;
   1237 
   1238 	if (GST_EVENT_TYPE(event) == GST_EVENT_TAG) {
   1239 		/* we check the tags, mp3 has tags that are importants and
   1240 		 * are outside caps */
   1241 		gst_event_parse_tag(event, &taglist);
   1242 		gst_tag_list_foreach(taglist, gst_avdtp_sink_tag, self);
   1243 	}
   1244 
   1245 	return TRUE;
   1246 }
   1247 
   1248 static gboolean gst_avdtp_sink_transport_parse_property(GstAvdtpSink *self,
   1249 							DBusMessageIter *i)
   1250 {
   1251 	const char *key;
   1252 	DBusMessageIter variant_i;
   1253 
   1254 	if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
   1255 		GST_ERROR_OBJECT(self, "Property name not a string.");
   1256 		return FALSE;
   1257 	}
   1258 
   1259 	dbus_message_iter_get_basic(i, &key);
   1260 
   1261 	if (!dbus_message_iter_next(i))  {
   1262 		GST_ERROR_OBJECT(self, "Property value missing");
   1263 		return FALSE;
   1264 	}
   1265 
   1266 	if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
   1267 		GST_ERROR_OBJECT(self, "Property value not a variant.");
   1268 		return FALSE;
   1269 	}
   1270 
   1271 	dbus_message_iter_recurse(i, &variant_i);
   1272 
   1273 	switch (dbus_message_iter_get_arg_type(&variant_i)) {
   1274 	case DBUS_TYPE_BYTE: {
   1275 		uint8_t value;
   1276 		dbus_message_iter_get_basic(&variant_i, &value);
   1277 
   1278 		if (g_str_equal(key, "Codec") == TRUE)
   1279 			self->data->codec = value;
   1280 
   1281 		break;
   1282 	}
   1283 	case DBUS_TYPE_STRING: {
   1284 		const char *value;
   1285 		dbus_message_iter_get_basic(&variant_i, &value);
   1286 
   1287 		if (g_str_equal(key, "UUID") == TRUE) {
   1288 			g_free(self->data->uuid);
   1289 			self->data->uuid = g_strdup(value);
   1290 		}
   1291 
   1292 		break;
   1293 	}
   1294 	case DBUS_TYPE_ARRAY: {
   1295 		DBusMessageIter array_i;
   1296 		char *value;
   1297 		int size;
   1298 
   1299 		dbus_message_iter_recurse(&variant_i, &array_i);
   1300 		dbus_message_iter_get_fixed_array(&array_i, &value, &size);
   1301 
   1302 		if (g_str_equal(key, "Configuration")) {
   1303 			g_free(self->data->config);
   1304 			self->data->config = g_new0(guint8, size);
   1305 			self->data->config_size = size;
   1306 			memcpy(self->data->config, value, size);
   1307 		}
   1308 
   1309 		break;
   1310 	}
   1311 	}
   1312 
   1313 	return TRUE;
   1314 }
   1315 
   1316 static gboolean gst_avdtp_sink_transport_acquire(GstAvdtpSink *self)
   1317 {
   1318 	DBusMessage *msg, *reply;
   1319 	DBusError err;
   1320 	const char *access_type = "w";
   1321 	int fd;
   1322 	uint16_t imtu, omtu;
   1323 
   1324 	dbus_error_init(&err);
   1325 
   1326 	if (self->data->conn == NULL)
   1327 		self->data->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
   1328 
   1329 	msg = dbus_message_new_method_call("org.bluez", self->transport,
   1330 						"org.bluez.MediaTransport",
   1331 						"Acquire");
   1332 
   1333 	dbus_message_append_args(msg, DBUS_TYPE_STRING, &access_type,
   1334 					DBUS_TYPE_INVALID);
   1335 
   1336 	reply = dbus_connection_send_with_reply_and_block(self->data->conn,
   1337 							msg, -1, &err);
   1338 
   1339 	if (dbus_error_is_set(&err))
   1340 		goto fail;
   1341 
   1342 	if (dbus_message_get_args(reply, &err, DBUS_TYPE_UNIX_FD, &fd,
   1343 						DBUS_TYPE_UINT16, &imtu,
   1344 						DBUS_TYPE_UINT16, &omtu,
   1345 						DBUS_TYPE_INVALID) == FALSE)
   1346 		goto fail;
   1347 
   1348 	dbus_message_unref(reply);
   1349 
   1350 	self->stream = g_io_channel_unix_new(fd);
   1351 	g_io_channel_set_encoding(self->stream, NULL, NULL);
   1352 	g_io_channel_set_close_on_unref(self->stream, TRUE);
   1353 	self->data->link_mtu = omtu;
   1354 	GST_DEBUG_OBJECT(self, "stream_fd=%d mtu=%d", fd, omtu);
   1355 
   1356 	return TRUE;
   1357 
   1358 fail:
   1359 	GST_ERROR_OBJECT(self, "Failed to acquire transport stream: %s",
   1360 				err.message);
   1361 
   1362 	dbus_error_free(&err);
   1363 
   1364 	if (reply)
   1365 		dbus_message_unref(msg);
   1366 
   1367 	return FALSE;
   1368 }
   1369 
   1370 static gboolean gst_avdtp_sink_transport_get_properties(GstAvdtpSink *self)
   1371 {
   1372 	DBusMessage *msg, *reply;
   1373 	DBusMessageIter arg_i, ele_i;
   1374 	DBusError err;
   1375 
   1376 	dbus_error_init(&err);
   1377 
   1378 	/* Transport need to be acquire first to make sure the MTUs are
   1379 	   available */
   1380 	if (gst_avdtp_sink_transport_acquire(self) == FALSE)
   1381 		return FALSE;
   1382 
   1383 	msg = dbus_message_new_method_call("org.bluez", self->transport,
   1384 						"org.bluez.MediaTransport",
   1385 						"GetProperties");
   1386 	reply = dbus_connection_send_with_reply_and_block(self->data->conn,
   1387 							msg, -1, &err);
   1388 
   1389 	if (dbus_error_is_set(&err) || reply == NULL) {
   1390 		GST_ERROR_OBJECT(self, "Failed to get transport properties: %s",
   1391 					err.message);
   1392 		goto fail;
   1393 	}
   1394 
   1395 	if (!dbus_message_iter_init(reply, &arg_i)) {
   1396 		GST_ERROR_OBJECT(self, "GetProperties reply has no arguments.");
   1397 		goto fail;
   1398 	}
   1399 
   1400 	if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
   1401 		GST_ERROR_OBJECT(self, "GetProperties argument is not an array.");
   1402 		goto fail;
   1403 	}
   1404 
   1405 	dbus_message_iter_recurse(&arg_i, &ele_i);
   1406 	while (dbus_message_iter_get_arg_type(&ele_i) != DBUS_TYPE_INVALID) {
   1407 
   1408 		if (dbus_message_iter_get_arg_type(&ele_i) ==
   1409 				DBUS_TYPE_DICT_ENTRY) {
   1410 			DBusMessageIter dict_i;
   1411 
   1412 			dbus_message_iter_recurse(&ele_i, &dict_i);
   1413 
   1414 			gst_avdtp_sink_transport_parse_property(self, &dict_i);
   1415 		}
   1416 
   1417 		if (!dbus_message_iter_next(&ele_i))
   1418 			break;
   1419 	}
   1420 
   1421 	return gst_avdtp_sink_update_caps(self);
   1422 
   1423 fail:
   1424 	dbus_message_unref(msg);
   1425 	dbus_message_unref(reply);
   1426 	return FALSE;
   1427 
   1428 }
   1429 
   1430 static gboolean gst_avdtp_sink_start(GstBaseSink *basesink)
   1431 {
   1432 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1433 	gint sk;
   1434 	gint err;
   1435 
   1436 	GST_INFO_OBJECT(self, "start");
   1437 
   1438 	self->data = g_new0(struct bluetooth_data, 1);
   1439 
   1440 	self->stream = NULL;
   1441 	self->stream_caps = NULL;
   1442 	self->mp3_using_crc = -1;
   1443 	self->channel_mode = -1;
   1444 
   1445 	if (self->transport != NULL)
   1446 		return gst_avdtp_sink_transport_get_properties(self);
   1447 
   1448 	self->watch_id = 0;
   1449 
   1450 	sk = bt_audio_service_open();
   1451 	if (sk <= 0) {
   1452 		err = errno;
   1453 		GST_ERROR_OBJECT(self, "Cannot open connection to bt "
   1454 			"audio service: %s %d", strerror(err), err);
   1455 		goto failed;
   1456 	}
   1457 
   1458 	self->server = g_io_channel_unix_new(sk);
   1459 	g_io_channel_set_encoding(self->server, NULL, NULL);
   1460 	self->watch_id = g_io_add_watch(self->server, G_IO_HUP | G_IO_ERR |
   1461 					G_IO_NVAL, server_callback, self);
   1462 
   1463 	if (!gst_avdtp_sink_get_capabilities(self)) {
   1464 		GST_ERROR_OBJECT(self, "failed to get capabilities "
   1465 				"from device");
   1466 		goto failed;
   1467 	}
   1468 
   1469 	return TRUE;
   1470 
   1471 failed:
   1472 	bt_audio_service_close(sk);
   1473 	return FALSE;
   1474 }
   1475 
   1476 static gboolean gst_avdtp_sink_stream_start(GstAvdtpSink *self)
   1477 {
   1478 	gchar buf[BT_SUGGESTED_BUFFER_SIZE];
   1479 	struct bt_start_stream_req *req = (void *) buf;
   1480 	struct bt_start_stream_rsp *rsp = (void *) buf;
   1481 	struct bt_new_stream_ind *ind = (void *) buf;
   1482 	int err;
   1483 
   1484 	if (self->transport != NULL)
   1485 		return gst_avdtp_sink_conf_recv_stream_fd(self);
   1486 
   1487 	memset(req, 0, sizeof(buf));
   1488 	req->h.type = BT_REQUEST;
   1489 	req->h.name = BT_START_STREAM;
   1490 	req->h.length = sizeof(*req);
   1491 
   1492 	err = gst_avdtp_sink_audioservice_send(self, &req->h);
   1493 	if (err < 0) {
   1494 		GST_ERROR_OBJECT(self, "Error ocurred while sending "
   1495 					"start packet");
   1496 		return FALSE;
   1497 	}
   1498 
   1499 	rsp->h.length = sizeof(*rsp);
   1500 	err = gst_avdtp_sink_audioservice_expect(self, &rsp->h,
   1501 							BT_START_STREAM);
   1502 	if (err < 0) {
   1503 		GST_ERROR_OBJECT(self, "Error while stream "
   1504 			"start confirmation");
   1505 		return FALSE;
   1506 	}
   1507 
   1508 	ind->h.length = sizeof(*ind);
   1509 	err = gst_avdtp_sink_audioservice_expect(self, &ind->h,
   1510 							BT_NEW_STREAM);
   1511 	if (err < 0) {
   1512 		GST_ERROR_OBJECT(self, "Error while receiving "
   1513 			"stream filedescriptor");
   1514 		return FALSE;
   1515 	}
   1516 
   1517 	if (!gst_avdtp_sink_conf_recv_stream_fd(self))
   1518 		return FALSE;
   1519 
   1520 	return TRUE;
   1521 }
   1522 
   1523 static gboolean gst_avdtp_sink_init_mp3_pkt_conf(
   1524 		GstAvdtpSink *self, GstCaps *caps,
   1525 		mpeg_capabilities_t *pkt)
   1526 {
   1527 	const GValue *value = NULL;
   1528 	gint rate, layer;
   1529 	const gchar *name;
   1530 	GstStructure *structure = gst_caps_get_structure(caps, 0);
   1531 
   1532 	name = gst_structure_get_name(structure);
   1533 
   1534 	if (!(IS_MPEG_AUDIO(name))) {
   1535 		GST_ERROR_OBJECT(self, "Unexpected format %s, "
   1536 				"was expecting mp3", name);
   1537 		return FALSE;
   1538 	}
   1539 
   1540 	/* layer */
   1541 	value = gst_structure_get_value(structure, "layer");
   1542 	layer = g_value_get_int(value);
   1543 	if (layer == 1)
   1544 		pkt->layer = BT_MPEG_LAYER_1;
   1545 	else if (layer == 2)
   1546 		pkt->layer = BT_MPEG_LAYER_2;
   1547 	else if (layer == 3)
   1548 		pkt->layer = BT_MPEG_LAYER_3;
   1549 	else {
   1550 		GST_ERROR_OBJECT(self, "Unexpected layer: %d", layer);
   1551 		return FALSE;
   1552 	}
   1553 
   1554 	/* crc */
   1555 	if (self->mp3_using_crc != -1)
   1556 		pkt->crc = self->mp3_using_crc;
   1557 	else {
   1558 		GST_ERROR_OBJECT(self, "No info about crc was received, "
   1559 				" can't proceed");
   1560 		return FALSE;
   1561 	}
   1562 
   1563 	/* channel mode */
   1564 	if (self->channel_mode != -1)
   1565 		pkt->channel_mode = self->channel_mode;
   1566 	else {
   1567 		GST_ERROR_OBJECT(self, "No info about channel mode "
   1568 				"received, can't proceed");
   1569 		return FALSE;
   1570 	}
   1571 
   1572 	/* mpf - we will only use the mandatory one */
   1573 	pkt->mpf = 0;
   1574 
   1575 	value = gst_structure_get_value(structure, "rate");
   1576 	rate = g_value_get_int(value);
   1577 	if (rate == 44100)
   1578 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_44100;
   1579 	else if (rate == 48000)
   1580 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_48000;
   1581 	else if (rate == 32000)
   1582 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_32000;
   1583 	else if (rate == 24000)
   1584 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_24000;
   1585 	else if (rate == 22050)
   1586 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_22050;
   1587 	else if (rate == 16000)
   1588 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_16000;
   1589 	else {
   1590 		GST_ERROR_OBJECT(self, "Invalid rate while setting caps");
   1591 		return FALSE;
   1592 	}
   1593 
   1594 	/* vbr - we always say its vbr, we don't have how to know it */
   1595 	pkt->bitrate = 0x8000;
   1596 
   1597 	return TRUE;
   1598 }
   1599 
   1600 static gboolean gst_avdtp_sink_configure(GstAvdtpSink *self,
   1601 			GstCaps *caps)
   1602 {
   1603 	gchar buf[BT_SUGGESTED_BUFFER_SIZE];
   1604 	struct bt_open_req *open_req = (void *) buf;
   1605 	struct bt_open_rsp *open_rsp = (void *) buf;
   1606 	struct bt_set_configuration_req *req = (void *) buf;
   1607 	struct bt_set_configuration_rsp *rsp = (void *) buf;
   1608 	gboolean ret;
   1609 	gchar *temp;
   1610 	GstStructure *structure;
   1611 	codec_capabilities_t *codec = NULL;
   1612 	int err;
   1613 
   1614 	temp = gst_caps_to_string(caps);
   1615 	GST_DEBUG_OBJECT(self, "configuring device with caps: %s", temp);
   1616 	g_free(temp);
   1617 
   1618 	/* Transport already configured */
   1619 	if (self->transport != NULL)
   1620 		return TRUE;
   1621 
   1622 	structure = gst_caps_get_structure(caps, 0);
   1623 
   1624 	if (gst_structure_has_name(structure, "audio/x-sbc"))
   1625 		codec = (void *) gst_avdtp_find_caps(self, BT_A2DP_SBC_SINK);
   1626 	else if (gst_structure_has_name(structure, "audio/mpeg"))
   1627 		codec = (void *) gst_avdtp_find_caps(self, BT_A2DP_MPEG12_SINK);
   1628 
   1629 	if (codec == NULL) {
   1630 		GST_ERROR_OBJECT(self, "Couldn't parse caps "
   1631 				"to packet configuration");
   1632 		return FALSE;
   1633 	}
   1634 
   1635 	memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
   1636 	open_req->h.type = BT_REQUEST;
   1637 	open_req->h.name = BT_OPEN;
   1638 	open_req->h.length = sizeof(*open_req);
   1639 
   1640 	strncpy(open_req->destination, self->device, 18);
   1641 	open_req->seid = codec->seid;
   1642 	open_req->lock = BT_WRITE_LOCK;
   1643 
   1644 	err = gst_avdtp_sink_audioservice_send(self, &open_req->h);
   1645 	if (err < 0) {
   1646 		GST_ERROR_OBJECT(self, "Error ocurred while sending "
   1647 					"open packet");
   1648 		return FALSE;
   1649 	}
   1650 
   1651 	open_rsp->h.length = sizeof(*open_rsp);
   1652 	err = gst_avdtp_sink_audioservice_expect(self, &open_rsp->h,
   1653 								BT_OPEN);
   1654 	if (err < 0) {
   1655 		GST_ERROR_OBJECT(self, "Error while receiving device "
   1656 					"confirmation");
   1657 		return FALSE;
   1658 	}
   1659 
   1660 	memset(req, 0, sizeof(buf));
   1661 	req->h.type = BT_REQUEST;
   1662 	req->h.name = BT_SET_CONFIGURATION;
   1663 	req->h.length = sizeof(*req);
   1664 	memcpy(&req->codec, codec, sizeof(req->codec));
   1665 
   1666 	if (codec->type == BT_A2DP_SBC_SINK)
   1667 		ret = gst_avdtp_sink_init_sbc_pkt_conf(self, caps,
   1668 				(void *) &req->codec);
   1669 	else
   1670 		ret = gst_avdtp_sink_init_mp3_pkt_conf(self, caps,
   1671 				(void *) &req->codec);
   1672 
   1673 	if (!ret) {
   1674 		GST_ERROR_OBJECT(self, "Couldn't parse caps "
   1675 				"to packet configuration");
   1676 		return FALSE;
   1677 	}
   1678 
   1679 	req->h.length += req->codec.length - sizeof(req->codec);
   1680 	err = gst_avdtp_sink_audioservice_send(self, &req->h);
   1681 	if (err < 0) {
   1682 		GST_ERROR_OBJECT(self, "Error ocurred while sending "
   1683 					"configurarion packet");
   1684 		return FALSE;
   1685 	}
   1686 
   1687 	rsp->h.length = sizeof(*rsp);
   1688 	err = gst_avdtp_sink_audioservice_expect(self, &rsp->h,
   1689 							BT_SET_CONFIGURATION);
   1690 	if (err < 0) {
   1691 		GST_ERROR_OBJECT(self, "Error while receiving device "
   1692 					"confirmation");
   1693 		return FALSE;
   1694 	}
   1695 
   1696 	self->data->link_mtu = rsp->link_mtu;
   1697 
   1698 	return TRUE;
   1699 }
   1700 
   1701 static GstFlowReturn gst_avdtp_sink_preroll(GstBaseSink *basesink,
   1702 					GstBuffer *buffer)
   1703 {
   1704 	GstAvdtpSink *sink = GST_AVDTP_SINK(basesink);
   1705 	gboolean ret;
   1706 
   1707 	GST_AVDTP_SINK_MUTEX_LOCK(sink);
   1708 
   1709 	ret = gst_avdtp_sink_stream_start(sink);
   1710 
   1711 	GST_AVDTP_SINK_MUTEX_UNLOCK(sink);
   1712 
   1713 	if (!ret)
   1714 		return GST_FLOW_ERROR;
   1715 
   1716 	return GST_FLOW_OK;
   1717 }
   1718 
   1719 static GstFlowReturn gst_avdtp_sink_render(GstBaseSink *basesink,
   1720 					GstBuffer *buffer)
   1721 {
   1722 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1723 	ssize_t ret;
   1724 	int fd;
   1725 
   1726 	fd = g_io_channel_unix_get_fd(self->stream);
   1727 
   1728 	ret = write(fd, GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
   1729 	if (ret < 0) {
   1730 		GST_ERROR_OBJECT(self, "Error while writting to socket: %s",
   1731 							strerror(errno));
   1732 		return GST_FLOW_ERROR;
   1733 	}
   1734 
   1735 	return GST_FLOW_OK;
   1736 }
   1737 
   1738 static gboolean gst_avdtp_sink_unlock(GstBaseSink *basesink)
   1739 {
   1740 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1741 
   1742 	if (self->stream != NULL)
   1743 		g_io_channel_flush(self->stream, NULL);
   1744 
   1745 	return TRUE;
   1746 }
   1747 
   1748 static GstFlowReturn gst_avdtp_sink_buffer_alloc(GstBaseSink *basesink,
   1749 				guint64 offset, guint size, GstCaps *caps,
   1750 				GstBuffer **buf)
   1751 {
   1752 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1753 
   1754 	*buf = gst_buffer_new_and_alloc(size);
   1755 	if (!(*buf)) {
   1756 		GST_ERROR_OBJECT(self, "buffer allocation failed");
   1757 		return GST_FLOW_ERROR;
   1758 	}
   1759 
   1760 	gst_buffer_set_caps(*buf, caps);
   1761 
   1762 	GST_BUFFER_OFFSET(*buf) = offset;
   1763 
   1764 	return GST_FLOW_OK;
   1765 }
   1766 
   1767 static void gst_avdtp_sink_class_init(GstAvdtpSinkClass *klass)
   1768 {
   1769 	GObjectClass *object_class = G_OBJECT_CLASS(klass);
   1770 	GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS(klass);
   1771 
   1772 	parent_class = g_type_class_peek_parent(klass);
   1773 
   1774 	object_class->finalize = GST_DEBUG_FUNCPTR(
   1775 					gst_avdtp_sink_finalize);
   1776 	object_class->set_property = GST_DEBUG_FUNCPTR(
   1777 					gst_avdtp_sink_set_property);
   1778 	object_class->get_property = GST_DEBUG_FUNCPTR(
   1779 					gst_avdtp_sink_get_property);
   1780 
   1781 	basesink_class->start = GST_DEBUG_FUNCPTR(gst_avdtp_sink_start);
   1782 	basesink_class->stop = GST_DEBUG_FUNCPTR(gst_avdtp_sink_stop);
   1783 	basesink_class->render = GST_DEBUG_FUNCPTR(
   1784 					gst_avdtp_sink_render);
   1785 	basesink_class->preroll = GST_DEBUG_FUNCPTR(
   1786 					gst_avdtp_sink_preroll);
   1787 	basesink_class->unlock = GST_DEBUG_FUNCPTR(
   1788 					gst_avdtp_sink_unlock);
   1789 	basesink_class->event = GST_DEBUG_FUNCPTR(
   1790 					gst_avdtp_sink_event);
   1791 
   1792 	basesink_class->buffer_alloc =
   1793 		GST_DEBUG_FUNCPTR(gst_avdtp_sink_buffer_alloc);
   1794 
   1795 	g_object_class_install_property(object_class, PROP_DEVICE,
   1796 					g_param_spec_string("device", "Device",
   1797 					"Bluetooth remote device address",
   1798 					NULL, G_PARAM_READWRITE));
   1799 
   1800 	g_object_class_install_property(object_class, PROP_AUTOCONNECT,
   1801 					g_param_spec_boolean("auto-connect",
   1802 					"Auto-connect",
   1803 					"Automatically attempt to connect "
   1804 					"to device", DEFAULT_AUTOCONNECT,
   1805 					G_PARAM_READWRITE));
   1806 
   1807 	g_object_class_install_property(object_class, PROP_TRANSPORT,
   1808 					g_param_spec_string("transport",
   1809 					"Transport",
   1810 					"Use configured transport",
   1811 					NULL, G_PARAM_READWRITE));
   1812 
   1813 	GST_DEBUG_CATEGORY_INIT(avdtp_sink_debug, "avdtpsink", 0,
   1814 				"A2DP headset sink element");
   1815 }
   1816 
   1817 static void gst_avdtp_sink_init(GstAvdtpSink *self,
   1818 			GstAvdtpSinkClass *klass)
   1819 {
   1820 	self->device = NULL;
   1821 	self->transport = NULL;
   1822 	self->data = NULL;
   1823 
   1824 	self->stream = NULL;
   1825 
   1826 	self->dev_caps = NULL;
   1827 
   1828 	self->autoconnect = DEFAULT_AUTOCONNECT;
   1829 
   1830 	self->sink_lock = g_mutex_new();
   1831 
   1832 	/* FIXME this is for not synchronizing with clock, should be tested
   1833 	 * with devices to see the behaviour
   1834 	gst_base_sink_set_sync(GST_BASE_SINK(self), FALSE);
   1835 	*/
   1836 }
   1837 
   1838 static int gst_avdtp_sink_audioservice_send(GstAvdtpSink *self,
   1839 					const bt_audio_msg_header_t *msg)
   1840 {
   1841 	ssize_t written;
   1842 	const char *type, *name;
   1843 	uint16_t length;
   1844 	int fd;
   1845 
   1846 	length = msg->length ? msg->length : BT_SUGGESTED_BUFFER_SIZE;
   1847 
   1848 	fd = g_io_channel_unix_get_fd(self->server);
   1849 
   1850 	written = write(fd, msg, length);
   1851 	if (written < 0) {
   1852 		GST_ERROR_OBJECT(self, "Error sending data to audio service:"
   1853 			" %s", strerror(errno));
   1854 		return -errno;
   1855 	}
   1856 
   1857 	type = bt_audio_strtype(msg->type);
   1858 	name = bt_audio_strname(msg->name);
   1859 
   1860 	GST_DEBUG_OBJECT(self, "sent: %s -> %s", type, name);
   1861 
   1862 	return 0;
   1863 }
   1864 
   1865 static int gst_avdtp_sink_audioservice_recv(GstAvdtpSink *self,
   1866 						bt_audio_msg_header_t *inmsg)
   1867 {
   1868 	ssize_t bytes_read;
   1869 	const char *type, *name;
   1870 	uint16_t length;
   1871 	int fd, err = 0;
   1872 
   1873 	length = inmsg->length ? inmsg->length : BT_SUGGESTED_BUFFER_SIZE;
   1874 
   1875 	fd = g_io_channel_unix_get_fd(self->server);
   1876 
   1877 	bytes_read = read(fd, inmsg, length);
   1878 	if (bytes_read < 0) {
   1879 		GST_ERROR_OBJECT(self, "Error receiving data from "
   1880 				"audio service: %s", strerror(errno));
   1881 		return -errno;
   1882 	}
   1883 
   1884 	type = bt_audio_strtype(inmsg->type);
   1885 	if (!type) {
   1886 		err = -EINVAL;
   1887 		GST_ERROR_OBJECT(self, "Bogus message type %d "
   1888 				"received from audio service",
   1889 				inmsg->type);
   1890 	}
   1891 
   1892 	name = bt_audio_strname(inmsg->name);
   1893 	if (!name) {
   1894 		err = -EINVAL;
   1895 		GST_ERROR_OBJECT(self, "Bogus message name %d "
   1896 				"received from audio service",
   1897 				inmsg->name);
   1898 	}
   1899 
   1900 	if (inmsg->type == BT_ERROR) {
   1901 		bt_audio_error_t *msg = (void *) inmsg;
   1902 		err = -EINVAL;
   1903 		GST_ERROR_OBJECT(self, "%s failed : "
   1904 					"%s(%d)",
   1905 					name,
   1906 					strerror(msg->posix_errno),
   1907 					msg->posix_errno);
   1908 	}
   1909 
   1910 	GST_DEBUG_OBJECT(self, "received: %s <- %s", type, name);
   1911 
   1912 	return err;
   1913 }
   1914 
   1915 static int gst_avdtp_sink_audioservice_expect(GstAvdtpSink *self,
   1916 						bt_audio_msg_header_t *outmsg,
   1917 						guint8 expected_name)
   1918 {
   1919 	int err;
   1920 
   1921 	err = gst_avdtp_sink_audioservice_recv(self, outmsg);
   1922 	if (err < 0)
   1923 		return err;
   1924 
   1925 	if (outmsg->name != expected_name)
   1926 		return -EINVAL;
   1927 
   1928 	return 0;
   1929 }
   1930 
   1931 gboolean gst_avdtp_sink_plugin_init(GstPlugin *plugin)
   1932 {
   1933 	return gst_element_register(plugin, "avdtpsink", GST_RANK_NONE,
   1934 							GST_TYPE_AVDTP_SINK);
   1935 }
   1936 
   1937 
   1938 /* public functions */
   1939 GstCaps *gst_avdtp_sink_get_device_caps(GstAvdtpSink *sink)
   1940 {
   1941 	if (sink->dev_caps == NULL)
   1942 		return NULL;
   1943 
   1944 	return gst_caps_copy(sink->dev_caps);
   1945 }
   1946 
   1947 gboolean gst_avdtp_sink_set_device_caps(GstAvdtpSink *self,
   1948 			GstCaps *caps)
   1949 {
   1950 	gboolean ret;
   1951 
   1952 	GST_DEBUG_OBJECT(self, "setting device caps");
   1953 	GST_AVDTP_SINK_MUTEX_LOCK(self);
   1954 	ret = gst_avdtp_sink_configure(self, caps);
   1955 
   1956 	if (self->stream_caps)
   1957 		gst_caps_unref(self->stream_caps);
   1958 	self->stream_caps = gst_caps_ref(caps);
   1959 
   1960 	GST_AVDTP_SINK_MUTEX_UNLOCK(self);
   1961 
   1962 	return ret;
   1963 }
   1964 
   1965 guint gst_avdtp_sink_get_link_mtu(GstAvdtpSink *sink)
   1966 {
   1967 	return sink->data->link_mtu;
   1968 }
   1969 
   1970 void gst_avdtp_sink_set_device(GstAvdtpSink *self, const gchar *dev)
   1971 {
   1972 	if (self->device != NULL)
   1973 		g_free(self->device);
   1974 
   1975 	GST_LOG_OBJECT(self, "Setting device: %s", dev);
   1976 	self->device = g_strdup(dev);
   1977 }
   1978 
   1979 void gst_avdtp_sink_set_transport(GstAvdtpSink *self, const gchar *trans)
   1980 {
   1981 	if (self->transport != NULL)
   1982 		g_free(self->transport);
   1983 
   1984 	GST_LOG_OBJECT(self, "Setting transport: %s", trans);
   1985 	self->transport = g_strdup(trans);
   1986 }
   1987 
   1988 gchar *gst_avdtp_sink_get_device(GstAvdtpSink *self)
   1989 {
   1990 	return g_strdup(self->device);
   1991 }
   1992 
   1993 gchar *gst_avdtp_sink_get_transport(GstAvdtpSink *self)
   1994 {
   1995 	return g_strdup(self->transport);
   1996 }
   1997 
   1998 void gst_avdtp_sink_set_crc(GstAvdtpSink *self, gboolean crc)
   1999 {
   2000 	gint new_crc;
   2001 
   2002 	new_crc = crc ? CRC_PROTECTED : CRC_UNPROTECTED;
   2003 
   2004 	/* test if we already received a different crc */
   2005 	if (self->mp3_using_crc != -1 && new_crc != self->mp3_using_crc) {
   2006 		GST_WARNING_OBJECT(self, "crc changed during stream");
   2007 		return;
   2008 	}
   2009 	self->mp3_using_crc = new_crc;
   2010 
   2011 }
   2012 
   2013 void gst_avdtp_sink_set_channel_mode(GstAvdtpSink *self,
   2014 			const gchar *mode)
   2015 {
   2016 	gint new_mode;
   2017 
   2018 	new_mode = gst_avdtp_sink_get_channel_mode(mode);
   2019 
   2020 	if (self->channel_mode != -1 && new_mode != self->channel_mode) {
   2021 		GST_WARNING_OBJECT(self, "channel mode changed during stream");
   2022 		return;
   2023 	}
   2024 
   2025 	self->channel_mode = new_mode;
   2026 	if (self->channel_mode == -1)
   2027 		GST_WARNING_OBJECT(self, "Received invalid channel "
   2028 				"mode: %s", mode);
   2029 }
   2030