aboutsummaryrefslogtreecommitdiff
path: root/extension/content_script.js
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2023-08-30 18:11:57 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2023-08-30 18:11:57 +0200
commit284fc661dc7f18aa32d0dbcd8e7f98cb16af4bb7 (patch)
tree2aa2fd491858fc572ff6b95806b69ec9a79b29c8 /extension/content_script.js
parent8c7fb565690890d57b6af20ad0771a48cf82b4c7 (diff)
Add new RTC code
Diffstat (limited to 'extension/content_script.js')
-rw-r--r--extension/content_script.js94
1 files changed, 58 insertions, 36 deletions
diff --git a/extension/content_script.js b/extension/content_script.js
index 6785a65..8ca721a 100644
--- a/extension/content_script.js
+++ b/extension/content_script.js
@@ -40,9 +40,9 @@ const internals = {
internals.socket.on('connect', function() {
- console.log("Connected to signaling server, group: ", data.currentUrl);
+ console.log('Connected to signaling server, group: ', data.currentUrl);
internals.socket.emit('join', {
- 'url': data.currentUrl,
+ room: data.currentUrl,
});
});
@@ -53,58 +53,80 @@ const internals = {
internals.socket.on('addPeer', function(data) {
- console.log(data);
- Peers.add(data.peer_id, internals.tada);
- const peerId = data.peer_id;
+ Peers.add(data.peerId, internals.tada);
+ const peerId = data.peerId;
- const peerConn = new RTCPeerConnection(
- {"iceServers": internals.kIceServers},
- {"optional": [{"DtlsSrtpKeyAgreement": true}]}
+ const peerConnection = new RTCPeerConnection(
+ { iceServers: internals.kIceServers },
+ { optional: [{ DtlsSrtpKeyAgreement: true }] }
);
- internals.peers[peerId] = peerConn;
+ internals.peers[peerId] = peerConnection;
+ mediaStream.getTracks().forEach((track) => {
+ peerConnection.addTrack(track, localStream);
+ });
- peerConn.onicecandidate = (event) => {
+ peerConnection.onicecandidate = (event) => {
if (event.candidate) {
internals.socket.emit('relayICECandidate', {
- 'peer_id': peerId,
- 'ice_candidate': {
- 'sdpMLineIndex': event.candidate.sdpMLineIndex,
- 'candidate': event.candidate.candidate
- }
+ peerId: peerId,
+ candidate: event.candidate
});
}
}
- peerConn.onaddstream = (stream) => {
- console.log(`Received stream for peer ${peerId}`);
- console.log(stream);
- }
+ const remoteStream = new MediaStream();
+ peerConnection.ontrack = (event) => {
+ remoteStream.addTrack(event.track);
+ const remoteAudioElement = new Audio();
+ remoteAudioElement.srcObject = remoteStream;
+ remoteAudioElement.play();
+ };
- peerConn.addStream(mediaStream);
+ peerConnection.onnegotiationneeded = async () => {
+ console.log("Creating RTC offer to ", peerId);
+ const offer = await peerConnection.createOffer();
+ await peerConnection.setLocalDescription(offer);
- if (data.should_create_offer) {
- console.log("Creating RTC offer to ", peerId);
- peerConn.createOffer((local_description) => {
- console.log("Local offer description is: ", local_description);
- peerConn.setLocalDescription(local_description, () => {
- internals.socket.emit('relaySessionDescription', {
- 'peer_id': peerId,
- 'session_description': local_description
- });
+ // Emit the offer to the peer
+ socket.emit('relayOffer', { offer, peerId });
+ };
- console.log("Offer setLocalDescription succeeded");
- }, () => { console.log("Offer setLocalDescription failed!"); }
- );
- },
- (error) => { console.log("Error sending offer: ", error) }
- );
- }
console.log(`There are now ${Peers.count()} participants`);
});
+ socket.on('offerReceived', async (data) => {
+
+ const peerConnection = internals.peers[data.peerId];
+
+ const offer = new RTCSessionDescription(data.offer);
+ await peerConnection.setRemoteDescription(offer);
+
+ const answer = await peerConnection.createAnswer();
+ await peerConnection.setLocalDescription(answer);
+
+ // Send the answer to the peer
+ socket.emit('relayAnswer', { answer, peerId: data.peerId });
+ });
+
+ socket.on('answerReceived', async (data) => {
+
+ const peerConnection = internals.peers[data.peerId];
+ const answer = new RTCSessionDescription(data.answer);
+ await peerConnection.setRemoteDescription(answer);
+ });
+
+ socket.on('ICECandidateReceived', async (data) => {
+
+ const peerConnection = internals.peers[data.peerId];
+ const candidate = new RTCIceCandidate(data.candidate);
+ await peerConnection.addIceCandidate(candidate);
+ });
+
+
internals.socket.on('removePeer', function() {
+ delete internals.peers[data.peerId];
Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids.
console.log(`There are now ${Peers.count()} participants`);
});