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
#include <QTimer>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTcpSocket>
#include <QByteArray>
// Hyperion includes
#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
{
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;
};

View File

@ -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)),

View File

@ -3,11 +3,13 @@
#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(),
_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,19 +29,29 @@ 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)
{
std::cerr << "Error while requesting XBMC video info: " << reply->errorString().toStdString() << std::endl;
}
else
{
const QString jsonReply(reply->readAll());
if (jsonReply.contains("playerid"))
// 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"))
{
// 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
@ -53,7 +65,3 @@ void XBMCVideoChecker::receiveReply(QNetworkReply * reply)
}
}
// close reply
reply->close();
}

View File

@ -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);