2009-12-29 23:55:40 +01:00
|
|
|
/*
|
2013-01-21 17:36:19 +01:00
|
|
|
* Copyright (C) 2003-2013 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"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputInternal.hxx"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputPlugin.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.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
|
|
|
|
|
|
|
struct RewindInputStream {
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream base;
|
|
|
|
|
|
|
|
struct input_stream *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];
|
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream(input_stream *_input)
|
2013-01-28 23:41:45 +01:00
|
|
|
:base(rewind_input_plugin, _input->uri.c_str(),
|
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
|
|
|
|
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() {
|
|
|
|
struct input_stream *dest = &base;
|
|
|
|
const struct input_stream *src = input;
|
|
|
|
|
|
|
|
assert(dest != src);
|
|
|
|
|
|
|
|
bool dest_ready = dest->ready;
|
2011-09-15 20:24:15 +02:00
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
dest->ready = src->ready;
|
|
|
|
dest->seekable = src->seekable;
|
|
|
|
dest->size = src->size;
|
|
|
|
dest->offset = src->offset;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2013-01-28 23:41:45 +01:00
|
|
|
if (!dest_ready && src->ready)
|
|
|
|
dest->mime = src->mime;
|
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
|
|
|
|
input_rewind_close(struct input_stream *is)
|
|
|
|
{
|
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-08-10 18:02:44 +02:00
|
|
|
input_rewind_check(struct input_stream *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
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return r->input->Check(error);
|
2011-09-16 21:06:12 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 22:25:29 +02:00
|
|
|
static void
|
|
|
|
input_rewind_update(struct input_stream *is)
|
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2011-09-14 22:25:29 +02:00
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
if (!r->ReadingFromBuffer())
|
|
|
|
r->CopyAttributes();
|
2011-09-14 22:25:29 +02:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
static Tag *
|
2009-12-29 23:55:40 +01:00
|
|
|
input_rewind_tag(struct input_stream *is)
|
|
|
|
{
|
2013-01-21 17:36:19 +01:00
|
|
|
RewindInputStream *r = (RewindInputStream *)is;
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return r->input->ReadTag();
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
static bool
|
|
|
|
input_rewind_available(struct input_stream *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-09-05 00:06:31 +02:00
|
|
|
return r->input->IsAvailable();
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2009-12-30 23:24:11 +01:00
|
|
|
input_rewind_read(struct input_stream *is, void *ptr, size_t size,
|
2013-08-10 18:02:44 +02:00
|
|
|
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
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
if (r->ReadingFromBuffer()) {
|
2009-12-29 23:55:40 +01:00
|
|
|
/* buffered read */
|
|
|
|
|
|
|
|
assert(r->head == (size_t)is->offset);
|
2009-12-30 23:27:37 +01:00
|
|
|
assert(r->tail == (size_t)r->input->offset);
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
if (size > r->tail - r->head)
|
|
|
|
size = r->tail - r->head;
|
|
|
|
|
|
|
|
memcpy(ptr, r->buffer + r->head, size);
|
|
|
|
r->head += size;
|
|
|
|
is->offset += size;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
} else {
|
|
|
|
/* pass method call to underlying stream */
|
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
size_t nbytes = r->input->Read(ptr, size, error);
|
2009-12-29 23:55:40 +01:00
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
if (r->input->offset > (goffset)sizeof(r->buffer))
|
2009-12-29 23:55:40 +01:00
|
|
|
/* disable buffering */
|
|
|
|
r->tail = 0;
|
|
|
|
else if (r->tail == (size_t)is->offset) {
|
|
|
|
/* append to buffer */
|
|
|
|
|
|
|
|
memcpy(r->buffer + r->tail, ptr, nbytes);
|
|
|
|
r->tail += nbytes;
|
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
assert(r->tail == (size_t)r->input->offset);
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
r->CopyAttributes();
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2009-12-30 23:27:37 +01:00
|
|
|
input_rewind_eof(struct input_stream *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-09-05 00:06:31 +02:00
|
|
|
return !r->ReadingFromBuffer() && r->input->IsEOF();
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2009-12-30 23:24:11 +01:00
|
|
|
input_rewind_seek(struct input_stream *is, goffset offset, int whence,
|
2013-08-10 18:02:44 +02:00
|
|
|
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
|
|
|
|
|
|
|
assert(is->ready);
|
|
|
|
|
2009-12-30 23:24:11 +01:00
|
|
|
if (whence == SEEK_SET && r->tail > 0 && offset <= (goffset)r->tail) {
|
2009-12-29 23:55:40 +01:00
|
|
|
/* buffered seek */
|
|
|
|
|
2013-01-21 17:36:19 +01:00
|
|
|
assert(!r->ReadingFromBuffer() ||
|
2009-12-29 23:55:40 +01:00
|
|
|
r->head == (size_t)is->offset);
|
2009-12-30 23:27:37 +01:00
|
|
|
assert(r->tail == (size_t)r->input->offset);
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
r->head = (size_t)offset;
|
|
|
|
is->offset = offset;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
2013-09-05 00:06:31 +02:00
|
|
|
bool success = r->input->Seek(offset, whence, error);
|
2013-01-21 17:36:19 +01:00
|
|
|
r->CopyAttributes();
|
2009-12-29 23:55:40 +01:00
|
|
|
|
|
|
|
/* disable the buffer, because r->input has left the
|
|
|
|
buffered range now */
|
|
|
|
r->tail = 0;
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream *
|
2009-12-29 23:55:40 +01:00
|
|
|
input_rewind_open(struct input_stream *is)
|
|
|
|
{
|
|
|
|
assert(is != NULL);
|
|
|
|
assert(is->offset == 0);
|
|
|
|
|
2010-01-04 21:42:11 +01:00
|
|
|
if (is->seekable)
|
|
|
|
/* 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);
|
2009-12-30 23:27:37 +01:00
|
|
|
return &c->base;
|
2009-12-29 23:55:40 +01:00
|
|
|
}
|