git-svn-id: svn://elaine/murooma/trunk@318 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-12-24 08:42:19 +00:00
parent 83eed0be78
commit bccffc7432
4 changed files with 37 additions and 11 deletions

View file

@ -60,7 +60,10 @@ void Controller::start(const PcmDevice& pcmDevice, const std::string& _ip, size_
void Controller::stop() void Controller::stop()
{ {
cout << "Stopping\n";
active_ = false; active_ = false;
controllerThread->join();
clientConnection->stop();
} }
@ -77,16 +80,16 @@ void Controller::worker()
clientConnection->start(); clientConnection->start();
RequestMsg requestMsg(serversettings); RequestMsg requestMsg(serversettings);
shared_ptr<ServerSettings> serverSettings(NULL); shared_ptr<ServerSettings> serverSettings(NULL);
while (!(serverSettings = clientConnection->sendReq<ServerSettings>(&requestMsg))); while (active_ && !(serverSettings = clientConnection->sendReq<ServerSettings>(&requestMsg)));
cout << "ServerSettings buffer: " << serverSettings->bufferMs << "\n"; cout << "ServerSettings buffer: " << serverSettings->bufferMs << "\n";
requestMsg.request = sampleformat; requestMsg.request = sampleformat;
while (!(sampleFormat = clientConnection->sendReq<SampleFormat>(&requestMsg))); while (active_ && !(sampleFormat = clientConnection->sendReq<SampleFormat>(&requestMsg)));
cout << "SampleFormat rate: " << sampleFormat->rate << ", bits: " << sampleFormat->bits << ", channels: " << sampleFormat->channels << "\n"; cout << "SampleFormat rate: " << sampleFormat->rate << ", bits: " << sampleFormat->bits << ", channels: " << sampleFormat->channels << "\n";
requestMsg.request = header; requestMsg.request = header;
shared_ptr<HeaderMessage> headerChunk(NULL); shared_ptr<HeaderMessage> headerChunk(NULL);
while (!(headerChunk = clientConnection->sendReq<HeaderMessage>(&requestMsg))); while (active_ && !(headerChunk = clientConnection->sendReq<HeaderMessage>(&requestMsg)));
cout << "Codec: " << headerChunk->codec << "\n"; cout << "Codec: " << headerChunk->codec << "\n";
if (headerChunk->codec == "ogg") if (headerChunk->codec == "ogg")
decoder = new OggDecoder(); decoder = new OggDecoder();
@ -95,7 +98,7 @@ void Controller::worker()
decoder->setHeader(headerChunk.get()); decoder->setHeader(headerChunk.get());
RequestMsg timeReq(timemsg); RequestMsg timeReq(timemsg);
for (size_t n=0; n<50; ++n) for (size_t n=0; n<50 && active_; ++n)
{ {
shared_ptr<TimeMsg> reply = clientConnection->sendReq<TimeMsg>(&timeReq, chronos::msec(2000)); shared_ptr<TimeMsg> reply = clientConnection->sendReq<TimeMsg>(&timeReq, chronos::msec(2000));
if (reply) if (reply)
@ -115,7 +118,7 @@ void Controller::worker()
CommandMsg startStream("startStream"); CommandMsg startStream("startStream");
shared_ptr<AckMsg> ackMsg(NULL); shared_ptr<AckMsg> ackMsg(NULL);
while (!(ackMsg = clientConnection->sendReq<AckMsg>(&startStream))); while (active_ && !(ackMsg = clientConnection->sendReq<AckMsg>(&startStream)));
try try
{ {
@ -152,9 +155,11 @@ void Controller::worker()
cout << "Stopping clientConnection\n"; cout << "Stopping clientConnection\n";
clientConnection->stop(); clientConnection->stop();
cout << "done\n"; cout << "done\n";
if (active_)
usleep(1000000); usleep(1000000);
} }
} }
cout << "Thread stopped\n";
} }

View file

@ -11,6 +11,7 @@
#include "common/utils.h" #include "common/utils.h"
#include "common/log.h" #include "common/log.h"
#include "common/signalHandler.h"
#include "controller.h" #include "controller.h"
#include "alsaPlayer.h" #include "alsaPlayer.h"
@ -18,6 +19,8 @@
using namespace std; using namespace std;
namespace po = boost::program_options; namespace po = boost::program_options;
//bool g_terminated = false;
std::condition_variable terminateSignaled;
PcmDevice getPcmDevice(const std::string& soundcard) PcmDevice getPcmDevice(const std::string& soundcard)
{ {
@ -87,6 +90,10 @@ int main (int argc, char *argv[])
return 1; return 1;
} }
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
std::clog.rdbuf(new Log("snapclient", LOG_DAEMON)); std::clog.rdbuf(new Log("snapclient", LOG_DAEMON));
if (runAsDaemon) if (runAsDaemon)
{ {
@ -105,8 +112,11 @@ int main (int argc, char *argv[])
Controller controller; Controller controller;
controller.start(pcmDevice, ip, port, latency); controller.start(pcmDevice, ip, port, latency);
while(true)
usleep(10000); std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
terminateSignaled.wait(lck);
controller.stop();
return 0; return 0;
} }

View file

@ -3,8 +3,9 @@
#include <signal.h> #include <signal.h>
#include <syslog.h> #include <syslog.h>
#include <condition_variable>
extern bool g_terminated; extern std::condition_variable terminateSignaled;
void signal_handler(int sig) void signal_handler(int sig)
{ {
@ -16,7 +17,11 @@ void signal_handler(int sig)
break; break;
case SIGTERM: case SIGTERM:
syslog(LOG_WARNING, "Received SIGTERM signal."); syslog(LOG_WARNING, "Received SIGTERM signal.");
g_terminated = true; terminateSignaled.notify_all();
break;
case SIGINT:
syslog(LOG_WARNING, "Received SIGINT signal.");
terminateSignaled.notify_all();
break; break;
default: default:
syslog(LOG_WARNING, "Unhandled signal "); syslog(LOG_WARNING, "Unhandled signal ");

View file

@ -8,7 +8,7 @@
#include "message/message.h" #include "message/message.h"
#include "pcmEncoder.h" #include "pcmEncoder.h"
#include "oggEncoder.h" #include "oggEncoder.h"
#include "controlServer.h" c#include "controlServer.h"
bool g_terminated = false; bool g_terminated = false;
@ -18,6 +18,7 @@ namespace po = boost::program_options;
using namespace std; using namespace std;
std::condition_variable terminateSignaled;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -76,6 +77,11 @@ int main(int argc, char* argv[])
encoder.reset(new OggEncoder(sampleFormat)); encoder.reset(new OggEncoder(sampleFormat));
else if (codec == "pcm") else if (codec == "pcm")
encoder.reset(new PcmEncoder(sampleFormat)); encoder.reset(new PcmEncoder(sampleFormat));
else if (codec == "flac")
{
cout << "Not yet supported\n";
return 1;
}
else else
{ {
cout << "unknown codec: " << codec << "\n"; cout << "unknown codec: " << codec << "\n";