Changed XBMC video checker from HTPP (using QNetworkAccessManager) to a plain TCP socket because of thre required CPU

This commit is contained in:
johan 2013-08-23 21:40:42 +02:00
parent c4eb715592
commit 026937d5e1
4 changed files with 51 additions and 36 deletions

View File

@ -8,36 +8,43 @@
// QT includes // QT includes
#include <QTimer> #include <QTimer>
#include <QString> #include <QString>
#include <QNetworkAccessManager> #include <QTcpSocket>
#include <QNetworkRequest> #include <QByteArray>
#include <QNetworkReply>
// Hyperion includes // Hyperion includes
#include <hyperion/Hyperion.h> #include <hyperion/Hyperion.h>
/// 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 class XBMCVideoChecker : public QObject
{ {
Q_OBJECT Q_OBJECT
public: 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(); void start();
private slots: private slots:
void sendRequest(); void sendRequest();
void receiveReply(QNetworkReply * reply); void receiveReply();
private: private:
const QString _address;
const uint16_t _port;
const QByteArray _request;
QTimer _timer; QTimer _timer;
QNetworkAccessManager _networkManager; QTcpSocket _socket;
QNetworkRequest _request;
Hyperion * _hyperion; Hyperion * _hyperion;
int _priority; const int _priority;
}; };

View File

@ -15,7 +15,7 @@
DispmanxWrapper::DispmanxWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) : DispmanxWrapper::DispmanxWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) :
_updateInterval_ms(1000/updateRate_Hz), _updateInterval_ms(1000/updateRate_Hz),
_timeout_ms(2 * _updateInterval_ms), _timeout_ms(2 * _updateInterval_ms),
_priority(128), _priority(1000),
_timer(), _timer(),
_image(grabWidth, grabHeight), _image(grabWidth, grabHeight),
_frameGrabber(new DispmanxFrameGrabber(grabWidth, grabHeight)), _frameGrabber(new DispmanxFrameGrabber(grabWidth, grabHeight)),

View File

@ -3,11 +3,13 @@
#include <xbmcvideochecker/XBMCVideoChecker.h> #include <xbmcvideochecker/XBMCVideoChecker.h>
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(), QObject(),
_address(address),
_port(port),
_request("{\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}"),
_timer(), _timer(),
_networkManager(), _socket(),
_request(QUrl(QString("http://%1/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}").arg(address))),
_hyperion(hyperion), _hyperion(hyperion),
_priority(priority) _priority(priority)
{ {
@ -16,8 +18,8 @@ XBMCVideoChecker::XBMCVideoChecker(QString address, uint64_t interval_ms, Hyperi
_timer.setInterval(interval_ms); _timer.setInterval(interval_ms);
connect(&_timer, SIGNAL(timeout()), this, SLOT(sendRequest())); connect(&_timer, SIGNAL(timeout()), this, SLOT(sendRequest()));
//setup network manager // setup socket
connect(&_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveReply(QNetworkReply*))); connect(&_socket, SIGNAL(readyRead()), this, SLOT(receiveReply()));
} }
void XBMCVideoChecker::start() void XBMCVideoChecker::start()
@ -27,33 +29,39 @@ void XBMCVideoChecker::start()
void XBMCVideoChecker::sendRequest() 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 else
{ {
const QString jsonReply(reply->readAll()); // Nothing is playing. set our priority channel completely to black
if (jsonReply.contains("playerid")) // The timeout is used to have the channel cleared after 30 seconds of connection problems...
{ _hyperion->setColor(_priority, RgbColor::BLACK, 30000);
// 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);
}
} }
// close reply
reply->close();
} }

View File

@ -51,7 +51,7 @@ int main(int argc, char** argv)
RainbowBootSequence bootSequence(&hyperion); RainbowBootSequence bootSequence(&hyperion);
bootSequence.start(); bootSequence.start();
XBMCVideoChecker xbmcVideoChecker("127.0.0.1", 1000, &hyperion, 127); XBMCVideoChecker xbmcVideoChecker("127.0.0.1", 9090, 1000, &hyperion, 999);
xbmcVideoChecker.start(); xbmcVideoChecker.start();
DispmanxWrapper dispmanx(64, 64, 10, &hyperion); DispmanxWrapper dispmanx(64, 64, 10, &hyperion);