Commit Graph
43 Commits
Author SHA1 Message Date
SteveGilvarryandClaude Opus 4.8 45099e53a7 fix: address review feedback on camera buffer overflow fix
- zm_libvnc_camera.cpp resize(): return FALSE and log if av_malloc fails,
  rather than returning TRUE with a null frameBuffer (libVNC would then
  write into it and crash). Matters more now that size_t sizing can
  request large allocations for server-advertised dimensions.
- zm_libvnc_camera.cpp: compute the buffer sizes in the scale Debug() in
  size_t with %zu, so the logged sizes can't overflow int and match the
  values passed to SWScale::Convert.
- zm_libvlc_camera: widen LibvlcPrivateData::bufferSize to size_t and
  compute it in size_t in PrimeCapture, so the allocation itself can't
  overflow. Image::Assign now passes the same stored bufferSize used for
  the allocation, so the read size can't exceed the buffer. Widen the
  compare loop index to size_t to match.

Verified: both translation units compile with -Werror -fsyntax-only.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 07:54:15 +10:00
SteveGilvarryandClaude Opus 4.8 286cb90883 fix: compute camera frame buffer sizes in size_t to avoid int overflow
The frame buffer size calculations in the libVNC and libVLC camera
backends multiplied 16/32-bit operands and only widened the result to
size_t afterwards, so the multiplication itself could overflow before
the conversion. Flagged by CodeQL cpp/integer-multiplication-cast-to-long
(alerts 282, 145, 85). The VNC framebuffer dimensions are advertised by
the remote server (uint16_t), making the inputs externally influenced.

Cast the first operand to size_t so each product is computed in size_t:
- zm_libvnc_camera.cpp: SwScale::Convert src size (framebufferWidth *
  framebufferHeight * 4) and dest size (width * height * colours)
- zm_libvnc_camera.cpp: resize() bufferSize, changed from int to size_t
  (not converted to a larger type, so CodeQL did not flag it, but the
  same overflow applied); Debug format updated to %zu
- zm_libvlc_camera.cpp: Image::Assign size (width * height * mBpp)

Verified: both translation units compile with -Werror -fsyntax-only.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 21:55:06 +10:00
SteveGilvarryandClaude Fable 5 7e15324385 fix: corrupted scaled/converted output for rotated monitors — SWScale guessed buffer alignment from width
SWScale::Convert chose av_image_fill_arrays alignment by heuristic
(width % 32 ? 1 : 32) for both buffers. Image buffers are always laid
out align-32, so any width not divisible by 32 made Convert read luma
rows 16 bytes short and chroma planes from packed offsets: diagonal
shear plus garbage chroma. Rotating a monitor is what produces such
widths (1280x720 ROTATE_270 -> 720 wide, 3840x2160 ROTATE_90 -> 2160
wide, both % 32 == 16), so every scaled view and every re-encode of a
rotated monitor was corrupted while unrotated monitors (1280/2688/3840
all % 32 == 0) were untouched. The rotate/flip segfault fix exposed
this: before it, rotated planar frames crashed zmc before reaching
Scale.

Alignment is a fact about how a buffer was laid out, not something
derivable from dimensions. Convert now takes explicit in/out alignment:
Image::Scale passes 32/32, the videostore encode path mirrors
get_out_frame's allocation choice, libvnc passes 1 (packed VNC
framebuffer) and 32 (Image WriteBuffer). Remove the unused
SetDefaults/ConvertDefaults API rather than threading alignments
through dead code.

Tests: new Scale regression case on a 720x1280 YUV420P image
(column-banded luma, uniform chroma) fails before the fix exactly as
observed live (sheared rows, V plane reading 0) and passes after.
Full suite 84/84.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-13 10:19:09 +10:00
Isaac ConnorandClaude Opus 4.7 6765962737 fix: nullptr-safe pixfmt name; route GetImage/getSnapshot through ReadShmFrame (PR #4788)
- Added zm_get_pix_fmt_name() in zm_pixformat.h: av_get_pix_fmt_name()
  returns nullptr for unknown formats (including AV_PIX_FMT_NONE), and
  passing nullptr to %s is undefined and can crash. Wraps with an
  "unknown" fallback. Replaced the five raw uses flagged by review
  (RemoteCameraRtsp/FfmpegCamera/VncCamera/LocalCamera constructors and
  MonitorStream::sendFrame's Debug log) with the wrapper. Each can hit
  AV_PIX_FMT_NONE before the first frame or via an unexpected
  (colours, subpixelorder) pair.
- Monitor::GetImage and Monitor::getSnapshot now route the slot read
  through ReadShmFrame so the per-slot AVPixelFormat zmc recorded is
  adopted on image_buffer[index] before its bytes are interpreted.
  Without this, the JPEG encode in GetImage and the ZMPacket returned
  by getSnapshot would interpret SHM bytes using the placeholder format
  set at attach time and produce garbled output when the slot's actual
  format differs (the contract previously relied on callers calling
  ReadShmFrame themselves). getSnapshot/GetTimestamp drop const since
  ReadShmFrame mutates image_buffer[index]; callers in this codebase
  already use a non-const Monitor pointer.
- Monitor::connect: updated the misleading "+64 bytes reserved" comment
  to match the actual reservation of 63 + (alignof(AVPixelFormat) - 1).

refs #4788

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-31 09:22:09 -04:00
Isaac ConnorandClaude Opus 4.7 e5446d3af5 fix: report AVPixelFormat in Panic messages for actionable diagnosis (PR #4788)
Six format-dispatch fallbacks were still printing only the legacy
`colours` value, but the dispatch in each spot is now keyed off
pixelFormat/imagePixFormat. With the GRAY8/YUV420P alias collision
(both colours=1) the legacy value frequently doesn't identify the true
format, so a "Unexpected colours: 1" log on a misroute is useless.

Updated each Panic to include the AVPixelFormat enum value and its
human-readable name (via av_get_pix_fmt_name), plus the legacy
(colours, subpixelorder) for context:
- RemoteCameraRtsp constructor
- FfmpegCamera constructor
- VncCamera constructor
- LocalCamera conversion-selection branch
- VideoStream::SetupCodec mpeg helper
- Image::Delta unknown-format fallback

refs #4788

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 23:03:33 -04:00
Isaac Connor 7b21371083 Reapply "fix: replace ZM_COLOUR system with AVPixelFormat for format dispatch"
This reverts commit 60b6b37c60.
2026-05-03 12:24:43 -04:00
Isaac Connor 60b6b37c60 Revert "fix: replace ZM_COLOUR system with AVPixelFormat for format dispatch" 2026-05-02 18:26:48 -04:00
Isaac ConnorandClaude Opus 4.6 b416aa5293 fix: replace ZM_COLOUR_*/ZM_SUBPIX_ORDER_* with AVPixelFormat for format dispatch
ZM_COLOUR_GRAY8, ZM_COLOUR_YUV420P, and ZM_COLOUR_YUVJ420P were all
defined to 1, making format identification via colours ambiguous.
LocalCamera misidentified YUV420P as GRAY8, causing V4L2 MJPEG cameras
to decode to grayscale via expensive sws_scale conversion.

Replace the legacy ZM_COLOUR_*/ZM_SUBPIX_ORDER_* integer pair with
AVPixelFormat as the single source of truth for pixel format dispatch:

- Add src/zm_pixformat.h with central format helpers:
  zm_pixformat_from_colours, zm_colours_from_pixformat,
  zm_bytes_per_pixel, zm_db_colours_to_pixformat, zm_is_rgb32,
  zm_is_rgb24, zm_is_yuv420
- Add AVPixelFormat pixelFormat member + PixelFormat() accessor to Camera
- Add PixFormat() accessor to Image, delegate AVPixFormat methods
  to shared helpers
- Migrate all ~100 format dispatch comparisons in zm_image.cpp,
  zm_local_camera.cpp, zm_ffmpeg_camera.cpp, zm_remote_camera_rtsp.cpp,
  zm_libvlc_camera.cpp, zm_libvnc_camera.cpp, zm_monitor.cpp,
  zm_mpeg.cpp from colours/subpixelorder checks to imagePixFormat/
  AVPixelFormat checks
- Deprecate GetFFMPEGPixelFormat, delegate to zm_pixformat_from_colours
- Fix DeColourise bug: imagePixFormat was not updated to GRAY8
- Deprecate ZM_COLOUR_* and ZM_SUBPIX_ORDER_* constants in zm_rgb.h
- Add deprecation notice on Monitor.Colours web UI dropdown
- Add 13 Catch2 test cases (105 assertions) for format mapping helpers

refs #4735

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 11:42:06 -04:00
Isaac ConnorandClaude Opus 4.5 bc2337d3c7 fix: fix typo, add null check, and use correct free function in VncCamera
- Fix typo: "GotFrameBufferUpdateallback" -> "GotFrameBufferUpdateCallback"
- Add null check after malloc in GetCredentialsCallback
- Use av_free() instead of free() in Close() since buffer was allocated
  with av_malloc()

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 12:09:44 -05:00
Aaron Kling c4683d90a9 Format code using astyle google format
Commands used:
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.cpp
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.h
2024-03-26 13:43:58 -05:00
Doug Nazar 110cc436a3 Switch from av_init_packet() to av_packet_alloc()
Remove all uses of deprecated av_init_packet() and switch any stack
based AVPackets to unique_ptrs allocated with av_packet_alloc().

Ensure that all code paths call av_packet_unref() after use to reset
before next usage.
2022-07-29 14:56:26 -04:00
Isaac Connor 7b9c86111c Move Cleanup and framebuffer freeing into Close() so that we don't crash on Reload 2021-11-29 16:21:34 -05:00
Isaac Connor 01834d4ddc finish converting Capture to std::shared_ptr 2021-05-09 07:35:47 -04:00
Peter Keresztes Schmidt 68bedfe48f Fix logging format string mismatches
* Remove SZFMTD format macro and use %zu instead for size_t. %zu is understood by every compiler nowadays.
2021-04-30 00:26:24 +02:00
Isaac Connor 2d6d39acee init mVncData 2021-04-20 11:48:21 -04:00
Isaac Connor aadfa1e6e1 Remove warning on fail to Prime. zmc will log it 2021-04-11 12:59:18 -04:00
Isaac Connor c7754e5be8 Default to port 5900 2021-03-30 11:37:00 -04:00
pmccarty e3349c54fa Fix logic to detect libvnc shared library. 2021-03-27 10:58:30 -04:00
Isaac Connor f4e60be444 If we failed to bind libvnc then fail Prime to prevent crash 2021-03-14 22:08:48 -04:00
Isaac Connor b87d859f72 Set the packet's stream_index to the packetqueue stream. Rename get_ functions to get 2021-03-03 12:06:34 -05:00
Isaac Connor 8aeb4ab758 Switch db_mutex to a std::mutex. Use modern locking with it. Use zmDbDo or dbQueue.push where appropriate. code cleanup. 2021-02-25 12:26:26 -05:00
Isaac Connor 3ce0564e9c Code style spacing and cleanups. 2021-02-24 10:10:37 -05:00
Isaac Connor f440ecb235 Add dlcose, and a resize() function to allocate the framebuffer our own way 2021-02-19 20:42:07 -05:00
Isaac Connor 9502762ce8 WIP cleanup. Don't need to malloc ram as rfbInit will do it. We may need to allocated a larger buffer though, so may need to give vncserver a different malloc function 2021-02-18 13:47:27 -05:00
Isaac Connor 3e10c3a8a4 Fix return codes 2021-02-17 16:42:48 -05:00
Peter Keresztes Schmidt f43507dce0 Use the power of smart pointers to manage Monitor instances 2021-02-07 21:20:45 +01:00
Peter Keresztes Schmidt 0dbc39ee25 Cleanup and reorganize includes
With this commit a unified structure for includes is introduced.
The general rules:
 * Only include what you need
 * Include wherever possible in the cpp and forward-declare in the header

 The includes are sorted in a local to global fashion. This means for the include order:
  0. If cpp file: The corresponding h file and an empty line
  1. Includes from the project sorted alphabetically
  2. System/library includes sorted alphabetically
  3. Conditional includes
2021-02-04 18:02:01 +01:00
Isaac Connor 61bffdb249 fix compiler warning 2020-12-27 11:57:23 -05:00
Isaac Connor 3893e97e94 Quieten compiler warnings 2020-12-23 18:18:30 -05:00
Isaac Connor b261fbb397 Merge branch 'master' into zma_to_thread 2020-12-07 16:26:26 -05:00
Isaac Connor d5295a4464 Fix segfaults and valgrind complaints. Initialize vncClientData, handle failure to connect freeing our client. 2020-11-17 11:08:11 -05:00
Isaac Connor 53fe4c7f1b Handle error conditions 2020-09-09 12:55:08 -04:00
Isaac Connor 7b52237593 spacing and correct a libvlc=>libvncclient 2020-09-09 10:25:55 -04:00
Peter Keresztes Schmidt 8f980a1168 Convert NULL/0 to nullptr
The 0 -> nullptr changes should definitely improve readability.
2020-08-26 22:03:40 +02:00
Isaac Connor 9b05337e57 Priming capture logs should be debug level, especially if they ar egoing to log passwords 2020-07-13 15:03:04 -04:00
hax0kartik 640dfdd4a0 Dynamically load libvnc at runtime 2020-04-14 02:46:01 +05:30
hax0kartik 04a6d5e42e Fix a memory leak, shift rfbClientCleanup to Terminate() 2020-03-30 10:11:42 -07:00
hax0kartik 6886b2b375 Modify Convert to scale as well 2020-03-28 15:17:19 -07:00
Isaac Connor b93f5d970c sws_scale directly into the image. Allocate and de-allocate sws in Prime and Close. Seems to fix scaling. 2020-03-28 16:03:52 -04:00
Isaac Connor e894102cb7 debug 2020-03-28 11:59:05 -04:00
hax0kartik a781cc2c87 WIP scaling 2020-03-27 14:32:50 -04:00
hax0kartik d61e5e42ca Use preprocessors to ensure libvnc monitor is only built when libvnc is found 2020-03-27 14:32:50 -04:00
hax0kartik ae80b3b859 Start implementing VNC 2020-03-27 14:32:50 -04:00