MusicPipe.cxx 2.16 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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 21
#include "MusicPipe.hxx"
#include "MusicChunk.hxx"
22

23 24
#include <assert.h>

25
#ifndef NDEBUG
26 27

bool
28
MusicPipe::Contains(const MusicChunk *chunk) const noexcept
29
{
30
	const std::lock_guard<Mutex> protect(mutex);
31

32
	for (const MusicChunk *i = head.get(); i != nullptr; i = i->next.get())
33
		if (i == chunk)
34 35 36 37 38
			return true;

	return false;
}

39 40
#endif

41
MusicChunkPtr
42
MusicPipe::Shift() noexcept
43
{
44
	const std::lock_guard<Mutex> protect(mutex);
45

46
	auto chunk = std::move(head);
47
	if (chunk != nullptr) {
48
		assert(!chunk->IsEmpty());
49

50
		head = std::move(chunk->next);
51
		--size;
52

53 54 55
		if (head == nullptr) {
			assert(size == 0);
			assert(tail_r == &chunk->next);
56

57
			tail_r = &head;
58
		} else {
59 60
			assert(size > 0);
			assert(tail_r != &chunk->next);
61
		}
62 63

#ifndef NDEBUG
64 65
		if (size == 0)
			audio_format.Clear();
66
#endif
67
	}
68

69
	return chunk;
70 71
}

72
void
73
MusicPipe::Clear() noexcept
74
{
75
	while (Shift()) {}
76
}
77

78
void
79
MusicPipe::Push(MusicChunkPtr chunk) noexcept
80
{
81
	assert(!chunk->IsEmpty());
82
	assert(chunk->length == 0 || chunk->audio_format.IsValid());
83

84
	const std::lock_guard<Mutex> protect(mutex);
85

86 87 88
	assert(size > 0 || !audio_format.IsDefined());
	assert(!audio_format.IsDefined() ||
	       chunk->CheckFormat(audio_format));
89 90

#ifndef NDEBUG
91 92
	if (!audio_format.IsDefined() && chunk->length > 0)
		audio_format = chunk->audio_format;
93 94
#endif

95 96 97
	chunk->next.reset();
	*tail_r = std::move(chunk);
	tail_r = &(*tail_r)->next;
98

99
	++size;
100
}