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 <pthread.h>
7 #include <sys/param.h>
8 #include <syslog.h>
9 
10 #include "byte_buffer.h"
11 #include "cras_audio_area.h"
12 #include "cras_config.h"
13 #include "cras_iodev.h"
14 #include "cras_iodev_list.h"
15 #include "cras_types.h"
16 #include "cras_util.h"
17 #include "sfh.h"
18 #include "utlist.h"
19 
20 #define LOOPBACK_BUFFER_SIZE 8192
21 
22 static const char *loopdev_names[LOOPBACK_NUM_TYPES] = {
23 	"Post Mix Pre DSP Loopback",
24 	"Post DSP Loopback",
25 };
26 
27 static size_t loopback_supported_rates[] = {
28 	48000, 0
29 };
30 
31 static size_t loopback_supported_channel_counts[] = {
32 	2, 0
33 };
34 
35 static snd_pcm_format_t loopback_supported_formats[] = {
36 	SND_PCM_FORMAT_S16_LE,
37 	0
38 };
39 
40 /* loopack iodev.  Keep state of a loopback device.
41  *    sample_buffer - Pointer to sample buffer.
42  */
43 struct loopback_iodev {
44 	struct cras_iodev base;
45 	enum CRAS_LOOPBACK_TYPE loopback_type;
46 	struct timespec last_filled;
47 	struct byte_buffer *sample_buffer;
48 };
49 
sample_hook(const uint8_t * frames,unsigned int nframes,const struct cras_audio_format * fmt,void * cb_data)50 static int sample_hook(const uint8_t *frames, unsigned int nframes,
51 		       const struct cras_audio_format *fmt,
52 		       void *cb_data)
53 {
54 	struct loopback_iodev *loopdev = (struct loopback_iodev *)cb_data;
55 	struct byte_buffer *sbuf = loopdev->sample_buffer;
56 	unsigned int frame_bytes = cras_get_format_bytes(fmt);
57 	unsigned int frames_to_copy, bytes_to_copy;
58 	struct cras_iodev *edev = cras_iodev_list_get_first_enabled_iodev(
59 			CRAS_STREAM_OUTPUT);
60 
61 	/* If there's no active streams, the logic in frames_queued will fill
62 	 * zeros for loopback capture, do not accept zeros for draining device.
63 	 */
64 	if (!edev || !edev->streams)
65 		return 0;
66 
67 	frames_to_copy = MIN(buf_writable(sbuf) / frame_bytes, nframes);
68 	if (!frames_to_copy)
69 		return 0;
70 
71 	bytes_to_copy = frames_to_copy * frame_bytes;
72 	memcpy(buf_write_pointer(sbuf), frames, bytes_to_copy);
73 	buf_increment_write(sbuf, bytes_to_copy);
74 	clock_gettime(CLOCK_MONOTONIC_RAW, &loopdev->last_filled);
75 
76 	return frames_to_copy;
77 }
78 
register_loopback_hook(enum CRAS_LOOPBACK_TYPE loopback_type,struct cras_iodev * iodev,loopback_hook_t hook,void * cb_data)79 static void register_loopback_hook(enum CRAS_LOOPBACK_TYPE loopback_type,
80 				   struct cras_iodev *iodev,
81 				   loopback_hook_t hook, void *cb_data)
82 {
83 	if (!iodev) {
84 		syslog(LOG_ERR, "Failed to register loopback hook.");
85 		return;
86 	}
87 
88 	if (loopback_type == LOOPBACK_POST_MIX_PRE_DSP)
89 		cras_iodev_register_pre_dsp_hook(iodev, hook, cb_data);
90 	else if (loopback_type == LOOPBACK_POST_DSP)
91 		cras_iodev_register_post_dsp_hook(iodev, hook, cb_data);
92 }
93 
device_enabled_hook(struct cras_iodev * iodev,void * cb_data)94 static void device_enabled_hook(struct cras_iodev *iodev, void *cb_data)
95 {
96 	struct loopback_iodev *loopdev = (struct loopback_iodev *)cb_data;
97 	struct cras_iodev *edev;
98 
99 	if (iodev->direction != CRAS_STREAM_OUTPUT)
100 		return;
101 
102 	/* Register loopback hook onto first enabled iodev. */
103 	edev = cras_iodev_list_get_first_enabled_iodev(
104 			CRAS_STREAM_OUTPUT);
105 	register_loopback_hook(loopdev->loopback_type, edev,
106 			       sample_hook, cb_data);
107 }
108 
device_disabled_hook(struct cras_iodev * iodev,void * cb_data)109 static void device_disabled_hook(struct cras_iodev *iodev, void *cb_data)
110 {
111 	struct loopback_iodev *loopdev = (struct loopback_iodev *)cb_data;
112 
113 	if (iodev->direction != CRAS_STREAM_OUTPUT)
114 		return;
115 
116 	/* Unregister loopback hook from disabled iodev. */
117 	register_loopback_hook(loopdev->loopback_type, iodev, NULL,
118 			       NULL);
119 }
120 
121 /*
122  * iodev callbacks.
123  */
124 
frames_queued(const struct cras_iodev * iodev,struct timespec * hw_tstamp)125 static int frames_queued(const struct cras_iodev *iodev,
126 			 struct timespec *hw_tstamp)
127 {
128 	struct loopback_iodev *loopdev = (struct loopback_iodev *)iodev;
129 	struct byte_buffer *sbuf = loopdev->sample_buffer;
130 	unsigned int frame_bytes = cras_get_format_bytes(iodev->format);
131 	struct cras_iodev *edev = cras_iodev_list_get_first_enabled_iodev(
132 			CRAS_STREAM_OUTPUT);
133 
134 	if (!edev || !edev->streams) {
135 		unsigned int frames_since_last, frames_to_fill, bytes_to_fill;
136 
137 		frames_since_last = cras_frames_since_time(
138 				&loopdev->last_filled,
139 				iodev->format->frame_rate);
140 		frames_to_fill = MIN(buf_writable(sbuf) / frame_bytes,
141 				     frames_since_last);
142 		if (frames_to_fill > 0) {
143 			bytes_to_fill = frames_to_fill * frame_bytes;
144 			memset(buf_write_pointer(sbuf), 0, bytes_to_fill);
145 			buf_increment_write(sbuf, bytes_to_fill);
146 			clock_gettime(CLOCK_MONOTONIC_RAW,
147 				      &loopdev->last_filled);
148 		}
149 	}
150 	*hw_tstamp = loopdev->last_filled;
151 	return buf_queued(sbuf) / frame_bytes;
152 }
153 
delay_frames(const struct cras_iodev * iodev)154 static int delay_frames(const struct cras_iodev *iodev)
155 {
156 	struct timespec tstamp;
157 
158 	return frames_queued(iodev, &tstamp);
159 }
160 
close_record_dev(struct cras_iodev * iodev)161 static int close_record_dev(struct cras_iodev *iodev)
162 {
163 	struct loopback_iodev *loopdev = (struct loopback_iodev *)iodev;
164 	struct byte_buffer *sbuf = loopdev->sample_buffer;
165 	struct cras_iodev *edev;
166 
167 	cras_iodev_free_format(iodev);
168 	cras_iodev_free_audio_area(iodev);
169 	buf_reset(sbuf);
170 
171 	edev = cras_iodev_list_get_first_enabled_iodev(CRAS_STREAM_OUTPUT);
172 	register_loopback_hook(loopdev->loopback_type, edev, NULL, NULL);
173 	cras_iodev_list_set_device_enabled_callback(NULL, NULL, (void *)iodev);
174 
175 	return 0;
176 }
177 
configure_record_dev(struct cras_iodev * iodev)178 static int configure_record_dev(struct cras_iodev *iodev)
179 {
180 	struct loopback_iodev *loopdev = (struct loopback_iodev *)iodev;
181 	struct cras_iodev *edev;
182 
183 	cras_iodev_init_audio_area(iodev, iodev->format->num_channels);
184 	clock_gettime(CLOCK_MONOTONIC_RAW, &loopdev->last_filled);
185 
186 	edev = cras_iodev_list_get_first_enabled_iodev(CRAS_STREAM_OUTPUT);
187 	register_loopback_hook(loopdev->loopback_type, edev, sample_hook,
188 			       (void *)iodev);
189 	cras_iodev_list_set_device_enabled_callback(device_enabled_hook,
190 						    device_disabled_hook,
191 						    (void *)iodev);
192 
193 	return 0;
194 }
195 
get_record_buffer(struct cras_iodev * iodev,struct cras_audio_area ** area,unsigned * frames)196 static int get_record_buffer(struct cras_iodev *iodev,
197 		      struct cras_audio_area **area,
198 		      unsigned *frames)
199 {
200 	struct loopback_iodev *loopdev = (struct loopback_iodev *)iodev;
201 	struct byte_buffer *sbuf = loopdev->sample_buffer;
202 	unsigned int frame_bytes = cras_get_format_bytes(iodev->format);
203 	unsigned int avail_frames = buf_readable(sbuf) / frame_bytes;
204 
205 	*frames = MIN(avail_frames, *frames);
206 	iodev->area->frames = *frames;
207 	cras_audio_area_config_buf_pointers(iodev->area, iodev->format,
208 					    buf_read_pointer(sbuf));
209 	*area = iodev->area;
210 
211 	return 0;
212 }
213 
put_record_buffer(struct cras_iodev * iodev,unsigned nframes)214 static int put_record_buffer(struct cras_iodev *iodev, unsigned nframes)
215 {
216 	struct loopback_iodev *loopdev = (struct loopback_iodev *)iodev;
217 	struct byte_buffer *sbuf = loopdev->sample_buffer;
218 	unsigned int frame_bytes = cras_get_format_bytes(iodev->format);
219 
220 	buf_increment_read(sbuf, nframes * frame_bytes);
221 
222 	return 0;
223 }
224 
flush_record_buffer(struct cras_iodev * iodev)225 static int flush_record_buffer(struct cras_iodev *iodev)
226 {
227 	struct loopback_iodev *loopdev = (struct loopback_iodev *)iodev;
228 	struct byte_buffer *sbuf = loopdev->sample_buffer;
229 	unsigned int queued_bytes = buf_queued(sbuf);
230 	buf_increment_read(sbuf, queued_bytes);
231 	return 0;
232 }
233 
update_active_node(struct cras_iodev * iodev,unsigned node_idx,unsigned dev_enabled)234 static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
235 			       unsigned dev_enabled)
236 {
237 }
238 
create_loopback_iodev(enum CRAS_LOOPBACK_TYPE type)239 static struct cras_iodev *create_loopback_iodev(enum CRAS_LOOPBACK_TYPE type)
240 {
241 	struct loopback_iodev *loopback_iodev;
242 	struct cras_iodev *iodev;
243 
244 	loopback_iodev = calloc(1, sizeof(*loopback_iodev));
245 	if (loopback_iodev == NULL)
246 		return NULL;
247 
248 	loopback_iodev->sample_buffer = byte_buffer_create(1024*16*4);
249 	if (loopback_iodev->sample_buffer == NULL) {
250 		free(loopback_iodev);
251 		return NULL;
252 	}
253 
254 	loopback_iodev->loopback_type = type;
255 
256 	iodev = &loopback_iodev->base;
257 	iodev->direction = CRAS_STREAM_INPUT;
258 	snprintf(iodev->info.name, ARRAY_SIZE(iodev->info.name), "%s",
259 		 loopdev_names[type]);
260 	iodev->info.name[ARRAY_SIZE(iodev->info.name) - 1] = '\0';
261 	iodev->info.stable_id = SuperFastHash(iodev->info.name,
262 					      strlen(iodev->info.name),
263 					      strlen(iodev->info.name));
264 	iodev->info.stable_id_new = iodev->info.stable_id;
265 
266 	iodev->supported_rates = loopback_supported_rates;
267 	iodev->supported_channel_counts = loopback_supported_channel_counts;
268 	iodev->supported_formats = loopback_supported_formats;
269 	iodev->buffer_size = LOOPBACK_BUFFER_SIZE;
270 
271 	iodev->frames_queued = frames_queued;
272 	iodev->delay_frames = delay_frames;
273 	iodev->update_active_node = update_active_node;
274 	iodev->configure_dev = configure_record_dev;
275 	iodev->close_dev = close_record_dev;
276 	iodev->get_buffer = get_record_buffer;
277 	iodev->put_buffer = put_record_buffer;
278 	iodev->flush_buffer = flush_record_buffer;
279 
280 	return iodev;
281 }
282 
283 /*
284  * Exported Interface.
285  */
286 
loopback_iodev_create(enum CRAS_LOOPBACK_TYPE type)287 struct cras_iodev *loopback_iodev_create(enum CRAS_LOOPBACK_TYPE type)
288 {
289 	struct cras_iodev *iodev;
290 	struct cras_ionode *node;
291 	enum CRAS_NODE_TYPE node_type;
292 
293 	switch (type) {
294 	case LOOPBACK_POST_MIX_PRE_DSP:
295 		node_type = CRAS_NODE_TYPE_POST_MIX_PRE_DSP;
296 		break;
297 	case LOOPBACK_POST_DSP:
298 		node_type = CRAS_NODE_TYPE_POST_DSP;
299 		break;
300 	default:
301 		return NULL;
302 	}
303 
304 	iodev = create_loopback_iodev(type);
305 	if (iodev == NULL)
306 		return NULL;
307 
308 	/* Create a dummy ionode */
309 	node = (struct cras_ionode *)calloc(1, sizeof(*node));
310 	node->dev = iodev;
311 	node->type = node_type;
312 	node->plugged = 1;
313 	node->volume = 100;
314 	node->stable_id = iodev->info.stable_id;
315 	node->stable_id_new = iodev->info.stable_id_new;
316 	node->software_volume_needed = 0;
317 	node->max_software_gain = 0;
318 	strcpy(node->name, loopdev_names[type]);
319 	cras_iodev_add_node(iodev, node);
320 	cras_iodev_set_active_node(iodev, node);
321 
322 	cras_iodev_list_add_input(iodev);
323 
324 	return iodev;
325 }
326 
loopback_iodev_destroy(struct cras_iodev * iodev)327 void loopback_iodev_destroy(struct cras_iodev *iodev)
328 {
329 	struct loopback_iodev *loopdev = (struct loopback_iodev *)iodev;
330 	struct byte_buffer *sbuf = loopdev->sample_buffer;
331 
332 	cras_iodev_list_rm_input(iodev);
333         free(iodev->nodes);
334 
335 	byte_buffer_destroy(&sbuf);
336 	free(loopdev);
337 }
338