spreadcast
Advanced tools
Comparing version 0.2.1 to 0.2.2
{ | ||
"name": "spreadcast", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Broadcast WebRTC streams to many receivers", | ||
@@ -27,3 +27,3 @@ "main": "src/server.js", | ||
"bufferutil": "^1.3.0", | ||
"eakwell": "^0.2.5", | ||
"eakwell": "^0.2.6", | ||
"freeice": "^2.2.0", | ||
@@ -30,0 +30,0 @@ "utf-8-validate": "^2.0.0", |
@@ -56,5 +56,6 @@ # spreadcast.js | ||
} | ||
}, function(error, video) { | ||
if(error) return console.error(error); | ||
}).then(function(video) { | ||
document.body.appendChild(video); | ||
}).catch(function(error) { | ||
console.error(error); | ||
}); | ||
@@ -61,0 +62,0 @@ ``` |
@@ -5,2 +5,3 @@ var freeice = require('freeice'); | ||
var Socket = require('./socket.js'); | ||
var Storage = require('./storage.js'); | ||
@@ -10,4 +11,2 @@ var Broadcast = function(broadcastName, roomName, keepVideos) { | ||
Socket.init(); | ||
var stream; | ||
@@ -21,4 +20,5 @@ var video; | ||
var shutdown = false; | ||
var stopRecord = null; | ||
var socket = new Socket.Socket(); | ||
var socket = new Socket(); | ||
@@ -159,4 +159,28 @@ socket.onerror = function(error) { | ||
socket.close(); | ||
if(self.onStop) self.onStop(); | ||
self.onStop && self.onStop(); | ||
stopRecord && stopRecord(); | ||
}; | ||
var record = function(cb, chunksize) { | ||
chunksize = chunksize || Infinity; | ||
var recordedBlobs = []; | ||
var mediaRecorder = new MediaRecorder(stream); | ||
var buffersize = 0; | ||
mediaRecorder.ondataavailable = function(e) { | ||
if(e.data && e.data.size > 0) { | ||
if(buffersize + e.data.size > chunksize && recordedBlobs.length) { | ||
cb && cb(new Blob(recordedBlobs, {type: 'video/webm'})); | ||
recordedBlobs = []; | ||
buffersize = 0; | ||
} | ||
recordedBlobs.push(e.data); | ||
buffersize += e.data.size; | ||
} | ||
}; | ||
mediaRecorder.start(10); | ||
return function() { | ||
mediaRecorder.stop(); | ||
cb && recordedBlobs.length && cb(new Blob(recordedBlobs, {type: 'video/webm'})); | ||
}; | ||
}; | ||
@@ -234,17 +258,44 @@ self.publish = function(constraints, cb) { | ||
self.record = function() { | ||
var recordedBlobs = []; | ||
var mediaRecorder = new MediaRecorder(stream); | ||
mediaRecorder.ondataavailable = function(e) { | ||
if (e.data && e.data.size > 0) { | ||
recordedBlobs.push(e.data); | ||
} | ||
self.record = function(cb) { | ||
// Record stream to disk | ||
var recordId = _.uuid(); | ||
var storage = new Storage(); | ||
var stopRec = record(function(chunk) { | ||
storage.store(chunk, recordId); | ||
}, 50000); | ||
// Start uploading chunks to the server | ||
var uploading = true; | ||
var uploadingFinished = false; | ||
var uploadChunks = function() { | ||
if(uploadingFinished) return cb(null); | ||
storage.retrieve(recordId).then(function(chunk) { | ||
return cb(chunk); | ||
}).then(uploadChunks).catch(function() { | ||
if(!uploading) uploadingFinished = true; | ||
return _.delay(1000).then(uploadChunks); | ||
}); | ||
}; | ||
mediaRecorder.start(10); | ||
var stopRecord = function() { | ||
mediaRecorder.stop(); | ||
return new Blob(recordedBlobs, {type: 'video/webm'}); | ||
uploadChunks(); | ||
// Recording shall end automatically when the broadcast stops | ||
stopRecord = function() { | ||
uploading = false; | ||
stopRec && stopRec(); | ||
stopRec = null; | ||
}; | ||
return stopRecord; | ||
}; | ||
self.snapshot = function() { | ||
if(!video) return; | ||
var canvas = document.createElement('canvas'); | ||
canvas.width = video.videoWidth; | ||
canvas.height = video.videoHeight; | ||
var context = canvas.getContext('2d'); | ||
context.drawImage(video, 0, 0, canvas.width, canvas.height); | ||
return canvas.toDataURL('image/png'); | ||
}; | ||
}; | ||
@@ -251,0 +302,0 @@ |
@@ -7,9 +7,10 @@ var _ = require('eakwell'); | ||
var Room = function(roomName) { | ||
Socket.init(); | ||
var self = this; | ||
self.name = roomName; | ||
var publisher; | ||
var receivers = {}; | ||
var socket = new Socket.Socket(); | ||
var socket = new Socket(); | ||
@@ -40,3 +41,3 @@ socket.onerror = function(error) { | ||
var receive = function(streamId) { | ||
var receiver = new Broadcast(streamId, roomName, true); | ||
var receiver = new Broadcast(streamId, roomName, !!self.onRemoveStream); | ||
receiver.receive(function(error, video) { | ||
@@ -46,3 +47,3 @@ if(error) return console.error(error); | ||
receiver.onStop = function() { | ||
self.onRemoveStream(video, streamId); | ||
self.onRemoveStream && self.onRemoveStream(video, streamId); | ||
}; | ||
@@ -55,2 +56,3 @@ }); | ||
return new Promise(function(ok, fail) { | ||
if(publisher) return fail('Publishing already'); | ||
publisher = new Broadcast(userName || _.uuid(), roomName); | ||
@@ -66,4 +68,14 @@ publisher.publish(constraints, function(error, video) { | ||
if(publisher) publisher.stop(); | ||
publisher = null; | ||
}; | ||
self.record = function(cb) { | ||
if(!publisher) throw 'Not publishing'; | ||
return publisher.record(cb); | ||
}; | ||
self.snapshot = function() { | ||
return publisher && publisher.snapshot(); | ||
}; | ||
self.stop = function() { | ||
@@ -75,2 +87,3 @@ self.unpublish(); | ||
receivers = {}; | ||
socket.close(); | ||
}; | ||
@@ -77,0 +90,0 @@ }; |
@@ -9,2 +9,3 @@ var _ = require('eakwell'); | ||
Room: require('./room.js'), | ||
Storage: require('./storage.js'), | ||
SocketServer: SocketServer, | ||
@@ -11,0 +12,0 @@ |
@@ -11,2 +11,4 @@ var _ = require('eakwell'); | ||
init(); | ||
self.channel = channel || 'spreadcast'; | ||
@@ -21,3 +23,5 @@ self.sessionId = _.uuid(); | ||
}; | ||
if(socket) socket.send(JSON.stringify(data)); | ||
try { | ||
if(socket) socket.send(JSON.stringify(data)); | ||
} catch(e) {} | ||
}); | ||
@@ -27,3 +31,3 @@ }; | ||
self.close = function() { | ||
_.remove(instances, self); | ||
if(!_.remove(instances, self)) return; | ||
self.onclose && self.onclose(); | ||
@@ -33,2 +37,3 @@ self.send({ | ||
}); | ||
if(!instances.length) close(); | ||
}; | ||
@@ -48,2 +53,3 @@ | ||
}); | ||
shutdown = false; | ||
socket.onclose = function() { | ||
@@ -93,9 +99,6 @@ if(shutdown) return; | ||
socket = null; | ||
socketReady = null; | ||
instances = []; | ||
}; | ||
module.exports = { | ||
init: init, | ||
close: close, | ||
Socket: Socket | ||
}; | ||
module.exports = Socket; |
Sorry, the diff of this file is too big to display
12
1039
66
85993
Updatedeakwell@^0.2.6