Skip to content

Commit

Permalink
homekit: clean up late generator bug
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Apr 3, 2024
1 parent f2de58f commit 207cb9d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
14 changes: 5 additions & 9 deletions plugins/homekit/src/types/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,20 @@ addSupportedType({
const isRecordingEnabled = device.interfaces.includes(ScryptedInterface.MotionSensor);

let configuration: CameraRecordingConfiguration;
const openRecordingStreams = new Map<number, Deferred<any>>();
const openRecordingStreams = new Set<number>();
if (isRecordingEnabled) {
recordingDelegate = {
updateRecordingConfiguration(newConfiguration: CameraRecordingConfiguration) {
configuration = newConfiguration;
},
handleRecordingStreamRequest(streamId: number): AsyncGenerator<RecordingPacket> {
const ret = handleFragmentsRequests(streamId, device, configuration, console, homekitPlugin);
const d = new Deferred<any>();
d.promise.then(reason => {
ret.throw(new Error(reason.toString()));
openRecordingStreams.delete(streamId);
});
openRecordingStreams.set(streamId, d);
const ret = handleFragmentsRequests(streamId, device, configuration, console, homekitPlugin,
() => openRecordingStreams.has(streamId));
openRecordingStreams.add(streamId);
return ret;
},
closeRecordingStream(streamId, reason) {
openRecordingStreams.get(streamId)?.resolve(reason);
openRecordingStreams.delete(streamId);
},
updateRecordingActive(active) {
},
Expand Down
9 changes: 6 additions & 3 deletions plugins/homekit/src/types/camera/camera-recording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async function checkMp4StartsWithKeyFrame(console: Console, mp4: Buffer) {
}

export async function* handleFragmentsRequests(streamId: number, device: ScryptedDevice & VideoCamera & MotionSensor & AudioSensor,
configuration: CameraRecordingConfiguration, console: Console, homekitPlugin: HomeKitPlugin): AsyncGenerator<RecordingPacket> {
configuration: CameraRecordingConfiguration, console: Console, homekitPlugin: HomeKitPlugin, isOpen: () => boolean): AsyncGenerator<RecordingPacket> {

// homekitPlugin.storageSettings.values.lastKnownHomeHub = connection.remoteAddress;

Expand Down Expand Up @@ -319,6 +319,7 @@ export async function* handleFragmentsRequests(streamId: number, device: Scrypte
let needSkip = true;
let ftyp: Buffer[];
let moov: Buffer[];

for await (const box of generator) {
const { header, type, data } = box;
// console.log('motion fragment box', type);
Expand All @@ -331,7 +332,7 @@ export async function* handleFragmentsRequests(streamId: number, device: Scrypte
checkMp4 = false;
// pending will contain the moof
try {
if (!await checkMp4StartsWithKeyFrame(console, Buffer.concat([...ftyp, ...moov, ...pending, header, data]))) {
if (false && !await checkMp4StartsWithKeyFrame(console, Buffer.concat([...ftyp, ...moov, ...pending, header, data]))) {
needSkip = false;
pending = [];
continue;
Expand Down Expand Up @@ -360,17 +361,19 @@ export async function* handleFragmentsRequests(streamId: number, device: Scrypte
data: fragment,
isLast,
}
if (!isOpen())
return;
yield recordingPacket;
if (wasLast)
break;
}
}
console.log(`motion recording finished`);
}
catch (e) {
console.log(`motion recording completed ${e}`);
}
finally {
console.log(`motion recording finished`);
clearTimeout(videoTimeout);
cleanupPipes();
recordingFile?.end();
Expand Down

0 comments on commit 207cb9d

Please sign in to comment.