run_output.cxx 4.79 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 "output/Internal.hxx"
22
#include "output/OutputPlugin.hxx"
23 24 25
#include "config/ConfigData.hxx"
#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 "PlayerControl.hxx"
35
#include "stdbin.h"
36
#include "util/Error.hxx"
37
#include "Log.hxx"
38

39
#ifdef HAVE_GLIB
40
#include <glib.h>
41
#endif
42 43 44 45

#include <assert.h>
#include <string.h>
#include <unistd.h>
46
#include <stdlib.h>
47
#include <stdio.h>
48

49
const struct filter_plugin *
50
filter_plugin_by_name(gcc_unused const char *name)
51 52 53 54 55
{
	assert(false);
	return NULL;
}

56 57
PlayerControl::PlayerControl(PlayerListener &_listener,
			     MultipleOutputs &_outputs,
58 59 60 61 62
			     unsigned _buffer_chunks,
			     unsigned _buffered_before_play)
	:listener(_listener), outputs(_outputs),
	 buffer_chunks(_buffer_chunks),
	 buffered_before_play(_buffered_before_play) {}
63
PlayerControl::~PlayerControl() {}
64

65
static AudioOutput *
66
load_audio_output(EventLoop &event_loop, const char *name)
67
{
68 69
	const config_param *param =
		config_find_block(CONF_AUDIO_OUTPUT, "name", name);
70
	if (param == NULL) {
71
		fprintf(stderr, "No such configured audio output: %s\n", name);
72
		return nullptr;
73 74
	}

75 76
	static struct PlayerControl dummy_player_control(*(PlayerListener *)nullptr,
							 *(MultipleOutputs *)nullptr,
77
							 32, 4);
78

79
	Error error;
80
	AudioOutput *ao =
81 82 83
		audio_output_new(event_loop, *param,
				 *(MixerListener *)nullptr,
				 dummy_player_control,
84
				 error);
85
	if (ao == nullptr)
86
		LogError(error);
87

88
	return ao;
89 90
}

91
static bool
92
run_output(AudioOutput *ao, AudioFormat audio_format)
93 94 95
{
	/* open the audio output */

96 97
	Error error;
	if (!ao_plugin_enable(ao, error)) {
98
		LogError(error, "Failed to enable audio output");
99 100 101
		return false;
	}

102
	if (!ao_plugin_open(ao, audio_format, error)) {
103
		ao_plugin_disable(ao);
104
		LogError(error, "Failed to open audio output");
105 106 107 108
		return false;
	}

	struct audio_format_string af_string;
109 110
	fprintf(stderr, "audio_format=%s\n",
		audio_format_to_string(audio_format, &af_string));
111

112
	size_t frame_size = audio_format.GetFrameSize();
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

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

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

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

148
	ao_plugin_close(ao);
149
	ao_plugin_disable(ao);
150 151 152
	return true;
}

153 154
int main(int argc, char **argv)
{
155
	Error error;
156 157

	if (argc < 3 || argc > 4) {
158 159
		fprintf(stderr, "Usage: run_output CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
160 161
	}

162 163
	const Path config_path = Path::FromFS(argv[1]);

164
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
165

166
#ifdef HAVE_GLIB
167
#if !GLIB_CHECK_VERSION(2,32,0)
168
	g_thread_init(NULL);
169
#endif
170
#endif
171 172 173 174

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

	config_global_init();
175
	if (!ReadConfigFile(config_path, error)) {
176 177
		LogError(error);
		return EXIT_FAILURE;
178
	}
179

180
	EventLoop event_loop;
181

182
	const ScopeIOThread io_thread;
183

184 185
	/* initialize the audio output */

186
	AudioOutput *ao = load_audio_output(event_loop, argv[2]);
187
	if (ao == NULL)
188 189 190 191 192
		return 1;

	/* parse the audio format */

	if (argc > 3) {
193
		if (!audio_format_parse(audio_format, argv[3], false, error)) {
194 195
			LogError(error, "Failed to parse audio format");
			return EXIT_FAILURE;
196 197 198
		}
	}

199
	/* do it */
200

201
	bool success = run_output(ao, audio_format);
202 203 204

	/* cleanup and exit */

205
	audio_output_free(ao);
206 207 208

	config_global_finish();

209
	return success ? EXIT_SUCCESS : EXIT_FAILURE;
210
}