Skip to content

Commit

Permalink
onvif/unifi: two way audio quality
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Mar 6, 2024
1 parent afd4927 commit 1c3c75d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions plugins/onvif/src/onvif-intercom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class OnvifIntercom implements Intercom {
encoderArguments: [
'-acodec', defaultMatch.ffmpegEncoder,
'-ar', defaultMatch.clock.toString(),
"-b:a", "64k",
'-ac', defaultMatch.channels?.toString() || '1',
],
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/unifi-protect/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/unifi-protect/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/unifi-protect",
"version": "0.0.142",
"version": "0.0.143",
"description": "Unifi Protect Plugin for Scrypted",
"author": "Scrypted",
"license": "Apache",
Expand Down
45 changes: 32 additions & 13 deletions plugins/unifi-protect/src/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { fitHeightToWidth } from "@scrypted/common/src/resolution-utils";
import sdk, { Camera, DeviceProvider, FFmpegInput, Intercom, MediaObject, MediaStreamOptions, MediaStreamUrl, MotionSensor, Notifier, NotifierOptions, ObjectDetectionTypes, ObjectDetector, ObjectsDetected, OnOff, Online, PanTiltZoom, PanTiltZoomCommand, PictureOptions, ResponseMediaStreamOptions, ResponsePictureOptions, ScryptedDeviceBase, ScryptedInterface, ScryptedMimeTypes, Setting, Settings, VideoCamera, VideoCameraConfiguration } from "@scrypted/sdk";
import child_process, { ChildProcess } from 'child_process';
import { once } from "events";
import { Readable } from "stream";
import { PassThrough, Readable } from "stream";
import WS from 'ws';
import { UnifiProtect } from "./main";
import { MOTION_SENSOR_TIMEOUT, UnifiMotionDevice, debounceMotionDetected } from './motion';
import { FeatureFlagsShim } from "./shim";
import { ProtectCameraChannelConfig, ProtectCameraConfigInterface, ProtectCameraLcdMessagePayload } from "./unifi-protect";
import { readLength } from '@scrypted/common/src/read-stream';

const { log, deviceManager, mediaManager } = sdk;

Expand Down Expand Up @@ -135,14 +136,15 @@ export class UnifiCamera extends ScryptedDeviceBase implements Notifier, Interco
args.push(
"-acodec", "aac",
"-profile:a", "aac_low",
"-threads", "0",
// "-threads", "0",
"-avioflags", "direct",
'-fflags', '+flush_packets', '-flush_packets', '1',
"-flags", "+global_header",
"-ar", camera.talkbackSettings.samplingRate.toString(),
"-ac", camera.talkbackSettings.channels.toString(),
"-b:a", "16k",
"-b:a", "64k",
"-f", "adts",
"-muxdelay", "0",
`pipe:3`,
);

Expand All @@ -165,16 +167,33 @@ export class UnifiCamera extends ScryptedDeviceBase implements Notifier, Interco

try {
while (websocket.readyState === WS.OPEN) {
await once(socket, 'readable');
while (true) {
const data = socket.read();
if (!data)
break;
websocket.send(data, e => {
if (e) {
safeKillFFmpeg(cp);
}
});
// this parses out full adts packets to ensure there's no truncation on ws message boundary, but
// does not seem to matter.
// preferring this as it seems to be the "right" thing to do.
if (true) {
const buffers: Buffer[] = [];
do {
const header = await readLength(socket, 7);
const frameLength = ((header[3] & 0x03) << 11) | (header[4] << 3) | ((header[5] & 0xE0) >> 5);
const need = frameLength - 7;
const data = await readLength(socket, need);
buffers.push(header, data);
}
while (socket.readableLength > 7 && buffers.length < 10);
websocket.send(Buffer.concat(buffers));
}
else {
await once(socket, 'readable');
while (true) {
const data = socket.read();
if (!data)
break;
websocket.send(data, e => {
if (e) {
safeKillFFmpeg(cp);
}
});
}
}
}
}
Expand Down

0 comments on commit 1c3c75d

Please sign in to comment.