-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
203 lines (179 loc) · 5.57 KB
/
options.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* global chrome, MediaRecorder, FileReader */
// highly inspired from https://github.com/SamuelScheit/puppeteer-stream
const streams = {};
const peers = {};
const socket = io.connect("http://localhost:8000");
window.log = (...args) => {
console.log(...args);
};
socket.on("ping", (room) => {
try {
if (streams[room] === undefined) {
throw new Error(`no stream for room "${room}"`);
}
socket.emit("pong", room);
} catch (err) {
window.log(
"ping error",
room,
socket.id,
err.message || JSON.stringify(err)
);
}
});
socket.on("joined", async (room, socketId) => {
try {
if (streams[room] === undefined) {
throw new Error(`no stream yet for room "${room}"`);
}
window.log(`joined: ${room}`, socketId);
let makingOffer = true;
const remotePeerConnection = new RTCPeerConnection(null);
window.log("Created remote peer connection object remotePeerConnection.");
const stream = streams[room];
stream
.getTracks()
.forEach((track) => remotePeerConnection.addTrack(track, stream));
remotePeerConnection.addEventListener("icecandidate", handleIceCandidate);
// remotePeerConnection.addEventListener('iceconnectionstatechange', handleIceCandidate);
remotePeerConnection.onnegotiationneeded = async () => {
window.log("BROADCASTER onnegotiationneeded", makingOffer);
try {
if (!makingOffer) return;
makingOffer = true;
await setLocalAndSendMessage(await remotePeerConnection.createOffer());
} catch (err) {
window.log("error", err);
} finally {
makingOffer = false;
}
};
remotePeerConnection.oniceconnectionstatechange = () => {
if (remotePeerConnection.iceConnectionState === "failed") {
remotePeerConnection.restartIce();
}
};
function handleIceCandidate(event) {
window.log(
"icecandidate event: ",
event.candidate,
socketId,
remotePeerConnection.iceGatheringState
);
if (event.candidate !== null) {
window.log("new candidate");
sendMessage(socketId, {
type: "candidate",
candidate: event.candidate.toJSON(),
});
} else {
window.log("End of candidates.");
}
}
async function setLocalAndSendMessage(offer) {
await remotePeerConnection.setLocalDescription(offer);
window.log("setLocalAndSendMessage");
sendMessage(socketId, offer);
}
await setLocalAndSendMessage(await remotePeerConnection.createOffer());
makingOffer = false;
window.log("!! peer is ready", socketId);
peers[socketId] = remotePeerConnection;
} catch (err) {
window.log("error", room, socketId, err.message || JSON.stringify(err));
}
// (setLocalAndSendMessage, handleCreateOfferError);
});
// This client receives a message
socket.on("message", (message, peerSocketId) => {
try {
window.log("broadcaster received message:" /* , message */, peerSocketId);
if (!peers[peerSocketId]) {
window.log(
`no peer connection found for ${peerSocketId}... retry later!`
);
return;
}
if (message.type === "answer") {
window.log("set remote description", peerSocketId);
peers[peerSocketId].setRemoteDescription(
new RTCSessionDescription(message)
);
} else if (message.type === "candidate") {
const candidate = new RTCIceCandidate(message.candidate);
peers[peerSocketId].addIceCandidate(candidate);
window.log(
"remotePeerConnection.iceGatheringState",
peers[peerSocketId].iceGatheringState
);
} else {
window.log("!!! unhandled message", message);
}
} catch (err) {
window.log("on receive message error", err.message || JSON.stringify(err));
}
});
/// /////////////////////////////////////////////
function sendMessage(room, message) {
window.log("Client sending message"); // , message, room);
socket.emit("message", message, room);
}
const tabIdToRoom /*= {[tabId: string]: string} */ = {};
chrome.tabs.onRemoved.addListener((tabId, detachInfo) => {
const room = tabIdToRoom[tabId];
window.log("TAB CLOSED", tabId, room, detachInfo);
if (room) {
streams[room] = undefined;
}
});
async function START_RECORDING({ index, room: roomName, zoom }) {
const room = roomName || `room${index}`;
try {
const currentTab = await new Promise((resolve, reject) =>
chrome.tabs.query({ active: true }, (tab) => {
if (!tab) {
reject("no current active tab");
return;
}
resolve(tab);
})
);
if (zoom) {
chrome.tabs.setZoom(currentTab.id, zoom);
}
const width = 1280; // 960; // 1920; // 960
const height = 720; // 540; // 1080; // 540
const fps = 24;
const stream = await new Promise((resolve, reject) =>
chrome.tabCapture.capture(
{
audio: true,
video: true,
videoConstraints: {
mandatory: {
// minWidth: width,
// minHeight: height,
maxWidth: width,
maxHeight: height,
maxFrameRate: fps,
},
},
},
(streamResult) => {
if (!streamResult) {
reject(new Error("got no stream"));
return;
}
resolve(streamResult);
}
)
);
socket.emit("join", room, "broadcaster");
streams[room] = stream;
tabIdToRoom[currentTab.id] = room;
window.log("!!! room ready", room);
} catch (err) {
window.log("ERROR", room, err.message || err || "unknown");
throw err;
}
}