input_stream.c 3 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "input_stream.h"
Max Kellermann's avatar
Max Kellermann committed
20
#include "config.h"
21

22
#include "input_file.h"
23 24

#ifdef ENABLE_ARCHIVE
25
#include "input_archive.h"
26
#endif
Max Kellermann's avatar
Max Kellermann committed
27 28 29 30

#ifdef HAVE_CURL
#include "input_curl.h"
#endif
31

32 33 34 35
#ifdef ENABLE_MMS
#include "input_mms.h"
#endif

36
#include <glib.h>
37
#include <assert.h>
38

39 40
static const struct input_plugin *const input_plugins[] = {
	&input_plugin_file,
41
#ifdef ENABLE_ARCHIVE
42
	&input_plugin_archive,
43
#endif
44 45 46
#ifdef HAVE_CURL
	&input_plugin_curl,
#endif
47 48 49
#ifdef ENABLE_MMS
	&input_plugin_mms,
#endif
50 51 52 53 54
};

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

55
void input_stream_global_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
56
{
Max Kellermann's avatar
Max Kellermann committed
57 58 59
#ifdef HAVE_CURL
	input_curl_global_init();
#endif
60 61
}

62 63
void input_stream_global_finish(void)
{
Max Kellermann's avatar
Max Kellermann committed
64 65 66
#ifdef HAVE_CURL
	input_curl_global_finish();
#endif
67 68
}

69
bool
70
input_stream_open(struct input_stream *is, const char *url)
Avuton Olrich's avatar
Avuton Olrich committed
71
{
72 73
	is->seekable = false;
	is->ready = false;
74
	is->offset = 0;
75
	is->size = -1;
76 77 78
	is->error = 0;
	is->mime = NULL;

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

82
		if (plugin->open(is, url)) {
83 84 85 86 87 88
			assert(is->plugin != NULL);
			assert(is->plugin->open == NULL ||
			       is->plugin == plugin);
			assert(is->plugin->close != NULL);
			assert(is->plugin->read != NULL);
			assert(is->plugin->eof != NULL);
89
			assert(!is->seekable || is->plugin->seek != NULL);
90

91
			return true;
92 93
		}
	}
94

95
	return false;
96 97
}

98
bool
99
input_stream_seek(struct input_stream *is, off_t offset, int whence)
Avuton Olrich's avatar
Avuton Olrich committed
100
{
101 102 103
	if (is->plugin->seek == NULL)
		return false;

104
	return is->plugin->seek(is, offset, whence);
105 106
}

107 108 109 110 111 112 113 114 115 116
struct tag *
input_stream_tag(struct input_stream *is)
{
	assert(is != NULL);

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

117 118
size_t
input_stream_read(struct input_stream *is, void *ptr, size_t size)
119
{
120 121 122
	assert(ptr != NULL);
	assert(size > 0);

123
	return is->plugin->read(is, ptr, size);
124 125
}

126
void input_stream_close(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
127
{
128
	is->plugin->close(is);
129 130

	g_free(is->mime);
131
}
132

133
bool input_stream_eof(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
134
{
135
	return is->plugin->eof(is);
136
}
137

138
int input_stream_buffer(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
139
{
140 141 142
	if (is->plugin->buffer == NULL)
		return 0;

143
	return is->plugin->buffer(is);
144
}