starting with video sync

This commit is contained in:
Manuel Araoz 2014-04-23 21:20:44 -03:00
commit fbe7a34197
8 changed files with 181 additions and 98 deletions

View file

@ -1,80 +1,99 @@
'use strict';
angular.module('copay.controllerUtils').factory('controllerUtils', function ($rootScope, $location, Socket) {
var root = {};
angular.module('copay.controllerUtils')
.factory('controllerUtils', function($rootScope, $sce, $location, Socket, video) {
var root = {};
$rootScope.videoSrc = {};
$rootScope.getVideoURL = function(copayer) {
return decodeURIComponent($rootScope.videoSrc[copayer]);
};
root.logout = function() {
console.log('### DELETING WALLET'); //TODO
$rootScope.wallet = null;
delete $rootScope['wallet'];
$rootScope.totalBalance = 0;
$location.path('signin');
};
root.logout = function() {
console.log('### DELETING WALLET'); //TODO
$rootScope.wallet = null;
delete $rootScope['wallet'];
$rootScope.totalBalance = 0;
$location.path('signin');
};
root.onError = function(scope) {
if (scope) scope.loading = false;
$rootScope.flashMessage = {type:'error', message: 'Could not connect to peer'};
root.logout();
}
root.onErrorDigest = function(scope) {
root.onError(scope);
$rootScope.$digest();
}
root.setupUxHandlers = function(w) {
w.on('badMessage', function(peerId) {
$rootScope.flashMessage = {type:'error', message: 'Received wrong message from peer id:' + peerId};
});
w.on('created', function() {
$location.path('peer');
$rootScope.wallet = w;
root.updateBalance();
});
w.on('refresh', function() {
console.log('[controllerUtils.js] Refreshing'); //TODO
root.updateBalance();
});
w.on('openError', root.onErrorDigest);
w.on('close', root.onErrorDigest);
w.netStart();
};
root.updateBalance = function() {
var w = $rootScope.wallet;
if (!w) return;
w.getBalance(false,function(balance, balanceByAddr) {
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
console.log('New balance:', balance);
w.getBalance(true,function(balance) {
$rootScope.availableBalance = balance;
$rootScope.$digest();
});
});
};
root.setSocketHandlers = function() {
Socket.removeAllListeners();
var addrs = $rootScope.wallet.getAddressesStr();
for(var i = 0; i < addrs.length; i++) {
console.log('### SUBSCRIBE TO', addrs[i]);
Socket.emit('subscribe', addrs[i]);
root.onError = function(scope) {
if (scope) scope.loading = false;
$rootScope.flashMessage = {
type: 'error',
message: 'Could not connect to peer: ' +
scope
};
root.logout();
}
console.log('[controllerUtils.js.64]'); //TODO
addrs.forEach(function(addr) {
Socket.on(addr, function(txid) {
console.log('Received!', txid);
root.onErrorDigest = function(scope) {
root.onError(scope);
$rootScope.$digest();
}
root.setupUxHandlers = function(w) {
var handlePeerVideo = function(err, peerID, url) {
if (err) {
root.onErrorDigest(err);
}
$sce.trustAsResourceUrl(url);
$rootScope.videoSrc[peerID] = encodeURIComponent(url);
$rootScope.$apply();
};
w.on('badMessage', function(peerId) {
$rootScope.flashMessage = {
type: 'error',
message: 'Received wrong message from peer id:' + peerId
};
});
w.on('created', function(selfpeer) {
video.setOwnPeer(selfpeer, handlePeerVideo);
$location.path('peer');
$rootScope.wallet = w;
root.updateBalance();
});
});
};
w.on('refresh', function() {
console.log('[controllerUtils.js] Refreshing'); //TODO
root.updateBalance();
});
w.on('openError', root.onErrorDigest);
w.on('peer', function(peerID) {
video.addPeer(peerID, handlePeerVideo);
});
w.on('close', root.onErrorDigest);
w.netStart();
};
return root;
});
root.updateBalance = function() {
var w = $rootScope.wallet;
if (!w) return;
w.getBalance(false, function(balance, balanceByAddr) {
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
console.log('New balance:', balance);
w.getBalance(true, function(balance) {
$rootScope.availableBalance = balance;
$rootScope.$digest();
});
});
};
root.setSocketHandlers = function() {
Socket.removeAllListeners();
var addrs = $rootScope.wallet.getAddressesStr();
for (var i = 0; i < addrs.length; i++) {
console.log('### SUBSCRIBE TO', addrs[i]);
Socket.emit('subscribe', addrs[i]);
}
console.log('[controllerUtils.js.64]'); //TODO
addrs.forEach(function(addr) {
Socket.on(addr, function(txid) {
console.log('Received!', txid);
root.updateBalance();
});
});
};
return root;
});

52
js/services/video.js Normal file
View file

@ -0,0 +1,52 @@
'use strict';
var Video = function() {
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
};
Video.prototype.setOwnPeer = function(peer, cb) {
var self = this;
navigator.getUserMedia({
audio: true,
video: true
}, function(stream) {
// Set your video displays
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('error', function(err) {
console.log('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.addCall = function(call, cb) {
var peerID = call.id;
// Wait for stream on the call, then set peer video display
call.on('stream', function(stream) {
cb(null, peerID, URL.createObjectURL(stream));
});
call.on('close', function() {
// TODO: use peerID
});
}
angular.module('copay.video').value('video', new Video());