2009-12-29 23:55:40 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-12-29 23:55:40 +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"
|
2013-01-21 17:36:19 +01:00
|
|
|
#include "RewindInputPlugin.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "../InputStream.hxx"
|
|
|
|
#include "../InputPlugin.hxx"
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2009-12-29 23:55:40 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
extern const InputPlugin rewind_input_plugin;
|
2013-01-21 17:36:19 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
class RewindInputStream {
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream base;
|
2009-12-30 23:27:37 +01:00
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *input;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The read position within the buffer. Undefined as long as
|
2013-01-21 17:36:19 +01:00
|
|
|
* ReadingFromBuffer() returns false.
|
2009-12-29 23:55:40 +01:00
|
|
|
*/
|
|
|
|
size_t head;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The write/append position within the buffer.
|
|
|
|
*/
|
|
|
|
size_t tail;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The size of this buffer is the maximum number of bytes
|
|
|
|
* which can be rewinded cheaply without passing the "seek"
|
|
|
|
* call to CURL.
|
|
|
|
*
|
|
|
|
* The origin of this buffer is always the beginning of the
|
|
|
|
* stream (offset 0).
|
|
|
|
*/
|
|
|
|
char buffer[64 * 1024];
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
public:
|
2013-10-23 22:08:59 +02:00
|
|
|
RewindInputStream(InputStream *_input)
|
2014-05-11 15:34:48 +02:00
|
|
|
:base(rewind_input_plugin, _input->GetURI(),
|
2013-01-28 23:35:01 +01:00
|
|
|
_input->mutex, _input->cond),
|
2013-01-28 20:32:23 +01:00
|
|
|
input(_input), tail(0) {
|
2013-01-21 17:36:19 +01:00
|
|
|
}
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
~RewindInputStream() {
|
2013-09-05 00:06:31 +02:00
|
|
|
input->Close();
|
2013-01-21 17:36:19 +01:00
|
|
|
}
|
2010-11-05 18:25:29 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
InputStream *GetBase() {
|
|
|
|
return &base;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check(Error &error) {
|
|
|
|
return input->Check(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update() {
|
|
|
|
if (!ReadingFromBuffer())
|
|
|
|
CopyAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tag *ReadTag() {
|
|
|
|
return input->ReadTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAvailable() {
|
|
|
|
return input->IsAvailable();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Read(void *ptr, size_t size, Error &error);
|
|
|
|
|
|
|
|
bool IsEOF() {
|
|
|
|
return !ReadingFromBuffer() && input->IsEOF();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Seek(InputPlugin::offset_type offset, int whence, Error &error);
|
|
|
|
|
|
|
|
private:
|
2013-01-21 17:36:19 +01:00
|
|
|
/**
|
|
|
|
* Are we currently reading from the buffer, and does the
|
|
|
|
* buffer contain more data for the next read operation?
|
|
|
|
*/
|
|
|
|
bool ReadingFromBuffer() const {
|
|
|
|
return tail > 0 && base.offset < input->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy public attributes from the underlying input stream to the
|
|
|
|
* "rewind" input stream. This function is called when a method of
|
|
|
|
* the underlying stream has returned, which may have modified these
|
|
|
|
* attributes.
|
|
|
|
*/
|
|
|
|
void CopyAttributes() {
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *dest = &base;
|
|
|
|
const InputStream *src = input;
|
2013-01-21 17:36:19 +01:00
|
|
|
|
|
|
|
assert(dest != src);
|
|
|
|
|
2014-05-11 15:34:48 +02:00
|
|
|
if (!dest->IsReady() && src->IsReady()) {
|
|
|
|
if (src->HasMimeType())
|
|
|
|
dest->SetMimeType(src->GetMimeType());
|
2011-09-15 20:24:15 +02:00
|
|
|
|
2014-05-11 15:34:48 +02:00
|
|
|
dest->size = src->GetSize();
|
|
|
|
dest->seekable = src->IsSeekable();
|
|
|
|
dest->SetReady();
|
|
|
|
}
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 15:34:48 +02:00
|
|
|
dest->offset = src->offset;
|
2010-09-22 22:20:50 +02:00
|
|
|
}
|
2013-01-21 17:36:19 +01:00
|
|
|
};
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
input_rewind_close(InputStream *is)
|
2009-12-29 23:55:40 +01:00
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
delete r;
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 21:06:12 +02:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
input_rewind_check(InputStream *is, Error &error)
|
2011-09-16 21:06:12 +02:00
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2011-09-16 21:06:12 +02:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
return r->Check(error);
|
2011-09-16 21:06:12 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 22:25:29 +02:00
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
input_rewind_update(InputStream *is)
|
2011-09-14 22:25:29 +02:00
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2011-09-14 22:25:29 +02:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
r->Update();
|
2011-09-14 22:25:29 +02:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
static Tag *
|
2013-10-23 22:08:59 +02:00
|
|
|
input_rewind_tag(InputStream *is)
|
2009-12-29 23:55:40 +01:00
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
return r->ReadTag();
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
input_rewind_available(InputStream *is)
|
2009-12-29 23:55:40 +01:00
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
return r->IsAvailable();
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
inline size_t
|
|
|
|
RewindInputStream::Read(void *ptr, size_t size, Error &error)
|
2009-12-29 23:55:40 +01:00
|
|
|
{
|
2014-05-11 16:03:33 +02:00
|
|
|
if (ReadingFromBuffer()) {
|
2009-12-29 23:55:40 +01:00
|
|
|
/* buffered read */
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
assert(head == (size_t)base.offset);
|
|
|
|
assert(tail == (size_t)input->offset);
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
if (size > tail - head)
|
|
|
|
size = tail - head;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
memcpy(ptr, buffer + head, size);
|
|
|
|
head += size;
|
|
|
|
base.offset += size;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
return size;
|
|
|
|
} else {
|
|
|
|
/* pass method call to underlying stream */
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
size_t nbytes = input->Read(ptr, size, error);
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
if (input->offset > (InputPlugin::offset_type)sizeof(buffer))
|
2009-12-29 23:55:40 +01:00
|
|
|
/* disable buffering */
|
2014-05-11 16:03:33 +02:00
|
|
|
tail = 0;
|
|
|
|
else if (tail == (size_t)base.offset) {
|
2009-12-29 23:55:40 +01:00
|
|
|
/* append to buffer */
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
memcpy(buffer + tail, ptr, nbytes);
|
|
|
|
tail += nbytes;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
assert(tail == (size_t)input->offset);
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
CopyAttributes();
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
static size_t
|
|
|
|
input_rewind_read(InputStream *is, void *ptr, size_t size,
|
|
|
|
Error &error)
|
2009-12-29 23:55:40 +01:00
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
return r->Read(ptr, size, error);
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-05-11 16:03:33 +02:00
|
|
|
input_rewind_eof(InputStream *is)
|
2009-12-29 23:55:40 +01:00
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
return r->IsEOF();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
RewindInputStream::Seek(InputPlugin::offset_type offset, int whence,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
assert(base.IsReady());
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
if (whence == SEEK_SET && tail > 0 &&
|
|
|
|
offset <= (InputPlugin::offset_type)tail) {
|
2009-12-29 23:55:40 +01:00
|
|
|
/* buffered seek */
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
assert(!ReadingFromBuffer() ||
|
|
|
|
head == (size_t)base.offset);
|
|
|
|
assert(tail == (size_t)input->offset);
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
head = (size_t)offset;
|
|
|
|
base.offset = offset;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
2014-05-11 16:03:33 +02:00
|
|
|
bool success = input->Seek(offset, whence, error);
|
|
|
|
CopyAttributes();
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
/* disable the buffer, because input has left the
|
2009-12-29 23:55:40 +01:00
|
|
|
buffered range now */
|
2014-05-11 16:03:33 +02:00
|
|
|
tail = 0;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 16:03:33 +02:00
|
|
|
static bool
|
|
|
|
input_rewind_seek(InputStream *is, InputPlugin::offset_type offset,
|
|
|
|
int whence,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
|
|
|
|
|
|
|
return r->Seek(offset, whence, error);
|
|
|
|
}
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const InputPlugin rewind_input_plugin = {
|
2013-01-21 17:36:19 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
input_rewind_close,
|
|
|
|
input_rewind_check,
|
|
|
|
input_rewind_update,
|
|
|
|
input_rewind_tag,
|
|
|
|
input_rewind_available,
|
|
|
|
input_rewind_read,
|
|
|
|
input_rewind_eof,
|
|
|
|
input_rewind_seek,
|
2009-12-29 23:55:40 +01:00
|
|
|
};
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *
|
|
|
|
input_rewind_open(InputStream *is)
|
2009-12-29 23:55:40 +01:00
|
|
|
{
|
2013-10-28 23:58:17 +01:00
|
|
|
assert(is != nullptr);
|
2009-12-29 23:55:40 +01:00
|
|
|
assert(is->offset == 0);
|
|
|
|
|
2014-05-11 15:34:48 +02:00
|
|
|
if (is->IsReady() && is->IsSeekable())
|
2010-01-04 21:42:11 +01:00
|
|
|
/* seekable resources don't need this plugin */
|
2009-12-30 23:27:37 +01:00
|
|
|
return is;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *c = new RewindInputStream(is);
|
2014-05-11 16:03:33 +02:00
|
|
|
return c->GetBase();
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|