forked from Monibuca/plugin-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (192 loc) · 5.03 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
package record
import (
"encoding/json"
. "github.com/Monibuca/engine/v3"
. "github.com/Monibuca/utils/v3"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"sync"
)
var config struct {
Path string
AutoPublish bool
AutoRecord bool
}
type RecordingInfo struct {
ID string `json:"id"`
Subscribe *Subscriber `json:"-"` // 视频流
Filepath string `json:"filepath"` // 文件路径
Recording bool `json:"recording"` // 是否正在录制
}
var recordings sync.Map
type FlvFileInfo struct {
Path string
Size int64
Duration uint32
}
func init() {
InstallPlugin(&PluginConfig{
Name: "Record",
Config: &config,
Run: run,
HotConfig: map[string]func(interface{}){
"AutoPublish": func(v interface{}) {
config.AutoPublish = v.(bool)
},
"AutoRecord": func(v interface{}) {
config.AutoRecord = v.(bool)
},
},
})
}
func run() {
go AddHook(HOOK_SUBSCRIBE, onSubscribe)
go AddHook(HOOK_PUBLISH, onPublish)
os.MkdirAll(config.Path, 0755)
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/status", func(w http.ResponseWriter, r *http.Request) {
CORS(w, r)
var recordingInfo RecordingInfo
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
if stream, ok := recordings.Load(streamPath); ok {
if output, ok := stream.(*RecordingInfo); ok {
recordingInfo.Recording = output.Recording
recordingInfo.Filepath = output.Filepath
recordingInfo.ID = output.ID
}
}
}
data, _ := json.Marshal(recordingInfo)
w.Write(data)
})
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 {
if output, ok := stream.(*RecordingInfo); ok {
output.Subscribe.Close()
output.Recording = false
recordings.Store(filePath, output)
w.Write([]byte("success"))
} else {
w.Write([]byte("no right stream"))
}
} 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 onSubscribe(v interface{}) {
s := v.(*Subscriber)
if config.AutoPublish {
filePath := filepath.Join(config.Path, s.StreamPath+".flv")
if s.Publisher == nil && Exist(filePath) {
PublishFlvFile(s.StreamPath)
}
}
}
func onPublish(v interface{}) {
p := v.(*Stream)
if config.AutoRecord {
_ = 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
}
}