• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2014 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  * The dev_stream structure is used for mapping streams to a device.  In
6  * addition to the rstream, other mixing information is stored here.
7  */
8 
9 #ifndef DEV_STREAM_H_
10 #define DEV_STREAM_H_
11 
12 #include <stdint.h>
13 #include <sys/time.h>
14 
15 #include "cras_types.h"
16 #include "cras_rstream.h"
17 
18 struct cras_audio_area;
19 struct cras_fmt_conv;
20 struct cras_iodev;
21 
22 /*
23  * Linked list of streams of audio from/to a client.
24  * Args:
25  *    dev_id - Index of the hw device.
26  *    stream - The rstream attached to a device.
27  *    conv - Sample rate or format converter.
28  *    conv_buffer - The buffer for converter if needed.
29  *    conv_buffer_size_frames - Size of conv_buffer in frames.
30  *    dev_rate - Sampling rate of device. This is set when dev_stream is
31  *               created.
32  *    is_running - For input stream, it should be set to true after it is added
33  *                 into device. For output stream, it should be set to true
34  *                 just before its first fetch to avoid affecting other existing
35  *                 streams.
36  */
37 struct dev_stream {
38 	unsigned int dev_id;
39 	struct cras_rstream *stream;
40 	struct cras_fmt_conv *conv;
41 	struct byte_buffer *conv_buffer;
42 	struct cras_audio_area *conv_area;
43 	unsigned int conv_buffer_size_frames;
44 	size_t dev_rate;
45 	struct dev_stream *prev, *next;
46 	int is_running;
47 };
48 
49 /*
50  * Creates a dev_stream.
51  *
52  * Args:
53  *    stream - The associated rstream.
54  *    dev_id - Index of the device.
55  *    dev_fmt - The format of the device.
56  *    dev_ptr - A pointer to the device
57  *    cb_ts - A pointer to the initial callback time.
58  *    sleep_interval_ts - A pointer to the initial sleep interval.
59  *        Set to null to calculate the value from device rate and block size.
60  *        Note that we need this argument so that output device sleep interval
61  *        can use input device sleep interval in the beginning to have perfect
62  *        alignment in WebRTC use case.
63  * Returns the pointer to the created dev_stream.
64  */
65 struct dev_stream *dev_stream_create(struct cras_rstream *stream,
66 				     unsigned int dev_id,
67 				     const struct cras_audio_format *dev_fmt,
68 				     void *dev_ptr, struct timespec *cb_ts,
69 				     const struct timespec *sleep_interval_ts);
70 void dev_stream_destroy(struct dev_stream *dev_stream);
71 
72 /*
73  * Update the estimated sample rate of the device. For multiple active
74  * devices case, the linear resampler will be configured by the estimated
75  * rate ration of the main device and the current active device the
76  * rstream attaches to.
77  *
78  * Args:
79  *    dev_stream - The structure holding the stream.
80  *    dev_rate - The sample rate device is using.
81  *    dev_rate_ratio - The ratio of estimated rate and used rate.
82  *    main_rate_ratio - The ratio of estimated rate and used rate of
83  *        main device.
84  *    coarse_rate_adjust - The flag to indicate the direction device
85  *        sample rate should adjust to.
86  */
87 void dev_stream_set_dev_rate(struct dev_stream *dev_stream,
88 			     unsigned int dev_rate, double dev_rate_ratio,
89 			     double main_rate_ratio, int coarse_rate_adjust);
90 
91 /*
92  * Renders count frames from shm into dst.  Updates count if anything is
93  * written. If it's muted and the only stream zero memory.
94  * Args:
95  *    dev_stream - The struct holding the stream to mix.
96  *    format - The format of the audio device.
97  *    dst - The destination buffer for mixing.
98  *    num_to_write - The number of frames written.
99  */
100 int dev_stream_mix(struct dev_stream *dev_stream,
101 		   const struct cras_audio_format *fmt, uint8_t *dst,
102 		   unsigned int num_to_write);
103 
104 /*
105  * Reads froms from the source into the dev_stream.
106  * Args:
107  *    dev_stream - The struct holding the stream to mix to.
108  *    area - The area to copy audio from.
109  *    area_offset - The offset at which to start reading from area.
110  *    software_gain_scaler - The software gain scaler.
111  */
112 unsigned int dev_stream_capture(struct dev_stream *dev_stream,
113 				const struct cras_audio_area *area,
114 				unsigned int area_offset,
115 				float software_gain_scaler);
116 
117 /* Returns the number of iodevs this stream has attached to. */
118 int dev_stream_attached_devs(const struct dev_stream *dev_stream);
119 
120 /* Updates the number of queued frames in dev_stream. */
121 void dev_stream_update_frames(const struct dev_stream *dev_stream);
122 
123 /*
124  * Returns the number of playback frames queued in shared memory.  This is a
125  * post-format-conversion number.  If the stream is 24k with 10 frames queued
126  * and the device is playing at 48k, 20 will be returned.
127  */
128 int dev_stream_playback_frames(const struct dev_stream *dev_stream);
129 
130 /*
131  * Returns the number of frames free to be written to in a capture stream.  This
132  * number is also post format conversion, similar to playback_frames above.
133  */
134 unsigned int dev_stream_capture_avail(const struct dev_stream *dev_stream);
135 
136 /*
137  * Returns the callback threshold, if necesary converted from a stream frame
138  * count to a device frame count.
139  */
140 unsigned int dev_stream_cb_threshold(const struct dev_stream *dev_stream);
141 
142 /* Update next callback time for the stream. */
143 void dev_stream_update_next_wake_time(struct dev_stream *dev_stream);
144 
145 /*
146  * If enough samples have been captured, post them to the client.
147  * TODO(dgreid) - see if this function can be eliminated.
148  */
149 int dev_stream_capture_update_rstream(struct dev_stream *dev_stream);
150 
151 /* Updates the read buffer pointers for the stream. */
152 int dev_stream_playback_update_rstream(struct dev_stream *dev_stream);
153 
154 /* Fill ts with the time the playback sample will be played. */
155 void cras_set_playback_timestamp(size_t frame_rate, size_t frames,
156 				 struct cras_timespec *ts);
157 
158 /* Fill ts with the time the capture sample was recorded. */
159 void cras_set_capture_timestamp(size_t frame_rate, size_t frames,
160 				struct cras_timespec *ts);
161 
162 /* Fill shm ts with the time the playback sample will be played or the capture
163  * sample was captured depending on the direction of the stream.
164  * Args:
165  *    delay_frames - The delay reproted by the device, in frames at the device's
166  *      sample rate.
167  */
168 void dev_stream_set_delay(const struct dev_stream *dev_stream,
169 			  unsigned int delay_frames);
170 
171 /* Ask the client for cb_threshold samples of audio to play. */
172 int dev_stream_request_playback_samples(struct dev_stream *dev_stream,
173 					const struct timespec *now);
174 
175 /*
176  * Gets the wake up time for a dev_stream.
177  * For an input stream, it considers both needed samples and proper time
178  * interval between each callbacks.
179  * Args:
180  *   dev_stream[in]: The dev_stream to check wake up time.
181  *   curr_level[in]: The current level of device.
182  *   level_tstamp[in]: The time stamp when getting current level of device.
183  *   cap_limit[in]: The number of frames that can be captured across all
184  *                  streams.
185  *   is_cap_limit_stream[in]: 1 if this stream is causing cap_limit.
186  *   wake_time_out[out]: A timespec for wake up time.
187  * Returns:
188  *   0 on success; negative error code on failure.
189  *   A positive value if there is no need to set wake up time for this stream.
190  */
191 int dev_stream_wake_time(struct dev_stream *dev_stream, unsigned int curr_level,
192 			 struct timespec *level_tstamp, unsigned int cap_limit,
193 			 int is_cap_limit_stream,
194 			 struct timespec *wake_time_out);
195 
196 /*
197  * Returns a non-negative fd if the fd is expecting a message and should be
198  * added to the list of descriptors to poll.
199  */
200 int dev_stream_poll_stream_fd(const struct dev_stream *dev_stream);
201 
dev_stream_is_running(struct dev_stream * dev_stream)202 static inline int dev_stream_is_running(struct dev_stream *dev_stream)
203 {
204 	return dev_stream->is_running;
205 }
206 
dev_stream_set_running(struct dev_stream * dev_stream)207 static inline void dev_stream_set_running(struct dev_stream *dev_stream)
208 {
209 	dev_stream->is_running = 1;
210 }
211 
212 static inline const struct timespec *
dev_stream_next_cb_ts(const struct dev_stream * dev_stream)213 dev_stream_next_cb_ts(const struct dev_stream *dev_stream)
214 {
215 	if (dev_stream->stream->flags & USE_DEV_TIMING)
216 		return NULL;
217 
218 	return &dev_stream->stream->next_cb_ts;
219 }
220 
221 static inline const struct timespec *
dev_stream_sleep_interval_ts(struct dev_stream * dev_stream)222 dev_stream_sleep_interval_ts(struct dev_stream *dev_stream)
223 {
224 	return &dev_stream->stream->sleep_interval_ts;
225 }
226 
227 int dev_stream_is_pending_reply(const struct dev_stream *dev_stream);
228 
229 /*
230  * Reads any pending audio message from the socket.
231  */
232 int dev_stream_flush_old_audio_messages(struct dev_stream *dev_stream);
233 
234 #endif /* DEV_STREAM_H_ */
235