summaryrefslogtreecommitdiffstats
path: root/audio/pcm_bluetooth.c
blob: 29583940aca3d5a97ba6293087b967a999382736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 *
 *  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 <sys/socket.h>
#include <sys/un.h>

#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/sco.h>

#include "ipc.h"

#define DBG(fmt, arg...)  printf("DEBUG: %s: " fmt "\n" , __FUNCTION__ , ## arg)

#ifndef SCO_TXBUFS
#define SCO_TXBUFS 0x03
#endif

#ifndef SCO_RXBUFS
#define SCO_RXBUFS 0x04
#endif

struct bluetooth_data {
	snd_pcm_ioplug_t io;
	snd_pcm_sframes_t hw_ptr;
	struct ipc_data_cfg cfg;	/* Bluetooth device config */
	int sock;			/* Daemon unix socket */
	uint8_t *buffer;		/* Transfer buffer */
	uint8_t count;			/* Transfer buffer counter */
};

static int bluetooth_start(snd_pcm_ioplug_t *io)
{
	DBG("io %p", io);

	return 0;
}

static int bluetooth_stop(snd_pcm_ioplug_t *io)
{
	DBG("io %p", io);

	return 0;
}

static snd_pcm_sframes_t bluetooth_pointer(snd_pcm_ioplug_t *io)
{
	struct bluetooth_data *data = io->private_data;

	DBG("io %p", io);

	DBG("hw_ptr=%lu", data->hw_ptr);

	return data->hw_ptr;
}

static int bluetooth_close(snd_pcm_ioplug_t *io)
{
	struct bluetooth_data *data = io->private_data;

	DBG("io %p", io);

	free(data);

	return 0;
}


static int bluetooth_prepare(snd_pcm_ioplug_t *io)
{
	struct bluetooth_data *data = io->private_data;

	DBG("Preparing with io->period_size = %lu, io->buffer_size = %lu", io->period_size, io->buffer_size);

	if (io->stream == SND_PCM_STREAM_PLAYBACK) {
		/* If not null for playback, xmms doesn't display time correctly */
		data->hw_ptr = 0;
	}
	else {
		/* ALSA library is really picky on the fact hw_ptr is not null. If it is, capture won't start */
		data->hw_ptr = io->period_size;
	}
	return 0;
}

static int bluetooth_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params)
{
	struct bluetooth_data *data = io->private_data;
	struct ipc_data_cfg cfg = data->cfg;
	uint32_t period_count = io->buffer_size / io->period_size;

	DBG("period_count = %d", period_count);

	if(setsockopt(cfg.fd, SOL_SCO,
			io->stream == SND_PCM_STREAM_PLAYBACK ? SCO_TXBUFS : SCO_RXBUFS,
			&period_count,
			sizeof(period_count)) == 0) {
		return 0;
	} else if(setsockopt(cfg.fd, SOL_SCO,
			io->stream == SND_PCM_STREAM_PLAYBACK ? SO_SNDBUF : SO_RCVBUF,
			&period_count,
			sizeof(period_count)) == 0) {
                return 0;
        } else {
		SNDERR("Unable to set number of SCO buffers : please upgrade your Kernel !");
		return -EINVAL;
	}
}

static snd_pcm_sframes_t bluetooth_read(snd_pcm_ioplug_t *io,
					const snd_pcm_channel_area_t *areas,
					snd_pcm_uframes_t offset,
					snd_pcm_uframes_t size)
{
	struct bluetooth_data *data = io->private_data;
	struct ipc_data_cfg cfg = data->cfg;

	snd_pcm_sframes_t ret = 0;

	DBG("areas->step=%u, areas->first=%u, offset=%lu, size=%lu, io->nonblock=%u",
		areas->step, areas->first, offset, size, io->nonblock);

	if (data->count == 0) {
		int nrecv;

		nrecv = recv(cfg.fd, data->buffer, cfg.pkt_len,
			MSG_WAITALL | (io->nonblock ? MSG_DONTWAIT : 0 ));

		if (nrecv == cfg.pkt_len) {
			ret = 0;
			/* Increment hardware transmition pointer */
			data->hw_ptr = (data->hw_ptr + cfg.pkt_len / cfg.sample_size) % io->buffer_size;
		}
		else if (nrecv > 0) {
			ret = -EIO;
			SNDERR(strerror(-ret));
		}
		else if (nrecv == -1 && errno == EAGAIN) {
			ret = -EAGAIN;
		}
		else { /* nrecv < 0 */
			/* EPIPE means device underrun in ALSA world. But we mean we lost contact
			with server, so we have to find another error code */
			ret = (errno == EPIPE ? -EIO : -errno);
			SYSERR("Lost contact with headsetd");
		}
	}
	if(ret == 0) { /* Still ok, proceed */
		snd_pcm_uframes_t frames_to_write;
		unsigned char *buff;

		buff = (unsigned char *) areas->addr + (areas->first + areas->step * offset) / 8;

		if((data->count + cfg.sample_size * size) <= cfg.pkt_len)
			frames_to_write = size;
		else
			frames_to_write = (cfg.pkt_len - data->count) / cfg.sample_size;

		memcpy(buff, data->buffer + data->count, areas->step / 8 * frames_to_write);
		data->count += (areas->step / 8 * frames_to_write);
		data->count %= cfg.pkt_len;
		/* Return written frames count */
		ret = frames_to_write;
	}

	DBG("returning %d", (int)ret);
	return ret;
}

static snd_pcm_sframes_t bluetooth_write(snd_pcm_ioplug_t *io,
					const snd_pcm_channel_area_t *areas,
					snd_pcm_uframes_t offset,
					snd_pcm_uframes_t size)
{
	struct bluetooth_data *data = io->private_data;
	struct ipc_data_cfg cfg = data->cfg;
	snd_pcm_sframes_t ret = 0;
	snd_pcm_uframes_t frames_to_read;
	unsigned char *buff;

	DBG("areas->step=%u, areas->first=%u, offset=%lu, size=%lu, io->nonblock=%u",
		areas->step, areas->first, offset, size, io->nonblock);

	if ((data->count + cfg.sample_size * size) <= cfg.pkt_len)
		frames_to_read = size;
	else
		frames_to_read = (cfg.pkt_len - data->count) / cfg.sample_size;

	/* Ready for more data */
	buff = (unsigned char *) areas->addr + (areas->first + areas->step * offset) / 8;
	memcpy(data->buffer + data->count, buff, areas->step / 8 * frames_to_read);

	if ((data->count + areas->step / 8 * frames_to_read) == cfg.pkt_len) {
		int rsend;
		/* Actually send packet */
		rsend = send(cfg.fd, data->buffer, cfg.pkt_len, io->nonblock ? MSG_DONTWAIT : 0);
		if (rsend > 0) {
			/* Reset count pointer */
			data->count = 0;

			/* Increment hardware transmition pointer */
			data->hw_ptr = (data->hw_ptr + cfg.pkt_len / cfg.sample_size) % io->buffer_size;

			ret = frames_to_read;
		}
		else {
			/* EPIPE means device underrun in ALSA world. But we mean we lost contact
                           with server, so we have to find another error code */
			ret = (errno == EPIPE ? -EIO : -errno);
			if(errno == EPIPE)
				SYSERR("Lost contact with headsetd");
		}
	}
	else {
		/* Remember we have some frame in the pipe now */
		data->count += areas->step / 8 * frames_to_read;
		/* Ask for more */
		ret = frames_to_read;
	}

	DBG("returning %d", (int)ret);
	return ret;
}

static snd_pcm_ioplug_callback_t bluetooth_playback_callback = {
	.start		= bluetooth_start,
	.stop		= bluetooth_stop,
	.pointer	= bluetooth_pointer,
	.close		= bluetooth_close,
	.hw_params	= bluetooth_hw_params,
	.prepare	= bluetooth_prepare,
	.transfer	= bluetooth_write,
};

static snd_pcm_ioplug_callback_t bluetooth_capture_callback = {
	.start		= bluetooth_start,
	.stop		= bluetooth_stop,
	.pointer	= bluetooth_pointer,
	.close		= bluetooth_close,
	.hw_params	= bluetooth_hw_params,
	.prepare	= bluetooth_prepare,
	.transfer	= bluetooth_read,
};

#define ARRAY_NELEMS(a) (sizeof((a)) / sizeof((a)[0]))

static int bluetooth_hw_constraint(snd_pcm_ioplug_t *io)
{
	snd_pcm_access_t access_list[] = {
		SND_PCM_ACCESS_RW_INTERLEAVED,
		/* Mmap access is really useless fo this driver, but we
		 * support it because some pieces of software out there
		 * insist on using it */
		SND_PCM_ACCESS_MMAP_INTERLEAVED
	};
	unsigned int format_list[] = {
		SND_PCM_FORMAT_S16_LE
	};
	int err;

	err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
					ARRAY_NELEMS(access_list), access_list);
	if (err < 0)
		return err;

	err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
					ARRAY_NELEMS(format_list), format_list);
	if (err < 0)
		return err;

	err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS, 1, 1);
	if (err < 0)
		return err;

	err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE, 8000, 8000);
	if (err < 0)
		return err;

	err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES, 48, 48);
	if (err < 0)
		return err;

	err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS, 2, 200);
	if (err < 0)
		return err;

	return 0;
}

static int bluetooth_cfg(struct bluetooth_data *data)
{
	struct ipc_packet pkt;
	int res;

	DBG("Sending PKT_TYPE_CFG_REQ...");
	pkt.type = PKT_TYPE_CFG_REQ;
	pkt.role = PKT_ROLE_NONE;
	res = send(data->sock, &pkt, sizeof(struct ipc_packet), 0);
	if (res < 0)
		return errno;
	DBG("OK - %d bytes sent", res);

	DBG("Waiting for response...");
	do {
		int len = sizeof(struct ipc_packet) + sizeof(struct ipc_data_cfg);
		struct ipc_packet *pkt_ptr;

		pkt_ptr = malloc(sizeof(struct ipc_packet) + sizeof(struct ipc_data_cfg));
		res = recv(data->sock, pkt_ptr, len, MSG_WAITALL | (data->io.nonblock ? MSG_DONTWAIT : 0 ));
	} while ((res < 0) && (errno == EINTR));
	if (res < 0)
		return -errno;
	DBG("OK - %d bytes received", res);

	if (pkt.type != PKT_TYPE_CFG_RSP) {
		SNDERR("Unexpected packet type received: type = %d", pkt.type);
		return -EINVAL;
	}

	if (pkt.error != PKT_ERROR_NONE) {
		SNDERR("Error while configuring device: error = %d", pkt.error);
		return pkt.error;
	}

	if (pkt.length != sizeof(struct ipc_data_cfg)) {
		SNDERR("Error while configuring device: packet size doesn't match");
		return -EINVAL;
	}

	memcpy(&data->cfg, &pkt.data, pkt.length);

	if (data->cfg.fd == -1) {
		SNDERR("Error while configuring device: could not acquire audio socket");
		return -EINVAL;
	}

	DBG("Device configuration:");
	DBG("fd=%d, fd_opt=%u, channels=%u, pkt_len=%u, sample_size=%u, rate=%u",
		data->cfg.fd, data->cfg.fd_opt, data->cfg.channels,
		data->cfg.pkt_len, data->cfg.sample_size, data->cfg.rate);

	return 0;
}

static int bluetooth_init(struct bluetooth_data *data)
{
	int sk, err, id;
	struct sockaddr_un addr;

	id = abs(getpid() * rand());

	sk = socket(PF_LOCAL, SOCK_DGRAM, 0);
	if (sk < 0) {
		SNDERR("Can't open socket");
		return -errno;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	snprintf(addr.sun_path + 1, UNIX_PATH_MAX - 2, "%s/%d",
			IPC_SOCKET_NAME, id);

	DBG("Binding address: %s", addr.sun_path + 1);
	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		SNDERR("Can't bind socket");
		close(sk);
		return -errno;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	snprintf(addr.sun_path + 1, UNIX_PATH_MAX - 2, "%s", IPC_SOCKET_NAME);

	DBG("Connecting to address: %s", addr.sun_path + 1);
	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		SNDERR("Can't connect socket");
		close(sk);
		return -errno;
	}

	data = malloc(sizeof(*data));
	if (!data) {
		close(sk);
		return -ENOMEM;
	}

	memset(data, 0, sizeof(*data));

	data->sock = sk;

	if ((err = bluetooth_cfg(data)) < 0)
		return err;

	data->buffer = malloc(data->cfg.pkt_len);

	memset(data->buffer, 0, data->cfg.pkt_len);

	return 0;
}

SND_PCM_PLUGIN_DEFINE_FUNC(bluetooth)
{
//	snd_config_iterator_t iter, next;
	struct bluetooth_data data;
	int err;

	DBG("Bluetooth PCM plugin blablabla (%s)",
		stream == SND_PCM_STREAM_PLAYBACK ? "Playback" : "Capture");

//	snd_config_for_each(iter, next, conf) {
//	}

	DBG("Initing Bluetooth...");
	err = bluetooth_init(&data);
	if (err < 0)
		goto error;
	DBG("Done");

	data.io.version = SND_PCM_IOPLUG_VERSION;
	data.io.name = "Bluetooth Audio Device";
	data.io.mmap_rw =  0; /* No direct mmap communication */

	data.io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
		&bluetooth_playback_callback : &bluetooth_capture_callback;
	data.io.poll_fd = data.cfg.fd;
	data.io.poll_events = POLLIN;
	data.io.private_data = &data;

	err = snd_pcm_ioplug_create(&data.io, name, stream, mode);
	if (err < 0)
		goto error;

	err = bluetooth_hw_constraint(&data.io);
	if (err < 0) {
		snd_pcm_ioplug_delete(&data.io);
		goto error;
	}

	*pcmp = data.io.pcm;

	return 0;

error:
	close(data.sock);

	free(&data);

	return err;
}

SND_PCM_PLUGIN_SYMBOL(bluetooth);