-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
219 lines (201 loc) · 6.54 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
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
package monitor
import (
_ "embed"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"gopkg.in/yaml.v3"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/common"
"m7s.live/engine/v4/config"
// badger "github.com/dgraph-io/badger/v3"
)
type Index struct {
CreateTime int64 `yaml:"time"`
StreamPath string `yaml:"path"`
}
type DelaySnap struct {
Time int64 `yaml:"time"`
Delay map[string]uint32 `yaml:"delay"`
}
type TrackSnap struct {
Time int64 `yaml:"time"`
BPS int `yaml:"bps"`
FPS int `yaml:"fps"`
Drops int `yaml:"drops"`
RBSize int `yaml:"rb"`
}
type MonitorSuber struct {
Subscriber
subIndex int
subfp map[ISubscriber]*os.File
fp *os.File
tracks map[common.Track]*os.File
dir string
}
func (r *MonitorSuber) Start(streamPath string) {
if MonitorPlugin.Subscribe(streamPath, r) == nil {
r.SubPulse()
}
}
func (r *MonitorSuber) OnEvent(event any) {
switch v := event.(type) {
case PulseEvent:
r.Stream.Tracks.Range(func(k string, t common.Track) {
appendYaml(r.tracks[t], TrackSnap{
Time: v.Time.UnixMilli(),
BPS: t.GetBPS(),
FPS: t.GetFPS(),
Drops: t.GetDrops(),
RBSize: t.GetRBSize(),
})
})
r.Stream.Subscribers.RangeAll(func(sub ISubscriber) {
var snap = DelaySnap{
Time: v.Time.UnixMilli(),
Delay: map[string]uint32{},
}
suber := sub.GetSubscriber()
if suber.AudioReader != nil {
snap.Delay[suber.AudioReader.Track.GetName()] = suber.AudioReader.Delay
}
if suber.VideoReader != nil {
snap.Delay[suber.VideoReader.Track.GetName()] = suber.VideoReader.Delay
}
appendYaml(r.subfp[sub], snap)
})
case common.Track:
r.tracks[v] = conf.OpenYaml(r.dir, "track", v.GetName())
// appendYaml(r.fp, map[string]any{"time": time.Now().UnixMilli(), "event": "publish", "type": v.Target.GetType()})
}
}
//go:embed default.yaml
var defaultYaml DefaultYaml
type MonitorConfig struct {
config.HTTP
config.Subscribe
Path string `default:"monitor" desc:"存储路径"` // 存储路径
indexFP *os.File
fileServer http.Handler
today string
}
var conf MonitorConfig
var MonitorPlugin = InstallPlugin(&conf, defaultYaml)
var streams map[*Stream]*MonitorSuber
func (conf *MonitorConfig) OnEvent(event any) {
switch v := event.(type) {
case FirstConfig:
streams = make(map[*Stream]*MonitorSuber)
conf.fileServer = http.FileServer(http.Dir(conf.Path))
case SEcreate:
// conf.Report("create", &ReportCreateStream{v.Target.Path, time.Now().Unix()})
var suber MonitorSuber
streams[v.Target] = &suber
if today := time.Now().Format("2006-01-02"); conf.indexFP == nil {
conf.indexFP = conf.OpenYaml(today)
conf.today = today
} else if conf.today != today {
conf.indexFP.Close()
conf.indexFP = conf.OpenYaml(today)
conf.today = today
}
appendYaml(conf.indexFP, Index{CreateTime: v.Time.UnixMilli(), StreamPath: v.Target.Path})
suber.dir = filepath.Join(v.Target.Path, time.Now().Format("2006-01-02T15:04:05"))
suber.fp = conf.OpenYaml(suber.dir, "stream")
suber.subfp = make(map[ISubscriber]*os.File)
suber.tracks = make(map[common.Track]*os.File)
go suber.Start(v.Target.Path)
case SEpublish:
appendYaml(streams[v.Target].fp, map[string]any{"time": v.Time.UnixMilli(), "event": "publish", "type": v.Target.GetType()})
case SErepublish:
appendYaml(streams[v.Target].fp, map[string]any{"time": v.Time.UnixMilli(), "event": "republish", "type": v.Target.GetType()})
case SEclose:
monitor := streams[v.Target]
appendYaml(monitor.fp, map[string]any{"time": v.Time.UnixMilli(), "event": "close", "action": v.Action})
delete(streams, v.Target)
case SEwaitClose:
monitor := streams[v.Target]
appendYaml(monitor.fp, map[string]any{"time": v.Time.UnixMilli(), "event": "waitclose", "action": v.Action})
case SEwaitPublish:
monitor := streams[v.Target]
appendYaml(monitor.fp, map[string]any{"time": v.Time.UnixMilli(), "event": "waitpublish", "action": v.Action})
case ISubscriber:
suber := v.GetSubscriber()
monitor := streams[suber.Stream]
monitor.subIndex++
monitor.subfp[v] = conf.OpenYaml(monitor.dir, "subscriber", fmt.Sprintf("%d", monitor.subIndex))
appendYaml(monitor.fp, map[string]any{"time": time.Now().UnixMilli(), "event": "subscribe", "type": suber.Type, "id": suber.ID})
case UnsubscribeEvent:
s := v.Target.GetSubscriber().Stream
monitor := streams[s]
monitor.subfp[v.Target].Close()
appendYaml(monitor.fp, map[string]any{"time": v.Time.UnixMilli(), "event": "unsubscribe"})
}
}
func (conf *MonitorConfig) API_list_stream(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
var streams []Index
timeRange := query.Get("time")
streamPath := query.Get("streamPath")
if timeRange == "" {
conf.ReadYaml(&streams, time.Now().Format("2006-01-02"))
} else if tt := strings.Split(timeRange, "-"); len(tt) == 2 {
t, _ := strconv.ParseInt(tt[0], 10, 64)
start := time.UnixMilli(t)
t, _ = strconv.ParseInt(tt[1], 10, 64)
end := time.UnixMilli(t)
for ; !start.After(end); start = start.AddDate(0, 0, 1) {
var s []Index
conf.ReadYaml(&s, start.Format("2006-01-02"))
if streamPath != "" {
for _, v := range s {
if v.StreamPath == streamPath {
streams = append(streams, v)
}
}
} else {
streams = append(streams, s...)
}
}
}
if err := yaml.NewEncoder(w).Encode(streams); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (conf *MonitorConfig) API_list_track(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
streamPath := query.Get("streamPath")
var tracks []string
filepath.Walk(filepath.Join(conf.Path, streamPath, "track"), func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
trackName, _, _ := strings.Cut(info.Name(), ".")
tracks = append(tracks, trackName)
return nil
})
if err := yaml.NewEncoder(w).Encode(tracks); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (conf *MonitorConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conf.fileServer.ServeHTTP(w, r)
}
func appendYaml[T any](fp *os.File, data T) {
out, _ := yaml.Marshal([]T{data})
fp.Write(out)
}
func (conf *MonitorConfig) OpenYaml(path ...string) (f *os.File) {
fp := filepath.Join(append([]string{conf.Path}, path...)...) + ".yaml"
os.MkdirAll(filepath.Dir(fp), 0766)
f, _ = os.OpenFile(fp, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
return
}
func (conf *MonitorConfig) ReadYaml(data any, path ...string) {
f, _ := os.Open(filepath.Join(append([]string{conf.Path}, path...)...) + ".yaml")
yaml.NewDecoder(f).Decode(data)
}