PCM reader is configured by URI

This commit is contained in:
badaix 2016-01-24 13:55:05 +01:00
parent c4094a2175
commit a9015edb22
9 changed files with 108 additions and 84 deletions

View file

@ -24,6 +24,7 @@
#include "pcmReader.h"
#include "../encoder/encoderFactory.h"
#include "common/utils.h"
#include "common/compat.h"
#include "common/snapException.h"
#include "common/log.h"
@ -35,6 +36,7 @@ ReaderUri::ReaderUri(const std::string& uri)
{
// https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
// scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
// would be more elegant with regex. Not yet supported on my dev machine's gcc 4.8 :(
size_t pos;
this->uri = uri;
string tmp(uri);
@ -44,7 +46,7 @@ ReaderUri::ReaderUri(const std::string& uri)
throw invalid_argument("missing ':'");
scheme = tmp.substr(0, pos);
tmp = tmp.substr(pos + 1);
// logD << "scheme: '" << scheme << "' tmp: '" << tmp << "'\n";
logE << "scheme: '" << scheme << "' tmp: '" << tmp << "'\n";
if (tmp.find("//") != 0)
throw invalid_argument("missing host separator: '//'");
@ -54,9 +56,9 @@ ReaderUri::ReaderUri(const std::string& uri)
if (pos == string::npos)
throw invalid_argument("missing path separator: '/'");
host = tmp.substr(0, pos);
tmp = tmp.substr(pos + 1);
tmp = tmp.substr(pos);
path = tmp;
// logD << "host: '" << host << "' tmp: '" << tmp << "' path: '" << path << "'\n";
logE << "host: '" << host << "' tmp: '" << tmp << "' path: '" << path << "'\n";
pos = tmp.find('?');
if (pos == string::npos)
@ -65,7 +67,7 @@ ReaderUri::ReaderUri(const std::string& uri)
path = tmp.substr(0, pos);
tmp = tmp.substr(pos + 1);
string queryStr = tmp;
// logD << "path: '" << path << "' tmp: '" << tmp << "' query: '" << queryStr << "'\n";
logE << "path: '" << path << "' tmp: '" << tmp << "' query: '" << queryStr << "'\n";
pos = tmp.find('#');
if (pos != string::npos)
@ -73,7 +75,7 @@ ReaderUri::ReaderUri(const std::string& uri)
queryStr = tmp.substr(0, pos);
tmp = tmp.substr(pos + 1);
fragment = tmp;
// logD << "query: '" << queryStr << "' fragment: '" << fragment << "' tmp: '" << tmp << "'\n";
logE << "query: '" << queryStr << "' fragment: '" << fragment << "' tmp: '" << tmp << "'\n";
}
vector<string> keyValueList = split(queryStr, '&');
@ -89,10 +91,24 @@ ReaderUri::ReaderUri(const std::string& uri)
}
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)
PcmReader::PcmReader(PcmListener* pcmListener, const ReaderUri& uri) : pcmListener_(pcmListener), uri_(uri), pcmReadMs_(20)
{
EncoderFactory encoderFactory;
encoder_.reset(encoderFactory.createEncoder(codec));
if (uri_.query.find("codec") == uri_.query.end())
throw SnapException("Stream URI must have a codec");
encoder_.reset(encoderFactory.createEncoder(uri_.query["codec"]));
if (uri_.query.find("name") == uri_.query.end())
throw SnapException("Stream URI must have a name");
name_ = uri_.query["name"];
if (uri_.query.find("sampleformat") == uri_.query.end())
throw SnapException("Stream URI must have a sampleformat");
sampleFormat_ = SampleFormat(uri_.query["sampleformat"]);
logE << "PcmReader sampleFormat: " << sampleFormat_.getFormat() << "\n";
if (uri_.query.find("buffer_ms") != uri_.query.end())
pcmReadMs_ = cpt::stoul(uri_.query["buffer_ms"]);
}
@ -108,8 +124,27 @@ msg::Header* PcmReader::getHeader()
}
const ReaderUri& PcmReader::getUri() const
{
return uri_;
}
const std::string& PcmReader::getName() const
{
return name_;
}
const SampleFormat& PcmReader::getSampleFormat() const
{
return sampleFormat_;
}
void PcmReader::start()
{
logE << "PcmReader start: " << sampleFormat_.getFormat() << "\n";
encoder_->init(this, sampleFormat_);
active_ = true;
readerThread_ = thread(&PcmReader::worker, this);

View file

@ -74,7 +74,7 @@ class PcmReader : public EncoderListener
{
public:
/// ctor. Encoded PCM data is passed to the PcmListener
PcmReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs = 20);
PcmReader(PcmListener* pcmListener, const ReaderUri& uri);
virtual ~PcmReader();
virtual void start();
@ -84,6 +84,10 @@ public:
virtual void onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration);
virtual msg::Header* getHeader();
virtual const ReaderUri& getUri() const;
virtual const std::string& getName() const;
virtual const SampleFormat& getSampleFormat() const;
protected:
virtual void worker() = 0;
int fd_;
@ -91,9 +95,11 @@ protected:
std::atomic<bool> active_;
std::thread readerThread_;
PcmListener* pcmListener_;
msg::SampleFormat sampleFormat_;
ReaderUri uri_;
SampleFormat sampleFormat_;
size_t pcmReadMs_;
std::unique_ptr<Encoder> encoder_;
std::string name_;
};

View file

@ -19,35 +19,32 @@
#include "common/utils.h"
#include "pcmReaderFactory.h"
#include "pipeReader.h"
#include "common/log.h"
using namespace std;
PcmReader* PcmReaderFactory::createPcmReader(const std::string& uri) const
PcmReader* PcmReaderFactory::createPcmReader(PcmListener* pcmListener, const std::string& uri, const std::string& defaultSampleFormat, const std::string& defaultCodec, size_t defaultReadBufferMs)
{
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;
ReaderUri readerUri(uri);
if (readerUri.query.find("sampleformat") == readerUri.query.end())
readerUri.query["sampleformat"] = defaultSampleFormat;
if (readerUri.query.find("codec") == readerUri.query.end())
readerUri.query["codec"] = defaultCodec;
logE << "\nURI: " << readerUri.uri << "\nscheme: " << readerUri.scheme << "\nhost: "
<< readerUri.host << "\npath: " << readerUri.path << "\nfragment: " << readerUri.fragment << "\n";
for (auto kv: readerUri.query)
logE << "key: '" << kv.first << "' value: '" << kv.second << "'\n";
if (readerUri.scheme == "pipe")
return new PipeReader(pcmListener, readerUri);//, sampleFormat, codec, pcmReadMs);
return NULL;
}

View file

@ -7,7 +7,7 @@
class PcmReaderFactory
{
public:
PcmReader* createPcmReader(const std::string& uri) const;
static PcmReader* createPcmReader(PcmListener* pcmListener, const std::string& uri, const std::string& defaultSampleFormat, const std::string& defaultCodec, size_t defaultReadBufferMs = 20);
};

View file

@ -32,13 +32,13 @@ using namespace std;
PipeReader::PipeReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs) : PcmReader(pcmListener, sampleFormat, codec, fifoName, pcmReadMs)
PipeReader::PipeReader(PcmListener* pcmListener, const ReaderUri& uri) : PcmReader(pcmListener, uri)
{
umask(0);
mkfifo(fifoName.c_str(), 0666);
fd_ = open(fifoName.c_str(), O_RDONLY | O_NONBLOCK);
mkfifo(uri_.path.c_str(), 0666);
fd_ = open(uri_.path.c_str(), O_RDONLY | O_NONBLOCK);
if (fd_ == -1)
throw SnapException("failed to open fifo: \"" + fifoName + "\"");
throw SnapException("failed to open fifo: \"" + uri_.path + "\"");
}

View file

@ -33,7 +33,7 @@ class PipeReader : public PcmReader
{
public:
/// ctor. Encoded PCM data is passed to the PipeListener
PipeReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs = 20);
PipeReader(PcmListener* pcmListener, const ReaderUri& uri);
virtual ~PipeReader();
protected: