2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2014-03-15 16:42:15 +01:00
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
2014-03-15 16:42:15 +01:00
|
|
|
#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
|
|
|
{
|
2022-04-26 20:43:31 +02:00
|
|
|
allocation.SetName("InputStream");
|
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
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<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);
|
2022-05-20 10:59:53 +02:00
|
|
|
nbytes = ThreadRead(w.data(), w.size());
|
2016-09-16 17:20:55 +02:00
|
|
|
} 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
|
2019-05-29 13:31:54 +02:00
|
|
|
ThreadInputStream::IsAvailable() const 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
|
2019-04-26 19:19:45 +02:00
|
|
|
ThreadInputStream::Read(std::unique_lock<Mutex> &lock,
|
|
|
|
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()) {
|
2022-05-20 10:59:53 +02:00
|
|
|
size_t nbytes = std::min(read_size, r.size());
|
|
|
|
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);
|
2019-04-26 19:19:45 +02:00
|
|
|
cond_handler.cond.wait(lock);
|
2014-03-15 16:42:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-29 13:31:54 +02:00
|
|
|
ThreadInputStream::IsEOF() const 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
|
|
|
}
|