NullOutputPlugin.cxx 1.86 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "NullOutputPlugin.hxx"
21
#include "../OutputAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "../Timer.hxx"
23

24
class NullOutput final  : AudioOutput {
25
	const bool sync;
26

27
	Timer *timer;
28

29
public:
30
	NullOutput(const ConfigBlock &block)
31
		:AudioOutput(0),
32
		 sync(block.GetBlockValue("sync", true)) {}
33

34 35 36 37
	static AudioOutput *Create(EventLoop &,
				   const ConfigBlock &block) {
		return new NullOutput(block);
	}
38

39 40
private:
	void Open(AudioFormat &audio_format) override {
41 42 43
		if (sync)
			timer = new Timer(audio_format);
	}
Max Kellermann's avatar
Max Kellermann committed
44

45
	void Close() noexcept override {
46 47 48
		if (sync)
			delete timer;
	}
49

50
	std::chrono::steady_clock::duration Delay() const noexcept override {
51
		return sync && timer->IsStarted()
52
			? timer->GetDelay()
53
			: std::chrono::steady_clock::duration::zero();
54
	}
55

56
	size_t Play(gcc_unused const void *chunk, size_t size) override {
57 58 59 60 61
		if (sync) {
			if (!timer->IsStarted())
				timer->Start();
			timer->Add(size);
		}
62

63 64
		return size;
	}
65

66
	void Cancel() noexcept override {
67 68 69 70
		if (sync)
			timer->Reset();
	}
};
71

72
const struct AudioOutputPlugin null_output_plugin = {
73 74
	"null",
	nullptr,
75
	&NullOutput::Create,
76
	nullptr,
77
};