2014-04-23 17:20:44 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Video = function() {
|
|
|
|
navigator.getUserMedia = navigator.getUserMedia ||
|
|
|
|
navigator.webkitGetUserMedia ||
|
|
|
|
navigator.mozGetUserMedia;
|
2014-04-24 14:24:03 -07:00
|
|
|
|
|
|
|
this.mediaConnections = {};
|
2014-04-28 07:56:27 -07:00
|
|
|
this.localStream = null;
|
2014-04-23 17:20:44 -07:00
|
|
|
};
|
|
|
|
|
2014-04-24 12:07:49 -07:00
|
|
|
Video.prototype.setOwnPeer = function(peer, wallet, cb) {
|
2014-04-23 17:20:44 -07:00
|
|
|
var self = this;
|
|
|
|
|
2014-05-20 06:50:50 -07:00
|
|
|
var VWIDTH = 320;
|
|
|
|
var VHEIGHT = 320;
|
|
|
|
var constraints = {
|
2014-04-23 17:20:44 -07:00
|
|
|
audio: true,
|
2014-05-20 06:50:50 -07:00
|
|
|
video: {
|
|
|
|
mandatory: {
|
|
|
|
maxWidth: VWIDTH,
|
|
|
|
maxHeight: VHEIGHT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
navigator.getUserMedia(constraints, function(stream) {
|
2014-04-24 12:07:49 -07:00
|
|
|
// This is called when user accepts using webcam
|
|
|
|
self.localStream = stream;
|
|
|
|
var online = wallet.getOnlinePeerIDs();
|
2014-04-24 14:24:03 -07:00
|
|
|
for (var i = 0; i < online.length; i++) {
|
2014-04-24 12:07:49 -07:00
|
|
|
var o = online[i];
|
|
|
|
if (o !== peer.id) {
|
|
|
|
self.callPeer(o, cb);
|
|
|
|
}
|
2014-04-24 14:24:03 -07:00
|
|
|
}
|
2014-04-23 17:20:44 -07:00
|
|
|
cb(null, peer.id, URL.createObjectURL(stream));
|
|
|
|
}, function() {
|
|
|
|
cb(new Error('Failed to access the webcam and microphone.'));
|
|
|
|
});
|
2014-04-24 12:07:49 -07:00
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
// Receiving a call
|
2014-04-24 12:07:49 -07:00
|
|
|
peer.on('call', function(mediaConnection) {
|
|
|
|
if (self.localStream) {
|
|
|
|
mediaConnection.answer(self.localStream);
|
|
|
|
} else {
|
|
|
|
mediaConnection.answer();
|
|
|
|
}
|
|
|
|
self._addCall(mediaConnection, cb);
|
2014-04-23 17:20:44 -07:00
|
|
|
});
|
|
|
|
this.peer = peer;
|
|
|
|
};
|
|
|
|
|
2014-04-24 12:07:49 -07:00
|
|
|
Video.prototype.callPeer = function(peerID, cb) {
|
|
|
|
if (this.localStream) {
|
|
|
|
var mediaConnection = this.peer.call(peerID, this.localStream);
|
|
|
|
this._addCall(mediaConnection, cb);
|
|
|
|
}
|
2014-04-23 17:20:44 -07:00
|
|
|
};
|
|
|
|
|
2014-04-24 12:07:49 -07:00
|
|
|
Video.prototype._addCall = function(mediaConnection, cb) {
|
2014-04-29 14:20:44 -07:00
|
|
|
var self = this;
|
2014-04-24 12:07:49 -07:00
|
|
|
var peerID = mediaConnection.peer;
|
2014-04-23 17:20:44 -07:00
|
|
|
|
|
|
|
// Wait for stream on the call, then set peer video display
|
2014-04-24 12:07:49 -07:00
|
|
|
mediaConnection.on('stream', function(stream) {
|
2014-04-23 17:20:44 -07:00
|
|
|
cb(null, peerID, URL.createObjectURL(stream));
|
|
|
|
});
|
|
|
|
|
2014-04-24 12:07:49 -07:00
|
|
|
mediaConnection.on('close', function() {
|
2014-04-28 07:56:27 -07:00
|
|
|
cb(true, peerID, null); // ask to stop video streaming in UI
|
2014-04-24 12:07:49 -07:00
|
|
|
});
|
2014-04-28 07:56:27 -07:00
|
|
|
mediaConnection.on('error', function(e) {
|
|
|
|
cb(e, peerID, null);
|
2014-04-23 17:20:44 -07:00
|
|
|
});
|
2014-04-28 07:56:27 -07:00
|
|
|
this.mediaConnections[peerID] = mediaConnection;
|
2014-04-23 17:20:44 -07:00
|
|
|
}
|
|
|
|
|
2014-04-28 07:56:27 -07:00
|
|
|
Video.prototype.close = function() {
|
2014-04-30 15:50:13 -07:00
|
|
|
if (this.localStream) {
|
2014-04-30 08:58:40 -07:00
|
|
|
this.localStream.stop();
|
|
|
|
this.localStream.mozSrcObject = null;
|
|
|
|
this.localStream.src = "";
|
|
|
|
this.localStream.src = null;
|
|
|
|
this.localStream = null;
|
|
|
|
}
|
2014-04-28 07:56:27 -07:00
|
|
|
for (var i = 0; this.mediaConnections.length; i++) {
|
|
|
|
this.mediaConnections[i].close();
|
|
|
|
}
|
|
|
|
this.mediaConnections = {};
|
|
|
|
};
|
|
|
|
|
2014-06-03 13:42:36 -07:00
|
|
|
angular.module('copayApp.services').value('video', new Video());
|