mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Handles peer ready & peer destroyed streams events
This commit is contained in:
		@@ -86,6 +86,22 @@ class UserLeftCallEvent {
 | 
			
		||||
  UserLeftCallEvent(this.callID, this.userID);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Call peer ready event
 | 
			
		||||
class CallPeerReadyEvent {
 | 
			
		||||
  final int callID;
 | 
			
		||||
  final int peerID;
 | 
			
		||||
 | 
			
		||||
  CallPeerReadyEvent(this.callID, this.peerID);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Call peer interrupted streaming event
 | 
			
		||||
class CallPeerInterruptedStreamingEvent {
 | 
			
		||||
  final int callID;
 | 
			
		||||
  final int peerID;
 | 
			
		||||
 | 
			
		||||
  CallPeerInterruptedStreamingEvent(this.callID, this.peerID);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Call closed event
 | 
			
		||||
class CallClosedEvent {
 | 
			
		||||
  final int callID;
 | 
			
		||||
 
 | 
			
		||||
@@ -26,7 +26,6 @@ class WebSocketHelper {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Get WebSocket access token
 | 
			
		||||
  //TODO : Handles the case user tokens were destroyed (auto sign-out user)
 | 
			
		||||
  static Future<String> _getWsToken() async =>
 | 
			
		||||
      (await APIRequest(uri: "ws/token", needLogin: true).exec())
 | 
			
		||||
          .assertOk()
 | 
			
		||||
@@ -173,7 +172,19 @@ class WebSocketHelper {
 | 
			
		||||
            UserLeftCallEvent(msg.data["callID"], msg.data["userID"]));
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
      // The call has been close
 | 
			
		||||
      // Call peer ready event
 | 
			
		||||
      case "call_peer_ready":
 | 
			
		||||
        EventsHelper.emit(
 | 
			
		||||
            CallPeerReadyEvent(msg.data["callID"], msg.data["peerID"]));
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
      // Call peer interrupted streaming event
 | 
			
		||||
      case "call_peer_interrupted_streaming":
 | 
			
		||||
        EventsHelper.emit(CallPeerInterruptedStreamingEvent(
 | 
			
		||||
            msg.data["callID"], msg.data["peerID"]));
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
      // The call has been closed
 | 
			
		||||
      case "call_closed":
 | 
			
		||||
        EventsHelper.emit(CallClosedEvent(msg.data));
 | 
			
		||||
        break;
 | 
			
		||||
 
 | 
			
		||||
@@ -11,4 +11,7 @@ class CallMembersList extends AbstractList<CallMember> {
 | 
			
		||||
 | 
			
		||||
  /// Remove a specific member from this list
 | 
			
		||||
  void removeUser(int userID) => this.removeWhere((f) => f.id == userID);
 | 
			
		||||
 | 
			
		||||
  /// Get the connection of a specific user
 | 
			
		||||
  CallMember getUser(int userID) => this.firstWhere((f) => f.id == userID);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,9 +8,9 @@ enum MemberStatus { JOINED, READY }
 | 
			
		||||
 | 
			
		||||
class CallMember {
 | 
			
		||||
  final int id;
 | 
			
		||||
  final MemberStatus status;
 | 
			
		||||
  MemberStatus status;
 | 
			
		||||
 | 
			
		||||
  const CallMember({
 | 
			
		||||
  CallMember({
 | 
			
		||||
    @required this.id,
 | 
			
		||||
    this.status = MemberStatus.JOINED,
 | 
			
		||||
  })  : assert(id != null),
 | 
			
		||||
 
 | 
			
		||||
@@ -82,6 +82,7 @@ class _CallScreenState extends SafeState<CallScreen> {
 | 
			
		||||
 | 
			
		||||
      // Register to events
 | 
			
		||||
      this.listenChangeState<UserJoinedCallEvent>((e) {
 | 
			
		||||
        // TODO : get user information if required
 | 
			
		||||
        if (e.callID == convID) _membersList.add(CallMember(id: e.userID));
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
@@ -89,6 +90,14 @@ class _CallScreenState extends SafeState<CallScreen> {
 | 
			
		||||
        if (e.callID == convID) _removeMember(e.userID);
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      this.listen<CallPeerReadyEvent>((e) {
 | 
			
		||||
        if (e.callID == convID) _memberReady(e.peerID);
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      this.listen<CallPeerInterruptedStreamingEvent>((e) {
 | 
			
		||||
        if (e.callID == convID) _removeRemotePeerConnection(e.peerID);
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      this.listen<CallClosedEvent>((e) {
 | 
			
		||||
        if (e.callID == convID) _leaveCall(needConfirm: false);
 | 
			
		||||
      });
 | 
			
		||||
@@ -98,7 +107,7 @@ class _CallScreenState extends SafeState<CallScreen> {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Do clean up operations when call screen is destroyed
 | 
			
		||||
  /// Do clean up operations when call screen is destroyed
 | 
			
		||||
  void _endCall() async {
 | 
			
		||||
    try {
 | 
			
		||||
      // Leave the call
 | 
			
		||||
@@ -108,7 +117,7 @@ class _CallScreenState extends SafeState<CallScreen> {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Make us leave the call
 | 
			
		||||
  /// Make us leave the call
 | 
			
		||||
  void _leaveCall({bool needConfirm = true}) async {
 | 
			
		||||
    if (needConfirm &&
 | 
			
		||||
        !await showConfirmDialog(
 | 
			
		||||
@@ -118,7 +127,23 @@ class _CallScreenState extends SafeState<CallScreen> {
 | 
			
		||||
    MainController.of(context).popPage();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Call this when a user started to stream media
 | 
			
		||||
  void _memberReady(int memberID) {
 | 
			
		||||
    _membersList.getUser(memberID).status = MemberStatus.READY;
 | 
			
		||||
 | 
			
		||||
    setState((){});
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Call this when a user has interrupted streaming
 | 
			
		||||
  void _removeRemotePeerConnection(int memberID) {
 | 
			
		||||
    _membersList.getUser(memberID).status = MemberStatus.JOINED;
 | 
			
		||||
    setState((){});
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Call this when a members has completely left the call
 | 
			
		||||
  void _removeMember(int memberID) {
 | 
			
		||||
    _removeRemotePeerConnection(memberID);
 | 
			
		||||
 | 
			
		||||
    _membersList.removeUser(memberID);
 | 
			
		||||
    setState(() {});
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user