Chromium Code Reviews| Index: chrome/common/extensions/docs/examples/api/displaySource/tabCast/background.js |
| diff --git a/chrome/common/extensions/docs/examples/api/displaySource/tabCast/background.js b/chrome/common/extensions/docs/examples/api/displaySource/tabCast/background.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f6737f8924031d7eb40a33854563167f7a388fa3 |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/examples/api/displaySource/tabCast/background.js |
| @@ -0,0 +1,109 @@ |
| +// 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 SessionInfo = {}; |
|
asargent_no_longer_on_chrome
2016/03/17 19:24:28
nit: variable names should use camelCase.
https:
|
| + |
| +/** |
| + * 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 (SessionInfo.Stream) { |
| + SessionInfo.Stream.getTracks().forEach(function (track) { track.stop(); }); |
| + delete SessionInfo.Stream; |
| + } |
| + delete SessionInfo.SinkId; |
| +}); |
| + |
| +chrome.displaySource.onSinksUpdated.addListener(function(updatedSinks) { |
| + console.log('Sinks updated'); |
| + chrome.runtime.sendMessage({ sinksUpdated: true, |
| + currentSinkId: SessionInfo.SinkId, |
| + sinksList: updatedSinks}); |
| +}); |
| + |
| +function Start(sinkId) { |
|
asargent_no_longer_on_chrome
2016/03/17 19:24:29
nit: function names should use camelCase
https://
|
| + // If no session, proceed. |
| + if (!SessionInfo.SinkId) { |
| + SessionInfo.SinkId = parseInt(sinkId); |
|
asargent_no_longer_on_chrome
2016/03/17 19:24:28
nit: variable names should use camelCase
|
| + captureTabAndStartSession(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) { |
| + SessionInfo.Stream = stream; |
|
asargent_no_longer_on_chrome
2016/03/17 19:24:29
nit: variable names should use camelCase
|
| + var session_info = { |
| + sinkId: sink_id, |
| + videoTrack: SessionInfo.Stream.getVideoTracks()[0], |
| + audioTrack: SessionInfo.Stream.getAudioTracks()[0] |
| + }; |
| + |
| + function onStarted() { |
| + if (chrome.runtime.error) { |
| + console.log('The Session to sink ' + SessionInfo.SinkId |
| + + 'could not start, error: ' |
| + + chrome.runtime.lastError.message); |
| + } else { |
| + console.log('The Session to sink ' + 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() { |
|
asargent_no_longer_on_chrome
2016/03/17 19:24:29
nit: function names should use camelCase
|
| + function onTerminated() { |
| + if (chrome.runtime.lastError) { |
| + console.log('The Session to sink ' + SessionInfo.SinkId |
| + + 'could not terminate, error: ' |
| + + chrome.runtime.lastError.message); |
| + } else { |
| + console.log('The Session to sink ' + SessionInfo.SinkId |
| + + ' has terminated.'); |
| + } |
| + } |
| + console.log('Terminating session to sink: ' + SessionInfo.SinkId); |
| + chrome.displaySource.terminateSession(SessionInfo.SinkId, onTerminated); |
| +} |