dev_stream.h revision ed0e3ba0dc54a1ddd192ae571e15c0acebacbc81
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
17struct cras_audio_area;
18struct cras_fmt_conv;
19struct cras_rstream;
20
21/*
22 * Linked list of streams of audio from/to a client.
23 * Args:
24 *    dev_id - Index of the hw device.
25 *    stream - The rstream attached to a device.
26 *    conv - Sample rate or format converter.
27 *    conv_buffer - The buffer for converter if needed.
28 *    conv_buffer_size_frames - Size of conv_buffer in frames.
29 *    skip_mix - Don't mix this next time streams are mixed.
30 *    mix_offset - Current mix progress in the buffer.
31 */
32struct dev_stream {
33	unsigned int dev_id;
34	struct cras_rstream *stream;
35	struct cras_fmt_conv *conv;
36	struct byte_buffer *conv_buffer;
37	struct cras_audio_area *conv_area;
38	unsigned int conv_buffer_size_frames;
39	unsigned int skip_mix;
40	unsigned int mix_offset;
41	struct dev_stream *prev, *next;
42};
43
44struct dev_stream *dev_stream_create(struct cras_rstream *stream,
45				     unsigned int dev_id,
46				     const struct cras_audio_format *dev_fmt);
47void dev_stream_destroy(struct dev_stream *dev_stream);
48
49/*
50 * Renders count frames from shm into dst.  Updates count if anything is
51 * written. If it's muted and the only stream zero memory.
52 * Args:
53 *    dev_stream - The struct holding the stream to mix.
54 *    num_channels - The number of channels on the device.
55 *    dst - The destination buffer for mixing.
56 *    count - The number of frames written.
57 *    index - The index of the stream writing to this buffer.
58 */
59unsigned int dev_stream_mix(struct dev_stream *dev_stream,
60			    size_t num_channels,
61			    uint8_t *dst,
62			    size_t *count,
63			    size_t *index);
64
65/*
66 * Reads froms from the source into the dev_stream.
67 * Args:
68 *    dev_stream - The struct holding the stream to mix to.
69 *    area - The area to copy audio from.
70 *    index - The index of the buffer to copy to the dev stream.
71 */
72void dev_stream_capture(struct dev_stream *dev_stream,
73			const struct cras_audio_area *area,
74			unsigned int dev_index);
75
76/*
77 * Returns the number of playback frames queued in shared memory.  This is a
78 * post-format-conversion number.  If the stream is 24k with 10 frames queued
79 * and the device is playing at 48k, 20 will be returned.
80 */
81int dev_stream_playback_frames(const struct dev_stream *dev_stream);
82
83/*
84 * Returns the number of frames free to be written to in a capture stream.  This
85 * number is also post format conversion, similar to playback_frames above.
86 */
87unsigned int dev_stream_capture_avail(const struct dev_stream *dev_stream);
88
89/*
90 * If enough samples have been captured, post them to the client.
91 * TODO(dgreid) - see if this function can be eliminated.
92 */
93int dev_stream_capture_update_rstream(struct dev_stream *dev_stream);
94
95/* Fill ts with the time the playback sample will be played. */
96void cras_set_playback_timestamp(size_t frame_rate,
97				 size_t frames,
98				 struct cras_timespec *ts);
99
100/* Fill ts with the time the capture sample was recorded. */
101void cras_set_capture_timestamp(size_t frame_rate,
102				size_t frames,
103				struct cras_timespec *ts);
104
105/* Fill shm ts with the time the playback sample will be played or the capture
106 * sample was captured depending on the direction of the stream.
107 * Args:
108 *    delay_frames - The delay reproted by the device, in frames at the device's
109 *      sample rate.
110 */
111void dev_stream_set_delay(const struct dev_stream *dev_stream,
112			  unsigned int delay_frames);
113
114/* Ask the client for cb_threshold samples of audio to play. */
115int dev_stream_request_playback_samples(struct dev_stream *dev_stream);
116
117static inline const struct timespec *
118dev_stream_next_cb_ts(struct dev_stream *dev_stream)
119{
120	return &dev_stream->stream->next_cb_ts;
121}
122
123static inline const struct timespec *
124dev_stream_sleep_interval_ts(struct dev_stream *dev_stream)
125{
126	return &dev_stream->stream->sleep_interval_ts;
127}
128
129#endif /* DEV_STREAM_H_ */
130