-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinbound-call.ts
79 lines (67 loc) · 2.46 KB
/
inbound-call.ts
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
import fs from "node:fs";
import process from "node:process";
import type { RtpPacket } from "werift-rtp";
import Softphone from "../src/index.js";
// import waitFor from 'wait-for-async';
const softphone = new Softphone({
outboundProxy: process.env.SIP_INFO_OUTBOUND_PROXY!,
username: process.env.SIP_INFO_USERNAME!,
password: process.env.SIP_INFO_PASSWORD!,
authorizationId: process.env.SIP_INFO_AUTHORIZATION_ID!,
domain: process.env.SIP_INFO_DOMAIN!,
// codec: "OPUS/16000", // optional, default is "OPUS/16000", you may also specify "PCMU/8000", "OPUS/48000/2"
});
softphone.enableDebugMode(); // print all SIP messages
const main = async () => {
await softphone.register();
// detect inbound call
softphone.on("invite", async (inviteMessage) => {
// decline the call
// await waitFor({ interval: 1000 });
// await softphone.decline(inviteMessage);
// answer the call
const callSession = await softphone.answer(inviteMessage);
// receive audio
const writeStream = fs.createWriteStream(`${callSession.callId}.wav`, {
flags: "a",
});
callSession.on("audioPacket", (rtpPacket: RtpPacket) => {
writeStream.write(rtpPacket.payload);
});
// call transfer
// await waitFor({ interval: 3000 });
// callSession.transfer(process.env.ANOTHER_CALLEE_FOR_TESTING!);
// // send audio to remote peer
// const streamer = callSession.streamAudio(fs.readFileSync('demos/test.wav'));
// // You may subscribe to the 'finished' event of the streamer to know when the audio sending is finished
// streamer.once('finished', () => {
// console.log('audio sending finished');
// });
// // you may pause/resume/stop audio sending at any time
// await waitFor({ interval: 3000 });
// streamer.pause();
// await waitFor({ interval: 3000 });
// streamer.resume();
// await waitFor({ interval: 2000 });
// streamer.stop();
// // you may start/restart the streaming:
// streamer.start();
// either you or the peer hang up
callSession.once("disposed", () => {
writeStream.close();
});
// receive DTMF
callSession.on("dtmf", (digit) => {
console.log("dtmf", digit);
});
// // send DTMF
// await waitFor({ interval: 2000 });
// callSession.sendDTMF('1');
// await waitFor({ interval: 2000 });
// callSession.sendDTMF('#');
// // hang up the call
// await waitFor({ interval: 5000 });
// callSession.hangup();
});
};
main();