1 #ifndef foomemblockqhfoo
2 #define foomemblockqhfoo
3
4 /***
5 This file is part of PulseAudio.
6
7 Copyright 2004-2006 Lennart Poettering
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
21 ***/
22 // GCC does not warn for unused *static inline* functions, but clang does.
23 #ifdef __clang__
24 #pragma clang diagnostic push
25 #pragma clang diagnostic ignored "-Wunused-function"
26 #endif
27
28 #include <sys/types.h>
29 #include <inttypes.h>
30
31 #include <pulsecore/mcalign.h>
32 #include <pulsecore/memblock.h>
33 #include <pulsecore/memchunk.h>
34 #include <pulse/def.h>
35
36 /* A memblockq is a queue of pa_memchunks (yep, the name is not
37 * perfect). It is similar to the ring buffers used by most other
38 * audio software. In contrast to a ring buffer this memblockq data
39 * type doesn't need to copy any data around, it just maintains
40 * references to reference counted memory blocks. */
41
42 typedef struct pa_memblockq pa_memblockq;
43
44 /* Parameters:
45
46 - name: name for debugging purposes
47
48 - idx: start value for both read and write index
49
50 - maxlength: maximum length of queue. If more data is pushed into
51 the queue, the operation will fail. Must not be 0.
52
53 - tlength: the target length of the queue. Pass 0 for the default.
54
55 - ss: Sample spec describing the queue contents. Only multiples
56 of the frame size as implied by the sample spec are
57 allowed into the queue or can be popped from it.
58
59 - prebuf: If the queue runs empty wait until this many bytes are in
60 queue again before passing the first byte out. If set
61 to 0 pa_memblockq_pop() will return a silence memblock
62 if no data is in the queue and will never fail. Pass
63 (size_t) -1 for the default.
64
65 - minreq: pa_memblockq_pop_missing() will only return values greater
66 than this value. Pass 0 for the default.
67
68 - maxrewind: how many bytes of history to keep in the queue
69
70 - silence: return this memchunk when reading uninitialized data
71 */
72 pa_memblockq* pa_memblockq_new(
73 const char *name,
74 int64_t idx,
75 size_t maxlength,
76 size_t tlength,
77 const pa_sample_spec *sample_spec,
78 size_t prebuf,
79 size_t minreq,
80 size_t maxrewind,
81 pa_memchunk *silence);
82
83 struct list_item {
84 struct list_item *next, *prev;
85 int64_t index;
86 pa_memchunk chunk;
87 };
88
89 PA_STATIC_FLIST_DECLARE(list_items, 0, pa_xfree);
90
91 struct pa_memblockq {
92 struct list_item *blocks, *blocks_tail;
93 struct list_item *current_read, *current_write;
94 unsigned n_blocks;
95 size_t maxlength, tlength, base, prebuf, minreq, maxrewind;
96 int64_t read_index, write_index;
97 bool in_prebuf;
98 pa_memchunk silence;
99 pa_mcalign *mcalign;
100 int64_t missing, requested;
101 char *name;
102 pa_sample_spec sample_spec;
103 };
104
drop_block(pa_memblockq * bq,struct list_item * q)105 static void drop_block(pa_memblockq *bq, struct list_item *q)
106 {
107 pa_assert(bq);
108 pa_assert(q);
109
110 pa_assert(bq->n_blocks >= 1);
111
112 if (q->prev) {
113 q->prev->next = q->next;
114 } else {
115 pa_assert(bq->blocks == q);
116 bq->blocks = q->next;
117 }
118
119 if (q->next) {
120 q->next->prev = q->prev;
121 } else {
122 pa_assert(bq->blocks_tail == q);
123 bq->blocks_tail = q->prev;
124 }
125
126 if (bq->current_write == q) {
127 bq->current_write = q->prev;
128 }
129
130 if (bq->current_read == q) {
131 bq->current_read = q->next;
132 }
133
134 pa_memblock_unref(q->chunk.memblock);
135
136 if (pa_flist_push(PA_STATIC_FLIST_GET(list_items), q) < 0) {
137 pa_xfree(q);
138 }
139
140 bq->n_blocks--;
141 }
142
drop_backlog(pa_memblockq * bq)143 static void drop_backlog(pa_memblockq *bq)
144 {
145 int64_t boundary;
146 pa_assert(bq);
147
148 boundary = bq->read_index - (int64_t) bq->maxrewind;
149
150 while (bq->blocks && (bq->blocks->index + (int64_t) bq->blocks->chunk.length <= boundary)) {
151 drop_block(bq, bq->blocks);
152 }
153 }
154
155 void pa_memblockq_free(pa_memblockq*bq);
156
157 /* Push a new memory chunk into the queue. */
158 int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *chunk);
159
160 /* Push a new memory chunk into the queue, but filter it through a
161 * pa_mcalign object. Don't mix this with pa_memblockq_seek() unless
162 * you know what you do. */
163 int pa_memblockq_push_align(pa_memblockq* bq, const pa_memchunk *chunk);
164
165 /* Manipulate the write pointer */
166 void pa_memblockq_seek(pa_memblockq *bq, int64_t offset, pa_seek_mode_t seek, bool account);
167
168 /* Return a copy of the next memory chunk in the queue. It is not
169 * removed from the queue. There are two reasons this function might
170 * fail: 1. prebuffering is active, 2. queue is empty and no silence
171 * memblock was passed at initialization. If the queue is not empty,
172 * but we're currently at a hole in the queue and no silence memblock
173 * was passed we return the length of the hole in chunk->length. */
174 int pa_memblockq_peek(pa_memblockq* bq, pa_memchunk *chunk);
175
176 /* Much like pa_memblockq_peek, but guarantees that the returned chunk
177 * will have a length of the block size passed. You must configure a
178 * silence memchunk for this memblockq if you use this call. */
179 int pa_memblockq_peek_fixed_size(pa_memblockq *bq, size_t block_size, pa_memchunk *chunk);
180
181 /* Drop the specified bytes from the queue. */
182 void pa_memblockq_drop(pa_memblockq *bq, size_t length);
183
184 /* Rewind the read index. If the history is shorter than the specified length we'll point to silence afterwards. */
185 void pa_memblockq_rewind(pa_memblockq *bq, size_t length);
186
187 /* Test if the pa_memblockq is currently readable, that is, more data than base */
188 bool pa_memblockq_is_readable(pa_memblockq *bq);
189
190 /* Return the length of the queue in bytes */
191 size_t pa_memblockq_get_length(pa_memblockq *bq);
192
193 /* Return the number of bytes that are missing since the last call to
194 * this function, reset the internal counter to 0. */
195 size_t pa_memblockq_pop_missing(pa_memblockq *bq);
196
197 /* Directly moves the data from the source memblockq into bq */
198 int pa_memblockq_splice(pa_memblockq *bq, pa_memblockq *source);
199
200 /* Set the queue to silence, set write index to read index */
201 void pa_memblockq_flush_write(pa_memblockq *bq, bool account);
202
203 /* Set the queue to silence, set write read index to write index*/
204 void pa_memblockq_flush_read(pa_memblockq *bq);
205
206 /* Ignore prebuf for now */
207 void pa_memblockq_prebuf_disable(pa_memblockq *bq);
208
209 /* Force prebuf */
210 void pa_memblockq_prebuf_force(pa_memblockq *bq);
211
212 /* Return the maximum length of the queue in bytes */
213 size_t pa_memblockq_get_maxlength(pa_memblockq *bq);
214
215 /* Get Target length */
216 size_t pa_memblockq_get_tlength(pa_memblockq *bq);
217
218 /* Return the prebuffer length in bytes */
219 size_t pa_memblockq_get_prebuf(pa_memblockq *bq);
220
221 /* Returns the minimal request value */
222 size_t pa_memblockq_get_minreq(pa_memblockq *bq);
223
224 /* Returns the maximal rewind value */
225 size_t pa_memblockq_get_maxrewind(pa_memblockq *bq);
226
227 /* Return the base unit in bytes */
228 size_t pa_memblockq_get_base(pa_memblockq *bq);
229
230 /* Return the current read index */
231 int64_t pa_memblockq_get_read_index(pa_memblockq *bq);
232
233 /* Return the current write index */
234 int64_t pa_memblockq_get_write_index(pa_memblockq *bq);
235
236 /* Change metrics. Always call in order. */
237 void pa_memblockq_set_maxlength(pa_memblockq *memblockq, size_t maxlength); /* might modify tlength, prebuf, minreq too */
238 void pa_memblockq_set_tlength(pa_memblockq *memblockq, size_t tlength); /* might modify minreq, too */
239 void pa_memblockq_set_minreq(pa_memblockq *memblockq, size_t minreq); /* might modify prebuf, too */
240 void pa_memblockq_set_prebuf(pa_memblockq *memblockq, size_t prebuf);
241 void pa_memblockq_set_maxrewind(pa_memblockq *memblockq, size_t maxrewind); /* Set the maximum history size */
242 void pa_memblockq_set_silence(pa_memblockq *memblockq, pa_memchunk *silence);
243
244 /* Apply the data from pa_buffer_attr */
245 void pa_memblockq_apply_attr(pa_memblockq *memblockq, const pa_buffer_attr *a);
246 void pa_memblockq_get_attr(pa_memblockq *bq, pa_buffer_attr *a);
247
248 /* Call pa_memchunk_will_need() for every chunk in the queue from the current read pointer to the end */
249 void pa_memblockq_willneed(pa_memblockq *bq);
250
251 /* Check whether the memblockq is completely empty, i.e. no data
252 * neither left nor right of the read pointer, and hence no buffered
253 * data for the future nor data in the backlog. */
254 bool pa_memblockq_is_empty(pa_memblockq *bq);
255
256 /* Drop everything in the queue, but don't modify the indexes */
257 void pa_memblockq_silence(pa_memblockq *bq);
258
259 /* Check whether we currently are in prebuf state */
260 bool pa_memblockq_prebuf_active(pa_memblockq *bq);
261
262 /* Return how many items are currently stored in the queue */
263 unsigned pa_memblockq_get_nblocks(pa_memblockq *bq);
264
265 #endif
266