2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
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"
|
2022-11-28 21:58:21 +01:00
|
|
|
#include "lib/fmt/RuntimeError.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"
|
2022-11-29 11:27:59 +01:00
|
|
|
#include "cmdline/OptionDef.hxx"
|
|
|
|
#include "cmdline/OptionParser.hxx"
|
2023-03-29 10:06:16 +02:00
|
|
|
#include "io/FileDescriptor.hxx"
|
2017-01-17 22:04:31 +01:00
|
|
|
#include "util/StringBuffer.hxx"
|
2016-11-09 11:56:01 +01:00
|
|
|
#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();
|
2022-07-04 19:01:11 +02:00
|
|
|
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
|
|
|
|
2022-07-04 19:01:11 +02:00
|
|
|
if (args.size() > 2)
|
2021-03-05 17:37:10 +01:00
|
|
|
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)
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("No such configured audio output: {}",
|
|
|
|
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");
|
|
|
|
|
2023-03-07 11:56:49 +01:00
|
|
|
const auto *plugin = GetAudioOutputPluginByName(plugin_name);
|
2017-08-07 21:55:29 +02:00
|
|
|
if (plugin == nullptr)
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("No such audio output plugin: {}",
|
|
|
|
plugin_name);
|
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());
|
|
|
|
|
2023-09-27 10:46:43 +02:00
|
|
|
ssize_t nbytes = in_fd.Read(dest);
|
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
|
|
|
|
2022-05-10 17:26:41 +02:00
|
|
|
src = src.first(src.size() - src.size() % in_frame_size);
|
2021-03-08 16:58:12 +01:00
|
|
|
if (src.empty())
|
|
|
|
continue;
|
2011-08-30 22:17:46 +02:00
|
|
|
|
2022-07-12 12:31:35 +02:00
|
|
|
size_t consumed = ao.Play(src);
|
2021-03-08 16:58:12 +01:00
|
|
|
|
2022-05-10 17:26:41 +02:00
|
|
|
assert(consumed <= src.size());
|
2021-03-08 16:58:12 +01:00
|
|
|
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
|
|
|
}
|