Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of heroku.com:webrtc-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ekr committed Mar 13, 2012
2 parents 574b5cf + e6c3cd7 commit 0b3d799
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
66 changes: 39 additions & 27 deletions static/static/js/demo.js
Expand Up @@ -41,13 +41,15 @@ var ajax = function(params) {
};


var CallingClient = function(config_, username, peer, video_, start_call) {
console.log("Calling client constructor");
var poll_timeout = 500; // ms


var CallingClient = function(config_, username, peer, video_, ready_cb) {
var poll_timeout = 1000; // ms
var pc = null;
var config = $.extend({}, config_);
var video = $.extend({}, video_);
var state = "INIT";
var localstream = null;


if (!config.stun) {
console.log("Need to provide STUN server");
Expand Down Expand Up @@ -80,10 +82,12 @@ var CallingClient = function(config_, username, peer, video_, start_call) {
var js = JSON.parse(msg);
log("Received message " + JSON.stringify(js));

if (state == "INIT")
addStream();

if (!pc) {
startup();
}

pc.processSignalingMessage(js.body);

setTimeout(poll, poll_timeout);
};

Expand All @@ -102,20 +106,24 @@ var CallingClient = function(config_, username, peer, video_, start_call) {
// Media processing
var mediasuccess = function(stream) {
log("Got stream");
pc.addStream(stream);
state = "STARTED";
localstream = stream;

// Set video
if (video) {
video.local.style.opacity = 1;
// video.local.style.opacity = 1;
video.local.src = webkitURL.createObjectURL(stream);
};

poll();
ready_cb();
};

var mediafailure = function() {
console.log("Couldn't get media");
};
var addStream = function() {
console.log("Adding a stream");
try {
navigator.webkitGetUserMedia( video ? "video, audio" : "video",
mediasuccess, mediafailure);
Expand All @@ -130,10 +138,9 @@ var CallingClient = function(config_, username, peer, video_, start_call) {

// Set video
if (video) {
video.remote.style.opacity = 1;
// video.remote.style.opacity = 1;
video.remote.src = webkitURL.createObjectURL(ev.stream);
};

};

var onConnecting = function() {
Expand All @@ -153,29 +160,34 @@ var CallingClient = function(config_, username, peer, video_, start_call) {
var onStateChange = function() {
log("onStateChange()");
log("state = " + pc.readyState);
}

var pc = new webkitPeerConnection("STUN "+config.stun, signaling);
pc.onaddstream = onAddStream;
pc.onconnecting = onConnecting;
pc.onmessage = onMessage;
pc.onopen = onOpen;
pc.onremovestream = onRemoveStream;
pc.onstatechange = onStateChange;
};

console.log("Made PeerConnection");
var startup = function() {
pc = new webkitPeerConnection("STUN "+config.stun, signaling);
pc.onaddstream = onAddStream;
pc.onconnecting = onConnecting;
pc.onmessage = onMessage;
pc.onopen = onOpen;
pc.onremovestream = onRemoveStream;
pc.onstatechange = onStateChange;
pc.addStream(localstream);
console.log("Made PeerConnection");
};

if (start_call) {
addStream();
}
// This is needed
addStream();

// Start polling
poll();
return {
startup:startup
};
};



default_config = {
stun:'stun.l.google.com:19302'
};

//new CallingClient(config, "abc", "def", video, true);
//new CallingClient(config, "abc", "def", video, true);


14 changes: 12 additions & 2 deletions views/demo.html
Expand Up @@ -31,11 +31,13 @@ <h3>ROAP Test</h3>
</div>

<div id="local">
Local
<video
width="100%" height="100%" id="localVideo" autoplay="autoplay">
</video>
</div>
<div id="remote">
Remote
<video width="100%" height="100%" id="remoteVideo" autoplay="autoplay">
</video>
</div>
Expand All @@ -44,11 +46,19 @@ <h3>ROAP Test</h3>
<script>
function WebRTCDemo(me, them, start) {
var video = {};

var cc;

video.local = document.getElementById("localVideo");
video.remote = document.getElementById("remoteVideo");

new CallingClient(default_config, me, them, video, start);
cc = new CallingClient(default_config, me, them, video, function() {
console.log("Calling client ready");
if (start) {
cc.startup();
}
});

return cc;
}
</script>

Expand Down

0 comments on commit 0b3d799

Please sign in to comment.