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

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

37 38 39
class FifoOutput {
	friend struct AudioOutputWrapper<FifoOutput>;

40
	AudioOutput base;
41

42
	const AllocatedPath path;
43 44
	std::string path_utf8;

45 46 47
	int input = -1;
	int output = -1;
	bool created = false;
48
	Timer *timer;
49

50
public:
51
	FifoOutput(const ConfigBlock &block);
52

53
	~FifoOutput() {
54
		CloseFifo();
55 56
	}

57
	static FifoOutput *Create(const ConfigBlock &block);
58

59 60
	void Create();
	void Check();
61 62
	void Delete();

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

66
	void Open(AudioFormat &audio_format);
67 68
	void Close();

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

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

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

	path_utf8 = path.ToUTF8();

	OpenFifo();
}

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

94 95 96 97
	try {
		RemoveFile(path);
	} catch (const std::runtime_error &e) {
		LogError(e, "Could not remove FIFO");
98 99 100
		return;
	}

101
	created = false;
102 103
}

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

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

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

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

129
	created = true;
130 131
}

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

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

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

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

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

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

171
inline FifoOutput *
172
FifoOutput::Create(const ConfigBlock &block)
173
{
174
	return new FifoOutput(block);
175 176
}

177 178
void
FifoOutput::Open(AudioFormat &audio_format)
179
{
180
	timer = new Timer(audio_format);
181 182
}

183 184
void
FifoOutput::Close()
185
{
186
	delete timer;
187 188
}

189 190
inline void
FifoOutput::Cancel()
191
{
192
	timer->Reset();
193

194
	ssize_t bytes;
195
	do {
196 197
		char buffer[16384];
		bytes = read(input, buffer, sizeof(buffer));
198
	} while (bytes > 0 && errno != EINTR);
199 200

	if (bytes < 0 && errno != EAGAIN) {
201 202
		FormatErrno(fifo_output_domain,
			    "Flush of FIFO \"%s\" failed",
203
			    path_utf8.c_str());
204 205 206
	}
}

207
inline std::chrono::steady_clock::duration
208
FifoOutput::Delay() const noexcept
209
{
210
	return timer->IsStarted()
211
		? timer->GetDelay()
212
		: std::chrono::steady_clock::duration::zero();
213 214
}

215
inline size_t
216
FifoOutput::Play(const void *chunk, size_t size)
217
{
218 219 220
	if (!timer->IsStarted())
		timer->Start();
	timer->Add(size);
221

222
	while (true) {
223
		ssize_t bytes = write(output, chunk, size);
224 225 226
		if (bytes > 0)
			return (size_t)bytes;

227 228 229 230
		if (bytes < 0) {
			switch (errno) {
			case EAGAIN:
				/* The pipe is full, so empty it */
231
				Cancel();
232 233 234 235 236
				continue;
			case EINTR:
				continue;
			}

237
			throw FormatErrno("Failed to write to FIFO %s",
238
					  path_utf8.c_str());
239 240 241 242
		}
	}
}

243 244
typedef AudioOutputWrapper<FifoOutput> Wrapper;

245
const struct AudioOutputPlugin fifo_output_plugin = {
246 247
	"fifo",
	nullptr,
248 249
	&Wrapper::Init,
	&Wrapper::Finish,
250 251
	nullptr,
	nullptr,
252 253
	&Wrapper::Open,
	&Wrapper::Close,
254
	&Wrapper::Delay,
255
	nullptr,
256
	&Wrapper::Play,
257
	nullptr,
258
	&Wrapper::Cancel,
259 260
	nullptr,
	nullptr,
261
};