NullOutputPlugin.cxx 2.14 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"
23
#include "../Wrapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "../Timer.hxx"
25

26 27 28
class NullOutput {
	friend struct AudioOutputWrapper<NullOutput>;

29
	AudioOutput base;
30

31
	const bool sync;
32

33
	Timer *timer;
34

35
public:
36 37 38
	NullOutput(const ConfigBlock &block)
		:base(null_output_plugin, block),
		 sync(block.GetBlockValue("sync", true)) {}
39

40
	static NullOutput *Create(const ConfigBlock &block);
41

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

47 48 49 50
	void Close() {
		if (sync)
			delete timer;
	}
51

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

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

65 66
		return size;
	}
67

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

74
inline NullOutput *
75
NullOutput::Create(const ConfigBlock &block)
76
{
77
	return new NullOutput(block);
78 79
}

80
typedef AudioOutputWrapper<NullOutput> Wrapper;
81

82
const struct AudioOutputPlugin null_output_plugin = {
83 84
	"null",
	nullptr,
85 86
	&Wrapper::Init,
	&Wrapper::Finish,
87 88
	nullptr,
	nullptr,
89 90 91
	&Wrapper::Open,
	&Wrapper::Close,
	&Wrapper::Delay,
92
	nullptr,
93
	&Wrapper::Play,
94
	nullptr,
95
	&Wrapper::Cancel,
96 97
	nullptr,
	nullptr,
98
};