Skip to content

Fix black preview when the phone refuses USB transfers (0.1.1) - #23

Merged
JKook-Plus merged 1 commit into
mainfrom
fix/usb-transfers-enomem
Nov 22, 2026
Merged

JKook-Plus merged 1 commit into
mainfrom
fix/usb-transfers-enomem

Conversation

@JKook-Plus

Copy link
Copy Markdown
Owner

Problem

On a Pixel 7 Pro (Android 17) with the 349c:8319 endoscope, the preview stayed black while the app said "Now streaming MJPG 640x480 25 fps".

Logging added to libuvc showed the cause. libuvc queues 100 isochronous transfers of 96 KB each, and the kernel needs one physically contiguous buffer per transfer. With the phone short of memory, USBDEVFS_SUBMITURB failed with errno 12 (ENOMEM) from the 2nd to 5th transfer onward. libuvc freed every refused transfer. If even the first submit failed, it still returned success. The stream lost its transfers one by one and went quiet with no error.

Changes

  • patches/libuvc-0.0.8-transfers.patch (new):
    • Each isochronous transfer is now 32 KB at most (10 x 3000 bytes for this camera instead of 32 x 3000).
    • A transfer that is refused, or that fails as a whole, is parked instead of freed. uvc_retry_parked_transfers() queues parked transfers again, at most every 20 ms.
    • uvc_stream_stop frees parked transfers so it cannot wait forever.
    • uvc_submit_hook lets tests make submissions fail.
  • usbcam.cpp:
    • The event thread retries parked transfers. A videoLock keeps that away from stream start and stop; the event thread only uses try_lock, so stopping cannot deadlock.
    • The video interface is set back to alt 0 before negotiating.
    • If the camera still answers the start with an I/O error, the USB port is reset once and the button status transfer is queued again. This was seen after the app was killed mid-stream: the camera failed every commit request until it was replugged.
    • Debug builds only: setprop debug.hardline.usbfail N fails N% of submissions.
  • CameraController: if no frame arrives for 5 s, the stream is restarted. After 3 restarts in a minute the app shows an error instead of a silent black screen.
  • Version 0.1.1 (versionCode 2), changelog, BUILDING.md and TESTING.md.

Testing

Emulator (virtual UVC camera, debug build):

  • Normal streaming, isochronous and bulk.
  • 30% of submissions failing: video keeps moving.
  • 100% failing: 3 restarts, then the error dialog. Video comes back by itself once the failures stop.
  • Safely eject while transfers are parked: returns at once.
  • Reopen afterwards streams normally.

Pixel 7 Pro with the real camera (release build):

  • All 100 transfers of 30 KB were accepted, where 96 KB ones had been refused.
  • Streamed for 2 minutes with no refusals or stalls.
  • Installing over a running stream twice: both restarted cleanly.
  • Reset path, forced with a throwaway build: the reset succeeds, the stream restarts and the button transfer is queued again.

Unit tests and lint pass.

🤖 Generated with Claude Code

On a Pixel 7 Pro short of memory the kernel refused libuvc's 96 KB
isochronous transfers with ENOMEM. libuvc freed each refused transfer,
and even reported success when the first one failed, so the stream lost
its transfers and went black while the app still said "streaming".

- New libuvc patch: transfers of 32 KB at most, refused or failed
  transfers are parked and queued again from the event thread, parked
  transfers are freed on stop, and a submit hook for tests.
- usbcam.cpp: retries parked transfers, resets the video interface to
  alt 0 before negotiating, and resets the USB port once if the camera
  still refuses to start with an I/O error (seen after the app was
  killed mid-stream). Debug builds read debug.hardline.usbfail to fail
  a share of submissions on purpose.
- CameraController: restarts a stream that delivers no frames for 5 s,
  and shows an error after 3 restarts in a minute.
- Version 0.1.1 (versionCode 2), changelog and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JKook-Plus
JKook-Plus merged commit 6fe7c1b into main Nov 22, 2026
1 check passed
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.

1 participant