-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
204 lines (170 loc) · 5.58 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
// +build !js
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/pion/webrtc/v3"
)
var ECHO_TEST_SERVER_URL = "http://localhost:8080/offer"
var CONNECTION_ATTEMPT_TIMEOUT_SECONDS = 10
var SUCCESS_RETURN_VALUE = 0
var ERROR_RETURN_VALUE = 1
func main() {
echoTestServerURL := ECHO_TEST_SERVER_URL
if len(os.Args) > 1 {
echoTestServerURL = os.Args[1]
println("Echo test server URL set to:", echoTestServerURL)
}
connectResult := false
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
fmt.Println("Panic returning status:", ERROR_RETURN_VALUE)
os.Exit(ERROR_RETURN_VALUE)
}()
ctx, cancel := context.WithTimeout(context.Background(),
time.Duration(CONNECTION_ATTEMPT_TIMEOUT_SECONDS)*time.Second)
defer cancel()
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
// Create a MediaEngine object to configure the supported codec
m := webrtc.MediaEngine{}
// Setup the codecs you want to use.
// We'll use a VP8 and Opus but you can also define your own
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 96,
}, webrtc.RTPCodecTypeVideo); err != nil {
panic(err)
}
// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(&m))
// Prepare the configuration
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
// Create a new RTCPeerConnection
peerConnection, err := api.NewPeerConnection(config)
if err != nil {
panic(err)
}
// Create Track that we send video back to browser on
outputTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
if err != nil {
panic(err)
}
// Add this newly created track to the PeerConnection
rtpSender, err := peerConnection.AddTrack(outputTrack)
if err != nil {
panic(err)
}
// Read incoming RTCP packets
// Before these packets are retuned they are processed by interceptors. For things
// like NACK this needs to be called.
go func() {
rtcpBuf := make([]byte, 1500)
for {
if _, _, rtcpErr := rtpSender.Read(rtcpBuf); rtcpErr != nil {
return
}
}
}()
// Add ICE candidates to the local offer (simulates non-trickle).
peerConnection.OnICECandidate(func(c *webrtc.ICECandidate) {
if c == nil {
//fmt.Println(peerConnection.LocalDescription())
}
})
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
panic(err)
}
// Sets the LocalDescription, and starts our UDP listeners
if err = peerConnection.SetLocalDescription(offer); 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("ICE connection state has changed %s.\n", connectionState.String())
})
peerConnection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
fmt.Printf("Peer connection state has changed to %s.\n", state.String())
if state == webrtc.PeerConnectionStateConnected {
connectResult = true
peerConnection.Close()
cancel()
} else if state == webrtc.PeerConnectionStateDisconnected ||
state == webrtc.PeerConnectionStateFailed ||
state == webrtc.PeerConnectionStateClosed {
connectResult = true
cancel()
}
})
peerConnection.OnSignalingStateChange(func(state webrtc.SignalingState) {
fmt.Printf("Signaling state has changed to %s.\n", state.String())
})
// 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)
// Block until ICE Gathering is complete, disabling trickle ICE
// we do this because we only can exchange one signaling message
// in a production application you should exchange ICE Candidates via OnICECandidate
<-gatherComplete
fmt.Printf("Attempting to POST offer to %s.\n", echoTestServerURL)
// POST the offer to the echo server.
offerWithIce := peerConnection.LocalDescription()
offerBuf := new(bytes.Buffer)
json.NewEncoder(offerBuf).Encode(offerWithIce)
req, err := http.NewRequest("POST", echoTestServerURL, offerBuf)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: time.Duration(CONNECTION_ATTEMPT_TIMEOUT_SECONDS) * time.Second}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("POST offer response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("response Body:", string(body))
// Set the remote SessionDescription
var answer webrtc.SessionDescription
err = json.NewDecoder(strings.NewReader(string(body))).Decode(&answer)
if err != nil {
panic(err)
}
err = peerConnection.SetRemoteDescription(answer)
if err != nil {
panic(err)
}
// Output the answer in base64 so we can paste it in browser
//fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
select {
case <-ctx.Done():
fmt.Println("Context done.")
}
if connectResult {
fmt.Println("Connection attempt successful returning status:", SUCCESS_RETURN_VALUE)
os.Exit(SUCCESS_RETURN_VALUE)
} else {
fmt.Println("Connection attempt failed returning status:", ERROR_RETURN_VALUE)
os.Exit(ERROR_RETURN_VALUE)
}
}