mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
* Remove docs folder. This was a redirect from a GitHub pages site that does not appear to be in use. * Rename api folder to api-samples. * Move examples to functional-samples folder. * Move cookbook sample to functional-samples. * Move tutorials to functional-samples folder. * Move mv2 and apps folders to _archive. * Rename tools folder to .repo. * Move reference folder to functional-samples. * Update README. Update README with new relative links for reorg. * Update README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> --------- Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
111 lines
3.3 KiB
JavaScript
111 lines
3.3 KiB
JavaScript
// 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);
|
|
}
|