run_input.c 3.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
#include "io_thread.h"
22
#include "input_init.h"
23 24
#include "input_stream.h"
#include "tag_save.h"
25
#include "tag.h"
26
#include "conf.h"
27
#include "stdbin.h"
28

29 30 31 32
#ifdef ENABLE_ARCHIVE
#include "archive_list.h"
#endif

33 34 35
#include <glib.h>

#include <unistd.h>
36
#include <stdlib.h>
37 38 39 40 41 42 43 44 45 46 47

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);
}

48 49
static int
dump_input_stream(struct input_stream *is)
50
{
51
	GError *error = NULL;
52 53 54 55
	char buffer[4096];
	size_t num_read;
	ssize_t num_written;

56
	input_stream_lock(is);
57

58
	/* wait until the stream becomes ready */
59

60
	input_stream_wait_ready(is);
61

62 63 64
	if (!input_stream_check(is, &error)) {
		g_warning("%s", error->message);
		g_error_free(error);
65
		input_stream_unlock(is);
66
		return EXIT_FAILURE;
67 68 69 70
	}

	/* print meta data */

71 72
	if (is->mime != NULL)
		g_printerr("MIME type: %s\n", is->mime);
73 74 75

	/* read data and tags from the stream */

76 77
	while (!input_stream_eof(is)) {
		struct tag *tag = input_stream_tag(is);
78 79 80 81 82 83
		if (tag != NULL) {
			g_printerr("Received a tag:\n");
			tag_save(stderr, tag);
			tag_free(tag);
		}

84 85 86 87 88 89 90 91
		num_read = input_stream_read(is, buffer, sizeof(buffer),
					     &error);
		if (num_read == 0) {
			if (error != NULL) {
				g_warning("%s", error->message);
				g_error_free(error);
			}

92
			break;
93
		}
94 95 96 97 98 99

		num_written = write(1, buffer, num_read);
		if (num_written <= 0)
			break;
	}

100 101 102
	if (!input_stream_check(is, &error)) {
		g_warning("%s", error->message);
		g_error_free(error);
103
		input_stream_unlock(is);
104 105 106
		return EXIT_FAILURE;
	}

107
	input_stream_unlock(is);
108

109 110 111 112 113
	return 0;
}

int main(int argc, char **argv)
{
Max Kellermann's avatar
Max Kellermann committed
114
	GError *error = NULL;
115
	struct input_stream *is;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	int ret;

	if (argc != 2) {
		g_printerr("Usage: run_input URI\n");
		return 1;
	}

	/* initialize GLib */

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

	/* initialize MPD */

	config_global_init();

132 133 134 135 136 137 138
	io_thread_init();
	if (!io_thread_start(&error)) {
		g_warning("%s", error->message);
		g_error_free(error);
		return EXIT_FAILURE;
	}

139 140 141 142
#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

Max Kellermann's avatar
Max Kellermann committed
143 144 145 146 147
	if (!input_stream_global_init(&error)) {
		g_warning("%s", error->message);
		g_error_free(error);
		return 2;
	}
148 149 150

	/* open the stream and dump it */

151 152 153 154
	GMutex *mutex = g_mutex_new();
	GCond *cond = g_cond_new();

	is = input_stream_open(argv[1], mutex, cond, &error);
155 156 157
	if (is != NULL) {
		ret = dump_input_stream(is);
		input_stream_close(is);
158
	} else {
159 160 161 162 163
		if (error != NULL) {
			g_warning("%s", error->message);
			g_error_free(error);
		} else
			g_printerr("input_stream_open() failed\n");
164 165 166
		ret = 2;
	}

167 168 169
	g_cond_free(cond);
	g_mutex_free(mutex);

170 171 172
	/* deinitialize everything */

	input_stream_global_finish();
173 174 175 176 177

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

178 179
	io_thread_deinit();

180 181
	config_global_finish();

182
	return ret;
183
}