mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
A phone showing what the paired computer plays restamped each player state with the time it arrived, because the two clocks may run seconds apart. The time the state spent on the way was lost, so the position, and the lyrics with it, trailed the computer's audio by the network delay, often a fraction of a second over Wi-Fi. The phone now measures the computer's clock from its `GET state` replies, stamped halfway through each round trip, keeping the sample with the shortest round trip. Pushed states move onto the phone's clock by that offset. A few pushes read the clock again until three samples exist, then once a minute; reconnecting starts over. Before any sample, or for a stamp over 5 s old, a state counts as read when it arrived.
45 lines
1.7 KiB
Dart
45 lines
1.7 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:mornye/services/link/link_clock_offset.dart';
|
|
|
|
void main() {
|
|
test('takes the offset of the shortest round trip', () {
|
|
final clock = LinkClockOffset();
|
|
expect(clock.offsetMs, isNull);
|
|
|
|
// The other clock runs 5 s ahead. A slow reply, stamped early on its
|
|
// way, says less than a quick one.
|
|
clock.add(sentMs: 1000, receivedMs: 1400, remoteMs: 6050);
|
|
expect(clock.offsetMs, 4850);
|
|
clock.add(sentMs: 2000, receivedMs: 2020, remoteMs: 7010);
|
|
expect(clock.offsetMs, 5000);
|
|
clock.add(sentMs: 3000, receivedMs: 3300, remoteMs: 8290);
|
|
expect(clock.offsetMs, 5000);
|
|
});
|
|
|
|
test('drops replies that say nothing useful', () {
|
|
final clock = LinkClockOffset(maxRoundTripMs: 2000);
|
|
clock.add(sentMs: 1000, receivedMs: 900, remoteMs: 6000);
|
|
clock.add(sentMs: 1000, receivedMs: 3500, remoteMs: 6000);
|
|
clock.add(sentMs: 1000, receivedMs: 1010, remoteMs: 0);
|
|
expect(clock.offsetMs, isNull);
|
|
});
|
|
|
|
test('keeps only recent samples and asks for more while few or old', () {
|
|
final clock = LinkClockOffset(keep: 2, minimumSamples: 2);
|
|
expect(clock.wantsSample(0), isTrue);
|
|
clock.add(sentMs: 0, receivedMs: 10, remoteMs: 100);
|
|
expect(clock.wantsSample(10), isTrue);
|
|
clock.add(sentMs: 20, receivedMs: 60, remoteMs: 140);
|
|
expect(clock.wantsSample(60), isFalse);
|
|
expect(clock.wantsSample(60060), isTrue);
|
|
|
|
// The quick first sample ages out with a third.
|
|
clock.add(sentMs: 100, receivedMs: 140, remoteMs: 230);
|
|
expect(clock.offsetMs, 100);
|
|
|
|
clock.clear();
|
|
expect(clock.offsetMs, isNull);
|
|
expect(clock.wantsSample(200), isTrue);
|
|
});
|
|
}
|