-
Notifications
You must be signed in to change notification settings - Fork 64
/
http.go
129 lines (125 loc) · 2.78 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
127
128
129
package main
import (
"log"
"net/http"
"sort"
"time"
"github.com/deepch/vdk/av"
"github.com/deepch/vdk/format/mp4f"
"github.com/gin-gonic/gin"
"golang.org/x/net/websocket"
)
func serveHTTP() {
router := gin.Default()
gin.SetMode(gin.DebugMode)
router.LoadHTMLGlob("web/templates/*")
router.GET("/", func(c *gin.Context) {
fi, all := Config.list()
sort.Strings(all)
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Config.Server.HTTPPort,
"suuid": fi,
"suuidMap": all,
"version": time.Now().String(),
})
})
router.GET("/player/:suuid", func(c *gin.Context) {
_, all := Config.list()
sort.Strings(all)
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Config.Server.HTTPPort,
"suuid": c.Param("suuid"),
"suuidMap": all,
"version": time.Now().String(),
})
})
router.GET("/ws/:suuid", func(c *gin.Context) {
handler := websocket.Handler(ws)
handler.ServeHTTP(c.Writer, c.Request)
})
router.StaticFS("/static", http.Dir("web/static"))
err := router.Run(Config.Server.HTTPPort)
if err != nil {
log.Fatalln(err)
}
}
func ws(ws *websocket.Conn) {
defer ws.Close()
suuid := ws.Request().FormValue("suuid")
log.Println("Request", suuid)
if !Config.ext(suuid) {
log.Println("Stream Not Found")
return
}
Config.RunIFNotRun(suuid)
ws.SetWriteDeadline(time.Now().Add(5 * time.Second))
cuuid, ch := Config.clAd(suuid)
defer Config.clDe(suuid, cuuid)
codecs := Config.coGe(suuid)
if codecs == nil {
log.Println("Codecs Error")
return
}
for i, codec := range codecs {
if codec.Type().IsAudio() && codec.Type() != av.AAC {
log.Println("Track", i, "Audio Codec Work Only AAC")
}
}
muxer := mp4f.NewMuxer(nil)
err := muxer.WriteHeader(codecs)
if err != nil {
log.Println("muxer.WriteHeader", err)
return
}
meta, init := muxer.GetInit(codecs)
err = websocket.Message.Send(ws, append([]byte{9}, meta...))
if err != nil {
log.Println("websocket.Message.Send", err)
return
}
err = websocket.Message.Send(ws, init)
if err != nil {
return
}
var start bool
go func() {
for {
var message string
err := websocket.Message.Receive(ws, &message)
if err != nil {
ws.Close()
return
}
}
}()
noVideo := time.NewTimer(10 * time.Second)
var timeLine = make(map[int8]time.Duration)
for {
select {
case <-noVideo.C:
log.Println("noVideo")
return
case pck := <-ch:
if pck.IsKeyFrame {
noVideo.Reset(10 * time.Second)
start = true
}
if !start {
continue
}
timeLine[pck.Idx] += pck.Duration
pck.Time = timeLine[pck.Idx]
ready, buf, _ := muxer.WritePacket(pck, false)
if ready {
err = ws.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
return
}
err := websocket.Message.Send(ws, buf)
if err != nil {
return
}
}
}
}
}