forked from langhuihui/monibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuller.go
298 lines (271 loc) · 6.75 KB
/
puller.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package m7s
import (
"io"
"math"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
"m7s.live/v5/pkg"
"m7s.live/v5/pkg/config"
"m7s.live/v5/pkg/task"
"m7s.live/v5/pkg/util"
)
type (
Connection struct {
task.Job
Plugin *Plugin
StreamPath string // 对应本地流
Args url.Values
RemoteURL string // 远程服务器地址(用于推拉)
HTTPClient *http.Client
Header http.Header
}
IPuller interface {
task.ITask
GetPullJob() *PullJob
}
Puller = func(config.Pull) IPuller
PullJob struct {
Connection
Publisher *Publisher
PublishConfig config.Publish
puller IPuller
conf *config.Pull
}
HTTPFilePuller struct {
task.Task
PullJob PullJob
io.ReadCloser
}
RecordFilePuller struct {
task.Task
PullJob PullJob
PullStartTime, PullEndTime time.Time
Streams []RecordStream
File *os.File
MaxTS int64
seekChan chan time.Time
Type string
Loop int
}
wsReadCloser struct {
ws *websocket.Conn
}
)
func (conn *Connection) Init(plugin *Plugin, streamPath string, href string, proxyConf string, header http.Header) {
conn.RemoteURL = href
conn.StreamPath = streamPath
conn.Plugin = plugin
conn.Header = header
conn.HTTPClient = http.DefaultClient
if proxyConf != "" {
proxy, err := url.Parse(proxyConf)
if err != nil {
return
}
transport := &http.Transport{Proxy: http.ProxyURL(proxy)}
conn.HTTPClient = &http.Client{Transport: transport}
}
}
func (p *PullJob) GetPullJob() *PullJob {
return p
}
func (p *PullJob) Init(puller IPuller, plugin *Plugin, streamPath string, conf config.Pull, pubConf *config.Publish) *PullJob {
if pubConf == nil {
p.PublishConfig = plugin.GetCommonConf().Publish
} else {
p.PublishConfig = *pubConf
}
p.PublishConfig.PubType = PublishTypePull
p.Args = url.Values(conf.Args.DeepClone())
p.conf = &conf
remoteURL := conf.URL
u, err := url.Parse(remoteURL)
if err == nil {
if u.Host == "" {
// file
remoteURL = u.Path
}
if p.Args == nil {
p.Args = u.Query()
} else {
for k, v := range u.Query() {
for _, vv := range v {
p.Args.Add(k, vv)
}
}
}
}
p.Connection.Init(plugin, streamPath, remoteURL, conf.Proxy, http.Header(conf.Header))
p.puller = puller
p.SetDescriptions(task.Description{
"plugin": plugin.Meta.Name,
"streamPath": streamPath,
"url": conf.URL,
"args": conf.Args,
"maxRetry": conf.MaxRetry,
})
puller.SetRetry(conf.MaxRetry, conf.RetryInterval)
plugin.Server.Pulls.Add(p, plugin.Logger.With("pullURL", conf.URL, "streamPath", streamPath))
return p
}
func (p *PullJob) GetKey() string {
return p.StreamPath
}
func (p *PullJob) Publish() (err error) {
streamPath := p.StreamPath
if len(p.Args) > 0 {
streamPath += "?" + p.Args.Encode()
}
p.Publisher, err = p.Plugin.PublishWithConfig(p.puller.GetTask().Context, streamPath, p.PublishConfig)
if err == nil && p.conf.MaxRetry != 0 {
p.Publisher.OnDispose(func() {
if p.Publisher.StopReasonIs(pkg.ErrPublishDelayCloseTimeout, task.ErrStopByUser) {
p.Stop(p.Publisher.StopReason())
} else {
p.puller.Stop(p.Publisher.StopReason())
}
})
}
return
}
func (p *PullJob) Start() (err error) {
s := p.Plugin.Server
if _, ok := s.Pulls.Get(p.GetKey()); ok {
return pkg.ErrStreamExist
}
p.AddTask(p.puller, p.Logger)
return
}
func (p *HTTPFilePuller) Start() (err error) {
if err = p.PullJob.Publish(); err != nil {
return
}
remoteURL := p.PullJob.RemoteURL
if strings.HasPrefix(remoteURL, "http") {
var res *http.Response
if res, err = p.PullJob.HTTPClient.Get(remoteURL); err == nil {
if res.StatusCode != http.StatusOK {
return io.EOF
}
p.ReadCloser = res.Body
}
} else if strings.HasPrefix(remoteURL, "ws") {
var ws *websocket.Conn
dialer := websocket.Dialer{
HandshakeTimeout: 10 * time.Second,
}
if ws, _, err = dialer.Dial(remoteURL, nil); err == nil {
p.ReadCloser = &wsReadCloser{ws: ws}
}
} else {
var res *os.File
if res, err = os.Open(remoteURL); err == nil {
p.ReadCloser = res
}
//p.PullJob.Publisher.Publish.Speed = 1
}
return
}
func (p *HTTPFilePuller) GetPullJob() *PullJob {
return &p.PullJob
}
func (p *HTTPFilePuller) Dispose() {
p.ReadCloser.Close()
}
func (p *RecordFilePuller) GetPullJob() *PullJob {
return &p.PullJob
}
func (p *RecordFilePuller) queryRecordStreams(startTime, endTime time.Time) (err error) {
if p.PullJob.Plugin.DB == nil {
return pkg.ErrNoDB
}
queryRecord := RecordStream{
Mode: RecordModeAuto,
Type: p.Type,
}
tx := p.PullJob.Plugin.DB.Where(&queryRecord).Find(&p.Streams, "end_time>=? AND start_time<=? AND stream_path=?", startTime, endTime, p.PullJob.RemoteURL)
if tx.Error != nil {
return tx.Error
}
if len(p.Streams) == 0 {
return pkg.ErrNotFound
}
for _, stream := range p.Streams {
p.Debug("queryRecordStreams", "filePath", stream.FilePath)
}
p.MaxTS = endTime.Sub(startTime).Milliseconds()
return nil
}
func (p *RecordFilePuller) Start() (err error) {
p.SetRetry(0, 0)
if p.PullJob.Plugin.DB == nil {
return pkg.ErrNoDB
}
p.PullJob.PublishConfig.PubType = PublishTypeVod
if err = p.PullJob.Publish(); err != nil {
return
}
if p.PullStartTime, p.PullEndTime, err = util.TimeRangeQueryParse(p.PullJob.Args); err != nil {
return
}
p.seekChan = make(chan time.Time, 1)
loop := p.PullJob.Args.Get(util.LoopKey)
p.Loop, err = strconv.Atoi(loop)
if err != nil || p.Loop < 0 {
p.Loop = math.MaxInt32
}
publisher := p.PullJob.Publisher
publisher.OnSeek = func(seekTime time.Time) {
// p.PullStartTime = seekTime
// p.SetRetry(1, 0)
// if util.UnixTimeReg.MatchString(p.PullJob.Args.Get(util.EndKey)) {
// p.PullJob.Args.Set(util.StartKey, strconv.FormatInt(seekTime.Unix(), 10))
// } else {
// p.PullJob.Args.Set(util.StartKey, seekTime.Local().Format(util.LocalTimeFormat))
// }
select {
case p.seekChan <- seekTime:
default:
}
}
return p.queryRecordStreams(p.PullStartTime, p.PullEndTime)
}
func (p *RecordFilePuller) GetSeekChan() chan time.Time {
return p.seekChan
}
func (p *RecordFilePuller) Dispose() {
if p.File != nil {
p.File.Close()
}
close(p.seekChan)
}
func (w *wsReadCloser) Read(p []byte) (n int, err error) {
_, message, err := w.ws.ReadMessage()
if err != nil {
return 0, err
}
return copy(p, message), nil
}
func (w *wsReadCloser) Close() error {
return w.ws.Close()
}
func (p *RecordFilePuller) CheckSeek() (needSeek bool, err error) {
select {
case p.PullStartTime = <-p.seekChan:
if err = p.queryRecordStreams(p.PullStartTime, p.PullEndTime); err != nil {
return
}
if p.File != nil {
p.File.Close()
p.File = nil
}
needSeek = true
default:
}
return
}