• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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 <poll.h>
7 #include <syslog.h>
8 
9 #include "audio_thread_log.h"
10 #include "cras_audio_area.h"
11 #include "cras_iodev.h"
12 #include "cras_non_empty_audio_handler.h"
13 #include "cras_rstream.h"
14 #include "cras_server_metrics.h"
15 #include "dev_stream.h"
16 #include "input_data.h"
17 #include "polled_interval_checker.h"
18 #include "utlist.h"
19 
20 #include "dev_io.h"
21 
22 static const struct timespec playback_wake_fuzz_ts = {
23 	0, 500 * 1000 /* 500 usec. */
24 };
25 
26 /* The maximum time to wait before checking the device's non-empty status. */
27 static const int NON_EMPTY_UPDATE_INTERVAL_SEC = 5;
28 
29 /*
30  * The minimum number of consecutive seconds of empty audio that must be
31  * played before a device is considered to be playing empty audio.
32  */
33 static const int MIN_EMPTY_PERIOD_SEC = 30;
34 
35 /* The number of devices playing/capturing non-empty stream(s). */
36 static int non_empty_device_count = 0;
37 
38 /* Gets the master device which the stream is attached to. */
39 static inline
get_master_dev(const struct dev_stream * stream)40 struct cras_iodev *get_master_dev(const struct dev_stream *stream)
41 {
42 	return (struct cras_iodev *)stream->stream->master_dev.dev_ptr;
43 }
44 
45 /* Updates the estimated sample rate of open device to all attached
46  * streams.
47  */
update_estimated_rate(struct open_dev * adev)48 static void update_estimated_rate(struct open_dev *adev)
49 {
50 	struct cras_iodev *master_dev;
51 	struct cras_iodev *dev = adev->dev;
52 	struct dev_stream *dev_stream;
53 
54 	DL_FOREACH(dev->streams, dev_stream) {
55 		master_dev = get_master_dev(dev_stream);
56 		if (master_dev == NULL) {
57 			syslog(LOG_ERR, "Fail to find master open dev.");
58 			continue;
59 		}
60 
61 		dev_stream_set_dev_rate(dev_stream,
62 				dev->ext_format->frame_rate,
63 				cras_iodev_get_est_rate_ratio(dev),
64 				cras_iodev_get_est_rate_ratio(master_dev),
65 				adev->coarse_rate_adjust);
66 	}
67 }
68 
69 /*
70  * Counts the number of devices which are currently playing/capturing non-empty
71  * audio.
72  */
count_non_empty_dev(struct open_dev * adevs)73 static inline int count_non_empty_dev(struct open_dev *adevs) {
74 	int count = 0;
75 	struct open_dev *adev;
76 	DL_FOREACH(adevs, adev) {
77 		if (!adev->empty_pi || !pic_interval_elapsed(adev->empty_pi))
78 			count++;
79 	}
80 	return count;
81 }
82 
check_non_empty_state_transition(struct open_dev * adevs)83 static void check_non_empty_state_transition(struct open_dev *adevs) {
84 	int new_non_empty_dev_count = count_non_empty_dev(adevs);
85 
86 	// If we have transitioned to or from a state with 0 non-empty devices,
87 	// notify the main thread to update system state.
88 	if ((non_empty_device_count == 0) != (new_non_empty_dev_count == 0))
89 		cras_non_empty_audio_send_msg(
90 			new_non_empty_dev_count > 0 ? 1 : 0);
91 
92 	non_empty_device_count = new_non_empty_dev_count;
93 }
94 
95 /* Asks any stream with room for more data. Sets the time stamp for all streams.
96  * Args:
97  *    adev - The output device streams are attached to.
98  * Returns:
99  *    0 on success, negative error on failure. If failed, can assume that all
100  *    streams have been removed from the device.
101  */
fetch_streams(struct open_dev * adev)102 static int fetch_streams(struct open_dev *adev)
103 {
104 	struct dev_stream *dev_stream;
105 	struct cras_iodev *odev = adev->dev;
106 	int rc;
107 	int delay;
108 
109 	delay = cras_iodev_delay_frames(odev);
110 	if (delay < 0)
111 		return delay;
112 
113 	DL_FOREACH(adev->dev->streams, dev_stream) {
114 		struct cras_rstream *rstream = dev_stream->stream;
115 		struct cras_audio_shm *shm =
116 			cras_rstream_output_shm(rstream);
117 		const struct timespec *next_cb_ts;
118 		struct timespec now;
119 
120 		clock_gettime(CLOCK_MONOTONIC_RAW, &now);
121 
122 		if (dev_stream_is_pending_reply(dev_stream)) {
123 			dev_stream_flush_old_audio_messages(dev_stream);
124 			cras_rstream_record_fetch_interval(dev_stream->stream,
125 							   &now);
126 		}
127 
128 		if (cras_shm_get_frames(shm) < 0)
129 			cras_rstream_set_is_draining(rstream, 1);
130 
131 		if (cras_rstream_get_is_draining(dev_stream->stream))
132 			continue;
133 
134 		next_cb_ts = dev_stream_next_cb_ts(dev_stream);
135 		if (!next_cb_ts)
136 			continue;
137 
138 		/* Check if it's time to get more data from this stream.
139 		 * Allow for waking up a little early. */
140 		add_timespecs(&now, &playback_wake_fuzz_ts);
141 		if (!timespec_after(&now, next_cb_ts))
142 			continue;
143 
144 		if (!dev_stream_can_fetch(dev_stream)) {
145 			ATLOG(atlog, AUDIO_THREAD_STREAM_SKIP_CB,
146 			      cras_rstream_id(rstream),
147 			      shm->area->write_offset[0],
148 			      shm->area->write_offset[1]);
149 			continue;
150 		}
151 
152 		dev_stream_set_delay(dev_stream, delay);
153 
154 		ATLOG(atlog, AUDIO_THREAD_FETCH_STREAM, rstream->stream_id,
155 		      cras_rstream_get_cb_threshold(rstream), delay);
156 
157 		rc = dev_stream_request_playback_samples(dev_stream, &now);
158 		if (rc < 0) {
159 			syslog(LOG_ERR, "fetch err: %d for %x",
160 			       rc, cras_rstream_id(rstream));
161 			cras_rstream_set_is_draining(rstream, 1);
162 		}
163 	}
164 
165 	return 0;
166 }
167 
168 /* Gets the max delay frames of open input devices. */
input_delay_frames(struct open_dev * adevs)169 static int input_delay_frames(struct open_dev *adevs)
170 {
171 	struct open_dev *adev;
172 	int delay;
173 	int max_delay = 0;
174 
175 	DL_FOREACH(adevs, adev) {
176 		if (!cras_iodev_is_open(adev->dev))
177 			continue;
178 		delay = cras_iodev_delay_frames(adev->dev);
179 		if (delay < 0)
180 			return delay;
181 		if (delay > max_delay)
182 			max_delay = delay;
183 	}
184 	return max_delay;
185 }
186 
187 /* Sets the stream delay.
188  * Args:
189  *    adev[in] - The device to capture from.
190  */
set_stream_delay(struct open_dev * adev)191 static unsigned int set_stream_delay(struct open_dev *adev)
192 {
193 	struct dev_stream *stream;
194 	int delay;
195 
196 	/* TODO(dgreid) - Setting delay from last dev only. */
197 	delay = input_delay_frames(adev);
198 
199 	DL_FOREACH(adev->dev->streams, stream) {
200 		if (stream->stream->flags & TRIGGER_ONLY)
201 			continue;
202 
203 		dev_stream_set_delay(stream, delay);
204 	}
205 
206 	return 0;
207 }
208 
209 /* Gets the minimum amount of space available for writing across all streams.
210  * Args:
211  *    adev[in] - The device to capture from.
212  *    write_limit[in] - Initial limit to number of frames to capture.
213  *    limit_stream[out] - The pointer to the pointer of stream which
214  *                        causes capture limit.
215  *                        Output NULL if there is no stream that causes
216  *                        capture limit less than the initial limit.
217  */
get_stream_limit(struct open_dev * adev,unsigned int write_limit,struct dev_stream ** limit_stream)218 static unsigned int get_stream_limit(
219 		struct open_dev *adev,
220 		unsigned int write_limit,
221 		struct dev_stream **limit_stream)
222 {
223 	struct cras_rstream *rstream;
224 	struct cras_audio_shm *shm;
225 	struct dev_stream *stream;
226 	unsigned int avail;
227 
228 	*limit_stream = NULL;
229 
230 	DL_FOREACH(adev->dev->streams, stream) {
231 		rstream = stream->stream;
232 		if (rstream->flags & TRIGGER_ONLY)
233 			continue;
234 
235 		shm = cras_rstream_input_shm(rstream);
236 		if (cras_shm_check_write_overrun(shm))
237 			ATLOG(atlog, AUDIO_THREAD_READ_OVERRUN,
238 			      adev->dev->info.idx, rstream->stream_id,
239 			      shm->area->num_overruns);
240 		avail = dev_stream_capture_avail(stream);
241 		if (avail < write_limit) {
242 			write_limit = avail;
243 			*limit_stream = stream;
244 		}
245 	}
246 
247 	return write_limit;
248 }
249 
250 /*
251  * The minimum wake time for a input device, which is 5ms. It's only used by
252  * function get_input_dev_max_wake_ts.
253  */
254 static const struct timespec min_input_dev_wake_ts = {
255 	0, 5 * 1000 * 1000 /* 5 ms. */
256 };
257 
258 /*
259  * Get input device maximum sleep time, which is the approximate time that the
260  * device will have hw_level = buffer_size / 2 samples. Some devices have
261  * capture period = 2 so the audio_thread should wake up and consume some
262  * samples from hardware at that time. To prevent busy loop occurs, the returned
263  * sleep time should be >= 5ms.
264  *
265  * Returns: 0 on success negative error on device failure.
266  */
get_input_dev_max_wake_ts(struct open_dev * adev,unsigned int curr_level,struct timespec * res_ts)267 static int get_input_dev_max_wake_ts(
268 	struct open_dev *adev,
269 	unsigned int curr_level,
270 	struct timespec *res_ts)
271 {
272 	struct timespec dev_wake_ts, now;
273 	unsigned int dev_rate, half_buffer_size, target_frames;
274 
275 	if(!adev || !adev->dev || !adev->dev->format ||
276 	   !adev->dev->format->frame_rate || !adev->dev->buffer_size)
277 		return -EINVAL;
278 
279 	*res_ts = min_input_dev_wake_ts;
280 
281 	dev_rate = adev->dev->format->frame_rate;
282 	half_buffer_size = adev->dev->buffer_size / 2;
283 	if(curr_level < half_buffer_size)
284 		target_frames = half_buffer_size - curr_level;
285 	else
286 		target_frames = 0;
287 
288 	cras_frames_to_time(target_frames, dev_rate, &dev_wake_ts);
289 
290 	if (timespec_after(&dev_wake_ts, res_ts)) {
291 		*res_ts = dev_wake_ts;
292 	}
293 
294 	clock_gettime(CLOCK_MONOTONIC_RAW, &now);
295 	add_timespecs(res_ts, &now);
296 	return 0;
297 }
298 
299 /*
300  * Set wake_ts for this device to be the earliest wake up time for
301  * dev_streams.
302  */
set_input_dev_wake_ts(struct open_dev * adev)303 static int set_input_dev_wake_ts(struct open_dev *adev)
304 {
305 	int rc;
306 	struct timespec level_tstamp, wake_time_out, min_ts, now, dev_wake_ts;
307 	unsigned int curr_level, cap_limit;
308 	struct dev_stream *stream;
309 	struct dev_stream *cap_limit_stream;
310 
311 	/* Limit the sleep time to 20 seconds. */
312 	min_ts.tv_sec = 20;
313 	min_ts.tv_nsec = 0;
314 	clock_gettime(CLOCK_MONOTONIC_RAW, &now);
315 	add_timespecs(&min_ts, &now);
316 
317 	rc = cras_iodev_frames_queued(adev->dev, &level_tstamp);
318 	if (rc < 0)
319 		return rc;
320 	curr_level = rc;
321 	if (!timespec_is_nonzero(&level_tstamp))
322 		clock_gettime(CLOCK_MONOTONIC_RAW, &level_tstamp);
323 
324 
325 	cap_limit = get_stream_limit(adev, UINT_MAX, &cap_limit_stream);
326 
327 	/*
328 	 * Loop through streams to find the earliest time audio thread
329 	 * should wake up.
330 	 */
331 	DL_FOREACH(adev->dev->streams, stream) {
332 		wake_time_out = min_ts;
333 		rc = dev_stream_wake_time(
334 			stream,
335 			curr_level,
336 			&level_tstamp,
337 			cap_limit,
338 			cap_limit_stream == stream,
339 			&wake_time_out);
340 
341 		/*
342 		 * rc > 0 means there is no need to set wake up time for this
343 		 * stream.
344 		 */
345 		if (rc > 0)
346 			continue;
347 
348 		if (rc < 0)
349 			return rc;
350 
351 		if (timespec_after(&min_ts, &wake_time_out)) {
352 			min_ts = wake_time_out;
353 		}
354 	}
355 
356 	if(adev->dev->active_node &&
357 	   adev->dev->active_node->type != CRAS_NODE_TYPE_HOTWORD) {
358 		rc = get_input_dev_max_wake_ts(adev, curr_level, &dev_wake_ts);
359 		if(rc < 0) {
360 			syslog(LOG_ERR,
361 			       "Failed to call get_input_dev_max_wake_ts."
362 			       "rc = %d", rc);
363 		} else if(timespec_after(&min_ts, &dev_wake_ts)) {
364 			min_ts = dev_wake_ts;
365 		}
366 	}
367 
368 	adev->wake_ts = min_ts;
369 	return rc;
370 }
371 
372 /* Read samples from an input device to the specified stream.
373  * Args:
374  *    adev - The device to capture samples from.
375  * Returns 0 on success.
376  */
capture_to_streams(struct open_dev * adev)377 static int capture_to_streams(struct open_dev *adev)
378 {
379 	struct cras_iodev *idev = adev->dev;
380 	snd_pcm_uframes_t remainder, hw_level, cap_limit;
381 	struct timespec hw_tstamp;
382 	int rc;
383 	struct dev_stream *cap_limit_stream;
384 	struct dev_stream *stream;
385 
386 	DL_FOREACH(adev->dev->streams, stream)
387 		dev_stream_flush_old_audio_messages(stream);
388 
389 	rc = cras_iodev_frames_queued(idev, &hw_tstamp);
390 	if (rc < 0)
391 		return rc;
392 	hw_level = rc;
393 
394 	cras_iodev_update_highest_hw_level(idev, hw_level);
395 
396 	ATLOG(atlog, AUDIO_THREAD_READ_AUDIO_TSTAMP, idev->info.idx,
397 	      hw_tstamp.tv_sec, hw_tstamp.tv_nsec);
398 	if (timespec_is_nonzero(&hw_tstamp)) {
399 		if (hw_level < idev->min_cb_level / 2)
400 			adev->coarse_rate_adjust = 1;
401 		else if (hw_level > idev->max_cb_level * 2)
402 			adev->coarse_rate_adjust = -1;
403 		else
404 			adev->coarse_rate_adjust = 0;
405 		if (cras_iodev_update_rate(idev, hw_level, &hw_tstamp))
406 			update_estimated_rate(adev);
407 	}
408 
409 	cap_limit = get_stream_limit(adev, hw_level, &cap_limit_stream);
410 	set_stream_delay(adev);
411 
412 	remainder = MIN(hw_level, cap_limit);
413 
414 	ATLOG(atlog, AUDIO_THREAD_READ_AUDIO, idev->info.idx,
415 	      hw_level, remainder);
416 
417 	if (cras_iodev_state(idev) != CRAS_IODEV_STATE_NORMAL_RUN)
418 		return 0;
419 
420 	while (remainder > 0) {
421 		struct cras_audio_area *area = NULL;
422 		unsigned int nread, total_read;
423 
424 		nread = remainder;
425 
426 		rc = cras_iodev_get_input_buffer(idev, &nread);
427 		if (rc < 0 || nread == 0)
428 			return rc;
429 
430 		DL_FOREACH(adev->dev->streams, stream) {
431 			unsigned int this_read;
432 			unsigned int area_offset;
433 			float software_gain_scaler;
434 
435 			if ((stream->stream->flags & TRIGGER_ONLY) &&
436 			    stream->stream->triggered)
437 				continue;
438 
439 			input_data_get_for_stream(idev->input_data, stream->stream,
440 						  idev->buf_state,
441 						  &area, &area_offset);
442 			/*
443 			 * APM has more advanced gain control mechanism, so
444 			 * don't apply the CRAS software gain to this stream
445 			 * if APM is used.
446 			 */
447 			software_gain_scaler = stream->stream->apm_list
448 				? 1.0f
449 				: cras_iodev_get_software_gain_scaler(idev);
450 
451 			this_read = dev_stream_capture(
452 					stream, area, area_offset,
453 					software_gain_scaler);
454 
455 			input_data_put_for_stream(idev->input_data, stream->stream,
456 						  idev->buf_state, this_read);
457 		}
458 
459 		rc = cras_iodev_put_input_buffer(idev);
460 		if (rc < 0)
461 			return rc;
462 
463 		total_read = rc;
464 		remainder -= nread;
465 
466 		if (total_read < nread)
467 			break;
468 	}
469 
470 	ATLOG(atlog, AUDIO_THREAD_READ_AUDIO_DONE, remainder, 0, 0);
471 
472 	return 0;
473 }
474 
475 /* Fill the buffer with samples from the attached streams.
476  * Args:
477  *    odevs - The list of open output devices, provided so streams can be
478  *            removed from all devices on error.
479  *    adev - The device to write to.
480  *    dst - The buffer to put the samples in (returned from snd_pcm_mmap_begin)
481  *    write_limit - The maximum number of frames to write to dst.
482  *
483  * Returns:
484  *    The number of frames rendered on success, a negative error code otherwise.
485  *    This number of frames is the minimum of the amount of frames each stream
486  *    could provide which is the maximum that can currently be rendered.
487  */
write_streams(struct open_dev ** odevs,struct open_dev * adev,uint8_t * dst,size_t write_limit)488 static int write_streams(struct open_dev **odevs,
489 			 struct open_dev *adev,
490 			 uint8_t *dst,
491 			 size_t write_limit)
492 {
493 	struct cras_iodev *odev = adev->dev;
494 	struct dev_stream *curr;
495 	unsigned int max_offset = 0;
496 	unsigned int frame_bytes = cras_get_format_bytes(odev->ext_format);
497 	unsigned int num_playing = 0;
498 	unsigned int drain_limit = write_limit;
499 
500 	/* Mix as much as we can, the minimum fill level of any stream. */
501 	max_offset = cras_iodev_max_stream_offset(odev);
502 
503         /* Mix as much as we can, the minimum fill level of any stream. */
504 	DL_FOREACH(adev->dev->streams, curr) {
505 		int dev_frames;
506 
507 		/* If this is a single output dev stream, updates the latest
508 		 * number of frames for playback. */
509 		if (dev_stream_attached_devs(curr) == 1)
510 			dev_stream_update_frames(curr);
511 
512 		dev_frames = dev_stream_playback_frames(curr);
513 		if (dev_frames < 0) {
514 			dev_io_remove_stream(
515 				odevs,
516 				curr->stream, NULL);
517 			continue;
518 		}
519 		ATLOG(atlog, AUDIO_THREAD_WRITE_STREAMS_STREAM,
520 		      curr->stream->stream_id, dev_frames,
521 		      dev_stream_is_pending_reply(curr));
522 		if (cras_rstream_get_is_draining(curr->stream)) {
523 			drain_limit = MIN((size_t)dev_frames, drain_limit);
524 			if (!dev_frames)
525 				dev_io_remove_stream(
526 					odevs,
527 					curr->stream, NULL);
528 		} else {
529 			write_limit = MIN((size_t)dev_frames, write_limit);
530 			num_playing++;
531 		}
532 	}
533 
534 	if (!num_playing)
535 		write_limit = drain_limit;
536 
537 	if (write_limit > max_offset)
538 		memset(dst + max_offset * frame_bytes, 0,
539 		       (write_limit - max_offset) * frame_bytes);
540 
541 	ATLOG(atlog, AUDIO_THREAD_WRITE_STREAMS_MIX,
542 	      write_limit, max_offset, 0);
543 
544 	DL_FOREACH(adev->dev->streams, curr) {
545 		unsigned int offset;
546 		int nwritten;
547 
548 		offset = cras_iodev_stream_offset(odev, curr);
549 		if (offset >= write_limit)
550 			continue;
551 		nwritten = dev_stream_mix(curr, odev->ext_format,
552 					  dst + frame_bytes * offset,
553 					  write_limit - offset);
554 
555 		if (nwritten < 0) {
556 			dev_io_remove_stream(odevs, curr->stream, NULL);
557 			continue;
558 		}
559 
560 		cras_iodev_stream_written(odev, curr, nwritten);
561 	}
562 
563 	write_limit = cras_iodev_all_streams_written(odev);
564 
565 	ATLOG(atlog, AUDIO_THREAD_WRITE_STREAMS_MIXED, write_limit, 0, 0);
566 
567 	return write_limit;
568 }
569 
570 /* Update next wake up time of the device.
571  * Args:
572  *    adev[in] - The device to update to.
573  *    hw_level[out] - Pointer to number of frames in hardware.
574  */
update_dev_wakeup_time(struct open_dev * adev,unsigned int * hw_level)575 void update_dev_wakeup_time(struct open_dev *adev, unsigned int *hw_level)
576 {
577 	struct timespec now;
578 	struct timespec sleep_time;
579 	double est_rate;
580 	unsigned int frames_to_play_in_sleep;
581 
582 	clock_gettime(CLOCK_MONOTONIC_RAW, &now);
583 
584 	frames_to_play_in_sleep = cras_iodev_frames_to_play_in_sleep(
585 			adev->dev, hw_level, &adev->wake_ts);
586 	if (!timespec_is_nonzero(&adev->wake_ts))
587 		adev->wake_ts = now;
588 
589 	if (cras_iodev_state(adev->dev) == CRAS_IODEV_STATE_NORMAL_RUN)
590 		cras_iodev_update_highest_hw_level(adev->dev, *hw_level);
591 
592 	est_rate = adev->dev->ext_format->frame_rate *
593 			cras_iodev_get_est_rate_ratio(adev->dev);
594 
595 	ATLOG(atlog, AUDIO_THREAD_SET_DEV_WAKE, adev->dev->info.idx,
596 	      *hw_level, frames_to_play_in_sleep);
597 
598 	cras_frames_to_time_precise(
599 			frames_to_play_in_sleep,
600 			est_rate,
601 			&sleep_time);
602 
603 	add_timespecs(&adev->wake_ts, &sleep_time);
604 
605 	ATLOG(atlog, AUDIO_THREAD_DEV_SLEEP_TIME, adev->dev->info.idx,
606 	      adev->wake_ts.tv_sec, adev->wake_ts.tv_nsec);
607 }
608 
609 /* Returns 0 on success negative error on device failure. */
write_output_samples(struct open_dev ** odevs,struct open_dev * adev,struct cras_fmt_conv * output_converter)610 int write_output_samples(struct open_dev **odevs,
611 			 struct open_dev *adev,
612 			 struct cras_fmt_conv *output_converter)
613 {
614 	struct cras_iodev *odev = adev->dev;
615 	unsigned int hw_level;
616 	struct timespec hw_tstamp;
617 	unsigned int frames, fr_to_req;
618 	snd_pcm_sframes_t written;
619 	snd_pcm_uframes_t total_written = 0;
620 	int rc;
621 	int non_empty = 0;
622 	int *non_empty_ptr = NULL;
623 	uint8_t *dst = NULL;
624 	struct cras_audio_area *area = NULL;
625 
626 	/* Possibly fill zeros for no_stream state and possibly transit state.
627 	 */
628 	rc = cras_iodev_prepare_output_before_write_samples(odev);
629 	if (rc < 0) {
630 		syslog(LOG_ERR, "Failed to prepare output dev for write");
631 		return rc;
632 	}
633 
634 	if (cras_iodev_state(odev) != CRAS_IODEV_STATE_NORMAL_RUN)
635 		return 0;
636 
637 	rc = cras_iodev_frames_queued(odev, &hw_tstamp);
638 	if (rc < 0)
639 		return rc;
640 	hw_level = rc;
641 
642 	ATLOG(atlog, AUDIO_THREAD_FILL_AUDIO_TSTAMP, adev->dev->info.idx,
643 	      hw_tstamp.tv_sec, hw_tstamp.tv_nsec);
644 	if (timespec_is_nonzero(&hw_tstamp)) {
645 		if (hw_level < odev->min_cb_level / 2)
646 			adev->coarse_rate_adjust = 1;
647 		else if (hw_level > odev->max_cb_level * 2)
648 			adev->coarse_rate_adjust = -1;
649 		else
650 			adev->coarse_rate_adjust = 0;
651 
652 		if (cras_iodev_update_rate(odev, hw_level, &hw_tstamp))
653 			update_estimated_rate(adev);
654 	}
655 	ATLOG(atlog, AUDIO_THREAD_FILL_AUDIO, adev->dev->info.idx, hw_level, 0);
656 
657 	/* Don't request more than hardware can hold. Note that min_buffer_level
658 	 * has been subtracted from the actual hw_level so we need to take it
659 	 * into account here. */
660 	fr_to_req = cras_iodev_buffer_avail(odev, hw_level);
661 
662 	/* Have to loop writing to the device, will be at most 2 loops, this
663 	 * only happens when the circular buffer is at the end and returns us a
664 	 * partial area to write to from mmap_begin */
665 	while (total_written < fr_to_req) {
666 		frames = fr_to_req - total_written;
667 		rc = cras_iodev_get_output_buffer(odev, &area, &frames);
668 		if (rc < 0)
669 			return rc;
670 
671 		/* TODO(dgreid) - This assumes interleaved audio. */
672 		dst = area->channels[0].buf;
673 		written = write_streams(odevs, adev, dst, frames);
674 		if (written < 0) /* pcm has been closed */
675 			return (int)written;
676 
677 		if (written < (snd_pcm_sframes_t)frames)
678 			/* Got all the samples from client that we can, but it
679 			 * won't fill the request. */
680 			fr_to_req = 0; /* break out after committing samples */
681 
682 		// This interval is lazily initialized once per device.
683 		// Note that newly opened devices are considered non-empty
684 		// (until their status is updated through the normal flow).
685 		if (!adev->non_empty_check_pi) {
686 			adev->non_empty_check_pi = pic_polled_interval_create(
687 				NON_EMPTY_UPDATE_INTERVAL_SEC);
688 		}
689 
690 		// If we were empty last iteration, or the sampling interval
691 		// has elapsed, check for emptiness.
692 		if (adev->empty_pi ||
693 			pic_interval_elapsed(adev->non_empty_check_pi)) {
694 			non_empty_ptr = &non_empty;
695 			pic_interval_reset(adev->non_empty_check_pi);
696 		}
697 
698 		rc = cras_iodev_put_output_buffer(odev, dst, written,
699 						  non_empty_ptr,
700 						  output_converter);
701 
702 		if (rc < 0)
703 			return rc;
704 		total_written += written;
705 
706 		if (non_empty && adev->empty_pi) {
707 			// We're not empty, but we were previously.
708 			// Reset the empty period.
709 			pic_polled_interval_destroy(&adev->empty_pi);
710 		}
711 
712 		if (non_empty_ptr && !non_empty && !adev->empty_pi)
713 			// We checked for emptiness, we were empty, and we
714 			// previously weren't. Start the empty period.
715 			adev->empty_pi = pic_polled_interval_create(
716 				MIN_EMPTY_PERIOD_SEC);
717 	}
718 
719 	ATLOG(atlog, AUDIO_THREAD_FILL_AUDIO_DONE, hw_level,
720 	      total_written, odev->min_cb_level);
721 
722 	return total_written;
723 }
724 
725 /*
726  * Public funcitons.
727  */
728 
dev_io_send_captured_samples(struct open_dev * idev_list)729 int dev_io_send_captured_samples(struct open_dev *idev_list)
730 {
731 	struct open_dev *adev;
732 	int rc;
733 
734 	// TODO(dgreid) - once per rstream, not once per dev_stream.
735 	DL_FOREACH(idev_list, adev) {
736 		struct dev_stream *stream;
737 
738 		if (!cras_iodev_is_open(adev->dev))
739 			continue;
740 
741 		/* Post samples to rstream if there are enough samples. */
742 		DL_FOREACH(adev->dev->streams, stream) {
743 			dev_stream_capture_update_rstream(stream);
744 		}
745 
746 		/* Set wake_ts for this device. */
747 		rc = set_input_dev_wake_ts(adev);
748 		if (rc < 0)
749 			return rc;
750 	}
751 
752 	return 0;
753 }
754 
handle_dev_err(int err_rc,struct open_dev ** odevs,struct open_dev * adev)755 static void handle_dev_err(
756 		int err_rc,
757 		struct open_dev **odevs,
758 		struct open_dev *adev)
759 {
760 	if (err_rc == -EPIPE) {
761 		/* Handle severe underrun. */
762 		ATLOG(atlog, AUDIO_THREAD_SEVERE_UNDERRUN,
763 		      adev->dev->info.idx, 0, 0);
764 		cras_iodev_reset_request(adev->dev);
765 	} else {
766 		/* Device error, close it. */
767 		dev_io_rm_open_dev(odevs, adev);
768 	}
769 }
770 
dev_io_capture(struct open_dev ** list)771 int dev_io_capture(struct open_dev **list)
772 {
773 	struct open_dev *idev_list = *list;
774 	struct open_dev *adev;
775 	int rc;
776 
777 	DL_FOREACH(idev_list, adev) {
778 		if (!cras_iodev_is_open(adev->dev))
779 			continue;
780 		rc = capture_to_streams(adev);
781 		if (rc < 0)
782 			handle_dev_err(rc, list, adev);
783 	}
784 
785 	return 0;
786 }
787 
dev_io_playback_fetch(struct open_dev * odev_list)788 void dev_io_playback_fetch(struct open_dev *odev_list)
789 {
790 	struct open_dev *adev;
791 
792 	DL_FOREACH(odev_list, adev) {
793 		if (!cras_iodev_is_open(adev->dev))
794 			continue;
795 		fetch_streams(adev);
796 	}
797 }
798 
dev_io_playback_write(struct open_dev ** odevs,struct cras_fmt_conv * output_converter)799 int dev_io_playback_write(struct open_dev **odevs,
800 			  struct cras_fmt_conv *output_converter)
801 {
802 	struct open_dev *adev;
803 	struct dev_stream *curr;
804 	int rc;
805 	unsigned int hw_level, total_written;
806 
807 	/* For multiple output case, update the number of queued frames in shm
808 	 * of all streams before starting write output samples. */
809 	adev = *odevs;
810 	if (adev && adev->next) {
811 		DL_FOREACH(*odevs, adev) {
812 			DL_FOREACH(adev->dev->streams, curr)
813 				dev_stream_update_frames(curr);
814 		}
815 	}
816 
817 	DL_FOREACH(*odevs, adev) {
818 		if (!cras_iodev_is_open(adev->dev))
819 			continue;
820 
821 		rc = write_output_samples(odevs, adev, output_converter);
822 		if (rc < 0) {
823 			handle_dev_err(rc, odevs, adev);
824 		} else {
825 			total_written = rc;
826 
827 			/*
828 			 * Skip the underrun check and device wake up time update if
829 			 * device should not wake up.
830 			 */
831 			if (!cras_iodev_odev_should_wake(adev->dev))
832 				continue;
833 
834 			/*
835 			 * Update device wake up time and get the new hardware
836 			 * level.
837 			 */
838 			update_dev_wakeup_time(adev, &hw_level);
839 
840 			/*
841 			 * If new hardware level is less than or equal to the
842 			 * written frames, we can suppose underrun happened. But
843 			 * keep in mind there may have a false positive. If
844 			 * hardware level changed just after frames being
845 			 * written, we may get hw_level <= total_written here
846 			 * without underrun happened. However, we can still
847 			 * treat it as underrun because it is an abnormal state
848 			 * we should handle it.
849 			 */
850 			if (hw_level <= total_written) {
851 				ATLOG(atlog, AUDIO_THREAD_UNDERRUN,
852 				      adev->dev->info.idx,
853 				      hw_level, total_written);
854 				rc = cras_iodev_output_underrun(adev->dev);
855 				if(rc < 0) {
856 					handle_dev_err(rc, odevs, adev);
857 				} else {
858 					update_dev_wakeup_time(adev, &hw_level);
859 				}
860 			}
861 		}
862 	}
863 
864 	/* TODO(dgreid) - once per rstream, not once per dev_stream. */
865 	DL_FOREACH(*odevs, adev) {
866 		struct dev_stream *stream;
867 		if (!cras_iodev_is_open(adev->dev))
868 			continue;
869 		DL_FOREACH(adev->dev->streams, stream) {
870 			dev_stream_playback_update_rstream(stream);
871 		}
872 	}
873 
874 	return 0;
875 }
876 
dev_io_run(struct open_dev ** odevs,struct open_dev ** idevs,struct cras_fmt_conv * output_converter)877 void dev_io_run(struct open_dev **odevs, struct open_dev **idevs,
878 		struct cras_fmt_conv *output_converter)
879 {
880 	pic_update_current_time();
881 
882 	dev_io_playback_fetch(*odevs);
883 	dev_io_capture(idevs);
884 	dev_io_send_captured_samples(*idevs);
885 	dev_io_playback_write(odevs, output_converter);
886 
887 	check_non_empty_state_transition(*odevs);
888 }
889 
input_adev_ignore_wake(const struct open_dev * adev)890 static int input_adev_ignore_wake(const struct open_dev *adev)
891 {
892 	if (!cras_iodev_is_open(adev->dev))
893 		return 1;
894 
895 	if (!adev->dev->active_node)
896 		return 1;
897 
898 	if (adev->dev->active_node->type == CRAS_NODE_TYPE_HOTWORD &&
899 	    !cras_iodev_input_streaming(adev->dev))
900 		return 1;
901 
902 	return 0;
903 }
904 
dev_io_next_input_wake(struct open_dev ** idevs,struct timespec * min_ts)905 int dev_io_next_input_wake(struct open_dev **idevs, struct timespec *min_ts)
906 {
907 	struct open_dev *adev;
908 	int ret = 0; /* The total number of devices to wait on. */
909 
910 	DL_FOREACH(*idevs, adev) {
911 		if (input_adev_ignore_wake(adev))
912 			continue;
913 		ret++;
914 		ATLOG(atlog, AUDIO_THREAD_DEV_SLEEP_TIME, adev->dev->info.idx,
915 		      adev->wake_ts.tv_sec, adev->wake_ts.tv_nsec);
916 		if (timespec_after(min_ts, &adev->wake_ts))
917 			*min_ts = adev->wake_ts;
918 	}
919 
920 	return ret;
921 }
922 
dev_io_find_open_dev(struct open_dev * odev_list,const struct cras_iodev * dev)923 struct open_dev *dev_io_find_open_dev(struct open_dev *odev_list,
924 				      const struct cras_iodev *dev)
925 {
926 	struct open_dev *odev;
927 	DL_FOREACH(odev_list, odev)
928 		if (odev->dev == dev)
929 			return odev;
930 	return NULL;
931 }
932 
dev_io_rm_open_dev(struct open_dev ** odev_list,struct open_dev * dev_to_rm)933 void dev_io_rm_open_dev(struct open_dev **odev_list,
934 			struct open_dev *dev_to_rm)
935 {
936 	struct open_dev *odev;
937 	struct dev_stream *dev_stream;
938 
939 	/* Do nothing if dev_to_rm wasn't already in the active dev list. */
940 	odev = dev_io_find_open_dev(*odev_list, dev_to_rm->dev);
941 	if (!odev)
942 		return;
943 
944 
945 	DL_DELETE(*odev_list, dev_to_rm);
946 
947 	/* Metrics logs the number of underruns of this device. */
948 	cras_server_metrics_num_underruns(
949 		cras_iodev_get_num_underruns(dev_to_rm->dev));
950 
951 	/* Metrics logs the highest_hw_level of this device. */
952 	cras_server_metrics_highest_hw_level(
953 		dev_to_rm->dev->highest_hw_level, dev_to_rm->dev->direction);
954 
955 	check_non_empty_state_transition(*odev_list);
956 
957 	ATLOG(atlog, AUDIO_THREAD_DEV_REMOVED, dev_to_rm->dev->info.idx, 0, 0);
958 
959 	DL_FOREACH(dev_to_rm->dev->streams, dev_stream) {
960 		cras_iodev_rm_stream(dev_to_rm->dev, dev_stream->stream);
961 		dev_stream_destroy(dev_stream);
962 	}
963 
964 	if (dev_to_rm->empty_pi)
965 		pic_polled_interval_destroy(&dev_to_rm->empty_pi);
966 	if (dev_to_rm->non_empty_check_pi)
967 		pic_polled_interval_destroy(&dev_to_rm->non_empty_check_pi);
968 	free(dev_to_rm);
969 }
970 
delete_stream_from_dev(struct cras_iodev * dev,struct cras_rstream * stream)971 static void delete_stream_from_dev(struct cras_iodev *dev,
972 				   struct cras_rstream *stream)
973 {
974 	struct dev_stream *out;
975 
976 	out = cras_iodev_rm_stream(dev, stream);
977 	if (out)
978 		dev_stream_destroy(out);
979 }
980 
dev_io_remove_stream(struct open_dev ** dev_list,struct cras_rstream * stream,struct cras_iodev * dev)981 int dev_io_remove_stream(struct open_dev **dev_list,
982 			 struct cras_rstream *stream,
983 			 struct cras_iodev *dev)
984 {
985 	struct open_dev *open_dev;
986 	struct timespec delay;
987 	unsigned fetch_delay_msec;
988 
989 	/* Metrics log the longest fetch delay of this stream. */
990 	if (timespec_after(&stream->longest_fetch_interval,
991 			   &stream->sleep_interval_ts)) {
992 		subtract_timespecs(&stream->longest_fetch_interval,
993 				   &stream->sleep_interval_ts,
994 				   &delay);
995 		fetch_delay_msec = delay.tv_sec * 1000 +
996 				   delay.tv_nsec / 1000000;
997 		if (fetch_delay_msec)
998 			cras_server_metrics_longest_fetch_delay(
999 					fetch_delay_msec);
1000 	}
1001 
1002 	ATLOG(atlog, AUDIO_THREAD_STREAM_REMOVED, stream->stream_id, 0, 0);
1003 
1004 	if (dev == NULL) {
1005 		DL_FOREACH(*dev_list, open_dev) {
1006 			delete_stream_from_dev(open_dev->dev, stream);
1007 		}
1008 	} else {
1009 		delete_stream_from_dev(dev, stream);
1010 	}
1011 
1012 	return 0;
1013 }
1014