unifi-protect 4.8.2 → 4.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -7
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/protect-api-events.d.ts +65 -2
- package/dist/protect-api-events.js +20 -78
- package/dist/protect-api-events.js.map +1 -1
- package/dist/protect-api-livestream.d.ts +97 -0
- package/dist/protect-api-livestream.js +93 -29
- package/dist/protect-api-livestream.js.map +1 -1
- package/dist/protect-api.d.ts +59 -11
- package/dist/protect-api.js +63 -32
- package/dist/protect-api.js.map +1 -1
- package/dist/protect-logging.d.ts +14 -0
- package/dist/protect-types.d.ts +215 -49
- package/dist/protect-types.js +4 -0
- package/dist/protect-types.js.map +1 -1
- package/package.json +16 -16
package/README.md
CHANGED
|
@@ -46,19 +46,19 @@ npm install unifi-protect
|
|
|
46
46
|
|
|
47
47
|
Documentation and examples for using this library to access UniFi Protect controllers is [available here](https://github.com/hjdhjd/unifi-protect/blob/main/docs/classes/ProtectApi.md). Additionally, if you'd like to see all this in action in a well-documented, real-world example, please take a good look at my [homebridge-unifi-protect](https://github.com/hjdhjd/homebridge-unifi-protect) project. It relies heavily on this library for the core functionality it provides.
|
|
48
48
|
|
|
49
|
-
## UniFi Protect Realtime
|
|
50
|
-
So...how does UniFi Protect provide realtime
|
|
49
|
+
## UniFi Protect Realtime Events API
|
|
50
|
+
So...how does UniFi Protect provide realtime events? On UniFi OS-based controllers, it uses a websocket called `updates`. This connection provides a realtime stream of health, status, and events that the cameras encounter - including motion events and doorbell ring events.
|
|
51
51
|
|
|
52
|
-
Reverse engineering the realtime
|
|
52
|
+
Reverse engineering the realtime events API is a bit more difficult than the system events API because it's based on a binary protocol. The Protect system events API is a steady stream of JSONs published on all UniFi OS controllers over the `system` websocket. It's used by more than just UniFi Protect, which makes it interesting for future exploration.
|
|
53
53
|
|
|
54
|
-
The Protect realtime
|
|
54
|
+
The Protect realtime events API, however, is a binary protocol published over the `updates` websocket, and until now has been undocumented. I spent time analyzing what's happening in the Protect browser webUI as well as observing the controller and various Protect versions themselves to reverse engineer what's going on. Pouring through obfuscated code is like solving a puzzle with all the pieces in front of you - you know it's all there, you're just not always sure how it fits together.
|
|
55
55
|
|
|
56
56
|
For the impatient, you can take a look at the code for how to decode and read the binary protocol here in [protect-updates-api.ts](https://github.com/hjdhjd/unifi-protect/blob/master/src/protect-api-events.ts) and the interface information located in [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/master/src/protect-types.ts) as well.
|
|
57
57
|
|
|
58
58
|
I welcome any additions or corrections to the protocol for the benefit of the community. I hope this helps others launch their own exploration and create new and interesting Protect-enabled capabilities.
|
|
59
59
|
|
|
60
60
|
#### Non-Ubiquiti Apps Using the Protect API
|
|
61
|
-
This list represents all known apps that are using the realtime
|
|
61
|
+
This list represents all known apps that are using the realtime events API for UniFi Protect. If you're using the information you discovered on this page for your own UniFi Protect-based solution, please open an issue and I'm happy to add a link to it below. I hope this can serve as a repository of sorts for UniFi Protect-based apps and solutions in the community.
|
|
62
62
|
|
|
63
63
|
* [homebridge-unifi-protect](https://github.com/hjdhjd/homebridge-unifi-protect): Seamless integration of UniFi Protect into HomeKit with support for cameras, doorbells, and more.
|
|
64
64
|
|
|
@@ -72,7 +72,7 @@ Those are the basics and gets us up and running. Now, to explain how the updates
|
|
|
72
72
|
#### Updates Websocket Binary Format
|
|
73
73
|
UniFi OS update data packets are used to provide a realtime stream of updates to Protect. It differs from the system events API in that the system events API appears to be shared across other applications (Network, Access, etc.) while the updates events API appears to only be utilized by Protect and not shared by other applications, although the protocol is shared.
|
|
74
74
|
|
|
75
|
-
The `updates` websocket is used by the UniFi Protect webUI and native applications to provide realtime
|
|
75
|
+
The `updates` websocket is used by the UniFi Protect webUI and native applications to provide realtime events back to the controller. UniFi cameras and doorbells also use a websocket to provide those same updates to the Protect controller. The `updates` websocket uses a binary protocol to encode data largely to minimize bandwidth requirements, and provide access to more than JSON data, if needed.
|
|
76
76
|
|
|
77
77
|
So how does it all work? Cameras continuously stream updates to the UniFi Protect controller containing things like camera health, statistics, and - crucially for us - events such as motion and doorbell ring. A complete update packet is composed of four frames:
|
|
78
78
|
|
|
@@ -125,7 +125,7 @@ The final part of the update packet is the data frame. The data frame can be thr
|
|
|
125
125
|
#### Tips
|
|
126
126
|
* `update` actions are always tied to any valid modelKey that exists in the bootstrap JSON. The exception is `events` which is tied to the Protect events history list that it maintains. The supported modelKeys from the bootstrap JSON are: `bridge`, `camera`, `group`, `light`, `liveview`, `nvr`, `sensor`, `user`, and `viewer`.
|
|
127
127
|
* `add` actions are always tied to the `event` modelKey and indicate the beginning of an event item in the Protect events list. A subsequent `update` action is sent signaling the end of the event capture, and it's confidence score for motion detection.
|
|
128
|
-
* This is **not** the same thing as motion detection. If you want to detect motion, you should watch the `update` action for `camera` modelKeys, and look for a JSON that updates `lastMotion`. For doorbell rings, `lastRing`. The Protect events list is useful for the Protect app, but it's of limited utility to HomeKit, and it's slow relative to just looking for the `lastMotion` JSON that tends to be much more timely in its delivery. If you want true realtime
|
|
128
|
+
* This is **not** the same thing as motion detection. If you want to detect motion, you should watch the `update` action for `camera` modelKeys, and look for a JSON that updates `lastMotion`. For doorbell rings, `lastRing`. The Protect events list is useful for the Protect app, but it's of limited utility to HomeKit, and it's slow relative to just looking for the `lastMotion` JSON that tends to be much more timely in its delivery. If you want true realtime events, you want to look at the `update` action.
|
|
129
129
|
* JSONs are only payload type that seems to be sent, although the protocol is designed to accept all three.
|
|
130
130
|
* With the exception of `update` actions with a `modelKey` of `event`, JSONs are always a subset of the bootstrap JSON, indexed off of `modelKey`. So for a `modelKey` of `camera`, the data payload is always a subset of ProtectCameraConfigInterface (see [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/src/protect-types.ts#L108)).
|
|
131
131
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,gBAAgB;AAChB,cAAc,kBAAkB,CAAC;AAEjC,OAAO,EAAE,gBAAgB,EAAsB,MAAM,yBAAyB,CAAC;AAC/E,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -1,4 +1,64 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* Utilities to help decode packets from the UniFi Protect realtime events API.
|
|
4
|
+
*
|
|
5
|
+
* UniFi OS update events data packets are used to provide a realtime stream of updates to Protect. It differs from the system events API in that the system events API
|
|
6
|
+
* appears to be shared across other applications (Network, Access, etc.) while the updates events API appears to only be utilized by Protect and not shared by other
|
|
7
|
+
* applications, although the protocol is shared.
|
|
8
|
+
*
|
|
9
|
+
* So how does it all work? Cameras continuously stream updates to the UniFi Protect controller containing things like camera health, statistics, and, crucially for us,
|
|
10
|
+
* events such as motion and doorbell ring. A complete update packet is composed of four frames:
|
|
11
|
+
*
|
|
12
|
+
* ```
|
|
13
|
+
* ----------------
|
|
14
|
+
* | Header Frame | (8 bytes)
|
|
15
|
+
* ----------------
|
|
16
|
+
* | Action Frame |
|
|
17
|
+
* ----------------
|
|
18
|
+
* | Header Frame | (8 bytes)
|
|
19
|
+
* ----------------
|
|
20
|
+
* | Data Frame |
|
|
21
|
+
* ----------------
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* The header frame is required overhead since websockets provide only a transport medium. It's purpose is to tell us what's coming in the frame that follows.
|
|
25
|
+
*
|
|
26
|
+
* The action frame identifies what the action and category that the update contains:
|
|
27
|
+
*
|
|
28
|
+
* | Property | Description |
|
|
29
|
+
* |-------------|----------------------------------------------------------------------------------|
|
|
30
|
+
* | action | What action is being taken. Known actions are `add` and `update`. |
|
|
31
|
+
* | id | The identifier for the device we're updating. |
|
|
32
|
+
* | modelKey | The device model category that we're updating. |
|
|
33
|
+
* | newUpdateId | A new UUID generated on a per-update basis. This can be safely ignored it seems. |
|
|
34
|
+
*
|
|
35
|
+
* The final part of the update packet is the data frame. The data frame can be three different types of data - although in practice, I've only seen JSONs come across.
|
|
36
|
+
* Those types are:
|
|
37
|
+
*
|
|
38
|
+
* | Payload Type | Description |
|
|
39
|
+
* |--------------|------------------------------------------------------------------------------------------------------------|
|
|
40
|
+
* | 1 | JSON. For update actions that are not events, this is always a subset of the configuration bootstrap JSON. |
|
|
41
|
+
* | 2 | A UTF8-encoded string |
|
|
42
|
+
* | 3 | Node Buffer |
|
|
43
|
+
*
|
|
44
|
+
* Some tips:
|
|
45
|
+
*
|
|
46
|
+
* - `update` actions are always tied to the following modelKeys: camera, event, nvr, and user.
|
|
47
|
+
*
|
|
48
|
+
* - `add` actions are always tied to the `event` modelKey and indicate the beginning of an event item in the Protect events list. A subsequent `update` action is sent
|
|
49
|
+
* signaling the end of the event capture, and it's confidence score for motion detection.
|
|
50
|
+
*
|
|
51
|
+
* - The above is NOT the same thing as motion detection. If you want to detect motion, you should watch the `update` action for `camera` modelKeys, and look for a JSON
|
|
52
|
+
* that updates lastMotion. For doorbell rings, lastRing. The Protect events list is useful for the Protect app, but it's of limited utility to HomeKit, and it's slow
|
|
53
|
+
* = relative to looking for lastMotion that is. If you want true realtime updates, you want to look at the `update` action.
|
|
54
|
+
*
|
|
55
|
+
* - JSONs are only payload type that seems to be sent, although the protocol is designed to accept all three.
|
|
56
|
+
*
|
|
57
|
+
* - With the exception of update actions with a modelKey of event, JSONs are always a subset of the bootstrap JSON, indexed off of modelKey. So for a modelKey of camera,
|
|
58
|
+
* the data payload is always a subset of {@link ProtectTypes.ProtectCameraConfigInterface | ProtectCameraConfigInterface}.
|
|
59
|
+
*
|
|
60
|
+
* @module ProtectApiEvents
|
|
61
|
+
*/
|
|
2
62
|
import { ProtectLogging } from "./protect-logging.js";
|
|
3
63
|
/**
|
|
4
64
|
* UniFi Protect event packet.
|
|
@@ -34,6 +94,8 @@ export type ProtectEventHeader = {
|
|
|
34
94
|
* UniFi Protect event utility class that provides functions for decoding realtime event API packet frames.
|
|
35
95
|
*/
|
|
36
96
|
export declare class ProtectApiEvents {
|
|
97
|
+
/** @internal */
|
|
98
|
+
constructor();
|
|
37
99
|
/**
|
|
38
100
|
* Decode a UniFi Protect event packet.
|
|
39
101
|
*
|
|
@@ -41,8 +103,9 @@ export declare class ProtectApiEvents {
|
|
|
41
103
|
* @param packet - Input packet to decode.
|
|
42
104
|
*
|
|
43
105
|
* @remarks A UniFi Protect event packet is an encoded representation of state updates that occur in a UniFi Protect controller. This utility function takes an
|
|
44
|
-
*
|
|
45
|
-
*
|
|
106
|
+
* encoded packet as an input, and decodes it into an event header and payload that can be acted upon. An example of it's use is in {@link ProtectApi} where, once
|
|
107
|
+
* successfully logged into the Protect controller, events are generated automatically and can be accessed by listening to `message` events emitted by
|
|
108
|
+
* {@link ProtectApi}.
|
|
46
109
|
*/
|
|
47
110
|
static decodePacket(log: ProtectLogging, packet: Buffer): ProtectEventPacket | null;
|
|
48
111
|
private static decodeFrame;
|
|
@@ -1,68 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
/*
|
|
3
|
-
* The UniFi Protect realtime updates API is largely undocumented and has been reverse engineered mostly through
|
|
4
|
-
* trial and error, as well as observing the Protect controller in action.
|
|
5
|
-
*
|
|
6
|
-
* Here's how to get started with the UniFi Protect Updates API:
|
|
7
|
-
*
|
|
8
|
-
* 1. Login to the UniFi Protect controller, obtain the bootstrap JSON.
|
|
9
|
-
* 2. Open the websocket to the updates URL (see protect-api.ts).
|
|
10
|
-
*
|
|
11
|
-
* Then you're ready to listen to messages. You can see an example of this in protect-nvr.ts.
|
|
12
|
-
*
|
|
13
|
-
* Those are the basics and gets us up and running. Now, to explain how the updates API works...
|
|
14
|
-
*
|
|
15
|
-
* UniFi OS update data packets are used to provide a realtime stream of updates to Protect. It differs from
|
|
16
|
-
* the system events API in that the system events API appears to be shared across other applications (Network, Access, etc.)
|
|
17
|
-
* while the updates events API appears to only be utilized by Protect and not shared by other applications, although the protocol
|
|
18
|
-
* is shared.
|
|
19
|
-
*
|
|
20
|
-
* So how does it all work? Cameras continuously stream updates to the UniFi Protect controller containing things like camera
|
|
21
|
-
* health, statistics, and - crucially for us - events such as motion and doorbell ring. A complete update packet is composed of four
|
|
22
|
-
* frames:
|
|
23
|
-
*
|
|
24
|
-
* Header Frame (8 bytes)
|
|
25
|
-
* Action Frame
|
|
26
|
-
* Header Frame (8 bytes)
|
|
27
|
-
* Data Frame
|
|
28
|
-
*
|
|
29
|
-
* The header frame is required overhead since websockets provide only a transport medium. It's purpose is to tell us what's
|
|
30
|
-
* coming in the frame that follows.
|
|
31
|
-
*
|
|
32
|
-
* The action frame identifies what the action and category that the update contains:
|
|
33
|
-
*
|
|
34
|
-
* Property Description
|
|
35
|
-
* -------- -----------
|
|
36
|
-
* action What action is being taken. Known actions are "add" and "update".
|
|
37
|
-
* id The identifier for the device we're updating.
|
|
38
|
-
* modelKey The device model category that we're updating.
|
|
39
|
-
* newUpdateId A new UUID generated on a per-update basis. This can be safely ignored it seems.
|
|
1
|
+
/* Copyright(C) 2019-2024, HJD (https://github.com/hjdhjd). All rights reserved.
|
|
40
2
|
*
|
|
41
|
-
*
|
|
42
|
-
* practice, I've only seen JSONs come across. Those types are:
|
|
43
|
-
*
|
|
44
|
-
* Payload Type Description
|
|
45
|
-
* 1 JSON. For update actions that are not events, this is always a subset of the configuration bootstrap JSON.
|
|
46
|
-
* 2 A UTF8-encoded string
|
|
47
|
-
* 3 Node Buffer
|
|
48
|
-
*
|
|
49
|
-
* Some tips:
|
|
50
|
-
*
|
|
51
|
-
* - "update" actions are always tied to the following modelKeys: camera, event, nvr, and user.
|
|
52
|
-
*
|
|
53
|
-
* - "add" actions are always tied to the "event" modelKey and indicate the beginning of an event item in the Protect events list.
|
|
54
|
-
* A subsequent "update" action is sent signaling the end of the event capture, and it's confidence score for motion detection.
|
|
55
|
-
*
|
|
56
|
-
* - The above is NOT the same thing as motion detection. If you want to detect motion, you should watch the "update" action for "camera"
|
|
57
|
-
* modelKeys, and look for a JSON that updates lastMotion. For doorbell rings, lastRing. The Protect events list is useful for the
|
|
58
|
-
* Protect app, but it's of limited utility to HomeKit, and it's slow - relative to looking for lastMotion that is. If you want true
|
|
59
|
-
* realtime updates, you want to look at the "update" action.
|
|
60
|
-
*
|
|
61
|
-
* - JSONs are only payload type that seems to be sent, although the protocol is designed to accept all three.
|
|
62
|
-
*
|
|
63
|
-
* - With the exception of update actions with a modelKey of event, JSONs are always a subset of the bootstrap JSON, indexed off
|
|
64
|
-
* of modelKey. So for a modelKey of camera, the data payload is always a subset of ProtectCameraConfigInterface (see protect-types.ts).
|
|
3
|
+
* protect-api-events.ts: Our UniFi Protect realtime events API packet utilities.
|
|
65
4
|
*/
|
|
5
|
+
import zlib from "node:zlib";
|
|
66
6
|
// UniFi Protect events API packet header size, in bytes.
|
|
67
7
|
const EVENT_PACKET_HEADER_SIZE = 8;
|
|
68
8
|
// Update realtime API packet types.
|
|
@@ -78,14 +18,18 @@ var EventPayloadType;
|
|
|
78
18
|
EventPayloadType[EventPayloadType["STRING"] = 2] = "STRING";
|
|
79
19
|
EventPayloadType[EventPayloadType["BUFFER"] = 3] = "BUFFER";
|
|
80
20
|
})(EventPayloadType || (EventPayloadType = {}));
|
|
81
|
-
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
82
23
|
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
24
|
+
* A packet header is composed of 8 bytes in this order:
|
|
25
|
+
*
|
|
26
|
+
* | Byte Offset | Description | Bits | Values |
|
|
27
|
+
* |-------------|----------------|------|-----------------------------------------------------------------------|
|
|
28
|
+
* | 0 | Packet Type | 8 | 1 - header frame, 2 - payload frame. |
|
|
29
|
+
* | 1 | Payload Format | 8 | 1 - JSON object, 2 - UTF8-encoded string, 3 - Node Buffer. |
|
|
30
|
+
* | 2 | Deflated | 8 | 0 - uncompressed, 1 - compressed / deflated (zlib-based compression). |
|
|
31
|
+
* | 3 | Unknown | 8 | Always 0. Possibly reserved for future use by Ubiquiti? |
|
|
32
|
+
* | 4-7 | Payload Size: | 32 | Size of payload in network-byte order (big endian). |
|
|
89
33
|
*/
|
|
90
34
|
var ProtectEventPacketHeader;
|
|
91
35
|
(function (ProtectEventPacketHeader) {
|
|
@@ -98,8 +42,10 @@ var ProtectEventPacketHeader;
|
|
|
98
42
|
/**
|
|
99
43
|
* UniFi Protect event utility class that provides functions for decoding realtime event API packet frames.
|
|
100
44
|
*/
|
|
101
|
-
// A utility class for decoding UniFi Protect events.
|
|
102
45
|
export class ProtectApiEvents {
|
|
46
|
+
/** @internal */
|
|
47
|
+
constructor() {
|
|
48
|
+
}
|
|
103
49
|
/**
|
|
104
50
|
* Decode a UniFi Protect event packet.
|
|
105
51
|
*
|
|
@@ -107,10 +53,10 @@ export class ProtectApiEvents {
|
|
|
107
53
|
* @param packet - Input packet to decode.
|
|
108
54
|
*
|
|
109
55
|
* @remarks A UniFi Protect event packet is an encoded representation of state updates that occur in a UniFi Protect controller. This utility function takes an
|
|
110
|
-
*
|
|
111
|
-
*
|
|
56
|
+
* encoded packet as an input, and decodes it into an event header and payload that can be acted upon. An example of it's use is in {@link ProtectApi} where, once
|
|
57
|
+
* successfully logged into the Protect controller, events are generated automatically and can be accessed by listening to `message` events emitted by
|
|
58
|
+
* {@link ProtectApi}.
|
|
112
59
|
*/
|
|
113
|
-
// Process an update data packet and return the action and payload.
|
|
114
60
|
static decodePacket(log, packet) {
|
|
115
61
|
// What we need to do here is to split this packet into the header and payload, and decode them.
|
|
116
62
|
let dataOffset;
|
|
@@ -157,17 +103,13 @@ export class ProtectApiEvents {
|
|
|
157
103
|
case EventPayloadType.JSON:
|
|
158
104
|
// If it's data payload, it can be anything.
|
|
159
105
|
return JSON.parse(payload.toString());
|
|
160
|
-
break;
|
|
161
106
|
case EventPayloadType.STRING:
|
|
162
107
|
return payload.toString("utf8");
|
|
163
|
-
break;
|
|
164
108
|
case EventPayloadType.BUFFER:
|
|
165
109
|
return payload;
|
|
166
|
-
break;
|
|
167
110
|
default:
|
|
168
111
|
log.error("Unknown payload packet type received in the realtime events API: %s.", payloadFormat);
|
|
169
112
|
return null;
|
|
170
|
-
break;
|
|
171
113
|
}
|
|
172
114
|
}
|
|
173
115
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protect-api-events.js","sourceRoot":"","sources":["../src/protect-api-events.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"protect-api-events.js","sourceRoot":"","sources":["../src/protect-api-events.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA+DH,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,yDAAyD;AACzD,MAAM,wBAAwB,GAAG,CAAC,CAAC;AAEnC,oCAAoC;AACpC,IAAK,sBAIJ;AAJD,WAAK,sBAAsB;IAEzB,uEAAU,CAAA;IACV,yEAAW,CAAA;AACb,CAAC,EAJI,sBAAsB,KAAtB,sBAAsB,QAI1B;AAED,qCAAqC;AACrC,IAAK,gBAKJ;AALD,WAAK,gBAAgB;IAEnB,uDAAQ,CAAA;IACR,2DAAU,CAAA;IACV,2DAAU,CAAA;AACZ,CAAC,EALI,gBAAgB,KAAhB,gBAAgB,QAKpB;AAED;;;;;;;;;;;;GAYG;AACH,IAAK,wBAOJ;AAPD,WAAK,wBAAwB;IAE3B,uEAAQ,CAAA;IACR,2FAAkB,CAAA;IAClB,+EAAY,CAAA;IACZ,6EAAW,CAAA;IACX,uFAAgB,CAAA;AAClB,CAAC,EAPI,wBAAwB,KAAxB,wBAAwB,QAO5B;AAoCD;;GAEG;AACH,MAAM,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 +1,117 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
+
/**
|
|
4
|
+
* Access a direct MP4 livestream for a UniFi Protect camera.
|
|
5
|
+
*
|
|
6
|
+
* The UniFi Protect livestream API is largely undocumented and has been reverse engineered mostly through
|
|
7
|
+
* trial and error, as well as observing the Protect controller in action. It builds on the works of others in the
|
|
8
|
+
* community - particularly https://github.com/XciD - who have experimented and successfully gotten parts of this API decoded.
|
|
9
|
+
* As always, this work stands on the contributions of others and the work that's come before it, and I want to acknowledge those
|
|
10
|
+
* that paved the way.
|
|
11
|
+
*
|
|
12
|
+
* Let's start by defining some terms. In the MP4 world, an MP4 file (or stream) is composed of multiple atoms or segments.
|
|
13
|
+
* Think of these as packet types that contain specific pieces of information that are needed to put together a valid MP4 file.
|
|
14
|
+
* For our purposes, we're primarily interested in four types of MP4 boxes:
|
|
15
|
+
*
|
|
16
|
+
* | Box | Description |
|
|
17
|
+
* |-------|-----------------------------------------------------------------------------------------------------------------------------------------------|
|
|
18
|
+
* | FTYP | File type box. This contains codec and file information for the stream that follows it. It must be at the beginning of any stream, and preceded by the FTYP box. |
|
|
19
|
+
* | MDAT | Media data box. This contains a segment of the actual audio and video data in the MP4 stream. It is always paired with an MOOF box, which contains the metadata describing this payload in an MDAT box. |
|
|
20
|
+
* | MOOF | Movie fragment box. This defines the metadata for a specific segment of audio and video. It is always paired with an MDAT box, which contains the actual data. |
|
|
21
|
+
* | MOOV | Movie metadata box. This contains all the metadata information about the stream that follows. It must be at the beginning of any stream, and preceded by the FTYP box. The Protect livestream API actually combines the FTYP and MOOV boxes, conveniently giving us a complete initialization segment. |
|
|
22
|
+
*
|
|
23
|
+
* Every fMP4 stream begins with an initialization segment comprised of the FTYP and MOOV boxes. It defines the file type,
|
|
24
|
+
* what the movie metadata is, and other characteristics. Think of it as the header for the entire stream.
|
|
25
|
+
*
|
|
26
|
+
* After the header, every fMP4 stream has a series of segments (sometimes called fragments, hence the term fMP4),
|
|
27
|
+
* that consist of a pair of moof / mdat boxes that includes all the audio and video for that segment. You end up with something
|
|
28
|
+
* that looks like:
|
|
29
|
+
*
|
|
30
|
+
* ```
|
|
31
|
+
* |ftyp|moov|moof|mdat|moof|mdat...
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* The UniFi Protect livestream API provides a straightforward interface to generate bespoke fMP4 streams that
|
|
35
|
+
* can be tailored depending on your needs. This implementation of the API allows you to access those streams, retrieve all the relevant boxes/atoms you need to
|
|
36
|
+
* manipulate them for your application.
|
|
37
|
+
*
|
|
38
|
+
* @module ProtectLivestream
|
|
39
|
+
*/
|
|
3
40
|
import { EventEmitter } from "node:events";
|
|
4
41
|
import { ProtectApi } from "./protect-api.js";
|
|
5
42
|
import { ProtectLogging } from "./protect-logging.js";
|
|
43
|
+
/**
|
|
44
|
+
* This class provides a complete event-driven API to access the UniFi Protect Livestream API endpoint.
|
|
45
|
+
*
|
|
46
|
+
* 1. Create an instance of the {@link ProtectApi | UniFi Protect API} to connect to the Protect controller.
|
|
47
|
+
*
|
|
48
|
+
* 2. Create an instance of {@link ProtectLivestream | Protect Livestream} using the API instance above either directly, or through calling
|
|
49
|
+
* {@link ProtectApi.createLivestream | createLivestream} method on an instance of {@link ProtectApi} (preferred).
|
|
50
|
+
*
|
|
51
|
+
* 3. Start a livestream using {@link start}, stop it with {@link stop}, and listen for events.
|
|
52
|
+
*
|
|
53
|
+
* 3. Listen for `message` events emitted by {@link ProtectLivestream} which provides Buffers containing the raw fMP4 segment data as it's produced by Protect. You can
|
|
54
|
+
* alternatively listen individually for the initialization segment or regular fMP4 segments if you'd like to distinguish between the two types of segments.
|
|
55
|
+
*
|
|
56
|
+
* Those are the basics that gets us up and running.
|
|
57
|
+
*/
|
|
6
58
|
export declare class ProtectLivestream extends EventEmitter {
|
|
7
59
|
private _initSegment;
|
|
60
|
+
private _codec;
|
|
8
61
|
private api;
|
|
9
62
|
private errorHandler;
|
|
10
63
|
private log;
|
|
11
64
|
private segmentHandler;
|
|
12
65
|
private ws;
|
|
13
66
|
constructor(api: ProtectApi, log: ProtectLogging);
|
|
67
|
+
/**
|
|
68
|
+
* Start an fMP4 livestream session from the Protect controller.
|
|
69
|
+
*
|
|
70
|
+
* @param cameraId - Protect camera device ID property from the camera's {@link ProtectTypes.ProtectCameraConfigInterface | ProtectCameraConfig}.
|
|
71
|
+
* @param channel - Camera channel to use, indexing the channels array in the camera's {@link ProtectTypes.ProtectCameraConfigInterface | ProtectCameraConfig}.
|
|
72
|
+
* @param lens - Optionally specify alternate cameras on a Protect device, such as a package camera.
|
|
73
|
+
* @param segmentLength - Optionally specify the segment length, in milliseconds, of each fMP4 segment. Defaults to 100ms.
|
|
74
|
+
* @param requestId - Optionally specify a request ID to the Protect controller. This is primarily used for logging purposes.
|
|
75
|
+
*
|
|
76
|
+
* @returns Returns `true` if the livestream has successfully started, `false` otherwise.
|
|
77
|
+
*
|
|
78
|
+
* @remarks Once a livestream session has started, the following events can be listened for:
|
|
79
|
+
*
|
|
80
|
+
* | Event | Description |
|
|
81
|
+
* |---------------|----------------------------------------------------------------------------------------------------------------------------------------------|
|
|
82
|
+
* | `close` | Livestream has been closed. |
|
|
83
|
+
* | `codec` | The codec and container format in use in the livestream. The codec information will be passed as an argument to any listeners. |
|
|
84
|
+
* | `initsegment` | An fMP4 initialization segment has been received. The segment will be passed as an argument to any listeners. |
|
|
85
|
+
* | `message` | An fMP4 segment has been received. No distinction is made between segment types. The segment will be passed as an argument to any listeners. |
|
|
86
|
+
* | `segment` | A non-initialization fMP4 segment has been received. The segment will be passed as an argument to any listeners. |
|
|
87
|
+
*/
|
|
14
88
|
start(cameraId: string, channel: number, lens?: number, segmentLength?: number, requestId?: string): Promise<boolean>;
|
|
89
|
+
/**
|
|
90
|
+
* Stop an fMP4 livestream session from the Protect controller.
|
|
91
|
+
*/
|
|
15
92
|
stop(): void;
|
|
16
93
|
private launchLivestream;
|
|
17
94
|
private processLivestream;
|
|
95
|
+
/**
|
|
96
|
+
* Retrieve the initialization segment that must be at the start of every fMP4 stream.
|
|
97
|
+
*
|
|
98
|
+
* @returns Returns a promise that resolves once the initialization segment has been seen, or returning it immediately if it already has been.
|
|
99
|
+
*/
|
|
18
100
|
getInitSegment(): Promise<Buffer>;
|
|
101
|
+
/**
|
|
102
|
+
* The initialization segment that must be at the start of every fMP4 stream.
|
|
103
|
+
*
|
|
104
|
+
* @returns Returns the initialization segment if it exists, or `null` otherwise.
|
|
105
|
+
*/
|
|
19
106
|
get initSegment(): Buffer | null;
|
|
107
|
+
/**
|
|
108
|
+
* The codecs in use for this livestream session.
|
|
109
|
+
*
|
|
110
|
+
* @returns Returns a string containing the codec information,if it exists, or `null` otherwise.
|
|
111
|
+
*
|
|
112
|
+
* @remarks Codec information is provided as `codec,container` where codec is either `hev` (H.265) or `avc` (H.264). The container format is always `mp4`.
|
|
113
|
+
*
|
|
114
|
+
* @example `hev1.1.6.L150,mp4a.40.2`
|
|
115
|
+
*/
|
|
116
|
+
get codec(): string;
|
|
20
117
|
}
|