Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/track-stats-monitor-switch
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
minor type="added" "Track.statsMonitorEnabled turns off the per-track getStats monitor for apps that do not use the stats events"
7 changes: 6 additions & 1 deletion lib/src/track/track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ abstract class Track extends DisposableChangeNotifier with EventsEmittable<Track
await startCapture();

_active = true;
startMonitor();
if (statsMonitorEnabled) startMonitor();
return true;
}

Expand Down Expand Up @@ -167,6 +167,11 @@ abstract class Track extends DisposableChangeNotifier with EventsEmittable<Track
}
}

/// Whether tracks started from now on poll WebRTC stats every [monitorFrequency] ms.
/// Defaults to true. Disabling this stops automatic stats events and updates to
/// cached stats such as currentBitrate; it does not stop monitors already running.
static bool statsMonitorEnabled = true;

Timer? _monitorTimer;

@internal
Expand Down
26 changes: 26 additions & 0 deletions test/track/stats_monitor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ class _SlowTrack extends Track {
}

void main() {
tearDown(() => Track.statsMonitorEnabled = true);

testWidgets('statsMonitorEnabled decides whether tracks started afterwards poll', (tester) async {
await tester.pumpWidget(const SizedBox());
final running = _SlowTrack();
await running.start();
Track.statsMonitorEnabled = false;
final off = _SlowTrack();
await off.start();
await tester.pump(const Duration(seconds: 6));
expect(off.pending, isEmpty);
expect(running.pending.length, 1, reason: 'a track started before keeps its monitor');

Track.statsMonitorEnabled = true;
final on = _SlowTrack();
await on.start();
await tester.pump(const Duration(seconds: 2));
expect(on.pending.length, 1);
running.stopMonitor();
on.stopMonitor();
for (final pending in [...running.pending, ...on.pending]) {
pending.complete(true);
}
await tester.pump();
});

testWidgets('a slow stats request does not overlap, and monitoring resumes and stops', (tester) async {
await tester.pumpWidget(const SizedBox());
final track = _SlowTrack();
Expand Down
Loading