This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gw.js
330 lines (305 loc) · 10.8 KB
/
gw.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Quick & dirty Kurento to SIP gateway
// process.env.DEBUG = 'rtcninja*';
var kurento = require('kurento-client');
console.log('Detected ' + getIPAddress() + ' IP');
var kurento_addr = '127.0.0.1';
var kurento_uri = 'ws://' + kurento_addr + ':8888/kurento';
var playfile_uri = "file:///tmp/player.webm";
var kurentoClient = null;
var call_number = require('minimist')(process.argv.slice(2), opts={string: 'call'})['call'];
var wait_for_call = require('minimist')(process.argv.slice(2))['wait'];
var ua;
if (!call_number && !wait_for_call) {
console.log('Usage: nodejs gw.js [--call phone_number] [--wait]');
process.exit(0);
}
function CallMediaPipeline() {
this.pipeline = null;
}
function getKurentoClient(callback) {
if (kurentoClient !== null) {
return callback(null, kurentoClient);
}
kurento(kurento_uri, function(error, _kurentoClient) {
if (error) {
var message = 'Coult not find media server at address ' + kurento_uri;
return callback(message + ". Exiting with error " + error);
}
kurentoClient = _kurentoClient;
callback(null, kurentoClient);
});
}
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && alias.address !== '172.17.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
function replace_ip(sdp, ip) {
if (!ip)
ip = getIPAddress();
return sdp.replace(new RegExp("IN IP4 .*","g"), "IN IP4 " + ip);
}
CallMediaPipeline.prototype.createPipeline = function(callback) {
var self = this;
getKurentoClient(function(error, kurentoClient) {
if (error) {
return callback(error);
}
kurentoClient.create('MediaPipeline', function(error, pipeline) {
if (error) {
return callback(error);
}
self.pipeline = pipeline;
pipeline.create('PlayerEndpoint', {uri: playfile_uri, useEncodedMedia: false}, function(error, playerEndpoint) {
if (error) {
return callback(error)
}
self.pipeline.pe = playerEndpoint;
playerEndpoint.on('EndOfStream', function() {
console.log('*** END OF STREAM');
self.pipeline.release();
ua.stop();
process.exit(0);
});
// console.log('PlayerEndpoint created');
var recordParams = {
stopOnEndOfStream: true,
mediaProfile: 'WEBM_AUDIO_ONLY',
uri: 'file:///tmp/record.webm'
};
pipeline.create('RecorderEndpoint', recordParams, function(error, recorder) {
pipeline.create('RtpEndpoint', function(error, rtpe) {
self.pipeline.rtpe = rtpe;
self.pipeline.rece = recorder;
// connect to myRTPEndpoint (rx to us)
rtpe.connect(recorder,function(error) {
// console.log('recorder endpoint connected');
});
rtpe.on('MediaStateChanged', function (event) {
console.log('MediaStateChanged to ' + event.newState);
if (wait_for_call && (event.oldState !== event.newState && event.newState == "CONNECTED"))
start_media(pipeline);
});
rtpe.on('ConnectionStateChanged', function (event) {
console.log('ConnectionStateChanged to ' + event.newState);
});
// connect to myRTPEndpoint (tx from us)
playerEndpoint.connect(rtpe,function(error) {
// console.log('player endpoint connected');
});
rtpe.generateOffer(function(error, offer) {
// this is offer for receiving side (recorder.sdp)
// that we will send to asterisk as local offer
callback(null, offer);
}); // generateOffer
}); // create('RtpEndpoint')
}); // create('RecorderEndpoint')
}); // create('PlayerEndpoint')
}) // create('MediaPipeline')
}); // getKurentoClient
} // CallMediaPipeline.prototype.createPipeline
var JsSIP = require('jssip');
const NodeWebSocket = require('jssip-node-websocket');
// use local asterisk
var asterisk_addr = '127.0.0.1';
var socket = new NodeWebSocket('ws://' + asterisk_addr + ':8088/ws');
var reg_sip_user = '100';
var reg_sip_user_pass = '100_pass';
var configuration = {
'uri' : 'sip:' + reg_sip_user + '@' + asterisk_addr,
'password' : reg_sip_user_pass,
display_name : 'Alice',
sockets : [ socket ]
};
try {
var ua_ = new JsSIP.UA(configuration);
ua = ua_;
} catch (e) {
console.log(e);
return;
}
function start_media(pipeline)
{
pipeline.pe.play(function(error) {
if (error) {
reject('play error');
}
console.log('Kurento is playing');
});
pipeline.rece.record(() => console.log("Kurento is recording"));
}
var call_eventHandlers = {
'progress': function(data) { console.log('call in progress'); },
'confirmed': function(data) {
console.log('call confirmed');
start_media(call_options.pipeline);
}
};
var call_options = {
'eventHandlers': call_eventHandlers,
'extraHeaders': [ 'X-Foo: foo', 'X-Bar: bar' ],
'mediaConstraints': {'audio': true, 'video': false },
};
ua.on('registered', function(e) {
console.log('registered');
if (call_number) {
// create new Kurento pipeline for call
createPipeline(call_options).then(
result => {
console.log('outgoing call pipeline created');
console.log('initiated call');
ua.call('sip:' + call_number + '@' + asterisk_addr, call_options);
},
reject => {
console.log('Error creating pipeline');
call.terminate();
return;
}
);
}
});
ua.on('registrationFailed', function(err) {
console.log('registrationFailed: ' + err.cause);
});
ua.on('connecting', function() {
console.log('connecting to SIP server');
});
ua.on('connected', function() {
console.log('connected to SIP server');
});
ua.on('disconnected', function() {
console.log('disconnected from SIP server');
});
ua.on('newMessage', function() {
console.log('newMessage');
});
function createPipeline(call) {
return new Promise(function(resolve, reject) {
var pipeline = new CallMediaPipeline();
pipeline.createPipeline(function(error, kurento_offer) {
if (error) {
reject(error);
} else {
call.pipeline = pipeline.pipeline;
call.pipeline.kurento_offer = replace_ip(kurento_offer, kurento_addr);
resolve(kurento_offer);
}
});
});
}
function send_answer_to_kurento(pipeline) {
return new Promise(function(resolve, reject) {
pipeline.rtpe.processAnswer(pipeline.sip_offer, function (error, sdpAnswer) {
if (error) {
reject('Kurento processAnswer error:' + error);
}
resolve();
}); // processAnswer
});
}
var outgoing = false;
ua.on('newRTCSession', function(data) {
console.log('new ' + (data.originator == 'remote' ? 'incoming' : 'outgoing') + ' call');
var call = data.session;
if (data.originator == 'remote') { // incoming call
console.log('Call from: ' + call.request.headers.From[0].parsed.uri.toAor());
// create new Kurento pipeline for call
createPipeline(call).then(
result => {
console.log('incoming call pipeline created');
console.log('answering incoming call');
call.answer();
},
reject => {
console.log('Error creating pipeline');
call.terminate();
return;
}
);
} else {
console.log('Call to: ' + call.request.headers.To[0]);
call.pipeline = call_options.pipeline;
call_options.call = call;
outgoing = true;
}
call.on('ended', function(data) {
if (call.pipeline)
call.pipeline.release();
console.log('Call ended: ' + data.cause);
if (outgoing) {
ua.stop();
process.exit(0);
}
});
call.on('failed', function(data) {
console.log('Call failed: ' + data.cause);
if (call.pipeline)
call.pipeline.release();
console.log('Call ended: ' + data.cause);
if (outgoing) {
ua.stop();
process.exit(0);
}
});
call.on('reinvite', function(data) {
console.log('Got SIP reINVITE');
});
call.on('update', function(data) {
console.log('Got SIP UPDATE');
});
call.on('sdp', function(data) {
if (data.originator == 'remote') {
if (call.pipeline.sip_offer) {
call.pipeline.sip_offer = null;
console.log('Renegotiate requested');
// recreate the RTPEndpoint, attach recorder to it and start media again
call_options.pipeline.rece.stop();
call_options.pipeline.pe.stop();
call_options.pipeline.rtpe.release();
call_options.pipeline.create('RtpEndpoint', function(error, rtpe) {
call_options.pipeline.rtpe = rtpe;
rtpe.connect(call_options.pipeline.rece, function(error) {
rtpe.on('MediaStateChanged', function (event) {
console.log('reINVITE: MediaStateChanged to ' + event.newState);
if (event.oldState !== event.newState && event.newState == "CONNECTED")
start_media(call.pipeline);
});
rtpe.on('ConnectionStateChanged', function (event) {
console.log('reINVITE: ConnectionStateChanged to ' + event.newState);
});
call_options.pipeline.pe.connect(rtpe);
rtpe.generateOffer(function(error, kurento_offer) {
call.pipeline.kurento_offer = replace_ip(kurento_offer, kurento_addr);
call.renegotiate();
});
});
});
return;
}
// console.log('first remote sdp: ' + data.sdp);
call.pipeline.sip_offer = data.sdp;
send_answer_to_kurento(call.pipeline).then(
result => {
console.log('Answer to Kurento sent');
},
reject => {
console.log('Error sending answer to Kurento');
call.terminate();
return;
}
);
}
if (data.originator == 'local') {
data.sdp = call.pipeline.kurento_offer;
// console.log('local sdp: ' + data.sdp);
}
});
});
ua.start();