Skip to content

Commit

Permalink
snapshot: better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Apr 3, 2024
1 parent 95ac72c commit e6cb411
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
4 changes: 2 additions & 2 deletions plugins/snapshot/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/snapshot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/snapshot",
"version": "0.2.43",
"version": "0.2.44",
"description": "Snapshot Plugin for Scrypted",
"scripts": {
"scrypted-setup-project": "scrypted-setup-project",
Expand Down
2 changes: 1 addition & 1 deletion plugins/snapshot/src/image-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export class ImageConverter extends ScryptedDeviceBase implements BufferConverte
const op = parseImageOp(mime.parameters);
const ffmpegInput = JSON.parse(data.toString()) as FFmpegInput;

return processImageOp(ffmpegInput, op, parseFloat(mime.parameters.get('time')), options?.sourceId, this.plugin.debugConsole);
return processImageOp(ffmpegInput, op, parseFloat(mime.parameters.get('time')), options?.sourceId, !!this.plugin.debugConsole);
}
}
2 changes: 2 additions & 0 deletions plugins/snapshot/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase<Camera> implements Camera {
const takePicture = await preparePrebufferSnapshot()
if (!takePicture)
throw e;
this.console.error('Snapshot failed, falling back to prebuffer', e);
return takePicture();
}

Expand Down Expand Up @@ -505,6 +506,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase<Camera> implements Camera {
return this.progressPicture.promise;
}
else {
this.console.error('Snapshot failed', e);
this.errorPicture = singletonPromise(this.errorPicture,
() => this.createTextErrorImage('Snapshot Failed'));
return this.errorPicture.promise;
Expand Down
57 changes: 40 additions & 17 deletions plugins/snapshot/src/parse-dims.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sdk, { FFmpegInput, RecordingStreamThumbnailOptions } from '@scrypted/sdk';
import { Console } from 'console';
import { PassThrough } from 'stream';
import url from 'url';
import type { MIMETypeParameters } from 'whatwg-mimetype';
import { FFmpegImageFilterOptions, ffmpegFilterImage, ffmpegFilterImageBuffer } from './ffmpeg-image-filter';
Expand Down Expand Up @@ -71,7 +73,7 @@ export function toImageOp(options: RecordingStreamThumbnailOptions) {
return ret;
}

export async function processImageOp(input: string | FFmpegInput | Buffer, op: ImageOp, time: number, sourceId: string, debugConsole: Console): Promise<Buffer> {
export async function processImageOp(input: string | FFmpegInput | Buffer, op: ImageOp, time: number, sourceId: string, debug?: boolean): Promise<Buffer> {
const { crop, resize } = op;
const { width, height, fractional } = resize || {};
const { left, top, right, bottom, fractional: cropFractional } = crop || {};
Expand Down Expand Up @@ -120,8 +122,19 @@ export async function processImageOp(input: string | FFmpegInput | Buffer, op: I
}
}

const out = new PassThrough();
let console = new Console(out, out);
const printConsole = () => {
if (!console)
return;
console = undefined;
const data = out.read().toString();
const deviceConsole = sdk.deviceManager.getMixinConsole(sourceId);
deviceConsole.log(data);
}

const ffmpegOpts: FFmpegImageFilterOptions = {
console: debugConsole,
console,
ffmpegPath: await sdk.mediaManager.getFFmpegPath(),
resize: width === undefined && height === undefined
? undefined
Expand All @@ -143,22 +156,32 @@ export async function processImageOp(input: string | FFmpegInput | Buffer, op: I
time,
};

if (Buffer.isBuffer(input)) {
return ffmpegFilterImageBuffer(input, ffmpegOpts);
}
try {
if (Buffer.isBuffer(input)) {
return await ffmpegFilterImageBuffer(input, ffmpegOpts);
}

const ffmpegInput: FFmpegInput = typeof input !== 'string'
? input
: {
inputArguments: [
'-i', input,
]
};
const ffmpegInput: FFmpegInput = typeof input !== 'string'
? input
: {
inputArguments: [
'-i', input,
]
};

const args = [
...ffmpegInput.inputArguments,
...(ffmpegInput.h264EncoderArguments || []),
];
const args = [
...ffmpegInput.inputArguments,
...(ffmpegInput.h264EncoderArguments || []),
];

return ffmpegFilterImage(args, ffmpegOpts);
return await ffmpegFilterImage(args, ffmpegOpts);
}
catch (e) {
printConsole();
throw e;
}
finally {
if (debug)
printConsole();
}
}

0 comments on commit e6cb411

Please sign in to comment.