docs/call-establishment.md
Tinode supports peer to peer video calls over WebRTC. The diagram below illustrates a call establishment flow between two users Alice and Bob. The flow is conceptually similar to SIP, but uses native Tinode messages for transport.
Notes:
{note} messages with the call's topic and seq fields set.{info} messages on me topic with the call's src (call topic) and seq fields set (and/or in data push notifications).The flow may be broken down into 4 phases:
sequenceDiagram
participant A as Alice
participant S as Tinode Server
participant B as Bob
rect rgb(212, 242, 255)
Note over A: Alice initiates a call
A->>S: 1. {pub head:webrtc=started}
S->>A: 2. {ctrl params:seq=123}
S->>+B: 3. {info seq=123 event=invite}
S-->>B: or {data seq=123 head:webrtc=started}
push notification
B->>-S: 4. {note seq=123 event=ringing}
S->>A: 5. {info seq=123 event=ringing}
end
Note over S: Bob's client ringing
waiting for Bob to accept
rect rgb(212, 242, 255)
Note over B: Bob accepts the call
B->>S: 6. {note seq=123 event=accept}
S->>A: 7a. {info seq=123 event=accept}
S->>B: 7b. {info seq=123 event=accept}
S-->>B: {data seq=124 head:webrtc=accepted,replace=123}
S-->>A: {data seq=124 head:webrtc=accepted,replace=123}
end
Note over S: Call accepted, peer metadata exchange
A->>S: 8. {note seq=123 event=offer}
S->>+B: 9. {info seq=123 event=offer}
B->>-S: 10. {note seq=123 event=answer}
S->>A: 11. {info seq=123 event=answer}
rect rgb(212, 242, 255)
Note over S: ICE candidate exchange
loop
A->>S: 12. {note seq=123 event=ice-candidate}
S->>B: 13. {info seq=123 event=ice-candidate}
B->>S: 14. {note seq=123 event=ice-candidate}
S->>A: 15. {info seq=123 event=ice-candidate}
end
end
Note over S: Call established
conversation in progress
rect rgb(212, 242, 255)
Note over S: Call termination
alt
A->>S: 16a. {note seq=123 event=hang-up}
B->>S: 16b. {note seq=123 event=hang-up}
end
alt
S->>B: 17a. {info seq=123 event=hang-up}
S->>A: 17b. {info seq=123 event=hang-up}
end
S-->>B: {data seq=125 head:webrtc=finished,replace=123}
S-->>A: {data seq=125 head:webrtc=finished,replace=123}
end
Alice initiates a call by posting a video call message (with webrtc=started header){ctrl} message containing the seq id of the call.invite event message to Bob (all clients).webrtc=started field to Bob.Bob displays the incoming call UI.Bob replies with a ringing event.ringing event to Alice. The latter now plays the ringing sound.Alice may receive multiple ringing events as each separate instance of Bob acknowldges receipt of the call invitation separately.Alice and server will wait for up to a server configured timeout for Bob to accept the call and then hang up.Bob accepts the call by sending an accept event.accept event to Alice and Bob.webrtc=accepted header.Bob's sessions except the one that accepted the call may silently dismiss the incoming call UI.Alice sends an offer event containing an SDP payload.offer to Bob.offer, Bob replies with an answer event containing an SDP payload.Bob's answer event to Alice.Steps 12-15 are Ice candidate exchange between Alice and Bob.
At this point the call is officially established. Alice and Bob can see and hear each other.
Alice sends a hang-up event to server.hang-up event to Bob.Additionally, the server broadcasts a replacement for the call data message with webrtc=finished header.
Push notifications for the replacement message are sent as well.