run_output.cxx 4.67 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "output/Internal.hxx"
22
#include "output/OutputPlugin.hxx"
23
#include "config/Param.hxx"
24 25
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "Idle.hxx"
27 28
#include "Main.hxx"
#include "event/Loop.hxx"
29
#include "ScopeIOThread.hxx"
30
#include "fs/Path.hxx"
31
#include "AudioParser.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "pcm/PcmConvert.hxx"
33
#include "filter/FilterRegistry.hxx"
34
#include "player/Control.hxx"
35
#include "util/Error.hxx"
36
#include "Log.hxx"
37 38 39 40

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

44
const struct filter_plugin *
45
filter_plugin_by_name(gcc_unused const char *name)
46 47 48 49 50
{
	assert(false);
	return NULL;
}

51 52
PlayerControl::PlayerControl(PlayerListener &_listener,
			     MultipleOutputs &_outputs,
53 54 55 56 57
			     unsigned _buffer_chunks,
			     unsigned _buffered_before_play)
	:listener(_listener), outputs(_outputs),
	 buffer_chunks(_buffer_chunks),
	 buffered_before_play(_buffered_before_play) {}
58
PlayerControl::~PlayerControl() {}
59

60
static AudioOutput *
61
load_audio_output(EventLoop &event_loop, const char *name)
62
{
63 64
	const auto *param = config_find_block(ConfigBlockOption::AUDIO_OUTPUT,
					      "name", name);
65
	if (param == NULL) {
66
		fprintf(stderr, "No such configured audio output: %s\n", name);
67
		return nullptr;
68 69
	}

70 71
	static struct PlayerControl dummy_player_control(*(PlayerListener *)nullptr,
							 *(MultipleOutputs *)nullptr,
72
							 32, 4);
73

74
	Error error;
75
	AudioOutput *ao =
76 77 78
		audio_output_new(event_loop, *param,
				 *(MixerListener *)nullptr,
				 dummy_player_control,
79
				 error);
80
	if (ao == nullptr)
81
		LogError(error);
82

83
	return ao;
84 85
}

86
static bool
87
run_output(AudioOutput *ao, AudioFormat audio_format)
88 89 90
{
	/* open the audio output */

91 92
	Error error;
	if (!ao_plugin_enable(ao, error)) {
93
		LogError(error, "Failed to enable audio output");
94 95 96
		return false;
	}

97
	if (!ao_plugin_open(ao, audio_format, error)) {
98
		ao_plugin_disable(ao);
99
		LogError(error, "Failed to open audio output");
100 101 102 103
		return false;
	}

	struct audio_format_string af_string;
104 105
	fprintf(stderr, "audio_format=%s\n",
		audio_format_to_string(audio_format, &af_string));
106

107
	size_t frame_size = audio_format.GetFrameSize();
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

	/* 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) {
125
			size_t consumed = ao_plugin_play(ao,
126
							 buffer, play_length,
127
							 error);
128
			if (consumed == 0) {
129
				ao_plugin_close(ao);
130
				ao_plugin_disable(ao);
131
				LogError(error, "Failed to play");
132 133 134 135 136 137 138 139 140 141 142
				return false;
			}

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

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

143
	ao_plugin_close(ao);
144
	ao_plugin_disable(ao);
145 146 147
	return true;
}

148
int main(int argc, char **argv)
149
try {
150
	Error error;
151 152

	if (argc < 3 || argc > 4) {
153 154
		fprintf(stderr, "Usage: run_output CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
155 156
	}

157 158
	const Path config_path = Path::FromFS(argv[1]);

159
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
160

161 162 163
	/* read configuration file (mpd.conf) */

	config_global_init();
164
	ReadConfigFile(config_path);
165

166
	EventLoop event_loop;
167

168
	const ScopeIOThread io_thread;
169

170 171
	/* initialize the audio output */

172
	AudioOutput *ao = load_audio_output(event_loop, argv[2]);
173
	if (ao == NULL)
174 175 176 177 178
		return 1;

	/* parse the audio format */

	if (argc > 3) {
179
		if (!audio_format_parse(audio_format, argv[3], false, error)) {
180 181
			LogError(error, "Failed to parse audio format");
			return EXIT_FAILURE;
182 183 184
		}
	}

185
	/* do it */
186

187
	bool success = run_output(ao, audio_format);
188 189 190

	/* cleanup and exit */

191
	audio_output_free(ao);
192 193 194

	config_global_finish();

195
	return success ? EXIT_SUCCESS : EXIT_FAILURE;
196 197 198 199
 } catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
 }