2009-08-26 20:08:13 +02:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-08-26 20:08:13 +02: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.
|
|
|
|
*
|
|
|
|
* 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-10-11 23:32:22 +02:00
|
|
|
#include "config.h" /* must be first for large file support */
|
2013-07-28 12:45:48 +02:00
|
|
|
#include "Mpg123DecoderPlugin.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2014-02-07 18:52:19 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
2009-08-26 20:08:13 +02:00
|
|
|
|
|
|
|
#include <mpg123.h>
|
2013-11-28 11:50:54 +01:00
|
|
|
|
2011-01-21 14:25:52 +01:00
|
|
|
#include <stdio.h>
|
2009-08-26 20:08:13 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain mpg123_domain("mpg123");
|
2009-08-26 20:08:13 +02:00
|
|
|
|
|
|
|
static bool
|
2013-08-04 11:30:26 +02:00
|
|
|
mpd_mpg123_init(gcc_unused const config_param ¶m)
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
mpg123_init();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_mpg123_finish(void)
|
|
|
|
{
|
|
|
|
mpg123_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a file with an existing #mpg123_handle.
|
|
|
|
*
|
|
|
|
* @param handle a handle which was created before; on error, this
|
|
|
|
* function will not free it
|
|
|
|
* @param audio_format this parameter is filled after successful
|
|
|
|
* return
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
mpd_mpg123_open(mpg123_handle *handle, const char *path_fs,
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat &audio_format)
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
/* mpg123_open() wants a writable string :-( */
|
2013-10-19 17:46:00 +02:00
|
|
|
char *const path2 = const_cast<char *>(path_fs);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
int error = mpg123_open(handle, path2);
|
2009-08-26 20:08:13 +02:00
|
|
|
if (error != MPG123_OK) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"libmpg123 failed to open %s: %s",
|
|
|
|
path_fs, mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* obtain the audio format */
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
long rate;
|
|
|
|
int channels, encoding;
|
2009-08-26 20:08:13 +02:00
|
|
|
error = mpg123_getformat(handle, &rate, &channels, &encoding);
|
|
|
|
if (error != MPG123_OK) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"mpg123_getformat() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoding != MPG123_ENC_SIGNED_16) {
|
|
|
|
/* other formats not yet implemented */
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"expected MPG123_ENC_SIGNED_16, got %d",
|
|
|
|
encoding);
|
2009-08-26 20:08:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error2;
|
2013-08-03 21:00:50 +02:00
|
|
|
if (!audio_format_init_checked(audio_format, rate, SampleFormat::S16,
|
2013-08-10 18:02:44 +02:00
|
|
|
channels, error2)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error2);
|
2009-08-26 20:08:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-02-07 18:52:19 +01:00
|
|
|
mpd_mpg123_file_decode(Decoder &decoder, Path path_fs)
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
/* open the file */
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
int error;
|
|
|
|
mpg123_handle *const handle = mpg123_new(nullptr, &error);
|
2013-07-28 12:45:48 +02:00
|
|
|
if (handle == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(mpg123_domain,
|
|
|
|
"mpg123_new() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2014-02-07 18:52:19 +01:00
|
|
|
if (!mpd_mpg123_open(handle, path_fs.c_str(), audio_format)) {
|
2009-08-26 20:08:13 +02:00
|
|
|
mpg123_delete(handle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
const off_t num_samples = mpg123_length(handle);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
|
|
|
/* tell MPD core we're ready */
|
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
const auto duration =
|
|
|
|
SongTime::FromScale<uint64_t>(num_samples,
|
|
|
|
audio_format.sample_rate);
|
|
|
|
|
|
|
|
decoder_initialized(decoder, audio_format, true, duration);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
struct mpg123_frameinfo info;
|
2011-01-21 14:25:46 +01:00
|
|
|
if (mpg123_info(handle, &info) != MPG123_OK) {
|
|
|
|
info.vbr = MPG123_CBR;
|
|
|
|
info.bitrate = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (info.vbr) {
|
|
|
|
case MPG123_ABR:
|
|
|
|
info.bitrate = info.abr_rate;
|
|
|
|
break;
|
|
|
|
case MPG123_CBR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
info.bitrate = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-26 20:08:13 +02:00
|
|
|
/* the decoder main loop */
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2009-08-26 20:08:13 +02:00
|
|
|
do {
|
|
|
|
/* decode */
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
unsigned char buffer[8192];
|
|
|
|
size_t nbytes;
|
2009-08-26 20:08:13 +02:00
|
|
|
error = mpg123_read(handle, buffer, sizeof(buffer), &nbytes);
|
|
|
|
if (error != MPG123_OK) {
|
|
|
|
if (error != MPG123_DONE)
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"mpg123_read() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-21 14:25:46 +01:00
|
|
|
/* update bitrate for ABR/VBR */
|
|
|
|
if (info.vbr != MPG123_CBR) {
|
|
|
|
/* FIXME: maybe skip, as too expensive? */
|
|
|
|
/* FIXME: maybe, (info.vbr == MPG123_VBR) ? */
|
|
|
|
if (mpg123_info (handle, &info) != MPG123_OK)
|
|
|
|
info.bitrate = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-26 20:08:13 +02:00
|
|
|
/* send to MPD */
|
|
|
|
|
2013-07-28 12:45:48 +02:00
|
|
|
cmd = decoder_data(decoder, nullptr, buffer, nbytes, info.bitrate);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2014-08-26 11:31:57 +02:00
|
|
|
off_t c = decoder_seek_where_frame(decoder);
|
2011-01-21 14:25:52 +01:00
|
|
|
c = mpg123_seek(handle, c, SEEK_SET);
|
|
|
|
if (c < 0)
|
|
|
|
decoder_seek_error(decoder);
|
|
|
|
else {
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
decoder_timestamp(decoder, c/(double)audio_format.sample_rate);
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
cmd = DecoderCommand::NONE;
|
2011-01-21 14:25:52 +01:00
|
|
|
}
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd == DecoderCommand::NONE);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
|
|
|
|
mpg123_delete(handle);
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2014-02-07 18:52:19 +01:00
|
|
|
mpd_mpg123_scan_file(Path path_fs,
|
2012-02-11 19:12:02 +01:00
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
int error;
|
2014-09-24 19:13:20 +02:00
|
|
|
mpg123_handle *const handle = mpg123_new(nullptr, &error);
|
2013-07-28 12:45:48 +02:00
|
|
|
if (handle == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(mpg123_domain,
|
|
|
|
"mpg123_new() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2014-02-07 18:52:19 +01:00
|
|
|
if (!mpd_mpg123_open(handle, path_fs.c_str(), audio_format)) {
|
2009-08-26 20:08:13 +02:00
|
|
|
mpg123_delete(handle);
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
const off_t num_samples = mpg123_length(handle);
|
2009-08-26 20:08:13 +02:00
|
|
|
if (num_samples <= 0) {
|
|
|
|
mpg123_delete(handle);
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ID3 tag support not yet implemented */
|
|
|
|
|
|
|
|
mpg123_delete(handle);
|
2012-02-11 19:12:02 +01:00
|
|
|
|
2014-08-29 22:43:36 +02:00
|
|
|
const auto duration =
|
|
|
|
SongTime::FromScale<uint64_t>(num_samples,
|
|
|
|
audio_format.sample_rate);
|
|
|
|
|
|
|
|
tag_handler_invoke_duration(handler, handler_ctx, duration);
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const mpg123_suffixes[] = {
|
|
|
|
"mp3",
|
2013-07-28 12:45:48 +02:00
|
|
|
nullptr
|
2009-08-26 20:08:13 +02:00
|
|
|
};
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin mpg123_decoder_plugin = {
|
2013-07-28 12:45:48 +02:00
|
|
|
"mpg123",
|
|
|
|
mpd_mpg123_init,
|
|
|
|
mpd_mpg123_finish,
|
2009-08-26 20:08:13 +02:00
|
|
|
/* streaming not yet implemented */
|
2013-07-28 12:45:48 +02:00
|
|
|
nullptr,
|
|
|
|
mpd_mpg123_file_decode,
|
|
|
|
mpd_mpg123_scan_file,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
mpg123_suffixes,
|
|
|
|
nullptr,
|
2009-08-26 20:08:13 +02:00
|
|
|
};
|