Home | History | Annotate | Download | only in common
      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  * Types commonly used in the client and server are defined here.
      8  */
      9 #ifndef CRAS_TYPES_H_
     10 #define CRAS_TYPES_H_
     11 
     12 #include <stdint.h>
     13 #include <stdlib.h>
     14 
     15 #include "cras_audio_format.h"
     16 #include "cras_iodev_info.h"
     17 
     18 /* Architecture independent timespec */
     19 struct __attribute__ ((__packed__)) cras_timespec {
     20 	int64_t tv_sec;
     21 	int64_t tv_nsec;
     22 };
     23 
     24 /* Some special device index values. */
     25 enum CRAS_SPECIAL_DEVICE {
     26 	NO_DEVICE,
     27 	SILENT_RECORD_DEVICE,
     28 	SILENT_PLAYBACK_DEVICE,
     29 	MAX_SPECIAL_DEVICE_IDX
     30 };
     31 
     32 /*
     33  * Types of test iodevs supported.
     34  */
     35 enum TEST_IODEV_TYPE {
     36 	TEST_IODEV_HOTWORD,
     37 };
     38 
     39 
     40 /* Commands for test iodevs. */
     41 enum CRAS_TEST_IODEV_CMD {
     42 	TEST_IODEV_CMD_HOTWORD_TRIGGER,
     43 };
     44 
     45 /* Directions of audio streams.
     46  * Input, Output, or loopback.
     47  *
     48  * Note that we use enum CRAS_STREAM_DIRECTION to access the elements in
     49  * num_active_streams in cras_server_state. For example,
     50  * num_active_streams[CRAS_STREAM_OUTPUT] is the number of active
     51  * streams with direction CRAS_STREAM_OUTPUT.
     52  */
     53 enum CRAS_STREAM_DIRECTION {
     54 	CRAS_STREAM_OUTPUT,
     55 	CRAS_STREAM_INPUT,
     56 	CRAS_STREAM_UNDEFINED,
     57 	CRAS_STREAM_POST_MIX_PRE_DSP,
     58 	CRAS_NUM_DIRECTIONS
     59 };
     60 
     61 /*
     62  * Flags for stream types.
     63  *  BULK_AUDIO_OK - This stream is OK with receiving up to a full shm of samples
     64  *      in a single callback.
     65  *  USE_DEV_TIMING - Don't wake up based on stream timing.  Only wake when the
     66  *      device is ready. Input streams only.
     67  *  HOTWORD_STREAM - This stream is used only to listen for hotwords such as "OK
     68  *      Google".  Hardware will wake the device when this phrase is heard.
     69  */
     70 enum CRAS_INPUT_STREAM_FLAG {
     71 	BULK_AUDIO_OK = 0x01,
     72 	USE_DEV_TIMING = 0x02,
     73 	HOTWORD_STREAM = BULK_AUDIO_OK | USE_DEV_TIMING,
     74 };
     75 
     76 /*
     77  * Types of Loopback stream.
     78  */
     79 enum CRAS_LOOPBACK_TYPE {
     80 	LOOPBACK_POST_MIX_PRE_DSP,
     81 	LOOPBACK_POST_DSP,
     82 	LOOPBACK_NUM_TYPES,
     83 };
     84 
     85 static inline int cras_stream_uses_output_hw(enum CRAS_STREAM_DIRECTION dir)
     86 {
     87 	return dir == CRAS_STREAM_OUTPUT;
     88 }
     89 
     90 static inline int cras_stream_uses_input_hw(enum CRAS_STREAM_DIRECTION dir)
     91 {
     92 	return dir == CRAS_STREAM_INPUT;
     93 }
     94 
     95 static inline int cras_stream_has_input(enum CRAS_STREAM_DIRECTION dir)
     96 {
     97 	return dir != CRAS_STREAM_OUTPUT;
     98 }
     99 
    100 static inline int cras_stream_is_loopback(enum CRAS_STREAM_DIRECTION dir)
    101 {
    102 	return dir == CRAS_STREAM_POST_MIX_PRE_DSP;
    103 }
    104 
    105 /* Types of audio streams. */
    106 enum CRAS_STREAM_TYPE {
    107 	CRAS_STREAM_TYPE_DEFAULT,
    108 	CRAS_STREAM_TYPE_MULTIMEDIA,
    109 	CRAS_STREAM_TYPE_VOICE_COMMUNICATION,
    110 	CRAS_STREAM_TYPE_SPEECH_RECOGNITION,
    111 	CRAS_STREAM_TYPE_PRO_AUDIO,
    112 	CRAS_STREAM_NUM_TYPES,
    113 };
    114 
    115 #define ENUM_STR(x) case x: return #x;
    116 
    117 static inline const char *cras_stream_type_str(
    118 		enum CRAS_STREAM_TYPE stream_type)
    119 {
    120 	switch(stream_type) {
    121 	ENUM_STR(CRAS_STREAM_TYPE_DEFAULT)
    122 	ENUM_STR(CRAS_STREAM_TYPE_MULTIMEDIA)
    123 	ENUM_STR(CRAS_STREAM_TYPE_VOICE_COMMUNICATION)
    124 	ENUM_STR(CRAS_STREAM_TYPE_SPEECH_RECOGNITION)
    125 	ENUM_STR(CRAS_STREAM_TYPE_PRO_AUDIO)
    126 	default:
    127 		return "INVALID_STREAM_TYPE";
    128 	}
    129 }
    130 
    131 /* Information about a client attached to the server. */
    132 struct __attribute__ ((__packed__)) cras_attached_client_info {
    133 	uint32_t id;
    134 	int32_t pid;
    135 	uint32_t uid;
    136 	uint32_t gid;
    137 };
    138 
    139 /* Each ionode has a unique id. The top 32 bits are the device index, lower 32
    140  * are the node index. */
    141 typedef uint64_t cras_node_id_t;
    142 
    143 static inline cras_node_id_t cras_make_node_id(uint32_t dev_index,
    144 					       uint32_t node_index)
    145 {
    146 	cras_node_id_t id = dev_index;
    147 	return (id << 32) | node_index;
    148 }
    149 
    150 static inline uint32_t dev_index_of(cras_node_id_t id)
    151 {
    152 	return (uint32_t) (id >> 32);
    153 }
    154 
    155 static inline uint32_t node_index_of(cras_node_id_t id)
    156 {
    157 	return (uint32_t) id;
    158 }
    159 
    160 #define CRAS_MAX_IODEVS 20
    161 #define CRAS_MAX_IONODES 20
    162 #define CRAS_MAX_ATTACHED_CLIENTS 20
    163 #define CRAS_HOTWORD_STRING_SIZE 256
    164 #define MAX_DEBUG_DEVS 4
    165 #define MAX_DEBUG_STREAMS 8
    166 #define AUDIO_THREAD_EVENT_LOG_SIZE (1024*6)
    167 
    168 /* There are 8 bits of space for events. */
    169 enum AUDIO_THREAD_LOG_EVENTS {
    170 	AUDIO_THREAD_WAKE,
    171 	AUDIO_THREAD_SLEEP,
    172 	AUDIO_THREAD_READ_AUDIO,
    173 	AUDIO_THREAD_READ_AUDIO_TSTAMP,
    174 	AUDIO_THREAD_READ_AUDIO_DONE,
    175 	AUDIO_THREAD_READ_OVERRUN,
    176 	AUDIO_THREAD_FILL_AUDIO,
    177 	AUDIO_THREAD_FILL_AUDIO_TSTAMP,
    178 	AUDIO_THREAD_FILL_AUDIO_DONE,
    179 	AUDIO_THREAD_WRITE_STREAMS_WAIT,
    180 	AUDIO_THREAD_WRITE_STREAMS_WAIT_TO,
    181 	AUDIO_THREAD_WRITE_STREAMS_MIX,
    182 	AUDIO_THREAD_WRITE_STREAMS_MIXED,
    183 	AUDIO_THREAD_WRITE_STREAMS_STREAM,
    184 	AUDIO_THREAD_FETCH_STREAM,
    185 	AUDIO_THREAD_STREAM_ADDED,
    186 	AUDIO_THREAD_STREAM_REMOVED,
    187 	AUDIO_THREAD_A2DP_ENCODE,
    188 	AUDIO_THREAD_A2DP_WRITE,
    189 	AUDIO_THREAD_DEV_STREAM_MIX,
    190 	AUDIO_THREAD_CAPTURE_POST,
    191 	AUDIO_THREAD_CAPTURE_WRITE,
    192 	AUDIO_THREAD_CONV_COPY,
    193 	AUDIO_THREAD_STREAM_SLEEP_TIME,
    194 	AUDIO_THREAD_STREAM_SLEEP_ADJUST,
    195 	AUDIO_THREAD_STREAM_SKIP_CB,
    196 	AUDIO_THREAD_DEV_SLEEP_TIME,
    197 	AUDIO_THREAD_SET_DEV_WAKE,
    198 	AUDIO_THREAD_DEV_ADDED,
    199 	AUDIO_THREAD_DEV_REMOVED,
    200 	AUDIO_THREAD_IODEV_CB,
    201 	AUDIO_THREAD_PB_MSG,
    202 	AUDIO_THREAD_ODEV_NO_STREAMS,
    203 	AUDIO_THREAD_ODEV_START,
    204 	AUDIO_THREAD_ODEV_LEAVE_NO_STREAMS,
    205 	AUDIO_THREAD_ODEV_DEFAULT_NO_STREAMS,
    206 	AUDIO_THREAD_FILL_ODEV_ZEROS,
    207 	AUDIO_THREAD_SEVERE_UNDERRUN,
    208 };
    209 
    210 struct __attribute__ ((__packed__)) audio_thread_event {
    211 	uint32_t tag_sec;
    212 	uint32_t nsec;
    213 	uint32_t data1;
    214 	uint32_t data2;
    215 	uint32_t data3;
    216 };
    217 
    218 /* Ring buffer of log events from the audio thread. */
    219 struct __attribute__ ((__packed__)) audio_thread_event_log {
    220 	uint32_t write_pos;
    221 	uint32_t len;
    222 	struct audio_thread_event log[AUDIO_THREAD_EVENT_LOG_SIZE];
    223 };
    224 
    225 struct __attribute__ ((__packed__)) audio_dev_debug_info {
    226 	char dev_name[CRAS_NODE_NAME_BUFFER_SIZE];
    227 	uint32_t buffer_size;
    228 	uint32_t min_buffer_level;
    229 	uint32_t min_cb_level;
    230 	uint32_t max_cb_level;
    231 	uint32_t frame_rate;
    232 	uint32_t num_channels;
    233 	double est_rate_ratio;
    234 	uint8_t direction;
    235 	uint32_t num_underruns;
    236 	uint32_t num_severe_underruns;
    237 };
    238 
    239 struct __attribute__ ((__packed__)) audio_stream_debug_info {
    240 	uint64_t stream_id;
    241 	uint32_t dev_idx;
    242 	uint32_t direction;
    243 	uint32_t stream_type;
    244 	uint32_t buffer_frames;
    245 	uint32_t cb_threshold;
    246 	uint32_t flags;
    247 	uint32_t frame_rate;
    248 	uint32_t num_channels;
    249 	uint32_t longest_fetch_sec;
    250 	uint32_t longest_fetch_nsec;
    251 	uint32_t num_overruns;
    252 	int8_t channel_layout[CRAS_CH_MAX];
    253 };
    254 
    255 /* Debug info shared from server to client. */
    256 struct __attribute__ ((__packed__)) audio_debug_info {
    257 	uint32_t num_streams;
    258 	uint32_t num_devs;
    259 	struct audio_dev_debug_info devs[MAX_DEBUG_DEVS];
    260 	struct audio_stream_debug_info streams[MAX_DEBUG_STREAMS];
    261 	struct audio_thread_event_log log;
    262 };
    263 
    264 
    265 /* The server state that is shared with clients.
    266  *    state_version - Version of this structure.
    267  *    volume - index from 0-100.
    268  *    min_volume_dBFS - volume in dB * 100 when volume = 1.
    269  *    max_volume_dBFS - volume in dB * 100 when volume = max.
    270  *    mute - 0 = unmuted, 1 = muted by system (device switch, suspend, etc).
    271  *    user_mute - 0 = unmuted, 1 = muted by user.
    272  *    mute_locked - 0 = unlocked, 1 = locked.
    273  *    suspended - 1 = suspended, 0 = resumed.
    274  *    capture_gain - Capture gain in dBFS * 100.
    275  *    capture_gain_target - Target capture gain in dBFS * 100. The actual
    276  *                          capture gain will be subjected to current
    277  *                          supported range. When active device/node changes,
    278  *                          supported range changes accordingly. System state
    279  *                          should try to re-apply target gain subjected to new
    280  *                          range.
    281  *    capture_mute - 0 = unmuted, 1 = muted.
    282  *    capture_mute_locked - 0 = unlocked, 1 = locked.
    283  *    min_capture_gain - Min allowed capture gain in dBFS * 100.
    284  *    max_capture_gain - Max allowed capture gain in dBFS * 100.
    285  *    num_streams_attached - Total number of streams since server started.
    286  *    num_output_devs - Number of available output devices.
    287  *    num_input_devs - Number of available input devices.
    288  *    output_devs - Output audio devices currently attached.
    289  *    input_devs - Input audio devices currently attached.
    290  *    num_output_nodes - Number of available output nodes.
    291  *    num_input_nodes - Number of available input nodes.
    292  *    output_nodes - Output nodes currently attached.
    293  *    input_nodes - Input nodes currently attached.
    294  *    num_attached_clients - Number of clients attached to server.
    295  *    client_info - List of first 20 attached clients.
    296  *    update_count - Incremented twice each time the struct is updated.  Odd
    297  *        during updates.
    298  *    num_active_streams - An array containing numbers or active
    299  *        streams of different directions.
    300  *    last_active_stream_time - Time the last stream was removed.  Can be used
    301  *        to determine how long audio has been idle.
    302  *    audio_debug_info - Debug data filled in when a client requests it. This
    303  *        isn't protected against concurrent updating, only one client should
    304  *        use it.
    305  */
    306 #define CRAS_SERVER_STATE_VERSION 2
    307 struct __attribute__ ((packed, aligned(4))) cras_server_state {
    308 	uint32_t state_version;
    309 	uint32_t volume;
    310 	int32_t min_volume_dBFS;
    311 	int32_t max_volume_dBFS;
    312 	int32_t mute;
    313 	int32_t user_mute;
    314 	int32_t mute_locked;
    315 	int32_t suspended;
    316 	int32_t capture_gain;
    317 	int32_t capture_gain_target;
    318 	int32_t capture_mute;
    319 	int32_t capture_mute_locked;
    320 	int32_t min_capture_gain;
    321 	int32_t max_capture_gain;
    322 	uint32_t num_streams_attached;
    323 	uint32_t num_output_devs;
    324 	uint32_t num_input_devs;
    325 	struct cras_iodev_info output_devs[CRAS_MAX_IODEVS];
    326 	struct cras_iodev_info input_devs[CRAS_MAX_IODEVS];
    327 	uint32_t num_output_nodes;
    328 	uint32_t num_input_nodes;
    329 	struct cras_ionode_info output_nodes[CRAS_MAX_IONODES];
    330 	struct cras_ionode_info input_nodes[CRAS_MAX_IONODES];
    331 	uint32_t num_attached_clients;
    332 	struct cras_attached_client_info client_info[CRAS_MAX_ATTACHED_CLIENTS];
    333 	uint32_t update_count;
    334 	uint32_t num_active_streams[CRAS_NUM_DIRECTIONS];
    335 	struct cras_timespec last_active_stream_time;
    336 	struct audio_debug_info audio_debug_info;
    337 };
    338 
    339 /* Actions for card add/remove/change. */
    340 enum cras_notify_device_action { /* Must match gavd action definitions.  */
    341 	CRAS_DEVICE_ACTION_ADD    = 0,
    342 	CRAS_DEVICE_ACTION_REMOVE = 1,
    343 	CRAS_DEVICE_ACTION_CHANGE = 2,
    344 };
    345 
    346 /* Information about an ALSA card to be added to the system.
    347  *    card_type - Either internal card or a USB sound card.
    348  *    card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
    349  *    priority - Base priority to give devices found on this card. Zero is the
    350  *      lowest priority.  Non-primary devices on the card will be given a
    351  *      lowered priority.
    352  *    usb_vendor_id - vendor ID if the device is on the USB bus.
    353  *    usb_product_id - product ID if the device is on the USB bus.
    354  *    usb_serial_number - serial number if the device is on the USB bus.
    355  *    usb_desc_checksum - the checksum of the USB descriptors if the device
    356  *      is on the USB bus.
    357  */
    358 enum CRAS_ALSA_CARD_TYPE {
    359 	ALSA_CARD_TYPE_INTERNAL,
    360 	ALSA_CARD_TYPE_USB,
    361 };
    362 #define USB_SERIAL_NUMBER_BUFFER_SIZE 64
    363 struct __attribute__ ((__packed__)) cras_alsa_card_info {
    364 	enum CRAS_ALSA_CARD_TYPE card_type;
    365 	uint32_t card_index;
    366 	uint32_t usb_vendor_id;
    367 	uint32_t usb_product_id;
    368 	char usb_serial_number[USB_SERIAL_NUMBER_BUFFER_SIZE];
    369 	uint32_t usb_desc_checksum;
    370 };
    371 
    372 /* Unique identifier for each active stream.
    373  * The top 16 bits are the client number, lower 16 are the stream number.
    374  */
    375 typedef uint32_t cras_stream_id_t;
    376 /* Generates a stream id for client stream. */
    377 static inline cras_stream_id_t cras_get_stream_id(uint16_t client_id,
    378 						  uint16_t stream_id)
    379 {
    380 	return (cras_stream_id_t)(((client_id & 0x0000ffff) << 16) |
    381 				  (stream_id & 0x0000ffff));
    382 }
    383 
    384 enum CRAS_NODE_TYPE {
    385 	/* These value can be used for output nodes. */
    386 	CRAS_NODE_TYPE_INTERNAL_SPEAKER,
    387 	CRAS_NODE_TYPE_HEADPHONE,
    388 	CRAS_NODE_TYPE_HDMI,
    389 	CRAS_NODE_TYPE_HAPTIC,
    390 	CRAS_NODE_TYPE_LINEOUT,
    391 	/* These value can be used for input nodes. */
    392 	CRAS_NODE_TYPE_MIC,
    393 	CRAS_NODE_TYPE_HOTWORD,
    394 	CRAS_NODE_TYPE_POST_MIX_PRE_DSP,
    395 	CRAS_NODE_TYPE_POST_DSP,
    396 	/* These value can be used for both output and input nodes. */
    397 	CRAS_NODE_TYPE_USB,
    398 	CRAS_NODE_TYPE_BLUETOOTH,
    399 	CRAS_NODE_TYPE_UNKNOWN,
    400 };
    401 
    402 /* Position values to described where a node locates on the system.
    403  * NODE_POSITION_EXTERNAL - The node works only when peripheral
    404  *     is plugged.
    405  * NODE_POSITION_INTERNAL - The node lives on the system and doesn't
    406  *     have specific direction.
    407  * NODE_POSITION_FRONT - The node locates on the side of system that
    408  *     faces user.
    409  * NODE_POSITION_REAR - The node locates on the opposite side of
    410  *     the system that faces user.
    411  * NODE_POSITION_KEYBOARD - The node locates under the keyboard.
    412  */
    413 enum CRAS_NODE_POSITION {
    414 	NODE_POSITION_EXTERNAL,
    415 	NODE_POSITION_INTERNAL,
    416 	NODE_POSITION_FRONT,
    417 	NODE_POSITION_REAR,
    418 	NODE_POSITION_KEYBOARD,
    419 };
    420 
    421 #endif /* CRAS_TYPES_H_ */
    422