PipeOutputPlugin.cxx 2.87 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "config/ConfigError.hxx"
24 25
#include "util/Error.hxx"
#include "util/Domain.hxx"
26

27
#include <string>
28

29 30
#include <stdio.h>

31
struct PipeOutput {
32
	AudioOutput base;
33

34
	std::string cmd;
35
	FILE *fh;
36

37 38 39
	PipeOutput()
		:base(pipe_output_plugin) {}

40
	bool Initialize(const config_param &param, Error &error) {
41
		return base.Configure(param, error);
42 43
	}

44
	bool Configure(const config_param &param, Error &error);
45 46
};

47
static constexpr Domain pipe_output_domain("pipe_output");
48

49
inline bool
50
PipeOutput::Configure(const config_param &param, Error &error)
51
{
52 53
	cmd = param.GetBlockValue("command", "");
	if (cmd.empty()) {
54 55
		error.Set(config_domain,
			  "No \"command\" parameter specified");
56 57 58 59 60 61
		return false;
	}

	return true;
}

62
static AudioOutput *
63
pipe_output_init(const config_param &param, Error &error)
64
{
65
	PipeOutput *pd = new PipeOutput();
66

67
	if (!pd->Initialize(param, error)) {
68 69
		delete pd;
		return nullptr;
70 71
	}

72
	if (!pd->Configure(param, error)) {
73 74
		delete pd;
		return nullptr;
75 76
	}

77
	return &pd->base;
78 79 80
}

static void
81
pipe_output_finish(AudioOutput *ao)
82
{
83
	PipeOutput *pd = (PipeOutput *)ao;
84

85
	delete pd;
86 87 88
}

static bool
89
pipe_output_open(AudioOutput *ao,
90
		 gcc_unused AudioFormat &audio_format,
91
		 Error &error)
92
{
93
	PipeOutput *pd = (PipeOutput *)ao;
94

95
	pd->fh = popen(pd->cmd.c_str(), "w");
96
	if (pd->fh == nullptr) {
97
		error.FormatErrno("Error opening pipe \"%s\"",
98
				  pd->cmd.c_str());
99 100 101 102 103 104 105
		return false;
	}

	return true;
}

static void
106
pipe_output_close(AudioOutput *ao)
107
{
108
	PipeOutput *pd = (PipeOutput *)ao;
109 110 111 112 113

	pclose(pd->fh);
}

static size_t
114
pipe_output_play(AudioOutput *ao, const void *chunk, size_t size,
115
		 Error &error)
116
{
117
	PipeOutput *pd = (PipeOutput *)ao;
118 119 120 121
	size_t ret;

	ret = fwrite(chunk, 1, size, pd->fh);
	if (ret == 0)
122
		error.SetErrno("Write error on pipe");
123 124 125 126

	return ret;
}

127
const struct AudioOutputPlugin pipe_output_plugin = {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	"pipe",
	nullptr,
	pipe_output_init,
	pipe_output_finish,
	nullptr,
	nullptr,
	pipe_output_open,
	pipe_output_close,
	nullptr,
	nullptr,
	pipe_output_play,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
143
};