PeakBuffer.cxx 2.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "PeakBuffer.hxx"
21
#include "DynamicFifoBuffer.hxx"
22 23 24 25 26 27 28 29

#include <algorithm>

#include <assert.h>
#include <string.h>

PeakBuffer::~PeakBuffer()
{
30 31
	delete normal_buffer;
	delete peak_buffer;
32 33 34 35 36
}

bool
PeakBuffer::IsEmpty() const
{
37 38
	return (normal_buffer == nullptr || normal_buffer->IsEmpty()) &&
		(peak_buffer == nullptr || peak_buffer->IsEmpty());
39 40
}

41
WritableBuffer<void>
42
PeakBuffer::Read() const
43 44
{
	if (normal_buffer != nullptr) {
45
		const auto p = normal_buffer->Read();
46
		if (!p.IsEmpty())
47
			return p.ToVoid();
48 49 50
	}

	if (peak_buffer != nullptr) {
51
		const auto p = peak_buffer->Read();
52
		if (!p.IsEmpty())
53
			return p.ToVoid();
54 55 56 57 58 59 60 61
	}

	return nullptr;
}

void
PeakBuffer::Consume(size_t length)
{
62 63
	if (normal_buffer != nullptr && !normal_buffer->IsEmpty()) {
		normal_buffer->Consume(length);
64 65 66
		return;
	}

67 68 69 70
	if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) {
		peak_buffer->Consume(length);
		if (peak_buffer->IsEmpty()) {
			delete peak_buffer;
71 72 73 74 75 76 77 78
			peak_buffer = nullptr;
		}

		return;
	}
}

static size_t
79
AppendTo(DynamicFifoBuffer<uint8_t> &buffer, const void *data, size_t length)
80 81 82 83 84 85 86
{
	assert(data != nullptr);
	assert(length > 0);

	size_t total = 0;

	do {
87
		const auto p = buffer.Write();
88
		if (p.IsEmpty())
89 90
			break;

91 92 93
		const size_t nbytes = std::min(length, p.size);
		memcpy(p.data, data, nbytes);
		buffer.Append(nbytes);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

		data = (const uint8_t *)data + nbytes;
		length -= nbytes;
		total += nbytes;
	} while (length > 0);

	return total;
}

bool
PeakBuffer::Append(const void *data, size_t length)
{
	if (length == 0)
		return true;

109 110
	if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) {
		size_t nbytes = AppendTo(*peak_buffer, data, length);
111 112 113 114
		return nbytes == length;
	}

	if (normal_buffer == nullptr)
115
		normal_buffer = new DynamicFifoBuffer<uint8_t>(normal_size);
116

117
	size_t nbytes = AppendTo(*normal_buffer, data, length);
118 119 120 121 122 123 124
	if (nbytes > 0) {
		data = (const uint8_t *)data + nbytes;
		length -= nbytes;
		if (length == 0)
			return true;
	}

125 126 127
	if (peak_buffer == nullptr) {
		if (peak_size > 0)
			peak_buffer = new DynamicFifoBuffer<uint8_t>(peak_size);
128 129 130 131
		if (peak_buffer == nullptr)
			return false;
	}

132
	nbytes = AppendTo(*peak_buffer, data, length);
133 134
	return nbytes == length;
}