2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 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"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-01-10 10:15:09 +01:00
|
|
|
#include "InputRegistry.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputPlugin.hxx"
|
2013-01-21 17:36:19 +01:00
|
|
|
#include "input/RewindInputPlugin.hxx"
|
2013-01-10 10:15:09 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2013-01-07 23:23:58 +01:00
|
|
|
#include "uri.h"
|
2013-01-10 10:15:09 +01:00
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream *
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_open(const char *url,
|
2013-01-27 17:20:50 +01:00
|
|
|
Mutex &mutex, Cond &cond,
|
2011-09-14 21:46:41 +02:00
|
|
|
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);
|
|
|
|
|
2012-06-12 22:21:48 +02:00
|
|
|
input_plugins_for_each_enabled(plugin) {
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream *is;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
is = plugin->open(url, mutex, cond, &error);
|
2009-12-30 23:27:37 +01:00
|
|
|
if (is != NULL) {
|
2013-01-28 23:35:01 +01:00
|
|
|
assert(is->plugin.close != NULL);
|
|
|
|
assert(is->plugin.read != NULL);
|
|
|
|
assert(is->plugin.eof != NULL);
|
|
|
|
assert(!is->seekable || is->plugin.seek != NULL);
|
2009-01-30 00:40:14 +01:00
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
is = input_rewind_open(is);
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
return is;
|
2009-11-14 23:53:04 +01:00
|
|
|
} else if (error != NULL) {
|
|
|
|
g_propagate_error(error_r, error);
|
2009-12-30 23:27:37 +01:00
|
|
|
return NULL;
|
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");
|
2011-09-15 09:48:28 +02:00
|
|
|
return NULL;
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 21:06:12 +02:00
|
|
|
bool
|
|
|
|
input_stream_check(struct input_stream *is, GError **error_r)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
return is->plugin.check == NULL ||
|
|
|
|
is->plugin.check(is, error_r);
|
2011-09-16 21:06:12 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 22:25:29 +02:00
|
|
|
void
|
|
|
|
input_stream_update(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
if (is->plugin.update != NULL)
|
|
|
|
is->plugin.update(is);
|
2011-09-14 22:25:29 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
void
|
|
|
|
input_stream_wait_ready(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
input_stream_update(is);
|
|
|
|
if (is->ready)
|
|
|
|
break;
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
is->cond.wait(is->mutex);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_stream_lock_wait_ready(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(is->mutex);
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_wait_ready(is);
|
|
|
|
}
|
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
const char *
|
|
|
|
input_stream_get_mime_type(const struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->ready);
|
|
|
|
|
2013-01-28 23:41:45 +01:00
|
|
|
return is->mime.empty() ? nullptr : is->mime.c_str();
|
2013-01-24 19:14:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_stream_override_mime_type(struct input_stream *is, const char *mime)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->ready);
|
|
|
|
|
2013-01-28 23:41:45 +01:00
|
|
|
is->mime = mime;
|
2013-01-24 19:14:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
goffset
|
|
|
|
input_stream_get_size(const struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->ready);
|
|
|
|
|
|
|
|
return is->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
goffset
|
|
|
|
input_stream_get_offset(const struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->ready);
|
|
|
|
|
|
|
|
return is->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
input_stream_is_seekable(const struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->ready);
|
|
|
|
|
|
|
|
return is->seekable;
|
|
|
|
}
|
|
|
|
|
2013-01-07 23:23:58 +01:00
|
|
|
bool
|
|
|
|
input_stream_cheap_seeking(const struct input_stream *is)
|
|
|
|
{
|
2013-01-28 23:41:45 +01:00
|
|
|
return is->seekable && !uri_has_scheme(is->uri.c_str());
|
2013-01-07 23:23:58 +01: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
|
|
|
{
|
2011-09-14 21:46:41 +02:00
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
if (is->plugin.seek == NULL)
|
2009-01-30 00:58:03 +01:00
|
|
|
return false;
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
return is->plugin.seek(is, offset, whence, error_r);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
bool
|
|
|
|
input_stream_lock_seek(struct input_stream *is, goffset offset, int whence,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
if (is->plugin.seek == NULL)
|
2011-09-14 21:46:41 +02:00
|
|
|
return false;
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(is->mutex);
|
2013-01-27 17:20:50 +01:00
|
|
|
return input_stream_seek(is, offset, whence, error_r);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
struct tag *
|
|
|
|
input_stream_tag(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
return is->plugin.tag != NULL
|
|
|
|
? is->plugin.tag(is)
|
2009-01-03 23:29:45 +01:00
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
struct tag *
|
|
|
|
input_stream_lock_tag(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
if (is->plugin.tag == NULL)
|
2013-01-10 10:15:09 +01:00
|
|
|
return nullptr;
|
2011-09-14 21:46:41 +02:00
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(is->mutex);
|
2013-01-27 17:20:50 +01:00
|
|
|
return input_stream_tag(is);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
input_stream_available(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
return is->plugin.available != NULL
|
|
|
|
? is->plugin.available(is)
|
2011-09-14 21:46:41 +02:00
|
|
|
: true;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
return is->plugin.read(is, ptr, size, error_r);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
size_t
|
|
|
|
input_stream_lock_read(struct input_stream *is, void *ptr, size_t size,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(size > 0);
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(is->mutex);
|
2013-01-27 17:20:50 +01:00
|
|
|
return input_stream_read(is, ptr, size, error_r);
|
2011-09-14 21:46:41 +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
|
|
|
{
|
2013-01-28 23:35:01 +01:00
|
|
|
is->plugin.close(is);
|
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
|
|
|
{
|
2013-01-28 23:35:01 +01:00
|
|
|
return is->plugin.eof(is);
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
2004-05-18 11:54:45 +02:00
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
bool
|
|
|
|
input_stream_lock_eof(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2011-09-14 21:46:41 +02:00
|
|
|
assert(is != NULL);
|
2009-01-30 00:58:03 +01:00
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(is->mutex);
|
2013-01-27 17:20:50 +01:00
|
|
|
return input_stream_eof(is);
|
2004-05-18 11:54:45 +02:00
|
|
|
}
|
2011-09-14 21:46:41 +02:00
|
|
|
|