forked from Monibuca/plugin-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (163 loc) · 4.06 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
package record
import (
"encoding/json"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"sync"
. "github.com/Monibuca/engine/v3"
. "github.com/Monibuca/utils/v3"
)
var config struct {
Path string
AutoRecord bool
}
var recordings sync.Map
type FlvFileInfo struct {
Path string
Size int64
Duration uint32
}
type FileWr interface {
io.Reader
io.Writer
io.Seeker
io.Closer
}
var ExtraConfig struct {
CreateFileFn func(filename string) (FileWr,error)
AutoRecordFilter func(stream string) bool
}
func init() {
pc := PluginConfig{
Name: "Record",
Config: &config,
HotConfig: map[string]func(interface{}){
"AutoRecord": func(v interface{}) {
config.AutoRecord = v.(bool)
},
},
}
pc.Install(run)
}
func run() {
go AddHook(HOOK_PUBLISH, onPublish)
os.MkdirAll(config.Path, 0755)
http.HandleFunc("/vod/", VodHandler)
http.HandleFunc("/api/record/flv/list", func(w http.ResponseWriter, r *http.Request) {
CORS(w, r)
if files, err := tree(config.Path, 0); err == nil {
var bytes []byte
if bytes, err = json.Marshal(files); err == nil {
w.Write(bytes)
} else {
w.Write([]byte("{\"err\":\"" + err.Error() + "\"}"))
}
} else {
w.Write([]byte("{\"err\":\"" + err.Error() + "\"}"))
}
})
http.HandleFunc("/api/record/flv", func(w http.ResponseWriter, r *http.Request) {
CORS(w, r)
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
if err := SaveFlv(streamPath, r.URL.Query().Get("append") == "true"); err != nil {
w.Write([]byte(err.Error()))
} else {
w.Write([]byte("success"))
}
} else {
w.Write([]byte("no streamPath"))
}
})
http.HandleFunc("/api/record/flv/stop", func(w http.ResponseWriter, r *http.Request) {
CORS(w, r)
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
filePath := filepath.Join(config.Path, streamPath+".flv")
if stream, ok := recordings.Load(filePath); ok {
output := stream.(*Subscriber)
output.Close()
w.Write([]byte("success"))
} else {
w.Write([]byte("no query stream"))
}
} else {
w.Write([]byte("no such stream"))
}
})
http.HandleFunc("/api/record/flv/play", func(w http.ResponseWriter, r *http.Request) {
CORS(w, r)
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
if err := PublishFlvFile(streamPath); err != nil {
w.Write([]byte(err.Error()))
} else {
w.Write([]byte("success"))
}
} else {
w.Write([]byte("no streamPath"))
}
})
http.HandleFunc("/api/record/flv/delete", func(w http.ResponseWriter, r *http.Request) {
CORS(w, r)
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
filePath := filepath.Join(config.Path, streamPath+".flv")
if Exist(filePath) {
if err := os.Remove(filePath); err != nil {
w.Write([]byte(err.Error()))
} else {
w.Write([]byte("success"))
}
} else {
w.Write([]byte("no such file"))
}
} else {
w.Write([]byte("no streamPath"))
}
})
}
func onPublish(p *Stream) {
if config.AutoRecord || (ExtraConfig.AutoRecordFilter != nil && ExtraConfig.AutoRecordFilter(p.StreamPath)) {
SaveFlv(p.StreamPath, false)
}
}
func tree(dstPath string, level int) (files []*FlvFileInfo, err error) {
var dstF *os.File
dstF, err = os.Open(dstPath)
if err != nil {
return
}
defer dstF.Close()
fileInfo, err := dstF.Stat()
if err != nil {
return
}
if !fileInfo.IsDir() { //如果dstF是文件
if path.Ext(fileInfo.Name()) == ".flv" {
p := strings.TrimPrefix(dstPath, config.Path)
p = strings.ReplaceAll(p, "\\", "/")
files = append(files, &FlvFileInfo{
Path: strings.TrimPrefix(p, "/"),
Size: fileInfo.Size(),
Duration: getDuration(dstF),
})
}
return
} else { //如果dstF是文件夹
var dir []os.FileInfo
dir, err = dstF.Readdir(0) //获取文件夹下各个文件或文件夹的fileInfo
if err != nil {
return
}
for _, fileInfo = range dir {
var _files []*FlvFileInfo
_files, err = tree(filepath.Join(dstPath, fileInfo.Name()), level+1)
if err != nil {
return
}
files = append(files, _files...)
}
return
}
}