PipeOutputPlugin.cxx 2.64 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * http://www.musicpd.org
 *
 * 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 "PipeOutputPlugin.hxx"
22
#include "../OutputAPI.hxx"
23
#include "../Wrapper.hxx"
24
#include "config/ConfigError.hxx"
25
#include "util/Error.hxx"
26

27
#include <string>
28

29 30
#include <stdio.h>

31
class PipeOutput {
32
	friend struct AudioOutputWrapper<PipeOutput>;
33

34
	AudioOutput base;
35

36
	std::string cmd;
37
	FILE *fh;
38

39 40 41
	PipeOutput()
		:base(pipe_output_plugin) {}

42
	bool Configure(const ConfigBlock &block, Error &error);
43

44
public:
45
	static PipeOutput *Create(const ConfigBlock &block, Error &error);
46

47 48 49 50 51 52 53
	bool Open(AudioFormat &audio_format, Error &error);

	void Close() {
		pclose(fh);
	}

	size_t Play(const void *chunk, size_t size, Error &error);
54 55
};

56
inline bool
57
PipeOutput::Configure(const ConfigBlock &block, Error &error)
58
{
59
	if (!base.Configure(block, error))
60 61
		return false;

62
	cmd = block.GetBlockValue("command", "");
63
	if (cmd.empty()) {
64 65
		error.Set(config_domain,
			  "No \"command\" parameter specified");
66 67 68 69 70 71
		return false;
	}

	return true;
}

72
inline PipeOutput *
73
PipeOutput::Create(const ConfigBlock &block, Error &error)
74
{
75
	PipeOutput *po = new PipeOutput();
76

77
	if (!po->Configure(block, error)) {
78
		delete po;
79
		return nullptr;
80 81
	}

82
	return po;
83 84
}

85 86
inline bool
PipeOutput::Open(gcc_unused AudioFormat &audio_format, Error &error)
87
{
88 89
	fh = popen(cmd.c_str(), "w");
	if (fh == nullptr) {
90
		error.FormatErrno("Error opening pipe \"%s\"",
91
				  cmd.c_str());
92 93 94 95 96 97
		return false;
	}

	return true;
}

98 99 100 101 102 103 104 105
inline size_t
PipeOutput::Play(const void *chunk, size_t size, Error &error)
{
	size_t nbytes = fwrite(chunk, 1, size, fh);
	if (nbytes == 0)
		error.SetErrno("Write error on pipe");

	return nbytes;
106 107
}

108
typedef AudioOutputWrapper<PipeOutput> Wrapper;
109

110
const struct AudioOutputPlugin pipe_output_plugin = {
111 112
	"pipe",
	nullptr,
113 114
	&Wrapper::Init,
	&Wrapper::Finish,
115 116
	nullptr,
	nullptr,
117 118
	&Wrapper::Open,
	&Wrapper::Close,
119 120
	nullptr,
	nullptr,
121
	&Wrapper::Play,
122 123 124 125
	nullptr,
	nullptr,
	nullptr,
	nullptr,
126
};