17
17
* the use of this software.
21
* Because ALSA is not thread-safe (despite claims to the contrary) we use non-
22
* blocking output in the pump thread with the mutex locked, then unlock the
23
* mutex and wait for more room in the buffer with poll() while other threads
24
* lock the mutex and read the output time. We poll a pipe of our own as well
25
* as the ALSA file descriptors so that we can wake up the pump thread when
28
* When paused, or when it comes to the end of the data given it, the pump will
29
* wait on alsa_cond for the signal to continue. When it has more data waiting,
30
* however, it will be sitting in poll() waiting for ALSA's signal that more
31
* data can be written.
33
* * After adding more data to the buffer, and after resuming from pause,
34
* signal on alsa_cond to wake the pump. (There is no need to signal when
36
* * After setting the pump_quit flag, signal on alsa_cond AND the poll_pipe
37
* before joining the thread.
48
#include <alsa/asoundlib.h>
51
#include <audacious/audconfig.h>
52
#include <audacious/debug.h>
22
#define LEAST_BUFFER 100 /* milliseconds */
56
#define CHECK_VAL_RECOVER(value, function, ...) \
58
(value) = function (__VA_ARGS__); \
60
CHECK (snd_pcm_recover, alsa_handle, (value), 0); \
61
CHECK_VAL ((value), function, __VA_ARGS__); \
65
#define CHECK_RECOVER(function, ...) \
68
CHECK_VAL_RECOVER (error2, function, __VA_ARGS__); \
25
71
static snd_pcm_t * alsa_handle;
26
static GCond * alsa_cond;
27
static gboolean initted;
72
static char alsa_initted;
73
static pthread_mutex_t alsa_mutex = PTHREAD_MUTEX_INITIALIZER;
74
static pthread_cond_t alsa_cond = PTHREAD_COND_INITIALIZER;
29
76
static snd_pcm_format_t alsa_format;
30
static gint alsa_channels, alsa_rate;
77
static int alsa_channels, alsa_rate;
32
79
static void * alsa_buffer;
33
static gint alsa_buffer_length, alsa_buffer_data_start, alsa_buffer_data_length;
34
static gboolean read_locked;
36
static gint64 alsa_time; /* microseconds */
37
static gboolean alsa_paused;
38
static gint alsa_paused_time;
40
static gboolean pump_quit;
41
static GThread * pump_thread;
80
static int alsa_buffer_length, alsa_buffer_data_start, alsa_buffer_data_length;
81
static int alsa_period; /* milliseconds */
83
static int64_t alsa_written; /* frames */
84
static char alsa_prebuffer, alsa_paused;
85
static int alsa_paused_time; /* milliseconds */
87
static int poll_pipe[2];
88
static int poll_count;
89
static struct pollfd * poll_handles;
91
static char pump_quit;
92
static pthread_t pump_thread;
43
94
static snd_mixer_t * alsa_mixer;
44
95
static snd_mixer_elem_t * alsa_mixer_element;
97
static char poll_setup (void)
101
ERROR ("Failed to create pipe: %s.\n", strerror (errno));
105
if (fcntl (poll_pipe[0], F_SETFL, O_NONBLOCK))
107
ERROR ("Failed to set O_NONBLOCK on pipe: %s.\n", strerror (errno));
108
close (poll_pipe[0]);
109
close (poll_pipe[1]);
113
poll_count = 1 + snd_pcm_poll_descriptors_count (alsa_handle);
114
poll_handles = malloc (sizeof (struct pollfd) * poll_count);
115
poll_handles[0].fd = poll_pipe[0];
116
poll_handles[0].events = POLLIN;
117
poll_count = 1 + snd_pcm_poll_descriptors (alsa_handle, poll_handles + 1,
123
static void poll_sleep (void)
125
if (poll (poll_handles, poll_count, -1) < 0)
127
ERROR ("Failed to poll: %s.\n", strerror (errno));
131
if (poll_handles[0].revents & POLLIN)
134
while (read (poll_pipe[0], & c, 1) == 1)
139
static void poll_wake (void)
142
if (write (poll_pipe[1], & c, 1) < 0)
143
ERROR ("Failed to write to pipe: %s.\n", strerror (errno));
146
static void poll_cleanup (void)
148
close (poll_pipe[0]);
149
close (poll_pipe[1]);
46
153
static void * pump (void * unused)
48
snd_pcm_status_t * status;
51
g_mutex_lock (alsa_mutex);
53
g_cond_signal (alsa_cond);
55
snd_pcm_status_alloca (& status);
155
pthread_mutex_lock (& alsa_mutex);
156
pthread_cond_broadcast (& alsa_cond); /* signal thread started */
158
gboolean workaround = FALSE;
57
161
while (! pump_quit)
163
if (alsa_prebuffer || alsa_paused || ! snd_pcm_bytes_to_frames
164
(alsa_handle, alsa_buffer_data_length))
61
g_cond_wait (alsa_cond, alsa_mutex);
166
pthread_cond_wait (& alsa_cond, & alsa_mutex);
65
length = snd_pcm_frames_to_bytes (alsa_handle, LEAST_BUFFER / 2 *
171
CHECK_VAL_RECOVER (length, snd_pcm_avail_update, alsa_handle);
178
length = snd_pcm_frames_to_bytes (alsa_handle, length);
67
179
length = MIN (length, alsa_buffer_data_length);
68
180
length = MIN (length, alsa_buffer_length - alsa_buffer_data_start);
72
g_cond_wait (alsa_cond, alsa_mutex);
77
g_mutex_unlock (alsa_mutex);
79
length = snd_pcm_writei (alsa_handle, (gchar *) alsa_buffer +
80
alsa_buffer_data_start, snd_pcm_bytes_to_frames (alsa_handle, length));
82
g_mutex_lock (alsa_mutex);
86
if (! pump_quit && ! alsa_paused) /* ignore errors caused by drop */
87
CHECK (snd_pcm_recover, alsa_handle, length, 0);
93
length = snd_pcm_frames_to_bytes (alsa_handle, length);
95
alsa_buffer_data_start = (alsa_buffer_data_start + length) %
97
alsa_buffer_data_length -= length;
100
g_cond_signal (alsa_cond);
181
length = snd_pcm_bytes_to_frames (alsa_handle, length);
184
CHECK_VAL_RECOVER (written, snd_pcm_writei, alsa_handle, (char *)
185
alsa_buffer + alsa_buffer_data_start, length);
187
written = snd_pcm_frames_to_bytes (alsa_handle, written);
188
alsa_buffer_data_start += written;
189
alsa_buffer_data_length -= written;
191
pthread_cond_broadcast (& alsa_cond); /* signal write complete */
193
if (alsa_buffer_data_start == alsa_buffer_length)
195
alsa_buffer_data_start = 0;
199
if (! snd_pcm_bytes_to_frames (alsa_handle, alsa_buffer_data_length))
203
pthread_mutex_unlock (& alsa_mutex);
207
static gboolean warned = FALSE;
210
fprintf (stderr, "\n** WARNING **\nAudacious has detected that "
211
"your ALSA device has a broken timer. A workaround\nis being "
212
"used to prevent CPU overload. Please report this problem to "
213
"your\nLinux distributor or to the ALSA developers.\n\n");
220
if (workaround && slept)
222
const struct timespec delay = {.tv_sec = 0, .tv_nsec = 600000 *
224
nanosleep (& delay, NULL);
232
pthread_mutex_lock (& alsa_mutex);
103
g_mutex_unlock (alsa_mutex);
236
pthread_mutex_unlock (& alsa_mutex);
240
static void pump_start (void)
242
AUDDBG ("Starting pump.\n");
243
pthread_create (& pump_thread, NULL, pump, NULL);
244
pthread_cond_wait (& alsa_cond, & alsa_mutex);
247
static void pump_stop (void)
249
AUDDBG ("Stopping pump.\n");
251
pthread_cond_broadcast (& alsa_cond);
253
pthread_mutex_unlock (& alsa_mutex);
254
pthread_join (pump_thread, NULL);
255
pthread_mutex_lock (& alsa_mutex);
107
259
static void start_playback (void)
109
261
AUDDBG ("Starting playback.\n");
111
if (snd_pcm_state (alsa_handle) == SND_PCM_STATE_PAUSED)
112
CHECK (snd_pcm_pause, alsa_handle, 0);
114
CHECK (snd_pcm_prepare, alsa_handle);
118
g_cond_signal (alsa_cond);
121
static gint real_output_time (void)
123
snd_pcm_status_t * status;
126
snd_pcm_status_alloca (& status);
127
CHECK (snd_pcm_status, alsa_handle, status);
128
time = (alsa_time - (gint64) (snd_pcm_bytes_to_frames (alsa_handle,
129
alsa_buffer_data_length) + snd_pcm_status_get_delay (status)) * 1000000 /
262
CHECK (snd_pcm_prepare, alsa_handle);
266
pthread_cond_broadcast (& alsa_cond);
269
static int get_delay (void)
271
snd_pcm_sframes_t delay = 0;
273
CHECK_RECOVER (snd_pcm_delay, alsa_handle, & delay);
279
static int get_output_time (void)
281
return (int64_t) (alsa_written - snd_pcm_bytes_to_frames (alsa_handle,
282
alsa_buffer_data_length) - get_delay ()) * 1000 / alsa_rate;
136
285
OutputPluginInitStatus alsa_init (void)
138
alsa_mutex = g_mutex_new ();
139
287
alsa_handle = NULL;
140
alsa_cond = g_cond_new ();
143
289
return OUTPUT_PLUGIN_INIT_FOUND_DEVICES;
146
292
void alsa_soft_init (void)
294
pthread_mutex_lock (& alsa_mutex);
150
298
AUDDBG ("Initialize.\n");
151
299
alsa_config_load ();
152
300
alsa_open_mixer ();
304
pthread_mutex_unlock (& alsa_mutex);
157
307
void alsa_cleanup (void)
161
311
AUDDBG ("Cleanup.\n");
162
312
alsa_close_mixer ();
163
313
alsa_config_save ();
166
g_mutex_free (alsa_mutex);
167
g_cond_free (alsa_cond);
170
static snd_pcm_format_t convert_aud_format (AFormat aud_format)
317
static int convert_aud_format (int aud_format)
175
snd_pcm_format_t format;
321
int aud_format, format;
193
339
{FMT_U32_BE, SND_PCM_FORMAT_U32_BE},
198
for (count = 0; count < G_N_ELEMENTS (table); count ++)
342
for (int count = 0; count < G_N_ELEMENTS (table); count ++)
200
if (table[count].aud_format == aud_format)
201
return table[count].format;
344
if (table[count].aud_format == aud_format)
345
return table[count].format;
204
348
return SND_PCM_FORMAT_UNKNOWN;
207
gint alsa_open_audio (AFormat aud_format, gint rate, gint channels)
351
int alsa_open_audio (int aud_format, int rate, int channels)
209
snd_pcm_format_t format = convert_aud_format (aud_format);
210
snd_pcm_hw_params_t * params;
212
snd_pcm_uframes_t frames, period;
213
gint hard_buffer, soft_buffer;
215
g_mutex_lock (alsa_mutex);
216
353
alsa_soft_init ();
354
pthread_mutex_lock (& alsa_mutex);
356
assert (alsa_handle == NULL);
358
int format = convert_aud_format (aud_format);
218
359
AUDDBG ("Opening PCM device %s for %s, %d channels, %d Hz.\n",
219
360
alsa_config_pcm, snd_pcm_format_name (format), channels, rate);
220
361
CHECK_NOISY (snd_pcm_open, & alsa_handle, alsa_config_pcm,
221
362
SND_PCM_STREAM_PLAYBACK, 0);
364
snd_pcm_hw_params_t * params;
223
365
snd_pcm_hw_params_alloca (& params);
224
366
CHECK_NOISY (snd_pcm_hw_params_any, alsa_handle, params);
225
367
CHECK_NOISY (snd_pcm_hw_params_set_access, alsa_handle, params,
226
368
SND_PCM_ACCESS_RW_INTERLEAVED);
227
370
CHECK_NOISY (snd_pcm_hw_params_set_format, alsa_handle, params, format);
228
371
CHECK_NOISY (snd_pcm_hw_params_set_channels, alsa_handle, params, channels);
229
372
CHECK_NOISY (snd_pcm_hw_params_set_rate, alsa_handle, params, rate, 0);
230
useconds = 1000 * LEAST_BUFFER;
231
CHECK_NOISY (snd_pcm_hw_params_set_buffer_time_min, alsa_handle, params,
233
useconds = 1000 * MAX (LEAST_BUFFER * 11 / 10, aud_cfg->output_buffer_size /
235
CHECK_NOISY (snd_pcm_hw_params_set_buffer_time_max, alsa_handle, params,
237
CHECK_NOISY (snd_pcm_hw_params, alsa_handle, params);
239
374
alsa_format = format;
240
375
alsa_channels = channels;
241
376
alsa_rate = rate;
243
CHECK_NOISY (snd_pcm_get_params, alsa_handle, & frames, & period);
244
hard_buffer = (gint64) frames * 1000 / rate;
245
soft_buffer = MAX (LEAST_BUFFER, aud_cfg->output_buffer_size - hard_buffer);
246
AUDDBG ("Hardware buffer %d ms, software buffer %d ms.\n", hard_buffer,
249
alsa_buffer_length = snd_pcm_frames_to_bytes (alsa_handle, (gint64)
378
unsigned int useconds = 1000 * MIN (1000, aud_cfg->output_buffer_size / 2);
380
CHECK_NOISY (snd_pcm_hw_params_set_buffer_time_near, alsa_handle, params,
381
& useconds, & direction);
382
int hard_buffer = useconds / 1000;
384
useconds = 1000 * (hard_buffer / 4);
386
CHECK_NOISY (snd_pcm_hw_params_set_period_time_near, alsa_handle, params,
387
& useconds, & direction);
388
alsa_period = useconds / 1000;
390
CHECK_NOISY (snd_pcm_hw_params, alsa_handle, params);
392
int soft_buffer = MAX (aud_cfg->output_buffer_size / 2,
393
aud_cfg->output_buffer_size - hard_buffer);
394
AUDDBG ("Buffer: hardware %d ms, software %d ms, period %d ms.\n",
395
hard_buffer, soft_buffer, alsa_period);
397
alsa_buffer_length = snd_pcm_frames_to_bytes (alsa_handle, (int64_t)
250
398
soft_buffer * rate / 1000);
251
alsa_buffer = g_malloc (alsa_buffer_length);
399
alsa_buffer = malloc (alsa_buffer_length);
252
400
alsa_buffer_data_start = 0;
253
401
alsa_buffer_data_length = 0;
256
alsa_paused = TRUE; /* for buffering */
257
406
alsa_paused_time = 0;
260
pump_thread = g_thread_create (pump, NULL, TRUE, NULL);
261
g_cond_wait (alsa_cond, alsa_mutex);
263
g_mutex_unlock (alsa_mutex);
413
pthread_mutex_unlock (& alsa_mutex);
270
420
alsa_handle = NULL;
273
g_mutex_unlock (alsa_mutex);
423
pthread_mutex_unlock (& alsa_mutex);
277
427
void alsa_close_audio (void)
279
429
AUDDBG ("Closing audio.\n");
280
g_mutex_lock (alsa_mutex);
283
if (! alsa_config_drop_workaround)
284
CHECK (snd_pcm_drop, alsa_handle);
430
pthread_mutex_lock (& alsa_mutex);
432
assert (alsa_handle != NULL);
435
CHECK (snd_pcm_drop, alsa_handle);
287
g_cond_signal (alsa_cond);
288
g_mutex_unlock (alsa_mutex);
289
g_thread_join (pump_thread);
290
g_mutex_lock (alsa_mutex);
292
g_free (alsa_buffer);
293
440
snd_pcm_close (alsa_handle);
294
441
alsa_handle = NULL;
295
g_mutex_unlock (alsa_mutex);
298
void alsa_write_audio (void * data, gint length)
300
g_mutex_lock (alsa_mutex);
304
gint writable = MIN (alsa_buffer_length - alsa_buffer_data_length,
306
gint start = (alsa_buffer_data_start + alsa_buffer_data_length) %
309
if (writable > alsa_buffer_length - start)
443
pthread_mutex_unlock (& alsa_mutex);
446
int alsa_buffer_free (void)
448
pthread_mutex_lock (& alsa_mutex);
449
int avail = alsa_buffer_length - alsa_buffer_data_length;
450
pthread_mutex_unlock (& alsa_mutex);
454
void alsa_write_audio (void * data, int length)
456
pthread_mutex_lock (& alsa_mutex);
458
int start = (alsa_buffer_data_start + alsa_buffer_data_length) %
461
assert (length <= alsa_buffer_length - alsa_buffer_data_length);
463
if (length > alsa_buffer_length - start)
465
int part = alsa_buffer_length - start;
467
memcpy ((char *) alsa_buffer + start, data, part);
468
memcpy (alsa_buffer, (char *) data + part, length - part);
471
memcpy ((char *) alsa_buffer + start, data, length);
473
alsa_buffer_data_length += length;
474
alsa_written += snd_pcm_bytes_to_frames (alsa_handle, length);
477
pthread_cond_broadcast (& alsa_cond);
479
pthread_mutex_unlock (& alsa_mutex);
482
void alsa_period_wait (void)
484
pthread_mutex_lock (& alsa_mutex);
486
while (alsa_buffer_data_length == alsa_buffer_length)
311
gint part = alsa_buffer_length - start;
313
memcpy ((gint8 *) alsa_buffer + start, data, part);
314
memcpy (alsa_buffer, (gint8 *) data + part, writable - part);
493
pthread_cond_broadcast (& alsa_cond);
317
memcpy ((gint8 *) alsa_buffer + start, data, writable);
319
data = (gint8 *) data + writable;
322
alsa_buffer_data_length += writable;
323
alsa_time += (gint64) snd_pcm_bytes_to_frames (alsa_handle, writable) *
329
if (alsa_paused) /* buffering completed */
332
g_cond_signal (alsa_cond);
333
g_cond_wait (alsa_cond, alsa_mutex);
496
pthread_cond_wait (& alsa_cond, & alsa_mutex);
336
g_mutex_unlock (alsa_mutex);
499
pthread_mutex_unlock (& alsa_mutex);
339
502
void alsa_drain (void)
341
504
AUDDBG ("Drain.\n");
342
g_mutex_lock (alsa_mutex);
344
while (alsa_buffer_data_length > 0)
346
if (alsa_paused) /* buffering completed */
349
g_cond_wait (alsa_cond, alsa_mutex);
352
g_mutex_unlock (alsa_mutex);
354
CHECK (snd_pcm_drain, alsa_handle);
505
pthread_mutex_lock (& alsa_mutex);
507
assert (! alsa_paused);
512
while (snd_pcm_bytes_to_frames (alsa_handle, alsa_buffer_data_length))
513
pthread_cond_wait (& alsa_cond, & alsa_mutex);
517
if (alsa_config_drain_workaround)
519
int d = get_delay () * 1000 / alsa_rate;
520
struct timespec delay = {.tv_sec = d / 1000, .tv_nsec = d % 1000 *
523
pthread_mutex_unlock (& alsa_mutex);
524
nanosleep (& delay, NULL);
525
pthread_mutex_lock (& alsa_mutex);
532
CHECK_VAL (state, snd_pcm_state, alsa_handle);
534
if (state != SND_PCM_STATE_RUNNING && state !=
535
SND_PCM_STATE_DRAINING)
538
pthread_mutex_unlock (& alsa_mutex);
540
pthread_mutex_lock (& alsa_mutex);
547
pthread_mutex_unlock (& alsa_mutex);
360
void alsa_set_written_time (gint time)
551
void alsa_set_written_time (int time)
362
553
AUDDBG ("Setting time counter to %d.\n", time);
363
g_mutex_lock (alsa_mutex);
364
alsa_time = 1000 * (gint64) time;
365
g_mutex_unlock (alsa_mutex);
368
gint alsa_written_time (void)
372
g_mutex_lock (alsa_mutex);
373
time = alsa_time / 1000;
374
g_mutex_unlock (alsa_mutex);
378
gint alsa_output_time (void)
382
g_mutex_lock (alsa_mutex);
385
time = alsa_paused_time;
387
time = real_output_time ();
389
g_mutex_unlock (alsa_mutex);
393
void alsa_flush (gint time)
554
pthread_mutex_lock (& alsa_mutex);
555
alsa_written = (int64_t) time * alsa_rate / 1000;
556
pthread_mutex_unlock (& alsa_mutex);
559
int alsa_written_time (void)
561
pthread_mutex_lock (& alsa_mutex);
562
int time = (int64_t) alsa_written * 1000 / alsa_rate;
563
pthread_mutex_unlock (& alsa_mutex);
567
int alsa_output_time (void)
569
pthread_mutex_lock (& alsa_mutex);
570
int time = (alsa_prebuffer || alsa_paused) ? alsa_paused_time :
572
pthread_mutex_unlock (& alsa_mutex);
576
void alsa_flush (int time)
395
578
AUDDBG ("Seek requested; discarding buffer.\n");
396
g_mutex_lock (alsa_mutex);
398
alsa_time = (gint64) time * 1000;
399
alsa_paused = TRUE; /* for buffering */
579
pthread_mutex_lock (& alsa_mutex);
582
CHECK (snd_pcm_drop, alsa_handle);
585
alsa_buffer_data_start = 0;
586
alsa_buffer_data_length = 0;
588
alsa_written = (int64_t) time * alsa_rate / 1000;
400
590
alsa_paused_time = time;
402
if (! alsa_config_drop_workaround)
403
CHECK (snd_pcm_drop, alsa_handle);
407
g_cond_wait (alsa_cond, alsa_mutex);
409
alsa_buffer_data_start = 0;
410
alsa_buffer_data_length = 0;
412
g_cond_signal (alsa_cond);
413
g_mutex_unlock (alsa_mutex);
592
pthread_cond_broadcast (& alsa_cond); /* interrupt period wait */
596
pthread_mutex_unlock (& alsa_mutex);
416
void alsa_pause (gshort pause)
599
void alsa_pause (short pause)
418
601
AUDDBG ("%sause.\n", pause ? "P" : "Unp");
419
g_mutex_lock (alsa_mutex);
602
pthread_mutex_lock (& alsa_mutex);
606
if (! alsa_prebuffer)
609
alsa_paused_time = get_output_time ();
611
CHECK (snd_pcm_pause, alsa_handle, pause);
616
pthread_cond_broadcast (& alsa_cond);
618
pthread_mutex_unlock (& alsa_mutex);
622
AUDDBG ("Trying to work around broken pause.\n");
424
alsa_paused_time = real_output_time ();
426
CHECK (snd_pcm_pause, alsa_handle, pause);
430
g_cond_signal (alsa_cond);
431
g_mutex_unlock (alsa_mutex);
625
snd_pcm_drop (alsa_handle);
627
snd_pcm_prepare (alsa_handle);
434
632
void alsa_open_mixer (void)