run_filter.cxx 3.99 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21 22
#include "ConfigData.hxx"
#include "ConfigGlobal.hxx"
23
#include "fs/Path.hxx"
24
#include "AudioParser.hxx"
25
#include "AudioFormat.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "FilterPlugin.hxx"
27
#include "FilterInternal.hxx"
28
#include "pcm/Volume.hxx"
29
#include "MixerControl.hxx"
30
#include "stdbin.h"
31 32
#include "util/Error.hxx"
#include "system/FatalError.hxx"
33
#include "Log.hxx"
34 35 36 37 38

#include <glib.h>

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

44
bool
45
mixer_set_volume(gcc_unused Mixer *mixer,
46
		 gcc_unused unsigned volume, gcc_unused Error &error)
47 48 49 50
{
	return true;
}

51
static const struct config_param *
52
find_named_config_block(ConfigOption option, const char *name)
53 54 55
{
	const struct config_param *param = NULL;

56
	while ((param = config_get_next_param(option, param)) != NULL) {
57
		const char *current_name = param->GetBlockValue("name");
58 59 60 61 62 63 64
		if (current_name != NULL && strcmp(current_name, name) == 0)
			return param;
	}

	return NULL;
}

65
static Filter *
66 67 68 69
load_filter(const char *name)
{
	const struct config_param *param;

70
	param = find_named_config_block(CONF_AUDIO_FILTER, name);
71
	if (param == NULL) {
72
		fprintf(stderr, "No such configured filter: %s\n", name);
73
		return nullptr;
74 75
	}

76 77
	Error error;
	Filter *filter = filter_configured_new(*param, error);
78
	if (filter == NULL) {
79
		LogError(error, "Failed to load filter");
80 81 82 83 84 85 86 87
		return NULL;
	}

	return filter;
}

int main(int argc, char **argv)
{
88
	struct audio_format_string af_string;
89
	Error error2;
90 91 92
	char buffer[4096];

	if (argc < 3 || argc > 4) {
93 94
		fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
95 96
	}

97 98
	const Path config_path = Path::FromFS(argv[1]);

99
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
100

101 102
	/* initialize GLib */

103
#if !GLIB_CHECK_VERSION(2,32,0)
104
	g_thread_init(NULL);
105 106
#endif

107 108 109
	/* read configuration file (mpd.conf) */

	config_global_init();
110 111
	if (!ReadConfigFile(config_path, error2))
		FatalError(error2);
112 113 114 115

	/* parse the audio format */

	if (argc > 3) {
116 117
		Error error;
		if (!audio_format_parse(audio_format, argv[3], false, error)) {
118 119
			LogError(error, "Failed to parse audio format");
			return EXIT_FAILURE;
120 121 122 123 124
		}
	}

	/* initialize the filter */

125
	Filter *filter = load_filter(argv[2]);
126
	if (filter == NULL)
127
		return EXIT_FAILURE;
128 129 130

	/* open the filter */

131 132
	Error error;
	const AudioFormat out_audio_format = filter->Open(audio_format, error);
133
	if (!out_audio_format.IsDefined()) {
134
		LogError(error, "Failed to open filter");
135
		delete filter;
136
		return EXIT_FAILURE;
137 138
	}

139 140
	fprintf(stderr, "audio_format=%s\n",
		audio_format_to_string(out_audio_format, &af_string));
141 142 143 144 145 146 147 148 149 150 151 152

	/* play */

	while (true) {
		ssize_t nbytes;
		size_t length;
		const void *dest;

		nbytes = read(0, buffer, sizeof(buffer));
		if (nbytes <= 0)
			break;

153
		dest = filter->FilterPCM(buffer, (size_t)nbytes,
154
					 &length, error);
155
		if (dest == NULL) {
156
			LogError(error, "Filter failed");
157 158
			filter->Close();
			delete filter;
159
			return EXIT_FAILURE;
160 161 162 163
		}

		nbytes = write(1, dest, length);
		if (nbytes < 0) {
164 165
			fprintf(stderr, "Failed to write: %s\n",
				strerror(errno));
166 167
			filter->Close();
			delete filter;
168 169 170 171 172 173
			return 1;
		}
	}

	/* cleanup and exit */

174 175
	filter->Close();
	delete filter;
176 177 178 179 180

	config_global_finish();

	return 0;
}