OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 var g_sessionInfo = {}; |
| 6 |
| 7 /** |
| 8 * When extension icon clicked, get device list |
| 9 * Then return the list to popup page |
| 10 */ |
| 11 chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { |
| 12 if (message.browserActionClicked) { |
| 13 getDeviceList(function(deviceList) { |
| 14 sendResponse({ returnDeviceList: deviceList }); |
| 15 }); |
| 16 } |
| 17 return true; |
| 18 }); |
| 19 |
| 20 function getDeviceList(callback) { |
| 21 chrome.displaySource.getAvailableSinks(callback); |
| 22 } |
| 23 |
| 24 chrome.displaySource.onSessionTerminated.addListener(function(terminatedSink) { |
| 25 chrome.runtime.sendMessage({ sessionTerminated: true, |
| 26 currentSinkId: terminatedSink }); |
| 27 |
| 28 if (g_sessionInfo.stream) { |
| 29 g_sessionInfo.stream.getTracks().forEach(function (track) { |
| 30 track.stop(); }); |
| 31 delete g_sessionInfo.stream; |
| 32 } |
| 33 delete g_sessionInfo.sinkId; |
| 34 }); |
| 35 |
| 36 chrome.displaySource.onSinksUpdated.addListener(function(updatedSinks) { |
| 37 console.log('Sinks updated'); |
| 38 chrome.runtime.sendMessage({ sinksUpdated: true, |
| 39 currentSinkId: g_sessionInfo.sinkId, |
| 40 sinksList: updatedSinks}); |
| 41 }); |
| 42 |
| 43 function start(sinkId) { |
| 44 // If no session, proceed. |
| 45 if (!g_sessionInfo.sinkId) { |
| 46 g_sessionInfo.sinkId = parseInt(sinkId); |
| 47 captureTabAndStartSession(g_sessionInfo.sinkId); |
| 48 } |
| 49 } |
| 50 |
| 51 function captureTabAndStartSession(sink_id) { |
| 52 chrome.tabs.getCurrent(function(tab) { |
| 53 var video_constraints = { |
| 54 mandatory: { |
| 55 chromeMediaSource: 'tab', |
| 56 minWidth: 1920, |
| 57 minHeight: 1080, |
| 58 maxWidth: 1920, |
| 59 maxHeight: 1080, |
| 60 minFrameRate: 60, |
| 61 maxFrameRate: 60 |
| 62 } |
| 63 }; |
| 64 |
| 65 var constraints = { |
| 66 audio: true, |
| 67 video: true, |
| 68 videoConstraints: video_constraints |
| 69 }; |
| 70 |
| 71 function onStream(stream) { |
| 72 g_sessionInfo.stream = stream; |
| 73 var session_info = { |
| 74 sinkId: sink_id, |
| 75 videoTrack: g_sessionInfo.stream.getVideoTracks()[0], |
| 76 audioTrack: g_sessionInfo.stream.getAudioTracks()[0] |
| 77 }; |
| 78 |
| 79 function onStarted() { |
| 80 if (chrome.runtime.error) { |
| 81 console.log('The Session to sink ' + g_sessionInfo.sinkId |
| 82 + 'could not start, error: ' |
| 83 + chrome.runtime.lastError.message); |
| 84 } else { |
| 85 console.log('The Session to sink ' + g_sessionInfo.sinkId |
| 86 + ' has started.'); |
| 87 } |
| 88 } |
| 89 console.log('Starting session to sink: ' + sink_id); |
| 90 chrome.displaySource.startSession(session_info, onStarted); |
| 91 } |
| 92 |
| 93 chrome.tabCapture.capture(constraints, onStream); |
| 94 }); |
| 95 } |
| 96 |
| 97 function stop() { |
| 98 function onTerminated() { |
| 99 if (chrome.runtime.lastError) { |
| 100 console.log('The Session to sink ' + g_sessionInfo.sinkId |
| 101 + 'could not terminate, error: ' |
| 102 + chrome.runtime.lastError.message); |
| 103 } else { |
| 104 console.log('The Session to sink ' + g_sessionInfo.sinkId |
| 105 + ' has terminated.'); |
| 106 } |
| 107 } |
| 108 console.log('Terminating session to sink: ' + g_sessionInfo.sinkId); |
| 109 chrome.displaySource.terminateSession(g_sessionInfo.sinkId, onTerminated); |
| 110 } |
OLD | NEW |