DecoderBuffer.cxx 1.57 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "DecoderBuffer.hxx"
22
#include "DecoderAPI.hxx"
23 24

bool
25
DecoderBuffer::Fill()
26
{
27
	auto w = buffer.Write();
28
	if (w.IsEmpty())
29 30 31
		/* buffer is full */
		return false;

32
	size_t nbytes = decoder_read(decoder, is,
33
				     w.data, w.size);
34 35 36 37 38
	if (nbytes == 0)
		/* end of file, I/O error or decoder command
		   received */
		return false;

39
	buffer.Append(nbytes);
40 41 42
	return true;
}

43
ConstBuffer<void>
44
DecoderBuffer::Need(size_t min_size)
45 46
{
	while (true) {
47
		const auto r = Read();
48 49
		if (r.size >= min_size)
			return r;
50

51
		if (!Fill())
52 53 54 55
			return nullptr;
	}
}

56
bool
57
DecoderBuffer::Skip(size_t nbytes)
58
{
59
	const auto r = buffer.Read();
60
	if (r.size >= nbytes) {
61
		buffer.Consume(nbytes);
62 63
		return true;
	}
64

65
	buffer.Clear();
66
	nbytes -= r.size;
67

68
	return decoder_skip(decoder, is, nbytes);
69
}