The PipeWire audio, microphone and camera drivers use positional
initializers for their event tables, plain loops in place of compound
literals and SPA_FOR_EACH_ELEMENT_VAR, and declarations at block start,
so they compile as C89. Under C89_BUILD the SPA and PipeWire include
directories are read as system headers and given __inline__, since
those headers are C99 and outside our control.
The linux-c89 job installs libpipewire-0.3-dev, so configure enables
the PipeWire drivers there and every one of their sources is held to
-std=c89 in CI.
Configure the cached scaler from each decoded frame's color metadata, with decoder metadata fallback and format defaults for unlabelled RGB, gray and YUVJ. Keep direct producer-buffer conversion. Cover full-to-limited range and BT.709-to-BT.601 transitions, plus unlabelled grayscale, checking every scaled output pixel.
Park the capture worker on EAGAIN or EINTR until another frontend poll requests a frame, using an eventcount and poll epoch to avoid lost wakeups. Stop wakes parked capture before joining. End non-transient read failures after one diagnostic. Cover idle and poll-driven retries, quiet transient errors, terminal errors, parked shutdown and recovery.
Avoid flushing a missing decoder when startup fails after opening the input. Free the temporary options dictionary on every failure path. Cover stream discovery failure, invalid decoder calls during cleanup, and decoded pixels after a successful restart.
Use each decoded frame's dimensions and pixel format to refresh the cached scaler. Keep the requested output size and convert directly into the producer buffer. Cover a real H.264 stream changing from 32x16 YUV420P to 64x32 YUV444P, checking uniform pixels from both segments.
Exit the capture worker on ENODEV instead of repeatedly reading and logging a removed device. Preserve existing handling of other read errors and allow explicit stop/start recovery. Cover one failed read and successful restart with the live-driver fixture.
Install the shutdown callback before each input-open attempt and clear stale shutdown state before reopening. Treat a requested read interruption as normal worker completion.
Cover a controlled blocking read, failed-open fallback and restart, verifying cancellation occurs without the fixture timeout.
Receive all available frames after packet submission, retry retained input after decoder output makes progress, and flush delayed frames at input EOF. Keep direct conversion into producer-owned slots and stop checks between frames.
Cover buffered multi-frame output and a real H.264 B-frame source whose final frames require decoder drain. Treat already-drained EOF as normal during teardown.
Retain the selected stream index and release other packets without decoding or converting them. Cover an audio-first, video-second lavfi source, verifying video pixels and packet routing without additional copies.
Treat EOF as completion rather than retrying the finished source and logging indefinitely. Keep the last published frame available, and assert one EOF read in the finite-source fixture.
Clear the atomic shutdown flag before launching the new capture worker. Require actual converted pixels after two starts, rather than accepting the initialized black frame as restart progress.
Probe streams and open the decoder using codec parameters rather than capture-device options, so requested video_size cannot replace actual input dimensions. Advance leftover-option iteration and test a 34x18 source converted to 17x9.
Point swscale output at the producer-owned triple-buffer slot, eliminating the staging image and full-frame copy. Align and pad output allocations for direct conversion, and retain slot ownership through callback completion.
Guard against staging copies and check slow-callback stability and odd-size converted pixels in the synthetic camera fixture.
Deliver aligned native ARGB frames directly from capture storage, preserving chunk offset and pitch. Hold the capture buffer until the callback returns, then queue it once. Retain scaler conversion for other formats.
Verify pointer identity, padded-row pixels, untouched conversion output and callback-before-return ownership in the scripted driver fixture.
SPA format names describe byte order, while scaler ARGB/ABGR formats describe native 32-bit words. Advertise BGRA/RGBA on little-endian hosts and retain ARGB/ABGR on big-endian hosts. Update negotiation and pixel regressions to use the corresponding native layouts.
Release output and scaler resources when PipeWire clears the format, and suppress conversion until a new output buffer is configured. Cover stale-frame suppression, repeated clear, unrelated parameters and renegotiation.
Free the previous output allocation before replacing it during format negotiation. Exercise repeated format and size changes with leak checking enabled.
Use the negotiated pixel size for both buffer requests and scaler input strides. Cover ARGB, BGR and YUY2 negotiation, with pixel-exact ARGB and BGR conversion using the packed-stride fallback.
Request deactivation while streaming; a paused stream is already stopped. Cover streaming stop, paused stop, restart, and already-started behavior in the scripted driver fixture.
Use the SPA chunk offset and stride when converting camera frames, validate the contiguous image extent, and restore the negotiated stride after conversion. Return every dequeued buffer once.
Add pixel-exact coverage for offset padded rows and a subsequent packed frame with implicit stride.
preprocess_image() looped until pw_stream_dequeue_buffer() handed it a
buffer, which parks the main thread until the camera's next frame
lands - a full camera period on every poll a slower camera has nothing
for, and indefinitely once the stream stalls. A poll now makes one
attempt and reports no frame when the queue is empty.
A buffer without CPU-visible data was also dropped without being
queued back, so each one shrank the pool the stream has to work with.
Every dequeued buffer now goes back on every exit.
samples/camera/pipewire_poll drives the driver's poll against a
scripted stream with the pw_stream calls link-wrapped and checks each
case; it runs in the samples/cores workflow next to the ffmpeg camera
lane.
RETRO_CAMERA_BUFFER_PLANAR hands a core the capture device's own 4:2:0
planes, untouched, for the duration of the callback; the frame
callback sits past the end of retro_camera_callback and is read only
when its caps bit is set, so older cores are never read past.
The Android driver picks the cheapest delivery the core accepts: the
SurfaceTexture for a GL hardware context, the planes for a planar
core, and XRGB8888 converted once by image_yuv_blit only for a core
that takes nothing else. Planes come from Camera2 through the NDK
(resolved at runtime, the HAL buffer mapped where it lies) and fall
back to the legacy Camera's NV21 preview buffers, rotated between the
camera and the reader without a copy. No permission is declared: a
port that declares CAMERA gets the prompt, others get no camera.
The badge cache decodes into a still surface and hands the surface
over, so every image-made texture in the tree is now a gfx_surface's
and the frontend's texture pokes have no caller outside it. A badge
that lands after its slot was reset or reused is freed by the load
itself, as are the surfaces of challenge indicators cleared or torn
down and of a popup dropped from a full ring. The Android camera
deletes the GL texture it made with GL.
The iOS and tvOS paths take the same treatment as macOS, down to an iOS
12 floor and with deprecations reported: the UIKit, GameController,
MetricKit, AVFoundation and CoreLocation calls above the floor go
through the apple_runtime.h sends behind their OS checks, enum values
newer than the floor are compared by value, and the display link's
frame-rate range is set and read by selector on every platform without
an SDK gate. apple_runtime.h gains a 9-16 byte struct getter and
-init sends whose casts carry the init family's ownership.
Code that uses a framework throughout - the keypress and device
haptics, camera authorization, speech - moves into classes carrying
its availability, so it calls the framework directly, and is reached
by selector through a class looked up once. Deprecated calls with a
successor on every floor take it; the rest are sent by selector. The
iOS display link and GCDWebServer's lastModifiedDate declare their
ownership, and the iOS display link's rate read is guarded for systems
before iOS 10.3.
Everything the macOS build calls above its deployment floor goes
through the apple_runtime.h sends behind its existing OS check: the
GameController physical input profile and gesture state, GCMotion's
activation and acceleration, the haptics rumbler's own macOS 11
methods, the CocoaView display link and its frame-rate range, the
location authorisation status, the PQ colour space name, BC texture
support and the screen's maximum frame rate. Element names and
exported constants are looked up by symbol once, selectors on the poll
paths are cached, and a struct the ABI returns in memory goes through
objc_msgSend_stret everywhere but arm64. memmap resolves
pthread_jit_write_protect_np at load.
Deprecated spellings with a same-valued successor take the successor,
mapped back in cocoa_defines.h for older SDKs; deprecated calls with
no successor on the floor are sent by selector, and the companion
converts window and screen coordinates by the frame origin.
One binary runs from the deployment floor up to the current OS, so an
API newer than the floor is called behind an apple_runtime_available()
check. Written as a message expression that call trips
-Wunguarded-availability-new on every build, and fails to compile on
an SDK that does not declare it. apple_runtime.h gains macros that
send through objc_msgSend with a selector looked up by name - the call
a message expression compiles to, so nothing is slower and nothing is
left to inlining - plus lookups for a class and an exported constant
by name. The Metal EDR calls, the App Shortcuts bridge, the
orientation update, the scene geometry read on the sensor path
(selectors cached in statics) and the external camera type go through
them.
metal.m declares its HDR-only methods under METAL_HDR_AVAILABLE, so the
tvOS build no longer reports them as unimplemented, and the end-of-frame
composite call sits under the same gate. sdl2_gfx counts atlas pixels
in size_t.
gfx_display: GFX_DISPLAY_STAT_FLUSH_IDX() indexes the flush counters
without arithmetic between two enum types.
gl2 and the recorder size their readback buffers in size_t, matching
VIDEO_SCALE_AREA. The remaining -Wshorten-64-to-32 sites (rgui, xmb,
ozone, widgets, menu_cbs_ok, the ffmpeg camera and core) cast at the
point the value enters a narrower parameter or field, and xmb/ozone
keep their horizontal-list offset in size_t. Two unused shader
parameter locals and an unused netplay 'user' local are dropped.
apple_runtime_os_version() is inline, as GCDWebServer includes the
header on targets where no caller remains.
Statics rely on implicit zero initialisation instead of {0}, and local
aggregates whose first member is an array get the matching braces, so
GCC 4.x -Wmissing-braces stays quiet. Locals the compiler cannot prove
written are initialised (rh265 keep[], rflac block header, query.c
lengths), loops older VRP misreads are reshaped, the unused SDL2
geometry forward declaration is gone, and task_queue no longer names a
variable main.
rzstd probes BMI2 with __get_cpuid_max/__cpuid_count, which every
cpuid.h the branch accepts provides. Hole punching needs uClibc-ng 1.0
with its Linux-specific API on uClibc. The sinc resampler's pop_options
does not warn on GCC 4.x MIPS. generate_wayland_protos.sh strips the
event type and deprecated-since attributes for scanners whose DTD
predates them, which ignore them anyway.
A poll hands the core the newest finished frame, and the decode
thread fills the next one while the core reads, however long the
core takes. Each turn releases the packet av_read_frame() filled, and
stop() frees the conversion planes start() allocated.
ffmpeg_live_test adds a counting source whose last frame a poll must
show, and a 300 ms frame callback the thread must keep decoding
through; its ASan lane checks for leaks.
The camera poll in runloop_iterate() tested RUNLOOP_WORK_CAMERA and
then re-tested five more things every frame: settings->bools.
camera_allow, cb.caps, driver, driver->poll, data. The bit is meant to
mean "poll this every frame", so those conjuncts belong at the edge
that raises it, not on the frame path.
driver_camera_start() now raises the bit only when the start
succeeded, the driver can be polled, and the core registered caps
(data and driver are preconditions of reaching start at all). Stop
and teardown already drop it. The one condition that could change
underneath - the user flipping camera_allow off mid-session - gets
its own edge: the setting row's command trigger fires
CMD_EVENT_CAMERA_ALLOW_APPLY, which stops a running camera (and so
drops the bit). Allowing again does nothing by itself, as before: the
core has to call start() for the camera to come up.
A trigger rather than a change handler because the generated
settings glue (msg_hash, configuration.c, the string passes) only
defines the base S_BOOL variant for this row; other rows already pass
a CMD_EVENT_* through the same consumers, which discard it.
Two visible changes: a disallowed-while-running camera is now
actually stopped instead of merely no longer polled while still
capturing, and the frame path is one bit test. Frontend-overhead
harness passes.
The post-core_run product block called into four translation units
every frame to find out nothing needed doing: cheat_manager_apply_
retro_cheats() loads a cold global to see there is no cheat list,
presence_update() calls discord_update() and steam_update_presence()
which each compare a status word and return, the camera poll reads
settings_t for camera_allow before the caps check, and
bsv_movie_next_frame() pulled two settings through config_get_ptr()
before checking whether a movie handle exists at all.
Give each an up/down owner and a bit on runloop_state_t::frame_work,
raised and dropped by the product itself through
runloop_frame_work_set():
RUNLOOP_WORK_CHEATS - cheat_manager_new/realloc raise it when a
list is allocated, cheat_manager_free drops it
RUNLOOP_WORK_PRESENCE - raised with Discord init and with Steam
(mist) init; dropped with whichever of the
two is the last compiled-in sink
RUNLOOP_WORK_CAMERA - driver_camera_start raises it when the core's
start succeeds, driver_camera_stop and the
driver teardown drop it. The remaining
camera_allow/caps/driver checks stay behind
the bit; a camera the core registered but
never started is no longer polled.
BSV needs no bit: the movie handles live on input_st, which the frame
already has in cache, so the iterate tests the pointers directly and
bsv_movie_next_frame() checks its handle before touching settings.
FreeBSD's PipeWire 1.6.9 headers declare pipewire_init(int *, char ***),
so the camera driver's static pipewire_init() fails to compile there:
"static declaration of 'pipewire_init' follows non-static declaration".
This is what the games/retroarch port's patch-camera_drivers_pipewire.c
worked around.
Rename the driver's five static entry points to pwire_camera_* so they
cannot collide with anything libpipewire exports, matching the pwire_
prefix the audio driver already uses. No behaviour change.
camera_driver_t init takes the requested frame size as one word, which
the frontend packs from the setting or the core's request. The FFmpeg
driver keeps its requested and target sizes that way, V4L2 the size
its device negotiated, and the PipeWire format builder takes the
size packed. The FFmpeg live test checks that frames arrive at the
size asked for.
@available has two problems for RetroArch's range of targets:
1. It needs a clang new enough to know the syntax, so the ancient
toolchains building the legacy iOS/macOS targets cannot compile
any file that uses it.
2. It lowers to a call to __isPlatformVersionAtLeast() every time
it is evaluated, including in per-frame and per-input paths.
Introduce apple_runtime.h with apple_runtime_available(mac, ios,
tvos), a drop-in replacement that resolves the running OS version
exactly once per process and caches it in a static int; after
warm-up every check is a single load and integer compare, cheaper
than @available. Platforms the original check did not list are
passed as 0, preserving @available's semantics of returning true
on unlisted platforms.
The version is read from UIDevice systemVersion on iOS/tvOS (never
shimmed, works on any version), and on macOS from the
kern.osproductversion sysctl - which reports the real OS version
even for binaries linked against pre-11.0 SDKs, where the
NSProcessInfo APIs (and @available's own plist source) are subject
to the 10.16 compatibility shim - with an
operatingSystemVersionString fallback for pre-10.13.4 systems.
This also fixes the broken iOS 9 / iOS 6 CI builds: cocoa_common.m
used UIWindowScene.effectiveGeometry, an iOS 16 API that older SDKs
(e.g. iPhoneOS 14.2) do not declare at all, so @available could not
save compilation. That call chain is now resolved entirely at
runtime via objc_msgSend (identical cost to a compiled property
access), with the capability probe cached in a static.
All 84 live @available sites across 13 files are converted.
Behavior on every OS version is unchanged: the same paths are taken
at the same version thresholds. Swift #available sites are
untouched; Swift enforces availability at compile time and those
files only build on modern toolchains.
Existing compile-time SDK gates (e.g. around NSWorkspace
isVoiceOverEnabled and the Metal HDR blocks) are kept, since they
guard symbol declarations, not runtime dispatch; comments updated
accordingly.
-setupCameraSession stores a freshly allocated AVCaptureSession and
AVCaptureVideoDataOutput into strong properties, whose setters retain
what they are handed, so each object stood at two references and the
one dropped when the next init assigned over it left the previous
session alive. The manager is a singleton that outlives a driver
instance, and the session holds the capture pipeline, so a core started
with a camera a second time left the first one running. Releasing the
allocation's own reference once the property holds it lets the old
session go where it is replaced.
apple_mrc_ownership_check.py grows a second pass for the shape:
a retaining property handed an expression that is already +1, with
nothing releasing it nearby. It reads the same way as the first pass
and is a report rather than a gate. The line is correct under ARC,
which is what lets it through review of a file built manual-retain-
release.
The camera driver had no test, because testing it meant owning a
webcam. It does not: the driver reads from a libavdevice input
format, and built with that format set to libavfilter a filter graph
is the device. The whole pipeline then runs with nothing attached -
avformat opens the source, the poll thread reads packets, the
decoder decodes them, swscale converts into the target buffer and
poll() takes the frame out under the buffer's lock. Two seams make
that possible, and both are worth having on their own account:
- FFMPEG_CAMERA_DEFAULT_BACKEND respects a definition the build has
already made, rather than choosing by platform regardless.
- The device the frontend names is used. It was taken as an argument
and never read: a user who picked a camera got whichever one
enumerated first. A named device is now the source url and
enumeration is skipped.
ffmpeg_live_test drives what the threading has to get right and what
no eyeball on a webcam would show: open, run and close; stop and
start three times over; stop while the poll thread is mid-frame,
five times with the stop at a different point each time - which is
the ordering fixed a moment ago, where the flush used to run against
a live decoding thread; free without stopping first, as a host
dropping content does; poll before starting; and a source that does
not exist. Every frame is read pixel by pixel, so a buffer that was
freed or is the wrong size shows under ASan rather than as a
wrong-looking picture.
And one in libretro-common, which UBSan found on the first run:
memalign_alloc keeps the pointer to free in the word before the
address it returns, so that address must be aligned for a pointer
whatever the caller asked for. The camera asks for 4, which on a
64-bit host put the header on a four-byte boundary - a misaligned
store of a pointer, undefined everywhere and a fault on a target
that does not allow it. A boundary is a minimum, so it is raised to
a pointer's.
Plain, ASan/UBSan and TSan all clean; in the cores samples CI.
Auditing the two other ffmpeg users the way the core was audited.
camera/drivers/ffmpeg.c had two things:
- ffmpeg_camera_stop() flushed the decoder and then joined the poll
thread. The poll thread is the decoder's other user - it sends
every packet it reads and receives every frame - and an
AVCodecContext takes one user at a time. So the flush ran against a
live decoding thread. The join comes first now, and the flush
after, where the context has one user again.
- 'done', the poll thread's loop condition, was a volatile bool: set
by the main thread in stop(), read by the thread every turn, with
nothing held on either side - target_buffer_lock covers the frame,
not this. volatile is not a synchronisation primitive; it stops the
compiler caching the load and orders nothing, so the thread had no
guarantee of seeing what the main thread wrote before it. An
atomic, as the ffmpeg core's own flags became for the same reason.
record/drivers/record_ffmpeg.c came out clean. Its queues are
already lock-free retro_spsc rings and 'alive' is already an atomic.
'can_sleep' carried a volatile and does not need one: every access is
under cond_lock, or - the one in init_thread() - before the thread
that reads it exists. The volatile said the opposite, that here was a
flag crossing threads without a lock, so it is gone and the comment
says what actually protects it. No behaviour change there.
The record pipeline harness passes, plain and under TSan.
MSYS2 updated mingw-w64-ffmpeg to 9.0, which removes the deprecated
AVCodec.sample_fmts / AVCodec.supported_samplerates arrays and breaks
the MSYS2 CI build in record_ffmpeg.c.
record_ffmpeg: add accessors that use avcodec_get_supported_config()
where available (lavc >= 61.13.100, FFmpeg 7.1+) and fall back to the
legacy AVCodec struct members on older versions. NULL now means
unrestricted instead of being dereferenced, hardening both paths.
camera/ffmpeg: version-gate const-ness of the AVInputFormat/AVCodec
pointers; lavf 59 (FFmpeg 5.0) made that API const-correct, lavf 58
(FFmpeg 4.x, the driver's minimum) warns on const pointers.
Verified: compile matrix with production flags against FFmpeg 3.4.13,
4.4.5, 5.1.6, 6.1.2, 7.1.1, 8.1.2 and 9.0 all clean; full configure +
make against FFmpeg 9.0 builds and links; resolver logic runtime-tested
under ASan/UBSan against FFmpeg 9.0 (new path) and 6.1.2 (legacy path)
with identical results and no sanitizer findings.
* Emscripten libretro-common fixes
* memmap: gate the reserve/commit path off for emscripten instead of dropping mman
Emscripten was added to the "No mman available" platform list in
memmap.h, which reaches further than intended. That branch does not
just disable things: it makes memmap.c define mmap(), munmap() and
mprotect() as global symbols with malloc/free semantics on a platform
whose libc already provides working ones, and it declares mmap() with
a size_t offset where <sys/mman.h> uses off_t, so any translation unit
that pulls in both headers gets conflicting declarations. The shim's
mmap() also ignores fildes and offset, so a file-backed request would
return uninitialised memory; that is invisible today only because
Makefile.emscripten never sets HAVE_MMAP and the mapping path in
vfs_implementation.c is therefore compiled out.
What is actually unusable on emscripten is the reserve/commit model,
and that is unusable by construction rather than by omission: a wasm
linear memory grows monotonically, is committed in full, and has no
unmap, no shrink and no page protection. An anonymous PROT_NONE
mapping is a memalign of the whole length followed by a memset over
it, so memreserve() commits and touches every byte the reservation
existed to avoid, mprotect() and madvise() cannot make commit or
decommit mean anything, and munmap() only accepts an exact length
match, so the range cannot be released. Opening large content through
data_transfer would allocate the whole file up front and never give it
back.
Leave memmap.h alone and gate MEMMAP_HAVE_RESERVE instead, so
mempagesize() reports 0, memreserve() returns NULL and callers take
the bounded-allocation fallback they already have for platforms
without virtual memory. memsync() is excluded from the msync() branch
for the same reason: emscripten's msync() only accepts the exact base
of a live mapping and returns -EINVAL for an arbitrary range, while a
wasm linear memory has no separate instruction cache to flush, so the
fallthrough's 0 is both correct and the only honest answer.
mmap(), munmap(), mprotect() and memprotect() keep working through
libc as before.
* memmap: round memrelease() to the page size memreserve() rounded to
memreserve() rounds the requested length up to a whole number of pages
and maps that, but memrelease() passed the caller's unrounded length
straight to munmap(). Linux rounds the length up internally so the
mismatch is invisible there, which is how it survived; an
implementation that takes the length literally and declines a partial
unmap fails the call outright and leaks the entire reservation
whenever the length was not already page-aligned.
Apply the same rounding on the way out. mempagesize() is non-zero
wherever this branch compiles - MEMMAP_HAVE_RESERVE is only defined
when _SC_PAGESIZE is available - but the guard costs nothing and keeps
the expression from dividing the range by zero if that ever changes.
* Revert "memmap: gate the reserve/commit path off for emscripten instead of dropping mman"
This reverts commit 11b85a9421.
* Fix remaining uses of EMSCRIPTEN define
---------
Co-authored-by: libretroadmin <[email protected]>
AVCaptureDevice authorizationStatusForMediaType:, requestAccessForMediaType:
completionHandler: and the AVAuthorizationStatus enum are macOS 10.14+ (iOS
7+). Building RetroArch_Metal.xcodeproj against the macOS 10.13 deployment
target emitted 'only available on macOS 10.14 or newer' for each.
Wrap both call sites in if (@available(macOS 10.14, *)), matching the
@available idiom already used in this file. Pre-Mojave macOS had no camera
TCC prompt, so the legacy branch treats access as implicitly granted and
proceeds, preserving behaviour on older systems.
The iOS/tvOS device-discovery path used two static AVCaptureDeviceType
array literals and failed to build:
avfoundation.m: Use of undeclared identifier 'AVCaptureDeviceTypeExternal'
avfoundation.m: 'AVCaptureDeviceTypeBuiltInUltraWideCamera' is only
available on iOS 13.0 or newer
These are two different problems:
- AVCaptureDeviceTypeExternal exists only in the iOS 17 / Mac Catalyst 17
SDK and is unavailable on tvOS. "Undeclared identifier" means the symbol
is absent from the SDK being compiled against, so a runtime @available
check cannot rescue it; the reference itself must be removed at compile
time on older SDKs. Guard it with #if __IPHONE_OS_VERSION_MAX_ALLOWED >=
170000 (matching the existing idiom in ui/drivers/cocoa) plus a runtime
@available(iOS 17.0, *) for the deployment target.
- AVCaptureDeviceTypeBuiltInUltraWideCamera was introduced in iOS 13. The
symbol is present in the SDK, but it was referenced unconditionally
while the deployment target can be lower, hence the availability error.
Guard it with @available(iOS 13.0, *).
Build the type list into an NSMutableArray so each constant can carry its
own compile-time and/or runtime guard instead of living in an
all-or-nothing literal. Built-in wide-angle and telephoto remain the
unconditional baseline; external is listed first to prefer an attached
external camera when present. No behavioural change on iOS 17+; older
SDKs and lower deployment targets now compile. The mutable array uses
only the autoreleased convenience constructor, so it stays valid under
both ARC and MRC.
Audit of the frameBuffer lifecycle turned up three small issues in
camera/drivers/avfoundation.m that all cluster around the same ivar:
1. avfoundation_init's setupCameraSession-failed error path does
free(avf->manager.frameBuffer) but does not null the field.
The manager is a singleton, so the next avfoundation_init
inherits a dangling pointer until its own calloc overwrites
the slot. Everything in this driver runs on the main thread
(capture delegate queue is dispatch_get_main_queue(), poll
runs from rarch_draw_observer on the main run loop, init/free
are on the main thread) so no concurrent access can observe
the dangling value - but it is still defensive-programming-
wrong to leave a singleton ivar pointing at freed memory.
2. avfoundation_poll's color-bars fallback calloc'd and freed a
throwaway buffer on every poll while the camera session was
warming up or stopped. For a typical 1280x720 camera that is
~3.5 MB of allocator churn per poll, fired at runloop rate
until the session reports isRunning. Since we already have
a cached per-driver frameBuffer on the manager (allocated in
init, size matches avf->width x avf->height exactly), paint
the color-bars pattern directly into it and hand that to
frame_raw_cb. The captureOutput callback will overwrite the
same buffer with real camera frames once the session starts.
3. The 'Camera not running, generating color bars...' log at the
top of that fallback path fires every poll during the warmup
window - the same per-frame log-spam pattern that commit
141d2a6 already fixed for two other logs in the file. Gate
this one behind #ifdef DEBUG to match.
Threading model (unchanged): the capture delegate is invoked on the
main queue (set via setSampleBufferDelegate:queue: to
dispatch_get_main_queue()) and the consumer of frameBuffer reads on
the main thread as well (runloop_iterate runs from a
CFRunLoopObserver attached to CFRunLoopGetMain() on macOS, from
CADisplayLink on iOS/tvOS - both main-thread). Writer and reader
are fully serialized by the main run loop, so reuse of frameBuffer
across both paths needs no additional synchronization.
captureOutput:didOutputSampleBuffer:fromConnection: allocated and freed
four full-frame scratch buffers on every callback: an intermediate
colour-converted image, a rotated image, an optional mirrored image,
and a scaled image. For a 720p BGRA camera at 30 fps that was roughly
3.5 MB × 4 allocations × 30 Hz of allocator churn on the main thread
(the delegate queue is dispatch_get_main_queue() - see
setSampleBufferDelegate:queue: in setupCameraSession).
Cache the buffers on the AVCameraManager singleton via private ivars
in a class extension. Grow on demand (realloc only when the required
size exceeds the current capacity), otherwise reuse. Camera source
resolution is effectively constant for the life of a session, so
after the first frame no allocator work happens. Scratch buffers are
freed in avfoundation_free via a new teardownScratchBuffers method,
so a driver teardown/re-init with a different target resolution does
not leak stale capacity.
The original code swapped rotatedBuffer.data = mirroredBuffer on
successful mirror; with stable per-frame buffer identities that trick
no longer fits. Replace it with a local 'scaleSource' pointer that
selects between the rotated and mirrored buffer, preserving the same
fallback semantics (mirror failure scales from the rotated buffer).
Also silence two per-frame RARCH_LOG statements (the aspect-fill
scaling dimensions and the 270-degree rotation notice) by putting
them behind #ifdef DEBUG. They duplicate information already logged
once at init and produced one log line per camera frame in release
builds.
ARC / MRC compatibility: the file is compiled under MRC in the top-
level Makefile and the RetroArch*.xcodeproj / RetroArch_OSX107 / iOS13
projects, and under ARC in the iOS6-11 projects and the iOS Theos
build. The new code uses only plain-C pointers for the scratch
buffers (invisible to ARC's memory management), no explicit
retain/release/autorelease calls, and a method name
(teardownScratchBuffers) that avoids the release/init/copy/new/
mutableCopy selector families that ARC treats specially. No
-dealloc override is added - it would require [super dealloc] under
MRC (banned under ARC), and the singleton is never dealloc'd in
practice; teardownScratchBuffers from avfoundation_free is the real
cleanup path.
Thread-safety: unchanged. The capture delegate is invoked on the
main queue and the consumer of frameBuffer reads on the main thread
as well; no new synchronization introduced. The scratch-buffer
realloc happens in the same callback that uses them.