Skip to content

Commit

Permalink
Add TURN support
Browse files Browse the repository at this point in the history
The client receives a turn login on room join if the server supports TURN.
It first tries a connection via stun. If that fails, it tries again with
turn using the received login.
  • Loading branch information
farao authored and janlelis committed Jul 11, 2020
1 parent e6f6392 commit 4239dfe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
24 changes: 20 additions & 4 deletions coffee/remote_peer.coffee
Expand Up @@ -29,6 +29,7 @@ class palava.RemotePeer extends palava.Peer
@setupPeerConnection(offers)
@setupDistributor()

@offers = offers
if offers
@sendOffer()

Expand All @@ -52,11 +53,11 @@ class palava.RemotePeer extends palava.Peer
options = []
if @room.options.stun
options.push({urls: [@room.options.stun]})
if @room.options.turn
if @room.options.turn && @turnCredentials
options.push
urls: [@room.options.turn.url]
username: @room.options.turn.username
credential: @room.options.turn.password
urls: [@room.options.turn]
username: @turnCredentials.user
credential: @turnCredentials.password
{iceServers: options}

# Sets up the peer connection and its events
Expand Down Expand Up @@ -132,6 +133,21 @@ class palava.RemotePeer extends palava.Peer

@peerConnection

# Check if turn was already tried as (last) connection option
#
# @return [Boolean] true if turn was tried by using the tryTurn function
#
hasTriedTurn: => !!@turnCredentials

# Check if turn was already tried as (last) connection option
#
# @return [Object] true if turn was tried by using the tryTurn function
#
tryTurn: (credentials) =>
@closePeerConnection()
@turnCredentials = credentials
@setupPeerConnection(@offers)

# Sets up the distributor connecting to the participant
#
# @nodoc
Expand Down
2 changes: 1 addition & 1 deletion coffee/room.coffee
Expand Up @@ -58,7 +58,7 @@ class palava.Room extends @EventEmitter
for peer in msg.peers
offers = !palava.browser.isChrome()
newPeer = new palava.RemotePeer(peer.peer_id, peer.status, @, offers)
@emit "joined", @
@emit "joined", msg.own_id, msg.turn_user, msg.turn_password

@distributor.on 'new_peer', (msg) =>
offers = msg.status.user_agent == 'chrome'
Expand Down
12 changes: 10 additions & 2 deletions coffee/session.coffee
Expand Up @@ -158,7 +158,11 @@ class palava.Session extends @EventEmitter
@tearDown(true)
@emit 'room_join_error', @room
@room.on 'full', => @emit 'room_full', @room
@room.on 'joined', => @emit 'room_joined', @room
@room.on 'joined', (u, p) =>
@turnCredentials =
user: tu
password: tpw
@emit 'room_joined', @room
@room.on 'left', => @emit 'room_left', @room
@room.on 'peer_joined', (p) => @emit 'peer_joined', p
@room.on 'peer_offer', (p) => @emit 'peer_offer', p
Expand All @@ -168,7 +172,11 @@ class palava.Session extends @EventEmitter
@room.on 'peer_stream_removed', (p) => @emit 'peer_stream_removed', p
@room.on 'peer_connection_pending', (p) => @emit 'peer_connection_pending', p
@room.on 'peer_connection_established', (p) => @emit 'peer_connection_established', p
@room.on 'peer_connection_failed', (p) => @emit 'peer_connection_failed', p
@room.on 'peer_connection_failed', (p) =>
if !p.hasTriedTurn && @turnCredentials
p.tryTurn @turnCredentials
else
@emit 'peer_connection_failed', p
@room.on 'peer_connection_disconnected', (p) => @emit 'peer_connection_disconnected', p
@room.on 'peer_connection_closed', (p) => @emit 'peer_connection_closed', p
@room.on 'peer_left', (p) => @emit 'peer_left', p
Expand Down

0 comments on commit 4239dfe

Please sign in to comment.