-
Notifications
You must be signed in to change notification settings - Fork 19
/
whip.go
151 lines (128 loc) · 3.7 KB
/
whip.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
package main
import (
"bytes"
"crypto/tls"
"io"
"log"
"net/http"
"net/url"
"github.com/pion/mediadevices"
"github.com/pion/webrtc/v3"
)
type WHIPClient struct {
endpoint string
token string
resourceUrl string
}
func NewWHIPClient(endpoint string, token string) *WHIPClient {
client := new(WHIPClient)
client.endpoint = endpoint
client.token = token
return client
}
func (whip *WHIPClient) Publish(stream mediadevices.MediaStream, mediaEngine webrtc.MediaEngine, iceServers []webrtc.ICEServer, skipTlsAuth bool) {
config := webrtc.Configuration{
ICEServers: iceServers,
}
settings := webrtc.SettingEngine{}
// settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
pc, err := webrtc.NewAPI(
webrtc.WithMediaEngine(&mediaEngine),
webrtc.WithSettingEngine(settings),
).NewPeerConnection(config)
if err != nil {
log.Fatal("Unexpected error building the PeerConnection. ", err)
}
for _, track := range stream.GetTracks() {
track.OnEnded(func(err error) {
log.Println("Track ended with error, ", err)
})
_, err = pc.AddTransceiverFromTrack(track,
webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
},
)
if err != nil {
panic(err)
}
}
pc.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
log.Printf("PeerConnection State has changed %s \n", connectionState.String())
})
offer, err := pc.CreateOffer(nil)
if err != nil {
log.Fatal("PeerConnection could not create offer. ", err)
}
err = pc.SetLocalDescription(offer)
if err != nil {
log.Fatal("PeerConnection could not set local offer. ", err)
}
// 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 := webrtc.GatheringCompletePromise(pc)
<-gatherComplete
// log.Println(pc.LocalDescription().SDP)
var sdp = []byte(pc.LocalDescription().SDP)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipTlsAuth,
},
},
}
req, err := http.NewRequest("POST", whip.endpoint, bytes.NewBuffer(sdp))
if err != nil {
log.Fatal("Unexpected error building http request. ", err)
}
req.Header.Add("Content-Type", "application/sdp")
if whip.token != "" {
req.Header.Add("Authorization", "Bearer "+whip.token)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Failed http POST request. ", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
// log.Println(string(body))
if resp.StatusCode != 201 {
log.Fatalf("Non Successful POST: %d", resp.StatusCode)
}
resourceUrl, err := url.Parse(resp.Header.Get("Location"))
if err != nil {
log.Fatal("Failed to parse resource url. ", err)
}
base, err := url.Parse(whip.endpoint)
if err != nil {
log.Fatal("Failed to parse base url. ", err)
}
whip.resourceUrl = base.ResolveReference(resourceUrl).String()
answer := webrtc.SessionDescription{}
answer.Type = webrtc.SDPTypeAnswer
answer.SDP = string(body)
err = pc.SetRemoteDescription(answer)
if err != nil {
log.Fatal("PeerConnection could not set remote answer. ", err)
}
}
func (whip *WHIPClient) Close(skipTlsAuth bool) {
req, err := http.NewRequest("DELETE", whip.resourceUrl, nil)
if err != nil {
log.Fatal("Unexpected error building http request. ", err)
}
if whip.token != "" {
req.Header.Add("Authorization", "Bearer "+whip.token)
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipTlsAuth,
},
},
}
_, err = client.Do(req)
if err != nil {
log.Fatal("Failed http DELETE request. ", err)
}
}