Home | History | Annotate | Download | only in server
      1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  *
      5  * Handles various system-level settings.
      6  *
      7  * Volume:  The system volume is represented as a value from 0 to 100.  This
      8  * number will be interpreted by the output device and applied to the hardware.
      9  * The value will be mapped to dB by the active device as it will know its curve
     10  * the best.
     11  */
     12 
     13 #ifndef CRAS_SYSTEM_STATE_H_
     14 #define CRAS_SYSTEM_STATE_H_
     15 
     16 #include <stddef.h>
     17 
     18 #include "cras_types.h"
     19 
     20 #define CRAS_MAX_SYSTEM_VOLUME 100
     21 #define DEFAULT_CAPTURE_GAIN 2000 /* 20dB of gain. */
     22 /* Default to 1--dB of range for palyback and capture. */
     23 #define DEFAULT_MIN_VOLUME_DBFS -10000
     24 #define DEFAULT_MAX_VOLUME_DBFS 0
     25 #define DEFAULT_MIN_CAPTURE_GAIN -5000
     26 #define DEFAULT_MAX_CAPTURE_GAIN 5000
     27 
     28 struct cras_tm;
     29 
     30 /* Initialize system settings.
     31  *
     32  * Args:
     33  *    device_config_dir - Directory for device configs where volume curves live.
     34  */
     35 void cras_system_state_init(const char *device_config_dir);
     36 void cras_system_state_deinit();
     37 
     38 /* Sets the suffix string to control which UCM config fo load. */
     39 void cras_system_state_set_internal_ucm_suffix(const char *internal_ucm_suffix);
     40 
     41 /* Sets the system volume.  Will be applied by the active device. */
     42 void cras_system_set_volume(size_t volume);
     43 /* Gets the current system volume. */
     44 size_t cras_system_get_volume();
     45 
     46 /* Sets the system capture volume.  Will be applied by the active device. */
     47 void cras_system_set_capture_gain(long gain);
     48 /* Gets the current system capture volume. */
     49 long cras_system_get_capture_gain();
     50 
     51 /* Sets if the system is muted by the user. */
     52 void cras_system_set_user_mute(int muted);
     53 /* Sets if the system is muted for . */
     54 void cras_system_set_mute(int muted);
     55 /* Sets if the system muting is locked or not. */
     56 void cras_system_set_mute_locked(int locked);
     57 /* Gets the current mute state of the system. */
     58 int cras_system_get_mute();
     59 /* Gets the current user mute state. */
     60 int cras_system_get_user_mute();
     61 /* Gets the current system mute state. */
     62 int cras_system_get_system_mute();
     63 /* Gets if the system muting is locked or not. */
     64 int cras_system_get_mute_locked();
     65 
     66 /* Gets the suspend state of audio. */
     67 int cras_system_get_suspended();
     68 
     69 /* Sets the suspend state of audio.
     70  * Args:
     71  *    suspend - True for suspend, false for resume.
     72  */
     73 void cras_system_set_suspended(int suspend);
     74 
     75 /* Sets if the system capture path is muted or not. */
     76 void cras_system_set_capture_mute(int muted);
     77 /* Sets if the system capture path muting is locked or not. */
     78 void cras_system_set_capture_mute_locked(int locked);
     79 /* Gets the current mute state of the system capture path. */
     80 int cras_system_get_capture_mute();
     81 /* Gets if the system capture path muting is locked or not. */
     82 int cras_system_get_capture_mute_locked();
     83 
     84 /* Sets the value in dB of the MAX and MIN volume settings.  This will allow
     85  * clients to query what range of control is available.  Both arguments are
     86  * specified as dB * 100.
     87  * Args:
     88  *     min - dB value when volume = 1 (0 mutes).
     89  *     max - dB value when volume = CRAS_MAX_SYSTEM_VOLUME
     90  */
     91 void cras_system_set_volume_limits(long min, long max);
     92 /* Returns the dB value when volume = 1, in dB * 100. */
     93 long cras_system_get_min_volume();
     94 /* Returns the dB value when volume = CRAS_MAX_SYSTEM_VOLUME, in dB * 100. */
     95 long cras_system_get_max_volume();
     96 
     97 /* Sets the limits in dB * 100 of the MAX and MIN capture gain.  This will allow
     98  * clients to query what range of control is available.  Both arguments are
     99  * specified as dB * 100.
    100  * Args:
    101  *     min - minimum allowed capture gain.
    102  *     max - maximum allowed capture gaax.
    103  */
    104 void cras_system_set_capture_gain_limits(long min, long max);
    105 /* Returns the max value allowed for capture gain in dB * 100. */
    106 long cras_system_get_min_capture_gain();
    107 /* Returns the min value allowed for capture gain in dB * 100. */
    108 long cras_system_get_max_capture_gain();
    109 
    110 /* Adds a card at the given index to the system.  When a new card is found
    111  * (through a udev event notification) this will add the card to the system,
    112  * causing its devices to become available for playback/capture.
    113  * Args:
    114  *    alsa_card_info - Info about the alsa card (Index, type, etc.).
    115  * Returns:
    116  *    0 on success, negative error on failure (Can't create or card already
    117  *    exists).
    118  */
    119 int cras_system_add_alsa_card(struct cras_alsa_card_info *alsa_card_info);
    120 
    121 /* Removes a card.  When a device is removed this will do the cleanup.  Device
    122  * at index must have been added using cras_system_add_alsa_card().
    123  * Args:
    124  *    alsa_card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
    125  * Returns:
    126  *    0 on success, negative error on failure (Can't destroy or card doesn't
    127  *    exist).
    128  */
    129 int cras_system_remove_alsa_card(size_t alsa_card_index);
    130 
    131 /* Checks if an alsa card has been added to the system.
    132  * Args:
    133  *    alsa_card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
    134  * Returns:
    135  *    1 if the card has already been added, 0 if not.
    136  */
    137 int cras_system_alsa_card_exists(unsigned alsa_card_index);
    138 
    139 /* Poll interface provides a way of getting a callback when 'select'
    140  * returns for a given file descriptor.
    141  */
    142 
    143 /* Register the function to use to inform the select loop about new
    144  * file descriptors and callbacks.
    145  * Args:
    146  *    add - The function to call when a new fd is added.
    147  *    rm - The function to call when a new fd is removed.
    148  *    select_data - Additional value passed back to add and remove.
    149  * Returns:
    150  *    0 on success, or -EBUSY if there is already a registered handler.
    151  */
    152 int cras_system_set_select_handler(int (*add)(int fd,
    153 					      void (*callback)(void *data),
    154 					      void *callback_data,
    155 					      void *select_data),
    156 				  void (*rm)(int fd, void *select_data),
    157 				  void *select_data);
    158 
    159 /* Adds the fd and callback pair.  When select indicates that fd is readable,
    160  * the callback will be called.
    161  * Args:
    162  *    fd - The file descriptor to pass to select(2).
    163  *    callback - The callback to call when fd is ready.
    164  *    callback_data - Value passed back to the callback.
    165  * Returns:
    166  *    0 on success or a negative error code on failure.
    167  */
    168 int cras_system_add_select_fd(int fd,
    169 			      void (*callback)(void *data),
    170 			      void *callback_data);
    171 
    172 /* Removes the fd from the list of fds that are passed to select.
    173  * Args:
    174  *    fd - The file descriptor to remove from the list.
    175  */
    176 void cras_system_rm_select_fd(int fd);
    177 
    178 /* Signals that an audio input or output stream has been added to the system.
    179  * This allows the count of active streams can be used to notice when the audio
    180  * subsystem is idle.
    181  * Args:
    182  *   direction - Directions of audio streams.
    183  */
    184 void cras_system_state_stream_added(enum CRAS_STREAM_DIRECTION direction);
    185 
    186 /* Signals that an audio input or output stream has been removed from the
    187  * system.  This allows the count of active streams can be used to notice when
    188  * the audio subsystem is idle.
    189  * Args:
    190  *   direction - Directions of audio stream.
    191  */
    192 void cras_system_state_stream_removed(enum CRAS_STREAM_DIRECTION direction);
    193 
    194 /* Returns the number of active playback and capture streams. */
    195 unsigned cras_system_state_get_active_streams();
    196 
    197 /* Returns the number of active streams with given direction.
    198  * Args:
    199  *   direction - Directions of audio stream.
    200  */
    201 unsigned cras_system_state_get_active_streams_by_direction(
    202 	enum CRAS_STREAM_DIRECTION direction);
    203 
    204 /* Fills ts with the time the last stream was removed from the system, the time
    205  * the stream count went to zero.
    206  */
    207 void cras_system_state_get_last_stream_active_time(struct cras_timespec *ts);
    208 
    209 /* Returns output devices information.
    210  * Args:
    211  *    devs - returns the array of output devices information.
    212  * Returns:
    213  *    number of output devices.
    214  */
    215 int cras_system_state_get_output_devs(const struct cras_iodev_info **devs);
    216 
    217 /* Returns input devices information.
    218  * Args:
    219  *    devs - returns the array of input devices information.
    220  * Returns:
    221  *    number of input devices.
    222  */
    223 int cras_system_state_get_input_devs(const struct cras_iodev_info **devs);
    224 
    225 /* Returns output nodes information.
    226  * Args:
    227  *    nodes - returns the array of output nodes information.
    228  * Returns:
    229  *    number of output nodes.
    230  */
    231 int cras_system_state_get_output_nodes(const struct cras_ionode_info **nodes);
    232 
    233 /* Returns input nodes information.
    234  * Args:
    235  *    nodes - returns the array of input nodes information.
    236  * Returns:
    237  *    number of input nodes.
    238  */
    239 int cras_system_state_get_input_nodes(const struct cras_ionode_info **nodes);
    240 
    241 /* Returns a pointer to the current system state that is shared with clients.
    242  * This also 'locks' the structure by incrementing the update count to an odd
    243  * value.
    244  */
    245 struct cras_server_state *cras_system_state_update_begin();
    246 
    247 /* Unlocks the system state structure that was updated after calling
    248  * cras_system_state_update_begin by again incrementing the update count.
    249  */
    250 void cras_system_state_update_complete();
    251 
    252 /* Gets a pointer to the system state without locking it.  Only used for debug
    253  * log.  Don't add calls to this function. */
    254 struct cras_server_state *cras_system_state_get_no_lock();
    255 
    256 /* Returns the shm fd for the server_state structure. */
    257 key_t cras_sys_state_shm_fd();
    258 
    259 /* Returns the timer manager. */
    260 struct cras_tm *cras_system_state_get_tm();
    261 
    262 #endif /* CRAS_SYSTEM_STATE_H_ */
    263