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 <stdbool.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 
16 #include "cras_audio_format.h"
17 #include "cras_iodev_info.h"
18 #include "packet_status_logger.h"
19 
20 /* Architecture independent timespec */
21 struct __attribute__((__packed__)) cras_timespec {
22 	int64_t tv_sec;
23 	int64_t tv_nsec;
24 };
25 
26 /* Some special device index values. */
27 enum CRAS_SPECIAL_DEVICE {
28 	NO_DEVICE,
29 	SILENT_RECORD_DEVICE,
30 	SILENT_PLAYBACK_DEVICE,
31 	SILENT_HOTWORD_DEVICE,
32 	MAX_SPECIAL_DEVICE_IDX
33 };
34 
35 /*
36  * Types of test iodevs supported.
37  */
38 enum TEST_IODEV_TYPE {
39 	TEST_IODEV_HOTWORD,
40 };
41 
42 /* Commands for test iodevs. */
43 enum CRAS_TEST_IODEV_CMD {
44 	TEST_IODEV_CMD_HOTWORD_TRIGGER,
45 };
46 
47 /* CRAS client connection types. */
48 enum CRAS_CONNECTION_TYPE {
49 	CRAS_CONTROL, // For legacy client.
50 	CRAS_PLAYBACK, // For playback client.
51 	CRAS_CAPTURE, // For capture client.
52 	CRAS_VMS_LEGACY, // For legacy client in vms.
53 	CRAS_VMS_UNIFIED, // For unified client in vms.
54 	CRAS_PLUGIN_PLAYBACK, // For playback client in vms/plugin.
55 	CRAS_PLUGIN_UNIFIED, // For unified client in vms/plugin.
56 	CRAS_NUM_CONN_TYPE,
57 };
58 
59 static inline bool
cras_validate_connection_type(enum CRAS_CONNECTION_TYPE conn_type)60 cras_validate_connection_type(enum CRAS_CONNECTION_TYPE conn_type)
61 {
62 	return 0 <= conn_type && conn_type < CRAS_NUM_CONN_TYPE;
63 }
64 
65 /* Directions of audio streams.
66  * Input, Output, or loopback.
67  *
68  * Note that we use enum CRAS_STREAM_DIRECTION to access the elements in
69  * num_active_streams in cras_server_state. For example,
70  * num_active_streams[CRAS_STREAM_OUTPUT] is the number of active
71  * streams with direction CRAS_STREAM_OUTPUT.
72  */
73 enum CRAS_STREAM_DIRECTION {
74 	CRAS_STREAM_OUTPUT,
75 	CRAS_STREAM_INPUT,
76 	CRAS_STREAM_UNDEFINED,
77 	CRAS_STREAM_POST_MIX_PRE_DSP,
78 	CRAS_NUM_DIRECTIONS
79 };
80 
81 /* Bitmask for supporting all CRAS_STREAM_DIRECTION. */
82 #define CRAS_STREAM_ALL_DIRECTION ((1 << CRAS_NUM_DIRECTIONS) - 1)
83 
84 /* Converts CRAS_STREAM_DIRECTION to bitmask.
85  * Args:
86  *   dir - An enum CRAS_STREAM_DIRECTION.
87  *
88  * Returns:
89  *   bitmask for the given direction on success, negative on failure.
90  */
91 static inline int
cras_stream_direction_mask(const enum CRAS_STREAM_DIRECTION dir)92 cras_stream_direction_mask(const enum CRAS_STREAM_DIRECTION dir)
93 {
94 	if (0 <= dir && dir < CRAS_NUM_DIRECTIONS)
95 		return (1 << dir);
96 	return -EINVAL;
97 }
98 
99 /*
100  * Flags for stream types.
101  *  BULK_AUDIO_OK - This stream is OK with receiving up to a full shm of samples
102  *      in a single callback.
103  *  USE_DEV_TIMING - Don't wake up based on stream timing.  Only wake when the
104  *      device is ready. Input streams only.
105  *  HOTWORD_STREAM - This stream is used only to listen for hotwords such as "OK
106  *      Google".  Hardware will wake the device when this phrase is heard.
107  *  TRIGGER_ONLY - This stream only wants to receive when the data is available
108  *      and does not want to receive data. Used with HOTWORD_STREAM.
109  *  SERVER_ONLY - This stream doesn't associate to a client. It's used mainly
110  *      for audio data to flow from hardware through iodev's dsp pipeline.
111  */
112 enum CRAS_INPUT_STREAM_FLAG {
113 	BULK_AUDIO_OK = 0x01,
114 	USE_DEV_TIMING = 0x02,
115 	HOTWORD_STREAM = BULK_AUDIO_OK | USE_DEV_TIMING,
116 	TRIGGER_ONLY = 0x04,
117 	SERVER_ONLY = 0x08,
118 };
119 
120 /*
121  * Types of Loopback stream.
122  */
123 enum CRAS_LOOPBACK_TYPE {
124 	LOOPBACK_POST_MIX_PRE_DSP,
125 	LOOPBACK_POST_DSP,
126 	LOOPBACK_NUM_TYPES,
127 };
128 
cras_stream_uses_output_hw(enum CRAS_STREAM_DIRECTION dir)129 static inline int cras_stream_uses_output_hw(enum CRAS_STREAM_DIRECTION dir)
130 {
131 	return dir == CRAS_STREAM_OUTPUT;
132 }
133 
cras_stream_uses_input_hw(enum CRAS_STREAM_DIRECTION dir)134 static inline int cras_stream_uses_input_hw(enum CRAS_STREAM_DIRECTION dir)
135 {
136 	return dir == CRAS_STREAM_INPUT;
137 }
138 
cras_stream_has_input(enum CRAS_STREAM_DIRECTION dir)139 static inline int cras_stream_has_input(enum CRAS_STREAM_DIRECTION dir)
140 {
141 	return dir != CRAS_STREAM_OUTPUT;
142 }
143 
cras_stream_is_loopback(enum CRAS_STREAM_DIRECTION dir)144 static inline int cras_stream_is_loopback(enum CRAS_STREAM_DIRECTION dir)
145 {
146 	return dir == CRAS_STREAM_POST_MIX_PRE_DSP;
147 }
148 
149 /* Types of audio streams. */
150 enum CRAS_STREAM_TYPE {
151 	CRAS_STREAM_TYPE_DEFAULT,
152 	CRAS_STREAM_TYPE_MULTIMEDIA,
153 	CRAS_STREAM_TYPE_VOICE_COMMUNICATION,
154 	CRAS_STREAM_TYPE_SPEECH_RECOGNITION,
155 	CRAS_STREAM_TYPE_PRO_AUDIO,
156 	CRAS_STREAM_TYPE_ACCESSIBILITY,
157 	CRAS_STREAM_NUM_TYPES,
158 };
159 
160 /* Types of audio clients. */
161 enum CRAS_CLIENT_TYPE {
162 	CRAS_CLIENT_TYPE_UNKNOWN, /* Unknown client */
163 	CRAS_CLIENT_TYPE_LEGACY, /* A client with old craslib (CRAS_PROTO_VER = 3) */
164 	CRAS_CLIENT_TYPE_TEST, /* cras_test_client */
165 	CRAS_CLIENT_TYPE_PCM, /* A client using CRAS via pcm, like aplay */
166 	CRAS_CLIENT_TYPE_CHROME, /* Chrome, UI */
167 	CRAS_CLIENT_TYPE_ARC, /* ARC++ */
168 	CRAS_CLIENT_TYPE_CROSVM, /* CROSVM */
169 	CRAS_CLIENT_TYPE_SERVER_STREAM, /* Server stream */
170 	CRAS_CLIENT_TYPE_LACROS, /* LaCrOS */
171 	CRAS_CLIENT_TYPE_PLUGIN, /* PluginVM */
172 	CRAS_CLIENT_TYPE_ARCVM, /* ARCVM */
173 	CRAS_NUM_CLIENT_TYPE, /* numbers of CRAS_CLIENT_TYPE */
174 };
175 
cras_validate_client_type(enum CRAS_CLIENT_TYPE client_type)176 static inline bool cras_validate_client_type(enum CRAS_CLIENT_TYPE client_type)
177 {
178 	return 0 <= client_type && client_type < CRAS_NUM_CLIENT_TYPE;
179 }
180 
181 #define ENUM_STR(x)                                                            \
182 	case x:                                                                \
183 		return #x;
184 
185 static inline const char *
cras_stream_type_str(enum CRAS_STREAM_TYPE stream_type)186 cras_stream_type_str(enum CRAS_STREAM_TYPE stream_type)
187 {
188 	// clang-format off
189 	switch (stream_type) {
190 	ENUM_STR(CRAS_STREAM_TYPE_DEFAULT)
191 	ENUM_STR(CRAS_STREAM_TYPE_MULTIMEDIA)
192 	ENUM_STR(CRAS_STREAM_TYPE_VOICE_COMMUNICATION)
193 	ENUM_STR(CRAS_STREAM_TYPE_SPEECH_RECOGNITION)
194 	ENUM_STR(CRAS_STREAM_TYPE_PRO_AUDIO)
195 	ENUM_STR(CRAS_STREAM_TYPE_ACCESSIBILITY)
196 	default:
197 		return "INVALID_STREAM_TYPE";
198 	}
199 	// clang-format on
200 }
201 
202 static inline const char *
cras_client_type_str(enum CRAS_CLIENT_TYPE client_type)203 cras_client_type_str(enum CRAS_CLIENT_TYPE client_type)
204 {
205 	// clang-format off
206 	switch (client_type) {
207 	ENUM_STR(CRAS_CLIENT_TYPE_UNKNOWN)
208 	ENUM_STR(CRAS_CLIENT_TYPE_LEGACY)
209 	ENUM_STR(CRAS_CLIENT_TYPE_TEST)
210 	ENUM_STR(CRAS_CLIENT_TYPE_PCM)
211 	ENUM_STR(CRAS_CLIENT_TYPE_CHROME)
212 	ENUM_STR(CRAS_CLIENT_TYPE_ARC)
213 	ENUM_STR(CRAS_CLIENT_TYPE_CROSVM)
214 	ENUM_STR(CRAS_CLIENT_TYPE_SERVER_STREAM)
215 	ENUM_STR(CRAS_CLIENT_TYPE_LACROS)
216 	ENUM_STR(CRAS_CLIENT_TYPE_PLUGIN)
217 	ENUM_STR(CRAS_CLIENT_TYPE_ARCVM)
218 	default:
219 		return "INVALID_CLIENT_TYPE";
220 	}
221 	// clang-format on
222 }
223 
224 /* Effects that can be enabled for a CRAS stream. */
225 enum CRAS_STREAM_EFFECT {
226 	APM_ECHO_CANCELLATION = (1 << 0),
227 	APM_NOISE_SUPRESSION = (1 << 1),
228 	APM_GAIN_CONTROL = (1 << 2),
229 	APM_VOICE_DETECTION = (1 << 3),
230 };
231 
232 /* Information about a client attached to the server. */
233 struct __attribute__((__packed__)) cras_attached_client_info {
234 	uint32_t id;
235 	int32_t pid;
236 	uint32_t uid;
237 	uint32_t gid;
238 };
239 
240 /* Each ionode has a unique id. The top 32 bits are the device index, lower 32
241  * are the node index. */
242 typedef uint64_t cras_node_id_t;
243 
cras_make_node_id(uint32_t dev_index,uint32_t node_index)244 static inline cras_node_id_t cras_make_node_id(uint32_t dev_index,
245 					       uint32_t node_index)
246 {
247 	cras_node_id_t id = dev_index;
248 	return (id << 32) | node_index;
249 }
250 
dev_index_of(cras_node_id_t id)251 static inline uint32_t dev_index_of(cras_node_id_t id)
252 {
253 	return (uint32_t)(id >> 32);
254 }
255 
node_index_of(cras_node_id_t id)256 static inline uint32_t node_index_of(cras_node_id_t id)
257 {
258 	return (uint32_t)id;
259 }
260 
261 #define CRAS_MAX_IODEVS 20
262 #define CRAS_MAX_IONODES 20
263 #define CRAS_MAX_ATTACHED_CLIENTS 20
264 #define CRAS_MAX_AUDIO_THREAD_SNAPSHOTS 10
265 #define CRAS_MAX_HOTWORD_MODEL_NAME_SIZE 12
266 #define MAX_DEBUG_DEVS 4
267 #define MAX_DEBUG_STREAMS 8
268 #define AUDIO_THREAD_EVENT_LOG_SIZE (1024 * 6)
269 #define CRAS_BT_EVENT_LOG_SIZE 1024
270 #define MAIN_THREAD_EVENT_LOG_SIZE 1024
271 
272 /* There are 8 bits of space for events. */
273 enum AUDIO_THREAD_LOG_EVENTS {
274 	AUDIO_THREAD_WAKE,
275 	AUDIO_THREAD_SLEEP,
276 	AUDIO_THREAD_READ_AUDIO,
277 	AUDIO_THREAD_READ_AUDIO_TSTAMP,
278 	AUDIO_THREAD_READ_AUDIO_DONE,
279 	AUDIO_THREAD_READ_OVERRUN,
280 	AUDIO_THREAD_FILL_AUDIO,
281 	AUDIO_THREAD_FILL_AUDIO_TSTAMP,
282 	AUDIO_THREAD_FILL_AUDIO_DONE,
283 	AUDIO_THREAD_WRITE_STREAMS_WAIT,
284 	AUDIO_THREAD_WRITE_STREAMS_WAIT_TO,
285 	AUDIO_THREAD_WRITE_STREAMS_MIX,
286 	AUDIO_THREAD_WRITE_STREAMS_MIXED,
287 	AUDIO_THREAD_WRITE_STREAMS_STREAM,
288 	AUDIO_THREAD_FETCH_STREAM,
289 	AUDIO_THREAD_STREAM_ADDED,
290 	AUDIO_THREAD_STREAM_REMOVED,
291 	AUDIO_THREAD_A2DP_FLUSH,
292 	AUDIO_THREAD_A2DP_THROTTLE_TIME,
293 	AUDIO_THREAD_A2DP_WRITE,
294 	AUDIO_THREAD_DEV_STREAM_MIX,
295 	AUDIO_THREAD_CAPTURE_POST,
296 	AUDIO_THREAD_CAPTURE_WRITE,
297 	AUDIO_THREAD_CONV_COPY,
298 	AUDIO_THREAD_STREAM_FETCH_PENDING,
299 	AUDIO_THREAD_STREAM_RESCHEDULE,
300 	AUDIO_THREAD_STREAM_SLEEP_TIME,
301 	AUDIO_THREAD_STREAM_SLEEP_ADJUST,
302 	AUDIO_THREAD_STREAM_SKIP_CB,
303 	AUDIO_THREAD_DEV_SLEEP_TIME,
304 	AUDIO_THREAD_SET_DEV_WAKE,
305 	AUDIO_THREAD_DEV_ADDED,
306 	AUDIO_THREAD_DEV_REMOVED,
307 	AUDIO_THREAD_IODEV_CB,
308 	AUDIO_THREAD_PB_MSG,
309 	AUDIO_THREAD_ODEV_NO_STREAMS,
310 	AUDIO_THREAD_ODEV_START,
311 	AUDIO_THREAD_ODEV_LEAVE_NO_STREAMS,
312 	AUDIO_THREAD_ODEV_DEFAULT_NO_STREAMS,
313 	AUDIO_THREAD_FILL_ODEV_ZEROS,
314 	AUDIO_THREAD_UNDERRUN,
315 	AUDIO_THREAD_SEVERE_UNDERRUN,
316 	AUDIO_THREAD_CAPTURE_DROP_TIME,
317 	AUDIO_THREAD_DEV_DROP_FRAMES,
318 	AUDIO_THREAD_LOOPBACK_PUT,
319 	AUDIO_THREAD_LOOPBACK_GET,
320 	AUDIO_THREAD_LOOPBACK_SAMPLE_HOOK,
321 	AUDIO_THREAD_DEV_OVERRUN,
322 };
323 
324 /* Important events in main thread.
325  * MAIN_THREAD_DEV_CLOSE - When an iodev closes at stream removal.
326  * MAIN_THREAD_DEV_DISABLE - When an iodev is removed from active dev list.
327  * MAIN_THREAD_DEV_INIT - When an iodev opens when stream attachs.
328  * MAIN_THREAD_DEV_REOPEN - When an iodev reopens for format change.
329  * MAIN_THREAD_ADD_ACTIVE_NODE - When an iodev is set as an additional
330  *    active device.
331  * MAIN_THREAD_SELECT_NODE - When UI selects an iodev as active.
332  * MAIN_THREAD_NODE_PLUGGED - When a jack of iodev is plugged/unplugged.
333  * MAIN_THREAD_ADD_TO_DEV_LIST - When iodev is added to list.
334  * MAIN_THREAD_INPUT_NODE_GAIN - When input node gain changes.
335  * MAIN_THREAD_OUTPUT_NODE_VOLUME - When output node volume changes.
336  * MAIN_THREAD_SET_OUTPUT_USER_MUTE - When output mute state is set.
337  * MAIN_THREAD_RESUME_DEVS - When system resumes and notifies CRAS.
338  * MAIN_THREAD_SUSPEND_DEVS - When system suspends and notifies CRAS.
339  * MAIN_THREAD_STREAM_ADDED - When an audio stream is added.
340  * MAIN_THREAD_STREAM_REMOVED - When an audio stream is removed.
341  */
342 enum MAIN_THREAD_LOG_EVENTS {
343 	/* iodev related */
344 	MAIN_THREAD_DEV_CLOSE,
345 	MAIN_THREAD_DEV_DISABLE,
346 	MAIN_THREAD_DEV_INIT,
347 	MAIN_THREAD_DEV_REOPEN,
348 	MAIN_THREAD_ADD_ACTIVE_NODE,
349 	MAIN_THREAD_SELECT_NODE,
350 	MAIN_THREAD_NODE_PLUGGED,
351 	MAIN_THREAD_ADD_TO_DEV_LIST,
352 	MAIN_THREAD_INPUT_NODE_GAIN,
353 	MAIN_THREAD_OUTPUT_NODE_VOLUME,
354 	MAIN_THREAD_SET_OUTPUT_USER_MUTE,
355 	MAIN_THREAD_RESUME_DEVS,
356 	MAIN_THREAD_SUSPEND_DEVS,
357 	/* stream related */
358 	MAIN_THREAD_STREAM_ADDED,
359 	MAIN_THREAD_STREAM_REMOVED,
360 };
361 
362 /* There are 8 bits of space for events. */
363 enum CRAS_BT_LOG_EVENTS {
364 	BT_ADAPTER_ADDED,
365 	BT_ADAPTER_REMOVED,
366 	BT_AUDIO_GATEWAY_INIT,
367 	BT_AUDIO_GATEWAY_START,
368 	BT_AVAILABLE_CODECS,
369 	BT_A2DP_CONFIGURED,
370 	BT_A2DP_START,
371 	BT_A2DP_SUSPENDED,
372 	BT_CODEC_SELECTION,
373 	BT_DEV_CONNECTED,
374 	BT_DEV_DISCONNECTED,
375 	BT_DEV_CONN_WATCH_CB,
376 	BT_DEV_SUSPEND_CB,
377 	BT_HFP_NEW_CONNECTION,
378 	BT_HFP_REQUEST_DISCONNECT,
379 	BT_HFP_SUPPORTED_FEATURES,
380 	BT_HFP_HF_INDICATOR,
381 	BT_HFP_SET_SPEAKER_GAIN,
382 	BT_HFP_UPDATE_SPEAKER_GAIN,
383 	BT_HSP_NEW_CONNECTION,
384 	BT_HSP_REQUEST_DISCONNECT,
385 	BT_NEW_AUDIO_PROFILE_AFTER_CONNECT,
386 	BT_RESET,
387 	BT_SCO_CONNECT,
388 	BT_TRANSPORT_ACQUIRE,
389 	BT_TRANSPORT_RELEASE,
390 	BT_TRANSPORT_SET_VOLUME,
391 	BT_TRANSPORT_UPDATE_VOLUME,
392 };
393 
394 struct __attribute__((__packed__)) audio_thread_event {
395 	uint32_t tag_sec;
396 	uint32_t nsec;
397 	uint32_t data1;
398 	uint32_t data2;
399 	uint32_t data3;
400 };
401 
402 /* Ring buffer of log events from the audio thread. */
403 struct __attribute__((__packed__)) audio_thread_event_log {
404 	uint64_t write_pos;
405 	uint64_t sync_write_pos;
406 	uint32_t len;
407 	struct audio_thread_event log[AUDIO_THREAD_EVENT_LOG_SIZE];
408 };
409 
410 struct __attribute__((__packed__)) audio_dev_debug_info {
411 	char dev_name[CRAS_NODE_NAME_BUFFER_SIZE];
412 	uint32_t buffer_size;
413 	uint32_t min_buffer_level;
414 	uint32_t min_cb_level;
415 	uint32_t max_cb_level;
416 	uint32_t frame_rate;
417 	uint32_t num_channels;
418 	double est_rate_ratio;
419 	uint8_t direction;
420 	uint32_t num_underruns;
421 	uint32_t num_severe_underruns;
422 	uint32_t highest_hw_level;
423 	uint32_t runtime_sec;
424 	uint32_t runtime_nsec;
425 	uint32_t longest_wake_sec;
426 	uint32_t longest_wake_nsec;
427 	double software_gain_scaler;
428 };
429 
430 struct __attribute__((__packed__)) audio_stream_debug_info {
431 	uint64_t stream_id;
432 	uint32_t dev_idx;
433 	uint32_t direction;
434 	uint32_t stream_type;
435 	uint32_t client_type;
436 	uint32_t buffer_frames;
437 	uint32_t cb_threshold;
438 	uint64_t effects;
439 	uint32_t flags;
440 	uint32_t frame_rate;
441 	uint32_t num_channels;
442 	uint32_t longest_fetch_sec;
443 	uint32_t longest_fetch_nsec;
444 	uint32_t num_missed_cb;
445 	uint32_t num_overruns;
446 	uint32_t is_pinned;
447 	uint32_t pinned_dev_idx;
448 	uint32_t runtime_sec;
449 	uint32_t runtime_nsec;
450 	double stream_volume;
451 	int8_t channel_layout[CRAS_CH_MAX];
452 };
453 
454 /* Debug info shared from server to client. */
455 struct __attribute__((__packed__)) audio_debug_info {
456 	uint32_t num_streams;
457 	uint32_t num_devs;
458 	struct audio_dev_debug_info devs[MAX_DEBUG_DEVS];
459 	struct audio_stream_debug_info streams[MAX_DEBUG_STREAMS];
460 	struct audio_thread_event_log log;
461 };
462 
463 struct __attribute__((__packed__)) main_thread_event {
464 	uint32_t tag_sec;
465 	uint32_t nsec;
466 	uint32_t data1;
467 	uint32_t data2;
468 	uint32_t data3;
469 };
470 
471 struct __attribute__((__packed__)) main_thread_event_log {
472 	uint32_t write_pos;
473 	uint32_t len;
474 	struct main_thread_event log[MAIN_THREAD_EVENT_LOG_SIZE];
475 };
476 
477 struct __attribute__((__packed__)) main_thread_debug_info {
478 	struct main_thread_event_log main_log;
479 };
480 
481 struct __attribute__((__packed__)) cras_bt_event {
482 	uint32_t tag_sec;
483 	uint32_t nsec;
484 	uint32_t data1;
485 	uint32_t data2;
486 };
487 
488 struct __attribute__((__packed__)) cras_bt_event_log {
489 	uint32_t write_pos;
490 	uint32_t len;
491 	struct cras_bt_event log[CRAS_BT_EVENT_LOG_SIZE];
492 };
493 
494 struct __attribute__((__packed__)) cras_bt_debug_info {
495 	struct cras_bt_event_log bt_log;
496 	struct packet_status_logger wbs_logger;
497 };
498 
499 /*
500  * All event enums should be less then AUDIO_THREAD_EVENT_TYPE_COUNT,
501  * or they will be ignored by the handler.
502  */
503 enum CRAS_AUDIO_THREAD_EVENT_TYPE {
504 	AUDIO_THREAD_EVENT_A2DP_OVERRUN,
505 	AUDIO_THREAD_EVENT_A2DP_THROTTLE,
506 	AUDIO_THREAD_EVENT_BUSYLOOP,
507 	AUDIO_THREAD_EVENT_DEBUG,
508 	AUDIO_THREAD_EVENT_SEVERE_UNDERRUN,
509 	AUDIO_THREAD_EVENT_UNDERRUN,
510 	AUDIO_THREAD_EVENT_DROP_SAMPLES,
511 	AUDIO_THREAD_EVENT_DEV_OVERRUN,
512 	AUDIO_THREAD_EVENT_TYPE_COUNT,
513 };
514 
515 /*
516  * Structure of snapshot for audio thread.
517  */
518 struct __attribute__((__packed__)) cras_audio_thread_snapshot {
519 	struct timespec timestamp;
520 	enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type;
521 	struct audio_debug_info audio_debug_info;
522 };
523 
524 /*
525  * Ring buffer for storing snapshots.
526  */
527 struct __attribute__((__packed__)) cras_audio_thread_snapshot_buffer {
528 	struct cras_audio_thread_snapshot
529 		snapshots[CRAS_MAX_AUDIO_THREAD_SNAPSHOTS];
530 	int pos;
531 };
532 
533 /* The server state that is shared with clients.
534  *    state_version - Version of this structure.
535  *    volume - index from 0-100.
536  *    min_volume_dBFS - volume in dB * 100 when volume = 1.
537  *    max_volume_dBFS - volume in dB * 100 when volume = max.
538  *    mute - 0 = unmuted, 1 = muted by system (device switch, suspend, etc).
539  *    user_mute - 0 = unmuted, 1 = muted by user.
540  *    mute_locked - 0 = unlocked, 1 = locked.
541  *    suspended - 1 = suspended, 0 = resumed.
542  *    capture_gain - Capture gain in dBFS * 100.
543  *    capture_mute - 0 = unmuted, 1 = muted.
544  *    capture_mute_locked - 0 = unlocked, 1 = locked.
545  *    num_streams_attached - Total number of streams since server started.
546  *    num_output_devs - Number of available output devices.
547  *    num_input_devs - Number of available input devices.
548  *    output_devs - Output audio devices currently attached.
549  *    input_devs - Input audio devices currently attached.
550  *    num_output_nodes - Number of available output nodes.
551  *    num_input_nodes - Number of available input nodes.
552  *    output_nodes - Output nodes currently attached.
553  *    input_nodes - Input nodes currently attached.
554  *    num_attached_clients - Number of clients attached to server.
555  *    client_info - List of first 20 attached clients.
556  *    update_count - Incremented twice each time the struct is updated.  Odd
557  *        during updates.
558  *    num_active_streams - An array containing numbers or active
559  *        streams of different directions.
560  *    last_active_stream_time - Time the last stream was removed.  Can be used
561  *        to determine how long audio has been idle.
562  *    audio_debug_info - Debug data filled in when a client requests it. This
563  *        isn't protected against concurrent updating, only one client should
564  *        use it.
565  *    default_output_buffer_size - Default output buffer size in frames.
566  *    non_empty_status - Whether any non-empty audio is being
567  *        played/captured.
568  *    aec_supported - Flag to indicate if system aec is supported.
569  *    aec_group_id  - Group ID for the system aec to use for separating aec
570  *        tunings.
571  *    snapshot_buffer - ring buffer for storing audio thread snapshots.
572  *    bt_debug_info - ring buffer for storing bluetooth event logs.
573  *    bt_wbs_enabled - Whether or not bluetooth wideband speech is enabled.
574  *    deprioritize_bt_wbs_mic - Whether Bluetooth wideband speech mic
575  *        should be deprioritized for selecting as default audio input.
576  *    main_thread_debug_info - ring buffer for storing main thread event logs.
577  *    num_input_streams_with_permission - An array containing numbers of input
578  *        streams with permission in each client type.
579  *    noise_cancellation_enabled - Whether or not Noise Cancellation is enabled.
580  *    hotword_pause_at_suspend - 1 = Pause hotword detection when the system
581  *        suspends. Hotword detection is resumed after system resumes.
582  *        0 - Hotword detection is allowed to continue running after system
583  *        suspends, so a detected hotword can wake up the device.
584  *
585  */
586 #define CRAS_SERVER_STATE_VERSION 2
587 struct __attribute__((packed, aligned(4))) cras_server_state {
588 	uint32_t state_version;
589 	uint32_t volume;
590 	int32_t min_volume_dBFS;
591 	int32_t max_volume_dBFS;
592 	int32_t mute;
593 	int32_t user_mute;
594 	int32_t mute_locked;
595 	int32_t suspended;
596 	int32_t capture_gain;
597 	int32_t capture_mute;
598 	int32_t capture_mute_locked;
599 	uint32_t num_streams_attached;
600 	uint32_t num_output_devs;
601 	uint32_t num_input_devs;
602 	struct cras_iodev_info output_devs[CRAS_MAX_IODEVS];
603 	struct cras_iodev_info input_devs[CRAS_MAX_IODEVS];
604 	uint32_t num_output_nodes;
605 	uint32_t num_input_nodes;
606 	struct cras_ionode_info output_nodes[CRAS_MAX_IONODES];
607 	struct cras_ionode_info input_nodes[CRAS_MAX_IONODES];
608 	uint32_t num_attached_clients;
609 	struct cras_attached_client_info client_info[CRAS_MAX_ATTACHED_CLIENTS];
610 	uint32_t update_count;
611 	uint32_t num_active_streams[CRAS_NUM_DIRECTIONS];
612 	struct cras_timespec last_active_stream_time;
613 	struct audio_debug_info audio_debug_info;
614 	int32_t default_output_buffer_size;
615 	int32_t non_empty_status;
616 	int32_t aec_supported;
617 	int32_t aec_group_id;
618 	struct cras_audio_thread_snapshot_buffer snapshot_buffer;
619 	struct cras_bt_debug_info bt_debug_info;
620 	int32_t bt_wbs_enabled;
621 	int32_t deprioritize_bt_wbs_mic;
622 	struct main_thread_debug_info main_thread_debug_info;
623 	uint32_t num_input_streams_with_permission[CRAS_NUM_CLIENT_TYPE];
624 	int32_t noise_cancellation_enabled;
625 	int32_t hotword_pause_at_suspend;
626 };
627 
628 /* Actions for card add/remove/change. */
629 enum cras_notify_device_action {
630 	/* Must match gavd action definitions.  */
631 	CRAS_DEVICE_ACTION_ADD = 0,
632 	CRAS_DEVICE_ACTION_REMOVE = 1,
633 	CRAS_DEVICE_ACTION_CHANGE = 2,
634 };
635 
636 /* Information about an ALSA card to be added to the system.
637  *    card_type - Either internal card or a USB sound card.
638  *    card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
639  *    priority - Base priority to give devices found on this card. Zero is the
640  *      lowest priority.  Non-primary devices on the card will be given a
641  *      lowered priority.
642  *    usb_vendor_id - vendor ID if the device is on the USB bus.
643  *    usb_product_id - product ID if the device is on the USB bus.
644  *    usb_serial_number - serial number if the device is on the USB bus.
645  *    usb_desc_checksum - the checksum of the USB descriptors if the device
646  *      is on the USB bus.
647  */
648 enum CRAS_ALSA_CARD_TYPE {
649 	ALSA_CARD_TYPE_INTERNAL,
650 	ALSA_CARD_TYPE_USB,
651 };
652 #define USB_SERIAL_NUMBER_BUFFER_SIZE 64
653 struct __attribute__((__packed__)) cras_alsa_card_info {
654 	enum CRAS_ALSA_CARD_TYPE card_type;
655 	uint32_t card_index;
656 	uint32_t usb_vendor_id;
657 	uint32_t usb_product_id;
658 	char usb_serial_number[USB_SERIAL_NUMBER_BUFFER_SIZE];
659 	uint32_t usb_desc_checksum;
660 };
661 
662 /* Unique identifier for each active stream.
663  * The top 16 bits are the client number, lower 16 are the stream number.
664  */
665 typedef uint32_t cras_stream_id_t;
666 /* Generates a stream id for client stream. */
cras_get_stream_id(uint16_t client_id,uint16_t stream_id)667 static inline cras_stream_id_t cras_get_stream_id(uint16_t client_id,
668 						  uint16_t stream_id)
669 {
670 	return (cras_stream_id_t)(((client_id & 0x0000ffff) << 16) |
671 				  (stream_id & 0x0000ffff));
672 }
673 /* Verify if the stream_id fits the given client_id */
cras_valid_stream_id(cras_stream_id_t stream_id,uint16_t client_id)674 static inline bool cras_valid_stream_id(cras_stream_id_t stream_id,
675 					uint16_t client_id)
676 {
677 	return ((stream_id >> 16) ^ client_id) == 0;
678 }
679 
680 enum CRAS_NODE_TYPE {
681 	/* These value can be used for output nodes. */
682 	CRAS_NODE_TYPE_INTERNAL_SPEAKER,
683 	CRAS_NODE_TYPE_HEADPHONE,
684 	CRAS_NODE_TYPE_HDMI,
685 	CRAS_NODE_TYPE_HAPTIC,
686 	CRAS_NODE_TYPE_LINEOUT,
687 	/* These value can be used for input nodes. */
688 	CRAS_NODE_TYPE_MIC,
689 	CRAS_NODE_TYPE_HOTWORD,
690 	CRAS_NODE_TYPE_POST_MIX_PRE_DSP,
691 	CRAS_NODE_TYPE_POST_DSP,
692 	/* Type for the legacy BT narrow band mic .*/
693 	CRAS_NODE_TYPE_BLUETOOTH_NB_MIC,
694 	/* These value can be used for both output and input nodes. */
695 	CRAS_NODE_TYPE_USB,
696 	CRAS_NODE_TYPE_BLUETOOTH,
697 	CRAS_NODE_TYPE_FALLBACK_NORMAL,
698 	CRAS_NODE_TYPE_FALLBACK_ABNORMAL,
699 	CRAS_NODE_TYPE_UNKNOWN,
700 	CRAS_NODE_TYPE_ECHO_REFERENCE,
701 	CRAS_NODE_TYPE_ALSA_LOOPBACK,
702 };
703 
704 /* Position values to described where a node locates on the system.
705  * NODE_POSITION_EXTERNAL - The node works only when peripheral
706  *     is plugged.
707  * NODE_POSITION_INTERNAL - The node lives on the system and doesn't
708  *     have specific direction.
709  * NODE_POSITION_FRONT - The node locates on the side of system that
710  *     faces user.
711  * NODE_POSITION_REAR - The node locates on the opposite side of
712  *     the system that faces user.
713  * NODE_POSITION_KEYBOARD - The node locates under the keyboard.
714  */
715 enum CRAS_NODE_POSITION {
716 	NODE_POSITION_EXTERNAL,
717 	NODE_POSITION_INTERNAL,
718 	NODE_POSITION_FRONT,
719 	NODE_POSITION_REAR,
720 	NODE_POSITION_KEYBOARD,
721 };
722 
723 #endif /* CRAS_TYPES_H_ */
724