Camera event stream, video player, some security tweaks

This commit is contained in:
ftc
2023-07-04 19:19:36 +03:00
parent 477afbecd1
commit 8aad01dc3c
9 changed files with 1951 additions and 7 deletions
+2
View File
@@ -21,8 +21,10 @@ class AppAsset extends AssetBundle
public $baseUrl = '@web';
public $css = [
'css/site.css',
'css/video-js.css',
];
public $js = [
'js/video.min.js',
];
public $depends = [
'yii\web\YiiAsset',
+4
View File
@@ -54,4 +54,8 @@ class ApiClient
urlencode('StartTime >=:'.$start_time).'.json?page='.$page);
}
public function getEvent($id)
{
return $this->sendRequest('events/'.$id.'.json');
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ $params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic-console',
'id' => 'glade-cameras',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\commands',
+2 -1
View File
@@ -4,7 +4,8 @@ $params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'id' => 'glade',
'name' => 'Ясная Поляна',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
+46 -1
View File
@@ -5,6 +5,9 @@ namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\ForbiddenHttpException;
class CameraController extends Controller
{
public function actionStream($id, $scale = 50)
@@ -72,12 +75,54 @@ class CameraController extends Controller
return $result;
}
public function actionViewArchive($event_id)
private function getZMEvent($event_id)
{
$client = new \app\components\ApiClient(Yii::$app->params['zmUrl'].'zm/api');
$json = $client->getEvent($event_id);
$r = json_decode($json, true);
$e = new \app\models\zm\Event($r['event']['Event']);
return $e;
}
private function checkCameraAccess($camera_id, $event_id)
{
$cms = Yii::$app->params['cameras'];
if (!isset($cms[$camera_id]))
{
throw new NotFoundHttpException();
}
$e = $this->getZMEvent($event_id);
if ($e->monitor_id !== $camera_id)
{
throw new ForbiddenHttpException();
}
}
public function actionViewArchive($camera_id, $event_id)
{
$this->checkCameraAccess($camera_id, $event_id);
return $this->render('view_archive',
[
'camera_id' => $camera_id,
'id' => $event_id,
'event' => $this->getZMEvent($event_id),
]);
}
public function actionStreamEvent($camera_id, $event_id)
{
$this->checkCameraAccess($camera_id, $event_id);
$source_url = Yii::$app->params['zmUrl'].'/zm/index.php?mode=mpeg&format=h264&eid='.$event_id.'&view=view_video';
$fp = fopen($source_url, 'r', false);
if ($fp)
{
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
// pass data
fpassthru($fp);
fclose($fp);
}
else throw new NotFoundHttpException();
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ $this->title = 'Камера '.$id;
Архив записей:<br/>
<ul>
<?php foreach ($archive as $e): ?>
<li><?= Html::a($e->start_time." - ".$e->end_time, ['camera/view-archive', 'event_id' => $e->id]); ?></li>
<li><?= Html::a($e->start_time." - ".$e->end_time, ['camera/view-archive', 'camera_id' => $id, 'event_id' => $e->id]); ?></li>
<?php endforeach; ?>
</ul>
</div>
+22 -1
View File
@@ -4,10 +4,31 @@
use yii\helpers\Url;
$this->title = 'Архив, запись #'.$id;
$source = Url::to(['camera/stream-event', 'camera_id' => $camera_id, 'event_id' => $id]);
?>
<div class="site-index">
<div class="body-content">
<div class="row">
TODO!
<?= $event->start_time; ?>&mdash; <?= $event->end_time; ?>
</div>
<div class="row">
<video
id="my-video"
class="video-js"
width="1920"
height="1080"
data-setup='{"controls": true, "fluid": false, "autoplay": true, "preload": "auto", "playbackRates": [ 0,0.25,0.5,1,2,5,10,16]}'
src="<?= $source; ?>"
>
<source src="<?= $source; ?>" type="video/mp4">
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to a
web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>
</p>
</video>
</div>
</div>
</div>
+1831
View File
File diff suppressed because one or more lines are too long
+40
View File
File diff suppressed because one or more lines are too long