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