@@ -44,6 +44,10 @@ mod x11_tests;
4444#[ cfg( test) ]
4545mod system_audio_channel_tests;
4646
47+ #[ cfg( test) ]
48+ #[ path = "linux_frame_pool_tests.rs" ]
49+ mod frame_pool_tests;
50+
4751use frame_scaler:: FrameScaler ;
4852
4953#[ derive( Debug ) ]
@@ -954,14 +958,29 @@ const CAPTURE_FRAME_POOL_CAPACITY: usize = 4;
954958
955959struct 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
960979impl 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-
10121060fn 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