2009-03-10 15:46:55 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2009-03-10 15:46:55 +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.
|
|
|
|
*
|
2017-08-07 18:14:54 +02:00
|
|
|
* This program is distributed in the hope that it will useful,
|
2009-03-10 15:46:55 +01:00
|
|
|
* 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-03-10 15:46:55 +01:00
|
|
|
*/
|
|
|
|
|
2017-08-07 21:55:29 +02:00
|
|
|
#include "output/Interface.hxx"
|
|
|
|
#include "output/Registry.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "output/OutputPlugin.hxx"
|
2019-02-05 21:56:20 +01:00
|
|
|
#include "ConfigGlue.hxx"
|
2017-02-10 22:14:07 +01:00
|
|
|
#include "event/Thread.hxx"
|
2013-01-29 17:23:35 +01:00
|
|
|
#include "fs/Path.hxx"
|
2020-04-02 16:40:18 +02:00
|
|
|
#include "fs/NarrowPath.hxx"
|
2020-01-18 20:07:09 +01:00
|
|
|
#include "pcm/AudioParser.hxx"
|
|
|
|
#include "pcm/AudioFormat.hxx"
|
2021-03-05 17:37:10 +01:00
|
|
|
#include "util/OptionDef.hxx"
|
|
|
|
#include "util/OptionParser.hxx"
|
2017-01-17 22:04:31 +01:00
|
|
|
#include "util/StringBuffer.hxx"
|
2016-11-09 11:56:01 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
|
|
|
#include "util/ScopeExit.hxx"
|
2021-03-08 16:58:12 +01:00
|
|
|
#include "util/StaticFifoBuffer.hxx"
|
2018-07-17 21:56:43 +02:00
|
|
|
#include "util/PrintException.hxx"
|
2021-03-05 17:37:10 +01:00
|
|
|
#include "LogBackend.hxx"
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2017-08-09 16:58:44 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-08-28 17:29:09 +02:00
|
|
|
#include <stdlib.h>
|
2014-01-07 23:57:39 +01:00
|
|
|
#include <stdio.h>
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2021-03-05 17:33:55 +01:00
|
|
|
struct CommandLine {
|
|
|
|
FromNarrowPath config_path;
|
|
|
|
|
|
|
|
const char *output_name = nullptr;
|
|
|
|
|
|
|
|
AudioFormat audio_format{44100, SampleFormat::S16, 2};
|
2021-03-05 17:37:10 +01:00
|
|
|
|
|
|
|
bool verbose = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Option {
|
|
|
|
OPTION_VERBOSE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr OptionDef option_defs[] = {
|
|
|
|
{"verbose", 'v', false, "Verbose logging"},
|
2021-03-05 17:33:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static CommandLine
|
|
|
|
ParseCommandLine(int argc, char **argv)
|
|
|
|
{
|
|
|
|
CommandLine c;
|
|
|
|
|
2021-03-05 17:37:10 +01:00
|
|
|
OptionParser option_parser(option_defs, argc, argv);
|
|
|
|
while (auto o = option_parser.Next()) {
|
|
|
|
switch (Option(o.index)) {
|
|
|
|
case OPTION_VERBOSE:
|
|
|
|
c.verbose = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto args = option_parser.GetRemaining();
|
|
|
|
if (args.size < 2 || args.size > 3)
|
2021-03-05 17:33:55 +01:00
|
|
|
throw std::runtime_error("Usage: run_output CONFIG NAME [FORMAT] <IN");
|
|
|
|
|
2021-03-05 17:37:10 +01:00
|
|
|
c.config_path = args[0];
|
|
|
|
c.output_name = args[1];
|
2021-03-05 17:33:55 +01:00
|
|
|
|
2021-03-05 17:37:10 +01:00
|
|
|
if (args.size > 2)
|
|
|
|
c.audio_format = ParseAudioFormat(args[2], false);
|
2021-03-05 17:33:55 +01:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
static std::unique_ptr<AudioOutput>
|
2018-07-17 22:12:56 +02:00
|
|
|
LoadAudioOutput(const ConfigData &config, EventLoop &event_loop,
|
|
|
|
const char *name)
|
2009-03-10 15:46:55 +01:00
|
|
|
{
|
2018-07-17 22:12:56 +02:00
|
|
|
const auto *block = config.FindBlock(ConfigBlockOption::AUDIO_OUTPUT,
|
|
|
|
"name", name);
|
2017-08-07 21:55:29 +02:00
|
|
|
if (block == nullptr)
|
|
|
|
throw FormatRuntimeError("No such configured audio output: %s",
|
2016-11-09 11:56:01 +01:00
|
|
|
name);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2017-08-07 21:55:29 +02:00
|
|
|
const char *plugin_name = block->GetBlockValue("type");
|
|
|
|
if (plugin_name == nullptr)
|
|
|
|
throw std::runtime_error("Missing \"type\" configuration");
|
|
|
|
|
|
|
|
const auto *plugin = AudioOutputPlugin_get(plugin_name);
|
|
|
|
if (plugin == nullptr)
|
|
|
|
throw FormatRuntimeError("No such audio output plugin: %s",
|
|
|
|
plugin_name);
|
2021-03-05 17:37:10 +01:00
|
|
|
#include "util/OptionDef.hxx"
|
|
|
|
#include "util/OptionParser.hxx"
|
2017-08-07 21:55:29 +02:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
return std::unique_ptr<AudioOutput>(ao_plugin_init(event_loop, *plugin,
|
|
|
|
*block));
|
2009-03-10 15:46:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
static void
|
2021-03-08 16:36:52 +01:00
|
|
|
RunOutput(AudioOutput &ao, AudioFormat audio_format,
|
|
|
|
FileDescriptor in_fd)
|
2011-08-30 22:17:46 +02:00
|
|
|
{
|
2021-03-08 17:25:57 +01:00
|
|
|
in_fd.SetBinaryMode();
|
|
|
|
|
2011-08-30 22:17:46 +02:00
|
|
|
/* open the audio output */
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
ao.Enable();
|
|
|
|
AtScopeExit(&ao) { ao.Disable(); };
|
2011-12-24 16:42:23 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
ao.Open(audio_format);
|
|
|
|
AtScopeExit(&ao) { ao.Close(); };
|
2011-08-30 22:17:46 +02:00
|
|
|
|
2013-12-24 14:44:08 +01:00
|
|
|
fprintf(stderr, "audio_format=%s\n",
|
2017-01-17 22:04:31 +01:00
|
|
|
ToString(audio_format).c_str());
|
2011-08-30 22:17:46 +02:00
|
|
|
|
2021-03-08 16:58:12 +01:00
|
|
|
const size_t in_frame_size = audio_format.GetFrameSize();
|
2011-08-30 22:17:46 +02:00
|
|
|
|
|
|
|
/* play */
|
|
|
|
|
2021-03-08 16:58:12 +01:00
|
|
|
StaticFifoBuffer<std::byte, 4096> buffer;
|
|
|
|
|
2011-08-30 22:17:46 +02:00
|
|
|
while (true) {
|
2021-03-08 16:58:12 +01:00
|
|
|
{
|
|
|
|
const auto dest = buffer.Write();
|
|
|
|
assert(!dest.empty());
|
|
|
|
|
|
|
|
ssize_t nbytes = in_fd.Read(dest.data, dest.size);
|
2011-08-30 22:17:46 +02:00
|
|
|
if (nbytes <= 0)
|
|
|
|
break;
|
|
|
|
|
2021-03-08 16:58:12 +01:00
|
|
|
buffer.Append(nbytes);
|
2011-08-30 22:17:46 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 16:58:12 +01:00
|
|
|
auto src = buffer.Read();
|
|
|
|
assert(!src.empty());
|
2011-08-30 22:17:46 +02:00
|
|
|
|
2021-03-08 16:58:12 +01:00
|
|
|
src.size -= src.size % in_frame_size;
|
|
|
|
if (src.empty())
|
|
|
|
continue;
|
2011-08-30 22:17:46 +02:00
|
|
|
|
2021-03-08 16:58:12 +01:00
|
|
|
size_t consumed = ao.Play(src.data, src.size);
|
|
|
|
|
|
|
|
assert(consumed <= src.size);
|
|
|
|
assert(consumed % in_frame_size == 0);
|
|
|
|
|
|
|
|
buffer.Consume(consumed);
|
2011-08-30 22:17:46 +02:00
|
|
|
}
|
2021-03-08 22:20:54 +01:00
|
|
|
|
|
|
|
ao.Drain();
|
2011-08-30 22:17:46 +02:00
|
|
|
}
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
int main(int argc, char **argv)
|
2015-12-16 11:12:30 +01:00
|
|
|
try {
|
2021-03-05 17:33:55 +01:00
|
|
|
const auto c = ParseCommandLine(argc, argv);
|
2021-03-05 17:37:10 +01:00
|
|
|
SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);
|
2009-07-19 17:24:43 +02:00
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
/* read configuration file (mpd.conf) */
|
|
|
|
|
2021-03-05 17:33:55 +01:00
|
|
|
const auto config = AutoLoadConfigFile(c.config_path);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2017-02-10 22:14:07 +01:00
|
|
|
EventThread io_thread;
|
|
|
|
io_thread.Start();
|
2011-08-28 17:29:09 +02:00
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
/* initialize the audio output */
|
|
|
|
|
2021-03-05 17:33:55 +01:00
|
|
|
auto ao = LoadAudioOutput(config, io_thread.GetEventLoop(),
|
|
|
|
c.output_name);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2011-08-30 22:17:46 +02:00
|
|
|
/* do it */
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2021-03-08 16:36:52 +01:00
|
|
|
RunOutput(*ao, c.audio_format, FileDescriptor(STDIN_FILENO));
|
2009-03-10 15:46:55 +01:00
|
|
|
|
|
|
|
/* cleanup and exit */
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
return EXIT_SUCCESS;
|
2018-07-17 21:56:43 +02:00
|
|
|
} catch (...) {
|
|
|
|
PrintException(std::current_exception());
|
2015-12-16 11:12:30 +01:00
|
|
|
return EXIT_FAILURE;
|
2018-07-17 21:56:43 +02:00
|
|
|
}
|