run_decoder.cxx 5.21 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
extern "C" {
23
#include "decoder_list.h"
24
}
25
#include "decoder_api.h"
26
#include "InputInit.hxx"
27 28
#include "input_stream.h"
#include "audio_format.h"
29
#include "stdbin.h"
30 31 32 33 34

#include <glib.h>

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

37 38 39 40 41 42 43 44 45 46
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);
}

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

	const struct decoder_plugin *plugin;

	bool initialized;
};

void
decoder_initialized(struct decoder *decoder,
		    const struct audio_format *audio_format,
		    G_GNUC_UNUSED bool seekable,
		    G_GNUC_UNUSED float total_time)
{
61 62
	struct audio_format_string af_string;

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

66 67
	g_printerr("audio_format=%s\n",
		   audio_format_to_string(audio_format, &af_string));
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 94 95

	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)
{
96
	return input_stream_lock_read(is, buffer, length, NULL);
97 98
}

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

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

enum decoder_command
decoder_tag(G_GNUC_UNUSED struct decoder *decoder,
	    G_GNUC_UNUSED struct input_stream *is,
	    G_GNUC_UNUSED const struct tag *tag)
{
	return DECODE_COMMAND_NONE;
}

123
void
124
decoder_replay_gain(G_GNUC_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 140 141 142 143 144 145 146
void
decoder_mixramp(G_GNUC_UNUSED struct decoder *decoder,
		char *mixramp_start, char *mixramp_end)
{
	g_free(mixramp_start);
	g_free(mixramp_end);
}

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

161
	g_thread_init(NULL);
162 163
	g_log_set_default_handler(my_log_func, NULL);

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

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

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

185 186
	decoder.initialized = false;

187 188 189 190
	if (decoder.plugin->file_decode != NULL) {
		decoder_plugin_file_decode(decoder.plugin, &decoder,
					   decoder.uri);
	} else if (decoder.plugin->stream_decode != NULL) {
191 192 193
		GMutex *mutex = g_mutex_new();
		GCond *cond = g_cond_new();

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

203
			return 1;
204
		}
205

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

208
		input_stream_close(is);
209 210 211

		g_cond_free(cond);
		g_mutex_free(mutex);
212 213 214 215 216 217
	} else {
		g_printerr("Decoder plugin is not usable\n");
		return 1;
	}

	decoder_plugin_deinit_all();
218
	input_stream_global_finish();
219
	io_thread_deinit();
220 221 222 223 224 225 226 227

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

	return 0;
}