forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.js
49 lines (42 loc) · 1.51 KB
/
receiver.js
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Note: |window.currentStream| was set in background.js.
// Stop video play-out, stop the MediaStreamTracks, and set style class to
// "shutdown".
function shutdownReceiver() {
if (!window.currentStream) {
return;
}
var player = document.getElementById('player');
player.srcObject = null;
var tracks = window.currentStream.getTracks();
for (var i = 0; i < tracks.length; ++i) {
tracks[i].stop();
}
window.currentStream = null;
document.body.className = 'shutdown';
}
window.addEventListener('load', function() {
// Start video play-out of the captured audio/video MediaStream once the page
// has loaded.
var player = document.getElementById('player');
player.addEventListener('canplay', function() {
this.volume = 0.75;
this.muted = false;
this.play();
});
player.setAttribute('controls', '1');
player.srcObject = window.currentStream;
// Add onended event listeners. This detects when tab capture was shut down by
// closing the tab being captured.
var tracks = window.currentStream.getTracks();
for (var i = 0; i < tracks.length; ++i) {
tracks[i].addEventListener('ended', function() {
console.log('MediaStreamTrack[' + i + '] ended, shutting down...');
shutdownReceiver();
});
}
});
// Shutdown when the receiver page is closed.
window.addEventListener('beforeunload', shutdownReceiver);