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 "ipc.h"
     41 #include "rtp.h"
     42 
     43 #include "gstpragma.h"
     44 #include "gstavdtpsink.h"
     45 
     46 GST_DEBUG_CATEGORY_STATIC(avdtp_sink_debug);
     47 #define GST_CAT_DEFAULT avdtp_sink_debug
     48 
     49 #define BUFFER_SIZE 2048
     50 #define TEMPLATE_MAX_BITPOOL 64
     51 #define CRC_PROTECTED 1
     52 #define CRC_UNPROTECTED 0
     53 
     54 #define DEFAULT_AUTOCONNECT TRUE
     55 
     56 #define GST_AVDTP_SINK_MUTEX_LOCK(s) G_STMT_START {	\
     57 		g_mutex_lock(s->sink_lock);		\
     58 	} G_STMT_END
     59 
     60 #define GST_AVDTP_SINK_MUTEX_UNLOCK(s) G_STMT_START {	\
     61 		g_mutex_unlock(s->sink_lock);		\
     62 	} G_STMT_END
     63 
     64 
     65 struct bluetooth_data {
     66 	struct bt_get_capabilities_rsp *caps; /* Bluetooth device caps */
     67 	guint link_mtu;
     68 
     69 	gchar buffer[BUFFER_SIZE];	/* Codec transfer buffer */
     70 };
     71 
     72 #define IS_SBC(n) (strcmp((n), "audio/x-sbc") == 0)
     73 #define IS_MPEG_AUDIO(n) (strcmp((n), "audio/mpeg") == 0)
     74 
     75 enum {
     76 	PROP_0,
     77 	PROP_DEVICE,
     78 	PROP_AUTOCONNECT
     79 };
     80 
     81 GST_BOILERPLATE(GstAvdtpSink, gst_avdtp_sink, GstBaseSink,
     82 			GST_TYPE_BASE_SINK);
     83 
     84 static const GstElementDetails avdtp_sink_details =
     85 	GST_ELEMENT_DETAILS("Bluetooth AVDTP sink",
     86 				"Sink/Audio",
     87 				"Plays audio to an A2DP device",
     88 				"Marcel Holtmann <marcel (at) holtmann.org>");
     89 
     90 static GstStaticPadTemplate avdtp_sink_factory =
     91 	GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
     92 		GST_STATIC_CAPS("application/x-rtp, "
     93 				"media = (string) \"audio\","
     94 				"payload = (int) "
     95 					GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
     96 				"clock-rate = (int) { 16000, 32000, "
     97 					"44100, 48000 }, "
     98 				"encoding-name = (string) \"SBC\"; "
     99 				"application/x-rtp, "
    100 				"media = (string) \"audio\", "
    101 				"payload = (int) "
    102 				GST_RTP_PAYLOAD_MPA_STRING ", "
    103 				"clock-rate = (int) 90000; "
    104 				"application/x-rtp, "
    105 				"media = (string) \"audio\", "
    106 				"payload = (int) "
    107 				GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
    108 				"clock-rate = (int) 90000, "
    109 				"encoding-name = (string) \"MPA\""
    110 				));
    111 
    112 static GIOError gst_avdtp_sink_audioservice_send(GstAvdtpSink *self,
    113 					const bt_audio_msg_header_t *msg);
    114 static GIOError gst_avdtp_sink_audioservice_expect(
    115 				GstAvdtpSink *self,
    116 				bt_audio_msg_header_t *outmsg,
    117 				guint8 expected_name);
    118 
    119 
    120 static void gst_avdtp_sink_base_init(gpointer g_class)
    121 {
    122 	GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
    123 
    124 	gst_element_class_add_pad_template(element_class,
    125 		gst_static_pad_template_get(&avdtp_sink_factory));
    126 
    127 	gst_element_class_set_details(element_class, &avdtp_sink_details);
    128 }
    129 
    130 static gboolean gst_avdtp_sink_stop(GstBaseSink *basesink)
    131 {
    132 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
    133 
    134 	GST_INFO_OBJECT(self, "stop");
    135 
    136 	if (self->watch_id != 0) {
    137 		g_source_remove(self->watch_id);
    138 		self->watch_id = 0;
    139 	}
    140 
    141 	if (self->server) {
    142 		bt_audio_service_close(g_io_channel_unix_get_fd(self->server));
    143 		g_io_channel_unref(self->server);
    144 		self->server = NULL;
    145 	}
    146 
    147 	if (self->stream) {
    148 		g_io_channel_shutdown(self->stream, TRUE, NULL);
    149 		g_io_channel_unref(self->stream);
    150 		self->stream = NULL;
    151 	}
    152 
    153 	if (self->data) {
    154 		g_free(self->data);
    155 		self->data = NULL;
    156 	}
    157 
    158 	if (self->stream_caps) {
    159 		gst_caps_unref(self->stream_caps);
    160 		self->stream_caps = NULL;
    161 	}
    162 
    163 	if (self->dev_caps) {
    164 		gst_caps_unref(self->dev_caps);
    165 		self->dev_caps = NULL;
    166 	}
    167 
    168 	return TRUE;
    169 }
    170 
    171 static void gst_avdtp_sink_finalize(GObject *object)
    172 {
    173 	GstAvdtpSink *self = GST_AVDTP_SINK(object);
    174 
    175 	if (self->data)
    176 		gst_avdtp_sink_stop(GST_BASE_SINK(self));
    177 
    178 	if (self->device)
    179 		g_free(self->device);
    180 
    181 	g_mutex_free(self->sink_lock);
    182 
    183 	G_OBJECT_CLASS(parent_class)->finalize(object);
    184 }
    185 
    186 static void gst_avdtp_sink_set_property(GObject *object, guint prop_id,
    187 					const GValue *value, GParamSpec *pspec)
    188 {
    189 	GstAvdtpSink *sink = GST_AVDTP_SINK(object);
    190 
    191 	switch (prop_id) {
    192 	case PROP_DEVICE:
    193 		if (sink->device)
    194 			g_free(sink->device);
    195 		sink->device = g_value_dup_string(value);
    196 		break;
    197 
    198 	case PROP_AUTOCONNECT:
    199 		sink->autoconnect = g_value_get_boolean(value);
    200 		break;
    201 	default:
    202 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    203 		break;
    204 	}
    205 }
    206 
    207 static void gst_avdtp_sink_get_property(GObject *object, guint prop_id,
    208 					GValue *value, GParamSpec *pspec)
    209 {
    210 	GstAvdtpSink *sink = GST_AVDTP_SINK(object);
    211 
    212 	switch (prop_id) {
    213 	case PROP_DEVICE:
    214 		g_value_set_string(value, sink->device);
    215 		break;
    216 
    217 	case PROP_AUTOCONNECT:
    218 		g_value_set_boolean(value, sink->autoconnect);
    219 		break;
    220 	default:
    221 		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    222 		break;
    223 	}
    224 }
    225 
    226 static gint gst_avdtp_sink_bluetooth_recvmsg_fd(GstAvdtpSink *sink)
    227 {
    228 	int err, ret;
    229 
    230 	ret = bt_audio_service_get_data_fd(
    231 			g_io_channel_unix_get_fd(sink->server));
    232 
    233 	if (ret < 0) {
    234 		err = errno;
    235 		GST_ERROR_OBJECT(sink, "Unable to receive fd: %s (%d)",
    236 				strerror(err), err);
    237 		return -err;
    238 	}
    239 
    240 	sink->stream = g_io_channel_unix_new(ret);
    241 	GST_DEBUG_OBJECT(sink, "stream_fd=%d", ret);
    242 
    243 	return 0;
    244 }
    245 
    246 static codec_capabilities_t *gst_avdtp_find_caps(GstAvdtpSink *sink,
    247 						uint8_t codec_type)
    248 {
    249 	struct bt_get_capabilities_rsp *rsp = sink->data->caps;
    250 	codec_capabilities_t *codec = (void *) rsp->data;
    251 	int bytes_left = rsp->h.length - sizeof(*rsp);
    252 
    253 	while (bytes_left > 0) {
    254 		if ((codec->type == codec_type) &&
    255 				!(codec->lock & BT_WRITE_LOCK))
    256 			break;
    257 
    258 		bytes_left -= codec->length;
    259 		codec = (void *) codec + codec->length;
    260 	}
    261 
    262 	if (bytes_left <= 0)
    263 		return NULL;
    264 
    265 	return codec;
    266 }
    267 
    268 static gboolean gst_avdtp_sink_init_sbc_pkt_conf(GstAvdtpSink *sink,
    269 					GstCaps *caps,
    270 					sbc_capabilities_t *pkt)
    271 {
    272 	sbc_capabilities_t *cfg;
    273 	const GValue *value = NULL;
    274 	const char *pref, *name;
    275 	gint rate, subbands, blocks;
    276 	GstStructure *structure = gst_caps_get_structure(caps, 0);
    277 
    278 	cfg = (void *) gst_avdtp_find_caps(sink, BT_A2DP_SBC_SINK);
    279 	name = gst_structure_get_name(structure);
    280 
    281 	if (!(IS_SBC(name))) {
    282 		GST_ERROR_OBJECT(sink, "Unexpected format %s, "
    283 				"was expecting sbc", name);
    284 		return FALSE;
    285 	}
    286 
    287 	value = gst_structure_get_value(structure, "rate");
    288 	rate = g_value_get_int(value);
    289 	if (rate == 44100)
    290 		cfg->frequency = BT_SBC_SAMPLING_FREQ_44100;
    291 	else if (rate == 48000)
    292 		cfg->frequency = BT_SBC_SAMPLING_FREQ_48000;
    293 	else if (rate == 32000)
    294 		cfg->frequency = BT_SBC_SAMPLING_FREQ_32000;
    295 	else if (rate == 16000)
    296 		cfg->frequency = BT_SBC_SAMPLING_FREQ_16000;
    297 	else {
    298 		GST_ERROR_OBJECT(sink, "Invalid rate while setting caps");
    299 		return FALSE;
    300 	}
    301 
    302 	value = gst_structure_get_value(structure, "mode");
    303 	pref = g_value_get_string(value);
    304 	if (strcmp(pref, "mono") == 0)
    305 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
    306 	else if (strcmp(pref, "dual") == 0)
    307 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
    308 	else if (strcmp(pref, "stereo") == 0)
    309 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
    310 	else if (strcmp(pref, "joint") == 0)
    311 		cfg->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
    312 	else {
    313 		GST_ERROR_OBJECT(sink, "Invalid mode %s", pref);
    314 		return FALSE;
    315 	}
    316 
    317 	value = gst_structure_get_value(structure, "allocation");
    318 	pref = g_value_get_string(value);
    319 	if (strcmp(pref, "loudness") == 0)
    320 		cfg->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
    321 	else if (strcmp(pref, "snr") == 0)
    322 		cfg->allocation_method = BT_A2DP_ALLOCATION_SNR;
    323 	else {
    324 		GST_ERROR_OBJECT(sink, "Invalid allocation: %s", pref);
    325 		return FALSE;
    326 	}
    327 
    328 	value = gst_structure_get_value(structure, "subbands");
    329 	subbands = g_value_get_int(value);
    330 	if (subbands == 8)
    331 		cfg->subbands = BT_A2DP_SUBBANDS_8;
    332 	else if (subbands == 4)
    333 		cfg->subbands = BT_A2DP_SUBBANDS_4;
    334 	else {
    335 		GST_ERROR_OBJECT(sink, "Invalid subbands %d", subbands);
    336 		return FALSE;
    337 	}
    338 
    339 	value = gst_structure_get_value(structure, "blocks");
    340 	blocks = g_value_get_int(value);
    341 	if (blocks == 16)
    342 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_16;
    343 	else if (blocks == 12)
    344 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_12;
    345 	else if (blocks == 8)
    346 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_8;
    347 	else if (blocks == 4)
    348 		cfg->block_length = BT_A2DP_BLOCK_LENGTH_4;
    349 	else {
    350 		GST_ERROR_OBJECT(sink, "Invalid blocks %d", blocks);
    351 		return FALSE;
    352 	}
    353 
    354 	value = gst_structure_get_value(structure, "bitpool");
    355 	cfg->max_bitpool = cfg->min_bitpool = g_value_get_int(value);
    356 
    357 	memcpy(pkt, cfg, sizeof(*pkt));
    358 
    359 	return TRUE;
    360 }
    361 
    362 static gboolean gst_avdtp_sink_conf_recv_stream_fd(
    363 					GstAvdtpSink *self)
    364 {
    365 	struct bluetooth_data *data = self->data;
    366 	gint ret;
    367 	GIOError err;
    368 	GError *gerr = NULL;
    369 	GIOStatus status;
    370 	GIOFlags flags;
    371 	gsize read;
    372 
    373 	ret = gst_avdtp_sink_bluetooth_recvmsg_fd(self);
    374 	if (ret < 0)
    375 		return FALSE;
    376 
    377 	if (!self->stream) {
    378 		GST_ERROR_OBJECT(self, "Error while configuring device: "
    379 				"could not acquire audio socket");
    380 		return FALSE;
    381 	}
    382 
    383 	/* set stream socket to nonblock */
    384 	GST_LOG_OBJECT(self, "setting stream socket to nonblock");
    385 	flags = g_io_channel_get_flags(self->stream);
    386 	flags |= G_IO_FLAG_NONBLOCK;
    387 	status = g_io_channel_set_flags(self->stream, flags, &gerr);
    388 	if (status != G_IO_STATUS_NORMAL) {
    389 		if (gerr)
    390 			GST_WARNING_OBJECT(self, "Error while "
    391 				"setting server socket to nonblock: "
    392 				"%s", gerr->message);
    393 		else
    394 			GST_WARNING_OBJECT(self, "Error while "
    395 					"setting server "
    396 					"socket to nonblock");
    397 	}
    398 
    399 	/* It is possible there is some outstanding
    400 	data in the pipe - we have to empty it */
    401 	GST_LOG_OBJECT(self, "emptying stream pipe");
    402 	while (1) {
    403 		err = g_io_channel_read(self->stream, data->buffer,
    404 					(gsize) data->link_mtu,
    405 					&read);
    406 		if (err != G_IO_ERROR_NONE || read <= 0)
    407 			break;
    408 	}
    409 
    410 	/* set stream socket to block */
    411 	GST_LOG_OBJECT(self, "setting stream socket to block");
    412 	flags = g_io_channel_get_flags(self->stream);
    413 	flags &= ~G_IO_FLAG_NONBLOCK;
    414 	status = g_io_channel_set_flags(self->stream, flags, &gerr);
    415 	if (status != G_IO_STATUS_NORMAL) {
    416 		if (gerr)
    417 			GST_WARNING_OBJECT(self, "Error while "
    418 				"setting server socket to block:"
    419 				"%s", gerr->message);
    420 		else
    421 			GST_WARNING_OBJECT(self, "Error while "
    422 				"setting server "
    423 				"socket to block");
    424 	}
    425 
    426 	memset(data->buffer, 0, sizeof(data->buffer));
    427 
    428 	return TRUE;
    429 }
    430 
    431 static gboolean server_callback(GIOChannel *chan,
    432 					GIOCondition cond, gpointer data)
    433 {
    434 	if (cond & G_IO_HUP || cond & G_IO_NVAL)
    435 		return FALSE;
    436 	else if (cond & G_IO_ERR)
    437 		GST_WARNING_OBJECT(GST_AVDTP_SINK(data),
    438 					"Untreated callback G_IO_ERR");
    439 
    440 	return TRUE;
    441 }
    442 
    443 static GstStructure *gst_avdtp_sink_parse_sbc_caps(
    444 			GstAvdtpSink *self, sbc_capabilities_t *sbc)
    445 {
    446 	GstStructure *structure;
    447 	GValue *value;
    448 	GValue *list;
    449 	gboolean mono, stereo;
    450 
    451 	structure = gst_structure_empty_new("audio/x-sbc");
    452 	value = g_value_init(g_new0(GValue, 1), G_TYPE_STRING);
    453 
    454 	/* mode */
    455 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    456 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
    457 		g_value_set_static_string(value, "mono");
    458 		gst_value_list_prepend_value(list, value);
    459 	}
    460 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) {
    461 		g_value_set_static_string(value, "stereo");
    462 		gst_value_list_prepend_value(list, value);
    463 	}
    464 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) {
    465 		g_value_set_static_string(value, "dual");
    466 		gst_value_list_prepend_value(list, value);
    467 	}
    468 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO) {
    469 		g_value_set_static_string(value, "joint");
    470 		gst_value_list_prepend_value(list, value);
    471 	}
    472 	g_value_unset(value);
    473 	if (list) {
    474 		gst_structure_set_value(structure, "mode", list);
    475 		g_free(list);
    476 		list = NULL;
    477 	}
    478 
    479 	/* subbands */
    480 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    481 	value = g_value_init(value, G_TYPE_INT);
    482 	if (sbc->subbands & BT_A2DP_SUBBANDS_4) {
    483 		g_value_set_int(value, 4);
    484 		gst_value_list_prepend_value(list, value);
    485 	}
    486 	if (sbc->subbands & BT_A2DP_SUBBANDS_8) {
    487 		g_value_set_int(value, 8);
    488 		gst_value_list_prepend_value(list, value);
    489 	}
    490 	g_value_unset(value);
    491 	if (list) {
    492 		gst_structure_set_value(structure, "subbands", list);
    493 		g_free(list);
    494 		list = NULL;
    495 	}
    496 
    497 	/* blocks */
    498 	value = g_value_init(value, G_TYPE_INT);
    499 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    500 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_16) {
    501 		g_value_set_int(value, 16);
    502 		gst_value_list_prepend_value(list, value);
    503 	}
    504 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_12) {
    505 		g_value_set_int(value, 12);
    506 		gst_value_list_prepend_value(list, value);
    507 	}
    508 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_8) {
    509 		g_value_set_int(value, 8);
    510 		gst_value_list_prepend_value(list, value);
    511 	}
    512 	if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_4) {
    513 		g_value_set_int(value, 4);
    514 		gst_value_list_prepend_value(list, value);
    515 	}
    516 	g_value_unset(value);
    517 	if (list) {
    518 		gst_structure_set_value(structure, "blocks", list);
    519 		g_free(list);
    520 		list = NULL;
    521 	}
    522 
    523 	/* allocation */
    524 	g_value_init(value, G_TYPE_STRING);
    525 	list = g_value_init(g_new0(GValue,1), GST_TYPE_LIST);
    526 	if (sbc->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS) {
    527 		g_value_set_static_string(value, "loudness");
    528 		gst_value_list_prepend_value(list, value);
    529 	}
    530 	if (sbc->allocation_method & BT_A2DP_ALLOCATION_SNR) {
    531 		g_value_set_static_string(value, "snr");
    532 		gst_value_list_prepend_value(list, value);
    533 	}
    534 	g_value_unset(value);
    535 	if (list) {
    536 		gst_structure_set_value(structure, "allocation", list);
    537 		g_free(list);
    538 		list = NULL;
    539 	}
    540 
    541 	/* rate */
    542 	g_value_init(value, G_TYPE_INT);
    543 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    544 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_48000) {
    545 		g_value_set_int(value, 48000);
    546 		gst_value_list_prepend_value(list, value);
    547 	}
    548 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_44100) {
    549 		g_value_set_int(value, 44100);
    550 		gst_value_list_prepend_value(list, value);
    551 	}
    552 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_32000) {
    553 		g_value_set_int(value, 32000);
    554 		gst_value_list_prepend_value(list, value);
    555 	}
    556 	if (sbc->frequency & BT_SBC_SAMPLING_FREQ_16000) {
    557 		g_value_set_int(value, 16000);
    558 		gst_value_list_prepend_value(list, value);
    559 	}
    560 	g_value_unset(value);
    561 	if (list) {
    562 		gst_structure_set_value(structure, "rate", list);
    563 		g_free(list);
    564 		list = NULL;
    565 	}
    566 
    567 	/* bitpool */
    568 	value = g_value_init(value, GST_TYPE_INT_RANGE);
    569 	gst_value_set_int_range(value,
    570 			MIN(sbc->min_bitpool, TEMPLATE_MAX_BITPOOL),
    571 			MIN(sbc->max_bitpool, TEMPLATE_MAX_BITPOOL));
    572 	gst_structure_set_value(structure, "bitpool", value);
    573 	g_value_unset(value);
    574 
    575 	/* channels */
    576 	mono = FALSE;
    577 	stereo = FALSE;
    578 	if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
    579 		mono = TRUE;
    580 	if ((sbc->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) ||
    581 			(sbc->channel_mode &
    582 			BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) ||
    583 			(sbc->channel_mode &
    584 			BT_A2DP_CHANNEL_MODE_JOINT_STEREO))
    585 		stereo = TRUE;
    586 
    587 	if (mono && stereo) {
    588 		g_value_init(value, GST_TYPE_INT_RANGE);
    589 		gst_value_set_int_range(value, 1, 2);
    590 	} else {
    591 		g_value_init(value, G_TYPE_INT);
    592 		if (mono)
    593 			g_value_set_int(value, 1);
    594 		else if (stereo)
    595 			g_value_set_int(value, 2);
    596 		else {
    597 			GST_ERROR_OBJECT(self,
    598 				"Unexpected number of channels");
    599 			g_value_set_int(value, 0);
    600 		}
    601 	}
    602 
    603 	gst_structure_set_value(structure, "channels", value);
    604 	g_free(value);
    605 
    606 	return structure;
    607 }
    608 
    609 static GstStructure *gst_avdtp_sink_parse_mpeg_caps(
    610 			GstAvdtpSink *self, mpeg_capabilities_t *mpeg)
    611 {
    612 	GstStructure *structure;
    613 	GValue *value;
    614 	GValue *list;
    615 	gboolean valid_layer = FALSE;
    616 	gboolean mono, stereo;
    617 
    618 	if (!mpeg)
    619 		return NULL;
    620 
    621 	GST_LOG_OBJECT(self, "parsing mpeg caps");
    622 
    623 	structure = gst_structure_empty_new("audio/mpeg");
    624 	value = g_new0(GValue, 1);
    625 	g_value_init(value, G_TYPE_INT);
    626 
    627 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    628 	g_value_set_int(value, 1);
    629 	gst_value_list_prepend_value(list, value);
    630 	g_value_set_int(value, 2);
    631 	gst_value_list_prepend_value(list, value);
    632 	gst_structure_set_value(structure, "mpegversion", list);
    633 	g_free(list);
    634 
    635 	/* layer */
    636 	GST_LOG_OBJECT(self, "setting mpeg layer");
    637 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    638 	if (mpeg->layer & BT_MPEG_LAYER_1) {
    639 		g_value_set_int(value, 1);
    640 		gst_value_list_prepend_value(list, value);
    641 		valid_layer = TRUE;
    642 	}
    643 	if (mpeg->layer & BT_MPEG_LAYER_2) {
    644 		g_value_set_int(value, 2);
    645 		gst_value_list_prepend_value(list, value);
    646 		valid_layer = TRUE;
    647 	}
    648 	if (mpeg->layer & BT_MPEG_LAYER_3) {
    649 		g_value_set_int(value, 3);
    650 		gst_value_list_prepend_value(list, value);
    651 		valid_layer = TRUE;
    652 	}
    653 	if (list) {
    654 		gst_structure_set_value(structure, "layer", list);
    655 		g_free(list);
    656 		list = NULL;
    657 	}
    658 
    659 	if (!valid_layer) {
    660 		gst_structure_free(structure);
    661 		g_free(value);
    662 		return NULL;
    663 	}
    664 
    665 	/* rate */
    666 	GST_LOG_OBJECT(self, "setting mpeg rate");
    667 	list = g_value_init(g_new0(GValue, 1), GST_TYPE_LIST);
    668 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_48000) {
    669 		g_value_set_int(value, 48000);
    670 		gst_value_list_prepend_value(list, value);
    671 	}
    672 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_44100) {
    673 		g_value_set_int(value, 44100);
    674 		gst_value_list_prepend_value(list, value);
    675 	}
    676 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_32000) {
    677 		g_value_set_int(value, 32000);
    678 		gst_value_list_prepend_value(list, value);
    679 	}
    680 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_24000) {
    681 		g_value_set_int(value, 24000);
    682 		gst_value_list_prepend_value(list, value);
    683 	}
    684 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_22050) {
    685 		g_value_set_int(value, 22050);
    686 		gst_value_list_prepend_value(list, value);
    687 	}
    688 	if (mpeg->frequency & BT_MPEG_SAMPLING_FREQ_16000) {
    689 		g_value_set_int(value, 16000);
    690 		gst_value_list_prepend_value(list, value);
    691 	}
    692 	g_value_unset(value);
    693 	if (list) {
    694 		gst_structure_set_value(structure, "rate", list);
    695 		g_free(list);
    696 		list = NULL;
    697 	}
    698 
    699 	/* channels */
    700 	GST_LOG_OBJECT(self, "setting mpeg channels");
    701 	mono = FALSE;
    702 	stereo = FALSE;
    703 	if (mpeg->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
    704 		mono = TRUE;
    705 	if ((mpeg->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) ||
    706 			(mpeg->channel_mode &
    707 			BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) ||
    708 			(mpeg->channel_mode &
    709 			BT_A2DP_CHANNEL_MODE_JOINT_STEREO))
    710 		stereo = TRUE;
    711 
    712 	if (mono && stereo) {
    713 		g_value_init(value, GST_TYPE_INT_RANGE);
    714 		gst_value_set_int_range(value, 1, 2);
    715 	} else {
    716 		g_value_init(value, G_TYPE_INT);
    717 		if (mono)
    718 			g_value_set_int(value, 1);
    719 		else if (stereo)
    720 			g_value_set_int(value, 2);
    721 		else {
    722 			GST_ERROR_OBJECT(self,
    723 				"Unexpected number of channels");
    724 			g_value_set_int(value, 0);
    725 		}
    726 	}
    727 	gst_structure_set_value(structure, "channels", value);
    728 	g_free(value);
    729 
    730 	return structure;
    731 }
    732 
    733 static gboolean gst_avdtp_sink_update_caps(GstAvdtpSink *self)
    734 {
    735 	sbc_capabilities_t *sbc;
    736 	mpeg_capabilities_t *mpeg;
    737 	GstStructure *sbc_structure;
    738 	GstStructure *mpeg_structure;
    739 	gchar *tmp;
    740 
    741 	GST_LOG_OBJECT(self, "updating device caps");
    742 
    743 	sbc = (void *) gst_avdtp_find_caps(self, BT_A2DP_SBC_SINK);
    744 	mpeg = (void *) gst_avdtp_find_caps(self, BT_A2DP_MPEG12_SINK);
    745 
    746 	sbc_structure = gst_avdtp_sink_parse_sbc_caps(self, sbc);
    747 	mpeg_structure = gst_avdtp_sink_parse_mpeg_caps(self, mpeg);
    748 
    749 	if (self->dev_caps != NULL)
    750 		gst_caps_unref(self->dev_caps);
    751 	self->dev_caps = gst_caps_new_full(sbc_structure, NULL);
    752 	if (mpeg_structure != NULL)
    753 		gst_caps_append_structure(self->dev_caps, mpeg_structure);
    754 
    755 	tmp = gst_caps_to_string(self->dev_caps);
    756 	GST_DEBUG_OBJECT(self, "Device capabilities: %s", tmp);
    757 	g_free(tmp);
    758 
    759 	return TRUE;
    760 }
    761 
    762 static gboolean gst_avdtp_sink_get_capabilities(GstAvdtpSink *self)
    763 {
    764 	gchar *buf[BT_SUGGESTED_BUFFER_SIZE];
    765 	struct bt_get_capabilities_req *req = (void *) buf;
    766 	struct bt_get_capabilities_rsp *rsp = (void *) buf;
    767 	GIOError io_error;
    768 
    769 	memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
    770 
    771 	req->h.type = BT_REQUEST;
    772 	req->h.name = BT_GET_CAPABILITIES;
    773 	req->h.length = sizeof(*req);
    774 
    775 	if (self->device == NULL)
    776 		return FALSE;
    777 	strncpy(req->destination, self->device, 18);
    778 	if (self->autoconnect)
    779 		req->flags |= BT_FLAG_AUTOCONNECT;
    780 
    781 	io_error = gst_avdtp_sink_audioservice_send(self, &req->h);
    782 	if (io_error != G_IO_ERROR_NONE) {
    783 		GST_ERROR_OBJECT(self, "Error while asking device caps");
    784 		return FALSE;
    785 	}
    786 
    787 	rsp->h.length = 0;
    788 	io_error = gst_avdtp_sink_audioservice_expect(self,
    789 			&rsp->h, BT_GET_CAPABILITIES);
    790 	if (io_error != G_IO_ERROR_NONE) {
    791 		GST_ERROR_OBJECT(self, "Error while getting device caps");
    792 		return FALSE;
    793 	}
    794 
    795 	self->data->caps = g_malloc0(rsp->h.length);
    796 	memcpy(self->data->caps, rsp, rsp->h.length);
    797 	if (!gst_avdtp_sink_update_caps(self)) {
    798 		GST_WARNING_OBJECT(self, "failed to update capabilities");
    799 		return FALSE;
    800 	}
    801 
    802 	return TRUE;
    803 }
    804 
    805 static gint gst_avdtp_sink_get_channel_mode(const gchar *mode)
    806 {
    807 	if (strcmp(mode, "stereo") == 0)
    808 		return BT_A2DP_CHANNEL_MODE_STEREO;
    809 	else if (strcmp(mode, "joint-stereo") == 0)
    810 		return BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
    811 	else if (strcmp(mode, "dual-channel") == 0)
    812 		return BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
    813 	else if (strcmp(mode, "mono") == 0)
    814 		return BT_A2DP_CHANNEL_MODE_MONO;
    815 	else
    816 		return -1;
    817 }
    818 
    819 static void gst_avdtp_sink_tag(const GstTagList *taglist,
    820 			const gchar *tag, gpointer user_data)
    821 {
    822 	gboolean crc;
    823 	gchar *channel_mode = NULL;
    824 	GstAvdtpSink *self = GST_AVDTP_SINK(user_data);
    825 
    826 	if (strcmp(tag, "has-crc") == 0) {
    827 
    828 		if (!gst_tag_list_get_boolean(taglist, tag, &crc)) {
    829 			GST_WARNING_OBJECT(self, "failed to get crc tag");
    830 			return;
    831 		}
    832 
    833 		gst_avdtp_sink_set_crc(self, crc);
    834 
    835 	} else if (strcmp(tag, "channel-mode") == 0) {
    836 
    837 		if (!gst_tag_list_get_string(taglist, tag, &channel_mode)) {
    838 			GST_WARNING_OBJECT(self,
    839 				"failed to get channel-mode tag");
    840 			return;
    841 		}
    842 
    843 		self->channel_mode = gst_avdtp_sink_get_channel_mode(
    844 					channel_mode);
    845 		if (self->channel_mode == -1)
    846 			GST_WARNING_OBJECT(self, "Received invalid channel "
    847 					"mode: %s", channel_mode);
    848 		g_free(channel_mode);
    849 
    850 	} else
    851 		GST_DEBUG_OBJECT(self, "received unused tag: %s", tag);
    852 }
    853 
    854 static gboolean gst_avdtp_sink_event(GstBaseSink *basesink,
    855 			GstEvent *event)
    856 {
    857 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
    858 	GstTagList *taglist = NULL;
    859 
    860 	if (GST_EVENT_TYPE(event) == GST_EVENT_TAG) {
    861 		/* we check the tags, mp3 has tags that are importants and
    862 		 * are outside caps */
    863 		gst_event_parse_tag(event, &taglist);
    864 		gst_tag_list_foreach(taglist, gst_avdtp_sink_tag, self);
    865 	}
    866 
    867 	return TRUE;
    868 }
    869 
    870 static gboolean gst_avdtp_sink_start(GstBaseSink *basesink)
    871 {
    872 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
    873 	gint sk;
    874 	gint err;
    875 
    876 	GST_INFO_OBJECT(self, "start");
    877 
    878 	self->watch_id = 0;
    879 
    880 	sk = bt_audio_service_open();
    881 	if (sk <= 0) {
    882 		err = errno;
    883 		GST_ERROR_OBJECT(self, "Cannot open connection to bt "
    884 			"audio service: %s %d", strerror(err), err);
    885 		goto failed;
    886 	}
    887 
    888 	self->server = g_io_channel_unix_new(sk);
    889 	self->watch_id = g_io_add_watch(self->server, G_IO_HUP | G_IO_ERR |
    890 					G_IO_NVAL, server_callback, self);
    891 
    892 	self->data = g_new0(struct bluetooth_data, 1);
    893 
    894 	self->stream = NULL;
    895 	self->stream_caps = NULL;
    896 	self->mp3_using_crc = -1;
    897 	self->channel_mode = -1;
    898 
    899 	if (!gst_avdtp_sink_get_capabilities(self)) {
    900 		GST_ERROR_OBJECT(self, "failed to get capabilities "
    901 				"from device");
    902 		goto failed;
    903 	}
    904 
    905 	return TRUE;
    906 
    907 failed:
    908 	bt_audio_service_close(sk);
    909 	return FALSE;
    910 }
    911 
    912 static gboolean gst_avdtp_sink_stream_start(GstAvdtpSink *self)
    913 {
    914 	gchar buf[BT_SUGGESTED_BUFFER_SIZE];
    915 	struct bt_start_stream_req *req = (void *) buf;
    916 	struct bt_start_stream_rsp *rsp = (void *) buf;
    917 	struct bt_new_stream_ind *ind = (void *) buf;
    918 	GIOError io_error;
    919 
    920 	memset(req, 0, sizeof(buf));
    921 	req->h.type = BT_REQUEST;
    922 	req->h.name = BT_START_STREAM;
    923 	req->h.length = sizeof(*req);
    924 
    925 	io_error = gst_avdtp_sink_audioservice_send(self, &req->h);
    926 	if (io_error != G_IO_ERROR_NONE) {
    927 		GST_ERROR_OBJECT(self, "Error ocurred while sending "
    928 					"start packet");
    929 		return FALSE;
    930 	}
    931 
    932 	rsp->h.length = sizeof(*rsp);
    933 	io_error = gst_avdtp_sink_audioservice_expect(self,
    934 			&rsp->h, BT_START_STREAM);
    935 	if (io_error != G_IO_ERROR_NONE) {
    936 		GST_ERROR_OBJECT(self, "Error while stream "
    937 			"start confirmation");
    938 		return FALSE;
    939 	}
    940 
    941 	ind->h.length = sizeof(*ind);
    942 	io_error = gst_avdtp_sink_audioservice_expect(self, &ind->h,
    943 			BT_NEW_STREAM);
    944 	if (io_error != G_IO_ERROR_NONE) {
    945 		GST_ERROR_OBJECT(self, "Error while receiving "
    946 			"stream filedescriptor");
    947 		return FALSE;
    948 	}
    949 
    950 	if (!gst_avdtp_sink_conf_recv_stream_fd(self))
    951 		return FALSE;
    952 
    953 	return TRUE;
    954 }
    955 
    956 static gboolean gst_avdtp_sink_init_mp3_pkt_conf(
    957 		GstAvdtpSink *self, GstCaps *caps,
    958 		mpeg_capabilities_t *pkt)
    959 {
    960 	const GValue *value = NULL;
    961 	gint rate, layer;
    962 	const gchar *name;
    963 	GstStructure *structure = gst_caps_get_structure(caps, 0);
    964 
    965 	name = gst_structure_get_name(structure);
    966 
    967 	if (!(IS_MPEG_AUDIO(name))) {
    968 		GST_ERROR_OBJECT(self, "Unexpected format %s, "
    969 				"was expecting mp3", name);
    970 		return FALSE;
    971 	}
    972 
    973 	/* layer */
    974 	value = gst_structure_get_value(structure, "layer");
    975 	layer = g_value_get_int(value);
    976 	if (layer == 1)
    977 		pkt->layer = BT_MPEG_LAYER_1;
    978 	else if (layer == 2)
    979 		pkt->layer = BT_MPEG_LAYER_2;
    980 	else if (layer == 3)
    981 		pkt->layer = BT_MPEG_LAYER_3;
    982 	else {
    983 		GST_ERROR_OBJECT(self, "Unexpected layer: %d", layer);
    984 		return FALSE;
    985 	}
    986 
    987 	/* crc */
    988 	if (self->mp3_using_crc != -1)
    989 		pkt->crc = self->mp3_using_crc;
    990 	else {
    991 		GST_ERROR_OBJECT(self, "No info about crc was received, "
    992 				" can't proceed");
    993 		return FALSE;
    994 	}
    995 
    996 	/* channel mode */
    997 	if (self->channel_mode != -1)
    998 		pkt->channel_mode = self->channel_mode;
    999 	else {
   1000 		GST_ERROR_OBJECT(self, "No info about channel mode "
   1001 				"received, can't proceed");
   1002 		return FALSE;
   1003 	}
   1004 
   1005 	/* mpf - we will only use the mandatory one */
   1006 	pkt->mpf = 0;
   1007 
   1008 	value = gst_structure_get_value(structure, "rate");
   1009 	rate = g_value_get_int(value);
   1010 	if (rate == 44100)
   1011 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_44100;
   1012 	else if (rate == 48000)
   1013 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_48000;
   1014 	else if (rate == 32000)
   1015 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_32000;
   1016 	else if (rate == 24000)
   1017 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_24000;
   1018 	else if (rate == 22050)
   1019 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_22050;
   1020 	else if (rate == 16000)
   1021 		pkt->frequency = BT_MPEG_SAMPLING_FREQ_16000;
   1022 	else {
   1023 		GST_ERROR_OBJECT(self, "Invalid rate while setting caps");
   1024 		return FALSE;
   1025 	}
   1026 
   1027 	/* vbr - we always say its vbr, we don't have how to know it */
   1028 	pkt->bitrate = 0x8000;
   1029 
   1030 	return TRUE;
   1031 }
   1032 
   1033 static gboolean gst_avdtp_sink_configure(GstAvdtpSink *self,
   1034 			GstCaps *caps)
   1035 {
   1036 	gchar buf[BT_SUGGESTED_BUFFER_SIZE];
   1037 	struct bt_open_req *open_req = (void *) buf;
   1038 	struct bt_open_rsp *open_rsp = (void *) buf;
   1039 	struct bt_set_configuration_req *req = (void *) buf;
   1040 	struct bt_set_configuration_rsp *rsp = (void *) buf;
   1041 	gboolean ret;
   1042 	GIOError io_error;
   1043 	gchar *temp;
   1044 	GstStructure *structure;
   1045 	codec_capabilities_t *codec = NULL;
   1046 
   1047 	temp = gst_caps_to_string(caps);
   1048 	GST_DEBUG_OBJECT(self, "configuring device with caps: %s", temp);
   1049 	g_free(temp);
   1050 
   1051 	structure = gst_caps_get_structure(caps, 0);
   1052 
   1053 	if (gst_structure_has_name(structure, "audio/x-sbc"))
   1054 		codec = (void *) gst_avdtp_find_caps(self, BT_A2DP_SBC_SINK);
   1055 	else if (gst_structure_has_name(structure, "audio/mpeg"))
   1056 		codec = (void *) gst_avdtp_find_caps(self, BT_A2DP_MPEG12_SINK);
   1057 
   1058 	if (codec == NULL) {
   1059 		GST_ERROR_OBJECT(self, "Couldn't parse caps "
   1060 				"to packet configuration");
   1061 		return FALSE;
   1062 	}
   1063 
   1064 	memset(req, 0, BT_SUGGESTED_BUFFER_SIZE);
   1065 	open_req->h.type = BT_REQUEST;
   1066 	open_req->h.name = BT_OPEN;
   1067 	open_req->h.length = sizeof(*open_req);
   1068 
   1069 	strncpy(open_req->destination, self->device, 18);
   1070 	open_req->seid = codec->seid;
   1071 	open_req->lock = BT_WRITE_LOCK;
   1072 
   1073 	io_error = gst_avdtp_sink_audioservice_send(self, &open_req->h);
   1074 	if (io_error != G_IO_ERROR_NONE) {
   1075 		GST_ERROR_OBJECT(self, "Error ocurred while sending "
   1076 					"open packet");
   1077 		return FALSE;
   1078 	}
   1079 
   1080 	open_rsp->h.length = sizeof(*open_rsp);
   1081 	io_error = gst_avdtp_sink_audioservice_expect(self,
   1082 			&open_rsp->h, BT_OPEN);
   1083 	if (io_error != G_IO_ERROR_NONE) {
   1084 		GST_ERROR_OBJECT(self, "Error while receiving device "
   1085 					"confirmation");
   1086 		return FALSE;
   1087 	}
   1088 
   1089 	memset(req, 0, sizeof(buf));
   1090 	req->h.type = BT_REQUEST;
   1091 	req->h.name = BT_SET_CONFIGURATION;
   1092 	req->h.length = sizeof(*req);
   1093 	memcpy(&req->codec, codec, sizeof(req->codec));
   1094 
   1095 	if (codec->type == BT_A2DP_SBC_SINK)
   1096 		ret = gst_avdtp_sink_init_sbc_pkt_conf(self, caps,
   1097 				(void *) &req->codec);
   1098 	else
   1099 		ret = gst_avdtp_sink_init_mp3_pkt_conf(self, caps,
   1100 				(void *) &req->codec);
   1101 
   1102 	if (!ret) {
   1103 		GST_ERROR_OBJECT(self, "Couldn't parse caps "
   1104 				"to packet configuration");
   1105 		return FALSE;
   1106 	}
   1107 
   1108 	req->h.length += req->codec.length - sizeof(req->codec);
   1109 	io_error = gst_avdtp_sink_audioservice_send(self, &req->h);
   1110 	if (io_error != G_IO_ERROR_NONE) {
   1111 		GST_ERROR_OBJECT(self, "Error ocurred while sending "
   1112 					"configurarion packet");
   1113 		return FALSE;
   1114 	}
   1115 
   1116 	rsp->h.length = sizeof(*rsp);
   1117 	io_error = gst_avdtp_sink_audioservice_expect(self,
   1118 			&rsp->h, BT_SET_CONFIGURATION);
   1119 	if (io_error != G_IO_ERROR_NONE) {
   1120 		GST_ERROR_OBJECT(self, "Error while receiving device "
   1121 					"confirmation");
   1122 		return FALSE;
   1123 	}
   1124 
   1125 	self->data->link_mtu = rsp->link_mtu;
   1126 
   1127 	return TRUE;
   1128 }
   1129 
   1130 static GstFlowReturn gst_avdtp_sink_preroll(GstBaseSink *basesink,
   1131 					GstBuffer *buffer)
   1132 {
   1133 	GstAvdtpSink *sink = GST_AVDTP_SINK(basesink);
   1134 	gboolean ret;
   1135 
   1136 	GST_AVDTP_SINK_MUTEX_LOCK(sink);
   1137 
   1138 	ret = gst_avdtp_sink_stream_start(sink);
   1139 
   1140 	GST_AVDTP_SINK_MUTEX_UNLOCK(sink);
   1141 
   1142 	if (!ret)
   1143 		return GST_FLOW_ERROR;
   1144 
   1145 	return GST_FLOW_OK;
   1146 }
   1147 
   1148 static GstFlowReturn gst_avdtp_sink_render(GstBaseSink *basesink,
   1149 					GstBuffer *buffer)
   1150 {
   1151 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1152 	gsize ret;
   1153 	GIOError err;
   1154 
   1155 	err = g_io_channel_write(self->stream,
   1156 				(gchar *) GST_BUFFER_DATA(buffer),
   1157 				(gsize) (GST_BUFFER_SIZE(buffer)), &ret);
   1158 
   1159 	if (err != G_IO_ERROR_NONE) {
   1160 		GST_ERROR_OBJECT(self, "Error while writting to socket: %d %s",
   1161 				errno, strerror(errno));
   1162 		return GST_FLOW_ERROR;
   1163 	}
   1164 
   1165 	return GST_FLOW_OK;
   1166 }
   1167 
   1168 static gboolean gst_avdtp_sink_unlock(GstBaseSink *basesink)
   1169 {
   1170 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1171 
   1172 	if (self->stream != NULL)
   1173 		g_io_channel_flush(self->stream, NULL);
   1174 
   1175 	return TRUE;
   1176 }
   1177 
   1178 static GstFlowReturn gst_avdtp_sink_buffer_alloc(GstBaseSink *basesink,
   1179 				guint64 offset, guint size, GstCaps *caps,
   1180 				GstBuffer **buf)
   1181 {
   1182 	GstAvdtpSink *self = GST_AVDTP_SINK(basesink);
   1183 
   1184 	*buf = gst_buffer_new_and_alloc(size);
   1185 	if (!(*buf)) {
   1186 		GST_ERROR_OBJECT(self, "buffer allocation failed");
   1187 		return GST_FLOW_ERROR;
   1188 	}
   1189 
   1190 	gst_buffer_set_caps(*buf, caps);
   1191 
   1192 	GST_BUFFER_OFFSET(*buf) = offset;
   1193 
   1194 	return GST_FLOW_OK;
   1195 }
   1196 
   1197 static void gst_avdtp_sink_class_init(GstAvdtpSinkClass *klass)
   1198 {
   1199 	GObjectClass *object_class = G_OBJECT_CLASS(klass);
   1200 	GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS(klass);
   1201 
   1202 	parent_class = g_type_class_peek_parent(klass);
   1203 
   1204 	object_class->finalize = GST_DEBUG_FUNCPTR(
   1205 					gst_avdtp_sink_finalize);
   1206 	object_class->set_property = GST_DEBUG_FUNCPTR(
   1207 					gst_avdtp_sink_set_property);
   1208 	object_class->get_property = GST_DEBUG_FUNCPTR(
   1209 					gst_avdtp_sink_get_property);
   1210 
   1211 	basesink_class->start = GST_DEBUG_FUNCPTR(gst_avdtp_sink_start);
   1212 	basesink_class->stop = GST_DEBUG_FUNCPTR(gst_avdtp_sink_stop);
   1213 	basesink_class->render = GST_DEBUG_FUNCPTR(
   1214 					gst_avdtp_sink_render);
   1215 	basesink_class->preroll = GST_DEBUG_FUNCPTR(
   1216 					gst_avdtp_sink_preroll);
   1217 	basesink_class->unlock = GST_DEBUG_FUNCPTR(
   1218 					gst_avdtp_sink_unlock);
   1219 	basesink_class->event = GST_DEBUG_FUNCPTR(
   1220 					gst_avdtp_sink_event);
   1221 
   1222 	basesink_class->buffer_alloc =
   1223 		GST_DEBUG_FUNCPTR(gst_avdtp_sink_buffer_alloc);
   1224 
   1225 	g_object_class_install_property(object_class, PROP_DEVICE,
   1226 					g_param_spec_string("device", "Device",
   1227 					"Bluetooth remote device address",
   1228 					NULL, G_PARAM_READWRITE));
   1229 
   1230 	g_object_class_install_property(object_class, PROP_AUTOCONNECT,
   1231 					g_param_spec_boolean("auto-connect",
   1232 					"Auto-connect",
   1233 					"Automatically attempt to connect "
   1234 					"to device", DEFAULT_AUTOCONNECT,
   1235 					G_PARAM_READWRITE));
   1236 
   1237 	GST_DEBUG_CATEGORY_INIT(avdtp_sink_debug, "avdtpsink", 0,
   1238 				"A2DP headset sink element");
   1239 }
   1240 
   1241 static void gst_avdtp_sink_init(GstAvdtpSink *self,
   1242 			GstAvdtpSinkClass *klass)
   1243 {
   1244 	self->device = NULL;
   1245 	self->data = NULL;
   1246 
   1247 	self->stream = NULL;
   1248 
   1249 	self->dev_caps = NULL;
   1250 
   1251 	self->autoconnect = DEFAULT_AUTOCONNECT;
   1252 
   1253 	self->sink_lock = g_mutex_new();
   1254 
   1255 	/* FIXME this is for not synchronizing with clock, should be tested
   1256 	 * with devices to see the behaviour
   1257 	gst_base_sink_set_sync(GST_BASE_SINK(self), FALSE);
   1258 	*/
   1259 }
   1260 
   1261 static GIOError gst_avdtp_sink_audioservice_send(
   1262 					GstAvdtpSink *self,
   1263 					const bt_audio_msg_header_t *msg)
   1264 {
   1265 	GIOError error;
   1266 	gsize written;
   1267 	const char *type, *name;
   1268 	uint16_t length;
   1269 
   1270 	length = msg->length ? msg->length : BT_SUGGESTED_BUFFER_SIZE;
   1271 
   1272 	error = g_io_channel_write(self->server, (const gchar *) msg, length,
   1273 								&written);
   1274 	if (error != G_IO_ERROR_NONE)
   1275 		GST_ERROR_OBJECT(self, "Error sending data to audio service:"
   1276 			" %s(%d)", strerror(errno), errno);
   1277 
   1278 	type = bt_audio_strtype(msg->type);
   1279 	name = bt_audio_strname(msg->name);
   1280 
   1281 	GST_DEBUG_OBJECT(self, "sent: %s -> %s", type, name);
   1282 
   1283 	return error;
   1284 }
   1285 
   1286 static GIOError gst_avdtp_sink_audioservice_recv(
   1287 					GstAvdtpSink *self,
   1288 					bt_audio_msg_header_t *inmsg)
   1289 {
   1290 	GIOError status;
   1291 	gsize bytes_read;
   1292 	const char *type, *name;
   1293 	uint16_t length;
   1294 
   1295 	length = inmsg->length ? inmsg->length : BT_SUGGESTED_BUFFER_SIZE;
   1296 
   1297 	status = g_io_channel_read(self->server, (gchar *) inmsg, length,
   1298 								&bytes_read);
   1299 	if (status != G_IO_ERROR_NONE) {
   1300 		GST_ERROR_OBJECT(self, "Error receiving data from "
   1301 				"audio service");
   1302 		return status;
   1303 	}
   1304 
   1305 	type = bt_audio_strtype(inmsg->type);
   1306 	if (!type) {
   1307 		status = G_IO_ERROR_INVAL;
   1308 		GST_ERROR_OBJECT(self, "Bogus message type %d "
   1309 				"received from audio service",
   1310 				inmsg->type);
   1311 	}
   1312 
   1313 	name = bt_audio_strname(inmsg->name);
   1314 	if (!name) {
   1315 		status = G_IO_ERROR_INVAL;
   1316 		GST_ERROR_OBJECT(self, "Bogus message name %d "
   1317 				"received from audio service",
   1318 				inmsg->name);
   1319 	}
   1320 
   1321 	if (inmsg->type == BT_ERROR) {
   1322 		bt_audio_error_t *err = (void *) inmsg;
   1323 		status = G_IO_ERROR_INVAL;
   1324 		GST_ERROR_OBJECT(self, "%s failed : "
   1325 					"%s(%d)",
   1326 					name,
   1327 					strerror(err->posix_errno),
   1328 					err->posix_errno);
   1329 	}
   1330 
   1331 	GST_DEBUG_OBJECT(self, "received: %s <- %s", type, name);
   1332 
   1333 	return status;
   1334 }
   1335 
   1336 static GIOError gst_avdtp_sink_audioservice_expect(
   1337 			GstAvdtpSink *self, bt_audio_msg_header_t *outmsg,
   1338 			guint8 expected_name)
   1339 {
   1340 	GIOError status;
   1341 
   1342 	status = gst_avdtp_sink_audioservice_recv(self, outmsg);
   1343 	if (status != G_IO_ERROR_NONE)
   1344 		return status;
   1345 
   1346 	if (outmsg->name != expected_name)
   1347 		status = G_IO_ERROR_INVAL;
   1348 
   1349 	return status;
   1350 }
   1351 
   1352 gboolean gst_avdtp_sink_plugin_init(GstPlugin *plugin)
   1353 {
   1354 	return gst_element_register(plugin, "avdtpsink", GST_RANK_NONE,
   1355 							GST_TYPE_AVDTP_SINK);
   1356 }
   1357 
   1358 
   1359 /* public functions */
   1360 GstCaps *gst_avdtp_sink_get_device_caps(GstAvdtpSink *sink)
   1361 {
   1362 	if (sink->dev_caps == NULL)
   1363 		return NULL;
   1364 
   1365 	return gst_caps_copy(sink->dev_caps);
   1366 }
   1367 
   1368 gboolean gst_avdtp_sink_set_device_caps(GstAvdtpSink *self,
   1369 			GstCaps *caps)
   1370 {
   1371 	gboolean ret;
   1372 
   1373 	GST_DEBUG_OBJECT(self, "setting device caps");
   1374 	GST_AVDTP_SINK_MUTEX_LOCK(self);
   1375 	ret = gst_avdtp_sink_configure(self, caps);
   1376 
   1377 	if (self->stream_caps)
   1378 		gst_caps_unref(self->stream_caps);
   1379 	self->stream_caps = gst_caps_ref(caps);
   1380 
   1381 	GST_AVDTP_SINK_MUTEX_UNLOCK(self);
   1382 
   1383 	return ret;
   1384 }
   1385 
   1386 guint gst_avdtp_sink_get_link_mtu(GstAvdtpSink *sink)
   1387 {
   1388 	return sink->data->link_mtu;
   1389 }
   1390 
   1391 void gst_avdtp_sink_set_device(GstAvdtpSink *self, const gchar *dev)
   1392 {
   1393 	if (self->device != NULL)
   1394 		g_free(self->device);
   1395 
   1396 	GST_LOG_OBJECT(self, "Setting device: %s", dev);
   1397 	self->device = g_strdup(dev);
   1398 }
   1399 
   1400 gchar *gst_avdtp_sink_get_device(GstAvdtpSink *self)
   1401 {
   1402 	return g_strdup(self->device);
   1403 }
   1404 
   1405 void gst_avdtp_sink_set_crc(GstAvdtpSink *self, gboolean crc)
   1406 {
   1407 	gint new_crc;
   1408 
   1409 	new_crc = crc ? CRC_PROTECTED : CRC_UNPROTECTED;
   1410 
   1411 	/* test if we already received a different crc */
   1412 	if (self->mp3_using_crc != -1 && new_crc != self->mp3_using_crc) {
   1413 		GST_WARNING_OBJECT(self, "crc changed during stream");
   1414 		return;
   1415 	}
   1416 	self->mp3_using_crc = new_crc;
   1417 
   1418 }
   1419 
   1420 void gst_avdtp_sink_set_channel_mode(GstAvdtpSink *self,
   1421 			const gchar *mode)
   1422 {
   1423 	gint new_mode;
   1424 
   1425 	new_mode = gst_avdtp_sink_get_channel_mode(mode);
   1426 
   1427 	if (self->channel_mode != -1 && new_mode != self->channel_mode) {
   1428 		GST_WARNING_OBJECT(self, "channel mode changed during stream");
   1429 		return;
   1430 	}
   1431 
   1432 	self->channel_mode = new_mode;
   1433 	if (self->channel_mode == -1)
   1434 		GST_WARNING_OBJECT(self, "Received invalid channel "
   1435 				"mode: %s", mode);
   1436 }
   1437