diff --git a/include/xbmcvideochecker/XBMCVideoChecker.h b/include/xbmcvideochecker/XBMCVideoChecker.h index 61e7ac19..29035f85 100644 --- a/include/xbmcvideochecker/XBMCVideoChecker.h +++ b/include/xbmcvideochecker/XBMCVideoChecker.h @@ -8,36 +8,43 @@ // QT includes #include #include -#include -#include -#include +#include +#include // Hyperion includes #include +/// Check if XBMC is playing something. When it does not, this class will send all black data Hyperion to +/// override (grabbed) data with a lower priority +/// +/// Note: The json TCP server needs to be enabled manually in XBMC in System/Settings/Network/Services class XBMCVideoChecker : public QObject { Q_OBJECT public: - XBMCVideoChecker(QString address, uint64_t interval, Hyperion * hyperion, int priority); + XBMCVideoChecker(QString address, uint16_t port, uint64_t interval, Hyperion * hyperion, int priority); void start(); private slots: void sendRequest(); - void receiveReply(QNetworkReply * reply); + void receiveReply(); private: + const QString _address; + + const uint16_t _port; + + const QByteArray _request; + QTimer _timer; - QNetworkAccessManager _networkManager; - - QNetworkRequest _request; + QTcpSocket _socket; Hyperion * _hyperion; - int _priority; + const int _priority; }; diff --git a/libsrc/dispmanx-grabber/DispmanxWrapper.cpp b/libsrc/dispmanx-grabber/DispmanxWrapper.cpp index ad8261f8..b785db19 100644 --- a/libsrc/dispmanx-grabber/DispmanxWrapper.cpp +++ b/libsrc/dispmanx-grabber/DispmanxWrapper.cpp @@ -15,7 +15,7 @@ DispmanxWrapper::DispmanxWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) : _updateInterval_ms(1000/updateRate_Hz), _timeout_ms(2 * _updateInterval_ms), - _priority(128), + _priority(1000), _timer(), _image(grabWidth, grabHeight), _frameGrabber(new DispmanxFrameGrabber(grabWidth, grabHeight)), diff --git a/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp b/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp index 2d4a16bf..7896ddbf 100644 --- a/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp +++ b/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp @@ -3,11 +3,13 @@ #include -XBMCVideoChecker::XBMCVideoChecker(QString address, uint64_t interval_ms, Hyperion * hyperion, int priority) : +XBMCVideoChecker::XBMCVideoChecker(QString address, uint16_t port, uint64_t interval_ms, Hyperion * hyperion, int priority) : QObject(), + _address(address), + _port(port), + _request("{\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}"), _timer(), - _networkManager(), - _request(QUrl(QString("http://%1/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}").arg(address))), + _socket(), _hyperion(hyperion), _priority(priority) { @@ -16,8 +18,8 @@ XBMCVideoChecker::XBMCVideoChecker(QString address, uint64_t interval_ms, Hyperi _timer.setInterval(interval_ms); connect(&_timer, SIGNAL(timeout()), this, SLOT(sendRequest())); - //setup network manager - connect(&_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveReply(QNetworkReply*))); + // setup socket + connect(&_socket, SIGNAL(readyRead()), this, SLOT(receiveReply())); } void XBMCVideoChecker::start() @@ -27,33 +29,39 @@ void XBMCVideoChecker::start() void XBMCVideoChecker::sendRequest() { - _networkManager.get(_request); + switch (_socket.state()) + { + case QTcpSocket::UnconnectedState: + // not connected. try to connect + std::cout << "Connecting to " << _address.toStdString() << ":" << _port << " to check XBMC player status" << std::endl; + _socket.connectToHost(_address, _port); + break; + case QTcpSocket::ConnectedState: + // write the request on the socket + _socket.write(_request); + break; + default: + // whatever. let's check again at the next timer tick + break; + } } -void XBMCVideoChecker::receiveReply(QNetworkReply * reply) +void XBMCVideoChecker::receiveReply() { - if (reply->error() != QNetworkReply::NoError) + // expect that the reply is received as a single message. Probaly oke considering the size of the expected reply + QString reply(_socket.readAll()); + + if (reply.contains("playerid")) { - std::cerr << "Error while requesting XBMC video info: " << reply->errorString().toStdString() << std::endl; + // something is playing. check for "video" to check if a video is playing + // clear our priority channel to allow the grabbed vido colors to be shown + _hyperion->clear(_priority); } else { - const QString jsonReply(reply->readAll()); - if (jsonReply.contains("playerid")) - { - // something is playing. check for "video" to check if a video is playing - // clear our priority channel to allow the grabbed vido colors to be shown - _hyperion->clear(_priority); - } - else - { - // Nothing is playing. set our priority channel completely to black - // The timeout is used to have the channel cleared after 30 seconds of connection problems... - _hyperion->setColor(_priority, RgbColor::BLACK, 30000); - } + // Nothing is playing. set our priority channel completely to black + // The timeout is used to have the channel cleared after 30 seconds of connection problems... + _hyperion->setColor(_priority, RgbColor::BLACK, 30000); } - - // close reply - reply->close(); } diff --git a/src/hyperiond/hyperiond.cpp b/src/hyperiond/hyperiond.cpp index dc42115e..58ec19c7 100644 --- a/src/hyperiond/hyperiond.cpp +++ b/src/hyperiond/hyperiond.cpp @@ -51,7 +51,7 @@ int main(int argc, char** argv) RainbowBootSequence bootSequence(&hyperion); bootSequence.start(); - XBMCVideoChecker xbmcVideoChecker("127.0.0.1", 1000, &hyperion, 127); + XBMCVideoChecker xbmcVideoChecker("127.0.0.1", 9090, 1000, &hyperion, 999); xbmcVideoChecker.start(); DispmanxWrapper dispmanx(64, 64, 10, &hyperion);