report stream state

This commit is contained in:
badaix 2016-03-06 22:30:23 +01:00
parent cd2daa2cf9
commit 94629f9909
9 changed files with 79 additions and 12 deletions

View file

@ -55,7 +55,7 @@ Config::Config()
client->fromJson(*it); client->fromJson(*it);
client->connected = false; client->connected = false;
clients.push_back(client); clients.push_back(client);
std::cout << "Client:\n" << std::setw(4) << client->toJson() << '\n'; // logO << "Client:\n" << std::setw(4) << client->toJson() << '\n';
} }
} }
} }

View file

@ -58,6 +58,8 @@ void FileReader::worker()
size_t length = ifs.tellg(); size_t length = ifs.tellg();
ifs.seekg (0, ifs.beg); ifs.seekg (0, ifs.beg);
setState(kPlaying);
while (active_) while (active_)
{ {
gettimeofday(&tvChunk, NULL); gettimeofday(&tvChunk, NULL);

View file

@ -32,7 +32,7 @@ using namespace std;
PcmReader::PcmReader(PcmListener* pcmListener, const ReaderUri& uri) : pcmListener_(pcmListener), uri_(uri), pcmReadMs_(20) PcmReader::PcmReader(PcmListener* pcmListener, const ReaderUri& uri) : pcmListener_(pcmListener), uri_(uri), pcmReadMs_(20), state_(kIdle)
{ {
EncoderFactory encoderFactory; EncoderFactory encoderFactory;
if (uri_.query.find("codec") == uri_.query.end()) if (uri_.query.find("codec") == uri_.query.end())
@ -104,6 +104,22 @@ void PcmReader::stop()
} }
ReaderState PcmReader::getState() const
{
return state_;
}
void PcmReader::setState(const ReaderState& newState)
{
if (newState != state_)
{
state_ = newState;
pcmListener_->onStateChanged(this, newState);
}
}
void PcmReader::onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration) void PcmReader::onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration)
{ {
// logO << "onChunkEncoded: " << duration << " us\n"; // logO << "onChunkEncoded: " << duration << " us\n";
@ -116,3 +132,22 @@ void PcmReader::onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, dou
pcmListener_->onChunkRead(this, chunk, duration); pcmListener_->onChunkRead(this, chunk, duration);
} }
json PcmReader::toJson() const
{
string state("idle");
if (state_ == kIdle)
state = "idle";
else if (state_ == kPlaying)
state = "playing";
else if (state_ == kDisabled)
state = "disabled";
json j = {
{"uri", uri_.toJson()},
{"id", uri_.id()},
{"status", state}
};
return j;
}

View file

@ -25,12 +25,20 @@
#include <map> #include <map>
#include "readerUri.h" #include "readerUri.h"
#include "../encoder/encoder.h" #include "../encoder/encoder.h"
#include "../json/json.hpp"
#include "message/sampleFormat.h" #include "message/sampleFormat.h"
#include "message/header.h" #include "message/header.h"
class PcmReader; class PcmReader;
enum ReaderState
{
kIdle = 0,
kPlaying = 1,
kDisabled = 2
};
/// Callback interface for users of PcmReader /// Callback interface for users of PcmReader
/** /**
@ -39,6 +47,7 @@ class PcmReader;
class PcmListener class PcmListener
{ {
public: public:
virtual void onStateChanged(const PcmReader* pcmReader, const ReaderState& state) = 0;
virtual void onChunkRead(const PcmReader* pcmReader, const msg::PcmChunk* chunk, double duration) = 0; virtual void onChunkRead(const PcmReader* pcmReader, const msg::PcmChunk* chunk, double duration) = 0;
virtual void onResync(const PcmReader* pcmReader, double ms) = 0; virtual void onResync(const PcmReader* pcmReader, double ms) = 0;
}; };
@ -68,8 +77,14 @@ public:
virtual const std::string& getName() const; virtual const std::string& getName() const;
virtual const SampleFormat& getSampleFormat() const; virtual const SampleFormat& getSampleFormat() const;
virtual ReaderState getState() const;
virtual json toJson() const;
protected: protected:
virtual void worker() = 0; virtual void worker() = 0;
void setState(const ReaderState& newState);
timeval tvEncodedChunk_; timeval tvEncodedChunk_;
std::atomic<bool> active_; std::atomic<bool> active_;
std::thread readerThread_; std::thread readerThread_;
@ -79,6 +94,7 @@ protected:
size_t pcmReadMs_; size_t pcmReadMs_;
std::unique_ptr<Encoder> encoder_; std::unique_ptr<Encoder> encoder_;
std::string name_; std::string name_;
ReaderState state_;
}; };

View file

@ -76,7 +76,10 @@ void PipeReader::worker()
{ {
int count = read(fd_, chunk->payload + len, toRead - len); int count = read(fd_, chunk->payload + len, toRead - len);
if (count < 0) if (count < 0)
{
setState(kIdle);
usleep(100*1000); usleep(100*1000);
}
else if (count == 0) else if (count == 0)
throw SnapException("end of file"); throw SnapException("end of file");
else else
@ -92,6 +95,7 @@ void PipeReader::worker()
if (nextTick >= currentTick) if (nextTick >= currentTick)
{ {
// logO << "sleep: " << nextTick - currentTick << "\n"; // logO << "sleep: " << nextTick - currentTick << "\n";
setState(kPlaying);
usleep((nextTick - currentTick) * 1000); usleep((nextTick - currentTick) * 1000);
} }
else else

View file

@ -108,14 +108,14 @@ ReaderUri::ReaderUri(const std::string& readerUri)
json ReaderUri::toJson() const json ReaderUri::toJson() const
{ {
json j; json j = {
j["uri"] = uri; {"raw", uri},
j["scheme"] = scheme; {"scheme", scheme},
j["host"] = host; {"host", host},
j["path"] = path; {"path", path},
j["fragment"] = fragment; {"fragment", fragment},
j["query"] = json(query); {"query", query}
j["id"] = id_; };
return j; return j;
} }

View file

@ -114,7 +114,7 @@ json StreamManager::toJson() const
{ {
json result = json::array(); json result = json::array();
for (auto stream: streams_) for (auto stream: streams_)
result.push_back(stream->getUri().toJson()); result.push_back(stream->toJson());
return result; return result;
} }

View file

@ -40,6 +40,15 @@ StreamServer::~StreamServer()
} }
void StreamServer::onStateChanged(const PcmReader* pcmReader, const ReaderState& state)
{
logO << "onStateChanged (" << pcmReader->getName() << "): " << state << "\n";
// logO << pcmReader->toJson().dump(4);
json notification = JsonNotification::getJson("Stream.OnUpdate", pcmReader->toJson());
controlServer_->send(notification.dump(), NULL);
}
void StreamServer::onChunkRead(const PcmReader* pcmReader, const msg::PcmChunk* chunk, double duration) void StreamServer::onChunkRead(const PcmReader* pcmReader, const msg::PcmChunk* chunk, double duration)
{ {
// logO << "onChunkRead (" << pcmReader->getName() << "): " << duration << "ms\n"; // logO << "onChunkRead (" << pcmReader->getName() << "): " << duration << "ms\n";
@ -301,7 +310,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
connection->send(headerChunk.get()); connection->send(headerChunk.get());
json notification = JsonNotification::getJson("Client.OnConnect", client->toJson()); json notification = JsonNotification::getJson("Client.OnConnect", client->toJson());
logO << notification.dump(4) << "\n"; // logO << notification.dump(4) << "\n";
controlServer_->send(notification.dump()); controlServer_->send(notification.dump());
} }
} }

View file

@ -89,6 +89,7 @@ public:
virtual void onMessageReceived(ControlSession* connection, const std::string& message); virtual void onMessageReceived(ControlSession* connection, const std::string& message);
/// Implementation of PcmListener /// Implementation of PcmListener
virtual void onStateChanged(const PcmReader* pcmReader, const ReaderState& state);
virtual void onChunkRead(const PcmReader* pcmReader, const msg::PcmChunk* chunk, double duration); virtual void onChunkRead(const PcmReader* pcmReader, const msg::PcmChunk* chunk, double duration);
virtual void onResync(const PcmReader* pcmReader, double ms); virtual void onResync(const PcmReader* pcmReader, double ms);