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-04-08 23:30:21 +02:00
|
|
|
#include "util/UriUtil.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2004-05-04 21:08:46 +02:00
|
|
|
|
2008-11-15 19:27:30 +01:00
|
|
|
#include <assert.h>
|
2008-10-26 19:30:13 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain input_domain("input");
|
2009-11-14 23:53:04 +01:00
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream *
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::Open(const char *url,
|
|
|
|
Mutex &mutex, Cond &cond,
|
|
|
|
Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
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
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
is = plugin->open(url, mutex, cond, error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (is != nullptr) {
|
|
|
|
assert(is->plugin.close != nullptr);
|
|
|
|
assert(is->plugin.read != nullptr);
|
|
|
|
assert(is->plugin.eof != nullptr);
|
|
|
|
assert(!is->seekable || is->plugin.seek != nullptr);
|
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;
|
2013-08-10 18:02:44 +02:00
|
|
|
} else if (error.IsDefined())
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2008-10-26 20:38:44 +01:00
|
|
|
}
|
2004-05-04 21:08:46 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(input_domain, "Unrecognized URI");
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 21:06:12 +02:00
|
|
|
bool
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::Check(Error &error)
|
2011-09-16 21:06:12 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
return plugin.check == nullptr || plugin.check(this, error);
|
2011-09-16 21:06:12 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 22:25:29 +02:00
|
|
|
void
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::Update()
|
2011-09-14 22:25:29 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
if (plugin.update != nullptr)
|
|
|
|
plugin.update(this);
|
2011-09-14 22:25:29 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
void
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::WaitReady()
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
|
|
|
while (true) {
|
2013-09-05 00:06:31 +02:00
|
|
|
Update();
|
|
|
|
if (ready)
|
2011-09-14 21:46:41 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
cond.wait(mutex);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::LockWaitReady()
|
2013-01-24 19:14:40 +01:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
WaitReady();
|
2013-01-24 19:14:40 +01:00
|
|
|
}
|
|
|
|
|
2013-01-07 23:23:58 +01:00
|
|
|
bool
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::CheapSeeking() const
|
2013-01-07 23:23:58 +01:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
return IsSeekable() && !uri_has_scheme(uri.c_str());
|
2013-01-07 23:23:58 +01:00
|
|
|
}
|
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
bool
|
2013-10-17 09:43:55 +02:00
|
|
|
input_stream::Seek(offset_type _offset, int whence, Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
if (plugin.seek == nullptr)
|
2009-01-30 00:58:03 +01:00
|
|
|
return false;
|
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return plugin.seek(this, _offset, whence, error);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
bool
|
2013-10-17 09:43:55 +02:00
|
|
|
input_stream::LockSeek(offset_type _offset, int whence, Error &error)
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
if (plugin.seek == nullptr)
|
2011-09-14 21:46:41 +02:00
|
|
|
return false;
|
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
return Seek(_offset, whence, error);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::ReadTag()
|
2009-01-03 23:29:45 +01:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
return plugin.tag != nullptr
|
|
|
|
? plugin.tag(this)
|
|
|
|
: nullptr;
|
2009-01-03 23:29:45 +01:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::LockReadTag()
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
if (plugin.tag == nullptr)
|
2013-01-10 10:15:09 +01:00
|
|
|
return nullptr;
|
2011-09-14 21:46:41 +02:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
return ReadTag();
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::IsAvailable()
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
return plugin.available != nullptr
|
|
|
|
? plugin.available(this)
|
2011-09-14 21:46:41 +02:00
|
|
|
: true;
|
|
|
|
}
|
|
|
|
|
2008-10-26 20:34:47 +01:00
|
|
|
size_t
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::Read(void *ptr, size_t _size, Error &error)
|
2004-05-04 21:08:46 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(ptr != nullptr);
|
2013-09-05 00:06:31 +02:00
|
|
|
assert(_size > 0);
|
2008-11-15 19:27:30 +01:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return plugin.read(this, ptr, _size, error);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
size_t
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::LockRead(void *ptr, size_t _size, Error &error)
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(ptr != nullptr);
|
2013-09-05 00:06:31 +02:00
|
|
|
assert(_size > 0);
|
2011-09-14 21:46:41 +02:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
return Read(ptr, _size, error);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
void
|
|
|
|
input_stream::Close()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
plugin.close(this);
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
bool
|
|
|
|
input_stream::IsEOF()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
return plugin.eof(this);
|
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
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::LockIsEOF()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
return IsEOF();
|
2004-05-18 11:54:45 +02:00
|
|
|
}
|
2011-09-14 21:46:41 +02:00
|
|
|
|