decoder/mpg123: move code to Decode()

This commit is contained in:
Max Kellermann
2024-04-03 22:45:41 +02:00
parent a20a83eb76
commit a53db82ae4

View File

@@ -175,27 +175,13 @@ mpd_mpg123_meta(DecoderClient &client, mpg123_handle *const handle)
} }
static void static void
mpd_mpg123_file_decode(DecoderClient &client, Path path_fs) Decode(DecoderClient &client, mpg123_handle &handle)
{ {
/* open the file */
int error;
mpg123_handle *const handle = mpg123_new(nullptr, &error);
if (handle == nullptr) {
FmtError(mpg123_domain,
"mpg123_new() failed: {}",
mpg123_plain_strerror(error));
return;
}
AtScopeExit(handle) { mpg123_delete(handle); };
AudioFormat audio_format; AudioFormat audio_format;
if (!mpd_mpg123_open(handle, path_fs) || if (!GetAudioFormat(handle, audio_format))
!GetAudioFormat(*handle, audio_format))
return; return;
const off_t num_samples = mpg123_length(handle); const off_t num_samples = mpg123_length(&handle);
/* tell MPD core we're ready */ /* tell MPD core we're ready */
@@ -206,7 +192,7 @@ mpd_mpg123_file_decode(DecoderClient &client, Path path_fs)
client.Ready(audio_format, true, duration); client.Ready(audio_format, true, duration);
struct mpg123_frameinfo info; struct mpg123_frameinfo info;
if (mpg123_info(handle, &info) != MPG123_OK) { if (mpg123_info(&handle, &info) != MPG123_OK) {
info.vbr = MPG123_CBR; info.vbr = MPG123_CBR;
info.bitrate = 0; info.bitrate = 0;
} }
@@ -225,14 +211,14 @@ mpd_mpg123_file_decode(DecoderClient &client, Path path_fs)
DecoderCommand cmd; DecoderCommand cmd;
do { do {
/* read metadata */ /* read metadata */
mpd_mpg123_meta(client, handle); mpd_mpg123_meta(client, &handle);
/* decode */ /* decode */
unsigned char buffer[8192]; unsigned char buffer[8192];
size_t nbytes; size_t nbytes;
error = mpg123_read(handle, buffer, sizeof(buffer), &nbytes); if (int error = mpg123_read(&handle, buffer, sizeof(buffer), &nbytes);
if (error != MPG123_OK) { error != MPG123_OK) {
if (error != MPG123_DONE) if (error != MPG123_DONE)
FmtWarning(mpg123_domain, FmtWarning(mpg123_domain,
"mpg123_read() failed: {}", "mpg123_read() failed: {}",
@@ -244,7 +230,7 @@ mpd_mpg123_file_decode(DecoderClient &client, Path path_fs)
if (info.vbr != MPG123_CBR) { if (info.vbr != MPG123_CBR) {
/* FIXME: maybe skip, as too expensive? */ /* FIXME: maybe skip, as too expensive? */
/* FIXME: maybe, (info.vbr == MPG123_VBR) ? */ /* FIXME: maybe, (info.vbr == MPG123_VBR) ? */
if (mpg123_info (handle, &info) != MPG123_OK) if (mpg123_info(&handle, &info) != MPG123_OK)
info.bitrate = 0; info.bitrate = 0;
} }
@@ -255,7 +241,7 @@ mpd_mpg123_file_decode(DecoderClient &client, Path path_fs)
if (cmd == DecoderCommand::SEEK) { if (cmd == DecoderCommand::SEEK) {
off_t c = client.GetSeekFrame(); off_t c = client.GetSeekFrame();
c = mpg123_seek(handle, c, SEEK_SET); c = mpg123_seek(&handle, c, SEEK_SET);
if (c < 0) if (c < 0)
client.SeekError(); client.SeekError();
else { else {
@@ -268,6 +254,28 @@ mpd_mpg123_file_decode(DecoderClient &client, Path path_fs)
} while (cmd == DecoderCommand::NONE); } while (cmd == DecoderCommand::NONE);
} }
static void
mpd_mpg123_file_decode(DecoderClient &client, Path path_fs)
{
/* open the file */
int error;
mpg123_handle *const handle = mpg123_new(nullptr, &error);
if (handle == nullptr) {
FmtError(mpg123_domain,
"mpg123_new() failed: {}",
mpg123_plain_strerror(error));
return;
}
AtScopeExit(handle) { mpg123_delete(handle); };
if (!mpd_mpg123_open(handle, path_fs))
return;
Decode(client, *handle);
}
static bool static bool
mpd_mpg123_scan_file(Path path_fs, TagHandler &handler) noexcept mpd_mpg123_scan_file(Path path_fs, TagHandler &handler) noexcept
{ {