summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_bt_io.c
blob: 1363d90d508d7066c12b86b2e56a3ad629f27fe0 (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <syslog.h>

#include "cras_bt_io.h"
#include "cras_bt_device.h"
#include "cras_utf8.h"
#include "cras_iodev.h"
#include "cras_iodev_list.h"
#include "sfh.h"
#include "utlist.h"

#define DEFAULT_BT_DEVICE_NAME "BLUETOOTH"

/* Extends cras_ionode to hold bluetooth profile information
 * so that iodevs of different profile(A2DP or HFP/HSP) can be
 * associated with the same bt_io.
 * Members:
 *    base - The base class cras_ionode.
 *    profile_dev - Pointer to the profile specific iodev.
 *    profile - The bluetooth profile profile_dev runs on.
 */
struct bt_node {
	struct cras_ionode base;
	struct cras_iodev *profile_dev;
	unsigned int profile;
};

/* The structure represents a virtual input or output device of a
 * bluetooth audio device, speaker or headset for example. A node
 * will be added to this virtual iodev for each profile supported
 * by the bluetooth audio device.
 * Member:
 *    base - The base class cras_iodev
 *    next_node_id - The index will give to the next node
 */
struct bt_io {
	struct cras_iodev base;
	unsigned int next_node_id;
	struct cras_bt_device *device;
};

/* Returns the active profile specific iodev. */
static struct cras_iodev *active_profile_dev(const struct cras_iodev *iodev)
{
	struct bt_node *active = (struct bt_node *)iodev->active_node;

	return active->profile_dev;
}

/* Adds a profile specific iodev to btio. */
static struct cras_ionode *add_profile_dev(struct cras_iodev *bt_iodev,
					   struct cras_iodev *dev,
					   enum cras_bt_device_profile profile)
{
	struct bt_node *n;
	struct bt_io *btio = (struct bt_io *)bt_iodev;

	n = (struct bt_node *)calloc(1, sizeof(*n));
	if (!n)
		return NULL;

	n->base.dev = bt_iodev;
	n->base.idx = btio->next_node_id++;
	n->base.type = CRAS_NODE_TYPE_BLUETOOTH;
	n->base.volume = 100;
	n->base.stable_id = dev->info.stable_id;
	n->base.stable_id_new = dev->info.stable_id_new;
	n->base.max_software_gain = 0;
	gettimeofday(&n->base.plugged_time, NULL);

	strcpy(n->base.name, dev->info.name);
	n->profile_dev = dev;
	n->profile = profile;

	cras_iodev_add_node(bt_iodev, &n->base);
	return &n->base;
}

/* Forces bt device to switch to use the given profile. Note that if
 * it has already been open for streaming, the new active profile will
 * take effect after the related btio(s) are reopened.
 */
static void bt_switch_to_profile(struct cras_bt_device *device,
				 enum cras_bt_device_profile profile)
{
	switch (profile) {
	case CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY:
	case CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY:
		cras_bt_device_set_active_profile(device,
				CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY |
				CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY);
		break;
	case CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE:
		cras_bt_device_set_active_profile(device,
				CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
		break;
	default:
		syslog(LOG_ERR, "Unexpect profile %u", profile);
		break;
	}
}

/* Checks if bt device is active for the given profile.
 */
static int device_using_profile(struct cras_bt_device *device,
				unsigned int profile)
{
	return cras_bt_device_get_active_profile(device) & profile;
}

/* Checks if the condition is met to switch to a different profile
 * when trying to set the format to btio before open it. Base on two
 * rules:
 * (1) Prefer to use A2DP for output since the audio quality is better.
 * (2) Must use HFP/HSP for input since A2DP doesn't support audio input.
 *
 * If the profile switch happens, return non-zero error code, otherwise
 * return zero.
 */
static int update_supported_formats(struct cras_iodev *iodev)
{
	struct bt_io *btio = (struct bt_io *)iodev;
	struct cras_iodev *dev = active_profile_dev(iodev);
	int rc, length, i;

	/* Force to use HFP if opening input dev. */
	if (device_using_profile(btio->device,
				 CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE) &&
	    iodev->direction == CRAS_STREAM_INPUT) {
		bt_switch_to_profile(btio->device,
				     CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY);
		cras_bt_device_switch_profile_enable_dev(btio->device, iodev);
		return -EAGAIN;
	}

	if (dev->format == NULL) {
		dev->format = (struct cras_audio_format *)
				malloc(sizeof(*dev->format));
		*dev->format = *iodev->format;
	}

	if (dev->update_supported_formats) {
		rc = dev->update_supported_formats(dev);
		if (rc)
			return rc;
	}

	/* Fill in the supported rates and channel counts. */
	for (length = 0; dev->supported_rates[length]; length++);
	free(iodev->supported_rates);
	iodev->supported_rates = (size_t *)malloc(
			(length + 1) * sizeof(*iodev->supported_rates));
	for (i = 0; i < length + 1; i++)
		iodev->supported_rates[i] = dev->supported_rates[i];

	for (length = 0; dev->supported_channel_counts[length]; length++);
	iodev->supported_channel_counts = (size_t *)malloc(
		(length + 1) * sizeof(*iodev->supported_channel_counts));
	for (i = 0; i < length + 1; i++)
		iodev->supported_channel_counts[i] =
			dev->supported_channel_counts[i];

	for (length = 0; dev->supported_formats[length]; length++);
	iodev->supported_formats = (snd_pcm_format_t *)malloc(
		(length + 1) * sizeof(*iodev->supported_formats));
	for (i = 0; i < length + 1; i++)
		iodev->supported_formats[i] =
			dev->supported_formats[i];
	return 0;
}

static int open_dev(struct cras_iodev *iodev)
{
	int rc;
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;

	/* Fill back the format iodev is using. */
	*dev->format = *iodev->format;

	rc = dev->open_dev(dev);
	if (rc) {
		/* Free format here to assure the update_supported_format
		 * callback will be called before any future open_dev call. */
		cras_iodev_free_format(iodev);
		return rc;
	}

	iodev->buffer_size = dev->buffer_size;
	iodev->min_buffer_level = dev->min_buffer_level;
	return 0;
}

static int close_dev(struct cras_iodev *iodev)
{
	struct bt_io *btio = (struct bt_io *)iodev;
	int rc;
	struct cras_iodev *dev = active_profile_dev(iodev);

	/* Force back to A2DP if closing HFP. */
	if (device_using_profile(btio->device,
			CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY |
			CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY) &&
	    iodev->direction == CRAS_STREAM_INPUT &&
	    cras_bt_device_has_a2dp(btio->device)) {
		cras_bt_device_set_active_profile(btio->device,
				CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
		cras_bt_device_switch_profile(btio->device, iodev);
	}

	rc = dev->close_dev(dev);
	if (rc < 0)
		return rc;
	cras_iodev_free_format(iodev);
	return 0;
}

static void set_bt_volume(struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);

	if (dev->active_node)
		dev->active_node->volume = iodev->active_node->volume;

	/* The parent bt_iodev could set software_volume_needed flag for cases
	 * that software volume provides better experience across profiles
	 * (HFP and A2DP). Otherwise, use the profile specific implementation
	 * to adjust volume. */
	if (dev->set_volume && !iodev->software_volume_needed)
		dev->set_volume(dev);
}

static int frames_queued(const struct cras_iodev *iodev,
			 struct timespec *tstamp)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->frames_queued(dev, tstamp);
}

static int delay_frames(const struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->delay_frames(dev);
}

static int get_buffer(struct cras_iodev *iodev,
		      struct cras_audio_area **area,
		      unsigned *frames)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->get_buffer(dev, area, frames);
}

static int put_buffer(struct cras_iodev *iodev, unsigned nwritten)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->put_buffer(dev, nwritten);
}

static int flush_buffer(struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->flush_buffer(dev);
}

/* If the first private iodev doesn't match the active profile stored on
 * device, select to the correct private iodev.
 */
static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
			       unsigned dev_enabled)
{
	struct bt_io *btio = (struct bt_io *)iodev;
	struct cras_ionode *node;
	struct bt_node *active = (struct bt_node *)iodev->active_node;

	if (device_using_profile(btio->device, active->profile))
		return;

	/* Switch to the correct dev using active_profile. */
	DL_FOREACH(iodev->nodes, node) {
		struct bt_node *n = (struct bt_node *)node;
		if (n == active)
			continue;

		if (device_using_profile(btio->device, n->profile)) {
			active->profile = n->profile;
			active->profile_dev = n->profile_dev;

			/* Set volume for the new profile. */
			set_bt_volume(iodev);
		}
	}
}

struct cras_iodev *cras_bt_io_create(struct cras_bt_device *device,
				     struct cras_iodev *dev,
				     enum cras_bt_device_profile profile)
{
	int err;
	struct bt_io *btio;
	struct cras_iodev *iodev;
	struct cras_ionode *node;
	struct bt_node *active;

	if (!dev)
		return NULL;

	btio = (struct bt_io *)calloc(1, sizeof(*btio));
	if (!btio)
		goto error;
	btio->device = device;

	iodev = &btio->base;
	iodev->direction = dev->direction;
	strcpy(iodev->info.name, dev->info.name);
	iodev->info.stable_id = dev->info.stable_id;
	iodev->info.stable_id_new = dev->info.stable_id_new;

	iodev->open_dev = open_dev;
	iodev->frames_queued = frames_queued;
	iodev->delay_frames = delay_frames;
	iodev->get_buffer = get_buffer;
	iodev->put_buffer = put_buffer;
	iodev->flush_buffer = flush_buffer;
	iodev->close_dev = close_dev;
	iodev->update_supported_formats = update_supported_formats;
	iodev->update_active_node = update_active_node;
	iodev->software_volume_needed = 1;
	iodev->set_volume = set_bt_volume;
	iodev->no_stream = cras_iodev_default_no_stream_playback;

	/* Create the dummy node set to plugged so it's the only node exposed
	 * to UI, and point it to the first profile dev. */
	active = (struct bt_node *)calloc(1, sizeof(*active));
	if (!active)
		return NULL;
	active->base.dev = iodev;
	active->base.idx = btio->next_node_id++;
	active->base.type = CRAS_NODE_TYPE_BLUETOOTH;
	active->base.volume = 100;
	active->base.plugged = 1;
	active->base.stable_id = SuperFastHash(
		cras_bt_device_object_path(device),
		strlen(cras_bt_device_object_path(device)),
		strlen(cras_bt_device_object_path(device)));
	active->base.stable_id_new = active->base.stable_id;
	active->profile = profile;
	active->profile_dev = dev;
	gettimeofday(&active->base.plugged_time, NULL);
	strcpy(active->base.name, dev->info.name);
	/* The node name exposed to UI should be a valid UTF8 string. */
	if (!is_utf8_string(active->base.name))
		strcpy(active->base.name, DEFAULT_BT_DEVICE_NAME);
	cras_iodev_add_node(iodev, &active->base);

	node = add_profile_dev(&btio->base, dev, profile);
	if (node == NULL)
		goto error;

	/* Default active profile to a2dp whenever it's allowed. */
	if (!cras_bt_device_get_active_profile(device) ||
	    (profile == CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE &&
	     cras_bt_device_can_switch_to_a2dp(device)))
		bt_switch_to_profile(device, profile);

	if (iodev->direction == CRAS_STREAM_OUTPUT)
		err = cras_iodev_list_add_output(iodev);
	else
		err = cras_iodev_list_add_input(iodev);
	if (err)
		goto error;

	cras_iodev_set_active_node(iodev, &active->base);
	return &btio->base;

error:
	if (btio)
		free(btio);
	return NULL;
}

void cras_bt_io_destroy(struct cras_iodev *bt_iodev)
{
	int rc;
	struct bt_io *btio = (struct bt_io *)bt_iodev;
	struct cras_ionode *node;
	struct bt_node *n;

	if (bt_iodev->direction == CRAS_STREAM_OUTPUT)
		rc = cras_iodev_list_rm_output(bt_iodev);
	else
		rc = cras_iodev_list_rm_input(bt_iodev);
	if (rc == -EBUSY)
		return;

	DL_FOREACH(bt_iodev->nodes, node) {
		n = (struct bt_node *)node;
		cras_iodev_rm_node(bt_iodev, node);
		free(n);
	}
	free(btio);
}

struct cras_ionode *cras_bt_io_get_profile(struct cras_iodev *bt_iodev,
					   enum cras_bt_device_profile profile)
{
	struct cras_ionode *node;
	DL_FOREACH(bt_iodev->nodes, node) {
		struct bt_node *n = (struct bt_node *)node;
		if (n->profile & profile)
			return node;
	}
	return NULL;
}

int cras_bt_io_append(struct cras_iodev *bt_iodev,
		      struct cras_iodev *dev,
		      enum cras_bt_device_profile profile)
{
	struct cras_ionode *node;
	struct bt_io *btio = (struct bt_io *)bt_iodev;

	if (cras_bt_io_get_profile(bt_iodev, profile))
		return -EEXIST;

	node = add_profile_dev(bt_iodev, dev, profile);
	if (!node)
		return -ENOMEM;

	if (profile == CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE &&
	    cras_bt_device_can_switch_to_a2dp(btio->device)) {
		bt_switch_to_profile(btio->device,
				     CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
		cras_bt_device_switch_profile(btio->device, bt_iodev);
		syslog(LOG_ERR, "Switch to A2DP on append");
	}
	return 0;
}

int cras_bt_io_on_profile(struct cras_iodev *bt_iodev,
			  enum cras_bt_device_profile profile)
{
	struct bt_node *btnode = (struct bt_node *)bt_iodev->active_node;
	return !!(profile & btnode->profile);
}

int cras_bt_io_update_buffer_size(struct cras_iodev *bt_iodev)
{
	struct cras_iodev *dev = active_profile_dev(bt_iodev);
	if (!dev)
		return -EINVAL;

	bt_iodev->buffer_size = dev->buffer_size;
	return 0;
}

unsigned int cras_bt_io_try_remove(struct cras_iodev *bt_iodev,
				   struct cras_iodev *dev)
{
	struct cras_ionode *node;
	struct bt_node *active, *btnode;
	unsigned int try_profile = 0;

	active = (struct bt_node *)bt_iodev->active_node;

	if (active->profile_dev == dev) {
		DL_FOREACH(bt_iodev->nodes, node) {
			btnode = (struct bt_node *)node;
			/* Skip the active node and the node we're trying
			 * to remove. */
			if (btnode == active || btnode->profile_dev == dev)
				continue;
			try_profile = btnode->profile;
			break;
		}
	} else {
		try_profile = active->profile;
	}
	return try_profile;
}

int cras_bt_io_remove(struct cras_iodev *bt_iodev,
		      struct cras_iodev *dev)
{
	struct cras_ionode *node;
	struct bt_node *btnode;

	DL_FOREACH(bt_iodev->nodes, node) {
		btnode = (struct bt_node *)node;
		if (btnode->profile_dev != dev)
			continue;

		/* If this is the active node, reset it. Otherwise delete
		 * this node. */
		if (node == bt_iodev->active_node) {
			btnode->profile_dev = NULL;
			btnode->profile = 0;
		} else {
			DL_DELETE(bt_iodev->nodes, node);
			free(node);
		}
	}

	/* The node of active profile could have been removed, update it.
	 * Return err when fail to locate the active profile dev. */
	update_active_node(bt_iodev, 0, 1);
	btnode = (struct bt_node *)bt_iodev->active_node;
	if ((btnode->profile == 0) || (btnode->profile_dev == NULL))
		return -EINVAL;

	return 0;
}