FifoOutputPlugin.cxx 5.54 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "config/ConfigError.hxx"
23
#include "../OutputAPI.hxx"
24
#include "../Wrapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "../Timer.hxx"
26
#include "fs/AllocatedPath.hxx"
27
#include "fs/FileSystem.hxx"
28
#include "fs/FileInfo.hxx"
29 30
#include "util/Error.hxx"
#include "util/Domain.hxx"
31
#include "Log.hxx"
32
#include "open.h"
33

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

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

41
	AudioOutput base;
42

43
	AllocatedPath path;
44 45
	std::string path_utf8;

46 47
	int input;
	int output;
48
	bool created;
49
	Timer *timer;
50

51
public:
52
	FifoOutput()
53 54
		:base(fifo_output_plugin),
		 path(AllocatedPath::Null()), input(-1), output(-1),
55
		 created(false) {}
56

57
	~FifoOutput() {
58
		CloseFifo();
59 60
	}

61 62
	bool Initialize(const ConfigBlock &block, Error &error) {
		return base.Configure(block, error);
63 64
	}

65 66
	static FifoOutput *Create(const ConfigBlock &block, Error &error);

67 68
	bool Create(Error &error);
	bool Check(Error &error);
69 70
	void Delete();

71 72
	bool OpenFifo(Error &error);
	void CloseFifo();
73

74 75 76
	bool Open(AudioFormat &audio_format, Error &error);
	void Close();

77 78 79
	unsigned Delay() const;
	size_t Play(const void *chunk, size_t size, Error &error);
	void Cancel();
Max Kellermann's avatar
Max Kellermann committed
80
};
81

82
static constexpr Domain fifo_output_domain("fifo_output");
83

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

90
	if (!RemoveFile(path)) {
91 92 93
		FormatErrno(fifo_output_domain,
			    "Could not remove FIFO \"%s\"",
			    path_utf8.c_str());
94 95 96
		return;
	}

97
	created = false;
98 99
}

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

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

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

118
inline bool
119
FifoOutput::Create(Error &error)
120
{
121
	if (!MakeFifo(path, 0666)) {
122 123
		error.FormatErrno("Couldn't create FIFO \"%s\"",
				  path_utf8.c_str());
124
		return false;
125 126
	}

127
	created = true;
128
	return true;
129 130
}

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

141 142
		error.FormatErrno("Failed to stat FIFO \"%s\"",
				  path_utf8.c_str());
143
		return false;
144 145 146
	}

	if (!S_ISFIFO(st.st_mode)) {
147 148 149
		error.Format(fifo_output_domain,
			     "\"%s\" already exists, but is not a FIFO",
			     path_utf8.c_str());
150
		return false;
151 152
	}

153
	return true;
154 155
}

156
inline bool
157
FifoOutput::OpenFifo(Error &error)
158
{
159
	if (!Check(error))
160
		return false;
161

162
	input = OpenFile(path, O_RDONLY|O_NONBLOCK|O_BINARY, 0);
163
	if (input < 0) {
164 165
		error.FormatErrno("Could not open FIFO \"%s\" for reading",
				  path_utf8.c_str());
166
		CloseFifo();
167
		return false;
168 169
	}

170
	output = OpenFile(path, O_WRONLY|O_NONBLOCK|O_BINARY, 0);
171
	if (output < 0) {
172 173
		error.FormatErrno("Could not open FIFO \"%s\" for writing",
				  path_utf8.c_str());
174
		CloseFifo();
175
		return false;
176 177
	}

178
	return true;
179 180
}

181 182
inline FifoOutput *
FifoOutput::Create(const ConfigBlock &block, Error &error)
183
{
184 185
	FifoOutput *fd = new FifoOutput();

186
	fd->path = block.GetBlockPath("path", error);
187 188
	if (fd->path.IsNull()) {
		delete fd;
189 190 191 192

		if (!error.IsDefined())
			error.Set(config_domain,
				  "No \"path\" parameter specified");
193
		return nullptr;
194 195
	}

196
	fd->path_utf8 = fd->path.ToUTF8();
197

198
	if (!fd->Initialize(block, error)) {
199 200
		delete fd;
		return nullptr;
201 202
	}

203
	if (!fd->OpenFifo(error)) {
204 205
		delete fd;
		return nullptr;
206 207
	}

208
	return fd;
209 210
}

211 212
bool
FifoOutput::Open(AudioFormat &audio_format, gcc_unused Error &error)
213
{
214
	timer = new Timer(audio_format);
215
	return true;
216 217
}

218 219
void
FifoOutput::Close()
220
{
221
	delete timer;
222 223
}

224 225
inline void
FifoOutput::Cancel()
226
{
227
	timer->Reset();
228

229
	ssize_t bytes;
230
	do {
231 232
		char buffer[16384];
		bytes = read(input, buffer, sizeof(buffer));
233
	} while (bytes > 0 && errno != EINTR);
234 235

	if (bytes < 0 && errno != EAGAIN) {
236 237
		FormatErrno(fifo_output_domain,
			    "Flush of FIFO \"%s\" failed",
238
			    path_utf8.c_str());
239 240 241
	}
}

242 243
inline unsigned
FifoOutput::Delay() const
244
{
245 246
	return timer->IsStarted()
		? timer->GetDelay()
247 248 249
		: 0;
}

250 251
inline size_t
FifoOutput::Play(const void *chunk, size_t size, Error &error)
252
{
253 254 255
	if (!timer->IsStarted())
		timer->Start();
	timer->Add(size);
256

257
	while (true) {
258
		ssize_t bytes = write(output, chunk, size);
259 260 261
		if (bytes > 0)
			return (size_t)bytes;

262 263 264 265
		if (bytes < 0) {
			switch (errno) {
			case EAGAIN:
				/* The pipe is full, so empty it */
266
				Cancel();
267 268 269 270 271
				continue;
			case EINTR:
				continue;
			}

272
			error.FormatErrno("Failed to write to FIFO %s",
273
					  path_utf8.c_str());
274
			return 0;
275 276 277 278
		}
	}
}

279 280
typedef AudioOutputWrapper<FifoOutput> Wrapper;

281
const struct AudioOutputPlugin fifo_output_plugin = {
282 283
	"fifo",
	nullptr,
284 285
	&Wrapper::Init,
	&Wrapper::Finish,
286 287
	nullptr,
	nullptr,
288 289
	&Wrapper::Open,
	&Wrapper::Close,
290
	&Wrapper::Delay,
291
	nullptr,
292
	&Wrapper::Play,
293
	nullptr,
294
	&Wrapper::Cancel,
295 296
	nullptr,
	nullptr,
297
};