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