1/* Copyright (c) 2012 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 <stdlib.h>
8#include <sys/param.h>
9#include <sys/time.h>
10#include <syslog.h>
11#include <time.h>
12
13#include "audio_thread.h"
14#include "audio_thread_log.h"
15#include "buffer_share.h"
16#include "cras_audio_area.h"
17#include "cras_device_monitor.h"
18#include "cras_dsp.h"
19#include "cras_dsp_pipeline.h"
20#include "cras_fmt_conv.h"
21#include "cras_iodev.h"
22#include "cras_iodev_list.h"
23#include "cras_mix.h"
24#include "cras_ramp.h"
25#include "cras_rstream.h"
26#include "cras_system_state.h"
27#include "cras_util.h"
28#include "dev_stream.h"
29#include "utlist.h"
30#include "rate_estimator.h"
31#include "softvol_curve.h"
32
33static const float RAMP_UNMUTE_DURATION_SECS = 0.5;
34static const float RAMP_NEW_STREAM_DURATION_SECS = 0.01;
35static const float RAMP_MUTE_DURATION_SECS = 0.1;
36
37static const struct timespec rate_estimation_window_sz = {
38	20, 0 /* 20 sec. */
39};
40static const double rate_estimation_smooth_factor = 0.9f;
41
42static void cras_iodev_alloc_dsp(struct cras_iodev *iodev);
43
44static int default_no_stream_playback(struct cras_iodev *odev)
45{
46	int rc;
47	unsigned int hw_level, fr_to_write;
48	unsigned int target_hw_level = odev->min_cb_level * 2;
49	struct timespec hw_tstamp;
50
51	/* The default action for no stream playback is to fill zeros. */
52	rc = cras_iodev_frames_queued(odev, &hw_tstamp);
53	if (rc < 0)
54		return rc;
55	hw_level = rc;
56
57	ATLOG(atlog, AUDIO_THREAD_ODEV_DEFAULT_NO_STREAMS,
58	      odev->info.idx, hw_level, target_hw_level);
59
60	fr_to_write = cras_iodev_buffer_avail(odev, hw_level);
61	if (hw_level <= target_hw_level) {
62		fr_to_write = MIN(target_hw_level - hw_level, fr_to_write);
63		return cras_iodev_fill_odev_zeros(odev, fr_to_write);
64	}
65	return 0;
66}
67
68static int cras_iodev_start(struct cras_iodev *iodev)
69{
70	int rc;
71	if (!cras_iodev_is_open(iodev))
72		return -EPERM;
73	if (!iodev->start) {
74		syslog(LOG_ERR,
75		       "start called on device %s not supporting start ops",
76		       iodev->info.name);
77		return -EINVAL;
78	}
79	rc = iodev->start(iodev);
80	if (rc)
81		return rc;
82	iodev->state = CRAS_IODEV_STATE_NORMAL_RUN;
83	return 0;
84}
85
86/* Gets the number of frames ready for this device to play.
87 * It is the minimum number of available samples in dev_streams.
88 */
89static unsigned int dev_playback_frames(struct cras_iodev* odev)
90{
91	struct dev_stream *curr;
92	int frames = 0;
93
94	DL_FOREACH(odev->streams, curr) {
95		int dev_frames;
96
97		/* If this is a single output dev stream, updates the latest
98		 * number of frames for playback. */
99		if (dev_stream_attached_devs(curr) == 1)
100			dev_stream_update_frames(curr);
101
102		dev_frames = dev_stream_playback_frames(curr);
103		/* Do not handle stream error or end of draining in this
104		 * function because they should be handled in write_streams. */
105		if (dev_frames < 0)
106			continue;
107		if (!dev_frames) {
108			if(cras_rstream_get_is_draining(curr->stream))
109				continue;
110			else
111				return 0;
112		}
113		if (frames == 0)
114			frames = dev_frames;
115		else
116			frames = MIN(dev_frames, frames);
117	}
118	return frames;
119}
120
121/* Let device enter/leave no stream playback.
122 * Args:
123 *    iodev[in] - The output device.
124 *    enable[in] - 1 to enter no stream playback, 0 to leave.
125 * Returns:
126 *    0 on success. Negative error code on failure.
127 */
128static int cras_iodev_no_stream_playback_transition(struct cras_iodev *odev,
129						    int enable)
130{
131	int rc;
132
133	if (odev->direction != CRAS_STREAM_OUTPUT)
134		return -EINVAL;
135
136	/* This function is for transition between normal run and
137	 * no stream run state.
138	 */
139	if ((odev->state != CRAS_IODEV_STATE_NORMAL_RUN) &&
140	    (odev->state != CRAS_IODEV_STATE_NO_STREAM_RUN))
141		return -EINVAL;
142
143	if (enable) {
144		ATLOG(atlog, AUDIO_THREAD_ODEV_NO_STREAMS,
145		      odev->info.idx, 0, 0);
146	} else {
147		ATLOG(atlog, AUDIO_THREAD_ODEV_LEAVE_NO_STREAMS,
148		      odev->info.idx, 0, 0);
149	}
150
151	rc = odev->no_stream(odev, enable);
152	if (rc < 0)
153		return rc;
154	if (enable)
155		odev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;
156	else
157		odev->state = CRAS_IODEV_STATE_NORMAL_RUN;
158	return 0;
159}
160
161/* Determines if the output device should mute. It considers system mute,
162 * system volume, and active node volume on the device. */
163static int output_should_mute(struct cras_iodev *odev)
164{
165	/* System mute has highest priority. */
166	if (cras_system_get_mute())
167		return 1;
168
169	/* consider system volume and active node volume. */
170	return cras_iodev_is_zero_volume(odev);
171}
172
173int cras_iodev_is_zero_volume(const struct cras_iodev *odev)
174{
175	size_t system_volume;
176	unsigned int adjusted_node_volume;
177
178	system_volume = cras_system_get_volume();
179	if (odev->active_node) {
180		adjusted_node_volume = cras_iodev_adjust_node_volume(
181				odev->active_node, system_volume);
182		return (adjusted_node_volume == 0);
183	}
184	return (system_volume == 0);
185}
186
187/* Output device state transition diagram:
188 *
189 *                           ----------------
190 *  -------------<-----------| S0  Closed   |------<-------.
191 *  |                        ----------------              |
192 *  |                           |   iodev_list enables     |
193 *  |                           |   device and adds to     |
194 *  |                           V   audio thread           | iodev_list removes
195 *  |                        ----------------              | device from
196 *  |                        | S1  Open     |              | audio_thread and
197 *  |                        ----------------              | closes device
198 *  | Device with dummy start       |                      |
199 *  | ops transits into             | Sample is ready      |
200 *  | no stream state right         V                      |
201 *  | after open.            ----------------              |
202 *  |                        | S2  Normal   |              |
203 *  |                        ----------------              |
204 *  |                           |        ^                 |
205 *  |       There is no stream  |        | Sample is ready |
206 *  |                           V        |                 |
207 *  |                        ----------------              |
208 *  ------------->-----------| S3 No Stream |------->------
209 *                           ----------------
210 *
211 *  Device in open_devs can be in one of S1, S2, S3.
212 *
213 * cras_iodev_output_event_sample_ready change device state from S1 or S3 into
214 * S2.
215 */
216static int cras_iodev_output_event_sample_ready(struct cras_iodev *odev)
217{
218	if (odev->state == CRAS_IODEV_STATE_OPEN ||
219	    odev->state == CRAS_IODEV_STATE_NO_STREAM_RUN) {
220		/* Starts ramping up if device should not be muted.
221		 * Both mute and volume are taken into consideration.
222		 */
223		if (odev->ramp && !output_should_mute(odev))
224			cras_iodev_start_ramp(
225				odev,
226				CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK);
227	}
228
229	if (odev->state == CRAS_IODEV_STATE_OPEN) {
230		/* S1 => S2:
231		 * If device is not started yet, and there is sample ready from
232		 * stream, fill 1 min_cb_level of zeros first and fill sample
233		 * from stream later.
234		 * Starts the device here to finish state transition. */
235		cras_iodev_fill_odev_zeros(odev, odev->min_cb_level);
236		ATLOG(atlog, AUDIO_THREAD_ODEV_START,
237				odev->info.idx, odev->min_cb_level, 0);
238		return cras_iodev_start(odev);
239	} else if (odev->state == CRAS_IODEV_STATE_NO_STREAM_RUN) {
240		/* S3 => S2:
241		 * Device in no stream state get sample ready. Leave no stream
242		 * state and transit to normal run state.*/
243		return cras_iodev_no_stream_playback_transition(odev, 0);
244	} else {
245		syslog(LOG_ERR,
246		       "Device %s in state %d received sample ready event",
247		       odev->info.name, odev->state);
248		return -EINVAL;
249	}
250	return 0;
251}
252
253/*
254 * Exported Interface.
255 */
256
257/* Finds the supported sample rate that best suits the requested rate, "rrate".
258 * Exact matches have highest priority, then integer multiples, then the default
259 * rate for the device. */
260static size_t get_best_rate(struct cras_iodev *iodev, size_t rrate)
261{
262	size_t i;
263	size_t best;
264
265	if (iodev->supported_rates[0] == 0) /* No rates supported */
266		return 0;
267
268	for (i = 0, best = 0; iodev->supported_rates[i] != 0; i++) {
269		if (rrate == iodev->supported_rates[i] &&
270		    rrate >= 44100)
271			return rrate;
272		if (best == 0 && (rrate % iodev->supported_rates[i] == 0 ||
273				  iodev->supported_rates[i] % rrate == 0))
274			best = iodev->supported_rates[i];
275	}
276
277	if (best)
278		return best;
279	return iodev->supported_rates[0];
280}
281
282/* Finds the best match for the channel count.  The following match rules
283 * will apply in order and return the value once matched:
284 * 1. Match the exact given channel count.
285 * 2. Match the preferred channel count.
286 * 3. The first channel count in the list.
287 */
288static size_t get_best_channel_count(struct cras_iodev *iodev, size_t count)
289{
290	static const size_t preferred_channel_count = 2;
291	size_t i;
292
293	assert(iodev->supported_channel_counts[0] != 0);
294
295	for (i = 0; iodev->supported_channel_counts[i] != 0; i++) {
296		if (iodev->supported_channel_counts[i] == count)
297			return count;
298	}
299
300	/* If provided count is not supported, search for preferred
301	 * channel count to which we're good at converting.
302	 */
303	for (i = 0; iodev->supported_channel_counts[i] != 0; i++) {
304		if (iodev->supported_channel_counts[i] ==
305				preferred_channel_count)
306			return preferred_channel_count;
307	}
308
309	return iodev->supported_channel_counts[0];
310}
311
312/* finds the best match for the current format. If no exact match is
313 * found, use the first. */
314static snd_pcm_format_t get_best_pcm_format(struct cras_iodev *iodev,
315					    snd_pcm_format_t fmt)
316{
317	size_t i;
318
319	for (i = 0; iodev->supported_formats[i] != 0; i++) {
320		if (fmt == iodev->supported_formats[i])
321			return fmt;
322	}
323
324	return iodev->supported_formats[0];
325}
326
327/* Set default channel layout to an iodev. */
328static void set_default_channel_layout(struct cras_iodev *iodev)
329{
330	int8_t default_layout[CRAS_CH_MAX];
331	size_t i;
332
333	for (i = 0; i < CRAS_CH_MAX; i++)
334		default_layout[i] = i < iodev->format->num_channels ? i : -1;
335
336	cras_audio_format_set_channel_layout(iodev->format, default_layout);
337	cras_audio_format_set_channel_layout(iodev->ext_format, default_layout);
338}
339
340/* Applies the DSP to the samples for the iodev if applicable. */
341static void apply_dsp(struct cras_iodev *iodev, uint8_t *buf, size_t frames)
342{
343	struct cras_dsp_context *ctx;
344	struct pipeline *pipeline;
345
346	ctx = iodev->dsp_context;
347	if (!ctx)
348		return;
349
350	pipeline = cras_dsp_get_pipeline(ctx);
351	if (!pipeline)
352		return;
353
354	cras_dsp_pipeline_apply(pipeline,
355				buf,
356				frames);
357
358	cras_dsp_put_pipeline(ctx);
359}
360
361static void cras_iodev_free_dsp(struct cras_iodev *iodev)
362{
363	if (iodev->dsp_context) {
364		cras_dsp_context_free(iodev->dsp_context);
365		iodev->dsp_context = NULL;
366	}
367}
368
369/* Modifies the number of channels in device format to the one that will be
370 * presented to the device after any channel changes from the DSP. */
371static inline void adjust_dev_channel_for_dsp(const struct cras_iodev *iodev)
372{
373	struct cras_dsp_context *ctx = iodev->dsp_context;
374
375	if (!ctx || !cras_dsp_get_pipeline(ctx))
376		return;
377
378	if (iodev->direction == CRAS_STREAM_OUTPUT) {
379		iodev->format->num_channels =
380			cras_dsp_num_output_channels(ctx);
381		iodev->ext_format->num_channels =
382			cras_dsp_num_input_channels(ctx);
383	} else {
384		iodev->format->num_channels =
385			cras_dsp_num_input_channels(ctx);
386		iodev->ext_format->num_channels =
387			cras_dsp_num_output_channels(ctx);
388	}
389
390	cras_dsp_put_pipeline(ctx);
391}
392
393/* Updates channel layout based on the number of channels set by a
394 * client stream. When successful we need to update the new channel
395 * layout to ext_format, otherwise we should set a default value
396 * to both format and ext_format.
397 */
398static void update_channel_layout(struct cras_iodev *iodev)
399{
400	int rc;
401
402	/*
403	 * Output devices like internal speakers and headphones are 2-channel
404	 * and do not need to update channel layout.
405	 * For HDMI and USB devices that might have more than 2 channels, update
406	 * channel layout only if more than 2 channel is requested.
407	 */
408	if (iodev->direction == CRAS_STREAM_OUTPUT &&
409	    iodev->format->num_channels <= 2) {
410		set_default_channel_layout(iodev);
411		return;
412	}
413
414	if (iodev->update_channel_layout == NULL)
415		return;
416
417	rc = iodev->update_channel_layout(iodev);
418	if (rc < 0) {
419		set_default_channel_layout(iodev);
420	} else {
421		cras_audio_format_set_channel_layout(
422				iodev->ext_format,
423				iodev->format->channel_layout);
424	}
425}
426
427int cras_iodev_set_format(struct cras_iodev *iodev,
428			  const struct cras_audio_format *fmt)
429{
430	size_t actual_rate, actual_num_channels;
431	snd_pcm_format_t actual_format;
432	int rc;
433
434	/* If this device isn't already using a format, try to match the one
435	 * requested in "fmt". */
436	if (iodev->format == NULL) {
437		iodev->format = malloc(sizeof(struct cras_audio_format));
438		iodev->ext_format = malloc(sizeof(struct cras_audio_format));
439		if (!iodev->format || !iodev->ext_format)
440			return -ENOMEM;
441		*iodev->format = *fmt;
442		*iodev->ext_format = *fmt;
443
444		if (iodev->update_supported_formats) {
445			rc = iodev->update_supported_formats(iodev);
446			if (rc) {
447				syslog(LOG_ERR, "Failed to update formats");
448				goto error;
449			}
450		}
451
452		/* Finds the actual rate of device before allocating DSP
453		 * because DSP needs to use the rate of device, not rate of
454		 * stream. */
455		actual_rate = get_best_rate(iodev, fmt->frame_rate);
456		iodev->format->frame_rate = actual_rate;
457		iodev->ext_format->frame_rate = actual_rate;
458
459		cras_iodev_alloc_dsp(iodev);
460		if (iodev->dsp_context)
461			adjust_dev_channel_for_dsp(iodev);
462
463		actual_num_channels = get_best_channel_count(iodev,
464					iodev->format->num_channels);
465		actual_format = get_best_pcm_format(iodev, fmt->format);
466		if (actual_rate == 0 || actual_num_channels == 0 ||
467		    actual_format == 0) {
468			/* No compatible frame rate found. */
469			rc = -EINVAL;
470			goto error;
471		}
472		iodev->format->format = actual_format;
473		iodev->ext_format->format = actual_format;
474		if (iodev->format->num_channels != actual_num_channels) {
475			/* If the DSP for this device doesn't match, drop it. */
476			iodev->format->num_channels = actual_num_channels;
477			iodev->ext_format->num_channels = actual_num_channels;
478			cras_iodev_free_dsp(iodev);
479		}
480
481		update_channel_layout(iodev);
482
483		if (!iodev->rate_est)
484			iodev->rate_est = rate_estimator_create(
485						actual_rate,
486						&rate_estimation_window_sz,
487						rate_estimation_smooth_factor);
488		else
489			rate_estimator_reset_rate(iodev->rate_est, actual_rate);
490	}
491
492	return 0;
493
494error:
495	free(iodev->format);
496	free(iodev->ext_format);
497	iodev->format = NULL;
498	iodev->ext_format = NULL;
499	return rc;
500}
501
502void cras_iodev_update_dsp(struct cras_iodev *iodev)
503{
504	char swap_lr_disabled = 1;
505
506	if (!iodev->dsp_context)
507		return;
508
509	cras_dsp_set_variable_string(iodev->dsp_context, "dsp_name",
510				     iodev->dsp_name ? : "");
511
512	if (iodev->active_node && iodev->active_node->left_right_swapped)
513		swap_lr_disabled = 0;
514
515	cras_dsp_set_variable_boolean(iodev->dsp_context, "swap_lr_disabled",
516				      swap_lr_disabled);
517
518	cras_dsp_load_pipeline(iodev->dsp_context);
519}
520
521
522int cras_iodev_dsp_set_swap_mode_for_node(struct cras_iodev *iodev,
523					   struct cras_ionode *node, int enable)
524{
525	if (node->left_right_swapped == enable)
526		return 0;
527
528	/* Sets left_right_swapped property on the node. It will be used
529	 * when cras_iodev_update_dsp is called. */
530	node->left_right_swapped = enable;
531
532	/* Possibly updates dsp if the node is active on the device and there
533	 * is dsp context. If dsp context is not created yet,
534	 * cras_iodev_update_dsp returns right away. */
535	if (iodev->active_node == node)
536		cras_iodev_update_dsp(iodev);
537	return 0;
538}
539
540void cras_iodev_free_format(struct cras_iodev *iodev)
541{
542	free(iodev->format);
543	free(iodev->ext_format);
544	iodev->format = NULL;
545	iodev->ext_format = NULL;
546}
547
548
549void cras_iodev_init_audio_area(struct cras_iodev *iodev,
550				int num_channels)
551{
552	if (iodev->area)
553		cras_iodev_free_audio_area(iodev);
554
555	iodev->area = cras_audio_area_create(num_channels);
556	cras_audio_area_config_channels(iodev->area, iodev->format);
557}
558
559void cras_iodev_free_audio_area(struct cras_iodev *iodev)
560{
561	if (!iodev->area)
562		return;
563
564	cras_audio_area_destroy(iodev->area);
565	iodev->area = NULL;
566}
567
568void cras_iodev_free_resources(struct cras_iodev *iodev)
569{
570	cras_iodev_free_dsp(iodev);
571	rate_estimator_destroy(iodev->rate_est);
572	if (iodev->ramp)
573		cras_ramp_destroy(iodev->ramp);
574}
575
576static void cras_iodev_alloc_dsp(struct cras_iodev *iodev)
577{
578	const char *purpose;
579
580	if (iodev->direction == CRAS_STREAM_OUTPUT)
581		purpose = "playback";
582	else
583		purpose = "capture";
584
585	cras_iodev_free_dsp(iodev);
586	iodev->dsp_context = cras_dsp_context_new(iodev->format->frame_rate,
587						  purpose);
588	cras_iodev_update_dsp(iodev);
589}
590
591void cras_iodev_fill_time_from_frames(size_t frames,
592				      size_t frame_rate,
593				      struct timespec *ts)
594{
595	uint64_t to_play_usec;
596
597	ts->tv_sec = 0;
598	/* adjust sleep time to target our callback threshold */
599	to_play_usec = (uint64_t)frames * 1000000L / (uint64_t)frame_rate;
600
601	while (to_play_usec > 1000000) {
602		ts->tv_sec++;
603		to_play_usec -= 1000000;
604	}
605	ts->tv_nsec = to_play_usec * 1000;
606}
607
608/* This is called when a node is plugged/unplugged */
609static void plug_node(struct cras_ionode *node, int plugged)
610{
611	if (node->plugged == plugged)
612		return;
613	node->plugged = plugged;
614	if (plugged) {
615		gettimeofday(&node->plugged_time, NULL);
616	} else if (node == node->dev->active_node) {
617		cras_iodev_list_disable_dev(node->dev);
618	}
619	cras_iodev_list_notify_nodes_changed();
620}
621
622static void set_node_volume(struct cras_ionode *node, int value)
623{
624	struct cras_iodev *dev = node->dev;
625	unsigned int volume;
626
627	if (dev->direction != CRAS_STREAM_OUTPUT)
628		return;
629
630	volume = (unsigned int)MIN(value, 100);
631	node->volume = volume;
632	if (dev->set_volume)
633		dev->set_volume(dev);
634
635	cras_iodev_list_notify_node_volume(node);
636}
637
638static void set_node_capture_gain(struct cras_ionode *node, int value)
639{
640	struct cras_iodev *dev = node->dev;
641
642	if (dev->direction != CRAS_STREAM_INPUT)
643		return;
644
645	node->capture_gain = (long)value;
646	if (dev->set_capture_gain)
647		dev->set_capture_gain(dev);
648
649	cras_iodev_list_notify_node_capture_gain(node);
650}
651
652static void set_node_left_right_swapped(struct cras_ionode *node, int value)
653{
654	struct cras_iodev *dev = node->dev;
655	int rc;
656
657	if (!dev->set_swap_mode_for_node)
658		return;
659	rc = dev->set_swap_mode_for_node(dev, node, value);
660	if (rc) {
661		syslog(LOG_ERR,
662		       "Failed to set swap mode on node %s to %d; error %d",
663		       node->name, value, rc);
664		return;
665	}
666	node->left_right_swapped = value;
667	cras_iodev_list_notify_node_left_right_swapped(node);
668	return;
669}
670
671int cras_iodev_set_node_attr(struct cras_ionode *ionode,
672			     enum ionode_attr attr, int value)
673{
674	switch (attr) {
675	case IONODE_ATTR_PLUGGED:
676		plug_node(ionode, value);
677		break;
678	case IONODE_ATTR_VOLUME:
679		set_node_volume(ionode, value);
680		break;
681	case IONODE_ATTR_CAPTURE_GAIN:
682		set_node_capture_gain(ionode, value);
683		break;
684	case IONODE_ATTR_SWAP_LEFT_RIGHT:
685		set_node_left_right_swapped(ionode, value);
686		break;
687	default:
688		return -EINVAL;
689	}
690
691	return 0;
692}
693
694void cras_iodev_add_node(struct cras_iodev *iodev, struct cras_ionode *node)
695{
696	DL_APPEND(iodev->nodes, node);
697	cras_iodev_list_notify_nodes_changed();
698}
699
700void cras_iodev_rm_node(struct cras_iodev *iodev, struct cras_ionode *node)
701{
702	DL_DELETE(iodev->nodes, node);
703	cras_iodev_list_notify_nodes_changed();
704}
705
706void cras_iodev_set_active_node(struct cras_iodev *iodev,
707				struct cras_ionode *node)
708{
709	iodev->active_node = node;
710	cras_iodev_list_notify_active_node_changed(iodev->direction);
711}
712
713float cras_iodev_get_software_volume_scaler(struct cras_iodev *iodev)
714{
715	unsigned int volume;
716
717	volume = cras_iodev_adjust_active_node_volume(
718			iodev, cras_system_get_volume());
719
720	if (iodev->active_node && iodev->active_node->softvol_scalers)
721		return iodev->active_node->softvol_scalers[volume];
722	return softvol_get_scaler(volume);
723}
724
725float cras_iodev_get_software_gain_scaler(const struct cras_iodev *iodev) {
726	float scaler = 1.0f;
727	if (cras_iodev_software_volume_needed(iodev)) {
728		long gain = cras_iodev_adjust_active_node_gain(
729				iodev, cras_system_get_capture_gain());
730		scaler = convert_softvol_scaler_from_dB(gain);
731	}
732	return scaler;
733}
734
735int cras_iodev_add_stream(struct cras_iodev *iodev,
736			  struct dev_stream *stream)
737{
738	unsigned int cb_threshold = dev_stream_cb_threshold(stream);
739	DL_APPEND(iodev->streams, stream);
740
741	if (!iodev->buf_state)
742		iodev->buf_state = buffer_share_create(iodev->buffer_size);
743	buffer_share_add_id(iodev->buf_state, stream->stream->stream_id, NULL);
744
745	iodev->min_cb_level = MIN(iodev->min_cb_level, cb_threshold);
746	iodev->max_cb_level = MAX(iodev->max_cb_level, cb_threshold);
747	return 0;
748}
749
750struct dev_stream *cras_iodev_rm_stream(struct cras_iodev *iodev,
751					const struct cras_rstream *rstream)
752{
753	struct dev_stream *out;
754	struct dev_stream *ret = NULL;
755	unsigned int cb_threshold;
756	unsigned int old_min_cb_level = iodev->min_cb_level;
757
758	iodev->min_cb_level = iodev->buffer_size / 2;
759	iodev->max_cb_level = 0;
760	DL_FOREACH(iodev->streams, out) {
761		if (out->stream == rstream) {
762			buffer_share_rm_id(iodev->buf_state,
763					   rstream->stream_id);
764			ret = out;
765			DL_DELETE(iodev->streams, out);
766			continue;
767		}
768		cb_threshold = dev_stream_cb_threshold(out);
769		iodev->min_cb_level = MIN(iodev->min_cb_level, cb_threshold);
770		iodev->max_cb_level = MAX(iodev->max_cb_level, cb_threshold);
771	}
772
773	if (!iodev->streams) {
774		buffer_share_destroy(iodev->buf_state);
775		iodev->buf_state = NULL;
776		iodev->min_cb_level = old_min_cb_level;
777		/* Let output device transit into no stream state if it's
778		 * in normal run state now. Leave input device in normal
779		 * run state. */
780		if ((iodev->direction == CRAS_STREAM_OUTPUT) &&
781		    (iodev->state == CRAS_IODEV_STATE_NORMAL_RUN))
782			cras_iodev_no_stream_playback_transition(iodev, 1);
783	}
784	return ret;
785}
786
787unsigned int cras_iodev_stream_offset(struct cras_iodev *iodev,
788				      struct dev_stream *stream)
789{
790	return buffer_share_id_offset(iodev->buf_state,
791				      stream->stream->stream_id);
792}
793
794void cras_iodev_stream_written(struct cras_iodev *iodev,
795			       struct dev_stream *stream,
796			       unsigned int nwritten)
797{
798	buffer_share_offset_update(iodev->buf_state,
799				   stream->stream->stream_id, nwritten);
800}
801
802unsigned int cras_iodev_all_streams_written(struct cras_iodev *iodev)
803{
804	if (!iodev->buf_state)
805		return 0;
806	return buffer_share_get_new_write_point(iodev->buf_state);
807}
808
809unsigned int cras_iodev_max_stream_offset(const struct cras_iodev *iodev)
810{
811	unsigned int max = 0;
812	struct dev_stream *curr;
813
814	DL_FOREACH(iodev->streams, curr) {
815		max = MAX(max,
816			  buffer_share_id_offset(iodev->buf_state,
817						 curr->stream->stream_id));
818	}
819
820	return max;
821}
822
823int cras_iodev_open(struct cras_iodev *iodev, unsigned int cb_level)
824{
825	int rc;
826
827	rc = iodev->open_dev(iodev);
828	if (rc < 0)
829		return rc;
830
831	/* Make sure the min_cb_level doesn't get too large. */
832	iodev->min_cb_level = MIN(iodev->buffer_size / 2, cb_level);
833	iodev->max_cb_level = 0;
834
835	iodev->reset_request_pending = 0;
836	iodev->state = CRAS_IODEV_STATE_OPEN;
837
838	if (iodev->direction == CRAS_STREAM_OUTPUT) {
839		/* If device supports start ops, device can be in open state.
840		 * Otherwise, device starts running right after opening. */
841		if (iodev->start)
842			iodev->state = CRAS_IODEV_STATE_OPEN;
843		else
844			iodev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;
845	} else {
846		/* Input device starts running right after opening.
847		 * No stream state is only for output device. Input device
848		 * should be in normal run state. */
849		iodev->state = CRAS_IODEV_STATE_NORMAL_RUN;
850	}
851
852	return 0;
853}
854
855enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev *iodev)
856{
857	return iodev->state;
858}
859
860int cras_iodev_close(struct cras_iodev *iodev)
861{
862	int rc;
863	if (!cras_iodev_is_open(iodev))
864		return 0;
865
866	rc = iodev->close_dev(iodev);
867	if (rc)
868		return rc;
869	iodev->state = CRAS_IODEV_STATE_CLOSE;
870	if (iodev->ramp)
871		cras_ramp_reset(iodev->ramp);
872	return 0;
873}
874
875int cras_iodev_put_input_buffer(struct cras_iodev *iodev, unsigned int nframes)
876{
877	rate_estimator_add_frames(iodev->rate_est, -nframes);
878	return iodev->put_buffer(iodev, nframes);
879}
880
881int cras_iodev_put_output_buffer(struct cras_iodev *iodev, uint8_t *frames,
882				 unsigned int nframes)
883{
884	const struct cras_audio_format *fmt = iodev->format;
885	struct cras_fmt_conv * remix_converter =
886			audio_thread_get_global_remix_converter();
887	struct cras_ramp_action ramp_action = {
888		.type = CRAS_RAMP_ACTION_NONE,
889		.scaler = 0.0f,
890		.increment = 0.0f,
891	};
892	float software_volume_scaler;
893	int software_volume_needed = cras_iodev_software_volume_needed(iodev);
894
895	if (iodev->pre_dsp_hook)
896		iodev->pre_dsp_hook(frames, nframes, iodev->ext_format,
897				    iodev->pre_dsp_hook_cb_data);
898
899	if (iodev->ramp) {
900		ramp_action = cras_ramp_get_current_action(iodev->ramp);
901	}
902
903	/* Mute samples if adjusted volume is 0 or system is muted, plus
904	 * that this device is not ramping. */
905	if (output_should_mute(iodev) &&
906	    ramp_action.type != CRAS_RAMP_ACTION_PARTIAL) {
907		const unsigned int frame_bytes = cras_get_format_bytes(fmt);
908		cras_mix_mute_buffer(frames, frame_bytes, nframes);
909	} else {
910		apply_dsp(iodev, frames, nframes);
911
912		if (iodev->post_dsp_hook)
913			iodev->post_dsp_hook(frames, nframes, fmt,
914					     iodev->post_dsp_hook_cb_data);
915
916		/* Compute scaler for software volume if needed. */
917		if (software_volume_needed) {
918			software_volume_scaler =
919				cras_iodev_get_software_volume_scaler(iodev);
920		}
921
922		if (ramp_action.type == CRAS_RAMP_ACTION_PARTIAL) {
923			/* Scale with increment for ramp and possibly
924			 * software volume using cras_scale_buffer_increment.*/
925			float starting_scaler = ramp_action.scaler;
926			float increment = ramp_action.increment;
927
928			if (software_volume_needed) {
929				starting_scaler *= software_volume_scaler;
930				increment *= software_volume_scaler;
931			}
932
933			cras_scale_buffer_increment(
934					fmt->format, frames, nframes,
935					starting_scaler, increment,
936					fmt->num_channels);
937			cras_ramp_update_ramped_frames(iodev->ramp, nframes);
938		} else if (software_volume_needed) {
939			/* Just scale for software volume using
940			 * cras_scale_buffer. */
941			unsigned int nsamples = nframes * fmt->num_channels;
942			cras_scale_buffer(fmt->format, frames,
943					  nsamples, software_volume_scaler);
944		}
945	}
946
947	if (remix_converter)
948		cras_channel_remix_convert(remix_converter,
949				   iodev->format,
950				   frames,
951				   nframes);
952	rate_estimator_add_frames(iodev->rate_est, nframes);
953	return iodev->put_buffer(iodev, nframes);
954}
955
956int cras_iodev_get_input_buffer(struct cras_iodev *iodev,
957				struct cras_audio_area **area,
958				unsigned *frames)
959{
960	const struct cras_audio_format *fmt = iodev->format;
961	const unsigned int frame_bytes = cras_get_format_bytes(fmt);
962	uint8_t *hw_buffer;
963	int rc;
964	unsigned frame_requested = *frames;
965
966	rc = iodev->get_buffer(iodev, area, frames);
967	if (rc < 0 || *frames == 0)
968		return rc;
969	if (*frames > frame_requested) {
970		syslog(LOG_ERR,
971		       "frames returned from get_buffer is greater than "
972		       "requested: %u > %u", *frames, frame_requested);
973		return -EINVAL;
974	}
975
976	/* TODO(dgreid) - This assumes interleaved audio. */
977	hw_buffer = (*area)->channels[0].buf;
978
979	if (cras_system_get_capture_mute())
980		cras_mix_mute_buffer(hw_buffer, frame_bytes, *frames);
981	else
982		apply_dsp(iodev, hw_buffer, *frames); /* TODO-applied 2x */
983
984	return rc;
985}
986
987int cras_iodev_get_output_buffer(struct cras_iodev *iodev,
988				 struct cras_audio_area **area,
989				 unsigned *frames)
990{
991	int rc;
992	unsigned frame_requested = *frames;
993
994	rc = iodev->get_buffer(iodev, area, frames);
995	if (*frames > frame_requested) {
996		syslog(LOG_ERR,
997		       "frames returned from get_buffer is greater than "
998		       "requested: %u > %u", *frames, frame_requested);
999		return -EINVAL;
1000	}
1001	return rc;
1002}
1003
1004int cras_iodev_update_rate(struct cras_iodev *iodev, unsigned int level,
1005			   struct timespec *level_tstamp)
1006{
1007	return rate_estimator_check(iodev->rate_est, level, level_tstamp);
1008}
1009
1010int cras_iodev_reset_rate_estimator(const struct cras_iodev *iodev)
1011{
1012	rate_estimator_reset_rate(iodev->rate_est,
1013				  iodev->ext_format->frame_rate);
1014	return 0;
1015}
1016
1017double cras_iodev_get_est_rate_ratio(const struct cras_iodev *iodev)
1018{
1019	return rate_estimator_get_rate(iodev->rate_est) /
1020			iodev->ext_format->frame_rate;
1021}
1022
1023int cras_iodev_get_dsp_delay(const struct cras_iodev *iodev)
1024{
1025	struct cras_dsp_context *ctx;
1026	struct pipeline *pipeline;
1027	int delay;
1028
1029	ctx = iodev->dsp_context;
1030	if (!ctx)
1031		return 0;
1032
1033	pipeline = cras_dsp_get_pipeline(ctx);
1034	if (!pipeline)
1035		return 0;
1036
1037	delay = cras_dsp_pipeline_get_delay(pipeline);
1038
1039	cras_dsp_put_pipeline(ctx);
1040	return delay;
1041}
1042
1043int cras_iodev_frames_queued(struct cras_iodev *iodev,
1044			     struct timespec *hw_tstamp)
1045{
1046	int rc;
1047
1048	rc = iodev->frames_queued(iodev, hw_tstamp);
1049	if (rc < 0 || iodev->direction == CRAS_STREAM_INPUT)
1050		return rc;
1051
1052	if (rc < iodev->min_buffer_level)
1053		return 0;
1054
1055	return rc - iodev->min_buffer_level;
1056}
1057
1058int cras_iodev_buffer_avail(struct cras_iodev *iodev, unsigned hw_level)
1059{
1060	if (iodev->direction == CRAS_STREAM_INPUT)
1061		return hw_level;
1062
1063	if (hw_level + iodev->min_buffer_level > iodev->buffer_size)
1064		return 0;
1065
1066	return iodev->buffer_size - iodev->min_buffer_level - hw_level;
1067}
1068
1069void cras_iodev_register_pre_dsp_hook(struct cras_iodev *iodev,
1070				      loopback_hook_t loop_cb,
1071				      void *cb_data)
1072{
1073	iodev->pre_dsp_hook = loop_cb;
1074	iodev->pre_dsp_hook_cb_data = cb_data;
1075}
1076
1077void cras_iodev_register_post_dsp_hook(struct cras_iodev *iodev,
1078				       loopback_hook_t loop_cb,
1079				       void *cb_data)
1080{
1081	iodev->post_dsp_hook = loop_cb;
1082	iodev->post_dsp_hook_cb_data = cb_data;
1083}
1084
1085int cras_iodev_fill_odev_zeros(struct cras_iodev *odev, unsigned int frames)
1086{
1087	struct cras_audio_area *area = NULL;
1088	unsigned int frame_bytes, frames_written;
1089	int rc;
1090	uint8_t *buf;
1091
1092	if (odev->direction != CRAS_STREAM_OUTPUT)
1093		return -EINVAL;
1094
1095	ATLOG(atlog, AUDIO_THREAD_FILL_ODEV_ZEROS, odev->info.idx, frames, 0);
1096
1097	frame_bytes = cras_get_format_bytes(odev->ext_format);
1098	while (frames > 0) {
1099		frames_written = frames;
1100		rc = cras_iodev_get_output_buffer(odev, &area, &frames_written);
1101		if (rc < 0) {
1102			syslog(LOG_ERR, "fill zeros fail: %d", rc);
1103			return rc;
1104		}
1105
1106		/* This assumes consecutive channel areas. */
1107		buf = area->channels[0].buf;
1108		memset(buf, 0, frames_written * frame_bytes);
1109		cras_iodev_put_output_buffer(odev, buf, frames_written);
1110		frames -= frames_written;
1111	}
1112
1113	return 0;
1114}
1115
1116int cras_iodev_output_underrun(struct cras_iodev *odev) {
1117	if (odev->output_underrun)
1118		return odev->output_underrun(odev);
1119	else
1120		return cras_iodev_fill_odev_zeros(odev, odev->min_cb_level);
1121}
1122
1123int cras_iodev_odev_should_wake(const struct cras_iodev *odev)
1124{
1125	if (odev->direction != CRAS_STREAM_OUTPUT)
1126		return 0;
1127
1128	if (odev->output_should_wake)
1129		return odev->output_should_wake(odev);
1130
1131	/* Do not wake up for device not started yet. */
1132	return (odev->state == CRAS_IODEV_STATE_NORMAL_RUN ||
1133	        odev->state == CRAS_IODEV_STATE_NO_STREAM_RUN);
1134}
1135
1136unsigned int cras_iodev_frames_to_play_in_sleep(struct cras_iodev *odev,
1137						unsigned int *hw_level,
1138						struct timespec *hw_tstamp)
1139{
1140	int rc;
1141
1142	rc = cras_iodev_frames_queued(odev, hw_tstamp);
1143	*hw_level = (rc < 0) ? 0 : rc;
1144
1145	if (odev->streams) {
1146		/* Schedule that audio thread will wake up when
1147		 * hw_level drops to 0.
1148		 * This should not cause underrun because audio thread
1149		 * should be waken up by the reply from client. */
1150		return *hw_level;
1151	}
1152	/* When this device has no stream, schedule audio thread to wake up
1153	 * when hw_level drops to min_cb_level so audio thread can fill
1154	 * zeros to it. */
1155	if (*hw_level > odev->min_cb_level)
1156		return *hw_level - odev->min_cb_level;
1157	else
1158		return 0;
1159}
1160
1161int cras_iodev_default_no_stream_playback(struct cras_iodev *odev, int enable)
1162{
1163	if (enable)
1164		return default_no_stream_playback(odev);
1165	return 0;
1166}
1167
1168int cras_iodev_prepare_output_before_write_samples(struct cras_iodev *odev)
1169{
1170	int may_enter_normal_run;
1171	enum CRAS_IODEV_STATE state;
1172
1173	if (odev->direction != CRAS_STREAM_OUTPUT)
1174		return -EINVAL;
1175
1176	state = cras_iodev_state(odev);
1177
1178	may_enter_normal_run = (state == CRAS_IODEV_STATE_OPEN ||
1179		                state == CRAS_IODEV_STATE_NO_STREAM_RUN);
1180
1181	if (may_enter_normal_run && dev_playback_frames(odev))
1182		return cras_iodev_output_event_sample_ready(odev);
1183
1184	/* no_stream ops is called every cycle in no_stream state. */
1185	if (state == CRAS_IODEV_STATE_NO_STREAM_RUN)
1186		return odev->no_stream(odev, 1);
1187
1188	return 0;
1189}
1190
1191unsigned int cras_iodev_get_num_underruns(const struct cras_iodev *iodev)
1192{
1193	if (iodev->get_num_underruns)
1194		return iodev->get_num_underruns(iodev);
1195	return 0;
1196}
1197
1198unsigned int cras_iodev_get_num_severe_underruns(const struct cras_iodev *iodev)
1199{
1200	if (iodev->get_num_severe_underruns)
1201		return iodev->get_num_severe_underruns(iodev);
1202	return 0;
1203}
1204
1205int cras_iodev_reset_request(struct cras_iodev* iodev)
1206{
1207	/* Ignore requests if there is a pending request.
1208	 * This function sends the request from audio thread to main
1209	 * thread when audio thread finds a device is in a bad state
1210	 * e.g. severe underrun. Before main thread receives the
1211	 * request and resets device, audio thread might try to send
1212	 * multiple requests because it finds device is still in bad
1213	 * state. We should ignore requests in this cause. Otherwise,
1214	 * main thread will reset device multiple times.
1215	 * The flag is cleared in cras_iodev_open.
1216	 * */
1217	if (iodev->reset_request_pending)
1218		return 0;
1219	iodev->reset_request_pending = 1;
1220	return cras_device_monitor_reset_device(iodev);
1221}
1222
1223static void ramp_mute_callback(void *data)
1224{
1225	struct cras_iodev *odev = (struct cras_iodev *)data;
1226	cras_device_monitor_set_device_mute_state(odev);
1227}
1228
1229/* Used in audio thread. Check the docstrings of CRAS_IODEV_RAMP_REQUEST. */
1230int cras_iodev_start_ramp(struct cras_iodev *odev,
1231			  enum CRAS_IODEV_RAMP_REQUEST request)
1232{
1233	cras_ramp_cb cb = NULL;
1234	void *cb_data = NULL;
1235	int rc, up;
1236	float duration_secs;
1237
1238	/* Ignores request if device is closed. */
1239	if (!cras_iodev_is_open(odev))
1240		return 0;
1241
1242	switch (request) {
1243	case CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE:
1244		up = 1;
1245		duration_secs = RAMP_UNMUTE_DURATION_SECS;
1246		break;
1247	case CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK:
1248		up = 1;
1249		duration_secs = RAMP_NEW_STREAM_DURATION_SECS;
1250		break;
1251	/* Unmute -> mute. Callback to set mute state should be called after
1252	 * ramping is done. */
1253	case CRAS_IODEV_RAMP_REQUEST_DOWN_MUTE:
1254		up = 0;
1255		duration_secs = RAMP_MUTE_DURATION_SECS;
1256		cb = ramp_mute_callback;
1257		cb_data = (void*)odev;
1258		break;
1259	default:
1260		return -EINVAL;
1261	}
1262
1263	/* Starts ramping. */
1264	rc = cras_ramp_start(
1265			odev->ramp, up,
1266			duration_secs * odev->format->frame_rate,
1267			cb, cb_data);
1268
1269	if (rc)
1270		return rc;
1271
1272	/* Mute -> unmute case, unmute state should be set after ramping is
1273	 * started so device can start playing with samples close to 0. */
1274	if (request == CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE)
1275		cras_device_monitor_set_device_mute_state(odev);
1276
1277	return 0;
1278}
1279
1280int cras_iodev_set_mute(struct cras_iodev* iodev)
1281{
1282	if (!cras_iodev_is_open(iodev))
1283		return 0;
1284
1285	if (iodev->set_mute)
1286		iodev->set_mute(iodev);
1287	return 0;
1288}
1289