mirror of
https://github.com/badaix/snapcast.git
synced 2025-04-29 10:17:16 +02:00
added pcmReaderFactory
This commit is contained in:
parent
48627f2ec0
commit
b46b5efde4
10 changed files with 172 additions and 7 deletions
|
@ -24,6 +24,7 @@
|
|||
#include <cctype>
|
||||
#include <locale>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
|
|
@ -13,7 +13,7 @@ CXX = /usr/bin/g++
|
|||
CFLAGS = -std=c++0x -Wall -Wno-unused-function -O3 -pthread -DASIO_STANDALONE -DVERSION=\"$(VERSION)\" -I.. -I../externals/asio/asio/include -I../externals/popl/include
|
||||
LDFLAGS = -lrt -lvorbis -lvorbisenc -logg -lFLAC -lavahi-client -lavahi-common
|
||||
|
||||
OBJ = snapServer.o config.o controlServer.o controlSession.o streamServer.o streamSession.o json/jsonrpc.o pcmreader/pcmReader.o pcmreader/pipeReader.o encoder/encoderFactory.o encoder/flacEncoder.o encoder/pcmEncoder.o encoder/oggEncoder.o publishAvahi.o ../common/log.o ../message/pcmChunk.o ../message/sampleFormat.o
|
||||
OBJ = snapServer.o config.o controlServer.o controlSession.o streamServer.o streamSession.o json/jsonrpc.o pcmreader/pcmReaderFactory.o pcmreader/pcmReader.o pcmreader/pipeReader.o encoder/encoderFactory.o encoder/flacEncoder.o encoder/pcmEncoder.o encoder/oggEncoder.o publishAvahi.o ../common/log.o ../message/pcmChunk.o ../message/sampleFormat.o
|
||||
BIN = snapserver
|
||||
|
||||
all: $(TARGET)
|
||||
|
|
|
@ -23,13 +23,70 @@
|
|||
|
||||
#include "pcmReader.h"
|
||||
#include "../encoder/encoderFactory.h"
|
||||
#include "common/log.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/snapException.h"
|
||||
#include "common/log.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
ReaderUri::ReaderUri(const std::string& uri)
|
||||
{
|
||||
// https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
|
||||
// scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
|
||||
size_t pos;
|
||||
this->uri = uri;
|
||||
string tmp(uri);
|
||||
|
||||
pos = tmp.find(':');
|
||||
if (pos == string::npos)
|
||||
throw invalid_argument("missing ':'");
|
||||
scheme = tmp.substr(0, pos);
|
||||
tmp = tmp.substr(pos + 1);
|
||||
// logD << "scheme: '" << scheme << "' tmp: '" << tmp << "'\n";
|
||||
|
||||
if (tmp.find("//") != 0)
|
||||
throw invalid_argument("missing host separator: '//'");
|
||||
tmp = tmp.substr(2);
|
||||
|
||||
pos = tmp.find('/');
|
||||
if (pos == string::npos)
|
||||
throw invalid_argument("missing path separator: '/'");
|
||||
host = tmp.substr(0, pos);
|
||||
tmp = tmp.substr(pos + 1);
|
||||
path = tmp;
|
||||
// logD << "host: '" << host << "' tmp: '" << tmp << "' path: '" << path << "'\n";
|
||||
|
||||
pos = tmp.find('?');
|
||||
if (pos == string::npos)
|
||||
return;
|
||||
|
||||
path = tmp.substr(0, pos);
|
||||
tmp = tmp.substr(pos + 1);
|
||||
string queryStr = tmp;
|
||||
// logD << "path: '" << path << "' tmp: '" << tmp << "' query: '" << queryStr << "'\n";
|
||||
|
||||
pos = tmp.find('#');
|
||||
if (pos != string::npos)
|
||||
{
|
||||
queryStr = tmp.substr(0, pos);
|
||||
tmp = tmp.substr(pos + 1);
|
||||
fragment = tmp;
|
||||
// logD << "query: '" << queryStr << "' fragment: '" << fragment << "' tmp: '" << tmp << "'\n";
|
||||
}
|
||||
|
||||
vector<string> keyValueList = split(queryStr, '&');
|
||||
for (auto& kv: keyValueList)
|
||||
{
|
||||
pos = kv.find('=');
|
||||
if (pos != string::npos)
|
||||
query[kv.substr(0, pos)] = kv.substr(pos+1);
|
||||
}
|
||||
|
||||
// for (auto& kv: query)
|
||||
// logD << "key: '" << kv.first << "' value: '" << kv.second << "'\n";
|
||||
}
|
||||
|
||||
|
||||
PcmReader::PcmReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs) : pcmListener_(pcmListener), sampleFormat_(sampleFormat), pcmReadMs_(pcmReadMs)
|
||||
|
@ -42,7 +99,6 @@ PcmReader::PcmReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFo
|
|||
PcmReader::~PcmReader()
|
||||
{
|
||||
stop();
|
||||
close(fd_);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "../encoder/encoder.h"
|
||||
#include "message/sampleFormat.h"
|
||||
#include "message/header.h"
|
||||
|
@ -42,6 +43,26 @@ public:
|
|||
};
|
||||
|
||||
|
||||
struct ReaderUri
|
||||
{
|
||||
ReaderUri(const std::string& uri);
|
||||
std::string uri;
|
||||
std::string scheme;
|
||||
/* struct Authority
|
||||
{
|
||||
std::string username;
|
||||
std::string password;
|
||||
std::string host;
|
||||
size_t port;
|
||||
};
|
||||
Authority authority;
|
||||
*/
|
||||
std::string host;
|
||||
std::string path;
|
||||
std::map<std::string, std::string> query;
|
||||
std::string fragment;
|
||||
};
|
||||
|
||||
|
||||
/// Reads and decodes PCM data
|
||||
/**
|
||||
|
|
54
server/pcmreader/pcmReaderFactory.cpp
Normal file
54
server/pcmreader/pcmReaderFactory.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2015 Johannes Pohl
|
||||
|
||||
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 3 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.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include "common/utils.h"
|
||||
#include "pcmReaderFactory.h"
|
||||
#include "pipeReader.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
PcmReader* PcmReaderFactory::createPcmReader(const std::string& uri) const
|
||||
{
|
||||
PcmReader* pcmReader = NULL;
|
||||
/*
|
||||
std::string codec(codecSettings);
|
||||
std::string codecOptions;
|
||||
if (codec.find(":") != std::string::npos)
|
||||
{
|
||||
codecOptions = trim_copy(codec.substr(codec.find(":") + 1));
|
||||
codec = trim_copy(codec.substr(0, codec.find(":")));
|
||||
}
|
||||
if (codec == "ogg")
|
||||
encoder = new OggEncoder(codecOptions);
|
||||
else if (codec == "pcm")
|
||||
encoder = new PcmEncoder(codecOptions);
|
||||
else if (codec == "flac")
|
||||
encoder = new FlacEncoder(codecOptions);
|
||||
else
|
||||
{
|
||||
cout << "unknown codec: " << codec << "\n";
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
return pcmReader;
|
||||
}
|
||||
|
||||
|
||||
|
14
server/pcmreader/pcmReaderFactory.h
Normal file
14
server/pcmreader/pcmReaderFactory.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef PCM_READER_FACTORY_H
|
||||
#define PCM_READER_FACTORY_H
|
||||
|
||||
#include <string>
|
||||
#include "pcmReader.h"
|
||||
|
||||
class PcmReaderFactory
|
||||
{
|
||||
public:
|
||||
PcmReader* createPcmReader(const std::string& uri) const;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -44,6 +44,7 @@ PipeReader::PipeReader(PcmListener* pcmListener, const msg::SampleFormat& sample
|
|||
|
||||
PipeReader::~PipeReader()
|
||||
{
|
||||
close(fd_);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ int main(int argc, char* argv[])
|
|||
try
|
||||
{
|
||||
StreamServerSettings settings;
|
||||
std::string pcmStream = "pipe:///tmp/snapfifo";
|
||||
int processPriority(-3);
|
||||
|
||||
Switch helpSwitch("h", "help", "produce help message");
|
||||
|
@ -54,7 +55,7 @@ int main(int argc, char* argv[])
|
|||
Value<size_t> controlPortValue("", "controlPort", "Remote control port", settings.controlPort, &settings.controlPort);
|
||||
Value<string> sampleFormatValue("s", "sampleformat", "sample format", settings.sampleFormat.getFormat());
|
||||
Value<string> codecValue("c", "codec", "transport codec [flac|ogg|pcm][:options]\nType codec:? to get codec specific options", settings.codec, &settings.codec);
|
||||
Value<string> fifoValue("f", "fifo", "name of the input fifo file", settings.fifoName, &settings.fifoName);
|
||||
Value<string> fifoValue("f", "fifo", "name of the input fifo file", pcmStream, &pcmStream);
|
||||
Implicit<int> daemonOption("d", "daemon", "daemonize\noptional process priority [-20..19]", 0, &processPriority);
|
||||
Value<int> bufferValue("b", "buffer", "buffer [ms]", settings.bufferMs, &settings.bufferMs);
|
||||
Value<size_t> pipeBufferValue("", "pipeReadBuffer", "pipe read buffer [ms]", settings.pipeReadMs, &settings.pipeReadMs);
|
||||
|
@ -99,6 +100,15 @@ int main(int argc, char* argv[])
|
|||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (!fifoValue.isSet())
|
||||
settings.pcmStreams.push_back(fifoValue.getValue());
|
||||
|
||||
for (size_t n=0; n<fifoValue.count(); ++n)
|
||||
{
|
||||
cout << fifoValue.getValue(n) << "\n";
|
||||
settings.pcmStreams.push_back(fifoValue.getValue(n));
|
||||
}
|
||||
|
||||
if (settings.codec.find(":?") != string::npos)
|
||||
{
|
||||
EncoderFactory encoderFactory;
|
||||
|
|
|
@ -349,10 +349,19 @@ void StreamServer::handleAccept(socket_ptr socket)
|
|||
|
||||
void StreamServer::start()
|
||||
{
|
||||
for (auto& s: settings_.pcmStreams)
|
||||
{
|
||||
ReaderUri uri(s);
|
||||
logO << "URI: " << uri.uri << "\nscheme: " << uri.scheme << "\nhost: " << uri.host << "\npath: " << uri.path << "\nfragment: " << uri.fragment << "\n";
|
||||
for (auto kv: uri.query)
|
||||
logD << "key: '" << kv.first << "' value: '" << kv.second << "'\n";
|
||||
}
|
||||
|
||||
controlServer_.reset(new ControlServer(io_service_, settings_.controlPort, this));
|
||||
controlServer_->start();
|
||||
|
||||
pcmReader_ .reset(new PipeReader(this, settings_.sampleFormat, settings_.codec, settings_.fifoName, settings_.pipeReadMs));
|
||||
settings_.pcmStreams[0] = "/tmp/snapfifo";
|
||||
pcmReader_.reset(new PipeReader(this, settings_.sampleFormat, settings_.codec, settings_.pcmStreams[0], settings_.pipeReadMs));
|
||||
pcmReader_->start();
|
||||
acceptor_ = make_shared<tcp::acceptor>(*io_service_, tcp::endpoint(tcp::v4(), settings_.port));
|
||||
startAccept();
|
||||
|
|
|
@ -46,7 +46,6 @@ struct StreamServerSettings
|
|||
StreamServerSettings() :
|
||||
port(1704),
|
||||
controlPort(1705),
|
||||
fifoName("/tmp/snapfifo"),
|
||||
codec("flac"),
|
||||
bufferMs(1000),
|
||||
sampleFormat("48000:16:2"),
|
||||
|
@ -55,7 +54,7 @@ struct StreamServerSettings
|
|||
}
|
||||
size_t port;
|
||||
size_t controlPort;
|
||||
std::string fifoName;
|
||||
std::vector<std::string> pcmStreams;
|
||||
std::string codec;
|
||||
int32_t bufferMs;
|
||||
msg::SampleFormat sampleFormat;
|
||||
|
|
Loading…
Add table
Reference in a new issue