2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-05-04 21:08:46 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-05-04 21:08:46 +02:00
|
|
|
*/
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
#include "config.h"
|
2009-12-14 22:41:29 +01:00
|
|
|
#include "input_stream.h"
|
|
|
|
#include "input_registry.h"
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "input_plugin.h"
|
2009-12-29 23:55:40 +01:00
|
|
|
#include "input/rewind_input_plugin.h"
|
2004-05-04 21:08:46 +02:00
|
|
|
|
2008-10-26 21:02:49 +01:00
|
|
|
#include <glib.h>
|
2008-11-15 19:27:30 +01:00
|
|
|
#include <assert.h>
|
2008-10-26 19:30:13 +01:00
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
static inline GQuark
|
|
|
|
input_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("input");
|
|
|
|
}
|
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
bool
|
2009-11-14 23:53:04 +01:00
|
|
|
input_stream_open(struct input_stream *is, const char *url, GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-11-14 23:53:04 +01:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
assert(error_r == NULL || *error_r == NULL);
|
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
is->seekable = false;
|
|
|
|
is->ready = false;
|
2008-10-26 20:34:47 +01:00
|
|
|
is->offset = 0;
|
2008-11-16 20:42:08 +01:00
|
|
|
is->size = -1;
|
2008-10-26 20:34:47 +01:00
|
|
|
is->mime = NULL;
|
|
|
|
|
2009-12-14 22:43:00 +01:00
|
|
|
for (unsigned i = 0; input_plugins[i] != NULL; ++i) {
|
2008-10-26 20:38:44 +01:00
|
|
|
const struct input_plugin *plugin = input_plugins[i];
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
if (!input_plugins_enabled[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (plugin->open(is, url, &error)) {
|
2009-01-30 00:40:14 +01:00
|
|
|
assert(is->plugin != NULL);
|
|
|
|
assert(is->plugin->close != NULL);
|
|
|
|
assert(is->plugin->read != NULL);
|
|
|
|
assert(is->plugin->eof != NULL);
|
2009-01-30 00:58:03 +01:00
|
|
|
assert(!is->seekable || is->plugin->seek != NULL);
|
2009-01-30 00:40:14 +01:00
|
|
|
|
2009-12-29 23:55:40 +01:00
|
|
|
input_rewind_open(is);
|
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
return true;
|
2009-11-14 23:53:04 +01:00
|
|
|
} else if (error != NULL) {
|
|
|
|
g_propagate_error(error_r, error);
|
|
|
|
return false;
|
2008-10-26 20:38:44 +01:00
|
|
|
}
|
|
|
|
}
|
2004-05-04 21:08:46 +02:00
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
g_set_error(error_r, input_quark(), 0, "Unrecognized URI");
|
2008-10-26 20:56:46 +01:00
|
|
|
return false;
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
bool
|
2009-11-14 23:53:04 +01:00
|
|
|
input_stream_seek(struct input_stream *is, goffset offset, int whence,
|
|
|
|
GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-30 00:58:03 +01:00
|
|
|
if (is->plugin->seek == NULL)
|
|
|
|
return false;
|
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
return is->plugin->seek(is, offset, whence, error_r);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
struct tag *
|
|
|
|
input_stream_tag(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
|
|
|
return is->plugin->tag != NULL
|
|
|
|
? is->plugin->tag(is)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-26 20:34:47 +01:00
|
|
|
size_t
|
2009-11-14 23:53:04 +01:00
|
|
|
input_stream_read(struct input_stream *is, void *ptr, size_t size,
|
|
|
|
GError **error_r)
|
2004-05-04 21:08:46 +02:00
|
|
|
{
|
2008-11-15 19:27:30 +01:00
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(size > 0);
|
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
return is->plugin->read(is, ptr, size, error_r);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2008-10-26 20:54:52 +01:00
|
|
|
void input_stream_close(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-26 20:54:52 +01:00
|
|
|
is->plugin->close(is);
|
2008-10-26 21:02:49 +01:00
|
|
|
|
|
|
|
g_free(is->mime);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
bool input_stream_eof(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-26 20:38:44 +01:00
|
|
|
return is->plugin->eof(is);
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
2004-05-18 11:54:45 +02:00
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
int
|
|
|
|
input_stream_buffer(struct input_stream *is, GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-30 00:58:03 +01:00
|
|
|
if (is->plugin->buffer == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
return is->plugin->buffer(is, error_r);
|
2004-05-18 11:54:45 +02:00
|
|
|
}
|