unifi-protect 4.27.7 → 4.29.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 +27 -10
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protect-api-events.d.ts +23 -33
- package/dist/protect-api-events.js +74 -68
- package/dist/protect-api-events.js.map +1 -1
- package/dist/protect-api-livestream.d.ts +8 -6
- package/dist/protect-api-livestream.js +119 -119
- package/dist/protect-api-livestream.js.map +1 -1
- package/dist/protect-api.d.ts +192 -180
- package/dist/protect-api.js +355 -312
- package/dist/protect-api.js.map +1 -1
- package/dist/protect-types.d.ts +522 -365
- package/dist/settings.d.ts +1 -0
- package/dist/settings.js +2 -0
- package/dist/settings.js.map +1 -1
- package/dist/util/ufp.js +908 -152
- package/dist/util/ufp.js.map +1 -1
- package/package.json +9 -12
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
</DIV>
|
|
13
13
|
</SPAN>
|
|
14
14
|
|
|
15
|
-
`unifi-protect` is a library that
|
|
15
|
+
`unifi-protect` is a library that enables you to connect to and communicate with the Ubiquiti UniFi Protect API and ecosystem. [UniFi Protect](https://ui.com/camera-security) is [Ubiquiti's](https://www.ui.com) next-generation video security platform, with rich camera, doorbell, and NVR controller hardware options for you to choose from, as well as an app which you can use to view, configure and manage your video camera and doorbells.
|
|
16
16
|
|
|
17
17
|
## Why use this library for UniFi Protect support?
|
|
18
18
|
In short - because I use it every day to support a very popular [Homebridge](https://homebridge.io) plugin named [homebridge-unifi-protect](https://www.npmjs.com/package/homebridge-unifi-protect) that I maintain. I have been occasionally asked if I would consider packaging the core API library separately from the plugin so that other open source projects can take advantage of the work that's been done here to understand and decode the UniFi Protect API.
|
|
@@ -42,6 +42,23 @@ To use this library in Node, install it from the command line:
|
|
|
42
42
|
npm install unifi-protect
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { ProtectApi } from "unifi-protect";
|
|
49
|
+
|
|
50
|
+
const protect = new ProtectApi();
|
|
51
|
+
|
|
52
|
+
await protect.login("192.168.1.1", "username", "password");
|
|
53
|
+
await protect.getBootstrap();
|
|
54
|
+
|
|
55
|
+
// Access your devices.
|
|
56
|
+
const cameras = protect.bootstrap?.cameras ?? [];
|
|
57
|
+
|
|
58
|
+
// Listen for real-time events.
|
|
59
|
+
protect.on("message", (packet) => console.log(packet.header.modelKey, packet.header.action));
|
|
60
|
+
```
|
|
61
|
+
|
|
45
62
|
## Documentation
|
|
46
63
|
|
|
47
64
|
Documentation and examples for using this library to access UniFi Protect controllers is [available here](https://github.com/hjdhjd/unifi-protect/blob/main/docs/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.
|
|
@@ -53,7 +70,7 @@ Reverse engineering the realtime events API is a bit more difficult than the sys
|
|
|
53
70
|
|
|
54
71
|
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
72
|
|
|
56
|
-
For the impatient, you can take a look at the code for how to decode and read the binary protocol here in [protect-
|
|
73
|
+
For the impatient, you can take a look at the code for how to decode and read the binary protocol here in [protect-api-events.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-api-events.ts) and the interface information located in [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-types.ts) as well.
|
|
57
74
|
|
|
58
75
|
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
76
|
|
|
@@ -63,9 +80,9 @@ This list represents all known apps that are using the realtime events API for U
|
|
|
63
80
|
* [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
81
|
|
|
65
82
|
#### Connecting
|
|
66
|
-
* Login to the UniFi Protect controller and obtain the bootstrap JSON. The URL is: `https://protect-nvr-ip/proxy/protect/api/bootstrap`. You can look through [protect-api.ts](https://github.com/hjdhjd/unifi-protect/blob/
|
|
67
|
-
* Open the websocket to the updates URL. The URL is: `wss://protect-nvr-ip/proxy/protect/ws/updates?lastUpdateId
|
|
68
|
-
* Then you're ready to listen to messages. You can see an [example of this in protect-
|
|
83
|
+
* Login to the UniFi Protect controller and obtain the bootstrap JSON. The URL is: `https://protect-nvr-ip/proxy/protect/api/bootstrap`. You can look through [protect-api.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-api.ts) for a better understanding of the Protect login process and how to obtain the bootstrap JSON.
|
|
84
|
+
* Open the websocket to the updates URL. The URL is: `wss://protect-nvr-ip/proxy/protect/ws/updates?lastUpdateId=X`. You can grab lastUpdateId from the bootstrap JSON in the prior step. You can [see an example in protect-api.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-api.ts).
|
|
85
|
+
* Then you're ready to listen to messages. You can see an [example of this in protect-api.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-api.ts).
|
|
69
86
|
|
|
70
87
|
Those are the basics and gets us up and running. Now, to explain how the updates API works...
|
|
71
88
|
|
|
@@ -89,7 +106,7 @@ So how does it all work? Cameras continuously stream updates to the UniFi Protec
|
|
|
89
106
|
Let's look at each of these.
|
|
90
107
|
|
|
91
108
|
##### Header Frame
|
|
92
|
-
The header frame is required overhead since websockets provide only a transport medium.
|
|
109
|
+
The header frame is required overhead since websockets provide only a transport medium. Its purpose is to tell us what's coming in the frame that follows.
|
|
93
110
|
|
|
94
111
|
A packet header is composed of 8 bytes in this order:
|
|
95
112
|
|
|
@@ -118,16 +135,16 @@ The final part of the update packet is the data frame. The data frame can be thr
|
|
|
118
135
|
|
|
119
136
|
| Payload Type | Description
|
|
120
137
|
|--------------|------------------------------------------------------------------------------------
|
|
121
|
-
| 1 | JSON. If the action frame's `action` property is set to `update` and the `modelKey` property is not set to `event` (e.g. `camera`), this will **always** a subset of the [configuration bootstrap JSON](https://github.com/hjdhjd/unifi-protect/blob/src/protect-types.ts
|
|
138
|
+
| 1 | JSON. If the action frame's `action` property is set to `update` and the `modelKey` property is not set to `event` (e.g. `camera`), this will **always** be a subset of the [configuration bootstrap JSON](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-types.ts).
|
|
122
139
|
| 2 | A UTF8-encoded string.
|
|
123
140
|
| 3 | Node Buffer.
|
|
124
141
|
|
|
125
142
|
#### Tips
|
|
126
|
-
* `update` actions are always tied to any valid modelKey that exists in the bootstrap JSON. The exception is `
|
|
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
|
|
143
|
+
* `update` actions are always tied to any valid modelKey that exists in the bootstrap JSON. The exception is `event` which is tied to the Protect events history list that it maintains. The supported modelKeys from the bootstrap JSON are: `bridge`, `camera`, `chime`, `group`, `light`, `liveview`, `nvr`, `sensor`, `user`, and `viewer`.
|
|
144
|
+
* `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 its confidence score for motion detection.
|
|
128
145
|
* 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
146
|
* JSONs are only payload type that seems to be sent, although the protocol is designed to accept all three.
|
|
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
|
|
147
|
+
* 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/main/src/protect-types.ts)).
|
|
131
148
|
|
|
132
149
|
## Library Development Dashboard
|
|
133
150
|
This is mostly of interest to the true developer nerds amongst us.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** @internal */
|
|
2
2
|
export * from "./protect-api.js";
|
|
3
|
-
export type { ProtectLivestream } from "./protect-api-livestream.js";
|
|
4
|
-
export {
|
|
3
|
+
export type { LivestreamOptions, ProtectLivestream } from "./protect-api-livestream.js";
|
|
4
|
+
export { type ProtectEventHeader, type ProtectEventPacket, decodePacket } from "./protect-api-events.js";
|
|
5
5
|
export * from "./protect-logging.js";
|
|
6
6
|
export * from "./protect-types.js";
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
/** @internal */
|
|
6
6
|
// Export our API.
|
|
7
7
|
export * from "./protect-api.js";
|
|
8
|
-
export {
|
|
8
|
+
export { decodePacket } from "./protect-api-events.js";
|
|
9
9
|
export * from "./protect-logging.js";
|
|
10
10
|
export * from "./protect-types.js";
|
|
11
11
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,gBAAgB;AAEhB,kBAAkB;AAClB,cAAc,kBAAkB,CAAC;AAEjC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,gBAAgB;AAEhB,kBAAkB;AAClB,cAAc,kBAAkB,CAAC;AAEjC,OAAO,EAAoD,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACzG,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* ----------------
|
|
21
21
|
* ```
|
|
22
22
|
*
|
|
23
|
-
* The header frame is required overhead since websockets provide only a transport medium.
|
|
23
|
+
* The header frame is required overhead since websockets provide only a transport medium. Its purpose is to tell us what's coming in the frame that follows.
|
|
24
24
|
*
|
|
25
25
|
* The action frame identifies what the action and category that the update contains:
|
|
26
26
|
*
|
|
@@ -42,73 +42,63 @@
|
|
|
42
42
|
*
|
|
43
43
|
* Some tips:
|
|
44
44
|
*
|
|
45
|
-
* - `update` actions are always tied to the
|
|
45
|
+
* - `update` actions are always tied to any valid modelKey that exists in the bootstrap JSON. The exception is `event` which is tied to the Protect events history list
|
|
46
|
+
* that it maintains. The supported modelKeys from the bootstrap JSON are: `bridge`, `camera`, `chime`, `group`, `light`, `liveview`, `nvr`, `sensor`, `user`, and
|
|
47
|
+
* `viewer`.
|
|
46
48
|
*
|
|
47
49
|
* - `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
|
|
48
|
-
* signaling the end of the event capture, and
|
|
50
|
+
* signaling the end of the event capture, and its confidence score for motion detection.
|
|
49
51
|
*
|
|
50
52
|
* - 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
|
|
51
53
|
* 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
|
|
52
|
-
*
|
|
54
|
+
* relative to just looking for the lastMotion update. If you want true realtime updates, you want to look at the `update` action.
|
|
53
55
|
*
|
|
54
56
|
* - JSONs are only payload type that seems to be sent, although the protocol is designed to accept all three.
|
|
55
57
|
*
|
|
56
58
|
* - 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,
|
|
57
59
|
* the data payload is always a subset of {@link ProtectTypes.ProtectCameraConfigInterface | ProtectCameraConfigInterface}.
|
|
58
60
|
*
|
|
59
|
-
* @module
|
|
61
|
+
* @module ProtectEvents
|
|
60
62
|
*/
|
|
61
63
|
import type { Nullable } from "./protect-types.js";
|
|
62
64
|
import type { ProtectLogging } from "./protect-logging.js";
|
|
63
65
|
/**
|
|
64
66
|
* UniFi Protect event packet.
|
|
65
67
|
*
|
|
66
|
-
* @param header - Protect event header.
|
|
67
|
-
* @param payload - Protect event payload.
|
|
68
|
-
*
|
|
69
68
|
* @remarks A UniFi Protect event packet represents a realtime event update from a UniFi Protect controller. There are two components to each packet, a `header` and
|
|
70
69
|
* a `payload`. The `header` contains information about which Protect device and what action category it belongs to. The `payload` contains the detailed information
|
|
71
70
|
* related to the device and action specified in the header.
|
|
72
71
|
*/
|
|
73
|
-
export
|
|
72
|
+
export interface ProtectEventPacket {
|
|
74
73
|
header: ProtectEventHeader;
|
|
75
74
|
payload: unknown;
|
|
76
|
-
}
|
|
75
|
+
}
|
|
77
76
|
/**
|
|
78
77
|
* UniFi Protect event header.
|
|
79
78
|
*
|
|
80
|
-
* @param header - Protect event header.
|
|
81
|
-
* @param payload - Protect event payload.
|
|
82
|
-
*
|
|
83
79
|
* @remarks A UniFi Protect event packet represents a realtime event update from a UniFi Protect controller. There are two components to each packet, a `header` and
|
|
84
80
|
* a `payload`. The `header` contains information about which Protect device and what action category it belongs to and can contain arbitrary information, though has
|
|
85
81
|
* a few properties that are always present (`action`, `id`, `modelKey`, and `newUpdateId`). The `payload` contains the detailed information related to the device and
|
|
86
82
|
* action specified in the header.
|
|
87
83
|
*/
|
|
88
|
-
export
|
|
84
|
+
export interface ProtectEventHeader {
|
|
89
85
|
action: string;
|
|
90
86
|
id: string;
|
|
91
87
|
modelKey: string;
|
|
92
88
|
newUpdateId: string;
|
|
93
89
|
[key: string]: boolean | number | object | string;
|
|
94
|
-
}
|
|
90
|
+
}
|
|
95
91
|
/**
|
|
96
|
-
* UniFi Protect event
|
|
92
|
+
* Decode a UniFi Protect event packet.
|
|
93
|
+
*
|
|
94
|
+
* @param log - Logging functions to use.
|
|
95
|
+
* @param packet - Input packet to decode.
|
|
96
|
+
*
|
|
97
|
+
* @returns Promise resolving to a decoded event packet, or `null` if decoding fails.
|
|
98
|
+
*
|
|
99
|
+
* @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
|
|
100
|
+
* encoded packet as an input, and decodes it into an event header and payload that can be acted upon. An example of its use is in {@link ProtectApi} where, once
|
|
101
|
+
* successfully logged into the Protect controller, events are generated automatically and can be accessed by listening to `message` events emitted by
|
|
102
|
+
* {@link ProtectApi}.
|
|
97
103
|
*/
|
|
98
|
-
export declare
|
|
99
|
-
/** @internal */
|
|
100
|
-
constructor();
|
|
101
|
-
/**
|
|
102
|
-
* Decode a UniFi Protect event packet.
|
|
103
|
-
*
|
|
104
|
-
* @param log - Logging functions to use.
|
|
105
|
-
* @param packet - Input packet to decode.
|
|
106
|
-
*
|
|
107
|
-
* @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
|
|
108
|
-
* 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
|
|
109
|
-
* successfully logged into the Protect controller, events are generated automatically and can be accessed by listening to `message` events emitted by
|
|
110
|
-
* {@link ProtectApi}.
|
|
111
|
-
*/
|
|
112
|
-
static decodePacket(log: ProtectLogging, packet: Buffer): Nullable<ProtectEventPacket>;
|
|
113
|
-
private static decodeFrame;
|
|
114
|
-
}
|
|
104
|
+
export declare function decodePacket(log: ProtectLogging, packet: Buffer): Promise<Nullable<ProtectEventPacket>>;
|
|
@@ -2,14 +2,17 @@
|
|
|
2
2
|
*
|
|
3
3
|
* protect-api-events.ts: Our UniFi Protect realtime events API packet utilities.
|
|
4
4
|
*/
|
|
5
|
+
import { promisify } from "node:util";
|
|
5
6
|
import zlib from "node:zlib";
|
|
7
|
+
// Async zlib inflate for non-blocking decompression on the libuv threadpool.
|
|
8
|
+
const inflateAsync = promisify(zlib.inflate);
|
|
6
9
|
// UniFi Protect events API packet header size, in bytes.
|
|
7
10
|
const EVENT_PACKET_HEADER_SIZE = 8;
|
|
8
11
|
// Update realtime API packet types.
|
|
9
12
|
var ProtectEventPacketType;
|
|
10
13
|
(function (ProtectEventPacketType) {
|
|
11
|
-
ProtectEventPacketType[ProtectEventPacketType["
|
|
12
|
-
ProtectEventPacketType[ProtectEventPacketType["
|
|
14
|
+
ProtectEventPacketType[ProtectEventPacketType["ACTION"] = 1] = "ACTION";
|
|
15
|
+
ProtectEventPacketType[ProtectEventPacketType["DATA"] = 2] = "DATA";
|
|
13
16
|
})(ProtectEventPacketType || (ProtectEventPacketType = {}));
|
|
14
17
|
// Update realtime API payload types.
|
|
15
18
|
var EventPayloadType;
|
|
@@ -25,7 +28,7 @@ var EventPayloadType;
|
|
|
25
28
|
*
|
|
26
29
|
* | Byte Offset | Description | Bits | Values |
|
|
27
30
|
* |-------------|----------------|------|-----------------------------------------------------------------------|
|
|
28
|
-
* | 0 | Packet Type | 8 | 1 -
|
|
31
|
+
* | 0 | Packet Type | 8 | 1 - action frame, 2 - data frame. |
|
|
29
32
|
* | 1 | Payload Format | 8 | 1 - JSON object, 2 - UTF8-encoded string, 3 - Node Buffer. |
|
|
30
33
|
* | 2 | Deflated | 8 | 0 - uncompressed, 1 - compressed / deflated (zlib-based compression). |
|
|
31
34
|
* | 3 | Unknown | 8 | Always 0. Possibly reserved for future use by Ubiquiti? |
|
|
@@ -40,77 +43,80 @@ var ProtectEventPacketHeader;
|
|
|
40
43
|
ProtectEventPacketHeader[ProtectEventPacketHeader["PAYLOAD_SIZE"] = 4] = "PAYLOAD_SIZE";
|
|
41
44
|
})(ProtectEventPacketHeader || (ProtectEventPacketHeader = {}));
|
|
42
45
|
/**
|
|
43
|
-
* UniFi Protect event
|
|
46
|
+
* Decode a UniFi Protect event packet.
|
|
47
|
+
*
|
|
48
|
+
* @param log - Logging functions to use.
|
|
49
|
+
* @param packet - Input packet to decode.
|
|
50
|
+
*
|
|
51
|
+
* @returns Promise resolving to a decoded event packet, or `null` if decoding fails.
|
|
52
|
+
*
|
|
53
|
+
* @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
|
|
54
|
+
* encoded packet as an input, and decodes it into an event header and payload that can be acted upon. An example of its use is in {@link ProtectApi} where, once
|
|
55
|
+
* successfully logged into the Protect controller, events are generated automatically and can be accessed by listening to `message` events emitted by
|
|
56
|
+
* {@link ProtectApi}.
|
|
44
57
|
*/
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
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}.
|
|
59
|
-
*/
|
|
60
|
-
static decodePacket(log, packet) {
|
|
61
|
-
// What we need to do here is to split this packet into the header and payload, and decode them.
|
|
62
|
-
let dataOffset;
|
|
63
|
-
try {
|
|
64
|
-
// The fourth byte holds our payload size. When you add the payload size to our header frame size, you get the location of the
|
|
65
|
-
// data header frame.
|
|
66
|
-
dataOffset = packet.readUInt32BE(ProtectEventPacketHeader.PAYLOAD_SIZE) + EVENT_PACKET_HEADER_SIZE;
|
|
67
|
-
// Validate our packet size, just in case we have more or less data than we expect. If we do, we're done for now.
|
|
68
|
-
if (packet.length !== (dataOffset + EVENT_PACKET_HEADER_SIZE + packet.readUInt32BE(dataOffset + ProtectEventPacketHeader.PAYLOAD_SIZE))) {
|
|
69
|
-
throw new Error("Packet length doesn't match header information.");
|
|
70
|
-
}
|
|
58
|
+
export async function decodePacket(log, packet) {
|
|
59
|
+
// What we need to do here is to split this packet into the header and payload, and decode them.
|
|
60
|
+
let dataOffset;
|
|
61
|
+
try {
|
|
62
|
+
// The payload size begins at byte offset 4 as a big-endian 32-bit integer. When you add the payload size to our header frame size, you get the location of the
|
|
63
|
+
// data header frame.
|
|
64
|
+
dataOffset = packet.readUInt32BE(ProtectEventPacketHeader.PAYLOAD_SIZE) + EVENT_PACKET_HEADER_SIZE;
|
|
65
|
+
// Validate our packet size, just in case we have more or less data than we expect. If we do, we're done for now.
|
|
66
|
+
if (packet.length !== (dataOffset + EVENT_PACKET_HEADER_SIZE + packet.readUInt32BE(dataOffset + ProtectEventPacketHeader.PAYLOAD_SIZE))) {
|
|
67
|
+
throw new Error("Packet length doesn't match header information.");
|
|
71
68
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
log.error("Realtime events API: error decoding update packet: %s.", error);
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
// Decode the action and payload frames in parallel. Both frames are independent and may require zlib decompression, so inflating them concurrently maximizes
|
|
75
|
+
// throughput on the libuv threadpool.
|
|
76
|
+
try {
|
|
77
|
+
const [actionFrame, dataFrame] = await Promise.all([
|
|
78
|
+
decodeFrame(log, packet.subarray(0, dataOffset), ProtectEventPacketType.ACTION),
|
|
79
|
+
decodeFrame(log, packet.subarray(dataOffset), ProtectEventPacketType.DATA)
|
|
80
|
+
]);
|
|
81
|
+
if (!actionFrame || !dataFrame) {
|
|
80
82
|
return null;
|
|
81
83
|
}
|
|
82
|
-
return ({ header:
|
|
84
|
+
return ({ header: actionFrame, payload: dataFrame });
|
|
83
85
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
catch (error) {
|
|
87
|
+
log.error("Realtime events API: error decoding update packet: %s.", error);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Decode a frame, composed of a header and payload, received through the update events API.
|
|
92
|
+
async function decodeFrame(log, packet, packetType) {
|
|
93
|
+
// Read the packet frame type.
|
|
94
|
+
const frameType = packet.readUInt8(ProtectEventPacketHeader.TYPE);
|
|
95
|
+
// This isn't the frame type we were expecting - we're done.
|
|
96
|
+
if (packetType !== frameType) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
// Read the payload format.
|
|
100
|
+
const payloadFormat = packet.readUInt8(ProtectEventPacketHeader.PAYLOAD_FORMAT);
|
|
101
|
+
// Decompress the payload if the deflated flag is set, using async inflate to avoid blocking the event loop. For uncompressed payloads, we just slice past the header.
|
|
102
|
+
const payload = packet.readUInt8(ProtectEventPacketHeader.DEFLATED) ?
|
|
103
|
+
await inflateAsync(packet.subarray(EVENT_PACKET_HEADER_SIZE)) : packet.subarray(EVENT_PACKET_HEADER_SIZE);
|
|
104
|
+
// If it's an action frame, it can only have one format.
|
|
105
|
+
if (frameType === ProtectEventPacketType.ACTION) {
|
|
106
|
+
return (payloadFormat === EventPayloadType.JSON) ? JSON.parse(payload.toString()) : null;
|
|
107
|
+
}
|
|
108
|
+
// Process the payload format accordingly.
|
|
109
|
+
switch (payloadFormat) {
|
|
110
|
+
case EventPayloadType.JSON:
|
|
111
|
+
// If it's data payload, it can be anything.
|
|
112
|
+
return JSON.parse(payload.toString());
|
|
113
|
+
case EventPayloadType.STRING:
|
|
114
|
+
return payload.toString("utf8");
|
|
115
|
+
case EventPayloadType.BUFFER:
|
|
116
|
+
return payload;
|
|
117
|
+
default:
|
|
118
|
+
log.error("Unknown payload packet type received in the realtime events API: %s.", payloadFormat);
|
|
90
119
|
return null;
|
|
91
|
-
}
|
|
92
|
-
// Read the payload format.
|
|
93
|
-
const payloadFormat = packet.readUInt8(ProtectEventPacketHeader.PAYLOAD_FORMAT);
|
|
94
|
-
// Check to see if we're compressed or not, and inflate if needed after skipping past the 8-byte header.
|
|
95
|
-
const payload = packet.readUInt8(ProtectEventPacketHeader.DEFLATED) ?
|
|
96
|
-
zlib.inflateSync(packet.subarray(EVENT_PACKET_HEADER_SIZE)) : packet.subarray(EVENT_PACKET_HEADER_SIZE);
|
|
97
|
-
// If it's a header, it can only have one format.
|
|
98
|
-
if (frameType === ProtectEventPacketType.HEADER) {
|
|
99
|
-
return (payloadFormat === EventPayloadType.JSON) ? JSON.parse(payload.toString()) : null;
|
|
100
|
-
}
|
|
101
|
-
// Process the payload format accordingly.
|
|
102
|
-
switch (payloadFormat) {
|
|
103
|
-
case EventPayloadType.JSON:
|
|
104
|
-
// If it's data payload, it can be anything.
|
|
105
|
-
return JSON.parse(payload.toString());
|
|
106
|
-
case EventPayloadType.STRING:
|
|
107
|
-
return payload.toString("utf8");
|
|
108
|
-
case EventPayloadType.BUFFER:
|
|
109
|
-
return payload;
|
|
110
|
-
default:
|
|
111
|
-
log.error("Unknown payload packet type received in the realtime events API: %s.", payloadFormat);
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
120
|
}
|
|
115
121
|
}
|
|
116
122
|
//# sourceMappingURL=protect-api-events.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protect-api-events.js","sourceRoot":"","sources":["../src/protect-api-events.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"protect-api-events.js","sourceRoot":"","sources":["../src/protect-api-events.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkEH,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,6EAA6E;AAC7E,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,CAA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AC,QAAQ,CAAC,wBAAwB,CAAC,CAAC;IAE5G,wDAAwD;IACxD,IAAG,SAAS,KAAK,sBAAsB,CAAC,MAAM,EAAE,CAAC;QAE/C,OAAO,CAAC,aAAa,KAAK,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAuB,CAAC,CAAC,CAAC,IAAI,CAAC;IACjH,CAAC;IAED,0CAA0C;IAC1C,QAAO,aAAa,EAAE,CAAC;QAErB,KAAK,gBAAgB,CAAC,IAAI;YAExB,4CAA4C;YAC5C,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAS,CAAC;QAEhD,KAAK,gBAAgB,CAAC,MAAM;YAE1B,OAAO,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAElC,KAAK,gBAAgB,CAAC,MAAM;YAE1B,OAAO,OAAO,CAAC;QAEjB;YAEE,GAAG,CAAC,KAAK,CAAC,sEAAsE,EAAE,aAAa,CAAC,CAAC;YAEjG,OAAO,IAAI,CAAC;IAChB,CAAC;AACH,CAAC"}
|
|
@@ -32,7 +32,7 @@ export interface LivestreamOptions {
|
|
|
32
32
|
*
|
|
33
33
|
* 3. Start a livestream using {@link start}, stop it with {@link stop}, and listen for events.
|
|
34
34
|
*
|
|
35
|
-
*
|
|
35
|
+
* 4. 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
|
|
36
36
|
* alternatively listen individually for the initialization segment or regular fMP4 segments if you'd like to distinguish between the two types of segments.
|
|
37
37
|
*
|
|
38
38
|
* Those are the basics that gets us up and running.
|
|
@@ -58,14 +58,12 @@ export declare class ProtectLivestream extends EventEmitter {
|
|
|
58
58
|
private _initSegment;
|
|
59
59
|
private _codec;
|
|
60
60
|
private _stream;
|
|
61
|
-
private agent;
|
|
62
61
|
private api;
|
|
63
|
-
private errorHandler;
|
|
64
62
|
private heartbeat;
|
|
65
63
|
private initSegmentPromise?;
|
|
66
64
|
private lastMessage;
|
|
67
65
|
private log;
|
|
68
|
-
private
|
|
66
|
+
private sessionAbort;
|
|
69
67
|
private ws;
|
|
70
68
|
constructor(api: ProtectApi, log: ProtectLogging);
|
|
71
69
|
/**
|
|
@@ -82,7 +80,7 @@ export declare class ProtectLivestream extends EventEmitter {
|
|
|
82
80
|
* @event initsegment - Emitted with the initialization segment Buffer containing FTYP and MOOV boxes (stream mode disabled only).
|
|
83
81
|
* @event message - Emitted with each complete fMP4 segment Buffer, both initialization and regular segments (stream mode disabled only).
|
|
84
82
|
* @event segment - Emitted with each complete non-initialization segment Buffer containing MOOF/MDAT pairs (stream mode disabled only).
|
|
85
|
-
* @event timestamps - Emitted with decode timestamp arrays when
|
|
83
|
+
* @event timestamps - Emitted with decode timestamp arrays when emitTimestamps is enabled in options.
|
|
86
84
|
*
|
|
87
85
|
* @remarks Once a livestream session has started, the following events can be listened for (unless you've specified `useStream` in `options`, in which case only the
|
|
88
86
|
* `close` event is available):
|
|
@@ -126,9 +124,13 @@ export declare class ProtectLivestream extends EventEmitter {
|
|
|
126
124
|
/**
|
|
127
125
|
* Retrieve the initialization segment that must be at the start of every fMP4 stream.
|
|
128
126
|
*
|
|
127
|
+
* @remarks If the stream is stopped before the initialization segment is received, the returned promise rejects with an AbortError. Callers should handle this
|
|
128
|
+
* rejection or wrap the call with a timeout.
|
|
129
|
+
*
|
|
129
130
|
* @returns Returns a promise that resolves once the initialization segment has been seen, or returning it immediately if it already has been.
|
|
130
131
|
*/
|
|
131
132
|
getInitSegment(): Promise<Buffer>;
|
|
133
|
+
private awaitInitSegment;
|
|
132
134
|
/**
|
|
133
135
|
* The initialization segment that must be at the start of every fMP4 stream.
|
|
134
136
|
*
|
|
@@ -138,7 +140,7 @@ export declare class ProtectLivestream extends EventEmitter {
|
|
|
138
140
|
/**
|
|
139
141
|
* The codecs in use for this livestream session.
|
|
140
142
|
*
|
|
141
|
-
* @returns Returns a string containing the codec information,if it exists, or `null` otherwise.
|
|
143
|
+
* @returns Returns a string containing the codec information, if it exists, or `null` otherwise.
|
|
142
144
|
*
|
|
143
145
|
* @remarks Codec information is provided as `codec,container` where codec is either `avc` (H.264) or `hev` (H.265). The container format is always `mp4`.
|
|
144
146
|
*
|