@@ -68,12 +68,16 @@ async fn receive_data(
68
68
initial_data_offset : usize ,
69
69
initial_request_length : usize ,
70
70
request_sent_time : Instant ,
71
- mut measure_ping_time : bool ,
72
- finish_tx : mpsc:: UnboundedSender < ( ) > ,
73
71
) {
74
72
let mut data_offset = initial_data_offset;
75
73
let mut request_length = initial_request_length;
76
74
75
+ let old_number_of_request = shared
76
+ . number_of_open_requests
77
+ . fetch_add ( 1 , atomic:: Ordering :: SeqCst ) ;
78
+
79
+ let mut measure_ping_time = old_number_of_request == 0 ;
80
+
77
81
let result = loop {
78
82
let data = match data_rx. next ( ) . await {
79
83
Some ( Ok ( data) ) => data,
@@ -121,7 +125,9 @@ async fn receive_data(
121
125
shared. cond . notify_all ( ) ;
122
126
}
123
127
124
- let _ = finish_tx. send ( ( ) ) ;
128
+ shared
129
+ . number_of_open_requests
130
+ . fetch_sub ( 1 , atomic:: Ordering :: SeqCst ) ;
125
131
126
132
if result. is_err ( ) {
127
133
warn ! (
@@ -144,9 +150,6 @@ struct AudioFileFetch {
144
150
file_data_tx : mpsc:: UnboundedSender < ReceivedData > ,
145
151
complete_tx : Option < oneshot:: Sender < NamedTempFile > > ,
146
152
network_response_times_ms : Vec < usize > ,
147
- number_of_open_requests : usize ,
148
-
149
- download_finish_tx : mpsc:: UnboundedSender < ( ) > ,
150
153
}
151
154
152
155
// Might be replaced by enum from std once stable
@@ -214,11 +217,7 @@ impl AudioFileFetch {
214
217
range. start ,
215
218
range. length ,
216
219
Instant :: now ( ) ,
217
- self . number_of_open_requests == 0 ,
218
- self . download_finish_tx . clone ( ) ,
219
220
) ) ;
220
-
221
- self . number_of_open_requests += 1 ;
222
221
}
223
222
}
224
223
@@ -341,7 +340,6 @@ impl AudioFileFetch {
341
340
}
342
341
StreamLoaderCommand :: StreamMode ( ) => {
343
342
* ( self . shared . download_strategy . lock ( ) . unwrap ( ) ) = DownloadStrategy :: Streaming ( ) ;
344
- self . trigger_preload ( ) ;
345
343
}
346
344
StreamLoaderCommand :: Close ( ) => return ControlFlow :: Break ,
347
345
}
@@ -355,36 +353,6 @@ impl AudioFileFetch {
355
353
output. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
356
354
let _ = complete_tx. send ( output) ;
357
355
}
358
-
359
- fn trigger_preload ( & mut self ) {
360
- if self . number_of_open_requests >= MAX_PREFETCH_REQUESTS {
361
- return ;
362
- }
363
-
364
- let max_requests_to_send = MAX_PREFETCH_REQUESTS - self . number_of_open_requests ;
365
-
366
- let bytes_pending: usize = {
367
- let download_status = self . shared . download_status . lock ( ) . unwrap ( ) ;
368
- download_status
369
- . requested
370
- . minus ( & download_status. downloaded )
371
- . len ( )
372
- } ;
373
-
374
- let ping_time_seconds =
375
- 0.001 * self . shared . ping_time_ms . load ( atomic:: Ordering :: Relaxed ) as f64 ;
376
- let download_rate = self . session . channel ( ) . get_download_rate_estimate ( ) ;
377
-
378
- let desired_pending_bytes = max (
379
- ( PREFETCH_THRESHOLD_FACTOR * ping_time_seconds * self . shared . stream_data_rate as f64 )
380
- as usize ,
381
- ( FAST_PREFETCH_THRESHOLD_FACTOR * ping_time_seconds * download_rate as f64 ) as usize ,
382
- ) ;
383
-
384
- if bytes_pending < desired_pending_bytes {
385
- self . pre_fetch_more_data ( desired_pending_bytes - bytes_pending, max_requests_to_send) ;
386
- }
387
- }
388
356
}
389
357
390
358
pub ( super ) async fn audio_file_fetch (
@@ -399,7 +367,6 @@ pub(super) async fn audio_file_fetch(
399
367
complete_tx : oneshot:: Sender < NamedTempFile > ,
400
368
) {
401
369
let ( file_data_tx, mut file_data_rx) = mpsc:: unbounded_channel ( ) ;
402
- let ( download_finish_tx, mut download_finish_rx) = mpsc:: unbounded_channel ( ) ;
403
370
404
371
{
405
372
let requested_range = Range :: new ( 0 , initial_data_length) ;
@@ -414,8 +381,6 @@ pub(super) async fn audio_file_fetch(
414
381
0 ,
415
382
initial_data_length,
416
383
initial_request_sent_time,
417
- true ,
418
- download_finish_tx. clone ( ) ,
419
384
) ) ;
420
385
421
386
let mut fetch = AudioFileFetch {
@@ -426,9 +391,6 @@ pub(super) async fn audio_file_fetch(
426
391
file_data_tx,
427
392
complete_tx : Some ( complete_tx) ,
428
393
network_response_times_ms : Vec :: new ( ) ,
429
- number_of_open_requests : 1 ,
430
-
431
- download_finish_tx,
432
394
} ;
433
395
434
396
loop {
@@ -442,12 +404,42 @@ pub(super) async fn audio_file_fetch(
442
404
if data. map_or( true , |data| fetch. handle_file_data( data) == ControlFlow :: Break ) {
443
405
break ;
444
406
}
445
- } ,
446
- _ = download_finish_rx. recv( ) => {
447
- fetch. number_of_open_requests -= 1 ;
407
+ }
408
+ }
409
+
410
+ if fetch. get_download_strategy ( ) == DownloadStrategy :: Streaming ( ) {
411
+ let number_of_open_requests = fetch
412
+ . shared
413
+ . number_of_open_requests
414
+ . load ( atomic:: Ordering :: SeqCst ) ;
415
+ if number_of_open_requests < MAX_PREFETCH_REQUESTS {
416
+ let max_requests_to_send = MAX_PREFETCH_REQUESTS - number_of_open_requests;
417
+
418
+ let bytes_pending: usize = {
419
+ let download_status = fetch. shared . download_status . lock ( ) . unwrap ( ) ;
420
+ download_status
421
+ . requested
422
+ . minus ( & download_status. downloaded )
423
+ . len ( )
424
+ } ;
448
425
449
- if fetch. get_download_strategy( ) == DownloadStrategy :: Streaming ( ) {
450
- fetch. trigger_preload( ) ;
426
+ let ping_time_seconds =
427
+ 0.001 * fetch. shared . ping_time_ms . load ( atomic:: Ordering :: Relaxed ) as f64 ;
428
+ let download_rate = fetch. session . channel ( ) . get_download_rate_estimate ( ) ;
429
+
430
+ let desired_pending_bytes = max (
431
+ ( PREFETCH_THRESHOLD_FACTOR
432
+ * ping_time_seconds
433
+ * fetch. shared . stream_data_rate as f64 ) as usize ,
434
+ ( FAST_PREFETCH_THRESHOLD_FACTOR * ping_time_seconds * download_rate as f64 )
435
+ as usize ,
436
+ ) ;
437
+
438
+ if bytes_pending < desired_pending_bytes {
439
+ fetch. pre_fetch_more_data (
440
+ desired_pending_bytes - bytes_pending,
441
+ max_requests_to_send,
442
+ ) ;
451
443
}
452
444
}
453
445
}
0 commit comments