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.
|
|
|
|
*
|
|
|
|
* 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.
|
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"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "output/Internal.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "output/OutputPlugin.hxx"
|
2016-12-14 12:14:57 +01:00
|
|
|
#include "output/Client.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"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-01-15 07:53:44 +01:00
|
|
|
#include "Main.hxx"
|
|
|
|
#include "event/Loop.hxx"
|
2014-10-07 20:02:13 +02:00
|
|
|
#include "ScopeIOThread.hxx"
|
2013-01-29 17:23:35 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-01-30 21:47:12 +01:00
|
|
|
#include "AudioParser.hxx"
|
2016-11-25 13:54:55 +01:00
|
|
|
#include "ReplayGainConfig.hxx"
|
2013-04-09 01:24:52 +02:00
|
|
|
#include "pcm/PcmConvert.hxx"
|
2014-01-24 16:31:52 +01:00
|
|
|
#include "filter/FilterRegistry.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
|
|
|
|
|
|
|
#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
|
|
|
|
2016-12-14 12:14:57 +01:00
|
|
|
class DummyAudioOutputClient final : public AudioOutputClient {
|
|
|
|
public:
|
|
|
|
/* virtual methods from AudioOutputClient */
|
|
|
|
void ChunksConsumed() override {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyEnabled() override {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-23 17:43:50 +01:00
|
|
|
const FilterPlugin *
|
2013-08-04 23:48:01 +02:00
|
|
|
filter_plugin_by_name(gcc_unused const char *name)
|
2009-07-06 10:01:47 +02:00
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
static AudioOutput *
|
2016-12-14 12:14:57 +01:00
|
|
|
load_audio_output(EventLoop &event_loop, AudioOutputClient &client,
|
|
|
|
const char *name)
|
2009-03-10 15:46:55 +01:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
const auto *param = config_find_block(ConfigBlockOption::AUDIO_OUTPUT,
|
|
|
|
"name", name);
|
2016-11-09 11:56:01 +01:00
|
|
|
if (param == NULL)
|
|
|
|
throw FormatRuntimeError("No such configured audio output: %s\n",
|
|
|
|
name);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2016-11-25 13:54:55 +01:00
|
|
|
return audio_output_new(event_loop, ReplayGainConfig(), *param,
|
2016-11-09 11:56:01 +01:00
|
|
|
*(MixerListener *)nullptr,
|
2016-12-14 12:14:57 +01:00
|
|
|
client);
|
2009-03-10 15:46:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
static void
|
2017-01-25 09:48:59 +01:00
|
|
|
run_output(AudioOutput &ao, AudioFormat audio_format)
|
2011-08-30 22:17:46 +02:00
|
|
|
{
|
|
|
|
/* open the audio output */
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
ao_plugin_enable(ao);
|
2017-01-25 09:48:59 +01:00
|
|
|
AtScopeExit(&ao) { ao_plugin_disable(ao); };
|
2011-12-24 16:42:23 +01:00
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
ao_plugin_open(ao, audio_format);
|
2017-01-25 09:48:59 +01:00
|
|
|
AtScopeExit(&ao) { ao_plugin_close(ao); };
|
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) {
|
2011-09-16 23:31:48 +02:00
|
|
|
size_t consumed = ao_plugin_play(ao,
|
2016-11-09 11:56:01 +01:00
|
|
|
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
|
|
|
|
2014-02-05 00:02:02 +01:00
|
|
|
EventLoop event_loop;
|
2013-01-15 07:53:44 +01:00
|
|
|
|
2014-10-07 20:02:13 +02:00
|
|
|
const ScopeIOThread io_thread;
|
2011-08-28 17:29:09 +02:00
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
/* initialize the audio output */
|
|
|
|
|
2016-12-14 12:14:57 +01:00
|
|
|
DummyAudioOutputClient client;
|
|
|
|
AudioOutput *ao = load_audio_output(event_loop, client, 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 */
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
audio_output_free(ao);
|
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;
|
|
|
|
}
|