|
1 | | -window.onload = () => { |
2 | | - let streamId; |
3 | | - let interval = setInterval(() => { |
4 | | - streamId = localStorage.getItem("streamId"); |
5 | | - if (streamId) { |
6 | | - clearInterval(interval); |
7 | | - setStreamId(streamId); |
8 | | - } |
9 | | - }, 500); |
10 | | -}; |
| 1 | +chrome.runtime.onMessage.addListener(function (request) { |
| 2 | + const { targetTabId, consumerTabId } = request; |
| 3 | + testGetMediaStreamId(targetTabId, consumerTabId); |
| 4 | +}); |
11 | 5 |
|
12 | | -function setStreamId(streamId) { |
13 | | - navigator.mediaDevices |
14 | | - .getUserMedia({ |
15 | | - audio: { |
16 | | - mandatory: { |
17 | | - chromeMediaSource: "tab", |
18 | | - chromeMediaSourceId: streamId, |
| 6 | +function testGetMediaStreamId(targetTabId, consumerTabId) { |
| 7 | + chrome.tabCapture.getMediaStreamId( |
| 8 | + { targetTabId, consumerTabId }, |
| 9 | + function (streamId) { |
| 10 | + if (!streamId) return; |
| 11 | + navigator.webkitGetUserMedia( |
| 12 | + { |
| 13 | + audio: { |
| 14 | + mandatory: { |
| 15 | + chromeMediaSource: "tab", // The media source must be 'tab' here. |
| 16 | + chromeMediaSourceId: streamId, |
| 17 | + }, |
| 18 | + }, |
| 19 | + video: false, |
| 20 | + }, |
| 21 | + function (stream) { |
| 22 | + draw(stream); |
19 | 23 | }, |
20 | | - }, |
21 | | - }) |
22 | | - .then((tabStream) => { |
23 | | - // at this point the sound of the tab becomes muted with no way to unmute it |
24 | | - const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); |
25 | | - const source = audioCtx.createMediaStreamSource(tabStream); |
26 | | - const analyser = audioCtx.createAnalyser(); |
27 | | - analyser.fftSize = 2048; |
28 | | - const bufferLength = analyser.frequencyBinCount; |
29 | | - const dataArray = new Uint8Array(bufferLength); |
30 | | - source.connect(analyser); |
31 | | - analyser.connect(audioCtx.destination); |
| 24 | + function (error) { |
| 25 | + console.error(error); |
| 26 | + } |
| 27 | + ); |
| 28 | + } |
| 29 | + ); |
| 30 | +} |
32 | 31 |
|
33 | | - const canvas = document.createElement("canvas"); |
34 | | - canvas.width = 800; |
35 | | - canvas.height = 200; |
36 | | - canvas.style.cssText = |
37 | | - "position: fixed; top: 0; left: 0; z-index: 2147483647; background: #333a;"; |
38 | | - document.body.appendChild(canvas); |
39 | | - const canvasCtx = canvas.getContext("2d"); |
| 32 | +function draw(stream) { |
| 33 | + // at this point the sound of the tab becomes muted with no way to unmute it |
| 34 | + const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); |
| 35 | + const source = audioCtx.createMediaStreamSource(stream); |
| 36 | + const analyser = audioCtx.createAnalyser(); |
| 37 | + analyser.fftSize = 2048; |
| 38 | + const bufferLength = analyser.frequencyBinCount; |
| 39 | + const dataArray = new Uint8Array(bufferLength); |
| 40 | + source.connect(analyser); |
| 41 | + analyser.connect(audioCtx.destination); |
40 | 42 |
|
41 | | - function draw() { |
42 | | - analyser.getByteFrequencyData(dataArray); |
43 | | - canvasCtx.clearRect(0, 0, canvas.width, canvas.height); |
44 | | - canvasCtx.beginPath(); |
45 | | - const barWidth = ~~(bufferLength / canvas.width); |
46 | | - for (let x = 0; x < canvas.width; x++) { |
47 | | - let i = x * barWidth; |
48 | | - let item = dataArray[i]; |
49 | | - const barHeight = map(item, 0, 255, 0, canvas.height); |
50 | | - canvasCtx.lineTo(x, canvas.height - barHeight); |
51 | | - } |
52 | | - canvasCtx.strokeStyle = "rgba(255, 255, 255, 0.9)"; |
53 | | - canvasCtx.stroke(); |
54 | | - requestAnimationFrame(draw); |
55 | | - } |
| 43 | + const canvas = document.createElement("canvas"); |
| 44 | + canvas.width = 800; |
| 45 | + canvas.height = 200; |
| 46 | + canvas.style.cssText = |
| 47 | + "position: fixed; top: 0; left: 0; z-index: 2147483647; background: #333a;"; |
| 48 | + document.body.appendChild(canvas); |
| 49 | + const canvasCtx = canvas.getContext("2d"); |
| 50 | + |
| 51 | + function draw() { |
| 52 | + analyser.getByteFrequencyData(dataArray); |
| 53 | + canvasCtx.clearRect(0, 0, canvas.width, canvas.height); |
| 54 | + canvasCtx.beginPath(); |
| 55 | + const barWidth = ~~(bufferLength / canvas.width); |
| 56 | + for (let x = 0; x < canvas.width; x++) { |
| 57 | + let i = x * barWidth; |
| 58 | + let item = dataArray[i]; |
| 59 | + const barHeight = map(item, 0, 255, 0, canvas.height); |
| 60 | + canvasCtx.lineTo(x, canvas.height - barHeight); |
| 61 | + } |
| 62 | + canvasCtx.strokeStyle = "rgba(255, 255, 255, 0.9)"; |
| 63 | + canvasCtx.stroke(); |
| 64 | + requestAnimationFrame(draw); |
| 65 | + } |
56 | 66 |
|
57 | | - function map(x, in_min, in_max, out_min, out_max) { |
58 | | - return ( |
59 | | - ((x - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min |
60 | | - ); |
61 | | - } |
| 67 | + function map(x, in_min, in_max, out_min, out_max) { |
| 68 | + return ((x - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min; |
| 69 | + } |
62 | 70 |
|
63 | | - requestAnimationFrame(draw); |
64 | | - }) |
65 | | - .catch((e) => { |
66 | | - console.log("ERROR", e); |
67 | | - }); |
| 71 | + requestAnimationFrame(draw); |
68 | 72 | } |
0 commit comments