-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
142 lines (121 loc) · 3.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
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/coder-with-a-bushido/neralai/internal/hls"
"github.com/coder-with-a-bushido/neralai/internal/whip"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
)
func main() {
whip.Init()
hls.Init()
r := chi.NewRouter()
// Adds CORS headers
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"*"},
}))
// WHIP endpoint - Start new stream
r.Post("/stream", startNewStream)
// Reserved WHIP endpoint methods for future
// ref: `draft-ietf-wish-whip-01`
r.Get("/stream", r.MethodNotAllowedHandler())
r.Head("/stream", r.MethodNotAllowedHandler())
r.Put("/stream", r.MethodNotAllowedHandler())
// WHIP resources endpoint - operations on existing stream
r.Delete("/stream/{resourceId}", stopStream)
// TODO: WHIP resources endpoint - Trickle ICE
r.Patch("/stream/{resourceId}", r.MethodNotAllowedHandler())
// Reserved WHIP resources endpoint methods for future
// ref: `draft-ietf-wish-whip-01`
r.Get("/stream/{resourceId}", r.MethodNotAllowedHandler())
r.Head("/stream/{resourceId}", r.MethodNotAllowedHandler())
r.Post("/stream/{resourceId}", r.MethodNotAllowedHandler())
r.Put("/stream/{resourceId}", r.MethodNotAllowedHandler())
// Serve HLS files for resources from the output directory
r.Get("/stream/{resourceId}/hls/*", serveHLSFiles)
log.Println("Starting server at port 8080")
log.Fatal((&http.Server{
Handler: r,
Addr: ":8080",
}).ListenAndServe())
defer cleanup()
// Cleanup on exit
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
go func() {
<-sigs
cleanup()
os.Exit(0)
}()
}
func cleanup() {
whip.CleanUp()
hls.CleanUp()
}
func startNewStream(w http.ResponseWriter, r *http.Request) {
offerSDP, err := io.ReadAll(r.Body)
if err != nil {
writeBadRequest(w, err.Error())
return
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
disconnect := make(chan struct{})
answerSDP, resourceID, err := whip.NewConnection(ctx, string(offerSDP), disconnect)
if err != nil {
writeInternalServerError(w, err.Error())
cancel()
return
}
w.Header().Set("Location", string("http://"+r.Host+"/stream/"+resourceID))
w.Header().Set("Content-Type", "application/sdp")
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, answerSDP)
hls.NewStreamFromWHIPResource(ctx, resourceID)
go func() {
<-disconnect
cancel()
}()
}
func stopStream(w http.ResponseWriter, r *http.Request) {
resourceId := chi.URLParam(r, "resourceId")
resource := whip.GetResource(resourceId)
if resource == nil {
writeBadRequest(w, "Invalid Resource ID")
return
}
resource.Disconnect <- struct{}{}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w)
}
func serveHLSFiles(w http.ResponseWriter, r *http.Request) {
resourceId := chi.URLParam(r, "resourceId")
resource := whip.GetResource(resourceId)
if resource == nil {
writeBadRequest(w, "Invalid Resource ID")
return
}
filePath := "output" + strings.TrimPrefix(r.URL.Path, "/stream")
http.ServeFile(w, r, filePath)
}
func writeBadRequest(w http.ResponseWriter, errorStr string) {
writeHTTPError(w, errorStr, http.StatusBadRequest)
}
func writeInternalServerError(w http.ResponseWriter, errorStr string) {
writeHTTPError(w, errorStr, http.StatusInternalServerError)
}
func writeHTTPError(w http.ResponseWriter, err string, code int) {
log.Println(err)
http.Error(w, err, code)
}