forked from deepch/RTSPtoWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiHTTPMSE.go
150 lines (144 loc) · 3.85 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
package main
import (
"time"
"github.com/deepch/vdk/format/mp4f"
"github.com/sirupsen/logrus"
"golang.org/x/net/websocket"
)
//HTTPAPIServerStreamMSE func
func HTTPAPIServerStreamMSE(ws *websocket.Conn) {
requestLogger := log.WithFields(logrus.Fields{
"module": "http_mse",
"stream": ws.Request().FormValue("uuid"),
"channel": ws.Request().FormValue("channel"),
"func": "HTTPAPIServerStreamMSE",
})
defer func() {
err := ws.Close()
requestLogger.WithFields(logrus.Fields{
"call": "Close",
}).Errorln(err)
log.Println("Client Full Exit")
}()
if !Storage.StreamChannelExist(ws.Request().FormValue("uuid"), ws.Request().FormValue("channel")) {
requestLogger.WithFields(logrus.Fields{
"call": "StreamChannelExist",
}).Errorln(ErrorStreamNotFound.Error())
return
}
if !RemoteAuthorization("WS", ws.Request().FormValue("uuid"), ws.Request().FormValue("channel"), ws.Request().FormValue("token"), ws.Request().RemoteAddr) {
requestLogger.WithFields(logrus.Fields{
"call": "RemoteAuthorization",
}).Errorln(ErrorStreamNotFound.Error())
return
}
Storage.StreamChannelRun(ws.Request().FormValue("uuid"), ws.Request().FormValue("channel"))
err := ws.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(ws.Request().FormValue("uuid"), ws.Request().FormValue("channel"), MSE)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "ClientAdd",
}).Errorln(err.Error())
return
}
defer Storage.ClientDelete(ws.Request().FormValue("uuid"), cid, ws.Request().FormValue("channel"))
codecs, err := Storage.StreamChannelCodecs(ws.Request().FormValue("uuid"), ws.Request().FormValue("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 = websocket.Message.Send(ws, append([]byte{9}, meta...))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Send",
}).Errorln(err.Error())
return
}
err = websocket.Message.Send(ws, init)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Send",
}).Errorln(err.Error())
return
}
var videoStart bool
controlExit := make(chan bool, 10)
go func() {
defer func() {
controlExit <- true
}()
for {
var message string
err := websocket.Message.Receive(ws, &message)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Receive",
}).Errorln(err.Error())
return
}
}
}()
noVideo := time.NewTimer(10 * time.Second)
for {
select {
case <-controlExit:
requestLogger.WithFields(logrus.Fields{
"call": "controlExit",
}).Errorln("Client Reader 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 := ws.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)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Send",
}).Errorln(err.Error())
return
}
}
}
}
}