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 * Remote Stream - An audio steam from/to a client.
8 */
9 #ifndef CRAS_RSTREAM_H_
10 #define CRAS_RSTREAM_H_
11
12 #include "cras_apm_list.h"
13 #include "cras_shm.h"
14 #include "cras_types.h"
15
16 struct cras_connect_message;
17 struct cras_rclient;
18 struct dev_mix;
19
20 /* Holds informations about the master active device.
21 * Members:
22 * dev_id - id of the master device.
23 * dev_ptr - pointer to the master device.
24 */
25 struct master_dev_info {
26 int dev_id;
27 void *dev_ptr;
28 };
29
30 /* cras_rstream is used to manage an active audio stream from
31 * a client. Each client can have any number of open streams for
32 * playing or recording.
33 * Members:
34 * stream_id - identifier for this stream.
35 * stream_type - not used.
36 * client_type - The client type of this stream, like Chrome, ARC++.
37 * direction - input or output.
38 * flags - Indicative of what special handling is needed.
39 * fd - Socket for requesting and sending audio buffer events.
40 * buffer_frames - Buffer size in frames.
41 * cb_threshold - Callback client when this much is left.
42 * master_dev_info - The info of the master device this stream attaches to.
43 * is_draining - The stream is draining and waiting to be removed.
44 * client - The client who uses this stream.
45 * shm - shared memory
46 * audio_area - space for playback/capture audio
47 * format - format of the stream
48 * next_cb_ts - Next callback time for this stream.
49 * sleep_interval_ts - Time between audio callbacks.
50 * last_fetch_ts - The time of the last stream fetch.
51 * longest_fetch_interval_ts - Longest interval between two fetches.
52 * start_ts - The time when the stream started.
53 * first_missed_cb_ts - The time when the first missed callback happens.
54 * buf_state - State of the buffer from all devices for this stream.
55 * apm_list - List of audio processing module instances.
56 * num_attached_devs - Number of iodevs this stream has attached to.
57 * num_missed_cb - Number of callback schedules have been missed.
58 * queued_frames - Cached value of the number of queued frames in shm.
59 * is_pinned - True if the stream is a pinned stream, false otherwise.
60 * pinned_dev_idx - device the stream is pinned, 0 if none.
61 * triggered - True if already notified TRIGGER_ONLY stream, false otherwise.
62 */
63 struct cras_rstream {
64 cras_stream_id_t stream_id;
65 enum CRAS_STREAM_TYPE stream_type;
66 enum CRAS_CLIENT_TYPE client_type;
67 enum CRAS_STREAM_DIRECTION direction;
68 uint32_t flags;
69 int fd;
70 size_t buffer_frames;
71 size_t cb_threshold;
72 int is_draining;
73 struct master_dev_info master_dev;
74 struct cras_rclient *client;
75 struct cras_audio_shm *shm;
76 struct cras_audio_area *audio_area;
77 struct cras_audio_format format;
78 struct timespec next_cb_ts;
79 struct timespec sleep_interval_ts;
80 struct timespec last_fetch_ts;
81 struct timespec longest_fetch_interval;
82 struct timespec start_ts;
83 struct timespec first_missed_cb_ts;
84 struct buffer_share *buf_state;
85 struct cras_apm_list *apm_list;
86 int num_attached_devs;
87 int num_missed_cb;
88 int queued_frames;
89 int is_pinned;
90 uint32_t pinned_dev_idx;
91 int triggered;
92 struct cras_rstream *prev, *next;
93 };
94
95 /* Config for creating an rstream.
96 * stream_type - CRAS_STREAM_TYPE.
97 * client_type - CRAS_CLIENT_TYPE.
98 * direction - CRAS_STREAM_OUTPUT or CRAS_STREAM_INPUT.
99 * dev_idx - Pin to this device if != NO_DEVICE.
100 * flags - Any special handling for this stream.
101 * effects - Bit map of effects to be enabled on this stream.
102 * format - The audio format the stream wishes to use.
103 * buffer_frames - Total number of audio frames to buffer.
104 * cb_threshold - # of frames when to request more from the client.
105 * audio_fd - The fd to read/write audio signals to. May be -1 for server
106 * stream. Some functions may mutably borrow the config and move
107 * the fd ownership.
108 * client_shm_fd - The shm fd to use to back the samples area. May be -1.
109 * Some functions may dup this fd while borrowing the config.
110 * client_shm_size - The size of shm area backed by client_shm_fd.
111 * client - The client that owns this stream.
112 */
113 struct cras_rstream_config {
114 cras_stream_id_t stream_id;
115 enum CRAS_STREAM_TYPE stream_type;
116 enum CRAS_CLIENT_TYPE client_type;
117 enum CRAS_STREAM_DIRECTION direction;
118 uint32_t dev_idx;
119 uint32_t flags;
120 uint32_t effects;
121 const struct cras_audio_format *format;
122 size_t buffer_frames;
123 size_t cb_threshold;
124 int audio_fd;
125 int client_shm_fd;
126 size_t client_shm_size;
127 struct cras_rclient *client;
128 };
129
130 /* Fills cras_rstream_config with given parameters.
131 *
132 * Args:
133 * audio_fd - The audio fd pointer from client. Its ownership will be moved to
134 * stream_config.
135 * client_shm_fd - The shared memory fd pointer for samples from client. Its
136 * ownership will be moved to stream_config.
137 * Other args - See comments in struct cras_rstream_config.
138 */
139 void cras_rstream_config_init(
140 struct cras_rclient *client, cras_stream_id_t stream_id,
141 enum CRAS_STREAM_TYPE stream_type, enum CRAS_CLIENT_TYPE client_type,
142 enum CRAS_STREAM_DIRECTION direction, uint32_t dev_idx, uint32_t flags,
143 uint32_t effects, const struct cras_audio_format *format,
144 size_t buffer_frames, size_t cb_threshold, int *audio_fd,
145 int *client_shm_fd, size_t client_shm_size,
146 struct cras_rstream_config *stream_config);
147
148 /* Fills cras_rstream_config with given parameters and a cras_connect_message.
149 *
150 * Args:
151 * client - The rclient which handles the connect message.
152 * msg - The cras_connect_message from client.
153 * aud_fd - The audio fd pointer from client. Its ownership will be moved to
154 * stream_config.
155 * client_shm_fd - The shared memory fd pointer for samples from client. Its
156 * ownership will be moved to stream_config.
157 * remote_format - The remote_format for the config.
158 * stream_config - The cras_rstream_config to be filled.
159 */
160 void cras_rstream_config_init_with_message(
161 struct cras_rclient *client, const struct cras_connect_message *msg,
162 int *aud_fd, int *client_shm_fd,
163 const struct cras_audio_format *remote_format,
164 struct cras_rstream_config *stream_config);
165
166 /* Cleans up given cras_rstream_config. All fds inside the config will be
167 * closed.
168 *
169 * Args:
170 * stream_config - The config to be cleaned up.
171 */
172 void cras_rstream_config_cleanup(struct cras_rstream_config *stream_config);
173
174 /* Creates an rstream.
175 * Args:
176 * config - Params for configuration of the new rstream. It's a mutable
177 * borrow.
178 * stream_out - Filled with the newly created stream pointer.
179 * Returns:
180 * 0 on success, EINVAL if an invalid argument is passed, or ENOMEM if out of
181 * memory.
182 */
183 int cras_rstream_create(struct cras_rstream_config *config,
184 struct cras_rstream **stream_out);
185
186 /* Destroys an rstream. */
187 void cras_rstream_destroy(struct cras_rstream *stream);
188
189 /* Gets the id of the stream */
190 static inline cras_stream_id_t
cras_rstream_id(const struct cras_rstream * stream)191 cras_rstream_id(const struct cras_rstream *stream)
192 {
193 return stream->stream_id;
194 }
195
196 /* Gets the total buffer size in frames for the given client stream. */
197 static inline size_t
cras_rstream_get_buffer_frames(const struct cras_rstream * stream)198 cras_rstream_get_buffer_frames(const struct cras_rstream *stream)
199 {
200 return stream->buffer_frames;
201 }
202
203 /* Gets the callback threshold in frames for the given client stream. */
204 static inline size_t
cras_rstream_get_cb_threshold(const struct cras_rstream * stream)205 cras_rstream_get_cb_threshold(const struct cras_rstream *stream)
206 {
207 return stream->cb_threshold;
208 }
209
210 /* Gets the max write size for the stream. */
211 static inline size_t
cras_rstream_get_max_write_frames(const struct cras_rstream * stream)212 cras_rstream_get_max_write_frames(const struct cras_rstream *stream)
213 {
214 if (stream->flags & BULK_AUDIO_OK)
215 return cras_rstream_get_buffer_frames(stream);
216 return cras_rstream_get_cb_threshold(stream);
217 }
218
219 /* Gets the stream type of this stream. */
220 static inline enum CRAS_STREAM_TYPE
cras_rstream_get_type(const struct cras_rstream * stream)221 cras_rstream_get_type(const struct cras_rstream *stream)
222 {
223 return stream->stream_type;
224 }
225
226 /* Gets the direction (input/output/loopback) of the stream. */
227 static inline enum CRAS_STREAM_DIRECTION
cras_rstream_get_direction(const struct cras_rstream * stream)228 cras_rstream_get_direction(const struct cras_rstream *stream)
229 {
230 return stream->direction;
231 }
232
233 /* Gets the format for the stream. */
cras_rstream_set_format(struct cras_rstream * stream,const struct cras_audio_format * fmt)234 static inline void cras_rstream_set_format(struct cras_rstream *stream,
235 const struct cras_audio_format *fmt)
236 {
237 stream->format = *fmt;
238 }
239
240 /* Sets the format for the stream. */
cras_rstream_get_format(const struct cras_rstream * stream,struct cras_audio_format * fmt)241 static inline int cras_rstream_get_format(const struct cras_rstream *stream,
242 struct cras_audio_format *fmt)
243 {
244 *fmt = stream->format;
245 return 0;
246 }
247
248 /* Gets the fd to be used to poll this client for audio. */
cras_rstream_get_audio_fd(const struct cras_rstream * stream)249 static inline int cras_rstream_get_audio_fd(const struct cras_rstream *stream)
250 {
251 return stream->fd;
252 }
253
254 /* Gets the is_draning flag. */
255 static inline int
cras_rstream_get_is_draining(const struct cras_rstream * stream)256 cras_rstream_get_is_draining(const struct cras_rstream *stream)
257 {
258 return stream->is_draining;
259 }
260
261 /* Sets the is_draning flag. */
cras_rstream_set_is_draining(struct cras_rstream * stream,int is_draining)262 static inline void cras_rstream_set_is_draining(struct cras_rstream *stream,
263 int is_draining)
264 {
265 stream->is_draining = is_draining;
266 }
267
268 /* Gets the shm fds used for the stream shm */
cras_rstream_get_shm_fds(const struct cras_rstream * stream,int * header_fd,int * samples_fd)269 static inline int cras_rstream_get_shm_fds(const struct cras_rstream *stream,
270 int *header_fd, int *samples_fd)
271 {
272 if (!header_fd || !samples_fd)
273 return -EINVAL;
274
275 *header_fd = stream->shm->header_info.fd;
276 *samples_fd = stream->shm->samples_info.fd;
277
278 return 0;
279 }
280
281 /* Gets the size of the shm area used for samples for this stream. */
282 static inline size_t
cras_rstream_get_samples_shm_size(const struct cras_rstream * stream)283 cras_rstream_get_samples_shm_size(const struct cras_rstream *stream)
284 {
285 return cras_shm_samples_size(stream->shm);
286 }
287
288 /* Gets shared memory region for this stream. */
289 static inline struct cras_audio_shm *
cras_rstream_shm(struct cras_rstream * stream)290 cras_rstream_shm(struct cras_rstream *stream)
291 {
292 return stream->shm;
293 }
294
295 /* Checks if the stream uses an output device. */
stream_uses_output(const struct cras_rstream * s)296 static inline int stream_uses_output(const struct cras_rstream *s)
297 {
298 return cras_stream_uses_output_hw(s->direction);
299 }
300
301 /* Checks if the stream uses an input device. */
stream_uses_input(const struct cras_rstream * s)302 static inline int stream_uses_input(const struct cras_rstream *s)
303 {
304 return cras_stream_uses_input_hw(s->direction);
305 }
306
stream_is_server_only(const struct cras_rstream * s)307 static inline int stream_is_server_only(const struct cras_rstream *s)
308 {
309 return s->flags & SERVER_ONLY;
310 }
311
312 /* Gets the enabled effects of this stream. */
313 unsigned int cras_rstream_get_effects(const struct cras_rstream *stream);
314
315 /* Gets the format of data after stream specific processing. */
316 struct cras_audio_format *
317 cras_rstream_post_processing_format(const struct cras_rstream *stream,
318 void *dev_ptr);
319
320 /* Checks how much time has passed since last stream fetch and records
321 * the longest fetch interval. */
322 void cras_rstream_record_fetch_interval(struct cras_rstream *rstream,
323 const struct timespec *now);
324
325 /* Requests min_req frames from the client. */
326 int cras_rstream_request_audio(struct cras_rstream *stream,
327 const struct timespec *now);
328
329 /* Tells a capture client that count frames are ready. */
330 int cras_rstream_audio_ready(struct cras_rstream *stream, size_t count);
331
332 /* Let the rstream know when a device is added or removed. */
333 void cras_rstream_dev_attach(struct cras_rstream *rstream, unsigned int dev_id,
334 void *dev_ptr);
335 void cras_rstream_dev_detach(struct cras_rstream *rstream, unsigned int dev_id);
336
337 /* A device using this stream has read or written samples. */
338 void cras_rstream_dev_offset_update(struct cras_rstream *rstream,
339 unsigned int frames, unsigned int dev_id);
340
341 void cras_rstream_update_input_write_pointer(struct cras_rstream *rstream);
342 void cras_rstream_update_output_read_pointer(struct cras_rstream *rstream);
343
344 unsigned int cras_rstream_dev_offset(const struct cras_rstream *rstream,
345 unsigned int dev_id);
346
cras_rstream_level(struct cras_rstream * rstream)347 static inline unsigned int cras_rstream_level(struct cras_rstream *rstream)
348 {
349 const struct cras_audio_shm *shm = cras_rstream_shm(rstream);
350 return cras_shm_frames_written(shm);
351 }
352
cras_rstream_input_level_met(struct cras_rstream * rstream)353 static inline int cras_rstream_input_level_met(struct cras_rstream *rstream)
354 {
355 const struct cras_audio_shm *shm = cras_rstream_shm(rstream);
356 return cras_shm_frames_written(shm) >= rstream->cb_threshold;
357 }
358
359 /* Updates the number of queued frames in shm. The queued frames should be
360 * updated everytime before calling cras_rstream_playable_frames.
361 */
362 void cras_rstream_update_queued_frames(struct cras_rstream *rstream);
363
364 /* Returns the number of playable samples in shm for the given device id. */
365 unsigned int cras_rstream_playable_frames(struct cras_rstream *rstream,
366 unsigned int dev_id);
367
368 /* Returns the volume scaler for this stream. */
369 float cras_rstream_get_volume_scaler(struct cras_rstream *rstream);
370
371 /* Returns a pointer to readable frames, fills frames with the number of frames
372 * available. */
373 uint8_t *cras_rstream_get_readable_frames(struct cras_rstream *rstream,
374 unsigned int offset, size_t *frames);
375
376 /* Returns non-zero if the stream is muted. */
377 int cras_rstream_get_mute(const struct cras_rstream *rstream);
378
379 /*
380 * Returns non-zero if the stream is pending a reply from client.
381 * - For playback, stream is waiting for AUDIO_MESSAGE_DATA_READY message from
382 * client.
383 * - For capture, stream is waiting for AUDIO_MESSAGE_DATA_CAPTURED message
384 * from client.
385 */
386 int cras_rstream_is_pending_reply(const struct cras_rstream *stream);
387
388 /*
389 * Reads any pending audio message from the socket.
390 */
391 int cras_rstream_flush_old_audio_messages(struct cras_rstream *stream);
392
393 #endif /* CRAS_RSTREAM_H_ */
394