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