bluez: Fix a2dpsink coding style problems and improve compatibility with some players.

This commit is contained in:
Luiz Augusto von Dentz 2007-10-24 21:13:12 +00:00 committed by Tim-Philipp Müller
parent 53dfa00805
commit cb09a130ae
2 changed files with 144 additions and 126 deletions

View file

@ -70,25 +70,19 @@ GST_DEBUG_CATEGORY_STATIC (a2dp_sink_debug);
g_cond_signal (s->con_conf_end); \ g_cond_signal (s->con_conf_end); \
} G_STMT_END } G_STMT_END
struct bluetooth_a2dp struct bluetooth_data
{ {
struct ipc_data_cfg cfg; /* Bluetooth device config */
sbc_t sbc; /* Codec data */ sbc_t sbc; /* Codec data */
int codesize; /* SBC codesize */ int codesize; /* SBC codesize */
int samples; /* Number of encoded samples */ int samples; /* Number of encoded samples */
uint8_t buffer[BUFFER_SIZE]; /* Codec transfer buffer */ gchar buffer[BUFFER_SIZE]; /* Codec transfer buffer */
int count; /* Codec transfer buffer counter */ gsize count; /* Codec transfer buffer counter */
int nsamples; /* Cumulative number of codec samples */ int nsamples; /* Cumulative number of codec samples */
uint16_t seq_num; /* Cumulative packet sequence */ uint16_t seq_num; /* Cumulative packet sequence */
int frame_count; /* Current frames in buffer */ int frame_count; /* Current frames in buffer */
}; };
struct bluetooth_data
{
struct ipc_data_cfg cfg; /* Bluetooth device config */
uint8_t buffer[BUFFER_SIZE]; /* Encoded transfer buffer */
int count; /* Transfer buffer counter */
struct bluetooth_a2dp a2dp; /* A2DP data */
};
#define IS_SBC(n) (strcmp((n), "audio/x-sbc") == 0) #define IS_SBC(n) (strcmp((n), "audio/x-sbc") == 0)
#define IS_MPEG(n) (strcmp((n), "audio/mpeg") == 0) #define IS_MPEG(n) (strcmp((n), "audio/mpeg") == 0)
@ -137,15 +131,13 @@ static gboolean
gst_a2dp_sink_stop (GstBaseSink * basesink) gst_a2dp_sink_stop (GstBaseSink * basesink)
{ {
GstA2dpSink *self = GST_A2DP_SINK (basesink); GstA2dpSink *self = GST_A2DP_SINK (basesink);
struct bluetooth_a2dp *a2dp = &self->data->a2dp;
self->con_state = NOT_CONFIGURED; self->con_state = NOT_CONFIGURED;
self->total = 0; self->total = 0;
if (self->stream) { if (self->watch_id != 0) {
g_io_channel_close (self->stream); g_source_remove (self->watch_id);
g_io_channel_unref (self->stream); self->watch_id = 0;
self->stream = NULL;
} }
if (self->server) { if (self->server) {
@ -154,10 +146,9 @@ gst_a2dp_sink_stop (GstBaseSink * basesink)
self->stream = NULL; self->stream = NULL;
} }
if (self->data->cfg.codec == CFG_CODEC_SBC)
sbc_finish (&a2dp->sbc);
if (self->data) { if (self->data) {
if (self->data->cfg.codec == CFG_CODEC_SBC)
sbc_finish (&self->data->sbc);
g_free (self->data); g_free (self->data);
self->data = NULL; self->data = NULL;
} }
@ -252,6 +243,7 @@ gst_a2dp_sink_bluetooth_recvmsg_fd (GstA2dpSink * sink)
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
stream_fd = (*(int *) CMSG_DATA (cmsg)); stream_fd = (*(int *) CMSG_DATA (cmsg));
sink->stream = g_io_channel_unix_new (stream_fd); sink->stream = g_io_channel_unix_new (stream_fd);
GST_DEBUG_OBJECT (sink, "stream_fd=%d", stream_fd); GST_DEBUG_OBJECT (sink, "stream_fd=%d", stream_fd);
return 0; return 0;
} }
@ -261,14 +253,14 @@ gst_a2dp_sink_bluetooth_recvmsg_fd (GstA2dpSink * sink)
} }
static int static int
gst_a2dp_sink_bluetooth_a2dp_init (GstA2dpSink * sink, gst_a2dp_sink_bluetooth_a2dp_init (GstA2dpSink * self,
struct ipc_codec_sbc *sbc) struct ipc_codec_sbc *sbc)
{ {
struct bluetooth_a2dp *a2dp = &sink->data->a2dp; struct bluetooth_data *data = self->data;
struct ipc_data_cfg *cfg = &sink->data->cfg; struct ipc_data_cfg *cfg = &data->cfg;
if (cfg == NULL) { if (cfg == NULL) {
GST_ERROR_OBJECT (sink, "Error getting codec parameters"); GST_ERROR_OBJECT (self, "Error getting codec parameters");
return -1; return -1;
} }
@ -276,22 +268,24 @@ gst_a2dp_sink_bluetooth_a2dp_init (GstA2dpSink * sink,
return -1; return -1;
/* FIXME: init using flags? */ /* FIXME: init using flags? */
sbc_init (&a2dp->sbc, 0); sbc_init (&data->sbc, 0);
a2dp->sbc.rate = cfg->rate; data->sbc.rate = cfg->rate;
a2dp->sbc.channels = cfg->mode == CFG_MODE_MONO ? 1 : 2; data->sbc.channels = cfg->mode == CFG_MODE_MONO ? 1 : 2;
if (cfg->mode == CFG_MODE_MONO || cfg->mode == CFG_MODE_JOINT_STEREO) if (cfg->mode == CFG_MODE_MONO || cfg->mode == CFG_MODE_JOINT_STEREO)
a2dp->sbc.joint = 1; data->sbc.joint = 1;
a2dp->sbc.allocation = sbc->allocation; data->sbc.allocation = sbc->allocation;
a2dp->sbc.subbands = sbc->subbands; data->sbc.subbands = sbc->subbands;
a2dp->sbc.blocks = sbc->blocks; data->sbc.blocks = sbc->blocks;
a2dp->sbc.bitpool = sbc->bitpool; data->sbc.bitpool = sbc->bitpool;
a2dp->codesize = a2dp->sbc.subbands * a2dp->sbc.blocks * data->codesize = data->sbc.subbands * data->sbc.blocks *
a2dp->sbc.channels * 2; data->sbc.channels * 2;
a2dp->count = sizeof (struct rtp_header) + sizeof (struct rtp_payload); data->count = sizeof (struct rtp_header) + sizeof (struct rtp_payload);
GST_DEBUG_OBJECT (sink, "Codec parameters: \ GST_DEBUG_OBJECT (self, "Codec parameters: "
\tallocation=%u\n\tsubbands=%u\n \ "\tallocation=%u\n\tsubbands=%u\n "
\tblocks=%u\n\tbitpool=%u\n", a2dp->sbc.allocation, a2dp->sbc.subbands, a2dp->sbc.blocks, a2dp->sbc.bitpool); "\tblocks=%u\n\tbitpool=%u\n",
data->sbc.allocation, data->sbc.subbands,
data->sbc.blocks, data->sbc.bitpool);
return 0; return 0;
} }
@ -381,21 +375,19 @@ gst_a2dp_sink_conf_resp (GstA2dpSink * sink)
io_error = g_io_channel_read (sink->server, (gchar *) buf, io_error = g_io_channel_read (sink->server, (gchar *) buf,
sizeof (*pkt) + sizeof (*cfg), &ret); sizeof (*pkt) + sizeof (*cfg), &ret);
if (io_error != G_IO_ERROR_NONE && ret > 0) { if (io_error != G_IO_ERROR_NONE && ret > 0) {
GST_ERROR_OBJECT (sink, "Error ocurred while receiving \ GST_ERROR_OBJECT (sink, "Error ocurred while receiving "
configurarion packet answer"); "configurarion packet answer");
return FALSE; return FALSE;
} }
sink->total = ret; sink->total = ret;
if (pkt->type != PKT_TYPE_CFG_RSP) { if (pkt->type != PKT_TYPE_CFG_RSP) {
GST_ERROR_OBJECT (sink, "Unexpected packet type %d \ GST_ERROR_OBJECT (sink, "Unexpected packet type %d " "received", pkt->type);
received", pkt->type);
return FALSE; return FALSE;
} }
if (pkt->error != PKT_ERROR_NONE) { if (pkt->error != PKT_ERROR_NONE) {
GST_ERROR_OBJECT (sink, "Error %d while configuring \ GST_ERROR_OBJECT (sink, "Error %d while configuring " "device", pkt->error);
device", pkt->error);
return FALSE; return FALSE;
} }
@ -420,8 +412,8 @@ gst_a2dp_sink_conf_recv_dev_conf (GstA2dpSink * sink)
io_error = g_io_channel_read (sink->server, (gchar *) sbc, io_error = g_io_channel_read (sink->server, (gchar *) sbc,
sizeof (*sbc), &ret); sizeof (*sbc), &ret);
if (io_error != G_IO_ERROR_NONE) { if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (sink, "Error while reading data from socket \ GST_ERROR_OBJECT (sink, "Error while reading data from socket "
%s (%d)", strerror (errno), errno); "%s (%d)", strerror (errno), errno);
return FALSE; return FALSE;
} else if (ret == 0) { } else if (ret == 0) {
GST_ERROR_OBJECT (sink, "Read 0 bytes from socket"); GST_ERROR_OBJECT (sink, "Read 0 bytes from socket");
@ -430,17 +422,20 @@ gst_a2dp_sink_conf_recv_dev_conf (GstA2dpSink * sink)
sink->total += ret; sink->total += ret;
GST_DEBUG_OBJECT (sink, "OK - %d bytes received", sink->total); GST_DEBUG_OBJECT (sink, "OK - %d bytes received", sink->total);
#if 0
if (pkt->length != (sink->total - sizeof (struct ipc_packet))) { if (pkt->length != (sink->total - sizeof (struct ipc_packet))) {
GST_ERROR_OBJECT (sink, "Error while configuring device: " GST_ERROR_OBJECT (sink, "Error while configuring device: "
"packet size doesn't match"); "packet size doesn't match");
return FALSE; return FALSE;
} }
#endif
memcpy (&sink->data->cfg, cfg, sizeof (*cfg)); memcpy (&sink->data->cfg, cfg, sizeof (*cfg));
GST_DEBUG_OBJECT (sink, "Device configuration:\n\tchannel=%p\n\t\ GST_DEBUG_OBJECT (sink, "Device configuration:\n\tchannel=%p\n\t"
fd_opt=%u\n\tpkt_len=%u\n\tsample_size=%u\n\trate=%u", sink->stream, sink->data->cfg.fd_opt, sink->data->cfg.pkt_len, sink->data->cfg.sample_size, sink->data->cfg.rate); "fd_opt=%u\n\tpkt_len=%u\n\tsample_size=%u\n\trate=%u",
sink->stream, sink->data->cfg.fd_opt,
sink->data->cfg.pkt_len, sink->data->cfg.sample_size,
sink->data->cfg.rate);
if (sink->data->cfg.codec == CFG_CODEC_SBC) { if (sink->data->cfg.codec == CFG_CODEC_SBC) {
ret = gst_a2dp_sink_bluetooth_a2dp_init (sink, sbc); ret = gst_a2dp_sink_bluetooth_a2dp_init (sink, sbc);
@ -452,32 +447,65 @@ gst_a2dp_sink_conf_recv_dev_conf (GstA2dpSink * sink)
} }
static gboolean static gboolean
gst_a2dp_sink_conf_recv_stream_fd (GstA2dpSink * sink) gst_a2dp_sink_conf_recv_stream_fd (GstA2dpSink * self)
{ {
struct bluetooth_data *data = self->data;
gint ret; gint ret;
GIOError err; GIOError err;
GError *gerr = NULL;
GIOStatus status;
GIOFlags flags;
gsize read; gsize read;
ret = gst_a2dp_sink_bluetooth_recvmsg_fd (sink); ret = gst_a2dp_sink_bluetooth_recvmsg_fd (self);
if (ret < 0) if (ret < 0)
return FALSE; return FALSE;
if (!sink->stream) { if (!self->stream) {
GST_ERROR_OBJECT (sink, "Error while configuring device: " GST_ERROR_OBJECT (self, "Error while configuring device: "
"could not acquire audio socket"); "could not acquire audio socket");
return FALSE; 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");
}
/* It is possible there is some outstanding /* It is possible there is some outstanding
data in the pipe - we have to empty it */ data in the pipe - we have to empty it */
GST_LOG_OBJECT (self, "emptying stream pipe");
while (1) { while (1) {
err = g_io_channel_read (sink->stream, err = g_io_channel_read (self->stream, data->buffer,
(gchar *) sink->data->buffer, (gsize) sink->data->cfg.pkt_len, &read); (gsize) data->cfg.pkt_len, &read);
if (err != G_IO_ERROR_NONE || read <= 0) if (err != G_IO_ERROR_NONE || read <= 0)
break; break;
} }
memset (sink->data->buffer, 0, sizeof (sink->data->buffer)); /* 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; return TRUE;
} }
@ -518,27 +546,22 @@ gst_a2dp_sink_conf_recv_data (GstA2dpSink * sink)
static gboolean static gboolean
server_callback (GIOChannel * chan, GIOCondition cond, gpointer data) server_callback (GIOChannel * chan, GIOCondition cond, gpointer data)
{ {
GstA2dpSink *sink = GST_A2DP_SINK (data); GstA2dpSink *sink;
switch (cond) { if (cond & G_IO_HUP || cond & G_IO_NVAL)
case G_IO_IN: return FALSE;
if (sink->con_state != NOT_CONFIGURED && sink->con_state != CONFIGURED) else if (cond & G_IO_ERR) {
gst_a2dp_sink_conf_recv_data (sink); sink = GST_A2DP_SINK (data);
else GST_WARNING_OBJECT (sink, "Untreated callback G_IO_ERR");
GST_WARNING_OBJECT (sink, "Unexpected data received"); } else if (cond & G_IO_IN) {
break; sink = GST_A2DP_SINK (data);
case G_IO_HUP: if (sink->con_state != NOT_CONFIGURED && sink->con_state != CONFIGURED)
return FALSE; gst_a2dp_sink_conf_recv_data (sink);
break; else
case G_IO_ERR: GST_WARNING_OBJECT (sink, "Unexpected data received");
GST_WARNING_OBJECT (sink, "Untreated callback G_IO_ERR"); } else {
break; sink = GST_A2DP_SINK (data);
case G_IO_NVAL: GST_WARNING_OBJECT (sink, "Unexpected callback call");
return FALSE;
break;
default:
GST_WARNING_OBJECT (sink, "Unexpected callback call");
break;
} }
return TRUE; return TRUE;
@ -552,6 +575,8 @@ gst_a2dp_sink_start (GstBaseSink * basesink)
gint sk; gint sk;
gint err; gint err;
self->watch_id = 0;
sk = socket (PF_LOCAL, SOCK_STREAM, 0); sk = socket (PF_LOCAL, SOCK_STREAM, 0);
if (sk < 0) { if (sk < 0) {
err = errno; err = errno;
@ -568,11 +593,16 @@ gst_a2dp_sink_start (GstBaseSink * basesink)
self->server = g_io_channel_unix_new (sk); self->server = g_io_channel_unix_new (sk);
g_io_add_watch (self->server, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, self->watch_id = g_io_add_watch (self->server, G_IO_IN | G_IO_HUP |
server_callback, self); G_IO_ERR | G_IO_NVAL, server_callback, self);
self->data = g_new0 (struct bluetooth_data, 1); self->data = g_new0 (struct bluetooth_data, 1);
memset (self->data, 0, sizeof (struct bluetooth_data));
self->stream = NULL;
self->con_state = NOT_CONFIGURED;
self->total = 0;
self->waiting_con_conf = FALSE;
return TRUE; return TRUE;
} }
@ -591,8 +621,8 @@ gst_a2dp_sink_send_conf_pkt (GstA2dpSink * sink, GstCaps * caps)
memset (pkt, 0, sizeof (buf)); memset (pkt, 0, sizeof (buf));
ret = gst_a2dp_sink_init_pkt_conf (sink, caps, pkt); ret = gst_a2dp_sink_init_pkt_conf (sink, caps, pkt);
if (!ret) { if (!ret) {
GST_ERROR_OBJECT (sink, "Couldn't initialize parse caps \ GST_ERROR_OBJECT (sink, "Couldn't initialize parse caps "
to packet configuration"); "to packet configuration");
return FALSE; return FALSE;
} }
@ -601,8 +631,8 @@ gst_a2dp_sink_send_conf_pkt (GstA2dpSink * sink, GstCaps * caps)
io_error = g_io_channel_write (sink->server, (gchar *) pkt, io_error = g_io_channel_write (sink->server, (gchar *) pkt,
sizeof (*pkt) + pkt->length, &bytes_sent); sizeof (*pkt) + pkt->length, &bytes_sent);
if (io_error != G_IO_ERROR_NONE) { if (io_error != G_IO_ERROR_NONE) {
GST_ERROR_OBJECT (sink, "Error ocurred while sending \ GST_ERROR_OBJECT (sink, "Error ocurred while sending "
configurarion packet"); "configurarion packet");
sink->con_state = NOT_CONFIGURED; sink->con_state = NOT_CONFIGURED;
return FALSE; return FALSE;
} }
@ -650,47 +680,37 @@ gst_a2dp_sink_preroll (GstBaseSink * basesink, GstBuffer * buffer)
} }
static int static int
gst_a2dp_sink_avdtp_write (GstA2dpSink * sink) gst_a2dp_sink_avdtp_write (GstA2dpSink * self)
{ {
int ret = 0; gsize ret;
struct bluetooth_data *data; struct bluetooth_data *data = self->data;
struct rtp_header *header; struct rtp_header *header;
struct rtp_payload *payload; struct rtp_payload *payload;
struct bluetooth_a2dp *a2dp;
GIOError err; GIOError err;
data = sink->data; header = (void *) data->buffer;
a2dp = &data->a2dp; payload = (void *) (data->buffer + sizeof (*header));
header = (void *) a2dp->buffer; memset (data->buffer, 0, sizeof (*header) + sizeof (*payload));
payload = (void *) (a2dp->buffer + sizeof (*header));
memset (a2dp->buffer, 0, sizeof (*header) + sizeof (*payload)); payload->frame_count = data->frame_count;
payload->frame_count = a2dp->frame_count;
header->v = 2; header->v = 2;
header->pt = 1; header->pt = 1;
header->sequence_number = htons (a2dp->seq_num); header->sequence_number = htons (data->seq_num);
header->timestamp = htonl (a2dp->nsamples); header->timestamp = htonl (data->nsamples);
header->ssrc = htonl (1); header->ssrc = htonl (1);
while (1) { err = g_io_channel_write (self->stream, data->buffer, data->count, &ret);
err = g_io_channel_write (sink->stream, (const char *) a2dp->buffer, if (err != G_IO_ERROR_NONE) {
(gsize) a2dp->count, (gsize *) & ret); GST_ERROR_OBJECT (self, "Error while sending data");
ret = -1;
if (err == G_IO_ERROR_AGAIN) {
usleep (100);
continue;
}
break;
} }
/* Reset buffer of data to send */ /* Reset buffer of data to send */
a2dp->count = sizeof (struct rtp_header) + sizeof (struct rtp_payload); data->count = sizeof (struct rtp_header) + sizeof (struct rtp_payload);
a2dp->frame_count = 0; data->frame_count = 0;
a2dp->samples = 0; data->samples = 0;
a2dp->seq_num++; data->seq_num++;
return ret; return ret;
} }
@ -698,29 +718,22 @@ gst_a2dp_sink_avdtp_write (GstA2dpSink * sink)
static GstFlowReturn static GstFlowReturn
gst_a2dp_sink_render (GstBaseSink * basesink, GstBuffer * buffer) gst_a2dp_sink_render (GstBaseSink * basesink, GstBuffer * buffer)
{ {
GstA2dpSink *sink; GstA2dpSink *self = GST_A2DP_SINK (basesink);
struct bluetooth_data *data; struct bluetooth_data *data = self->data;
struct bluetooth_a2dp *a2dp; gint encoded;
gint encoded, frame_size = 1024; gint ret;
gint ret = 0;
sink = GST_A2DP_SINK (basesink);
data = (struct bluetooth_data *) sink->data;
a2dp = &data->a2dp;
encoded = GST_BUFFER_SIZE (buffer); encoded = GST_BUFFER_SIZE (buffer);
if (a2dp->count + encoded >= data->cfg.pkt_len) { if (data->count + encoded >= data->cfg.pkt_len) {
ret = gst_a2dp_sink_avdtp_write (sink); ret = gst_a2dp_sink_avdtp_write (self);
if (ret < 0) if (ret < 0)
return GST_FLOW_ERROR; return GST_FLOW_ERROR;
} }
memcpy (a2dp->buffer + a2dp->count, GST_BUFFER_DATA (buffer), encoded); memcpy (data->buffer + data->count, GST_BUFFER_DATA (buffer), encoded);
a2dp->count += encoded; data->count += encoded;
a2dp->frame_count++; data->frame_count++;
a2dp->samples += encoded / frame_size;
a2dp->nsamples += encoded / frame_size;
return GST_FLOW_OK; return GST_FLOW_OK;
} }
@ -741,6 +754,11 @@ gst_a2dp_sink_set_caps (GstBaseSink * basesink, GstCaps * caps)
static gboolean static gboolean
gst_a2dp_sink_unlock (GstBaseSink * basesink) gst_a2dp_sink_unlock (GstBaseSink * basesink)
{ {
GstA2dpSink *self = GST_A2DP_SINK (basesink);
if (self->stream != NULL)
g_io_channel_flush (self->stream, NULL);
return TRUE; return TRUE;
} }

View file

@ -67,7 +67,7 @@ struct _GstA2dpSink {
GMutex *sink_lock; GMutex *sink_lock;
gint total; gint total;
guint watch_id;
}; };
struct _GstA2dpSinkClass { struct _GstA2dpSinkClass {