forked from Sean-Der/OBS2Browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (109 loc) · 2.74 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"sync/atomic"
"golang.org/x/net/websocket"
)
func htmlHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, `
<html>
<head>
</head>
<body>
<button onclick="connect()">Connect</button>
</body>
<script>
window.connect = () => {
document.body.innerHTML = ''
const url = `+"`"+`${window.location.protocol === 'http:' ? 'ws' : 'wss'}://${window.location.hostname}:${window.location.port}/websocket`+"`"+`
const ws = new WebSocket(url)
ws.onmessage = msg => {
const pc = new RTCPeerConnection()
pc.onicecandidate = event => {
if (event.candidate === null) {
ws.send(pc.currentLocalDescription.sdp)
}
}
let added = false
pc.ontrack = function (event) {
if (added) {
return
}
added = true
const el = document.createElement('video')
el.srcObject = event.streams[0]
el.autoplay = true
el.controls = true
document.body.appendChild(el)
event.track.onmute = function(event) {
el.parentNode.removeChild(el)
}
}
pc.setRemoteDescription({sdp: msg.data, type: 'offer'})
pc.createAnswer().then(answer => {
pc.setLocalDescription(answer)
})
}
}
</script>
</html>
`)
}
var (
answerChan chan string
currentWebsocket atomic.Value
nilWs *websocket.Conn
)
func whipHandler(w http.ResponseWriter, req *http.Request) {
log.Println("Accepted WHIP Request")
offer, err := io.ReadAll(req.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
panic(err)
}
ws, ok := currentWebsocket.Load().(*websocket.Conn)
if !ok || ws == nil {
w.WriteHeader(http.StatusInternalServerError)
panic("WHIP Offer received with no WebSocket client connected")
}
if err := websocket.Message.Send(ws, string(offer)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
panic(err)
}
w.Header().Set("content-type", "application/sdp")
w.Header().Set("location", "/")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, <-answerChan)
}
func websocketHandler(ws *websocket.Conn) {
log.Println("WebSocket connected")
currentWebsocket.Store(ws)
for {
var answer string
if err := websocket.Message.Receive(ws, &answer); err != nil {
break
}
answerChan <- answer
}
log.Println("WebSocket disconnected")
currentWebsocket.Store(nilWs)
}
func main() {
currentWebsocket.Store(nilWs)
answerChan = make(chan string)
http.HandleFunc("/", htmlHandler)
http.HandleFunc("/whip", whipHandler)
http.Handle("/websocket", websocket.Handler(func(ws *websocket.Conn) {
websocketHandler(ws)
}))
httpAddr := os.Getenv("HTTP_ADDR")
if len(httpAddr) == 0 {
httpAddr = ":80"
}
log.Printf("Starting HTTP Server on %s", httpAddr)
panic(http.ListenAndServe(httpAddr, nil))
}