-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathmain.go
130 lines (105 loc) · 3.24 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
// sip-over-websocket-to-webrtc demonstrates how to connect to a SIP Server via Websocket
package main
import (
"flag"
"fmt"
"github.com/pion/example-webrtc-applications/v3/sip-over-websocket-to-webrtc/softphone"
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media/oggwriter"
)
// nolint
var (
username = flag.String("username", "1000", "Extension you wish to register as")
password = flag.String("password", "", "Password for the extension you wish to register as")
extension = flag.String("extension", "9198", "Extension you wish to call")
host = flag.String("host", "", "Host that websocket is available on")
port = flag.String("port", "5066", "Port that websocket is available on")
)
func main() {
flag.Parse()
if *host == "" || *port == "" || *password == "" {
panic("-host -port and -password are required")
}
conn := softphone.NewSoftPhone(softphone.SIPInfoResponse{
Username: *username,
AuthorizationID: *username,
Password: *password,
Domain: *host,
Transport: "ws",
OutboundProxy: *host + ":" + *port,
})
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
pc.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
})
oggFile, err := oggwriter.New("output.ogg", 48000, 2)
if err != nil {
panic(err)
}
pc.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
fmt.Println("Got Opus track, saving to disk as output.ogg")
for {
rtpPacket, _, readErr := track.ReadRTP()
if readErr != nil {
panic(readErr)
}
if readErr := oggFile.WriteRTP(rtpPacket); readErr != nil {
panic(readErr)
}
}
})
if _, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
offer, err := pc.CreateOffer(nil)
if err != nil {
panic(err)
}
if err := pc.SetLocalDescription(offer); err != nil {
panic(err)
}
gotAnswer := false
conn.OnOK(func(okBody string) {
if gotAnswer {
return
}
gotAnswer = true
okBody += "a=mid:0\r\n"
if err := pc.SetRemoteDescription(webrtc.SessionDescription{Type: webrtc.SDPTypeAnswer, SDP: okBody}); err != nil {
panic(err)
}
})
conn.Invite(*extension, rewriteSDP(offer.SDP))
select {}
}
// Apply the following transformations for FreeSWITCH
// * Add fake srflx candidate to each media section
// * Add msid to each media section
// * Make bundle first attribute at session level.
func rewriteSDP(in string) string {
parsed := &sdp.SessionDescription{}
if err := parsed.Unmarshal([]byte(in)); err != nil {
panic(err)
}
// Reverse global attributes
for i, j := 0, len(parsed.Attributes)-1; i < j; i, j = i+1, j-1 {
parsed.Attributes[i], parsed.Attributes[j] = parsed.Attributes[j], parsed.Attributes[i]
}
parsed.MediaDescriptions[0].Attributes = append(parsed.MediaDescriptions[0].Attributes, sdp.Attribute{
Key: "candidate",
Value: "79019993 1 udp 1686052607 1.1.1.1 9 typ srflx",
})
out, err := parsed.Marshal()
if err != nil {
panic(err)
}
return string(out)
}