andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 1 | // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the LICENSE file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 8 | // |
| 9 | var localStreams = []; |
| 10 | var remoteStreams = []; |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 11 | |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 12 | function ping(callback) { |
| 13 | callback("pong"); |
| 14 | } |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 15 | |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 16 | function getUserMedia(constraints, onSuccessCallback, onFailCallback){ |
| 17 | console.log("Getting user media."); |
| 18 | navigator.webkitGetUserMedia(constraints, |
| 19 | onSuccessCallbackWraper, onFailCallback); |
| 20 | |
| 21 | function onSuccessCallbackWraper(stream) { |
| 22 | console.log("GetUserMedia success."); |
| 23 | localStreams[stream.id] = stream; |
| 24 | onSuccessCallback(stream); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | function createPeerConnection(doneCallback, failCallback) { |
| 29 | console.log("Creating peer connection"); |
| 30 | var obj = {}; |
| 31 | var pc = new webkitRTCPeerConnection(null); |
| 32 | |
| 33 | expose(obj, pc, "close"); |
| 34 | expose(obj, pc, "createOffer"); |
| 35 | expose(obj, pc, "createAnswer"); |
| 36 | expose(obj, pc, "addEventListener"); |
| 37 | expose(obj, pc, "addIceCandidate", { 0: RTCIceCandidate}); |
| 38 | expose(obj, pc, "setRemoteDescription", { 0: RTCSessionDescription }); |
| 39 | expose(obj, pc, "setLocalDescription", { 0: RTCSessionDescription }); |
| 40 | |
| 41 | obj.addStream = function(stream) { |
| 42 | console.log("Adding local stream."); |
| 43 | var tempStream = localStreams[stream.id]; |
| 44 | if (!tempStream) { |
| 45 | console.log("Undefined stream!"); |
| 46 | return; |
| 47 | } |
| 48 | pc.addStream(tempStream); |
| 49 | }; |
| 50 | |
houssainy@google.com | 24f62e1 | 2014-09-29 09:36:28 +0000 | [diff] [blame] | 51 | // Return an array of Objects, each Object is a copy of RTCStateReport |
| 52 | // and has the following attributes (id, type, names, and stats). |
| 53 | // names: array originaly returned by calling RTCStateReport.names(). |
| 54 | // stats: dictionary of stat name as key and stat value as dictionary |
| 55 | // value. |
| 56 | obj.getStats = function(callback, mediaTrack) { |
| 57 | pc.getStats(onStatsReady, mediaTrack); |
| 58 | |
| 59 | function onStatsReady(stateResponse) { |
| 60 | var outputReports = []; |
| 61 | var reports = stateResponse.result(); |
| 62 | for (index in reports) { |
| 63 | var report = {}; |
| 64 | report.id = reports[index].id; |
| 65 | report.type = reports[index].type; |
| 66 | report.names = reports[index].names(); |
| 67 | report.stats = []; |
| 68 | populateStats(reports[index], report.stats); |
| 69 | |
| 70 | outputReports.push(report); |
| 71 | } |
| 72 | |
| 73 | callback(outputReports); |
| 74 | } |
| 75 | |
| 76 | function populateStats(report, stats) { |
| 77 | var names = report.names(); |
| 78 | for (index in names) { |
| 79 | stats.push({ |
| 80 | name: names[index], |
| 81 | stat: report.stat(names[index]), |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | }; |
| 87 | |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 88 | pc.addEventListener('addstream', function(event) { |
| 89 | remoteStreams[event.stream.id] = event.stream; |
| 90 | }); |
| 91 | |
| 92 | doneCallback(obj); |
houssainy@google.com | 24f62e1 | 2014-09-29 09:36:28 +0000 | [diff] [blame] | 93 | }; |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 94 | |
| 95 | function showStream(streamId, autoplay, muted) { |
| 96 | var stream = getStreamFromIdentifier_(streamId); |
| 97 | var video = document.createElement('video'); |
| 98 | video.autoplay = autoplay; |
| 99 | video.muted = muted; |
| 100 | document.body.appendChild(video); |
| 101 | video.src = URL.createObjectURL(stream); |
| 102 | console.log("Stream " + stream.id + " attached to video element"); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 105 | function getStreamFromIdentifier_(id) { |
| 106 | var tempStream = localStreams[id]; |
| 107 | if (tempStream) |
| 108 | return tempStream; |
| 109 | tempStream = remoteStreams[id]; |
| 110 | if (tempStream) |
| 111 | return tempStream; |
| 112 | console.log(id + " is not id for stream."); |
| 113 | return null; |
houssainy@google.com | 24f62e1 | 2014-09-29 09:36:28 +0000 | [diff] [blame] | 114 | }; |
houssainy@google.com | 07ca949 | 2014-09-22 13:52:39 +0000 | [diff] [blame] | 115 | |
| 116 | connectToServer({ |
| 117 | ping: ping, |
| 118 | getUserMedia: getUserMedia, |
| 119 | createPeerConnection: createPeerConnection, |
| 120 | showStream: showStream, |
| 121 | }); |