homekit: snapshot perf fix

This commit is contained in:
Koushik Dutta
2021-09-13 20:41:58 -07:00
parent a54cd7e112
commit 947b4abc7c
3 changed files with 20 additions and 7 deletions

View File

@@ -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",

View File

@@ -40,5 +40,5 @@
"@types/qrcode": "^1.4.1",
"@types/url-parse": "^1.4.3"
},
"version": "0.0.54"
"version": "0.0.56"
}

View File

@@ -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()) {