PulseOutputPlugin.cxx 20.3 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21
#include "PulseOutputPlugin.hxx"
22
#include "OutputAPI.hxx"
23
#include "MixerList.hxx"
24
#include "mixer/PulseMixerPlugin.hxx"
25 26
#include "util/Error.hxx"
#include "util/Domain.hxx"
27
#include "Log.hxx"
28

29
#include <glib.h>
30

31 32 33
#include <pulse/thread-mainloop.h>
#include <pulse/context.h>
#include <pulse/stream.h>
34 35
#include <pulse/introspect.h>
#include <pulse/subscribe.h>
36
#include <pulse/error.h>
37
#include <pulse/version.h>
38

39
#include <assert.h>
40
#include <stddef.h>
41

42
#define MPD_PULSE_NAME "Music Player Daemon"
43

44
struct PulseOutput {
45 46
	struct audio_output base;

47 48 49 50
	const char *name;
	const char *server;
	const char *sink;

51
	PulseMixer *mixer;
52 53 54 55 56 57 58 59

	struct pa_threaded_mainloop *mainloop;
	struct pa_context *context;
	struct pa_stream *stream;

	size_t writable;
};

60 61 62 63
static constexpr Domain pulse_output_domain("pulse_output");

static void
SetError(Error &error, pa_context *context, const char *msg)
64
{
65 66
	const int e = pa_context_errno(context);
	error.Format(pulse_output_domain, e, "%s: %s", msg, pa_strerror(e));
67 68
}

69
void
70
pulse_output_lock(PulseOutput *po)
71 72 73 74 75
{
	pa_threaded_mainloop_lock(po->mainloop);
}

void
76
pulse_output_unlock(PulseOutput *po)
77 78 79 80
{
	pa_threaded_mainloop_unlock(po->mainloop);
}

81
void
82
pulse_output_set_mixer(PulseOutput *po, PulseMixer *pm)
83
{
84 85 86
	assert(po != nullptr);
	assert(po->mixer == nullptr);
	assert(pm != nullptr);
87 88 89

	po->mixer = pm;

90
	if (po->mainloop == nullptr)
91 92
		return;

93 94
	pa_threaded_mainloop_lock(po->mainloop);

95
	if (po->context != nullptr &&
96 97 98
	    pa_context_get_state(po->context) == PA_CONTEXT_READY) {
		pulse_mixer_on_connect(pm, po->context);

99
		if (po->stream != nullptr &&
100 101 102 103 104 105 106 107
		    pa_stream_get_state(po->stream) == PA_STREAM_READY)
			pulse_mixer_on_change(pm, po->context, po->stream);
	}

	pa_threaded_mainloop_unlock(po->mainloop);
}

void
108
pulse_output_clear_mixer(PulseOutput *po, gcc_unused PulseMixer *pm)
109
{
110 111
	assert(po != nullptr);
	assert(pm != nullptr);
112 113
	assert(po->mixer == pm);

114
	po->mixer = nullptr;
115 116 117
}

bool
118 119
pulse_output_set_volume(PulseOutput *po, const pa_cvolume *volume,
			Error &error)
120 121 122
{
	pa_operation *o;

123
	if (po->context == nullptr || po->stream == nullptr ||
124
	    pa_stream_get_state(po->stream) != PA_STREAM_READY) {
125
		error.Set(pulse_output_domain, "disconnected");
126 127 128 129 130
		return false;
	}

	o = pa_context_set_sink_input_volume(po->context,
					     pa_stream_get_index(po->stream),
131 132
					     volume, nullptr, nullptr);
	if (o == nullptr) {
133 134
		SetError(error, po->context,
			 "failed to set PulseAudio volume");
135 136 137 138 139 140 141
		return false;
	}

	pa_operation_unref(o);
	return true;
}

142 143 144 145 146 147 148 149 150 151 152 153 154
/**
 * \brief waits for a pulseaudio operation to finish, frees it and
 *     unlocks the mainloop
 * \param operation the operation to wait for
 * \return true if operation has finished normally (DONE state),
 *     false otherwise
 */
static bool
pulse_wait_for_operation(struct pa_threaded_mainloop *mainloop,
			 struct pa_operation *operation)
{
	pa_operation_state_t state;

155 156
	assert(mainloop != nullptr);
	assert(operation != nullptr);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

	state = pa_operation_get_state(operation);
	while (state == PA_OPERATION_RUNNING) {
		pa_threaded_mainloop_wait(mainloop);
		state = pa_operation_get_state(operation);
	}

	pa_operation_unref(operation);

	return state == PA_OPERATION_DONE;
}

/**
 * Callback function for stream operation.  It just sends a signal to
 * the caller thread, to wake pulse_wait_for_operation() up.
 */
static void
174 175
pulse_output_stream_success_cb(gcc_unused pa_stream *s,
			       gcc_unused int success, void *userdata)
176
{
177
	PulseOutput *po = (PulseOutput *)userdata;
178 179 180 181

	pa_threaded_mainloop_signal(po->mainloop, 0);
}

182
static void
183 184
pulse_output_context_state_cb(struct pa_context *context, void *userdata)
{
185
	PulseOutput *po = (PulseOutput *)userdata;
186 187 188

	switch (pa_context_get_state(context)) {
	case PA_CONTEXT_READY:
189
		if (po->mixer != nullptr)
190 191 192 193 194
			pulse_mixer_on_connect(po->mixer, context);

		pa_threaded_mainloop_signal(po->mainloop, 0);
		break;

195 196
	case PA_CONTEXT_TERMINATED:
	case PA_CONTEXT_FAILED:
197
		if (po->mixer != nullptr)
198 199
			pulse_mixer_on_disconnect(po->mixer);

200 201 202 203 204 205 206 207 208 209 210 211 212
		/* the caller thread might be waiting for these
		   states */
		pa_threaded_mainloop_signal(po->mainloop, 0);
		break;

	case PA_CONTEXT_UNCONNECTED:
	case PA_CONTEXT_CONNECTING:
	case PA_CONTEXT_AUTHORIZING:
	case PA_CONTEXT_SETTING_NAME:
		break;
	}
}

213 214 215 216 217
static void
pulse_output_subscribe_cb(pa_context *context,
			  pa_subscription_event_type_t t,
			  uint32_t idx, void *userdata)
{
218 219 220 221 222
	PulseOutput *po = (PulseOutput *)userdata;
	pa_subscription_event_type_t facility =
		pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK);
	pa_subscription_event_type_t type =
		pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_TYPE_MASK);
223

224
	if (po->mixer != nullptr &&
225
	    facility == PA_SUBSCRIPTION_EVENT_SINK_INPUT &&
226
	    po->stream != nullptr &&
227 228 229 230 231 232 233
	    pa_stream_get_state(po->stream) == PA_STREAM_READY &&
	    idx == pa_stream_get_index(po->stream) &&
	    (type == PA_SUBSCRIPTION_EVENT_NEW ||
	     type == PA_SUBSCRIPTION_EVENT_CHANGE))
		pulse_mixer_on_change(po->mixer, context, po->stream);
}

234 235 236 237 238 239
/**
 * Attempt to connect asynchronously to the PulseAudio server.
 *
 * @return true on success, false on error
 */
static bool
240
pulse_output_connect(PulseOutput *po, Error &error)
241
{
242 243
	assert(po != nullptr);
	assert(po->context != nullptr);
244

245 246 247 248
	if (pa_context_connect(po->context, po->server,
			       (pa_context_flags_t)0, nullptr) < 0) {
		SetError(error, po->context,
			 "pa_context_connect() has failed");
249 250
		return false;
	}
251

252 253
	return true;
}
254

255 256 257 258
/**
 * Frees and clears the stream.
 */
static void
259
pulse_output_delete_stream(PulseOutput *po)
260
{
261 262
	assert(po != nullptr);
	assert(po->stream != nullptr);
263

264
	pa_stream_set_suspended_callback(po->stream, nullptr, nullptr);
265

266 267
	pa_stream_set_state_callback(po->stream, nullptr, nullptr);
	pa_stream_set_write_callback(po->stream, nullptr, nullptr);
268

269 270
	pa_stream_disconnect(po->stream);
	pa_stream_unref(po->stream);
271
	po->stream = nullptr;
272 273
}

274 275
/**
 * Frees and clears the context.
276 277
 *
 * Caller must lock the main loop.
278 279
 */
static void
280
pulse_output_delete_context(PulseOutput *po)
281
{
282 283
	assert(po != nullptr);
	assert(po->context != nullptr);
284

285 286
	pa_context_set_state_callback(po->context, nullptr, nullptr);
	pa_context_set_subscribe_callback(po->context, nullptr, nullptr);
287

288 289
	pa_context_disconnect(po->context);
	pa_context_unref(po->context);
290
	po->context = nullptr;
291 292
}

293 294 295
/**
 * Create, set up and connect a context.
 *
296 297
 * Caller must lock the main loop.
 *
298 299 300
 * @return true on success, false on error
 */
static bool
301
pulse_output_setup_context(PulseOutput *po, Error &error)
302
{
303 304
	assert(po != nullptr);
	assert(po->mainloop != nullptr);
305

306 307
	po->context = pa_context_new(pa_threaded_mainloop_get_api(po->mainloop),
				     MPD_PULSE_NAME);
308
	if (po->context == nullptr) {
309
		error.Set(pulse_output_domain, "pa_context_new() has failed");
310 311 312 313 314
		return false;
	}

	pa_context_set_state_callback(po->context,
				      pulse_output_context_state_cb, po);
315 316
	pa_context_set_subscribe_callback(po->context,
					  pulse_output_subscribe_cb, po);
317

318
	if (!pulse_output_connect(po, error)) {
319
		pulse_output_delete_context(po);
320 321 322 323
		return false;
	}

	return true;
324 325
}

326
static struct audio_output *
327
pulse_output_init(const config_param &param, Error &error)
328
{
329
	PulseOutput *po;
330

331 332
	g_setenv("PULSE_PROP_media.role", "music", true);

333
	po = new PulseOutput();
334
	if (!ao_base_init(&po->base, &pulse_output_plugin, param, error)) {
335 336
		delete po;
		return nullptr;
337 338
	}

339 340 341
	po->name = param.GetBlockValue("name", "mpd_pulse");
	po->server = param.GetBlockValue("server");
	po->sink = param.GetBlockValue("sink");
342

343 344 345 346
	po->mixer = nullptr;
	po->mainloop = nullptr;
	po->context = nullptr;
	po->stream = nullptr;
347

348
	return &po->base;
349 350 351
}

static void
352
pulse_output_finish(struct audio_output *ao)
353
{
354
	PulseOutput *po = (PulseOutput *)ao;
355

356
	ao_base_finish(&po->base);
357
	delete po;
358 359 360
}

static bool
361
pulse_output_enable(struct audio_output *ao, Error &error)
362
{
363
	PulseOutput *po = (PulseOutput *)ao;
364

365 366
	assert(po->mainloop == nullptr);
	assert(po->context == nullptr);
367

368 369 370
	/* create the libpulse mainloop and start the thread */

	po->mainloop = pa_threaded_mainloop_new();
371
	if (po->mainloop == nullptr) {
372 373
		g_free(po);

374 375
		error.Set(pulse_output_domain,
			  "pa_threaded_mainloop_new() has failed");
376
		return false;
377 378 379 380 381 382 383
	}

	pa_threaded_mainloop_lock(po->mainloop);

	if (pa_threaded_mainloop_start(po->mainloop) < 0) {
		pa_threaded_mainloop_unlock(po->mainloop);
		pa_threaded_mainloop_free(po->mainloop);
384
		po->mainloop = nullptr;
385

386 387
		error.Set(pulse_output_domain,
			  "pa_threaded_mainloop_start() has failed");
388 389 390 391 392
		return false;
	}

	/* create the libpulse context and connect it */

393
	if (!pulse_output_setup_context(po, error)) {
394 395 396
		pa_threaded_mainloop_unlock(po->mainloop);
		pa_threaded_mainloop_stop(po->mainloop);
		pa_threaded_mainloop_free(po->mainloop);
397
		po->mainloop = nullptr;
398
		return false;
399 400 401 402
	}

	pa_threaded_mainloop_unlock(po->mainloop);

403
	return true;
404 405
}

406
static void
407
pulse_output_disable(struct audio_output *ao)
408
{
409
	PulseOutput *po = (PulseOutput *)ao;
410

411
	assert(po->mainloop != nullptr);
412

413
	pa_threaded_mainloop_stop(po->mainloop);
414
	if (po->context != nullptr)
415 416
		pulse_output_delete_context(po);
	pa_threaded_mainloop_free(po->mainloop);
417
	po->mainloop = nullptr;
418 419
}

420 421 422 423
/**
 * Check if the context is (already) connected, and waits if not.  If
 * the context has been disconnected, retry to connect.
 *
424 425
 * Caller must lock the main loop.
 *
426 427 428
 * @return true on success, false on error
 */
static bool
429
pulse_output_wait_connection(PulseOutput *po, Error &error)
430
{
431
	assert(po->mainloop != nullptr);
432

433
	pa_context_state_t state;
434

435
	if (po->context == nullptr && !pulse_output_setup_context(po, error))
436
		return false;
437 438 439 440 441 442 443 444 445 446 447 448

	while (true) {
		state = pa_context_get_state(po->context);
		switch (state) {
		case PA_CONTEXT_READY:
			/* nothing to do */
			return true;

		case PA_CONTEXT_UNCONNECTED:
		case PA_CONTEXT_TERMINATED:
		case PA_CONTEXT_FAILED:
			/* failure */
449
			SetError(error, po->context, "failed to connect");
450 451 452 453 454 455 456 457 458 459
			pulse_output_delete_context(po);
			return false;

		case PA_CONTEXT_CONNECTING:
		case PA_CONTEXT_AUTHORIZING:
		case PA_CONTEXT_SETTING_NAME:
			/* wait some more */
			pa_threaded_mainloop_wait(po->mainloop);
			break;
		}
460
	}
461
}
462

463
static void
464
pulse_output_stream_suspended_cb(gcc_unused pa_stream *stream, void *userdata)
465
{
466
	PulseOutput *po = (PulseOutput *)userdata;
467

468 469
	assert(stream == po->stream || po->stream == nullptr);
	assert(po->mainloop != nullptr);
470 471 472 473 474 475

	/* wake up the main loop to break out of the loop in
	   pulse_output_play() */
	pa_threaded_mainloop_signal(po->mainloop, 0);
}

476 477 478
static void
pulse_output_stream_state_cb(pa_stream *stream, void *userdata)
{
479
	PulseOutput *po = (PulseOutput *)userdata;
480

481 482 483
	assert(stream == po->stream || po->stream == nullptr);
	assert(po->mainloop != nullptr);
	assert(po->context != nullptr);
484

485 486
	switch (pa_stream_get_state(stream)) {
	case PA_STREAM_READY:
487
		if (po->mixer != nullptr)
488 489 490 491 492
			pulse_mixer_on_change(po->mixer, po->context, stream);

		pa_threaded_mainloop_signal(po->mainloop, 0);
		break;

493 494
	case PA_STREAM_FAILED:
	case PA_STREAM_TERMINATED:
495
		if (po->mixer != nullptr)
496 497
			pulse_mixer_on_disconnect(po->mixer);

498 499 500 501 502 503 504 505
		pa_threaded_mainloop_signal(po->mainloop, 0);
		break;

	case PA_STREAM_UNCONNECTED:
	case PA_STREAM_CREATING:
		break;
	}
}
506

507
static void
508
pulse_output_stream_write_cb(gcc_unused pa_stream *stream, size_t nbytes,
509 510
			     void *userdata)
{
511
	PulseOutput *po = (PulseOutput *)userdata;
512

513
	assert(po->mainloop != nullptr);
514

515 516
	po->writable = nbytes;
	pa_threaded_mainloop_signal(po->mainloop, 0);
517 518
}

519 520 521 522 523 524 525 526
/**
 * Create, set up and connect a context.
 *
 * Caller must lock the main loop.
 *
 * @return true on success, false on error
 */
static bool
527
pulse_output_setup_stream(PulseOutput *po, const pa_sample_spec *ss,
528
			  Error &error)
529
{
530 531
	assert(po != nullptr);
	assert(po->context != nullptr);
532

533 534
	po->stream = pa_stream_new(po->context, po->name, ss, nullptr);
	if (po->stream == nullptr) {
535
		SetError(error, po->context, "pa_stream_new() has failed");
536 537 538 539 540 541 542 543 544 545 546 547 548 549
		return false;
	}

	pa_stream_set_suspended_callback(po->stream,
					 pulse_output_stream_suspended_cb, po);

	pa_stream_set_state_callback(po->stream,
				     pulse_output_stream_state_cb, po);
	pa_stream_set_write_callback(po->stream,
				     pulse_output_stream_write_cb, po);

	return true;
}

550
static bool
551
pulse_output_open(struct audio_output *ao, AudioFormat &audio_format,
552
		  Error &error)
553
{
554
	PulseOutput *po = (PulseOutput *)ao;
555 556
	pa_sample_spec ss;

557
	assert(po->mainloop != nullptr);
558

559 560
	pa_threaded_mainloop_lock(po->mainloop);

561
	if (po->context != nullptr) {
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
		switch (pa_context_get_state(po->context)) {
		case PA_CONTEXT_UNCONNECTED:
		case PA_CONTEXT_TERMINATED:
		case PA_CONTEXT_FAILED:
			/* the connection was closed meanwhile; delete
			   it, and pulse_output_wait_connection() will
			   reopen it */
			pulse_output_delete_context(po);
			break;

		case PA_CONTEXT_READY:
		case PA_CONTEXT_CONNECTING:
		case PA_CONTEXT_AUTHORIZING:
		case PA_CONTEXT_SETTING_NAME:
			break;
		}
	}

580
	if (!pulse_output_wait_connection(po, error)) {
581
		pa_threaded_mainloop_unlock(po->mainloop);
582
		return false;
583
	}
584

585 586
	/* MPD doesn't support the other pulseaudio sample formats, so
	   we just force MPD to send us everything as 16 bit */
587
	audio_format.format = SampleFormat::S16;
588 589

	ss.format = PA_SAMPLE_S16NE;
590 591
	ss.rate = audio_format.sample_rate;
	ss.channels = audio_format.channels;
592

593 594
	/* create a stream .. */

595
	if (!pulse_output_setup_stream(po, &ss, error)) {
596 597 598 599 600 601
		pa_threaded_mainloop_unlock(po->mainloop);
		return false;
	}

	/* .. and connect it (asynchronously) */

602 603 604
	if (pa_stream_connect_playback(po->stream, po->sink,
				       nullptr, pa_stream_flags_t(0),
				       nullptr, nullptr) < 0) {
605
		pulse_output_delete_stream(po);
606

607 608
		SetError(error, po->context,
			 "pa_stream_connect_playback() has failed");
609
		pa_threaded_mainloop_unlock(po->mainloop);
610
		return false;
611 612
	}

613 614
	pa_threaded_mainloop_unlock(po->mainloop);

615
	return true;
616 617
}

618
static void
619
pulse_output_close(struct audio_output *ao)
620
{
621
	PulseOutput *po = (PulseOutput *)ao;
622 623
	pa_operation *o;

624
	assert(po->mainloop != nullptr);
625

626 627 628 629 630
	pa_threaded_mainloop_lock(po->mainloop);

	if (pa_stream_get_state(po->stream) == PA_STREAM_READY) {
		o = pa_stream_drain(po->stream,
				    pulse_output_stream_success_cb, po);
631
		if (o == nullptr) {
632 633 634
			FormatWarning(pulse_output_domain,
				      "pa_stream_drain() has failed: %s",
				      pa_strerror(pa_context_errno(po->context)));
635 636 637 638
		} else
			pulse_wait_for_operation(po->mainloop, o);
	}

639
	pulse_output_delete_stream(po);
640

641
	if (po->context != nullptr &&
642 643 644 645 646 647 648 649 650 651 652 653 654
	    pa_context_get_state(po->context) != PA_CONTEXT_READY)
		pulse_output_delete_context(po);

	pa_threaded_mainloop_unlock(po->mainloop);
}

/**
 * Check if the stream is (already) connected, and waits if not.  The
 * mainloop must be locked before calling this function.
 *
 * @return true on success, false on error
 */
static bool
655
pulse_output_wait_stream(PulseOutput *po, Error &error)
656
{
657 658 659 660
	while (true) {
		switch (pa_stream_get_state(po->stream)) {
		case PA_STREAM_READY:
			return true;
661

662 663 664
		case PA_STREAM_FAILED:
		case PA_STREAM_TERMINATED:
		case PA_STREAM_UNCONNECTED:
665 666
			SetError(error, po->context,
				 "failed to connect the stream");
667
			return false;
668

669 670 671 672
		case PA_STREAM_CREATING:
			pa_threaded_mainloop_wait(po->mainloop);
			break;
		}
673
	}
674 675
}

676 677 678 679
/**
 * Sets cork mode on the stream.
 */
static bool
680
pulse_output_stream_pause(PulseOutput *po, bool pause,
681
			  Error &error)
682 683 684
{
	pa_operation *o;

685 686 687
	assert(po->mainloop != nullptr);
	assert(po->context != nullptr);
	assert(po->stream != nullptr);
688 689 690

	o = pa_stream_cork(po->stream, pause,
			   pulse_output_stream_success_cb, po);
691
	if (o == nullptr) {
692
		SetError(error, po->context, "pa_stream_cork() has failed");
693 694 695 696
		return false;
	}

	if (!pulse_wait_for_operation(po->mainloop, o)) {
697
		SetError(error, po->context, "pa_stream_cork() has failed");
698 699 700 701
		return false;
	}

	return true;
702 703
}

704 705 706
static unsigned
pulse_output_delay(struct audio_output *ao)
{
707
	PulseOutput *po = (PulseOutput *)ao;
708 709 710 711
	unsigned result = 0;

	pa_threaded_mainloop_lock(po->mainloop);

712
	if (po->base.pause && pa_stream_is_corked(po->stream) &&
713 714 715 716 717 718 719 720 721
	    pa_stream_get_state(po->stream) == PA_STREAM_READY)
		/* idle while paused */
		result = 1000;

	pa_threaded_mainloop_unlock(po->mainloop);

	return result;
}

722
static size_t
723
pulse_output_play(struct audio_output *ao, const void *chunk, size_t size,
724
		  Error &error)
725
{
726
	PulseOutput *po = (PulseOutput *)ao;
727

728 729
	assert(po->mainloop != nullptr);
	assert(po->stream != nullptr);
730 731 732 733 734

	pa_threaded_mainloop_lock(po->mainloop);

	/* check if the stream is (already) connected */

735
	if (!pulse_output_wait_stream(po, error)) {
736 737 738 739
		pa_threaded_mainloop_unlock(po->mainloop);
		return 0;
	}

740
	assert(po->context != nullptr);
741 742 743

	/* unpause if previously paused */

744
	if (pa_stream_is_corked(po->stream) &&
745
	    !pulse_output_stream_pause(po, false, error)) {
746
		pa_threaded_mainloop_unlock(po->mainloop);
747
		return 0;
748
	}
749 750 751 752

	/* wait until the server allows us to write */

	while (po->writable == 0) {
753 754
		if (pa_stream_is_suspended(po->stream)) {
			pa_threaded_mainloop_unlock(po->mainloop);
755
			error.Set(pulse_output_domain, "suspended");
756 757 758
			return 0;
		}

759 760 761 762
		pa_threaded_mainloop_wait(po->mainloop);

		if (pa_stream_get_state(po->stream) != PA_STREAM_READY) {
			pa_threaded_mainloop_unlock(po->mainloop);
763
			error.Set(pulse_output_domain, "disconnected");
764
			return 0;
765 766 767 768 769 770 771 772 773 774 775
		}
	}

	/* now write */

	if (size > po->writable)
		/* don't send more than possible */
		size = po->writable;

	po->writable -= size;

776 777
	int result = pa_stream_write(po->stream, chunk, size, nullptr,
				     0, PA_SEEK_RELATIVE);
778
	pa_threaded_mainloop_unlock(po->mainloop);
779 780
	if (result < 0) {
		SetError(error, po->context, "pa_stream_write() failed");
781
		return 0;
782 783
	}

784
	return size;
785 786
}

787
static void
788
pulse_output_cancel(struct audio_output *ao)
789
{
790
	PulseOutput *po = (PulseOutput *)ao;
791 792
	pa_operation *o;

793 794
	assert(po->mainloop != nullptr);
	assert(po->stream != nullptr);
795 796 797 798 799 800 801 802 803 804

	pa_threaded_mainloop_lock(po->mainloop);

	if (pa_stream_get_state(po->stream) != PA_STREAM_READY) {
		/* no need to flush when the stream isn't connected
		   yet */
		pa_threaded_mainloop_unlock(po->mainloop);
		return;
	}

805
	assert(po->context != nullptr);
806 807

	o = pa_stream_flush(po->stream, pulse_output_stream_success_cb, po);
808
	if (o == nullptr) {
809 810 811
		FormatWarning(pulse_output_domain,
			      "pa_stream_flush() has failed: %s",
			      pa_strerror(pa_context_errno(po->context)));
812 813 814 815 816 817 818 819 820
		pa_threaded_mainloop_unlock(po->mainloop);
		return;
	}

	pulse_wait_for_operation(po->mainloop, o);
	pa_threaded_mainloop_unlock(po->mainloop);
}

static bool
821
pulse_output_pause(struct audio_output *ao)
822
{
823
	PulseOutput *po = (PulseOutput *)ao;
824

825 826
	assert(po->mainloop != nullptr);
	assert(po->stream != nullptr);
827 828 829 830 831

	pa_threaded_mainloop_lock(po->mainloop);

	/* check if the stream is (already/still) connected */

832 833
	Error error;
	if (!pulse_output_wait_stream(po, error)) {
834
		pa_threaded_mainloop_unlock(po->mainloop);
835
		LogError(error);
836 837 838
		return false;
	}

839
	assert(po->context != nullptr);
840 841 842

	/* cork the stream */

843
	if (!pa_stream_is_corked(po->stream) &&
844
	    !pulse_output_stream_pause(po, true, error)) {
845
		pa_threaded_mainloop_unlock(po->mainloop);
846
		LogError(error);
847 848 849 850 851 852 853 854 855 856 857 858 859
		return false;
	}

	pa_threaded_mainloop_unlock(po->mainloop);

	return true;
}

static bool
pulse_output_test_default_device(void)
{
	bool success;

860
	const config_param empty;
861 862
	PulseOutput *po = (PulseOutput *)
		pulse_output_init(empty, IgnoreError());
863
	if (po == nullptr)
864 865
		return false;

866
	success = pulse_output_wait_connection(po, IgnoreError());
867
	pulse_output_finish(&po->base);
868 869 870 871

	return success;
}

872
const struct audio_output_plugin pulse_output_plugin = {
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	"pulse",
	pulse_output_test_default_device,
	pulse_output_init,
	pulse_output_finish,
	pulse_output_enable,
	pulse_output_disable,
	pulse_output_open,
	pulse_output_close,
	pulse_output_delay,
	nullptr,
	pulse_output_play,
	nullptr,
	pulse_output_cancel,
	pulse_output_pause,

	&pulse_mixer_plugin,
889
};