1468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// 3468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// Use of this source code is governed by a BSD-style license 4468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// that can be found in the LICENSE file in the root of the source 5468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// tree. An additional intellectual property rights grant can be found 6468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// in the file PATENTS. All contributing project authors may 7468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// be found in the AUTHORS file in the root of the source tree. 807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com// 907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.comvar localStreams = []; 1007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.comvar remoteStreams = []; 1107ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 1207ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.comfunction ping(callback) { 1307ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com callback("pong"); 1407ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com} 1507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 1607ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.comfunction getUserMedia(constraints, onSuccessCallback, onFailCallback){ 1707ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com console.log("Getting user media."); 1807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com navigator.webkitGetUserMedia(constraints, 1907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com onSuccessCallbackWraper, onFailCallback); 2007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 2107ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com function onSuccessCallbackWraper(stream) { 2207ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com console.log("GetUserMedia success."); 2307ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com localStreams[stream.id] = stream; 2407ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com onSuccessCallback(stream); 2507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com } 2607ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com} 2707ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 283e2f8ff36cbe339ebc35d63d3cf8ed379dda3d59houssainy@google.comfunction createPeerConnection(config, doneCallback, failCallback) { 2907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com console.log("Creating peer connection"); 3007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com var obj = {}; 313e2f8ff36cbe339ebc35d63d3cf8ed379dda3d59houssainy@google.com var pc = new webkitRTCPeerConnection(config); 32468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org 3307ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com expose(obj, pc, "close"); 3407ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com expose(obj, pc, "createOffer"); 3507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com expose(obj, pc, "createAnswer"); 3607ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com expose(obj, pc, "addEventListener"); 3707ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com expose(obj, pc, "addIceCandidate", { 0: RTCIceCandidate}); 3807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com expose(obj, pc, "setRemoteDescription", { 0: RTCSessionDescription }); 3907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com expose(obj, pc, "setLocalDescription", { 0: RTCSessionDescription }); 4007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 4107ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com obj.addStream = function(stream) { 4207ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com console.log("Adding local stream."); 4307ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com var tempStream = localStreams[stream.id]; 4407ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com if (!tempStream) { 4507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com console.log("Undefined stream!"); 4607ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com return; 4707ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com } 4807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com pc.addStream(tempStream); 4907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com }; 5007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 5124f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com // Return an array of Objects, each Object is a copy of RTCStateReport 5224f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com // and has the following attributes (id, type, names, and stats). 5324f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com // names: array originaly returned by calling RTCStateReport.names(). 5424f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com // stats: dictionary of stat name as key and stat value as dictionary 5524f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com // value. 5624f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com obj.getStats = function(callback, mediaTrack) { 5724f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com pc.getStats(onStatsReady, mediaTrack); 5824f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com 5924f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com function onStatsReady(stateResponse) { 6024f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com var outputReports = []; 6124f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com var reports = stateResponse.result(); 6224f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com for (index in reports) { 6324f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com var report = {}; 6424f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com report.id = reports[index].id; 6524f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com report.type = reports[index].type; 6624f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com report.names = reports[index].names(); 6724f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com report.stats = []; 6824f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com populateStats(reports[index], report.stats); 6924f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com 7024f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com outputReports.push(report); 7124f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com } 7224f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com 7324f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com callback(outputReports); 7424f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com } 7524f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com 7624f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com function populateStats(report, stats) { 7724f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com var names = report.names(); 7824f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com for (index in names) { 7924f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com stats.push({ 8024f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com name: names[index], 8124f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com stat: report.stat(names[index]), 8224f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com }); 8324f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com } 8424f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com 8524f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com } 8624f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com }; 8724f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com 8807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com pc.addEventListener('addstream', function(event) { 8907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com remoteStreams[event.stream.id] = event.stream; 9007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com }); 9107ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 9207ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com doneCallback(obj); 9324f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com}; 9407ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 9507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.comfunction showStream(streamId, autoplay, muted) { 9607ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com var stream = getStreamFromIdentifier_(streamId); 9707ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com var video = document.createElement('video'); 9807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com video.autoplay = autoplay; 9907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com video.muted = muted; 10007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com document.body.appendChild(video); 10107ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com video.src = URL.createObjectURL(stream); 10207ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com console.log("Stream " + stream.id + " attached to video element"); 103468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org}; 104468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org 10507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.comfunction getStreamFromIdentifier_(id) { 10607ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com var tempStream = localStreams[id]; 10707ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com if (tempStream) 10807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com return tempStream; 10907ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com tempStream = remoteStreams[id]; 11007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com if (tempStream) 11107ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com return tempStream; 11207ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com console.log(id + " is not id for stream."); 11307ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com return null; 11424f62e1a28f8e3f9e50e55c2ac271e2b063c02f0houssainy@google.com}; 11507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com 116fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.comfunction downloadFile(path, onSuccess, onError) { 117fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com var xhr = new XMLHttpRequest(); 118fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com function onResult() { 119fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com if (xhr.readyState != 4) 120fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com return; 121fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com 122fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com if (xhr.status != 200) { 123fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com onError("Download request failed!"); 124fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com return; 125fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com } 126fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com onSuccess(xhr.responseText); 127fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com } 128fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com 129fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com xhr.onreadystatechange = onResult; 130fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com xhr.open('GET', path, true); 131fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com xhr.send(); 132fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com}; 133fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com 13407ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.comconnectToServer({ 13507ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com ping: ping, 13607ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com getUserMedia: getUserMedia, 13707ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com createPeerConnection: createPeerConnection, 13807ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com showStream: showStream, 139fce8f5d31958e8f7c49328449da80267df87fbbchoussainy@google.com downloadFile: downloadFile, 14007ca949346767d15c99268e7c415bb2c78748bfahoussainy@google.com}); 141