2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:27:04 +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.
|
2008-08-26 08:27:04 +02:00
|
|
|
*/
|
|
|
|
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2009-01-03 14:51:28 +01:00
|
|
|
#include <assert.h>
|
2014-05-22 11:10:41 +02:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
size_t
|
2016-11-18 07:13:35 +01:00
|
|
|
decoder_read(DecoderClient *client,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream &is,
|
2013-10-21 21:12:37 +02:00
|
|
|
void *buffer, size_t length)
|
2016-11-17 22:21:36 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(buffer != nullptr);
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2016-11-21 22:09:15 +01:00
|
|
|
/* XXX don't allow client==nullptr */
|
2016-11-17 22:21:36 +01:00
|
|
|
if (client != nullptr)
|
|
|
|
return client->Read(is, buffer, length);
|
2016-11-21 22:09:15 +01:00
|
|
|
|
2016-11-17 22:21:36 +01:00
|
|
|
try {
|
|
|
|
return is.LockRead(buffer, length);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2008-11-15 19:27:30 +01:00
|
|
|
return 0;
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
2008-08-26 08:27:14 +02:00
|
|
|
}
|
|
|
|
|
2013-12-14 12:43:06 +01:00
|
|
|
bool
|
2016-11-18 07:13:35 +01:00
|
|
|
decoder_read_full(DecoderClient *client, InputStream &is,
|
2013-12-14 12:43:06 +01:00
|
|
|
void *_buffer, size_t size)
|
|
|
|
{
|
|
|
|
uint8_t *buffer = (uint8_t *)_buffer;
|
|
|
|
|
|
|
|
while (size > 0) {
|
2016-11-18 07:13:35 +01:00
|
|
|
size_t nbytes = decoder_read(client, is, buffer, size);
|
2013-12-14 12:43:06 +01:00
|
|
|
if (nbytes == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer += nbytes;
|
|
|
|
size -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-14 12:21:23 +01:00
|
|
|
bool
|
2016-11-18 07:13:35 +01:00
|
|
|
decoder_skip(DecoderClient *client, InputStream &is, size_t size)
|
2013-12-14 12:21:23 +01:00
|
|
|
{
|
|
|
|
while (size > 0) {
|
|
|
|
char buffer[1024];
|
2016-11-18 07:13:35 +01:00
|
|
|
size_t nbytes = decoder_read(client, is, buffer,
|
2013-12-14 12:21:23 +01:00
|
|
|
std::min(sizeof(buffer), size));
|
|
|
|
if (nbytes == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|