forked from orbitbot/chrome-extensions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
110 lines (97 loc) · 3.31 KB
/
background.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
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
107
108
109
110
// 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.
var g_sessionInfo = {};
/**
* When extension icon clicked, get device list
* Then return the list to popup page
*/
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.browserActionClicked) {
getDeviceList(function(deviceList) {
sendResponse({ returnDeviceList: deviceList });
});
}
return true;
});
function getDeviceList(callback) {
chrome.displaySource.getAvailableSinks(callback);
}
chrome.displaySource.onSessionTerminated.addListener(function(terminatedSink) {
chrome.runtime.sendMessage({ sessionTerminated: true,
currentSinkId: terminatedSink });
if (g_sessionInfo.stream) {
g_sessionInfo.stream.getTracks().forEach(function (track) {
track.stop(); });
delete g_sessionInfo.stream;
}
delete g_sessionInfo.sinkId;
});
chrome.displaySource.onSinksUpdated.addListener(function(updatedSinks) {
console.log('Sinks updated');
chrome.runtime.sendMessage({ sinksUpdated: true,
currentSinkId: g_sessionInfo.sinkId,
sinksList: updatedSinks});
});
function start(sinkId) {
// If no session, proceed.
if (!g_sessionInfo.sinkId) {
g_sessionInfo.sinkId = parseInt(sinkId);
captureTabAndStartSession(g_sessionInfo.sinkId);
}
}
function captureTabAndStartSession(sink_id) {
chrome.tabs.getCurrent(function(tab) {
var video_constraints = {
mandatory: {
chromeMediaSource: 'tab',
minWidth: 1920,
minHeight: 1080,
maxWidth: 1920,
maxHeight: 1080,
minFrameRate: 60,
maxFrameRate: 60
}
};
var constraints = {
audio: true,
video: true,
videoConstraints: video_constraints
};
function onStream(stream) {
g_sessionInfo.stream = stream;
var session_info = {
sinkId: sink_id,
videoTrack: g_sessionInfo.stream.getVideoTracks()[0],
audioTrack: g_sessionInfo.stream.getAudioTracks()[0]
};
function onStarted() {
if (chrome.runtime.error) {
console.log('The Session to sink ' + g_sessionInfo.sinkId
+ 'could not start, error: '
+ chrome.runtime.lastError.message);
} else {
console.log('The Session to sink ' + g_sessionInfo.sinkId
+ ' has started.');
}
}
console.log('Starting session to sink: ' + sink_id);
chrome.displaySource.startSession(session_info, onStarted);
}
chrome.tabCapture.capture(constraints, onStream);
});
}
function stop() {
function onTerminated() {
if (chrome.runtime.lastError) {
console.log('The Session to sink ' + g_sessionInfo.sinkId
+ 'could not terminate, error: '
+ chrome.runtime.lastError.message);
} else {
console.log('The Session to sink ' + g_sessionInfo.sinkId
+ ' has terminated.');
}
}
console.log('Terminating session to sink: ' + g_sessionInfo.sinkId);
chrome.displaySource.terminateSession(g_sessionInfo.sinkId, onTerminated);
}