input_stream.c 4.19 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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 "input_plugin.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "config.h"
22
#include "conf.h"
23

24
#include "input/file_input_plugin.h"
25 26

#ifdef ENABLE_ARCHIVE
27
#include "input/archive_input_plugin.h"
28
#endif
Max Kellermann's avatar
Max Kellermann committed
29 30

#ifdef HAVE_CURL
31
#include "input/curl_input_plugin.h"
Max Kellermann's avatar
Max Kellermann committed
32
#endif
33

34 35
#include "input/lastfm_input_plugin.h"

36
#ifdef ENABLE_MMS
37
#include "input/mms_input_plugin.h"
38 39
#endif

40
#include <glib.h>
41
#include <assert.h>
42
#include <string.h>
43

44 45
static const struct input_plugin *const input_plugins[] = {
	&input_plugin_file,
46
#ifdef ENABLE_ARCHIVE
47
	&input_plugin_archive,
48
#endif
49 50 51
#ifdef HAVE_CURL
	&input_plugin_curl,
#endif
52 53 54
#ifdef ENABLE_LASTFM
	&lastfm_input_plugin,
#endif
55 56 57
#ifdef ENABLE_MMS
	&input_plugin_mms,
#endif
58 59
};

60 61
static bool input_plugins_enabled[G_N_ELEMENTS(input_plugins)];

62 63 64
static const unsigned num_input_plugins =
	sizeof(input_plugins) / sizeof(input_plugins[0]);

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
/**
 * Find the "input" configuration block for the specified plugin.
 *
 * @param plugin_name the name of the input plugin
 * @return the configuration block, or NULL if none was configured
 */
static const struct config_param *
input_plugin_config(const char *plugin_name)
{
	const struct config_param *param = NULL;

	while ((param = config_get_next_param(CONF_INPUT, param)) != NULL) {
		const char *name =
			config_get_block_string(param, "plugin", NULL);
		if (name == NULL)
			g_error("input configuration without 'plugin' name in line %d",
				param->line);

		if (strcmp(name, plugin_name) == 0)
			return param;
	}

	return NULL;
}

90
void input_stream_global_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
91
{
92 93 94 95 96 97 98 99 100 101
	for (unsigned i = 0; i < num_input_plugins; ++i) {
		const struct input_plugin *plugin = input_plugins[i];
		const struct config_param *param =
			input_plugin_config(plugin->name);

		if (!config_get_block_bool(param, "enabled", true))
			/* the plugin is disabled in mpd.conf */
			continue;

		if (plugin->init == NULL || plugin->init(param))
102
			input_plugins_enabled[i] = true;
103
	}
104 105
}

106 107
void input_stream_global_finish(void)
{
108 109 110 111
	for (unsigned i = 0; i < num_input_plugins; ++i)
		if (input_plugins_enabled[i] &&
		    input_plugins[i]->finish != NULL)
			input_plugins[i]->finish();
112 113
}

114
bool
115
input_stream_open(struct input_stream *is, const char *url)
Avuton Olrich's avatar
Avuton Olrich committed
116
{
117 118
	is->seekable = false;
	is->ready = false;
119
	is->offset = 0;
120
	is->size = -1;
121 122 123
	is->error = 0;
	is->mime = NULL;

124 125
	for (unsigned i = 0; i < num_input_plugins; ++i) {
		const struct input_plugin *plugin = input_plugins[i];
Max Kellermann's avatar
Max Kellermann committed
126

127
		if (input_plugins_enabled[i] && plugin->open(is, url)) {
128 129 130 131
			assert(is->plugin != NULL);
			assert(is->plugin->close != NULL);
			assert(is->plugin->read != NULL);
			assert(is->plugin->eof != NULL);
132
			assert(!is->seekable || is->plugin->seek != NULL);
133

134
			return true;
135 136
		}
	}
137

138
	return false;
139 140
}

141
bool
142
input_stream_seek(struct input_stream *is, off_t offset, int whence)
Avuton Olrich's avatar
Avuton Olrich committed
143
{
144 145 146
	if (is->plugin->seek == NULL)
		return false;

147
	return is->plugin->seek(is, offset, whence);
148 149
}

150 151 152 153 154 155 156 157 158 159
struct tag *
input_stream_tag(struct input_stream *is)
{
	assert(is != NULL);

	return is->plugin->tag != NULL
		? is->plugin->tag(is)
		: NULL;
}

160 161
size_t
input_stream_read(struct input_stream *is, void *ptr, size_t size)
162
{
163 164 165
	assert(ptr != NULL);
	assert(size > 0);

166
	return is->plugin->read(is, ptr, size);
167 168
}

169
void input_stream_close(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
170
{
171
	is->plugin->close(is);
172 173

	g_free(is->mime);
174
}
175

176
bool input_stream_eof(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
177
{
178
	return is->plugin->eof(is);
179
}
180

181
int input_stream_buffer(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
182
{
183 184 185
	if (is->plugin->buffer == NULL)
		return 0;

186
	return is->plugin->buffer(is);
187
}