@@ -222,7 +222,7 @@ impl MakeCapturePipeline for screen_capture::CMSampleBufferCapture {
222222 }
223223}
224224
225- // #[cfg(windows)]
225+ #[ cfg( windows) ]
226226impl MakeCapturePipeline for screen_capture:: Direct3DCapture {
227227 fn make_studio_mode_pipeline (
228228 mut builder : PipelineBuilder ,
@@ -235,19 +235,9 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
235235 where
236236 Self : Sized ,
237237 {
238- use cap_enc_mediafoundation:: { media:: MF_VERSION , video:: SampleWriter } ;
239- use windows:: {
240- Graphics :: SizeInt32 ,
241- Win32 :: {
242- Media :: MediaFoundation :: { MFSTARTUP_FULL , MFStartup } ,
243- System :: WinRT :: { RO_INIT_MULTITHREADED , RoInitialize } ,
244- } ,
245- } ;
238+ use windows:: Graphics :: SizeInt32 ;
246239
247- unsafe {
248- let _ = RoInitialize ( RO_INIT_MULTITHREADED ) ;
249- let _ = MFStartup ( MF_VERSION , MFSTARTUP_FULL ) ;
250- }
240+ cap_mediafoundation_utils:: thread_init ( ) ;
251241
252242 let screen_config = source. 0 . info ( ) ;
253243
@@ -324,8 +314,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
324314 use cap_enc_mediafoundation:: media:: MF_VERSION ;
325315 use windows:: Win32 :: Media :: MediaFoundation :: { self , MFSTARTUP_FULL , MFStartup } ;
326316
327- let _ = unsafe { RoInitialize ( RO_INIT_MULTITHREADED ) } ;
328- let _ = unsafe { MFStartup ( MF_VERSION , MFSTARTUP_FULL ) } ;
317+ cap_mediafoundation_utils:: thread_init ( ) ;
329318
330319 let _ = ready. send ( Ok ( ( ) ) ) ;
331320
@@ -435,9 +424,11 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
435424 where
436425 Self : Sized ,
437426 {
438- use cap_enc_ffmpeg:: { AACEncoder , AudioEncoder , H264Encoder , MP4File } ;
427+ use cap_enc_ffmpeg:: { AACEncoder , AudioEncoder } ;
439428 use windows:: Graphics :: SizeInt32 ;
440429
430+ cap_mediafoundation_utils:: thread_init ( ) ;
431+
441432 let ( audio_tx, audio_rx) = flume:: bounded ( 64 ) ;
442433 let mut audio_mixer = AudioMixer :: new ( audio_tx) ;
443434
@@ -469,6 +460,8 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
469460 )
470461 . unwrap ( ) ;
471462
463+ screen_encoder. start ( ) . unwrap ( ) ;
464+
472465 let mut screen_muxer = cap_mediafoundation_ffmpeg:: H264StreamMuxer :: add_stream (
473466 & mut output,
474467 cap_mediafoundation_ffmpeg:: MuxerConfig {
@@ -480,8 +473,6 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
480473 )
481474 . unwrap ( ) ;
482475
483- output. write_header ( ) . unwrap ( ) ;
484-
485476 let audio_encoder = has_audio_sources
486477 . then ( || {
487478 AACEncoder :: init ( "mic_audio" , AudioMixer :: info ( ) , & mut output)
@@ -491,11 +482,16 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
491482 . transpose ( )
492483 . unwrap ( ) ;
493484
485+ output. write_header ( ) . unwrap ( ) ;
486+
494487 let output = Arc :: new ( std:: sync:: Mutex :: new ( output) ) ;
495488
489+ let is_done = Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
490+
496491 if let Some ( mut audio_encoder) = audio_encoder {
497492 builder. spawn_source ( "audio_mixer" , audio_mixer) ;
498493
494+ let is_done = is_done. clone ( ) ;
499495 let output = output. clone ( ) ;
500496 builder. spawn_task ( "audio_encoding" , move |ready| {
501497 let _ = ready. send ( Ok ( ( ) ) ) ;
@@ -504,7 +500,9 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
504500 // mp4.queue_audio_frame(frame);
505501 // }
506502
507- if let Ok ( mut output) = output. lock ( ) {
503+ if let Ok ( mut output) = output. lock ( )
504+ && !is_done. load ( std:: sync:: atomic:: Ordering :: Relaxed )
505+ {
508506 audio_encoder. queue_frame ( frame, & mut * output) ;
509507 }
510508 }
@@ -538,10 +536,9 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
538536 } ;
539537 let first_time = first_time. get_or_insert ( frame_time) ;
540538
539+ let duration = frame_time. Duration - first_time. Duration ;
541540 let sample = VideoEncoderInputSample :: new (
542- TimeSpan {
543- Duration : frame_time. Duration - first_time. Duration ,
544- } ,
541+ TimeSpan { Duration : duration } ,
545542 frame. 0 . texture ( ) . clone ( ) ,
546543 ) ;
547544
@@ -566,7 +563,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
566563 }
567564
568565 screen_encoder. finish ( ) . unwrap ( ) ;
569-
566+ is_done . store ( true , std :: sync :: atomic :: Ordering :: Relaxed ) ;
570567 output. lock ( ) . unwrap ( ) . write_trailer ( ) . unwrap ( ) ;
571568
572569 Ok ( ( ) )
0 commit comments