mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-22 13:36:18 +02:00
configurable process priority
This commit is contained in:
parent
389bc92b2f
commit
83d7ca8856
2 changed files with 107 additions and 88 deletions
|
@ -61,104 +61,116 @@ PcmDevice getPcmDevice(const std::string& soundcard)
|
||||||
|
|
||||||
int main (int argc, char *argv[])
|
int main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
string soundcard;
|
try
|
||||||
string ip;
|
|
||||||
// int bufferMs;
|
|
||||||
size_t port;
|
|
||||||
size_t latency;
|
|
||||||
bool runAsDaemon;
|
|
||||||
bool listPcmDevices;
|
|
||||||
|
|
||||||
po::options_description desc("Allowed options");
|
|
||||||
desc.add_options()
|
|
||||||
("help,h", "produce help message")
|
|
||||||
("version,v", "show version number")
|
|
||||||
("list,l", po::bool_switch(&listPcmDevices)->default_value(false), "list pcm devices")
|
|
||||||
("ip,i", po::value<string>(&ip), "server IP")
|
|
||||||
("port,p", po::value<size_t>(&port)->default_value(98765), "server port")
|
|
||||||
("soundcard,s", po::value<string>(&soundcard)->default_value("default"), "index or name of the soundcard")
|
|
||||||
("daemon,d", po::bool_switch(&runAsDaemon)->default_value(false), "daemonize")
|
|
||||||
("latency", po::value<size_t>(&latency)->default_value(0), "latency")
|
|
||||||
;
|
|
||||||
|
|
||||||
po::variables_map vm;
|
|
||||||
po::store(po::parse_command_line(argc, argv, desc), vm);
|
|
||||||
po::notify(vm);
|
|
||||||
|
|
||||||
std::clog.rdbuf(new Log("snapclient", LOG_DAEMON));
|
|
||||||
|
|
||||||
if (vm.count("help"))
|
|
||||||
{
|
{
|
||||||
cout << desc << "\n";
|
string soundcard;
|
||||||
return 1;
|
string ip;
|
||||||
}
|
// int bufferMs;
|
||||||
|
size_t port;
|
||||||
|
size_t latency;
|
||||||
|
int runAsDaemon;
|
||||||
|
bool listPcmDevices;
|
||||||
|
|
||||||
if (vm.count("version"))
|
po::options_description desc("Allowed options");
|
||||||
{
|
desc.add_options()
|
||||||
cout << "snapclient v" << VERSION << "\n"
|
("help,h", "produce help message")
|
||||||
<< "Copyright (C) 2014 BadAix (snapcast@badaix.de).\n"
|
("version,v", "show version number")
|
||||||
<< "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
|
("list,l", po::bool_switch(&listPcmDevices)->default_value(false), "list pcm devices")
|
||||||
<< "This is free software: you are free to change and redistribute it.\n"
|
("ip,i", po::value<string>(&ip), "server IP")
|
||||||
<< "There is NO WARRANTY, to the extent permitted by law.\n\n"
|
("port,p", po::value<size_t>(&port)->default_value(98765), "server port")
|
||||||
<< "Written by Johannes M. Pohl.\n";
|
("soundcard,s", po::value<string>(&soundcard)->default_value("default"), "index or name of the soundcard")
|
||||||
return 1;
|
("daemon,d", po::value<int>(&runAsDaemon)->implicit_value(-5), "daemonize, optional process priority [-20..19]")
|
||||||
}
|
("latency", po::value<size_t>(&latency)->default_value(0), "latency of the soundcard")
|
||||||
|
;
|
||||||
|
|
||||||
if (listPcmDevices)
|
po::variables_map vm;
|
||||||
{
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
||||||
vector<PcmDevice> pcmDevices = Player::pcm_list();
|
po::notify(vm);
|
||||||
for (auto dev: pcmDevices)
|
|
||||||
|
std::clog.rdbuf(new Log("snapclient", LOG_DAEMON));
|
||||||
|
|
||||||
|
if (vm.count("help"))
|
||||||
{
|
{
|
||||||
cout << dev.idx << ": " << dev.name << "\n"
|
cout << desc << "\n";
|
||||||
<< dev.description << "\n\n";
|
return 1;
|
||||||
}
|
}
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
signal(SIGHUP, signal_handler);
|
if (vm.count("version"))
|
||||||
signal(SIGTERM, signal_handler);
|
|
||||||
signal(SIGINT, signal_handler);
|
|
||||||
|
|
||||||
if (runAsDaemon)
|
|
||||||
{
|
|
||||||
daemonize("/var/run/snapclient.pid");
|
|
||||||
setpriority(PRIO_PROCESS, 0, -5);
|
|
||||||
logS(kLogNotice) << "daemon started" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
PcmDevice pcmDevice = getPcmDevice(soundcard);
|
|
||||||
if (pcmDevice.idx == -1)
|
|
||||||
{
|
|
||||||
cout << "soundcard \"" << soundcard << "\" not found\n";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!vm.count("ip"))
|
|
||||||
{
|
|
||||||
BrowseAvahi browseAvahi;
|
|
||||||
AvahiResult avahiResult;
|
|
||||||
while (!g_terminated)
|
|
||||||
{
|
{
|
||||||
if (browseAvahi.browse("_snapcast._tcp", AVAHI_PROTO_INET, avahiResult, 5000))
|
cout << "snapclient v" << VERSION << "\n"
|
||||||
|
<< "Copyright (C) 2014 BadAix (snapcast@badaix.de).\n"
|
||||||
|
<< "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
|
||||||
|
<< "This is free software: you are free to change and redistribute it.\n"
|
||||||
|
<< "There is NO WARRANTY, to the extent permitted by law.\n\n"
|
||||||
|
<< "Written by Johannes M. Pohl.\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listPcmDevices)
|
||||||
|
{
|
||||||
|
vector<PcmDevice> pcmDevices = Player::pcm_list();
|
||||||
|
for (auto dev: pcmDevices)
|
||||||
{
|
{
|
||||||
ip = avahiResult.ip_;
|
cout << dev.idx << ": " << dev.name << "\n"
|
||||||
port = avahiResult.port_;
|
<< dev.description << "\n\n";
|
||||||
std::cout << ip << ":" << port << "\n";
|
}
|
||||||
break;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
signal(SIGHUP, signal_handler);
|
||||||
|
signal(SIGTERM, signal_handler);
|
||||||
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
|
if (vm.count("daemon"))
|
||||||
|
{
|
||||||
|
daemonize("/var/run/snapclient.pid");
|
||||||
|
if (runAsDaemon < -20)
|
||||||
|
runAsDaemon = -20;
|
||||||
|
else if (runAsDaemon > 19)
|
||||||
|
runAsDaemon = 10;
|
||||||
|
setpriority(PRIO_PROCESS, 0, runAsDaemon);
|
||||||
|
logS(kLogNotice) << "daemon started" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
PcmDevice pcmDevice = getPcmDevice(soundcard);
|
||||||
|
if (pcmDevice.idx == -1)
|
||||||
|
{
|
||||||
|
cout << "soundcard \"" << soundcard << "\" not found\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!vm.count("ip"))
|
||||||
|
{
|
||||||
|
BrowseAvahi browseAvahi;
|
||||||
|
AvahiResult avahiResult;
|
||||||
|
while (!g_terminated)
|
||||||
|
{
|
||||||
|
if (browseAvahi.browse("_snapcast._tcp", AVAHI_PROTO_INET, avahiResult, 5000))
|
||||||
|
{
|
||||||
|
ip = avahiResult.ip_;
|
||||||
|
port = avahiResult.port_;
|
||||||
|
std::cout << ip << ":" << port << "\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Controller controller;
|
Controller controller;
|
||||||
if (!g_terminated)
|
if (!g_terminated)
|
||||||
|
{
|
||||||
|
controller.start(pcmDevice, ip, port, latency);
|
||||||
|
while(!g_terminated)
|
||||||
|
usleep(100*1000);
|
||||||
|
controller.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
controller.start(pcmDevice, ip, port, latency);
|
logS(kLogErr) << "Exception: " << e.what() << std::endl;
|
||||||
while(!g_terminated)
|
|
||||||
usleep(100*1000);
|
|
||||||
controller.stop();
|
|
||||||
}
|
}
|
||||||
daemonShutdown();
|
|
||||||
|
|
||||||
|
logS(kLogNotice) << "daemon terminated." << endl;
|
||||||
|
daemonShutdown();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ int main(int argc, char* argv[])
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ControlServerSettings settings;
|
ControlServerSettings settings;
|
||||||
bool runAsDaemon;
|
int runAsDaemon;
|
||||||
string sampleFormat;
|
string sampleFormat;
|
||||||
|
|
||||||
po::options_description desc("Allowed options");
|
po::options_description desc("Allowed options");
|
||||||
|
@ -58,7 +58,7 @@ int main(int argc, char* argv[])
|
||||||
("sampleformat,s", po::value<string>(&sampleFormat)->default_value("44100:16:2"), "sample format")
|
("sampleformat,s", po::value<string>(&sampleFormat)->default_value("44100:16:2"), "sample format")
|
||||||
("codec,c", po::value<string>(&settings.codec)->default_value("flac"), "transport codec [flac|ogg|pcm][:options]. Type codec:? to get codec specific options")
|
("codec,c", po::value<string>(&settings.codec)->default_value("flac"), "transport codec [flac|ogg|pcm][:options]. Type codec:? to get codec specific options")
|
||||||
("fifo,f", po::value<string>(&settings.fifoName)->default_value("/tmp/snapfifo"), "name of the input fifo file")
|
("fifo,f", po::value<string>(&settings.fifoName)->default_value("/tmp/snapfifo"), "name of the input fifo file")
|
||||||
("daemon,d", po::bool_switch(&runAsDaemon)->default_value(false), "daemonize")
|
("daemon,d", po::value<int>(&runAsDaemon)->implicit_value(-5), "daemonize, optional process priority [-20..19]")
|
||||||
("buffer,b", po::value<int32_t>(&settings.bufferMs)->default_value(1000), "buffer [ms]")
|
("buffer,b", po::value<int32_t>(&settings.bufferMs)->default_value(1000), "buffer [ms]")
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -102,10 +102,14 @@ int main(int argc, char* argv[])
|
||||||
signal(SIGTERM, signal_handler);
|
signal(SIGTERM, signal_handler);
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
if (runAsDaemon)
|
if (vm.count("daemon"))
|
||||||
{
|
{
|
||||||
daemonize("/var/run/snapserver.pid");
|
daemonize("/var/run/snapserver.pid");
|
||||||
setpriority(PRIO_PROCESS, 0, -5);
|
if (runAsDaemon < -20)
|
||||||
|
runAsDaemon = -20;
|
||||||
|
else if (runAsDaemon > 19)
|
||||||
|
runAsDaemon = 10;
|
||||||
|
setpriority(PRIO_PROCESS, 0, runAsDaemon);
|
||||||
logS(kLogNotice) << "daemon started." << endl;
|
logS(kLogNotice) << "daemon started." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +118,8 @@ int main(int argc, char* argv[])
|
||||||
services.push_back(AvahiService("_snapcast._tcp", settings.port));
|
services.push_back(AvahiService("_snapcast._tcp", settings.port));
|
||||||
publishAvahi.publish(services);
|
publishAvahi.publish(services);
|
||||||
|
|
||||||
|
if (settings.bufferMs < 400)
|
||||||
|
settings.bufferMs = 400;
|
||||||
settings.sampleFormat = sampleFormat;
|
settings.sampleFormat = sampleFormat;
|
||||||
ControlServer* controlServer = new ControlServer(settings);
|
ControlServer* controlServer = new ControlServer(settings);
|
||||||
controlServer->start();
|
controlServer->start();
|
||||||
|
@ -128,6 +134,7 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
logS(kLogNotice) << "daemon terminated." << endl;
|
logS(kLogNotice) << "daemon terminated." << endl;
|
||||||
daemonShutdown();
|
daemonShutdown();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue