@@ -12,28 +12,43 @@ const bodyParser = require('body-parser')
12
12
var cors = require ( 'cors' )
13
13
const webrtc = require ( 'wrtc' )
14
14
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
16
21
17
22
app . use ( bodyParser . json ( ) )
18
23
app . use ( bodyParser . urlencoded ( { extended : true } ) )
19
24
app . use ( cors ( ) )
20
25
21
26
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 )
23
28
24
29
// Configure the peer connection
25
30
const peer = new webrtc . RTCPeerConnection ( {
26
31
iceServers : [
27
32
{
28
- urls : 'stun:stun.stunprotocol.org '
33
+ urls : 'stun:stun1.l.google.com:19302 '
29
34
}
30
35
]
31
36
} )
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
+
32
48
const desc = new webrtc . RTCSessionDescription ( body . sdp )
33
49
await peer . setRemoteDescription ( desc )
34
50
35
51
// Add the publisher's video stream to the subscriber's peer connection
36
- const topic = body . topic
37
52
if ( topic in senderStream ) {
38
53
senderStream [ topic ] . getTracks ( ) . forEach ( ( track ) => peer . addTrack ( track , senderStream [ topic ] ) )
39
54
}
@@ -50,19 +65,28 @@ app.post('/subscribe', async ({ body }, res) => {
50
65
} )
51
66
52
67
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 )
54
69
55
70
// Configure the peer connection
56
71
const peer = new webrtc . RTCPeerConnection ( {
57
72
iceServers : [
58
73
{
59
- urls : 'stun:stun.stunprotocol.org '
74
+ urls : 'stun:stun1.l.google.com:19302 '
60
75
}
61
76
]
62
77
} )
63
78
64
- // Send the publisher's video stream to all subscribers on that topic
79
+ // Close any old peers on the same IP address
65
80
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
66
90
peer . ontrack = ( e ) => handleTrackEvent ( e , topic )
67
91
68
92
// Create an answer to the publisher's offer
0 commit comments