@@ -25,9 +25,11 @@ app.use(bodyParser.json())
25
25
app . use ( bodyParser . urlencoded ( { extended : true } ) )
26
26
app . use ( cors ( ) )
27
27
28
+ const debug = false
29
+
28
30
app . post ( '/subscribe' , async ( { body } , res ) => {
29
31
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 )
31
33
32
34
// Configure the peer connection
33
35
const peer = new webrtc . RTCPeerConnection ( {
@@ -37,43 +39,94 @@ app.post('/subscribe', async ({ body }, res) => {
37
39
}
38
40
]
39
41
} )
42
+ if ( debug ) {
43
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: created peer connection object" )
44
+ }
40
45
41
46
// Close any old peers on the same IP address
42
47
const topic = body . topic
48
+ if ( debug ) {
49
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: got topic" , topic )
50
+ }
43
51
const key = body . ip + ':' + topic
52
+ if ( debug ) {
53
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: got key" , key )
54
+ }
44
55
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
+ }
45
59
const senders = subscribePeers [ key ] . getSenders ( )
60
+ if ( debug ) {
61
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: got senders for peer for key" )
62
+ }
46
63
senders . forEach ( ( sender ) => subscribePeers [ key ] . removeTrack ( sender ) )
64
+ if ( debug ) {
65
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: removed tracks" )
66
+ }
47
67
subscribePeers [ key ] . close ( )
68
+ if ( debug ) {
69
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: closed peer connection" )
70
+ }
48
71
}
49
72
subscribePeers [ key ] = peer
73
+ if ( debug ) {
74
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: set new peer connection" )
75
+ }
50
76
51
77
const desc = new webrtc . RTCSessionDescription ( body . sdp )
78
+ if ( debug ) {
79
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: created desc" )
80
+ }
52
81
await peer . setRemoteDescription ( desc )
82
+ if ( debug ) {
83
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: set remote desc" )
84
+ }
53
85
54
86
// Add the publisher's video stream to the subscriber's peer connection
55
87
if ( topic in senderStream ) {
88
+ if ( debug ) {
89
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: adding topics from publisher" )
90
+ }
56
91
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
+ }
57
95
}
58
96
59
97
// Create an answer to the publisher's offer
60
98
const answer = await peer . createAnswer ( )
99
+ if ( debug ) {
100
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: created answer" )
101
+ }
61
102
await peer . setLocalDescription ( answer )
103
+ if ( debug ) {
104
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: set local description" )
105
+ }
62
106
const payload = {
63
- sdp : peer . localDescription
107
+ sdp : answer
108
+ }
109
+ if ( debug ) {
110
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: created payload" )
64
111
}
65
112
66
113
// Send the answer to the publisher
67
114
res . json ( payload )
115
+ if ( debug ) {
116
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: set payload" )
117
+ }
68
118
} 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 )
70
120
res . sendStatus ( 500 )
121
+ if ( debug ) {
122
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "subscribe: sent error status 500" )
123
+ }
71
124
}
72
125
} )
73
126
74
127
app . post ( '/publish' , async ( { body } , res ) => {
75
128
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 )
77
130
78
131
// Configure the peer connection
79
132
const peer = new webrtc . RTCPeerConnection ( {
@@ -83,40 +136,88 @@ app.post('/publish', async ({ body }, res) => {
83
136
}
84
137
]
85
138
} )
139
+ if ( debug ) {
140
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: create peer" )
141
+ }
86
142
87
143
// Close any old peers on the same IP address
88
144
const topic = body . topic
145
+ if ( debug ) {
146
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: got topic" , topic )
147
+ }
89
148
const key = body . ip + ':' + topic
149
+ if ( debug ) {
150
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: got key" , key )
151
+ }
90
152
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
+ }
91
156
const senders = publishPeers [ key ] . getSenders ( )
157
+ if ( debug ) {
158
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: got senders for old key" )
159
+ }
92
160
senders . forEach ( ( sender ) => publishPeers [ key ] . removeTrack ( sender ) )
161
+ if ( debug ) {
162
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: removed tracks for old key" )
163
+ }
93
164
publishPeers [ key ] . close ( )
165
+ if ( debug ) {
166
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: closed old peer connection" )
167
+ }
94
168
}
95
169
publishPeers [ key ] = peer
170
+ if ( debug ) {
171
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: added new peer" )
172
+ }
96
173
97
174
// Send the publisher's video stream to all subscribers on that topic
98
175
peer . ontrack = ( e ) => handleTrackEvent ( e , topic )
176
+ if ( debug ) {
177
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: handled track events" )
178
+ }
99
179
100
180
// Create an answer to the publisher's offer
101
181
const desc = new webrtc . RTCSessionDescription ( body . sdp )
182
+ if ( debug ) {
183
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: got desc" )
184
+ }
102
185
await peer . setRemoteDescription ( desc )
186
+ if ( debug ) {
187
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: set remote description" )
188
+ }
103
189
const answer = await peer . createAnswer ( )
190
+ if ( debug ) {
191
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: got answer" )
192
+ }
104
193
await peer . setLocalDescription ( answer )
194
+ if ( debug ) {
195
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: set local description" )
196
+ }
105
197
const payload = {
106
- sdp : peer . localDescription
198
+ sdp : answer
199
+ }
200
+ if ( debug ) {
201
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: created payload" )
107
202
}
108
203
109
204
// Send the answer to the publisher
110
205
res . json ( payload )
206
+ if ( debug ) {
207
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: set json payload" )
208
+ }
111
209
} 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 )
113
211
res . sendStatus ( 500 )
212
+ if ( debug ) {
213
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , "publish: sent error status 500" )
214
+ }
114
215
}
115
216
} )
116
217
117
218
function handleTrackEvent ( e , topic ) {
118
- console . log ( 'Handle track for publisher' )
219
+ console . log ( Date ( Date . now ( ) ) . toString ( ) , 'Handle track for publisher' )
119
220
senderStream [ topic ] = e . streams [ 0 ]
120
221
}
121
222
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