2013-08-23 20:44:53 +02:00
|
|
|
// Qt includes
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include <xbmcvideochecker/XBMCVideoChecker.h>
|
|
|
|
|
2013-09-23 22:33:38 +02:00
|
|
|
XBMCVideoChecker::XBMCVideoChecker(const std::string & address, uint16_t port, uint64_t interval_ms, bool grabVideo, bool grabPhoto, bool grabAudio, bool grabMenu) :
|
2013-08-23 20:44:53 +02:00
|
|
|
QObject(),
|
2013-08-24 11:51:52 +02:00
|
|
|
_address(QString::fromStdString(address)),
|
2013-08-23 21:40:42 +02:00
|
|
|
_port(port),
|
2013-10-14 18:21:36 +02:00
|
|
|
_request(R"({"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":666})"),
|
2013-08-23 20:44:53 +02:00
|
|
|
_timer(),
|
2013-08-23 21:40:42 +02:00
|
|
|
_socket(),
|
2013-09-23 22:33:38 +02:00
|
|
|
_grabVideo(grabVideo),
|
|
|
|
_grabPhoto(grabPhoto),
|
|
|
|
_grabAudio(grabAudio),
|
|
|
|
_grabMenu(grabMenu),
|
|
|
|
_previousMode(GRABBINGMODE_INVALID)
|
2013-08-23 20:44:53 +02:00
|
|
|
{
|
|
|
|
// setup timer
|
|
|
|
_timer.setSingleShot(false);
|
|
|
|
_timer.setInterval(interval_ms);
|
|
|
|
connect(&_timer, SIGNAL(timeout()), this, SLOT(sendRequest()));
|
|
|
|
|
2013-08-23 21:40:42 +02:00
|
|
|
// setup socket
|
|
|
|
connect(&_socket, SIGNAL(readyRead()), this, SLOT(receiveReply()));
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::start()
|
|
|
|
{
|
|
|
|
_timer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::sendRequest()
|
|
|
|
{
|
2013-08-23 21:40:42 +02:00
|
|
|
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;
|
|
|
|
}
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:40:42 +02:00
|
|
|
void XBMCVideoChecker::receiveReply()
|
2013-08-23 20:44:53 +02:00
|
|
|
{
|
2013-08-24 11:51:52 +02:00
|
|
|
// expect that the reply is received as a single message. Probably oke considering the size of the expected reply
|
2013-08-23 21:40:42 +02:00
|
|
|
QString reply(_socket.readAll());
|
|
|
|
|
2013-09-24 21:26:50 +02:00
|
|
|
// check if the resply is a reply to one of my requests
|
|
|
|
if (!reply.contains("\"id\":666"))
|
|
|
|
{
|
|
|
|
// probably not. Leave this mreply as is and don't act on it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:33:38 +02:00
|
|
|
GrabbingMode newMode = GRABBINGMODE_INVALID;
|
|
|
|
if (reply.contains("video"))
|
2013-08-23 20:44:53 +02:00
|
|
|
{
|
2013-09-23 22:33:38 +02:00
|
|
|
// video is playing
|
|
|
|
newMode = _grabVideo ? GRABBINGMODE_VIDEO : GRABBINGMODE_OFF;
|
|
|
|
}
|
|
|
|
else if (reply.contains("picture"))
|
|
|
|
{
|
2013-09-24 21:45:27 +02:00
|
|
|
// picture viewer is playing
|
|
|
|
newMode = _grabPhoto ? GRABBINGMODE_PHOTO : GRABBINGMODE_OFF;
|
2013-09-23 22:33:38 +02:00
|
|
|
}
|
|
|
|
else if (reply.contains("audio"))
|
|
|
|
{
|
2013-09-24 21:45:27 +02:00
|
|
|
// audio is playing
|
|
|
|
newMode = _grabAudio ? GRABBINGMODE_AUDIO : GRABBINGMODE_OFF;
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-23 22:33:38 +02:00
|
|
|
// Nothing is playing.
|
|
|
|
newMode = _grabMenu ? GRABBINGMODE_MENU : GRABBINGMODE_OFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
// emit new state if applicable
|
|
|
|
if (newMode != _previousMode)
|
|
|
|
{
|
|
|
|
emit grabbingMode(newMode);
|
|
|
|
_previousMode = newMode;
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|