-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpeer.js
More file actions
139 lines (122 loc) · 3.61 KB
/
peer.js
File metadata and controls
139 lines (122 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const mediasoup = require('mediasoup');
const RTCPeerConnection = mediasoup.webrtc.RTCPeerConnection;
const RTCSessionDescription = mediasoup.webrtc.RTCSessionDescription;
/**
* Media Peer connection
*/
module.exports = class Peer {
constructor(options) {
// set peer required options.
this.options = options || {};
// field members
this.mediaRoom = this.options['mediaRoom'];
this.mediaPeer = this.options['mediaPeer'];
this.socket = this.options['socket'];
this.pc = null;
// default config
this.config = {
peerConnectionOptions: {
peer: this.mediaPeer,
usePlanB: true
}
}
// override default config
for (let opt in this.config) {
if (this.options.hasOwnProperty(opt)) {
this.config[opt] = this.options[opt];
}
}
// config log
this.logger = require('./logger')(`Peer ${this.id}`);
this.close = this.mediaPeer.close.bind(this.mediaPeer);
this.createPeerConnection(this.config.peerConnectionOptions);
}
get id() {
return this.socket && this.socket.id;
}
createPeerConnection(options) {
this.pc = new RTCPeerConnection(options);
return this.pc;
}
/**
* Process RoomRTC message
* @param {Object} msg
*/
processMessage(msg) {
this.logger.info('Preparing process msg, type:', msg.type);
if (msg.type === 'offer') {
this._handleMsgOffer(msg);
} else if (msg.type === 'answer') {
this._handleMsgAnswer(msg);
} else if (msg.type === 'bye') {
this.logger.info('Bye from client ?')
} else if (/^ice/.test(msg.type)) {
// this.logger.info('Do not process ice message', msg.type);
} else {
this.logger.info('Unknow message:', msg.type, msg);
}
}
sendSdpToPeer(description) {
let pid = this.id;
let sid = this.socket.sid || Date.now().toString();
let msg = {
from: pid,
sid: sid,
type: description.type,
payload: {
type: description.type,
sdp: description.sdp
}
}
// send msg back to client
// this.socket.send(msg);
this.socket.emit('message', msg);
}
sendSdpOffer(options) {
return this.pc.createOffer(options)
.then(desc => {
this.logger.info('pc.setLocalDescription(desc); ....');
return this.pc.setLocalDescription(desc);
})
.then(() => {
this.logger.info('Preparing sendSdpOffer to peer');
this.sendSdpToPeer(this.pc.localDescription);
})
.catch(err => {
this.logger.error('sendSdpToPeer error:', err);
});
}
_handleMsgOffer(msg) {
this.pc.on('negotiationneeded', () => {
this.logger.info('negotiationneeded send SDP re-offer:', this.id);
this.sendSdpOffer();
});
// Participant is required to join the mediaRoom by providing a capabilities SDP.
return this.pc.setCapabilities(msg.payload.sdp)
.then(() => {
this.logger.info('pc.setCapabilities(sdp) ok');
this.sendSdpOffer({
offerToReceiveAudio: 1,
offerToReceiveVideo: 1
});
})
.catch(err => {
this.logger.error('_handleMsgOffer, pc.setCapabilities error:', err);
});
}
_handleMsgAnswer(msg) {
let pid = this.id;
if (!this.pc) {
return this.logger.warn('Peer connection not found. id=', pid);
}
// Create sdp answer
let desc = new RTCSessionDescription(msg.payload);
this.pc.setRemoteDescription(desc)
.then(() => {
this.logger.info('setRemoteDescription for answer OK id=', pid);
})
.catch(err => {
this.logger.error('setRemoteDescription for Answer error: ', err);
})
}
}