homebridge-hikvision-dvr 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ Smoother live video when Homebridge runs on a PC or server (anything that isn't a Raspberry Pi).
6
+
7
+ - On a machine with 4 or more cores and no Pi hardware encoder, software-encoded streams keep the DVR's own frame timing instead of being forced to exactly 25 fps, and use the `veryfast` x264 preset instead of `ultrafast` for a cleaner picture. Forcing 25 fps duplicated and dropped frames irregularly, because the DVR's frames arrive unevenly, which showed as judder.
8
+ - Raspberry Pi behaviour is unchanged.
9
+
3
10
  ## 1.2.2
4
11
 
5
12
  Fixes video freezing and high load on a Raspberry Pi when several cameras are opened at once (for example the 4-camera grid on an Apple TV).
package/lib/camera.js CHANGED
@@ -6,6 +6,8 @@ const { RtspClient } = require('./rtsp');
6
6
  const { HikDecryptor } = require('./hikcrypt');
7
7
  const { FlvWriter } = require('./flv');
8
8
  const { run, spawn, AnnexBSplitter } = require('./ffmpeg');
9
+ const fs = require('fs');
10
+ const os = require('os');
9
11
 
10
12
  const START_CODE = Buffer.from([0, 0, 0, 1]);
11
13
  const PROFILES = ['baseline', 'main', 'high'];
@@ -20,6 +22,8 @@ const PREPARE_TTL = 10000; // a prepared-but-not-started session counts towards
20
22
  const MAIN_MAX_VIEWERS = 2; // up to this many concurrent viewers get the main stream
21
23
  const SMALL = { width: 640, height: 360, bitrate: 800 }; // light path: sub stream scaled up, hardware encoded
22
24
  const MAX_HW_SESSIONS = 4; // hardware encodes at once; further viewers get the sub stream passed through
25
+ // A PC or server (not a Pi: no V4L2 M2M encoder node, 4+ cores) has CPU to spare for software encoding.
26
+ const FAST_HOST = os.cpus().length >= 4 && !fs.existsSync('/dev/video11');
23
27
  const MAX_SW_SESSIONS = 2; // libx264 encodes at once while the hardware encoder is marked broken or one hardware encode failed
24
28
  // Hardware DECODE (-c:v h264_v4l2m2m before -i) was measured on the Pi 3 A+ and is deliberately not used: with the DVR's FLV pipe
25
29
  // input it was slower to start (2-7 s vs 1.5 s), used 2.5-3.3 cores instead of ~1 (ffmpeg copies every frame out of the codec's
@@ -281,9 +285,12 @@ class HikCamera {
281
285
  // DVR main streams are often anamorphic (e.g. 960x1080 "1080P Lite"); tell the player the display aspect is 16:9.
282
286
  first = this.spawn(session, [...input, '-c:v', 'copy', '-bsf:v', `h264_metadata=sample_aspect_ratio=${useSub ? '16/11' : '2/1'}`, ...srtpOut]);
283
287
  } else if (mode === 'libx264') {
284
- first = this.spawn(session, [...input, '-vf', scale, '-c:v', 'libx264', '-preset', 'ultrafast', '-tune', 'zerolatency',
288
+ // On a fast host: a better preset, and frames go out with the DVR's own timing. Forcing -r 25 duplicates and drops
289
+ // frames irregularly because the DVR's frames arrive unevenly, which shows as judder. On a Pi nothing changes.
290
+ const timing = FAST_HOST ? ['-fps_mode', 'passthrough'] : ['-r', String(fps)];
291
+ first = this.spawn(session, [...input, '-vf', scale, '-c:v', 'libx264', '-preset', FAST_HOST ? 'veryfast' : 'ultrafast', '-tune', 'zerolatency',
285
292
  '-profile:v', PROFILES[video.profile] || 'main', '-level:v', LEVELS[video.level] || '3.1', '-bf', '0',
286
- '-r', String(fps), '-g', String(fps * 2), '-b:v', `${bitrate}k`, '-maxrate', `${bitrate}k`, '-bufsize', `${bitrate * 2}k`,
293
+ ...timing, '-g', String(fps * 2), '-b:v', `${bitrate}k`, '-maxrate', `${bitrate}k`, '-bufsize', `${bitrate * 2}k`,
287
294
  ...srtpOut]);
288
295
  } else {
289
296
  // Pi hardware encoder doesn't repeat SPS/PPS before keyframes, which HomeKit needs.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "homebridge-hikvision-dvr",
3
3
  "displayName": "Hikvision DVR / NVR Cameras",
4
- "version": "1.2.2",
4
+ "version": "1.3.0",
5
5
  "description": "Hikvision, HiLook and other Hik-based DVR/NVR cameras in HomeKit, with automatic camera discovery and support for encrypted (Hik-Connect / HiLook) streams.",
6
6
  "main": "index.js",
7
7
  "license": "MIT",