Skip to content

Commit b34cb1b

Browse files
authored
fix call preference initialization during ringing (#921)
1 parent a625b3b commit b34cb1b

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

dogfooding/lib/screens/call_screen.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ class _CallScreenState extends State<CallScreen> {
143143
},
144144
onCallDisconnected: (disconnectedProperties) {
145145
final reason = disconnectedProperties.reason;
146+
147+
Navigator.of(context).pop();
148+
146149
if (reason is DisconnectReasonCancelled ||
147150
reason is DisconnectReasonEnded ||
148151
reason is DisconnectReasonLastParticipantLeft) {
149-
Navigator.of(context).pop();
150152
showFeedbackDialog(context, call: widget.call);
151153
}
152154
},

packages/stream_video/lib/src/call/call.dart

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ class Call {
189189
return Call._(
190190
coordinatorClient: coordinatorClient,
191191
streamVideo: streamVideo,
192-
preferences: finalCallPreferences,
193192
stateManager: stateManager,
194193
credentials: credentials,
195194
retryPolicy: finalRetryPolicy,
@@ -201,7 +200,6 @@ class Call {
201200
Call._({
202201
required CoordinatorClient coordinatorClient,
203202
required StreamVideo streamVideo,
204-
required CallPreferences preferences,
205203
required CallStateNotifier stateManager,
206204
required PermissionsManager permissionManager,
207205
required RetryPolicy retryPolicy,
@@ -217,7 +215,6 @@ class Call {
217215
_permissionsManager = permissionManager,
218216
_coordinatorClient = coordinatorClient,
219217
_streamVideo = streamVideo,
220-
_preferences = preferences,
221218
_retryPolicy = retryPolicy,
222219
_credentials = credentials,
223220
dynascaleManager = DynascaleManager(stateManager: stateManager) {
@@ -239,7 +236,6 @@ class Call {
239236
final CoordinatorClient _coordinatorClient;
240237
final StreamVideo _streamVideo;
241238
final RetryPolicy _retryPolicy;
242-
final CallPreferences _preferences;
243239
final CallSessionFactory _sessionFactory;
244240
final CallStateNotifier _stateManager;
245241
final PermissionsManager _permissionsManager;
@@ -468,7 +464,8 @@ class Call {
468464
return _handleClosedCaptionEvent(event);
469465
case StreamCallReactionEvent _:
470466
_reactionTimers.add(
471-
Timer(_preferences.reactionAutoDismissTime, () {
467+
Timer(_stateManager.callState.preferences.reactionAutoDismissTime,
468+
() {
472469
_stateManager.resetCallReaction(event.user.id);
473470
}),
474471
);
@@ -488,6 +485,11 @@ class Call {
488485
}
489486
}
490487

488+
void updateCallPreferences(CallPreferences preferences) {
489+
_logger.i(() => '[updateCallPreferences] $preferences');
490+
_stateManager.updateCallPreferences(preferences);
491+
}
492+
491493
/// Accepts the incoming call.
492494
Future<Result<None>> accept() async {
493495
final state = this.state.value;
@@ -592,7 +594,7 @@ class Call {
592594

593595
final status = await state.firstWhere(
594596
(it) => it.status is CallStatusConnected,
595-
timeLimit: _preferences.connectTimeout,
597+
timeLimit: _stateManager.callState.preferences.connectTimeout,
596598
);
597599

598600
if (status is! CallStatusConnected) {
@@ -710,7 +712,8 @@ class Call {
710712
});
711713
}
712714
},
713-
clientPublishOptions: _preferences.clientPublishOptions,
715+
clientPublishOptions:
716+
_stateManager.callState.preferences.clientPublishOptions,
714717
);
715718

716719
dynascaleManager.init(
@@ -1004,7 +1007,10 @@ class Call {
10041007
rtcManager: session.rtcManager!,
10051008
stateManager: _stateManager,
10061009
)
1007-
.run(interval: _preferences.callStatsReportingInterval)
1010+
.run(
1011+
interval:
1012+
_stateManager.callState.preferences.callStatsReportingInterval,
1013+
)
10081014
.listen((stats) {
10091015
_stats.emit(stats);
10101016
}),
@@ -1044,7 +1050,7 @@ class Call {
10441050
if (callParticipants.length == 1 &&
10451051
callParticipants.first.userId == _streamVideo.currentUser.id &&
10461052
state.value.isRingingFlow &&
1047-
_stateManager.callPreferences.dropIfAloneInRingingFlow) {
1053+
_stateManager.callState.preferences.dropIfAloneInRingingFlow) {
10481054
await leave();
10491055
}
10501056
} else if (sfuEvent is SfuHealthCheckResponseEvent) {
@@ -1585,9 +1591,10 @@ class Call {
15851591

15861592
final newQueue = [...queue, currentCaption];
15871593

1588-
final visibilityDurationMs =
1589-
_preferences.closedCaptionsVisibilityDurationMs;
1590-
final visibileCaptions = _preferences.closedCaptionsVisibleCaptions;
1594+
final visibilityDurationMs = _stateManager
1595+
.callState.preferences.closedCaptionsVisibilityDurationMs;
1596+
final visibileCaptions =
1597+
_stateManager.callState.preferences.closedCaptionsVisibleCaptions;
15911598

15921599
try {
15931600
// schedule the removal of the closed caption after the retention time
@@ -2572,10 +2579,10 @@ CallStateNotifier _makeStateManager(
25722579

25732580
return CallStateNotifier(
25742581
CallState(
2582+
preferences: callPreferences,
25752583
currentUserId: currentUserId,
25762584
callCid: callCid,
25772585
),
2578-
callPreferences,
25792586
);
25802587
}
25812588

packages/stream_video/lib/src/call/state/call_state_notifier.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:state_notifier/state_notifier.dart';
22

33
import '../../call_state.dart';
44
import '../../logger/impl/tagged_logger.dart';
5-
import '../../models/call_preferences.dart';
5+
import '../../models/models.dart';
66
import '../../state_emitter.dart';
77
import 'mixins/state_call_actions_mixin.dart';
88
import 'mixins/state_coordinator_mixin.dart';
@@ -19,17 +19,13 @@ class CallStateNotifier extends StateNotifier<CallState>
1919
StateRtcMixin,
2020
StateSfuMixin,
2121
StateCallActionsMixin {
22-
CallStateNotifier(CallState initialState, this.callPreferences)
23-
: super(initialState) {
22+
CallStateNotifier(CallState initialState) : super(initialState) {
2423
callStateStream =
2524
MutableStateEmitterImpl<CallState>(initialState, sync: true);
2625
}
2726

2827
final _logger = taggedLogger(tag: 'CallStateNotifier');
2928

30-
@override
31-
final CallPreferences callPreferences;
32-
3329
late final MutableStateEmitterImpl<CallState> callStateStream;
3430
CallState get callState => callStateStream.value;
3531

@@ -43,6 +39,11 @@ class CallStateNotifier extends StateNotifier<CallState>
4339
callStateStream.value = value;
4440
}
4541

42+
void updateCallPreferences(CallPreferences preferences) {
43+
_logger.v(() => '[updateCallPreferences] $preferences');
44+
state = state.copyWith(preferences: preferences);
45+
}
46+
4647
@override
4748
void dispose() {
4849
super.dispose();

packages/stream_video/lib/src/call/state/mixins/state_sfu_mixin.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import '../../../call_state.dart';
55
import '../../../logger/impl/tagged_logger.dart';
66
import '../../../models/call_participant_pin.dart';
77
import '../../../models/call_participant_state.dart';
8-
import '../../../models/call_preferences.dart';
98
import '../../../models/call_track_state.dart';
109
import '../../../sfu/data/events/sfu_events.dart';
1110
import '../../../sfu/data/models/sfu_pin.dart';
@@ -14,8 +13,6 @@ import '../../../sfu/sfu_extensions.dart';
1413
final _logger = taggedLogger(tag: 'SV:CoordNotifier');
1514

1615
mixin StateSfuMixin on StateNotifier<CallState> {
17-
CallPreferences get callPreferences;
18-
1916
void sfuParticipantLeft(
2017
SfuParticipantLeftEvent event,
2118
) {

packages/stream_video/lib/src/call_state.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ class CallState extends Equatable {
1212
factory CallState({
1313
required String currentUserId,
1414
required StreamCallCid callCid,
15+
required CallPreferences preferences,
1516
}) {
1617
return CallState._(
18+
preferences: preferences,
1719
currentUserId: currentUserId,
1820
callCid: callCid,
1921
createdByUserId: '',
@@ -55,6 +57,7 @@ class CallState extends Equatable {
5557
}
5658

5759
const CallState._({
60+
required this.preferences,
5861
required this.currentUserId,
5962
required this.callCid,
6063
required this.createdByUserId,
@@ -94,6 +97,7 @@ class CallState extends Equatable {
9497
required this.custom,
9598
});
9699

100+
final CallPreferences preferences;
97101
final String currentUserId;
98102
final StreamCallCid callCid;
99103
final String createdByUserId;
@@ -151,6 +155,7 @@ class CallState extends Equatable {
151155
/// Returns a copy of this [CallState] with the given fields replaced
152156
/// with the new values.
153157
CallState copyWith({
158+
CallPreferences? preferences,
154159
String? currentUserId,
155160
StreamCallCid? callCid,
156161
String? createdByUserId,
@@ -190,6 +195,7 @@ class CallState extends Equatable {
190195
Map<String, Object>? custom,
191196
}) {
192197
return CallState._(
198+
preferences: preferences ?? this.preferences,
193199
currentUserId: currentUserId ?? this.currentUserId,
194200
callCid: callCid ?? this.callCid,
195201
createdByUserId: createdByUserId ?? this.createdByUserId,

packages/stream_video/lib/src/stream_video.dart

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ class StreamVideo extends Disposable {
419419
event.data.ringing) {
420420
_logger.v(() => '[onCoordinatorEvent] onCallRinging: ${event.data}');
421421
final call = _makeCallFromRinging(data: event.data);
422-
423422
_state.incomingCall.value = call;
424423
} else if (event is CoordinatorConnectedEvent) {
425424
_logger.i(() => '[onCoordinatorEvent] connected ${event.userId}');
@@ -505,7 +504,7 @@ class StreamVideo extends Disposable {
505504
streamVideo: this,
506505
retryPolicy: _options.retryPolicy,
507506
sdpPolicy: _options.sdpPolicy,
508-
preferences: preferences,
507+
preferences: preferences ?? _options.defaultCallPreferences,
509508
);
510509
}
511510

@@ -519,7 +518,7 @@ class StreamVideo extends Disposable {
519518
streamVideo: this,
520519
retryPolicy: _options.retryPolicy,
521520
sdpPolicy: _options.sdpPolicy,
522-
preferences: preferences,
521+
preferences: preferences ?? _options.defaultCallPreferences,
523522
);
524523
}
525524

@@ -695,19 +694,26 @@ class StreamVideo extends Disposable {
695694
final cid = event.data.callCid;
696695
if (uuid == null || cid == null) return;
697696

698-
final call = await consumeIncomingCall(
697+
final consumeResult = await consumeIncomingCall(
699698
uuid: uuid,
700699
cid: cid,
701700
preferences: callPreferences,
702701
);
703-
final callToJoin = call.getDataOrNull();
702+
703+
if (consumeResult.isFailure) {
704+
_logger.w(
705+
() => '[onCallAccept] error consuming incoming call}',
706+
);
707+
return;
708+
}
709+
710+
final callToJoin = consumeResult.getDataOrNull();
704711
if (callToJoin == null) return;
705712

706713
final acceptResult = await callToJoin.accept();
707714

708-
// Return if cannot accept call
709715
if (acceptResult.isFailure) {
710-
_logger.d(() => '[onCallAccept] error accepting call: $call');
716+
_logger.d(() => '[onCallAccept] error accepting call: $callToJoin');
711717
return;
712718
}
713719

@@ -925,8 +931,13 @@ class StreamVideo extends Disposable {
925931
);
926932
}
927933

934+
// If call was already created by consuming ringing event, use the same instance.
928935
if (_state.incomingCall.valueOrNull?.callCid.value == cid) {
929-
return Result.success(_state.incomingCall.value!);
936+
final call = _state.incomingCall.value!;
937+
if (preferences != null) {
938+
call.updateCallPreferences(preferences);
939+
}
940+
return Result.success(call);
930941
}
931942

932943
final callCid = StreamCallCid(cid: cid);
@@ -941,7 +952,7 @@ class StreamVideo extends Disposable {
941952
ringing: true,
942953
metadata: callResult.data.metadata,
943954
),
944-
preferences: preferences,
955+
preferences: preferences ?? _options.defaultCallPreferences,
945956
);
946957

947958
return Result.success(call);
@@ -1069,6 +1080,7 @@ class StreamVideoOptions {
10691080
this.coordinatorWsUrl = _defaultCoordinatorWsUrl,
10701081
this.latencySettings = const LatencySettings(),
10711082
this.retryPolicy = const RetryPolicy(),
1083+
this.defaultCallPreferences,
10721084
this.sdpPolicy = const SdpPolicy(spdEditingEnabled: false),
10731085
this.audioProcessor,
10741086
this.logPriority = Priority.none,
@@ -1086,6 +1098,7 @@ class StreamVideoOptions {
10861098

10871099
/// Returns the current [RetryPolicy].
10881100
final RetryPolicy retryPolicy;
1101+
final CallPreferences? defaultCallPreferences;
10891102

10901103
/// Returns the current [SdpPolicy].
10911104
final SdpPolicy sdpPolicy;

0 commit comments

Comments
 (0)