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