@@ -95,6 +95,7 @@ class CallBloc extends Bloc<CallEvent, CallState> with WidgetsBindingObserver im
9595 Timer ? _presenceInfoSyncTimer;
9696
9797 late final PeerConnectionManager _peerConnectionManager;
98+ late final RenegotiationHandler _renegotiationHandler;
9899
99100 final _callkeepSound = WebtritCallkeepSound ();
100101
@@ -129,6 +130,7 @@ class CallBloc extends Bloc<CallEvent, CallState> with WidgetsBindingObserver im
129130 }) : super (const CallState ()) {
130131 _signalingClientFactory = signalingClientFactory;
131132 _peerConnectionManager = peerConnectionManager;
133+ _renegotiationHandler = RenegotiationHandler (callErrorReporter: callErrorReporter, sdpMunger: sdpMunger);
132134
133135 on < CallStarted > (_onCallStarted, transformer: sequential ());
134136 on < _AppLifecycleStateChanged > (_onAppLifecycleStateChanged, transformer: sequential ());
@@ -2782,45 +2784,19 @@ class CallBloc extends Bloc<CallEvent, CallState> with WidgetsBindingObserver im
27822784 onIceCandidate: (candidate) => add (_PeerConnectionEvent .iceCandidateIdentified (callId, candidate)),
27832785 onAddStream: (stream) => add (_PeerConnectionEvent .streamAdded (callId, stream)),
27842786 onRemoveStream: (stream) => add (_PeerConnectionEvent .streamRemoved (callId, stream)),
2785- onRenegotiationNeeded: (pc) => _handleRenegotiationNeeded (callId, lineId, pc),
2787+ onRenegotiationNeeded: (pc) => _renegotiationHandler.handle (callId, lineId, pc, (callId, lineId, jsep) async {
2788+ final updateRequest = UpdateRequest (
2789+ transaction: WebtritSignalingClient .generateTransactionId (),
2790+ line: lineId,
2791+ callId: callId,
2792+ jsep: jsep.toMap (),
2793+ );
2794+ await _signalingClient? .execute (updateRequest);
2795+ }),
27862796 ),
27872797 );
27882798 }
27892799
2790- Future <void > _handleRenegotiationNeeded (String callId, int ? lineId, RTCPeerConnection peerConnection) async {
2791- final stateBeforeOffer = peerConnection.signalingState;
2792- _logger.fine (() => 'onRenegotiationNeeded signalingState: $stateBeforeOffer ' );
2793- if (stateBeforeOffer == RTCSignalingState .RTCSignalingStateStable ) {
2794- final localDescription = await peerConnection.createOffer ({});
2795- sdpMunger? .apply (localDescription);
2796-
2797- final stateAfterOffer = peerConnection.signalingState;
2798- if (stateAfterOffer != RTCSignalingState .RTCSignalingStateStable ) {
2799- _logger.fine (
2800- () =>
2801- 'onRenegotiationNeeded: state changed to $stateAfterOffer after createOffer, skipping setLocalDescription' ,
2802- );
2803- return ;
2804- }
2805-
2806- // According to RFC 8829 5.6 (https://datatracker.ietf.org/doc/html/rfc8829#section-5.6),
2807- // localDescription should be set before sending the offer to transition into have-local-offer state.
2808- await peerConnection.setLocalDescription (localDescription);
2809-
2810- try {
2811- final updateRequest = UpdateRequest (
2812- transaction: WebtritSignalingClient .generateTransactionId (),
2813- line: lineId,
2814- callId: callId,
2815- jsep: localDescription.toMap (),
2816- );
2817- await _signalingClient? .execute (updateRequest);
2818- } catch (e, s) {
2819- callErrorReporter.handle (e, s, '_createPeerConnection:onRenegotiationNeeded error' );
2820- }
2821- }
2822- }
2823-
28242800 void _addToRecents (ActiveCall activeCall) {
28252801 final number = activeCall.handle.value;
28262802 final username = activeCall.displayName;
0 commit comments