Skip to content

Commit 7eebad9

Browse files
committed
fix: recycle writable Linux capture frames on final release
1 parent 95171f1 commit 7eebad9

4 files changed

Lines changed: 436 additions & 33 deletions

File tree

‎crates/recording/src/cursor.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ mod incremental_writer_tests {
21012101
// for that rename before the last flush, so the last write always
21022102
// replaces it with a regular file.
21032103
let deadline = Instant::now() + Duration::from_secs(30);
2104-
while !(outputs.cursor.as_ref().unwrap().exists() && !fifo.exists()) {
2104+
while !outputs.cursor.as_ref().unwrap().exists() || fifo.exists() {
21052105
assert!(
21062106
Instant::now() < deadline,
21072107
"the stalled write never completed"

‎crates/recording/src/instant_recording/linux_camera.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,65 @@ mod tests {
29892989
);
29902990
}
29912991

2992+
#[test]
2993+
#[ignore = "requires a running X11 desktop with a 1920x1080 capture region"]
2994+
fn real_x11_pooled_frames_compose_camera_in_place() {
2995+
use crate::screen_capture::{X11Grabber, X11InputConfig};
2996+
2997+
ffmpeg::init().unwrap();
2998+
let timestamp = Timestamp::Instant(Instant::now());
2999+
let camera = FFmpegVideoFrame {
3000+
inner: pattern_frame(Pixel::NV12, 640, 480, 5),
3001+
timestamp,
3002+
};
3003+
let config = X11InputConfig {
3004+
display_name: std::env::var("DISPLAY").unwrap(),
3005+
window_id: None,
3006+
x: 0,
3007+
y: 0,
3008+
width: 1920,
3009+
height: 1080,
3010+
fps: 30,
3011+
show_cursor: false,
3012+
};
3013+
for shm in [false, true] {
3014+
let mut grabber = X11Grabber::with_shm(&config, shm).unwrap();
3015+
assert_eq!(grabber.uses_shm(), shm);
3016+
let info = VideoInfo::from_raw_ffmpeg(Pixel::BGRZ, 1920, 1080, 30);
3017+
let mut compositor = Compositor::new(
3018+
info,
3019+
LinuxCameraPresentation::default_for(info.width, info.height),
3020+
);
3021+
let mut pointers = std::collections::BTreeSet::new();
3022+
let mut previous = None;
3023+
for index in 0..12 {
3024+
let mut screen = grabber.grab().unwrap();
3025+
screen.set_pts(Some(index));
3026+
let pixels = screen.data(0).as_ptr();
3027+
let buffer = unsafe { (*screen.as_ptr()).buf[0] };
3028+
pointers.insert(pixels);
3029+
assert_eq!(plane_buffer_refcount(&screen), 1);
3030+
let output = compositor
3031+
.compose(
3032+
FFmpegVideoFrame {
3033+
inner: screen,
3034+
timestamp,
3035+
},
3036+
&camera,
3037+
0,
3038+
)
3039+
.unwrap();
3040+
assert_eq!(output.inner.data(0).as_ptr(), pixels);
3041+
assert_eq!(unsafe { (*output.inner.as_ptr()).buf[0] }, buffer);
3042+
assert_eq!(output.inner.pts(), Some(index));
3043+
previous = Some(output);
3044+
std::hint::black_box(&previous);
3045+
}
3046+
assert_eq!(pointers.len(), 2);
3047+
drop(previous);
3048+
}
3049+
}
3050+
29923051
#[test]
29933052
fn compositor_converts_nv12_camera_without_mutating_shared_screen_and_preserves_pts() {
29943053
ffmpeg::init().expect("FFmpeg initializes");

‎crates/recording/src/sources/screen_capture/linux.rs‎

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ mod x11_tests;
4444
#[cfg(test)]
4545
mod system_audio_channel_tests;
4646

47+
#[cfg(test)]
48+
#[path = "linux_frame_pool_tests.rs"]
49+
mod frame_pool_tests;
50+
4751
use frame_scaler::FrameScaler;
4852

4953
#[derive(Debug)]
@@ -954,14 +958,29 @@ const CAPTURE_FRAME_POOL_CAPACITY: usize = 4;
954958

955959
struct CaptureFramePool {
956960
key: Option<(ffmpeg::format::Pixel, u32, u32)>,
957-
frames: Vec<ffmpeg::frame::Video>,
961+
frames: Arc<parking_lot::Mutex<Vec<ffmpeg::frame::Video>>>,
962+
}
963+
964+
struct CaptureFrameLease {
965+
frame: ffmpeg::frame::Video,
966+
frames: std::sync::Weak<parking_lot::Mutex<Vec<ffmpeg::frame::Video>>>,
967+
}
968+
969+
unsafe extern "C" fn recycle_capture_frame(opaque: *mut libc::c_void, _data: *mut u8) {
970+
let lease = unsafe { Box::from_raw(opaque.cast::<CaptureFrameLease>()) };
971+
if let Some(frames) = lease.frames.upgrade() {
972+
let mut frames = frames.lock();
973+
if frames.len() < CAPTURE_FRAME_POOL_CAPACITY {
974+
frames.push(lease.frame);
975+
}
976+
}
958977
}
959978

960979
impl CaptureFramePool {
961980
fn new() -> Self {
962981
Self {
963982
key: None,
964-
frames: Vec::new(),
983+
frames: Arc::default(),
965984
}
966985
}
967986

@@ -973,42 +992,71 @@ impl CaptureFramePool {
973992
) -> ffmpeg::frame::Video {
974993
if self.key != Some((format, width, height)) {
975994
self.key = Some((format, width, height));
976-
self.frames = (0..CAPTURE_FRAME_POOL_CAPACITY)
977-
.map(|_| ffmpeg::frame::Video::new(format, width, height))
978-
.collect();
995+
self.frames = Arc::new(parking_lot::Mutex::new(
996+
(0..CAPTURE_FRAME_POOL_CAPACITY)
997+
.map(|_| ffmpeg::frame::Video::new(format, width, height))
998+
.collect(),
999+
));
9791000
}
9801001

981-
if let Some(index) = self.frames.iter_mut().position(capture_frame_writable) {
982-
self.frames.swap_remove(index)
983-
} else {
984-
ffmpeg::frame::Video::new(format, width, height)
985-
}
1002+
self.frames
1003+
.lock()
1004+
.pop()
1005+
.unwrap_or_else(|| ffmpeg::frame::Video::new(format, width, height))
9861006
}
9871007

9881008
fn share(&mut self, frame: ffmpeg::frame::Video) -> ffmpeg::frame::Video {
1009+
let raw = unsafe { &*frame.as_ptr() };
1010+
if self.key != Some((frame.format(), frame.width(), frame.height()))
1011+
|| raw.buf[0].is_null()
1012+
|| raw.buf[1..].iter().any(|buffer| !buffer.is_null())
1013+
|| raw.nb_extended_buf != 0
1014+
{
1015+
return frame;
1016+
}
1017+
let backing = unsafe { *raw.buf[0] };
9891018
let mut shared = ffmpeg::frame::Video::empty();
9901019
let status = unsafe { ffmpeg::ffi::av_frame_ref(shared.as_mut_ptr(), frame.as_ptr()) };
991-
if status >= 0 {
992-
self.release(frame);
993-
shared
994-
} else {
995-
frame
1020+
if status < 0 {
1021+
return frame;
1022+
}
1023+
1024+
// Only the final-release callback owns the backing frame. Keeping it in
1025+
// the idle pool while live would force av_frame_make_writable to copy.
1026+
let lease = Box::into_raw(Box::new(CaptureFrameLease {
1027+
frame,
1028+
frames: Arc::downgrade(&self.frames),
1029+
}));
1030+
let buffer = unsafe {
1031+
ffmpeg::ffi::av_buffer_create(
1032+
backing.data,
1033+
backing.size,
1034+
Some(recycle_capture_frame),
1035+
lease.cast(),
1036+
0,
1037+
)
1038+
};
1039+
if buffer.is_null() {
1040+
return unsafe { Box::from_raw(lease) }.frame;
9961041
}
1042+
unsafe {
1043+
let raw = &mut *shared.as_mut_ptr();
1044+
ffmpeg::ffi::av_buffer_unref(&mut raw.buf[0]);
1045+
raw.buf[0] = buffer;
1046+
}
1047+
shared
9971048
}
9981049

9991050
fn release(&mut self, frame: ffmpeg::frame::Video) {
1000-
if self.key == Some((frame.format(), frame.width(), frame.height()))
1001-
&& self.frames.len() < CAPTURE_FRAME_POOL_CAPACITY
1002-
{
1003-
self.frames.push(frame);
1051+
if self.key == Some((frame.format(), frame.width(), frame.height())) {
1052+
let mut frames = self.frames.lock();
1053+
if frames.len() < CAPTURE_FRAME_POOL_CAPACITY {
1054+
frames.push(frame);
1055+
}
10041056
}
10051057
}
10061058
}
10071059

1008-
fn capture_frame_writable(frame: &mut ffmpeg::frame::Video) -> bool {
1009-
unsafe { ffmpeg::ffi::av_frame_is_writable(frame.as_mut_ptr()) != 0 }
1010-
}
1011-
10121060
fn frame_from_pipewire_data(
10131061
data: &mut spa::buffer::Data,
10141062
format: spa::param::video::VideoInfoRaw,
@@ -1049,7 +1097,7 @@ fn frame_from_pipewire_data(
10491097
let (crop_x, crop_y, crop_width, crop_height) =
10501098
pipewire_crop(source_width, source_height, crop_bounds)?;
10511099
let offset = chunk_offset as usize;
1052-
let dmabuf_fd = (data.type_() == spa::buffer::DataType::DmaBuf).then(|| data.fd() as i32);
1100+
let dmabuf_fd = (data.type_() == spa::buffer::DataType::DmaBuf).then(|| data.fd());
10531101
let mapping;
10541102
let source: &[u8] = match data.data() {
10551103
Some(source) => source,
@@ -3771,12 +3819,7 @@ mod pipewire_frame_tests {
37713819
let overflow = pool.share(overflow);
37723820
assert_eq!(overflow.data(0).as_ptr(), overflow_ptr);
37733821
assert!(overflow.data(0).iter().all(|byte| *byte == 9));
3774-
assert_eq!(pool.frames.len(), CAPTURE_FRAME_POOL_CAPACITY);
3775-
assert!(
3776-
pool.frames
3777-
.iter()
3778-
.all(|frame| pointers.contains(&frame.data(0).as_ptr()))
3779-
);
3822+
assert!(pool.frames.lock().is_empty());
37803823

37813824
for (index, frame) in held.iter().enumerate() {
37823825
let marker = (index + 1) as u8;
@@ -3788,11 +3831,14 @@ mod pipewire_frame_tests {
37883831
drop(held.remove(0));
37893832
let reused = pool.acquire(format, 4, 2);
37903833
assert_eq!(reused.data(0).as_ptr(), dropped_ptr);
3791-
assert_eq!(pool.frames.len(), CAPTURE_FRAME_POOL_CAPACITY - 1);
3834+
assert!(pool.frames.lock().is_empty());
37923835
for (frame, pointer) in held.iter().zip(&pointers) {
37933836
assert_eq!(frame.data(0).as_ptr(), *pointer);
37943837
}
3795-
let _overflow = overflow;
3838+
drop(reused);
3839+
drop(held);
3840+
drop(overflow);
3841+
assert_eq!(pool.frames.lock().len(), CAPTURE_FRAME_POOL_CAPACITY);
37963842
}
37973843

37983844
#[test]

0 commit comments

Comments
 (0)