forked from Monibuca/plugin-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (144 loc) · 3.77 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
package record
import (
"encoding/json"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"sync"
. "github.com/Monibuca/engine/v2"
. "github.com/Monibuca/engine/v2/util"
)
var config = struct {
Path string
AutoPublish bool
}{}
var recordings = sync.Map{}
type FlvFileInfo struct {
Path string
Size int64
Duration uint32
}
func init() {
InstallPlugin(&PluginConfig{
Name: "Record",
Type: PLUGIN_SUBSCRIBER,
Config: &config,
Run: run,
})
}
func run() {
OnSubscribeHooks.AddHook(onSubscribe)
os.MkdirAll(config.Path, 0777)
http.HandleFunc("/record/flv/list", func(writer http.ResponseWriter, r *http.Request) {
if files, err := tree(config.Path, 0); err == nil {
var bytes []byte
if bytes, err = json.Marshal(files); err == nil {
writer.Write(bytes)
} else {
writer.Write([]byte("{\"err\":\"" + err.Error() + "\"}"))
}
} else {
writer.Write([]byte("{\"err\":\"" + err.Error() + "\"}"))
}
})
http.HandleFunc("/record/flv", func(writer http.ResponseWriter, r *http.Request) {
if streamPath := r.URL.Query().Get("streamPath"); streamPath != "" {
if err := SaveFlv(streamPath, r.URL.Query().Get("append") == "true"); err != nil {
writer.Write([]byte(err.Error()))
} else {
writer.Write([]byte("success"))
}
} else {
writer.Write([]byte("no streamPath"))
}
})
http.HandleFunc("/record/flv/stop", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
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("/record/flv/play", func(w http.ResponseWriter, r *http.Request) {
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("/record/flv/delete", func(w http.ResponseWriter, r *http.Request) {
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(s *Subscriber) {
filePath := filepath.Join(config.Path, s.StreamPath+".flv")
if s.Publisher == nil && Exist(filePath) {
go PublishFlvFile(s.StreamPath)
}
}
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
}
}