api.chanshots.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /**
  73. * PixelCraft instance for further chanshots processing
  74. *
  75. * @var object
  76. */
  77. protected $pixelCraft = '';
  78. /**
  79. * Channel screenshots validation flag
  80. *
  81. * @var bool
  82. */
  83. protected $shotsValidationFlag = false;
  84. /**
  85. * Channel screenshots embedding flag
  86. *
  87. * @var bool
  88. */
  89. protected $shotsEmbedFlag = false;
  90. /**
  91. * Contains base64 encoded latest checked channel shot
  92. *
  93. * @var string
  94. */
  95. protected $lastCheckedShot = '';
  96. /**
  97. * Contains embedded channel shots watermark path
  98. *
  99. * @var string
  100. */
  101. protected $shotsWatermarkPath = '';
  102. public function __construct() {
  103. $this->loadConfigs();
  104. $this->initPixelCraft();
  105. }
  106. /**
  107. * Some predefined paths here
  108. */
  109. const SHOTS_SUBDIR = 'chanshots/';
  110. const SHOTS_EXT = '.jpg';
  111. const CHANSHOTS_PID = 'CHANSHOTS';
  112. /**
  113. * predefined shots paths
  114. */
  115. const ERR_NOSIG = 'skins/nosignal.gif';
  116. const ERR_CORRUPT = 'skins/error.gif';
  117. const ERR_DISABLD = 'skins/chanblock.gif';
  118. /**
  119. * Loads some required configs
  120. *
  121. * @global $ubillingConfig
  122. *
  123. * @return void
  124. */
  125. protected function loadConfigs() {
  126. global $ubillingConfig;
  127. $this->binPaths = $ubillingConfig->getBinpaths();
  128. $this->altCfg = $ubillingConfig->getAlter();
  129. $this->ffmpgPath = $this->binPaths['FFMPG_PATH'];
  130. $this->screenshotsPath = Storages::PATH_HOWL . self::SHOTS_SUBDIR;
  131. $this->shotsValidationFlag = $ubillingConfig->getAlterParam('CHANSHOTS_VALIDATION');
  132. $this->shotsEmbedFlag = $ubillingConfig->getAlterParam('CHANSHOTS_EMBED');
  133. $this->shotsWatermarkPath = $ubillingConfig->getAlterParam('CHANSHOTS_WATERMARK');
  134. }
  135. /**
  136. * Inits PixelCraft object instance
  137. *
  138. * @return void
  139. */
  140. protected function initPixelCraft() {
  141. $this->pixelCraft = new PixelCraft();
  142. //loading watermark once
  143. if (!empty($this->shotsWatermarkPath)) {
  144. $this->pixelCraft->loadWatermark($this->shotsWatermarkPath);
  145. }
  146. }
  147. /**
  148. * Inits cameras into protected prop and loads its full data
  149. *
  150. * @return void
  151. */
  152. protected function initCameras() {
  153. $this->cameras = new Cameras();
  154. $this->allCamerasData = $this->cameras->getAllCamerasFullData();
  155. }
  156. /**
  157. * Inits recorder instance for detecting is camera alive or not?
  158. *
  159. * @return void
  160. */
  161. protected function initRecorder() {
  162. $this->recorder = new Recorder();
  163. $this->cameraRecordersRunning = $this->recorder->getRunningRecorders();
  164. }
  165. /**
  166. * Returns screenshots base path
  167. *
  168. * @return string
  169. */
  170. public function getScreenshotsPath() {
  171. return ($this->screenshotsPath);
  172. }
  173. /**
  174. * Checks if a channel screenshot is valid.
  175. *
  176. * @param string $channelScreenshot The path to the channel screenshot file.
  177. * @return bool Returns true if the channel screenshot is valid, false otherwise.
  178. */
  179. public function isChannelScreenshotValid($channelScreenshot) {
  180. $result = true;
  181. if ($this->shotsValidationFlag) {
  182. $result = false;
  183. if (file_exists($channelScreenshot)) {
  184. $imageValid = $this->pixelCraft->isImageValid($channelScreenshot);
  185. if ($imageValid) {
  186. $result = true;
  187. if ($this->shotsEmbedFlag) {
  188. $this->embedScreenshotProcessing($channelScreenshot);
  189. }
  190. } else {
  191. if ($this->shotsEmbedFlag) {
  192. $this->lastCheckedShot = '';
  193. }
  194. }
  195. }
  196. }
  197. return ($result);
  198. }
  199. /**
  200. * Embedded channel screenshot processing
  201. *
  202. * @param string $channelScreenshot
  203. *
  204. * @return void
  205. */
  206. protected function embedScreenshotProcessing($channelScreenshot) {
  207. if ($this->shotsEmbedFlag) {
  208. $this->pixelCraft->loadImage($channelScreenshot);
  209. if (!empty($this->shotsWatermarkPath)) {
  210. $this->pixelCraft->drawWatermark(false, 0, 0);
  211. }
  212. $this->lastCheckedShot = $this->pixelCraft->getImageBase('jpeg', true);
  213. }
  214. }
  215. /**
  216. * Returns lastCheckedShot property content
  217. *
  218. * @return string
  219. */
  220. public function getLastCheckedShot() {
  221. return ($this->lastCheckedShot);
  222. }
  223. /**
  224. * Returns channel latest screenshot path
  225. *
  226. * @param string $channelId
  227. *
  228. * @return string/void
  229. */
  230. public function getChannelScreenShot($channelId) {
  231. $result = '';
  232. $channelId = ubRouting::filters($channelId, 'mres');
  233. if (file_exists($this->screenshotsPath)) {
  234. $screenshotName = $channelId . self::SHOTS_EXT;
  235. $fullPath = $this->screenshotsPath . $screenshotName;
  236. if (file_exists($fullPath)) {
  237. $result = $fullPath;
  238. }
  239. }
  240. return ($result);
  241. }
  242. /**
  243. * Inits storages into protected prop for further usage
  244. *
  245. * @return void
  246. */
  247. protected function initStorages() {
  248. $this->storages = new Storages();
  249. }
  250. /**
  251. * Allocates screenshots path, returns it if its writable
  252. *
  253. * @return string/void on error
  254. */
  255. protected function allocateScreenshotsPath() {
  256. $result = '';
  257. if (!file_exists($this->screenshotsPath)) {
  258. mkdir($this->screenshotsPath, 0777);
  259. chmod($this->screenshotsPath, 0777);
  260. log_register('CHANSHOTS ALLOCATED `' . $this->screenshotsPath . '`');
  261. } else {
  262. $result = $this->screenshotsPath;
  263. }
  264. return ($result);
  265. }
  266. /**
  267. * Cleanups old screenshot if it exists
  268. *
  269. * @param string $channel
  270. *
  271. * @return void
  272. */
  273. protected function flushOldScreenshot($channel) {
  274. if (!empty($channel)) {
  275. $fileName = $channel . self::SHOTS_EXT;
  276. $fullScreenShotPath = $this->screenshotsPath . $fileName;
  277. if (file_exists($fullScreenShotPath)) {
  278. unlink($fullScreenShotPath);
  279. }
  280. }
  281. }
  282. /**
  283. * Tooks screenshot from some channel chunk
  284. *
  285. * @param string $chunkPath
  286. * @param string $channel
  287. *
  288. * @return string
  289. */
  290. protected function takeChunkScreenshot($chunkPath, $channel) {
  291. $result = '';
  292. if (!empty($channel) and file_exists($chunkPath)) {
  293. $fileName = $channel . self::SHOTS_EXT;
  294. $fullScreenShotPath = $this->screenshotsPath . $fileName;
  295. //old screenshot cleanup
  296. $this->flushOldScreenshot($channel);
  297. //taking new screenshot for this channel
  298. $command = $this->ffmpgPath . ' -ss ' . $this->timeOffset . ' -i ' . $chunkPath . ' ' . $this->screenshotOpts . ' ' . $fullScreenShotPath;
  299. $result = shell_exec($command);
  300. }
  301. return ($result);
  302. }
  303. /**
  304. * Performs capturing screeenshots from all active camera channels
  305. *
  306. * @return void
  307. */
  308. public function run() {
  309. $process = new StarDust(self::CHANSHOTS_PID);
  310. if ($process->notRunning()) {
  311. $process->start();
  312. //preload required objects
  313. $this->initStorages();
  314. $this->initCameras();
  315. //any cameras here?
  316. if (!empty($this->allCamerasData)) {
  317. $this->initRecorder();
  318. $this->allocateScreenshotsPath();
  319. foreach ($this->allCamerasData as $eachCamId => $eachCameraData) {
  320. if ($eachCameraData['CAMERA']['active']) {
  321. $channel = $eachCameraData['CAMERA']['channel'];
  322. $storageId = $eachCameraData['CAMERA']['storageid'];
  323. //camera recorder now running?
  324. if (isset($this->cameraRecordersRunning[$eachCamId])) {
  325. $allCameraChunks = $this->storages->getChannelChunks($storageId, $channel);
  326. $chunksCount = sizeof($allCameraChunks);
  327. //dont shot single chunk - it may be unfinished
  328. if ($chunksCount > 1) {
  329. $lastChunk = array_pop($allCameraChunks);
  330. $secondLastChunk = array_pop($allCameraChunks);
  331. //taking screenshot from second last channel chunk
  332. $this->takeChunkScreenshot($secondLastChunk, $channel);
  333. }
  334. } else {
  335. //if no recorder now running just flush channel scrreenshot
  336. $this->flushOldScreenshot($channel);
  337. }
  338. }
  339. }
  340. }
  341. $process->stop();
  342. }
  343. }
  344. }