forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera-source.ts
656 lines (594 loc) · 19.4 KB
/
camera-source.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import { RingCamera } from 'ring-client-api'
import { hap } from './hap'
import {
doesFfmpegSupportCodec,
generateSrtpOptions,
ReturnAudioTranscoder,
RtpSplitter,
SrtpOptions,
} from '@homebridge/camera-utils'
import {
AudioStreamingCodecType,
AudioStreamingSamplerate,
CameraStreamingDelegate,
H264Level,
H264Profile,
PrepareStreamCallback,
PrepareStreamRequest,
SnapshotRequest,
SnapshotRequestCallback,
SRTPCryptoSuites,
StartStreamRequest,
StreamingRequest,
StreamRequestCallback,
} from 'homebridge'
import { logDebug, logError, logInfo } from 'ring-client-api/util'
import { debounceTime, delay, take } from 'rxjs/operators'
import { interval, merge, of, Subject } from 'rxjs'
import { readFile } from 'fs'
import { promisify } from 'util'
import { getFfmpegPath } from 'ring-client-api/ffmpeg'
import {
RtcpSenderInfo,
RtcpSrPacket,
RtpPacket,
SrtpSession,
SrtcpSession,
} from 'werift'
import type { StreamingSession } from 'ring-client-api/streaming/streaming-session'
import { OpusRepacketizer } from './opus-repacketizer'
const readFileAsync = promisify(readFile),
cameraOfflinePath = require.resolve('../media/camera-offline.jpg'),
snapshotsBlockedPath = require.resolve('../media/snapshots-blocked.jpg')
function getDurationSeconds(start: number) {
return (Date.now() - start) / 1000
}
function getSessionConfig(srtpOptions: SrtpOptions) {
return {
keys: {
localMasterKey: srtpOptions.srtpKey,
localMasterSalt: srtpOptions.srtpSalt,
remoteMasterKey: srtpOptions.srtpKey,
remoteMasterSalt: srtpOptions.srtpSalt,
},
profile: 1,
}
}
class StreamingSessionWrapper {
audioSsrc = hap.CameraController.generateSynchronisationSource()
videoSsrc = hap.CameraController.generateSynchronisationSource()
audioSrtp = generateSrtpOptions()
videoSrtp = generateSrtpOptions()
audioSplitter = new RtpSplitter()
videoSplitter = new RtpSplitter()
repacketizeAudioSplitter = new RtpSplitter()
libfdkAacInstalledPromise = doesFfmpegSupportCodec(
'libfdk_aac',
getFfmpegPath(),
)
.then((supported) => {
if (!supported) {
logError(
'Streaming video only - found ffmpeg, but libfdk_aac is not installed. See https://github.com/dgreif/ring/wiki/FFmpeg for details.',
)
}
return supported
})
.catch(() => {
logError(
'Streaming video only - ffmpeg was not found. See https://github.com/dgreif/ring/wiki/FFmpeg for details.',
)
return false
})
constructor(
public streamingSession: StreamingSession,
public prepareStreamRequest: PrepareStreamRequest,
public ringCamera: RingCamera,
public start: number,
) {
const {
targetAddress,
video: { port: videoPort },
} = prepareStreamRequest,
// used to encrypt rtcp to HomeKit for keepalive
videoSrtcpSession = new SrtcpSession(getSessionConfig(this.videoSrtp)),
onReturnPacketReceived = new Subject()
// Watch return packets to detect a dead stream from the HomeKit side
// This can happen if the user force-quits the Home app
this.videoSplitter.addMessageHandler(() => {
// return packet from HomeKit
onReturnPacketReceived.next(null)
return null
})
this.audioSplitter.addMessageHandler(() => {
// return packet from HomeKit
onReturnPacketReceived.next(null)
return null
})
streamingSession.addSubscriptions(
merge(of(true).pipe(delay(15000)), onReturnPacketReceived)
.pipe(debounceTime(5000))
.subscribe(() => {
logInfo(
`Live stream for ${
this.ringCamera.name
} appears to be inactive. (${getDurationSeconds(start)}s)`,
)
streamingSession.stop()
}),
)
// Periodically send a blank RTCP packet to the HomeKit video port
// Without this, HomeKit assumes the stream is dead after 30 second and sends a stop request
streamingSession.addSubscriptions(
interval(500).subscribe(() => {
const senderInfo = new RtcpSenderInfo({
ntpTimestamp: BigInt(0),
packetCount: 0,
octetCount: 0,
rtpTimestamp: 0,
}),
senderReport = new RtcpSrPacket({
ssrc: this.videoSsrc,
senderInfo: senderInfo,
}),
message = videoSrtcpSession.encrypt(senderReport.serialize())
this.videoSplitter
.send(message, {
port: videoPort,
address: targetAddress,
})
.catch(logError)
}),
)
}
private listenForAudioPackets(startStreamRequest: StartStreamRequest) {
const {
targetAddress,
audio: { port: audioPort },
} = this.prepareStreamRequest,
{
audio: {
codec: audioCodec,
sample_rate: audioSampleRate,
packet_time: audioPacketTime,
},
} = startStreamRequest,
// Repacketize the audio stream after it's been transcoded
opusRepacketizer = new OpusRepacketizer(audioPacketTime / 20),
audioIntervalScale = ((audioSampleRate / 8) * audioPacketTime) / 20,
audioSrtpSession = new SrtpSession(getSessionConfig(this.audioSrtp))
let firstTimestamp: number,
audioPacketCount = 0
this.repacketizeAudioSplitter.addMessageHandler(({ message }) => {
let rtp: RtpPacket | undefined = RtpPacket.deSerialize(message)
if (audioCodec === AudioStreamingCodecType.OPUS) {
// borrowed from scrypted
// Original source: https://github.com/koush/scrypted/blob/c13ba09889c3e0d9d3724cb7d49253c9d787fb97/plugins/homekit/src/types/camera/camera-streaming-srtp-sender.ts#L124-L143
rtp = opusRepacketizer.repacketize(rtp)
if (!rtp) {
return null
}
if (!firstTimestamp) {
firstTimestamp = rtp.header.timestamp
}
// from HAP spec:
// RTP Payload Format for Opus Speech and Audio Codec RFC 7587 with an exception
// that Opus audio RTP Timestamp shall be based on RFC 3550.
// RFC 3550 indicates that PCM audio based with a sample rate of 8k and a packet
// time of 20ms would have a monotonic interval of 8k / (1000 / 20) = 160.
// So 24k audio would have a monotonic interval of (24k / 8k) * 160 = 480.
// HAP spec also states that it may request packet times of 20, 30, 40, or 60.
// In practice, HAP has been seen to request 20 on LAN and 60 over LTE.
// So the RTP timestamp must scale accordingly.
// Further investigation indicates that HAP doesn't care about the actual sample rate at all,
// that's merely a suggestion. When encoding Opus, it can seemingly be an arbitrary sample rate,
// audio will work so long as the rtp timestamps are created properly: which is a construct of the sample rate
// HAP requests, and the packet time is respected,
// opus 48khz will work just fine.
rtp.header.timestamp =
(firstTimestamp + audioPacketCount * 160 * audioIntervalScale) %
0xffffffff
audioPacketCount++
}
// encrypt the packet
const encryptedPacket = audioSrtpSession.encrypt(rtp.payload, rtp.header)
// send the encrypted packet to HomeKit
this.audioSplitter
.send(encryptedPacket, {
port: audioPort,
address: targetAddress,
})
.catch(logError)
return null
})
}
async activate(request: StartStreamRequest) {
let sentVideo = false
const {
targetAddress,
video: { port: videoPort },
} = this.prepareStreamRequest,
// use to encrypt Ring video to HomeKit
videoSrtpSession = new SrtpSession(getSessionConfig(this.videoSrtp))
// Set up packet forwarding for video stream
this.streamingSession.addSubscriptions(
this.streamingSession.onVideoRtp.subscribe(({ header, payload }) => {
header.ssrc = this.videoSsrc
header.payloadType = request.video.pt
const encryptedPacket = videoSrtpSession.encrypt(payload, header)
if (!sentVideo) {
sentVideo = true
logInfo(
`Received stream data from ${
this.ringCamera.name
} (${getDurationSeconds(this.start)}s)`,
)
}
this.videoSplitter
.send(encryptedPacket, {
port: videoPort,
address: targetAddress,
})
.catch(logError)
}),
)
const shouldTranscodeAudio = await this.libfdkAacInstalledPromise
if (!shouldTranscodeAudio) {
return this.streamingSession.requestKeyFrame()
}
const transcodingPromise = this.streamingSession.startTranscoding({
input: ['-vn'],
audio: [
'-map',
'0:a',
...(request.audio.codec === AudioStreamingCodecType.OPUS
? [
// OPUS specific - it works, but audio is very choppy
'-acodec',
'libopus',
'-frame_duration',
request.audio.packet_time,
'-application',
'lowdelay',
]
: [
// AAC-eld specific
'-acodec',
'libfdk_aac',
'-profile:a',
'aac_eld',
'-eld_sbr:a',
'1',
'-eld_v2',
'1',
]),
// Shared options
'-flags',
'+global_header',
'-ac',
`${request.audio.channel}`,
'-ar',
`${request.audio.sample_rate}k`,
'-b:a',
`${request.audio.max_bit_rate}k`,
'-bufsize',
`${request.audio.max_bit_rate * 4}k`,
'-payload_type',
request.audio.pt,
'-ssrc',
this.audioSsrc,
'-f',
'rtp',
`rtp://127.0.0.1:${await this.repacketizeAudioSplitter
.portPromise}?pkt_size=376`,
],
video: false,
output: [],
})
let cameraSpeakerActive = false
// used to send return audio from HomeKit to Ring
const returnAudioTranscodedSplitter = new RtpSplitter(({ message }) => {
if (!cameraSpeakerActive) {
cameraSpeakerActive = true
this.streamingSession.activateCameraSpeaker()
}
// deserialize and send to Ring - werift will handle encryption and other header params
try {
const rtp: RtpPacket | undefined = RtpPacket.deSerialize(message)
this.streamingSession.sendAudioPacket(rtp)
} catch {
// deSerialize will sometimes fail, but the errors can be ignored
}
return null
}),
isRingUsingOpus = await this.streamingSession.isUsingOpus,
returnAudioTranscoder = new ReturnAudioTranscoder({
prepareStreamRequest: this.prepareStreamRequest,
startStreamRequest: request,
incomingAudioOptions: {
ssrc: this.audioSsrc,
rtcpPort: 0, // we don't care about rtcp for incoming audio
},
outputArgs: [
'-acodec',
...(isRingUsingOpus
? [
'libopus',
'-ac',
'1',
'-ar',
'24k',
'-b:a',
'24k',
'-application',
'lowdelay',
]
: ['pcm_mulaw', '-ac', 1, '-ar', '8k']),
'-flags',
'+global_header',
'-f',
'rtp',
`rtp://127.0.0.1:${await returnAudioTranscodedSplitter.portPromise}`,
],
ffmpegPath: getFfmpegPath(),
logger: {
info: logDebug,
error: logError,
},
logLabel: `Return Audio (${this.ringCamera.name})`,
returnAudioSplitter: this.audioSplitter,
})
this.streamingSession.onCallEnded.pipe(take(1)).subscribe(() => {
returnAudioTranscoder.stop()
returnAudioTranscodedSplitter.close()
})
this.listenForAudioPackets(request)
await returnAudioTranscoder.start()
await transcodingPromise
}
stop() {
this.audioSplitter.close()
this.repacketizeAudioSplitter.close()
this.videoSplitter.close()
this.streamingSession.stop()
}
}
export class CameraSource implements CameraStreamingDelegate {
public controller
private sessions: { [sessionKey: string]: StreamingSessionWrapper } = {}
private cachedSnapshot?: Buffer
constructor(
private ringCamera: RingCamera,
private useOpus = false,
) {
this.controller = new hap.CameraController({
cameraStreamCount: 10,
delegate: this,
streamingOptions: {
supportedCryptoSuites: [SRTPCryptoSuites.AES_CM_128_HMAC_SHA1_80],
video: {
resolutions: [
[1280, 720, 30],
[1024, 768, 30],
[640, 480, 30],
[640, 360, 30],
[480, 360, 30],
[480, 270, 30],
[320, 240, 30],
[320, 240, 15], // Apple Watch requires this configuration
[320, 180, 30],
],
codec: {
profiles: [H264Profile.BASELINE],
levels: [H264Level.LEVEL3_1],
},
},
audio: {
codecs: this.useOpus
? [
{
type: AudioStreamingCodecType.OPUS,
// required by watch
samplerate: AudioStreamingSamplerate.KHZ_8,
},
{
type: AudioStreamingCodecType.OPUS,
samplerate: AudioStreamingSamplerate.KHZ_16,
},
{
type: AudioStreamingCodecType.OPUS,
samplerate: AudioStreamingSamplerate.KHZ_24,
},
]
: [
{
type: AudioStreamingCodecType.AAC_ELD,
samplerate: AudioStreamingSamplerate.KHZ_16,
},
],
},
},
})
}
private previousLoadSnapshotPromise?: Promise<any>
async loadSnapshot(imageUuid?: string) {
// cache a promise of the snapshot load
// This prevents multiple concurrent requests for snapshot from pilling up and creating lots of logs
if (this.previousLoadSnapshotPromise) {
return this.previousLoadSnapshotPromise
}
this.previousLoadSnapshotPromise = this.loadAndCacheSnapshot(imageUuid)
try {
await this.previousLoadSnapshotPromise
} catch {
// ignore errors
} finally {
// clear so another request can be made
this.previousLoadSnapshotPromise = undefined
}
}
fn = 1
private async loadAndCacheSnapshot(imageUuid?: string) {
const start = Date.now()
logDebug(
`Loading new snapshot into cache for ${this.ringCamera.name}${
imageUuid ? ' by uuid' : ''
}`,
)
try {
const previousSnapshot = this.cachedSnapshot,
newSnapshot = await this.ringCamera.getSnapshot({ uuid: imageUuid })
this.cachedSnapshot = newSnapshot
if (previousSnapshot !== newSnapshot) {
// Keep the snapshots in cache 2 minutes longer than their lifetime
// This allows users on LTE with wired camera to get snapshots each 60 second pull even though the cached snapshot is out of date
setTimeout(
() => {
if (this.cachedSnapshot === newSnapshot) {
this.cachedSnapshot = undefined
}
},
this.ringCamera.snapshotLifeTime + 2 * 60 * 1000,
)
}
logDebug(
`Snapshot cached for ${this.ringCamera.name}${
imageUuid ? ' by uuid' : ''
} (${getDurationSeconds(start)}s)`,
)
} catch (e: any) {
this.cachedSnapshot = undefined
logDebug(
`Failed to cache snapshot for ${
this.ringCamera.name
} (${getDurationSeconds(
start,
)}s), The camera currently reports that it is ${
this.ringCamera.isOffline ? 'offline' : 'online'
}`,
)
// log additioanl snapshot error message if one is present
if (e.message.includes('Snapshot')) {
logDebug(e.message)
}
}
}
private getCurrentSnapshot() {
if (this.ringCamera.isOffline) {
return readFileAsync(cameraOfflinePath)
}
if (this.ringCamera.snapshotsAreBlocked) {
return readFileAsync(snapshotsBlockedPath)
}
logDebug(
`${
this.cachedSnapshot ? 'Used cached snapshot' : 'No snapshot cached'
} for ${this.ringCamera.name}`,
)
if (!this.ringCamera.hasSnapshotWithinLifetime) {
this.loadSnapshot().catch(logError)
}
// may or may not have a snapshot cached
return this.cachedSnapshot
}
async handleSnapshotRequest(
request: SnapshotRequest,
callback: SnapshotRequestCallback,
) {
try {
const snapshot = await this.getCurrentSnapshot()
if (!snapshot) {
// return an error to prevent "empty image buffer" warnings
return callback(new Error('No Snapshot Cached'))
}
// Not currently resizing the image.
// HomeKit does a good job of resizing and doesn't seem to care if it's not right
callback(undefined, snapshot)
} catch (e: any) {
logError(`Error fetching snapshot for ${this.ringCamera.name}`)
logError(e)
callback(e)
}
}
async prepareStream(
request: PrepareStreamRequest,
callback: PrepareStreamCallback,
) {
const start = Date.now()
logInfo(`Preparing Live Stream for ${this.ringCamera.name}`)
try {
const liveCall = await this.ringCamera.startLiveCall(),
session = new StreamingSessionWrapper(
liveCall,
request,
this.ringCamera,
start,
)
this.sessions[request.sessionID] = session
logInfo(
`Stream Prepared for ${this.ringCamera.name} (${getDurationSeconds(
start,
)}s)`,
)
callback(undefined, {
audio: {
port: await session.audioSplitter.portPromise,
ssrc: session.audioSsrc,
srtp_key: session.audioSrtp.srtpKey,
srtp_salt: session.audioSrtp.srtpSalt,
},
video: {
port: await session.videoSplitter.portPromise,
ssrc: session.videoSsrc,
srtp_key: session.videoSrtp.srtpKey,
srtp_salt: session.videoSrtp.srtpSalt,
},
})
} catch (e: any) {
logError(
`Failed to prepare stream for ${
this.ringCamera.name
} (${getDurationSeconds(start)}s)`,
)
logError(e)
callback(e)
}
}
async handleStreamRequest(
request: StreamingRequest,
callback: StreamRequestCallback,
) {
const sessionID = request.sessionID,
session = this.sessions[sessionID],
requestType = request.type
if (!session) {
callback(new Error('Cannot find session for stream ' + sessionID))
return
}
if (requestType === 'start') {
logInfo(
`Activating stream for ${this.ringCamera.name} (${getDurationSeconds(
session.start,
)}s)`,
)
try {
await session.activate(request)
} catch (e) {
logError('Failed to activate stream')
logError(e)
callback(new Error('Failed to activate stream'))
return
}
logInfo(
`Streaming active for ${this.ringCamera.name} (${getDurationSeconds(
session.start,
)}s)`,
)
} else if (requestType === 'stop') {
logInfo(`Stopped Live Stream for ${this.ringCamera.name}`)
session.stop()
delete this.sessions[sessionID]
}
callback()
}
}