api.chanshots.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * Channel screenshots service implementation
  4. */
  5. class ChanShots {
  6. /**
  7. * Contains binpaths.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $binPaths = array();
  12. /**
  13. * Contains alter.ini config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $altCfg = array();
  18. /**
  19. * Cameras instance placeholder
  20. *
  21. * @var object
  22. */
  23. protected $cameras = '';
  24. /**
  25. * Recorder instance placeholder
  26. *
  27. * @var object
  28. */
  29. protected $recorder = '';
  30. /**
  31. * Storages instance placeholder.
  32. *
  33. * @var object
  34. */
  35. protected $storages = '';
  36. /**
  37. * Contains full cameras data as
  38. *
  39. * @var array
  40. */
  41. protected $allCamerasData = array();
  42. /**
  43. * Contains camera running recorders state as cameraId=>PID
  44. *
  45. * @var array
  46. */
  47. protected $cameraRecordersRunning = array();
  48. /**
  49. * Contains ffmpeg binary path
  50. *
  51. * @var string
  52. */
  53. protected $ffmpgPath = '';
  54. /**
  55. * Contains basic screenshots path
  56. *
  57. * @var string
  58. */
  59. protected $screenshotsPath = '';
  60. /**
  61. * Default chunk time offset to screenshot
  62. *
  63. * @var string
  64. */
  65. protected $timeOffset = '00:00:01';
  66. /**
  67. * Contains default screenshot options
  68. *
  69. * @var string
  70. */
  71. protected $screenshotOpts = '-loglevel error -frames:v 1 -q:v 15';
  72. public function __construct() {
  73. $this->loadConfigs();
  74. }
  75. /**
  76. * Some predefined paths here
  77. */
  78. const SHOTS_SUBDIR = 'chanshots/';
  79. const SHOTS_EXT = '.jpg';
  80. const CHANSHOTS_PID = 'CHANSHOTS';
  81. /**
  82. * Loads some required configs
  83. *
  84. * @global $ubillingConfig
  85. *
  86. * @return void
  87. */
  88. protected function loadConfigs() {
  89. global $ubillingConfig;
  90. $this->binPaths = $ubillingConfig->getBinpaths();
  91. $this->altCfg = $ubillingConfig->getAlter();
  92. $this->ffmpgPath = $this->binPaths['FFMPG_PATH'];
  93. $this->screenshotsPath = Storages::PATH_HOWL . self::SHOTS_SUBDIR;
  94. }
  95. /**
  96. * Inits cameras into protected prop and loads its full data
  97. *
  98. * @return void
  99. */
  100. protected function initCameras() {
  101. $this->cameras = new Cameras();
  102. $this->allCamerasData = $this->cameras->getAllCamerasFullData();
  103. }
  104. /**
  105. * Inits recorder instance for detecting is camera alive or not?
  106. *
  107. * @return void
  108. */
  109. protected function initRecorder() {
  110. $this->recorder = new Recorder();
  111. $this->cameraRecordersRunning = $this->recorder->getRunningRecorders();
  112. }
  113. /**
  114. * Returns screenshots base path
  115. *
  116. * @return string
  117. */
  118. public function getScreenshotsPath() {
  119. return($this->screenshotsPath);
  120. }
  121. /**
  122. * Returns channel latest screenshot path
  123. *
  124. * @param string $channelId
  125. *
  126. * @return string/void
  127. */
  128. public function getChannelScreenShot($channelId) {
  129. $result = '';
  130. $channelId = ubRouting::filters($channelId, 'mres');
  131. if (file_exists($this->screenshotsPath)) {
  132. $screenshotName = $channelId . self::SHOTS_EXT;
  133. $fullPath = $this->screenshotsPath . $screenshotName;
  134. if (file_exists($fullPath)) {
  135. $result = $fullPath;
  136. }
  137. }
  138. return($result);
  139. }
  140. /**
  141. * Inits storages into protected prop for further usage
  142. *
  143. * @return void
  144. */
  145. protected function initStorages() {
  146. $this->storages = new Storages();
  147. }
  148. /**
  149. * Allocates screenshots path, returns it if its writable
  150. *
  151. * @return string/void on error
  152. */
  153. protected function allocateScreenshotsPath() {
  154. $result = '';
  155. if (!file_exists($this->screenshotsPath)) {
  156. mkdir($this->screenshotsPath, 0777);
  157. chmod($this->screenshotsPath, 0777);
  158. log_register('CHANSHOTS ALLOCATED `' . $this->screenshotsPath . '`');
  159. } else {
  160. $result = $this->screenshotsPath;
  161. }
  162. return($result);
  163. }
  164. /**
  165. * Cleanups old screenshot if it exists
  166. *
  167. * @param string $channel
  168. *
  169. * @return void
  170. */
  171. protected function flushOldScreenshot($channel) {
  172. if (!empty($channel)) {
  173. $fileName = $channel . self::SHOTS_EXT;
  174. $fullScreenShotPath = $this->screenshotsPath . $fileName;
  175. if (file_exists($fullScreenShotPath)) {
  176. unlink($fullScreenShotPath);
  177. }
  178. }
  179. }
  180. /**
  181. * Tooks screenshot from some channel chunk
  182. *
  183. * @param string $chunkPath
  184. * @param string $channel
  185. *
  186. * @return string
  187. */
  188. protected function takeChunkScreenshot($chunkPath, $channel) {
  189. $result = '';
  190. if (!empty($channel) AND file_exists($chunkPath)) {
  191. $fileName = $channel . self::SHOTS_EXT;
  192. $fullScreenShotPath = $this->screenshotsPath . $fileName;
  193. //old screenshot cleanup
  194. $this->flushOldScreenshot($channel);
  195. //taking new screenshot for this channel
  196. $command = $this->ffmpgPath . ' -ss ' . $this->timeOffset . ' -i ' . $chunkPath . ' ' . $this->screenshotOpts . ' ' . $fullScreenShotPath;
  197. $result = shell_exec($command);
  198. }
  199. return($result);
  200. }
  201. /**
  202. * Performs capturing screeenshots from all active camera channels
  203. *
  204. * @return void
  205. */
  206. public function run() {
  207. $process = new StarDust(self::CHANSHOTS_PID);
  208. if ($process->notRunning()) {
  209. $process->start();
  210. //preload required objects
  211. $this->initStorages();
  212. $this->initCameras();
  213. //any cameras here?
  214. if (!empty($this->allCamerasData)) {
  215. $this->initRecorder();
  216. $this->allocateScreenshotsPath();
  217. foreach ($this->allCamerasData as $eachCamId => $eachCameraData) {
  218. if ($eachCameraData['CAMERA']['active']) {
  219. $channel = $eachCameraData['CAMERA']['channel'];
  220. $storageId = $eachCameraData['CAMERA']['storageid'];
  221. //camera recorder now running?
  222. if (isset($this->cameraRecordersRunning[$eachCamId])) {
  223. $allCameraChunks = $this->storages->getChannelChunks($storageId, $channel);
  224. $chunksCount = sizeof($allCameraChunks);
  225. //dont shot single chunk - it may be unfinished
  226. if ($chunksCount > 1) {
  227. $lastChunk = array_pop($allCameraChunks);
  228. $secondLastChunk = array_pop($allCameraChunks);
  229. //taking screenshot from second last channel chunk
  230. $this->takeChunkScreenshot($secondLastChunk, $channel);
  231. }
  232. } else {
  233. //if no recorder now running just flush channel scrreenshot
  234. $this->flushOldScreenshot($channel);
  235. }
  236. }
  237. }
  238. }
  239. $process->stop();
  240. }
  241. }
  242. /**
  243. * Renders small channel preview for camera lists
  244. *
  245. * @param string $channel
  246. * @param string $screenshot
  247. *
  248. * @return string
  249. */
  250. public function renderListBox($channel, $screenshot) {
  251. $result = wf_tag('style');
  252. $result .= ' .preview' . $channel . ' {
  253. position: relative;
  254. margin-right: 10px;
  255. }
  256. .preview' . $channel . ' img {
  257. object-fit: cover;
  258. width: 124px;
  259. height: 75px;
  260. }
  261. ';
  262. $result .= wf_tag('style', true);
  263. $result .= wf_tag('span', false, 'preview' . $channel);
  264. $result .= wf_tag('img', false, 'preview' . $channel, 'src="' . $screenshot . '" style="width:124px;"');
  265. $result .= wf_tag('span', true);
  266. return($result);
  267. }
  268. }