2014-03-15 16:42:15 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2014-03-15 16:42:15 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ThreadInputStream.hxx"
|
2018-06-22 19:37:18 +02:00
|
|
|
#include "CondHandler.hxx"
|
2014-03-15 16:42:15 +01:00
|
|
|
#include "thread/Name.hxx"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-09-21 21:55:24 +02:00
|
|
|
ThreadInputStream::ThreadInputStream(const char *_plugin,
|
|
|
|
const char *_uri,
|
2018-06-22 19:37:18 +02:00
|
|
|
Mutex &_mutex,
|
2018-01-20 18:53:18 +01:00
|
|
|
size_t _buffer_size) noexcept
|
2018-06-22 19:37:18 +02:00
|
|
|
:InputStream(_uri, _mutex),
|
2017-09-21 21:55:24 +02:00
|
|
|
plugin(_plugin),
|
|
|
|
thread(BIND_THIS_METHOD(ThreadFunc)),
|
2017-09-21 21:52:44 +02:00
|
|
|
allocation(_buffer_size),
|
|
|
|
buffer(&allocation.front(), allocation.size())
|
2017-09-21 21:55:24 +02:00
|
|
|
{
|
2017-09-21 21:52:44 +02:00
|
|
|
allocation.ForkCow(false);
|
2017-09-21 21:55:24 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:23:31 +01:00
|
|
|
void
|
|
|
|
ThreadInputStream::Stop() noexcept
|
2014-03-15 16:42:15 +01:00
|
|
|
{
|
2018-03-15 19:23:31 +01:00
|
|
|
if (!thread.IsDefined())
|
|
|
|
return;
|
|
|
|
|
2016-09-16 17:22:16 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
2016-09-16 17:22:16 +02:00
|
|
|
close = true;
|
2019-04-25 18:33:09 +02:00
|
|
|
wake_cond.notify_one();
|
2016-09-16 17:22:16 +02:00
|
|
|
}
|
2014-05-11 16:59:19 +02:00
|
|
|
|
|
|
|
Cancel();
|
|
|
|
|
|
|
|
thread.Join();
|
|
|
|
|
2017-09-21 21:52:44 +02:00
|
|
|
buffer.Clear();
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 19:06:30 +02:00
|
|
|
void
|
|
|
|
ThreadInputStream::Start()
|
2014-03-15 16:42:15 +01:00
|
|
|
{
|
2017-02-10 22:41:11 +01:00
|
|
|
thread.Start();
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:53:18 +01:00
|
|
|
inline void
|
|
|
|
ThreadInputStream::ThreadFunc() noexcept
|
2014-03-15 16:42:15 +01:00
|
|
|
{
|
2014-05-11 18:25:55 +02:00
|
|
|
FormatThreadName("input:%s", plugin);
|
2014-03-15 16:42:15 +01:00
|
|
|
|
2019-04-25 18:53:38 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
2016-09-16 17:22:16 +02:00
|
|
|
|
2016-09-16 17:20:55 +02:00
|
|
|
try {
|
|
|
|
Open();
|
|
|
|
} catch (...) {
|
|
|
|
postponed_exception = std::current_exception();
|
2018-03-14 13:15:03 +01:00
|
|
|
SetReady();
|
2014-03-15 16:42:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we're ready, tell it to our client */
|
2014-05-11 16:02:57 +02:00
|
|
|
SetReady();
|
2014-03-15 16:42:15 +01:00
|
|
|
|
|
|
|
while (!close) {
|
2016-09-16 17:20:55 +02:00
|
|
|
assert(!postponed_exception);
|
2014-03-15 16:42:15 +01:00
|
|
|
|
2017-09-21 21:52:44 +02:00
|
|
|
auto w = buffer.Write();
|
2017-11-10 19:24:33 +01:00
|
|
|
if (w.empty()) {
|
2019-04-25 18:53:38 +02:00
|
|
|
wake_cond.wait(lock);
|
2014-03-15 16:42:15 +01:00
|
|
|
} else {
|
2016-09-16 17:22:16 +02:00
|
|
|
size_t nbytes;
|
|
|
|
|
2016-09-16 17:20:55 +02:00
|
|
|
try {
|
2016-09-16 17:22:16 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2016-09-16 17:20:55 +02:00
|
|
|
nbytes = ThreadRead(w.data, w.size);
|
|
|
|
} catch (...) {
|
|
|
|
postponed_exception = std::current_exception();
|
2018-06-22 19:37:18 +02:00
|
|
|
InvokeOnAvailable();
|
2016-09-16 17:20:55 +02:00
|
|
|
break;
|
2016-09-16 17:22:16 +02:00
|
|
|
}
|
2014-03-15 16:42:15 +01:00
|
|
|
|
2018-06-22 19:37:18 +02:00
|
|
|
InvokeOnAvailable();
|
2014-03-15 16:42:15 +01:00
|
|
|
|
|
|
|
if (nbytes == 0) {
|
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-21 21:52:44 +02:00
|
|
|
buffer.Append(nbytes);
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
ThreadInputStream::Check()
|
2014-03-15 16:42:15 +01:00
|
|
|
{
|
2014-10-11 21:57:31 +02:00
|
|
|
assert(!thread.IsInside());
|
|
|
|
|
2016-09-16 17:20:55 +02:00
|
|
|
if (postponed_exception)
|
|
|
|
std::rethrow_exception(postponed_exception);
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
ThreadInputStream::IsAvailable() noexcept
|
2014-03-15 16:42:15 +01:00
|
|
|
{
|
2014-10-11 21:57:31 +02:00
|
|
|
assert(!thread.IsInside());
|
|
|
|
|
2017-11-10 19:24:33 +01:00
|
|
|
return !buffer.empty() || eof || postponed_exception;
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2016-09-09 18:47:42 +02:00
|
|
|
ThreadInputStream::Read(void *ptr, size_t read_size)
|
2014-03-15 16:42:15 +01:00
|
|
|
{
|
2014-10-11 21:57:31 +02:00
|
|
|
assert(!thread.IsInside());
|
|
|
|
|
2018-06-22 19:37:18 +02:00
|
|
|
CondInputStreamHandler cond_handler;
|
|
|
|
|
2014-03-15 16:42:15 +01:00
|
|
|
while (true) {
|
2016-09-16 17:20:55 +02:00
|
|
|
if (postponed_exception)
|
|
|
|
std::rethrow_exception(postponed_exception);
|
2014-03-15 16:42:15 +01:00
|
|
|
|
2017-09-21 21:52:44 +02:00
|
|
|
auto r = buffer.Read();
|
2017-11-10 19:24:33 +01:00
|
|
|
if (!r.empty()) {
|
2014-05-11 16:02:57 +02:00
|
|
|
size_t nbytes = std::min(read_size, r.size);
|
2014-03-15 16:42:15 +01:00
|
|
|
memcpy(ptr, r.data, nbytes);
|
2017-09-21 21:52:44 +02:00
|
|
|
buffer.Consume(nbytes);
|
2019-04-25 18:33:09 +02:00
|
|
|
wake_cond.notify_all();
|
2014-05-11 16:02:57 +02:00
|
|
|
offset += nbytes;
|
2014-03-15 16:42:15 +01:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eof)
|
|
|
|
return 0;
|
|
|
|
|
2018-06-22 19:37:18 +02:00
|
|
|
const ScopeExchangeInputStreamHandler h(*this, &cond_handler);
|
|
|
|
cond_handler.cond.wait(mutex);
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
ThreadInputStream::IsEOF() noexcept
|
2014-03-15 16:42:15 +01:00
|
|
|
{
|
2014-10-11 21:57:31 +02:00
|
|
|
assert(!thread.IsInside());
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
return eof;
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|