|
| 1 | +// Copyright 2024 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc; |
| 16 | +import 'package:meta/meta.dart'; |
| 17 | + |
| 18 | +import '../events.dart'; |
| 19 | +import '../logger.dart'; |
| 20 | +import '../types/other.dart'; |
| 21 | + |
| 22 | +typedef PendingTrackSubscriber = Future<bool> Function(PendingTrack entry); |
| 23 | +typedef TrackExceptionEmitter = void Function(TrackSubscriptionExceptionEvent event); |
| 24 | + |
| 25 | +/// Helper that queues subscriber tracks when participant metadata isn't ready yet. |
| 26 | +@internal |
| 27 | +class PendingTrackQueue { |
| 28 | + final int maxSize; |
| 29 | + final Duration ttl; |
| 30 | + final TrackExceptionEmitter emitException; |
| 31 | + |
| 32 | + // keyed by participant sid |
| 33 | + final Map<String, List<PendingTrack>> _pending = {}; |
| 34 | + |
| 35 | + PendingTrackQueue({ |
| 36 | + required this.ttl, |
| 37 | + required this.emitException, |
| 38 | + this.maxSize = 100, |
| 39 | + }); |
| 40 | + |
| 41 | + void enqueue({ |
| 42 | + required rtc.MediaStreamTrack track, |
| 43 | + required rtc.MediaStream stream, |
| 44 | + required rtc.RTCRtpReceiver? receiver, |
| 45 | + required String participantSid, |
| 46 | + required String trackSid, |
| 47 | + required ConnectionState connectionState, |
| 48 | + }) { |
| 49 | + // If we're already disconnected, drop immediately. |
| 50 | + if (connectionState == ConnectionState.disconnected) { |
| 51 | + final event = TrackSubscriptionExceptionEvent( |
| 52 | + participant: null, |
| 53 | + sid: trackSid, |
| 54 | + reason: TrackSubscribeFailReason.noParticipantFound, |
| 55 | + ); |
| 56 | + logger.warning('Dropping pending track while disconnected trackSid:$trackSid participantSid:$participantSid'); |
| 57 | + emitException(event); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + _removeExpired(); |
| 62 | + |
| 63 | + final totalPending = _pending.values.fold<int>(0, (sum, list) => sum + list.length); |
| 64 | + if (totalPending >= maxSize) { |
| 65 | + final event = TrackSubscriptionExceptionEvent( |
| 66 | + participant: null, |
| 67 | + sid: trackSid, |
| 68 | + reason: TrackSubscribeFailReason.noParticipantFound, |
| 69 | + ); |
| 70 | + logger.severe('Pending track queue full, dropping trackSid:$trackSid participantSid:$participantSid'); |
| 71 | + emitException(event); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + final expiresAt = DateTime.now().add(ttl); |
| 76 | + logger.fine('Queueing pending trackSid:$trackSid participantSid:$participantSid until metadata is ready'); |
| 77 | + final entry = PendingTrack( |
| 78 | + track: track, |
| 79 | + stream: stream, |
| 80 | + receiver: receiver, |
| 81 | + participantSid: participantSid, |
| 82 | + trackSid: trackSid, |
| 83 | + expiresAt: expiresAt, |
| 84 | + ); |
| 85 | + final list = _pending.putIfAbsent(participantSid, () => []); |
| 86 | + list.add(entry); |
| 87 | + } |
| 88 | + |
| 89 | + @internal |
| 90 | + Future<void> flush({ |
| 91 | + required bool isConnected, |
| 92 | + String? participantSid, |
| 93 | + required PendingTrackSubscriber subscriber, |
| 94 | + }) async { |
| 95 | + _removeExpired(); |
| 96 | + if (!isConnected) return; |
| 97 | + |
| 98 | + final Iterable<PendingTrack> source = participantSid != null |
| 99 | + ? List<PendingTrack>.from(_pending[participantSid] ?? const []) |
| 100 | + : _pending.values.expand((e) => e).toList(); |
| 101 | + |
| 102 | + for (final item in source) { |
| 103 | + final success = await subscriber(item); |
| 104 | + if (success) { |
| 105 | + _pending[item.participantSid]?.remove(item); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + void _removeExpired() { |
| 111 | + final now = DateTime.now(); |
| 112 | + _pending.forEach((sid, list) { |
| 113 | + final expired = list.where((p) => p.expiresAt.isBefore(now)).toList(); |
| 114 | + for (final item in expired) { |
| 115 | + list.remove(item); |
| 116 | + final event = TrackSubscriptionExceptionEvent( |
| 117 | + participant: null, |
| 118 | + sid: item.trackSid, |
| 119 | + reason: TrackSubscribeFailReason.noParticipantFound, |
| 120 | + ); |
| 121 | + logger.warning('Pending track expired waiting for participant metadata: $event'); |
| 122 | + emitException(event); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +@internal |
| 129 | +class PendingTrack { |
| 130 | + final rtc.MediaStreamTrack track; |
| 131 | + final rtc.MediaStream stream; |
| 132 | + final rtc.RTCRtpReceiver? receiver; |
| 133 | + final String participantSid; |
| 134 | + final String trackSid; |
| 135 | + final DateTime expiresAt; |
| 136 | + |
| 137 | + PendingTrack({ |
| 138 | + required this.track, |
| 139 | + required this.stream, |
| 140 | + required this.receiver, |
| 141 | + required this.participantSid, |
| 142 | + required this.trackSid, |
| 143 | + required this.expiresAt, |
| 144 | + }); |
| 145 | +} |
0 commit comments