forked from deepch/RTSPtoWSMP4f
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
126 lines (117 loc) · 2.77 KB
/
http.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
package main
import (
"log"
"net/http"
"sort"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/webver/vdk/format/mp4f"
)
func serveHTTP(config *ConfigST) {
router := gin.Default()
ginCfg := cors.DefaultConfig()
ginCfg.AllowAllOrigins = true
router.Use(cors.New(ginCfg))
router.GET("/list", func(c *gin.Context) {
_, all := config.List()
sort.Strings(all)
c.JSON(200, all)
})
router.GET("/status", func(c *gin.Context) {
c.JSON(200, config)
})
router.GET("/ws/:suuid", func(c *gin.Context) {
wshandler(config, c.Writer, c.Request)
})
router.GET("/hls/:file", func(c *gin.Context) {
file := c.Param("file")
c.Header("Cache-Control", "no-cache")
c.FileFromFS(file, http.Dir("./hls"))
})
httpServer := &http.Server{
Addr: config.Server.HTTPPort,
Handler: router,
ReadTimeout: time.Duration(config.Server.HTTPTimeout) * time.Second,
WriteTimeout: time.Duration(config.Server.HTTPTimeout) * time.Second,
}
err := httpServer.ListenAndServe()
//err := router.Run(Config.Server.HTTPPort)
if err != nil {
log.Fatalln(err)
}
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func wshandler(config *ConfigST, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Failed to set websocket upgrade: %+v\n", err)
return
}
defer func() {
err = conn.Close()
if err != nil {
log.Print(err)
}
log.Println("WebSocket connection terminated.")
}()
suuid := r.FormValue("suuid")
log.Println("Request", suuid)
if config.Ext(suuid) {
conn.SetWriteDeadline(time.Now().Add(50 * time.Second))
cuuid, ch := config.ClientAdd(suuid)
defer config.ClientDelete(suuid, cuuid)
codecs := config.CodecGet(suuid)
if codecs == nil {
log.Println("No Codec Info")
return
}
muxer := mp4f.NewMuxer(nil)
muxer.WriteHeader(codecs)
meta, init := muxer.GetInit(codecs)
err := conn.WriteMessage(websocket.BinaryMessage, append([]byte{9}, meta...))
if err != nil {
return
}
err = conn.WriteMessage(websocket.BinaryMessage, init)
if err != nil {
return
}
var start bool
quitCh := make(chan bool)
go func(q chan bool) {
//Читаем что бы отрабаытваел onClose
_, _, err := conn.ReadMessage()
if err != nil {
q <- true
return
}
}(quitCh)
for {
select {
case <-quitCh:
return
case pck := <-ch:
if pck.IsKeyFrame {
start = true
}
if !start {
continue
}
ready, buf, _ := muxer.WritePacket(pck, false)
if ready {
conn.SetWriteDeadline(time.Now().Add(100 * time.Second))
err := conn.WriteMessage(websocket.BinaryMessage, buf)
if err != nil {
return
}
}
}
}
}
}