mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Use WsChannel to handle media webrtc handshake
This commit is contained in:
@@ -6,13 +6,12 @@ module.exports =
|
||||
class MediaConnection
|
||||
_.extend @prototype, require('event-emitter')
|
||||
|
||||
local: null
|
||||
remote: null
|
||||
channel: null
|
||||
connection: null
|
||||
stream: null
|
||||
isLeader: null
|
||||
|
||||
constructor: (@local, @remote, {@isLeader}={}) ->
|
||||
constructor: (@channel, {@isLeader}={}) ->
|
||||
|
||||
start: ->
|
||||
video = config.get('collaboration.enableVideo') ? true
|
||||
@@ -31,40 +30,44 @@ class MediaConnection
|
||||
onUserMediaAvailable: (stream) =>
|
||||
@connection = new webkitRTCPeerConnection(sessionUtils.getIceServers())
|
||||
@connection.addStream(stream)
|
||||
@remote.on 'changed', @onRemoteSignal
|
||||
@channel.on 'media-handshake', (event) =>
|
||||
try
|
||||
@onSignal(event)
|
||||
catch e
|
||||
console.error event
|
||||
throw e
|
||||
|
||||
@connection.onicecandidate = (event) =>
|
||||
return unless event.candidate?
|
||||
@local.set 'candidate', event.candidate
|
||||
@channel.send 'media-handshake', {candidate: event.candidate}
|
||||
|
||||
@connection.onaddstream = (event) =>
|
||||
@stream = event.stream
|
||||
@trigger 'stream-ready', @stream
|
||||
|
||||
@local.set 'ready', true unless @isLeader
|
||||
unless @isLeader
|
||||
@channel.send 'media-handshake', {ready: true}
|
||||
|
||||
onRemoteSignal: ({key, newValue}) =>
|
||||
switch key
|
||||
when 'ready'
|
||||
success = (description) =>
|
||||
@connection.setLocalDescription(description)
|
||||
@local.set 'description', description
|
||||
@connection.createOffer success, console.error
|
||||
onSignal: (event) =>
|
||||
if value = event.ready
|
||||
success = (description) =>
|
||||
@connection.setLocalDescription(description)
|
||||
@channel.send 'media-handshake', {description}
|
||||
@connection.createOffer success, console.error
|
||||
|
||||
when 'description'
|
||||
remoteDescription = newValue.toObject()
|
||||
sessionDescription = new RTCSessionDescription(remoteDescription)
|
||||
@connection.setRemoteDescription(sessionDescription)
|
||||
else if value = event.description
|
||||
remoteDescription = value
|
||||
sessionDescription = new RTCSessionDescription(remoteDescription)
|
||||
@connection.setRemoteDescription(sessionDescription)
|
||||
|
||||
if not @isLeader
|
||||
success = (localDescription) =>
|
||||
@connection.setLocalDescription(localDescription)
|
||||
@local.set('description', localDescription)
|
||||
@connection.createAnswer success, console.error
|
||||
if not @isLeader
|
||||
success = (localDescription) =>
|
||||
@connection.setLocalDescription(localDescription)
|
||||
@channel.send 'media-handshake', {description: localDescription}
|
||||
@connection.createAnswer success, console.error
|
||||
|
||||
when 'candidate'
|
||||
remoteCandidate = new RTCIceCandidate newValue.toObject()
|
||||
@connection.addIceCandidate(remoteCandidate)
|
||||
|
||||
else
|
||||
throw new Error("Unknown remote key '#{key}'")
|
||||
else if value = event.candidate
|
||||
remoteCandidate = new RTCIceCandidate value
|
||||
@connection.addIceCandidate(remoteCandidate)
|
||||
else
|
||||
throw new Error("Unknown remote key '#{event}'")
|
||||
|
||||
@@ -80,12 +80,7 @@ class Session
|
||||
@trigger 'started', @getParticipants()
|
||||
|
||||
createMediaConnection: ->
|
||||
guest = @doc.get('collaborationState.guest')
|
||||
host = @doc.get('collaborationState.host')
|
||||
if @isLeader()
|
||||
new MediaConnection(guest, host, isLeader: true)
|
||||
else
|
||||
new MediaConnection(host, guest)
|
||||
new MediaConnection(@channel, isLeader: @isLeader())
|
||||
|
||||
waitForStream: (callback) ->
|
||||
@mediaConnection.waitForStream callback
|
||||
|
||||
Reference in New Issue
Block a user