summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_hfp_iodev.c
blob: 6a4ced04522c579db3f866efc35fd87229d95833 (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
/* Copyright (c) 2013 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 <stdbool.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <syslog.h>

#include "cras_audio_area.h"
#include "cras_hfp_ag_profile.h"
#include "cras_hfp_iodev.h"
#include "cras_hfp_info.h"
#include "cras_hfp_slc.h"
#include "cras_iodev.h"
#include "cras_system_state.h"
#include "cras_util.h"
#include "utlist.h"

/* Implementation of bluetooth hands-free profile iodev.
 * Members:
 *    base - The cras_iodev structure base class.
 *    device - The assciated bt_device.
 *    slc - Handle to the HFP service level connection.
 *    info - hfp_info taking care of SCO data read/write.
 *    drain_complete - Flag to indicate if valid samples are drained
 *        in no stream state. Only used for output.
 *    filled_zeros - Number of zero data in frames have been filled
 *        to buffer of hfp_info in no stream state. Only used for output
 */
struct hfp_io {
	struct cras_iodev base;
	struct cras_bt_device *device;
	struct hfp_slc_handle *slc;
	struct hfp_info *info;
	bool drain_complete;
	unsigned int filled_zeros;
};

static int update_supported_formats(struct cras_iodev *iodev)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;

	free(iodev->supported_rates);
	iodev->supported_rates = (size_t *)malloc(2 * sizeof(size_t));

	/* 16 bit, mono, 8kHz for narrowband and 16KHz for wideband */
	iodev->supported_rates[0] =
		(hfp_slc_get_selected_codec(hfpio->slc) == HFP_CODEC_ID_MSBC) ?
			16000 :
			8000;
	iodev->supported_rates[1] = 0;

	free(iodev->supported_channel_counts);
	iodev->supported_channel_counts = (size_t *)malloc(2 * sizeof(size_t));
	iodev->supported_channel_counts[0] = 1;
	iodev->supported_channel_counts[1] = 0;

	free(iodev->supported_formats);
	iodev->supported_formats =
		(snd_pcm_format_t *)malloc(2 * sizeof(snd_pcm_format_t));
	iodev->supported_formats[0] = SND_PCM_FORMAT_S16_LE;
	iodev->supported_formats[1] = 0;

	return 0;
}

static int no_stream(struct cras_iodev *iodev, int enable)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;
	struct timespec hw_tstamp;
	unsigned int hw_level;
	unsigned int level_target;

	if (iodev->direction != CRAS_STREAM_OUTPUT)
		return 0;

	hw_level = iodev->frames_queued(iodev, &hw_tstamp);
	if (enable) {
		if (!hfpio->drain_complete && (hw_level <= hfpio->filled_zeros))
			hfpio->drain_complete = 1;
		hfpio->filled_zeros += hfp_fill_output_with_zeros(
			hfpio->info, iodev->buffer_size);
		return 0;
	}

	/* Leave no stream state.*/
	level_target = iodev->min_cb_level;
	if (hfpio->drain_complete) {
		hfp_force_output_level(hfpio->info, level_target);
	} else {
		unsigned int valid_samples = 0;
		if (hw_level > hfpio->filled_zeros)
			valid_samples = hw_level - hfpio->filled_zeros;
		level_target = MAX(level_target, valid_samples);

		if (level_target > hw_level)
			hfp_fill_output_with_zeros(hfpio->info,
						   level_target - hw_level);
		else
			hfp_force_output_level(hfpio->info, level_target);
	}
	hfpio->drain_complete = 0;
	hfpio->filled_zeros = 0;

	return 0;
}

static int frames_queued(const struct cras_iodev *iodev,
			 struct timespec *tstamp)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;

	if (!hfp_info_running(hfpio->info))
		return -1;

	/* Do not enable timestamp mechanism on HFP device because last time
	 * stamp might be a long time ago and it is not really useful. */
	clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
	return hfp_buf_queued(hfpio->info, iodev->direction);
}

static int output_underrun(struct cras_iodev *iodev)
{
	/* Handle it the same way as cras_iodev_output_underrun(). */
	return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level);
}

static int configure_dev(struct cras_iodev *iodev)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;
	int sk, err, mtu;

	/* Assert format is set before opening device. */
	if (iodev->format == NULL)
		return -EINVAL;

	iodev->format->format = SND_PCM_FORMAT_S16_LE;
	cras_iodev_init_audio_area(iodev, iodev->format->num_channels);

	if (hfp_info_running(hfpio->info))
		goto add_dev;

	/*
	 * Might require a codec negotiation before building the sco connection.
	 */
	hfp_slc_codec_connection_setup(hfpio->slc);

	sk = cras_bt_device_sco_connect(hfpio->device,
					hfp_slc_get_selected_codec(hfpio->slc));
	if (sk < 0)
		goto error;

	mtu = cras_bt_device_sco_packet_size(
		hfpio->device, sk, hfp_slc_get_selected_codec(hfpio->slc));

	/* Start hfp_info */
	err = hfp_info_start(sk, mtu, hfp_slc_get_selected_codec(hfpio->slc),
			     hfpio->info);
	if (err)
		goto error;

	hfpio->drain_complete = 0;
	hfpio->filled_zeros = 0;
add_dev:
	hfp_info_add_iodev(hfpio->info, iodev->direction, iodev->format);
	hfp_set_call_status(hfpio->slc, 1);

	iodev->buffer_size = hfp_buf_size(hfpio->info, iodev->direction);

	return 0;
error:
	syslog(LOG_ERR, "Failed to open HFP iodev");
	return -1;
}

static int close_dev(struct cras_iodev *iodev)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;

	hfp_info_rm_iodev(hfpio->info, iodev->direction);
	if (hfp_info_running(hfpio->info) && !hfp_info_has_iodev(hfpio->info)) {
		hfp_info_stop(hfpio->info);
		hfp_set_call_status(hfpio->slc, 0);
	}

	cras_iodev_free_format(iodev);
	cras_iodev_free_audio_area(iodev);
	return 0;
}

static void set_hfp_volume(struct cras_iodev *iodev)
{
	size_t volume;
	struct hfp_io *hfpio = (struct hfp_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(hfpio->slc, volume);
}

static int delay_frames(const struct cras_iodev *iodev)
{
	struct timespec tstamp;

	return frames_queued(iodev, &tstamp);
}

static int get_buffer(struct cras_iodev *iodev, struct cras_audio_area **area,
		      unsigned *frames)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;
	uint8_t *dst = NULL;

	if (!hfp_info_running(hfpio->info))
		return -1;

	hfp_buf_acquire(hfpio->info, iodev->direction, &dst, frames);

	iodev->area->frames = *frames;
	/* HFP is mono only. */
	iodev->area->channels[0].step_bytes =
		cras_get_format_bytes(iodev->format);
	iodev->area->channels[0].buf = dst;

	*area = iodev->area;
	return 0;
}

static int put_buffer(struct cras_iodev *iodev, unsigned nwritten)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;

	if (!hfp_info_running(hfpio->info))
		return -1;

	hfp_buf_release(hfpio->info, iodev->direction, nwritten);
	return 0;
}

static int flush_buffer(struct cras_iodev *iodev)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;
	unsigned nframes;

	if (iodev->direction == CRAS_STREAM_INPUT) {
		nframes = hfp_buf_queued(hfpio->info, iodev->direction);
		hfp_buf_release(hfpio->info, iodev->direction, nframes);
	}
	return 0;
}

static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
			       unsigned dev_enabled)
{
}

int hfp_iodev_is_hsp(struct cras_iodev *iodev)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;
	return hfp_slc_is_hsp(hfpio->slc);
}

void hfp_free_resources(struct hfp_io *hfpio)
{
	struct cras_ionode *node;
	node = hfpio->base.active_node;
	if (node) {
		cras_iodev_rm_node(&hfpio->base, node);
		free(node);
	}
	free(hfpio->base.supported_channel_counts);
	free(hfpio->base.supported_rates);
	free(hfpio->base.supported_formats);
	cras_iodev_free_resources(&hfpio->base);
}

struct cras_iodev *hfp_iodev_create(enum CRAS_STREAM_DIRECTION dir,
				    struct cras_bt_device *device,
				    struct hfp_slc_handle *slc,
				    enum cras_bt_device_profile profile,
				    struct hfp_info *info)
{
	struct hfp_io *hfpio;
	struct cras_iodev *iodev;
	struct cras_ionode *node;
	const char *name;

	hfpio = (struct hfp_io *)calloc(1, sizeof(*hfpio));
	if (!hfpio)
		goto error;

	iodev = &hfpio->base;
	iodev->direction = dir;

	hfpio->device = device;
	hfpio->slc = slc;

	/* 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->configure_dev = configure_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->no_stream = no_stream;
	iodev->close_dev = close_dev;
	iodev->update_supported_formats = update_supported_formats;
	iodev->update_active_node = update_active_node;
	iodev->set_volume = set_hfp_volume;
	iodev->output_underrun = output_underrun;

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

	node->plugged = 1;
	/* If headset mic doesn't support the wideband speech, report a
	 * different node type so UI can set different plug priority. */
	node->type = CRAS_NODE_TYPE_BLUETOOTH;
	if (!hfp_slc_get_wideband_speech_supported(hfpio->slc) &&
	    (dir == 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 iodev and node. */
	cras_iodev_add_node(iodev, node);
	cras_iodev_set_active_node(iodev, node);
	cras_bt_device_append_iodev(device, iodev, profile);

	hfpio->info = info;

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

	ewma_power_disable(&iodev->ewma);

	return iodev;

error:
	if (hfpio) {
		hfp_free_resources(hfpio);
		free(hfpio);
	}
	return NULL;
}

void hfp_iodev_destroy(struct cras_iodev *iodev)
{
	struct hfp_io *hfpio = (struct hfp_io *)iodev;

	cras_bt_device_rm_iodev(hfpio->device, iodev);
	hfp_free_resources(hfpio);
	free(hfpio);
}