summarylogtreecommitdiffstats
path: root/0001-Pulseaudio-backend.patch
blob: 76277f9d40e72ad132ff6a890e94cad269cd55cc (plain)
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
From 61f9ecdfb20787e916a871c9632f6d375de3242c Mon Sep 17 00:00:00 2001
From: Vladimir Protasov <eoranged@ya.ru>
Date: Wed, 23 Jul 2014 01:47:39 +0400
Subject: [PATCH 1/3] Pulseaudio backend.

http://moc.daper.net/node/831
Thanks for marienz.

Signed-off-by: Hans-Nikolai Viessmann <hv15@hw.ac.uk>
---
 audio.c      |  12 +
 configure.in |  15 ++
 options.c    |   5 +-
 pulse.c      | 705 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 pulse.h      |  14 ++
 5 files changed, 749 insertions(+), 2 deletions(-)
 create mode 100644 pulse.c
 create mode 100644 pulse.h

diff --git a/audio.c b/audio.c
index 57f6a97..839cc8a 100644
--- a/audio.c
+++ b/audio.c
@@ -32,6 +32,9 @@
 #include "log.h"
 #include "lists.h"
 
+#ifdef HAVE_PULSE
+# include "pulse.h"
+#endif
 #ifdef HAVE_OSS
 # include "oss.h"
 #endif
@@ -887,6 +890,15 @@ static void find_working_driver (lists_t_strs *drivers, struct hw_funcs *funcs)
 		}
 #endif
 
+#ifdef HAVE_PULSE
+		if (!strcasecmp(name, "pulseaudio")) {
+			pulse_funcs (funcs);
+			printf ("Trying PulseAudio...\n");
+			if (funcs->init(&hw_caps))
+				return;
+		}
+#endif
+
 #ifdef HAVE_OSS
 		if (!strcasecmp(name, "oss")) {
 			oss_funcs (funcs);
diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -345,6 +345,9 @@
 #ifdef HAVE_SNDIO
 	printf (" SNDIO");
 #endif
+#ifdef HAVE_PULSE
+    printf(" PULSE");
+#endif
 #ifdef HAVE_ALSA
 	printf (" ALSA");
 #endif
diff --git a/configure.in b/configure.in
index 974403e..77432c8 100644
--- a/configure.in
+++ b/configure.in
@@ -157,6 +157,21 @@ then
 	                             [#include <db.h>]])
 fi
 
+AC_ARG_WITH(pulse, AS_HELP_STRING(--without-pulse,
+				  Compile without PulseAudio support.))
+
+if test "x$with_pulse" != "xno"
+then
+	PKG_CHECK_MODULES(PULSE, [libpulse],
+		 [SOUND_DRIVERS="$SOUND_DRIVERS PULSE"
+		  EXTRA_OBJS="$EXTRA_OBJS pulse.o"
+		  AC_DEFINE([HAVE_PULSE], 1, [Define if you have PulseAudio.])
+		  EXTRA_LIBS="$EXTRA_LIBS $PULSE_LIBS"
+		  CFLAGS="$CFLAGS $PULSE_CFLAGS"],
+		  [true])
+fi
+
+
 AC_ARG_WITH(oss, AS_HELP_STRING([--without-oss],
                                 [Compile without OSS support]))
 
diff --git a/options.c b/options.c
index 3ec4a28..496b1f8 100644
--- a/options.c
+++ b/options.c
@@ -567,10 +567,11 @@ void options_init ()
 
 #ifdef OPENBSD
 	add_list ("SoundDriver", "SNDIO:JACK:OSS",
-	          CHECK_DISCRETE(5), "SNDIO", "Jack", "ALSA", "OSS", "null");
+		  CHECK_DISCRETE(5), "SNDIO", "PulseAudio", "Jack", "ALSA", "OSS", "null");
+
 #else
 	add_list ("SoundDriver", "Jack:ALSA:OSS",
-	          CHECK_DISCRETE(5), "SNDIO", "Jack", "ALSA", "OSS", "null");
+	          CHECK_DISCRETE(5), "SNDIO", "PulseAudio", "Jack", "ALSA", "OSS", "null");
 #endif
 
 	add_str  ("JackClientName", "moc", CHECK_NONE);
diff --git a/pulse.c b/pulse.c
new file mode 100644
index 0000000..d5724dd
--- /dev/null
+++ b/pulse.c
@@ -0,0 +1,705 @@
+/*
+ * MOC - music on console
+ * Copyright (C) 2011 Marien Zwart <marienz@marienz.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.
+ *
+ */
+
+/* PulseAudio backend.
+ *
+ * FEATURES:
+ *
+ * Does not autostart a PulseAudio server, but uses an already-started
+ * one, which should be better than alsa-through-pulse.
+ *
+ * Supports control of either our stream's or our entire sink's volume
+ * while we are actually playing. Volume control while paused is
+ * intentionally unsupported: the PulseAudio documentation strongly
+ * suggests not passing in an initial volume when creating a stream
+ * (allowing the server to track this instead), and we do not know
+ * which sink to control if we do not have a stream open.
+ *
+ * IMPLEMENTATION:
+ *
+ * Most client-side (resource allocation) errors are fatal. Failure to
+ * create a server context or stream is not fatal (and MOC should cope
+ * with these failures too), but server communication failures later
+ * on are currently not handled (MOC has no great way for us to tell
+ * it we no longer work, and I am not sure if attempting to reconnect
+ * is worth it or even a good idea).
+ *
+ * The pulse "simple" API is too simple: it combines connecting to the
+ * server and opening a stream into one operation, while I want to
+ * connect to the server when MOC starts (and fall back to a different
+ * backend if there is no server), and I cannot open a stream at that
+ * time since I do not know the audio format yet.
+ *
+ * PulseAudio strongly recommends we use a high-latency connection,
+ * which the MOC frontend code might not expect from its audio
+ * backend. We'll see.
+ *
+ * We map MOC's percentage volumes linearly to pulse's PA_VOLUME_MUTED
+ * (0) .. PA_VOLUME_NORM range. This is what the PulseAudio docs recommend
+ * ( http://pulseaudio.org/wiki/WritingVolumeControlUIs ). It does mean
+ * PulseAudio volumes above PA_VOLUME_NORM do not work well with MOC.
+ *
+ * Comments in audio.h claim "All functions are executed only by one
+ * thread" (referring to the function in the hw_funcs struct). This is
+ * a blatant lie. Most of them are invoked off the "output buffer"
+ * thread (out_buf.c) but at least the "playing" thread (audio.c)
+ * calls audio_close which calls our close function. We can mostly
+ * ignore this problem because we serialize on the pulseaudio threaded
+ * mainloop lock. But it does mean that functions that are normally
+ * only called between open and close (like reset) are sometimes
+ * called without us having a stream. Bulletproof, therefore:
+ * serialize setting/unsetting our global stream using the threaded
+ * mainloop lock, and check for that stream being non-null before
+ * using it.
+ *
+ * I am not convinced there are no further dragons lurking here: can
+ * the "playing" thread(s) close and reopen our output stream while
+ * the "output buffer" thread is sending output there? We can bail if
+ * our stream is simply closed, but we do not currently detect it
+ * being reopened and no longer using the same sample format, which
+ * might have interesting results...
+ *
+ * Also, read_mixer is called from the main server thread (handling
+ * commands). This crashed me once when it got at a stream that was in
+ * the "creating" state and therefore did not have a valid stream
+ * index yet. Fixed by only assigning to the stream global when the
+ * stream is valid.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#define DEBUG
+
+#include <pulse/pulseaudio.h>
+#include "common.h"
+#include "log.h"
+#include "audio.h"
+
+
+/* The pulse mainloop and context are initialized in pulse_init and
+ * destroyed in pulse_shutdown.
+ */
+static pa_threaded_mainloop *mainloop = NULL;
+static pa_context *context = NULL;
+
+/* The stream is initialized in pulse_open and destroyed in pulse_close. */
+static pa_stream *stream = NULL;
+
+static int showing_sink_volume = 0;
+
+/* Callbacks that do nothing but wake up the mainloop. */
+
+static void context_state_callback (pa_context *context ATTR_UNUSED,
+				    void *userdata)
+{
+	pa_threaded_mainloop *m = userdata;
+
+	pa_threaded_mainloop_signal (m, 0);
+}
+
+static void stream_state_callback (pa_stream *stream ATTR_UNUSED,
+				   void *userdata)
+{
+	pa_threaded_mainloop *m = userdata;
+
+	pa_threaded_mainloop_signal (m, 0);
+}
+
+static void stream_write_callback (pa_stream *stream ATTR_UNUSED,
+				   size_t nbytes ATTR_UNUSED, void *userdata)
+{
+	pa_threaded_mainloop *m = userdata;
+
+	pa_threaded_mainloop_signal (m, 0);
+}
+
+/* Initialize pulse mainloop and context. Failure to connect to the
+ * pulse daemon is nonfatal, everything else is fatal (as it
+ * presumably means we ran out of resources).
+ */
+static int pulse_init (struct output_driver_caps *caps)
+{
+	pa_context *c;
+	pa_proplist *proplist;
+
+	assert (!mainloop);
+	assert (!context);
+
+	mainloop = pa_threaded_mainloop_new ();
+	if (!mainloop)
+		fatal ("Cannot create PulseAudio mainloop");
+
+	if (pa_threaded_mainloop_start (mainloop) < 0)
+		fatal ("Cannot start PulseAudio mainloop");
+
+	/* TODO: possibly add more props.
+	 *
+	 * There are a few we could set in proplist.h but nothing I
+	 * expect to be very useful.
+	 *
+	 * http://pulseaudio.org/wiki/ApplicationProperties recommends
+	 * setting at least application.name, icon.name and media.role.
+	 *
+	 * No need to set application.name here, the name passed to
+	 * pa_context_new_with_proplist overrides it.
+	 */
+	proplist = pa_proplist_new ();
+	if (!proplist)
+		fatal ("Cannot allocate PulseAudio proplist");
+
+	pa_proplist_sets (proplist,
+			  PA_PROP_APPLICATION_VERSION, PACKAGE_VERSION);
+	pa_proplist_sets (proplist, PA_PROP_MEDIA_ROLE, "music");
+	pa_proplist_sets (proplist, PA_PROP_APPLICATION_ID, "net.daper.moc");
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	c = pa_context_new_with_proplist (
+		pa_threaded_mainloop_get_api (mainloop),
+		PACKAGE_NAME, proplist);
+	pa_proplist_free (proplist);
+
+	if (!c)
+		fatal ("Cannot allocate PulseAudio context");
+
+	pa_context_set_state_callback (c, context_state_callback, mainloop);
+
+	/* Ignore return value, rely on state being set properly */
+	pa_context_connect (c, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
+
+	while (1) {
+		pa_context_state_t state = pa_context_get_state (c);
+
+		if (state == PA_CONTEXT_READY)
+			break;
+
+		if (!PA_CONTEXT_IS_GOOD (state)) {
+			error ("PulseAudio connection failed: %s",
+			       pa_strerror (pa_context_errno (c)));
+
+			goto unlock_and_fail;
+		}
+
+		debug ("waiting for context to become ready...");
+		pa_threaded_mainloop_wait (mainloop);
+	}
+
+	/* Only set the global now that the context is actually ready */
+	context = c;
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	/* We just make up the hardware capabilities, since pulse is
+	 * supposed to be abstracting these out. Assume pulse will
+	 * deal with anything we want to throw at it, and that we will
+	 * only want mono or stereo audio.
+	 */
+	caps->min_channels = 1;
+	caps->max_channels = 2;
+	caps->formats = (SFMT_S8 | SFMT_S16 | SFMT_S32 |
+			 SFMT_FLOAT | SFMT_BE | SFMT_LE);
+
+	return 1;
+
+unlock_and_fail:
+
+	pa_context_unref (c);
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	pa_threaded_mainloop_stop (mainloop);
+	pa_threaded_mainloop_free (mainloop);
+	mainloop = NULL;
+
+	return 0;
+}
+
+static void pulse_shutdown (void)
+{
+	pa_threaded_mainloop_lock (mainloop);
+
+	pa_context_disconnect (context);
+	pa_context_unref (context);
+	context = NULL;
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	pa_threaded_mainloop_stop (mainloop);
+	pa_threaded_mainloop_free (mainloop);
+	mainloop = NULL;
+}
+
+static int pulse_open (struct sound_params *sound_params)
+{
+	pa_sample_spec ss;
+	pa_buffer_attr ba;
+	pa_stream *s;
+
+	assert (!stream);
+	/* Initialize everything to -1, which in practice gets us
+	 * about 2 seconds of latency (which is fine). This is not the
+	 * same as passing NULL for this struct, which gets us an
+	 * unnecessarily short alsa-like latency.
+	 */
+	ba.fragsize = (uint32_t) -1;
+	ba.tlength = (uint32_t) -1;
+	ba.prebuf = (uint32_t) -1;
+	ba.minreq = (uint32_t) -1;
+	ba.maxlength = (uint32_t) -1;
+
+	ss.channels = sound_params->channels;
+	ss.rate = sound_params->rate;
+	switch (sound_params->fmt) {
+	case SFMT_U8:
+		ss.format = PA_SAMPLE_U8;
+		break;
+	case SFMT_S16 | SFMT_LE:
+		ss.format = PA_SAMPLE_S16LE;
+		break;
+	case SFMT_S16 | SFMT_BE:
+		ss.format = PA_SAMPLE_S16BE;
+		break;
+	case SFMT_FLOAT | SFMT_LE:
+		ss.format = PA_SAMPLE_FLOAT32LE;
+		break;
+	case SFMT_FLOAT | SFMT_BE:
+		ss.format = PA_SAMPLE_FLOAT32BE;
+		break;
+	case SFMT_S32 | SFMT_LE:
+		ss.format = PA_SAMPLE_S32LE;
+		break;
+	case SFMT_S32 | SFMT_BE:
+		ss.format = PA_SAMPLE_S32BE;
+		break;
+
+	default:
+		fatal ("pulse: got unrequested format");
+	}
+
+	debug ("opening stream");
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	/* TODO: figure out if there are useful stream properties to set.
+	 *
+	 * I do not really see any in proplist.h that we can set from
+	 * here (there are media title/artist/etc props but we do not
+	 * have that data available here).
+	 */
+	s = pa_stream_new (context, "music", &ss, NULL);
+	if (!s)
+		fatal ("pulse: stream allocation failed");
+
+	pa_stream_set_state_callback (s, stream_state_callback, mainloop);
+	pa_stream_set_write_callback (s, stream_write_callback, mainloop);
+
+	/* Ignore return value, rely on failed stream state instead. */
+	pa_stream_connect_playback (
+		s, NULL, &ba,
+		PA_STREAM_INTERPOLATE_TIMING |
+		PA_STREAM_AUTO_TIMING_UPDATE |
+		PA_STREAM_ADJUST_LATENCY,
+		NULL, NULL);
+
+	while (1) {
+		pa_stream_state_t state = pa_stream_get_state (s);
+
+		if (state == PA_STREAM_READY)
+			break;
+
+		if (!PA_STREAM_IS_GOOD (state)) {
+			error ("PulseAudio stream connection failed");
+
+			goto fail;
+		}
+
+		debug ("waiting for stream to become ready...");
+		pa_threaded_mainloop_wait (mainloop);
+	}
+
+	/* Only set the global stream now that it is actually ready */
+	stream = s;
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	return 1;
+
+fail:
+	pa_stream_unref (s);
+
+	pa_threaded_mainloop_unlock (mainloop);
+	return 0;
+}
+
+static void pulse_close (void)
+{
+	debug ("closing stream");
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	pa_stream_disconnect (stream);
+	pa_stream_unref (stream);
+	stream = NULL;
+
+	pa_threaded_mainloop_unlock (mainloop);
+}
+
+static int pulse_play (const char *buff, const size_t size)
+{
+	size_t offset = 0;
+
+	debug ("Got %d bytes to play", (int)size);
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	/* The buffer is usually writable when we get here, and there
+	 * are usually few (if any) writes after the first one. So
+	 * there is no point in doing further writes directly from the
+	 * callback: we can just do all writes from this thread.
+	 */
+
+	/* Break out of the loop if some other thread manages to close
+	 * our stream underneath us.
+	 */
+	while (stream) {
+		size_t towrite = MIN(pa_stream_writable_size (stream),
+				     size - offset);
+		debug ("writing %d bytes", (int)towrite);
+
+		/* We have no working way of dealing with errors
+		 * (see below). */
+		if (pa_stream_write(stream, buff + offset, towrite,
+				    NULL, 0, PA_SEEK_RELATIVE))
+			error ("pa_stream_write failed");
+
+		offset += towrite;
+
+		if (offset >= size)
+			break;
+
+		pa_threaded_mainloop_wait (mainloop);
+	}
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	debug ("Done playing!");
+
+	/* We should always return size, calling code does not deal
+	 * well with anything else. Only read the rest if you want to
+	 * know why.
+	 *
+	 * The output buffer reader thread (out_buf.c:read_thread)
+	 * repeatedly loads some 64k/0.1s of audio into a buffer on
+	 * the stack, then calls audio_send_pcm repeatedly until this
+	 * entire buffer has been processed (similar to the loop in
+	 * this function). audio_send_pcm applies the softmixer and
+	 * equalizer, then feeds the result to this function, passing
+	 * through our return value.
+	 *
+	 * So if we return less than size the equalizer/softmixer is
+	 * re-applied to the remaining data, which is silly. Also,
+	 * audio_send_pcm checks for our return value being zero and
+	 * calls fatal() if it is, so try to always process *some*
+	 * data. Also, out_buf.c uses the return value of this
+	 * function from the last run through its inner loop to update
+	 * its time attribute, which means it will be interestingly
+	 * off if that loop ran more than once.
+	 *
+	 * Oh, and alsa.c seems to think it can return -1 to indicate
+	 * failure, which will cause out_buf.c to rewind its buffer
+	 * (to before its start, usually).
+	 */
+	return size;
+}
+
+static void volume_cb (const pa_cvolume *v, void *userdata)
+{
+	int *result = userdata;
+
+	if (v)
+		*result = 100 * pa_cvolume_avg (v) / PA_VOLUME_NORM;
+
+	pa_threaded_mainloop_signal (mainloop, 0);
+}
+
+static void sink_volume_cb (pa_context *c ATTR_UNUSED,
+			    const pa_sink_info *i, int eol ATTR_UNUSED,
+			    void *userdata)
+{
+	volume_cb (i ? &i->volume : NULL, userdata);
+}
+
+static void sink_input_volume_cb (pa_context *c ATTR_UNUSED,
+				  const pa_sink_input_info *i,
+				  int eol ATTR_UNUSED,
+				  void *userdata ATTR_UNUSED)
+{
+	volume_cb (i ? &i->volume : NULL, userdata);
+}
+
+static int pulse_read_mixer (void)
+{
+	pa_operation *op;
+	int result = 0;
+
+	debug ("read mixer");
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	if (stream) {
+		if (showing_sink_volume)
+			op = pa_context_get_sink_info_by_index (
+				context, pa_stream_get_device_index (stream),
+				sink_volume_cb, &result);
+		else
+			op = pa_context_get_sink_input_info (
+				context, pa_stream_get_index (stream),
+				sink_input_volume_cb, &result);
+
+		while (pa_operation_get_state (op) == PA_OPERATION_RUNNING)
+			pa_threaded_mainloop_wait (mainloop);
+
+		pa_operation_unref (op);
+	}
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	return result;
+}
+
+static void pulse_set_mixer (int vol)
+{
+	pa_cvolume v;
+	pa_operation *op;
+
+	/* Setting volume for one channel does the right thing. */
+	pa_cvolume_set(&v, 1, vol * PA_VOLUME_NORM / 100);
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	if (stream) {
+		if (showing_sink_volume)
+			op = pa_context_set_sink_volume_by_index (
+				context, pa_stream_get_device_index (stream),
+				&v, NULL, NULL);
+		else
+			op = pa_context_set_sink_input_volume (
+				context, pa_stream_get_index (stream),
+				&v, NULL, NULL);
+
+		pa_operation_unref (op);
+	}
+
+	pa_threaded_mainloop_unlock (mainloop);
+}
+
+static int pulse_get_buff_fill (void)
+{
+	/* This function is problematic. MOC uses it to for the "time
+	 * remaining" in the UI, but calls it more than once per
+	 * second (after each chunk of audio played, not for each
+	 * playback time update). We have to be fairly accurate here
+	 * for that time remaining to not jump weirdly. But PulseAudio
+	 * cannot give us a 100% accurate value here, as it involves a
+	 * server roundtrip. And if we call this a lot it suggests
+	 * switching to a mode where the value is interpolated, making
+	 * it presumably more inaccurate (see the flags we pass to
+	 * pa_stream_connect_playback).
+	 *
+	 * MOC also contains what I believe to be a race: it calls
+	 * audio_get_buff_fill "soon" (after playing the first chunk)
+	 * after starting playback of the next song, at which point we
+	 * still have part of the previous song buffered. This means
+	 * our position into the new song is negative, which fails an
+	 * assert (in out_buf.c:out_buf_time_get). There is no sane
+	 * way for us to detect this condition. I believe no other
+	 * backend triggers this because the assert sits after an
+	 * implicit float -> int seconds conversion, which means we
+	 * have to be off by at least an entire second to get a
+	 * negative value, and none of the other backends have buffers
+	 * that large (alsa buffers are supposedly a few 100 ms).
+	 */
+	pa_usec_t buffered_usecs = 0;
+	int buffered_bytes = 0;
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	/* Using pa_stream_get_timing_info and returning the distance
+	 * between write_index and read_index would be more obvious,
+	 * but because of how the result is actually used I believe
+	 * using the latency value is slightly more correct, and it
+	 * makes the following crash-avoidance hack more obvious.
+	 */
+
+	/* This function will frequently fail the first time we call
+	 * it (pulse does not have the requested data yet). We ignore
+	 * that and just return 0.
+	 *
+	 * Deal with stream being NULL too, just in case this is
+	 * called in a racy fashion similar to how reset() is.
+	 */
+	if (stream &&
+	    pa_stream_get_latency (stream, &buffered_usecs, NULL) >= 0) {
+		/* Crash-avoidance HACK: floor our latency to at most
+		 * 1 second. It is usually more, but reporting that at
+		 * the start of playback crashes MOC, and we cannot
+		 * sanely detect when reporting it is safe.
+		 */
+		if (buffered_usecs > 1000000)
+			buffered_usecs = 1000000;
+
+		buffered_bytes = pa_usec_to_bytes (
+			buffered_usecs,
+			pa_stream_get_sample_spec (stream));
+	}
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	debug ("buffer fill: %d usec / %d bytes",
+	       (int) buffered_usecs, (int) buffered_bytes);
+
+	return buffered_bytes;
+}
+
+static void flush_callback (pa_stream *s ATTR_UNUSED, int success,
+			    void *userdata)
+{
+	int *result = userdata;
+
+	*result = success;
+
+	pa_threaded_mainloop_signal (mainloop, 0);
+}
+
+static int pulse_reset (void)
+{
+	pa_operation *op;
+	int result = 0;
+
+	debug ("reset requested");
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	/* We *should* have a stream here, but MOC is racy, so bulletproof */
+	if (stream) {
+		op = pa_stream_flush (stream, flush_callback, &result);
+
+		while (pa_operation_get_state (op) == PA_OPERATION_RUNNING)
+			pa_threaded_mainloop_wait (mainloop);
+
+		pa_operation_unref (op);
+	} else
+		logit ("pulse_reset() called without a stream");
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	return result;
+}
+
+static int pulse_get_rate (void)
+{
+	/* This is called once right after open. Do not bother making
+	 * this fast. */
+
+	int result;
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	if (stream)
+		result = pa_stream_get_sample_spec (stream)->rate;
+	else {
+		error ("get_rate called without a stream");
+		result = 0;
+	}
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	return result;
+}
+
+static void pulse_toggle_mixer_channel (void)
+{
+	showing_sink_volume = !showing_sink_volume;
+}
+
+static void sink_name_cb (pa_context *c ATTR_UNUSED,
+			  const pa_sink_info *i, int eol ATTR_UNUSED,
+			  void *userdata)
+{
+	char **result = userdata;
+
+	if (i && !*result)
+		*result = xstrdup (i->name);
+
+	pa_threaded_mainloop_signal (mainloop, 0);
+}
+
+static void sink_input_name_cb (pa_context *c ATTR_UNUSED,
+				const pa_sink_input_info *i,
+				int eol ATTR_UNUSED,
+				void *userdata)
+{
+	char **result = userdata;
+
+	if (i && !*result)
+		*result = xstrdup (i->name);
+
+	pa_threaded_mainloop_signal (mainloop, 0);
+}
+
+static char *pulse_get_mixer_channel_name (void)
+{
+	char *result = NULL;
+	pa_operation *op;
+
+	pa_threaded_mainloop_lock (mainloop);
+
+	if (stream) {
+		if (showing_sink_volume)
+			op = pa_context_get_sink_info_by_index (
+				context, pa_stream_get_device_index (stream),
+				sink_name_cb, &result);
+		else
+			op = pa_context_get_sink_input_info (
+				context, pa_stream_get_index (stream),
+				sink_input_name_cb, &result);
+
+		while (pa_operation_get_state (op) == PA_OPERATION_RUNNING)
+			pa_threaded_mainloop_wait (mainloop);
+
+		pa_operation_unref (op);
+	}
+
+	pa_threaded_mainloop_unlock (mainloop);
+
+	if (!result)
+		result = xstrdup ("disconnected");
+
+	return result;
+}
+
+void pulse_funcs (struct hw_funcs *funcs)
+{
+	funcs->init = pulse_init;
+	funcs->shutdown = pulse_shutdown;
+	funcs->open = pulse_open;
+	funcs->close = pulse_close;
+	funcs->play = pulse_play;
+	funcs->read_mixer = pulse_read_mixer;
+	funcs->set_mixer = pulse_set_mixer;
+	funcs->get_buff_fill = pulse_get_buff_fill;
+	funcs->reset = pulse_reset;
+	funcs->get_rate = pulse_get_rate;
+	funcs->toggle_mixer_channel = pulse_toggle_mixer_channel;
+	funcs->get_mixer_channel_name = pulse_get_mixer_channel_name;
+}
diff --git a/pulse.h b/pulse.h
new file mode 100644
index 0000000..7f99067
--- /dev/null
+++ b/pulse.h
@@ -0,0 +1,14 @@
+#ifndef PULSE_H
+#define PULSE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void pulse_funcs (struct hw_funcs *funcs);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
-- 
2.12.1