-
Notifications
You must be signed in to change notification settings - Fork 318
/
apiHTTPSaveMP4.go
129 lines (120 loc) · 3.25 KB
/
apiHTTPSaveMP4.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 (
"fmt"
"github.com/deepch/vdk/format/mp4"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"os"
"time"
)
// HTTPAPIServerStreamSaveToMP4 func
func HTTPAPIServerStreamSaveToMP4(c *gin.Context) {
var err error
requestLogger := log.WithFields(logrus.Fields{
"module": "http_save_mp4",
"stream": c.Param("uuid"),
"channel": c.Param("channel"),
"func": "HTTPAPIServerStreamSaveToMP4",
})
defer func() {
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Close",
}).Errorln(err)
}
}()
if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) {
requestLogger.WithFields(logrus.Fields{
"call": "StreamChannelExist",
}).Errorln(ErrorStreamNotFound.Error())
return
}
if !RemoteAuthorization("save", c.Param("uuid"), c.Param("channel"), c.Query("token"), c.ClientIP()) {
requestLogger.WithFields(logrus.Fields{
"call": "RemoteAuthorization",
}).Errorln(ErrorStreamUnauthorized.Error())
return
}
c.Writer.Write([]byte("await save started"))
go func() {
Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel"))
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
}
err = os.MkdirAll(fmt.Sprintf("save/%s/%s/", c.Param("uuid"), c.Param("channel")), 0755)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "MkdirAll",
}).Errorln(err.Error())
}
f, err := os.Create(fmt.Sprintf("save/%s/%s/%s.mp4", c.Param("uuid"), c.Param("channel"), time.Now().String()))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "Create",
}).Errorln(err.Error())
}
defer f.Close()
muxer := mp4.NewMuxer(f)
err = muxer.WriteHeader(codecs)
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "WriteHeader",
}).Errorln(err.Error())
return
}
defer muxer.WriteTrailer()
var videoStart bool
controlExit := make(chan bool, 10)
dur, err := time.ParseDuration(c.Param("duration"))
if err != nil {
requestLogger.WithFields(logrus.Fields{
"call": "ParseDuration",
}).Errorln(err.Error())
}
saveLimit := time.NewTimer(dur)
noVideo := time.NewTimer(10 * time.Second)
defer log.Println("client exit")
for {
select {
case <-controlExit:
requestLogger.WithFields(logrus.Fields{
"call": "controlExit",
}).Errorln("Client Reader Exit")
return
case <-saveLimit.C:
requestLogger.WithFields(logrus.Fields{
"call": "saveLimit",
}).Errorln("Saved Limit End")
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
}
if err = muxer.WritePacket(*pck); err != nil {
return
}
}
}
}()
}