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