-
Notifications
You must be signed in to change notification settings - Fork 22
/
sipstack.js
313 lines (266 loc) · 8.8 KB
/
sipstack.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
var drachtio = require('drachtio');
var appSip = drachtio() ;
var debug = require('debug')('basic') ;
var transform = require('sdp-transform');
var config = require('./config');
function getConnectionIp (sdpJson){
return sdpJson.connection.ip;
}
function getPortByType (sdpJson,type){
var media = sdpJson.media;
for (var i = 0 ; i < media.length ; i++){
if (media[i].type == type){
return media[i].port;
}
}
return 0;
}
var SipStack = function () {};
SipStack.appSip = drachtio();
SipStack.dialogs = {};
SipStack.res = {};
SipStack.sessionIds = {};
SipStack.sessionIdByCalls = {};
SipStack.requests = {};
SipStack.localSDP = {};
SipStack.options = {};
SipStack.mediastack = null;
SipStack.prototype.log = function () {
console.log('doo!');
}
SipStack.kurentoPipelineRelease = function (sessionId) {
console.log('Bad callback end') ;
};
SipStack.appSip.use('bye', function( req, res){
console.log('BYE recieved: %s', JSON.stringify(res) ) ;
var callId = res.msg.headers['call-id'];
console.log('BYE recieved: %s index %s' , JSON.stringify(res),callId ) ;
// get sessionId
var sessionId = SipStack.sessionIdByCalls[callId];
SipStack.kurentoPipelineRelease(sessionId);
var dialog = SipStack.dialogs[sessionId];
delete SipStack.sessionIds[dialog];
delete SipStack.dialogs[sessionId];
delete SipStack.sessionIdByCalls[callId];
delete SipStack.localSDP[sessionId];
res.send(200) ;
}) ;
SipStack.appSip.use('invite', function( req, res){
console.log('INviTE recieved: %s', JSON.stringify(res) ) ;
console.log(req.body);
var callId = res.msg.headers['call-id'];
// console.log('INVITE recieved: %s index %s' , JSON.stringify(req),callId ) ;
// get sessionId
var sessionId = SipStack.sessionIdByCalls[callId];
if (sessionId){
var remoteSdpStr = req.body;
var dialog = SipStack.dialogs[sessionId];
var options = SipStack.options[sessionId];
if (options.renegotiate == "none"){
res.send( 200,{
body: SipStack.localSDP[sessionId]
});
}
else if (options.renegotiate == "rtp-webrtc"){
SipStack.mediastack.renegotiateRTP(sessionId,remoteSdpStr,function(err,newLocalSdp){
console.log("RTP renegotiated");
if (err) {
console.log(err);
return;
}
SipStack.res[sessionId] = res;
SipStack.localSDP[sessionId] = newLocalSdp;
SipStack.mediastack.renegotiateWebRTC(sessionId,function(err){
console.log("WebRTC renegotiated");
});
});
}
else if (!options || !options.renegotiate || options.renegotiate == "rtp"){ // Default renegotiate only RTP
SipStack.mediastack.renegotiateRTP(sessionId,remoteSdpStr,function(err,newLocalSdp){
console.log("RTP renegotiated");
if (err) {
console.log(err);
return;
}
SipStack.localSDP[sessionId] = newLocalSdp;
res.send( 200,{
body: SipStack.localSDP[sessionId]
},function(){
setTimeout( () => {SipStack.mediastack.reconnect(sessionId);},500);
});
});
}
}
else
res.send(404) ;
});
SipStack.prototype.reponseToReInvite = function (sessionId){
if (SipStack.res[sessionId]){
console.log("Send 200 OK"+SipStack.localSDP[sessionId]);
SipStack.res[sessionId].send( 200,{
body: SipStack.localSDP[sessionId]},function(){
//SipStack.mediastack.reconnect(sessionId);
setTimeout( () => {SipStack.mediastack.reconnect(sessionId);},500);
});
}
}
SipStack.prototype.init = function (callback){
SipStack.sipServerConnected = false;
SipStack.kurentoPipelineRelease = callback;
SipStack.sessions = {};
SipStack.appSip.connect(config.drachtio,
function(err, hostport){
if( err ) throw err ;
console.log("Connected to SIP server");
sipServerConnected = true;
}
) ;
}
SipStack.prototype.invite = function (sessionId,from,to,localSDP,options,callback){
console.log('Mediastack in sip id=' + SipStack.mediastack.id);
var sipDest = to;
console.log("Send invite to "+ sipDest);
console.log("Send local SDP to "+ localSDP);
console.log("Invite options"+ options);
if (sipDest.indexOf("sip:")!=0)
sipDest="sip:"+sipDest;
if (from.indexOf("sip:")!=0)
from="sip:"+from;
if (sipServerConnected){
SipStack.appSip.request({
uri: sipDest,
method: 'INVITE',
headers: {
'User-Agent': 'dracht.io',
'from': from
},
body: localSDP
},
function( err, req ){
if( err ) {
console.log('Error '+ err ) ;
return callback("reject",null);
}
console.log('sent request: %s', JSON.stringify(req) ) ;
req.on('response', function(res,ack){
console.log('received response with status: %s', res.status) ;
console.log('recieved response: %s', JSON.stringify(res) ) ;
if( res.status == 200) {
var remoteSdpStr = res.body;
remoteSdpStr+="a=ptime:20";
var h264Payload = getH264Payload(remoteSdpStr);
remoteSdpStr = remoteSdpStr.replace(new RegExp("a=rtcp-fb:\\*", "g"), "a=rtcp-fb:"+h264Payload);
console.log('recieved response: %s', remoteSdpStr);
console.log(req.msg.headers);
console.log('dialogId recv: ', res.stackDialogId);
SipStack.dialogs[sessionId] = res.stackDialogId ;
SipStack.localSDP[sessionId] = localSDP;
SipStack.options[sessionId] = options;
SipStack.sessionIds[res.stackDialogId] = sessionId;
var callId = req.msg.headers["call-id"];
SipStack.sessionIdByCalls[callId] = sessionId;
delete SipStack.requests[sessionId];
ack() ;
console.log("Ack sended");
return callback(null,remoteSdpStr);
}
else if (res.status >=300){
console.log('Invite rejected');
SipStack.kurentoPipelineRelease(sessionId);
return callback("reject",null);
}
else if (res.status < 200){
console.log('Intermediate repsonse');
SipStack.requests[sessionId] = req ;
}
}) ;
}
);
return "";
}
return "";
};
SipStack.prototype.bye = function (sessionId,sdpLocal) {
if (SipStack.requests[sessionId]!=undefined){
SipStack.requests[sessionId].cancel();
}
else {
var dialog = SipStack.dialogs[sessionId];
if (dialog != undefined){
SipStack.appSip.request({
method: 'BYE',
stackDialogId: dialog
}, function(err, req){
if( err || req == undefined) {
console.log(err);
return;
}
var callId = req.msg.headers["call-id"];
delete SipStack.sessionIds[dialog];
delete SipStack.dialogs[sessionId];
delete SipStack.sessionIdByCalls[callId];
delete SipStack.localSDP[sessionId];
delete SipStack.options[sessionId];
req.on('response', function(response){
console.log('BYE '+response.status);
});
}) ;
}
}
};
SipStack.prototype.infoDtmf = function (sessionId,dtmf) {
var dialog = SipStack.dialogs[sessionId];
if (dialog != undefined){
var messageBody="Signal="+dtmf+"\r\nDuration=160\r\n";
SipStack.appSip.request({
method: 'INFO',
stackDialogId: dialog,
headers: {
'User-Agent': 'dracht.io',
'Content-Type': 'application/dtmf-relay'
},
body: messageBody
}, function(err, req){
if(req!=undefined)
console.log('sent request: %s', JSON.stringify(req) ) ;
if( err || req == undefined) {
console.log(err);
console.log(req);
return;
}
req.on('response', function(response){
console.log('INFO '+response.status);
});
}) ;
}
};
SipStack.prototype.response = function (sessionId) {
};
SipStack.prototype.registerWebRTCEndpoint = function (from) {
};
SipStack.prototype.registerSIP = function (from,host,password) {
};
function getH264Payload(sdp){
var sdpObject = transform.parse(sdp);
// if (sdpObject.media.length < 2)
// return 0;
console.log("RTP lenght"+sdpObject.media[1].rtp.length);
var newRTP = [];
for (var i=0; i < sdpObject.media[1].rtp.length ; i++){
console.log("Media 1 Codec : "+sdpObject.media[1].rtp[i].codec);
if (sdpObject.media[1].rtp[i].codec=="H264"){
newRTP.push(sdpObject.media[1].rtp[i]);
break;
}
}
var h264Payload=0;
if (newRTP.length > 0)
h264Payload = newRTP[0].payload;
return h264Payload;
}
SipStack.prototype.setMediaSatck = function (media){
SipStack.mediastack = media;
}
/*Asuming a classic sdp with media[1] as the only video media*/
SipStack.prototype.getH264Payload = getH264Payload;
module.exports = new SipStack();