spreadcast
Advanced tools
Comparing version 0.0.5 to 0.1.0
{ | ||
"name": "spreadcast", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Broadcast a WebRTC stream to many subscribers", | ||
@@ -5,0 +5,0 @@ "main": "src/spreadcast.js", |
@@ -10,2 +10,3 @@ var _ = require('eakwell'); | ||
var wsUrl = location.origin.replace(/^http/, 'ws'); | ||
var roomName; | ||
@@ -23,82 +24,89 @@ | ||
var wsUrl = location.origin.replace(/^http/, 'ws'); | ||
var socket = Client.socket || new WebSocket(wsUrl); | ||
// Client.socket = socket; | ||
var socket; | ||
var sockReady; | ||
var sockReady = new Promise(function(ok, fail) { | ||
socket.onopen = function(event) { | ||
console.log("Socket open"); | ||
ok(); | ||
}; | ||
}); | ||
var send = function(data) { | ||
sockReady.then(function() { | ||
socket.send(JSON.stringify(data)); | ||
var openSocket = function() { | ||
socket = new WebSocket(wsUrl); | ||
sockReady = new Promise(function(ok, fail) { | ||
socket.onopen = function(event) { | ||
console.log("Socket open"); | ||
ok(); | ||
}; | ||
}); | ||
}; | ||
socket.onerror = function(error) { | ||
console.log('WebSocket Error ' + error); | ||
}; | ||
socket.onerror = function(error) { | ||
console.log('WebSocket Error', error); | ||
}; | ||
socket.onclose = function(event) { | ||
console.log('WebSocket was closed ' + event); | ||
}; | ||
socket.onclose = function(event) { | ||
console.log('WebSocket was closed', event); | ||
_.defer(function() { | ||
openSocket(); | ||
if(senderPeer) sockReady.then(reconnect); | ||
}, 1000); | ||
}; | ||
socket.onmessage = function(event) { | ||
var data = JSON.parse(event.data); | ||
switch(data.type) { | ||
case 'offer': | ||
console.log("Got offer from receiver " + data.fromReceiver); | ||
var peer = new RTCPeerConnection(null); | ||
peer.onicecandidate = function(e) { | ||
if(e.candidate) { | ||
socket.onmessage = function(event) { | ||
var data = JSON.parse(event.data); | ||
switch(data.type) { | ||
case 'offer': | ||
console.log("Got offer from receiver " + data.fromReceiver); | ||
var peer = new RTCPeerConnection(null); | ||
peer.onicecandidate = function(e) { | ||
if(e.candidate) { | ||
send({ | ||
type: 'iceCandidate', | ||
roomName: roomName, | ||
candidate: e.candidate, | ||
to: data.fromReceiver | ||
}); | ||
} | ||
}; | ||
peer.addStream(localStream || remoteStream); | ||
// var stream = localStream || remoteStream; | ||
// stream.getTracks().forEach(track => peer.addTrack(track, stream)); | ||
peer.setRemoteDescription(data.offer); | ||
peer.createAnswer().then(function(desc) { | ||
peer.setLocalDescription(desc); | ||
send({ | ||
type: 'iceCandidate', | ||
type: 'answer', | ||
answer: desc, | ||
roomName: roomName, | ||
candidate: e.candidate, | ||
to: data.fromReceiver | ||
toReceiver: data.fromReceiver | ||
}); | ||
} | ||
}; | ||
peer.addStream(localStream || remoteStream); | ||
// var stream = localStream || remoteStream; | ||
// stream.getTracks().forEach(track => peer.addTrack(track, stream)); | ||
peer.setRemoteDescription(data.offer); | ||
peer.createAnswer().then(function(desc) { | ||
peer.setLocalDescription(desc); | ||
send({ | ||
type: 'answer', | ||
answer: desc, | ||
roomName: roomName, | ||
toReceiver: data.fromReceiver | ||
}); | ||
}); | ||
receiverPeers[data.fromReceiver] = peer; | ||
break; | ||
case 'answer': | ||
senderId = data.fromSender; | ||
console.log("Got answer from sender " + senderId); | ||
senderPeer.setRemoteDescription(data.answer); | ||
break; | ||
case 'iceCandidate': | ||
console.log("Got iceCandidate from " + data.from); | ||
var peer = (data.from == senderId ? senderPeer : receiverPeers[data.from]); | ||
if(peer) peer.addIceCandidate(data.candidate); | ||
break; | ||
receiverPeers[data.fromReceiver] = peer; | ||
break; | ||
case 'answer': | ||
senderId = data.fromSender; | ||
console.log("Got answer from sender " + senderId); | ||
senderPeer.setRemoteDescription(data.answer); | ||
break; | ||
case 'iceCandidate': | ||
console.log("Got iceCandidate from " + data.from); | ||
var peer = (data.from == senderId ? senderPeer : receiverPeers[data.from]); | ||
if(peer) peer.addIceCandidate(data.candidate); | ||
break; | ||
case 'stop': | ||
stop(); | ||
break; | ||
case 'stop': | ||
stop(); | ||
break; | ||
case 'reconnect': | ||
stop(); | ||
self.receive(roomName); | ||
break; | ||
} | ||
case 'reconnect': | ||
reconnect(); | ||
break; | ||
} | ||
}; | ||
}; | ||
openSocket(); | ||
var send = function(data) { | ||
sockReady.then(function() { | ||
socket.send(JSON.stringify(data)); | ||
}); | ||
}; | ||
var createVideoElement = function() { | ||
@@ -135,2 +143,3 @@ var video = document.createElement('video'); | ||
senderId = null; | ||
remoteStream = null; | ||
_.each(receiverPeers, function(peer) { | ||
@@ -140,6 +149,16 @@ peer.close(); | ||
receiverPeers = {}; | ||
if(localStream) localStream.getTracks().forEach(track => track.stop()); | ||
if(localStream) { | ||
_.each(localStream.getTracks(), function(track) { | ||
track.stop(); | ||
}); | ||
localStream = null; | ||
} | ||
if(remoteVideo) remoteVideo.parentElement.removeChild(remoteVideo); | ||
if(localVideo) localVideo.parentElement.removeChild(localVideo); | ||
}; | ||
var reconnect = function() { | ||
stop(); | ||
self.receive(roomName); | ||
}; | ||
@@ -210,2 +229,3 @@ self.publish = function(name, constraints) { | ||
stop(); | ||
roomName = null; | ||
}; | ||
@@ -212,0 +232,0 @@ }; |
@@ -7,3 +7,4 @@ var _ = require('eakwell'); | ||
var rooms = {}; | ||
var maxLeechers = 1; | ||
var pingInterval = 10 * 1000; | ||
var maxLeechers = options.maxLeechers || 1; | ||
@@ -27,2 +28,6 @@ var closeRoom = function(roomName) { | ||
var ping = setInterval(function() { | ||
socket.ping(null, null, true); | ||
}, pingInterval); | ||
socket.on('message', function incoming(msg) { | ||
@@ -57,3 +62,3 @@ var data = JSON.parse(msg); | ||
} else { | ||
// Find uncongested receiver to use as a proxy | ||
// Find uncongested receiver to use as a relay | ||
var freeReceivers = _.select(room.receivers, function(receiver) { | ||
@@ -115,2 +120,3 @@ return _.size(receiver.leechers) < maxLeechers; | ||
socket.on('close', function() { | ||
clearInterval(ping); | ||
_.each(rooms, function(room, name) { | ||
@@ -117,0 +123,0 @@ if(room.sender.id == sessionId) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13063
340