run_output.cxx 3.63 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
#include "util/StringBuffer.hxx"
30 31
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"
32
#include "util/PrintException.hxx"
33

34
#include <cassert>
35 36
#include <memory>

37 38
#include <string.h>
#include <unistd.h>
39
#include <stdlib.h>
40
#include <stdio.h>
41

42
static std::unique_ptr<AudioOutput>
43 44
LoadAudioOutput(const ConfigData &config, EventLoop &event_loop,
		const char *name)
45
{
46 47
	const auto *block = config.FindBlock(ConfigBlockOption::AUDIO_OUTPUT,
					     "name", name);
48 49
	if (block == nullptr)
		throw FormatRuntimeError("No such configured audio output: %s",
50
					 name);
51

52 53 54 55 56 57 58 59 60
	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);

61 62
	return std::unique_ptr<AudioOutput>(ao_plugin_init(event_loop, *plugin,
							   *block));
63 64
}

65
static void
66
run_output(AudioOutput &ao, AudioFormat audio_format)
67 68 69
{
	/* open the audio output */

70 71
	ao.Enable();
	AtScopeExit(&ao) { ao.Disable(); };
72

73 74
	ao.Open(audio_format);
	AtScopeExit(&ao) { ao.Close(); };
75

76
	fprintf(stderr, "audio_format=%s\n",
77
		ToString(audio_format).c_str());
78

79
	size_t frame_size = audio_format.GetFrameSize();
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

	/* play */

	size_t length = 0;
	char buffer[4096];
	while (true) {
		if (length < sizeof(buffer)) {
			ssize_t nbytes = read(0, buffer + length,
					      sizeof(buffer) - length);
			if (nbytes <= 0)
				break;

			length += (size_t)nbytes;
		}

		size_t play_length = (length / frame_size) * frame_size;
		if (play_length > 0) {
97
			size_t consumed = ao.Play(buffer, play_length);
98 99 100 101 102 103 104 105 106 107

			assert(consumed <= length);
			assert(consumed % frame_size == 0);

			length -= consumed;
			memmove(buffer, buffer + consumed, length);
		}
	}
}

108
int main(int argc, char **argv)
109
try {
110
	if (argc < 3 || argc > 4) {
111 112
		fprintf(stderr, "Usage: run_output CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
113 114
	}

115
	const FromNarrowPath config_path = argv[1];
116

117
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
118

119 120
	/* read configuration file (mpd.conf) */

121
	const auto config = AutoLoadConfigFile(config_path);
122

123 124
	EventThread io_thread;
	io_thread.Start();
125

126 127
	/* initialize the audio output */

128
	auto ao = LoadAudioOutput(config, io_thread.GetEventLoop(), argv[2]);
129 130 131

	/* parse the audio format */

132 133
	if (argc > 3)
		audio_format = ParseAudioFormat(argv[3], false);
134

135
	/* do it */
136

137
	run_output(*ao, audio_format);
138 139 140

	/* cleanup and exit */

141
	return EXIT_SUCCESS;
142 143
} catch (...) {
	PrintException(std::current_exception());
144
	return EXIT_FAILURE;
145
}