-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
103 lines (89 loc) · 3.09 KB
/
Copy pathoffscreen.js
File metadata and controls
103 lines (89 loc) · 3.09 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
// Keep track of active audio elements by tabId so we can change routes later
const activeTabs = new Map();
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'GET_TAB_STATE') {
const tabState = activeTabs.get(message.tabId);
if (tabState) {
sendResponse({ deviceId: tabState.sinkId || 'default', volume: tabState.volume });
} else {
sendResponse(null);
}
return true; // Keep channel open for sendResponse
}
if (message.action === 'CHECK_TAB_CAPTURED') {
sendResponse({ isCaptured: activeTabs.has(message.tabId) });
return true;
}
if (message.action === 'CHANGE_SINK') {
const tabState = activeTabs.get(message.tabId);
if (tabState && tabState.ctx) {
tabState.ctx.setSinkId(message.deviceId)
.then(() => {
tabState.sinkId = message.deviceId;
console.log(`Changed sink for tab ${message.tabId} to ${message.deviceId}`);
})
.catch(console.error);
}
return;
}
if (message.action === 'CHANGE_VOLUME') {
const tabState = activeTabs.get(message.tabId);
if (tabState && tabState.gainNode) {
tabState.gainNode.gain.value = message.volume;
tabState.volume = message.volume;
console.log(`Changed volume for tab ${message.tabId} to ${message.volume}`);
}
return;
}
if (message.action === 'PLAY_STREAM_ON_DEVICE') {
routeStreamToDevice(message.streamId, message.tabId, message.deviceId, message.volume);
}
});
async function routeStreamToDevice(streamId, tabId, deviceId, volume) {
try {
// 1. Capture the tab audio stream using the streamId
const stream = await navigator.mediaDevices.getUserMedia({
audio: {
mandatory: {
chromeMediaSource: 'tab',
chromeMediaSourceId: streamId
}
},
video: false
});
// 2. Create or reuse an AudioContext for low-latency playback
let tabState = activeTabs.get(tabId);
if (!tabState) {
const ctx = new AudioContext({ latencyHint: 'interactive' });
const source = ctx.createMediaStreamSource(stream);
const gainNode = ctx.createGain();
source.connect(gainNode);
gainNode.connect(ctx.destination);
tabState = {
ctx,
gainNode,
sinkId: 'default',
volume: 1.0
};
activeTabs.set(tabId, tabState);
// Clean up if the stream ends (e.g. tab is closed)
stream.getTracks()[0].onended = () => {
tabState.ctx.close();
activeTabs.delete(tabId);
};
}
// 3. Route the audio to the selected speaker deviceId and set volume
if (volume !== undefined) {
tabState.gainNode.gain.value = volume;
tabState.volume = volume;
}
await tabState.ctx.setSinkId(deviceId);
tabState.sinkId = deviceId;
if (tabState.ctx.state === 'suspended') {
await tabState.ctx.resume();
}
console.log(`Successfully routed stream for tab ${tabId} to device ${deviceId} at volume ${tabState.volume}`);
} catch (error) {
console.error('Error routing tab audio:', error);
}
}