run_output.cxx 3.71 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
#include "config.h"
21 22
#include "output/Interface.hxx"
#include "output/Registry.hxx"
23
#include "output/OutputPlugin.hxx"
24
#include "config/Param.hxx"
25 26
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
27
#include "config/Block.hxx"
28
#include "event/Thread.hxx"
29
#include "fs/Path.hxx"
30
#include "AudioParser.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "pcm/PcmConvert.hxx"
32
#include "util/StringBuffer.hxx"
33 34
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"
35
#include "Log.hxx"
36

37 38
#include <memory>

39 40 41
#include <assert.h>
#include <string.h>
#include <unistd.h>
42
#include <stdlib.h>
43
#include <stdio.h>
44

45
static std::unique_ptr<AudioOutput>
46
load_audio_output(EventLoop &event_loop, const char *name)
47
{
48
	const auto *block = config_find_block(ConfigBlockOption::AUDIO_OUTPUT,
49
					      "name", name);
50 51
	if (block == nullptr)
		throw FormatRuntimeError("No such configured audio output: %s",
52
					 name);
53

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

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

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

72 73
	ao.Enable();
	AtScopeExit(&ao) { ao.Disable(); };
74

75 76
	ao.Open(audio_format);
	AtScopeExit(&ao) { ao.Close(); };
77

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

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

	/* 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) {
99
			size_t consumed = ao.Play(buffer, play_length);
100 101 102 103 104 105 106 107 108 109

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

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

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

117 118
	const Path config_path = Path::FromFS(argv[1]);

119
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
120

121 122 123
	/* read configuration file (mpd.conf) */

	config_global_init();
124
	ReadConfigFile(config_path);
125

126 127
	EventThread io_thread;
	io_thread.Start();
128

129 130
	/* initialize the audio output */

131
	auto ao = load_audio_output(io_thread.GetEventLoop(), argv[2]);
132 133 134

	/* parse the audio format */

135 136
	if (argc > 3)
		audio_format = ParseAudioFormat(argv[3], false);
137

138
	/* do it */
139

140
	run_output(*ao, audio_format);
141 142 143

	/* cleanup and exit */

144
	ao.reset();
145 146 147

	config_global_finish();

148
	return EXIT_SUCCESS;
149 150 151 152
 } catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
 }