Skip to content

Commit

Permalink
fixes from testing on lovelace
Browse files Browse the repository at this point in the history
  • Loading branch information
amalnanavati committed Jan 15, 2024
1 parent 870df90 commit ef76ab9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 23 additions & 16 deletions feedingwebapp/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var cors = require('cors')
const webrtc = require('wrtc')

let senderStream = {} // key: topic, value: MediaStream
let publishPeers = {} // key: IP4, value: RTCPeerConnection
let subscribePeers = {} // key: IP4, value: RTCPeerConnection
let publishPeers = {} // key: IP4:topic, value: RTCPeerConnection
let subscribePeers = {} // key: IP4:topic, value: RTCPeerConnection

function sdpToIP4(sdp) {
const sdpLines = sdp.split('\r\n')
Expand All @@ -34,25 +34,27 @@ app.post('/subscribe', async ({ body }, res) => {
const peer = new webrtc.RTCPeerConnection({
iceServers: [
{
urls: 'stun:stun.stunprotocol.org'
urls: 'stun:stun1.l.google.com:19302'
}
]
})

// Close any old peers on the same IP address
const ip4 = sdpToIP4(body.sdp.sdp)
if (ip4 in subscribePeers) {
const senders = subscribePeers[ip4].getSenders()
senders.forEach((sender) => subscribePeers[ip4].removeTrack(sender))
subscribePeers[ip4].close()
const topic = body.topic
const key = ip4 + ':' + topic
console.log('subscriber key', key, 'sdp', body.sdp.sdp)
if (key in subscribePeers) {
const senders = subscribePeers[key].getSenders()
senders.forEach((sender) => subscribePeers[key].removeTrack(sender))
subscribePeers[key].close()
}
subscribePeers[ip4] = peer
subscribePeers[key] = peer

const desc = new webrtc.RTCSessionDescription(body.sdp)
await peer.setRemoteDescription(desc)

// Add the publisher's video stream to the subscriber's peer connection
const topic = body.topic
if (topic in senderStream) {
senderStream[topic].getTracks().forEach((track) => peer.addTrack(track, senderStream[topic]))
}
Expand All @@ -75,22 +77,27 @@ app.post('/publish', async ({ body }, res) => {
const peer = new webrtc.RTCPeerConnection({
iceServers: [
{
urls: 'stun:stun.stunprotocol.org'
urls: 'stun:stun1.l.google.com:19302'
}
]
})

// Close any old peers on the same IP address
console.log(body.sdp)
const ip4 = sdpToIP4(body.sdp.sdp)
if (ip4 in publishPeers) {
const senders = publishPeers[ip4].getSenders()
senders.forEach((sender) => publishPeers[ip4].removeTrack(sender))
publishPeers[ip4].close()
const topic = body.topic
const key = ip4 + ':' + topic
console.log('ip4', ip4, 'publishPeers', publishPeers, ip4 in publishPeers)
if (key in publishPeers) {
console.log('get senders')
const senders = publishPeers[key].getSenders()
console.log('close senders')
senders.forEach((sender) => publishPeers[key].removeTrack(sender))
console.log('close peer connection')
publishPeers[key].close()
}
publishPeers[key] = peer

// Send the publisher's video stream to all subscribers on that topic
const topic = body.topic
peer.ontrack = (e) => handleTrackEvent(e, topic)

// Create an answer to the publisher's offer
Expand Down
2 changes: 1 addition & 1 deletion feedingwebapp/src/webrtc/webrtc_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createPeerConnection(url, topic, onTrackAdded, onConnectionEnd)
const peerConnection = new RTCPeerConnection({
iceServers: [
{
urls: 'stun:stun.stunprotocol.org'
urls: 'stun:stun1.l.google.com:19302'
}
]
})
Expand Down

0 comments on commit ef76ab9

Please sign in to comment.