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