Skip to content

main, gain: put the low-light frame rate setting back after streaming - #18

Merged
RyanTheTide merged 1 commit into
mainfrom
fix/restore-exposure-priority
Nov 21, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
fix/restore-exposure-priority

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Problem. With low-light-frame-rate = on (the default) the filter turns on V4L2_CID_EXPOSURE_AUTO_PRIORITY before the camera streams, and never turns it off again. V4L2 controls outlive the open file, so after any PipeWire camera use, a program that opens the camera directly (Firefox's V4L2 backend, GStreamer v4l2src) gets dynamic frame rate. In low light it runs at 15 fps, though the driver default is a fixed frame rate.

Change. Remember the value the control had before the filter changed it, and write it back when the source node reports idle or suspended. At that point the camera has stopped and the control can be written again. While another client keeps the camera busy, the write is retried at the next idle. A user setting that already matches the filter's choice is left alone. The low-light behaviour of the filtered PipeWire stream itself is unchanged.

Testing. 15-inch M3 MacBook Air (J615), production kernel 7.3.0-aurora-fleet78, PipeWire 1.6.9, dark room. Each round: set the control, stream 90 buffers through the default (filtered) PipeWire source, read the control back, then open /dev/video0 directly at 1080p30.

control after the PipeWire stream next direct open
packaged 0.1.0-9, control 0 before 1, 3/3 (still 1 after 10 s) 15.91 fps, 3/3
this branch, control 0 before 0, 3/3 (back within 10 ms of the stream ending) 30.00 fps, 3/3
this branch, control 1 before (user's own) 1, kept 15.91 fps (as asked)

No "not restored" messages in the daemon log. meson test: 10/10 on x86. The aarch64 build is the daemon only (-Dgpu=disabled), in a Fedora 44 container.

Still to run. The same row on the M4 Air (J713) and a ref Mac. The fix only uses the generic V4L2 control and PipeWire node state, so it does not depend on the machine.

Details for review
  • The restore runs in src_info() on a PW_NODE_CHANGE_MASK_STATE change to IDLE or SUSPENDED, and only when the filter's own input stream is gone (!c->in). The ISP driver grabs the control while streaming, so a write before stream-off would get -EBUSY. Waiting for the node state means no timer or delay is needed.
  • -EBUSY keeps the saved value for the next idle. Any other error drops it and logs once.
  • If the input restarts before the camera has stopped, the first saved value is kept (no G_CTRL on the restart), so the filter never saves its own setting as the user's.
  • Found while proving media: apple: isp: M3 Air camera on 27.2 firmware; T8122 and T8132 gain and exposure linux#2670 on the J615 default kernel: the gst/PipeWire rows run after a PipeWire stream showed about 15 fps in the dark.

Assisted-by: LLM (Claude:Opus 5.5)

With low-light-frame-rate = on the filter turns on
V4L2_CID_EXPOSURE_AUTO_PRIORITY before the camera streams. V4L2
controls outlive the open file, so the camera kept it after the stream
ended: a program that then opened the camera directly (Firefox's V4L2
backend, GStreamer v4l2src) ran at 15 fps in low light although the
driver default is a fixed frame rate.

Remember the value the control had before the filter changed it, and
write it back once the source node reports idle or suspended, when the
camera has stopped and the control can be written again. A value the
user set and that matches the filter's choice is left alone.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

No blockers.

I checked the save in cam_start_input() (src/main.c:655-667), the restore in src_info() (src/main.c:928-938) and the G_CTRL/S_CTRL order in src/gain.c:78-83. The restore waits until the filter's input is gone and the source node is idle or suspended. A restart does not save the filter's own value. A user value equal to the filter's is not touched. On -EBUSY the restore is retried at the next idle. These all hold.

  1. src/main.c:1002-1006: cam_destroy() drops prio_saved without writing it back. If the daemon quits (SIGTERM via pw_main_loop_quit, src/main.c:1155) or the source node goes away while the filter is streaming, the camera keeps the filter's value. The next direct open then runs at 15 fps in low light, the case this PR fixes. After cam_stop_input() in cam_destroy(), try one restore with cdn_v4l2_set_exposure_priority(c->v4l2_path, c->prio_prev, NULL) when prio_saved is set, and log it if it fails. If the camera has not stopped yet it returns -EBUSY and nothing is lost. Or say in the README that a value set during a stream stays if the service is stopped mid-stream.

HW row: J713 (M4 Air) and one ref Mac on the production kernel, the same three-row table (control 0 before: 0 after and 30.00 fps on the next direct open; control 1 before: 1 kept). Add one row that stops the service in the middle of a PipeWire stream and then opens the camera directly. Regression: 0 AVC/0 failed units/0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial review at head b953de04ee340ab33b464ce96daf1c652090760e: Approve (0 blockers).

  • Code & safety (src/gain.c:63-86, src/main.c:95-96,655-667,928-938): cdn_v4l2_set_exposure_priority() queries capability, reads the previous value via VIDIOC_G_CTRL, writes the new value via VIDIOC_S_CTRL, and only records prio_prev on success when the value actually changed. Restoring in src_info() on PW_NODE_CHANGE_MASK_STATE to IDLE/SUSPENDED when !c->in avoids timers/sleeps, handles restarts without clobbering prio_prev, and retries on -EBUSY. Compiled src/gain.c and src/main.c locally with -Wall -Wextra -Werror with zero warnings.
  • Checklist compliance: Uses standard V4L2 control and PipeWire node state with no model/chip gating, no hacks/sleeps, clean human Signed-off-by only on commit b953de04, and Assisted-by on PR body.
  • Evidence: Complete 3x3 J615 hardware matrix on production kernel 7.3.0-aurora-fleet78 (control 0 restored within 10 ms 3/3 -> 30.00 fps; user control 1 preserved -> 15.91 fps). Cross-board confirmation on J713 / ref Mac remains noted.
  • Non-blocking improvements (for follow-up or polish):
    • src/main.c:1002-1015 (cam_destroy()): as previously noted, if the daemon stops (SIGTERM) or the source node disappears mid-stream, prio_saved is dropped without a best-effort restore attempt (or documenting that limitation in README.md).
    • src/main.c:655-672 (cam_start_input()): if pw_stream_connect() fails (or the client disconnects before the source node leaves IDLE/SUSPENDED), no subsequent PW_NODE_CHANGE_MASK_STATE transition to IDLE/SUSPENDED will arrive to trigger restore until a later PipeWire stream runs.

Assisted-by: LLM (Google:Gemini 4 Neon)

@RyanTheTide
RyanTheTide merged commit 6ae2f1d into main Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants