mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-15 10:06:43 +02:00
requests are enum
git-svn-id: svn://elaine/murooma/trunk@304 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
e5f064a01f
commit
6fbabaae82
10 changed files with 80 additions and 48 deletions
|
@ -3,6 +3,7 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "common/log.h"
|
#include "common/log.h"
|
||||||
#include "clientConnection.h"
|
#include "clientConnection.h"
|
||||||
|
#include "common/utils.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,6 +55,7 @@ void ClientConnection::start()
|
||||||
// setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
// setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||||
// setsockopt(socket->native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
// setsockopt(socket->native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||||
socket->connect(*iterator);
|
socket->connect(*iterator);
|
||||||
|
cout << "MAC: \"" << getMacAddress(socket->native()) << "\"\n";
|
||||||
connected_ = true;
|
connected_ = true;
|
||||||
cout << "connected\n";
|
cout << "connected\n";
|
||||||
std::clog << kLogNotice << "connected\n";// to " << ip << ":" << port << std::endl;
|
std::clog << kLogNotice << "connected\n";// to " << ip << ":" << port << std::endl;
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include "message/timeMsg.h"
|
#include "message/timeMsg.h"
|
||||||
#include "message/requestMsg.h"
|
#include "message/requestMsg.h"
|
||||||
#include "message/ackMsg.h"
|
#include "message/ackMsg.h"
|
||||||
|
#include "message/commandMsg.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -73,16 +74,16 @@ void Controller::worker()
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
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, 1000)));
|
while (!(serverSettings = clientConnection->sendReq<ServerSettings>(&requestMsg, 1000)));
|
||||||
cout << "ServerSettings buffer: " << serverSettings->bufferMs << "\n";
|
cout << "ServerSettings buffer: " << serverSettings->bufferMs << "\n";
|
||||||
|
|
||||||
requestMsg.request = "sampleFormat";
|
requestMsg.request = sampleformat;
|
||||||
while (!(sampleFormat = clientConnection->sendReq<SampleFormat>(&requestMsg, 1000)));
|
while (!(sampleFormat = clientConnection->sendReq<SampleFormat>(&requestMsg, 1000)));
|
||||||
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 = "headerChunk";
|
requestMsg.request = header;
|
||||||
shared_ptr<HeaderMessage> headerChunk(NULL);
|
shared_ptr<HeaderMessage> headerChunk(NULL);
|
||||||
while (!(headerChunk = clientConnection->sendReq<HeaderMessage>(&requestMsg, 1000)));
|
while (!(headerChunk = clientConnection->sendReq<HeaderMessage>(&requestMsg, 1000)));
|
||||||
cout << "Codec: " << headerChunk->codec << "\n";
|
cout << "Codec: " << headerChunk->codec << "\n";
|
||||||
|
@ -92,7 +93,7 @@ void Controller::worker()
|
||||||
decoder = new PcmDecoder();
|
decoder = new PcmDecoder();
|
||||||
decoder->setHeader(headerChunk.get());
|
decoder->setHeader(headerChunk.get());
|
||||||
|
|
||||||
RequestMsg timeReq("time");
|
RequestMsg timeReq(timemsg);
|
||||||
for (size_t n=0; n<50; ++n)
|
for (size_t n=0; n<50; ++n)
|
||||||
{
|
{
|
||||||
shared_ptr<TimeMsg> reply = clientConnection->sendReq<TimeMsg>(&timeReq, 2000);
|
shared_ptr<TimeMsg> reply = clientConnection->sendReq<TimeMsg>(&timeReq, 2000);
|
||||||
|
@ -111,7 +112,7 @@ void Controller::worker()
|
||||||
Player player(pcmDevice_, stream);
|
Player player(pcmDevice_, stream);
|
||||||
player.start();
|
player.start();
|
||||||
|
|
||||||
RequestMsg startStream("startStream");
|
CommandMsg startStream("startStream");
|
||||||
shared_ptr<AckMsg> ackMsg(NULL);
|
shared_ptr<AckMsg> ackMsg(NULL);
|
||||||
while (!(ackMsg = clientConnection->sendReq<AckMsg>(&startStream, 1000)));
|
while (!(ackMsg = clientConnection->sendReq<AckMsg>(&startStream, 1000)));
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
class PcmDevice
|
struct PcmDevice
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
PcmDevice() : idx(-1){};
|
PcmDevice() : idx(-1){};
|
||||||
int idx;
|
int idx;
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
|
@ -52,17 +52,16 @@ int main (int argc, char *argv[])
|
||||||
size_t port;
|
size_t port;
|
||||||
bool runAsDaemon;
|
bool runAsDaemon;
|
||||||
bool listPcmDevices;
|
bool listPcmDevices;
|
||||||
// string sampleFormat;
|
|
||||||
po::options_description desc("Allowed options");
|
po::options_description desc("Allowed options");
|
||||||
desc.add_options()
|
desc.add_options()
|
||||||
("help,h", "produce help message")
|
("help,h", "produce help message")
|
||||||
("port,p", po::value<size_t>(&port)->default_value(98765), "port where the server listens on")
|
("list,l", po::bool_switch(&listPcmDevices)->default_value(false), "list pcm devices")
|
||||||
("ip,i", po::value<string>(&ip)->default_value("192.168.0.2"), "server IP")
|
("ip,i", po::value<string>(&ip)->default_value("192.168.0.2"), "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")
|
("soundcard,s", po::value<string>(&soundcard)->default_value("default"), "index or name of the soundcard")
|
||||||
// ("sampleformat,f", po::value<string>(&sampleFormat)->default_value("48000:16:2"), "sample format")
|
|
||||||
// ("buffer,b", po::value<int>(&bufferMs)->default_value(300), "buffer size [ms]")
|
// ("buffer,b", po::value<int>(&bufferMs)->default_value(300), "buffer size [ms]")
|
||||||
("daemon,d", po::bool_switch(&runAsDaemon)->default_value(false), "daemonize")
|
("daemon,d", po::bool_switch(&runAsDaemon)->default_value(false), "daemonize")
|
||||||
("list,l", po::bool_switch(&listPcmDevices)->default_value(false), "list pcm devices")
|
|
||||||
;
|
;
|
||||||
|
|
||||||
po::variables_map vm;
|
po::variables_map vm;
|
||||||
|
|
|
@ -9,8 +9,12 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
// trim from start
|
// trim from start
|
||||||
static inline std::string <rim(std::string &s)
|
static inline std::string <rim(std::string &s)
|
||||||
|
@ -33,22 +37,52 @@ static inline std::string &trim(std::string &s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static std::string getMacAddress()
|
static std::string getMacAddress(int sock)
|
||||||
{
|
{
|
||||||
std::ifstream t("/sys/class/net/eth0/address");
|
struct ifreq ifr;
|
||||||
|
struct ifconf ifc;
|
||||||
|
char buf[1024];
|
||||||
|
int success = 0;
|
||||||
|
|
||||||
|
if (sock < 0)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
ifc.ifc_len = sizeof(buf);
|
||||||
|
ifc.ifc_buf = buf;
|
||||||
|
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
struct ifreq* it = ifc.ifc_req;
|
||||||
|
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
|
||||||
|
|
||||||
|
for (; it != end; ++it) {
|
||||||
|
strcpy(ifr.ifr_name, it->ifr_name);
|
||||||
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
|
||||||
|
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
|
||||||
|
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
|
||||||
|
success = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { /* handle error */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
char mac[19];
|
||||||
|
sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
|
(unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2],
|
||||||
|
(unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
|
||||||
|
return mac;
|
||||||
|
}
|
||||||
|
/* std::ifstream t("/sys/class/net/eth0/address");
|
||||||
std::string str((std::istreambuf_iterator<char>(t)),
|
std::string str((std::istreambuf_iterator<char>(t)),
|
||||||
std::istreambuf_iterator<char>());
|
std::istreambuf_iterator<char>());
|
||||||
return trim(str);
|
return trim(str);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
std::vector<std::string> split(const std::string& str)
|
|
||||||
{
|
|
||||||
std::istringstream iss(str);
|
|
||||||
std::vector<std::string> splitStr;
|
|
||||||
std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter<std::vector<std::string> >(splitStr));
|
|
||||||
return splitStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void daemonize()
|
static void daemonize()
|
||||||
|
|
|
@ -37,7 +37,8 @@ enum message_type
|
||||||
serversettings = 4,
|
serversettings = 4,
|
||||||
timemsg = 5,
|
timemsg = 5,
|
||||||
requestmsg = 6,
|
requestmsg = 6,
|
||||||
ackMsg = 7
|
ackMsg = 7,
|
||||||
|
commandmsg = 8
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,11 @@
|
||||||
class RequestMsg : public BaseMessage
|
class RequestMsg : public BaseMessage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RequestMsg() : BaseMessage(message_type::requestmsg), request("")
|
RequestMsg() : BaseMessage(message_type::requestmsg), request(base)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestMsg(const std::string& _request) : BaseMessage(message_type::requestmsg), request(_request)
|
RequestMsg(message_type _request) : BaseMessage(message_type::requestmsg), request(_request)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,25 +22,20 @@ public:
|
||||||
|
|
||||||
virtual void read(std::istream& stream)
|
virtual void read(std::istream& stream)
|
||||||
{
|
{
|
||||||
int16_t size;
|
stream.read(reinterpret_cast<char *>(&request), sizeof(int16_t));
|
||||||
stream.read(reinterpret_cast<char *>(&size), sizeof(int16_t));
|
|
||||||
request.resize(size);
|
|
||||||
stream.read(&request[0], size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual uint32_t getSize()
|
virtual uint32_t getSize()
|
||||||
{
|
{
|
||||||
return sizeof(int16_t) + request.size();
|
return sizeof(int16_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string request;
|
message_type request;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void doserialize(std::ostream& stream)
|
virtual void doserialize(std::ostream& stream)
|
||||||
{
|
{
|
||||||
int16_t size(request.size());
|
stream.write(reinterpret_cast<char *>(&request), sizeof(int16_t));
|
||||||
stream.write(reinterpret_cast<char *>(&size), sizeof(int16_t));
|
|
||||||
stream.write(request.c_str(), size);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include "message/timeMsg.h"
|
#include "message/timeMsg.h"
|
||||||
#include "message/ackMsg.h"
|
#include "message/ackMsg.h"
|
||||||
#include "message/requestMsg.h"
|
#include "message/requestMsg.h"
|
||||||
|
#include "message/commandMsg.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,7 +33,6 @@ void ControlServer::send(shared_ptr<BaseMessage> message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void ControlServer::onMessageReceived(ServerSession* connection, const BaseMessage& baseMessage, char* buffer)
|
void ControlServer::onMessageReceived(ServerSession* connection, const BaseMessage& baseMessage, char* buffer)
|
||||||
{
|
{
|
||||||
// cout << "onMessageReceived: " << baseMessage.type << ", size: " << baseMessage.size << ", sent: " << baseMessage.sent.sec << "," << baseMessage.sent.usec << ", recv: " << baseMessage.received.sec << "," << baseMessage.received.usec << "\n";
|
// cout << "onMessageReceived: " << baseMessage.type << ", size: " << baseMessage.size << ", sent: " << baseMessage.sent.sec << "," << baseMessage.sent.usec << ", recv: " << baseMessage.received.sec << "," << baseMessage.received.usec << "\n";
|
||||||
|
@ -41,7 +41,7 @@ void ControlServer::onMessageReceived(ServerSession* connection, const BaseMessa
|
||||||
RequestMsg requestMsg;
|
RequestMsg requestMsg;
|
||||||
requestMsg.deserialize(baseMessage, buffer);
|
requestMsg.deserialize(baseMessage, buffer);
|
||||||
// cout << "request: " << requestMsg.request << "\n";
|
// cout << "request: " << requestMsg.request << "\n";
|
||||||
if (requestMsg.request == "time")
|
if (requestMsg.request == timemsg)
|
||||||
{
|
{
|
||||||
// timeMsg.latency = (timeMsg.received.sec - timeMsg.sent.sec) * 1000000 + (timeMsg.received.usec - timeMsg.sent.usec);
|
// timeMsg.latency = (timeMsg.received.sec - timeMsg.sent.sec) * 1000000 + (timeMsg.received.usec - timeMsg.sent.usec);
|
||||||
TimeMsg timeMsg;
|
TimeMsg timeMsg;
|
||||||
|
@ -51,25 +51,30 @@ void ControlServer::onMessageReceived(ServerSession* connection, const BaseMessa
|
||||||
// cout << "Latency: " << diff.sec << "." << diff.usec << "\n";
|
// cout << "Latency: " << diff.sec << "." << diff.usec << "\n";
|
||||||
connection->send(&timeMsg);
|
connection->send(&timeMsg);
|
||||||
}
|
}
|
||||||
else if (requestMsg.request == "serverSettings")
|
else if (requestMsg.request == serversettings)
|
||||||
{
|
{
|
||||||
serverSettings->refersTo = requestMsg.id;
|
serverSettings->refersTo = requestMsg.id;
|
||||||
connection->send(serverSettings);
|
connection->send(serverSettings);
|
||||||
}
|
}
|
||||||
else if (requestMsg.request == "sampleFormat")
|
else if (requestMsg.request == sampleformat)
|
||||||
{
|
{
|
||||||
sampleFormat->refersTo = requestMsg.id;
|
sampleFormat->refersTo = requestMsg.id;
|
||||||
connection->send(sampleFormat);
|
connection->send(sampleFormat);
|
||||||
}
|
}
|
||||||
else if (requestMsg.request == "headerChunk")
|
else if (requestMsg.request == header)
|
||||||
{
|
{
|
||||||
headerChunk->refersTo = requestMsg.id;
|
headerChunk->refersTo = requestMsg.id;
|
||||||
connection->send(headerChunk);
|
connection->send(headerChunk);
|
||||||
}
|
}
|
||||||
else if (requestMsg.request == "startStream")
|
}
|
||||||
|
else if (baseMessage.type == message_type::commandmsg)
|
||||||
|
{
|
||||||
|
CommandMsg commandMsg;
|
||||||
|
commandMsg.deserialize(baseMessage, buffer);
|
||||||
|
if (commandMsg.command == "startStream")
|
||||||
{
|
{
|
||||||
AckMsg ackMsg;
|
AckMsg ackMsg;
|
||||||
ackMsg.refersTo = requestMsg.id;
|
ackMsg.refersTo = commandMsg.id;
|
||||||
connection->send(&ackMsg);
|
connection->send(&ackMsg);
|
||||||
connection->setStreamActive(true);
|
connection->setStreamActive(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,6 @@ PcmEncoder::PcmEncoder(const SampleFormat& format) : Encoder(format)
|
||||||
|
|
||||||
double PcmEncoder::encode(PcmChunk* chunk)
|
double PcmEncoder::encode(PcmChunk* chunk)
|
||||||
{
|
{
|
||||||
/* WireChunk* wireChunk = chunk->wireChunk;
|
|
||||||
for (size_t n=0; n<wireChunk->length; ++n)
|
|
||||||
wireChunk->payload[n] *= 1;
|
|
||||||
*/
|
|
||||||
return chunk->duration<chronos::msec>().count();
|
return chunk->duration<chronos::msec>().count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,10 +34,10 @@ int main(int argc, char* argv[])
|
||||||
po::options_description desc("Allowed options");
|
po::options_description desc("Allowed options");
|
||||||
desc.add_options()
|
desc.add_options()
|
||||||
("help,h", "produce help message")
|
("help,h", "produce help message")
|
||||||
("port,p", po::value<size_t>(&port)->default_value(98765), "port to listen on")
|
("port,p", po::value<size_t>(&port)->default_value(98765), "server port")
|
||||||
("sampleformat,s", po::value<string>(&sampleFormat)->default_value("48000:16:2"), "sample format")
|
("sampleformat,s", po::value<string>(&sampleFormat)->default_value("48000:16:2"), "sample format")
|
||||||
("codec,c", po::value<string>(&codec)->default_value("ogg"), "transport codec [ogg|pcm]")
|
("codec,c", po::value<string>(&codec)->default_value("ogg"), "transport codec [ogg|pcm]")
|
||||||
("fifo,f", po::value<string>(&fifoName)->default_value("/tmp/snapfifo"), "name of fifo file")
|
("fifo,f", po::value<string>(&fifoName)->default_value("/tmp/snapfifo"), "name of the input fifo file")
|
||||||
("daemon,d", po::bool_switch(&runAsDaemon)->default_value(false), "daemonize")
|
("daemon,d", po::bool_switch(&runAsDaemon)->default_value(false), "daemonize")
|
||||||
("buffer,b", po::value<int32_t>(&bufferMs)->default_value(500), "buffer [ms]")
|
("buffer,b", po::value<int32_t>(&bufferMs)->default_value(500), "buffer [ms]")
|
||||||
;
|
;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue