FlacInput.cxx 4.11 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 *
 * 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.
 */

#include "config.h"
21
#include "FlacInput.hxx"
22
#include "FlacDomain.hxx"
23
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "input/InputStream.hxx"
25
#include "Log.hxx"
26
#include "Compiler.h"
27

28 29
#include <stdexcept>

30
FLAC__StreamDecoderReadStatus
31
FlacInput::Read(FLAC__byte buffer[], size_t *bytes)
32 33 34 35 36
{
	size_t r = decoder_read(decoder, input_stream, (void *)buffer, *bytes);
	*bytes = r;

	if (r == 0) {
37
		if (input_stream.LockIsEOF() ||
38
		    (decoder != nullptr &&
39
		     decoder_get_command(*decoder) != DecoderCommand::NONE))
40 41 42 43 44 45 46 47 48
			return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
		else
			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
	}

	return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}

FLAC__StreamDecoderSeekStatus
49
FlacInput::Seek(FLAC__uint64 absolute_byte_offset)
50
{
51
	if (!input_stream.IsSeekable())
52 53
		return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;

54 55 56 57 58
	try {
		input_stream.LockSeek(absolute_byte_offset);
		return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
	} catch (const std::runtime_error &e) {
		LogError(e);
59
		return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
60
	}
61 62 63
}

FLAC__StreamDecoderTellStatus
64
FlacInput::Tell(FLAC__uint64 *absolute_byte_offset)
65
{
66
	if (!input_stream.IsSeekable())
67 68
		return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;

69
	*absolute_byte_offset = (FLAC__uint64)input_stream.GetOffset();
70 71 72 73
	return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}

FLAC__StreamDecoderLengthStatus
74
FlacInput::Length(FLAC__uint64 *stream_length)
75
{
76
	if (!input_stream.KnownSize())
77 78
		return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;

79
	*stream_length = (FLAC__uint64)input_stream.GetSize();
80 81 82 83
	return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}

FLAC__bool
84
FlacInput::Eof()
85 86
{
	return (decoder != nullptr &&
87 88
		decoder_get_command(*decoder) != DecoderCommand::NONE &&
		decoder_get_command(*decoder) != DecoderCommand::SEEK) ||
89
		input_stream.LockIsEOF();
90 91 92
}

void
93
FlacInput::Error(FLAC__StreamDecoderErrorStatus status)
94 95
{
	if (decoder == nullptr ||
96
	    decoder_get_command(*decoder) != DecoderCommand::STOP)
97 98
		LogWarning(flac_domain,
			   FLAC__StreamDecoderErrorStatusString[status]);
99 100 101
}

FLAC__StreamDecoderReadStatus
102
FlacInput::Read(gcc_unused const FLAC__StreamDecoder *flac_decoder,
103 104 105
		FLAC__byte buffer[], size_t *bytes,
		void *client_data)
{
106
	FlacInput *i = (FlacInput *)client_data;
107 108 109 110 111

	return i->Read(buffer, bytes);
}

FLAC__StreamDecoderSeekStatus
112
FlacInput::Seek(gcc_unused const FLAC__StreamDecoder *flac_decoder,
113 114
		FLAC__uint64 absolute_byte_offset, void *client_data)
{
115
	FlacInput *i = (FlacInput *)client_data;
116 117 118 119 120

	return i->Seek(absolute_byte_offset);
}

FLAC__StreamDecoderTellStatus
121
FlacInput::Tell(gcc_unused const FLAC__StreamDecoder *flac_decoder,
122 123
		FLAC__uint64 *absolute_byte_offset, void *client_data)
{
124
	FlacInput *i = (FlacInput *)client_data;
125 126 127 128 129

	return i->Tell(absolute_byte_offset);
}

FLAC__StreamDecoderLengthStatus
130
FlacInput::Length(gcc_unused const FLAC__StreamDecoder *flac_decoder,
131 132
		  FLAC__uint64 *stream_length, void *client_data)
{
133
	FlacInput *i = (FlacInput *)client_data;
134 135 136 137 138

	return i->Length(stream_length);
}

FLAC__bool
139
FlacInput::Eof(gcc_unused const FLAC__StreamDecoder *flac_decoder,
140 141
	       void *client_data)
{
142
	FlacInput *i = (FlacInput *)client_data;
143 144 145 146 147

	return i->Eof();
}

void
148
FlacInput::Error(gcc_unused const FLAC__StreamDecoder *decoder,
149 150
		 FLAC__StreamDecoderErrorStatus status, void *client_data)
{
151
	FlacInput *i = (FlacInput *)client_data;
152 153 154 155

	i->Error(status);
}