summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_hfp_alsa_iodev.c
blob: c1b60b30fe939ebf70dca01bce3cef39ce230ee3 (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
/* Copyright 2019 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 <sys/socket.h>
#include <sys/time.h>
#include <syslog.h>

#include "cras_audio_area.h"
#include "cras_hfp_slc.h"
#include "cras_iodev.h"
#include "cras_system_state.h"
#include "cras_util.h"
#include "utlist.h"
#include "cras_bt_device.h"

#include "cras_hfp_alsa_iodev.h"

/* Object to represent a special HFP iodev which would be managed by bt_io but
 * playback/capture via an inner ALSA iodev.
 * Members:
 *    base - The base class cras_iodev.
 *    device - The corresponding remote BT device.
 *    slc - The service level connection.
 *    aio - The effective iodev for playback/capture.
 */
struct hfp_alsa_io {
	struct cras_iodev base;
	struct cras_bt_device *device;
	struct hfp_slc_handle *slc;
	struct cras_iodev *aio;
};

static int hfp_alsa_get_valid_frames(struct cras_iodev *iodev,
				     struct timespec *hw_tstamp)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->get_valid_frames(aio, hw_tstamp);
}

static int hfp_alsa_open_dev(struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->open_dev(aio);
}

static int hfp_alsa_update_supported_formats(struct cras_iodev *iodev)
{
	/* 16 bit, mono, 8kHz (narrow band speech); */
	free(iodev->supported_rates);
	iodev->supported_rates = malloc(2 * sizeof(*iodev->supported_rates));
	if (!iodev->supported_rates)
		return -ENOMEM;
	iodev->supported_rates[0] = 8000;
	iodev->supported_rates[1] = 0;

	free(iodev->supported_channel_counts);
	iodev->supported_channel_counts =
		malloc(2 * sizeof(*iodev->supported_channel_counts));
	if (!iodev->supported_channel_counts)
		return -ENOMEM;
	iodev->supported_channel_counts[0] = 1;
	iodev->supported_channel_counts[1] = 0;

	free(iodev->supported_formats);
	iodev->supported_formats =
		malloc(2 * sizeof(*iodev->supported_formats));
	if (!iodev->supported_formats)
		return -ENOMEM;
	iodev->supported_formats[0] = SND_PCM_FORMAT_S16_LE;
	iodev->supported_formats[1] = 0;

	return 0;
}

static int hfp_alsa_configure_dev(struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;
	int rc;

	/* Fill back the format iodev is using. */
	if (aio->format == NULL) {
		aio->format = (struct cras_audio_format *)malloc(
			sizeof(*aio->format));
		if (!aio->format)
			return -ENOMEM;
		*aio->format = *iodev->format;
	}

	rc = aio->configure_dev(aio);
	if (rc) {
		syslog(LOG_ERR, "Failed to configure aio: %d\n", rc);
		return rc;
	}

	rc = cras_bt_device_get_sco(
		hfp_alsa_io->device,
		hfp_slc_get_selected_codec(hfp_alsa_io->slc));
	if (rc < 0) {
		syslog(LOG_ERR, "Failed to get sco: %d\n", rc);
		return rc;
	}

	hfp_set_call_status(hfp_alsa_io->slc, 1);
	iodev->buffer_size = aio->buffer_size;

	return 0;
}

static int hfp_alsa_close_dev(struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	hfp_set_call_status(hfp_alsa_io->slc, 0);
	cras_bt_device_put_sco(hfp_alsa_io->device);
	cras_iodev_free_format(iodev);
	return aio->close_dev(aio);
}

static int hfp_alsa_frames_queued(const struct cras_iodev *iodev,
				  struct timespec *tstamp)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->frames_queued(aio, tstamp);
}

static int hfp_alsa_delay_frames(const struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->delay_frames(aio);
}

static int hfp_alsa_get_buffer(struct cras_iodev *iodev,
			       struct cras_audio_area **area, unsigned *frames)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->get_buffer(aio, area, frames);
}

static int hfp_alsa_put_buffer(struct cras_iodev *iodev, unsigned nwritten)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->put_buffer(aio, nwritten);
}

static int hfp_alsa_flush_buffer(struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->flush_buffer(aio);
}

static void hfp_alsa_update_active_node(struct cras_iodev *iodev,
					unsigned node_idx, unsigned dev_enabled)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	aio->update_active_node(aio, node_idx, dev_enabled);
}

static int hfp_alsa_start(const struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->start(aio);
}

static void hfp_alsa_set_volume(struct cras_iodev *iodev)
{
	size_t volume;
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;

	volume = cras_system_get_volume();
	if (iodev->active_node)
		volume = cras_iodev_adjust_node_volume(iodev->active_node,
						       volume);

	hfp_event_speaker_gain(hfp_alsa_io->slc, volume);
}

static int hfp_alsa_no_stream(struct cras_iodev *iodev, int enable)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	/*
	 * Copy iodev->min_cb_level and iodev->max_cb_level from the parent
	 * (i.e. hfp_alsa_iodev).  no_stream() of alsa_io will use them.
	 */
	aio->min_cb_level = iodev->min_cb_level;
	aio->max_cb_level = iodev->max_cb_level;
	return aio->no_stream(aio, enable);
}

static int hfp_alsa_is_free_running(const struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	return aio->is_free_running(aio);
}

static int hfp_alsa_output_underrun(struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_iodev *aio = hfp_alsa_io->aio;

	/*
	 * Copy iodev->min_cb_level and iodev->max_cb_level from the parent
	 * (i.e. hfp_alsa_iodev).  output_underrun() of alsa_io will use them.
	 */
	aio->min_cb_level = iodev->min_cb_level;
	aio->max_cb_level = iodev->max_cb_level;

	return aio->output_underrun(aio);
}

struct cras_iodev *hfp_alsa_iodev_create(struct cras_iodev *aio,
					 struct cras_bt_device *device,
					 struct hfp_slc_handle *slc,
					 enum cras_bt_device_profile profile)
{
	struct hfp_alsa_io *hfp_alsa_io;
	struct cras_iodev *iodev;
	struct cras_ionode *node;
	const char *name;

	hfp_alsa_io = calloc(1, sizeof(*hfp_alsa_io));
	if (!hfp_alsa_io)
		return NULL;

	iodev = &hfp_alsa_io->base;
	iodev->direction = aio->direction;

	hfp_alsa_io->device = device;
	hfp_alsa_io->slc = slc;
	hfp_alsa_io->aio = aio;

	/* Set iodev's name to device readable name or the address. */
	name = cras_bt_device_name(device);
	if (!name)
		name = cras_bt_device_object_path(device);
	snprintf(iodev->info.name, sizeof(iodev->info.name), "%s", name);
	iodev->info.name[ARRAY_SIZE(iodev->info.name) - 1] = 0;
	iodev->info.stable_id = cras_bt_device_get_stable_id(device);

	iodev->open_dev = hfp_alsa_open_dev;
	iodev->update_supported_formats = hfp_alsa_update_supported_formats;
	iodev->configure_dev = hfp_alsa_configure_dev;
	iodev->close_dev = hfp_alsa_close_dev;

	iodev->frames_queued = hfp_alsa_frames_queued;
	iodev->delay_frames = hfp_alsa_delay_frames;
	iodev->get_buffer = hfp_alsa_get_buffer;
	iodev->put_buffer = hfp_alsa_put_buffer;
	iodev->flush_buffer = hfp_alsa_flush_buffer;

	iodev->update_active_node = hfp_alsa_update_active_node;
	iodev->start = hfp_alsa_start;
	iodev->set_volume = hfp_alsa_set_volume;
	iodev->get_valid_frames = hfp_alsa_get_valid_frames;
	iodev->no_stream = hfp_alsa_no_stream;
	iodev->is_free_running = hfp_alsa_is_free_running;
	iodev->output_underrun = hfp_alsa_output_underrun;

	iodev->min_buffer_level = aio->min_buffer_level;

	node = calloc(1, sizeof(*node));
	node->dev = iodev;
	strcpy(node->name, iodev->info.name);

	node->plugged = 1;
	/* If headset mic uses legacy narrow band, i.e CVSD codec, report a
	 * different node type so UI can set different plug priority. */
	node->type = CRAS_NODE_TYPE_BLUETOOTH;
	if ((hfp_slc_get_selected_codec(hfp_alsa_io->slc) ==
	     HFP_CODEC_ID_CVSD) &&
	    (iodev->direction == CRAS_STREAM_INPUT))
		node->type = CRAS_NODE_TYPE_BLUETOOTH_NB_MIC;
	node->volume = 100;
	gettimeofday(&node->plugged_time, NULL);

	/* Prepare active node before append, so bt_io can extract correct
	 * info from hfp_alsa iodev and node. */
	cras_iodev_add_node(iodev, node);
	cras_iodev_set_active_node(iodev, node);
	cras_bt_device_append_iodev(device, iodev, profile);

	/* Record max supported channels into cras_iodev_info. */
	iodev->info.max_supported_channels = 1;

	/* Specifically disable EWMA calculation on this and the child iodev. */
	ewma_power_disable(&iodev->ewma);
	ewma_power_disable(&aio->ewma);

	return iodev;
}

void hfp_alsa_iodev_destroy(struct cras_iodev *iodev)
{
	struct hfp_alsa_io *hfp_alsa_io = (struct hfp_alsa_io *)iodev;
	struct cras_ionode *node;

	cras_bt_device_rm_iodev(hfp_alsa_io->device, iodev);

	node = iodev->active_node;
	if (node) {
		cras_iodev_rm_node(iodev, node);
		free(node);
	}

	free(iodev->supported_channel_counts);
	free(iodev->supported_rates);
	free(iodev->supported_formats);
	cras_iodev_free_resources(iodev);

	free(hfp_alsa_io);
}