FifoOutputPlugin.cxx 4.68 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 "FifoOutputPlugin.hxx"
21
#include "../OutputAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "../Timer.hxx"
23
#include "fs/AllocatedPath.hxx"
24
#include "fs/FileSystem.hxx"
25
#include "fs/FileInfo.hxx"
26
#include "util/Domain.hxx"
27
#include "util/RuntimeError.hxx"
28
#include "Log.hxx"
29
#include "open.h"
30

31
#include <sys/stat.h>
32 33
#include <errno.h>
#include <unistd.h>
34

35
class FifoOutput final : AudioOutput {
36
	const AllocatedPath path;
37 38
	std::string path_utf8;

39 40 41
	int input = -1;
	int output = -1;
	bool created = false;
42
	Timer *timer;
43

44
public:
45
	FifoOutput(const ConfigBlock &block);
46

47
	~FifoOutput() {
48
		CloseFifo();
49 50
	}

51 52 53 54
	static AudioOutput *Create(EventLoop &,
				   const ConfigBlock &block) {
		return new FifoOutput(block);
	}
55

56
private:
57 58
	void Create();
	void Check();
59 60
	void Delete();

61
	void OpenFifo();
62
	void CloseFifo();
63

64 65
	void Open(AudioFormat &audio_format) override;
	void Close() noexcept override;
66

67 68 69
	std::chrono::steady_clock::duration Delay() const noexcept override;
	size_t Play(const void *chunk, size_t size) override;
	void Cancel() noexcept override;
Max Kellermann's avatar
Max Kellermann committed
70
};
71

72
static constexpr Domain fifo_output_domain("fifo_output");
73

74
FifoOutput::FifoOutput(const ConfigBlock &block)
75
	:AudioOutput(0),
76 77 78 79 80 81 82 83 84 85
	 path(block.GetPath("path"))
{
	if (path.IsNull())
		throw std::runtime_error("No \"path\" parameter specified");

	path_utf8 = path.ToUTF8();

	OpenFifo();
}

86 87
inline void
FifoOutput::Delete()
88
{
89 90
	FormatDebug(fifo_output_domain,
		    "Removing FIFO \"%s\"", path_utf8.c_str());
91

92 93
	try {
		RemoveFile(path);
94 95
	} catch (...) {
		LogError(std::current_exception(), "Could not remove FIFO");
96 97 98
		return;
	}

99
	created = false;
100 101
}

102
void
103
FifoOutput::CloseFifo()
104
{
105 106 107
	if (input >= 0) {
		close(input);
		input = -1;
108 109
	}

110 111 112
	if (output >= 0) {
		close(output);
		output = -1;
113 114
	}

115 116
	FileInfo fi;
	if (created && GetFileInfo(path, fi))
117
		Delete();
118 119
}

120 121
inline void
FifoOutput::Create()
122
{
123 124
	if (!MakeFifo(path, 0666))
		throw FormatErrno("Couldn't create FIFO \"%s\"",
125
				  path_utf8.c_str());
126

127
	created = true;
128 129
}

130 131
inline void
FifoOutput::Check()
132 133
{
	struct stat st;
134
	if (!StatFile(path, st)) {
135 136
		if (errno == ENOENT) {
			/* Path doesn't exist */
137 138
			Create();
			return;
139 140
		}

141
		throw FormatErrno("Failed to stat FIFO \"%s\"",
142
				  path_utf8.c_str());
143 144
	}

145 146 147
	if (!S_ISFIFO(st.st_mode))
		throw FormatRuntimeError("\"%s\" already exists, but is not a FIFO",
					 path_utf8.c_str());
148 149
}

150 151 152 153
inline void
FifoOutput::OpenFifo()
try {
	Check();
154

155
	input = OpenFile(path, O_RDONLY|O_NONBLOCK|O_BINARY, 0).Steal();
156 157
	if (input < 0)
		throw FormatErrno("Could not open FIFO \"%s\" for reading",
158
				  path_utf8.c_str());
159

160
	output = OpenFile(path, O_WRONLY|O_NONBLOCK|O_BINARY, 0).Steal();
161 162
	if (output < 0)
		throw FormatErrno("Could not open FIFO \"%s\" for writing",
163
				  path_utf8.c_str());
164 165 166
} catch (...) {
	CloseFifo();
	throw;
167 168
}

169 170
void
FifoOutput::Open(AudioFormat &audio_format)
171
{
172
	timer = new Timer(audio_format);
173 174
}

175
void
176
FifoOutput::Close() noexcept
177
{
178
	delete timer;
179 180
}

181 182
void
FifoOutput::Cancel() noexcept
183
{
184
	timer->Reset();
185

186
	ssize_t bytes;
187
	do {
188 189
		char buffer[16384];
		bytes = read(input, buffer, sizeof(buffer));
190
	} while (bytes > 0 && errno != EINTR);
191 192

	if (bytes < 0 && errno != EAGAIN) {
193 194
		FormatErrno(fifo_output_domain,
			    "Flush of FIFO \"%s\" failed",
195
			    path_utf8.c_str());
196 197 198
	}
}

199
std::chrono::steady_clock::duration
200
FifoOutput::Delay() const noexcept
201
{
202
	return timer->IsStarted()
203
		? timer->GetDelay()
204
		: std::chrono::steady_clock::duration::zero();
205 206
}

207
size_t
208
FifoOutput::Play(const void *chunk, size_t size)
209
{
210 211 212
	if (!timer->IsStarted())
		timer->Start();
	timer->Add(size);
213

214
	while (true) {
215
		ssize_t bytes = write(output, chunk, size);
216 217 218
		if (bytes > 0)
			return (size_t)bytes;

219 220 221 222
		if (bytes < 0) {
			switch (errno) {
			case EAGAIN:
				/* The pipe is full, so empty it */
223
				Cancel();
224 225 226 227 228
				continue;
			case EINTR:
				continue;
			}

229
			throw FormatErrno("Failed to write to FIFO %s",
230
					  path_utf8.c_str());
231 232 233 234
		}
	}
}

235
const struct AudioOutputPlugin fifo_output_plugin = {
236 237
	"fifo",
	nullptr,
238
	&FifoOutput::Create,
239
	nullptr,
240
};