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