sip-lab 1.15.0 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,466 @@
1
+ const sip = require ('../index.js')
2
+ const Zeq = require('@mayama/zeq')
3
+ const m = require('data-matching')
4
+ const sip_msg = require('sip-matching')
5
+ const sdp = require('sdp-matching')
6
+
7
+ // here we create our Zeq instance
8
+ var z = new Zeq()
9
+
10
+ async function test() {
11
+ sip.dtmf_aggregation_on(500)
12
+
13
+ z.trap_events(sip.event_source, 'event', (evt) => {
14
+ var e = evt.args[0]
15
+ return e
16
+ })
17
+
18
+ // Let's ignore '100 Trying'
19
+ z.add_event_filter({
20
+ event: 'response',
21
+ msg: sip_msg({
22
+ $rs: '100',
23
+ }),
24
+ })
25
+
26
+ console.log(sip.start((data) => { console.log(data)} ))
27
+
28
+ const t1 = sip.transport.create({address: "127.0.0.1"})
29
+ const t2 = sip.transport.create({address: "127.0.0.1"})
30
+
31
+ console.log("t1", t1)
32
+ console.log("t2", t2)
33
+
34
+ const oc = sip.call.create(t1.id, {from_uri: 'sip:alice@test.com', to_uri: `sip:bob@${t2.address}:${t2.port}`,
35
+ media: [
36
+ {
37
+ type: 'audio',
38
+ secure: false,
39
+ },
40
+ {
41
+ type: 'audio',
42
+ secure: true,
43
+ },
44
+ ]})
45
+
46
+ await z.wait([
47
+ {
48
+ event: "incoming_call",
49
+ call_id: m.collect("call_id"),
50
+ transport_id: t2.id,
51
+ },
52
+ ], 1000)
53
+
54
+ const ic = {
55
+ id: z.store.call_id,
56
+ sip_call_id: z.store.sip_call_id,
57
+ }
58
+
59
+ sip.call.respond(ic.id, {code: 200, reason: 'OK', media: [
60
+ {
61
+ type: 'audio',
62
+ secure: false,
63
+ },
64
+ {
65
+ type: 'audio',
66
+ secure: true,
67
+ },
68
+ ]})
69
+
70
+ await z.wait([
71
+ {
72
+ event: 'response',
73
+ call_id: oc.id,
74
+ method: 'INVITE',
75
+ msg: sip_msg({
76
+ $rs: '200',
77
+ $rr: 'OK',
78
+ '$(hdrcnt(VIA))': 1,
79
+ $fU: 'alice',
80
+ $fd: 'test.com',
81
+ $tU: 'bob',
82
+ '$hdr(content-type)': 'application/sdp',
83
+ $rb: '!{_}a=sendrecv',
84
+ }),
85
+ },
86
+ {
87
+ event: 'media_update',
88
+ call_id: oc.id,
89
+ status: 'ok',
90
+ },
91
+ {
92
+ event: 'media_update',
93
+ call_id: ic.id,
94
+ status: 'ok',
95
+ },
96
+ ], 1000)
97
+
98
+ sip.call.send_dtmf(oc.id, {digits: '1234', mode: 1})
99
+ sip.call.send_dtmf(ic.id, {digits: '1234', mode: 1})
100
+
101
+ await z.wait([
102
+ {
103
+ event: 'dtmf',
104
+ call_id: ic.id,
105
+ digits: '1234',
106
+ mode: 1,
107
+ media_id: 0,
108
+ },
109
+ {
110
+ event: 'dtmf',
111
+ call_id: ic.id,
112
+ digits: '1234',
113
+ mode: 1,
114
+ media_id: 1,
115
+ },
116
+ {
117
+ event: 'dtmf',
118
+ call_id: oc.id,
119
+ digits: '1234',
120
+ mode: 1,
121
+ media_id: 0,
122
+ },
123
+ {
124
+ event: 'dtmf',
125
+ call_id: oc.id,
126
+ digits: '1234',
127
+ mode: 1,
128
+ media_id: 1,
129
+ },
130
+ ], 2000)
131
+
132
+ sip.call.reinvite(oc.id, {media: [
133
+ {
134
+ type: 'audio',
135
+ secure: false,
136
+ },
137
+ {
138
+ type: 'audio',
139
+ secure: true,
140
+ },
141
+ ]})
142
+
143
+ await z.wait([
144
+ {
145
+ event: 'reinvite',
146
+ call_id: ic.id,
147
+ },
148
+ ], 500)
149
+
150
+ sip.call.respond(ic.id, {code: 200, reason: 'OK', media: [
151
+ {
152
+ type: 'audio',
153
+ secure: false,
154
+ },
155
+ {
156
+ type: 'audio',
157
+ secure: true,
158
+ },
159
+ ]})
160
+
161
+ await z.wait([
162
+ {
163
+ event: 'response',
164
+ call_id: oc.id,
165
+ method: 'INVITE',
166
+ msg: sip_msg({
167
+ $rs: '200',
168
+ $rb: sdp.jsonpath_matcher({
169
+ '$.media.length': [2],
170
+ '$.media[*].desc.type': ['audio', 'audio'],
171
+ '$.media[*].desc.port': [m.nonzero, m.nonzero],
172
+ '$.media[*].desc.protocol': ['RTP/AVP', 'RTP/SAVP'],
173
+ }),
174
+ }),
175
+ },
176
+ {
177
+ event: 'media_update',
178
+ call_id: ic.id,
179
+ status: 'ok',
180
+ media: m.fm([
181
+ m.pm({
182
+ type: 'audio',
183
+ protocol: 'RTP/AVP',
184
+ local: {
185
+ mode: 'sendrecv'
186
+ },
187
+ remote: {
188
+ mode: 'sendrecv'
189
+ },
190
+ fmt: [
191
+ '0 PCMU/8000',
192
+ '120 telephone-event/8000'
193
+ ]
194
+ }),
195
+ m.pm({
196
+ type: 'audio',
197
+ protocol: 'RTP/SAVP',
198
+ local: {
199
+ mode: 'sendrecv'
200
+ },
201
+ remote: {
202
+ mode: 'sendrecv'
203
+ },
204
+ fmt: [
205
+ '0 PCMU/8000',
206
+ '120 telephone-event/8000'
207
+ ]
208
+ }),
209
+ ]),
210
+ },
211
+ {
212
+ event: 'media_update',
213
+ call_id: oc.id,
214
+ status: 'ok',
215
+ media: m.fm([
216
+ m.pm({
217
+ type: 'audio',
218
+ protocol: 'RTP/AVP',
219
+ local: {
220
+ mode: 'sendrecv'
221
+ },
222
+ remote: {
223
+ mode: 'sendrecv'
224
+ },
225
+ fmt: [
226
+ '0 PCMU/8000',
227
+ '120 telephone-event/8000'
228
+ ]
229
+ }),
230
+ m.pm({
231
+ type: 'audio',
232
+ protocol: 'RTP/SAVP',
233
+ local: {
234
+ mode: 'sendrecv'
235
+ },
236
+ remote: {
237
+ mode: 'sendrecv'
238
+ },
239
+ fmt: [
240
+ '0 PCMU/8000',
241
+ '120 telephone-event/8000'
242
+ ]
243
+ }),
244
+ ]),
245
+ },
246
+ ], 1000)
247
+
248
+ sip.call.send_dtmf(oc.id, {digits: '1234', mode: 1})
249
+ sip.call.send_dtmf(ic.id, {digits: '1234', mode: 1})
250
+
251
+ await z.wait([
252
+ {
253
+ event: 'dtmf',
254
+ call_id: ic.id,
255
+ digits: '1234',
256
+ mode: 1,
257
+ media_id: 0,
258
+ },
259
+ {
260
+ event: 'dtmf',
261
+ call_id: ic.id,
262
+ digits: '1234',
263
+ mode: 1,
264
+ media_id: 1,
265
+ },
266
+ {
267
+ event: 'dtmf',
268
+ call_id: oc.id,
269
+ digits: '1234',
270
+ mode: 1,
271
+ media_id: 0,
272
+ },
273
+ {
274
+ event: 'dtmf',
275
+ call_id: oc.id,
276
+ digits: '1234',
277
+ mode: 1,
278
+ media_id: 1,
279
+ },
280
+ ], 2000)
281
+
282
+
283
+ sip.call.reinvite(ic.id, {media: [
284
+ {
285
+ type: 'audio',
286
+ secure: false,
287
+ },
288
+ {
289
+ type: 'audio',
290
+ secure: true,
291
+ },
292
+ ]})
293
+
294
+ await z.wait([
295
+ {
296
+ event: 'reinvite',
297
+ call_id: oc.id,
298
+ },
299
+ ], 500)
300
+
301
+ sip.call.respond(oc.id, {code: 200, reason: 'OK', media: [
302
+ {
303
+ type: 'audio',
304
+ secure: false,
305
+ },
306
+ {
307
+ type: 'audio',
308
+ secure: true,
309
+ },
310
+ ]})
311
+
312
+ await z.wait([
313
+ {
314
+ event: 'response',
315
+ call_id: ic.id,
316
+ method: 'INVITE',
317
+ msg: sip_msg({
318
+ $rs: '200',
319
+ $rb: sdp.jsonpath_matcher({
320
+ '$.media.length': [2],
321
+ '$.media[*].desc.type': ['audio', 'audio'],
322
+ '$.media[*].desc.port': [m.nonzero, m.nonzero],
323
+ '$.media[*].desc.protocol': ['RTP/AVP', 'RTP/SAVP'],
324
+ }),
325
+ }),
326
+ },
327
+ {
328
+ event: 'media_update',
329
+ call_id: oc.id,
330
+ status: 'ok',
331
+ media: m.fm([
332
+ m.pm({
333
+ type: 'audio',
334
+ protocol: 'RTP/AVP',
335
+ local: {
336
+ mode: 'sendrecv'
337
+ },
338
+ remote: {
339
+ mode: 'sendrecv'
340
+ },
341
+ fmt: [
342
+ '0 PCMU/8000',
343
+ '120 telephone-event/8000'
344
+ ]
345
+ }),
346
+ m.pm({
347
+ type: 'audio',
348
+ protocol: 'RTP/SAVP',
349
+ local: {
350
+ mode: 'sendrecv'
351
+ },
352
+ remote: {
353
+ mode: 'sendrecv'
354
+ },
355
+ fmt: [
356
+ '0 PCMU/8000',
357
+ '120 telephone-event/8000'
358
+ ]
359
+ }),
360
+ ]),
361
+ },
362
+ {
363
+ event: 'media_update',
364
+ call_id: ic.id,
365
+ status: 'ok',
366
+ media: m.fm([
367
+ m.pm({
368
+ type: 'audio',
369
+ protocol: 'RTP/AVP',
370
+ local: {
371
+ mode: 'sendrecv'
372
+ },
373
+ remote: {
374
+ mode: 'sendrecv'
375
+ },
376
+ fmt: [
377
+ '0 PCMU/8000',
378
+ '120 telephone-event/8000'
379
+ ]
380
+ }),
381
+ m.pm({
382
+ type: 'audio',
383
+ protocol: 'RTP/SAVP',
384
+ local: {
385
+ mode: 'sendrecv'
386
+ },
387
+ remote: {
388
+ mode: 'sendrecv'
389
+ },
390
+ fmt: [
391
+ '0 PCMU/8000',
392
+ '120 telephone-event/8000'
393
+ ]
394
+ }),
395
+ ]),
396
+ },
397
+ ], 1000)
398
+
399
+ sip.call.send_dtmf(oc.id, {digits: '1234', mode: 1})
400
+ sip.call.send_dtmf(ic.id, {digits: '1234', mode: 1})
401
+
402
+ await z.wait([
403
+ {
404
+ event: 'dtmf',
405
+ call_id: ic.id,
406
+ digits: '1234',
407
+ mode: 1,
408
+ media_id: 0,
409
+ },
410
+ {
411
+ event: 'dtmf',
412
+ call_id: ic.id,
413
+ digits: '1234',
414
+ mode: 1,
415
+ media_id: 1,
416
+ },
417
+ {
418
+ event: 'dtmf',
419
+ call_id: oc.id,
420
+ digits: '1234',
421
+ mode: 1,
422
+ media_id: 0,
423
+ },
424
+ {
425
+ event: 'dtmf',
426
+ call_id: oc.id,
427
+ digits: '1234',
428
+ mode: 1,
429
+ media_id: 1,
430
+ },
431
+ ], 2000)
432
+
433
+ sip.call.terminate(oc.id)
434
+
435
+ await z.wait([
436
+ {
437
+ event: 'response',
438
+ call_id: oc.id,
439
+ method: 'BYE',
440
+ msg: sip_msg({
441
+ $rs: '200',
442
+ $rr: 'OK',
443
+ }),
444
+ },
445
+ {
446
+ event: 'call_ended',
447
+ call_id: oc.id,
448
+ },
449
+ {
450
+ event: 'call_ended',
451
+ call_id: ic.id,
452
+ },
453
+ ], 1000)
454
+
455
+ console.log("Success")
456
+
457
+ sip.stop()
458
+ }
459
+
460
+
461
+ test()
462
+ .catch(e => {
463
+ console.error(e)
464
+ process.exit(1)
465
+ })
466
+