|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +chrome.runtime.onMessage.addListener(async (message) => { |
| 16 | + if (message.target === 'offscreen') { |
| 17 | + switch (message.type) { |
| 18 | + case 'start-recording': |
| 19 | + startRecording(message.data); |
| 20 | + break; |
| 21 | + case 'stop-recording': |
| 22 | + stopRecording(); |
| 23 | + break; |
| 24 | + default: |
| 25 | + throw new Error('Unrecognized message:', message.type); |
| 26 | + } |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | +let recorder; |
| 31 | +let data = []; |
| 32 | + |
| 33 | +async function startRecording(streamId) { |
| 34 | + if (recorder?.state === 'recording') { |
| 35 | + throw new Error('Called startRecording while recording is in progress.'); |
| 36 | + } |
| 37 | + |
| 38 | + const media = await navigator.mediaDevices.getUserMedia({ |
| 39 | + audio: { |
| 40 | + mandatory: { |
| 41 | + chromeMediaSource: 'tab', |
| 42 | + chromeMediaSourceId: streamId |
| 43 | + } |
| 44 | + }, |
| 45 | + video: { |
| 46 | + mandatory: { |
| 47 | + chromeMediaSource: 'tab', |
| 48 | + chromeMediaSourceId: streamId |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + // Continue to play the captured audio to the user. |
| 54 | + const output = new AudioContext(); |
| 55 | + const source = output.createMediaStreamSource(media); |
| 56 | + source.connect(output.destination); |
| 57 | + |
| 58 | + // Start recording. |
| 59 | + recorder = new MediaRecorder(media, { mimeType: 'video/webm' }); |
| 60 | + recorder.ondataavailable = (event) => data.push(event.data); |
| 61 | + recorder.onstop = () => { |
| 62 | + const blob = new Blob(data, { type: 'video/webm' }); |
| 63 | + window.open(URL.createObjectURL(blob), '_blank'); |
| 64 | + |
| 65 | + // Clear state ready for next recording |
| 66 | + recorder = undefined; |
| 67 | + data = []; |
| 68 | + }; |
| 69 | + recorder.start(); |
| 70 | + |
| 71 | + // Record the current state in the URL. This provides a very low-bandwidth |
| 72 | + // way of communicating with the service worker (the service worker can check |
| 73 | + // the URL of the document and see the current recording state). We can't |
| 74 | + // store that directly in the service worker as it may be terminated while |
| 75 | + // recording is in progress. We could write it to storage but that slightly |
| 76 | + // increases the risk of things getting out of sync. |
| 77 | + window.location.hash = 'recording'; |
| 78 | +} |
| 79 | + |
| 80 | +async function stopRecording() { |
| 81 | + recorder.stop(); |
| 82 | + |
| 83 | + // Stopping the tracks makes sure the recording icon in the tab is removed. |
| 84 | + recorder.stream.getTracks().forEach((t) => t.stop()); |
| 85 | + |
| 86 | + // Update current state in URL |
| 87 | + window.location.hash = ''; |
| 88 | + |
| 89 | + // Note: In a real extension, you would want to write the recording to a more |
| 90 | + // permanent location (e.g IndexedDB) and then close the offscreen document, |
| 91 | + // to avoid keeping a document around unnecessarily. Here we avoid that to |
| 92 | + // make sure the browser keeps the Object URL we create (see above) and to |
| 93 | + // keep the sample fairly simple to follow. |
| 94 | +} |
0 commit comments