• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6  *  Copyright (C) 2012 Collabora Ltd.
7  *
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public
11  *  License as published by the Free Software Foundation; either
12  *  version 2.1 of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <string.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 
36 #include <bluetooth/bluetooth.h>
37 #include "a2dp-codecs.h"
38 
39 #include <gio/gunixfdlist.h>
40 #include <gst/gst.h>
41 #include "gstavdtputil.h"
42 #include "bluez.h"
43 
44 #define TEMPLATE_MAX_BITPOOL 64
45 
46 GST_DEBUG_CATEGORY_EXTERN (avdtp_debug);
47 #define GST_CAT_DEFAULT avdtp_debug
48 
49 static void gst_avdtp_connection_transport_release (GstAvdtpConnection * conn);
50 
51 static gboolean
on_state_change(BluezMediaTransport1 * proxy,GParamSpec * pspec,GstAvdtpConnection * conn)52 on_state_change (BluezMediaTransport1 * proxy, GParamSpec * pspec,
53     GstAvdtpConnection * conn)
54 {
55   const gchar *newstate;
56   gboolean is_idle;
57 
58   newstate = bluez_media_transport1_get_state (proxy);
59   is_idle = g_str_equal (newstate, "idle");
60 
61   if (!conn->data.is_acquired && !is_idle) {
62     GST_DEBUG ("Re-acquiring connection");
63     gst_avdtp_connection_acquire (conn, TRUE);
64 
65   } else if (is_idle) {
66     /* We don't know if we need to release the transport -- that may have been
67      * done for us by bluez already! Or not ... so release it just in case, but
68      * mark its stale beforehand to suppress any errors. */
69     GST_DEBUG ("Marking connection stale");
70     conn->data.is_acquired = FALSE;
71     gst_avdtp_connection_transport_release (conn);
72 
73   } else
74     GST_DEBUG ("State is %s, acquired is %s", newstate,
75         conn->data.is_acquired ? "true" : "false");
76 
77   return TRUE;
78 }
79 
80 gboolean
gst_avdtp_connection_acquire(GstAvdtpConnection * conn,gboolean use_try)81 gst_avdtp_connection_acquire (GstAvdtpConnection * conn, gboolean use_try)
82 {
83   GVariant *handle = NULL;
84   GUnixFDList *fd_list = NULL;
85   GError *err = NULL;
86   int fd;
87   uint16_t imtu, omtu;
88 
89   if (conn->transport == NULL) {
90     GST_ERROR ("No transport specified");
91     return FALSE;
92   }
93 
94   if (conn->data.conn == NULL) {
95     conn->data.conn =
96         bluez_media_transport1_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
97         G_DBUS_PROXY_FLAGS_NONE, "org.bluez", conn->transport, NULL, &err);
98 
99     if (conn->data.conn == NULL) {
100       GST_ERROR ("Failed to create proxy for media transport: %s",
101           err && err->message ? err->message : "Unknown error");
102       g_clear_error (&err);
103       return FALSE;
104     }
105 
106     g_signal_connect (conn->data.conn, "notify::state",
107         G_CALLBACK (on_state_change), conn);
108   }
109 
110   if (conn->data.is_acquired) {
111     GST_INFO ("Transport is already acquired");
112     return TRUE;
113   }
114 
115   if (use_try) {
116     if (!bluez_media_transport1_call_try_acquire_sync (conn->data.conn,
117             NULL, &handle, &imtu, &omtu, &fd_list, NULL, &err))
118       goto fail;
119   } else {
120     if (!bluez_media_transport1_call_acquire_sync (conn->data.conn,
121             NULL, &handle, &imtu, &omtu, &fd_list, NULL, &err))
122       goto fail;
123   }
124 
125   fd = g_unix_fd_list_get (fd_list, g_variant_get_handle (handle), &err);
126   if (fd < 0)
127     goto fail;
128 
129   g_variant_unref (handle);
130   g_object_unref (fd_list);
131   conn->stream = g_io_channel_unix_new (fd);
132   g_io_channel_set_encoding (conn->stream, NULL, NULL);
133   g_io_channel_set_close_on_unref (conn->stream, TRUE);
134   conn->data.link_mtu = omtu;
135   conn->data.is_acquired = TRUE;
136 
137   return TRUE;
138 
139 fail:
140   GST_ERROR ("Failed to %s transport stream: %s", use_try ? "try_acquire" :
141       "acquire", err && err->message ? err->message : "unknown error");
142 
143   g_clear_error (&err);
144   if (handle)
145     g_variant_unref (handle);
146 
147   conn->data.is_acquired = FALSE;
148   return FALSE;
149 }
150 
151 static void
gst_avdtp_connection_transport_release(GstAvdtpConnection * conn)152 gst_avdtp_connection_transport_release (GstAvdtpConnection * conn)
153 {
154   GError *err = NULL;
155 
156   if (!bluez_media_transport1_call_release_sync (conn->data.conn, NULL, &err)) {
157     /* We don't care about errors if the transport was already marked stale */
158     if (!conn->data.is_acquired) {
159       g_clear_error (&err);
160       return;
161     }
162 
163     GST_ERROR ("Failed to release transport stream: %s", err->message ?
164         err->message : "unknown error");
165     g_clear_error (&err);
166   }
167   conn->data.is_acquired = FALSE;
168 }
169 
170 void
gst_avdtp_connection_release(GstAvdtpConnection * conn)171 gst_avdtp_connection_release (GstAvdtpConnection * conn)
172 {
173   if (conn->stream) {
174     g_io_channel_shutdown (conn->stream, TRUE, NULL);
175     g_io_channel_unref (conn->stream);
176     conn->stream = NULL;
177   }
178 
179   if (conn->data.uuid) {
180     g_free (conn->data.uuid);
181     conn->data.uuid = NULL;
182   }
183 
184   if (conn->data.config) {
185     g_free (conn->data.config);
186     conn->data.config = NULL;
187   }
188 
189   if (conn->data.conn) {
190     if (conn->transport)
191       gst_avdtp_connection_transport_release (conn);
192 
193     g_clear_object (&conn->data.conn);
194   }
195 }
196 
197 void
gst_avdtp_connection_reset(GstAvdtpConnection * conn)198 gst_avdtp_connection_reset (GstAvdtpConnection * conn)
199 {
200   gst_avdtp_connection_release (conn);
201 
202   if (conn->device) {
203     g_free (conn->device);
204     conn->device = NULL;
205   }
206 
207   if (conn->transport) {
208     g_free (conn->transport);
209     conn->transport = NULL;
210   }
211 }
212 
213 void
gst_avdtp_connection_set_device(GstAvdtpConnection * conn,const char * device)214 gst_avdtp_connection_set_device (GstAvdtpConnection * conn, const char *device)
215 {
216   g_free (conn->device);
217 
218   conn->device = g_strdup (device);
219 }
220 
221 void
gst_avdtp_connection_set_transport(GstAvdtpConnection * conn,const char * transport)222 gst_avdtp_connection_set_transport (GstAvdtpConnection * conn,
223     const char *transport)
224 {
225   g_free (conn->transport);
226 
227   conn->transport = g_strdup (transport);
228 }
229 
230 gboolean
gst_avdtp_connection_get_properties(GstAvdtpConnection * conn)231 gst_avdtp_connection_get_properties (GstAvdtpConnection * conn)
232 {
233   GVariant *var;
234 
235   conn->data.codec = bluez_media_transport1_get_codec (conn->data.conn);
236 
237   conn->data.uuid = bluez_media_transport1_dup_uuid (conn->data.conn);
238 
239   var = bluez_media_transport1_dup_configuration (conn->data.conn);
240   conn->data.config_size = g_variant_get_size (var);
241   conn->data.config = g_new0 (guint8, conn->data.config_size);
242   g_variant_store (var, conn->data.config);
243   g_variant_unref (var);
244 
245   return TRUE;
246 }
247 
248 static GstStructure *
gst_avdtp_util_parse_sbc_raw(void * config)249 gst_avdtp_util_parse_sbc_raw (void *config)
250 {
251   a2dp_sbc_t *sbc = (a2dp_sbc_t *) config;
252   GstStructure *structure;
253   GValue *value;
254   GValue *list;
255   gboolean mono, stereo;
256 
257   structure = gst_structure_new_empty ("audio/x-sbc");
258   value = g_value_init (g_new0 (GValue, 1), G_TYPE_STRING);
259   list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
260 
261   /* mode */
262   if (sbc->channel_mode & SBC_CHANNEL_MODE_MONO) {
263     g_value_set_static_string (value, "mono");
264     gst_value_list_prepend_value (list, value);
265   }
266   if (sbc->channel_mode & SBC_CHANNEL_MODE_STEREO) {
267     g_value_set_static_string (value, "stereo");
268     gst_value_list_prepend_value (list, value);
269   }
270   if (sbc->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL) {
271     g_value_set_static_string (value, "dual");
272     gst_value_list_prepend_value (list, value);
273   }
274   if (sbc->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO) {
275     g_value_set_static_string (value, "joint");
276     gst_value_list_prepend_value (list, value);
277   }
278   if (gst_value_list_get_size (list) == 1)
279     gst_structure_set_value (structure, "channel-mode", value);
280   else
281     gst_structure_take_value (structure, "channel-mode", list);
282 
283   g_value_unset (value);
284   g_value_reset (list);
285 
286   /* subbands */
287   value = g_value_init (value, G_TYPE_INT);
288   if (sbc->subbands & SBC_SUBBANDS_4) {
289     g_value_set_int (value, 4);
290     gst_value_list_prepend_value (list, value);
291   }
292   if (sbc->subbands & SBC_SUBBANDS_8) {
293     g_value_set_int (value, 8);
294     gst_value_list_prepend_value (list, value);
295   }
296   if (gst_value_list_get_size (list) == 1)
297     gst_structure_set_value (structure, "subbands", value);
298   else
299     gst_structure_take_value (structure, "subbands", list);
300 
301   g_value_unset (value);
302   g_value_reset (list);
303 
304   /* blocks */
305   value = g_value_init (value, G_TYPE_INT);
306   if (sbc->block_length & SBC_BLOCK_LENGTH_16) {
307     g_value_set_int (value, 16);
308     gst_value_list_prepend_value (list, value);
309   }
310   if (sbc->block_length & SBC_BLOCK_LENGTH_12) {
311     g_value_set_int (value, 12);
312     gst_value_list_prepend_value (list, value);
313   }
314   if (sbc->block_length & SBC_BLOCK_LENGTH_8) {
315     g_value_set_int (value, 8);
316     gst_value_list_prepend_value (list, value);
317   }
318   if (sbc->block_length & SBC_BLOCK_LENGTH_4) {
319     g_value_set_int (value, 4);
320     gst_value_list_prepend_value (list, value);
321   }
322   if (gst_value_list_get_size (list) == 1)
323     gst_structure_set_value (structure, "blocks", value);
324   else
325     gst_structure_take_value (structure, "blocks", list);
326 
327   g_value_unset (value);
328   g_value_reset (list);
329 
330   /* allocation */
331   g_value_init (value, G_TYPE_STRING);
332   if (sbc->allocation_method & SBC_ALLOCATION_LOUDNESS) {
333     g_value_set_static_string (value, "loudness");
334     gst_value_list_prepend_value (list, value);
335   }
336   if (sbc->allocation_method & SBC_ALLOCATION_SNR) {
337     g_value_set_static_string (value, "snr");
338     gst_value_list_prepend_value (list, value);
339   }
340   if (gst_value_list_get_size (list) == 1)
341     gst_structure_set_value (structure, "allocation-method", value);
342   else
343     gst_structure_take_value (structure, "allocation-method", list);
344 
345   g_value_unset (value);
346   g_value_reset (list);
347 
348   /* rate */
349   g_value_init (value, G_TYPE_INT);
350   if (sbc->frequency & SBC_SAMPLING_FREQ_48000) {
351     g_value_set_int (value, 48000);
352     gst_value_list_prepend_value (list, value);
353   }
354   if (sbc->frequency & SBC_SAMPLING_FREQ_44100) {
355     g_value_set_int (value, 44100);
356     gst_value_list_prepend_value (list, value);
357   }
358   if (sbc->frequency & SBC_SAMPLING_FREQ_32000) {
359     g_value_set_int (value, 32000);
360     gst_value_list_prepend_value (list, value);
361   }
362   if (sbc->frequency & SBC_SAMPLING_FREQ_16000) {
363     g_value_set_int (value, 16000);
364     gst_value_list_prepend_value (list, value);
365   }
366   if (gst_value_list_get_size (list) == 1)
367     gst_structure_set_value (structure, "rate", value);
368   else
369     gst_structure_take_value (structure, "rate", list);
370 
371   g_value_unset (value);
372   g_value_reset (list);
373 
374   /* bitpool */
375   value = g_value_init (value, GST_TYPE_INT_RANGE);
376   gst_value_set_int_range (value,
377       MIN (sbc->min_bitpool, TEMPLATE_MAX_BITPOOL),
378       MIN (sbc->max_bitpool, TEMPLATE_MAX_BITPOOL));
379   gst_structure_set_value (structure, "bitpool", value);
380   g_value_unset (value);
381 
382   /* channels */
383   mono = FALSE;
384   stereo = FALSE;
385   if (sbc->channel_mode & SBC_CHANNEL_MODE_MONO)
386     mono = TRUE;
387   if ((sbc->channel_mode & SBC_CHANNEL_MODE_STEREO) ||
388       (sbc->channel_mode &
389           SBC_CHANNEL_MODE_DUAL_CHANNEL) ||
390       (sbc->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO))
391     stereo = TRUE;
392 
393   if (mono && stereo) {
394     g_value_init (value, GST_TYPE_INT_RANGE);
395     gst_value_set_int_range (value, 1, 2);
396   } else {
397     g_value_init (value, G_TYPE_INT);
398     if (mono)
399       g_value_set_int (value, 1);
400     else if (stereo)
401       g_value_set_int (value, 2);
402     else {
403       GST_ERROR ("Unexpected number of channels");
404       g_value_set_int (value, 0);
405     }
406   }
407 
408   gst_structure_set_value (structure, "channels", value);
409 
410   g_value_unset (value);
411   g_free (value);
412   g_value_unset (list);
413   g_free (list);
414 
415   return structure;
416 }
417 
418 static GstStructure *
gst_avdtp_util_parse_mpeg_raw(void * config)419 gst_avdtp_util_parse_mpeg_raw (void *config)
420 {
421   a2dp_mpeg_t *mpeg = (a2dp_mpeg_t *) config;
422   GstStructure *structure;
423   GValue *value;
424   GValue *list;
425   gboolean valid_layer = FALSE;
426   gboolean mono, stereo;
427 
428   structure = gst_structure_new_empty ("audio/mpeg");
429   value = g_new0 (GValue, 1);
430   g_value_init (value, G_TYPE_INT);
431 
432   list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
433   g_value_set_int (value, 1);
434   gst_value_list_prepend_value (list, value);
435   g_value_set_int (value, 2);
436   gst_value_list_prepend_value (list, value);
437   gst_structure_set_value (structure, "mpegversion", list);
438   g_free (list);
439 
440   /* layer */
441   list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
442   if (mpeg->layer & MPEG_LAYER_MP1) {
443     g_value_set_int (value, 1);
444     gst_value_list_prepend_value (list, value);
445     valid_layer = TRUE;
446   }
447   if (mpeg->layer & MPEG_LAYER_MP2) {
448     g_value_set_int (value, 2);
449     gst_value_list_prepend_value (list, value);
450     valid_layer = TRUE;
451   }
452   if (mpeg->layer & MPEG_LAYER_MP3) {
453     g_value_set_int (value, 3);
454     gst_value_list_prepend_value (list, value);
455     valid_layer = TRUE;
456   }
457   if (list) {
458     if (gst_value_list_get_size (list) == 1)
459       gst_structure_set_value (structure, "layer", value);
460     else
461       gst_structure_set_value (structure, "layer", list);
462     g_free (list);
463     list = NULL;
464   }
465 
466   if (!valid_layer) {
467     gst_structure_free (structure);
468     g_free (value);
469     return NULL;
470   }
471 
472   /* rate */
473   list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
474   if (mpeg->frequency & MPEG_SAMPLING_FREQ_48000) {
475     g_value_set_int (value, 48000);
476     gst_value_list_prepend_value (list, value);
477   }
478   if (mpeg->frequency & MPEG_SAMPLING_FREQ_44100) {
479     g_value_set_int (value, 44100);
480     gst_value_list_prepend_value (list, value);
481   }
482   if (mpeg->frequency & MPEG_SAMPLING_FREQ_32000) {
483     g_value_set_int (value, 32000);
484     gst_value_list_prepend_value (list, value);
485   }
486   if (mpeg->frequency & MPEG_SAMPLING_FREQ_24000) {
487     g_value_set_int (value, 24000);
488     gst_value_list_prepend_value (list, value);
489   }
490   if (mpeg->frequency & MPEG_SAMPLING_FREQ_22050) {
491     g_value_set_int (value, 22050);
492     gst_value_list_prepend_value (list, value);
493   }
494   if (mpeg->frequency & MPEG_SAMPLING_FREQ_16000) {
495     g_value_set_int (value, 16000);
496     gst_value_list_prepend_value (list, value);
497   }
498   g_value_unset (value);
499   if (list) {
500     if (gst_value_list_get_size (list) == 1)
501       gst_structure_set_value (structure, "rate", value);
502     else
503       gst_structure_set_value (structure, "rate", list);
504     g_free (list);
505     list = NULL;
506   }
507 
508   /* channels */
509   mono = FALSE;
510   stereo = FALSE;
511   if (mpeg->channel_mode & MPEG_CHANNEL_MODE_MONO)
512     mono = TRUE;
513   if ((mpeg->channel_mode & MPEG_CHANNEL_MODE_STEREO) ||
514       (mpeg->channel_mode &
515           MPEG_CHANNEL_MODE_DUAL_CHANNEL) ||
516       (mpeg->channel_mode & MPEG_CHANNEL_MODE_JOINT_STEREO))
517     stereo = TRUE;
518 
519   if (mono && stereo) {
520     g_value_init (value, GST_TYPE_INT_RANGE);
521     gst_value_set_int_range (value, 1, 2);
522   } else {
523     g_value_init (value, G_TYPE_INT);
524     if (mono)
525       g_value_set_int (value, 1);
526     else if (stereo)
527       g_value_set_int (value, 2);
528     else {
529       GST_ERROR ("Unexpected number of channels");
530       g_value_set_int (value, 0);
531     }
532   }
533   gst_structure_set_value (structure, "channels", value);
534   g_free (value);
535 
536   return structure;
537 }
538 
539 static GstStructure *
gst_avdtp_util_parse_aac_raw(void * config)540 gst_avdtp_util_parse_aac_raw (void *config)
541 {
542   GstStructure *structure;
543   GValue value = G_VALUE_INIT;
544   GValue value_str = G_VALUE_INIT;
545   GValue list = G_VALUE_INIT;
546   a2dp_aac_t aac_local = { 0 };
547   a2dp_aac_t *aac = &aac_local;
548 
549 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
550   uint8_t *raw = (uint8_t *) config;
551   aac->object_type = raw[0];
552   aac->frequency = (raw[1] << 4) | ((raw[2] & 0xFF) >> 4);
553   aac->channels = (raw[2] >> 2) & 0x3;
554   aac->rfa = raw[2] & 0x3;
555   aac->vbr = (raw[3] >> 7) & 0x1;
556   aac->bitrate = (raw[4] << 16) | (raw[3] << 8) | raw[4];
557   aac->bitrate &= ~0x800000;
558 #elif G_BYTE_ORDER == G_BIG_ENDIAN
559   *aac = *((a2dp_aac_t *) config);
560 #else
561 #error "Unknown byte order"
562 #endif
563 
564   GST_LOG ("aac objtype=%x freq=%x rfa=%x channels=%x vbr=%x bitrate=%x",
565       aac->object_type, aac->frequency, aac->rfa, aac->channels, aac->vbr,
566       aac->bitrate);
567 
568   structure = gst_structure_new_empty ("audio/mpeg");
569   g_value_init (&value, G_TYPE_INT);
570   g_value_init (&value_str, G_TYPE_STRING);
571 
572   /* mpegversion */
573   g_value_init (&list, GST_TYPE_LIST);
574   if (aac->object_type & AAC_OBJECT_TYPE_MPEG2_AAC_LC) {
575     g_value_set_int (&value, 2);
576     gst_value_list_prepend_value (&list, &value);
577   }
578   if ((aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LC)
579       || (aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LTP)
580       || (aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_SCALABLE)) {
581     g_value_set_int (&value, 4);
582     gst_value_list_prepend_value (&list, &value);
583   }
584   if (gst_value_list_get_size (&list) == 1)
585     gst_structure_set_value (structure, "mpegversion", &value);
586   else
587     gst_structure_set_value (structure, "mpegversion", &list);
588 
589   g_value_reset (&list);
590 
591   /* base-profile */
592   if (aac->object_type & AAC_OBJECT_TYPE_MPEG2_AAC_LC
593       || aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LC) {
594     g_value_set_string (&value_str, "lc");
595     gst_value_list_prepend_value (&list, &value_str);
596   }
597   if (aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LTP) {
598     g_value_set_string (&value_str, "ltp");
599     gst_value_list_prepend_value (&list, &value_str);
600   }
601   if (aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_SCALABLE) {
602     g_value_set_string (&value_str, "ssr");
603     gst_value_list_prepend_value (&list, &value_str);
604   }
605   if (gst_value_list_get_size (&list) == 1)
606     gst_structure_set_value (structure, "base-profile", &value_str);
607   else
608     gst_structure_set_value (structure, "base-profile", &list);
609 
610   g_value_reset (&list);
611 
612   /* rate */
613   g_value_init (&list, GST_TYPE_LIST);
614   if (aac->frequency & AAC_SAMPLING_FREQ_8000) {
615     g_value_set_int (&value, 8000);
616     gst_value_list_prepend_value (&list, &value);
617   }
618   if (aac->frequency & AAC_SAMPLING_FREQ_11025) {
619     g_value_set_int (&value, 11025);
620     gst_value_list_prepend_value (&list, &value);
621   }
622   if (aac->frequency & AAC_SAMPLING_FREQ_12000) {
623     g_value_set_int (&value, 12000);
624     gst_value_list_prepend_value (&list, &value);
625   }
626   if (aac->frequency & AAC_SAMPLING_FREQ_16000) {
627     g_value_set_int (&value, 16000);
628     gst_value_list_prepend_value (&list, &value);
629   }
630   if (aac->frequency & AAC_SAMPLING_FREQ_22050) {
631     g_value_set_int (&value, 22050);
632     gst_value_list_prepend_value (&list, &value);
633   }
634   if (aac->frequency & AAC_SAMPLING_FREQ_24000) {
635     g_value_set_int (&value, 24000);
636     gst_value_list_prepend_value (&list, &value);
637   }
638   if (aac->frequency & AAC_SAMPLING_FREQ_32000) {
639     g_value_set_int (&value, 32000);
640     gst_value_list_prepend_value (&list, &value);
641   }
642   if (aac->frequency & AAC_SAMPLING_FREQ_44100) {
643     g_value_set_int (&value, 44100);
644     gst_value_list_prepend_value (&list, &value);
645   }
646   if (aac->frequency & AAC_SAMPLING_FREQ_48000) {
647     g_value_set_int (&value, 48000);
648     gst_value_list_prepend_value (&list, &value);
649   }
650   if (aac->frequency & AAC_SAMPLING_FREQ_64000) {
651     g_value_set_int (&value, 64000);
652     gst_value_list_prepend_value (&list, &value);
653   }
654   if (aac->frequency & AAC_SAMPLING_FREQ_88200) {
655     g_value_set_int (&value, 88200);
656     gst_value_list_prepend_value (&list, &value);
657   }
658   if (aac->frequency & AAC_SAMPLING_FREQ_96000) {
659     g_value_set_int (&value, 96000);
660     gst_value_list_prepend_value (&list, &value);
661   }
662   if (gst_value_list_get_size (&list) == 1)
663     gst_structure_set_value (structure, "rate", &value);
664   else
665     gst_structure_set_value (structure, "rate", &list);
666 
667   g_value_reset (&list);
668 
669   /* channels */
670   g_value_init (&list, GST_TYPE_LIST);
671   if (aac->channels & AAC_CHANNELS_1) {
672     g_value_set_int (&value, 1);
673     gst_value_list_prepend_value (&list, &value);
674   }
675   if (aac->channels & AAC_CHANNELS_2) {
676     g_value_set_int (&value, 2);
677     gst_value_list_prepend_value (&list, &value);
678   }
679   if (gst_value_list_get_size (&list) == 1)
680     gst_structure_set_value (structure, "channels", &value);
681   else
682     gst_structure_set_value (structure, "channels", &list);
683 
684   GST_LOG ("AAC caps: %" GST_PTR_FORMAT, structure);
685 
686   g_value_unset (&list);
687   g_value_unset (&value);
688   g_value_unset (&value_str);
689 
690   return structure;
691 }
692 
693 static GstStructure *
gst_avdtp_util_parse_ldac_raw(void * config)694 gst_avdtp_util_parse_ldac_raw (void *config)
695 {
696   /* We assume the vendor/codec ID have been verified already */
697   a2dp_ldac_t *ldac = (a2dp_ldac_t *) config;
698   GstStructure *structure;
699   GValue value = G_VALUE_INIT;
700   GValue list = G_VALUE_INIT;
701   gboolean mono, stereo;
702 
703   structure = gst_structure_new_empty ("audio/x-ldac");
704 
705   g_value_init (&list, GST_TYPE_LIST);
706   g_value_init (&value, G_TYPE_INT);
707 
708   /* rate */
709   if (ldac->frequency & LDAC_SAMPLING_FREQ_44100) {
710     g_value_set_int (&value, 44100);
711     gst_value_list_prepend_value (&list, &value);
712   }
713   if (ldac->frequency & LDAC_SAMPLING_FREQ_48000) {
714     g_value_set_int (&value, 48000);
715     gst_value_list_prepend_value (&list, &value);
716   }
717   if (ldac->frequency & LDAC_SAMPLING_FREQ_88200) {
718     g_value_set_int (&value, 88200);
719     gst_value_list_prepend_value (&list, &value);
720   }
721   if (ldac->frequency & LDAC_SAMPLING_FREQ_96000) {
722     g_value_set_int (&value, 96000);
723     gst_value_list_prepend_value (&list, &value);
724   }
725 
726   if (gst_value_list_get_size (&list) == 1)
727     gst_structure_set_value (structure, "rate", &value);
728   else
729     gst_structure_set_value (structure, "rate", &list);
730 
731   g_value_unset (&value);
732   g_value_reset (&list);
733 
734   /* channels */
735   mono = FALSE;
736   stereo = FALSE;
737   if (ldac->channel_mode & LDAC_CHANNEL_MODE_MONO)
738     mono = TRUE;
739   if ((ldac->channel_mode & LDAC_CHANNEL_MODE_STEREO) ||
740       (ldac->channel_mode & LDAC_CHANNEL_MODE_DUAL))
741     stereo = TRUE;
742 
743   if (mono && stereo) {
744     g_value_init (&value, GST_TYPE_INT_RANGE);
745     gst_value_set_int_range (&value, 1, 2);
746   } else {
747     g_value_init (&value, G_TYPE_INT);
748     if (mono)
749       g_value_set_int (&value, 1);
750     else if (stereo)
751       g_value_set_int (&value, 2);
752     else {
753       GST_ERROR ("Unexpected number of channels");
754       g_value_set_int (&value, 0);
755     }
756   }
757   gst_structure_set_value (structure, "channels", &value);
758 
759   g_value_unset (&value);
760   g_value_init (&value, G_TYPE_STRING);
761 
762   /* channel mode */
763   if (ldac->channel_mode & LDAC_CHANNEL_MODE_MONO) {
764     g_value_set_static_string (&value, "mono");
765     gst_value_list_prepend_value (&list, &value);
766   }
767   if (ldac->channel_mode & LDAC_CHANNEL_MODE_STEREO) {
768     g_value_set_static_string (&value, "stereo");
769     gst_value_list_prepend_value (&list, &value);
770   }
771   if (ldac->channel_mode & LDAC_CHANNEL_MODE_DUAL) {
772     g_value_set_static_string (&value, "dual");
773     gst_value_list_prepend_value (&list, &value);
774   }
775 
776   if (gst_value_list_get_size (&list) == 1)
777     gst_structure_set_value (structure, "channel-mode", &value);
778   else
779     gst_structure_take_value (structure, "channel-mode", &list);
780 
781   g_value_unset (&value);
782   g_value_unset (&list);
783 
784   return structure;
785 }
786 
787 static GstStructure *
gst_avdtp_util_parse_vendor_raw(void * config)788 gst_avdtp_util_parse_vendor_raw (void *config)
789 {
790   a2dp_vendor_codec_t *vendor = (a2dp_vendor_codec_t *) config;
791 
792   if (A2DP_GET_VENDOR_ID (*vendor) == SONY_VENDOR_ID &&
793       A2DP_GET_CODEC_ID (*vendor) == LDAC_CODEC_ID)
794     return gst_avdtp_util_parse_ldac_raw (config);
795   else
796     return NULL;
797 }
798 
799 GstCaps *
gst_avdtp_connection_get_caps(GstAvdtpConnection * conn)800 gst_avdtp_connection_get_caps (GstAvdtpConnection * conn)
801 {
802   GstCaps *caps;
803   GstStructure *structure;
804 
805   if (conn->data.config_size == 0 || conn->data.config == NULL)
806     return NULL;
807 
808   switch (conn->data.codec) {
809     case A2DP_CODEC_SBC:
810       structure = gst_avdtp_util_parse_sbc_raw (conn->data.config);
811       break;
812     case A2DP_CODEC_MPEG12:
813       structure = gst_avdtp_util_parse_mpeg_raw (conn->data.config);
814       break;
815     case A2DP_CODEC_MPEG24:
816       structure = gst_avdtp_util_parse_aac_raw (conn->data.config);
817       break;
818     case A2DP_CODEC_VENDOR:
819       structure = gst_avdtp_util_parse_vendor_raw (conn->data.config);
820       break;
821     default:
822       GST_ERROR ("Unsupported configuration");
823       return NULL;
824   }
825 
826   if (structure == NULL)
827     return FALSE;
828 
829   caps = gst_caps_new_full (structure, NULL);
830 
831   return caps;
832 }
833 
834 void
gst_avdtp_connection_notify_volume(GstAvdtpConnection * conn,GObject * source,const gchar * property)835 gst_avdtp_connection_notify_volume (GstAvdtpConnection * conn,
836     GObject * source, const gchar * property)
837 {
838   g_object_bind_property (source, property, conn->data.conn, "volume",
839       G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
840 }
841 
842 gboolean
gst_avdtp_connection_conf_recv_stream_fd(GstAvdtpConnection * conn)843 gst_avdtp_connection_conf_recv_stream_fd (GstAvdtpConnection * conn)
844 {
845   struct bluetooth_data *data = &conn->data;
846   GIOStatus status;
847   GIOFlags flags;
848   int fd;
849   int priority;
850 
851   /* Proceed if stream was already acquired */
852   if (conn->stream == NULL) {
853     GST_ERROR ("Error while configuring device: "
854         "could not acquire audio socket");
855     return FALSE;
856   }
857 
858   /* set stream socket to nonblock */
859   flags = g_io_channel_get_flags (conn->stream);
860   flags |= G_IO_FLAG_NONBLOCK;
861   status = g_io_channel_set_flags (conn->stream, flags, NULL);
862   if (status != G_IO_STATUS_NORMAL)
863     GST_WARNING ("Error while setting server socket to nonblock");
864 
865   fd = g_io_channel_unix_get_fd (conn->stream);
866 
867   /* It is possible there is some outstanding
868      data in the pipe - we have to empty it */
869   while (1) {
870     ssize_t bread = read (fd, data->buffer, data->link_mtu);
871     if (bread <= 0)
872       break;
873   }
874 
875   /* set stream socket to block */
876   flags = g_io_channel_get_flags (conn->stream);
877   flags &= ~G_IO_FLAG_NONBLOCK;
878   status = g_io_channel_set_flags (conn->stream, flags, NULL);
879   if (status != G_IO_STATUS_NORMAL)
880     GST_WARNING ("Error while setting server socket to block");
881 
882   priority = 6;
883   if (setsockopt (fd, SOL_SOCKET, SO_PRIORITY, (const void *) &priority,
884           sizeof (priority)) < 0)
885     GST_WARNING ("Unable to set socket to low delay");
886 
887   memset (data->buffer, 0, sizeof (data->buffer));
888 
889   return TRUE;
890 }
891