Skip to content

Commit 961bce4

Browse files
authored
Make server.js delete old peer connections on the same IP (#118)
* Make server.js delete old peer connections on the same IP * fixes from testing on lovelace * Include the IP in the request * Add note about one publisher or subscriber at a time
1 parent be2d0ea commit 961bce4

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

feedingwebapp/server.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,43 @@ const bodyParser = require('body-parser')
1212
var cors = require('cors')
1313
const webrtc = require('wrtc')
1414

15-
let senderStream = {}
15+
let senderStream = {} // key: topic, value: MediaStream
16+
// NOTE: There is something wrong with the IPs being passed in being
17+
// all or mostly the same. As a result, in essense this only allows
18+
// one publisher or subscriber at a time.
19+
let publishPeers = {} // key: IP4:topic, value: RTCPeerConnection
20+
let subscribePeers = {} // key: IP4:topic, value: RTCPeerConnection
1621

1722
app.use(bodyParser.json())
1823
app.use(bodyParser.urlencoded({ extended: true }))
1924
app.use(cors())
2025

2126
app.post('/subscribe', async ({ body }, res) => {
22-
console.log('got subscriber on topic: ' + body.topic)
27+
console.log('got subscriber on IP', body.ip, 'for topic', body.topic)
2328

2429
// Configure the peer connection
2530
const peer = new webrtc.RTCPeerConnection({
2631
iceServers: [
2732
{
28-
urls: 'stun:stun.stunprotocol.org'
33+
urls: 'stun:stun1.l.google.com:19302'
2934
}
3035
]
3136
})
37+
38+
// Close any old peers on the same IP address
39+
const topic = body.topic
40+
const key = body.ip + ':' + topic
41+
if (key in subscribePeers) {
42+
const senders = subscribePeers[key].getSenders()
43+
senders.forEach((sender) => subscribePeers[key].removeTrack(sender))
44+
subscribePeers[key].close()
45+
}
46+
subscribePeers[key] = peer
47+
3248
const desc = new webrtc.RTCSessionDescription(body.sdp)
3349
await peer.setRemoteDescription(desc)
3450

3551
// Add the publisher's video stream to the subscriber's peer connection
36-
const topic = body.topic
3752
if (topic in senderStream) {
3853
senderStream[topic].getTracks().forEach((track) => peer.addTrack(track, senderStream[topic]))
3954
}
@@ -50,19 +65,28 @@ app.post('/subscribe', async ({ body }, res) => {
5065
})
5166

5267
app.post('/publish', async ({ body }, res) => {
53-
console.log('got publisher on topic: ' + body.topic)
68+
console.log('got publisher on IP', body.ip, 'for topic', body.topic)
5469

5570
// Configure the peer connection
5671
const peer = new webrtc.RTCPeerConnection({
5772
iceServers: [
5873
{
59-
urls: 'stun:stun.stunprotocol.org'
74+
urls: 'stun:stun1.l.google.com:19302'
6075
}
6176
]
6277
})
6378

64-
// Send the publisher's video stream to all subscribers on that topic
79+
// Close any old peers on the same IP address
6580
const topic = body.topic
81+
const key = body.ip + ':' + topic
82+
if (key in publishPeers) {
83+
const senders = publishPeers[key].getSenders()
84+
senders.forEach((sender) => publishPeers[key].removeTrack(sender))
85+
publishPeers[key].close()
86+
}
87+
publishPeers[key] = peer
88+
89+
// Send the publisher's video stream to all subscribers on that topic
6690
peer.ontrack = (e) => handleTrackEvent(e, topic)
6791

6892
// Create an answer to the publisher's offer

feedingwebapp/src/Pages/Home/VideoFeed.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import PropTypes from 'prop-types'
66
// Local Imports
77
import { CAMERA_FEED_TOPIC, REALSENSE_WIDTH, REALSENSE_HEIGHT } from '../Constants'
88
import { useWindowSize } from '../../helpers'
9-
import { createPeerConnection } from '../../webrtc/webrtc_helpers'
9+
import { createPeerConnection, closePeerConnection } from '../../webrtc/webrtc_helpers'
1010

1111
/**
1212
* Takes in an imageWidth and imageHeight, and returns a width and height that
@@ -100,7 +100,7 @@ const VideoFeed = (props) => {
100100
peer.addTransceiver('video', { direction: 'recvonly' })
101101

102102
return () => {
103-
peer.close()
103+
closePeerConnection(peer)
104104
}
105105
}, [props.topic, props.webrtcURL, videoRef])
106106

feedingwebapp/src/webrtc/webrtc_helpers.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function createPeerConnection(url, topic, onTrackAdded, onConnectionEnd)
1717
const peerConnection = new RTCPeerConnection({
1818
iceServers: [
1919
{
20-
urls: 'stun:stun.stunprotocol.org'
20+
urls: 'stun:stun1.l.google.com:19302'
2121
}
2222
]
2323
})
@@ -28,9 +28,11 @@ export function createPeerConnection(url, topic, onTrackAdded, onConnectionEnd)
2828
try {
2929
const offer = await peerConnection.createOffer()
3030
await peerConnection.setLocalDescription(offer)
31+
const ip = await getIPAddress()
3132
const payload = {
3233
sdp: peerConnection.localDescription,
33-
topic: topic
34+
topic: topic,
35+
ip: ip
3436
}
3537
console.log('sending payload', payload)
3638
const { data } = await axios.post(url, payload)
@@ -53,7 +55,7 @@ export function createPeerConnection(url, topic, onTrackAdded, onConnectionEnd)
5355
if (peerConnection.connectionState === 'failed' || peerConnection.connectionState === 'disconnected') {
5456
console.error(peerConnection.connectionState, 'Resetting the PeerConnection')
5557
if (onConnectionEnd) onConnectionEnd()
56-
createPeerConnection()
58+
// peerConnection = createPeerConnection(url, topic, onTrackAdded, onConnectionEnd)
5759
}
5860
console.log('peerConnection.onconnectionstatechange', peerConnection.connectionState)
5961
}
@@ -69,3 +71,24 @@ export function createPeerConnection(url, topic, onTrackAdded, onConnectionEnd)
6971
return
7072
}
7173
}
74+
75+
/**
76+
* Closes the given peer connection.
77+
* @param {object} peerConnection The RTCPeerConnection to close.
78+
*/
79+
export function closePeerConnection(peerConnection) {
80+
if (!peerConnection) return
81+
console.log('Closing RTCPeerConnection', peerConnection)
82+
if (peerConnection.connectionState !== 'closed') {
83+
const senders = peerConnection.getSenders()
84+
senders.forEach((sender) => peerConnection.removeTrack(sender))
85+
peerConnection.close()
86+
console.log('Closed RTCPeerConnection')
87+
}
88+
}
89+
90+
export async function getIPAddress() {
91+
const res = await axios.get('https://api.ipify.org/?format=json')
92+
console.log(res.data)
93+
return res.data.ip
94+
}

0 commit comments

Comments
 (0)