2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2013-04-17 22:58:33 +02:00
|
|
|
#include "Instance.hxx"
|
2013-01-03 17:31:38 +01:00
|
|
|
#include "CommandLine.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "PlaylistFile.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicChunk.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "StateFile.hxx"
|
2013-01-02 22:43:56 +01:00
|
|
|
#include "Mapper.hxx"
|
2013-01-03 03:06:45 +01:00
|
|
|
#include "Permission.hxx"
|
2013-01-03 11:05:44 +01:00
|
|
|
#include "Listen.hxx"
|
2018-01-29 23:53:52 +01:00
|
|
|
#include "client/Listener.hxx"
|
2019-04-03 21:31:32 +02:00
|
|
|
#include "client/Config.hxx"
|
2019-04-03 20:59:00 +02:00
|
|
|
#include "client/List.hxx"
|
2013-10-20 13:41:04 +02:00
|
|
|
#include "command/AllCommands.hxx"
|
2013-01-04 22:42:05 +01:00
|
|
|
#include "Partition.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Config.hxx"
|
2016-11-25 11:13:08 +01:00
|
|
|
#include "ReplayGainGlobal.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-01-09 22:33:06 +01:00
|
|
|
#include "Log.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "LogInit.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/Init.hxx"
|
2013-01-10 19:13:00 +01:00
|
|
|
#include "event/Loop.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2013-10-17 22:13:54 +02:00
|
|
|
#include "fs/Config.hxx"
|
2014-01-23 23:30:12 +01:00
|
|
|
#include "playlist/PlaylistRegistry.hxx"
|
2014-01-24 00:14:54 +01:00
|
|
|
#include "zeroconf/ZeroconfGlue.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "decoder/DecoderList.hxx"
|
2016-12-03 14:12:08 +01:00
|
|
|
#include "AudioParser.hxx"
|
2013-11-29 10:06:20 +01:00
|
|
|
#include "pcm/PcmConvert.hxx"
|
2014-01-24 16:12:10 +01:00
|
|
|
#include "unix/SignalHandlers.hxx"
|
2014-01-30 18:37:30 +01:00
|
|
|
#include "thread/Slack.hxx"
|
2018-02-24 22:31:40 +01:00
|
|
|
#include "net/Init.hxx"
|
2014-06-10 22:38:00 +02:00
|
|
|
#include "lib/icu/Init.hxx"
|
2018-08-19 22:47:05 +02:00
|
|
|
#include "config/File.hxx"
|
2018-08-19 23:01:30 +02:00
|
|
|
#include "config/Check.hxx"
|
2018-07-17 22:51:18 +02:00
|
|
|
#include "config/Data.hxx"
|
2015-01-21 21:14:25 +01:00
|
|
|
#include "config/Param.hxx"
|
2018-08-19 22:29:39 +02:00
|
|
|
#include "config/Path.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "config/Defaults.hxx"
|
|
|
|
#include "config/Option.hxx"
|
|
|
|
#include "config/Domain.hxx"
|
2016-12-03 13:56:25 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2018-08-01 20:14:31 +02:00
|
|
|
#include "util/ScopeExit.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
|
|
|
#include "unix/Daemon.hxx"
|
|
|
|
#endif
|
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
#include "db/update/Service.hxx"
|
2014-02-17 21:47:15 +01:00
|
|
|
#include "db/Configured.hxx"
|
2014-02-17 20:57:56 +01:00
|
|
|
#include "db/DatabasePlugin.hxx"
|
2014-02-26 09:17:41 +01:00
|
|
|
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
|
2014-02-09 07:21:25 +01:00
|
|
|
#include "storage/Configured.hxx"
|
2014-02-09 07:44:07 +01:00
|
|
|
#include "storage/CompositeStorage.hxx"
|
2014-02-18 21:05:04 +01:00
|
|
|
#ifdef ENABLE_INOTIFY
|
|
|
|
#include "db/update/InotifyUpdate.hxx"
|
|
|
|
#endif
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
|
|
|
#include "neighbor/Glue.hxx"
|
|
|
|
#endif
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
#ifdef ENABLE_SQLITE
|
2019-04-24 15:15:19 +02:00
|
|
|
#include "sticker/Database.hxx"
|
2009-01-19 18:51:57 +01:00
|
|
|
#endif
|
|
|
|
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2014-01-24 00:09:37 +01:00
|
|
|
#include "archive/ArchiveList.hxx"
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2012-09-28 00:40:00 +02:00
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#ifdef ANDROID
|
2014-03-01 09:19:32 +01:00
|
|
|
#include "java/Global.hxx"
|
|
|
|
#include "java/File.hxx"
|
2014-03-01 17:46:59 +01:00
|
|
|
#include "android/Environment.hxx"
|
2014-03-01 20:20:29 +01:00
|
|
|
#include "android/Context.hxx"
|
2014-11-02 16:52:43 +01:00
|
|
|
#include "android/LogListener.hxx"
|
2014-07-30 10:22:22 +02:00
|
|
|
#include "fs/FileSystem.hxx"
|
2014-02-18 08:33:06 +01:00
|
|
|
#include "org_musicpd_Bridge.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-01 19:24:50 +02:00
|
|
|
#ifdef ENABLE_DBUS
|
|
|
|
#include "lib/dbus/Init.hxx"
|
2015-03-21 14:18:15 +01:00
|
|
|
#endif
|
|
|
|
|
2018-08-01 19:24:50 +02:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
#include <systemd/sd-daemon.h>
|
2018-06-03 10:37:54 +02:00
|
|
|
#endif
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:43:00 +01:00
|
|
|
|
2009-01-05 09:31:09 +01:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2008-11-05 18:37:39 +01:00
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-15 12:32:15 +01:00
|
|
|
#include <limits.h>
|
|
|
|
|
2017-05-15 22:40:23 +02:00
|
|
|
static constexpr size_t KILOBYTE = 1024;
|
|
|
|
static constexpr size_t MEGABYTE = 1024 * KILOBYTE;
|
|
|
|
|
|
|
|
static constexpr size_t DEFAULT_BUFFER_SIZE = 4 * MEGABYTE;
|
2017-05-16 07:20:47 +02:00
|
|
|
|
2018-07-16 10:31:42 +02:00
|
|
|
static constexpr
|
2017-05-16 07:20:47 +02:00
|
|
|
size_t MIN_BUFFER_SIZE = std::max(CHUNK_SIZE * 32,
|
|
|
|
64 * KILOBYTE);
|
2017-05-15 22:39:57 +02:00
|
|
|
|
2014-03-01 20:20:29 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
Context *context;
|
2014-11-02 16:52:43 +01:00
|
|
|
LogListener *logListener;
|
2014-03-01 20:20:29 +01:00
|
|
|
#endif
|
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
Instance *global_instance;
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2016-12-10 00:00:50 +01:00
|
|
|
struct Config {
|
2016-12-03 13:15:56 +01:00
|
|
|
ReplayGainConfig replay_gain;
|
2016-12-10 00:00:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static Config
|
2018-07-17 22:32:56 +02:00
|
|
|
LoadConfig(const ConfigData &config)
|
2016-12-10 00:00:50 +01:00
|
|
|
{
|
2018-07-17 22:32:56 +02:00
|
|
|
return {LoadReplayGainConfig(config)};
|
2016-12-10 00:00:50 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2014-02-21 23:12:51 +01:00
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
static void
|
2018-07-17 22:48:58 +02:00
|
|
|
glue_daemonize_init(const struct options *options,
|
|
|
|
const ConfigData &config)
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
2018-07-17 22:48:58 +02:00
|
|
|
daemonize_init(config.GetString(ConfigOption::USER),
|
|
|
|
config.GetString(ConfigOption::GROUP),
|
|
|
|
config.GetPath(ConfigOption::PID_FILE));
|
2009-07-15 18:58:12 +02:00
|
|
|
|
|
|
|
if (options->kill)
|
|
|
|
daemonize_kill();
|
|
|
|
}
|
|
|
|
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
static void
|
2018-07-17 22:48:58 +02:00
|
|
|
glue_mapper_init(const ConfigData &config)
|
2009-07-15 18:58:19 +02:00
|
|
|
{
|
2018-07-17 22:48:58 +02:00
|
|
|
mapper_init(config.GetPath(ConfigOption::PLAYLIST_DIR));
|
2014-02-07 23:37:39 +01:00
|
|
|
}
|
2014-02-07 23:13:24 +01:00
|
|
|
|
2014-02-07 22:54:18 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
static void
|
2019-05-29 21:22:25 +02:00
|
|
|
InitStorage(Instance &instance, EventLoop &event_loop,
|
|
|
|
const ConfigData &config)
|
2014-02-07 23:37:39 +01:00
|
|
|
{
|
2018-07-17 22:57:08 +02:00
|
|
|
auto storage = CreateConfiguredStorage(config, event_loop);
|
2014-02-09 07:44:07 +01:00
|
|
|
if (storage == nullptr)
|
2016-10-27 08:40:40 +02:00
|
|
|
return;
|
2014-02-09 07:44:07 +01:00
|
|
|
|
|
|
|
CompositeStorage *composite = new CompositeStorage();
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.storage = composite;
|
2018-01-02 16:18:34 +01:00
|
|
|
composite->Mount("", std::move(storage));
|
2009-07-15 18:58:19 +02:00
|
|
|
}
|
|
|
|
|
2009-02-02 16:57:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the database. If this function returns false, this has not
|
|
|
|
* succeeded, and the caller should create the database after the
|
|
|
|
* process has been daemonized.
|
|
|
|
*/
|
|
|
|
static bool
|
2019-05-29 21:22:25 +02:00
|
|
|
glue_db_init_and_load(Instance &instance, const ConfigData &config)
|
2006-07-19 20:04:15 +02:00
|
|
|
{
|
2019-05-29 21:22:25 +02:00
|
|
|
auto db = CreateConfiguredDatabase(config, instance.event_loop,
|
|
|
|
instance.io_thread.GetEventLoop(),
|
|
|
|
instance);
|
2019-02-20 20:31:08 +01:00
|
|
|
if (!db)
|
2016-10-28 23:00:10 +02:00
|
|
|
return true;
|
2012-08-08 08:36:14 +02:00
|
|
|
|
2019-02-20 20:31:08 +01:00
|
|
|
if (db->GetPlugin().RequireStorage()) {
|
2019-05-29 21:22:25 +02:00
|
|
|
InitStorage(instance, instance.io_thread.GetEventLoop(),
|
|
|
|
config);
|
2014-02-17 22:12:33 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
if (instance.storage == nullptr) {
|
2014-02-17 20:57:56 +01:00
|
|
|
LogDefault(config_domain,
|
|
|
|
"Found database setting without "
|
|
|
|
"music_directory - disabling database");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-17 22:57:08 +02:00
|
|
|
if (IsStorageConfigured(config))
|
2014-02-17 20:57:56 +01:00
|
|
|
LogDefault(config_domain,
|
|
|
|
"Ignoring the storage configuration "
|
|
|
|
"because the database does not need it");
|
2014-02-17 22:12:33 +01:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:52:11 +01:00
|
|
|
try {
|
2019-02-20 20:31:08 +01:00
|
|
|
db->Open();
|
2017-01-23 18:52:11 +01:00
|
|
|
} catch (...) {
|
|
|
|
std::throw_with_nested(std::runtime_error("Failed to open database plugin"));
|
|
|
|
}
|
2009-02-02 16:57:24 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.database = std::move(db);
|
2019-02-20 20:31:08 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
auto *sdb = dynamic_cast<SimpleDatabase *>(instance.database.get());
|
2019-02-20 20:31:08 +01:00
|
|
|
if (sdb == nullptr)
|
2014-02-04 10:09:09 +01:00
|
|
|
return true;
|
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.update = new UpdateService(config,
|
|
|
|
instance.event_loop, *sdb,
|
|
|
|
static_cast<CompositeStorage &>(*instance.storage),
|
|
|
|
instance);
|
2014-02-04 10:09:09 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
/* run database update after daemonization? */
|
2019-02-20 20:31:08 +01:00
|
|
|
return sdb->FileExists();
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2014-02-17 21:42:58 +01:00
|
|
|
static bool
|
2019-05-29 21:22:25 +02:00
|
|
|
InitDatabaseAndStorage(Instance &instance, const ConfigData &config)
|
2014-02-17 21:42:58 +01:00
|
|
|
{
|
2019-05-29 21:22:25 +02:00
|
|
|
const bool create_db = !glue_db_init_and_load(instance, config);
|
2014-02-17 21:42:58 +01:00
|
|
|
return create_db;
|
|
|
|
}
|
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
|
|
|
|
2019-04-24 15:18:01 +02:00
|
|
|
#ifdef ENABLE_SQLITE
|
|
|
|
|
2009-07-15 18:58:12 +02:00
|
|
|
/**
|
|
|
|
* Configure and initialize the sticker subsystem.
|
|
|
|
*/
|
2019-04-24 15:18:01 +02:00
|
|
|
static std::unique_ptr<StickerDatabase>
|
|
|
|
LoadStickerDatabase(const ConfigData &config)
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
2018-07-17 22:48:58 +02:00
|
|
|
auto sticker_file = config.GetPath(ConfigOption::STICKER_FILE);
|
2016-11-02 10:01:29 +01:00
|
|
|
if (sticker_file.IsNull())
|
2019-04-24 15:18:01 +02:00
|
|
|
return nullptr;
|
2009-07-15 18:58:12 +02:00
|
|
|
|
2019-04-24 15:18:01 +02:00
|
|
|
return std::make_unique<StickerDatabase>(std::move(sticker_file));
|
2009-07-15 18:58:12 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 15:18:01 +02:00
|
|
|
#endif
|
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
static void
|
2019-05-29 21:22:25 +02:00
|
|
|
glue_state_file_init(Instance &instance, const ConfigData &raw_config)
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
2018-07-17 23:27:50 +02:00
|
|
|
StateFileConfig config(raw_config);
|
|
|
|
if (!config.IsEnabled())
|
2016-11-02 10:01:29 +01:00
|
|
|
return;
|
2014-08-24 12:59:45 +02:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.state_file = new StateFile(std::move(config),
|
|
|
|
instance.partitions.front(),
|
|
|
|
instance.event_loop);
|
|
|
|
instance.state_file->Read();
|
2009-07-15 18:58:12 +02:00
|
|
|
}
|
|
|
|
|
2009-01-18 17:32:43 +01:00
|
|
|
/**
|
|
|
|
* Initialize the decoder and player core, including the music pipe.
|
|
|
|
*/
|
|
|
|
static void
|
2019-05-29 21:22:25 +02:00
|
|
|
initialize_decoder_and_player(Instance &instance,
|
|
|
|
const ConfigData &config,
|
2018-07-17 22:48:58 +02:00
|
|
|
const ReplayGainConfig &replay_gain_config)
|
2009-01-18 17:32:43 +01:00
|
|
|
{
|
2016-10-28 11:38:37 +02:00
|
|
|
const ConfigParam *param;
|
2009-01-18 17:32:43 +01:00
|
|
|
|
2014-01-18 11:34:13 +01:00
|
|
|
size_t buffer_size;
|
2018-07-17 22:48:58 +02:00
|
|
|
param = config.GetParam(ConfigOption::AUDIO_BUFFER_SIZE);
|
2013-09-26 17:31:50 +02:00
|
|
|
if (param != nullptr) {
|
2014-01-18 11:34:13 +01:00
|
|
|
char *test;
|
2013-10-15 22:32:39 +02:00
|
|
|
long tmp = strtol(param->value.c_str(), &test, 10);
|
2012-02-15 20:35:06 +01:00
|
|
|
if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
|
2018-08-19 23:13:29 +02:00
|
|
|
throw FormatRuntimeError("buffer size \"%s\" is not a "
|
|
|
|
"positive integer, line %i",
|
|
|
|
param->value.c_str(), param->line);
|
2017-05-15 22:40:23 +02:00
|
|
|
buffer_size = tmp * KILOBYTE;
|
2017-05-15 22:39:57 +02:00
|
|
|
|
|
|
|
if (buffer_size < MIN_BUFFER_SIZE) {
|
|
|
|
FormatWarning(config_domain, "buffer size %lu is too small, using %lu bytes instead",
|
|
|
|
(unsigned long)buffer_size,
|
|
|
|
(unsigned long)MIN_BUFFER_SIZE);
|
|
|
|
buffer_size = MIN_BUFFER_SIZE;
|
|
|
|
}
|
2009-01-18 17:32:43 +01:00
|
|
|
} else
|
|
|
|
buffer_size = DEFAULT_BUFFER_SIZE;
|
|
|
|
|
2014-01-18 11:34:13 +01:00
|
|
|
const unsigned buffered_chunks = buffer_size / CHUNK_SIZE;
|
2009-01-18 17:32:43 +01:00
|
|
|
|
|
|
|
if (buffered_chunks >= 1 << 15)
|
2018-08-19 23:13:29 +02:00
|
|
|
throw FormatRuntimeError("buffer size \"%lu\" is too big",
|
|
|
|
(unsigned long)buffer_size);
|
2009-01-18 17:32:43 +01:00
|
|
|
|
2013-01-04 22:42:05 +01:00
|
|
|
const unsigned max_length =
|
2018-07-17 22:48:58 +02:00
|
|
|
config.GetPositive(ConfigOption::MAX_PLAYLIST_LENGTH,
|
|
|
|
DEFAULT_PLAYLIST_MAX_LENGTH);
|
2013-01-04 22:42:05 +01:00
|
|
|
|
2016-12-03 14:12:08 +01:00
|
|
|
AudioFormat configured_audio_format = AudioFormat::Undefined();
|
2018-07-17 22:48:58 +02:00
|
|
|
param = config.GetParam(ConfigOption::AUDIO_OUTPUT_FORMAT);
|
2016-12-03 14:12:08 +01:00
|
|
|
if (param != nullptr) {
|
|
|
|
try {
|
|
|
|
configured_audio_format = ParseAudioFormat(param->value.c_str(),
|
|
|
|
true);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2016-12-03 14:12:08 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("error parsing line %i",
|
|
|
|
param->line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.partitions.emplace_back(instance,
|
|
|
|
"default",
|
|
|
|
max_length,
|
|
|
|
buffered_chunks,
|
|
|
|
configured_audio_format,
|
|
|
|
replay_gain_config);
|
|
|
|
auto &partition = instance.partitions.back();
|
2016-12-03 13:56:25 +01:00
|
|
|
|
|
|
|
try {
|
2018-07-17 22:48:58 +02:00
|
|
|
param = config.GetParam(ConfigOption::REPLAYGAIN);
|
2016-12-03 13:56:25 +01:00
|
|
|
if (param != nullptr)
|
2017-02-17 23:21:20 +01:00
|
|
|
partition.replay_gain_mode =
|
2016-12-03 13:56:25 +01:00
|
|
|
FromString(param->value.c_str());
|
|
|
|
} catch (...) {
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to parse line %i",
|
|
|
|
param->line));
|
|
|
|
}
|
2009-01-18 17:32:43 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:45:48 +01:00
|
|
|
inline void
|
|
|
|
Instance::BeginShutdownUpdate() noexcept
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
#ifdef ENABLE_INOTIFY
|
|
|
|
mpd_inotify_finish();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (update != nullptr)
|
|
|
|
update->CancelAllAsync();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
Instance::BeginShutdownPartitions() noexcept
|
|
|
|
{
|
2018-01-29 23:53:52 +01:00
|
|
|
for (auto &partition : partitions) {
|
2018-01-29 23:45:48 +01:00
|
|
|
partition.pc.Kill();
|
2018-01-29 23:53:52 +01:00
|
|
|
partition.listener.reset();
|
|
|
|
}
|
2018-01-29 23:45:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:27:07 +01:00
|
|
|
void
|
2019-04-24 14:56:08 +02:00
|
|
|
Instance::OnIdle(unsigned flags) noexcept
|
2008-12-30 19:24:39 +01:00
|
|
|
{
|
2013-01-16 23:23:30 +01:00
|
|
|
/* send "idle" notifications to all subscribed
|
2008-12-30 19:24:39 +01:00
|
|
|
clients */
|
2016-03-10 09:27:07 +01:00
|
|
|
client_list->IdleAdd(flags);
|
2013-04-08 22:31:51 +02:00
|
|
|
|
2013-07-30 08:30:56 +02:00
|
|
|
if (flags & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT) &&
|
2016-03-10 09:27:07 +01:00
|
|
|
state_file != nullptr)
|
|
|
|
state_file->CheckModified();
|
2008-12-30 19:24:39 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#ifndef ANDROID
|
|
|
|
|
2018-08-01 19:38:25 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) noexcept
|
2010-09-21 03:28:08 +02:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2010-09-21 03:28:08 +02:00
|
|
|
return win32_main(argc, argv);
|
|
|
|
#else
|
|
|
|
return mpd_main(argc, argv);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#endif
|
|
|
|
|
2019-05-29 14:52:58 +02:00
|
|
|
static void
|
2019-05-29 21:22:25 +02:00
|
|
|
mpd_main_after_fork(Instance &instance,
|
|
|
|
const ConfigData &raw_config,
|
2018-08-01 19:38:25 +02:00
|
|
|
const Config &config);
|
2014-10-13 00:29:43 +02:00
|
|
|
|
2019-05-29 14:52:58 +02:00
|
|
|
static inline void
|
2019-05-29 15:08:26 +02:00
|
|
|
MainConfigured(const struct options &options, const ConfigData &raw_config)
|
2018-08-01 20:13:27 +02:00
|
|
|
{
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2008-12-30 16:28:13 +01:00
|
|
|
daemonize_close_stdin();
|
2014-11-20 20:38:37 +01:00
|
|
|
#endif
|
2008-12-30 16:28:13 +01:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifndef ANDROID
|
2009-01-05 09:31:09 +01:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2008-11-05 18:37:39 +01:00
|
|
|
/* initialize locale */
|
|
|
|
setlocale(LC_CTYPE,"");
|
2015-06-23 13:26:45 +02:00
|
|
|
setlocale(LC_COLLATE, "");
|
2008-11-05 18:37:39 +01:00
|
|
|
#endif
|
2013-04-17 01:53:10 +02:00
|
|
|
#endif
|
2008-11-05 18:21:49 +01:00
|
|
|
|
2018-08-02 10:16:56 +02:00
|
|
|
const ScopeIcuInit icu_init;
|
2018-02-24 22:31:40 +01:00
|
|
|
const ScopeNetInit net_init;
|
|
|
|
|
2018-08-01 19:24:50 +02:00
|
|
|
#ifdef ENABLE_DBUS
|
|
|
|
const ODBus::ScopeInit dbus_init;
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 22:29:39 +02:00
|
|
|
InitPathParser(raw_config);
|
2018-07-17 22:48:43 +02:00
|
|
|
const auto config = LoadConfig(raw_config);
|
2016-12-10 00:00:50 +01:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2018-07-17 22:48:58 +02:00
|
|
|
glue_daemonize_init(&options, raw_config);
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2005-03-09 04:13:33 +01:00
|
|
|
|
2018-07-17 22:48:43 +02:00
|
|
|
TagLoadConfig(raw_config);
|
2011-09-09 21:40:28 +02:00
|
|
|
|
2018-07-17 23:02:06 +02:00
|
|
|
log_init(raw_config, options.verbose, options.log_stderr);
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
Instance instance;
|
|
|
|
global_instance = &instance;
|
2013-04-17 22:58:33 +02:00
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.neighbors = std::make_unique<NeighborGlue>();
|
|
|
|
instance.neighbors->Init(raw_config,
|
|
|
|
instance.io_thread.GetEventLoop(),
|
|
|
|
instance);
|
2014-01-18 16:36:42 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
if (instance.neighbors->IsEmpty())
|
|
|
|
instance.neighbors.reset();
|
2014-01-18 16:36:42 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-21 22:36:13 +01:00
|
|
|
const unsigned max_clients =
|
2018-07-17 22:48:58 +02:00
|
|
|
raw_config.GetPositive(ConfigOption::MAX_CONN, 10);
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.client_list = new ClientList(max_clients);
|
2013-01-16 22:55:33 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
initialize_decoder_and_player(instance,
|
|
|
|
raw_config, config.replay_gain);
|
2014-02-20 00:03:49 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
listen_global_init(raw_config, *instance.partitions.front().listener);
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2009-01-18 17:15:34 +01:00
|
|
|
daemonize_set_user();
|
2014-01-18 12:01:09 +01:00
|
|
|
daemonize_begin(options.daemon);
|
2018-08-06 10:47:06 +02:00
|
|
|
AtScopeExit() { daemonize_finish(); };
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
mpd_main_after_fork(instance, raw_config, config);
|
2018-08-01 20:13:27 +02:00
|
|
|
}
|
2018-03-03 20:56:28 +01:00
|
|
|
|
2019-05-29 14:58:24 +02:00
|
|
|
#ifndef ANDROID
|
|
|
|
|
2019-05-29 15:08:26 +02:00
|
|
|
static inline void
|
|
|
|
MainOrThrow(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct options options;
|
|
|
|
ConfigData raw_config;
|
|
|
|
|
|
|
|
ParseCommandLine(argc, argv, options, raw_config);
|
|
|
|
|
|
|
|
MainConfigured(options, raw_config);
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:38:25 +02:00
|
|
|
int mpd_main(int argc, char *argv[]) noexcept
|
2018-08-01 20:13:27 +02:00
|
|
|
{
|
2018-08-01 20:14:31 +02:00
|
|
|
AtScopeExit() { log_deinit(); };
|
|
|
|
|
2018-08-01 20:13:27 +02:00
|
|
|
try {
|
2019-05-29 14:52:58 +02:00
|
|
|
MainOrThrow(argc, argv);
|
|
|
|
return EXIT_SUCCESS;
|
2018-08-09 11:14:40 +02:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2018-08-01 20:13:27 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-10-13 00:29:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:58:24 +02:00
|
|
|
#endif /* !ANDROID */
|
|
|
|
|
2019-05-29 14:52:58 +02:00
|
|
|
static void
|
2019-05-29 21:22:25 +02:00
|
|
|
mpd_main_after_fork(Instance &instance,
|
|
|
|
const ConfigData &raw_config, const Config &config)
|
2018-08-01 20:14:05 +02:00
|
|
|
{
|
2018-07-17 23:09:56 +02:00
|
|
|
ConfigureFS(raw_config);
|
2019-02-05 23:02:50 +01:00
|
|
|
AtScopeExit() { DeinitFS(); };
|
2011-09-09 21:32:12 +02:00
|
|
|
|
2018-07-17 22:48:58 +02:00
|
|
|
glue_mapper_init(raw_config);
|
2011-09-09 21:32:12 +02:00
|
|
|
|
2018-07-17 23:00:42 +02:00
|
|
|
initPermissions(raw_config);
|
2018-07-17 22:48:43 +02:00
|
|
|
spl_global_init(raw_config);
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2019-02-05 21:40:07 +01:00
|
|
|
const ScopeArchivePluginsInit archive_plugins_init;
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2011-10-08 13:14:29 +02:00
|
|
|
|
2018-07-17 23:04:26 +02:00
|
|
|
pcm_convert_global_init(raw_config);
|
2011-10-08 13:14:29 +02:00
|
|
|
|
2019-02-05 22:11:45 +01:00
|
|
|
const ScopeDecoderPluginsInit decoder_plugins_init(raw_config);
|
2006-08-06 08:40:11 +02:00
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2019-05-29 21:22:25 +02:00
|
|
|
const bool create_db = InitDatabaseAndStorage(instance, raw_config);
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2014-01-29 20:16:43 +01:00
|
|
|
|
2019-04-24 15:18:01 +02:00
|
|
|
#ifdef ENABLE_SQLITE
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.sticker_database = LoadStickerDatabase(raw_config);
|
2019-04-24 15:18:01 +02:00
|
|
|
#endif
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
command_init();
|
2016-12-03 13:15:56 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
for (auto &partition : instance.partitions) {
|
|
|
|
partition.outputs.Configure(instance.rtio_thread.GetEventLoop(),
|
2018-07-17 23:13:35 +02:00
|
|
|
raw_config,
|
2017-02-17 23:21:20 +01:00
|
|
|
config.replay_gain,
|
|
|
|
partition.pc);
|
|
|
|
partition.UpdateEffectiveReplayGainMode();
|
|
|
|
}
|
2017-01-03 13:44:38 +01:00
|
|
|
|
2018-07-17 23:07:50 +02:00
|
|
|
client_manager_init(raw_config);
|
2019-02-05 21:50:31 +01:00
|
|
|
const ScopeInputPluginsInit input_plugins_init(raw_config,
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.io_thread.GetEventLoop());
|
2019-02-05 23:03:29 +01:00
|
|
|
const ScopePlaylistPluginsInit playlist_plugins_init(raw_config);
|
2006-07-21 19:48:54 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2014-01-18 12:01:09 +01:00
|
|
|
daemonize_commit();
|
2014-11-20 20:38:37 +01:00
|
|
|
#endif
|
2005-11-16 15:43:04 +01:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifndef ANDROID
|
2016-12-04 20:15:45 +01:00
|
|
|
setup_log_output();
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
const ScopeSignalHandlersInit signal_handlers_init(instance.event_loop);
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2005-11-18 13:09:05 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.io_thread.Start();
|
|
|
|
instance.rtio_thread.Start();
|
2011-08-24 02:34:28 +02:00
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
2019-05-29 21:22:25 +02:00
|
|
|
if (instance.neighbors != nullptr)
|
|
|
|
instance.neighbors->Open();
|
2019-04-25 13:03:51 +02:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
AtScopeExit(&instance) {
|
|
|
|
if (instance.neighbors != nullptr)
|
|
|
|
instance.neighbors->Close();
|
2019-04-25 13:03:51 +02:00
|
|
|
};
|
2014-01-18 16:36:42 +01:00
|
|
|
#endif
|
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
ZeroconfInit(raw_config, instance.event_loop);
|
2007-06-04 19:41:18 +02:00
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2009-02-02 16:57:24 +01:00
|
|
|
if (create_db) {
|
2009-09-20 23:31:35 +02:00
|
|
|
/* the database failed to load: recreate the
|
|
|
|
database */
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.update->Enqueue("", true);
|
2009-02-02 16:57:24 +01:00
|
|
|
}
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2009-02-02 16:57:24 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
glue_state_file_init(instance, raw_config);
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2014-02-18 21:05:04 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2018-07-17 22:48:58 +02:00
|
|
|
if (raw_config.GetBool(ConfigOption::AUTO_UPDATE, false)) {
|
2009-10-24 18:24:29 +02:00
|
|
|
#ifdef ENABLE_INOTIFY
|
2019-05-29 21:22:25 +02:00
|
|
|
if (instance.storage != nullptr &&
|
|
|
|
instance.update != nullptr)
|
|
|
|
mpd_inotify_init(instance.event_loop,
|
|
|
|
*instance.storage,
|
|
|
|
*instance.update,
|
2018-07-17 22:48:58 +02:00
|
|
|
raw_config.GetUnsigned(ConfigOption::AUTO_UPDATE_DEPTH,
|
|
|
|
INT_MAX));
|
2009-10-26 19:14:40 +01:00
|
|
|
#else
|
2015-09-17 23:15:48 +02:00
|
|
|
FormatWarning(config_domain,
|
2013-09-27 22:31:24 +02:00
|
|
|
"inotify: auto_update was disabled. enable during compilation phase");
|
2009-10-24 18:24:29 +02:00
|
|
|
#endif
|
2014-01-18 11:20:20 +01:00
|
|
|
}
|
2014-02-18 21:05:04 +01:00
|
|
|
#endif
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2018-08-19 23:01:30 +02:00
|
|
|
Check(raw_config);
|
2009-06-19 09:02:14 +02:00
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
/* enable all audio outputs (if not already done by
|
|
|
|
playlist_state_restore() */
|
2019-05-29 21:22:25 +02:00
|
|
|
for (auto &partition : instance.partitions)
|
2017-02-17 23:21:20 +01:00
|
|
|
partition.pc.LockUpdateAudio();
|
2009-10-23 10:55:52 +02:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2010-09-21 03:28:08 +02:00
|
|
|
win32_app_started();
|
|
|
|
#endif
|
2008-10-14 22:38:14 +02:00
|
|
|
|
2014-01-30 18:37:30 +01:00
|
|
|
/* the MPD frontend does not care about timer slack; set it to
|
|
|
|
a huge value to allow the kernel to reduce CPU wakeups */
|
2019-05-29 21:11:32 +02:00
|
|
|
SetThreadTimerSlack(std::chrono::milliseconds(100));
|
2014-01-30 18:37:30 +01:00
|
|
|
|
2015-03-21 14:18:15 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
sd_notify(0, "READY=1");
|
|
|
|
#endif
|
|
|
|
|
2010-09-21 03:28:08 +02:00
|
|
|
/* run the main loop */
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.event_loop.Run();
|
2008-11-21 18:36:33 +01:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2010-09-21 03:28:08 +02:00
|
|
|
win32_app_stopping();
|
|
|
|
#endif
|
|
|
|
|
2008-12-30 19:24:39 +01:00
|
|
|
/* cleanup */
|
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.BeginShutdownUpdate();
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
if (instance.state_file != nullptr) {
|
|
|
|
instance.state_file->Write();
|
|
|
|
delete instance.state_file;
|
2013-01-14 11:00:22 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:53:52 +01:00
|
|
|
ZeroconfDeinit();
|
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
instance.BeginShutdownPartitions();
|
2017-02-17 23:21:20 +01:00
|
|
|
|
2019-05-29 21:22:25 +02:00
|
|
|
delete instance.client_list;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2014-02-18 08:33:06 +01:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
|
2019-05-29 14:58:24 +02:00
|
|
|
static void
|
|
|
|
AndroidMain()
|
|
|
|
{
|
|
|
|
struct options options;
|
|
|
|
ConfigData raw_config;
|
|
|
|
|
|
|
|
const auto sdcard = Environment::getExternalStorageDirectory();
|
|
|
|
if (!sdcard.IsNull()) {
|
|
|
|
const auto config_path =
|
|
|
|
sdcard / Path::FromFS("mpd.conf");
|
|
|
|
if (FileExists(config_path))
|
|
|
|
ReadConfigFile(raw_config, config_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
MainConfigured(options, raw_config);
|
|
|
|
}
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
gcc_visibility_default
|
|
|
|
JNIEXPORT void JNICALL
|
2014-11-02 16:52:43 +01:00
|
|
|
Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context, jobject _logListener)
|
2014-02-18 08:33:06 +01:00
|
|
|
{
|
2014-03-01 09:19:32 +01:00
|
|
|
Java::Init(env);
|
2019-04-24 14:08:51 +02:00
|
|
|
Java::Object::Initialise(env);
|
2014-03-01 09:19:32 +01:00
|
|
|
Java::File::Initialise(env);
|
2014-03-01 17:46:59 +01:00
|
|
|
Environment::Initialise(env);
|
2019-05-29 15:01:23 +02:00
|
|
|
AtScopeExit(env) { Environment::Deinitialise(env); };
|
2014-03-01 09:19:32 +01:00
|
|
|
|
2014-03-01 20:20:29 +01:00
|
|
|
context = new Context(env, _context);
|
2019-05-29 15:01:23 +02:00
|
|
|
AtScopeExit() { delete context; };
|
|
|
|
|
2014-11-02 16:52:43 +01:00
|
|
|
if (_logListener != nullptr)
|
|
|
|
logListener = new LogListener(env, _logListener);
|
2019-05-29 15:01:23 +02:00
|
|
|
AtScopeExit() { delete logListener; };
|
2014-03-01 20:20:29 +01:00
|
|
|
|
2019-05-29 14:58:24 +02:00
|
|
|
try {
|
|
|
|
AndroidMain();
|
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
|
|
|
}
|
2014-02-18 08:33:06 +01:00
|
|
|
}
|
|
|
|
|
2014-09-15 14:03:19 +02:00
|
|
|
gcc_visibility_default
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_musicpd_Bridge_shutdown(JNIEnv *, jclass)
|
|
|
|
{
|
2019-05-29 21:22:25 +02:00
|
|
|
if (global_instance != nullptr)
|
|
|
|
global_instance->Break();
|
2014-09-15 14:03:19 +02:00
|
|
|
}
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#endif
|