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