dump_playlist.cxx 5.71 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 14 15 16 17 18 19
 * 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.
 *
 * 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.
 */

20
#include "config.h"
21 22
#include "TagSave.hxx"
#include "song.h"
23
#include "Directory.hxx"
24 25 26
#include "input_stream.h"
#include "conf.h"
#include "decoder_api.h"
27 28

extern "C" {
29
#include "io_thread.h"
30
#include "input_init.h"
31
#include "decoder_list.h"
32 33
#include "playlist_list.h"
#include "playlist_plugin.h"
34
}
35 36 37 38

#include <glib.h>

#include <unistd.h>
39
#include <stdlib.h>
40

41
Directory::Directory() {}
42 43
Directory::~Directory() {}

44 45 46 47 48 49 50 51 52 53
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);
}

54 55 56 57 58 59 60 61 62 63 64 65 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 94 95 96 97 98 99 100 101 102 103
void
decoder_initialized(G_GNUC_UNUSED struct decoder *decoder,
		    G_GNUC_UNUSED const struct audio_format *audio_format,
		    G_GNUC_UNUSED bool seekable,
		    G_GNUC_UNUSED float total_time)
{
}

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

void
decoder_timestamp(G_GNUC_UNUSED struct decoder *decoder,
		  G_GNUC_UNUSED double t)
{
}

enum decoder_command
decoder_data(G_GNUC_UNUSED struct decoder *decoder,
	     G_GNUC_UNUSED struct input_stream *is,
	     const void *data, size_t datalen,
	     G_GNUC_UNUSED uint16_t kbit_rate)
{
104
	G_GNUC_UNUSED ssize_t nbytes = write(1, data, datalen);
105 106 107 108 109 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,
	    G_GNUC_UNUSED const struct tag *tag)
{
	return DECODE_COMMAND_NONE;
}

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

void
decoder_mixramp(G_GNUC_UNUSED struct decoder *decoder,
		char *mixramp_start, char *mixramp_end)
{
	g_free(mixramp_start);
	g_free(mixramp_end);
}

140 141 142
int main(int argc, char **argv)
{
	const char *uri;
143
	struct input_stream *is = NULL;
144
	bool success;
145
	GError *error = NULL;
146 147 148
	struct playlist_provider *playlist;
	struct song *song;

149 150
	if (argc != 3) {
		g_printerr("Usage: dump_playlist CONFIG URI\n");
151 152 153
		return 1;
	}

154
	uri = argv[2];
155 156 157 158 159 160 161 162 163

	/* initialize GLib */

	g_thread_init(NULL);
	g_log_set_default_handler(my_log_func, NULL);

	/* initialize MPD */

	config_global_init();
164 165
	success = config_read_file(argv[1], &error);
	if (!success) {
166
		g_printerr("%s\n", error->message);
167 168 169 170
		g_error_free(error);
		return 1;
	}

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

178 179 180 181 182 183
	if (!input_stream_global_init(&error)) {
		g_warning("%s", error->message);
		g_error_free(error);
		return 2;
	}

184
	playlist_list_global_init();
185
	decoder_plugin_init_all();
186

187
	/* open the playlist */
188

189 190 191 192
	GMutex *mutex = g_mutex_new();
	GCond *cond = g_cond_new();

	playlist = playlist_list_open_uri(uri, mutex, cond);
193 194
	if (playlist == NULL) {
		/* open the stream and wait until it becomes ready */
195

196
		is = input_stream_open(uri, mutex, cond, &error);
197
		if (is == NULL) {
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 2;
204
		}
205

206
		input_stream_lock_wait_ready(is);
207

208 209
		/* open the playlist */

210
		playlist = playlist_list_open_stream(is, uri);
211
		if (playlist == NULL) {
212
			input_stream_close(is);
213 214 215
			g_printerr("Failed to open playlist\n");
			return 2;
		}
216 217 218 219 220
	}

	/* dump the playlist */

	while ((song = playlist_plugin_read(playlist)) != NULL) {
221
		g_print("%s\n", song->uri);
222

223
		if (song->end_ms > 0)
224 225 226 227 228
			g_print("range: %u:%02u..%u:%02u\n",
				song->start_ms / 60000,
				(song->start_ms / 1000) % 60,
				song->end_ms / 60000,
				(song->end_ms / 1000) % 60);
229 230 231 232
		else if (song->start_ms > 0)
			g_print("range: %u:%02u..\n",
				song->start_ms / 60000,
				(song->start_ms / 1000) % 60);
233

234 235 236 237 238 239 240 241 242
		if (song->tag != NULL)
			tag_save(stdout, song->tag);

		song_free(song);
	}

	/* deinitialize everything */

	playlist_plugin_close(playlist);
243 244
	if (is != NULL)
		input_stream_close(is);
245 246 247 248

	g_cond_free(cond);
	g_mutex_free(mutex);

249
	decoder_plugin_deinit_all();
250 251
	playlist_list_global_finish();
	input_stream_global_finish();
252
	io_thread_deinit();
253 254 255 256
	config_global_finish();

	return 0;
}