run_decoder.cxx 5.12 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 "input_stream.h"
26
#include "AudioFormat.hxx"
27
#include "stdbin.h"
28 29 30 31 32

#include <glib.h>

#include <assert.h>
#include <unistd.h>
33
#include <stdlib.h>
34

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

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

	const struct decoder_plugin *plugin;

	bool initialized;
};

void
decoder_initialized(struct decoder *decoder,
55
		    const AudioFormat audio_format,
56 57 58
		    G_GNUC_UNUSED bool seekable,
		    G_GNUC_UNUSED float total_time)
{
59 60
	struct audio_format_string af_string;

61
	assert(!decoder->initialized);
62
	assert(audio_format.IsValid());
63

64 65
	g_printerr("audio_format=%s\n",
		   audio_format_to_string(audio_format, &af_string));
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

	decoder->initialized = true;
}

enum decoder_command
decoder_get_command(G_GNUC_UNUSED struct decoder *decoder)
{
	return DECODE_COMMAND_NONE;
}

void decoder_command_finished(G_GNUC_UNUSED struct decoder *decoder)
{
}

double decoder_seek_where(G_GNUC_UNUSED struct decoder *decoder)
{
	return 1.0;
}

void decoder_seek_error(G_GNUC_UNUSED struct decoder *decoder)
{
}

size_t
decoder_read(G_GNUC_UNUSED struct decoder *decoder,
	     struct input_stream *is,
	     void *buffer, size_t length)
{
94
	return input_stream_lock_read(is, buffer, length, NULL);
95 96
}

97 98 99 100 101 102
void
decoder_timestamp(G_GNUC_UNUSED struct decoder *decoder,
		  G_GNUC_UNUSED double t)
{
}

103 104 105 106
enum decoder_command
decoder_data(G_GNUC_UNUSED struct decoder *decoder,
	     G_GNUC_UNUSED struct input_stream *is,
	     const void *data, size_t datalen,
107
	     G_GNUC_UNUSED uint16_t kbit_rate)
108
{
109
	G_GNUC_UNUSED ssize_t nbytes = write(1, data, datalen);
110 111 112 113 114 115
	return DECODE_COMMAND_NONE;
}

enum decoder_command
decoder_tag(G_GNUC_UNUSED struct decoder *decoder,
	    G_GNUC_UNUSED struct input_stream *is,
116
	    G_GNUC_UNUSED Tag &&tag)
117 118 119 120
{
	return DECODE_COMMAND_NONE;
}

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

137 138 139 140 141 142 143 144
void
decoder_mixramp(G_GNUC_UNUSED struct decoder *decoder,
		char *mixramp_start, char *mixramp_end)
{
	g_free(mixramp_start);
	g_free(mixramp_end);
}

145 146
int main(int argc, char **argv)
{
147
	GError *error = NULL;
148 149 150 151 152 153 154 155 156 157 158
	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];

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

163 164
	g_log_set_default_handler(my_log_func, NULL);

165 166 167 168 169 170 171
	io_thread_init();
	if (!io_thread_start(&error)) {
		g_warning("%s", error->message);
		g_error_free(error);
		return EXIT_FAILURE;
	}

172 173 174 175 176 177
	if (!input_stream_global_init(&error)) {
		g_warning("%s", error->message);
		g_error_free(error);
		return 2;
	}

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

186 187
	decoder.initialized = false;

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

195
		struct input_stream *is =
196
			input_stream_open(decoder.uri, mutex, cond, &error);
197
		if (is == NULL) {
198 199 200 201 202 203
			if (error != NULL) {
				g_warning("%s", error->message);
				g_error_free(error);
			} else
				g_printerr("input_stream_open() failed\n");

204
			return 1;
205
		}
206

207
		decoder_plugin_stream_decode(decoder.plugin, &decoder, is);
208

209
		input_stream_close(is);
210 211 212 213 214 215
	} else {
		g_printerr("Decoder plugin is not usable\n");
		return 1;
	}

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

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

	return 0;
}