Fix rare panics on out-of-bounds stream position

This commit is contained in:
Roderick van Domburg 2022-01-09 22:46:44 +01:00
parent 75e6441db9
commit a62c1fea8f
No known key found for this signature in database
GPG key ID: FE2585E713F9F30A

View file

@ -1115,26 +1115,32 @@ impl Future for PlayerInternal {
// Only notify if we're skipped some packets *or* we are behind. // Only notify if we're skipped some packets *or* we are behind.
// If we're ahead it's probably due to a buffer of the backend // If we're ahead it's probably due to a buffer of the backend
// and we're actually in time. // and we're actually in time.
let new_stream_position = Duration::from_millis(
new_stream_position_ms as u64,
);
let notify_about_position = packet_position.skipped let notify_about_position = packet_position.skipped
|| match *reported_nominal_start_time { || match *reported_nominal_start_time {
None => true, None => true,
Some(reported_nominal_start_time) => { Some(reported_nominal_start_time) => {
let lag = (Instant::now() let mut notify = false;
- reported_nominal_start_time) if let Some(lag) = Instant::now()
.as_millis() .checked_duration_since(
as i64 reported_nominal_start_time,
- new_stream_position_ms as i64; )
lag > Duration::from_secs(1).as_millis() {
as i64 if let Some(lag) =
lag.checked_sub(new_stream_position)
{
notify =
lag > Duration::from_secs(1)
}
}
notify
} }
}; };
if notify_about_position { if notify_about_position {
*reported_nominal_start_time = Some( *reported_nominal_start_time =
Instant::now() Instant::now().checked_sub(new_stream_position);
- Duration::from_millis(
new_stream_position_ms as u64,
),
);
self.send_event(PlayerEvent::Playing { self.send_event(PlayerEvent::Playing {
track_id, track_id,
play_request_id, play_request_id,
@ -1539,9 +1545,8 @@ impl PlayerInternal {
duration_ms: loaded_track.duration_ms, duration_ms: loaded_track.duration_ms,
bytes_per_second: loaded_track.bytes_per_second, bytes_per_second: loaded_track.bytes_per_second,
stream_position_ms: loaded_track.stream_position_ms, stream_position_ms: loaded_track.stream_position_ms,
reported_nominal_start_time: Some( reported_nominal_start_time: Instant::now()
Instant::now() - Duration::from_millis(position_ms as u64), .checked_sub(Duration::from_millis(position_ms as u64)),
),
suggested_to_preload_next_track: false, suggested_to_preload_next_track: false,
is_explicit: loaded_track.is_explicit, is_explicit: loaded_track.is_explicit,
}; };
@ -1873,7 +1878,7 @@ impl PlayerInternal {
} = self.state } = self.state
{ {
*reported_nominal_start_time = *reported_nominal_start_time =
Some(Instant::now() - Duration::from_millis(position_ms as u64)); Instant::now().checked_sub(Duration::from_millis(position_ms as u64));
self.send_event(PlayerEvent::Playing { self.send_event(PlayerEvent::Playing {
track_id, track_id,
play_request_id, play_request_id,