gstreamer/sys/bluez/gsta2dpsink.c

988 lines
27 KiB
C
Raw Normal View History

/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <sys/un.h>
#include <sys/socket.h>
2007-10-18 21:50:00 +00:00
#include <fcntl.h>
#include <pthread.h>
#include <netinet/in.h>
#include <bluetooth/bluetooth.h>
#include "ipc.h"
2007-10-18 23:02:24 +00:00
#include "rtp.h"
#include "gstsbcutil.h"
#include "gsta2dpsink.h"
GST_DEBUG_CATEGORY_STATIC (a2dp_sink_debug);
#define GST_CAT_DEFAULT a2dp_sink_debug
2007-10-18 21:50:00 +00:00
#define BUFFER_SIZE 2048
#define TEMPLATE_MAX_BITPOOL_VALUE 64
2007-10-18 21:50:00 +00:00
#define GST_A2DP_SINK_MUTEX_LOCK(s) G_STMT_START { \
g_mutex_lock (s->sink_lock); \
} G_STMT_END
#define GST_A2DP_SINK_MUTEX_UNLOCK(s) G_STMT_START { \
g_mutex_unlock (s->sink_lock); \
} G_STMT_END
struct bluetooth_data
2007-10-18 21:50:00 +00:00
{
struct bt_getcapabilities_rsp caps; /* Bluetooth device capabilities */
struct bt_setconfiguration_rsp cfg; /* Bluetooth device configuration */
2007-10-18 21:50:00 +00:00
int samples; /* Number of encoded samples */
gchar buffer[BUFFER_SIZE]; /* Codec transfer buffer */
gsize count; /* Codec transfer buffer counter */
2007-10-18 21:50:00 +00:00
int nsamples; /* Cumulative number of codec samples */
uint16_t seq_num; /* Cumulative packet sequence */
int frame_count; /* Current frames in buffer */
};
2007-10-18 22:46:12 +00:00
#define IS_SBC(n) (strcmp((n), "audio/x-sbc") == 0)
#define IS_MPEG(n) (strcmp((n), "audio/mpeg") == 0)
enum
{
PROP_0,
PROP_DEVICE,
};
2007-10-18 21:50:00 +00:00
GST_BOILERPLATE (GstA2dpSink, gst_a2dp_sink, GstBaseSink, GST_TYPE_BASE_SINK);
static const GstElementDetails a2dp_sink_details =
GST_ELEMENT_DETAILS ("Bluetooth A2DP sink",
"Sink/Audio",
"Plays audio to an A2DP device",
"Marcel Holtmann <marcel@holtmann.org>");
static GstStaticPadTemplate a2dp_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-sbc, "
"rate = (int) { 16000, 32000, 44100, 48000 }, "
"channels = (int) [ 1, 2 ], "
"mode = (string) { mono, dual, stereo, joint }, "
"blocks = (int) { 4, 8, 12, 16 }, "
"subbands = (int) { 4, 8 }, " "allocation = (string) { snr, loudness },"
/* FIXME use constant here */
2007-10-29 15:02:26 +00:00
"bitpool = (int) [ 2, 64 ]; "
"audio/mpeg, "
"mpegversion = (int) 1, "
"layer = (int) [ 1, 3 ], "
"rate = (int) { 16000, 22050, 24000, 32000, 44100, 48000 }, "
"channels = (int) [ 1, 2 ]"));
static GIOError gst_a2dp_sink_audioservice_send (GstA2dpSink * self,
const bt_audio_msg_header_t * msg);
static GIOError gst_a2dp_sink_audioservice_expect (GstA2dpSink * self,
bt_audio_msg_header_t * outmsg, int expected_type);
static void
gst_a2dp_sink_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&a2dp_sink_factory));
gst_element_class_set_details (element_class, &a2dp_sink_details);
}
2007-10-18 21:50:00 +00:00
static gboolean
gst_a2dp_sink_stop (GstBaseSink * basesink)
{
GstA2dpSink *self = GST_A2DP_SINK (basesink);
2007-10-25 21:07:50 +00:00
GST_INFO_OBJECT (self, "stop");
if (self->watch_id != 0) {
g_source_remove (self->watch_id);
self->watch_id = 0;
2007-10-18 21:50:00 +00:00
}
if (self->stream) {
g_io_channel_flush (self->stream, NULL);
g_io_channel_close (self->stream);
g_io_channel_unref (self->stream);
self->stream = NULL;
}
2007-10-18 21:50:00 +00:00
if (self->server) {
bt_audio_service_close (g_io_channel_unix_get_fd (self->server));
2007-10-18 21:50:00 +00:00
g_io_channel_unref (self->server);
self->server = NULL;
2007-10-18 21:50:00 +00:00
}
if (self->data) {
g_free (self->data);
self->data = NULL;
}
if (self->stream_caps) {
gst_caps_unref (self->stream_caps);
self->stream_caps = NULL;
}
if (self->dev_caps) {
gst_caps_unref (self->dev_caps);
self->dev_caps = NULL;
}
2007-10-18 21:50:00 +00:00
return TRUE;
}
static void
gst_a2dp_sink_finalize (GObject * object)
{
2007-10-18 21:50:00 +00:00
GstA2dpSink *self = GST_A2DP_SINK (object);
if (self->data)
gst_a2dp_sink_stop (GST_BASE_SINK (self));
if (self->device)
g_free (self->device);
2007-10-18 21:50:00 +00:00
g_mutex_free (self->sink_lock);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_a2dp_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstA2dpSink *sink = GST_A2DP_SINK (object);
switch (prop_id) {
case PROP_DEVICE:
2007-10-18 21:50:00 +00:00
if (sink->device)
g_free (sink->device);
sink->device = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_a2dp_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstA2dpSink *sink = GST_A2DP_SINK (object);
switch (prop_id) {
case PROP_DEVICE:
g_value_set_string (value, sink->device);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
2007-10-18 21:50:00 +00:00
static gint
gst_a2dp_sink_bluetooth_recvmsg_fd (GstA2dpSink * sink)
{
int err, ret;
ret = bt_audio_service_get_data_fd (g_io_channel_unix_get_fd (sink->server));
2007-10-18 21:50:00 +00:00
if (ret < 0) {
err = errno;
GST_ERROR_OBJECT (sink, "Unable to receive fd: %s (%d)",
strerror (err), err);
return -err;
}
sink->stream = g_io_channel_unix_new (ret);
GST_DEBUG_OBJECT (sink, "stream_fd=%d", ret);
2007-10-18 21:50:00 +00:00
return 0;
}
static gboolean
2007-10-18 21:50:00 +00:00
gst_a2dp_sink_init_pkt_conf (GstA2dpSink * sink,
GstCaps * caps, sbc_capabilities_t * pkt)
{
sbc_capabilities_t *cfg = &sink->data->caps.sbc_capabilities;
2007-10-18 21:50:00 +00:00
const GValue *value = NULL;
const char *pref, *name;
gint rate, blocks, subbands;
2007-10-18 21:50:00 +00:00
GstStructure *structure = gst_caps_get_structure (caps, 0);
name = gst_structure_get_name (structure);
/* FIXME only sbc supported here, should suport mp3 */
if (!(IS_SBC (name))) {
GST_ERROR_OBJECT (sink, "Unsupported format %s", name);
return FALSE;
}
value = gst_structure_get_value (structure, "rate");
rate = g_value_get_int (value);
if (rate == 44100)
cfg->frequency = BT_A2DP_SAMPLING_FREQ_44100;
else if (rate == 48000)
cfg->frequency = BT_A2DP_SAMPLING_FREQ_48000;
else if (rate == 32000)
cfg->frequency = BT_A2DP_SAMPLING_FREQ_32000;
else if (rate == 16000)
cfg->frequency = BT_A2DP_SAMPLING_FREQ_16000;
else {
GST_ERROR_OBJECT (sink, "Invalid rate while setting caps");
return FALSE;
}
2007-10-18 21:50:00 +00:00
value = gst_structure_get_value (structure, "mode");
pref = g_value_get_string (value);
if (strcmp (pref, "auto") == 0)
cfg->channel_mode = BT_A2DP_CHANNEL_MODE_AUTO;
2007-10-18 21:50:00 +00:00
else if (strcmp (pref, "mono") == 0)
cfg->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
2007-10-18 21:50:00 +00:00
else if (strcmp (pref, "dual") == 0)
cfg->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
2007-10-18 21:50:00 +00:00
else if (strcmp (pref, "stereo") == 0)
cfg->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
2007-10-18 21:50:00 +00:00
else if (strcmp (pref, "joint") == 0)
cfg->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
2007-10-18 21:50:00 +00:00
else {
GST_ERROR_OBJECT (sink, "Invalid mode %s", pref);
return FALSE;
}
value = gst_structure_get_value (structure, "allocation");
pref = g_value_get_string (value);
if (strcmp (pref, "auto") == 0)
cfg->allocation_method = BT_A2DP_ALLOCATION_AUTO;
2007-10-18 21:50:00 +00:00
else if (strcmp (pref, "loudness") == 0)
cfg->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
2007-10-18 21:50:00 +00:00
else if (strcmp (pref, "snr") == 0)
cfg->allocation_method = BT_A2DP_ALLOCATION_SNR;
2007-10-18 21:50:00 +00:00
else {
GST_ERROR_OBJECT (sink, "Invalid allocation: %s", pref);
return FALSE;
}
value = gst_structure_get_value (structure, "subbands");
subbands = g_value_get_int (value);
if (subbands == 8)
cfg->subbands = BT_A2DP_SUBBANDS_8;
else if (subbands == 4)
cfg->subbands = BT_A2DP_SUBBANDS_4;
else {
GST_ERROR_OBJECT (sink, "Invalid subbands %d", subbands);
return FALSE;
}
2007-10-18 21:50:00 +00:00
value = gst_structure_get_value (structure, "blocks");
blocks = g_value_get_int (value);
if (blocks == 16)
cfg->block_length = BT_A2DP_BLOCK_LENGTH_16;
else if (blocks == 12)
cfg->block_length = BT_A2DP_BLOCK_LENGTH_12;
else if (blocks == 8)
cfg->block_length = BT_A2DP_BLOCK_LENGTH_8;
else if (blocks == 4)
cfg->block_length = BT_A2DP_BLOCK_LENGTH_4;
else {
GST_ERROR_OBJECT (sink, "Invalid blocks %d", blocks);
return FALSE;
}
2007-10-29 15:02:26 +00:00
/* FIXME min and max ??? */
2007-10-29 15:02:26 +00:00
value = gst_structure_get_value (structure, "bitpool");
cfg->max_bitpool = cfg->min_bitpool = g_value_get_int (value);
2007-10-18 21:50:00 +00:00
memcpy (pkt, cfg, sizeof (*pkt));
2007-10-24 21:40:35 +00:00
return TRUE;
}
static gboolean
gst_a2dp_sink_conf_recv_stream_fd (GstA2dpSink * self)
{
struct bluetooth_data *data = self->data;
2007-10-18 21:50:00 +00:00
gint ret;
GIOError err;
GError *gerr = NULL;
GIOStatus status;
GIOFlags flags;
2007-10-18 21:50:00 +00:00
gsize read;
ret = gst_a2dp_sink_bluetooth_recvmsg_fd (self);
2007-10-18 21:50:00 +00:00
if (ret < 0)
return FALSE;
if (!self->stream) {
GST_ERROR_OBJECT (self, "Error while configuring device: "
2007-10-18 22:46:12 +00:00
"could not acquire audio socket");
2007-10-18 21:50:00 +00:00
return FALSE;
}
/* set stream socket to nonblock */
GST_LOG_OBJECT (self, "setting stream socket to nonblock");
flags = g_io_channel_get_flags (self->stream);
flags |= G_IO_FLAG_NONBLOCK;
status = g_io_channel_set_flags (self->stream, flags, &gerr);
if (status != G_IO_STATUS_NORMAL) {
if (gerr)
GST_WARNING_OBJECT (self, "Error while "
"setting server socket to nonblock: " "%s", gerr->message);
else
GST_WARNING_OBJECT (self, "Error while "
"setting server " "socket to nonblock");
}
2007-10-18 21:50:00 +00:00
/* It is possible there is some outstanding
data in the pipe - we have to empty it */
GST_LOG_OBJECT (self, "emptying stream pipe");
2007-10-18 22:46:12 +00:00
while (1) {
err = g_io_channel_read (self->stream, data->buffer,
(gsize) data->cfg.link_mtu, &read);
2007-10-18 21:50:00 +00:00
if (err != G_IO_ERROR_NONE || read <= 0)
break;
}
/* set stream socket to block */
GST_LOG_OBJECT (self, "setting stream socket to block");
flags = g_io_channel_get_flags (self->stream);
flags &= ~G_IO_FLAG_NONBLOCK;
status = g_io_channel_set_flags (self->stream, flags, &gerr);
if (status != G_IO_STATUS_NORMAL) {
if (gerr)
GST_WARNING_OBJECT (self, "Error while "
"setting server socket to block:" "%s", gerr->message);
else
GST_WARNING_OBJECT (self, "Error while "
"setting server " "socket to block");
}
memset (data->buffer, 0, sizeof (data->buffer));
return TRUE;
}
2007-10-18 21:50:00 +00:00
static gboolean
server_callback (GIOChannel * chan, GIOCondition cond, gpointer data)
{
GstA2dpSink *sink;
2007-10-18 21:50:00 +00:00
if (cond & G_IO_HUP || cond & G_IO_NVAL)
return FALSE;
else if (cond & G_IO_ERR) {
sink = GST_A2DP_SINK (data);
GST_WARNING_OBJECT (sink, "Untreated callback G_IO_ERR");
}
return TRUE;
}
static gboolean
gst_a2dp_sink_update_caps (GstA2dpSink * self)
{
sbc_capabilities_t *sbc = &self->data->caps.sbc_capabilities;
GstStructure *structure;
GValue *value;
GValue *list;
gchar *tmp;
GST_LOG_OBJECT (self, "updating device caps");
structure = gst_structure_empty_new ("audio/x-sbc");
value = g_value_init (g_new0 (GValue, 1), G_TYPE_STRING);
/* mode */
list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
if (sbc->channel_mode == BT_A2DP_CHANNEL_MODE_AUTO) {
g_value_set_static_string (value, "joint");
gst_value_list_prepend_value (list, value);
g_value_set_static_string (value, "stereo");
gst_value_list_prepend_value (list, value);
g_value_set_static_string (value, "mono");
gst_value_list_prepend_value (list, value);
g_value_set_static_string (value, "dual");
gst_value_list_prepend_value (list, value);
} else {
if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
g_value_set_static_string (value, "mono");
gst_value_list_prepend_value (list, value);
}
if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) {
g_value_set_static_string (value, "stereo");
gst_value_list_prepend_value (list, value);
}
if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) {
g_value_set_static_string (value, "dual");
gst_value_list_prepend_value (list, value);
}
if (sbc->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO) {
g_value_set_static_string (value, "joint");
gst_value_list_prepend_value (list, value);
}
}
g_value_unset (value);
if (list) {
gst_structure_set_value (structure, "mode", list);
g_free (list);
list = NULL;
}
/* subbands */
list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
value = g_value_init (value, G_TYPE_INT);
if (sbc->subbands & BT_A2DP_SUBBANDS_4) {
g_value_set_int (value, 4);
gst_value_list_prepend_value (list, value);
}
if (sbc->subbands & BT_A2DP_SUBBANDS_8) {
g_value_set_int (value, 8);
gst_value_list_prepend_value (list, value);
}
g_value_unset (value);
if (list) {
gst_structure_set_value (structure, "subbands", list);
g_free (list);
list = NULL;
}
/* blocks */
value = g_value_init (value, G_TYPE_INT);
list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_16) {
g_value_set_int (value, 16);
gst_value_list_prepend_value (list, value);
}
if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_12) {
g_value_set_int (value, 12);
gst_value_list_prepend_value (list, value);
}
if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_8) {
g_value_set_int (value, 8);
gst_value_list_prepend_value (list, value);
}
if (sbc->block_length & BT_A2DP_BLOCK_LENGTH_4) {
g_value_set_int (value, 4);
gst_value_list_prepend_value (list, value);
}
g_value_unset (value);
if (list) {
gst_structure_set_value (structure, "blocks", list);
g_free (list);
list = NULL;
}
/* allocation */
g_value_init (value, G_TYPE_STRING);
list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
if (sbc->allocation_method == BT_A2DP_ALLOCATION_AUTO) {
g_value_set_static_string (value, "loudness");
gst_value_list_prepend_value (list, value);
g_value_set_static_string (value, "snr");
gst_value_list_prepend_value (list, value);
} else {
if (sbc->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS) {
g_value_set_static_string (value, "loudness");
gst_value_list_prepend_value (list, value);
}
if (sbc->allocation_method & BT_A2DP_ALLOCATION_SNR) {
g_value_set_static_string (value, "snr");
gst_value_list_prepend_value (list, value);
}
}
g_value_unset (value);
if (list) {
gst_structure_set_value (structure, "allocation", list);
g_free (list);
list = NULL;
}
/* rate */
g_value_init (value, G_TYPE_INT);
list = g_value_init (g_new0 (GValue, 1), GST_TYPE_LIST);
if (sbc->frequency & BT_A2DP_SAMPLING_FREQ_48000) {
g_value_set_int (value, 48000);
gst_value_list_prepend_value (list, value);
}
if (sbc->frequency & BT_A2DP_SAMPLING_FREQ_44100) {
g_value_set_int (value, 44100);
gst_value_list_prepend_value (list, value);
}
if (sbc->frequency & BT_A2DP_SAMPLING_FREQ_32000) {
g_value_set_int (value, 32000);
gst_value_list_prepend_value (list, value);
}
if (sbc->frequency & BT_A2DP_SAMPLING_FREQ_16000) {
g_value_set_int (value, 16000);
gst_value_list_prepend_value (list, value);
}
g_value_unset (value);
if (list) {
gst_structure_set_value (structure, "rate", list);
g_free (list);
list = NULL;
}
/* bitpool */
value = g_value_init (value, GST_TYPE_INT_RANGE);
gst_value_set_int_range (value,
MIN (sbc->min_bitpool, TEMPLATE_MAX_BITPOOL_VALUE),
MIN (sbc->max_bitpool, TEMPLATE_MAX_BITPOOL_VALUE));
gst_structure_set_value (structure, "bitpool", value);
/* channels */
gst_value_set_int_range (value, 1, 2);
gst_structure_set_value (structure, "channels", value);
g_free (value);
if (self->dev_caps != NULL)
gst_caps_unref (self->dev_caps);
self->dev_caps = gst_caps_new_full (structure, NULL);
tmp = gst_caps_to_string (self->dev_caps);
GST_DEBUG_OBJECT (self, "Device capabilities: %s", tmp);
g_free (tmp);
return TRUE;
}
static gboolean
gst_a2dp_sink_get_capabilities (GstA2dpSink * self)
{
gchar *buf[BT_AUDIO_IPC_PACKET_SIZE];
struct bt_getcapabilities_req *req = (void *) buf;
bt_audio_rsp_msg_header_t *rsp_hdr = (void *) buf;
struct bt_getcapabilities_rsp *rsp = (void *) buf;
GIOError io_error;
memset (req, 0, BT_AUDIO_IPC_PACKET_SIZE);
req->h.msg_type = BT_GETCAPABILITIES_REQ;
strncpy (req->device, self->device, 18);
io_error = gst_a2dp_sink_audioservice_send (self, &req->h);
if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error while asking device caps");
}
io_error = gst_a2dp_sink_audioservice_expect (self, &rsp_hdr->msg_h,
BT_GETCAPABILITIES_RSP);
if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error while getting device caps");
return FALSE;
}
if (rsp_hdr->posix_errno != 0) {
GST_ERROR_OBJECT (self, "BT_GETCAPABILITIES failed : %s(%d)",
strerror (rsp_hdr->posix_errno), rsp_hdr->posix_errno);
return FALSE;
}
memcpy (&self->data->caps, rsp, sizeof (*rsp));
if (!gst_a2dp_sink_update_caps (self)) {
GST_WARNING_OBJECT (self, "failed to update capabilities");
return FALSE;
2007-10-18 21:50:00 +00:00
}
2007-10-18 21:50:00 +00:00
return TRUE;
}
2007-10-18 21:50:00 +00:00
static gboolean
gst_a2dp_sink_start (GstBaseSink * basesink)
{
2007-10-18 21:50:00 +00:00
GstA2dpSink *self = GST_A2DP_SINK (basesink);
gint sk;
gint err;
2007-10-25 21:07:50 +00:00
GST_INFO_OBJECT (self, "start");
self->watch_id = 0;
sk = bt_audio_service_open ();
if (sk <= 0) {
2007-10-18 21:50:00 +00:00
err = errno;
GST_ERROR_OBJECT (self, "Cannot open connection to bt "
"audio service: %s %d", strerror (err), err);
goto failed;
2007-10-18 21:50:00 +00:00
}
self->server = g_io_channel_unix_new (sk);
self->watch_id = g_io_add_watch (self->server, G_IO_HUP | G_IO_ERR |
G_IO_NVAL, server_callback, self);
2007-10-18 21:50:00 +00:00
self->data = g_new0 (struct bluetooth_data, 1);
2007-10-25 21:07:50 +00:00
memset (self->data, 0, sizeof (struct bluetooth_data));
self->stream = NULL;
self->stream_caps = NULL;
if (!gst_a2dp_sink_get_capabilities (self))
goto failed;
2007-10-18 21:50:00 +00:00
return TRUE;
failed:
bt_audio_service_close (sk);
return FALSE;
}
static gboolean
gst_a2dp_sink_stream_start (GstA2dpSink * self)
{
gchar buf[BT_AUDIO_IPC_PACKET_SIZE];
struct bt_streamstart_req *req = (void *) buf;
bt_audio_rsp_msg_header_t *rsp_hdr = (void *) buf;
struct bt_streamfd_ind *ind = (void *) buf;
2007-10-18 21:50:00 +00:00
GIOError io_error;
GST_DEBUG_OBJECT (self, "stream start");
2007-10-18 21:50:00 +00:00
memset (req, 0, sizeof (buf));
req->h.msg_type = BT_STREAMSTART_REQ;
io_error = gst_a2dp_sink_audioservice_send (self, &req->h);
if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error ocurred while sending " "start packet");
2007-10-18 21:50:00 +00:00
return FALSE;
}
GST_DEBUG_OBJECT (self, "stream start packet sent");
2007-10-18 21:50:00 +00:00
io_error = gst_a2dp_sink_audioservice_expect (self, &rsp_hdr->msg_h,
BT_STREAMSTART_RSP);
2007-10-18 21:50:00 +00:00
if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error while stream start confirmation");
return FALSE;
}
if (rsp_hdr->posix_errno != 0) {
GST_ERROR_OBJECT (self, "BT_STREAMSTART_RSP failed : %s(%d)",
strerror (rsp_hdr->posix_errno), rsp_hdr->posix_errno);
2007-10-18 21:50:00 +00:00
return FALSE;
}
GST_DEBUG_OBJECT (self, "stream started");
io_error = gst_a2dp_sink_audioservice_expect (self, &ind->h, BT_STREAMFD_IND);
if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error while receiving stream fd");
return FALSE;
}
if (!gst_a2dp_sink_conf_recv_stream_fd (self))
return FALSE;
return TRUE;
}
2007-10-18 21:50:00 +00:00
static gboolean
gst_a2dp_sink_configure (GstA2dpSink * self, GstCaps * caps)
2007-10-18 21:50:00 +00:00
{
gchar buf[BT_AUDIO_IPC_PACKET_SIZE];
struct bt_setconfiguration_req *req = (void *) buf;
bt_audio_rsp_msg_header_t *rsp_hdr = (void *) buf;
struct bt_setconfiguration_rsp *rsp = (void *) buf;
2007-10-18 21:50:00 +00:00
gboolean ret;
GIOError io_error;
2007-10-18 21:50:00 +00:00
GST_DEBUG_OBJECT (self, "configuring device");
2007-10-18 21:50:00 +00:00
memset (req, 0, sizeof (buf));
req->h.msg_type = BT_SETCONFIGURATION_REQ;
req->access_mode = BT_CAPABILITIES_ACCESS_MODE_WRITE;
strncpy (req->device, self->device, 18);
ret = gst_a2dp_sink_init_pkt_conf (self, caps, &req->sbc_capabilities);
if (!ret) {
GST_ERROR_OBJECT (self, "Couldn't parse caps " "to packet configuration");
return FALSE;
}
io_error = gst_a2dp_sink_audioservice_send (self, &req->h);
if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error ocurred while sending "
"configurarion packet");
return FALSE;
}
2007-10-18 21:50:00 +00:00
GST_DEBUG_OBJECT (self, "configuration packet sent");
2007-10-18 21:50:00 +00:00
io_error = gst_a2dp_sink_audioservice_expect (self, &rsp_hdr->msg_h,
BT_SETCONFIGURATION_RSP);
if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error while receiving device confirmation");
return FALSE;
}
if (rsp_hdr->posix_errno != 0) {
GST_ERROR_OBJECT (self, "BT_SETCONFIGURATION_RSP failed : %s(%d)",
strerror (rsp_hdr->posix_errno), rsp_hdr->posix_errno);
return FALSE;
}
memcpy (&self->data->cfg, rsp, sizeof (*rsp));
GST_DEBUG_OBJECT (self, "configuration set");
return TRUE;
2007-10-18 21:50:00 +00:00
}
static GstFlowReturn
gst_a2dp_sink_preroll (GstBaseSink * basesink, GstBuffer * buffer)
{
GstA2dpSink *sink = GST_A2DP_SINK (basesink);
gboolean ret;
2007-10-18 21:50:00 +00:00
GST_A2DP_SINK_MUTEX_LOCK (sink);
ret = gst_a2dp_sink_stream_start (sink);
2007-10-18 21:50:00 +00:00
GST_A2DP_SINK_MUTEX_UNLOCK (sink);
if (!ret)
2007-10-18 21:50:00 +00:00
return GST_FLOW_ERROR;
return GST_FLOW_OK;
}
static int
gst_a2dp_sink_avdtp_write (GstA2dpSink * self)
2007-10-18 21:50:00 +00:00
{
gsize ret;
struct bluetooth_data *data = self->data;
2007-10-18 21:50:00 +00:00
struct rtp_header *header;
struct rtp_payload *payload;
GIOError err;
header = (void *) data->buffer;
payload = (void *) (data->buffer + sizeof (*header));
2007-10-18 21:50:00 +00:00
memset (data->buffer, 0, sizeof (*header) + sizeof (*payload));
2007-10-18 21:50:00 +00:00
payload->frame_count = data->frame_count;
2007-10-18 21:50:00 +00:00
header->v = 2;
header->pt = 1;
header->sequence_number = htons (data->seq_num);
header->timestamp = htonl (data->nsamples);
2007-10-18 21:50:00 +00:00
header->ssrc = htonl (1);
err = g_io_channel_write (self->stream, data->buffer, data->count, &ret);
if (err != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error while sending data");
ret = -1;
2007-10-18 21:50:00 +00:00
}
/* Reset buffer of data to send */
data->count = sizeof (struct rtp_header) + sizeof (struct rtp_payload);
data->frame_count = 0;
data->samples = 0;
data->seq_num++;
2007-10-18 21:50:00 +00:00
return ret;
}
static GstFlowReturn
gst_a2dp_sink_render (GstBaseSink * basesink, GstBuffer * buffer)
{
GstA2dpSink *self = GST_A2DP_SINK (basesink);
struct bluetooth_data *data = self->data;
gint encoded;
gint ret;
2007-10-18 21:50:00 +00:00
encoded = GST_BUFFER_SIZE (buffer);
if (data->count + encoded >= data->cfg.link_mtu) {
ret = gst_a2dp_sink_avdtp_write (self);
2007-10-18 21:50:00 +00:00
if (ret < 0)
return GST_FLOW_ERROR;
}
memcpy (data->buffer + data->count, GST_BUFFER_DATA (buffer), encoded);
data->count += encoded;
data->frame_count++;
2007-10-18 21:50:00 +00:00
return GST_FLOW_OK;
}
static GstCaps *
gst_a2dp_sink_get_caps (GstBaseSink * basesink)
{
GstA2dpSink *self = GST_A2DP_SINK (basesink);
if (self->dev_caps)
return gst_caps_ref (self->dev_caps);
return
gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (self)));
}
2007-10-18 21:50:00 +00:00
static gboolean
gst_a2dp_sink_set_caps (GstBaseSink * basesink, GstCaps * caps)
{
GstA2dpSink *self = GST_A2DP_SINK (basesink);
gboolean ret;
2007-10-18 21:50:00 +00:00
GST_A2DP_SINK_MUTEX_LOCK (self);
ret = gst_a2dp_sink_configure (self, caps);
if (self->stream_caps)
gst_caps_unref (self->stream_caps);
self->stream_caps = gst_caps_ref (caps);
2007-10-18 21:50:00 +00:00
GST_A2DP_SINK_MUTEX_UNLOCK (self);
return ret;
2007-10-18 21:50:00 +00:00
}
static gboolean
gst_a2dp_sink_unlock (GstBaseSink * basesink)
{
GstA2dpSink *self = GST_A2DP_SINK (basesink);
if (self->stream != NULL)
g_io_channel_flush (self->stream, NULL);
2007-10-18 21:50:00 +00:00
return TRUE;
}
static void
gst_a2dp_sink_class_init (GstA2dpSinkClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2007-10-18 21:50:00 +00:00
GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = GST_DEBUG_FUNCPTR (gst_a2dp_sink_finalize);
object_class->set_property = GST_DEBUG_FUNCPTR (gst_a2dp_sink_set_property);
object_class->get_property = GST_DEBUG_FUNCPTR (gst_a2dp_sink_get_property);
2007-10-18 21:50:00 +00:00
basesink_class->start = GST_DEBUG_FUNCPTR (gst_a2dp_sink_start);
basesink_class->stop = GST_DEBUG_FUNCPTR (gst_a2dp_sink_stop);
basesink_class->render = GST_DEBUG_FUNCPTR (gst_a2dp_sink_render);
basesink_class->preroll = GST_DEBUG_FUNCPTR (gst_a2dp_sink_preroll);
basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_a2dp_sink_set_caps);
basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_a2dp_sink_get_caps);
2007-10-18 21:50:00 +00:00
basesink_class->unlock = GST_DEBUG_FUNCPTR (gst_a2dp_sink_unlock);
g_object_class_install_property (object_class, PROP_DEVICE,
g_param_spec_string ("device", "Device",
2007-10-18 21:50:00 +00:00
"Bluetooth remote device address", NULL, G_PARAM_READWRITE));
GST_DEBUG_CATEGORY_INIT (a2dp_sink_debug, "a2dpsink", 0, "A2DP sink element");
}
static void
gst_a2dp_sink_init (GstA2dpSink * self, GstA2dpSinkClass * klass)
{
2007-10-18 21:50:00 +00:00
self->device = NULL;
self->data = NULL;
2007-10-18 21:50:00 +00:00
self->stream = NULL;
self->dev_caps = NULL;
2007-10-18 21:50:00 +00:00
self->sink_lock = g_mutex_new ();
}
static GIOError
gst_a2dp_sink_audioservice_send (GstA2dpSink * self,
const bt_audio_msg_header_t * msg)
{
gint err;
GIOError error;
gsize written;
GST_DEBUG_OBJECT (self, "sending %s", bt_audio_strmsg (msg->msg_type));
error = g_io_channel_write (self->server, (const gchar *) msg,
BT_AUDIO_IPC_PACKET_SIZE, &written);
if (error != G_IO_ERROR_NONE) {
err = errno;
GST_ERROR_OBJECT (self, "Error sending data to audio service:"
" %s(%d)", strerror (err), err);
}
return error;
}
static GIOError
gst_a2dp_sink_audioservice_recv (GstA2dpSink * self,
bt_audio_msg_header_t * inmsg)
{
GIOError status;
gsize bytes_read;
const char *type;
status = g_io_channel_read (self->server, (gchar *) inmsg,
BT_AUDIO_IPC_PACKET_SIZE, &bytes_read);
if (status != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (self, "Error receiving data from service");
return status;
}
type = bt_audio_strmsg (inmsg->msg_type);
if (!type) {
GST_ERROR_OBJECT (self, "Bogus message type %d "
"received from audio service", inmsg->msg_type);
return G_IO_ERROR_INVAL;
}
GST_DEBUG_OBJECT (self, "Received %s", type);
return status;
}
static GIOError
gst_a2dp_sink_audioservice_expect (GstA2dpSink * self,
bt_audio_msg_header_t * outmsg, int expected_type)
{
GIOError status;
status = gst_a2dp_sink_audioservice_recv (self, outmsg);
if (status != G_IO_ERROR_NONE)
return status;
if (outmsg->msg_type != expected_type) {
GST_ERROR_OBJECT (self, "Bogus message %s "
"received while %s was expected",
bt_audio_strmsg (outmsg->msg_type), bt_audio_strmsg (expected_type));
return G_IO_ERROR_INVAL;
}
return status;
}