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

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

28
	Timer *timer;
29

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

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

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

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

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

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

64 65
		return size;
	}
66

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

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