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