2
* Copyright (C) 2003-2013 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
#include "RewindInputPlugin.hxx"
22
#include "InputStream.hxx"
23
#include "InputPlugin.hxx"
24
#include "tag/Tag.hxx"
30
extern const InputPlugin rewind_input_plugin;
32
struct RewindInputStream {
38
* The read position within the buffer. Undefined as long as
39
* ReadingFromBuffer() returns false.
44
* The write/append position within the buffer.
49
* The size of this buffer is the maximum number of bytes
50
* which can be rewinded cheaply without passing the "seek"
53
* The origin of this buffer is always the beginning of the
56
char buffer[64 * 1024];
58
RewindInputStream(InputStream *_input)
59
:base(rewind_input_plugin, _input->uri.c_str(),
60
_input->mutex, _input->cond),
61
input(_input), tail(0) {
64
~RewindInputStream() {
69
* Are we currently reading from the buffer, and does the
70
* buffer contain more data for the next read operation?
72
bool ReadingFromBuffer() const {
73
return tail > 0 && base.offset < input->offset;
77
* Copy public attributes from the underlying input stream to the
78
* "rewind" input stream. This function is called when a method of
79
* the underlying stream has returned, which may have modified these
82
void CopyAttributes() {
83
InputStream *dest = &base;
84
const InputStream *src = input;
88
bool dest_ready = dest->ready;
90
dest->ready = src->ready;
91
dest->seekable = src->seekable;
92
dest->size = src->size;
93
dest->offset = src->offset;
95
if (!dest_ready && src->ready)
96
dest->mime = src->mime;
101
input_rewind_close(InputStream *is)
103
RewindInputStream *r = (RewindInputStream *)is;
109
input_rewind_check(InputStream *is, Error &error)
111
RewindInputStream *r = (RewindInputStream *)is;
113
return r->input->Check(error);
117
input_rewind_update(InputStream *is)
119
RewindInputStream *r = (RewindInputStream *)is;
121
if (!r->ReadingFromBuffer())
126
input_rewind_tag(InputStream *is)
128
RewindInputStream *r = (RewindInputStream *)is;
130
return r->input->ReadTag();
134
input_rewind_available(InputStream *is)
136
RewindInputStream *r = (RewindInputStream *)is;
138
return r->input->IsAvailable();
142
input_rewind_read(InputStream *is, void *ptr, size_t size,
145
RewindInputStream *r = (RewindInputStream *)is;
147
if (r->ReadingFromBuffer()) {
150
assert(r->head == (size_t)is->offset);
151
assert(r->tail == (size_t)r->input->offset);
153
if (size > r->tail - r->head)
154
size = r->tail - r->head;
156
memcpy(ptr, r->buffer + r->head, size);
162
/* pass method call to underlying stream */
164
size_t nbytes = r->input->Read(ptr, size, error);
166
if (r->input->offset > (InputPlugin::offset_type)sizeof(r->buffer))
167
/* disable buffering */
169
else if (r->tail == (size_t)is->offset) {
170
/* append to buffer */
172
memcpy(r->buffer + r->tail, ptr, nbytes);
175
assert(r->tail == (size_t)r->input->offset);
185
input_rewind_eof(InputStream *is)
187
RewindInputStream *r = (RewindInputStream *)is;
189
return !r->ReadingFromBuffer() && r->input->IsEOF();
193
input_rewind_seek(InputStream *is, InputPlugin::offset_type offset,
197
RewindInputStream *r = (RewindInputStream *)is;
201
if (whence == SEEK_SET && r->tail > 0 &&
202
offset <= (InputPlugin::offset_type)r->tail) {
205
assert(!r->ReadingFromBuffer() ||
206
r->head == (size_t)is->offset);
207
assert(r->tail == (size_t)r->input->offset);
209
r->head = (size_t)offset;
214
bool success = r->input->Seek(offset, whence, error);
217
/* disable the buffer, because r->input has left the
218
buffered range now */
225
const InputPlugin rewind_input_plugin = {
234
input_rewind_available,
241
input_rewind_open(InputStream *is)
243
assert(is != nullptr);
244
assert(is->offset == 0);
247
/* seekable resources don't need this plugin */
250
RewindInputStream *c = new RewindInputStream(is);