diff --git a/plugins/homekit/package-lock.json b/plugins/homekit/package-lock.json index 517f9d57d..235031a2c 100644 --- a/plugins/homekit/package-lock.json +++ b/plugins/homekit/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/homekit", - "version": "0.0.54", + "version": "0.0.56", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/homekit", - "version": "0.0.54", + "version": "0.0.56", "dependencies": { "hap-nodejs": "file:../HAP-NodeJS", "lodash": "^4.17.21", diff --git a/plugins/homekit/package.json b/plugins/homekit/package.json index 1d167a206..a28b454cc 100644 --- a/plugins/homekit/package.json +++ b/plugins/homekit/package.json @@ -40,5 +40,5 @@ "@types/qrcode": "^1.4.1", "@types/url-parse": "^1.4.3" }, - "version": "0.0.54" + "version": "0.0.56" } diff --git a/plugins/homekit/src/types/camera.ts b/plugins/homekit/src/types/camera.ts index 88c8063b4..9583b6059 100644 --- a/plugins/homekit/src/types/camera.ts +++ b/plugins/homekit/src/types/camera.ts @@ -15,6 +15,7 @@ import { CameraRecordingDelegate, CharacteristicEventTypes, CharacteristicValue, import { AudioRecordingCodec, AudioRecordingCodecType, AudioRecordingSamplerate, AudioRecordingSamplerateValues, CameraRecordingConfiguration, CameraRecordingOptions } from '../../HAP-NodeJS/src/lib/camera/RecordingManagement'; import { startFFMPegFragmetedMP4Session } from '@scrypted/common/src/ffmpeg-mp4-parser-session'; import { ffmpegLogInitialOutput } from '../../../../common/src/ffmpeg-helper'; +import throttle from 'lodash/throttle'; const { log, mediaManager, deviceManager } = sdk; @@ -153,15 +154,27 @@ addSupportedType({ session.audioReturn?.close(); } + const throttledTakePicture = throttle(async () => { + console.log('snapshot throttle fetch', device.name); + const media = await device.takePicture(); + const jpeg = await mediaManager.convertMediaObjectToBuffer(media, 'image/jpeg'); + return jpeg; + }, 9000, { + leading: true, + trailing: true, + }); + const delegate: CameraStreamingDelegate = { async handleSnapshotRequest(request: SnapshotRequest, callback: SnapshotRequestCallback) { try { - console.log('snapshot request', request); + // an idle Home.app will hit this endpoint every 10 seconds, and slow requests bog up the entire app. + // avoid slow requests by prefetching every 9 seconds. if (device.interfaces.includes(ScryptedInterface.Camera)) { - const media = await device.takePicture(); - const jpeg = await mediaManager.convertMediaObjectToBuffer(media, 'image/jpeg'); - callback(null, jpeg); + // this call is not a bug, to force lodash to take a picture on the trailing edge, + // throttle must be called twice. + throttledTakePicture(); + callback(null, await throttledTakePicture()); return; } if (lastPicture + 60000 > Date.now()) {