Skip to content

Commit 962c7bb

Browse files
committed
Address segfault by removing pc.localDescription
1 parent 2e8f3d3 commit 962c7bb

File tree

1 file changed

+109
-8
lines changed

1 file changed

+109
-8
lines changed

feedingwebapp/server.js

Lines changed: 109 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ app.use(bodyParser.json())
2525
app.use(bodyParser.urlencoded({ extended: true }))
2626
app.use(cors())
2727

28+
const debug = false
29+
2830
app.post('/subscribe', async ({ body }, res) => {
2931
try {
30-
console.log('got subscriber on IP', body.ip, 'for topic', body.topic)
32+
console.log(Date(Date.now()).toString(), 'subscribe: got POST on IP', body.ip, 'for topic', body.topic)
3133

3234
// Configure the peer connection
3335
const peer = new webrtc.RTCPeerConnection({
@@ -37,43 +39,94 @@ app.post('/subscribe', async ({ body }, res) => {
3739
}
3840
]
3941
})
42+
if (debug) {
43+
console.log(Date(Date.now()).toString(), "subscribe: created peer connection object")
44+
}
4045

4146
// Close any old peers on the same IP address
4247
const topic = body.topic
48+
if (debug) {
49+
console.log(Date(Date.now()).toString(), "subscribe: got topic", topic)
50+
}
4351
const key = body.ip + ':' + topic
52+
if (debug) {
53+
console.log(Date(Date.now()).toString(), "subscribe: got key", key)
54+
}
4455
if (key in subscribePeers && subscribePeers[key] && subscribePeers[key].connectionState !== 'closed') {
56+
if (debug) {
57+
console.log(Date(Date.now()).toString(), "subscribe: peer for key already exists")
58+
}
4559
const senders = subscribePeers[key].getSenders()
60+
if (debug) {
61+
console.log(Date(Date.now()).toString(), "subscribe: got senders for peer for key")
62+
}
4663
senders.forEach((sender) => subscribePeers[key].removeTrack(sender))
64+
if (debug) {
65+
console.log(Date(Date.now()).toString(), "subscribe: removed tracks")
66+
}
4767
subscribePeers[key].close()
68+
if (debug) {
69+
console.log(Date(Date.now()).toString(), "subscribe: closed peer connection")
70+
}
4871
}
4972
subscribePeers[key] = peer
73+
if (debug) {
74+
console.log(Date(Date.now()).toString(), "subscribe: set new peer connection")
75+
}
5076

5177
const desc = new webrtc.RTCSessionDescription(body.sdp)
78+
if (debug) {
79+
console.log(Date(Date.now()).toString(), "subscribe: created desc")
80+
}
5281
await peer.setRemoteDescription(desc)
82+
if (debug) {
83+
console.log(Date(Date.now()).toString(), "subscribe: set remote desc")
84+
}
5385

5486
// Add the publisher's video stream to the subscriber's peer connection
5587
if (topic in senderStream) {
88+
if (debug) {
89+
console.log(Date(Date.now()).toString(), "subscribe: adding topics from publisher")
90+
}
5691
senderStream[topic].getTracks().forEach((track) => peer.addTrack(track, senderStream[topic]))
92+
if (debug) {
93+
console.log(Date(Date.now()).toString(), "subscribe: added topics from publisher")
94+
}
5795
}
5896

5997
// Create an answer to the publisher's offer
6098
const answer = await peer.createAnswer()
99+
if (debug) {
100+
console.log(Date(Date.now()).toString(), "subscribe: created answer")
101+
}
61102
await peer.setLocalDescription(answer)
103+
if (debug) {
104+
console.log(Date(Date.now()).toString(), "subscribe: set local description")
105+
}
62106
const payload = {
63-
sdp: peer.localDescription
107+
sdp: answer
108+
}
109+
if (debug) {
110+
console.log(Date(Date.now()).toString(), "subscribe: created payload")
64111
}
65112

66113
// Send the answer to the publisher
67114
res.json(payload)
115+
if (debug) {
116+
console.log(Date(Date.now()).toString(), "subscribe: set payload")
117+
}
68118
} catch (err) {
69-
console.error('Failed to process subscriber, exception: ' + err.message)
119+
console.error(Date(Date.now()).toString(), 'subscribe: Failed to process subscriber, exception: ' + err.message)
70120
res.sendStatus(500)
121+
if (debug) {
122+
console.log(Date(Date.now()).toString(), "subscribe: sent error status 500")
123+
}
71124
}
72125
})
73126

74127
app.post('/publish', async ({ body }, res) => {
75128
try {
76-
console.log('got publisher on IP', body.ip, 'for topic', body.topic)
129+
console.log(Date(Date.now()).toString(), 'publish: got POST on IP', body.ip, 'for topic', body.topic)
77130

78131
// Configure the peer connection
79132
const peer = new webrtc.RTCPeerConnection({
@@ -83,40 +136,88 @@ app.post('/publish', async ({ body }, res) => {
83136
}
84137
]
85138
})
139+
if (debug) {
140+
console.log(Date(Date.now()).toString(), "publish: create peer")
141+
}
86142

87143
// Close any old peers on the same IP address
88144
const topic = body.topic
145+
if (debug) {
146+
console.log(Date(Date.now()).toString(), "publish: got topic", topic)
147+
}
89148
const key = body.ip + ':' + topic
149+
if (debug) {
150+
console.log(Date(Date.now()).toString(), "publish: got key", key)
151+
}
90152
if (key in publishPeers && publishPeers[key] && publishPeers[key].connectionState !== 'closed') {
153+
if (debug) {
154+
console.log(Date(Date.now()).toString(), "publish: found existing publisher for key", key)
155+
}
91156
const senders = publishPeers[key].getSenders()
157+
if (debug) {
158+
console.log(Date(Date.now()).toString(), "publish: got senders for old key")
159+
}
92160
senders.forEach((sender) => publishPeers[key].removeTrack(sender))
161+
if (debug) {
162+
console.log(Date(Date.now()).toString(), "publish: removed tracks for old key")
163+
}
93164
publishPeers[key].close()
165+
if (debug) {
166+
console.log(Date(Date.now()).toString(), "publish: closed old peer connection")
167+
}
94168
}
95169
publishPeers[key] = peer
170+
if (debug) {
171+
console.log(Date(Date.now()).toString(), "publish: added new peer")
172+
}
96173

97174
// Send the publisher's video stream to all subscribers on that topic
98175
peer.ontrack = (e) => handleTrackEvent(e, topic)
176+
if (debug) {
177+
console.log(Date(Date.now()).toString(), "publish: handled track events")
178+
}
99179

100180
// Create an answer to the publisher's offer
101181
const desc = new webrtc.RTCSessionDescription(body.sdp)
182+
if (debug) {
183+
console.log(Date(Date.now()).toString(), "publish: got desc")
184+
}
102185
await peer.setRemoteDescription(desc)
186+
if (debug) {
187+
console.log(Date(Date.now()).toString(), "publish: set remote description")
188+
}
103189
const answer = await peer.createAnswer()
190+
if (debug) {
191+
console.log(Date(Date.now()).toString(), "publish: got answer")
192+
}
104193
await peer.setLocalDescription(answer)
194+
if (debug) {
195+
console.log(Date(Date.now()).toString(), "publish: set local description")
196+
}
105197
const payload = {
106-
sdp: peer.localDescription
198+
sdp: answer
199+
}
200+
if (debug) {
201+
console.log(Date(Date.now()).toString(), "publish: created payload")
107202
}
108203

109204
// Send the answer to the publisher
110205
res.json(payload)
206+
if (debug) {
207+
console.log(Date(Date.now()).toString(), "publish: set json payload")
208+
}
111209
} catch (err) {
112-
console.error('Failed to process publisher, exception: ' + err.message)
210+
console.error(Date(Date.now()).toString(), 'publish: Failed to process publisher, exception: ' + err.message)
113211
res.sendStatus(500)
212+
if (debug) {
213+
console.log(Date(Date.now()).toString(), "publish: sent error status 500")
214+
}
114215
}
115216
})
116217

117218
function handleTrackEvent(e, topic) {
118-
console.log('Handle track for publisher')
219+
console.log(Date(Date.now()).toString(), 'Handle track for publisher')
119220
senderStream[topic] = e.streams[0]
120221
}
121222

122-
app.listen(process.env.REACT_APP_SIGNALLING_SERVER_PORT, () => console.log('Server started'))
223+
app.listen(process.env.REACT_APP_SIGNALLING_SERVER_PORT, () => console.log(Date(Date.now()).toString(), 'Server started'))

0 commit comments

Comments
 (0)