Home | History | Annotate | Download | only in browser
      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.
      8 //
      9 var localStreams = [];
     10 var remoteStreams = [];
     11 
     12 function ping(callback) {
     13   callback("pong");
     14 }
     15 
     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 
     51   pc.addEventListener('addstream', function(event) {
     52     remoteStreams[event.stream.id] = event.stream;
     53   });
     54 
     55   doneCallback(obj);
     56 }
     57 
     58 function showStream(streamId, autoplay, muted) {
     59   var stream = getStreamFromIdentifier_(streamId);
     60   var video = document.createElement('video');
     61   video.autoplay = autoplay;
     62   video.muted = muted;
     63   document.body.appendChild(video);
     64   video.src = URL.createObjectURL(stream);
     65   console.log("Stream " + stream.id + " attached to video element");
     66 };
     67 
     68 function getStreamFromIdentifier_(id) {
     69   var tempStream = localStreams[id];
     70   if (tempStream)
     71     return tempStream;
     72   tempStream = remoteStreams[id];
     73   if (tempStream)
     74     return tempStream;
     75   console.log(id + " is not id for stream.");
     76   return null;
     77 }
     78 
     79 connectToServer({
     80   ping: ping,
     81   getUserMedia: getUserMedia,
     82   createPeerConnection: createPeerConnection,
     83   showStream: showStream,
     84 });
     85