sip-lab 1.14.0 → 1.16.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,430 @@
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
+ port: 0, // media refused
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
+ media: m.fm([
91
+ m.pm({
92
+ type: 'audio',
93
+ protocol: 'RTP/AVP',
94
+ port: 0,
95
+ }),
96
+ m.pm({
97
+ type: 'audio',
98
+ protocol: 'RTP/SAVP',
99
+ local: {
100
+ mode: 'sendrecv'
101
+ },
102
+ remote: {
103
+ mode: 'sendrecv'
104
+ },
105
+ fmt: [
106
+ '0 PCMU/8000',
107
+ '120 telephone-event/8000'
108
+ ]
109
+ }),
110
+ ]),
111
+ },
112
+ {
113
+ event: 'media_update',
114
+ call_id: ic.id,
115
+ status: 'ok',
116
+ media: m.fm([
117
+ m.pm({
118
+ type: 'audio',
119
+ protocol: 'RTP/AVP',
120
+ port: 0,
121
+ }),
122
+ m.pm({
123
+ type: 'audio',
124
+ protocol: 'RTP/SAVP',
125
+ local: {
126
+ mode: 'sendrecv'
127
+ },
128
+ remote: {
129
+ mode: 'sendrecv'
130
+ },
131
+ fmt: [
132
+ '0 PCMU/8000',
133
+ '120 telephone-event/8000'
134
+ ]
135
+ }),
136
+ ]),
137
+ },
138
+ ], 1000)
139
+
140
+ sip.call.send_dtmf(oc.id, {digits: '1234', mode: 1})
141
+ sip.call.send_dtmf(ic.id, {digits: '1234', mode: 1})
142
+
143
+ await z.wait([
144
+ {
145
+ event: 'dtmf',
146
+ call_id: ic.id,
147
+ digits: '1234',
148
+ mode: 1,
149
+ media_id: 1,
150
+ },
151
+ {
152
+ event: 'dtmf',
153
+ call_id: oc.id,
154
+ digits: '1234',
155
+ mode: 1,
156
+ media_id: 1,
157
+ },
158
+ ], 2000)
159
+
160
+ sip.call.reinvite(oc.id, {media: [
161
+ {
162
+ type: 'audio',
163
+ secure: false,
164
+ },
165
+ {
166
+ type: 'audio',
167
+ secure: true,
168
+ },
169
+ ]})
170
+
171
+ await z.wait([
172
+ {
173
+ event: 'reinvite',
174
+ call_id: ic.id,
175
+ },
176
+ ], 500)
177
+
178
+ sip.call.respond(ic.id, {code: 200, reason: 'OK', media: [
179
+ {
180
+ type: 'audio',
181
+ port: 0, // media refused
182
+ },
183
+ {
184
+ type: 'audio',
185
+ secure: true,
186
+ },
187
+ ]})
188
+
189
+ await z.wait([
190
+ {
191
+ event: 'response',
192
+ call_id: oc.id,
193
+ method: 'INVITE',
194
+ msg: sip_msg({
195
+ $rs: '200',
196
+ $rb: sdp.jsonpath_matcher({
197
+ '$.media.length': [2],
198
+ '$.media[*].desc.type': ['audio', 'audio'],
199
+ '$.media[*].desc.port': ['0', m.nonzero],
200
+ '$.media[*].desc.protocol': ['RTP/AVP', 'RTP/SAVP'],
201
+ }),
202
+ }),
203
+ },
204
+ {
205
+ event: 'media_update',
206
+ call_id: ic.id,
207
+ status: 'ok',
208
+ media: m.fm([
209
+ m.pm({
210
+ type: 'audio',
211
+ protocol: 'RTP/AVP',
212
+ port: 0,
213
+ }),
214
+ m.pm({
215
+ type: 'audio',
216
+ protocol: 'RTP/SAVP',
217
+ local: {
218
+ mode: 'sendrecv'
219
+ },
220
+ remote: {
221
+ mode: 'sendrecv'
222
+ },
223
+ fmt: [
224
+ '0 PCMU/8000',
225
+ '120 telephone-event/8000'
226
+ ]
227
+ }),
228
+ ]),
229
+ },
230
+ {
231
+ event: 'media_update',
232
+ call_id: oc.id,
233
+ status: 'ok',
234
+ media: m.fm([
235
+ m.pm({
236
+ type: 'audio',
237
+ protocol: 'RTP/AVP',
238
+ port: 0,
239
+ }),
240
+ m.pm({
241
+ type: 'audio',
242
+ protocol: 'RTP/SAVP',
243
+ local: {
244
+ mode: 'sendrecv'
245
+ },
246
+ remote: {
247
+ mode: 'sendrecv'
248
+ },
249
+ fmt: [
250
+ '0 PCMU/8000',
251
+ '120 telephone-event/8000'
252
+ ]
253
+ }),
254
+ ]),
255
+ },
256
+ ], 1000)
257
+
258
+ sip.call.send_dtmf(oc.id, {digits: '1234', mode: 1})
259
+ sip.call.send_dtmf(ic.id, {digits: '1234', mode: 1})
260
+
261
+ await z.wait([
262
+ {
263
+ event: 'dtmf',
264
+ call_id: ic.id,
265
+ digits: '1234',
266
+ mode: 1,
267
+ media_id: 1,
268
+ },
269
+ {
270
+ event: 'dtmf',
271
+ call_id: oc.id,
272
+ digits: '1234',
273
+ mode: 1,
274
+ media_id: 1,
275
+ },
276
+ ], 2000)
277
+
278
+
279
+ sip.call.reinvite(ic.id, {media: [
280
+ {
281
+ type: 'audio',
282
+ secure: false,
283
+ },
284
+ {
285
+ type: 'audio',
286
+ secure: true,
287
+ },
288
+ ]})
289
+
290
+ await z.wait([
291
+ {
292
+ event: 'reinvite',
293
+ call_id: oc.id,
294
+ },
295
+ ], 500)
296
+
297
+ sip.call.respond(oc.id, {code: 200, reason: 'OK', media: [
298
+ {
299
+ type: 'audio',
300
+ port: 0, // media refused
301
+ },
302
+ {
303
+ type: 'audio',
304
+ secure: true,
305
+ },
306
+ ]})
307
+
308
+ await z.wait([
309
+ {
310
+ event: 'response',
311
+ call_id: ic.id,
312
+ method: 'INVITE',
313
+ msg: sip_msg({
314
+ $rs: '200',
315
+ $rb: sdp.jsonpath_matcher({
316
+ '$.media.length': [2],
317
+ '$.media[*].desc.type': ['audio', 'audio'],
318
+ '$.media[*].desc.port': ['0', m.nonzero],
319
+ '$.media[*].desc.protocol': ['RTP/AVP', 'RTP/SAVP'],
320
+ }),
321
+ }),
322
+ },
323
+ {
324
+ event: 'media_update',
325
+ call_id: oc.id,
326
+ status: 'ok',
327
+ media: m.fm([
328
+ m.pm({
329
+ type: 'audio',
330
+ protocol: 'RTP/AVP',
331
+ port: 0,
332
+ }),
333
+ m.pm({
334
+ type: 'audio',
335
+ protocol: 'RTP/SAVP',
336
+ local: {
337
+ mode: 'sendrecv'
338
+ },
339
+ remote: {
340
+ mode: 'sendrecv'
341
+ },
342
+ fmt: [
343
+ '0 PCMU/8000',
344
+ '120 telephone-event/8000'
345
+ ]
346
+ }),
347
+ ]),
348
+ },
349
+ {
350
+ event: 'media_update',
351
+ call_id: ic.id,
352
+ status: 'ok',
353
+ media: m.fm([
354
+ m.pm({
355
+ type: 'audio',
356
+ protocol: 'RTP/AVP',
357
+ port: 0,
358
+ }),
359
+ m.pm({
360
+ type: 'audio',
361
+ protocol: 'RTP/SAVP',
362
+ local: {
363
+ mode: 'sendrecv'
364
+ },
365
+ remote: {
366
+ mode: 'sendrecv'
367
+ },
368
+ fmt: [
369
+ '0 PCMU/8000',
370
+ '120 telephone-event/8000'
371
+ ]
372
+ }),
373
+ ]),
374
+ },
375
+ ], 1000)
376
+
377
+ sip.call.send_dtmf(oc.id, {digits: '1234', mode: 1})
378
+ sip.call.send_dtmf(ic.id, {digits: '1234', mode: 1})
379
+
380
+ await z.wait([
381
+ {
382
+ event: 'dtmf',
383
+ call_id: ic.id,
384
+ digits: '1234',
385
+ mode: 1,
386
+ media_id: 1,
387
+ },
388
+ {
389
+ event: 'dtmf',
390
+ call_id: oc.id,
391
+ digits: '1234',
392
+ mode: 1,
393
+ media_id: 1,
394
+ },
395
+ ], 2000)
396
+
397
+ sip.call.terminate(oc.id)
398
+
399
+ await z.wait([
400
+ {
401
+ event: 'response',
402
+ call_id: oc.id,
403
+ method: 'BYE',
404
+ msg: sip_msg({
405
+ $rs: '200',
406
+ $rr: 'OK',
407
+ }),
408
+ },
409
+ {
410
+ event: 'call_ended',
411
+ call_id: oc.id,
412
+ },
413
+ {
414
+ event: 'call_ended',
415
+ call_id: ic.id,
416
+ },
417
+ ], 1000)
418
+
419
+ console.log("Success")
420
+
421
+ sip.stop()
422
+ }
423
+
424
+
425
+ test()
426
+ .catch(e => {
427
+ console.error(e)
428
+ process.exit(1)
429
+ })
430
+
@@ -0,0 +1,189 @@
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
+ const sipjs = require('sipjs-lab')
8
+ const {endpoint, dialog} = require('sipjs-lab')
9
+
10
+ const util = require('util')
11
+
12
+ // here we create our Zeq instance
13
+ var z = new Zeq()
14
+
15
+ async function test() {
16
+ sip.dtmf_aggregation_on(500)
17
+
18
+ z.trap_events(sip.event_source, 'event', (evt) => {
19
+ var e = evt.args[0]
20
+ return e
21
+ })
22
+
23
+ z.trap_events(sipjs.event_source, 'event', (evt) => {
24
+ var e = evt.args[0]
25
+ return e
26
+ })
27
+
28
+ // Let's ignore '100 Trying'
29
+ z.add_event_filter({
30
+ event: 'response',
31
+ msg: sip_msg({
32
+ $rs: '100',
33
+ }),
34
+ })
35
+
36
+ console.log(sip.start((data) => { console.log(data)} ))
37
+
38
+ const t1 = sip.transport.create({address: "127.0.0.1"})
39
+
40
+ const e1_port = 7070
41
+ const e1 = endpoint.create({address: "127.0.0.1", port: e1_port, publicAddress: "127.0.0.1" })
42
+
43
+ const oc = sip.call.create(t1.id, {from_uri: 'sip:alice@test.com', to_uri: `sip:bob@127.0.0.1:${e1_port}`,
44
+ media: [
45
+ {
46
+ type: 'audio',
47
+ secure: false,
48
+ },
49
+ {
50
+ type: 'audio',
51
+ secure: true,
52
+ },
53
+ ]})
54
+
55
+ await z.wait([
56
+ {
57
+ source: 'sip_endpoint',
58
+ endpoint_id: m.collect('endpoint_id'),
59
+ req: m.collect('req', {
60
+ method: 'INVITE',
61
+ uri: 'sip:bob@127.0.0.1:7070',
62
+ version: '2.0',
63
+ headers: {
64
+ from: {
65
+ uri: 'sip:alice@test.com',
66
+ },
67
+ to: {
68
+ uri: 'sip:bob@127.0.0.1',
69
+ },
70
+ },
71
+ //content:
72
+ }),
73
+ event: 'dialog_offer',
74
+ dialog_id: m.collect('dialog_id'),
75
+ },
76
+ ], 1000)
77
+
78
+ const sdp_answer =`v=0
79
+ o=- 3914707056 3914707057 IN IP4 0.0.0.0
80
+ s=pjmedia
81
+ t=0 0
82
+ m=audio 10004 RTP/SAVP 0 120
83
+ c=IN IP4 127.0.0.1
84
+ b=TIAS:64000
85
+ a=rtcp:10005 IN IP4 127.0.0.1
86
+ a=sendrecv
87
+ a=rtpmap:0 PCMU/8000
88
+ a=rtpmap:120 telephone-event/8000
89
+ a=fmtp:120 0-16
90
+ a=crypto:1 AES_256_CM_HMAC_SHA1_80 inline:s6nwVuVNxwupG0Cls+/4X4L46Mx/fqoEMolllUrx/FNCFpVjG2kGFLxPRGvMNw==`
91
+
92
+ dialog.send_reply(
93
+ z.store.dialog_id,
94
+ z.store.req,
95
+ {
96
+ status: 200,
97
+ reason: 'OK',
98
+ headers: {
99
+ 'content-type': 'application/sdp',
100
+ },
101
+ content: sdp_answer,
102
+ }
103
+ )
104
+
105
+ await z.wait([
106
+ {
107
+ event: 'response',
108
+ call_id: oc.id,
109
+ method: 'INVITE',
110
+ msg: sip_msg({
111
+ $rs: '200',
112
+ $rr: 'OK',
113
+ '$(hdrcnt(VIA))': 1,
114
+ $fU: 'alice',
115
+ $fd: 'test.com',
116
+ $tU: 'bob',
117
+ '$hdr(content-type)': 'application/sdp',
118
+ $rb: '!{_}a=sendrecv',
119
+ }),
120
+ },
121
+ {
122
+ event: 'media_update',
123
+ call_id: oc.id,
124
+ status: 'ok',
125
+ media: m.fm([
126
+ m.pm({
127
+ type: 'audio',
128
+ protocol: 'RTP/AVP',
129
+ port: 0,
130
+ }),
131
+ m.pm({
132
+ type: 'audio',
133
+ protocol: 'RTP/SAVP',
134
+ local: {
135
+ mode: 'sendrecv'
136
+ },
137
+ remote: {
138
+ mode: 'sendrecv'
139
+ },
140
+ fmt: [
141
+ '0 PCMU/8000',
142
+ '120 telephone-event/8000'
143
+ ]
144
+ }),
145
+ ]),
146
+ },
147
+ {
148
+ source: 'sip_endpoint',
149
+ endpoint_id: z.store.endpoint_id,
150
+ req: {
151
+ method: 'ACK',
152
+ },
153
+ event: 'in_dialog_request',
154
+ dialog_id: z.store.dialog_id
155
+ },
156
+ ], 1000)
157
+
158
+ await z.sleep(1000)
159
+
160
+ sip.call.terminate(oc.id)
161
+
162
+ await z.wait([
163
+ {
164
+ event: 'response',
165
+ call_id: oc.id,
166
+ method: 'BYE',
167
+ msg: sip_msg({
168
+ $rs: '200',
169
+ $rr: 'OK',
170
+ }),
171
+ },
172
+ {
173
+ event: 'call_ended',
174
+ call_id: oc.id,
175
+ },
176
+ ], 1000)
177
+
178
+ console.log("Success")
179
+
180
+ sip.stop()
181
+ }
182
+
183
+
184
+ test()
185
+ .catch(e => {
186
+ console.error(e)
187
+ process.exit(1)
188
+ })
189
+