Download:
child 5:de01e29e06b5
parent 3:d69239d4ae6d
4:61a86e766285
Anton Shestakov <engored@ya.ru>, Tue, 12 Mar 2013 13:56:00 +0900
User can observe (establish a peer connection without providing any media streams).

1 файлов изменено, 55 вставок(+), 34 удалений(-) [+]
static/js/main.js file | annotate | diff | comparison | revisions
--- a/static/js/main.js Tue Mar 12 02:23:34 2013 +0900
+++ b/static/js/main.js Tue Mar 12 13:56:00 2013 +0900
@@ -1,59 +1,72 @@
var ws = new WebSocket('ws://' + location.host + '/ws');
+var initiator;
var pc;
function call() {
$('#btn-call').addClass('btn-active');
- init(true);
+ initiator = true;
+ init();
}
function receive() {
$('#btn-receive').addClass('btn-active');
- init(false);
+ initiator = false;
+ init();
}
-function init(initiator) {
+function init() {
var constraints = {
audio: $('#audio').prop('checked'),
video: $('#video').prop('checked')
};
- getUserMedia(constraints, function(stream) {
- pc = new RTCPeerConnection(null);
-
+ if (constraints.audio || constraints.video) {
+ getUserMedia(constraints, connect, fail);
+ } else {
+ connect();
+ }
+}
+
+
+function connect(stream) {
+ pc = new RTCPeerConnection(null);
+
+ if (stream) {
pc.addStream(stream);
- $('#local').prop('src', URL.createObjectURL(stream));
- pc.onaddstream = function(event) {
- $('#remote').prop('src', URL.createObjectURL(event.stream));
- logStreaming(true);
- };
- pc.onicecandidate = function(event) {
- if (event.candidate) {
- ws.send(JSON.stringify(event.candidate));
+ $('#local').attachStream(stream);
+ }
+
+ pc.onaddstream = function(event) {
+ $('#remote').attachStream(event.stream);
+ logStreaming(true);
+ };
+ pc.onicecandidate = function(event) {
+ if (event.candidate) {
+ ws.send(JSON.stringify(event.candidate));
+ }
+ };
+ ws.onmessage = function (event) {
+ var signal = JSON.parse(event.data);
+ if (signal.sdp) {
+ if (initiator) {
+ receiveAnswer(signal);
+ } else {
+ receiveOffer(signal);
}
- };
- ws.onmessage = function (event) {
- var signal = JSON.parse(event.data);
- if (signal.sdp) {
- if (initiator) {
- receiveAnswer(signal);
- } else {
- receiveOffer(signal);
- }
- } else if (signal.candidate) {
- pc.addIceCandidate(new RTCIceCandidate(signal));
- }
- };
-
- if (initiator) {
- createOffer();
- } else {
- log('waiting for offer...');
+ } else if (signal.candidate) {
+ pc.addIceCandidate(new RTCIceCandidate(signal));
}
- logStreaming(false);
- }, fail);
+ };
+
+ if (initiator) {
+ createOffer();
+ } else {
+ log('waiting for offer...');
+ }
+ logStreaming(false);
}
@@ -105,3 +118,11 @@
$('#status').text(Array.prototype.join.call(arguments, ' '));
console.error.apply(console, arguments);
}
+
+
+jQuery.fn.attachStream = function(stream) {
+ this.each(function() {
+ this.src = URL.createObjectURL(stream);
+ this.play();
+ });
+};