PipeOutputPlugin.cxx 2.78 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
#include "util/Error.hxx"
25

26
#include <string>
27

28 29
#include <stdio.h>

30
struct PipeOutput {
31
	AudioOutput base;
32

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

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

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

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

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

	return true;
}

59
static AudioOutput *
60
pipe_output_init(const config_param &param, Error &error)
61
{
62
	PipeOutput *pd = new PipeOutput();
63

64
	if (!pd->Initialize(param, error)) {
65 66
		delete pd;
		return nullptr;
67 68
	}

69
	if (!pd->Configure(param, error)) {
70 71
		delete pd;
		return nullptr;
72 73
	}

74
	return &pd->base;
75 76 77
}

static void
78
pipe_output_finish(AudioOutput *ao)
79
{
80
	PipeOutput *pd = (PipeOutput *)ao;
81

82
	delete pd;
83 84 85
}

static bool
86
pipe_output_open(AudioOutput *ao,
87
		 gcc_unused AudioFormat &audio_format,
88
		 Error &error)
89
{
90
	PipeOutput *pd = (PipeOutput *)ao;
91

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

	return true;
}

static void
103
pipe_output_close(AudioOutput *ao)
104
{
105
	PipeOutput *pd = (PipeOutput *)ao;
106 107 108 109 110

	pclose(pd->fh);
}

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

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

	return ret;
}

124
const struct AudioOutputPlugin pipe_output_plugin = {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	"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,
140
};