-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathmain.go
288 lines (241 loc) · 6.44 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
// sip demonstrates how to bridge SIP traffic and WebRTC
package main
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net"
"os"
"strings"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v4"
)
// nolint
var (
audioTrack *webrtc.TrackLocalStaticRTP
unicastAddress = flag.String("unicast-address", "", "IP of SIP Server (your public IP)")
sipPort = flag.Int("sip-port", 5060, "Port to listen for SIP Traffic")
contentTypeHeaderSDP = sip.ContentTypeHeader("application/sdp")
)
func main() {
// Parse the flags passed to program
flag.Parse()
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
for _, address := range addrs {
if *unicastAddress != "" {
break
}
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
*unicastAddress = ipnet.IP.String()
}
}
}
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
// Prepare the configuration
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
if err != nil {
panic(err)
}
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
})
// Create a audio track
audioTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypePCMU}, "audio", "pion")
if err != nil {
panic(err)
}
if _, err = peerConnection.AddTrack(audioTrack); err != nil {
panic(err)
}
// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
decode(readUntilNewline(), &offer)
// Set the remote SessionDescription
if err = peerConnection.SetRemoteDescription(offer); err != nil {
panic(err)
}
// Create an answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
// Sets the LocalDescription, and starts our UDP listeners
if err = peerConnection.SetLocalDescription(answer); err != nil {
panic(err)
}
<-gatherComplete
// Output the answer in base64 so we can paste it in browser
fmt.Println(encode(peerConnection.LocalDescription()))
// Create the SIP UA
sipUserAgent, err := sipgo.NewUA()
if err != nil {
panic(err)
}
// Create the SIP Server
sipServer, err := sipgo.NewServer(sipUserAgent)
if err != nil {
panic(err)
}
sipServer.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
rtpListenerPort := startRTPListener()
res := sip.NewResponseFromRequest(req, 200, "OK", generateAnswer(req.Body(), *unicastAddress, rtpListenerPort))
res.AppendHeader(&sip.ContactHeader{Address: sip.Uri{Host: *unicastAddress, Port: *sipPort}})
res.AppendHeader(&contentTypeHeaderSDP)
if err = tx.Respond(res); err != nil {
panic(err)
}
fmt.Printf("Accepting SIP Invite: %s\n", req.From())
})
sipServer.OnBye(func(req *sip.Request, tx sip.ServerTransaction) {
if err = tx.Respond(sip.NewResponseFromRequest(req, 200, "OK", nil)); err != nil {
panic(err)
}
})
sipServer.OnAck(func(req *sip.Request, tx sip.ServerTransaction) {
if err = tx.Respond(sip.NewResponseFromRequest(req, 200, "OK", nil)); err != nil {
panic(err)
}
})
fmt.Println("Starting SIP Listener")
// Start Listening for SIP Traffic
panic(sipServer.ListenAndServe(context.TODO(), "udp", fmt.Sprintf("0.0.0.0:%d", *sipPort)))
}
func startRTPListener() int {
conn, err := net.ListenUDP("udp", &net.UDPAddr{
Port: 0,
IP: net.ParseIP("0.0.0.0"),
})
if err != nil {
panic(err)
}
go func() {
buff := make([]byte, 1500)
for {
n, _, err := conn.ReadFromUDP(buff)
if err != nil {
panic(err)
}
if _, err := audioTrack.Write(buff[:n]); err != nil {
panic(err)
}
}
}()
udpAddr, ok := conn.LocalAddr().(*net.UDPAddr)
if !ok {
panic("Failed to cast *net.UDPAddr")
}
return udpAddr.Port
}
func generateAnswer(offer []byte, unicastAddress string, rtpListenerPort int) []byte {
offerParsed := sdp.SessionDescription{}
if err := offerParsed.Unmarshal(offer); err != nil {
panic(err)
}
answer := sdp.SessionDescription{
Version: 0,
Origin: sdp.Origin{
Username: "-",
SessionID: offerParsed.Origin.SessionID,
SessionVersion: offerParsed.Origin.SessionID + 2,
NetworkType: "IN",
AddressType: "IP4",
UnicastAddress: unicastAddress,
},
SessionName: "Pion",
ConnectionInformation: &sdp.ConnectionInformation{
NetworkType: "IN",
AddressType: "IP4",
Address: &sdp.Address{Address: unicastAddress},
},
TimeDescriptions: []sdp.TimeDescription{
{
Timing: sdp.Timing{
StartTime: 0,
StopTime: 0,
},
},
},
MediaDescriptions: []*sdp.MediaDescription{
{
MediaName: sdp.MediaName{
Media: "audio",
Port: sdp.RangedPort{Value: rtpListenerPort},
Protos: []string{"RTP", "AVP"},
Formats: []string{"0"},
},
Attributes: []sdp.Attribute{
{Key: "rtpmap", Value: "0 PCMU/8000"},
{Key: "ptime", Value: "20"},
{Key: "maxptime", Value: "150"},
{Key: "recvonly"},
},
},
},
}
answerByte, err := answer.Marshal()
if err != nil {
panic(err)
}
return answerByte
}
// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error
r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}
fmt.Println("")
return
}
// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(b)
}
// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}
if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}