RewindInputPlugin.cxx 5.39 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
21
#include "RewindInputPlugin.hxx"
22
#include "InputStream.hxx"
23
#include "InputPlugin.hxx"
24 25

#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
26
#include <string.h>
27 28
#include <stdio.h>

29
extern const InputPlugin rewind_input_plugin;
30 31

struct RewindInputStream {
32
	InputStream base;
33

34
	InputStream *input;
35 36 37

	/**
	 * The read position within the buffer.  Undefined as long as
38
	 * ReadingFromBuffer() returns false.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	 */
	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];

57
	RewindInputStream(InputStream *_input)
58
		:base(rewind_input_plugin, _input->uri.c_str(),
59
		      _input->mutex, _input->cond),
60
		 input(_input), tail(0) {
61
	}
62

63
	~RewindInputStream() {
64
		input->Close();
65
	}
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	/**
	 * 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() {
82 83
		InputStream *dest = &base;
		const InputStream *src = input;
84 85 86 87

		assert(dest != src);

		bool dest_ready = dest->ready;
88

89 90 91 92
		dest->ready = src->ready;
		dest->seekable = src->seekable;
		dest->size = src->size;
		dest->offset = src->offset;
93

94 95
		if (!dest_ready && src->ready)
			dest->mime = src->mime;
96
	}
97
};
98 99

static void
100
input_rewind_close(InputStream *is)
101
{
102
	RewindInputStream *r = (RewindInputStream *)is;
103

104
	delete r;
105 106
}

107
static bool
108
input_rewind_check(InputStream *is, Error &error)
109
{
110
	RewindInputStream *r = (RewindInputStream *)is;
111

112
	return r->input->Check(error);
113 114
}

115
static void
116
input_rewind_update(InputStream *is)
117
{
118
	RewindInputStream *r = (RewindInputStream *)is;
119

120 121
	if (!r->ReadingFromBuffer())
		r->CopyAttributes();
122 123
}

Max Kellermann's avatar
Max Kellermann committed
124
static Tag *
125
input_rewind_tag(InputStream *is)
126
{
127
	RewindInputStream *r = (RewindInputStream *)is;
128

129
	return r->input->ReadTag();
130 131
}

132
static bool
133
input_rewind_available(InputStream *is)
134
{
135
	RewindInputStream *r = (RewindInputStream *)is;
136

137
	return r->input->IsAvailable();
138 139 140
}

static size_t
141
input_rewind_read(InputStream *is, void *ptr, size_t size,
142
		  Error &error)
143
{
144
	RewindInputStream *r = (RewindInputStream *)is;
145

146
	if (r->ReadingFromBuffer()) {
147 148 149
		/* buffered read */

		assert(r->head == (size_t)is->offset);
150
		assert(r->tail == (size_t)r->input->offset);
151 152 153 154 155 156 157 158 159 160 161 162

		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 */

163
		size_t nbytes = r->input->Read(ptr, size, error);
164

165
		if (r->input->offset > (InputPlugin::offset_type)sizeof(r->buffer))
166 167 168 169 170 171 172 173
			/* 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;

174
			assert(r->tail == (size_t)r->input->offset);
175 176
		}

177
		r->CopyAttributes();
178 179 180 181 182 183

		return nbytes;
	}
}

static bool
184
input_rewind_eof(InputStream *is)
185
{
186
	RewindInputStream *r = (RewindInputStream *)is;
187

188
	return !r->ReadingFromBuffer() && r->input->IsEOF();
189 190 191
}

static bool
192
input_rewind_seek(InputStream *is, InputPlugin::offset_type offset,
193
		  int whence,
194
		  Error &error)
195
{
196
	RewindInputStream *r = (RewindInputStream *)is;
197 198 199

	assert(is->ready);

200 201
	if (whence == SEEK_SET && r->tail > 0 &&
	    offset <= (InputPlugin::offset_type)r->tail) {
202 203
		/* buffered seek */

204
		assert(!r->ReadingFromBuffer() ||
205
		       r->head == (size_t)is->offset);
206
		assert(r->tail == (size_t)r->input->offset);
207 208 209 210 211 212

		r->head = (size_t)offset;
		is->offset = offset;

		return true;
	} else {
213
		bool success = r->input->Seek(offset, whence, error);
214
		r->CopyAttributes();
215 216 217 218 219 220 221 222 223

		/* disable the buffer, because r->input has left the
		   buffered range now */
		r->tail = 0;

		return success;
	}
}

224
const InputPlugin rewind_input_plugin = {
225 226 227 228 229 230 231 232 233 234 235 236
	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,
237 238
};

239 240
InputStream *
input_rewind_open(InputStream *is)
241
{
242
	assert(is != nullptr);
243 244
	assert(is->offset == 0);

245 246
	if (is->seekable)
		/* seekable resources don't need this plugin */
247
		return is;
248

249
	RewindInputStream *c = new RewindInputStream(is);
250
	return &c->base;
251
}