NullOutputPlugin.cxx 2.32 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 32
	bool sync;

33
	Timer *timer;
34

35
public:
36 37 38
	NullOutput()
		:base(null_output_plugin) {}

39 40
	bool Initialize(const ConfigBlock &block, Error &error) {
		return base.Configure(block, error);
41
	}
42

43
	static NullOutput *Create(const ConfigBlock &block, Error &error);
44

45 46 47
	bool Open(AudioFormat &audio_format, gcc_unused Error &error) {
		if (sync)
			timer = new Timer(audio_format);
48

49 50
		return true;
	}
Max Kellermann's avatar
Max Kellermann committed
51

52 53 54 55
	void Close() {
		if (sync)
			delete timer;
	}
56

57 58 59 60 61
	unsigned Delay() const {
		return sync && timer->IsStarted()
			? timer->GetDelay()
			: 0;
	}
62

63 64 65 66 67 68 69
	size_t Play(gcc_unused const void *chunk, size_t size,
		    gcc_unused Error &error) {
		if (sync) {
			if (!timer->IsStarted())
				timer->Start();
			timer->Add(size);
		}
70

71 72
		return size;
	}
73

74 75 76 77 78
	void Cancel() {
		if (sync)
			timer->Reset();
	}
};
79

80
inline NullOutput *
81
NullOutput::Create(const ConfigBlock &block, Error &error)
82
{
83
	NullOutput *nd = new NullOutput();
84

85
	if (!nd->Initialize(block, error)) {
86 87 88
		delete nd;
		return nullptr;
	}
89

90
	nd->sync = block.GetBlockValue("sync", true);
91

92
	return nd;
93 94
}

95
typedef AudioOutputWrapper<NullOutput> Wrapper;
96

97
const struct AudioOutputPlugin null_output_plugin = {
98 99
	"null",
	nullptr,
100 101
	&Wrapper::Init,
	&Wrapper::Finish,
102 103
	nullptr,
	nullptr,
104 105 106
	&Wrapper::Open,
	&Wrapper::Close,
	&Wrapper::Delay,
107
	nullptr,
108
	&Wrapper::Play,
109
	nullptr,
110
	&Wrapper::Cancel,
111 112
	nullptr,
	nullptr,
113
};