self video working

This commit is contained in:
Manuel Araoz 2014-04-24 16:07:49 -03:00
commit f5b8aa7c61
4 changed files with 64 additions and 26 deletions

View file

@ -35,7 +35,7 @@ angular.module('copay.controllerUtils')
if (err) {
root.onErrorDigest(err);
}
$sce.trustAsResourceUrl(url);
alert('add this video url='+url+' for peer '+peerID);
$rootScope.videoSrc[peerID] = encodeURI(url);
$rootScope.$apply();
};
@ -45,9 +45,8 @@ angular.module('copay.controllerUtils')
message: 'Received wrong message from peer id:' + peerId
};
});
w.on('created', function(selfpeer) {
video.setOwnPeer(selfpeer, handlePeerVideo);
w.on('created', function(myPeerID) {
video.setOwnPeer(myPeerID, w, handlePeerVideo);
$location.path('peer');
$rootScope.wallet = w;
root.updateBalance();
@ -58,7 +57,7 @@ angular.module('copay.controllerUtils')
});
w.on('openError', root.onErrorDigest);
w.on('peer', function(peerID) {
video.addPeer(peerID, handlePeerVideo);
video.callPeer(peerID, handlePeerVideo);
});
w.on('close', root.onErrorDigest);
w.netStart();

View file

@ -6,46 +6,65 @@ var Video = function() {
navigator.mozGetUserMedia;
};
Video.prototype.setOwnPeer = function(peer, cb) {
Video.prototype.setOwnPeer = function(peer, wallet, cb) {
var self = this;
navigator.getUserMedia({
audio: true,
video: true
}, function(stream) {
// Set your video displays
// This is called when user accepts using webcam
self.localStream = stream;
var online = wallet.getOnlinePeerIDs();
for (var i=0; i<online.length; i++) {
var o = online[i];
if (o !== peer.id) {
self.callPeer(o, cb);
}
}
cb(null, peer.id, URL.createObjectURL(stream));
window.localStream = stream;
}, function() {
cb(new Error('Failed to access the webcam and microphone.'));
});
// Receiving a call
peer.on('call', function(call) {
// Answer the call automatically (instead of prompting user) for demo purposes
call.answer(window.localStream);
self.addCall(call, cb);
peer.on('call', function(mediaConnection) {
alert('answering call from ' + mediaConnection.peer);
if (self.localStream) {
mediaConnection.answer(self.localStream);
} else {
mediaConnection.answer();
}
self._addCall(mediaConnection, cb);
});
peer.on('error', function(err) {
console.log('ERROR on video peer '+err);
alert('error on video peer '+err);
});
this.peer = peer;
};
Video.prototype.addPeer = function(peerID, cb) {
var call = this.peer.call(peerID, window.localStream);
this.addCall(call, cb);
Video.prototype.callPeer = function(peerID, cb) {
if (this.localStream) {
var mediaConnection = this.peer.call(peerID, this.localStream);
this._addCall(mediaConnection, cb);
}
};
Video.prototype.addCall = function(call, cb) {
var peerID = call.id;
Video.prototype._addCall = function(mediaConnection, cb) {
var peerID = mediaConnection.peer;
alert('_addCall ' + peerID);
// Wait for stream on the call, then set peer video display
call.on('stream', function(stream) {
mediaConnection.on('stream', function(stream) {
alert('STREAM ON ADD CALL');
cb(null, peerID, URL.createObjectURL(stream));
});
call.on('close', function() {
// TODO: use peerID
mediaConnection.on('close', function() {
alert('CLOSEEEEEEEEEEEEEEE ON ADD CALL');
});
mediaConnection.on('error', function() {
alert('ERROR ON ADD CALL');
});
}