Wallet/js/services/video.js

86 lines
2.3 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-28 11:56:27 -03:00
this.localStream = null;
2014-04-30 16:11:55 -03:00
this.onlineSound = new Audio('./sound/online.wav');
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;
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];
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();
}
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) {
2014-04-29 18:20:44 -03:00
var self = this;
2014-04-24 16:07:49 -03:00
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-29 18:20:44 -03:00
self.onlineSound.play();
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() {
2014-04-28 11:56:27 -03:00
console.log('Media connection closed with ' + peerID);
cb(true, peerID, null); // ask to stop video streaming in UI
2014-04-24 16:07:49 -03:00
});
2014-04-28 11:56:27 -03:00
mediaConnection.on('error', function(e) {
console.log('Media connection error with ' + peerID);
cb(e, peerID, null);
2014-04-23 21:20:44 -03:00
});
2014-04-28 11:56:27 -03:00
this.mediaConnections[peerID] = mediaConnection;
2014-04-23 21:20:44 -03:00
}
2014-04-28 11:56:27 -03:00
Video.prototype.close = function() {
this.localStream.stop();
this.localStream.mozSrcObject = null;
this.localStream.src = "";
this.localStream.src = null;
this.localStream = null;
for (var i = 0; this.mediaConnections.length; i++) {
this.mediaConnections[i].close();
}
this.mediaConnections = {};
};
2014-04-23 21:20:44 -03:00
angular.module('copay.video').value('video', new Video());