diff --git a/samples/js/apprtc/apprtc.py b/samples/js/apprtc/apprtc.py index 4de7a05840..0547970b13 100644 --- a/samples/js/apprtc/apprtc.py +++ b/samples/js/apprtc/apprtc.py @@ -315,6 +315,7 @@ class MainPage(webapp2.RequestHandler): min_re = self.request.get('minre') max_re = self.request.get('maxre') hd_video = self.request.get('hd') + turn_url = 'https://computeengineondemand.appspot.com/' if hd_video.lower() == 'true': min_re = '1280x720' ts_pwd = self.request.get('tp') @@ -374,6 +375,7 @@ class MainPage(webapp2.RequestHandler): room_link = base_url + '?r=' + room_key room_link = append_url_arguments(self.request, room_link) + turn_url = turn_url + 'turn?' + 'username=' + user + '&key=4080218913' token = create_channel(room, user, token_timeout) pc_config = make_pc_config(stun_server, turn_server, ts_pwd) pc_constraints = make_pc_constraints(compat) @@ -387,7 +389,8 @@ class MainPage(webapp2.RequestHandler): 'pc_config': json.dumps(pc_config), 'pc_constraints': json.dumps(pc_constraints), 'offer_constraints': json.dumps(offer_constraints), - 'media_constraints': json.dumps(media_constraints) + 'media_constraints': json.dumps(media_constraints), + 'turn_url': turn_url } if unittest: target_page = 'test/test_' + unittest + '.html' diff --git a/samples/js/apprtc/index.html b/samples/js/apprtc/index.html index ebf35beaa9..e87bfd56c3 100644 --- a/samples/js/apprtc/index.html +++ b/samples/js/apprtc/index.html @@ -112,10 +112,13 @@ var remoteStream; var channel; var channelReady = false; + var turnReady = false; var pc; var socket; var initiator = {{ initiator }}; var started = false; + var pc_config = {{ pc_config|safe }}; + var pc_constraints = {{ pc_constraints|safe }}; // Set up audio and video regardless of what devices are present. var sdpConstraints = {'mandatory': { 'OfferToReceiveAudio':true, @@ -133,6 +136,7 @@ // NOTE: AppRTCClient.java searches & parses this line; update there when // changing here. openChannel('{{ token }}'); + requestTurn('{{ turn_url }}'); doGetUserMedia(); } @@ -148,6 +152,35 @@ socket = channel.open(handler); } + function requestTurn(turn_url) { + var turnExists = false; + for (var i in pc_config.iceServers) { + if (pc_config.iceServers[i].url.substr(0, 5) == 'turn:') { + turnExists = true; + turnReady = true; + break; + } + } + if (!turnExists) { + // No turn server. Get one from computeengineondemand.appspot.com: + xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = onTurnResult; + xmlhttp.open("GET", turn_url, true); + xmlhttp.send(); + } + } + + function onTurnResult() { + if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { + var turnServer = JSON.parse(xmlhttp.responseText); + pc_config.iceServers.push({ + "url": "turn:" + turnServer.username + "@" + turnServer.turn, + "credential": turnServer.password + }); + turnReady = true; + } + } + function resetStatus() { if (!initiator) { setStatus("Waiting for someone to join: {{ room_link }}"); @@ -170,13 +203,11 @@ } } - function createPeerConnection() { - var pc_config = {{ pc_config|safe }}; - var pc_constraints = {{ pc_constraints|safe }}; + function createPeerConnection() { // Force the use of a number IP STUN server for Firefox. if (webrtcDetectedBrowser == "firefox") { pc_config = {"iceServers":[{"url":"stun:23.21.150.121"}]}; - } + } try { // Create an RTCPeerConnection via the polyfill (adapter.js). pc = new RTCPeerConnection(pc_config, pc_constraints); @@ -189,13 +220,12 @@ alert("Cannot create RTCPeerConnection object; WebRTC is not supported by this browser."); return; } - pc.onaddstream = onRemoteStreamAdded; pc.onremovestream = onRemoteStreamRemoved; } function maybeStart() { - if (!started && localStream && channelReady) { + if (!started && localStream && channelReady && turnReady) { setStatus("Connecting..."); console.log("Creating PeerConnection."); createPeerConnection();