1 Commits
Author SHA1 Message Date
Chris Dixonandlibretroadmin d4908f40fb USB: EyeToy camera and microphone, through the frontend or V4L2/ALSA on Linux (#179)
* USB/EyeToy: the EyeToy and Capture Eye device models

PCSX2's Sony EyeToy (OV519) and Konami Capture Eye (OV511+) device
models, on the libretro-usb OHCI core the HID devices already use.
Nothing attaches them yet.

Pictures come from RETRO_ENVIRONMENT_GET_CAMERA_INTERFACE when the
frontend offers it, and otherwise, on Linux, from V4L2: the first node
that streams video capture, in YUYV. Sound comes from
GET_MICROPHONE_INTERFACE, or on Linux from ALSA: the capture card on the
camera's own USB device, else any USB one. libasound is opened with
dlopen, so the core gains no library dependency. Camera frames and
microphone samples are taken on the main thread, as the libretro API
requires, and handed to the emulator thread under a lock.

Against PCSX2:
- The microphone is read straight from the EyeToy's audio endpoint, so
  the separate USB microphone device it embedded is not needed.
- Frames become RGB once, at the size the game asked for whatever the
  camera delivers, mirrored there; MPEG, JPEG and the Capture Eye's
  greyscale are all encoded from that.
- JPEG mode has its own baseline encoder (4:2:2, as libjpeg was asked
  for) instead of libjpeg.
- jo_mpeg: Cb and Cr were B-Y and R-Y without the 1.772 and 1.402
  scale, so saturated colours overflowed the DC size table and the
  stream would not decode; YUYV chroma landed in the wrong columns; AC
  levels past +-127 kept an MPEG-1 prefix inside the MPEG-2 escape.
- The frame size the game writes is clamped to the 640x480 the buffers
  hold.

* libretro: the USB Camera option plugs an EyeToy in

System > USB Camera puts a Sony EyeToy or a Konami Capture Eye on the
first USB port that no keyboard or mouse holds. The frontend's camera
and microphone are asked for in retro_load_game, only when the option
is on, and pumped from retro_run; the per-frame HID update leaves the
camera's port alone.

* USB: savestates hold the controller and its devices

USBfreeze was a stub, so loading a state left the guest's USB driver
talking to a controller and devices that had forgotten their addresses,
configuration and registers; a keyboard, mouse or EyeToy in use stopped
answering.

The state gains a fixed-size USB block just before the extension block,
with a footer of its own: the OHCI registers, then per port what every
USB device has (address, configuration, endpoints) and what the device
adds, the HID event queues or the EyeToy's and its sensor's registers.
The EyeToy's frame in flight is not kept; it starts a new one. A state
without the block, or one whose ports held other devices, plugs those
devices in afresh, as PCSX2 does.

* USB/EyeToy: lock-free frame and sample handoffs, stack frames in budget, state slots validated, harnesses

The camera frame and the microphone samples crossed from the thread that
captures (the frontend's camera callback, or the V4L2 thread) to the one
that runs the device under a mutex, with GetImage copying the frame while
holding it. They now cross through two primitives in cam-frame.h built for
exactly that, on libretro-common atomics and nothing else. FrameMailbox is
three buffers and one exchanged slot: the producer encodes into its own,
publishes by swapping it into the slot, and the consumer swaps its own in
to take the newest; a frame published while the consumer is away is simply
replaced. SampleRing is a single-producer single-consumer ring; a full ring
drops at the producer, and the consumer trims its own backlog by advancing
its own index. What the device asked of the camera - wanted, format,
mirror, size, generation - travels in one atomic word, so an Open from the
emulator thread and a frame from the main thread never meet in a lock.

The stackframe lane was over budget on four new frames: the JPEG encoder
built its four Huffman tables on the stack per call, jo_write_mpeg kept its
macroblock scratch there, and the ALSA device search had two PATH_MAX
buffers. The tables are built once as statics, the scratch is a
FrameEncoder the caller owns, and realpath allocates.

A savestate slot naming a configuration or interface the device does not
have was half loaded and the device left running on it; the slot is now an
error, and a port whose slot errs, or holds another device, is cycled with
usb_port_reset, so the device model is at its defaults as a guest's port
reset would leave it.

tests/cam runs the mailbox and the ring from two threads, plain and under
TSan, and parses what the JPEG, IPU and YUV400 encoders emit; tests/usbstate
drives the real controller and device models through write, strip and read,
and the states that must replug: no block, another device on the port, a
bad configuration, a bad interface. Both run from run-all.sh.

---------

Co-authored-by: libretroadmin <[email protected]>
2026-10-17 20:17:25 +02:00