@@ -83,6 +83,12 @@ impl Stream {
8383
8484 let bps = sample_spec. format . bytes_per_sample ( ) ;
8585 let n_samples = buf. len ( ) / bps;
86+
87+ // SAFETY: We verify that:
88+ // - buf.as_ptr() points to valid memory for at least n_samples * bytes_per_sample
89+ // - n_samples is calculated from buf.len() / bytes_per_sample, ensuring validity
90+ // - The buffer remains valid for the duration of the callback
91+ // - sample_format matches the actual data layout in the buffer
8692 let mut data =
8793 unsafe { Data :: from_parts ( buf. as_mut_ptr ( ) . cast ( ) , n_samples, sample_format) } ;
8894
@@ -98,11 +104,13 @@ impl Stream {
98104 let stream = block_on ( client. create_playback_stream ( params, callback. as_playback_source ( ) ) )
99105 . map_err ( Into :: < BackendSpecificError > :: into) ?;
100106
101- // Spawn a thread to drive the stream future.
107+ // Spawn a thread to drive the stream future. It will exit automatically
108+ // when the stream is stopped by the user.
102109 let stream_clone = stream. clone ( ) ;
103110 let _worker_thread = std:: thread:: spawn ( move || block_on ( stream_clone. play_all ( ) ) ) ;
104111
105- // Spawn a thread to monitor the stream's latency in a loop.
112+ // Spawn a thread to monitor the stream's latency in a loop. It will
113+ // exit automatically when the stream ends.
106114 let stream_clone = stream. clone ( ) ;
107115 let latency_clone = current_latency_micros. clone ( ) ;
108116 std:: thread:: spawn ( move || loop {
@@ -161,6 +169,12 @@ impl Stream {
161169
162170 let bps = sample_spec. format . bytes_per_sample ( ) ;
163171 let n_samples = buf. len ( ) / bps;
172+
173+ // SAFETY: We verify that:
174+ // - buf.as_ptr() points to valid memory for at least n_samples * bytes_per_sample
175+ // - n_samples is calculated from buf.len() / bytes_per_sample, ensuring validity
176+ // - The buffer remains valid for the duration of the callback
177+ // - sample_format matches the actual data layout in the buffer
164178 let data =
165179 unsafe { Data :: from_parts ( buf. as_ptr ( ) as * mut _ , n_samples, sample_format) } ;
166180
@@ -170,7 +184,8 @@ impl Stream {
170184 let stream = block_on ( client. create_record_stream ( params, callback) )
171185 . map_err ( Into :: < BackendSpecificError > :: into) ?;
172186
173- // Spawn a thread to monitor the stream's latency in a loop.
187+ // Spawn a thread to monitor the stream's latency in a loop. It will
188+ // exit automatically when the stream ends.
174189 let stream_clone = stream. clone ( ) ;
175190 let latency_clone = current_latency_micros. clone ( ) ;
176191 std:: thread:: spawn ( move || loop {
0 commit comments