api.streamdog.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Live streams camera keep alive events management
  4. */
  5. class StreamDog {
  6. /**
  7. * System caching object placeholder
  8. *
  9. * @var object
  10. */
  11. protected $cache = '';
  12. /**
  13. * Predefined stuff here
  14. */
  15. const TIMEOUT = 60;
  16. const SUB_TIMEOUT=600;
  17. const CACHE_KEY = 'KEEPALIVE_';
  18. const CACHE_SUB = 'KEEPSUBALIVE_';
  19. const ROUTE_KEEPALIVE = 'keepstreamalive';
  20. const ROUTE_KEEPSUBALIVE = 'keepsubalive';
  21. public function __construct() {
  22. $this->initCache();
  23. }
  24. /**
  25. * Inits caching object instance for further usage
  26. *
  27. * @return void
  28. */
  29. protected function initCache() {
  30. $this->cache = new UbillingCache();
  31. }
  32. /**
  33. * Sets camera as alive
  34. *
  35. * @param int $cameraId
  36. *
  37. * @return void
  38. */
  39. public function keepAlive($cameraId) {
  40. $cameraId = ubRouting::filters($cameraId, 'int');
  41. $cacheKey = self::CACHE_KEY . $cameraId;
  42. $cachedData = $this->cache->get($cacheKey, self::TIMEOUT);
  43. if (empty($cachedData)) {
  44. $cachedData = time();
  45. }
  46. $this->cache->set($cacheKey, $cachedData, self::TIMEOUT);
  47. }
  48. /**
  49. * Sets camera sub-stream as alive
  50. *
  51. * @param int $cameraId
  52. *
  53. * @return void
  54. */
  55. public function keepSubAlive($cameraId) {
  56. $cameraId = ubRouting::filters($cameraId, 'int');
  57. $cacheKey = self::CACHE_SUB . $cameraId;
  58. $cachedData = $this->cache->get($cacheKey, self::SUB_TIMEOUT);
  59. if (empty($cachedData)) {
  60. $cachedData = time();
  61. }
  62. $this->cache->set($cacheKey, $cachedData, self::SUB_TIMEOUT);
  63. }
  64. /**
  65. * Checks is camera being watched by someone?
  66. *
  67. * @param int $cameraId
  68. *
  69. * @return bool
  70. */
  71. public function isCameraInUse($cameraId) {
  72. $result = false;
  73. $cameraId = ubRouting::filters($cameraId, 'int');
  74. $cacheKey = self::CACHE_KEY . $cameraId;
  75. $cachedData = $this->cache->get($cacheKey, self::TIMEOUT);
  76. if (!empty($cachedData)) {
  77. $result = true;
  78. }
  79. return ($result);
  80. }
  81. /**
  82. * Checks is camera low quality stream being watched by someone?
  83. *
  84. * @param int $cameraId
  85. *
  86. * @return bool
  87. */
  88. public function isCameraSubInUse($cameraId) {
  89. $result = false;
  90. $cameraId = ubRouting::filters($cameraId, 'int');
  91. $cacheKey = self::CACHE_SUB . $cameraId;
  92. $cachedData = $this->cache->get($cacheKey, self::SUB_TIMEOUT);
  93. if (!empty($cachedData)) {
  94. $result = true;
  95. }
  96. return ($result);
  97. }
  98. /**
  99. * Returns keep-alive JS code
  100. *
  101. * @param string $url
  102. * @param inst $timeout
  103. *
  104. * @return string
  105. */
  106. public function getKeepAliveCallback($url, $timeout) {
  107. $result = '';
  108. $result .= wf_tag('script', false, '', 'type="text/javascript"');
  109. $result .= '
  110. function keepAliveRequest () {
  111. $.ajax({
  112. type: "GET",
  113. url: "' . $url . '",
  114. cache: false
  115. });
  116. }
  117. var timer = setInterval(keepAliveRequest, ' . $timeout . ');
  118. ';
  119. $result .= wf_tag('script', true);
  120. return ($result);
  121. }
  122. }