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