• 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 struct dev_stream *dev_stream_create(struct cras_rstream *stream,
50 				     unsigned int dev_id,
51 				     const struct cras_audio_format *dev_fmt,
52 				     void *dev_ptr, struct timespec *cb_ts);
53 void dev_stream_destroy(struct dev_stream *dev_stream);
54 
55 /*
56  * Update the estimated sample rate of the device. For multiple active
57  * devices case, the linear resampler will be configured by the estimated
58  * rate ration of the master device and the current active device the
59  * rstream attaches to.
60  *
61  * Args:
62  *    dev_stream - The structure holding the stream.
63  *    dev_rate - The sample rate device is using.
64  *    dev_rate_ratio - The ratio of estimated rate and used rate.
65  *    master_rate_ratio - The ratio of estimated rate and used rate of
66  *        master device.
67  *    coarse_rate_adjust - The flag to indicate the direction device
68  *        sample rate should adjust to.
69  */
70 void dev_stream_set_dev_rate(struct dev_stream *dev_stream,
71 			     unsigned int dev_rate, double dev_rate_ratio,
72 			     double master_rate_ratio, int coarse_rate_adjust);
73 
74 /*
75  * Renders count frames from shm into dst.  Updates count if anything is
76  * written. If it's muted and the only stream zero memory.
77  * Args:
78  *    dev_stream - The struct holding the stream to mix.
79  *    format - The format of the audio device.
80  *    dst - The destination buffer for mixing.
81  *    num_to_write - The number of frames written.
82  */
83 int dev_stream_mix(struct dev_stream *dev_stream,
84 		   const struct cras_audio_format *fmt, uint8_t *dst,
85 		   unsigned int num_to_write);
86 
87 /*
88  * Reads froms from the source into the dev_stream.
89  * Args:
90  *    dev_stream - The struct holding the stream to mix to.
91  *    area - The area to copy audio from.
92  *    area_offset - The offset at which to start reading from area.
93  *    software_gain_scaler - The software gain scaler.
94  */
95 unsigned int dev_stream_capture(struct dev_stream *dev_stream,
96 				const struct cras_audio_area *area,
97 				unsigned int area_offset,
98 				float software_gain_scaler);
99 
100 /* Returns the number of iodevs this stream has attached to. */
101 int dev_stream_attached_devs(const struct dev_stream *dev_stream);
102 
103 /* Updates the number of queued frames in dev_stream. */
104 void dev_stream_update_frames(const struct dev_stream *dev_stream);
105 
106 /*
107  * Returns the number of playback frames queued in shared memory.  This is a
108  * post-format-conversion number.  If the stream is 24k with 10 frames queued
109  * and the device is playing at 48k, 20 will be returned.
110  */
111 int dev_stream_playback_frames(const struct dev_stream *dev_stream);
112 
113 /*
114  * Returns the number of frames free to be written to in a capture stream.  This
115  * number is also post format conversion, similar to playback_frames above.
116  */
117 unsigned int dev_stream_capture_avail(const struct dev_stream *dev_stream);
118 
119 /*
120  * Returns the callback threshold, if necesary converted from a stream frame
121  * count to a device frame count.
122  */
123 unsigned int dev_stream_cb_threshold(const struct dev_stream *dev_stream);
124 
125 /* Update next callback time for the stream. */
126 void dev_stream_update_next_wake_time(struct dev_stream *dev_stream);
127 
128 /*
129  * If enough samples have been captured, post them to the client.
130  * TODO(dgreid) - see if this function can be eliminated.
131  */
132 int dev_stream_capture_update_rstream(struct dev_stream *dev_stream);
133 
134 /* Updates the read buffer pointers for the stream. */
135 int dev_stream_playback_update_rstream(struct dev_stream *dev_stream);
136 
137 /* Fill ts with the time the playback sample will be played. */
138 void cras_set_playback_timestamp(size_t frame_rate, size_t frames,
139 				 struct cras_timespec *ts);
140 
141 /* Fill ts with the time the capture sample was recorded. */
142 void cras_set_capture_timestamp(size_t frame_rate, size_t frames,
143 				struct cras_timespec *ts);
144 
145 /* Fill shm ts with the time the playback sample will be played or the capture
146  * sample was captured depending on the direction of the stream.
147  * Args:
148  *    delay_frames - The delay reproted by the device, in frames at the device's
149  *      sample rate.
150  */
151 void dev_stream_set_delay(const struct dev_stream *dev_stream,
152 			  unsigned int delay_frames);
153 
154 /* Ask the client for cb_threshold samples of audio to play. */
155 int dev_stream_request_playback_samples(struct dev_stream *dev_stream,
156 					const struct timespec *now);
157 
158 /*
159  * Gets the wake up time for a dev_stream.
160  * For an input stream, it considers both needed samples and proper time
161  * interval between each callbacks.
162  * Args:
163  *   dev_stream[in]: The dev_stream to check wake up time.
164  *   curr_level[in]: The current level of device.
165  *   level_tstamp[in]: The time stamp when getting current level of device.
166  *   cap_limit[in]: The number of frames that can be captured across all
167  *                  streams.
168  *   is_cap_limit_stream[in]: 1 if this stream is causing cap_limit.
169  *   wake_time_out[out]: A timespec for wake up time.
170  * Returns:
171  *   0 on success; negative error code on failure.
172  *   A positive value if there is no need to set wake up time for this stream.
173  */
174 int dev_stream_wake_time(struct dev_stream *dev_stream, unsigned int curr_level,
175 			 struct timespec *level_tstamp, unsigned int cap_limit,
176 			 int is_cap_limit_stream,
177 			 struct timespec *wake_time_out);
178 
179 /*
180  * Returns a non-negative fd if the fd is expecting a message and should be
181  * added to the list of descriptors to poll.
182  */
183 int dev_stream_poll_stream_fd(const struct dev_stream *dev_stream);
184 
dev_stream_is_running(struct dev_stream * dev_stream)185 static inline int dev_stream_is_running(struct dev_stream *dev_stream)
186 {
187 	return dev_stream->is_running;
188 }
189 
dev_stream_set_running(struct dev_stream * dev_stream)190 static inline void dev_stream_set_running(struct dev_stream *dev_stream)
191 {
192 	dev_stream->is_running = 1;
193 }
194 
195 static inline const struct timespec *
dev_stream_next_cb_ts(const struct dev_stream * dev_stream)196 dev_stream_next_cb_ts(const struct dev_stream *dev_stream)
197 {
198 	if (dev_stream->stream->flags & USE_DEV_TIMING)
199 		return NULL;
200 
201 	return &dev_stream->stream->next_cb_ts;
202 }
203 
204 static inline const struct timespec *
dev_stream_sleep_interval_ts(struct dev_stream * dev_stream)205 dev_stream_sleep_interval_ts(struct dev_stream *dev_stream)
206 {
207 	return &dev_stream->stream->sleep_interval_ts;
208 }
209 
210 int dev_stream_is_pending_reply(const struct dev_stream *dev_stream);
211 
212 /*
213  * Reads any pending audio message from the socket.
214  */
215 int dev_stream_flush_old_audio_messages(struct dev_stream *dev_stream);
216 
217 #endif /* DEV_STREAM_H_ */
218