2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-05-07 04:42:49 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
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
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<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
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<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
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<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
|
|
|
}
|