Force unique stream name, use name as id

This commit is contained in:
badaix 2016-11-09 10:25:26 +01:00
parent d76aac0de7
commit 341466ceb5
7 changed files with 32 additions and 39 deletions

View file

@ -294,10 +294,10 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
// Assign and update stream // Assign and update stream
PcmStreamPtr stream = streamManager_->getStream(client->config.streamId); PcmStreamPtr stream = streamManager_->getStream(client->config.streamId);
if (stream == nullptr) if (!stream)
{ {
stream = streamManager_->getDefaultStream(); stream = streamManager_->getDefaultStream();
client->config.streamId = stream->getUri().id(); client->config.streamId = stream->getId();
} }
Config::instance().save(); Config::instance().save();
@ -384,8 +384,8 @@ void StreamServer::start()
//TODO: check uniqueness of the stream //TODO: check uniqueness of the stream
for (const auto& streamUri: settings_.pcmStreams) for (const auto& streamUri: settings_.pcmStreams)
{ {
PcmStream* stream = streamManager_->addStream(streamUri); PcmStreamPtr stream = streamManager_->addStream(streamUri);
if (stream != NULL) if (stream)
logO << "Stream: " << stream->getUri().toJson() << "\n"; logO << "Stream: " << stream->getUri().toJson() << "\n";
} }
streamManager_->start(); streamManager_->start();

View file

@ -77,6 +77,12 @@ const std::string& PcmStream::getName() const
} }
const std::string& PcmStream::getId() const
{
return getName();
}
const SampleFormat& PcmStream::getSampleFormat() const const SampleFormat& PcmStream::getSampleFormat() const
{ {
return sampleFormat_; return sampleFormat_;
@ -154,7 +160,7 @@ json PcmStream::toJson() const
json j = { json j = {
{"uri", uri_.toJson()}, {"uri", uri_.toJson()},
{"id", uri_.id()}, {"id", getId()},
{"status", state} {"status", state}
}; };
return j; return j;

View file

@ -78,6 +78,7 @@ public:
virtual const StreamUri& getUri() const; virtual const StreamUri& getUri() const;
virtual const std::string& getName() const; virtual const std::string& getName() const;
virtual const std::string& getId() const;
virtual const SampleFormat& getSampleFormat() const; virtual const SampleFormat& getSampleFormat() const;
virtual ReaderState getState() const; virtual ReaderState getState() const;

View file

@ -36,7 +36,7 @@ StreamManager::StreamManager(PcmListener* pcmListener, const std::string& defaul
} }
PcmStream* StreamManager::addStream(const std::string& uri) PcmStreamPtr StreamManager::addStream(const std::string& uri)
{ {
StreamUri streamUri(uri); StreamUri streamUri(uri);
@ -54,38 +54,44 @@ PcmStream* StreamManager::addStream(const std::string& uri)
// for (auto kv: streamUri.query) // for (auto kv: streamUri.query)
// logD << "key: '" << kv.first << "' value: '" << kv.second << "'\n"; // logD << "key: '" << kv.first << "' value: '" << kv.second << "'\n";
PcmStreamPtr stream(nullptr);
if (streamUri.scheme == "pipe") if (streamUri.scheme == "pipe")
{ {
streams_.push_back(make_shared<PipeStream>(pcmListener_, streamUri)); stream = make_shared<PipeStream>(pcmListener_, streamUri);
return streams_.back().get();
} }
else if (streamUri.scheme == "file") else if (streamUri.scheme == "file")
{ {
streams_.push_back(make_shared<FileStream>(pcmListener_, streamUri)); stream = make_shared<FileStream>(pcmListener_, streamUri);
return streams_.back().get();
} }
else if (streamUri.scheme == "process") else if (streamUri.scheme == "process")
{ {
streams_.push_back(make_shared<ProcessStream>(pcmListener_, streamUri)); stream = make_shared<ProcessStream>(pcmListener_, streamUri);
return streams_.back().get();
} }
else if (streamUri.scheme == "spotify") else if (streamUri.scheme == "spotify")
{ {
streams_.push_back(make_shared<SpotifyStream>(pcmListener_, streamUri)); stream = make_shared<SpotifyStream>(pcmListener_, streamUri);
return streams_.back().get();
} }
else if (streamUri.scheme == "airplay") else if (streamUri.scheme == "airplay")
{ {
streams_.push_back(make_shared<AirplayStream>(pcmListener_, streamUri)); stream = make_shared<AirplayStream>(pcmListener_, streamUri);
return streams_.back().get();
} }
else else
{ {
throw SnapException("Unknown stream type: " + streamUri.scheme); throw SnapException("Unknown stream type: " + streamUri.scheme);
} }
return NULL; if (stream)
{
for (auto s: streams_)
{
if (s->getName() == stream->getName())
throw SnapException("Stream with name \"" + stream->getName() + "\" already exists");
}
streams_.push_back(stream);
}
return stream;
} }
@ -108,7 +114,7 @@ const PcmStreamPtr StreamManager::getStream(const std::string& id)
{ {
for (auto stream: streams_) for (auto stream: streams_)
{ {
if (stream->getUri().id() == id) if (stream->getId() == id)
return stream; return stream;
} }
return nullptr; return nullptr;

View file

@ -13,7 +13,7 @@ class StreamManager
public: public:
StreamManager(PcmListener* pcmListener, const std::string& defaultSampleFormat, const std::string& defaultCodec, size_t defaultReadBufferMs = 20); StreamManager(PcmListener* pcmListener, const std::string& defaultSampleFormat, const std::string& defaultCodec, size_t defaultReadBufferMs = 20);
PcmStream* addStream(const std::string& uri); PcmStreamPtr addStream(const std::string& uri);
void start(); void start();
void stop(); void stop();
const std::vector<PcmStreamPtr>& getStreams(); const std::vector<PcmStreamPtr>& getStreams();

View file

@ -41,15 +41,6 @@ StreamUri::StreamUri(const std::string& streamUri)
string decodedUri = uriDecode(uri); string decodedUri = uriDecode(uri);
logD << "StreamUri: " << decodedUri << "\n"; logD << "StreamUri: " << decodedUri << "\n";
id_ = decodedUri;
pos = id_.find('?');
if (pos != string::npos)
id_ = id_.substr(0, pos);
pos = id_.find('#');
if (pos != string::npos)
id_ = id_.substr(0, pos);
logD << "id: '" << id_ << "'\n";
string tmp(decodedUri); string tmp(decodedUri);
pos = tmp.find(':'); pos = tmp.find(':');
@ -98,8 +89,6 @@ StreamUri::StreamUri(const std::string& streamUri)
string key = trim_copy(kv.substr(0, pos)); string key = trim_copy(kv.substr(0, pos));
string value = trim_copy(kv.substr(pos+1)); string value = trim_copy(kv.substr(pos+1));
query[key] = value; query[key] = value;
if (key == "id")
id_ = value;
} }
} }
} }
@ -119,12 +108,6 @@ json StreamUri::toJson() const
} }
std::string StreamUri::id() const
{
return id_;
}
std::string StreamUri::getQuery(const std::string& key, const std::string& def) const std::string StreamUri::getQuery(const std::string& key, const std::string& def) const
{ {
auto iter = query.find(key); auto iter = query.find(key);

View file

@ -49,9 +49,6 @@ struct StreamUri
std::string id() const; std::string id() const;
json toJson() const; json toJson() const;
std::string getQuery(const std::string& key, const std::string& def = "") const; std::string getQuery(const std::string& key, const std::string& def = "") const;
private:
std::string id_;
}; };