-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
190 lines (163 loc) · 4.64 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"github.com/google/uuid"
"github.com/pion/webrtc/v3"
)
func main() {
pcs := make(map[string]*webrtc.PeerConnection)
tracks := []*webrtc.TrackLocalStaticRTP{}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[len("/"):]
switch r.Method {
case http.MethodGet:
w.WriteHeader(http.StatusMethodNotAllowed)
case http.MethodPost:
// WHIP create
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
})
if err != nil {
log.Printf("Failed to create peer connection: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
pc.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
log.Printf("Connection State has changed %s", connectionState.String())
})
if id != "" {
// WHEP create
for _, t := range tracks {
if t.StreamID() != id {
continue
}
log.Printf("Adding track: %s", t.StreamID())
if _, err := pc.AddTransceiverFromTrack(t, webrtc.RtpTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly}); err != nil {
log.Printf("Failed to add track: %s", err)
return
}
}
} else {
pc.OnTrack(func(tr *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
log.Printf("Got track: %s", tr.StreamID())
tl, err := webrtc.NewTrackLocalStaticRTP(tr.Codec().RTPCodecCapability, tr.ID(), tr.StreamID())
if err != nil {
log.Printf("Failed to create track: %s", err)
return
}
tracks = append(tracks, tl)
defer func() {
for i, t := range tracks {
if t == tl {
tracks = append(tracks[:i], tracks[i+1:]...)
break
}
}
}()
for {
p, _, err := tr.ReadRTP()
if err != nil {
log.Printf("Failed to read RTP: %s", err)
return
}
if err := tl.WriteRTP(p); err != nil {
log.Printf("Failed to write RTP: %s", err)
return
}
}
})
}
gatherComplete := webrtc.GatheringCompletePromise(pc)
if len(body) > 0 {
if err := pc.SetRemoteDescription(webrtc.SessionDescription{
Type: webrtc.SDPTypeOffer,
SDP: string(body),
}); err != nil {
log.Printf("Failed to set remote description: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
answer, err := pc.CreateAnswer(nil)
if err != nil {
log.Printf("Failed to create answer: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := pc.SetLocalDescription(answer); err != nil {
log.Printf("Failed to set local description: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
} else {
offer, err := pc.CreateOffer(nil)
if err != nil {
log.Printf("Failed to create offer: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := pc.SetLocalDescription(offer); err != nil {
log.Printf("Failed to set local description: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
<-gatherComplete
pcid := uuid.NewString()
w.Header().Set("Content-Type", "application/sdp")
w.Header().Set("Location", fmt.Sprintf("http://%s/%s", r.Host, pcid))
if id == "" {
w.WriteHeader(http.StatusCreated)
}
if _, err := w.Write([]byte(pc.LocalDescription().SDP)); err != nil {
log.Printf("Failed to write response: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
pcs[pcid] = pc
case http.MethodPatch:
if r.Header.Get("Content-Type") != "application/sdp" {
// Trickle ICE/ICE restart not implemented
panic("Not implemented")
}
pc, ok := pcs[id]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if err := pc.SetRemoteDescription(webrtc.SessionDescription{
Type: webrtc.SDPTypeAnswer,
SDP: string(body),
}); err != nil {
log.Printf("Failed to set remote description: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
case http.MethodDelete:
pc, ok := pcs[id]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
if err := pc.Close(); err != nil {
log.Printf("Failed to close peer connection: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
delete(pcs, id)
}
})
panic(http.ListenAndServe(":8080", nil))
}