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 /*
      7  * cras_iodev represents playback or capture devices on the system.  Each iodev
      8  * will attach to a thread to render or capture audio.  For playback, this
      9  * thread will gather audio from the streams that are attached to the device and
     10  * render the samples it gets to the iodev.  For capture the process is
     11  * reversed, the samples are pulled from the device and passed on to the
     12  * attached streams.
     13  */
     14 #ifndef CRAS_IODEV_H_
     15 #define CRAS_IODEV_H_
     16 
     17 #include "cras_dsp.h"
     18 #include "cras_iodev_info.h"
     19 #include "cras_messages.h"
     20 
     21 struct buffer_share;
     22 struct cras_ramp;
     23 struct cras_rstream;
     24 struct cras_audio_area;
     25 struct cras_audio_format;
     26 struct audio_thread;
     27 struct cras_iodev;
     28 struct rate_estimator;
     29 
     30 /* Callback type for loopback listeners.  When enabled, this is called from the
     31  * playback path of an iodev with the samples that are being played back.
     32  */
     33 typedef int (*loopback_hook_t)(const uint8_t *frames, unsigned int nframes,
     34 			       const struct cras_audio_format *fmt,
     35 			       void *cb_data);
     36 
     37 /* State of an iodev.
     38  * no_stream state is only supported on output device.
     39  * Open state is only supported for device supporting start ops.
     40  */
     41 enum CRAS_IODEV_STATE {
     42 	CRAS_IODEV_STATE_CLOSE = 0,
     43 	CRAS_IODEV_STATE_OPEN = 1,
     44 	CRAS_IODEV_STATE_NORMAL_RUN = 2,
     45 	CRAS_IODEV_STATE_NO_STREAM_RUN = 3,
     46 };
     47 
     48 /* Holds an output/input node for this device.  An ionode is a control that
     49  * can be switched on and off such as headphones or speakers.
     50  * Members:
     51  *    dev - iodev which this node belongs to.
     52  *    idx - ionode index.
     53  *    plugged - true if the device is plugged.
     54  *    plugged_time - If plugged is true, this is the time it was attached.
     55  *    volume - per-node volume (0-100)
     56  *    capture_gain - per-node capture gain/attenuation (in 100*dBFS)
     57  *    left_right_swapped - If left and right output channels are swapped.
     58  *    type - Type displayed to the user.
     59  *    position - Specify where on the system this node locates.
     60  *    mic_positions - Whitespace-separated microphone positions using Cartesian
     61  *      coordinates in meters with ordering x, y, z. The string is formatted as:
     62  *      "x1 y1 z1 ... xn yn zn" for an n-microphone array.
     63  *    name - Name displayed to the user.
     64  *    active_hotword_model - name of the currently selected hotword model.
     65  *    softvol_scalers - pointer to software volume scalers.
     66  *    software_volume_needed - For output: True if the volume range of the node
     67  *      is smaller than desired. For input: True if this node needs software
     68  *      gain.
     69  *    max_software_gain - The maximum software gain in dBm if needed.
     70  *    stable_id - id for node that doesn't change after unplug/plug.
     71  *    stable_id_new - New stable_id, it will be deprecated and be put on
     72  *      stable_id.
     73  */
     74 struct cras_ionode {
     75 	struct cras_iodev *dev;
     76 	uint32_t idx;
     77 	int plugged;
     78 	struct timeval plugged_time;
     79 	unsigned int volume;
     80 	long capture_gain;
     81 	int left_right_swapped;
     82 	enum CRAS_NODE_TYPE type;
     83 	enum CRAS_NODE_POSITION position;
     84 	char mic_positions[CRAS_NODE_MIC_POS_BUFFER_SIZE];
     85 	char name[CRAS_NODE_NAME_BUFFER_SIZE];
     86 	char active_hotword_model[CRAS_NODE_HOTWORD_MODEL_BUFFER_SIZE];
     87 	float *softvol_scalers;
     88 	int software_volume_needed;
     89 	long max_software_gain;
     90 	unsigned int stable_id;
     91 	unsigned int stable_id_new;
     92 	struct cras_ionode *prev, *next;
     93 };
     94 
     95 /* An input or output device, that can have audio routed to/from it.
     96  * set_volume - Function to call if the system volume changes.
     97  * set_mute - Function to call if the system mute state changes.
     98  * set_capture_gain - Function to call if the system capture_gain changes.
     99  * set_capture_mute - Function to call if the system capture mute state changes.
    100  * set_swap_mode_for_node - Function to call to set swap mode for the node.
    101  * open_dev - Opens the device.
    102  * close_dev - Closes the device if it is open.
    103  * update_supported_formats - Refresh supported frame rates and channel counts.
    104  * frames_queued - The number of frames in the audio buffer, and fills tstamp
    105  *                 with the associated timestamp. The timestamp is {0, 0} when
    106  *                 the device hasn't started processing data (and on error).
    107  * delay_frames - The delay of the next sample in frames.
    108  * get_buffer - Returns a buffer to read/write to/from.
    109  * put_buffer - Marks a buffer from get_buffer as read/written.
    110  * flush_buffer - Flushes the buffer and return the number of frames flushed.
    111  * start - Starts running device. This is optionally supported on output device.
    112  *         If device supports this ops, device can be in CRAS_IODEV_STATE_OPEN
    113  *         state after being opened.
    114  *         If device does not support this ops, then device will be in
    115  *         CRAS_IODEV_STATE_NO_STREAM_RUN.
    116  * no_stream - (Optional) When there is no stream, we let device keep running
    117  *             for some time to save the time to open device for the next
    118  *             stream. This is the no stream state of an output device.
    119  *             The default action of no stream state is to fill zeros
    120  *             periodically. Device can implement this function to define
    121  *             its own optimization of entering/exiting no stream state.
    122  * output_should_wake - (Optional) Checks if audio thread should schedule a
    123  *                      wake for this output device. The default condition is
    124  *                      whether the device is running. Device can implement this
    125  *                      function to use its own condition.
    126  * output_underrun - (Optional) Handle output device underrun.
    127  * update_active_node - Update the active node when the selected device/node has
    128  *     changed.
    129  * update_channel_layout - Update the channel layout base on set iodev->format,
    130  *     expect the best available layout be filled to iodev->format.
    131  * set_hotword_model - Sets the hotword model to this iodev.
    132  * get_hotword_models - Gets a comma separated string of the list of supported
    133  *     hotword models of this iodev.
    134  * get_num_underruns - Gets number of underrun recorded so far.
    135  * get_num_severe_underruns - Gets number of severe underrun recorded since
    136  *                            iodev was created.
    137  * format - The audio format being rendered or captured to hardware.
    138  * ext_format - The audio format that is visible to the rest of the system.
    139  *     This can be different than the hardware if the device dsp changes it.
    140  * rate_est - Rate estimator to estimate the actual device rate.
    141  * area - Information about how the samples are stored.
    142  * info - Unique identifier for this device (index and name).
    143  * nodes - The output or input nodes available for this device.
    144  * active_node - The current node being used for playback or capture.
    145  * direction - Input or Output.
    146  * supported_rates - Array of sample rates supported by device 0-terminated.
    147  * supported_channel_counts - List of number of channels supported by device.
    148  * supported_formats - List of audio formats (s16le, s32le) supported by device.
    149  * buffer_size - Size of the audio buffer in frames.
    150  * min_buffer_level - Extra frames to keep queued in addition to requested.
    151  * dsp_context - The context used for dsp processing on the audio data.
    152  * dsp_name - The "dsp_name" dsp variable specified in the ucm config.
    153  * is_enabled - True if this iodev is enabled, false otherwise.
    154  * software_volume_needed - True if volume control is not supported by hardware.
    155  * streams - List of audio streams serviced by dev.
    156  * state - Device is in one of close, open, normal, or no_stream state defined
    157  *         in enum CRAS_IODEV_STATE.
    158  * min_cb_level - min callback level of any stream attached.
    159  * max_cb_level - max callback level of any stream attached.
    160  * buf_state - If multiple streams are writing to this device, then this
    161  *     keeps track of how much each stream has written.
    162  * idle_timeout - The timestamp when to close the dev after being idle.
    163  * pre_dsp_hook - Hook called before applying DSP, but after mixing.  Used for
    164  *     system loopback.
    165  * post_dsp_hook - Hook called after applying DSP.  Can be used for echo
    166  *     reference.
    167  * pre_dsp_hook_cb_data - Callback data that will be passing to pre_dsp_hook.
    168  * post_dsp_hook_cb_data - Callback data that will be passing to post_dsp_hook.
    169  * reset_request_pending - The flag for pending reset request.
    170  * ramp - The cras_ramp struct to control ramping up/down at mute/unmute and
    171  *        start of playback.
    172  */
    173 struct cras_iodev {
    174 	void (*set_volume)(struct cras_iodev *iodev);
    175 	void (*set_mute)(struct cras_iodev *iodev);
    176 	void (*set_capture_gain)(struct cras_iodev *iodev);
    177 	void (*set_capture_mute)(struct cras_iodev *iodev);
    178 	int (*set_swap_mode_for_node)(struct cras_iodev *iodev,
    179 				      struct cras_ionode *node,
    180 				      int enable);
    181 	int (*open_dev)(struct cras_iodev *iodev);
    182 	int (*close_dev)(struct cras_iodev *iodev);
    183 	int (*update_supported_formats)(struct cras_iodev *iodev);
    184 	int (*frames_queued)(const struct cras_iodev *iodev,
    185 			     struct timespec *tstamp);
    186 	int (*delay_frames)(const struct cras_iodev *iodev);
    187 	int (*get_buffer)(struct cras_iodev *iodev,
    188 			  struct cras_audio_area **area,
    189 			  unsigned *frames);
    190 	int (*put_buffer)(struct cras_iodev *iodev, unsigned nwritten);
    191 	int (*flush_buffer)(struct cras_iodev *iodev);
    192 	int (*start)(const struct cras_iodev *iodev);
    193 	int (*output_should_wake)(const struct cras_iodev *iodev);
    194 	int (*output_underrun)(struct cras_iodev *iodev);
    195 	int (*no_stream)(struct cras_iodev *iodev, int enable);
    196 	void (*update_active_node)(struct cras_iodev *iodev,
    197 				   unsigned node_idx, unsigned dev_enabled);
    198 	int (*update_channel_layout)(struct cras_iodev *iodev);
    199 	int (*set_hotword_model)(struct cras_iodev *iodev,
    200 				 const char *model_name);
    201 	char *(*get_hotword_models)(struct cras_iodev *iodev);
    202 	unsigned int (*get_num_underruns)(const struct cras_iodev *iodev);
    203 	unsigned int (*get_num_severe_underruns)(const struct cras_iodev *iodev);
    204 	struct cras_audio_format *format;
    205 	struct cras_audio_format *ext_format;
    206 	struct rate_estimator *rate_est;
    207 	struct cras_audio_area *area;
    208 	struct cras_iodev_info info;
    209 	struct cras_ionode *nodes;
    210 	struct cras_ionode *active_node;
    211 	enum CRAS_STREAM_DIRECTION direction;
    212 	size_t *supported_rates;
    213 	size_t *supported_channel_counts;
    214 	snd_pcm_format_t *supported_formats;
    215 	snd_pcm_uframes_t buffer_size;
    216 	unsigned int min_buffer_level;
    217 	struct cras_dsp_context *dsp_context;
    218 	const char *dsp_name;
    219 	int is_enabled;
    220 	int software_volume_needed;
    221 	struct dev_stream *streams;
    222 	enum CRAS_IODEV_STATE state;
    223 	unsigned int min_cb_level;
    224 	unsigned int max_cb_level;
    225 	struct buffer_share *buf_state;
    226 	struct timespec idle_timeout;
    227 	loopback_hook_t pre_dsp_hook;
    228 	loopback_hook_t post_dsp_hook;
    229 	void *pre_dsp_hook_cb_data;
    230 	void *post_dsp_hook_cb_data;
    231 	int reset_request_pending;
    232 	struct cras_ramp* ramp;
    233 	struct cras_iodev *prev, *next;
    234 };
    235 
    236 /*
    237  * Ramp request used in cras_iodev_start_ramp.
    238  *
    239  * - CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE: Mute->unmute.
    240  *   Change device to unmute state after ramping is stared,
    241  *                 that is, (a) in the plot.
    242  *
    243  *                                  ____
    244  *                            .... /
    245  *                      _____/
    246  *                          (a)
    247  *
    248  * - CRAS_IODEV_RAMP_REQUEST_DOWN_MUTE: Unmute->mute.
    249  *   Change device to mute state after ramping is done, that is,
    250  *                 (b) in the plot.
    251  *
    252  *                      _____
    253  *                           \....
    254  *                                \____
    255  *                                (b)
    256  *
    257  * - CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK: Ramping is requested because
    258  *   first sample of new stream is ready, there is no need to change mute/unmute
    259  *   state.
    260  */
    261 
    262 enum CRAS_IODEV_RAMP_REQUEST {
    263 	CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE = 0,
    264 	CRAS_IODEV_RAMP_REQUEST_DOWN_MUTE  = 1,
    265 	CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK = 2,
    266 };
    267 
    268 /*
    269  * Utility functions to be used by iodev implementations.
    270  */
    271 
    272 /* Sets up the iodev for the given format if possible.  If the iodev can't
    273  * handle the requested format, format conversion will happen in dev_stream.
    274  * It also allocates a dsp context for the iodev.
    275  * Args:
    276  *    iodev - the iodev you want the format for.
    277  *    fmt - the desired format.
    278  */
    279 int cras_iodev_set_format(struct cras_iodev *iodev,
    280 			  const struct cras_audio_format *fmt);
    281 
    282 /* Clear the format previously set for this iodev.
    283  *
    284  * Args:
    285  *    iodev - the iodev you want to free the format.
    286  */
    287 void cras_iodev_free_format(struct cras_iodev *iodev);
    288 
    289 /* Initializes the audio area for this iodev.
    290  * Args:
    291  *    iodev - the iodev to init audio area
    292  *    num_channels - the total number of channels
    293  */
    294 void cras_iodev_init_audio_area(struct cras_iodev *iodev,
    295 				int num_channels);
    296 
    297 /* Frees the audio area for this iodev.
    298  * Args:
    299  *    iodev - the iodev to free audio area
    300  */
    301 void cras_iodev_free_audio_area(struct cras_iodev *iodev);
    302 
    303 /* Free resources allocated for this iodev.
    304  *
    305  * Args:
    306  *    iodev - the iodev you want to free the resources for.
    307  */
    308 void cras_iodev_free_resources(struct cras_iodev *iodev);
    309 
    310 /* Fill timespec ts with the time to sleep based on the number of frames and
    311  * frame rate.
    312  * Args:
    313  *    frames - Number of frames in buffer..
    314  *    frame_rate - 44100, 48000, etc.
    315  *    ts - Filled with the time to sleep for.
    316  */
    317 void cras_iodev_fill_time_from_frames(size_t frames,
    318 				      size_t frame_rate,
    319 				      struct timespec *ts);
    320 
    321 /* Sets the timestamp for when the next sample will be rendered.  Determined by
    322  * combining the current time with the playback latency specified in frames.
    323  * Args:
    324  *    frame_rate - in Hz.
    325  *    frames - Delay specified in frames.
    326  *    ts - Filled with the time that the next sample will be played.
    327  */
    328 void cras_iodev_set_playback_timestamp(size_t frame_rate,
    329 				       size_t frames,
    330 				       struct cras_timespec *ts);
    331 
    332 /* Sets the time that the first sample in the buffer was captured at the ADC.
    333  * Args:
    334  *    frame_rate - in Hz.
    335  *    frames - Delay specified in frames.
    336  *    ts - Filled with the time that the next sample was captured.
    337  */
    338 void cras_iodev_set_capture_timestamp(size_t frame_rate,
    339 				      size_t frames,
    340 				      struct cras_timespec *ts);
    341 
    342 /* Update the "dsp_name" dsp variable. This may cause the dsp pipeline to be
    343  * reloaded.
    344  * Args:
    345  *    iodev - device which the state changes.
    346  */
    347 void cras_iodev_update_dsp(struct cras_iodev *iodev);
    348 
    349 
    350 /* Sets swap mode on a node using dsp. This function can be called when
    351  * dsp pipline is not created yet. It will take effect when dsp pipeline
    352  * is created later. If there is dsp pipeline, this function causes the dsp
    353  * pipeline to be reloaded and swap mode takes effect right away.
    354  * Args:
    355  *    iodev - device to be changed for swap mode.
    356  *    node - the node to be changed for swap mode.
    357  *    enable - 1 to enable swap mode, 0 otherwise.
    358  * Returns:
    359  *    0 on success, error code on failure.
    360  */
    361 int cras_iodev_dsp_set_swap_mode_for_node(struct cras_iodev *iodev,
    362 					   struct cras_ionode *node,
    363 					   int enable);
    364 
    365 /* Handles a plug event happening on this node.
    366  * Args:
    367  *    node - ionode on which a plug event was detected.
    368  *    plugged - true if the device was plugged, false for unplugged.
    369  */
    370 void cras_ionode_plug_event(struct cras_ionode *node, int plugged);
    371 
    372 /* Returns true if node a is preferred over node b. */
    373 int cras_ionode_better(struct cras_ionode *a, struct cras_ionode *b);
    374 
    375 /* Sets an attribute of an ionode on a device.
    376  * Args:
    377  *    ionode - ionode whose attribute we want to change.
    378  *    attr - the attribute we want to change.
    379  *    value - the value we want to set.
    380  */
    381 int cras_iodev_set_node_attr(struct cras_ionode *ionode,
    382 			     enum ionode_attr attr, int value);
    383 
    384 /* Adds a node to the iodev's node list. */
    385 void cras_iodev_add_node(struct cras_iodev *iodev, struct cras_ionode *node);
    386 
    387 /* Removes a node from iodev's node list. */
    388 void cras_iodev_rm_node(struct cras_iodev *iodev, struct cras_ionode *node);
    389 
    390 /* Assign a node to be the active node of the device */
    391 void cras_iodev_set_active_node(struct cras_iodev *iodev,
    392 				struct cras_ionode *node);
    393 
    394 /* Adjust the system volume based on the volume of the given node. */
    395 static inline unsigned int cras_iodev_adjust_node_volume(
    396 		const struct cras_ionode *node,
    397 		unsigned int system_volume)
    398 {
    399 	unsigned int node_vol_offset = 100 - node->volume;
    400 
    401 	if (system_volume > node_vol_offset)
    402 		return system_volume - node_vol_offset;
    403 	else
    404 		return 0;
    405 }
    406 
    407 /* Get the volume scaler for the active node. */
    408 static inline unsigned int cras_iodev_adjust_active_node_volume(
    409 		struct cras_iodev *iodev, unsigned int system_volume)
    410 {
    411 	if (!iodev->active_node)
    412 		return system_volume;
    413 
    414 	return cras_iodev_adjust_node_volume(iodev->active_node, system_volume);
    415 }
    416 
    417 /* Get the gain adjusted based on system for the active node. */
    418 static inline long cras_iodev_adjust_active_node_gain(
    419 		const struct cras_iodev *iodev, long system_gain)
    420 {
    421 	if (!iodev->active_node)
    422 		return system_gain;
    423 
    424 	return iodev->active_node->capture_gain + system_gain;
    425 }
    426 
    427 /* Returns true if the active node of the iodev needs software volume. */
    428 static inline int cras_iodev_software_volume_needed(
    429 		const struct cras_iodev *iodev)
    430 {
    431 	if (iodev->software_volume_needed)
    432 		return 1;
    433 
    434 	if (!iodev->active_node)
    435 		return 0;
    436 
    437 	return iodev->active_node->software_volume_needed;
    438 }
    439 
    440 /* Returns maximum software gain for the iodev.
    441  * Args:
    442  *    iodev - The device.
    443  * Returs:
    444  *    0 if software gain is not needed, or if there is no active node.
    445  *    Returns max_software_gain on active node if there is one. */
    446 static inline long cras_iodev_maximum_software_gain(
    447 		const struct cras_iodev *iodev)
    448 {
    449 	if (!cras_iodev_software_volume_needed(iodev))
    450 		return 0;
    451 	if (!iodev->active_node)
    452 		return 0;
    453 	return iodev->active_node->max_software_gain;
    454 }
    455 
    456 /* Gets the software gain scaler should be applied on the deivce.
    457  * Args:
    458  *    iodev - The device.
    459  * Returns:
    460  *    A scaler translated from system gain and active node gain dBm value.
    461  *    Returns 1.0 if software gain is not needed. */
    462 float cras_iodev_get_software_gain_scaler(const struct cras_iodev *iodev);
    463 
    464 /* Gets the software volume scaler of the iodev. The scaler should only be
    465  * applied if the device needs software volume. */
    466 float cras_iodev_get_software_volume_scaler(struct cras_iodev *iodev);
    467 
    468 /* Indicate that a stream has been added from the device. */
    469 int cras_iodev_add_stream(struct cras_iodev *iodev,
    470 			  struct dev_stream *stream);
    471 
    472 /* Indicate that a stream has been removed from the device. */
    473 struct dev_stream *cras_iodev_rm_stream(struct cras_iodev *iodev,
    474 					const struct cras_rstream *stream);
    475 
    476 /* Get the offset of this stream into the dev's buffer. */
    477 unsigned int cras_iodev_stream_offset(struct cras_iodev *iodev,
    478 				      struct dev_stream *stream);
    479 
    480 /* Get the maximum offset of any stream into the dev's buffer. */
    481 unsigned int cras_iodev_max_stream_offset(const struct cras_iodev *iodev);
    482 
    483 /* Tell the device how many frames the given stream wrote. */
    484 void cras_iodev_stream_written(struct cras_iodev *iodev,
    485 			       struct dev_stream *stream,
    486 			       unsigned int nwritten);
    487 
    488 /* All streams have written what they can, update the write pointers and return
    489  * the amount that has been filled by all streams and can be comitted to the
    490  * device.
    491  */
    492 unsigned int cras_iodev_all_streams_written(struct cras_iodev *iodev);
    493 
    494 /* Return the state of an iodev. */
    495 enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev *iodev);
    496 
    497 /* Open an iodev, does setup and invokes the open_dev callback. */
    498 int cras_iodev_open(struct cras_iodev *iodev, unsigned int cb_level);
    499 
    500 /* Open an iodev, does teardown and invokes the close_dev callback. */
    501 int cras_iodev_close(struct cras_iodev *iodev);
    502 
    503 /* Gets the available buffer to write/read audio.*/
    504 int cras_iodev_buffer_avail(struct cras_iodev *iodev, unsigned hw_level);
    505 
    506 /* Marks a buffer from get_buffer as read. */
    507 int cras_iodev_put_input_buffer(struct cras_iodev *iodev, unsigned int nframes);
    508 
    509 /* Marks a buffer from get_buffer as written. */
    510 int cras_iodev_put_output_buffer(struct cras_iodev *iodev, uint8_t *frames,
    511 				 unsigned int nframes);
    512 
    513 /* Returns a buffer to read from.
    514  * Args:
    515  *    iodev - The device.
    516  *    area - Filled with a pointer to the audio to read/write.
    517  *    frames - Filled with the number of frames that can be read/written.
    518  */
    519 int cras_iodev_get_input_buffer(struct cras_iodev *iodev,
    520 				struct cras_audio_area **area,
    521 				unsigned *frames);
    522 
    523 /* Returns a buffer to read from.
    524  * Args:
    525  *    iodev - The device.
    526  *    area - Filled with a pointer to the audio to read/write.
    527  *    frames - Filled with the number of frames that can be read/written.
    528  */
    529 int cras_iodev_get_output_buffer(struct cras_iodev *iodev,
    530 				 struct cras_audio_area **area,
    531 				 unsigned *frames);
    532 
    533 /* Update the estimated sample rate of the device. */
    534 int cras_iodev_update_rate(struct cras_iodev *iodev, unsigned int level,
    535 			   struct timespec *level_tstamp);
    536 
    537 /* Resets the rate estimator of the device. */
    538 int cras_iodev_reset_rate_estimator(const struct cras_iodev *iodev);
    539 
    540 /* Returns the rate of estimated frame rate and the claimed frame rate of
    541  * the device. */
    542 double cras_iodev_get_est_rate_ratio(const struct cras_iodev *iodev);
    543 
    544 /* Get the delay from DSP processing in frames. */
    545 int cras_iodev_get_dsp_delay(const struct cras_iodev *iodev);
    546 
    547 /* Returns the number of frames in the hardware buffer.
    548  * Args:
    549  *    iodev - The device.
    550  *    tstamp - The associated hardware time stamp.
    551  * Returns:
    552  *    Number of frames in the hardware buffer.
    553  *    Returns -EPIPE if there is severe underrun.
    554  */
    555 int cras_iodev_frames_queued(struct cras_iodev *iodev,
    556 			     struct timespec *tstamp);
    557 
    558 /* Get the delay for input/output in frames. */
    559 static inline int cras_iodev_delay_frames(const struct cras_iodev *iodev)
    560 {
    561 	return iodev->delay_frames(iodev) + cras_iodev_get_dsp_delay(iodev);
    562 }
    563 
    564 /* Returns true if the device is open. */
    565 static inline int cras_iodev_is_open(const struct cras_iodev *iodev)
    566 {
    567 	if (iodev && iodev->state != CRAS_IODEV_STATE_CLOSE)
    568 		return 1;
    569 	return 0;
    570 }
    571 
    572 /* Register a pre-dsp loopback hook.  Pass NULL to clear. */
    573 void cras_iodev_register_pre_dsp_hook(struct cras_iodev *iodev,
    574 				      loopback_hook_t loop_cb,
    575 				      void *cb_data);
    576 
    577 /* Register a post-dsp loopback hook.  Pass NULL to clear. */
    578 void cras_iodev_register_post_dsp_hook(struct cras_iodev *iodev,
    579 				       loopback_hook_t loop_cb,
    580 				       void *cb_data);
    581 
    582 /* Put 'frames' worth of zero samples into odev. */
    583 int cras_iodev_fill_odev_zeros(struct cras_iodev *odev, unsigned int frames);
    584 
    585 /* Gets the number of frames to play when audio thread sleeps.
    586  * Args:
    587  *    iodev[in] - The device.
    588  *    hw_level[out] - Pointer to number of frames in hardware.
    589  *    hw_tstamp[out] - Pointer to the timestamp for hw_level.
    590  * Returns:
    591  *    Number of frames to play in sleep for this output device.
    592  */
    593 unsigned int cras_iodev_frames_to_play_in_sleep(struct cras_iodev *odev,
    594 						unsigned int *hw_level,
    595 						struct timespec *hw_tstamp);
    596 
    597 /* Checks if audio thread should wake for this output device.
    598  * Args:
    599  *    iodev[in] - The output device.
    600  * Returns:
    601  *    1 if audio thread should wake for this output device. 0 otherwise.
    602  */
    603 int cras_iodev_odev_should_wake(const struct cras_iodev *odev);
    604 
    605 /* The default implementation of no_stream ops.
    606  * The default behavior is to fill some zeros when entering no stream state.
    607  * Note that when a device in no stream state enters into no stream state again,
    608  * device needs to fill some zeros again.
    609  * Do nothing to leave no stream state.
    610  * Args:
    611  *    iodev[in] - The output device.
    612  *    enable[in] - 1 to enter no stream playback, 0 to leave.
    613  * Returns:
    614  *    0 on success. Negative error code on failure.
    615  * */
    616 int cras_iodev_default_no_stream_playback(struct cras_iodev *odev, int enable);
    617 
    618 
    619 /* Get current state of iodev.
    620  * Args:
    621  *    iodev[in] - The device.
    622  * Returns:
    623  *    One of states defined in CRAS_IODEV_STATE.
    624  */
    625 enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev *iodev);
    626 
    627 /* Possibly transit state for output device.
    628  * Check if this output device needs to transit from open state/no_stream state
    629  * into normal run state. If device does not need transition and is still in
    630  * no stream state, call no_stream ops to do its work for one cycle.
    631  * Args:
    632  *    odev[in] - The output device.
    633  * Returns:
    634  *    0 on success. Negative error code on failure.
    635  */
    636 int cras_iodev_prepare_output_before_write_samples(struct cras_iodev *odev);
    637 
    638 /* Get number of underruns recorded so far.
    639  * Args:
    640  *    iodev[in] - The device.
    641  * Returns:
    642  *    An unsigned int for number of underruns recorded.
    643  */
    644 unsigned int cras_iodev_get_num_underruns(const struct cras_iodev *iodev);
    645 
    646 /* Get number of severe underruns recorded so far.
    647  * Args:
    648  *    iodev[in] - The device.
    649  * Returns:
    650  *    An unsigned int for number of severe underruns recorded since iodev
    651  *    was created.
    652  */
    653 unsigned int cras_iodev_get_num_severe_underruns(
    654 		const struct cras_iodev *iodev);
    655 
    656 /* Request main thread to re-open device. This should be used in audio thread
    657  * when it finds device is in a bad state. The request will be ignored if
    658  * there is still a pending request.
    659  * Args:
    660  *    iodev[in] - The device.
    661  * Returns:
    662  *    0 on success. Negative error code on failure.
    663  */
    664 int cras_iodev_reset_request(struct cras_iodev* iodev);
    665 
    666 /* Handle output underrun.
    667  * Args:
    668  *    odev[in] - The output device.
    669  * Returns:
    670  *    0 on success. Negative error code on failure.
    671  */
    672 int cras_iodev_output_underrun(struct cras_iodev *odev);
    673 
    674 /* Start ramping samples up/down on a device.
    675  * Args:
    676  *    iodev[in] - The device.
    677  *    request[in] - The request type. Check the docstrings of
    678  *                  CRAS_IODEV_RAMP_REQUEST.
    679  * Returns:
    680  *    0 on success. Negative error code on failure.
    681  */
    682 int cras_iodev_start_ramp(struct cras_iodev *odev,
    683 			  enum CRAS_IODEV_RAMP_REQUEST request);
    684 
    685 /* Set iodev to mute/unmute state.
    686  * Args:
    687  *    iodev[in] - The device.
    688  * Returns:
    689  *    0 on success. Negative error code on failure.
    690  */
    691 int cras_iodev_set_mute(struct cras_iodev* iodev);
    692 
    693 /*
    694  * Checks if an output iodev's volume is zero.
    695  * If there is an active node, check the adjusted node volume.
    696  * If there is no active node, check system volume.
    697  * Args:
    698  *    odev[in] - The device.
    699  * Returns:
    700  *    1 if device's volume is 0. 0 otherwise.
    701  */
    702 int cras_iodev_is_zero_volume(const struct cras_iodev *odev);
    703 
    704 #endif /* CRAS_IODEV_H_ */
    705