-
Notifications
You must be signed in to change notification settings - Fork 10
/
ws_handler.go
288 lines (272 loc) · 14.1 KB
/
ws_handler.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package videoserver
import (
"fmt"
"net/http"
"time"
"github.com/deepch/vdk/format/mp4f"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/rs/zerolog/log"
)
var (
keyFramesTimeout = 10 * time.Second
deadlineTimeout = 10 * time.Second
controlTimeout = 10 * time.Second
)
// wshandler is a websocket handler for user connection
func wshandler(wsUpgrader *websocket.Upgrader, w http.ResponseWriter, r *http.Request, streamsStorage *StreamsStorage, verboseLevel VerboseLevel) {
var streamID, clientID uuid.UUID
var mseExists, clientAdded bool
streamIDSTR := r.FormValue("stream_id")
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Msg("MSE Connected")
}
conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
errReason := "Can't call websocket upgrader"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
defer func() {
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Msg("Connection has been closed")
}
if mseExists && clientAdded {
streamsStorage.DeleteViewer(streamID, clientID)
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Client has been removed")
}
}
conn.Close()
}()
streamID, err = uuid.Parse(streamIDSTR)
if err != nil {
errReason := fmt.Sprintf("Not valid UUID: '%s'", streamIDSTR)
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
mseExists = streamsStorage.TypeExistsForStream(streamID, STREAM_TYPE_MSE)
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Bool("mse_exists", mseExists).Msg("Validate stream type")
}
if mseExists {
err = conn.SetWriteDeadline(time.Now().Add(deadlineTimeout))
if err != nil {
errReason := "Can't set deadline"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("event", EVENT_WS_PING).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
clientID, ch, err := streamsStorage.AddViewer(streamID)
if err != nil {
errReason := "Can't add client to the queue"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
clientAdded = true
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Client has been added")
}
codecData, err := streamsStorage.GetCodecsDataForStream(streamID)
if err != nil {
errReason := "Can't extract codec for stream"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Msg("Validate codecs")
}
if len(codecData) == 0 {
errReason := "No codec information"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
muxer := mp4f.NewMuxer(nil)
err = muxer.WriteHeader(codecData)
if err != nil {
errReason := "Can't write codec information to the header"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Msg("Write header to muxer")
}
meta, init := muxer.GetInit(codecData)
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Str("meta", meta).Any("init", init).Msg("Get meta information")
}
err = conn.WriteMessage(websocket.BinaryMessage, append([]byte{9}, meta...))
if err != nil {
errReason := "Can't write meta information"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Str("meta", meta).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Str("meta", meta).Any("init", init).Msg("Send meta information")
}
err = conn.WriteMessage(websocket.BinaryMessage, init)
if err != nil {
errReason := "Can't write initialization information"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Str("meta", meta).Any("init", init).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("remote_addr", r.RemoteAddr).Str("event", EVENT_WS_UPGRADER).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Str("meta", meta).Any("init", init).Msg("Send initialization message")
}
var start bool
quitCh := make(chan bool)
rxPingCh := make(chan bool)
go func(quit, ping chan bool) {
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Start loop in goroutine")
}
for {
msgType, data, err := conn.ReadMessage()
if err != nil {
quit <- true
errReason := "Can't read message"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Str("meta", meta).Any("init", init).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Int("message_type", msgType).Int("data_len", len(data)).Msg("Read message in a loop")
}
if msgType == websocket.TextMessage && len(data) > 0 && string(data) == "ping" {
select {
case ping <- true:
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Int("message_type", msgType).Int("data_len", len(data)).Msg("Message has been sent")
// message sent
default:
// message dropped
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Int("message_type", msgType).Int("data_len", len(data)).Msg("Message has been dropped")
}
}
}
}(quitCh, rxPingCh)
noKeyFrames := time.NewTimer(keyFramesTimeout)
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Start loop")
}
for {
select {
case <-noKeyFrames.C:
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("No keyframes has been met")
}
return
case <-quitCh:
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Quit")
}
return
case <-rxPingCh:
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("'Ping' has been recieved")
}
err := conn.WriteMessage(websocket.TextMessage, []byte("pong"))
if err != nil {
errReason := "Can't write PONG message"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("event", EVENT_WS_PING).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("codecs", codecData).Str("meta", meta).Any("init", init).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
case pck := <-ch:
if verboseLevel > VERBOSE_ADD {
log.Info().Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Packet has been recieved from stream source")
}
if pck.IsKeyFrame {
if verboseLevel > VERBOSE_SIMPLE {
log.Info().Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Packet is a keyframe")
}
noKeyFrames.Reset(keyFramesTimeout)
start = true
}
if !start {
if verboseLevel > VERBOSE_ADD {
log.Info().Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Msg("Stream has not been started")
}
continue
}
ready, buf, err := muxer.WritePacket(pck, false)
if err != nil {
errReason := "Can't write packet to the muxer"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("event", EVENT_WS_PING).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("packet_len", len(pck.Data)).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
if verboseLevel > VERBOSE_ADD {
log.Info().Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Bool("ready", ready).Int("buf_len", len(buf)).Msg("Write packet to the muxer")
}
if ready {
if verboseLevel > VERBOSE_ADD {
log.Info().Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Bool("ready", ready).Int("buf_len", len(buf)).Msg("Muxer is ready to write another packet")
}
err = conn.SetWriteDeadline(time.Now().Add(deadlineTimeout))
if err != nil {
errReason := "Can't set new deadline"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("event", EVENT_WS_PING).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("packet_len", len(pck.Data)).Bool("ready", ready).Int("buf_len", len(buf)).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
err := conn.WriteMessage(websocket.BinaryMessage, buf)
if err != nil {
errReason := "Can't write buffered message"
if verboseLevel > VERBOSE_NONE {
log.Error().Err(err).Str("scope", SCOPE_WS_HANDLER).Str("event", EVENT_WS_UPGRADER).Str("event", EVENT_WS_PING).Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Any("packet_len", len(pck.Data)).Bool("ready", ready).Int("buf_len", len(buf)).Msg(errReason)
}
closeWSwithError(conn, 1011, errReason)
return
}
if verboseLevel > VERBOSE_ADD {
log.Info().Str("remote_addr", r.RemoteAddr).Str("stream_id", streamIDSTR).Str("client_id", clientID.String()).Bool("ready", ready).Int("buf_len", len(buf)).Msg("Write buffer to the client")
}
}
}
}
}
}
func prepareError(code int16, message string) []byte {
buf := make([]byte, 0, 2+len(message))
h, l := uint8(code>>8), uint8(code&0xff)
buf = append(buf, h, l)
buf = append(buf, []byte(message)...)
return buf
}
func closeWSwithError(conn *websocket.Conn, code int16, message string) {
conn.WriteControl(8, prepareError(code, message), time.Now().Add(controlTimeout))
}