-
Notifications
You must be signed in to change notification settings - Fork 10
/
answer-and-talk.js
38 lines (35 loc) · 1.25 KB
/
answer-and-talk.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
import RingCentral from '@rc-ex/core'
import { nonstandard } from 'wrtc'
import mediaDevices from 'node-webrtc-media-devices'
import Speaker from 'speaker'
import Softphone from '../../src/index'
const rc = new RingCentral({
server: process.env.RINGCENTRAL_SERVER_URL,
clientId: process.env.RINGCENTRAL_CLIENT_ID,
clientSecret: process.env.RINGCENTRAL_CLIENT_SECRET
})
;(async () => {
await rc.authorize({
username: process.env.RINGCENTRAL_USERNAME,
extension: process.env.RINGCENTRAL_EXTENSION,
password: process.env.RINGCENTRAL_PASSWORD
})
const softphone = new Softphone(rc)
await softphone.register()
await rc.revoke() // rc is no longer needed
softphone.on('INVITE', async sipMessage => {
const inputAudioStream = await mediaDevices.getUserMedia({ audio: true, video: false })
softphone.answer(sipMessage, inputAudioStream)
softphone.once('track', e => {
const speaker = new Speaker({ channels: 1, bitDepth: 16, sampleRate: 48000, signed: true })
const audioSink = new nonstandard.RTCAudioSink(e.track)
audioSink.ondata = data => {
speaker.write(Buffer.from(data.samples.buffer))
}
softphone.once('BYE', () => {
audioSink.stop()
speaker.close()
})
})
})
})()