run_decoder.cxx 5.22 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 "IOThread.hxx"
22
#include "DecoderList.hxx"
23
#include "DecoderAPI.hxx"
24
#include "InputInit.hxx"
25
#include "InputStream.hxx"
26
#include "AudioFormat.hxx"
27
#include "util/Error.hxx"
28
#include "thread/Cond.hxx"
29
#include "Log.hxx"
30
#include "stdbin.h"
31 32 33 34 35

#include <glib.h>

#include <assert.h>
#include <unistd.h>
36
#include <stdlib.h>
37

38
static void
39 40
my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level,
	    const gchar *message, gcc_unused gpointer user_data)
41 42 43 44 45 46 47
{
	if (log_domain != NULL)
		g_printerr("%s: %s\n", log_domain, message);
	else
		g_printerr("%s\n", message);
}

48
struct Decoder {
49 50
	const char *uri;

51
	const struct DecoderPlugin *plugin;
52 53 54 55 56

	bool initialized;
};

void
57
decoder_initialized(Decoder &decoder,
58
		    const AudioFormat audio_format,
59
		    gcc_unused bool seekable,
60
		    float duration)
61
{
62 63
	struct audio_format_string af_string;

64
	assert(!decoder.initialized);
65
	assert(audio_format.IsValid());
66

67 68 69
	g_printerr("audio_format=%s duration=%f\n",
		   audio_format_to_string(audio_format, &af_string),
		   duration);
70

71
	decoder.initialized = true;
72 73
}

74
DecoderCommand
75
decoder_get_command(gcc_unused Decoder &decoder)
76
{
77
	return DecoderCommand::NONE;
78 79
}

80 81
void
decoder_command_finished(gcc_unused Decoder &decoder)
82 83 84
{
}

85 86
double
decoder_seek_where(gcc_unused Decoder &decoder)
87 88 89 90
{
	return 1.0;
}

91 92
void
decoder_seek_error(gcc_unused Decoder &decoder)
93 94 95 96
{
}

size_t
97
decoder_read(gcc_unused Decoder *decoder,
98
	     InputStream &is,
99 100
	     void *buffer, size_t length)
{
101
	return is.LockRead(buffer, length, IgnoreError());
102 103
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
bool
decoder_read_full(Decoder *decoder, InputStream &is,
		  void *_buffer, size_t size)
{
	uint8_t *buffer = (uint8_t *)_buffer;

	while (size > 0) {
		size_t nbytes = decoder_read(decoder, is, buffer, size);
		if (nbytes == 0)
			return false;

		buffer += nbytes;
		size -= nbytes;
	}

	return true;
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
bool
decoder_skip(Decoder *decoder, InputStream &is, size_t size)
{
	while (size > 0) {
		char buffer[1024];
		size_t nbytes = decoder_read(decoder, is, buffer,
					     std::min(sizeof(buffer), size));
		if (nbytes == 0)
			return false;

		size -= nbytes;
	}

	return true;
}

138
void
139
decoder_timestamp(gcc_unused Decoder &decoder,
140
		  gcc_unused double t)
141 142 143
{
}

144
DecoderCommand
145
decoder_data(gcc_unused Decoder &decoder,
146
	     gcc_unused InputStream *is,
147
	     const void *data, size_t datalen,
148
	     gcc_unused uint16_t kbit_rate)
149
{
150
	gcc_unused ssize_t nbytes = write(1, data, datalen);
151
	return DecoderCommand::NONE;
152 153
}

154
DecoderCommand
155
decoder_tag(gcc_unused Decoder &decoder,
156
	    gcc_unused InputStream *is,
157
	    gcc_unused Tag &&tag)
158
{
159
	return DecoderCommand::NONE;
160 161
}

162
void
163
decoder_replay_gain(gcc_unused Decoder &decoder,
164
		    const ReplayGainInfo *rgi)
165
{
166
	const ReplayGainTuple *tuple = &rgi->tuples[REPLAY_GAIN_ALBUM];
167
	if (tuple->IsDefined())
168 169 170
		g_printerr("replay_gain[album]: gain=%f peak=%f\n",
			   tuple->gain, tuple->peak);

171
	tuple = &rgi->tuples[REPLAY_GAIN_TRACK];
172
	if (tuple->IsDefined())
173 174
		g_printerr("replay_gain[track]: gain=%f peak=%f\n",
			   tuple->gain, tuple->peak);
175 176
}

177
void
178
decoder_mixramp(gcc_unused Decoder &decoder, gcc_unused MixRampInfo &&mix_ramp)
179 180 181
{
}

182 183 184 185 186 187 188 189 190
int main(int argc, char **argv)
{
	const char *decoder_name;

	if (argc != 3) {
		g_printerr("Usage: run_decoder DECODER URI >OUT\n");
		return 1;
	}

191
	Decoder decoder;
192 193 194
	decoder_name = argv[1];
	decoder.uri = argv[2];

195
#if !GLIB_CHECK_VERSION(2,32,0)
196
	g_thread_init(NULL);
197 198
#endif

199 200
	g_log_set_default_handler(my_log_func, NULL);

201
	io_thread_init();
202
	io_thread_start();
203

204 205
	Error error;
	if (!input_stream_global_init(error)) {
206
		LogError(error);
207 208 209
		return 2;
	}

210 211 212 213 214 215 216 217
	decoder_plugin_init_all();

	decoder.plugin = decoder_plugin_from_name(decoder_name);
	if (decoder.plugin == NULL) {
		g_printerr("No such decoder: %s\n", decoder_name);
		return 1;
	}

218 219
	decoder.initialized = false;

220
	if (decoder.plugin->file_decode != NULL) {
221
		decoder.plugin->FileDecode(decoder, decoder.uri);
222
	} else if (decoder.plugin->stream_decode != NULL) {
223 224
		Mutex mutex;
		Cond cond;
225

226 227
		InputStream *is =
			InputStream::Open(decoder.uri, mutex, cond, error);
228
		if (is == NULL) {
229
			if (error.IsDefined())
230
				LogError(error);
231
			else
232
				g_printerr("InputStream::Open() failed\n");
233

234
			return 1;
235
		}
236

237
		decoder.plugin->StreamDecode(decoder, *is);
238

239
		is->Close();
240 241 242 243 244 245
	} else {
		g_printerr("Decoder plugin is not usable\n");
		return 1;
	}

	decoder_plugin_deinit_all();
246
	input_stream_global_finish();
247
	io_thread_deinit();
248 249 250 251 252 253 254 255

	if (!decoder.initialized) {
		g_printerr("Decoding failed\n");
		return 1;
	}

	return 0;
}