aboutsummaryrefslogtreecommitdiff
path: root/webrtc/tools/rtcbot/test/webrtc_video_streaming.js
blob: 7b0457c96a0d662e33e99cc365d61fefe2f0e25f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS.  All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
//
// A unidirectional video and audio flowing test from bot 1 to bot 2.
// The test succeeds after collecting stats for 10 seconds from both bots
// and then write these stats to a file.
//
// Note: the source of the video and audio stream is getUserMedia().
//
function testVideoStreaming(bot1, bot2) {
  var pc1 = null;
  var pc2 = null;

  var report = test.createStatisticsReport("webrtc_video_streaming");

  test.wait([
      createPeerConnection.bind(bot1),
      createPeerConnection.bind(bot2) ],
    onPeerConnectionCreated);

  function createPeerConnection(done) {
    this.createPeerConnection(done, test.fail);
  }

  function onPeerConnectionCreated(peer1, peer2) {
    test.log("RTC Peers created.");
    pc1 = peer1;
    pc2 = peer2;
    pc1.addEventListener('addstream', test.fail);
    pc2.addEventListener('addstream', onAddStream);
    pc1.addEventListener('icecandidate', onIceCandidate.bind(pc2));
    pc2.addEventListener('icecandidate', onIceCandidate.bind(pc1));

    bot1.getUserMedia({video:true, audio:true}, onUserMediaSuccess, test.fail);

    function onUserMediaSuccess(stream) {
      test.log("User has granted access to local media.");
      pc1.addStream(stream);
      bot1.showStream(stream.id, true, true);

      createOfferAndAnswer();
    }
  }

  function onAddStream(event) {
    test.log("On Add stream.");
    bot2.showStream(event.stream.id, true, false);
  }

  function onIceCandidate(event) {
    if(event.candidate){
      test.log(event.candidate.candidate);
      this.addIceCandidate(event.candidate,
         onAddIceCandidateSuccess, test.fail);
    };

    function onAddIceCandidateSuccess() {
      test.log("Candidate added successfully");
    };
  }

  function createOfferAndAnswer() {
    test.log("Creating offer.");
    pc1.createOffer(gotOffer, test.fail);

    function gotOffer(offer) {
      test.log("Got offer");
      pc1.setLocalDescription(offer, onSetSessionDescriptionSuccess, test.fail);
      pc2.setRemoteDescription(offer, onSetSessionDescriptionSuccess,
          test.fail);
      test.log("Creating answer");
      pc2.createAnswer(gotAnswer, test.fail);
    }

    function gotAnswer(answer) {
      test.log("Got answer");
      pc2.setLocalDescription(answer, onSetSessionDescriptionSuccess,
          test.fail);
      pc1.setRemoteDescription(answer, onSetSessionDescriptionSuccess,
          test.fail);
      collectStats();
    }

    function onSetSessionDescriptionSuccess() {
      test.log("Set session description success.");
    }

    function collectStats() {
      report.collectStatsFromPeerConnection("bot1", pc1);
      report.collectStatsFromPeerConnection("bot2", pc2);

      setTimeout(function() {
        report.finish(test.done);
        }, 10000);
    }
  }
}

test.wait( [ test.spawnBot.bind(test, "alice"),
             test.spawnBot.bind(test, "bob") ],
          testVideoStreaming);