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