run_decoder.cxx 4.79 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 60
		    gcc_unused bool seekable,
		    gcc_unused float total_time)
61
{
62 63
	struct audio_format_string af_string;

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

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

70
	decoder.initialized = true;
71 72
}

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

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

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

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

size_t
96
decoder_read(gcc_unused Decoder *decoder,
97 98 99
	     struct input_stream *is,
	     void *buffer, size_t length)
{
100
	return is->LockRead(buffer, length, IgnoreError());
101 102
}

103
void
104
decoder_timestamp(gcc_unused Decoder &decoder,
105
		  gcc_unused double t)
106 107 108
{
}

109
DecoderCommand
110
decoder_data(gcc_unused Decoder &decoder,
111
	     gcc_unused struct input_stream *is,
112
	     const void *data, size_t datalen,
113
	     gcc_unused uint16_t kbit_rate)
114
{
115
	gcc_unused ssize_t nbytes = write(1, data, datalen);
116
	return DecoderCommand::NONE;
117 118
}

119
DecoderCommand
120
decoder_tag(gcc_unused Decoder &decoder,
121 122
	    gcc_unused struct input_stream *is,
	    gcc_unused Tag &&tag)
123
{
124
	return DecoderCommand::NONE;
125 126
}

127
void
128
decoder_replay_gain(gcc_unused Decoder &decoder,
129
		    const struct replay_gain_info *replay_gain_info)
130
{
131 132 133 134 135 136 137 138 139 140
	const struct replay_gain_tuple *tuple =
		&replay_gain_info->tuples[REPLAY_GAIN_ALBUM];
	if (replay_gain_tuple_defined(tuple))
		g_printerr("replay_gain[album]: gain=%f peak=%f\n",
			   tuple->gain, tuple->peak);

	tuple = &replay_gain_info->tuples[REPLAY_GAIN_TRACK];
	if (replay_gain_tuple_defined(tuple))
		g_printerr("replay_gain[track]: gain=%f peak=%f\n",
			   tuple->gain, tuple->peak);
141 142
}

143
void
144
decoder_mixramp(gcc_unused Decoder &decoder,
145 146 147 148 149 150
		char *mixramp_start, char *mixramp_end)
{
	g_free(mixramp_start);
	g_free(mixramp_end);
}

151 152 153 154 155 156 157 158 159
int main(int argc, char **argv)
{
	const char *decoder_name;

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

160
	Decoder decoder;
161 162 163
	decoder_name = argv[1];
	decoder.uri = argv[2];

164
#if !GLIB_CHECK_VERSION(2,32,0)
165
	g_thread_init(NULL);
166 167
#endif

168 169
	g_log_set_default_handler(my_log_func, NULL);

170
	io_thread_init();
171
	io_thread_start();
172

173 174
	Error error;
	if (!input_stream_global_init(error)) {
175
		LogError(error);
176 177 178
		return 2;
	}

179 180 181 182 183 184 185 186
	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;
	}

187 188
	decoder.initialized = false;

189
	if (decoder.plugin->file_decode != NULL) {
190
		decoder.plugin->FileDecode(decoder, decoder.uri);
191
	} else if (decoder.plugin->stream_decode != NULL) {
192 193
		Mutex mutex;
		Cond cond;
194

195 196
		input_stream *is =
			input_stream::Open(decoder.uri, mutex, cond, error);
197
		if (is == NULL) {
198
			if (error.IsDefined())
199
				LogError(error);
200
			else
201
				g_printerr("input_stream::Open() failed\n");
202

203
			return 1;
204
		}
205

206
		decoder.plugin->StreamDecode(decoder, *is);
207

208
		is->Close();
209 210 211 212 213 214
	} else {
		g_printerr("Decoder plugin is not usable\n");
		return 1;
	}

	decoder_plugin_deinit_all();
215
	input_stream_global_finish();
216
	io_thread_deinit();
217 218 219 220 221 222 223 224

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

	return 0;
}