ComponentRegister.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <hyperion/ComponentRegister.h>
  2. #include <iostream>
  3. #include <hyperion/Hyperion.h>
  4. #include <hyperion/GrabberWrapper.h>
  5. using namespace hyperion;
  6. ComponentRegister::ComponentRegister(Hyperion* hyperion)
  7. : _hyperion(hyperion)
  8. , _log(nullptr)
  9. {
  10. QString subComponent = hyperion->property("instance").toString();
  11. _log= Logger::getInstance("COMPONENTREG", subComponent);
  12. // init all comps to false
  13. QVector<hyperion::Components> vect;
  14. vect << COMP_ALL << COMP_SMOOTHING << COMP_LEDDEVICE;
  15. bool areScreenGrabberAvailable = !GrabberWrapper::availableGrabbers(GrabberTypeFilter::VIDEO).isEmpty();
  16. bool areVideoGrabberAvailable = !GrabberWrapper::availableGrabbers(GrabberTypeFilter::VIDEO).isEmpty();
  17. bool areAudioGrabberAvailable = !GrabberWrapper::availableGrabbers(GrabberTypeFilter::AUDIO).isEmpty();
  18. bool flatBufServerAvailable { false };
  19. bool protoBufServerAvailable{ false };
  20. #if defined(ENABLE_FLATBUF_SERVER)
  21. flatBufServerAvailable = true;
  22. #endif
  23. #if defined(ENABLE_PROTOBUF_SERVER)
  24. protoBufServerAvailable = true;
  25. #endif
  26. if (areScreenGrabberAvailable)
  27. {
  28. vect << COMP_GRABBER;
  29. }
  30. if (areAudioGrabberAvailable)
  31. {
  32. vect << COMP_AUDIO;
  33. }
  34. if (areVideoGrabberAvailable)
  35. {
  36. vect << COMP_V4L;
  37. }
  38. if (areScreenGrabberAvailable || areVideoGrabberAvailable || flatBufServerAvailable || protoBufServerAvailable)
  39. {
  40. vect << COMP_BLACKBORDER;
  41. }
  42. #if defined(ENABLE_BOBLIGHT_SERVER)
  43. vect << COMP_BOBLIGHTSERVER;
  44. #endif
  45. #if defined(ENABLE_FORWARDER)
  46. vect << COMP_FORWARDER;
  47. #endif
  48. for(auto e : std::as_const(vect))
  49. {
  50. _componentStates.emplace(e, (e == COMP_ALL));
  51. }
  52. connect(_hyperion, &Hyperion::compStateChangeRequest, this, &ComponentRegister::handleCompStateChangeRequest);
  53. connect(_hyperion, &Hyperion::compStateChangeRequestAll, this, &ComponentRegister::handleCompStateChangeRequestAll);
  54. }
  55. ComponentRegister::~ComponentRegister()
  56. {
  57. }
  58. int ComponentRegister::isComponentEnabled(hyperion::Components comp) const
  59. {
  60. return (_componentStates.count(comp)) ? _componentStates.at(comp) : -1;
  61. }
  62. void ComponentRegister::setNewComponentState(hyperion::Components comp, bool isActive)
  63. {
  64. if (_componentStates.count(comp) > 0)
  65. {
  66. if (_componentStates[comp] != isActive)
  67. {
  68. Debug(_log, "%s: %s", componentToString(comp), (isActive ? "enabled" : "disabled"));
  69. _componentStates[comp] = isActive;
  70. // emit component has changed state
  71. emit updatedComponentState(comp, isActive);
  72. }
  73. }
  74. }
  75. void ComponentRegister::handleCompStateChangeRequest(hyperion::Components comps, bool isActive)
  76. {
  77. if(comps == COMP_ALL )
  78. {
  79. handleCompStateChangeRequestAll(isActive,{});
  80. }
  81. }
  82. void ComponentRegister::handleCompStateChangeRequestAll(bool isActive, const ComponentList& excludeList)
  83. {
  84. if (!_inProgress)
  85. {
  86. _inProgress = true;
  87. if(!isActive)
  88. {
  89. if (excludeList.isEmpty())
  90. {
  91. Debug(_log,"Disable Hyperion instance, store current components' state");
  92. }
  93. else
  94. {
  95. Debug(_log,"Disable selected Hyperion components, store their current state");
  96. }
  97. for(const auto &comp : _componentStates)
  98. {
  99. if (!excludeList.contains(comp.first) && comp.first != COMP_ALL)
  100. {
  101. // save state
  102. _prevComponentStates.emplace(comp.first, comp.second);
  103. // disable if enabled
  104. if(comp.second)
  105. {
  106. emit _hyperion->compStateChangeRequest(comp.first, false);
  107. }
  108. }
  109. }
  110. if (excludeList.isEmpty())
  111. {
  112. setNewComponentState(COMP_ALL, false);
  113. }
  114. }
  115. else
  116. {
  117. if(isActive && !_prevComponentStates.empty())
  118. {
  119. if (excludeList.isEmpty())
  120. {
  121. Debug(_log,"Enable Hyperion instance, restore components' previous state");
  122. }
  123. else
  124. {
  125. Debug(_log,"Enable selected Hyperion components, restore their previous state");
  126. }
  127. for(const auto &comp : _prevComponentStates)
  128. {
  129. if (!excludeList.contains(comp.first) && comp.first != COMP_ALL)
  130. {
  131. // if comp was enabled, enable again
  132. if(comp.second)
  133. {
  134. emit _hyperion->compStateChangeRequest(comp.first, true);
  135. }
  136. }
  137. }
  138. _prevComponentStates.clear();
  139. if (excludeList.isEmpty())
  140. {
  141. setNewComponentState(COMP_ALL, true);
  142. }
  143. }
  144. }
  145. _inProgress = false;
  146. }
  147. }