-
Notifications
You must be signed in to change notification settings - Fork 38
/
out_buf.c
464 lines (367 loc) · 10.1 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* MOC - music on console
* Copyright (C) 2004,2005 Damian Pietras <[email protected]>
*
* 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.
*
*/
/* Defining OUT_TEST causes the raw audio samples to be written
* to the file 'out_test' in the current directory for debugging. */
/*#define OUT_TEST*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#ifdef OUT_TEST
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
/*#define DEBUG*/
#include "common.h"
#include "audio.h"
#include "log.h"
#include "fifo_buf.h"
#include "out_buf.h"
#include "options.h"
struct out_buf
{
struct fifo_buf *buf;
pthread_mutex_t mutex;
pthread_t tid; /* Thread id of the reading thread. */
/* Signals. */
pthread_cond_t play_cond; /* Something was written to the buffer. */
pthread_cond_t ready_cond; /* There is some space in the buffer. */
/* Optional callback called when there is some free space in
* the buffer. */
out_buf_free_callback *free_callback;
/* State flags of the buffer. */
int pause;
int exit; /* Exit when the buffer is empty. */
int stop; /* Don't play anything. */
int reset_dev; /* Request to the reading thread to reset the audio
device. */
float time; /* Time of played sound. */
int hardware_buf_fill; /* How the sound card buffer is filled. */
int read_thread_waiting; /* Is the read thread waiting for data? */
};
/* Don't play more than this value (in seconds) in one audio_play().
* This prevents locking. */
#define AUDIO_MAX_PLAY 0.1
#define AUDIO_MAX_PLAY_BYTES 32768
#ifdef OUT_TEST
static int fd;
#endif
static void set_realtime_prio ()
{
#ifdef HAVE_SCHED_GET_PRIORITY_MAX
int rc;
if (options_get_bool("UseRealtimePriority")) {
struct sched_param param;
param.sched_priority = sched_get_priority_max(SCHED_RR);
rc = pthread_setschedparam (pthread_self (), SCHED_RR, ¶m);
if (rc != 0)
log_errno ("Can't set realtime priority", rc);
}
#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 something in the buffer");
buf->read_thread_waiting = 1;
pthread_cond_wait (&buf->play_cond, &buf->mutex);
debug ("something appeared 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...");
xsleep (1, 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) {
int audio_bpf;
size_t play_buf_frames;
audio_bpf = audio_get_bpf();
play_buf_frames = MIN(audio_get_bps() * AUDIO_MAX_PLAY,
AUDIO_MAX_PLAY_BYTES) / audio_bpf;
play_buf_fill = fifo_buf_get(buf->buf, play_buf,
play_buf_frames * audio_bpf);
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);
#ifdef OUT_TEST
write (fd, play_buf + play_buf_pos, played);
#endif
play_buf_pos += played;
}
/*logit ("done sending PCM");*/
LOCK (buf->mutex);
/* Update time */
if (play_buf_fill && audio_get_bps())
buf->time += play_buf_fill / (float)audio_get_bps();
buf->hardware_buf_fill = audio_get_buf_fill();
}
}
UNLOCK (buf->mutex);
logit ("exiting");
return NULL;
}
/* Allocate and initialize the buf structure, size is the buffer size. */
struct out_buf *out_buf_new (int size)
{
int rc;
struct out_buf *buf;
assert (size > 0);
buf = xmalloc (sizeof (struct out_buf));
buf->buf = fifo_buf_new (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);
#ifdef OUT_TEST
fd = open ("out_test", O_CREAT | O_TRUNC | O_WRONLY, 0600);
#endif
rc = pthread_create (&buf->tid, NULL, read_thread, buf);
if (rc != 0)
fatal ("Can't create buffer thread: %s", xstrerror (rc));
return buf;
}
/* Wait for empty buffer, end playing, free resources allocated for the buf
* structure. Can be used only if nothing is played. */
void out_buf_free (struct out_buf *buf)
{
int rc;
assert (buf != NULL);
LOCK (buf->mutex);
buf->exit = 1;
pthread_cond_signal (&buf->play_cond);
UNLOCK (buf->mutex);
pthread_join (buf->tid, NULL);
/* Let other threads using this buffer know that the state of the
* buffer has changed. */
LOCK (buf->mutex);
fifo_buf_clear (buf->buf);
pthread_cond_broadcast (&buf->ready_cond);
UNLOCK (buf->mutex);
fifo_buf_free (buf->buf);
buf->buf = NULL;
rc = pthread_mutex_destroy (&buf->mutex);
if (rc != 0)
log_errno ("Destroying buffer mutex failed", rc);
rc = pthread_cond_destroy (&buf->play_cond);
if (rc != 0)
log_errno ("Destroying buffer play condition failed", rc);
rc = pthread_cond_destroy (&buf->ready_cond);
if (rc != 0)
log_errno ("Destroying buffer ready condition failed", rc);
free (buf);
logit ("buffer destroyed");
#ifdef OUT_TEST
close (fd);
#endif
}
/* 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);
}
/* Return the time in the audio which the user is currently hearing.
* If unplayed samples still remain in the hardware buffer from the
* previous audio then the value returned may be negative and it is
* up to the caller to handle this appropriately in the context of
* its own processing. */
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);
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");
}