run_output.cxx 4.57 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9
 * 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.
 *
10
 * This program is distributed in the hope that it will  useful,
11 12 13
 * 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 21
#include "output/Interface.hxx"
#include "output/Registry.hxx"
22
#include "output/OutputPlugin.hxx"
23
#include "ConfigGlue.hxx"
24
#include "event/Thread.hxx"
25
#include "fs/Path.hxx"
26
#include "fs/NarrowPath.hxx"
27 28
#include "pcm/AudioParser.hxx"
#include "pcm/AudioFormat.hxx"
29 30
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
31
#include "util/StringBuffer.hxx"
32 33
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"
34
#include "util/StaticFifoBuffer.hxx"
35
#include "util/PrintException.hxx"
36
#include "LogBackend.hxx"
37

38
#include <cassert>
39 40
#include <memory>

41 42
#include <string.h>
#include <unistd.h>
43
#include <stdlib.h>
44
#include <stdio.h>
45

46 47 48 49 50 51
struct CommandLine {
	FromNarrowPath config_path;

	const char *output_name = nullptr;

	AudioFormat audio_format{44100, SampleFormat::S16, 2};
52 53 54 55 56 57 58 59 60 61

	bool verbose = false;
};

enum Option {
	OPTION_VERBOSE,
};

static constexpr OptionDef option_defs[] = {
	{"verbose", 'v', false, "Verbose logging"},
62 63 64 65 66 67 68
};

static CommandLine
ParseCommandLine(int argc, char **argv)
{
	CommandLine c;

69 70 71 72 73 74 75 76 77 78 79
	OptionParser option_parser(option_defs, argc, argv);
	while (auto o = option_parser.Next()) {
		switch (Option(o.index)) {
		case OPTION_VERBOSE:
			c.verbose = true;
			break;
		}
	}

	auto args = option_parser.GetRemaining();
	if (args.size < 2 || args.size > 3)
80 81
		throw std::runtime_error("Usage: run_output CONFIG NAME [FORMAT] <IN");

82 83
	c.config_path = args[0];
	c.output_name = args[1];
84

85 86
	if (args.size > 2)
		c.audio_format = ParseAudioFormat(args[2], false);
87 88 89 90

	return c;
}

91
static std::unique_ptr<AudioOutput>
92 93
LoadAudioOutput(const ConfigData &config, EventLoop &event_loop,
		const char *name)
94
{
95 96
	const auto *block = config.FindBlock(ConfigBlockOption::AUDIO_OUTPUT,
					     "name", name);
97 98
	if (block == nullptr)
		throw FormatRuntimeError("No such configured audio output: %s",
99
					 name);
100

101 102 103 104 105 106 107 108
	const char *plugin_name = block->GetBlockValue("type");
	if (plugin_name == nullptr)
		throw std::runtime_error("Missing \"type\" configuration");

	const auto *plugin = AudioOutputPlugin_get(plugin_name);
	if (plugin == nullptr)
		throw FormatRuntimeError("No such audio output plugin: %s",
					 plugin_name);
109 110
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
111

112 113
	return std::unique_ptr<AudioOutput>(ao_plugin_init(event_loop, *plugin,
							   *block));
114 115
}

116
static void
117 118
RunOutput(AudioOutput &ao, AudioFormat audio_format,
	  FileDescriptor in_fd)
119
{
120 121
	in_fd.SetBinaryMode();

122 123
	/* open the audio output */

124 125
	ao.Enable();
	AtScopeExit(&ao) { ao.Disable(); };
126

127 128
	ao.Open(audio_format);
	AtScopeExit(&ao) { ao.Close(); };
129

130
	fprintf(stderr, "audio_format=%s\n",
131
		ToString(audio_format).c_str());
132

133
	const size_t in_frame_size = audio_format.GetFrameSize();
134 135 136

	/* play */

137 138
	StaticFifoBuffer<std::byte, 4096> buffer;

139
	while (true) {
140 141 142 143 144
		{
			const auto dest = buffer.Write();
			assert(!dest.empty());

			ssize_t nbytes = in_fd.Read(dest.data, dest.size);
145 146 147
			if (nbytes <= 0)
				break;

148
			buffer.Append(nbytes);
149 150
		}

151 152
		auto src = buffer.Read();
		assert(!src.empty());
153

154 155 156
		src.size -= src.size % in_frame_size;
		if (src.empty())
			continue;
157

158 159 160 161 162 163
		size_t consumed = ao.Play(src.data, src.size);

		assert(consumed <= src.size);
		assert(consumed % in_frame_size == 0);

		buffer.Consume(consumed);
164
	}
165 166

	ao.Drain();
167 168
}

169
int main(int argc, char **argv)
170
try {
171
	const auto c = ParseCommandLine(argc, argv);
172
	SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);
173

174 175
	/* read configuration file (mpd.conf) */

176
	const auto config = AutoLoadConfigFile(c.config_path);
177

178 179
	EventThread io_thread;
	io_thread.Start();
180

181 182
	/* initialize the audio output */

183 184
	auto ao = LoadAudioOutput(config, io_thread.GetEventLoop(),
				  c.output_name);
185

186
	/* do it */
187

188
	RunOutput(*ao, c.audio_format, FileDescriptor(STDIN_FILENO));
189 190 191

	/* cleanup and exit */

192
	return EXIT_SUCCESS;
193 194
} catch (...) {
	PrintException(std::current_exception());
195
	return EXIT_FAILURE;
196
}