2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicPipe.hxx"
|
|
|
|
#include "MusicChunk.hxx"
|
2009-03-08 13:45:24 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2019-07-05 09:59:00 +02:00
|
|
|
|
2009-03-08 13:45:24 +01:00
|
|
|
#ifndef NDEBUG
|
2009-03-09 19:15:54 +01:00
|
|
|
|
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
MusicPipe::Contains(const MusicChunk *chunk) const noexcept
|
2009-03-09 19:15:54 +01:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2009-03-09 19:15:54 +01:00
|
|
|
|
2018-06-23 17:58:42 +02:00
|
|
|
for (const MusicChunk *i = head.get(); i != nullptr; i = i->next.get())
|
2013-01-04 20:50:26 +01:00
|
|
|
if (i == chunk)
|
2009-03-09 19:15:54 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-08 13:45:24 +01:00
|
|
|
#endif
|
|
|
|
|
2017-12-30 18:00:40 +01:00
|
|
|
MusicChunkPtr
|
2017-06-03 21:33:44 +02:00
|
|
|
MusicPipe::Shift() noexcept
|
2008-04-12 06:11:23 +02:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2018-06-23 17:58:42 +02:00
|
|
|
auto chunk = std::move(head);
|
2013-09-26 21:51:45 +02:00
|
|
|
if (chunk != nullptr) {
|
2013-01-04 21:38:46 +01:00
|
|
|
assert(!chunk->IsEmpty());
|
2009-03-07 21:41:25 +01:00
|
|
|
|
2018-06-23 17:58:42 +02:00
|
|
|
head = std::move(chunk->next);
|
2013-09-26 21:51:45 +02:00
|
|
|
--size;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2013-09-26 21:51:45 +02:00
|
|
|
if (head == nullptr) {
|
|
|
|
assert(size == 0);
|
|
|
|
assert(tail_r == &chunk->next);
|
2009-01-17 13:09:29 +01:00
|
|
|
|
2013-09-26 21:51:45 +02:00
|
|
|
tail_r = &head;
|
2009-03-06 00:42:03 +01:00
|
|
|
} else {
|
2013-09-26 21:51:45 +02:00
|
|
|
assert(size > 0);
|
|
|
|
assert(tail_r != &chunk->next);
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
2009-03-07 21:40:13 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2013-09-26 21:51:45 +02:00
|
|
|
if (size == 0)
|
|
|
|
audio_format.Clear();
|
2009-03-07 21:40:13 +01:00
|
|
|
#endif
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
2009-01-17 13:09:29 +01:00
|
|
|
|
2018-06-23 17:58:42 +02:00
|
|
|
return chunk;
|
2008-11-02 16:56:49 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
void
|
2017-12-30 17:24:58 +01:00
|
|
|
MusicPipe::Clear() noexcept
|
2008-04-12 06:18:19 +02:00
|
|
|
{
|
2017-12-30 18:00:40 +01:00
|
|
|
while (Shift()) {}
|
2008-04-12 06:18:19 +02:00
|
|
|
}
|
2008-11-13 02:06:58 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
void
|
2017-12-30 18:00:40 +01:00
|
|
|
MusicPipe::Push(MusicChunkPtr chunk) noexcept
|
2008-11-13 02:09:33 +01:00
|
|
|
{
|
2013-01-04 21:38:46 +01:00
|
|
|
assert(!chunk->IsEmpty());
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(chunk->length == 0 || chunk->audio_format.IsValid());
|
2009-03-07 21:41:25 +01:00
|
|
|
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2008-11-13 02:09:33 +01:00
|
|
|
|
2013-09-26 21:51:45 +02:00
|
|
|
assert(size > 0 || !audio_format.IsDefined());
|
|
|
|
assert(!audio_format.IsDefined() ||
|
|
|
|
chunk->CheckFormat(audio_format));
|
2009-03-08 13:45:24 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2013-09-26 21:51:45 +02:00
|
|
|
if (!audio_format.IsDefined() && chunk->length > 0)
|
|
|
|
audio_format = chunk->audio_format;
|
2009-03-08 13:45:24 +01:00
|
|
|
#endif
|
|
|
|
|
2018-06-23 17:58:42 +02:00
|
|
|
chunk->next.reset();
|
|
|
|
*tail_r = std::move(chunk);
|
|
|
|
tail_r = &(*tail_r)->next;
|
2008-11-13 02:09:33 +01:00
|
|
|
|
2013-09-26 21:51:45 +02:00
|
|
|
++size;
|
2008-11-13 02:06:58 +01:00
|
|
|
}
|