@@ -2596,12 +2596,15 @@ int wasapi_stream_get_position(cubeb_stream * stm, uint64_t * position)
2596
2596
return CUBEB_ERROR;
2597
2597
}
2598
2598
2599
+ double resample_ratio = stream_to_mix_samplerate_ratio (stm->output_stream_params , stm->output_mix_params );
2600
+
2599
2601
/* Calculate how far behind the current stream head the playback cursor is. */
2600
- uint64_t stream_delay = static_cast <uint64_t >(current_stream_delay (stm) * stm->output_stream_params .rate );
2602
+ uint64_t resampler_delay = ceil (cubeb_resampler_latency (stm->resampler .get ()) * resample_ratio);
2603
+ uint64_t stream_delay = static_cast <uint64_t >((current_stream_delay (stm) * stm->output_stream_params .rate )) + resampler_delay;
2601
2604
2602
2605
/* Calculate the logical stream head in frames at the stream sample rate. */
2603
2606
uint64_t max_pos = stm->total_frames_written +
2604
- static_cast <uint64_t >(round (stm->frames_written * stream_to_mix_samplerate_ratio (stm-> output_stream_params , stm-> output_mix_params ) ));
2607
+ static_cast <uint64_t >(round (stm->frames_written * resample_ratio ));
2605
2608
2606
2609
*position = max_pos;
2607
2610
if (stream_delay <= *position) {
@@ -2637,13 +2640,18 @@ int wasapi_stream_get_latency(cubeb_stream * stm, uint32_t * latency)
2637
2640
if (FAILED (hr)) {
2638
2641
return CUBEB_ERROR;
2639
2642
}
2643
+
2644
+
2645
+ double resample_ratio = stream_to_mix_samplerate_ratio (stm->output_stream_params , stm->output_mix_params );
2646
+ uint32_t resample_latency = ceil (cubeb_resampler_latency (stm->resampler .get ()) * resample_ratio);
2647
+
2640
2648
// This happens on windows 10: no error, but always 0 for latency.
2641
2649
if (latency_hns == 0 ) {
2642
2650
double delay_s = current_stream_delay (stm);
2643
- // convert to sample-frames
2644
- *latency = delay_s * stm->output_stream_params .rate ;
2651
+ // convert to output(mix) rate sample-frames
2652
+ *latency = ( delay_s * stm->output_mix_params .rate ) + resample_latency ;
2645
2653
} else {
2646
- *latency = hns_to_frames (stm, latency_hns);
2654
+ *latency = hns_to_frames (stm, latency_hns) + resample_latency ;
2647
2655
}
2648
2656
2649
2657
return CUBEB_OK;
@@ -2664,7 +2672,11 @@ int wasapi_stream_get_input_latency(cubeb_stream * stm, uint32_t * latency)
2664
2672
return CUBEB_ERROR;
2665
2673
}
2666
2674
2667
- *latency = hns_to_frames (stm, stm->input_latency_hns );
2675
+
2676
+ double resample_ratio = stream_to_mix_samplerate_ratio (stm->output_stream_params , stm->output_mix_params );
2677
+ uint32_t resample_latency = ceil (cubeb_resampler_latency (stm->resampler .get ()) * resample_ratio);
2678
+
2679
+ *latency = hns_to_frames (stm, stm->input_latency_hns ) + resample_latency;
2668
2680
2669
2681
return CUBEB_OK;
2670
2682
}
0 commit comments