-
Notifications
You must be signed in to change notification settings - Fork 315
/
apiHTTPMSE.go
189 lines (178 loc) · 4.53 KB
/
apiHTTPMSE.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"time"
"github.com/gobwas/ws/wsutil"
"github.com/gobwas/ws"
"github.com/gin-gonic/gin"
"github.com/deepch/vdk/format/mp4f"
"github.com/sirupsen/logrus"
)
//HTTPAPIServerStreamMSE func
func HTTPAPIServerStreamMSE(c *gin.Context) {
conn, _, _, err := ws.UpgradeHTTP(c.Request, c.Writer)
if err != nil {
return
}
requestLogger := log.WithFields(logrus.Fields{
"module": "http_mse",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamMSE",
})
defer func() {
err = conn.Close()
requestLogger.WithFields(logrus.Fields{
"call": "Close",
}).Errorln(err)
log.Println("Client Full Exit")
}()
if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) {
requestLogger.WithFields(logrus.Fields{
"call": "StreamChannelExist",
}).Errorln(ErrorStreamNotFound.Error())
return
}
if !RemoteAuthorization("WS", c.Param("uuid"), c.Param("channel"), c.Query("token"), c.ClientIP()) {
requestLogger.WithFields(logrus.Fields{
"call": "RemoteAuthorization",
}).Errorln(ErrorStreamUnauthorized.Error())
return
}
Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel"))
err = conn.SetWriteDeadline(time.Now().Add(5 * time.Second))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "SetWriteDeadline",
}).Errorln(err.Error())
return
}
cid, ch, _, err := Storage.ClientAdd(c.Param("uuid"), c.Param("channel"), MSE)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "ClientAdd",
}).Errorln(err.Error())
return
}
defer Storage.ClientDelete(c.Param("uuid"), cid, c.Param("channel"))
codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel"))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "StreamCodecs",
}).Errorln(err.Error())
return
}
muxerMSE := mp4f.NewMuxer(nil)
err = muxerMSE.WriteHeader(codecs)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "WriteHeader",
}).Errorln(err.Error())
return
}
meta, init := muxerMSE.GetInit(codecs)
err = wsutil.WriteServerMessage(conn, ws.OpBinary, append([]byte{9}, meta...))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Send",
}).Errorln(err.Error())
return
}
err = wsutil.WriteServerMessage(conn, ws.OpBinary, init)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Send",
}).Errorln(err.Error())
return
}
var videoStart bool
controlExit := make(chan bool, 10)
noClient := time.NewTimer(10 * time.Second)
go func() {
defer func() {
controlExit <- true
}()
for {
header, _, err := wsutil.NextReader(conn, ws.StateServerSide)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Receive",
}).Errorln(err.Error())
return
}
switch header.OpCode {
case ws.OpPong:
noClient.Reset(10 * time.Second)
case ws.OpClose:
return
}
}
}()
noVideo := time.NewTimer(10 * time.Second)
pingTicker := time.NewTicker(500 * time.Millisecond)
defer pingTicker.Stop()
defer log.Println("client exit")
for {
select {
case <-pingTicker.C:
err = conn.SetWriteDeadline(time.Now().Add(3 * time.Second))
if err != nil {
return
}
buf, err := ws.CompileFrame(ws.NewPingFrame(nil))
if err != nil {
return
}
_, err = conn.Write(buf)
if err != nil {
return
}
case <-controlExit:
requestLogger.WithFields(logrus.Fields{
"call": "controlExit",
}).Errorln("Client Reader Exit")
return
case <-noClient.C:
requestLogger.WithFields(logrus.Fields{
"call": "ErrorClientOffline",
}).Errorln("Client OffLine Exit")
return
case <-noVideo.C:
requestLogger.WithFields(logrus.Fields{
"call": "ErrorStreamNoVideo",
}).Errorln(ErrorStreamNoVideo.Error())
return
case pck := <-ch:
if pck.IsKeyFrame {
noVideo.Reset(10 * time.Second)
videoStart = true
}
if !videoStart {
continue
}
ready, buf, err := muxerMSE.WritePacket(*pck, false)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "WritePacket",
}).Errorln(err.Error())
return
}
if ready {
err := conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "SetWriteDeadline",
}).Errorln(err.Error())
return
}
//err = websocket.Message.Send(ws, buf)
err = wsutil.WriteServerMessage(conn, ws.OpBinary, buf)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Send",
}).Errorln(err.Error())
return
}
}
}
}
}