-
Notifications
You must be signed in to change notification settings - Fork 0
/
sipstack.js
201 lines (168 loc) · 5.41 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
var drachtio = require('drachtio');
var appSip = drachtio() ;
var fs = require('fs') ;
var debug = require('debug')('basic') ;
var transform = require('sdp-transform');
var kill = require('tree-kill');
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.sessionIds = {};
SipStack.sessionIdByCalls = {};
SipStack.requests = {};
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];
res.send(200) ;
}) ;
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,callback){
var sipDest = to;
console.log("Send invite to "+ sipDest);
if (sipServerConnected){
SipStack.appSip.request({
uri: "sip:"+sipDest,
method: 'INVITE',
headers: {
'User-Agent': 'dracht.io',
'from': "sip:"+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+="\na=ptime:20";
console.log('recieved response: %s', remoteSdpStr);
console.log(req.msg.headers);
console.log('dialogId recv: ', res.stackDialogId);
SipStack.dialogs[sessionId] = res.stackDialogId ;
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];
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) {
};
module.exports = new SipStack();