2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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"
|
2014-05-21 18:21:25 +02:00
|
|
|
#include "thread/Cond.hxx"
|
2015-11-06 09:09:02 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2004-05-04 21:08:46 +02:00
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2008-11-15 19:27:30 +01:00
|
|
|
#include <assert.h>
|
2008-10-26 19:30:13 +01:00
|
|
|
|
2014-05-11 16:59:19 +02:00
|
|
|
InputStream::~InputStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
InputStream::Check()
|
2011-09-16 21:06:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-14 22:25:29 +02:00
|
|
|
void
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::Update()
|
2011-09-14 22:25:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-11 15:34:48 +02:00
|
|
|
void
|
|
|
|
InputStream::SetReady()
|
|
|
|
{
|
|
|
|
assert(!ready);
|
|
|
|
|
|
|
|
ready = true;
|
|
|
|
cond.broadcast();
|
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
void
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::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-10-23 22:08:59 +02:00
|
|
|
InputStream::LockWaitReady()
|
2013-01-24 19:14:40 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-09-05 00:06:31 +02:00
|
|
|
WaitReady();
|
2013-01-24 19:14:40 +01:00
|
|
|
}
|
|
|
|
|
2014-08-19 21:38:18 +02:00
|
|
|
/**
|
|
|
|
* Is seeking on resources behind this URI "expensive"? For example,
|
|
|
|
* seeking in a HTTP file requires opening a new connection with a new
|
|
|
|
* HTTP request.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
|
|
|
ExpensiveSeeking(const char *uri)
|
|
|
|
{
|
2014-08-19 21:39:12 +02:00
|
|
|
return StringStartsWith(uri, "http://") ||
|
|
|
|
StringStartsWith(uri, "https://");
|
2014-08-19 21:38:18 +02:00
|
|
|
}
|
|
|
|
|
2013-01-07 23:23:58 +01:00
|
|
|
bool
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::CheapSeeking() const
|
2013-01-07 23:23:58 +01:00
|
|
|
{
|
2014-08-19 21:38:18 +02:00
|
|
|
return IsSeekable() && !ExpensiveSeeking(uri.c_str());
|
2013-01-07 23:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
InputStream::Seek(gcc_unused offset_type new_offset)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2016-09-09 18:47:42 +02:00
|
|
|
throw std::runtime_error("Seeking is not implemented");
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
InputStream::LockSeek(offset_type _offset)
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2016-09-09 18:47:42 +02:00
|
|
|
Seek(_offset);
|
2013-10-23 21:22:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
InputStream::LockSkip(offset_type _offset)
|
2016-02-22 13:14:19 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2016-09-09 18:47:42 +02:00
|
|
|
Skip(_offset);
|
2016-02-22 13:14:19 +01:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::ReadTag()
|
2009-01-03 23:29:45 +01:00
|
|
|
{
|
2014-05-11 17:14:49 +02:00
|
|
|
return nullptr;
|
2009-01-03 23:29:45 +01:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::LockReadTag()
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-09-05 00:06:31 +02:00
|
|
|
return ReadTag();
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::IsAvailable()
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2014-05-11 17:14:49 +02:00
|
|
|
return true;
|
2004-05-04 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
size_t
|
2016-09-09 18:47:42 +02:00
|
|
|
InputStream::LockRead(void *ptr, size_t _size)
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
2014-12-26 14:26:31 +01:00
|
|
|
#if !CLANG_CHECK_VERSION(3,6)
|
|
|
|
/* disabled on clang due to -Wtautological-pointer-compare */
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(ptr != nullptr);
|
2014-12-26 14:26:31 +01:00
|
|
|
#endif
|
2013-09-05 00:06:31 +02:00
|
|
|
assert(_size > 0);
|
2011-09-14 21:46:41 +02:00
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2016-09-09 18:47:42 +02:00
|
|
|
return Read(ptr, _size);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
InputStream::ReadFull(void *_ptr, size_t _size)
|
2015-06-05 16:27:10 +02:00
|
|
|
{
|
|
|
|
uint8_t *ptr = (uint8_t *)_ptr;
|
|
|
|
|
|
|
|
size_t nbytes_total = 0;
|
|
|
|
while (_size > 0) {
|
2016-09-09 18:47:42 +02:00
|
|
|
size_t nbytes = Read(ptr + nbytes_total, _size);
|
2015-06-05 16:27:10 +02:00
|
|
|
if (nbytes == 0)
|
2016-09-09 18:47:42 +02:00
|
|
|
throw std::runtime_error("Unexpected end of file");
|
2015-06-05 16:27:10 +02:00
|
|
|
|
|
|
|
nbytes_total += nbytes;
|
|
|
|
_size -= nbytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
InputStream::LockReadFull(void *ptr, size_t _size)
|
2015-06-05 16:27:10 +02:00
|
|
|
{
|
|
|
|
#if !CLANG_CHECK_VERSION(3,6)
|
|
|
|
/* disabled on clang due to -Wtautological-pointer-compare */
|
|
|
|
assert(ptr != nullptr);
|
|
|
|
#endif
|
|
|
|
assert(_size > 0);
|
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2016-09-09 18:47:42 +02:00
|
|
|
ReadFull(ptr, _size);
|
2015-06-05 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
bool
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::LockIsEOF()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-09-05 00:06:31 +02:00
|
|
|
return IsEOF();
|
2004-05-18 11:54:45 +02:00
|
|
|
}
|