-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsoftphone_test.go
80 lines (67 loc) · 1.88 KB
/
softphone_test.go
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
80
package softphone
import (
"encoding/json"
"log"
"os"
"os/user"
"strings"
"testing"
"github.com/joho/godotenv"
"github.com/pion/webrtc/v3"
"github.com/ringcentral/ringcentral-go"
"github.com/zaf/g711"
)
func loadDotEnv() {
usr, err := user.Current()
if err == nil {
// ignore error because we fallback to sys env vars
godotenv.Overload(usr.HomeDir + "/.env.prod")
}
}
func TestSoftphone(t *testing.T) {
loadDotEnv()
rc := ringcentral.RestClient{
ClientID: os.Getenv("RINGCENTRAL_CLIENT_ID"),
ClientSecret: os.Getenv("RINGCENTRAL_CLIENT_SECRET"),
Server: os.Getenv("RINGCENTRAL_SERVER_URL"),
}
rc.Authorize(ringcentral.GetTokenRequest{
GrantType: "password",
Username: os.Getenv("RINGCENTRAL_USERNAME"),
Extension: os.Getenv("RINGCENTRAL_EXTENSION"),
Password: os.Getenv("RINGCENTRAL_PASSWORD"),
})
bytes := rc.Post("/restapi/v1.0/client-info/sip-provision", strings.NewReader(`{"sipInfo":[{"transport":"WSS"}]}`))
var createSipRegistrationResponse ringcentral.CreateSipRegistrationResponse
json.Unmarshal(bytes, &createSipRegistrationResponse)
if len(createSipRegistrationResponse.SipInfo) <= 0 {
t.Error("No SipInfo")
}
softphone := Softphone{
CreateSipRegistrationResponse: createSipRegistrationResponse,
}
softphone.Register()
softphone.OnInvite = func(inviteMessage SipMessage) {
softphone.Answer(inviteMessage)
}
softphone.OnTrack = func(track *webrtc.TrackRemote) {
fileName := "temp.raw"
os.Remove(fileName)
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
for {
rtp, _, err := track.ReadRTP()
if err != nil {
log.Fatal(err)
}
// g711.DecodeUlaw(...): change u-law PCM to LPCM
f.Write(g711.DecodeUlaw(rtp.Payload))
}
}
rc.Revoke()
select {} //block forever
}
// to play the audio: play -b 16 -e signed -c 1 -r 8000 temp.raw