• 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  *
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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 
30 /* FIXME: check which includes are really required */
31 #include <unistd.h>
32 #include <sys/un.h>
33 #include <sys/socket.h>
34 #include <fcntl.h>
35 #include <netinet/in.h>
36 
37 #include "a2dp-codecs.h"
38 
39 #include "gstbluezelements.h"
40 #include "gstavdtpsink.h"
41 
42 #include <gst/rtp/rtp.h>
43 
44 GST_DEBUG_CATEGORY_STATIC (avdtp_sink_debug);
45 #define GST_CAT_DEFAULT avdtp_sink_debug
46 
47 #define CRC_PROTECTED 1
48 #define CRC_UNPROTECTED 0
49 
50 #define DEFAULT_AUTOCONNECT TRUE
51 
52 #define GST_AVDTP_SINK_MUTEX_LOCK(s) G_STMT_START {	\
53 		g_mutex_lock(&s->sink_lock);		\
54 	} G_STMT_END
55 
56 #define GST_AVDTP_SINK_MUTEX_UNLOCK(s) G_STMT_START {	\
57 		g_mutex_unlock(&s->sink_lock);		\
58 	} G_STMT_END
59 
60 #define IS_SBC(n) (strcmp((n), "audio/x-sbc") == 0)
61 #define IS_MPEG_AUDIO(n) (strcmp((n), "audio/mpeg") == 0)
62 
63 enum
64 {
65   PROP_0,
66   PROP_DEVICE,
67   PROP_AUTOCONNECT,
68   PROP_TRANSPORT
69 };
70 
71 #define parent_class gst_avdtp_sink_parent_class
72 G_DEFINE_TYPE (GstAvdtpSink, gst_avdtp_sink, GST_TYPE_BASE_SINK);
73 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (avdtpsink, "avdtpsink", GST_RANK_NONE,
74     GST_TYPE_AVDTP_SINK, bluez_element_init (plugin));
75 
76 static GstStaticPadTemplate avdtp_sink_factory =
77     GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("application/x-rtp, "
79         "media = (string) \"audio\","
80         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
81         "clock-rate = (int) { 16000, 32000, 44100, 48000 }, "
82         "encoding-name = (string) \"SBC\"; "
83         "application/x-rtp, "
84         "media = (string) \"audio\", "
85         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
86         "clock-rate = (int) 90000; "
87         "application/x-rtp, "
88         "media = (string) \"audio\", "
89         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
90         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"; "
91         "application/x-rtp, "
92         "media = (string) \"audio\", "
93         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
94         "clock-rate = (int) { 44100, 48000, 88200, 96000 }, "
95         "encoding-name = (string) \"X-GST-LDAC\""));
96 
97 static gboolean
gst_avdtp_sink_stop(GstBaseSink * basesink)98 gst_avdtp_sink_stop (GstBaseSink * basesink)
99 {
100   GstAvdtpSink *self = GST_AVDTP_SINK (basesink);
101 
102   GST_INFO_OBJECT (self, "stop");
103 
104   if (self->watch_id != 0) {
105     g_source_remove (self->watch_id);
106     self->watch_id = 0;
107   }
108 
109   gst_avdtp_connection_release (&self->conn);
110 
111   if (self->stream_caps) {
112     gst_caps_unref (self->stream_caps);
113     self->stream_caps = NULL;
114   }
115 
116   if (self->dev_caps) {
117     gst_caps_unref (self->dev_caps);
118     self->dev_caps = NULL;
119   }
120 
121   return TRUE;
122 }
123 
124 static void
gst_avdtp_sink_finalize(GObject * object)125 gst_avdtp_sink_finalize (GObject * object)
126 {
127   GstAvdtpSink *self = GST_AVDTP_SINK (object);
128 
129   gst_avdtp_sink_stop (GST_BASE_SINK (self));
130 
131   gst_avdtp_connection_reset (&self->conn);
132 
133   g_mutex_clear (&self->sink_lock);
134 
135   G_OBJECT_CLASS (parent_class)->finalize (object);
136 }
137 
138 static void
gst_avdtp_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)139 gst_avdtp_sink_set_property (GObject * object, guint prop_id,
140     const GValue * value, GParamSpec * pspec)
141 {
142   GstAvdtpSink *sink = GST_AVDTP_SINK (object);
143 
144   switch (prop_id) {
145     case PROP_DEVICE:
146       gst_avdtp_connection_set_device (&sink->conn, g_value_get_string (value));
147       break;
148 
149     case PROP_AUTOCONNECT:
150       sink->autoconnect = g_value_get_boolean (value);
151       break;
152 
153     case PROP_TRANSPORT:
154       gst_avdtp_connection_set_transport (&sink->conn,
155           g_value_get_string (value));
156       break;
157 
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161   }
162 }
163 
164 static void
gst_avdtp_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)165 gst_avdtp_sink_get_property (GObject * object, guint prop_id,
166     GValue * value, GParamSpec * pspec)
167 {
168   GstAvdtpSink *sink = GST_AVDTP_SINK (object);
169 
170   switch (prop_id) {
171     case PROP_DEVICE:
172       g_value_set_string (value, sink->conn.device);
173       break;
174 
175     case PROP_AUTOCONNECT:
176       g_value_set_boolean (value, sink->autoconnect);
177       break;
178 
179     case PROP_TRANSPORT:
180       g_value_set_string (value, sink->conn.transport);
181       break;
182 
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186   }
187 }
188 
189 static gint
gst_avdtp_sink_get_channel_mode(const gchar * mode)190 gst_avdtp_sink_get_channel_mode (const gchar * mode)
191 {
192   if (strcmp (mode, "stereo") == 0)
193     return SBC_CHANNEL_MODE_STEREO;
194   else if (strcmp (mode, "joint-stereo") == 0)
195     return SBC_CHANNEL_MODE_JOINT_STEREO;
196   else if (strcmp (mode, "dual-channel") == 0)
197     return SBC_CHANNEL_MODE_DUAL_CHANNEL;
198   else if (strcmp (mode, "mono") == 0)
199     return SBC_CHANNEL_MODE_MONO;
200   else
201     return -1;
202 }
203 
204 static void
gst_avdtp_sink_tag(const GstTagList * taglist,const gchar * tag,gpointer user_data)205 gst_avdtp_sink_tag (const GstTagList * taglist,
206     const gchar * tag, gpointer user_data)
207 {
208   gboolean crc;
209   gchar *channel_mode = NULL;
210   GstAvdtpSink *self = GST_AVDTP_SINK (user_data);
211 
212   if (strcmp (tag, "has-crc") == 0) {
213 
214     if (!gst_tag_list_get_boolean (taglist, tag, &crc)) {
215       GST_WARNING_OBJECT (self, "failed to get crc tag");
216       return;
217     }
218 
219     gst_avdtp_sink_set_crc (self, crc);
220 
221   } else if (strcmp (tag, "channel-mode") == 0) {
222 
223     if (!gst_tag_list_get_string (taglist, tag, &channel_mode)) {
224       GST_WARNING_OBJECT (self, "failed to get channel-mode tag");
225       return;
226     }
227 
228     self->channel_mode = gst_avdtp_sink_get_channel_mode (channel_mode);
229     if (self->channel_mode == -1)
230       GST_WARNING_OBJECT (self, "Received invalid channel "
231           "mode: %s", channel_mode);
232     g_free (channel_mode);
233 
234   } else
235     GST_DEBUG_OBJECT (self, "received unused tag: %s", tag);
236 }
237 
238 static gboolean
gst_avdtp_sink_event(GstBaseSink * basesink,GstEvent * event)239 gst_avdtp_sink_event (GstBaseSink * basesink, GstEvent * event)
240 {
241   GstAvdtpSink *self = GST_AVDTP_SINK (basesink);
242   GstTagList *taglist = NULL;
243 
244   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
245     /* we check the tags, mp3 has tags that are importants and
246      * are outside caps */
247     gst_event_parse_tag (event, &taglist);
248     gst_tag_list_foreach (taglist, gst_avdtp_sink_tag, self);
249   }
250 
251   return GST_BASE_SINK_CLASS (parent_class)->event (basesink, event);
252 }
253 
254 static gboolean
gst_avdtp_sink_start(GstBaseSink * basesink)255 gst_avdtp_sink_start (GstBaseSink * basesink)
256 {
257   GstAvdtpSink *self = GST_AVDTP_SINK (basesink);
258 
259   GST_INFO_OBJECT (self, "start");
260 
261   self->stream_caps = NULL;
262   self->mp3_using_crc = -1;
263   self->channel_mode = -1;
264 
265   if (self->conn.transport == NULL)
266     return FALSE;
267 
268   if (!gst_avdtp_connection_acquire (&self->conn, FALSE)) {
269     GST_ERROR_OBJECT (self, "Failed to acquire connection");
270     return FALSE;
271   }
272 
273   if (!gst_avdtp_connection_get_properties (&self->conn)) {
274     GST_ERROR_OBJECT (self, "Failed to get transport properties");
275     return FALSE;
276   }
277 
278   if (self->dev_caps)
279     gst_caps_unref (self->dev_caps);
280 
281   self->dev_caps = gst_avdtp_connection_get_caps (&self->conn);
282 
283   if (!self->dev_caps) {
284     GST_ERROR_OBJECT (self, "Failed to get device caps");
285     return FALSE;
286   }
287 
288   GST_DEBUG_OBJECT (self, "Got connection caps: %" GST_PTR_FORMAT,
289       self->dev_caps);
290 
291   return TRUE;
292 }
293 
294 static GstFlowReturn
gst_avdtp_sink_preroll(GstBaseSink * basesink,GstBuffer * buffer)295 gst_avdtp_sink_preroll (GstBaseSink * basesink, GstBuffer * buffer)
296 {
297   GstAvdtpSink *sink = GST_AVDTP_SINK (basesink);
298   gboolean ret;
299 
300   GST_AVDTP_SINK_MUTEX_LOCK (sink);
301 
302   ret = gst_avdtp_connection_conf_recv_stream_fd (&sink->conn);
303 
304   GST_AVDTP_SINK_MUTEX_UNLOCK (sink);
305 
306   if (!ret)
307     return GST_FLOW_ERROR;
308 
309   return GST_FLOW_OK;
310 }
311 
312 static GstFlowReturn
gst_avdtp_sink_render(GstBaseSink * basesink,GstBuffer * buffer)313 gst_avdtp_sink_render (GstBaseSink * basesink, GstBuffer * buffer)
314 {
315   GstFlowReturn flow_ret = GST_FLOW_OK;
316   GstAvdtpSink *self = GST_AVDTP_SINK (basesink);
317   GstMapInfo map;
318   ssize_t ret;
319   int fd;
320 
321   if (!gst_buffer_map (buffer, &map, GST_MAP_READ))
322     return GST_FLOW_ERROR;
323 
324   /* FIXME: temporary sanity check */
325   g_assert (!(g_io_channel_get_flags (self->conn.stream) & G_IO_FLAG_NONBLOCK));
326 
327   /* FIXME: why not use g_io_channel_write_chars() instead? */
328   fd = g_io_channel_unix_get_fd (self->conn.stream);
329   ret = write (fd, map.data, map.size);
330   if (ret < 0) {
331     /* FIXME: since this is probably fatal, shouldn't we post an error here? */
332     GST_ERROR_OBJECT (self, "Error writing to socket: %s", g_strerror (errno));
333     flow_ret = GST_FLOW_ERROR;
334   }
335 
336   gst_buffer_unmap (buffer, &map);
337   return flow_ret;
338 }
339 
340 static gboolean
gst_avdtp_sink_unlock(GstBaseSink * basesink)341 gst_avdtp_sink_unlock (GstBaseSink * basesink)
342 {
343   GstAvdtpSink *self = GST_AVDTP_SINK (basesink);
344 
345   if (self->conn.stream != NULL)
346     g_io_channel_flush (self->conn.stream, NULL);
347 
348   return TRUE;
349 }
350 
351 static void
gst_avdtp_sink_class_init(GstAvdtpSinkClass * klass)352 gst_avdtp_sink_class_init (GstAvdtpSinkClass * klass)
353 {
354   GObjectClass *object_class = G_OBJECT_CLASS (klass);
355   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
356   GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
357 
358   parent_class = g_type_class_peek_parent (klass);
359 
360   object_class->finalize = GST_DEBUG_FUNCPTR (gst_avdtp_sink_finalize);
361   object_class->set_property = GST_DEBUG_FUNCPTR (gst_avdtp_sink_set_property);
362   object_class->get_property = GST_DEBUG_FUNCPTR (gst_avdtp_sink_get_property);
363 
364   basesink_class->start = GST_DEBUG_FUNCPTR (gst_avdtp_sink_start);
365   basesink_class->stop = GST_DEBUG_FUNCPTR (gst_avdtp_sink_stop);
366   basesink_class->render = GST_DEBUG_FUNCPTR (gst_avdtp_sink_render);
367   basesink_class->preroll = GST_DEBUG_FUNCPTR (gst_avdtp_sink_preroll);
368   basesink_class->unlock = GST_DEBUG_FUNCPTR (gst_avdtp_sink_unlock);
369   basesink_class->event = GST_DEBUG_FUNCPTR (gst_avdtp_sink_event);
370 
371   g_object_class_install_property (object_class, PROP_DEVICE,
372       g_param_spec_string ("device", "Device",
373           "Bluetooth remote device address", NULL, G_PARAM_READWRITE));
374 
375   g_object_class_install_property (object_class, PROP_AUTOCONNECT,
376       g_param_spec_boolean ("auto-connect",
377           "Auto-connect",
378           "Automatically attempt to connect "
379           "to device", DEFAULT_AUTOCONNECT, G_PARAM_READWRITE));
380 
381   g_object_class_install_property (object_class, PROP_TRANSPORT,
382       g_param_spec_string ("transport",
383           "Transport", "Use configured transport", NULL, G_PARAM_READWRITE));
384 
385   GST_DEBUG_CATEGORY_INIT (avdtp_sink_debug, "avdtpsink", 0,
386       "A2DP headset sink element");
387 
388   gst_element_class_add_static_pad_template (element_class,
389       &avdtp_sink_factory);
390 
391   gst_element_class_set_static_metadata (element_class, "Bluetooth AVDTP sink",
392       "Sink/Audio", "Plays audio to an A2DP device",
393       "Marcel Holtmann <marcel@holtmann.org>");
394 }
395 
396 static void
gst_avdtp_sink_init(GstAvdtpSink * self)397 gst_avdtp_sink_init (GstAvdtpSink * self)
398 {
399   self->conn.device = NULL;
400   self->conn.transport = NULL;
401 
402   self->conn.stream = NULL;
403 
404   self->dev_caps = NULL;
405 
406   self->autoconnect = DEFAULT_AUTOCONNECT;
407 
408   g_mutex_init (&self->sink_lock);
409 
410   /* FIXME this is for not synchronizing with clock, should be tested
411    * with devices to see the behaviour
412    gst_base_sink_set_sync(GST_BASE_SINK(self), FALSE);
413    */
414 }
415 
416 /* public functions */
417 GstCaps *
gst_avdtp_sink_get_device_caps(GstAvdtpSink * sink)418 gst_avdtp_sink_get_device_caps (GstAvdtpSink * sink)
419 {
420   if (sink->dev_caps == NULL)
421     return NULL;
422 
423   return gst_caps_copy (sink->dev_caps);
424 }
425 
426 guint
gst_avdtp_sink_get_link_mtu(GstAvdtpSink * sink)427 gst_avdtp_sink_get_link_mtu (GstAvdtpSink * sink)
428 {
429   return sink->conn.data.link_mtu;
430 }
431 
432 void
gst_avdtp_sink_set_device(GstAvdtpSink * self,const gchar * dev)433 gst_avdtp_sink_set_device (GstAvdtpSink * self, const gchar * dev)
434 {
435   g_free (self->conn.device);
436 
437   GST_LOG_OBJECT (self, "Setting device: %s", dev);
438   self->conn.device = g_strdup (dev);
439 }
440 
441 void
gst_avdtp_sink_set_transport(GstAvdtpSink * self,const gchar * trans)442 gst_avdtp_sink_set_transport (GstAvdtpSink * self, const gchar * trans)
443 {
444   g_free (self->conn.transport);
445 
446   GST_LOG_OBJECT (self, "Setting transport: %s", trans);
447   self->conn.transport = g_strdup (trans);
448 }
449 
450 gchar *
gst_avdtp_sink_get_device(GstAvdtpSink * self)451 gst_avdtp_sink_get_device (GstAvdtpSink * self)
452 {
453   return g_strdup (self->conn.device);
454 }
455 
456 gchar *
gst_avdtp_sink_get_transport(GstAvdtpSink * self)457 gst_avdtp_sink_get_transport (GstAvdtpSink * self)
458 {
459   return g_strdup (self->conn.transport);
460 }
461 
462 void
gst_avdtp_sink_set_crc(GstAvdtpSink * self,gboolean crc)463 gst_avdtp_sink_set_crc (GstAvdtpSink * self, gboolean crc)
464 {
465   gint new_crc;
466 
467   new_crc = crc ? CRC_PROTECTED : CRC_UNPROTECTED;
468 
469   /* test if we already received a different crc */
470   if (self->mp3_using_crc != -1 && new_crc != self->mp3_using_crc) {
471     GST_WARNING_OBJECT (self, "crc changed during stream");
472     return;
473   }
474   self->mp3_using_crc = new_crc;
475 
476 }
477 
478 void
gst_avdtp_sink_set_channel_mode(GstAvdtpSink * self,const gchar * mode)479 gst_avdtp_sink_set_channel_mode (GstAvdtpSink * self, const gchar * mode)
480 {
481   gint new_mode;
482 
483   new_mode = gst_avdtp_sink_get_channel_mode (mode);
484 
485   if (self->channel_mode != -1 && new_mode != self->channel_mode) {
486     GST_WARNING_OBJECT (self, "channel mode changed during stream");
487     return;
488   }
489 
490   self->channel_mode = new_mode;
491   if (self->channel_mode == -1)
492     GST_WARNING_OBJECT (self, "Received invalid channel mode: %s", mode);
493 }
494