@@ -96,15 +96,14 @@ impl Provider {
9696 ///
9797 /// On Windows, `extra_info` can be used to force backend selection with values like
9898 /// `"auto"`, `"msmf"`, `"dshow"`, or `"backend=<value>"`.
99- pub fn with_device_and_extra_info (
100- device_index : i32 ,
101- extra_info : Option < & str > ,
102- ) -> Result < Self > {
99+ pub fn with_device_and_extra_info ( device_index : i32 , extra_info : Option < & str > ) -> Result < Self > {
103100 let extra_info = optional_c_string ( extra_info, "extra info" ) ?;
104101 let handle = unsafe {
105102 sys:: ccap_provider_create_with_index (
106103 device_index,
107- extra_info. as_ref ( ) . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
104+ extra_info
105+ . as_ref ( )
106+ . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
108107 )
109108 } ;
110109 if handle. is_null ( ) {
@@ -144,7 +143,9 @@ impl Provider {
144143 let handle = unsafe {
145144 sys:: ccap_provider_create_with_device (
146145 c_name. as_ptr ( ) ,
147- extra_info. as_ref ( ) . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
146+ extra_info
147+ . as_ref ( )
148+ . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
148149 )
149150 } ;
150151 if handle. is_null ( ) {
@@ -271,6 +272,11 @@ impl Provider {
271272 auto_start : bool ,
272273 ) -> Result < ( ) > {
273274 if let Some ( name) = device_name {
275+ let c_name = CString :: new ( name) . map_err ( |_| {
276+ CcapError :: InvalidParameter ( "device name contains null byte" . to_string ( ) )
277+ } ) ?;
278+ let extra_info = optional_c_string ( extra_info, "extra info" ) ?;
279+
274280 // Recreate provider with specific device
275281 if !self . handle . is_null ( ) {
276282 // If the previous provider was running, stop it and detach callbacks
@@ -281,21 +287,27 @@ impl Provider {
281287 unsafe {
282288 sys:: ccap_provider_destroy ( self . handle ) ;
283289 }
290+ self . handle = ptr:: null_mut ( ) ;
291+ self . is_opened = false ;
292+ } else {
293+ self . cleanup_callback ( ) ;
284294 }
285- let c_name = CString :: new ( name) . map_err ( |_| {
286- CcapError :: InvalidParameter ( "device name contains null byte" . to_string ( ) )
287- } ) ?;
288- let extra_info = optional_c_string ( extra_info, "extra info" ) ?;
295+
289296 self . handle = unsafe {
290297 sys:: ccap_provider_create_with_device (
291298 c_name. as_ptr ( ) ,
292- extra_info. as_ref ( ) . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
299+ extra_info
300+ . as_ref ( )
301+ . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
293302 )
294303 } ;
295304 if self . handle . is_null ( ) {
296305 return Err ( CcapError :: InvalidDevice ( name. to_string ( ) ) ) ;
297306 }
298307 self . is_opened = true ;
308+ if !auto_start {
309+ self . stop_capture ( ) ?;
310+ }
299311 } else if extra_info. is_some ( ) {
300312 return self . open_with_index_and_extra_info ( -1 , extra_info, auto_start) ;
301313 } else {
@@ -581,6 +593,8 @@ impl Provider {
581593 extra_info : Option < & str > ,
582594 auto_start : bool ,
583595 ) -> Result < ( ) > {
596+ let extra_info = optional_c_string ( extra_info, "extra info" ) ?;
597+
584598 // If the previous provider was running, stop it and detach callbacks
585599 // before destroying the underlying handle.
586600 if !self . handle . is_null ( ) {
@@ -590,18 +604,20 @@ impl Provider {
590604 unsafe {
591605 sys:: ccap_provider_destroy ( self . handle ) ;
592606 }
607+ self . handle = ptr:: null_mut ( ) ;
608+ self . is_opened = false ;
593609 } else {
594610 // Clean up any stale callback allocation even if handle is null.
595611 self . cleanup_callback ( ) ;
596612 }
597613
598- let extra_info = optional_c_string ( extra_info, "extra info" ) ?;
599-
600614 // Create a new provider with the specified device index
601615 self . handle = unsafe {
602616 sys:: ccap_provider_create_with_index (
603617 device_index,
604- extra_info. as_ref ( ) . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
618+ extra_info
619+ . as_ref ( )
620+ . map_or ( ptr:: null ( ) , |value| value. as_ptr ( ) ) ,
605621 )
606622 } ;
607623
@@ -614,6 +630,9 @@ impl Provider {
614630
615631 // ccap C API contract: create_with_index opens the device.
616632 self . is_opened = true ;
633+ if !auto_start {
634+ self . stop_capture ( ) ?;
635+ }
617636 if auto_start {
618637 self . start_capture ( ) ?;
619638 }
0 commit comments