ProtoConnectionWrapper::ProtoConnectionWrapper(const QString &address, int priority, int duration_ms, bool skipProtoReply) : _priority(priority) , _duration_ms(duration_ms) , _connection(address.toStdString()) { _connection.setSkipReply(skipProtoReply); connect(&_connection, SIGNAL(setGrabbingMode(GrabbingMode)), this, SIGNAL(setGrabbingMode(GrabbingMode))); connect(&_connection, SIGNAL(setVideoMode(VideoMode)), this, SIGNAL(setVideoMode(VideoMode))); }
bool ProtoConnection::parseReply(const proto::HyperionReply &reply) { bool success = false; switch (reply.type()) { case proto::HyperionReply::REPLY: { if (!_skipReply) { if (!reply.success()) { if (reply.has_error()) { throw std::runtime_error("PROTOCONNECTION ERROR: " + reply.error()); } else { throw std::runtime_error("PROTOCONNECTION ERROR: No error info"); } } else { success = true; } } break; } case proto::HyperionReply::GRABBING: { int grabbing = reply.has_grabbing() ? reply.grabbing() : 7; GrabbingMode gMode = (GrabbingMode)grabbing; emit setGrabbingMode(gMode); break; } case proto::HyperionReply::VIDEO: { int video = reply.has_video() ? reply.video() : 0; VideoMode vMode = (VideoMode)video; emit setVideoMode(vMode); break; } } return success; }
XBMCVideoChecker::XBMCVideoChecker(const std::string & address, uint16_t port, bool grabVideo, bool grabPhoto, bool grabAudio, bool grabMenu, bool grabScreensaver, bool enable3DDetection) : QObject(), _address(QString::fromStdString(address)), _port(port), _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"]}})"), _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})"), _socket(), _grabVideo(grabVideo), _grabPhoto(grabPhoto), _grabAudio(grabAudio), _grabMenu(grabMenu), _grabScreensaver(grabScreensaver), _enable3DDetection(enable3DDetection), _previousScreensaverMode(false), _previousGrabbingMode(GRABBINGMODE_INVALID), _previousVideoMode(VIDEO_2D), _xbmcVersion(0) { // setup socket connect(&_socket, SIGNAL(readyRead()), this, SLOT(receiveReply())); 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))); } void XBMCVideoChecker::start() { reconnect(); } void XBMCVideoChecker::receiveReply() { // expect that the reply is received as a single message. Probably oke considering the size of the expected reply QString reply(_socket.readAll()); std::cout << "Message from XBMC: " << reply.toStdString() << std::endl; if (reply.contains("\"method\":\"Player.OnPlay\"")) { // send a request for the current player state _socket.write(_activePlayerRequest.toUtf8()); return; } else if (reply.contains("\"method\":\"Player.OnStop\"")) { // the player has stopped setGrabbingMode(_grabMenu ? GRABBINGMODE_MENU : GRABBINGMODE_OFF); setVideoMode(VIDEO_2D); } else if (reply.contains("\"method\":\"GUI.OnScreensaverActivated\"")) { setScreensaverMode(!_grabScreensaver); } else if (reply.contains("\"method\":\"GUI.OnScreensaverDeactivated\"")) { setScreensaverMode(false); } else if (reply.contains("\"id\":666")) { // Result of Player.GetActivePlayers // 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")) { if (_xbmcVersion >= 13) { // check of active stereoscopicmode _socket.write(_getStereoscopicMode.toUtf8()); } 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) { if (_xbmcVersion == 0) { _socket.write(_getXbmcVersion.toUtf8()); } } } else if (reply.contains("\"id\":669")) { QRegExp regex("\"mode\":\"(split_vertical|split_horizontal)\""); int pos = regex.indexIn(reply); if (pos > 0) { QString sMode = regex.cap(1); if (sMode == "split_vertical") { setVideoMode(VIDEO_3DSBS); } else if (sMode == "split_horizontal") { setVideoMode(VIDEO_3DTAB); } } } else if (reply.contains("\"id\":670")) { QRegExp regex("\"major\":(\\d+)"); int pos = regex.indexIn(reply); if (pos > 0) { _xbmcVersion = regex.cap(1).toInt(); } } }
int main(int argc, char** argv) { std::cout << "Application build time: " << __DATE__ << " " << __TIME__ << std::endl; // Initialising QCoreApplication QCoreApplication app(argc, argv); std::cout << "QCoreApplication initialised" << std::endl; signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); // force the locale setlocale(LC_ALL, "C"); QLocale::setDefault(QLocale::c()); if (argc < 2) { std::cout << "Missing required configuration file. Usage:" << std::endl; std::cout << "hyperiond [config.file]" << std::endl; return 1; } const std::string configFile = argv[1]; std::cout << "Selected configuration file: " << configFile.c_str() << std::endl; const Json::Value config = loadConfig(configFile); Hyperion hyperion(config); std::cout << "Hyperion created and initialised" << std::endl; // create boot sequence if the configuration is present if (config.isMember("bootsequence")) { const Json::Value effectConfig = config["bootsequence"]; // Get the parameters for the bootsequence const std::string effectName = effectConfig["effect"].asString(); const unsigned duration_ms = effectConfig["duration_ms"].asUInt(); const int priority = 0; hyperion.setColor(priority+1, ColorRgb::BLACK, duration_ms, false); if (effectConfig.isMember("args")) { const Json::Value effectConfigArgs = effectConfig["args"]; if (hyperion.setEffect(effectName, effectConfigArgs, priority, duration_ms) == 0) { std::cout << "Boot sequence(" << effectName << ") with user-defined arguments created and started" << std::endl; } else { std::cout << "Failed to start boot sequence: " << effectName << " with user-defined arguments" << std::endl; } } else { if (hyperion.setEffect(effectName, priority, duration_ms) == 0) { std::cout << "Boot sequence(" << effectName << ") created and started" << std::endl; } else { std::cout << "Failed to start boot sequence: " << effectName << std::endl; } } } // create XBMC video checker if the configuration is present XBMCVideoChecker * xbmcVideoChecker = nullptr; if (config.isMember("xbmcVideoChecker")) { const Json::Value & videoCheckerConfig = config["xbmcVideoChecker"]; xbmcVideoChecker = new XBMCVideoChecker( videoCheckerConfig["xbmcAddress"].asString(), videoCheckerConfig["xbmcTcpPort"].asUInt(), videoCheckerConfig["grabVideo"].asBool(), videoCheckerConfig["grabPictures"].asBool(), videoCheckerConfig["grabAudio"].asBool(), videoCheckerConfig["grabMenu"].asBool(), videoCheckerConfig.get("grabScreensaver", true).asBool(), videoCheckerConfig.get("enable3DDetection", true).asBool()); xbmcVideoChecker->start(); std::cout << "XBMC video checker created and started" << std::endl; } #ifdef ENABLE_DISPMANX // Construct and start the frame-grabber if the configuration is present DispmanxWrapper * dispmanx = nullptr; if (config.isMember("framegrabber")) { const Json::Value & frameGrabberConfig = config["framegrabber"]; dispmanx = new DispmanxWrapper( frameGrabberConfig["width"].asUInt(), frameGrabberConfig["height"].asUInt(), frameGrabberConfig["frequency_Hz"].asUInt(), &hyperion); if (xbmcVideoChecker != nullptr) { QObject::connect(xbmcVideoChecker, SIGNAL(grabbingMode(GrabbingMode)), dispmanx, SLOT(setGrabbingMode(GrabbingMode))); QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), dispmanx, SLOT(setVideoMode(VideoMode))); } dispmanx->start(); std::cout << "Frame grabber created and started" << std::endl; } #else #if !defined(ENABLE_OSX) && !defined(ENABLE_FB) if (config.isMember("framegrabber")) { std::cerr << "The dispmanx framegrabber can not be instantiated, becuse it has been left out from the build" << std::endl; } #endif #endif #ifdef ENABLE_V4L2 // construct and start the v4l2 grabber if the configuration is present V4L2Wrapper * v4l2Grabber = nullptr; if (config.isMember("grabber-v4l2")) { const Json::Value & grabberConfig = config["grabber-v4l2"]; v4l2Grabber = new V4L2Wrapper( grabberConfig.get("device", "/dev/video0").asString(), grabberConfig.get("input", 0).asInt(), parseVideoStandard(grabberConfig.get("standard", "no-change").asString()), parsePixelFormat(grabberConfig.get("pixelFormat", "no-change").asString()), grabberConfig.get("width", -1).asInt(), grabberConfig.get("height", -1).asInt(), grabberConfig.get("frameDecimation", 2).asInt(), grabberConfig.get("sizeDecimation", 8).asInt(), grabberConfig.get("redSignalThreshold", 0.0).asDouble(), grabberConfig.get("greenSignalThreshold", 0.0).asDouble(), grabberConfig.get("blueSignalThreshold", 0.0).asDouble(), &hyperion, grabberConfig.get("priority", 800).asInt()); v4l2Grabber->set3D(parse3DMode(grabberConfig.get("mode", "2D").asString())); v4l2Grabber->setCropping( grabberConfig.get("cropLeft", 0).asInt(), grabberConfig.get("cropRight", 0).asInt(), grabberConfig.get("cropTop", 0).asInt(), grabberConfig.get("cropBottom", 0).asInt()); v4l2Grabber->start(); std::cout << "V4l2 grabber created and started" << std::endl; } #else if (config.isMember("grabber-v4l2")) { std::cerr << "The v4l2 grabber can not be instantiated, becuse it has been left out from the build" << std::endl; } #endif #ifdef ENABLE_FB // Construct and start the framebuffer grabber if the configuration is present FramebufferWrapper * fbGrabber = nullptr; if (config.isMember("framegrabber")) { const Json::Value & grabberConfig = config["framegrabber"]; fbGrabber = new FramebufferWrapper( grabberConfig.get("device", "/dev/fb0").asString(), grabberConfig["width"].asUInt(), grabberConfig["height"].asUInt(), grabberConfig["frequency_Hz"].asUInt(), &hyperion); if (xbmcVideoChecker != nullptr) { QObject::connect(xbmcVideoChecker, SIGNAL(grabbingMode(GrabbingMode)), fbGrabber, SLOT(setGrabbingMode(GrabbingMode))); QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), fbGrabber, SLOT(setVideoMode(VideoMode))); } fbGrabber->start(); std::cout << "Framebuffer grabber created and started" << std::endl; } #else #if !defined(ENABLE_DISPMANX) && !defined(ENABLE_OSX) if (config.isMember("framegrabber")) { std::cerr << "The framebuffer grabber can not be instantiated, becuse it has been left out from the build" << std::endl; } #endif #endif #ifdef ENABLE_OSX // Construct and start the osx grabber if the configuration is present OsxWrapper * osxGrabber = nullptr; if (config.isMember("framegrabber")) { const Json::Value & grabberConfig = config["framegrabber"]; osxGrabber = new OsxWrapper( grabberConfig.get("display", 0).asUInt(), grabberConfig["width"].asUInt(), grabberConfig["height"].asUInt(), grabberConfig["frequency_Hz"].asUInt(), &hyperion); if (xbmcVideoChecker != nullptr) { QObject::connect(xbmcVideoChecker, SIGNAL(grabbingMode(GrabbingMode)), osxGrabber, SLOT(setGrabbingMode(GrabbingMode))); QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), osxGrabber, SLOT(setVideoMode(VideoMode))); } osxGrabber->start(); std::cout << "OSX grabber created and started" << std::endl; } #else #if !defined(ENABLE_DISPMANX) && !defined(ENABLE_FB) if (config.isMember("framegrabber")) { std::cerr << "The osx grabber can not be instantiated, becuse it has been left out from the build" << std::endl; } #endif #endif // Create Json server if configuration is present JsonServer * jsonServer = nullptr; if (config.isMember("jsonServer")) { const Json::Value & jsonServerConfig = config["jsonServer"]; jsonServer = new JsonServer(&hyperion, jsonServerConfig["port"].asUInt()); std::cout << "Json server created and started on port " << jsonServer->getPort() << std::endl; } #ifdef ENABLE_PROTOBUF // Create Proto server if configuration is present ProtoServer * protoServer = nullptr; if (config.isMember("protoServer")) { const Json::Value & protoServerConfig = config["protoServer"]; protoServer = new ProtoServer(&hyperion, protoServerConfig["port"].asUInt()); std::cout << "Proto server created and started on port " << protoServer->getPort() << std::endl; } #endif // Create Boblight server if configuration is present BoblightServer * boblightServer = nullptr; if (config.isMember("boblightServer")) { const Json::Value & boblightServerConfig = config["boblightServer"]; boblightServer = new BoblightServer(&hyperion, boblightServerConfig["port"].asUInt()); std::cout << "Boblight server created and started on port " << boblightServer->getPort() << std::endl; } // run the application int rc = app.exec(); std::cout << "Application closed with code " << rc << std::endl; // Delete all component #ifdef ENABLE_DISPMANX delete dispmanx; #endif #ifdef ENABLE_FB delete fbGrabber; #endif #ifdef ENABLE_OSX delete osxGrabber; #endif #ifdef ENABLE_V4L2 delete v4l2Grabber; #endif delete xbmcVideoChecker; delete jsonServer; #ifdef ENABLE_PROTOBUF delete protoServer; #endif delete boblightServer; // leave application return rc; }
int main(int argc, char** argv) { std::cout << "Application build time: " << __DATE__ << " " << __TIME__ << std::endl; // Initialising QCoreApplication QCoreApplication app(argc, argv); std::cout << "QCoreApplication initialised" << std::endl; signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); if (argc < 2) { std::cout << "Missing required configuration file. Usage:" << std::endl; std::cout << "hyperiond [config.file]" << std::endl; return 1; } const std::string configFile = argv[1]; std::cout << "Selected configuration file: " << configFile.c_str() << std::endl; const Json::Value config = loadConfig(configFile); Hyperion hyperion(config); std::cout << "Hyperion created and initialised" << std::endl; // create boot sequence if the configuration is present if (config.isMember("bootsequence")) { const Json::Value effectConfig = config["bootsequence"]; // Get the parameters for the bootsequence const std::string effectName = effectConfig["effect"].asString(); const unsigned duration_ms = effectConfig["duration_ms"].asUInt(); const int priority = 0; if (hyperion.setEffect(effectName, priority, duration_ms) == 0) { std::cout << "Boot sequence(" << effectName << ") created and started" << std::endl; } else { std::cout << "Failed to start boot sequence: " << effectName << std::endl; } } // create XBMC video checker if the configuration is present XBMCVideoChecker * xbmcVideoChecker = nullptr; if (config.isMember("xbmcVideoChecker")) { const Json::Value & videoCheckerConfig = config["xbmcVideoChecker"]; xbmcVideoChecker = new XBMCVideoChecker( videoCheckerConfig["xbmcAddress"].asString(), videoCheckerConfig["xbmcTcpPort"].asUInt(), videoCheckerConfig["grabVideo"].asBool(), videoCheckerConfig["grabPictures"].asBool(), videoCheckerConfig["grabAudio"].asBool(), videoCheckerConfig["grabMenu"].asBool(), videoCheckerConfig.get("grabScreensaver", true).asBool(), videoCheckerConfig.get("enable3DDetection", true).asBool()); xbmcVideoChecker->start(); std::cout << "XBMC video checker created and started" << std::endl; } #ifdef ENABLE_DISPMANX // Construct and start the frame-grabber if the configuration is present DispmanxWrapper * dispmanx = nullptr; if (config.isMember("framegrabber")) { const Json::Value & frameGrabberConfig = config["framegrabber"]; dispmanx = new DispmanxWrapper( frameGrabberConfig["width"].asUInt(), frameGrabberConfig["height"].asUInt(), frameGrabberConfig["frequency_Hz"].asUInt(), &hyperion); if (xbmcVideoChecker != nullptr) { QObject::connect(xbmcVideoChecker, SIGNAL(grabbingMode(GrabbingMode)), dispmanx, SLOT(setGrabbingMode(GrabbingMode))); QObject::connect(xbmcVideoChecker, SIGNAL(videoMode(VideoMode)), dispmanx, SLOT(setVideoMode(VideoMode))); } dispmanx->start(); std::cout << "Frame grabber created and started" << std::endl; } #else if (config.isMember("framegrabber")) { std::cerr << "The dispmanx framegrabber can not be instantiated, becuse it has been left out from the build" << std::endl; } #endif // Create Json server if configuration is present JsonServer * jsonServer = nullptr; if (config.isMember("jsonServer")) { const Json::Value & jsonServerConfig = config["jsonServer"]; jsonServer = new JsonServer(&hyperion, jsonServerConfig["port"].asUInt()); std::cout << "Json server created and started on port " << jsonServer->getPort() << std::endl; } // Create Proto server if configuration is present ProtoServer * protoServer = nullptr; if (config.isMember("protoServer")) { const Json::Value & protoServerConfig = config["protoServer"]; protoServer = new ProtoServer(&hyperion, protoServerConfig["port"].asUInt()); std::cout << "Proto server created and started on port " << protoServer->getPort() << std::endl; } // Create Boblight server if configuration is present BoblightServer * boblightServer = nullptr; if (config.isMember("boblightServer")) { const Json::Value & boblightServerConfig = config["boblightServer"]; boblightServer = new BoblightServer(&hyperion, boblightServerConfig["port"].asUInt()); std::cout << "Boblight server created and started on port " << boblightServer->getPort() << std::endl; } // run the application int rc = app.exec(); std::cout << "Application closed with code " << rc << std::endl; // Delete all component #ifdef ENABLE_DISPMANX delete dispmanx; #endif delete xbmcVideoChecker; delete jsonServer; delete protoServer; delete boblightServer; // leave application return rc; }