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"
|
2009-12-14 22:41:29 +01:00
|
|
|
#include "input_stream.h"
|
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,
|
|
|
|
GMutex *mutex, GCond *cond,
|
|
|
|
GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-11-14 23:53:04 +01:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
assert(mutex != NULL);
|
2009-11-14 23:53:04 +01:00
|
|
|
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) {
|
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-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);
|
|
|
|
assert(is->plugin != NULL);
|
|
|
|
|
|
|
|
return is->plugin->check == NULL ||
|
|
|
|
is->plugin->check(is, error_r);
|
|
|
|
}
|
|
|
|
|
2011-09-14 22:25:29 +02:00
|
|
|
void
|
|
|
|
input_stream_update(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->plugin != NULL);
|
|
|
|
|
|
|
|
if (is->plugin->update != NULL)
|
|
|
|
is->plugin->update(is);
|
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
void
|
|
|
|
input_stream_wait_ready(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->mutex != NULL);
|
|
|
|
assert(is->cond != NULL);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
input_stream_update(is);
|
|
|
|
if (is->ready)
|
|
|
|
break;
|
|
|
|
|
|
|
|
g_cond_wait(is->cond, is->mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_stream_lock_wait_ready(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->mutex != NULL);
|
|
|
|
assert(is->cond != NULL);
|
|
|
|
|
|
|
|
g_mutex_lock(is->mutex);
|
|
|
|
input_stream_wait_ready(is);
|
|
|
|
g_mutex_unlock(is->mutex);
|
|
|
|
}
|
|
|
|
|
2013-01-07 23:23:58 +01:00
|
|
|
bool
|
|
|
|
input_stream_cheap_seeking(const struct input_stream *is)
|
|
|
|
{
|
|
|
|
return is->seekable && (is->uri == NULL || !uri_has_scheme(is->uri));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
assert(is->plugin != NULL);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
assert(is->plugin != NULL);
|
|
|
|
|
|
|
|
if (is->plugin->seek == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (is->mutex == NULL)
|
|
|
|
/* no locking */
|
|
|
|
return input_stream_seek(is, offset, whence, error_r);
|
|
|
|
|
|
|
|
g_mutex_lock(is->mutex);
|
|
|
|
bool success = input_stream_seek(is, offset, whence, error_r);
|
|
|
|
g_mutex_unlock(is->mutex);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
struct tag *
|
|
|
|
input_stream_tag(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
2011-09-14 21:46:41 +02:00
|
|
|
assert(is->plugin != NULL);
|
2009-01-03 23:29:45 +01:00
|
|
|
|
|
|
|
return is->plugin->tag != NULL
|
|
|
|
? is->plugin->tag(is)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
struct tag *
|
|
|
|
input_stream_lock_tag(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->plugin != NULL);
|
|
|
|
|
|
|
|
if (is->plugin->tag == NULL)
|
2013-01-10 10:15:09 +01:00
|
|
|
return nullptr;
|
2011-09-14 21:46:41 +02:00
|
|
|
|
|
|
|
if (is->mutex == NULL)
|
|
|
|
/* no locking */
|
|
|
|
return input_stream_tag(is);
|
|
|
|
|
|
|
|
g_mutex_lock(is->mutex);
|
|
|
|
struct tag *tag = input_stream_tag(is);
|
|
|
|
g_mutex_unlock(is->mutex);
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
input_stream_available(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->plugin != NULL);
|
|
|
|
|
|
|
|
return is->plugin->available != NULL
|
|
|
|
? is->plugin->available(is)
|
|
|
|
: 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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (is->mutex == NULL)
|
|
|
|
/* no locking */
|
|
|
|
return input_stream_read(is, ptr, size, error_r);
|
|
|
|
|
|
|
|
g_mutex_lock(is->mutex);
|
|
|
|
size_t nbytes = input_stream_read(is, ptr, size, error_r);
|
|
|
|
g_mutex_unlock(is->mutex);
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2008-10-26 20:54:52 +01:00
|
|
|
void input_stream_close(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-12-30 23:27:37 +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
|
|
|
{
|
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
|
|
|
|
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);
|
|
|
|
assert(is->plugin != NULL);
|
|
|
|
|
|
|
|
if (is->mutex == NULL)
|
|
|
|
/* no locking */
|
|
|
|
return input_stream_eof(is);
|
2009-01-30 00:58:03 +01:00
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
g_mutex_lock(is->mutex);
|
|
|
|
bool eof = input_stream_eof(is);
|
|
|
|
g_mutex_unlock(is->mutex);
|
|
|
|
return eof;
|
2004-05-18 11:54:45 +02:00
|
|
|
}
|
2011-09-14 21:46:41 +02:00
|
|
|
|