dump_playlist.cxx 5.43 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
#include "TagSave.hxx"
22
#include "Song.hxx"
23
#include "SongEnumerator.hxx"
24
#include "Directory.hxx"
25
#include "InputStream.hxx"
26
#include "ConfigGlobal.hxx"
27
#include "DecoderAPI.hxx"
28
#include "DecoderList.hxx"
29
#include "InputInit.hxx"
30
#include "IOThread.hxx"
31
#include "PlaylistRegistry.hxx"
32
#include "PlaylistPlugin.hxx"
33
#include "fs/Path.hxx"
34
#include "util/Error.hxx"
35

36 37 38
#include <glib.h>

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

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

44
static void
45 46
my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level,
	    const gchar *message, gcc_unused gpointer user_data)
47 48 49 50 51 52 53
{
	if (log_domain != NULL)
		g_printerr("%s: %s\n", log_domain, message);
	else
		g_printerr("%s\n", message);
}

54
void
55 56 57 58
decoder_initialized(gcc_unused struct decoder *decoder,
		    gcc_unused const AudioFormat audio_format,
		    gcc_unused bool seekable,
		    gcc_unused float total_time)
59 60 61 62
{
}

enum decoder_command
63
decoder_get_command(gcc_unused struct decoder *decoder)
64 65 66 67 68
{
	return DECODE_COMMAND_NONE;
}

void
69
decoder_command_finished(gcc_unused struct decoder *decoder)
70 71 72 73
{
}

double
74
decoder_seek_where(gcc_unused struct decoder *decoder)
75 76 77 78 79
{
	return 1.0;
}

void
80
decoder_seek_error(gcc_unused struct decoder *decoder)
81 82 83 84
{
}

size_t
85
decoder_read(gcc_unused struct decoder *decoder,
86 87 88
	     struct input_stream *is,
	     void *buffer, size_t length)
{
89
	Error error;
90
	return is->LockRead(buffer, length, error);
91 92 93
}

void
94 95
decoder_timestamp(gcc_unused struct decoder *decoder,
		  gcc_unused double t)
96 97 98 99
{
}

enum decoder_command
100 101
decoder_data(gcc_unused struct decoder *decoder,
	     gcc_unused struct input_stream *is,
102
	     const void *data, size_t datalen,
103
	     gcc_unused uint16_t kbit_rate)
104
{
105
	gcc_unused ssize_t nbytes = write(1, data, datalen);
106 107 108 109
	return DECODE_COMMAND_NONE;
}

enum decoder_command
110 111 112
decoder_tag(gcc_unused struct decoder *decoder,
	    gcc_unused struct input_stream *is,
	    gcc_unused Tag &&tag)
113 114 115 116
{
	return DECODE_COMMAND_NONE;
}

117
void
118
decoder_replay_gain(gcc_unused struct decoder *decoder,
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		    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
134
decoder_mixramp(gcc_unused struct decoder *decoder,
135 136 137 138 139 140
		char *mixramp_start, char *mixramp_end)
{
	g_free(mixramp_start);
	g_free(mixramp_end);
}

141 142 143
int main(int argc, char **argv)
{
	const char *uri;
144
	struct input_stream *is = NULL;
145
	Song *song;
146

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

152
	const Path config_path = Path::FromFS(argv[1]);
153
	uri = argv[2];
154 155 156

	/* initialize GLib */

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

161 162 163 164 165
	g_log_set_default_handler(my_log_func, NULL);

	/* initialize MPD */

	config_global_init();
166 167 168 169

	Error error;
	if (!ReadConfigFile(config_path, error)) {
		g_printerr("%s\n", error.GetMessage());
170 171 172
		return 1;
	}

173
	io_thread_init();
174
	io_thread_start();
175

176 177
	if (!input_stream_global_init(error)) {
		g_warning("%s", error.GetMessage());
178 179 180
		return 2;
	}

181
	playlist_list_global_init();
182
	decoder_plugin_init_all();
183

184
	/* open the playlist */
185

186 187
	Mutex mutex;
	Cond cond;
188

189
	auto playlist = playlist_list_open_uri(uri, mutex, cond);
190 191
	if (playlist == NULL) {
		/* open the stream and wait until it becomes ready */
192

193
		is = input_stream::Open(uri, mutex, cond, error);
194
		if (is == NULL) {
195 196 197
			if (error.IsDefined())
				g_warning("%s", error.GetMessage());
			else
198
				g_printerr("input_stream::Open() failed\n");
199
			return 2;
200
		}
201

202
		is->LockWaitReady();
203

204 205
		/* open the playlist */

206
		playlist = playlist_list_open_stream(is, uri);
207
		if (playlist == NULL) {
208
			is->Close();
209 210 211
			g_printerr("Failed to open playlist\n");
			return 2;
		}
212 213 214 215
	}

	/* dump the playlist */

216
	while ((song = playlist->NextSong()) != NULL) {
217
		g_print("%s\n", song->uri);
218

219
		if (song->end_ms > 0)
220 221 222 223 224
			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);
225 226 227 228
		else if (song->start_ms > 0)
			g_print("range: %u:%02u..\n",
				song->start_ms / 60000,
				(song->start_ms / 1000) % 60);
229

230
		if (song->tag != NULL)
Max Kellermann's avatar
Max Kellermann committed
231
			tag_save(stdout, *song->tag);
232

233
		song->Free();
234 235 236 237
	}

	/* deinitialize everything */

238
	delete playlist;
239
	if (is != NULL)
240
		is->Close();
241

242
	decoder_plugin_deinit_all();
243 244
	playlist_list_global_finish();
	input_stream_global_finish();
245
	io_thread_deinit();
246 247 248 249
	config_global_finish();

	return 0;
}