Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use node.js ONVIF tool for getting RTSP URLs #666

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions misc/onvif/getRtspUrls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const onvif = require('onvif');

// Extract command-line arguments
const args = process.argv.slice(2); // Skip the first two elements
if (args.length != 3) {
console.error('Usage: node getRtspUrls.js <ip_address[:port]> <username> <password>');
process.exit(1);
}
const HOST_PORT = args[0]; // IP address and port (optional)
const USERNAME = args[1]; // Username
const PASSWORD = args[2]; // Password

// Split HOST_PORT by ':' to separate IP and port
const [HOST, PORT] = HOST_PORT.split(':');

// Default ONVIF port if not specified
const DEFAULT_PORT = 80;

// Validate input arguments
if (!HOST || !USERNAME || !PASSWORD) {
console.error('Usage: node getRtspUrls.js <ip_address[:port]> <username> <password>');
process.exit(1);
}

// Connect to the ONVIF camera
const camera = new onvif.Cam({
hostname: HOST,
username: USERNAME,
password: PASSWORD,
port: PORT || DEFAULT_PORT, // Use the specified port or default if not provided
}, function(err) {
if (err) {
return console.error(err);
}

// Get all the available media profiles
this.getProfiles((err, profiles) => {
if (err) {
return console.error(err);
}

// Fetch and output RTSP URLs for each profile
const rtspUrls = profiles.map(profile => {
return new Promise((resolve, reject) => {
this.getStreamUri({protocol: 'RTSP', profileToken: profile.token}, (err, stream) => {
if (err) {
reject(err);
} else {
resolve({profileName: profile.name, rtspUri: stream.uri});
}
});
});
});

Promise.all(rtspUrls)
.then(urls => {
console.log(JSON.stringify(urls, null, 2));
})
.catch(error => console.error(error));
});
});
29 changes: 18 additions & 11 deletions www/ajax/addip.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,25 @@ public function postCheckOnvifPort()
$pass = Inp::post('pass');
$onvif_addr = $ip.":".$port;

$p = @popen("/usr/lib/bluecherry/onvif_tool \"{$onvif_addr}\" \"{$user}\" \"{$pass}\" get_stream_urls", "r");

if (!$p){
data::responseJSON($stat, $msg);
exit;
$json_out = shell_exec("node /usr/share/bluecherry/onvif/getRtspUrls.js " . escapeshellarg($onvif_addr) .' '. escapeshellarg($user) .' '. escapeshellarg($pass));
if ($json_out) {
$urls = json_decode($json_out, /*associative=*/true);
$main_stream = $urls[0]['rtspUri'];
$sub_stream = $urls[1]['rtspUri'];
} else {
$p = @popen("/usr/lib/bluecherry/onvif_tool " . escapeshellarg($onvif_addr) .' '. escapeshellarg($user) .' '. escapeshellarg($pass). " get_stream_urls", "r");

if (!$p){
data::responseJSON($stat, $msg);
exit;
}

$media_service = fgets($p);
$main_stream = fgets($p);
$sub_stream = fgets($p);
pclose($p);
}

$media_service = fgets($p);
$main_stream = fgets($p);
$sub_stream = fgets($p);
pclose($p);
if ($media_service && $main_stream){
if ($main_stream) {
$stat = 6;
$msg = AIP_CHECK_ONVIF_SUCCESS;

Expand Down
38 changes: 23 additions & 15 deletions www/ajax/discoverCameras.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,24 +313,32 @@ public function postAdd()
foreach ($passwords as $pass_arr) {
foreach ($pass_arr as $login => $password) {
$onvif_username = $login;
$onvif_password = $password;
$onvif_password = $password;

try {
$p = @popen("/usr/lib/bluecherry/onvif_tool \"{$onvif_addr}\" \"{$onvif_username}\" \"{$onvif_password}\" get_stream_urls", "r");
if (!$p)
break;

$media_service = fgets($p);
if (!$media_service)
{
$err['onvif_ip'][] = $ip;
break(2);
}
$main_stream = fgets($p);
$sub_stream = fgets($p);
pclose($p);
$json_out = shell_exec("node /usr/share/bluecherry/onvif/getRtspUrls.js " . escapeshellarg($onvif_addr) .' '. escapeshellarg($onvif_username) .' '. escapeshellarg($onvif_password));
if ($json_out) {
$urls = json_decode($json_out, /*associative=*/true);
$main_stream = $urls[0]['rtspUri'];
$sub_stream = $urls[1]['rtspUri'];
} else {
$p = @popen("/usr/lib/bluecherry/onvif_tool " . escapeshellarg($onvif_addr) .' '. escapeshellarg($onvif_username) .' '. escapeshellarg($onvif_password). " get_stream_urls", "r");
if (!$p)
break;

$media_service = fgets($p);
if (!$media_service)
{
$err['onvif_ip'][] = $ip;
break(2);
}
$main_stream = fgets($p);
$sub_stream = fgets($p);
pclose($p);
}

if ($main_stream)
{
{
$password_ch = true;

$media_uri = trim($main_stream);
Expand Down
Loading