Skip to content

Commit 163ed30

Browse files
authored
Merge pull request #90 from lbr38/devel
6.2.0
2 parents 69e4a09 + 14ee063 commit 163ed30

File tree

31 files changed

+410
-111
lines changed

31 files changed

+410
-111
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
A dockerized web responsive interface to manage <a href="https://github.com/Motion-Project/motion"><b>motion</b></a> (an open-source motion detection software) and visualize cameras live stream.
44

55
<div align="center">
6-
<img src="https://github.com/user-attachments/assets/bdae2550-819d-40c4-895b-541ee64bdc03" width=25% align="top">
6+
<img src="https://github.com/user-attachments/assets/964f1307-c295-49f6-82e5-db6326f909b4" width=25% align="top">
77
&nbsp;
8-
<img src="https://github.com/user-attachments/assets/afe3e48a-3a26-4e75-a6a7-a97b2ac2bf9e" width=25% align="top">
8+
<img src="https://github.com/user-attachments/assets/caa8944d-5d8e-4b4b-a706-5a2e469dac7e" width=25% align="top">
99
&nbsp;
10-
<img src="https://github.com/user-attachments/assets/a2472f8b-24fc-4967-bb6a-f8ad8af95270" width=25% align="top">
10+
<img src="https://github.com/user-attachments/assets/7a9f1efd-bef0-42b9-ba8f-6c7bde8be028" width=25% align="top">
1111
</div>
1212
<br>
1313
<div align="center">
14-
<img src="https://github.com/user-attachments/assets/cb9137c7-484a-4c2c-ad0f-c33ef7a602bd" width=25% align="top">
14+
<img src="https://github.com/user-attachments/assets/0c3876f5-dfa6-45bd-a750-ca8b4cfe1133" width=25% align="top">
1515
&nbsp;
16-
<img src="https://github.com/user-attachments/assets/81c05e3f-599d-4cc1-9d9a-9748fce54763" width=25% align="top">
16+
<img src="https://github.com/user-attachments/assets/68cd77fd-69cf-46f4-aca2-528d5d42077f" width=25% align="top">
1717
&nbsp;
18-
<img src="https://github.com/user-attachments/assets/04b18116-2af0-4bd3-8438-e9f1fed8c7ed" width=25% align="top">
18+
<img src="https://github.com/user-attachments/assets/ba3352ea-4174-4707-9f4c-9f6bf2074968" width=25% align="top">
1919
</div>
2020

2121
<br>

docker/config/go2rtc/init

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@ test -f $DAEMON || exit 0
2121
#
2222
function start()
2323
{
24-
LOGFILE="/var/lib/motionui/go2rtc/logs/$(date +'%Y-%m-%d_%H-%M-%S')-go2rtc.log"
24+
# If go2rtc is running
25+
if ps aux | grep "/usr/local/bin/go2rtc" | grep -q -v "grep"; then
26+
echo "go2rtc is already running"
27+
exit 0
28+
fi
2529

26-
echo "Starting go2rtc"
30+
# Clean any existing PID file
31+
rm -f "$PIDFILE"
32+
33+
LOGFILE="/var/lib/motionui/go2rtc/logs/$(date +'%Y-%m-%d_%H-%M-%S')-go2rtc.log"
2734

2835
# Create PID directory
29-
mkdir -p /run/go2rtc -m 750
36+
mkdir -p /run/go2rtc
37+
chmod 755 /run/go2rtc
3038
chown www-data:www-data /run/go2rtc
3139

3240
# Create log directory
@@ -61,34 +69,58 @@ function start()
6169
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile \
6270
--chuid www-data --umask 002 \
6371
--startas /bin/bash -- -c "exec $DAEMON $DAEMON_OPTS > /var/lib/motionui/go2rtc/logs/go2rtc.log 2>&1"
72+
73+
# If no PID file was created, the process did not start properly
74+
if [ ! -f "$PIDFILE" ]; then
75+
echo "Error starting go2rtc: PID file is missing"
76+
exit 1
77+
fi
78+
79+
chmod 600 "$PIDFILE"
80+
chown www-data:www-data /run/go2rtc/go2rtc.pid
81+
82+
PID=$(cat "$PIDFILE")
83+
84+
# If the PID is empty, the process is not running
85+
if [ -z "$PID" ]; then
86+
echo "Error starting go2rtc: PID file is empty"
87+
exit 1
88+
fi
89+
90+
echo "go2rtc started, PID: $PID"
6491
}
6592

6693
#
6794
# Stop the go2rtc stream server
6895
#
6996
function stop()
7097
{
71-
echo "Stopping go2rtc"
98+
i="0"
7299

73-
if [ -f "$PIDFILE" ]; then
74-
PID=$(cat "$PIDFILE")
100+
# If go2rtc is running
101+
if ps aux | grep "/usr/local/bin/go2rtc" | grep -q -v "grep"; then
102+
echo "Stopping go2rtc"
75103

76-
# If the PID is empty, the process is not running
77-
if [ -z "$PID" ]; then
78-
exit 0
79-
fi
104+
# If a PID file exists, get the PID and kill the process
105+
if [ -f "$PIDFILE" ]; then
106+
PID=$(cat "$PIDFILE")
80107

81-
# Check if a process with this PID is still running
82-
if /usr/bin/ps -p "$PID" > /dev/null; then
83-
# echo "Force stopping go2rtc (kill)"
84-
kill "$PID"
108+
# If the PID is empty, the process is not running
109+
if [ -z "$PID" ]; then
110+
exit 0
111+
fi
112+
113+
# Check if a process with this PID is still running
114+
if /usr/bin/ps -p "$PID" > /dev/null; then
115+
# echo "Force stopping go2rtc (kill)"
116+
kill "$PID"
117+
fi
85118
fi
86119

87120
# Wait for the process to stop (timeout 10s)
88-
i="0"
89121
while true; do
90122
# Check if the process is still running, quit if not
91-
if ! ps aux | grep -q "/usr/local/bin/go2rtc" | grep -v "grep"; then
123+
if ! ps aux | grep "/usr/local/bin/go2rtc" | grep -q -v "grep"; then
92124
break
93125
fi
94126

@@ -100,10 +132,40 @@ function stop()
100132
(( i++ ))
101133
sleep 1
102134
done
135+
fi
103136

104-
# Remove PID file
105-
rm -f "$PIDFILE"
137+
# Remove PID file
138+
rm -f "$PIDFILE"
139+
}
140+
141+
#
142+
# Get go2rtc service status
143+
#
144+
function status()
145+
{
146+
# If go2rtc is not running, exit
147+
if ! ps aux | grep "/usr/local/bin/go2rtc" | grep -q -v "grep"; then
148+
echo "go2rtc is not running"
149+
exit 1
150+
fi
151+
152+
# If the process is running with no PID file, exit with an error
153+
if [ ! -f "$PIDFILE" ]; then
154+
echo "go2rtc is running but PID file is missing"
155+
exit 1
106156
fi
157+
158+
# Get PID from PID file
159+
PID=$(cat "$PIDFILE")
160+
161+
# If the PID is empty, exit with an error
162+
if [ -z "$PID" ]; then
163+
echo "go2rtc is running but PID file is empty"
164+
exit 1
165+
fi
166+
167+
echo "go2rtc is running, PID: $PID"
168+
exit 0
107169
}
108170

109171
case "$1" in
@@ -119,6 +181,10 @@ case "$1" in
119181
stop && start
120182
;;
121183
status)
184+
status
185+
186+
187+
122188
echo "Status go2rtc"
123189
if [ -f $PIDFILE ]; then
124190
echo -n "Running process for $NAME : "

docker/config/motion/init

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ case "$1" in
6464
i="0"
6565
while true; do
6666
# Check if the process is still running, quit if not
67-
if ! ps aux | grep -q "/usr/bin/motion" | grep -v "grep"; then
67+
if ! ps aux | grep "/usr/bin/motion" | grep -q -v "grep"; then
6868
break
6969
fi
7070

docker/config/nginx/motionui.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ server {
108108
}
109109

110110
# Go2rtc embedded websocket server access
111-
# Used to stream via WebRTC
111+
# Used to stream via WebRTC and MSE
112112
location /api/ws {
113113
proxy_pass http://127.0.0.1:1984;
114114
proxy_http_version 1.1;

docker/init

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ fi
5050
/bin/su -s /bin/bash -c "php $WWW_DIR/tools/database/initialize.php" www-data
5151
/bin/su -s /bin/bash -c "php $WWW_DIR/tools/database/update.php" www-data
5252

53-
# Start go2rtc in background, if not already running (database update may haved started it)
54-
if ! ps aux | grep "/usr/local/bin/go2rtc" | grep -v -q "grep"; then
55-
/usr/sbin/service go2rtc start
56-
fi
53+
# Make sure go2rtc service is started
54+
/usr/sbin/service go2rtc start
5755

5856
# Start shell service in background
5957
/bin/bash "$WWW_DIR/bin/service.sh" &

www/controllers/Camera/Camera.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ public function add(array $params) : void
195195
$motionParams['v4l2_device'] = ['status' => 'disabled', 'value' => ''];
196196
// Case the URL is /dev/video
197197
} else if (preg_match('#^/dev/video#', $url)) {
198-
$motionParams['v4l2_device'] = ['status' => 'enabled', 'value' => $url];
199-
$motionParams['netcam_url'] = ['status' => 'disabled', 'value' => ''];
198+
$motionParams['netcam_url'] = ['status' => 'enabled', 'value' => 'rtsp://127.0.0.1:8554/camera_' . $id . '?mp4'];
200199
$motionParams['movie_passthrough'] = ['status' => 'enabled', 'value' => 'off'];
200+
$motionParams['v4l2_device'] = ['status' => 'disabled', 'value' => ''];
201201
}
202202

203203
/**
@@ -314,6 +314,9 @@ public function edit(int $id, array $params) : void
314314
Param\Rotate::check($params['rotate']);
315315
Param\BasicAuthUsername::check($params['basic-auth-username']);
316316
Param\BasicAuthPassword::check($params['basic-auth-password']);
317+
Param\OnvifEnable::check($params['onvif-enable']);
318+
Param\OnvifPort::check($params['onvif-port']);
319+
// Param\OnvifUri::check($params['onvif-uri']);
317320

318321
/**
319322
* Set camera configuration
@@ -334,6 +337,47 @@ public function edit(int $id, array $params) : void
334337
$configuration['motion-detection-enable'] = $params['motion-detection-enable'];
335338
$configuration['timelapse-enable'] = $params['timelapse-enable'];
336339
$configuration['hardware-acceleration'] = $params['hardware-acceleration'];
340+
$configuration['onvif']['enable'] = $params['onvif-enable'];
341+
$configuration['onvif']['port'] = $params['onvif-port'];
342+
// $configuration['onvif']['uri'] = $params['onvif-uri'];
343+
344+
/**
345+
* Try to generate Onvif Url
346+
*/
347+
if ($params['onvif-enable'] == 'true') {
348+
// If the Url is a device, it cannot be moved
349+
if (preg_match('#/dev/video#', $params['url'])) {
350+
throw new Exception('Device camera are not movable');
351+
}
352+
353+
// Parse camera URL to extract IP/hostname
354+
$parsedUrl = parse_url($params['url']);
355+
356+
if ($parsedUrl === false or empty($parsedUrl)) {
357+
throw new Exception('Could not retrieve camera IP from URL');
358+
}
359+
360+
if (empty($parsedUrl['host'])) {
361+
throw new Exception('Could not determine camera IP from URL');
362+
}
363+
364+
$cameraIp = $parsedUrl['host'];
365+
366+
// Build Onvif URL
367+
$onvifUrl = 'http://' . $cameraIp;
368+
369+
// If a port is set, add it to the URL
370+
if (isset($params['onvif-port']) and $params['onvif-port'] > 0) {
371+
$onvifUrl .= ':' . $params['onvif-port'];
372+
}
373+
374+
// If a URI is set, add it to the URL
375+
// if (!empty($params['onvif-uri'])) {
376+
// $onvifUrl .= $params['onvif-uri'];
377+
// }
378+
379+
$configuration['onvif']['url'] = $onvifUrl;
380+
}
337381

338382
/**
339383
* Define proper URL for go2rtc and motion
@@ -345,9 +389,6 @@ public function edit(int $id, array $params) : void
345389
$url = preg_replace('#://#i', '://' . $configuration['basic-auth-username'] . ':' . $configuration['basic-auth-password'] . '@', $url);
346390
}
347391

348-
// First, add URL without filter to go2rtc
349-
// $go2rtcStreams[] = $url;
350-
351392
// Case the URL is http(s)://
352393
if (preg_match('#^https?://#', $url)) {
353394
$ffmpeg = true;
@@ -434,9 +475,9 @@ public function edit(int $id, array $params) : void
434475
$motionParams['v4l2_device'] = ['status' => 'disabled', 'value' => ''];
435476
// Case the URL is /dev/video
436477
} else if (preg_match('#^/dev/video#', $url)) {
437-
$motionParams['v4l2_device'] = ['status' => 'enabled', 'value' => $url];
438-
$motionParams['netcam_url'] = ['status' => 'disabled', 'value' => ''];
478+
$motionParams['netcam_url'] = ['status' => 'enabled', 'value' => 'rtsp://127.0.0.1:8554/camera_' . $id . '?mp4'];
439479
$motionParams['movie_passthrough'] = ['status' => 'enabled', 'value' => 'off'];
480+
$motionParams['v4l2_device'] = ['status' => 'disabled', 'value' => ''];
440481
}
441482

442483
/**

www/controllers/Camera/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public function getTemplate()
2828
'motion-detection-enable' => 'true',
2929
'timelapse-enable' => 'false',
3030
'hardware-acceleration' => 'false',
31+
'onvif' => [
32+
'enable' => 'false',
33+
'port' => '80',
34+
'uri' => '',
35+
'url' => '',
36+
],
3137
];
3238
}
3339
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Controllers\Camera\Param;
4+
5+
use Exception;
6+
7+
class OnvifEnable
8+
{
9+
/**
10+
* Check that Onvif enable is valid
11+
*/
12+
public static function check(string $enable) : void
13+
{
14+
if ($enable != "true" and $enable != "false") {
15+
throw new Exception('Onvif enable is invalid');
16+
}
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Controllers\Camera\Param;
4+
5+
use Exception;
6+
7+
class OnvifPort
8+
{
9+
/**
10+
* Check that port is valid
11+
*/
12+
public static function check(string $port) : void
13+
{
14+
if (empty($port)) {
15+
return;
16+
}
17+
18+
if ($port < 0 or $port > 65535) {
19+
throw new Exception('Onvif port is invalid');
20+
}
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Controllers\Camera\Param;
4+
5+
use Exception;
6+
7+
class OnvifUri
8+
{
9+
/**
10+
* Check that URI is valid
11+
*/
12+
public static function check(string $uri) : void
13+
{
14+
if (empty($uri)) {
15+
return;
16+
}
17+
18+
/**
19+
* Check that URI is valid
20+
* Should start with '/' or '?' and contain only allowed characters
21+
*/
22+
if (!preg_match('/^[\?\/a-zA-Z0-9\-\_\&\=\%]+$/', $uri)) {
23+
throw new Exception('Invalid ONVIF URI');
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)