2013-08-23 20:44:53 +02:00
|
|
|
// Qt includes
|
|
|
|
#include <QUrl>
|
2013-12-21 14:32:30 +01:00
|
|
|
#include <QRegExp>
|
|
|
|
#include <QStringRef>
|
2013-08-23 20:44:53 +02:00
|
|
|
|
|
|
|
#include <xbmcvideochecker/XBMCVideoChecker.h>
|
|
|
|
|
2013-12-21 14:32:30 +01:00
|
|
|
// Request player example:
|
|
|
|
// {"id":666,"jsonrpc":"2.0","method":"Player.GetActivePlayers"}
|
|
|
|
// {"id":666,"jsonrpc":"2.0","result":[{"playerid":1,"type":"video"}]}
|
|
|
|
|
|
|
|
// Request playing item example:
|
|
|
|
// {"id":667,"jsonrpc":"2.0","method":"Player.GetItem","params":{"playerid":1,"properties":["file"]}}
|
|
|
|
// {"id":667,"jsonrpc":"2.0","result":{"item":{"file":"smb://xbmc:xbmc@192.168.53.12/video/Movies/Avatar (2009)/Avatar.mkv","label":"Avatar","type":"unknown"}}}
|
|
|
|
|
|
|
|
// Request if screensaver is on
|
|
|
|
// {"id":668,"jsonrpc":"2.0","method":"XBMC.GetInfoBooleans","params":{"booleans":["System.ScreenSaverActive"]}}
|
|
|
|
// {"id":668,"jsonrpc":"2.0","result":{"System.ScreenSaverActive":false}}
|
|
|
|
|
2014-04-26 00:58:47 +02:00
|
|
|
// Request stereoscopicmode example:
|
2014-04-30 22:46:26 +02:00
|
|
|
// {"jsonrpc":"2.0","method":"GUI.GetProperties","params":{"properties":["stereoscopicmode"]},"id":669}
|
|
|
|
// {"id":669,"jsonrpc":"2.0","result":{"stereoscopicmode":{"label":"Nebeneinander","mode":"split_vertical"}}}
|
2014-04-26 00:58:47 +02:00
|
|
|
|
2013-12-21 14:32:30 +01:00
|
|
|
XBMCVideoChecker::XBMCVideoChecker(const std::string & address, uint16_t port, bool grabVideo, bool grabPhoto, bool grabAudio, bool grabMenu, bool grabScreensaver, bool enable3DDetection) :
|
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-12-21 14:32:30 +01:00
|
|
|
_activePlayerRequest(R"({"id":666,"jsonrpc":"2.0","method":"Player.GetActivePlayers"})"),
|
|
|
|
_currentPlayingItemRequest(R"({"id":667,"jsonrpc":"2.0","method":"Player.GetItem","params":{"playerid":%1,"properties":["file"]}})"),
|
|
|
|
_checkScreensaverRequest(R"({"id":668,"jsonrpc":"2.0","method":"XBMC.GetInfoBooleans","params":{"booleans":["System.ScreenSaverActive"]}})"),
|
2014-04-30 22:46:26 +02:00
|
|
|
_getStereoscopicMode(R"({"jsonrpc":"2.0","method":"GUI.GetProperties","params":{"properties":["stereoscopicmode"]},"id":669})"),
|
|
|
|
_getXbmcVersion(R"({"jsonrpc":"2.0","method":"Application.GetProperties","params":{"properties":["version"]},"id":670})"),
|
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),
|
2013-12-21 14:32:30 +01:00
|
|
|
_grabScreensaver(grabScreensaver),
|
|
|
|
_enable3DDetection(enable3DDetection),
|
|
|
|
_previousScreensaverMode(false),
|
|
|
|
_previousGrabbingMode(GRABBINGMODE_INVALID),
|
2014-04-30 22:46:26 +02:00
|
|
|
_previousVideoMode(VIDEO_2D),
|
|
|
|
_xbmcVersion(0)
|
2013-08-23 20:44:53 +02:00
|
|
|
{
|
2013-08-23 21:40:42 +02:00
|
|
|
// setup socket
|
|
|
|
connect(&_socket, SIGNAL(readyRead()), this, SLOT(receiveReply()));
|
2013-12-21 14:32:30 +01:00
|
|
|
connect(&_socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
|
|
|
connect(&_socket, SIGNAL(connected()), this, SLOT(connected()));
|
|
|
|
connect(&_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionError(QAbstractSocket::SocketError)));
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::start()
|
|
|
|
{
|
2013-12-21 17:20:02 +01:00
|
|
|
reconnect();
|
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());
|
|
|
|
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: Kodi Message: " << reply.toStdString() << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
|
|
|
|
if (reply.contains("\"method\":\"Player.OnPlay\""))
|
2013-09-24 21:26:50 +02:00
|
|
|
{
|
2013-12-21 14:32:30 +01:00
|
|
|
// send a request for the current player state
|
|
|
|
_socket.write(_activePlayerRequest.toUtf8());
|
2013-09-24 21:26:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
else if (reply.contains("\"method\":\"Player.OnStop\""))
|
2013-08-23 20:44:53 +02:00
|
|
|
{
|
2013-12-21 14:32:30 +01:00
|
|
|
// the player has stopped
|
|
|
|
setGrabbingMode(_grabMenu ? GRABBINGMODE_MENU : GRABBINGMODE_OFF);
|
|
|
|
setVideoMode(VIDEO_2D);
|
2013-09-23 22:33:38 +02:00
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
else if (reply.contains("\"method\":\"GUI.OnScreensaverActivated\""))
|
2013-09-23 22:33:38 +02:00
|
|
|
{
|
2013-12-21 14:32:30 +01:00
|
|
|
setScreensaverMode(!_grabScreensaver);
|
2013-09-23 22:33:38 +02:00
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
else if (reply.contains("\"method\":\"GUI.OnScreensaverDeactivated\""))
|
2013-09-23 22:33:38 +02:00
|
|
|
{
|
2013-12-21 14:32:30 +01:00
|
|
|
setScreensaverMode(false);
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
else if (reply.contains("\"id\":666"))
|
2013-08-23 20:44:53 +02:00
|
|
|
{
|
2013-12-21 14:32:30 +01:00
|
|
|
// Result of Player.GetActivePlayers
|
2013-09-23 22:33:38 +02:00
|
|
|
|
2013-12-21 14:32:30 +01:00
|
|
|
// always start a new video in 2D mode
|
|
|
|
emit videoMode(VIDEO_2D);
|
|
|
|
|
|
|
|
if (reply.contains("video"))
|
|
|
|
{
|
|
|
|
// video is playing
|
|
|
|
setGrabbingMode(_grabVideo ? GRABBINGMODE_VIDEO : GRABBINGMODE_OFF);
|
|
|
|
|
|
|
|
// we need to get the filename
|
|
|
|
// first retrieve the playerid
|
|
|
|
QString key = "\"playerid\":";
|
|
|
|
QRegExp regex(key + "(\\d+)");
|
|
|
|
int pos = regex.indexIn(reply);
|
|
|
|
if (pos > 0)
|
|
|
|
{
|
|
|
|
// now request info of the playing item
|
|
|
|
QStringRef idText(&reply, pos + key.length(), regex.matchedLength() - key.length());
|
|
|
|
_socket.write(_currentPlayingItemRequest.arg(idText.toString()).toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reply.contains("picture"))
|
|
|
|
{
|
|
|
|
// picture viewer is playing
|
|
|
|
setGrabbingMode(_grabPhoto ? GRABBINGMODE_PHOTO : GRABBINGMODE_OFF);
|
|
|
|
}
|
|
|
|
else if (reply.contains("audio"))
|
|
|
|
{
|
|
|
|
// audio is playing
|
|
|
|
setGrabbingMode(_grabAudio ? GRABBINGMODE_AUDIO : GRABBINGMODE_OFF);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Nothing is playing.
|
|
|
|
setGrabbingMode(_grabMenu ? GRABBINGMODE_MENU : GRABBINGMODE_OFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reply.contains("\"id\":667"))
|
2013-09-23 22:33:38 +02:00
|
|
|
{
|
2014-04-30 22:46:26 +02:00
|
|
|
if (_xbmcVersion >= 13)
|
2014-04-26 00:58:47 +02:00
|
|
|
{
|
2014-04-30 22:46:26 +02:00
|
|
|
// check of active stereoscopicmode
|
|
|
|
_socket.write(_getStereoscopicMode.toUtf8());
|
2014-04-26 00:58:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// result of Player.GetItem
|
|
|
|
// TODO: what if the filename contains a '"'. In Json this should have been escaped
|
|
|
|
QRegExp regex("\"file\":\"((?!\").)*\"");
|
|
|
|
int pos = regex.indexIn(reply);
|
|
|
|
if (pos > 0)
|
|
|
|
{
|
|
|
|
QStringRef filename = QStringRef(&reply, pos+8, regex.matchedLength()-9);
|
|
|
|
if (filename.contains("3DSBS", Qt::CaseInsensitive) || filename.contains("HSBS", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
setVideoMode(VIDEO_3DSBS);
|
|
|
|
}
|
|
|
|
else if (filename.contains("3DTAB", Qt::CaseInsensitive) || filename.contains("HTAB", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
setVideoMode(VIDEO_3DTAB);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setVideoMode(VIDEO_2D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reply.contains("\"id\":668"))
|
|
|
|
{
|
|
|
|
// result of System.ScreenSaverActive
|
|
|
|
bool active = reply.contains("\"System.ScreenSaverActive\":true");
|
|
|
|
setScreensaverMode(!_grabScreensaver && active);
|
|
|
|
|
|
|
|
// check here xbmc version
|
|
|
|
if (_socket.state() == QTcpSocket::ConnectedState)
|
|
|
|
{
|
2014-04-30 22:46:26 +02:00
|
|
|
if (_xbmcVersion == 0)
|
|
|
|
{
|
2014-04-26 00:58:47 +02:00
|
|
|
_socket.write(_getXbmcVersion.toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 22:46:26 +02:00
|
|
|
else if (reply.contains("\"id\":669"))
|
2014-04-26 00:58:47 +02:00
|
|
|
{
|
|
|
|
QRegExp regex("\"mode\":\"(split_vertical|split_horizontal)\"");
|
2013-12-21 14:32:30 +01:00
|
|
|
int pos = regex.indexIn(reply);
|
|
|
|
if (pos > 0)
|
2013-10-20 10:37:04 +02:00
|
|
|
{
|
2014-04-26 00:58:47 +02:00
|
|
|
QString sMode = regex.cap(1);
|
|
|
|
if (sMode == "split_vertical")
|
2013-12-21 14:32:30 +01:00
|
|
|
{
|
|
|
|
setVideoMode(VIDEO_3DSBS);
|
|
|
|
}
|
2014-04-26 00:58:47 +02:00
|
|
|
else if (sMode == "split_horizontal")
|
2013-12-21 14:32:30 +01:00
|
|
|
{
|
|
|
|
setVideoMode(VIDEO_3DTAB);
|
|
|
|
}
|
2013-10-20 10:37:04 +02:00
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
}
|
2014-04-30 22:46:26 +02:00
|
|
|
else if (reply.contains("\"id\":670"))
|
2013-12-21 14:32:30 +01:00
|
|
|
{
|
2014-04-26 00:58:47 +02:00
|
|
|
QRegExp regex("\"major\":(\\d+)");
|
|
|
|
int pos = regex.indexIn(reply);
|
|
|
|
if (pos > 0)
|
|
|
|
{
|
2014-04-30 22:46:26 +02:00
|
|
|
_xbmcVersion = regex.cap(1).toInt();
|
2014-04-26 00:58:47 +02:00
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::connected()
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: Kodi Connected" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
|
|
|
|
// send a request for the current player state
|
|
|
|
_socket.write(_activePlayerRequest.toUtf8());
|
|
|
|
_socket.write(_checkScreensaverRequest.toUtf8());
|
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::disconnected()
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: Kodi Disconnected" << std::endl;
|
2013-12-21 17:20:02 +01:00
|
|
|
reconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::reconnect()
|
|
|
|
{
|
|
|
|
if (_socket.state() == QTcpSocket::ConnectedState)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
|
|
|
|
// try to connect
|
2013-12-21 17:20:02 +01:00
|
|
|
switch (_socket.state())
|
|
|
|
{
|
|
|
|
case QTcpSocket::ConnectingState:
|
|
|
|
// Somehow when XBMC restarts we get stuck in connecting state
|
|
|
|
// If we get here we tried to connect already for 5 seconds. abort and try again in 1 second.
|
|
|
|
_socket.reset();
|
|
|
|
_socket.waitForDisconnected(50);
|
|
|
|
QTimer::singleShot(1000, this, SLOT(reconnect()));
|
|
|
|
break;
|
|
|
|
case QTcpSocket::UnconnectedState:
|
|
|
|
_socket.connectToHost(_address, _port);
|
|
|
|
QTimer::singleShot(10000, this, SLOT(reconnect()));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
QTimer::singleShot(10000, this, SLOT(reconnect()));
|
|
|
|
break;
|
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
}
|
2013-10-20 10:37:04 +02:00
|
|
|
|
2013-12-21 17:20:02 +01:00
|
|
|
void XBMCVideoChecker::connectionError(QAbstractSocket::SocketError error)
|
2013-12-21 14:32:30 +01:00
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK ERROR: Kodi Connection error (" << error << ")" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
|
2013-12-21 17:20:02 +01:00
|
|
|
// close the socket
|
|
|
|
_socket.close();
|
2013-12-21 14:32:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::setGrabbingMode(GrabbingMode newGrabbingMode)
|
|
|
|
{
|
|
|
|
if (newGrabbingMode == _previousGrabbingMode)
|
|
|
|
{
|
|
|
|
// no change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (newGrabbingMode)
|
|
|
|
{
|
|
|
|
case GRABBINGMODE_VIDEO:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to VIDEO mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case GRABBINGMODE_PHOTO:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to PHOTO mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case GRABBINGMODE_AUDIO:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to AUDIO mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case GRABBINGMODE_MENU:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to MENU mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case GRABBINGMODE_OFF:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to OFF mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case GRABBINGMODE_INVALID:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to INVALID mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only emit the new state when we want to grab in screensaver mode or when the screensaver is deactivated
|
|
|
|
if (!_previousScreensaverMode)
|
|
|
|
{
|
|
|
|
emit grabbingMode(newGrabbingMode);
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
2013-12-21 14:32:30 +01:00
|
|
|
_previousGrabbingMode = newGrabbingMode;
|
2013-08-23 20:44:53 +02:00
|
|
|
}
|
|
|
|
|
2013-12-21 14:32:30 +01:00
|
|
|
void XBMCVideoChecker::setScreensaverMode(bool isOnScreensaver)
|
|
|
|
{
|
|
|
|
if (isOnScreensaver == _previousScreensaverMode)
|
|
|
|
{
|
|
|
|
// no change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit grabbingMode(isOnScreensaver ? GRABBINGMODE_OFF : _previousGrabbingMode);
|
|
|
|
_previousScreensaverMode = isOnScreensaver;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XBMCVideoChecker::setVideoMode(VideoMode newVideoMode)
|
|
|
|
{
|
|
|
|
if (newVideoMode == _previousVideoMode)
|
|
|
|
{
|
|
|
|
// no change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (newVideoMode)
|
|
|
|
{
|
|
|
|
case VIDEO_2D:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to 2D mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case VIDEO_3DSBS:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to 3D SBS mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case VIDEO_3DTAB:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "KODICHECK INFO: switching to 3D TAB mode" << std::endl;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit videoMode(newVideoMode);
|
|
|
|
_previousVideoMode = newVideoMode;
|
|
|
|
}
|