Wallet/js/services/video.js

96 lines
2.5 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-06-18 13:01:50 -03:00
if (typeof Audio !== 'undefined') {
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;
2014-05-20 10:50:50 -03:00
var VWIDTH = 320;
var VHEIGHT = 320;
var constraints = {
2014-04-23 21:20:44 -03:00
audio: true,
2014-05-20 10:50:50 -03:00
video: {
mandatory: {
maxWidth: VWIDTH,
maxHeight: VHEIGHT,
}
}
};
navigator.getUserMedia(constraints, 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-06-18 13:01:50 -03:00
if (self.onlineSound) 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
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) {
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() {
2014-04-30 19:50:13 -03:00
if (this.localStream) {
2014-04-30 12:58:40 -03:00
this.localStream.stop();
this.localStream.mozSrcObject = null;
this.localStream.src = "";
this.localStream.src = null;
this.localStream = null;
}
2014-04-28 11:56:27 -03:00
for (var i = 0; this.mediaConnections.length; i++) {
this.mediaConnections[i].close();
}
this.mediaConnections = {};
};
2014-06-03 17:42:36 -03:00
angular.module('copayApp.services').value('video', new Video());