2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 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
|
|
|
*/
|
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2018-06-22 19:37:18 +02:00
|
|
|
#include "Handler.hxx"
|
2017-12-20 15:15:27 +01:00
|
|
|
#include "tag/Tag.hxx"
|
2018-08-02 10:38:20 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2004-05-04 21:08:46 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2016-09-09 18:47:42 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2020-02-01 13:47:16 +01:00
|
|
|
InputStream::~InputStream() noexcept = default;
|
2014-05-11 16:59:19 +02:00
|
|
|
|
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
|
2017-12-26 11:31:05 +01:00
|
|
|
InputStream::Update() noexcept
|
2011-09-14 22:25:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-11 15:34:48 +02:00
|
|
|
void
|
2017-12-26 11:31:05 +01:00
|
|
|
InputStream::SetReady() noexcept
|
2014-05-11 15:34:48 +02:00
|
|
|
{
|
|
|
|
assert(!ready);
|
|
|
|
|
|
|
|
ready = true;
|
|
|
|
|
2018-06-22 19:37:18 +02:00
|
|
|
InvokeOnReady();
|
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
|
2017-05-08 14:44:49 +02:00
|
|
|
ExpensiveSeeking(const char *uri) noexcept
|
2014-08-19 21:38:18 +02:00
|
|
|
{
|
2018-08-02 10:38:20 +02:00
|
|
|
return StringStartsWithCaseASCII(uri, "http://") ||
|
2018-08-02 11:07:40 +02:00
|
|
|
StringStartsWithCaseASCII(uri, "tidal://") ||
|
|
|
|
StringStartsWithCaseASCII(uri, "qobuz://") ||
|
2018-08-02 10:38:20 +02:00
|
|
|
StringStartsWithCaseASCII(uri, "https://");
|
2014-08-19 21:38:18 +02:00
|
|
|
}
|
|
|
|
|
2013-01-07 23:23:58 +01:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
InputStream::CheapSeeking() const noexcept
|
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
|
|
|
}
|
|
|
|
|
2020-03-17 07:02:35 +01:00
|
|
|
//[[noreturn]]
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
2020-03-12 20:56:11 +01:00
|
|
|
InputStream::Seek(std::unique_lock<Mutex> &, [[maybe_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
|
|
|
{
|
2019-04-26 19:19:45 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
Seek(lock, _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
|
|
|
{
|
2019-04-26 19:19:45 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
Skip(lock, _offset);
|
2016-02-22 13:14:19 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 15:15:27 +01:00
|
|
|
std::unique_ptr<Tag>
|
2019-04-05 08:57:42 +02:00
|
|
|
InputStream::ReadTag() noexcept
|
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
|
|
|
}
|
|
|
|
|
2017-12-20 15:15:27 +01:00
|
|
|
std::unique_ptr<Tag>
|
2019-04-05 08:57:42 +02:00
|
|
|
InputStream::LockReadTag() noexcept
|
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
|
2019-05-29 13:31:54 +02:00
|
|
|
InputStream::IsAvailable() const noexcept
|
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
|
|
|
|
2019-04-26 19:19:45 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
return Read(lock, ptr, _size);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
2019-04-26 19:19:45 +02:00
|
|
|
InputStream::ReadFull(std::unique_lock<Mutex> &lock, void *_ptr, size_t _size)
|
2015-06-05 16:27:10 +02:00
|
|
|
{
|
2020-02-01 13:55:08 +01:00
|
|
|
auto *ptr = (uint8_t *)_ptr;
|
2015-06-05 16:27:10 +02:00
|
|
|
|
|
|
|
size_t nbytes_total = 0;
|
|
|
|
while (_size > 0) {
|
2019-04-26 19:19:45 +02:00
|
|
|
size_t nbytes = Read(lock, 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);
|
|
|
|
|
2019-04-26 19:19:45 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
ReadFull(lock, ptr, _size);
|
2015-06-05 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
bool
|
2019-05-29 13:31:54 +02:00
|
|
|
InputStream::LockIsEOF() const noexcept
|
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
|
|
|
}
|
2018-06-22 19:37:18 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
InputStream::InvokeOnReady() noexcept
|
|
|
|
{
|
|
|
|
if (handler != nullptr)
|
|
|
|
handler->OnInputStreamReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputStream::InvokeOnAvailable() noexcept
|
|
|
|
{
|
|
|
|
if (handler != nullptr)
|
|
|
|
handler->OnInputStreamAvailable();
|
|
|
|
}
|