-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
172 lines (162 loc) · 4.27 KB
/
main.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
package record
import (
_ "embed"
"errors"
"io"
"net"
"sync"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/config"
"m7s.live/engine/v4/util"
)
type RecordConfig struct {
config.Subscribe
config.HTTP
Flv Record `desc:"flv录制配置"`
Mp4 Record `desc:"mp4录制配置"`
Fmp4 Record `desc:"fmp4录制配置"`
Hls Record `desc:"hls录制配置"`
Raw Record `desc:"视频裸流录制配置"`
RawAudio Record `desc:"音频裸流录制配置"`
recordings sync.Map
beforeDuration int `desc:"事件前缓存时长"`
afterDuration int `desc:"事件后缓存时长"`
MysqlDSN string `desc:"mysql数据库连接字符串"`
ExceptionPostUrl string `desc:"第三方异常上报地址"`
SqliteDbPath string `desc:"sqlite数据库路径"`
DiskMaxPercent float64 `desc:"硬盘使用百分之上限值,超过后报警"`
LocalIp string `desc:"本机IP"`
}
//go:embed default.yaml
var defaultYaml DefaultYaml
var ErrRecordExist = errors.New("recorder exist")
var RecordPluginConfig = &RecordConfig{
Flv: Record{
Path: "record/flv",
Ext: ".flv",
GetDurationFn: getFLVDuration,
},
Fmp4: Record{
Path: "record/fmp4",
Ext: ".mp4",
},
Mp4: Record{
Path: "record/mp4",
Ext: ".mp4",
},
Hls: Record{
Path: "record/hls",
Ext: ".m3u8",
},
Raw: Record{
Path: "record/raw",
Ext: ".", // 默认h264扩展名为.h264,h265扩展名为.h265
},
RawAudio: Record{
Path: "record/raw",
Ext: ".", // 默认aac扩展名为.aac,pcma扩展名为.pcma,pcmu扩展名为.pcmu
},
beforeDuration: 30,
afterDuration: 30,
MysqlDSN: "",
ExceptionPostUrl: "http://www.163.com",
SqliteDbPath: "./sqlite.db",
DiskMaxPercent: 80.00,
LocalIp: getLocalIP(),
}
var plugin = InstallPlugin(RecordPluginConfig, defaultYaml)
var exceptionChannel = make(chan *Exception)
func (conf *RecordConfig) OnEvent(event any) {
switch v := event.(type) {
case FirstConfig, config.Config:
//if conf.MysqlDSN == "" {
// plugin.Error("mysqlDSN 数据库连接配置为空,无法运行,请在config.yaml里配置")
//}
go func() { //处理所有异常,录像中断异常、录像读取异常、录像导出文件中断、磁盘容量低于阈值异常、磁盘异常
for exception := range exceptionChannel {
SendToThirdPartyAPI(exception)
}
}()
if conf.MysqlDSN == "" {
plugin.Info("sqliteDb filepath is" + conf.SqliteDbPath)
initSqliteDB(conf.SqliteDbPath)
} else {
plugin.Info("mysqlDSN is" + conf.MysqlDSN)
initMysqlDB(conf.MysqlDSN)
}
conf.Flv.Init()
conf.Mp4.Init()
conf.Fmp4.Init()
conf.Hls.Init()
conf.Raw.Init()
conf.RawAudio.Init()
case SEpublish:
streamPath := v.Target.Path
if conf.Flv.NeedRecord(streamPath) {
go NewFLVRecorder().Start(streamPath)
}
if conf.Mp4.NeedRecord(streamPath) {
go NewMP4Recorder().Start(streamPath)
}
if conf.Fmp4.NeedRecord(streamPath) {
go NewFMP4Recorder().Start(streamPath)
}
if conf.Hls.NeedRecord(streamPath) {
go NewHLSRecorder().Start(streamPath)
}
if conf.Raw.NeedRecord(streamPath) {
go NewRawRecorder().Start(streamPath)
}
if conf.RawAudio.NeedRecord(streamPath) {
go NewRawAudioRecorder().Start(streamPath)
}
}
}
func (conf *RecordConfig) getRecorderConfigByType(t string) (recorder *Record) {
switch t {
case "flv":
recorder = &conf.Flv
case "mp4":
recorder = &conf.Mp4
case "fmp4":
recorder = &conf.Fmp4
case "hls":
recorder = &conf.Hls
case "raw":
recorder = &conf.Raw
case "raw_audio":
recorder = &conf.RawAudio
}
return
}
func getFLVDuration(file io.ReadSeeker) uint32 {
_, err := file.Seek(-4, io.SeekEnd)
if err == nil {
var tagSize uint32
if tagSize, err = util.ReadByteToUint32(file, true); err == nil {
_, err = file.Seek(-int64(tagSize)-4, io.SeekEnd)
if err == nil {
_, timestamp, _, err := codec.ReadFLVTag(file)
if err == nil {
return timestamp
}
}
}
}
return 0
}
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
}
}
return ""
}