GrabberWrapper.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Hyperion includes
  2. #include <hyperion/GrabberWrapper.h>
  3. #include <hyperion/Grabber.h>
  4. #include <HyperionConfig.h>
  5. // utils includes
  6. #include <utils/GlobalSignals.h>
  7. // qt
  8. #include <QTimer>
  9. GrabberWrapper* GrabberWrapper::instance = nullptr;
  10. const int GrabberWrapper::DEFAULT_RATE_HZ = 25;
  11. const int GrabberWrapper::DEFAULT_MIN_GRAB_RATE_HZ = 1;
  12. const int GrabberWrapper::DEFAULT_MAX_GRAB_RATE_HZ = 30;
  13. const int GrabberWrapper::DEFAULT_PIXELDECIMATION = 8;
  14. /// Map of Hyperion instances with grabber name that requested screen capture
  15. QMap<int, QString> GrabberWrapper::GRABBER_SYS_CLIENTS = QMap<int, QString>();
  16. QMap<int, QString> GrabberWrapper::GRABBER_V4L_CLIENTS = QMap<int, QString>();
  17. QMap<int, QString> GrabberWrapper::GRABBER_AUDIO_CLIENTS = QMap<int, QString>();
  18. bool GrabberWrapper::GLOBAL_GRABBER_SYS_ENABLE = false;
  19. bool GrabberWrapper::GLOBAL_GRABBER_V4L_ENABLE = false;
  20. bool GrabberWrapper::GLOBAL_GRABBER_AUDIO_ENABLE = false;
  21. GrabberWrapper::GrabberWrapper(const QString& grabberName, Grabber * ggrabber, int updateRate_Hz)
  22. : _grabberName(grabberName)
  23. , _log(Logger::getInstance(grabberName.toUpper()))
  24. , _timer(new QTimer(this))
  25. , _updateInterval_ms(1000/updateRate_Hz)
  26. , _ggrabber(ggrabber)
  27. , _image(0,0)
  28. {
  29. GrabberWrapper::instance = this;
  30. // Configure the timer to generate events every n milliseconds
  31. _timer->setTimerType(Qt::PreciseTimer);
  32. _timer->setInterval(_updateInterval_ms);
  33. connect(_timer, &QTimer::timeout, this, &GrabberWrapper::action);
  34. // connect the image forwarding
  35. if (_grabberName.startsWith("V4L"))
  36. connect(this, &GrabberWrapper::systemImage, GlobalSignals::getInstance(), &GlobalSignals::setV4lImage);
  37. else if (_grabberName.startsWith("Audio"))
  38. connect(this, &GrabberWrapper::systemImage, GlobalSignals::getInstance(), &GlobalSignals::setAudioImage);
  39. else
  40. connect(this, &GrabberWrapper::systemImage, GlobalSignals::getInstance(), &GlobalSignals::setSystemImage);
  41. // listen for source requests
  42. connect(GlobalSignals::getInstance(), &GlobalSignals::requestSource, this, &GrabberWrapper::handleSourceRequest);
  43. }
  44. GrabberWrapper::~GrabberWrapper()
  45. {
  46. GrabberWrapper::instance = nullptr;
  47. }
  48. bool GrabberWrapper::start()
  49. {
  50. bool rc = false;
  51. if ( open() )
  52. {
  53. if (!_timer->isActive())
  54. {
  55. // Start the timer with the pre configured interval
  56. Debug(_log,"Grabber start()");
  57. _timer->start();
  58. }
  59. rc = _timer->isActive();
  60. }
  61. return rc;
  62. }
  63. void GrabberWrapper::stop()
  64. {
  65. if (_timer->isActive())
  66. {
  67. // Stop the timer, effectively stopping the process
  68. Debug(_log,"Grabber stop()");
  69. _timer->stop();
  70. }
  71. }
  72. bool GrabberWrapper::isActive() const
  73. {
  74. return _timer->isActive();
  75. }
  76. QStringList GrabberWrapper::getActive(int inst, GrabberTypeFilter type) const
  77. {
  78. QStringList result = QStringList();
  79. if (type == GrabberTypeFilter::SCREEN || type == GrabberTypeFilter::ALL)
  80. {
  81. if (GRABBER_SYS_CLIENTS.contains(inst))
  82. result << GRABBER_SYS_CLIENTS.value(inst);
  83. }
  84. if (type == GrabberTypeFilter::VIDEO || type == GrabberTypeFilter::ALL)
  85. {
  86. if (GRABBER_V4L_CLIENTS.contains(inst))
  87. result << GRABBER_V4L_CLIENTS.value(inst);
  88. }
  89. if (type == GrabberTypeFilter::AUDIO || type == GrabberTypeFilter::ALL)
  90. {
  91. if (GRABBER_AUDIO_CLIENTS.contains(inst))
  92. result << GRABBER_AUDIO_CLIENTS.value(inst);
  93. }
  94. return result;
  95. }
  96. QStringList GrabberWrapper::availableGrabbers(GrabberTypeFilter type)
  97. {
  98. QStringList grabbers;
  99. if (type == GrabberTypeFilter::SCREEN || type == GrabberTypeFilter::ALL)
  100. {
  101. #ifdef ENABLE_DISPMANX
  102. grabbers << "dispmanx";
  103. #endif
  104. #ifdef ENABLE_FB
  105. grabbers << "framebuffer";
  106. #endif
  107. #ifdef ENABLE_AMLOGIC
  108. grabbers << "amlogic";
  109. #endif
  110. #ifdef ENABLE_OSX
  111. grabbers << "osx";
  112. #endif
  113. #ifdef ENABLE_X11
  114. grabbers << "x11";
  115. #endif
  116. #ifdef ENABLE_XCB
  117. grabbers << "xcb";
  118. #endif
  119. #ifdef ENABLE_QT
  120. grabbers << "qt";
  121. #endif
  122. #ifdef ENABLE_DX
  123. grabbers << "dx";
  124. #endif
  125. }
  126. if (type == GrabberTypeFilter::VIDEO || type == GrabberTypeFilter::ALL)
  127. {
  128. #if defined(ENABLE_V4L2) || defined(ENABLE_MF)
  129. grabbers << "v4l2";
  130. #endif
  131. }
  132. if (type == GrabberTypeFilter::AUDIO || type == GrabberTypeFilter::ALL)
  133. {
  134. #ifdef ENABLE_AUDIO
  135. grabbers << "audio";
  136. #endif
  137. }
  138. return grabbers;
  139. }
  140. void GrabberWrapper::setVideoMode(VideoMode mode)
  141. {
  142. if (_ggrabber != nullptr)
  143. {
  144. Info(_log,"setVideoMode");
  145. _ggrabber->setVideoMode(mode);
  146. }
  147. }
  148. void GrabberWrapper::setFlipMode(const QString& flipMode)
  149. {
  150. _ggrabber->setFlipMode(parseFlipMode(flipMode));
  151. }
  152. void GrabberWrapper::setCropping(int cropLeft, int cropRight, int cropTop, int cropBottom)
  153. {
  154. _ggrabber->setCropping(cropLeft, cropRight, cropTop, cropBottom);
  155. }
  156. void GrabberWrapper::updateTimer(int interval)
  157. {
  158. if(_updateInterval_ms != interval)
  159. {
  160. _updateInterval_ms = interval;
  161. const bool& timerWasActive = _timer->isActive();
  162. _timer->stop();
  163. _timer->setInterval(_updateInterval_ms);
  164. if(timerWasActive)
  165. _timer->start();
  166. }
  167. }
  168. void GrabberWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
  169. {
  170. if (type == settings::SYSTEMCAPTURE &&
  171. !_grabberName.startsWith("V4L") &&
  172. !_grabberName.startsWith("Audio"))
  173. {
  174. // extract settings
  175. const QJsonObject& obj = config.object();
  176. // save current state
  177. bool isEnabled = getSysGrabberState();
  178. // set global grabber state
  179. setSysGrabberState(obj["enable"].toBool(false));
  180. if (getSysGrabberState())
  181. {
  182. // width/height
  183. _ggrabber->setWidthHeight(obj["width"].toInt(96), obj["height"].toInt(96));
  184. // display index for MAC
  185. _ggrabber->setDisplayIndex(obj["input"].toInt(0));
  186. // pixel decimation for x11
  187. _ggrabber->setPixelDecimation(obj["pixelDecimation"].toInt(DEFAULT_PIXELDECIMATION));
  188. // crop for system capture
  189. _ggrabber->setCropping(
  190. obj["cropLeft"].toInt(0),
  191. obj["cropRight"].toInt(0),
  192. obj["cropTop"].toInt(0),
  193. obj["cropBottom"].toInt(0));
  194. _ggrabber->setFramerate(obj["fps"].toInt(DEFAULT_RATE_HZ));
  195. // eval new update time
  196. updateTimer(_ggrabber->getUpdateInterval());
  197. // start if current state is not true
  198. if (!isEnabled)
  199. {
  200. start();
  201. }
  202. }
  203. else
  204. {
  205. stop();
  206. }
  207. }
  208. }
  209. void GrabberWrapper::handleSourceRequest(hyperion::Components component, int hyperionInd, bool listen)
  210. {
  211. if (component == hyperion::Components::COMP_GRABBER &&
  212. !_grabberName.startsWith("V4L") &&
  213. !_grabberName.startsWith("Audio"))
  214. {
  215. if (listen)
  216. GRABBER_SYS_CLIENTS.insert(hyperionInd, _grabberName);
  217. else
  218. GRABBER_SYS_CLIENTS.remove(hyperionInd);
  219. if (GRABBER_SYS_CLIENTS.empty() || !getSysGrabberState())
  220. stop();
  221. else
  222. start();
  223. }
  224. else if (component == hyperion::Components::COMP_V4L &&
  225. _grabberName.startsWith("V4L"))
  226. {
  227. if (listen)
  228. GRABBER_V4L_CLIENTS.insert(hyperionInd, _grabberName);
  229. else
  230. GRABBER_V4L_CLIENTS.remove(hyperionInd);
  231. if (GRABBER_V4L_CLIENTS.empty() || !getV4lGrabberState())
  232. stop();
  233. else
  234. start();
  235. }
  236. else if (component == hyperion::Components::COMP_AUDIO &&
  237. _grabberName.startsWith("Audio"))
  238. {
  239. if (listen)
  240. GRABBER_AUDIO_CLIENTS.insert(hyperionInd, _grabberName);
  241. else
  242. GRABBER_AUDIO_CLIENTS.remove(hyperionInd);
  243. if (GRABBER_AUDIO_CLIENTS.empty() || !getAudioGrabberState())
  244. stop();
  245. else
  246. start();
  247. }
  248. }
  249. void GrabberWrapper::tryStart()
  250. {
  251. // verify start condition
  252. if (!_grabberName.startsWith("V4L") &&
  253. !_grabberName.startsWith("Audio") &&
  254. !GRABBER_SYS_CLIENTS.empty() &&
  255. getSysGrabberState())
  256. {
  257. start();
  258. }
  259. }