DecoderBuffer.cxx 1.55 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "DecoderBuffer.hxx"
21
#include "DecoderAPI.hxx"
22 23

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

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

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

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

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

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

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

67
	return decoder_skip(client, is, nbytes);
68
}