dump_playlist.cxx 5.4 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
#include "Log.hxx"
36

37 38 39
#include <glib.h>

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

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

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

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

63
DecoderCommand
64
decoder_get_command(gcc_unused struct decoder *decoder)
65
{
66
	return DecoderCommand::NONE;
67 68 69
}

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

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

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

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

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

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

110
DecoderCommand
111 112 113
decoder_tag(gcc_unused struct decoder *decoder,
	    gcc_unused struct input_stream *is,
	    gcc_unused Tag &&tag)
114
{
115
	return DecoderCommand::NONE;
116 117
}

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

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

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

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

	/* initialize GLib */

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

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

	/* initialize MPD */

	config_global_init();
167 168 169 170

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

174
	io_thread_init();
175
	io_thread_start();
176

177
	if (!input_stream_global_init(error)) {
178
		LogError(error);
179 180 181
		return 2;
	}

182
	playlist_list_global_init();
183
	decoder_plugin_init_all();
184

185
	/* open the playlist */
186

187 188
	Mutex mutex;
	Cond cond;
189

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

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

203
		is->LockWaitReady();
204

205 206
		/* open the playlist */

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

	/* dump the playlist */

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

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

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

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

	/* deinitialize everything */

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

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

	return 0;
}