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