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