2009-02-17 22:39:45 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2009-02-17 22:39:45 +01:00
|
|
|
* 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.
|
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.
|
2009-02-17 22:39:45 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-04-17 22:45:10 +02:00
|
|
|
#include "DecoderBuffer.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2009-02-17 22:39:45 +01:00
|
|
|
|
|
|
|
bool
|
2014-09-22 08:26:04 +02:00
|
|
|
DecoderBuffer::Fill()
|
2009-02-17 22:39:45 +01:00
|
|
|
{
|
2014-09-22 08:26:04 +02:00
|
|
|
auto w = buffer.Write();
|
2014-09-21 13:39:26 +02:00
|
|
|
if (w.IsEmpty())
|
2009-02-17 22:39:45 +01:00
|
|
|
/* buffer is full */
|
|
|
|
return false;
|
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
size_t nbytes = decoder_read(decoder, is,
|
2014-09-21 13:39:26 +02:00
|
|
|
w.data, w.size);
|
2009-02-17 22:39:45 +01:00
|
|
|
if (nbytes == 0)
|
|
|
|
/* end of file, I/O error or decoder command
|
|
|
|
received */
|
|
|
|
return false;
|
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Append(nbytes);
|
2009-02-17 22:39:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-12 01:33:12 +02:00
|
|
|
ConstBuffer<void>
|
2014-09-22 08:26:04 +02:00
|
|
|
DecoderBuffer::Need(size_t min_size)
|
2014-07-12 01:33:12 +02:00
|
|
|
{
|
|
|
|
while (true) {
|
2014-09-22 08:26:04 +02:00
|
|
|
const auto r = Read();
|
2014-09-21 13:39:26 +02:00
|
|
|
if (r.size >= min_size)
|
|
|
|
return r;
|
2014-07-12 01:33:12 +02:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
if (!Fill())
|
2014-07-12 01:33:12 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-30 15:22:36 +02:00
|
|
|
bool
|
2014-09-22 08:26:04 +02:00
|
|
|
DecoderBuffer::Skip(size_t nbytes)
|
2009-09-30 15:22:36 +02:00
|
|
|
{
|
2014-09-22 08:26:04 +02:00
|
|
|
const auto r = buffer.Read();
|
2014-09-21 13:39:26 +02:00
|
|
|
if (r.size >= nbytes) {
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Consume(nbytes);
|
2014-07-12 02:26:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-30 15:22:36 +02:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Clear();
|
2014-09-21 13:39:26 +02:00
|
|
|
nbytes -= r.size;
|
2009-09-30 15:22:36 +02:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
return decoder_skip(decoder, is, nbytes);
|
2009-09-30 15:22:36 +02:00
|
|
|
}
|