Hi,
Some time ago I have bought Unifi camera - AI Theta (UVC-AI-Theta) - planning to connect to HA. I have assumed that nowadays all camera expose RTSP stream that can be easily used, however it is not a case with modern Unifi Protect camera as Ubiquity removed this feature.
However I have managed to use the video stream from camera by manually editing configuration files on camera to send the stream to go2rtc server on another computer.
Here is an instruction:
- SSH to camera
ssh ubnt:[email protected]
- Edit configuration files
Configuration files are stored in /var/etc which is mounted tmpfs and will be reset on every startup. There is possibility to save it, but I haven’t looked into it - there is bash alias save
that does something related to that.
Default configuration files are symlinks to files stored in non-volatile memory in /usr/etc, but they are read only, so we need to make copies in /var/etc first
cp /var/etc/ubnt_streamer* /var/etc
vi /var/etc/ubnt_streamer*
In JSON file look for path video => video1 (or video2, video3 depending on which stream you would like to use) => avserializer
Default type of the stream is “extendedFlv” which is non standard and intended for Unifi Protect console. If you are not using Unifi Protect console, change it to “flv”. If you would like to use the same stream for UP console and directly leave it as “extendedFlv” though this is not tested by me.
In destinations set address of some TCP port of a computer where you will run go2rtc in format: “tcp://192.168.0.11:9552”. Probably (not tested) you can put many destinations, so if there is one pointing to UP console, just add yours. If there is only “file:///dev/null”, you can replace it.
- restart streamer
theta-video restart
Probably other cameras have different tools to control streaming. You can look in /usr/bin for some similar commands.
- Install and set up go2rtc
Probably you could somehow use go2rtc built in HA, though I haven’t yet figured how to add custom stream, so I have used Synology for the purpose - there is go2rtc package in SynoCommunity.
Apart from go2rtc you need netcat, solcat or some trivial TCP server that just accepts connections and streams it to standard output.
In go2rtc configuration file put:
For netcat (not available on Synology)
streams:
theta: exec:nc -k -l 9552
For solcat (can be installed on Synology with a package SynoCli Network Tools)
streams:
theta: exec:socat TCP-LISTEN:9552 stdout
If you have used extendedFlv stream type you need another step in go2rtc source that cleans FLV removing extra data. Here is PHP script:
#!/usr/bin/env php
<?php
$input = fopen('php://stdin', 'r');
function out(string $data): void {
echo $data;
}
$header = fread($input, 3);
if($header !== 'FLV') {
throw new \Exception("Not a FLV stream");
}
out($header);
$fileVersion = fread($input, 1);
if($fileVersion !== "\x01") {
throw new \Exception("Unknown FLV version");
}
out($fileVersion);
$flags = fread($input, 1);
out($flags);
$dataOffset = fread($input, 4);
out($dataOffset);
$previousTagSize = fread($input, 4);
out($previousTagSize);
while(!feof($input)) {
$tagType = fread($input, 1);
if($tagType === FALSE || $tagType === '') {
break;
}
out($tagType);
if($debug) {
echo "tagType: ".unpack('C', $tagType)[1]."\n";
}
$dataSize = fread($input, 3);
if($dataSize === FALSE || $dataSize === '') {
break;
}
out($dataSize);
$dataSize = unpack('N', "\x00".$dataSize)[1];
$timeStamp = fread($input, 3);
if($timeStamp === FALSE || $timeStamp === '') {
break;
}
out($timeStamp);
$timeStampExtended = fread($input, 1);
if($timeStampExtended === FALSE) {
break;
}
out($timeStampExtended);
$streamId = fread($input, 3);
if($streamId === FALSE) {
break;
}
out($streamId);
// payload
$dataSizeToRead = $dataSize;
while($dataSizeToRead > 0) {
$payload = fread($input, $dataSizeToRead);
if($payload === FALSE) {
die();
}
out($payload);
$dataSizeToRead -= strlen($payload);
}
$previousTagSize = fread($input, 4);
if($previousTagSize === FALSE) {
break;
}
out($previousTagSize);
if($dataSize + 11 != unpack('N', $previousTagSize)[1]) {
throw new \Exception("Wrong tag size");
}
// skipping trailing data
$trash = fread($input, 16);
if($trash === FALSE) {
break;
}
}
In this case your go2rtc configuration will look as:
streams:
theta: exec:socat TCP-LISTEN:9552 stdout | /path/to/php/script.php
And that’s it. You can go to http://192.168.0.11:1984/stream.html?src=theta and your stream should be visible. You can also add it to HA as standard camera using rtsp://192.168.0.11:8554/theta?mp4
If your camera is adopted to UP console, the configuration file may be overwritten at some point. I don’t have UP console, so I can’t test it.
What can be improved:
- saving camera configuration to non-volatile memory
- using go2rtc from HA
Best regards,
Janusz