run_decoder.cxx 4.89 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 "Log.hxx"
29
#include "stdbin.h"
30 31 32 33 34

#include <glib.h>

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

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

47 48 49 50 51 52 53 54 55 56
struct decoder {
	const char *uri;

	const struct decoder_plugin *plugin;

	bool initialized;
};

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

63
	assert(!decoder->initialized);
64
	assert(audio_format.IsValid());
65

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

	decoder->initialized = true;
}

72
DecoderCommand
73
decoder_get_command(gcc_unused struct decoder *decoder)
74
{
75
	return DecoderCommand::NONE;
76 77
}

78
void decoder_command_finished(gcc_unused struct decoder *decoder)
79 80 81
{
}

82
double decoder_seek_where(gcc_unused struct decoder *decoder)
83 84 85 86
{
	return 1.0;
}

87
void decoder_seek_error(gcc_unused struct decoder *decoder)
88 89 90 91
{
}

size_t
92
decoder_read(gcc_unused struct decoder *decoder,
93 94 95
	     struct input_stream *is,
	     void *buffer, size_t length)
{
96
	return is->LockRead(buffer, length, IgnoreError());
97 98
}

99
void
100 101
decoder_timestamp(gcc_unused struct decoder *decoder,
		  gcc_unused double t)
102 103 104
{
}

105
DecoderCommand
106 107
decoder_data(gcc_unused struct decoder *decoder,
	     gcc_unused struct input_stream *is,
108
	     const void *data, size_t datalen,
109
	     gcc_unused uint16_t kbit_rate)
110
{
111
	gcc_unused ssize_t nbytes = write(1, data, datalen);
112
	return DecoderCommand::NONE;
113 114
}

115
DecoderCommand
116 117 118
decoder_tag(gcc_unused struct decoder *decoder,
	    gcc_unused struct input_stream *is,
	    gcc_unused Tag &&tag)
119
{
120
	return DecoderCommand::NONE;
121 122
}

123
void
124
decoder_replay_gain(gcc_unused struct decoder *decoder,
125
		    const struct replay_gain_info *replay_gain_info)
126
{
127 128 129 130 131 132 133 134 135 136
	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);
137 138
}

139
void
140
decoder_mixramp(gcc_unused struct decoder *decoder,
141 142 143 144 145 146
		char *mixramp_start, char *mixramp_end)
{
	g_free(mixramp_start);
	g_free(mixramp_end);
}

147 148 149 150 151 152 153 154 155 156 157 158 159
int main(int argc, char **argv)
{
	const char *decoder_name;
	struct decoder decoder;

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

	decoder_name = argv[1];
	decoder.uri = argv[2];

160
#if !GLIB_CHECK_VERSION(2,32,0)
161
	g_thread_init(NULL);
162 163
#endif

164 165
	g_log_set_default_handler(my_log_func, NULL);

166
	io_thread_init();
167
	io_thread_start();
168

169 170
	Error error;
	if (!input_stream_global_init(error)) {
171
		LogError(error);
172 173 174
		return 2;
	}

175 176 177 178 179 180 181 182
	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;
	}

183 184
	decoder.initialized = false;

185 186 187 188
	if (decoder.plugin->file_decode != NULL) {
		decoder_plugin_file_decode(decoder.plugin, &decoder,
					   decoder.uri);
	} else if (decoder.plugin->stream_decode != NULL) {
189 190
		Mutex mutex;
		Cond cond;
191

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

200
			return 1;
201
		}
202

203
		decoder_plugin_stream_decode(decoder.plugin, &decoder, is);
204

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

	decoder_plugin_deinit_all();
212
	input_stream_global_finish();
213
	io_thread_deinit();
214 215 216 217 218 219 220 221

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

	return 0;
}