summaryrefslogtreecommitdiff
path: root/src/output/plugins/wasapi/WasapiOutputPlugin.cxx
blob: 203896c7a5489a03807e47a8e46e61294bf226a7 (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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
/*
 * Copyright 2020-2021 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#include "WasapiOutputPlugin.hxx"
#include "ForMixer.hxx"
#include "AudioClient.hxx"
#include "Device.hxx"
#include "PropertyStore.hxx"
#include "output/OutputAPI.hxx"
#include "lib/icu/Win32.hxx"
#include "mixer/MixerList.hxx"
#include "output/Error.hxx"
#include "pcm/Export.hxx"
#include "thread/Cond.hxx"
#include "thread/Mutex.hxx"
#include "thread/Name.hxx"
#include "thread/Thread.hxx"
#include "util/AllocatedString.hxx"
#include "util/ConstBuffer.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"
#include "util/StringBuffer.hxx"
#include "win32/Com.hxx"
#include "win32/ComPtr.hxx"
#include "win32/ComWorker.hxx"
#include "win32/HResult.hxx"
#include "win32/WinEvent.hxx"
#include "Log.hxx"
#include "config.h"

#include <boost/lockfree/spsc_queue.hpp>

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <optional>
#include <variant>

#include <audioclient.h>
#include <initguid.h>
#include <functiondiscoverykeys_devpkey.h>
#include <mmdeviceapi.h>

namespace {
static constexpr Domain wasapi_output_domain("wasapi_output");

constexpr uint32_t
GetChannelMask(const uint8_t channels) noexcept
{
	switch (channels) {
	case 1:
		return KSAUDIO_SPEAKER_MONO;
	case 2:
		return KSAUDIO_SPEAKER_STEREO;
	case 3:
		return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER;
	case 4:
		return KSAUDIO_SPEAKER_QUAD;
	case 5:
		return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER |
		       SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
	case 6:
		return KSAUDIO_SPEAKER_5POINT1;
	case 7:
		return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
	case 8:
		return KSAUDIO_SPEAKER_7POINT1_SURROUND;
	default:
		gcc_unreachable();
	}
}

template <typename Functor>
inline bool
SafeSilenceTry(Functor &&functor) noexcept
{
	try {
		functor();
		return true;
	} catch (...) {
		return false;
	}
}

std::vector<WAVEFORMATEXTENSIBLE>
GetFormats(const AudioFormat &audio_format) noexcept
{
#ifdef ENABLE_DSD
	if (audio_format.format == SampleFormat::DSD) {
		AudioFormat dop_format = audio_format;
		PcmExport::Params params;
		params.dsd_mode = PcmExport::DsdMode::DOP;
		dop_format.sample_rate =
			params.CalcOutputSampleRate(audio_format.sample_rate);
		dop_format.format = SampleFormat::S24_P32;
		return GetFormats(dop_format);
	}
#endif
	std::vector<WAVEFORMATEXTENSIBLE> Result;
	if (audio_format.format == SampleFormat::S24_P32) {
		Result.resize(2);
		Result[0].Format.wBitsPerSample = 24;
		Result[0].Samples.wValidBitsPerSample = 24;
		Result[1].Format.wBitsPerSample = 32;
		Result[1].Samples.wValidBitsPerSample = 24;
	} else {
		Result.resize(1);
		Result[0].Format.wBitsPerSample = audio_format.GetSampleSize() * 8;
		Result[0].Samples.wValidBitsPerSample = audio_format.GetSampleSize() * 8;
	}
	const DWORD mask = GetChannelMask(audio_format.channels);
	const GUID guid = audio_format.format == SampleFormat::FLOAT
				  ? KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
				  : KSDATAFORMAT_SUBTYPE_PCM;
	for (auto &device_format : Result) {
		device_format.dwChannelMask = mask;
		device_format.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
		device_format.Format.nChannels = audio_format.channels;
		device_format.Format.nSamplesPerSec = audio_format.sample_rate;
		device_format.Format.cbSize =
			sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
		device_format.SubFormat = guid;
		device_format.Format.nBlockAlign = device_format.Format.nChannels *
						   device_format.Format.wBitsPerSample /
						   8;
		device_format.Format.nAvgBytesPerSec =
			audio_format.sample_rate * device_format.Format.nBlockAlign;
	}
	return Result;
}

#ifdef ENABLE_DSD
void
SetDSDFallback(AudioFormat &audio_format) noexcept
{
	audio_format.format = SampleFormat::FLOAT;
	audio_format.sample_rate = 384000;
}
#endif

} // namespace

class WasapiOutputThread {
	Thread thread{BIND_THIS_METHOD(Work)};
	WinEvent event;
	WinEvent data_poped;
	IAudioClient &client;
	ComPtr<IAudioRenderClient> render_client;
	const UINT32 frame_size;
	const UINT32 buffer_size_in_frames;
	const bool is_exclusive;

	/**
	 * This flag is only used by the calling thread
	 * (i.e. #OutputThread), and specifies whether the
	 * WasapiOutputThread has been told to play via Play().  This
	 * variable is somewhat redundant because we already have
	 * "state", but using this variable saves some overhead for
	 * atomic operations.
	 */
	bool playing = false;

	bool started = false;

	std::atomic_bool cancel = false;

	enum class Status : uint32_t { FINISH, PLAY, PAUSE };

	alignas(BOOST_LOCKFREE_CACHELINE_BYTES) std::atomic<Status> status =
		Status::PAUSE;
	alignas(BOOST_LOCKFREE_CACHELINE_BYTES) struct {
		std::atomic_bool occur = false;
		std::exception_ptr ptr = nullptr;
	} error;
	boost::lockfree::spsc_queue<BYTE> spsc_buffer;

public:
	WasapiOutputThread(IAudioClient &_client,
			   ComPtr<IAudioRenderClient> &&_render_client,
			   const UINT32 _frame_size, const UINT32 _buffer_size_in_frames,
			   bool _is_exclusive)
		:client(_client),
		 render_client(std::move(_render_client)), frame_size(_frame_size),
		 buffer_size_in_frames(_buffer_size_in_frames), is_exclusive(_is_exclusive),
		 spsc_buffer(_buffer_size_in_frames * 4 * _frame_size)
	{
		SetEventHandle(client, event.handle());
		thread.Start();
	}

	void Finish() noexcept {
		SetStatus(Status::FINISH);
		thread.Join();
	}

	void Play() noexcept {
		playing = true;
		SetStatus(Status::PLAY);
	}

	void Pause() noexcept {
		if (!playing)
			return;

		playing = false;
		SetStatus(Status::PAUSE);
	}

	std::size_t Push(ConstBuffer<void> input) noexcept {
		std::size_t consumed =
			spsc_buffer.push(static_cast<const BYTE *>(input.data),
					 input.size);

		if (!playing) {
			playing = true;
			Play();
		}

		return consumed;
	}

	/**
	 * Instruct the thread to discard the buffer (and wait for
	 * completion).  This needs to be done inside this thread,
	 * because only the consumer thread is allowed to do that.
	 */
	void Cancel() noexcept {
		cancel.store(true);
		event.Set();

		while (cancel.load() && !error.occur.load())
			Wait();

		/* not rethrowing the exception here via
		   CheckException() because this method must be
		   "noexcept"; the next WasapiOutput::Play() call will
		   throw */
	}

	/**
	 * Wait for the thread to finish some work (e.g. until some
	 * buffer space becomes available).
	 */
	void Wait() noexcept {
		data_poped.Wait();
	}

	void InterruptWaiter() noexcept {
		data_poped.Set();
	}

	void CheckException() {
		if (error.occur.load()) {
			std::rethrow_exception(error.ptr);
		}
	}

private:
	void SetStatus(Status s) noexcept {
		status.store(s);
		event.Set();
	}
	void Work() noexcept;
};

class WasapiOutput final : public AudioOutput {
	const bool is_exclusive;
	const bool enumerate_devices;
#ifdef ENABLE_DSD
	const bool dop_setting;
#endif

	/**
	 * Only valid if the output is open.
	 */
	bool paused;

	std::atomic_flag not_interrupted = true;

	const std::string device_config;

	std::shared_ptr<COMWorker> com_worker;
	ComPtr<IMMDevice> device;
	ComPtr<IAudioClient> client;
	WAVEFORMATEXTENSIBLE device_format;
	std::optional<WasapiOutputThread> thread;
	std::size_t watermark;
	std::optional<PcmExport> pcm_export;

public:
	static AudioOutput *Create(EventLoop &, const ConfigBlock &block);
	WasapiOutput(const ConfigBlock &block);

	auto GetComWorker() noexcept {
		// TODO: protect access to the shard_ptr
		return com_worker;
	}

	void Enable() override {
		com_worker = std::make_shared<COMWorker>();

		try {
			com_worker->Async([&]() { ChooseDevice(); }).get();
		} catch (...) {
			com_worker.reset();
			throw;
		}
	}
	void Disable() noexcept override {
		com_worker->Async([&]() { DoDisable(); }).get();
		com_worker.reset();
	}
	void Open(AudioFormat &audio_format) override {
		com_worker->Async([&]() { DoOpen(audio_format); }).get();
		paused = false;
	}
	void Close() noexcept override;
	std::chrono::steady_clock::duration Delay() const noexcept override;
	size_t Play(const void *chunk, size_t size) override;
	void Drain() override;
	void Cancel() noexcept override;
	bool Pause() override;
	void Interrupt() noexcept override;

	constexpr bool Exclusive() const { return is_exclusive; }
	constexpr size_t FrameSize() const { return device_format.Format.nBlockAlign; }
	constexpr size_t SampleRate() const {
		return device_format.Format.nSamplesPerSec;
	}

private:
	friend bool wasapi_is_exclusive(WasapiOutput &output) noexcept;
	friend IMMDevice *wasapi_output_get_device(WasapiOutput &output) noexcept;
	friend IAudioClient *wasapi_output_get_client(WasapiOutput &output) noexcept;

	void DoDisable() noexcept;
	void DoOpen(AudioFormat &audio_format);

	void ChooseDevice();
	bool TryFormatExclusive(const AudioFormat &audio_format);
	void FindExclusiveFormatSupported(AudioFormat &audio_format);
	void FindSharedFormatSupported(AudioFormat &audio_format);
	static void EnumerateDevices(IMMDeviceEnumerator &enumerator);
	static ComPtr<IMMDevice> GetDevice(IMMDeviceEnumerator &enumerator,
					   unsigned index);
	static ComPtr<IMMDevice> SearchDevice(IMMDeviceEnumerator &enumerator,
					      std::string_view name);
};

WasapiOutput &
wasapi_output_downcast(AudioOutput &output) noexcept
{
	return static_cast<WasapiOutput &>(output);
}

bool
wasapi_is_exclusive(WasapiOutput &output) noexcept
{
	return output.is_exclusive;
}

std::shared_ptr<COMWorker>
wasapi_output_get_com_worker(WasapiOutput &output) noexcept
{
	return output.GetComWorker();
}

IMMDevice *
wasapi_output_get_device(WasapiOutput &output) noexcept
{
	return output.device.get();
}

IAudioClient *
wasapi_output_get_client(WasapiOutput &output) noexcept
{
	return output.client.get();
}

inline void
WasapiOutputThread::Work() noexcept
try {
	SetThreadName("Wasapi Output Worker");
	FormatDebug(wasapi_output_domain, "Working thread started");
	COM com;

	AtScopeExit(this) {
		if (started) {
			try {
				Stop(client);
			} catch (...) {
				LogError(std::current_exception());
			}
		}
	};

	while (true) {
		event.Wait();

		if (cancel.load()) {
			spsc_buffer.consume_all([](auto &&) {});
			cancel.store(false);
			InterruptWaiter();
		}

		Status current_state = status.load();
		switch (current_state) {
		case Status::FINISH:
			FormatDebug(wasapi_output_domain,
				    "Working thread stopped");
			return;

		case Status::PAUSE:
			if (!started)
				/* don't bother starting the
				   IAudioClient if we're paused */
				continue;

			/* stop the IAudioClient while paused; it will
			   be restarted as soon as we're asked to
			   resume playback */
			Stop(client);
			started = false;
			continue;

		case Status::PLAY:
			break;
		}

		UINT32 write_in_frames = buffer_size_in_frames;
		if (!is_exclusive) {
			UINT32 data_in_frames =
				GetCurrentPaddingFrames(client);
			if (data_in_frames >= buffer_size_in_frames) {
				continue;
			}
			write_in_frames -= data_in_frames;
		}

		BYTE *data;
		DWORD mode = 0;

		if (HRESULT result =
		    render_client->GetBuffer(write_in_frames, &data);
		    FAILED(result)) {
			throw MakeHResultError(result, "Failed to get buffer");
		}

		AtScopeExit(&) {
			render_client->ReleaseBuffer(write_in_frames, mode);

			if (!started) {
				Start(client);
				started = true;
			}
		};

		const UINT32 write_size = write_in_frames * frame_size;
		UINT32 new_data_size = 0;
		new_data_size = spsc_buffer.pop(data, write_size);
		std::fill_n(data + new_data_size,
			    write_size - new_data_size, 0);
		InterruptWaiter();
	}
} catch (...) {
	error.ptr = std::current_exception();
	error.occur.store(true);

	/* wake up the client thread which may be inside Wait() */
	InterruptWaiter();
}

AudioOutput *
WasapiOutput::Create(EventLoop &, const ConfigBlock &block)
{
	return new WasapiOutput(block);
}

WasapiOutput::WasapiOutput(const ConfigBlock &block)
	:AudioOutput(FLAG_ENABLE_DISABLE | FLAG_PAUSE),
	 is_exclusive(block.GetBlockValue("exclusive", false)),
	 enumerate_devices(block.GetBlockValue("enumerate", false)),
#ifdef ENABLE_DSD
	 dop_setting(block.GetBlockValue("dop", false)),
#endif
	 device_config(block.GetBlockValue("device", ""))
{
}

/// run inside COMWorkerThread
void
WasapiOutput::DoDisable() noexcept
{
	assert(!thread);

	device.reset();
}

/// run inside COMWorkerThread
void
WasapiOutput::DoOpen(AudioFormat &audio_format)
{
	client.reset();

	if (GetState(*device) != DEVICE_STATE_ACTIVE) {
		device.reset();
		ChooseDevice();
	}

	client = Activate<IAudioClient>(*device);

	if (audio_format.channels > 8) {
		audio_format.channels = 8;
	}

#ifdef ENABLE_DSD
	if (!dop_setting && audio_format.format == SampleFormat::DSD) {
		SetDSDFallback(audio_format);
	}
#endif
	if (Exclusive()) {
		FindExclusiveFormatSupported(audio_format);
	} else {
		FindSharedFormatSupported(audio_format);
	}
	bool require_export = audio_format.format == SampleFormat::S24_P32;
#ifdef ENABLE_DSD
	require_export |= audio_format.format == SampleFormat::DSD;
#endif
	if (require_export) {
		PcmExport::Params params;
		params.dsd_mode = PcmExport::DsdMode::NONE;
#ifdef ENABLE_DSD
		if (audio_format.format == SampleFormat::DSD) {
			params.dsd_mode = PcmExport::DsdMode::DOP;
		}
#endif
		params.shift8 = false;
		params.pack24 = false;
		if (device_format.Format.wBitsPerSample == 32 &&
		    device_format.Samples.wValidBitsPerSample == 24) {
			params.shift8 = true;
		}
		if (device_format.Format.wBitsPerSample == 24) {
			params.pack24 = true;
		}
		FormatDebug(wasapi_output_domain, "Packing data: shift8=%d pack24=%d",
			    int(params.shift8), int(params.pack24));
		pcm_export.emplace();
		pcm_export->Open(audio_format.format, audio_format.channels, params);
	}

	using s = std::chrono::seconds;
	using ms = std::chrono::milliseconds;
	using ns = std::chrono::nanoseconds;
	using hundred_ns = std::chrono::duration<uint64_t, std::ratio<1, 10000000>>;

	// The unit in REFERENCE_TIME is hundred nanoseconds
	REFERENCE_TIME default_device_period, min_device_period;

	if (HRESULT result =
		    client->GetDevicePeriod(&default_device_period, &min_device_period);
	    FAILED(result)) {
		throw MakeHResultError(result, "Unable to get device period");
	}
	FormatDebug(wasapi_output_domain,
		    "Default device period: %I64u ns, Minimum device period: "
		    "%I64u ns",
		    ns(hundred_ns(default_device_period)).count(),
		    ns(hundred_ns(min_device_period)).count());

	REFERENCE_TIME buffer_duration;
	if (Exclusive()) {
		buffer_duration = default_device_period;
	} else {
		const REFERENCE_TIME align = hundred_ns(ms(50)).count();
		buffer_duration = (align / default_device_period) * default_device_period;
	}
	FormatDebug(wasapi_output_domain, "Buffer duration: %I64u ns",
		    size_t(ns(hundred_ns(buffer_duration)).count()));

	if (Exclusive()) {
		if (HRESULT result = client->Initialize(
			    AUDCLNT_SHAREMODE_EXCLUSIVE,
			    AUDCLNT_STREAMFLAGS_EVENTCALLBACK, buffer_duration,
			    buffer_duration,
			    reinterpret_cast<WAVEFORMATEX *>(&device_format), nullptr);
		    FAILED(result)) {
			if (result == AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED) {
				// https://docs.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient-initialize
				UINT32 buffer_size_in_frames =
					GetBufferSizeInFrames(*client);
				buffer_duration =
					std::ceil(double(buffer_size_in_frames *
							 hundred_ns(s(1)).count()) /
						  SampleRate());
				FormatDebug(
					wasapi_output_domain,
					"Aligned buffer duration: %I64u ns",
					size_t(ns(hundred_ns(buffer_duration)).count()));
				client.reset();
				client = Activate<IAudioClient>(*device);
				result = client->Initialize(
					AUDCLNT_SHAREMODE_EXCLUSIVE,
					AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
					buffer_duration, buffer_duration,
					reinterpret_cast<WAVEFORMATEX *>(&device_format),
					nullptr);
			}

			if (FAILED(result)) {
				throw MakeHResultError(result, "Unable to initialize audio client");
			}
		}
	} else {
		if (HRESULT result = client->Initialize(
			    AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
			    buffer_duration, 0,
			    reinterpret_cast<WAVEFORMATEX *>(&device_format), nullptr);
		    FAILED(result)) {
			throw MakeHResultError(result,
					       "Unable to initialize audio client");
		}
	}

	auto render_client = GetService<IAudioRenderClient>(*client);

	const UINT32 buffer_size_in_frames = GetBufferSizeInFrames(*client);

	watermark = buffer_size_in_frames * 3 * FrameSize();
	thread.emplace(*client, std::move(render_client), FrameSize(),
		       buffer_size_in_frames, is_exclusive);

	paused = false;
}

void
WasapiOutput::Close() noexcept
{
	assert(thread);

	try {
		thread->CheckException();
	} catch (...) {
		FormatError(std::current_exception(),
			    "exception while stopping");
	}
	thread->Finish();
	com_worker->Async([&]() {
		thread.reset();
		client.reset();
	}).get();
	pcm_export.reset();
}

std::chrono::steady_clock::duration
WasapiOutput::Delay() const noexcept
{
	if (paused) {
		// idle while paused
		return std::chrono::seconds(1);
	}

	return std::chrono::steady_clock::duration::zero();
}

size_t
WasapiOutput::Play(const void *chunk, size_t size)
{
	assert(thread);

	paused = false;

	not_interrupted.test_and_set();

	ConstBuffer<void> input(chunk, size);
	if (pcm_export) {
		input = pcm_export->Export(input);
	}
	if (input.empty())
		return size;

	do {
		const size_t consumed_size = thread->Push({chunk, size});

		if (consumed_size == 0) {
			thread->Wait();
			thread->CheckException();
			if (!not_interrupted.test_and_set()) {
				throw AudioOutputInterrupted{};
			}
			continue;
		}

		thread->CheckException();

		if (pcm_export) {
			return pcm_export->CalcInputSize(consumed_size);
		}
		return consumed_size;
	} while (true);
}

bool
WasapiOutput::Pause()
{
	paused = true;
	thread->Pause();
	thread->CheckException();
	return true;
}

void
WasapiOutput::Interrupt() noexcept
{
	if (thread) {
		not_interrupted.clear();
		thread->InterruptWaiter();
	}
}

void
WasapiOutput::Drain()
{
	assert(thread);

	// TODO implement

	thread->CheckException();
}

void
WasapiOutput::Cancel() noexcept
{
	assert(thread);

	thread->Cancel();
}

/// run inside COMWorkerThread
void
WasapiOutput::ChooseDevice()
{
	ComPtr<IMMDeviceEnumerator> enumerator;
	enumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr,
				    CLSCTX_INPROC_SERVER);

	if (enumerate_devices) {
		try {
			EnumerateDevices(*enumerator);
		} catch (...) {
			LogError(std::current_exception());
		}
	}

	if (!device_config.empty()) {
		unsigned int id;
		if (!SafeSilenceTry([this, &id]() { id = std::stoul(device_config); })) {
			device = SearchDevice(*enumerator, device_config);
			if (!device)
				throw FormatRuntimeError("Device '%s' not found",
							 device_config.c_str());
		} else
			device = GetDevice(*enumerator, id);
	} else {
		device = GetDefaultAudioEndpoint(*enumerator);
	}
}

/// run inside COMWorkerThread
bool
WasapiOutput::TryFormatExclusive(const AudioFormat &audio_format)
{
	for (auto test_format : GetFormats(audio_format)) {
		HRESULT result = client->IsFormatSupported(
			AUDCLNT_SHAREMODE_EXCLUSIVE,
			reinterpret_cast<WAVEFORMATEX *>(&test_format), nullptr);
		const auto format_string = ToString(audio_format);
		const auto result_string = std::string(HRESULTToString(result));
		FormatDebug(wasapi_output_domain, "Trying %s %lu %u-%u (exclusive) -> %s",
			    format_string.c_str(), test_format.Format.nSamplesPerSec,
			    test_format.Format.wBitsPerSample,
			    test_format.Samples.wValidBitsPerSample,
			    result_string.c_str());
		if (SUCCEEDED(result)) {
			device_format = test_format;
			return true;
		}

		if (result == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
			throw std::runtime_error("Exclusive mode not allowed");
	}
	return false;
}

/// run inside COMWorkerThread
void
WasapiOutput::FindExclusiveFormatSupported(AudioFormat &audio_format)
{
	for (uint8_t channels : {0, 2, 6, 8, 7, 1, 4, 5, 3}) {
		if (audio_format.channels == channels) {
			continue;
		}
		if (channels == 0) {
			channels = audio_format.channels;
		}
		auto old_channels = std::exchange(audio_format.channels, channels);
#ifdef ENABLE_DSD
		bool was_dsd = false;
		if (audio_format.format == SampleFormat::DSD) {
			if (dop_setting && TryFormatExclusive(audio_format)) {
				return;
			}
			was_dsd = true;
			SetDSDFallback(audio_format);
		}
#endif
		for (uint32_t rate : {0, 384000, 352800, 192000, 176400, 96000, 88200,
				      48000, 44100, 32000, 22050, 16000, 11025, 8000}) {
			if (audio_format.sample_rate <= rate) {
				continue;
			}
			if (rate == 0) {
				rate = audio_format.sample_rate;
			}
			auto old_rate = std::exchange(audio_format.sample_rate, rate);
			for (SampleFormat format : {
				     SampleFormat::UNDEFINED,
				     SampleFormat::S32,
				     SampleFormat::S24_P32,
				     SampleFormat::S16,
				     SampleFormat::S8,
			     }) {
				if (audio_format.format == format) {
					continue;
				}
				if (format == SampleFormat::UNDEFINED) {
					format = audio_format.format;
				}
				auto old_format =
					std::exchange(audio_format.format, format);
				if (TryFormatExclusive(audio_format)) {
					return;
				}
				audio_format.format = old_format;
			}
			audio_format.sample_rate = old_rate;
		}
#ifdef ENABLE_DSD
		if (was_dsd) {
			audio_format.format = SampleFormat::DSD;
		}
#endif
		audio_format.channels = old_channels;
	}
}

/// run inside COMWorkerThread
void
WasapiOutput::FindSharedFormatSupported(AudioFormat &audio_format)
{
	HRESULT result;

	// In shared mode, different sample rate is always unsupported.
	auto mixer_format = GetMixFormat(*client);

	audio_format.sample_rate = mixer_format->nSamplesPerSec;
	device_format = GetFormats(audio_format).front();

	ComHeapPtr<WAVEFORMATEXTENSIBLE> closest_format;
	result = client->IsFormatSupported(
		AUDCLNT_SHAREMODE_SHARED,
		reinterpret_cast<WAVEFORMATEX *>(&device_format),
		closest_format.AddressCast<WAVEFORMATEX>());
	{
		const auto format_string = ToString(audio_format);
		const auto result_string = std::string(HRESULTToString(result));
		FormatDebug(wasapi_output_domain, "Trying %s %lu %u-%u (shared) -> %s",
			    format_string.c_str(), device_format.Format.nSamplesPerSec,
			    device_format.Format.wBitsPerSample,
			    device_format.Samples.wValidBitsPerSample,
			    result_string.c_str());
	}

	if (FAILED(result) && result != AUDCLNT_E_UNSUPPORTED_FORMAT) {
		throw MakeHResultError(result, "IsFormatSupported failed");
	}

	switch (result) {
	case S_OK:
		break;
	case AUDCLNT_E_UNSUPPORTED_FORMAT:
	default:
		// Trying channels fallback.
		audio_format.channels = mixer_format->nChannels;

		device_format = GetFormats(audio_format).front();

		result = client->IsFormatSupported(
			AUDCLNT_SHAREMODE_SHARED,
			reinterpret_cast<WAVEFORMATEX *>(&device_format),
			closest_format.AddressCast<WAVEFORMATEX>());
		{
			const auto format_string = ToString(audio_format);
			const auto result_string = std::string(HRESULTToString(result));
			FormatDebug(wasapi_output_domain,
				    "Trying %s %lu %u-%u (shared) -> %s",
				    format_string.c_str(),
				    device_format.Format.nSamplesPerSec,
				    device_format.Format.wBitsPerSample,
				    device_format.Samples.wValidBitsPerSample,
				    result_string.c_str());
		}
		if (FAILED(result)) {
			throw MakeHResultError(result, "Format is not supported");
		}
		break;
	case S_FALSE:
		if (closest_format->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
			device_format = *closest_format;
		} else {
			device_format.Samples.wValidBitsPerSample =
				device_format.Format.wBitsPerSample;
			device_format.Format = closest_format->Format;
			switch (std::exchange(device_format.Format.wFormatTag,
					      WAVE_FORMAT_EXTENSIBLE)) {
			case WAVE_FORMAT_PCM:
				device_format.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
				break;
			case WAVE_FORMAT_IEEE_FLOAT:
				device_format.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
				break;
			default:
				gcc_unreachable();
			}
		}
		break;
	}

	// Copy closest match back to audio_format.
	audio_format.channels = device_format.Format.nChannels;
	audio_format.sample_rate = device_format.Format.nSamplesPerSec;
	if (device_format.SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
		switch (device_format.Format.wBitsPerSample) {
		case 8:
			audio_format.format = SampleFormat::S8;
			break;
		case 16:
			audio_format.format = SampleFormat::S16;
			break;
		case 32:
			audio_format.format =
				device_format.Samples.wValidBitsPerSample == 32
					? SampleFormat::S32
					: SampleFormat::S24_P32;
			break;
		}
	} else if (device_format.SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
		audio_format.format = SampleFormat::FLOAT;
	}
}

/// run inside COMWorkerThread
void
WasapiOutput::EnumerateDevices(IMMDeviceEnumerator &enumerator)
{
	const auto device_collection = EnumAudioEndpoints(enumerator);

	const UINT count = GetCount(*device_collection);
	for (UINT i = 0; i < count; ++i) {
		const auto enumerated_device = Item(*device_collection, i);

		const auto property_store =
			OpenPropertyStore(*enumerated_device);

		auto name = GetString(*property_store,
				      PKEY_Device_FriendlyName);
		if (name == nullptr)
			continue;

		FormatNotice(wasapi_output_domain,
			     "Device \"%u\" \"%s\"", i, name.c_str());
	}
}

/// run inside COMWorkerThread
ComPtr<IMMDevice>
WasapiOutput::GetDevice(IMMDeviceEnumerator &enumerator, unsigned index)
{
	const auto device_collection = EnumAudioEndpoints(enumerator);
	return Item(*device_collection, index);
}

/// run inside COMWorkerThread
ComPtr<IMMDevice>
WasapiOutput::SearchDevice(IMMDeviceEnumerator &enumerator,
			   std::string_view name)
{
	const auto device_collection = EnumAudioEndpoints(enumerator);

	const UINT count = GetCount(*device_collection);
	for (UINT i = 0; i < count; ++i) {
		auto d = Item(*device_collection, i);

		const auto property_store = OpenPropertyStore(*d);
		auto n = GetString(*property_store, PKEY_Device_FriendlyName);
		if (n != nullptr && name.compare(n) == 0)
			return d;
	}

	return nullptr;
}

static bool
wasapi_output_test_default_device()
{
	return true;
}

const struct AudioOutputPlugin wasapi_output_plugin = {
	"wasapi",
	wasapi_output_test_default_device,
	WasapiOutput::Create,
	&wasapi_mixer_plugin,
};