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 * This API creates multiple threads, one for control, and a thread per audio 6 * stream. The control thread is used to receive messages and notifications 7 * from the audio server, and manage the per-stream threads. API calls below 8 * may send messages to the control thread, or directly to the server. It is 9 * required that the control thread is running in order to support audio 10 * streams and notifications from the server. 11 * 12 * The API has multiple initialization sequences, but some of those can block 13 * while waiting for a response from the server. 14 * 15 * The following is the non-blocking API initialization sequence: 16 * cras_client_create() 17 * cras_client_set_connection_status_cb() (optional) 18 * cras_client_run_thread() 19 * cras_client_connect_async() 20 * 21 * The connection callback is executed asynchronously from the control thread 22 * when the connection has been established. The connection callback should be 23 * used to turn on or off interactions with any API call that communicates with 24 * the audio server or starts/stops audio streams. The above is implemented by 25 * cras_helper_create_connect_async(). 26 * 27 * The following alternative (deprecated) initialization sequence can ensure 28 * that the connection is established synchronously. 29 * 30 * Just connect to the server (no control thread): 31 * cras_client_create() 32 * cras_client_set_server_connection_cb() (optional) 33 * one of: 34 * cras_client_connect() (blocks forever) 35 * or 36 * cras_client_connect_timeout() (blocks for timeout) 37 * 38 * For API calls below that require the control thread to be running: 39 * cras_client_run_thread(); 40 * cras_client_connected_wait(); (blocks up to 1 sec.) 41 * 42 * The above minus setting the connection callback is implemented within 43 * cras_helper_create_connect(). 44 */ 45 46 #ifndef CRAS_CLIENT_H_ 47 #define CRAS_CLIENT_H_ 48 49 #ifdef __cplusplus 50 extern "C" { 51 #endif 52 53 #include <stdbool.h> 54 #include <stdint.h> 55 #include <sys/select.h> 56 57 #include "cras_iodev_info.h" 58 #include "cras_types.h" 59 #include "cras_util.h" 60 61 struct cras_client; 62 struct cras_hotword_handle; 63 struct cras_stream_params; 64 65 /* Callback for audio received or transmitted. 66 * Args (All pointer will be valid - except user_arg, that's up to the user): 67 * client: The client requesting service. 68 * stream_id - Unique identifier for the stream needing data read/written. 69 * samples - Read or write samples to/form here. 70 * frames - Maximum number of frames to read or write. 71 * sample_time - Playback time for the first sample read/written. 72 * user_arg - Value passed to add_stream; 73 * Return: 74 * Returns the number of frames read or written on success, or a negative 75 * number if there is a stream-fatal error. Returns EOF when the end of the 76 * stream is reached. 77 */ 78 typedef int (*cras_playback_cb_t)(struct cras_client *client, 79 cras_stream_id_t stream_id, 80 uint8_t *samples, 81 size_t frames, 82 const struct timespec *sample_time, 83 void *user_arg); 84 85 /* Callback for audio received and/or transmitted. 86 * Args (All pointer will be valid - except user_arg, that's up to the user): 87 * client: The client requesting service. 88 * stream_id - Unique identifier for the stream needing data read/written. 89 * captured_samples - Read samples form here. 90 * playback_samples - Read or write samples to here. 91 * frames - Maximum number of frames to read or write. 92 * captured_time - Time the first sample was read. 93 * playback_time - Playback time for the first sample written. 94 * user_arg - Value passed to add_stream; 95 * Return: 96 * Returns the number of frames read or written on success, or a negative 97 * number if there is a stream-fatal error. Returns EOF when the end of the 98 * stream is reached. 99 */ 100 typedef int (*cras_unified_cb_t)(struct cras_client *client, 101 cras_stream_id_t stream_id, 102 uint8_t *captured_samples, 103 uint8_t *playback_samples, 104 unsigned int frames, 105 const struct timespec *captured_time, 106 const struct timespec *playback_time, 107 void *user_arg); 108 109 /* Callback for handling stream errors. 110 * Args: 111 * client - The client created with cras_client_create(). 112 * stream_id - The ID for this stream. 113 * error - The error code, 114 * user_arg - The argument defined in cras_client_*_params_create(). 115 */ 116 typedef int (*cras_error_cb_t)(struct cras_client *client, 117 cras_stream_id_t stream_id, 118 int error, 119 void *user_arg); 120 121 /* Callback for handling server error. DEPRECATED 122 * 123 * Deprecated by cras_server_connection_status_cb_t: use that instead. 124 * This is equivalent to CRAS_CONN_STATUS_FAILED. 125 * 126 * This callback is executed rarely: only when the connection to the server has 127 * already been interrupted and could not be re-established due to resource 128 * allocation failure (memory or file-descriptors). The caller may attempt 129 * to reestablish communication once those resources are available with 130 * cras_client_connect_async(), or (blocking) cras_client_connect(). 131 * 132 * Args: 133 * client - The client created with cras_client_create(). 134 * user_arg - The argument defined in cras_client_set_server_errro_cb(). 135 */ 136 typedef void (*cras_server_error_cb_t)(struct cras_client *client, 137 void *user_arg); 138 139 /* Server connection status. */ 140 typedef enum cras_connection_status { 141 CRAS_CONN_STATUS_FAILED, 142 /* Resource allocation problem. Free resources, and retry the 143 * connection with cras_client_connect_async(), or (blocking) 144 * cras_client_connect(). Do not call cras_client_connect(), 145 * cras_client_connect_timeout(), or cras_client_destroy() 146 * from the callback. */ 147 CRAS_CONN_STATUS_DISCONNECTED, 148 /* The control thread is attempting to reconnect to the 149 * server in the background. Any attempt to access the 150 * server will fail or block (see 151 * cras_client_set_server_message_blocking(). */ 152 CRAS_CONN_STATUS_CONNECTED, 153 /* Connection is established. All state change callbacks 154 * have been re-registered, but audio streams must be 155 * restarted, and node state data must be updated. */ 156 } cras_connection_status_t; 157 158 /* Callback for handling server connection status. 159 * 160 * See also cras_client_set_connection_status_cb(). Do not call 161 * cras_client_connect(), cras_client_connect_timeout(), or 162 * cras_client_destroy() from this callback. 163 * 164 * Args: 165 * client - The client created with cras_client_create(). 166 * status - The status of the connection to the server. 167 * user_arg - The argument defined in 168 * cras_client_set_connection_status_cb(). 169 */ 170 typedef void (*cras_connection_status_cb_t)(struct cras_client *client, 171 cras_connection_status_t status, 172 void *user_arg); 173 174 /* Callback for setting thread priority. */ 175 typedef void (*cras_thread_priority_cb_t)(struct cras_client *client); 176 177 /* Callback for handling get hotword models reply. */ 178 typedef void (*get_hotword_models_cb_t)(struct cras_client *client, 179 const char *hotword_models); 180 181 /* Callback to wait for a hotword trigger. */ 182 typedef void (*cras_hotword_trigger_cb_t)(struct cras_client *client, 183 struct cras_hotword_handle *handle, 184 void *user_data); 185 186 /* Callback for handling hotword errors. */ 187 typedef int (*cras_hotword_error_cb_t)(struct cras_client *client, 188 struct cras_hotword_handle *handle, 189 int error, 190 void *user_data); 191 192 /* 193 * Client handling. 194 */ 195 196 /* Creates a new client. 197 * Args: 198 * client - Filled with a pointer to the new client. 199 * Returns: 200 * 0 on success (*client is filled with a valid cras_client pointer). 201 * Negative error code on failure(*client will be NULL). 202 */ 203 int cras_client_create(struct cras_client **client); 204 205 /* Destroys a client. 206 * Args: 207 * client - returned from "cras_client_create". 208 */ 209 void cras_client_destroy(struct cras_client *client); 210 211 /* Connects a client to the running server. 212 * Waits forever (until interrupted or connected). 213 * Args: 214 * client - pointer returned from "cras_client_create". 215 * Returns: 216 * 0 on success, or a negative error code on failure (from errno.h). 217 */ 218 int cras_client_connect(struct cras_client *client); 219 220 /* Connects a client to the running server, retries until timeout. 221 * Args: 222 * client - pointer returned from "cras_client_create". 223 * timeout_ms - timeout in milliseconds or negative to wait forever. 224 * Returns: 225 * 0 on success, or a negative error code on failure (from errno.h). 226 */ 227 int cras_client_connect_timeout(struct cras_client *client, 228 unsigned int timeout_ms); 229 230 /* Begins running the client control thread. 231 * 232 * Required for stream operations and other operations noted below. 233 * 234 * Args: 235 * client - the client to start (from cras_client_create). 236 * Returns: 237 * 0 on success or if the thread is already running, -EINVAL if the client 238 * pointer is NULL, or the negative result of pthread_create(). 239 */ 240 int cras_client_run_thread(struct cras_client *client); 241 242 /* Stops running a client. 243 * This function is executed automatically by cras_client_destroy(). 244 * Args: 245 * client - the client to stop (from cras_client_create). 246 * Returns: 247 * 0 on success or if the thread was already stopped, -EINVAL if the client 248 * isn't valid. 249 */ 250 int cras_client_stop(struct cras_client *client); 251 252 /* Wait up to 1 second for the client thread to complete the server connection. 253 * 254 * After cras_client_run_thread() is executed, this function can be used to 255 * ensure that the connection has been established with the server and ensure 256 * that any information about the server is up to date. If 257 * cras_client_run_thread() has not yet been executed, or cras_client_stop() 258 * was executed and thread isn't running, then this function returns -EINVAL. 259 * 260 * Args: 261 * client - pointer returned from "cras_client_create". 262 * Returns: 263 * 0 on success, or a negative error code on failure (from errno.h). 264 */ 265 int cras_client_connected_wait(struct cras_client *client); 266 267 /* Ask the client control thread to connect to the audio server. 268 * 269 * After cras_client_run_thread() is executed, this function can be used 270 * to ask the control thread to connect to the audio server asynchronously. 271 * The callback set with cras_client_set_connection_status_cb() will be 272 * executed when the connection is established. 273 * 274 * Args: 275 * client - The client from cras_client_create(). 276 * Returns: 277 * 0 on success, or a negative error code on failure (from errno.h). 278 * -EINVAL if the client pointer is invalid or the control thread is 279 * not running. 280 */ 281 int cras_client_connect_async(struct cras_client *client); 282 283 /* Sets server error callback. DEPRECATED 284 * 285 * See cras_server_error_cb_t for more information about this callback. 286 * 287 * Args: 288 * client - The client from cras_client_create. 289 * err_cb - The callback function to register. 290 * user_arg - Pointer that will be passed to the callback. 291 */ 292 void cras_client_set_server_error_cb(struct cras_client *client, 293 cras_server_error_cb_t err_cb, 294 void *user_arg); 295 296 /* Sets server connection status callback. 297 * 298 * See cras_connection_status_t for a description of the connection states 299 * and appropriate user action. 300 * 301 * Args: 302 * client - The client from cras_client_create. 303 * connection_cb - The callback function to register. 304 * user_arg - Pointer that will be passed to the callback. 305 */ 306 void cras_client_set_connection_status_cb( 307 struct cras_client *client, 308 cras_connection_status_cb_t connection_cb, 309 void *user_arg); 310 311 /* Sets callback for setting thread priority. 312 * Args: 313 * client - The client from cras_client_create. 314 * cb - The thread priority callback. 315 */ 316 void cras_client_set_thread_priority_cb(struct cras_client *client, 317 cras_thread_priority_cb_t cb); 318 319 /* Returns the current list of output devices. 320 * 321 * Requires that the connection to the server has been established. 322 * 323 * Data is copied and thus can become out of date. This call must be 324 * re-executed to get updates. 325 * 326 * Args: 327 * client - The client from cras_client_create. 328 * devs - Array that will be filled with device info. 329 * nodes - Array that will be filled with node info. 330 * *num_devs - Maximum number of devices to put in the array. 331 * *num_nodes - Maximum number of nodes to put in the array. 332 * Returns: 333 * 0 on success, -EINVAL if the client isn't valid or isn't running. 334 * *num_devs is set to the actual number of devices info filled. 335 * *num_nodes is set to the actual number of nodes info filled. 336 */ 337 int cras_client_get_output_devices(const struct cras_client *client, 338 struct cras_iodev_info *devs, 339 struct cras_ionode_info *nodes, 340 size_t *num_devs, size_t *num_nodes); 341 342 /* Returns the current list of input devices. 343 * 344 * Requires that the connection to the server has been established. 345 * 346 * Data is copied and thus can become out of date. This call must be 347 * re-executed to get updates. 348 * 349 * Args: 350 * client - The client from cras_client_create. 351 * devs - Array that will be filled with device info. 352 * nodes - Array that will be filled with node info. 353 * *num_devs - Maximum number of devices to put in the array. 354 * *num_nodes - Maximum number of nodes to put in the array. 355 * Returns: 356 * 0 on success, -EINVAL if the client isn't valid or isn't running. 357 * *num_devs is set to the actual number of devices info filled. 358 * *num_nodes is set to the actual number of nodes info filled. 359 */ 360 int cras_client_get_input_devices(const struct cras_client *client, 361 struct cras_iodev_info *devs, 362 struct cras_ionode_info *nodes, 363 size_t *num_devs, size_t *num_nodes); 364 365 /* Returns the current list of clients attached to the server. 366 * 367 * Requires that the connection to the server has been established. 368 * 369 * Data is copied and thus can become out of date. This call must be 370 * re-executed to get updates. 371 * 372 * Args: 373 * client - This client (from cras_client_create). 374 * clients - Array that will be filled with a list of attached clients. 375 * max_clients - Maximum number of clients to put in the array. 376 * Returns: 377 * The number of attached clients. This may be more that max_clients passed 378 * in, this indicates that all of the clients wouldn't fit in the provided 379 * array. 380 */ 381 int cras_client_get_attached_clients(const struct cras_client *client, 382 struct cras_attached_client_info *clients, 383 size_t max_clients); 384 385 /* Find a node info with the matching node id. 386 * 387 * Requires that the connection to the server has been established. 388 * 389 * Data is copied and thus can become out of date. This call must be 390 * re-executed to get updates. 391 * 392 * Args: 393 * client - This client (from cras_client_create). 394 * input - Non-zero for input nodes, zero for output nodes. 395 * node_id - The node id to look for. 396 * node_info - The information about the ionode will be returned here. 397 * Returns: 398 * 0 if successful, negative on error; -ENOENT if the node cannot be found. 399 */ 400 int cras_client_get_node_by_id(const struct cras_client *client, 401 int input, 402 const cras_node_id_t node_id, 403 struct cras_ionode_info* node_info); 404 405 /* Checks if the output device with the given name is currently plugged in. 406 * 407 * For internal devices this checks that jack state, for USB devices this will 408 * always be true if they are present. The name parameter can be the complete 409 * name or any unique prefix of the name. If the name is not unique the first 410 * matching name will be checked. 411 * 412 * Requires that the connection to the server has been established. 413 * 414 * Data is copied and thus can become out of date. This call must be 415 * re-executed to get updates. 416 * 417 * Args: 418 * client - The client from cras_client_create. 419 * name - Name of the device to check. 420 * Returns: 421 * 1 if the device exists and is plugged, 0 otherwise. 422 */ 423 int cras_client_output_dev_plugged(const struct cras_client *client, 424 const char *name); 425 426 /* Set the value of an attribute of an ionode. 427 * 428 * Args: 429 * client - The client from cras_client_create. 430 * node_id - The id of the ionode. 431 * attr - the attribute we want to change. 432 * value - the value we want to set. 433 * Returns: 434 * Returns 0 for success, negative on error (from errno.h). 435 */ 436 int cras_client_set_node_attr(struct cras_client *client, 437 cras_node_id_t node_id, 438 enum ionode_attr attr, 439 int value); 440 441 /* Select the preferred node for playback/capture. 442 * 443 * Args: 444 * client - The client from cras_client_create. 445 * direction - The direction of the ionode. 446 * node_id - The id of the ionode. If node_id is the special value 0, then 447 * the preference is cleared and cras will choose automatically. 448 */ 449 int cras_client_select_node(struct cras_client *client, 450 enum CRAS_STREAM_DIRECTION direction, 451 cras_node_id_t node_id); 452 453 /* Adds an active node for playback/capture. 454 * 455 * Args: 456 * client - The client from cras_client_create. 457 * direction - The direction of the ionode. 458 * node_id - The id of the ionode. If there's no node matching given 459 * id, nothing will happen in CRAS. 460 */ 461 int cras_client_add_active_node(struct cras_client *client, 462 enum CRAS_STREAM_DIRECTION direction, 463 cras_node_id_t node_id); 464 465 /* Removes an active node for playback/capture. 466 * 467 * Args: 468 * client - The client from cras_client_create. 469 * direction - The direction of the ionode. 470 * node_id - The id of the ionode. If there's no node matching given 471 * id, nothing will happen in CRAS. 472 */ 473 int cras_client_rm_active_node(struct cras_client *client, 474 enum CRAS_STREAM_DIRECTION direction, 475 cras_node_id_t node_id); 476 477 478 /* Asks the server to reload dsp plugin configuration from the ini file. 479 * 480 * Args: 481 * client - The client from cras_client_create. 482 * Returns: 483 * 0 on success, -EINVAL if the client isn't valid or isn't running. 484 */ 485 int cras_client_reload_dsp(struct cras_client *client); 486 487 /* Asks the server to dump current dsp information to syslog. 488 * 489 * Args: 490 * client - The client from cras_client_create. 491 * Returns: 492 * 0 on success, -EINVAL if the client isn't valid or isn't running. 493 */ 494 int cras_client_dump_dsp_info(struct cras_client *client); 495 496 /* Asks the server to dump current audio thread information. 497 * 498 * Args: 499 * client - The client from cras_client_create. 500 * cb - A function to call when the data is received. 501 * Returns: 502 * 0 on success, -EINVAL if the client isn't valid or isn't running. 503 */ 504 int cras_client_update_audio_debug_info( 505 struct cras_client *client, void (*cb)(struct cras_client *)); 506 507 /* Asks the server to dump current audio thread snapshots. 508 * 509 * Args: 510 * client - The client from cras_client_create. 511 * cb - A function to call when the data is received. 512 * Returns: 513 * 0 on success, -EINVAL if the client isn't valid or isn't running. 514 */ 515 int cras_client_update_audio_thread_snapshots( 516 struct cras_client *client, void (*cb)(struct cras_client *)); 517 518 /* 519 * Stream handling. 520 */ 521 522 /* Setup stream configuration parameters. 523 * Args: 524 * direction - playback(CRAS_STREAM_OUTPUT) or capture(CRAS_STREAM_INPUT). 525 * buffer_frames - total number of audio frames to buffer (dictates latency). 526 * cb_threshold - For playback, call back for more data when the buffer 527 * reaches this level. For capture, this is ignored (Audio callback will 528 * be called when buffer_frames have been captured). 529 * unused - No longer used. 530 * stream_type - media or talk (currently only support "default"). 531 * flags - None currently used. 532 * user_data - Pointer that will be passed to the callback. 533 * aud_cb - Called when audio is needed(playback) or ready(capture). Allowed 534 * return EOF to indicate that the stream should terminate. 535 * err_cb - Called when there is an error with the stream. 536 * format - The format of the audio stream. Specifies bits per sample, 537 * number of channels, and sample rate. 538 */ 539 struct cras_stream_params *cras_client_stream_params_create( 540 enum CRAS_STREAM_DIRECTION direction, 541 size_t buffer_frames, 542 size_t cb_threshold, 543 size_t unused, 544 enum CRAS_STREAM_TYPE stream_type, 545 uint32_t flags, 546 void *user_data, 547 cras_playback_cb_t aud_cb, 548 cras_error_cb_t err_cb, 549 struct cras_audio_format *format); 550 551 /* Functions to enable or disable specific effect on given stream parameter. 552 * Args: 553 * params - Stream configuration parameters. 554 */ 555 void cras_client_stream_params_enable_aec(struct cras_stream_params *params); 556 void cras_client_stream_params_disable_aec(struct cras_stream_params *params); 557 void cras_client_stream_params_enable_ns(struct cras_stream_params *params); 558 void cras_client_stream_params_disable_ns(struct cras_stream_params *params); 559 void cras_client_stream_params_enable_agc(struct cras_stream_params *params); 560 void cras_client_stream_params_disable_agc(struct cras_stream_params *params); 561 void cras_client_stream_params_enable_vad(struct cras_stream_params *params); 562 void cras_client_stream_params_disable_vad(struct cras_stream_params *params); 563 564 /* Setup stream configuration parameters. 565 * Args: 566 * direction - playback(CRAS_STREAM_OUTPUT) or capture(CRAS_STREAM_INPUT) or 567 * loopback(CRAS_STREAM_POST_MIX_PRE_DSP). 568 * block_size - The number of frames per callback(dictates latency). 569 * stream_type - media or talk (currently only support "default"). 570 * flags - None currently used. 571 * user_data - Pointer that will be passed to the callback. 572 * unified_cb - Called for streams that do simultaneous input/output. 573 * err_cb - Called when there is an error with the stream. 574 * format - The format of the audio stream. Specifies bits per sample, 575 * number of channels, and sample rate. 576 */ 577 struct cras_stream_params *cras_client_unified_params_create( 578 enum CRAS_STREAM_DIRECTION direction, 579 unsigned int block_size, 580 enum CRAS_STREAM_TYPE stream_type, 581 uint32_t flags, 582 void *user_data, 583 cras_unified_cb_t unified_cb, 584 cras_error_cb_t err_cb, 585 struct cras_audio_format *format); 586 587 /* Destroy stream params created with cras_client_stream_params_create. */ 588 void cras_client_stream_params_destroy(struct cras_stream_params *params); 589 590 /* Creates a new stream and return the stream id or < 0 on error. 591 * 592 * Requires execution of cras_client_run_thread(), and an active connection 593 * to the audio server. 594 * 595 * Args: 596 * client - The client to add the stream to (from cras_client_create). 597 * stream_id_out - On success will be filled with the new stream id. 598 * Guaranteed to be set before any callbacks are made. 599 * config - The cras_stream_params struct specifying the parameters for the 600 * stream. 601 * Returns: 602 * 0 on success, negative error code on failure (from errno.h). 603 */ 604 int cras_client_add_stream(struct cras_client *client, 605 cras_stream_id_t *stream_id_out, 606 struct cras_stream_params *config); 607 608 /* Creates a pinned stream and return the stream id or < 0 on error. 609 * 610 * Requires execution of cras_client_run_thread(), and an active connection 611 * to the audio server. 612 * 613 * Args: 614 * client - The client to add the stream to (from cras_client_create). 615 * dev_idx - Index of the device to attach the newly created stream. 616 * stream_id_out - On success will be filled with the new stream id. 617 * Guaranteed to be set before any callbacks are made. 618 * config - The cras_stream_params struct specifying the parameters for the 619 * stream. 620 * Returns: 621 * 0 on success, negative error code on failure (from errno.h). 622 */ 623 int cras_client_add_pinned_stream(struct cras_client *client, 624 uint32_t dev_idx, 625 cras_stream_id_t *stream_id_out, 626 struct cras_stream_params *config); 627 628 /* Removes a currently playing/capturing stream. 629 * 630 * Requires execution of cras_client_run_thread(). 631 * 632 * Args: 633 * client - Client to remove the stream (returned from cras_client_create). 634 * stream_id - ID returned from cras_client_add_stream to identify the stream 635 to remove. 636 * Returns: 637 * 0 on success negative error code on failure (from errno.h). 638 */ 639 int cras_client_rm_stream(struct cras_client *client, 640 cras_stream_id_t stream_id); 641 642 /* Sets the volume scaling factor for the given stream. 643 * 644 * Requires execution of cras_client_run_thread(). 645 * 646 * Args: 647 * client - Client owning the stream. 648 * stream_id - ID returned from cras_client_add_stream. 649 * volume_scaler - 0.0-1.0 the new value to scale this stream by. 650 */ 651 int cras_client_set_stream_volume(struct cras_client *client, 652 cras_stream_id_t stream_id, 653 float volume_scaler); 654 655 /* 656 * System level functions. 657 */ 658 659 /* Sets the volume of the system. 660 * 661 * Volume here ranges from 0 to 100, and will be translated to dB based on the 662 * output-specific volume curve. 663 * 664 * Args: 665 * client - The client from cras_client_create. 666 * volume - 0-100 the new volume index. 667 * Returns: 668 * 0 for success, -EPIPE if there is an I/O error talking to the server, or 669 * -EINVAL if 'client' is invalid. 670 */ 671 int cras_client_set_system_volume(struct cras_client *client, size_t volume); 672 673 /* Sets the capture gain of the system. 674 * 675 * Gain is specified in dBFS * 100. For example 5dB of gain would be specified 676 * with an argument of 500, while -10 would be specified with -1000. 677 * 678 * Args: 679 * client - The client from cras_client_create. 680 * gain - The gain in dBFS * 100. 681 * Returns: 682 * 0 for success, -EPIPE if there is an I/O error talking to the server, or 683 * -EINVAL if 'client' is invalid. 684 */ 685 int cras_client_set_system_capture_gain(struct cras_client *client, long gain); 686 687 /* Sets the mute state of the system. 688 * 689 * Args: 690 * client - The client from cras_client_create. 691 * mute - 0 is un-mute, 1 is muted. 692 * Returns: 693 * 0 for success, -EPIPE if there is an I/O error talking to the server, or 694 * -EINVAL if 'client' is invalid. 695 */ 696 int cras_client_set_system_mute(struct cras_client *client, int mute); 697 698 /* Sets the user mute state of the system. 699 * 700 * This is used for mutes caused by user interaction. Like the mute key. 701 * 702 * Args: 703 * client - The client from cras_client_create. 704 * mute - 0 is un-mute, 1 is muted. 705 * Returns: 706 * 0 for success, -EPIPE if there is an I/O error talking to the server, or 707 * -EINVAL if 'client' is invalid. 708 */ 709 int cras_client_set_user_mute(struct cras_client *client, int mute); 710 711 /* Sets the mute locked state of the system. 712 * 713 * Changing mute state is impossible when this flag is set to locked. 714 * 715 * Args: 716 * client - The client from cras_client_create. 717 * locked - 0 is un-locked, 1 is locked. 718 * Returns: 719 * 0 for success, -EPIPE if there is an I/O error talking to the server, or 720 * -EINVAL if 'client' is invalid. 721 */ 722 int cras_client_set_system_mute_locked(struct cras_client *client, int locked); 723 724 /* Sets the capture mute state of the system. 725 * 726 * Recordings will be muted when this is set. 727 * 728 * Args: 729 * client - The client from cras_client_create. 730 * mute - 0 is un-mute, 1 is muted. 731 * Returns: 732 * 0 for success, -EPIPE if there is an I/O error talking to the server, or 733 * -EINVAL if 'client' is invalid. 734 */ 735 int cras_client_set_system_capture_mute(struct cras_client *client, int mute); 736 737 /* Sets the capture mute locked state of the system. 738 * 739 * Changing mute state is impossible when this flag is set to locked. 740 * 741 * Args: 742 * client - The client from cras_client_create. 743 * locked - 0 is un-locked, 1 is locked. 744 * Returns: 745 * 0 for success, -EPIPE if there is an I/O error talking to the server, or 746 * -EINVAL if 'client' is invalid. 747 */ 748 int cras_client_set_system_capture_mute_locked(struct cras_client *client, 749 int locked); 750 751 /* Gets the current system volume. 752 * 753 * Requires that the connection to the server has been established. 754 * 755 * Args: 756 * client - The client from cras_client_create. 757 * Returns: 758 * The current system volume between 0 and 100. 759 */ 760 size_t cras_client_get_system_volume(const struct cras_client *client); 761 762 /* Gets the current system capture gain. 763 * 764 * Requires that the connection to the server has been established. 765 * 766 * Args: 767 * client - The client from cras_client_create. 768 * Returns: 769 * The current system capture volume in dB * 100. 770 */ 771 long cras_client_get_system_capture_gain(const struct cras_client *client); 772 773 /* Gets the current system mute state. 774 * 775 * Requires that the connection to the server has been established. 776 * 777 * Args: 778 * client - The client from cras_client_create. 779 * Returns: 780 * 0 if not muted, 1 if it is. 781 */ 782 int cras_client_get_system_muted(const struct cras_client *client); 783 784 /* Gets the current user mute state. 785 * 786 * Requires that the connection to the server has been established. 787 * 788 * Args: 789 * client - The client from cras_client_create. 790 * Returns: 791 * 0 if not muted, 1 if it is. 792 */ 793 int cras_client_get_user_muted(const struct cras_client *client); 794 795 /* Gets the current system capture mute state. 796 * 797 * Requires that the connection to the server has been established. 798 * 799 * Args: 800 * client - The client from cras_client_create. 801 * Returns: 802 * 0 if capture is not muted, 1 if it is. 803 */ 804 int cras_client_get_system_capture_muted(const struct cras_client *client); 805 806 /* Gets the current minimum system volume. 807 * Args: 808 * client - The client from cras_client_create. 809 * Returns: 810 * The minimum value for the current output device in dBFS * 100. This is 811 * the level of attenuation at volume == 1. 812 */ 813 long cras_client_get_system_min_volume(const struct cras_client *client); 814 815 /* Gets the current maximum system volume. 816 * Args: 817 * client - The client from cras_client_create. 818 * Returns: 819 * The maximum value for the current output device in dBFS * 100. This is 820 * the level of attenuation at volume == 100. 821 */ 822 long cras_client_get_system_max_volume(const struct cras_client *client); 823 824 /* Gets the current minimum system capture gain. 825 * 826 * Requires that the connection to the server has been established. 827 * 828 * Args: 829 * client - The client from cras_client_create. 830 * Returns: 831 * The minimum capture gain for the current input device in dBFS * 100. 832 */ 833 long cras_client_get_system_min_capture_gain(const struct cras_client *client); 834 835 /* Gets the current maximum system capture gain. 836 * 837 * Requires that the connection to the server has been established. 838 * 839 * Args: 840 * client - The client from cras_client_create. 841 * Returns: 842 * The maximum capture gain for the current input device in dBFS * 100. 843 */ 844 long cras_client_get_system_max_capture_gain(const struct cras_client *client); 845 846 /* Gets audio debug info. 847 * 848 * Requires that the connection to the server has been established. 849 * Access to the resulting pointer is not thread-safe. 850 * 851 * Args: 852 * client - The client from cras_client_create. 853 * Returns: 854 * A pointer to the debug info. This info is only updated when requested by 855 * calling cras_client_update_audio_debug_info. 856 */ 857 const struct audio_debug_info *cras_client_get_audio_debug_info( 858 const struct cras_client *client); 859 860 /* Gets audio thread snapshot buffer. 861 * 862 * Requires that the connection to the server has been established. 863 * Access to the resulting pointer is not thread-safe. 864 * 865 * Args: 866 * client - The client from cras_client_create. 867 * Returns: 868 * A pointer to the snapshot buffer. This info is only updated when 869 * requested by calling cras_client_update_audio_thread_snapshots. 870 */ 871 const struct cras_audio_thread_snapshot_buffer * 872 cras_client_get_audio_thread_snapshot_buffer( 873 const struct cras_client *client); 874 875 /* Gets the number of streams currently attached to the server. 876 * 877 * This is the total number of capture and playback streams. If the ts argument 878 * is not null, then it will be filled with the last time audio was played or 879 * recorded. ts will be set to the current time if streams are currently 880 * active. 881 * 882 * Requires that the connection to the server has been established. 883 * 884 * Args: 885 * client - The client from cras_client_create. 886 * ts - Filled with the timestamp of the last stream. 887 * Returns: 888 * The number of active streams. 889 */ 890 unsigned cras_client_get_num_active_streams(const struct cras_client *client, 891 struct timespec *ts); 892 893 894 /* 895 * Utility functions. 896 */ 897 898 /* Returns the number of bytes in an audio frame for a stream. 899 * Args: 900 * format - The format of the audio stream. Specifies bits per sample, 901 * number of channels, and sample rate. 902 * Returns: 903 * Positive number of bytes in a frame, or a negative error code if fmt is 904 * NULL. 905 */ 906 int cras_client_format_bytes_per_frame(struct cras_audio_format *fmt); 907 908 /* For playback streams, calculates the latency of the next sample written. 909 * Only valid when called from the audio callback function for the stream 910 * (aud_cb). 911 * Args: 912 * sample_time - The sample time stamp passed in to aud_cb. 913 * delay - Out parameter will be filled with the latency. 914 * Returns: 915 * 0 on success, -EINVAL if delay is NULL. 916 */ 917 int cras_client_calc_playback_latency(const struct timespec *sample_time, 918 struct timespec *delay); 919 920 /* For capture returns the latency of the next frame to be read from the buffer 921 * (based on when it was captured). Only valid when called from the audio 922 * callback function for the stream (aud_cb). 923 * Args: 924 * sample_time - The sample time stamp passed in to aud_cb. 925 * delay - Out parameter will be filled with the latency. 926 * Returns: 927 * 0 on success, -EINVAL if delay is NULL. 928 */ 929 int cras_client_calc_capture_latency(const struct timespec *sample_time, 930 struct timespec *delay); 931 932 /* Set the volume of the given output node. Only for output nodes. 933 * 934 * Args: 935 * client - The client from cras_client_create. 936 * node_id - ID of the node. 937 * volume - New value for node volume. 938 */ 939 int cras_client_set_node_volume(struct cras_client *client, 940 cras_node_id_t node_id, 941 uint8_t volume); 942 943 /* Swap the left and right channel of the given node. 944 * 945 * Args: 946 * client - The client from cras_client_create. 947 * node_id - ID of the node. 948 * enable - 1 to enable swap mode, 0 to disable. 949 */ 950 int cras_client_swap_node_left_right(struct cras_client *client, 951 cras_node_id_t node_id, int enable); 952 953 /* Set the capture gain of the given input node. Only for input nodes. 954 * 955 * Args: 956 * client - The client from cras_client_create. 957 * node_id - ID of the node. 958 * gain - New capture gain for the node. 959 */ 960 int cras_client_set_node_capture_gain(struct cras_client *client, 961 cras_node_id_t node_id, 962 long gain); 963 964 /* Add a test iodev to the iodev list. 965 * 966 * Args: 967 * client - The client from cras_client_create. 968 * type - The type of test iodev, see cras_types.h 969 */ 970 int cras_client_add_test_iodev(struct cras_client *client, 971 enum TEST_IODEV_TYPE type); 972 973 /* Send a test command to a test iodev. 974 * 975 * Args: 976 * client - The client from cras_client_create. 977 * iodev_idx - The index of the test iodev. 978 * command - The command to send. 979 * data_len - Length of command data. 980 * data - Command data. 981 */ 982 int cras_client_test_iodev_command(struct cras_client *client, 983 unsigned int iodev_idx, 984 enum CRAS_TEST_IODEV_CMD command, 985 unsigned int data_len, 986 const uint8_t *data); 987 988 /* Finds the first node of the given type. 989 * 990 * This is used for finding a special hotword node. 991 * 992 * Requires that the connection to the server has been established. 993 * 994 * Args: 995 * client - The client from cras_client_create. 996 * type - The type of device to find. 997 * direction - Search input or output devices. 998 * node_id - The found node on success. 999 * Returns: 1000 * 0 on success, a negative error on failure. 1001 */ 1002 int cras_client_get_first_node_type_idx(const struct cras_client *client, 1003 enum CRAS_NODE_TYPE type, 1004 enum CRAS_STREAM_DIRECTION direction, 1005 cras_node_id_t *node_id); 1006 1007 /* Finds the first device that contains a node of the given type. 1008 * 1009 * This is used for finding a special hotword device. 1010 * 1011 * Requires that the connection to the server has been established. 1012 * 1013 * Args: 1014 * client - The client from cras_client_create. 1015 * type - The type of device to find. 1016 * direction - Search input or output devices. 1017 * Returns the device index of a negative error on failure. 1018 */ 1019 int cras_client_get_first_dev_type_idx(const struct cras_client *client, 1020 enum CRAS_NODE_TYPE type, 1021 enum CRAS_STREAM_DIRECTION direction); 1022 1023 /* Sets the suspend state of audio playback and capture. 1024 * 1025 * Set this before putting the system into suspend. 1026 * 1027 * Args: 1028 * client - The client from cras_client_create. 1029 * suspend - Suspend the system if non-zero, otherwise resume. 1030 */ 1031 int cras_client_set_suspend(struct cras_client *client, int suspend); 1032 1033 /* Configures the global converter for output remixing. 1034 * 1035 * Args: 1036 * client - The client from cras_client_create. 1037 * num_channels - Number of output channels. 1038 * coefficient - Float array representing |num_channels| * |num_channels| 1039 * matrix. Channels of mixed PCM output will be remixed by 1040 * multiplying this matrix. 1041 */ 1042 int cras_client_config_global_remix(struct cras_client *client, 1043 unsigned num_channels, 1044 float *coefficient); 1045 1046 /* Gets the set of supported hotword language models on a node. The supported 1047 * models may differ on different nodes. 1048 * 1049 * Args: 1050 * client - The client from cras_client_create. 1051 * node_id - ID of a hotword input node (CRAS_NODE_TYPE_HOTWORD). 1052 * cb - The function to be called when hotword models are ready. 1053 * Returns: 1054 * 0 on success. 1055 */ 1056 int cras_client_get_hotword_models(struct cras_client *client, 1057 cras_node_id_t node_id, 1058 get_hotword_models_cb_t cb); 1059 1060 /* Sets the hotword language model on a node. If there are existing streams on 1061 * the hotword input node when this function is called, they need to be closed 1062 * then re-opend for the model change to take effect. 1063 * Args: 1064 * client - The client from cras_client_create. 1065 * node_id - ID of a hotword input node (CRAS_NODE_TYPE_HOTWORD). 1066 * model_name - Name of the model to use, e.g. "en_us". 1067 * Returns: 1068 * 0 on success. 1069 * -EINVAL if client or node_id is invalid. 1070 * -ENOENT if the specified model is not found. 1071 */ 1072 int cras_client_set_hotword_model(struct cras_client *client, 1073 cras_node_id_t node_id, 1074 const char *model_name); 1075 1076 /* 1077 * Creates a hotword stream and waits for the hotword to trigger. 1078 * 1079 * Args: 1080 * client - The client to add the stream to (from cras_client_create). 1081 * user_data - Pointer that will be passed to the callback. 1082 * trigger_cb - Called when a hotword is triggered. 1083 * err_cb - Called when there is an error with the stream. 1084 * handle_out - On success will be filled with a cras_hotword_handle. 1085 * Returns: 1086 * 0 on success, negative error code on failure (from errno.h). 1087 */ 1088 int cras_client_enable_hotword_callback( 1089 struct cras_client *client, 1090 void *user_data, 1091 cras_hotword_trigger_cb_t trigger_cb, 1092 cras_hotword_error_cb_t err_cb, 1093 struct cras_hotword_handle **handle_out); 1094 1095 /* 1096 * Closes a hotword stream that was created by cras_client_wait_for_hotword. 1097 * 1098 * Args: 1099 * client - Client to remove the stream (returned from cras_client_create). 1100 * handle - cras_hotword_handle returned from cras_client_wait_for_hotword. 1101 * Returns: 1102 * 0 on success negative error code on failure (from errno.h). 1103 */ 1104 int cras_client_disable_hotword_callback(struct cras_client *client, 1105 struct cras_hotword_handle *handle); 1106 1107 /* Starts or stops the aec dump task on server side. 1108 * Args: 1109 * client - The client from cras_client_create. 1110 * stream_id - The id of the input stream running with aec effect. 1111 * start - True to start APM debugging, otherwise to stop it. 1112 * fd - File descriptor of the file to store aec dump result. 1113 */ 1114 int cras_client_set_aec_dump(struct cras_client *client, 1115 cras_stream_id_t stream_id, 1116 int start, 1117 int fd); 1118 /* 1119 * Reloads the aec.ini config file on server side. 1120 */ 1121 int cras_client_reload_aec_config(struct cras_client *client); 1122 1123 /* 1124 * Returns if AEC is supported. 1125 */ 1126 int cras_client_get_aec_supported(struct cras_client *client); 1127 1128 /* Set the context pointer for system state change callbacks. 1129 * Args: 1130 * client - The client from cras_client_create. 1131 * context - The context pointer passed to all callbacks. 1132 */ 1133 void cras_client_set_state_change_callback_context( 1134 struct cras_client *client, void *context); 1135 1136 /* Output volume change callback. 1137 * 1138 * Args: 1139 * context - Context pointer set with 1140 * cras_client_set_state_change_callback_context(). 1141 * volume - The system output volume, ranging from 0 to 100. 1142 */ 1143 typedef void (*cras_client_output_volume_changed_callback)( 1144 void* context, int32_t volume); 1145 1146 /* Output mute change callback. 1147 * 1148 * Args: 1149 * context - Context pointer set with 1150 * cras_client_set_state_change_callback_context(). 1151 * muted - Non-zero when the audio is muted, zero otherwise. 1152 * user_muted - Non-zero when the audio has been muted by the 1153 * user, zero otherwise. 1154 * mute_locked - Non-zero when the mute funcion is locked, 1155 * zero otherwise. 1156 */ 1157 typedef void (*cras_client_output_mute_changed_callback)( 1158 void* context, int muted, int user_muted, int mute_locked); 1159 1160 /* Capture gain change callback. 1161 * 1162 * Args: 1163 * context - Context pointer set with 1164 * cras_client_set_state_change_callback_context(). 1165 * gain - The system capture gain, in centi-decibels. 1166 */ 1167 typedef void (*cras_client_capture_gain_changed_callback)( 1168 void* context, int32_t gain); 1169 1170 /* Capture mute change callback. 1171 * 1172 * Args: 1173 * context - Context pointer set with 1174 * cras_client_set_state_change_callback_context(). 1175 * muted - Non-zero when the audio is muted, zero otherwise. 1176 * mute_locked - Non-zero when the mute funcion is locked, 1177 * zero otherwise. 1178 */ 1179 typedef void (*cras_client_capture_mute_changed_callback)( 1180 void* context, int muted, int mute_locked); 1181 1182 /* Nodes change callback. 1183 * 1184 * Args: 1185 * context - Context pointer set with 1186 * cras_client_set_state_change_callback_context(). 1187 */ 1188 typedef void (*cras_client_nodes_changed_callback)(void* context); 1189 1190 /* Active node change callback. 1191 * 1192 * Args: 1193 * context - Context pointer set with 1194 * cras_client_set_state_change_callback_context(). 1195 * direction - Indicates the direction of the selected node. 1196 * node_id - The ID of the selected node. Special device ID values 1197 * defined by CRAS_SPECIAL_DEVICE will be used when no other 1198 * device or node is selected or between selections. 1199 */ 1200 typedef void (*cras_client_active_node_changed_callback)( 1201 void* context, enum CRAS_STREAM_DIRECTION direction, 1202 cras_node_id_t node_id); 1203 1204 /* Output node volume change callback. 1205 * 1206 * Args: 1207 * context - Context pointer set with 1208 * cras_client_set_state_change_callback_context(). 1209 * node_id - The ID of the output node. 1210 * volume - The volume for this node with range 0 to 100. 1211 */ 1212 typedef void (*cras_client_output_node_volume_changed_callback)( 1213 void* context, cras_node_id_t node_id, int32_t volume); 1214 1215 /* Node left right swapped change callback. 1216 * 1217 * Args: 1218 * context - Context pointer set with 1219 * cras_client_set_state_change_callback_context(). 1220 * node_id - The ID of the node. 1221 * swapped - Non-zero if the node is left-right swapped, zero otherwise. 1222 */ 1223 typedef void (*cras_client_node_left_right_swapped_changed_callback)( 1224 void* context, cras_node_id_t node_id, int swapped); 1225 1226 /* Input node gain change callback. 1227 * Args: 1228 * context - Context pointer set with 1229 * cras_client_set_state_change_callback_context(). 1230 * node_id - The ID of the input node. 1231 * gain - The gain for this node in centi-decibels. 1232 */ 1233 typedef void (*cras_client_input_node_gain_changed_callback)( 1234 void* context, cras_node_id_t node_id, int32_t gain); 1235 1236 /* Number of active streams change callback. 1237 * 1238 * Args: 1239 * context - Context pointer set with 1240 * cras_client_set_state_change_callback_context(). 1241 * direction - Indicates the direction of the stream's node. 1242 * num_active_streams - The number of active streams. 1243 */ 1244 typedef void (*cras_client_num_active_streams_changed_callback)( 1245 void* context, enum CRAS_STREAM_DIRECTION direction, 1246 uint32_t num_active_streams); 1247 1248 /* Set system state information callbacks. 1249 * NOTE: These callbacks are executed from the client control thread. 1250 * Each state change callback is given the context pointer set with 1251 * cras_client_set_state_change_callback_context(). The context pointer is 1252 * NULL by default. 1253 * Args: 1254 * client - The client from cras_client_create. 1255 * cb - The callback, or NULL to disable the call-back. 1256 * Returns: 1257 * 0 for success or negative errno error code on error. 1258 */ 1259 int cras_client_set_output_volume_changed_callback( 1260 struct cras_client *client, 1261 cras_client_output_volume_changed_callback cb); 1262 int cras_client_set_output_mute_changed_callback( 1263 struct cras_client *client, 1264 cras_client_output_mute_changed_callback cb); 1265 int cras_client_set_capture_gain_changed_callback( 1266 struct cras_client *client, 1267 cras_client_capture_gain_changed_callback cb); 1268 int cras_client_set_capture_mute_changed_callback( 1269 struct cras_client *client, 1270 cras_client_capture_mute_changed_callback cb); 1271 int cras_client_set_nodes_changed_callback( 1272 struct cras_client *client, 1273 cras_client_nodes_changed_callback cb); 1274 int cras_client_set_active_node_changed_callback( 1275 struct cras_client *client, 1276 cras_client_active_node_changed_callback cb); 1277 int cras_client_set_output_node_volume_changed_callback( 1278 struct cras_client *client, 1279 cras_client_output_node_volume_changed_callback cb); 1280 int cras_client_set_node_left_right_swapped_changed_callback( 1281 struct cras_client *client, 1282 cras_client_node_left_right_swapped_changed_callback cb); 1283 int cras_client_set_input_node_gain_changed_callback( 1284 struct cras_client *client, 1285 cras_client_input_node_gain_changed_callback cb); 1286 int cras_client_set_num_active_streams_changed_callback( 1287 struct cras_client *client, 1288 cras_client_num_active_streams_changed_callback cb); 1289 1290 #ifdef __cplusplus 1291 } 1292 #endif 1293 1294 #endif /* CRAS_CLIENT_H_ */ 1295