2009-03-10 15:46:55 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
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"
|
2015-01-21 21:14:25 +01:00
|
|
|
#include "config/Param.hxx"
|
2014-01-24 00:20:01 +01:00
|
|
|
#include "config/ConfigGlobal.hxx"
|
|
|
|
#include "config/ConfigOption.hxx"
|
2017-08-07 21:55:29 +02:00
|
|
|
#include "config/Block.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"
|
2013-01-30 21:47:12 +01:00
|
|
|
#include "AudioParser.hxx"
|
2013-04-09 01:24:52 +02:00
|
|
|
#include "pcm/PcmConvert.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"
|
2013-12-24 14:44:08 +01:00
|
|
|
#include "Log.hxx"
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#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
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
static std::unique_ptr<AudioOutput>
|
2017-05-23 00:31:13 +02:00
|
|
|
load_audio_output(EventLoop &event_loop, const char *name)
|
2009-03-10 15:46:55 +01:00
|
|
|
{
|
2017-08-07 21:55:29 +02:00
|
|
|
const auto *block = config_find_block(ConfigBlockOption::AUDIO_OUTPUT,
|
2015-01-21 22:13:44 +01:00
|
|
|
"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);
|
|
|
|
|
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
|
2017-08-07 21:55:29 +02:00
|
|
|
run_output(AudioOutput &ao, AudioFormat audio_format)
|
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
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
size_t frame_size = audio_format.GetFrameSize();
|
2011-08-30 22:17:46 +02:00
|
|
|
|
|
|
|
/* play */
|
|
|
|
|
|
|
|
size_t length = 0;
|
|
|
|
char buffer[4096];
|
|
|
|
while (true) {
|
|
|
|
if (length < sizeof(buffer)) {
|
|
|
|
ssize_t nbytes = read(0, buffer + length,
|
|
|
|
sizeof(buffer) - length);
|
|
|
|
if (nbytes <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
length += (size_t)nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t play_length = (length / frame_size) * frame_size;
|
|
|
|
if (play_length > 0) {
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t consumed = ao.Play(buffer, play_length);
|
2011-08-30 22:17:46 +02:00
|
|
|
|
|
|
|
assert(consumed <= length);
|
|
|
|
assert(consumed % frame_size == 0);
|
|
|
|
|
|
|
|
length -= consumed;
|
|
|
|
memmove(buffer, buffer + consumed, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
int main(int argc, char **argv)
|
2015-12-16 11:12:30 +01:00
|
|
|
try {
|
2009-03-10 15:46:55 +01:00
|
|
|
if (argc < 3 || argc > 4) {
|
2013-12-24 14:44:08 +01:00
|
|
|
fprintf(stderr, "Usage: run_output CONFIG NAME [FORMAT] <IN\n");
|
|
|
|
return EXIT_FAILURE;
|
2009-03-10 15:46:55 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:23:35 +01:00
|
|
|
const Path config_path = Path::FromFS(argv[1]);
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format(44100, SampleFormat::S16, 2);
|
2009-07-19 17:24:43 +02:00
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
/* read configuration file (mpd.conf) */
|
|
|
|
|
|
|
|
config_global_init();
|
2015-12-16 11:05:33 +01:00
|
|
|
ReadConfigFile(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 */
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
auto ao = load_audio_output(io_thread.GetEventLoop(), argv[2]);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
|
|
|
/* parse the audio format */
|
|
|
|
|
2016-10-28 21:46:20 +02:00
|
|
|
if (argc > 3)
|
|
|
|
audio_format = ParseAudioFormat(argv[3], false);
|
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
|
|
|
|
2017-01-25 09:48:59 +01:00
|
|
|
run_output(*ao, audio_format);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
|
|
|
/* cleanup and exit */
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
ao.reset();
|
2009-03-10 15:46:55 +01:00
|
|
|
|
|
|
|
config_global_finish();
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
return EXIT_SUCCESS;
|
2015-12-16 11:12:30 +01:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LogError(e);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|