10d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
20d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com//
30d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// Use of this source code is governed by a BSD-style license
40d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// that can be found in the LICENSE file in the root of the source
50d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// tree. An additional intellectual property rights grant can be found
60d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// in the file PATENTS.  All contributing project authors may
70d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// be found in the AUTHORS file in the root of the source tree.
80d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com//
90d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// A video conference between 3 bots streaming video and audio between
100d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// each other.
110d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// The test succeeds after establishing the call between the three
120d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// devices.
130d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com//
140d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com// Note: the source of the video and audio stream is getUserMedia().
150d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.comfunction testTwoWayVideoStreaming(test, bot1, bot2, bot3) {
160d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  var answersCount = 0;
170d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  var statsCollector;
180d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
190d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  test.wait([
200d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      createBotPeerConnectionsWithLocalStream.bind(bot1),
210d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      createBotPeerConnectionsWithLocalStream.bind(bot2),
220d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      createBotPeerConnectionsWithLocalStream.bind(bot3)],
230d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    onPeerConnectionCreated);
240d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
250d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  // done() callback is called with list of peers as argument.
260d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  function createBotPeerConnectionsWithLocalStream(done) {
270d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    var peerConnections = [];
280d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
290d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    this.getUserMedia({video:true, audio:true},
300d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        onUserMediaSuccess.bind(this), test.fail);
310d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
320d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    function onUserMediaSuccess(stream) {
330d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      test.log("User has granted access to local media.");
340d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      this.showStream(stream.id, true, true);
350d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
360d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      test.createTurnConfig(onTurnConfig.bind(this), test.fail);
370d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
380d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      function onTurnConfig(config) {
390d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        this.createPeerConnection(config, addStream.bind(this),
400d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com            test.fail);
410d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        this.createPeerConnection(config, addStream.bind(this),
420d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com            test.fail);
430d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      }
440d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
450d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      function addStream(pc) {
460d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        pc.addStream(stream);
470d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        pc.addEventListener('addstream', onAddStream.bind(this));
480d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
490d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        peerConnections.push(pc);
500d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        if(peerConnections.length == 2)
510d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com          done(peerConnections);
520d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      }
530d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    }
540d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  }
550d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
560d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  function onPeerConnectionCreated(peerConnections1,
570d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      peerConnections2, peerConnections3) {
580d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    test.log("RTC Peers created.");
590d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
600d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    // Bot1 and Bot2
610d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    establichCall(peerConnections1[0], peerConnections2[1]);
620d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    // Bot2 and Bot3
630d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    establichCall(peerConnections2[0], peerConnections3[1]);
640d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    // Bot3 and Bot1
650d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    establichCall(peerConnections3[0], peerConnections1[1]);
660d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  }
670d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
680d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  function establichCall(pc1, pc2) {
690d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    pc1.addEventListener('icecandidate', onIceCandidate.bind(pc2));
700d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    pc2.addEventListener('icecandidate', onIceCandidate.bind(pc1));
710d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
720d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    createOfferAndAnswer(pc1, pc2);
730d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  }
740d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
750d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  function onAddStream(event) {
760d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    test.log("On Add stream.");
770d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    this.showStream(event.stream.id, true, false);
780d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  }
790d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
800d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  function onIceCandidate(event) {
810d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    if(event.candidate) {
820d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      this.addIceCandidate(event.candidate,
830d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com         onAddIceCandidateSuccess, test.fail);
840d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    };
850d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
860d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    function onAddIceCandidateSuccess() {
870d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      test.log("Candidate added successfully");
880d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    };
890d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  }
900d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
910d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  function createOfferAndAnswer(pc1, pc2) {
920d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    test.log("Creating offer.");
930d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    pc1.createOffer(gotOffer, test.fail);
940d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
950d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    function gotOffer(offer) {
960d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      test.log("Got offer");
970d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      pc1.setLocalDescription(offer, onSetSessionDescriptionSuccess, test.fail);
980d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      pc2.setRemoteDescription(offer, onSetSessionDescriptionSuccess,
990d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com          test.fail);
1000d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      test.log("Creating answer");
1010d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      pc2.createAnswer(gotAnswer, test.fail);
1020d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    }
1030d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
1040d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    function gotAnswer(answer) {
1050d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      test.log("Got answer");
1060d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      pc2.setLocalDescription(answer, onSetSessionDescriptionSuccess,
1070d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com          test.fail);
1080d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      pc1.setRemoteDescription(answer, onSetSessionDescriptionSuccess,
1090d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com          test.fail);
1100d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
1110d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      answersCount++;
1120d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      if(answersCount == 3) {
1130d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        // SetTimeout used because creating the three answers will very fast
1140d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        // and test will success and the vm will be closed before establishing
1150d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        // the calls.
1160d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com        setTimeout(function() {
1170d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com            test.done();
1180d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com          }, 5000);
1190d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      }
1200d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    }
1210d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
1220d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    function onSetSessionDescriptionSuccess() {
1230d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com      test.log("Set session description success.");
1240d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com    }
1250d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com  }
1260d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com}
1270d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com
1280d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.comregisterBotTest('threeBotsVideoConference/android+android+chrome',
1290d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com                testTwoWayVideoStreaming, ['android-chrome', 'android-chrome',
1300d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com                'chrome']);
1310d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.comregisterBotTest('threeBotsVideoConference/chrome-chrome-chrome',
1320d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com                testTwoWayVideoStreaming, ['chrome', 'chrome', 'chrome']);
1330d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.comregisterBotTest('threeBotsVideoConference/android-android-android',
1340d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com                testTwoWayVideoStreaming, ['android-chrome', 'android-chrome',
1350d3e254c89d1816bd320dbd717af60d204cefccahoussainy@google.com                'android-chrome']);