167 lines
3.7 KiB
JavaScript
167 lines
3.7 KiB
JavaScript
// ______________________
|
|
// MultiStreamRecorder.js
|
|
|
|
function MultiStreamRecorder(arrayOfMediaStreams, options) {
|
|
arrayOfMediaStreams = arrayOfMediaStreams || [];
|
|
|
|
if (arrayOfMediaStreams instanceof MediaStream) {
|
|
arrayOfMediaStreams = [arrayOfMediaStreams];
|
|
}
|
|
|
|
var self = this;
|
|
|
|
var mixer;
|
|
var mediaRecorder;
|
|
|
|
options = options || {
|
|
mimeType: 'video/webm',
|
|
video: {
|
|
width: 360,
|
|
height: 240
|
|
}
|
|
};
|
|
|
|
if (!options.frameInterval) {
|
|
options.frameInterval = 10;
|
|
}
|
|
|
|
if (!options.video) {
|
|
options.video = {};
|
|
}
|
|
|
|
if (!options.video.width) {
|
|
options.video.width = 360;
|
|
}
|
|
|
|
if (!options.video.height) {
|
|
options.video.height = 240;
|
|
}
|
|
|
|
this.start = function(timeSlice) {
|
|
// github/muaz-khan/MultiStreamsMixer
|
|
mixer = new MultiStreamsMixer(arrayOfMediaStreams);
|
|
|
|
if (getVideoTracks().length) {
|
|
mixer.frameInterval = options.frameInterval || 10;
|
|
mixer.width = options.video.width || 360;
|
|
mixer.height = options.video.height || 240;
|
|
mixer.startDrawingFrames();
|
|
}
|
|
|
|
if (typeof self.previewStream === 'function') {
|
|
self.previewStream(mixer.getMixedStream());
|
|
}
|
|
|
|
// record using MediaRecorder API
|
|
mediaRecorder = new MediaStreamRecorder(mixer.getMixedStream());
|
|
|
|
for (var prop in self) {
|
|
if (typeof self[prop] !== 'function') {
|
|
mediaRecorder[prop] = self[prop];
|
|
}
|
|
}
|
|
|
|
mediaRecorder.ondataavailable = function(blob) {
|
|
self.ondataavailable(blob);
|
|
};
|
|
|
|
mediaRecorder.onstop = self.onstop;
|
|
|
|
mediaRecorder.start(timeSlice);
|
|
};
|
|
|
|
function getVideoTracks() {
|
|
var tracks = [];
|
|
arrayOfMediaStreams.forEach(function(stream) {
|
|
stream.getVideoTracks().forEach(function(track) {
|
|
tracks.push(track);
|
|
});
|
|
});
|
|
return tracks;
|
|
}
|
|
|
|
this.stop = function(callback) {
|
|
if (!mediaRecorder) {
|
|
return;
|
|
}
|
|
|
|
mediaRecorder.stop(function(blob) {
|
|
callback(blob);
|
|
});
|
|
};
|
|
|
|
this.pause = function() {
|
|
if (mediaRecorder) {
|
|
mediaRecorder.pause();
|
|
}
|
|
};
|
|
|
|
this.resume = function() {
|
|
if (mediaRecorder) {
|
|
mediaRecorder.resume();
|
|
}
|
|
};
|
|
|
|
this.clearRecordedData = function() {
|
|
if (mediaRecorder) {
|
|
mediaRecorder.clearRecordedData();
|
|
mediaRecorder = null;
|
|
}
|
|
|
|
if (mixer) {
|
|
mixer.releaseStreams();
|
|
mixer = null;
|
|
}
|
|
};
|
|
|
|
this.addStreams = this.addStream = function(streams) {
|
|
if (!streams) {
|
|
throw 'First parameter is required.';
|
|
}
|
|
|
|
if (!(streams instanceof Array)) {
|
|
streams = [streams];
|
|
}
|
|
|
|
arrayOfMediaStreams.concat(streams);
|
|
|
|
if (!mediaRecorder || !mixer) {
|
|
return;
|
|
}
|
|
|
|
mixer.appendStreams(streams);
|
|
};
|
|
|
|
this.resetVideoStreams = function(streams) {
|
|
if (!mixer) {
|
|
return;
|
|
}
|
|
|
|
if (streams && !(streams instanceof Array)) {
|
|
streams = [streams];
|
|
}
|
|
|
|
mixer.resetVideoStreams(streams);
|
|
};
|
|
|
|
this.ondataavailable = function(blob) {
|
|
if (self.disableLogs) {
|
|
return;
|
|
}
|
|
|
|
console.log('ondataavailable', blob);
|
|
};
|
|
|
|
this.onstop = function() {};
|
|
|
|
// for debugging
|
|
this.name = 'MultiStreamRecorder';
|
|
this.toString = function() {
|
|
return this.name;
|
|
};
|
|
}
|
|
|
|
if (typeof MediaStreamRecorder !== 'undefined') {
|
|
MediaStreamRecorder.MultiStreamRecorder = MultiStreamRecorder;
|
|
}
|