Use WsChannel to handle media webrtc handshake

This commit is contained in:
probablycorey
2013-07-26 08:54:07 -07:00
parent 8493ef092e
commit c7321a795b
2 changed files with 32 additions and 34 deletions

View File

@@ -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}'")

View File

@@ -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