@@ -57,6 +57,8 @@ pub struct RecordingSession {
5757 mp4_writer_worker : Option < JoinHandle < ( ) > > ,
5858 h264_frame_sender : Option < Sender < VideoFrameType > > ,
5959
60+ video_encoder : Option < VideoEncoder > ,
61+
6062 // statistic
6163 start_time : Instant ,
6264 total_frame_count : Arc < AtomicU64 > ,
@@ -104,6 +106,8 @@ impl RecordingSession {
104106 mp4_writer_worker : None ,
105107 h264_frame_sender : None ,
106108
109+ video_encoder : None ,
110+
107111 start_time : std:: time:: Instant :: now ( ) ,
108112 total_frame_count : Arc :: new ( AtomicU64 :: new ( 0 ) ) ,
109113 loss_frame_count : Arc :: new ( AtomicU64 :: new ( 0 ) ) ,
@@ -118,19 +122,23 @@ impl RecordingSession {
118122
119123 log:: info!( "capture thread counts: {thread_counts}" ) ;
120124
121- let frame_iterval_ms = self . config . frame_interval_ms ( ) ;
122- let fps_per_thread = self . config . fps . to_u32 ( ) as f64 / thread_counts as f64 ;
125+ self . start_time = std:: time:: Instant :: now ( ) ;
123126
124- let config = CaptureIterConfig {
125- name : self . config . screen_name . clone ( ) ,
126- include_cursor : self . config . include_cursor ,
127- fps : Some ( fps_per_thread) ,
128- cancel_sig : self . stop_sig . clone ( ) ,
129- } ;
127+ let ( encoder_width, encoder_height) = self . config . resolution . dimensions (
128+ self . config . screen_size . width as u32 ,
129+ self . config . screen_size . height as u32 ,
130+ ) ;
131+ let mut video_encoder = VideoEncoder :: new ( encoder_width, encoder_height, self . config . fps ) ?;
132+ let headers_data = video_encoder. headers ( ) ?. entirety ( ) . to_vec ( ) ;
133+ self . video_encoder = Some ( video_encoder) ;
130134
131- self . start_time = std :: time :: Instant :: now ( ) ;
135+ let ( audio_sender , speaker_sender ) = self . mp4_worker ( headers_data . clone ( ) ) ? ;
132136
133- let ( audio_sender, speaker_sender) = self . mp4_worker ( ) ?;
137+ if let Some ( ref sender) = self . h264_frame_sender {
138+ if let Err ( e) = sender. try_send ( VideoFrameType :: Frame ( headers_data) ) {
139+ log:: warn!( "Try send h264 header frames faield: {e}" ) ;
140+ }
141+ }
134142
135143 if let Some ( device_name) = self . config . audio_device_name . clone ( ) {
136144 self . enable_audio ( device_name. as_str ( ) , audio_sender) ?;
@@ -142,6 +150,15 @@ impl RecordingSession {
142150 log:: info!( "Enable speaker recording successfully" ) ;
143151 } ;
144152
153+ let frame_iterval_ms = self . config . frame_interval_ms ( ) ;
154+ let fps_per_thread = self . config . fps . to_u32 ( ) as f64 / thread_counts as f64 ;
155+ let config = CaptureIterConfig {
156+ name : self . config . screen_name . clone ( ) ,
157+ include_cursor : self . config . include_cursor ,
158+ fps : Some ( fps_per_thread) ,
159+ cancel_sig : self . stop_sig . clone ( ) ,
160+ } ;
161+
145162 for i in 0 ..thread_counts {
146163 let config_duplicate = config. clone ( ) ;
147164 let tx = self . frame_sender . clone ( ) . unwrap ( ) ;
@@ -173,29 +190,20 @@ impl RecordingSession {
173190 Ok ( ( ) )
174191 }
175192
176- pub fn wait ( self ) -> Result < ProgressState , RecorderError > {
193+ pub fn wait ( mut self ) -> Result < ProgressState , RecorderError > {
177194 let ( encoder_sender, encoder_receiver) = bounded ( ENCODER_WORKER_CHANNEL_SIZE ) ;
178195 let resize_handle = Self :: resize_worker ( & self , encoder_sender) ;
179196
180- let ( width, height) = self . config . resolution . dimensions (
181- self . config . screen_size . width as u32 ,
182- self . config . screen_size . height as u32 ,
183- ) ;
184- let mut video_encoder = VideoEncoder :: new ( width, height, self . config . fps ) ?;
185-
186- if let Some ( ref sender) = self . h264_frame_sender {
187- let headers = video_encoder. headers ( ) ?;
188- let headers_data = headers. entirety ( ) . to_vec ( ) ;
189- if let Err ( e) = sender. try_send ( VideoFrameType :: Frame ( headers_data) ) {
190- log:: warn!( "Try send h264 header frames faield: {e}" ) ;
191- }
192- }
193-
194197 loop {
195198 match encoder_receiver. recv ( ) {
196199 Ok ( ( total_frame_index, img) ) => {
197200 let now = std:: time:: Instant :: now ( ) ;
198- match video_encoder. encode_frame ( img. into ( ) ) {
201+ match self
202+ . video_encoder
203+ . as_mut ( )
204+ . unwrap ( )
205+ . encode_frame ( img. into ( ) )
206+ {
199207 Ok ( EncodedFrame :: Frame ( ( _, encoded_frame) ) ) => {
200208 log:: debug!(
201209 "total encoded frame[{total_frame_index}] {} bytes" ,
@@ -224,7 +232,7 @@ impl RecordingSession {
224232 _ => {
225233 log:: info!( "encoder receiver channel exit..." ) ;
226234 self . stop ( ) ;
227- self . wait_stop ( resize_handle, video_encoder ) ?;
235+ self . wait_stop ( resize_handle) ?;
228236 break ;
229237 }
230238 }
@@ -357,6 +365,7 @@ impl RecordingSession {
357365
358366 fn mp4_worker (
359367 & mut self ,
368+ video_encoder_header_data : Vec < u8 > ,
360369 ) -> Result < ( Option < Sender < Vec < f32 > > > , Option < Sender < Vec < f32 > > > ) , RecorderError > {
361370 let mut specs = vec ! [ ] ;
362371 if let Some ( ref device_name) = self . config . audio_device_name {
@@ -447,7 +456,7 @@ impl RecordingSession {
447456
448457 self . h264_frame_sender = Some ( mp4_processor. h264_sender ( ) ) ;
449458 let handle = thread:: spawn ( move || {
450- if let Err ( e) = mp4_processor. run_processing_loop ( None ) {
459+ if let Err ( e) = mp4_processor. run_processing_loop ( Some ( video_encoder_header_data ) ) {
451460 log:: warn!( "MP4 processing error: {}" , e) ;
452461 }
453462 } ) ;
@@ -456,11 +465,7 @@ impl RecordingSession {
456465 Ok ( ( audio_sender, speak_sender) )
457466 }
458467
459- fn wait_stop (
460- mut self ,
461- resize_handle : JoinHandle < ( ) > ,
462- encoder : VideoEncoder ,
463- ) -> Result < ( ) , RecorderError > {
468+ fn wait_stop ( mut self , resize_handle : JoinHandle < ( ) > ) -> Result < ( ) , RecorderError > {
464469 if let Some ( audio_recorder) = self . audio_recorder . take ( ) {
465470 audio_recorder. stop ( ) ;
466471 log:: info!( "audio recorder exit..." ) ;
@@ -488,7 +493,7 @@ impl RecordingSession {
488493 log:: info!( "join resize thread successfully" ) ;
489494 }
490495
491- match encoder . flush ( ) {
496+ match self . video_encoder . take ( ) . unwrap ( ) . flush ( ) {
492497 Ok ( mut flush) => {
493498 while let Some ( result) = flush. next ( ) {
494499 match result {
0 commit comments