FifoOutputPlugin.cxx 4.71 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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

Rosen Penev's avatar
Rosen Penev committed
31 32
#include <cerrno>

33
#include <sys/stat.h>
34
#include <unistd.h>
35

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

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

45
public:
Max Kellermann's avatar
Max Kellermann committed
46
	explicit FifoOutput(const ConfigBlock &block);
47

48
	~FifoOutput() override {
49
		CloseFifo();
50 51
	}

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

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

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

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

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

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

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

	path_utf8 = path.ToUTF8();

	OpenFifo();
}

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

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

100
	created = false;
101 102
}

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

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

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

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

128
	created = true;
129 130
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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