2014-02-06 07:29:26 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2014-02-06 07:29:26 +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 "config.h"
|
|
|
|
#include "NfsInputPlugin.hxx"
|
2014-03-15 15:29:10 +01:00
|
|
|
#include "../AsyncInputStream.hxx"
|
2014-02-06 07:29:26 +01:00
|
|
|
#include "../InputPlugin.hxx"
|
2014-03-15 15:29:10 +01:00
|
|
|
#include "lib/nfs/Glue.hxx"
|
|
|
|
#include "lib/nfs/FileReader.hxx"
|
2015-11-06 09:09:02 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2014-02-06 07:29:26 +01:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
|
2014-04-23 12:53:19 +02:00
|
|
|
#include <string.h>
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
/**
|
|
|
|
* Do not buffer more than this number of bytes. It should be a
|
|
|
|
* reasonable limit that doesn't make low-end machines suffer too
|
|
|
|
* much, but doesn't cause stuttering on high-latency lines.
|
|
|
|
*/
|
|
|
|
static const size_t NFS_MAX_BUFFERED = 512 * 1024;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resume the stream at this number of bytes after it has been paused.
|
|
|
|
*/
|
|
|
|
static const size_t NFS_RESUME_AT = 384 * 1024;
|
|
|
|
|
|
|
|
class NfsInputStream final : public AsyncInputStream, NfsFileReader {
|
|
|
|
uint64_t next_offset;
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2014-09-02 20:13:38 +02:00
|
|
|
bool reconnect_on_resume, reconnecting;
|
|
|
|
|
2014-02-06 07:29:26 +01:00
|
|
|
public:
|
2016-06-17 17:44:45 +02:00
|
|
|
NfsInputStream(const char *_uri, Mutex &_mutex, Cond &_cond)
|
2014-03-15 15:29:10 +01:00
|
|
|
:AsyncInputStream(_uri, _mutex, _cond,
|
2016-06-17 17:44:45 +02:00
|
|
|
NFS_MAX_BUFFERED,
|
2014-09-02 20:13:38 +02:00
|
|
|
NFS_RESUME_AT),
|
|
|
|
reconnect_on_resume(false), reconnecting(false) {}
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
virtual ~NfsInputStream() {
|
|
|
|
DeferClose();
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
void Open() {
|
2014-03-15 15:29:10 +01:00
|
|
|
assert(!IsReady());
|
2014-05-11 17:14:49 +02:00
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
NfsFileReader::Open(GetURI());
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
private:
|
2016-09-16 16:55:57 +02:00
|
|
|
void DoRead();
|
2014-03-15 15:29:10 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/* virtual methods from AsyncInputStream */
|
|
|
|
virtual void DoResume() override;
|
|
|
|
virtual void DoSeek(offset_type new_offset) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/* virtual methods from NfsFileReader */
|
|
|
|
void OnNfsFileOpen(uint64_t size) override;
|
|
|
|
void OnNfsFileRead(const void *data, size_t size) override;
|
2016-09-16 16:55:57 +02:00
|
|
|
void OnNfsFileError(std::exception_ptr &&e) override;
|
2014-05-11 17:14:49 +02:00
|
|
|
};
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2016-09-16 16:55:57 +02:00
|
|
|
void
|
2014-03-15 15:29:10 +01:00
|
|
|
NfsInputStream::DoRead()
|
2014-05-11 17:14:49 +02:00
|
|
|
{
|
2014-03-15 15:29:10 +01:00
|
|
|
assert(NfsFileReader::IsIdle());
|
|
|
|
|
|
|
|
int64_t remaining = size - next_offset;
|
|
|
|
if (remaining <= 0)
|
2016-09-16 16:55:57 +02:00
|
|
|
return;
|
2014-03-15 15:29:10 +01:00
|
|
|
|
2014-06-21 14:06:31 +02:00
|
|
|
const size_t buffer_space = GetBufferSpace();
|
|
|
|
if (buffer_space == 0) {
|
2014-03-15 15:29:10 +01:00
|
|
|
Pause();
|
2016-09-16 16:55:57 +02:00
|
|
|
return;
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
2014-06-21 14:06:31 +02:00
|
|
|
size_t nbytes = std::min<size_t>(std::min<uint64_t>(remaining, 32768),
|
|
|
|
buffer_space);
|
2014-05-11 17:14:49 +02:00
|
|
|
|
2016-09-16 16:55:57 +02:00
|
|
|
try {
|
2016-09-16 16:51:36 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2016-09-16 16:55:57 +02:00
|
|
|
NfsFileReader::Read(next_offset, nbytes);
|
|
|
|
} catch (...) {
|
|
|
|
postponed_exception = std::current_exception();
|
|
|
|
cond.broadcast();
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
2014-05-11 17:14:49 +02:00
|
|
|
}
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
void
|
|
|
|
NfsInputStream::DoResume()
|
|
|
|
{
|
2014-09-02 20:13:38 +02:00
|
|
|
if (reconnect_on_resume) {
|
|
|
|
/* the NFS connection has died while this stream was
|
|
|
|
"paused" - attempt to reconnect */
|
|
|
|
|
|
|
|
reconnect_on_resume = false;
|
|
|
|
reconnecting = true;
|
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
ScopeUnlock unlock(mutex);
|
2014-09-02 20:13:38 +02:00
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
NfsFileReader::Close();
|
|
|
|
NfsFileReader::Open(GetURI());
|
2014-09-02 20:13:38 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
assert(NfsFileReader::IsIdle());
|
|
|
|
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NfsInputStream::DoSeek(offset_type new_offset)
|
|
|
|
{
|
2016-09-16 16:51:36 +02:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
NfsFileReader::CancelRead();
|
|
|
|
}
|
2014-03-15 15:29:10 +01:00
|
|
|
|
|
|
|
next_offset = offset = new_offset;
|
|
|
|
SeekDone();
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NfsInputStream::OnNfsFileOpen(uint64_t _size)
|
|
|
|
{
|
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
|
2014-09-02 20:13:38 +02:00
|
|
|
if (reconnecting) {
|
|
|
|
/* reconnect has succeeded */
|
|
|
|
|
|
|
|
reconnecting = false;
|
|
|
|
DoRead();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
size = _size;
|
|
|
|
seekable = true;
|
|
|
|
next_offset = 0;
|
|
|
|
SetReady();
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NfsInputStream::OnNfsFileRead(const void *data, size_t data_size)
|
|
|
|
{
|
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
assert(!IsBufferFull());
|
|
|
|
assert(IsBufferFull() == (GetBufferSpace() == 0));
|
|
|
|
AppendToBuffer(data, data_size);
|
|
|
|
|
|
|
|
next_offset += data_size;
|
|
|
|
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-16 16:55:57 +02:00
|
|
|
NfsInputStream::OnNfsFileError(std::exception_ptr &&e)
|
2014-03-15 15:29:10 +01:00
|
|
|
{
|
|
|
|
const ScopeLock protect(mutex);
|
2014-09-02 20:13:38 +02:00
|
|
|
|
|
|
|
if (IsPaused()) {
|
|
|
|
/* while we're paused, don't report this error to the
|
|
|
|
client just yet (it might just be timeout, maybe
|
|
|
|
playback has been paused for quite some time) -
|
|
|
|
wait until the stream gets resumed and try to
|
|
|
|
reconnect, to give it another chance */
|
|
|
|
|
|
|
|
reconnect_on_resume = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-16 16:55:57 +02:00
|
|
|
postponed_exception = std::move(e);
|
2014-03-15 15:29:10 +01:00
|
|
|
|
|
|
|
if (IsSeekPending())
|
|
|
|
SeekDone();
|
|
|
|
else if (!IsReady())
|
|
|
|
SetReady();
|
2014-08-31 18:26:32 +02:00
|
|
|
else
|
|
|
|
cond.broadcast();
|
2014-03-15 15:29:10 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 07:29:26 +01:00
|
|
|
/*
|
|
|
|
* InputPlugin methods
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-09-05 12:05:54 +02:00
|
|
|
static void
|
|
|
|
input_nfs_init(const ConfigBlock &)
|
2014-03-15 15:29:10 +01:00
|
|
|
{
|
|
|
|
nfs_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_nfs_finish()
|
|
|
|
{
|
|
|
|
nfs_finish();
|
|
|
|
}
|
|
|
|
|
2014-02-06 07:29:26 +01:00
|
|
|
static InputStream *
|
|
|
|
input_nfs_open(const char *uri,
|
2016-09-09 15:37:06 +02:00
|
|
|
Mutex &mutex, Cond &cond)
|
2014-02-06 07:29:26 +01:00
|
|
|
{
|
|
|
|
if (!StringStartsWith(uri, "nfs://"))
|
|
|
|
return nullptr;
|
|
|
|
|
2016-06-17 17:44:45 +02:00
|
|
|
NfsInputStream *is = new NfsInputStream(uri, mutex, cond);
|
2016-09-09 15:37:06 +02:00
|
|
|
try {
|
|
|
|
is->Open();
|
|
|
|
} catch (...) {
|
2014-03-15 15:29:10 +01:00
|
|
|
delete is;
|
2016-09-09 15:37:06 +02:00
|
|
|
throw;
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
return is;
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const InputPlugin input_plugin_nfs = {
|
|
|
|
"nfs",
|
2014-03-15 15:29:10 +01:00
|
|
|
input_nfs_init,
|
|
|
|
input_nfs_finish,
|
2014-02-06 07:29:26 +01:00
|
|
|
input_nfs_open,
|
|
|
|
};
|