-
Notifications
You must be signed in to change notification settings - Fork 1
/
out_buf.c
405 lines (320 loc) · 8.62 KB
/
out_buf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* MOC - music on console
* Copyright (C) 2004,2005 Damian Pietras <daper@daper.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
/*#define DEBUG*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "common.h"
#include "audio.h"
#include "log.h"
#include "fifo_buf.h"
#include "out_buf.h"
#include "options.h"
/* Don't play more than that value (in seconds) in one audio_play().
* This prevent locking. */
#define AUDIO_MAX_PLAY 0.1
#define AUDIO_MAX_PLAY_BYTES 32768
/*static int fd;*/
static void set_realtime_prio ()
{
#ifdef HAVE_SCHED_GET_PRIORITY_MAX
if (options_get_int("UseRealtimePriority")) {
struct sched_param param;
param.sched_priority = sched_get_priority_max(SCHED_RR);
if (pthread_setschedparam(pthread_self(), SCHED_RR, ¶m))
logit ("Can't set realtime priority: %s",
strerror(errno));
}
#else
logit ("No sched_get_priority_max() function: realtime priority not "
"used.");
#endif
}
/* Reading thread of the buffer. */
static void *read_thread (void *arg)
{
struct out_buf *buf = (struct out_buf *)arg;
int audio_dev_closed = 0;
logit ("entering output buffer thread");
set_realtime_prio ();
LOCK (buf->mutex);
while (1) {
int played = 0;
char play_buf[AUDIO_MAX_PLAY_BYTES];
int play_buf_fill;
int play_buf_pos = 0;
if (buf->reset_dev && !audio_dev_closed) {
audio_reset ();
buf->reset_dev = 0;
}
if (buf->stop)
fifo_buf_clear (&buf->buf);
if (buf->free_callback) {
/* unlock the mutex to make calls to out_buf functions
* possible in the callback */
UNLOCK (buf->mutex);
buf->free_callback ();
LOCK (buf->mutex);
}
debug ("sending the signal");
pthread_cond_broadcast (&buf->ready_cond);
if ((fifo_buf_get_fill(&buf->buf) == 0 || buf->pause
|| buf->stop)
&& !buf->exit) {
if (buf->pause && !audio_dev_closed) {
logit ("Closing the device due to pause");
audio_close ();
audio_dev_closed = 1;
}
debug ("waiting for someting in the buffer");
buf->read_thread_waiting = 1;
pthread_cond_wait (&buf->play_cond, &buf->mutex);
debug ("someting appeard in the buffer");
}
buf->read_thread_waiting = 0;
if (audio_dev_closed && !buf->pause) {
logit ("Opening the device again after pause");
if (!audio_open(NULL)) {
logit ("Can't reopen the device! sleeping...");
sleep (1); /* there is no way to exit :( */
}
else
audio_dev_closed = 0;
}
if (fifo_buf_get_fill(&buf->buf) == 0) {
if (buf->exit) {
logit ("exit");
break;
}
logit ("buffer empty");
continue;
}
if (buf->pause) {
logit ("paused");
continue;
}
if (buf->stop) {
logit ("stopped");
continue;
}
if (!audio_dev_closed) {
play_buf_fill = fifo_buf_get(&buf->buf, play_buf,
MIN(audio_get_bps() * AUDIO_MAX_PLAY,
AUDIO_MAX_PLAY_BYTES));
UNLOCK (buf->mutex);
debug ("playing %d bytes", play_buf_fill);
while (play_buf_pos < play_buf_fill) {
played = audio_send_pcm (
play_buf + play_buf_pos,
play_buf_fill - play_buf_pos);
play_buf_pos += played;
}
/*logit ("done sending PCM");*/
/*write (fd, buf->buf + buf->pos, to_play);*/
LOCK (buf->mutex);
/* Update time */
if (played && audio_get_bps())
buf->time += played / (float)audio_get_bps();
buf->hardware_buf_fill = audio_get_buf_fill();
}
}
UNLOCK (buf->mutex);
logit ("exiting");
return NULL;
}
/* Initialize the buf structure, size is the buffer size. */
void out_buf_init (struct out_buf *buf, int size)
{
assert (buf != NULL);
assert (size > 0);
fifo_buf_init (&buf->buf, size);
buf->exit = 0;
buf->pause = 0;
buf->stop = 0;
buf->time = 0.0;
buf->reset_dev = 0;
buf->hardware_buf_fill = 0;
buf->read_thread_waiting = 0;
buf->free_callback = NULL;
pthread_mutex_init (&buf->mutex, NULL);
pthread_cond_init (&buf->play_cond, NULL);
pthread_cond_init (&buf->ready_cond, NULL);
/*fd = open ("out_test", O_CREAT | O_TRUNC | O_WRONLY, 0600);*/
if (pthread_create(&buf->tid, NULL, read_thread, buf)) {
logit ("Can't create buffer thread: %s", strerror(errno));
fatal ("Can't create buffer thread");
}
}
/* Wait for empty buffer, end playing, free resources allocated for the buf
* structure. Can be used only if nothing is played */
void out_buf_destroy (struct out_buf *buf)
{
assert (buf != NULL);
LOCK (buf->mutex);
buf->exit = 1;
pthread_cond_signal (&buf->play_cond);
UNLOCK (buf->mutex);
pthread_join (buf->tid, NULL);
/* Let know other threads using this buffer that the state of the
* buffer is different. */
LOCK (buf->mutex);
fifo_buf_clear (&buf->buf);
pthread_cond_broadcast (&buf->ready_cond);
UNLOCK (buf->mutex);
fifo_buf_destroy (&buf->buf);
if (pthread_mutex_destroy(&buf->mutex))
logit ("Destroying buffer mutex failed: %s", strerror(errno));
if (pthread_cond_destroy(&buf->play_cond))
logit ("Destroying buffer play condition failed: %s",
strerror(errno));
if (pthread_cond_destroy(&buf->ready_cond))
logit ("Destroying buffer ready condition failed: %s",
strerror(errno));
logit ("buffer destroyed");
/*close (fd);*/
}
/* Put data at the end of the buffer, return 0 if nothing was put. */
int out_buf_put (struct out_buf *buf, const char *data, int size)
{
int pos = 0;
/*logit ("got %d bytes to play", size);*/
while (size) {
int written;
LOCK (buf->mutex);
if (fifo_buf_get_space(&buf->buf) == 0 && !buf->stop) {
/*logit ("buffer full, waiting for the signal");*/
pthread_cond_wait (&buf->ready_cond, &buf->mutex);
/*logit ("buffer ready");*/
}
if (buf->stop) {
logit ("the buffer is stopped, refusing to write to the buffer");
UNLOCK (buf->mutex);
return 0;
}
written = fifo_buf_put (&buf->buf, data + pos, size);
if (written) {
pthread_cond_signal (&buf->play_cond);
size -= written;
pos += written;
}
UNLOCK (buf->mutex);
}
return 1;
}
void out_buf_pause (struct out_buf *buf)
{
LOCK (buf->mutex);
buf->pause = 1;
buf->reset_dev = 1;
UNLOCK (buf->mutex);
}
void out_buf_unpause (struct out_buf *buf)
{
LOCK (buf->mutex);
buf->pause = 0;
pthread_cond_signal (&buf->play_cond);
UNLOCK (buf->mutex);
}
/* Stop playing, after that buffer will refuse to play anything and ignore data
* sent by buf_put(). */
void out_buf_stop (struct out_buf *buf)
{
logit ("stopping the buffer");
LOCK (buf->mutex);
buf->stop = 1;
buf->pause = 0;
buf->reset_dev = 1;
logit ("sending signal");
pthread_cond_signal (&buf->play_cond);
logit ("waiting for signal");
pthread_cond_wait (&buf->ready_cond, &buf->mutex);
logit ("done");
UNLOCK (buf->mutex);
}
/* Reset the buffer state: this can by called ONLY when the buffer is stopped
* and buf_put is not used! */
void out_buf_reset (struct out_buf *buf)
{
logit ("resetting the buffer");
LOCK (buf->mutex);
fifo_buf_clear (&buf->buf);
buf->stop = 0;
buf->pause = 0;
buf->reset_dev = 0;
buf->hardware_buf_fill = 0;
UNLOCK (buf->mutex);
}
void out_buf_time_set (struct out_buf *buf, const float time)
{
LOCK (buf->mutex);
buf->time = time;
UNLOCK (buf->mutex);
}
int out_buf_time_get (struct out_buf *buf)
{
int time;
int bps = audio_get_bps ();
LOCK (buf->mutex);
time = buf->time - (bps ? buf->hardware_buf_fill / (float)bps : 0);
UNLOCK (buf->mutex);
assert (time >= 0);
return time;
}
void out_buf_set_free_callback (struct out_buf *buf,
out_buf_free_callback callback)
{
assert (buf != NULL);
LOCK (buf->mutex);
buf->free_callback = callback;
UNLOCK (buf->mutex);
}
int out_buf_get_free (struct out_buf *buf)
{
int space;
assert (buf != NULL);
LOCK (buf->mutex);
space = fifo_buf_get_space (&buf->buf);
UNLOCK (buf->mutex);
return space;
}
int out_buf_get_fill (struct out_buf *buf)
{
int fill;
assert (buf != NULL);
LOCK (buf->mutex);
fill = fifo_buf_get_fill (&buf->buf);
UNLOCK (buf->mutex);
return fill;
}
/* Wait until the read thread will stop and wait for data to come.
* This makes sure that the audio device isn't used (of course only if you
* don't put anything in the buffer). */
void out_buf_wait (struct out_buf *buf)
{
assert (buf != NULL);
logit ("Waiting for read thread to suspend...");
LOCK (buf->mutex);
while (!buf->read_thread_waiting) {
debug ("waiting....");
pthread_cond_wait (&buf->ready_cond, &buf->mutex);
}
UNLOCK (buf->mutex);
logit ("done");
}