Wallet/js/services/video.js

68 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-04-23 21:20:44 -03:00
'use strict';
var Video = function() {
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
2014-04-24 18:24:03 -03:00
this.mediaConnections = {};
2014-04-23 21:20:44 -03:00
};
2014-04-24 16:07:49 -03:00
Video.prototype.setOwnPeer = function(peer, wallet, cb) {
2014-04-23 21:20:44 -03:00
var self = this;
2014-04-24 20:56:36 -03:00
2014-04-23 21:20:44 -03:00
navigator.getUserMedia({
audio: true,
video: true
}, function(stream) {
2014-04-24 16:07:49 -03:00
// This is called when user accepts using webcam
self.localStream = stream;
var online = wallet.getOnlinePeerIDs();
2014-04-24 18:24:03 -03:00
for (var i = 0; i < online.length; i++) {
2014-04-24 16:07:49 -03:00
var o = online[i];
2014-04-24 18:24:03 -03:00
var mc = self.mediaConnections[o];
2014-04-24 16:07:49 -03:00
if (o !== peer.id) {
self.callPeer(o, cb);
}
2014-04-24 18:24:03 -03:00
}
2014-04-23 21:20:44 -03:00
cb(null, peer.id, URL.createObjectURL(stream));
}, function() {
cb(new Error('Failed to access the webcam and microphone.'));
});
2014-04-24 16:07:49 -03:00
2014-04-23 21:20:44 -03:00
// Receiving a call
2014-04-24 16:07:49 -03:00
peer.on('call', function(mediaConnection) {
if (self.localStream) {
mediaConnection.answer(self.localStream);
} else {
mediaConnection.answer();
}
2014-04-24 18:24:03 -03:00
self.mediaConnections[mediaConnection.peer] = mediaConnection;
2014-04-24 16:07:49 -03:00
self._addCall(mediaConnection, cb);
2014-04-23 21:20:44 -03:00
});
this.peer = peer;
};
2014-04-24 16:07:49 -03: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 21:20:44 -03:00
};
2014-04-24 16:07:49 -03:00
Video.prototype._addCall = function(mediaConnection, cb) {
var peerID = mediaConnection.peer;
2014-04-23 21:20:44 -03:00
// Wait for stream on the call, then set peer video display
2014-04-24 16:07:49 -03:00
mediaConnection.on('stream', function(stream) {
2014-04-23 21:20:44 -03:00
cb(null, peerID, URL.createObjectURL(stream));
});
2014-04-24 16:07:49 -03:00
mediaConnection.on('close', function() {
});
mediaConnection.on('error', function() {
2014-04-23 21:20:44 -03:00
});
}
angular.module('copay.video').value('video', new Video());