• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <stdint.h>
7 #include <sys/ioctl.h>
8 #include <sys/param.h>
9 #include <linux/sockios.h>
10 #include <sys/socket.h>
11 #include <sys/time.h>
12 #include <syslog.h>
13 #include <time.h>
14 
15 #include "audio_thread.h"
16 #include "audio_thread_log.h"
17 #include "byte_buffer.h"
18 #include "cras_iodev_list.h"
19 #include "cras_a2dp_endpoint.h"
20 #include "cras_a2dp_info.h"
21 #include "cras_a2dp_iodev.h"
22 #include "cras_audio_area.h"
23 #include "cras_bt_device.h"
24 #include "cras_iodev.h"
25 #include "cras_util.h"
26 #include "sfh.h"
27 #include "rtp.h"
28 #include "utlist.h"
29 
30 #define PCM_BUF_MAX_SIZE_FRAMES (4096 * 4)
31 #define PCM_BUF_MAX_SIZE_BYTES (PCM_BUF_MAX_SIZE_FRAMES * 4)
32 
33 /* no_stream target_frames in timespec. */
34 static const struct timespec no_stream_target_frames_ts = {
35 	0, 10 * 1000 * 1000 /* 10 msec. */
36 };
37 
38 /* Child of cras_iodev to handle bluetooth A2DP streaming.
39  * Members:
40  *    base - The cras_iodev structure "base class"
41  *    a2dp - The codec and encoded state of a2dp_io.
42  *    transport - The transport object for bluez media API.
43  *    sock_depth_frames - Socket depth in frames of the a2dp socket.
44  *    pcm_buf - Buffer to hold pcm samples before encode.
45  *    destroyed - Flag to note if this a2dp_io is about to destroy.
46  *    bt_written_frames - Accumulated frames written to a2dp socket. Used
47  *        together with the device open timestamp to estimate how many virtual
48  *        buffer is queued there.
49  *    dev_open_time - The last time a2dp_ios is opened.
50  *    drain_complete - Flag to indicate if valid frames have all been drained
51  *        in no stream state.
52  *    filled_zeros_bytes - Number of zero data in bytes that have been filled
53  *        in no stream state.
54  */
55 struct a2dp_io {
56 	struct cras_iodev base;
57 	struct a2dp_info a2dp;
58 	struct cras_bt_transport *transport;
59 	unsigned sock_depth_frames;
60 	struct byte_buffer *pcm_buf;
61 	int destroyed;
62 	uint64_t bt_written_frames;
63 	struct timespec dev_open_time;
64 	bool drain_complete;
65 	int filled_zeros_bytes;
66 };
67 
68 static int flush_data(void *arg);
69 
update_supported_formats(struct cras_iodev * iodev)70 static int update_supported_formats(struct cras_iodev *iodev)
71 {
72 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
73 	size_t rate = 0;
74 	size_t channel;
75 	a2dp_sbc_t a2dp;
76 
77 	cras_bt_transport_configuration(a2dpio->transport, &a2dp, sizeof(a2dp));
78 
79 	iodev->format->format = SND_PCM_FORMAT_S16_LE;
80 	channel = (a2dp.channel_mode == SBC_CHANNEL_MODE_MONO) ? 1 : 2;
81 
82 	if (a2dp.frequency & SBC_SAMPLING_FREQ_48000)
83 		rate = 48000;
84 	else if (a2dp.frequency & SBC_SAMPLING_FREQ_44100)
85 		rate = 44100;
86 	else if (a2dp.frequency & SBC_SAMPLING_FREQ_32000)
87 		rate = 32000;
88 	else if (a2dp.frequency & SBC_SAMPLING_FREQ_16000)
89 		rate = 16000;
90 
91 	free(iodev->supported_rates);
92 	iodev->supported_rates = (size_t *)malloc(2 * sizeof(rate));
93 	iodev->supported_rates[0] = rate;
94 	iodev->supported_rates[1] = 0;
95 
96 	free(iodev->supported_channel_counts);
97 	iodev->supported_channel_counts = (size_t *)malloc(2 * sizeof(channel));
98 	iodev->supported_channel_counts[0] = channel;
99 	iodev->supported_channel_counts[1] = 0;
100 
101 	free(iodev->supported_formats);
102 	iodev->supported_formats =
103 		(snd_pcm_format_t *)malloc(2 * sizeof(snd_pcm_format_t));
104 	iodev->supported_formats[0] = SND_PCM_FORMAT_S16_LE;
105 	iodev->supported_formats[1] = 0;
106 
107 	return 0;
108 }
109 
110 /* Calculates the number of virtual buffer in frames. Assuming all written
111  * buffer is consumed in a constant frame rate at bluetooth device side.
112  * Args:
113  *    iodev: The a2dp iodev to estimate the queued frames for.
114  *    fr: The amount of frames just transmitted.
115  */
bt_queued_frames(const struct cras_iodev * iodev,int fr)116 static int bt_queued_frames(const struct cras_iodev *iodev, int fr)
117 {
118 	uint64_t consumed;
119 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
120 
121 	/* Calculate consumed frames since device has opened */
122 	a2dpio->bt_written_frames += fr;
123 	consumed = cras_frames_since_time(&a2dpio->dev_open_time,
124 					  iodev->format->frame_rate);
125 
126 	if (a2dpio->bt_written_frames > consumed)
127 		return a2dpio->bt_written_frames - consumed;
128 	else
129 		return 0;
130 }
131 
frames_queued(const struct cras_iodev * iodev,struct timespec * tstamp)132 static int frames_queued(const struct cras_iodev *iodev,
133 			 struct timespec *tstamp)
134 {
135 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
136 	int estimate_queued_frames = bt_queued_frames(iodev, 0);
137 	int local_queued_frames = a2dp_queued_frames(&a2dpio->a2dp) +
138 				  buf_queued(a2dpio->pcm_buf) /
139 					  cras_get_format_bytes(iodev->format);
140 	clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
141 	return MIN(iodev->buffer_size,
142 		   MAX(estimate_queued_frames, local_queued_frames));
143 }
144 
no_stream(struct cras_iodev * iodev,int enable)145 static int no_stream(struct cras_iodev *iodev, int enable)
146 {
147 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
148 	unsigned int buf_avail;
149 	unsigned int format_bytes;
150 	unsigned int target_bytes;
151 	unsigned int target_total_bytes;
152 	unsigned int bt_queued_bytes;
153 	uint8_t *buf;
154 	struct timespec tstamp;
155 	int i;
156 
157 	format_bytes = cras_get_format_bytes(iodev->format);
158 
159 	if (enable) {
160 		/* Target to have let hw_level = 2 * (frames in 10ms) */
161 		bt_queued_bytes =
162 			cras_iodev_frames_queued(iodev, &tstamp) * format_bytes;
163 		target_total_bytes =
164 			2 *
165 			cras_time_to_frames(&no_stream_target_frames_ts,
166 					    iodev->format->frame_rate) *
167 			format_bytes;
168 		if (target_total_bytes <= bt_queued_bytes)
169 			return 0;
170 		target_total_bytes -= bt_queued_bytes;
171 
172 		/* Loop twice to make sure target_total_bytes are filled. */
173 		for (i = 0; i < 2; i++) {
174 			buf = buf_write_pointer_size(a2dpio->pcm_buf,
175 						     &buf_avail);
176 			if (buf_avail == 0 || target_total_bytes == 0)
177 				break;
178 			target_bytes = MIN(buf_avail, target_total_bytes);
179 			memset(buf, 0, target_bytes);
180 			buf_increment_write(a2dpio->pcm_buf, target_bytes);
181 			bt_queued_frames(iodev, target_bytes / format_bytes);
182 			target_total_bytes -= target_bytes;
183 		}
184 		flush_data(iodev);
185 		return 0;
186 	}
187 	return 0;
188 }
189 
configure_dev(struct cras_iodev * iodev)190 static int configure_dev(struct cras_iodev *iodev)
191 {
192 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
193 	int sock_depth;
194 	int err;
195 	socklen_t optlen;
196 
197 	err = cras_bt_transport_acquire(a2dpio->transport);
198 	if (err < 0) {
199 		syslog(LOG_ERR, "transport_acquire failed");
200 		return err;
201 	}
202 
203 	/* Apply the node's volume after transport is acquired. Doing this
204 	 * is necessary because the volume can not sync to hardware until
205 	 * it is opened. */
206 	iodev->set_volume(iodev);
207 
208 	/* Assert format is set before opening device. */
209 	if (iodev->format == NULL)
210 		return -EINVAL;
211 	iodev->format->format = SND_PCM_FORMAT_S16_LE;
212 	cras_iodev_init_audio_area(iodev, iodev->format->num_channels);
213 
214 	a2dpio->pcm_buf = byte_buffer_create(PCM_BUF_MAX_SIZE_BYTES);
215 	if (!a2dpio->pcm_buf)
216 		return -ENOMEM;
217 
218 	iodev->buffer_size = PCM_BUF_MAX_SIZE_FRAMES;
219 
220 	/* Set up the socket to hold two MTUs full of data before returning
221 	 * EAGAIN.  This will allow the write to be throttled when a reasonable
222 	 * amount of data is queued. */
223 	sock_depth = 2 * cras_bt_transport_write_mtu(a2dpio->transport);
224 	setsockopt(cras_bt_transport_fd(a2dpio->transport), SOL_SOCKET,
225 		   SO_SNDBUF, &sock_depth, sizeof(sock_depth));
226 
227 	optlen = sizeof(sock_depth);
228 	getsockopt(cras_bt_transport_fd(a2dpio->transport), SOL_SOCKET,
229 		   SO_SNDBUF, &sock_depth, &optlen);
230 	a2dpio->sock_depth_frames = a2dp_block_size(&a2dpio->a2dp, sock_depth) /
231 				    cras_get_format_bytes(iodev->format);
232 
233 	iodev->min_buffer_level = a2dpio->sock_depth_frames;
234 
235 	a2dpio->drain_complete = 0;
236 	a2dpio->filled_zeros_bytes = 0;
237 
238 	/* Initialize variables for bt_queued_frames() */
239 	a2dpio->bt_written_frames = 0;
240 	clock_gettime(CLOCK_MONOTONIC_RAW, &a2dpio->dev_open_time);
241 
242 	audio_thread_add_write_callback(cras_bt_transport_fd(a2dpio->transport),
243 					flush_data, iodev);
244 	audio_thread_enable_callback(cras_bt_transport_fd(a2dpio->transport),
245 				     0);
246 	return 0;
247 }
248 
close_dev(struct cras_iodev * iodev)249 static int close_dev(struct cras_iodev *iodev)
250 {
251 	int err;
252 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
253 	struct cras_bt_device *device;
254 
255 	if (!a2dpio->transport)
256 		return 0;
257 
258 	/* Remove audio thread callback and sync before releasing
259 	 * the transport. */
260 	audio_thread_rm_callback_sync(cras_iodev_list_get_audio_thread(),
261 				      cras_bt_transport_fd(a2dpio->transport));
262 
263 	err = cras_bt_transport_release(a2dpio->transport, !a2dpio->destroyed);
264 	if (err < 0)
265 		syslog(LOG_ERR, "transport_release failed");
266 
267 	device = cras_bt_transport_device(a2dpio->transport);
268 	if (device)
269 		cras_bt_device_cancel_suspend(device);
270 	a2dp_drain(&a2dpio->a2dp);
271 	byte_buffer_destroy(&a2dpio->pcm_buf);
272 	cras_iodev_free_format(iodev);
273 	cras_iodev_free_audio_area(iodev);
274 	return 0;
275 }
276 
277 /* Flushes queued buffer, including pcm and a2dp buffer.
278  * Returns:
279  *    0 when the flush succeeded, -1 when error occurred.
280  */
flush_data(void * arg)281 static int flush_data(void *arg)
282 {
283 	struct cras_iodev *iodev = (struct cras_iodev *)arg;
284 	int processed;
285 	size_t format_bytes;
286 	int written = 0;
287 	int queued_frames;
288 	struct a2dp_io *a2dpio;
289 	struct cras_bt_device *device;
290 
291 	a2dpio = (struct a2dp_io *)iodev;
292 	format_bytes = cras_get_format_bytes(iodev->format);
293 	device = cras_bt_transport_device(a2dpio->transport);
294 
295 	/* If bt device has been destroyed, this a2dp iodev will soon be
296 	 * destroyed as well. */
297 	if (device == NULL)
298 		return -EINVAL;
299 
300 encode_more:
301 	while (buf_queued(a2dpio->pcm_buf)) {
302 		processed = a2dp_encode(
303 			&a2dpio->a2dp, buf_read_pointer(a2dpio->pcm_buf),
304 			buf_readable(a2dpio->pcm_buf), format_bytes,
305 			cras_bt_transport_write_mtu(a2dpio->transport));
306 		ATLOG(atlog, AUDIO_THREAD_A2DP_ENCODE, processed,
307 		      buf_queued(a2dpio->pcm_buf),
308 		      buf_readable(a2dpio->pcm_buf));
309 		if (processed == -ENOSPC || processed == 0)
310 			break;
311 		if (processed < 0)
312 			return 0;
313 
314 		buf_increment_read(a2dpio->pcm_buf, processed);
315 	}
316 
317 	written = a2dp_write(&a2dpio->a2dp,
318 			     cras_bt_transport_fd(a2dpio->transport),
319 			     cras_bt_transport_write_mtu(a2dpio->transport));
320 	ATLOG(atlog, AUDIO_THREAD_A2DP_WRITE, written,
321 	      a2dp_queued_frames(&a2dpio->a2dp), 0);
322 	if (written == -EAGAIN) {
323 		/* If EAGAIN error lasts longer than 5 seconds, suspend the
324 		 * a2dp connection. */
325 		cras_bt_device_schedule_suspend(device, 5000);
326 		audio_thread_enable_callback(
327 			cras_bt_transport_fd(a2dpio->transport), 1);
328 		return 0;
329 	} else if (written < 0) {
330 		/* Suspend a2dp immediately when receives error other than
331 		 * EAGAIN. */
332 		cras_bt_device_cancel_suspend(device);
333 		cras_bt_device_schedule_suspend(device, 0);
334 		return written;
335 	}
336 
337 	/* Data succcessfully written to a2dp socket, cancel any scheduled
338 	 * suspend timer. */
339 	cras_bt_device_cancel_suspend(device);
340 
341 	/* If it looks okay to write more and we do have queued data, try
342 	 * encode more. But avoid the case when PCM buffer level is too close
343 	 * to min_buffer_level so that another A2DP write could causes underrun.
344 	 */
345 	queued_frames = buf_queued(a2dpio->pcm_buf) / format_bytes;
346 	if (written && (iodev->min_buffer_level + written < queued_frames))
347 		goto encode_more;
348 
349 	/* everything written. */
350 	audio_thread_enable_callback(cras_bt_transport_fd(a2dpio->transport),
351 				     0);
352 
353 	return 0;
354 }
355 
delay_frames(const struct cras_iodev * iodev)356 static int delay_frames(const struct cras_iodev *iodev)
357 {
358 	const struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
359 	struct timespec tstamp;
360 
361 	/* The number of frames in the pcm buffer plus two mtu packets */
362 	return frames_queued(iodev, &tstamp) + a2dpio->sock_depth_frames;
363 }
364 
get_buffer(struct cras_iodev * iodev,struct cras_audio_area ** area,unsigned * frames)365 static int get_buffer(struct cras_iodev *iodev, struct cras_audio_area **area,
366 		      unsigned *frames)
367 {
368 	size_t format_bytes;
369 	struct a2dp_io *a2dpio;
370 
371 	a2dpio = (struct a2dp_io *)iodev;
372 
373 	format_bytes = cras_get_format_bytes(iodev->format);
374 
375 	if (iodev->direction != CRAS_STREAM_OUTPUT)
376 		return 0;
377 
378 	*frames = MIN(*frames, buf_writable(a2dpio->pcm_buf) / format_bytes);
379 	iodev->area->frames = *frames;
380 	cras_audio_area_config_buf_pointers(iodev->area, iodev->format,
381 					    buf_write_pointer(a2dpio->pcm_buf));
382 	*area = iodev->area;
383 	return 0;
384 }
385 
put_buffer(struct cras_iodev * iodev,unsigned nwritten)386 static int put_buffer(struct cras_iodev *iodev, unsigned nwritten)
387 {
388 	size_t written_bytes;
389 	size_t format_bytes;
390 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
391 
392 	format_bytes = cras_get_format_bytes(iodev->format);
393 	written_bytes = nwritten * format_bytes;
394 
395 	if (written_bytes > buf_writable(a2dpio->pcm_buf))
396 		return -EINVAL;
397 
398 	buf_increment_write(a2dpio->pcm_buf, written_bytes);
399 
400 	/* Set dev open time at when the first data arrives. */
401 	if (nwritten && !a2dpio->bt_written_frames)
402 		clock_gettime(CLOCK_MONOTONIC_RAW, &a2dpio->dev_open_time);
403 
404 	bt_queued_frames(iodev, nwritten);
405 
406 	return flush_data(iodev);
407 }
408 
flush_buffer(struct cras_iodev * iodev)409 static int flush_buffer(struct cras_iodev *iodev)
410 {
411 	return 0;
412 }
413 
set_volume(struct cras_iodev * iodev)414 static void set_volume(struct cras_iodev *iodev)
415 {
416 	size_t volume;
417 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
418 	struct cras_bt_device *device =
419 		cras_bt_transport_device(a2dpio->transport);
420 
421 	if (!cras_bt_device_get_use_hardware_volume(device))
422 		return;
423 
424 	volume = iodev->active_node->volume * 127 / 100;
425 
426 	if (a2dpio->transport)
427 		cras_bt_transport_set_volume(a2dpio->transport, volume);
428 }
429 
update_active_node(struct cras_iodev * iodev,unsigned node_idx,unsigned dev_enabled)430 static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
431 			       unsigned dev_enabled)
432 {
433 }
434 
free_resources(struct a2dp_io * a2dpio)435 void free_resources(struct a2dp_io *a2dpio)
436 {
437 	struct cras_ionode *node;
438 
439 	node = a2dpio->base.active_node;
440 	if (node) {
441 		cras_iodev_rm_node(&a2dpio->base, node);
442 		free(node);
443 	}
444 	free(a2dpio->base.supported_channel_counts);
445 	free(a2dpio->base.supported_rates);
446 	free(a2dpio->base.supported_formats);
447 	destroy_a2dp(&a2dpio->a2dp);
448 }
449 
a2dp_iodev_create(struct cras_bt_transport * transport)450 struct cras_iodev *a2dp_iodev_create(struct cras_bt_transport *transport)
451 {
452 	int err;
453 	struct a2dp_io *a2dpio;
454 	struct cras_iodev *iodev;
455 	struct cras_ionode *node;
456 	a2dp_sbc_t a2dp;
457 	struct cras_bt_device *device;
458 	const char *name;
459 
460 	a2dpio = (struct a2dp_io *)calloc(1, sizeof(*a2dpio));
461 	if (!a2dpio)
462 		goto error;
463 
464 	a2dpio->transport = transport;
465 	cras_bt_transport_configuration(a2dpio->transport, &a2dp, sizeof(a2dp));
466 	err = init_a2dp(&a2dpio->a2dp, &a2dp);
467 	if (err) {
468 		syslog(LOG_ERR, "Fail to init a2dp");
469 		goto error;
470 	}
471 
472 	iodev = &a2dpio->base;
473 
474 	/* A2DP only does output now */
475 	iodev->direction = CRAS_STREAM_OUTPUT;
476 
477 	/* Set iodev's name by bluetooth device's readable name, if
478 	 * the readable name is not available, use address instead.
479 	 */
480 	device = cras_bt_transport_device(transport);
481 	name = cras_bt_device_name(device);
482 	if (!name)
483 		name = cras_bt_transport_object_path(a2dpio->transport);
484 
485 	snprintf(iodev->info.name, sizeof(iodev->info.name), "%s", name);
486 	iodev->info.name[ARRAY_SIZE(iodev->info.name) - 1] = '\0';
487 	iodev->info.stable_id =
488 		SuperFastHash(cras_bt_device_object_path(device),
489 			      strlen(cras_bt_device_object_path(device)),
490 			      strlen(cras_bt_device_object_path(device)));
491 
492 	iodev->configure_dev = configure_dev;
493 	iodev->frames_queued = frames_queued;
494 	iodev->delay_frames = delay_frames;
495 	iodev->get_buffer = get_buffer;
496 	iodev->put_buffer = put_buffer;
497 	iodev->flush_buffer = flush_buffer;
498 	iodev->no_stream = no_stream;
499 	iodev->close_dev = close_dev;
500 	iodev->update_supported_formats = update_supported_formats;
501 	iodev->update_active_node = update_active_node;
502 	iodev->set_volume = set_volume;
503 
504 	/* Create a dummy ionode */
505 	node = (struct cras_ionode *)calloc(1, sizeof(*node));
506 	node->dev = iodev;
507 	strcpy(node->name, iodev->info.name);
508 	node->plugged = 1;
509 	node->type = CRAS_NODE_TYPE_BLUETOOTH;
510 	node->volume = 100;
511 	gettimeofday(&node->plugged_time, NULL);
512 
513 	/* A2DP does output only */
514 	cras_bt_device_append_iodev(
515 		device, iodev, cras_bt_transport_profile(a2dpio->transport));
516 	cras_iodev_add_node(iodev, node);
517 	cras_iodev_set_active_node(iodev, node);
518 
519 	return iodev;
520 error:
521 	if (a2dpio) {
522 		free_resources(a2dpio);
523 		free(a2dpio);
524 	}
525 	return NULL;
526 }
527 
a2dp_iodev_destroy(struct cras_iodev * iodev)528 void a2dp_iodev_destroy(struct cras_iodev *iodev)
529 {
530 	struct a2dp_io *a2dpio = (struct a2dp_io *)iodev;
531 	struct cras_bt_device *device;
532 
533 	a2dpio->destroyed = 1;
534 	device = cras_bt_transport_device(a2dpio->transport);
535 
536 	/* A2DP does output only */
537 	cras_bt_device_rm_iodev(device, iodev);
538 
539 	/* Free resources when device successfully removed. */
540 	free_resources(a2dpio);
541 	cras_iodev_free_resources(iodev);
542 	free(a2dpio);
543 }
544