aboutsummaryrefslogtreecommitdiffstats
path: root/test/run_output.c
blob: 8ba5c1d8a744313664f3cb720ad2e21d52a96952 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (C) 2003-2010 The Music Player Daemon Project
 * 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.
 *
 * 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.
 */

#include "config.h"
#include "output_plugin.h"
#include "output_internal.h"
#include "output_control.h"
#include "conf.h"
#include "audio_parser.h"
#include "filter_registry.h"
#include "pcm_convert.h"
#include "event_pipe.h"
#include "idle.h"
#include "playlist.h"
#include "player_control.h"
#include "stdbin.h"

#include <glib.h>

#include <assert.h>
#include <string.h>
#include <unistd.h>

struct playlist g_playlist;

void
idle_add(G_GNUC_UNUSED unsigned flags)
{
}

void
event_pipe_emit(G_GNUC_UNUSED enum pipe_event event)
{
}

void pcm_convert_init(G_GNUC_UNUSED struct pcm_convert_state *state)
{
}

void pcm_convert_deinit(G_GNUC_UNUSED struct pcm_convert_state *state)
{
}

const void *
pcm_convert(G_GNUC_UNUSED struct pcm_convert_state *state,
	    G_GNUC_UNUSED const struct audio_format *src_format,
	    G_GNUC_UNUSED const void *src, G_GNUC_UNUSED size_t src_size,
	    G_GNUC_UNUSED const struct audio_format *dest_format,
	    G_GNUC_UNUSED size_t *dest_size_r,
	    GError **error_r)
{
	g_set_error(error_r, pcm_convert_quark(), 0,
		    "Not implemented");
	return NULL;
}

const struct filter_plugin *
filter_plugin_by_name(G_GNUC_UNUSED const char *name)
{
	assert(false);
	return NULL;
}

static const struct config_param *
find_named_config_block(const char *block, const char *name)
{
	const struct config_param *param = NULL;

	while ((param = config_get_next_param(block, param)) != NULL) {
		const char *current_name =
			config_get_block_string(param, "name", NULL);
		if (current_name != NULL && strcmp(current_name, name) == 0)
			return param;
	}

	return NULL;
}

static bool
load_audio_output(struct audio_output *ao, const char *name)
{
	const struct config_param *param;
	bool success;
	GError *error = NULL;

	param = find_named_config_block(CONF_AUDIO_OUTPUT, name);
	if (param == NULL) {
		g_printerr("No such configured audio output: %s\n", name);
		return false;
	}

	static struct player_control dummy_player_control;

	success = audio_output_init(ao, param, &dummy_player_control, &error);
	if (!success) {
		g_printerr("%s\n", error->message);
		g_error_free(error);
	}

	return success;
}

int main(int argc, char **argv)
{
	struct audio_output ao;
	struct audio_format audio_format;
	struct audio_format_string af_string;
	bool success;
	GError *error = NULL;
	char buffer[4096];
	ssize_t nbytes;
	size_t frame_size, length = 0, play_length, consumed;

	if (argc < 3 || argc > 4) {
		g_printerr("Usage: run_output CONFIG NAME [FORMAT] <IN\n");
		return 1;
	}

	audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);

	g_thread_init(NULL);

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

	config_global_init();
	success = config_read_file(argv[1], &error);
	if (!success) {
		g_printerr("%s:", error->message);
		g_error_free(error);
		return 1;
	}

	/* initialize the audio output */

	if (!load_audio_output(&ao, argv[2]))
		return 1;

	/* parse the audio format */

	if (argc > 3) {
		success = audio_format_parse(&audio_format, argv[3],
					     false, &error);
		if (!success) {
			g_printerr("Failed to parse audio format: %s\n",
				   error->message);
			g_error_free(error);
			return 1;
		}
	}

	/* open the audio output */

	success = ao_plugin_open(ao.plugin, ao.data, &audio_format, &error);
	if (!success) {
		g_printerr("Failed to open audio output: %s\n",
			   error->message);
		g_error_free(error);
		return 1;
	}

	g_printerr("audio_format=%s\n",
		   audio_format_to_string(&audio_format, &af_string));

	frame_size = audio_format_frame_size(&audio_format);

	/* play */

	while (true) {
		if (length < sizeof(buffer)) {
			nbytes = read(0, buffer + length, sizeof(buffer) - length);
			if (nbytes <= 0)
				break;

			length += (size_t)nbytes;
		}

		play_length = (length / frame_size) * frame_size;
		if (play_length > 0) {
			consumed = ao_plugin_play(ao.plugin, ao.data,
						  buffer, play_length,
						  &error);
			if (consumed == 0) {
				g_printerr("Failed to play: %s\n",
					   error->message);
				g_error_free(error);
				return 1;
			}

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

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

	/* cleanup and exit */

	ao_plugin_close(ao.plugin, ao.data);
	ao_plugin_finish(ao.plugin, ao.data);
	g_mutex_free(ao.mutex);

	config_global_finish();

	return 0;
}