-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffscreen.js
More file actions
151 lines (130 loc) · 4.3 KB
/
offscreen.js
File metadata and controls
151 lines (130 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Listen for messages from the service worker
chrome.runtime.onMessage.addListener((message, sender) => {
switch (message.type) {
case "start-recording":
startRecording(message.data);
break;
case "stop-recording":
stopRecording();
break;
default:
console.warn("Unknown request type:");
}
return true;
});
let mediaRecorder = null;
let recordedChunks = [];
/**
* Stops the recording and handles the cleanup.
*/
async function stopRecording () {
console.log("[offscreen] Stopping recording");
if (mediaRecorder?.state === "recording") {
mediaRecorder.stop();
mediaRecorder.stream.getTracks().forEach((track) => track.stop());
}
await stopTabCapture();
}
/**
* Stops the tab capture session if active.
*/
async function stopTabCapture () {
try {
const stream = await chrome.tabCapture.getCapturedTabs();
if (stream && stream.length > 0) {
stream[0].getTracks().forEach((track) => track.stop());
}
} catch (error) {
throw new Error("[offscreen] Error stopping tab capture:", error);
}
}
/**
* Starts the recording process for the provided stream ID.
* @param {string} streamId - The stream ID for the tab to be recorded.
*/
async function startRecording (streamId) {
try {
if (mediaRecorder?.state === "recording") {
throw new Error("[offscreen] Recording is already in progress.");
}
// Create media streams for tab capture and microphone
const tabStream = await getTabMediaStream(streamId);
const micStream = await getMicrophoneStream();
// Combine tab and microphone streams
const combinedStream = combineMediaStreams(tabStream, micStream);
mediaRecorder = new MediaRecorder(combinedStream, {
mimeType: "video/webm",
videoBitsPerSecond: 5000000,
});
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.onstop = handleRecordingStop;
mediaRecorder.start();
} catch (error) {
console.error("[offscreen] Error starting recording:", error);
}
}
/**
* Gets the media stream for the tab being captured.
* @param {string} streamId - The stream ID for the tab.
* @returns {Promise<MediaStream>} - The media stream for the tab.
*/
async function getTabMediaStream (streamId) {
return navigator.mediaDevices.getUserMedia({
audio: {
mandatory: {
chromeMediaSource: "tab",
chromeMediaSourceId: streamId,
},
},
video: {
mandatory: {
chromeMediaSource: "tab",
chromeMediaSourceId: streamId,
maxWidth: 1920,
maxHeight: 1080,
maxFrameRate: 30,
},
},
});
}
/**
* Gets the media stream for the microphone.
* @returns {Promise<MediaStream>} - The media stream for the microphone.
*/
async function getMicrophoneStream () {
return navigator.mediaDevices.getUserMedia({
audio: { echoCancellation: false },
});
}
/**
* Combines the tab and microphone media streams.
* @param {MediaStream} tabStream - The media stream for the tab.
* @param {MediaStream} micStream - The media stream for the microphone.
* @returns {MediaStream} - The combined media stream.
*/
function combineMediaStreams (tabStream, micStream) {
const audioContext = new AudioContext();
const audioDestination = audioContext.createMediaStreamDestination();
audioContext.createMediaStreamSource(micStream).connect(audioDestination);
audioContext.createMediaStreamSource(tabStream).connect(audioDestination);
return new MediaStream([
tabStream.getVideoTracks()[0],
audioDestination.stream.getTracks()[0],
]);
}
/**
* Handles the `ondataavailable` event for the MediaRecorder.
* @param {BlobEvent} event - The event containing the recorded data.
*/
function handleDataAvailable (event) {
recordedChunks.push(event.data);
}
/**
* Handles the `onstop` event for the MediaRecorder.
*/
async function handleRecordingStop () {
const recordedBlob = new Blob(recordedChunks, { type: "video/webm" });
const videoURL = URL.createObjectURL(recordedBlob);
window.open(videoURL);
recordedChunks = [];
}