forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecordingManagement.ts
369 lines (303 loc) · 12.7 KB
/
RecordingManagement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { Characteristic, CharacteristicEventTypes } from "../Characteristic"
import { CameraRecordingDelegate } from "../controller"
import { CameraRecordingManagement, SelectedCameraRecordingConfiguration } from "../definitions"
import { Service } from "../Service"
import { H264CodecParameters, H264Level, H264Profile, Resolution } from "./RTPStreamManagement"
import createDebug from 'debug';
import * as tlv from '../util/tlv';
import { VideoCodecType } from "."
const debug = createDebug('HAP-NodeJS:Camera:RecordingManagement');
export type CameraRecordingConfiguration = {
mediaContainerConfiguration: MediaContainerConfiguration & {
prebufferLength: number,
},
videoCodec: {
type: VideoCodecType;
iFrameInterval: number,
level: H264Level,
profile: H264Profile,
resolution: Resolution,
bitrate: number,
},
audioCodec: AudioRecordingCodec & {
bitrate: number,
samplerate: AudioRecordingSamplerate,
},
}
export type CameraRecordingOptions = {
prebufferLength: number;
eventTriggerOptions: number;
mediaContainerConfigurations: MediaContainerConfiguration[];
video: VideoRecordingOptions,
audio: AudioRecordingOptions,
motionService?: boolean;
}
export type MediaContainerConfiguration = {
type: number;
fragmentLength: number;
}
export type VideoRecordingOptions = {
// codec is defaulted to h264.
codec: H264CodecParameters,
resolutions: Resolution[],
}
export type AudioRecordingOptions = {
codecs: AudioRecordingCodec[],
}
export type AudioRecordingCodec = {
type: AudioRecordingCodecType,
audioChannels: number, // default 1
bitrateMode: AudioBitrate, // default VARIABLE, AAC-ELD or OPUS MUST support VARIABLE bitrate
samplerate: AudioRecordingSamplerate[] | AudioRecordingSamplerate,
}
const enum AudioBitrate {
VARIABLE = 0x00,
CONSTANT = 0x01,
}
const enum VideoCodecConfigurationTypes {
CODEC_TYPE = 0x01,
CODEC_PARAMETERS = 0x02,
ATTRIBUTES = 0x03,
}
const enum VideoCodecParametersTypes {
PROFILE_ID = 0x01,
LEVEL = 0x02,
BITRATE = 0x03,
IFRAME_INTERVAL = 0x04,
}
const enum VideoAttributesTypes {
IMAGE_WIDTH = 0x01,
IMAGE_HEIGHT = 0x02,
FRAME_RATE = 0x03,
}
const enum SelectedCameraRecordingConfigurationTypes {
SELECTED_GENERAL_CONFIGURATION = 0x01,
SELECTED_VIDEO_CONFIGURATION = 0x02,
SELECTED_AUDIO_CONFIGURATION = 0x03,
}
export type AudioRecordingParameters = {
audioChannels?: number, // default 1
bitrate?: AudioBitrate, // default VARIABLE
samplerate: AudioRecordingSamplerate[] | AudioRecordingSamplerate,
}
export const enum AudioRecordingCodecType { // codecs as defined by the HAP spec; only AAC-ELD and OPUS seem to work
AAC_LC = 0,
AAC_ELD = 1,
}
export const enum AudioRecordingSamplerate {
KHZ_8 = 0,
KHZ_16 = 1,
KHZ_24 = 2,
KHZ_32 = 3,
KHZ_44_1 = 4,
KHZ_48 = 5,
}
export const AudioRecordingSamplerateValues = {
0: 8,
1: 16,
2: 24,
3: 32,
4: 44.1,
5: 48,
};
const enum SupportedVideoRecordingConfigurationTypes {
VIDEO_CODEC_CONFIGURATION = 0x01,
}
const enum SupportedCameraRecordingConfigurationTypes {
PREBUFFER_LENGTH = 0x01,
EVENT_TRIGGER_OPTIONS = 0x02,
MEDIA_CONTAINER_CONFIGURATIONS = 0x03
}
const enum MediaContainerConfigurationTypes {
MEDIA_CONTAINER_TYPE = 0x01,
MEDIA_CONTAINER_PARAMETERS = 0x02,
}
const enum MediaContainerParameterTypes {
FRAGMENT_LENGTH = 0x01,
}
const enum AudioCodecParametersTypes {
CHANNEL = 0x01,
BIT_RATE = 0x02,
SAMPLE_RATE = 0x03,
MAX_AUDIO_BITRATE = 0x04 // only present in selected audio codec parameters tlv
}
const enum AudioCodecConfigurationTypes {
CODEC_TYPE = 0x01,
CODEC_PARAMETERS = 0x02,
}
const enum SupportedAudioRecordingConfigurationTypes {
AUDIO_CODEC_CONFIGURATION = 0x01,
}
export class RecordingManagement {
private delegate: CameraRecordingDelegate;
private service: CameraRecordingManagement;
private readonly supportedCameraRecordingConfiguration: string;
private readonly supportedVideoRecordingConfiguration: string;
private readonly supportedAudioRecordingConfiguration: string;
private static _supportedCameraRecordingConfiguration(options: CameraRecordingOptions): string {
const prebufferLength = Buffer.alloc(4);
const eventTriggerOptions = Buffer.alloc(8);
prebufferLength.writeInt32LE(options.prebufferLength, 0);
eventTriggerOptions.writeInt32LE(options.eventTriggerOptions, 0);
return tlv.encode(
SupportedCameraRecordingConfigurationTypes.PREBUFFER_LENGTH, prebufferLength,
SupportedCameraRecordingConfigurationTypes.EVENT_TRIGGER_OPTIONS, eventTriggerOptions,
SupportedCameraRecordingConfigurationTypes.MEDIA_CONTAINER_CONFIGURATIONS, options.mediaContainerConfigurations.map(config => {
const fragmentLength = Buffer.alloc(4);
fragmentLength.writeInt32LE(config.fragmentLength, 0);
return tlv.encode(
MediaContainerConfigurationTypes.MEDIA_CONTAINER_TYPE, config.type,
MediaContainerConfigurationTypes.MEDIA_CONTAINER_PARAMETERS, tlv.encode(
MediaContainerParameterTypes.FRAGMENT_LENGTH, fragmentLength,
)
);
})
).toString('base64');
}
private static _supportedVideoRecordingConfiguration(videoOptions: VideoRecordingOptions): string {
if (!videoOptions.codec) {
throw new Error('Video codec cannot be undefined');
}
if (!videoOptions.resolutions) {
throw new Error('Video resolutions cannot be undefined');
}
let codecParameters = tlv.encode(
VideoCodecParametersTypes.PROFILE_ID, videoOptions.codec.profiles,
VideoCodecParametersTypes.LEVEL, videoOptions.codec.levels,
);
const videoStreamConfiguration = tlv.encode(
VideoCodecConfigurationTypes.CODEC_TYPE, videoOptions.codec.type || VideoCodecType.H264,
VideoCodecConfigurationTypes.CODEC_PARAMETERS, codecParameters,
VideoCodecConfigurationTypes.ATTRIBUTES, videoOptions.resolutions.map(resolution => {
if (resolution.length != 3) {
throw new Error('Unexpected video resolution');
}
const width = Buffer.alloc(2);
const height = Buffer.alloc(2);
const frameRate = Buffer.alloc(1);
width.writeUInt16LE(resolution[0], 0);
height.writeUInt16LE(resolution[1], 0);
frameRate.writeUInt8(resolution[2], 0);
return tlv.encode(
VideoAttributesTypes.IMAGE_WIDTH, width,
VideoAttributesTypes.IMAGE_HEIGHT, height,
VideoAttributesTypes.FRAME_RATE, frameRate,
);
}),
);
return tlv.encode(
SupportedVideoRecordingConfigurationTypes.VIDEO_CODEC_CONFIGURATION, videoStreamConfiguration,
).toString('base64');
}
private _supportedAudioStreamConfiguration(audioOptions?: AudioRecordingOptions): string {
// Only AAC-ELD and OPUS are accepted by iOS currently, and we need to give it something it will accept
// for it to start the video stream.
const supportedCodecs: AudioRecordingCodec[] = (audioOptions && audioOptions.codecs) || [];
if (supportedCodecs.length === 0) { // Fake a Codec if we haven't got anything
debug("Client doesn't support any audio codec that HomeKit supports.");
supportedCodecs.push({
type: AudioRecordingCodecType.AAC_LC,
bitrateMode: 0,
audioChannels: 1,
samplerate: [AudioRecordingSamplerate.KHZ_16, AudioRecordingSamplerate.KHZ_24], // 16 and 24 must be supported
});
}
const codecConfigurations: Buffer[] = supportedCodecs.map(codec => {
const providedSamplerates = (typeof codec.samplerate === "number" ? [codec.samplerate] : codec.samplerate);
if (providedSamplerates.length === 0) {
throw new Error("Audio samplerate cannot be empty!");
}
const audioParameters = tlv.encode(
AudioCodecParametersTypes.CHANNEL, Math.max(1, codec.audioChannels || 1),
AudioCodecParametersTypes.BIT_RATE, codec.bitrateMode || AudioBitrate.VARIABLE,
AudioCodecParametersTypes.SAMPLE_RATE, providedSamplerates,
)
return tlv.encode(
AudioCodecConfigurationTypes.CODEC_TYPE, codec.type,
AudioCodecConfigurationTypes.CODEC_PARAMETERS, audioParameters
);
});
return tlv.encode(
SupportedAudioRecordingConfigurationTypes.AUDIO_CODEC_CONFIGURATION, codecConfigurations,
).toString("base64");
}
constructor(options: CameraRecordingOptions, delegate: CameraRecordingDelegate, service?: CameraRecordingManagement) {
this.delegate = delegate;
this.service = service || this.constructService();
this.setupServiceHandlers();
this.supportedCameraRecordingConfiguration = RecordingManagement._supportedCameraRecordingConfiguration(options);
this.supportedVideoRecordingConfiguration = RecordingManagement._supportedVideoRecordingConfiguration(options.video);
this.supportedAudioRecordingConfiguration = this._supportedAudioStreamConfiguration(options.audio);
}
private constructService(): CameraRecordingManagement {
const managementService = new Service.CameraRecordingManagement('', '');
managementService.getCharacteristic(Characteristic.SupportedCameraRecordingConfiguration)
.on('get', callback => {
callback(null, this.supportedCameraRecordingConfiguration);
});
managementService.getCharacteristic(Characteristic.SupportedVideoRecordingConfiguration)
.on('get', callback => {
callback(null, this.supportedVideoRecordingConfiguration);
});
managementService.getCharacteristic(Characteristic.SupportedAudioRecordingConfiguration)
.on('get', callback => {
callback(null, this.supportedAudioRecordingConfiguration);
});
return managementService;
}
private setupServiceHandlers() {
this.service.setCharacteristic(Characteristic.SelectedCameraRecordingConfiguration, '');
}
static parseSelectedConfiguration(selectedconfiguration: string): CameraRecordingConfiguration {
const decoded = tlv.decode(Buffer.from(selectedconfiguration, 'base64'));
const recording = tlv.decode(decoded[SelectedCameraRecordingConfigurationTypes.SELECTED_GENERAL_CONFIGURATION]);
const video = tlv.decode(decoded[SelectedCameraRecordingConfigurationTypes.SELECTED_VIDEO_CONFIGURATION]);
const audio = tlv.decode(decoded[SelectedCameraRecordingConfigurationTypes.SELECTED_AUDIO_CONFIGURATION]);
const vcodec = video[VideoCodecConfigurationTypes.CODEC_TYPE][0];
const vparameters = tlv.decode(video[VideoCodecConfigurationTypes.CODEC_PARAMETERS]);
const vattributes = tlv.decode(video[VideoCodecConfigurationTypes.ATTRIBUTES]);
const width = vattributes[VideoAttributesTypes.IMAGE_WIDTH].readInt16LE(0);
const height = vattributes[VideoAttributesTypes.IMAGE_HEIGHT].readInt16LE(0);
const framerate = vattributes[VideoAttributesTypes.FRAME_RATE][0];
const profile = vparameters[VideoCodecParametersTypes.PROFILE_ID][0];
const level = vparameters[VideoCodecParametersTypes.LEVEL][0];
const videoBitrate = vparameters[VideoCodecParametersTypes.BITRATE].readInt32LE(0);
const iFrameInterval = vparameters[VideoCodecParametersTypes.IFRAME_INTERVAL].readInt32LE(0);
const prebufferLength = recording[SupportedCameraRecordingConfigurationTypes.PREBUFFER_LENGTH].readInt32LE(0);
const mediaContainerConfiguration = tlv.decode(recording[SupportedCameraRecordingConfigurationTypes.MEDIA_CONTAINER_CONFIGURATIONS]);
const containerType = mediaContainerConfiguration[MediaContainerConfigurationTypes.MEDIA_CONTAINER_TYPE][0];
const mediaContainerParameters = tlv.decode(mediaContainerConfiguration[MediaContainerConfigurationTypes.MEDIA_CONTAINER_PARAMETERS]);
const fragmentLength = mediaContainerParameters[MediaContainerParameterTypes.FRAGMENT_LENGTH].readInt32LE(0);
const acodec = audio[AudioCodecConfigurationTypes.CODEC_TYPE][0];
const audioParameters = tlv.decode(audio[AudioCodecConfigurationTypes.CODEC_PARAMETERS]);
const audioChannels = audioParameters[AudioCodecParametersTypes.CHANNEL][0];
const samplerate = audioParameters[AudioCodecParametersTypes.SAMPLE_RATE][0];
const audioBitrateMode = audioParameters[AudioCodecParametersTypes.BIT_RATE][0];
const audioBitrate = audioParameters[AudioCodecParametersTypes.MAX_AUDIO_BITRATE].readUInt32LE(0);
return {
mediaContainerConfiguration: {
prebufferLength,
type: containerType,
fragmentLength,
},
videoCodec: {
type: vcodec,
bitrate: videoBitrate,
level,
profile,
resolution: [width, height, framerate],
iFrameInterval,
},
audioCodec: {
audioChannels,
type: acodec,
samplerate,
bitrateMode: audioBitrateMode,
bitrate: audioBitrate,
},
};
}
getService(): CameraRecordingManagement {
return this.service;
}
}