forked from langhuihui/monibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuller.go
More file actions
486 lines (441 loc) · 11.5 KB
/
puller.go
File metadata and controls
486 lines (441 loc) · 11.5 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package m7s
import (
"crypto/tls"
"io"
"math"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/langhuihui/gomem"
task "github.com/langhuihui/gotask"
pkg "m7s.live/v5/pkg"
"m7s.live/v5/pkg/config"
"m7s.live/v5/pkg/format"
"m7s.live/v5/pkg/storage"
"m7s.live/v5/pkg/util"
)
type (
Connection struct {
task.Job
Plugin *Plugin
StreamPath string // 对应本地流
Args url.Values
RemoteURL string // 远程服务器地址(用于推拉)
HTTPClient *http.Client
}
IPuller interface {
task.ITask
GetPullJob() *PullJob
}
PullerFactory = func(config.Pull) IPuller
PullJob struct {
Connection
*config.Pull
Publisher *Publisher
PublishConfig config.Publish
puller IPuller
Progress *SubscriptionProgress
}
HTTPFilePuller struct {
task.Task
PullJob PullJob
io.ReadCloser
}
RecordFilePuller struct {
task.Task
PullJob PullJob
PullStartTime, PullEndTime time.Time
Streams []RecordStream
File storage.File
MaxTS int64
seekChan chan time.Time
Type string
Loop int
}
wsReadCloser struct {
ws *websocket.Conn
}
)
// Fixed progress steps for HTTP file pull workflow
var httpFilePullSteps = []pkg.StepDef{
{Name: pkg.StepPublish, Description: "Publishing file stream"},
{Name: pkg.StepURLParsing, Description: "Determining file source type"},
{Name: pkg.StepConnection, Description: "Establishing file connection"},
{Name: pkg.StepParsing, Description: "Parsing file format"},
{Name: pkg.StepStreaming, Description: "Reading and publishing stream data"},
}
func (conn *Connection) Init(plugin *Plugin, streamPath string, href string, proxyConf string) {
conn.RemoteURL = href
conn.StreamPath = streamPath
conn.Plugin = plugin
// Create a custom HTTP client that ignores HTTPS certificate validation
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if proxyConf != "" {
proxy, err := url.Parse(proxyConf)
if err != nil {
return
}
tr.Proxy = http.ProxyURL(proxy)
}
conn.HTTPClient = &http.Client{Transport: tr}
}
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.Connection.Args = url.Values(conf.Args.DeepClone())
p.Pull = &conf
remoteURL := conf.URL
u, err := url.Parse(remoteURL)
if err == nil {
if u.Host == "" {
// file
remoteURL = u.Path
}
if p.Connection.Args == nil {
p.Connection.Args = u.Query()
} else {
for k, v := range u.Query() {
for _, vv := range v {
p.Connection.Args.Add(k, vv)
}
}
}
}
p.Connection.Init(plugin, streamPath, remoteURL, conf.Proxy)
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)
if sender, webhook := plugin.getHookSender(config.HookOnPullStart); sender != nil {
puller.OnStart(func() {
alarmInfo := AlarmInfo{
AlarmName: string(config.HookOnPullStart),
StreamPath: streamPath,
AlarmType: config.AlarmPullRecover,
}
sender(webhook, alarmInfo)
})
}
if sender, webhook := plugin.getHookSender(config.HookOnPullEnd); sender != nil {
puller.OnDispose(func() {
p.Fail(puller.StopReason().Error())
alarmInfo := AlarmInfo{
AlarmName: string(config.HookOnPullEnd),
AlarmDesc: puller.StopReason().Error(),
StreamPath: streamPath,
AlarmType: config.AlarmPullOffline,
}
sender(webhook, alarmInfo)
})
}
plugin.Server.Pulls.AddTask(p, plugin.Logger.With("pullURL", conf.URL, "streamPath", streamPath))
return p
}
func (p *PullJob) GetKey() string {
return p.StreamPath
}
// Strongly typed helper.
func (p *PullJob) GoToStepConst(name pkg.StepName) {
if p.Progress == nil {
return
}
// Find step index by name
stepIndex := -1
for i, step := range p.Progress.Steps {
if step.Name == string(name) {
stepIndex = i
break
}
}
if stepIndex >= 0 {
// complete current step if moving forward
cur := p.Progress.CurrentStep
if cur != stepIndex {
cs := &p.Progress.Steps[cur]
if cs.StartedAt.IsZero() {
cs.StartedAt = time.Now()
}
if cs.CompletedAt.IsZero() {
cs.CompletedAt = time.Now()
}
}
p.Progress.CurrentStep = stepIndex
ns := &p.Progress.Steps[stepIndex]
ns.Error = ""
if ns.StartedAt.IsZero() {
ns.StartedAt = time.Now()
}
}
}
// Fail marks the current step as failed with an error message
func (p *PullJob) Fail(errorMsg string) {
if p.Progress == nil {
return
}
idx := p.Progress.CurrentStep
if idx >= 0 && idx < len(p.Progress.Steps) {
s := &p.Progress.Steps[idx]
s.Error = errorMsg
if s.StartedAt.IsZero() {
s.StartedAt = time.Now()
}
if s.CompletedAt.IsZero() { // mark failed completion time
s.CompletedAt = time.Now()
}
}
}
// SetProgressSteps sets multiple steps from a string array where every two elements represent a step (name, description)
func (p *PullJob) SetProgressStepsDefs(defs []pkg.StepDef) {
if p.Progress == nil {
return
}
p.Progress.Steps = p.Progress.Steps[:0]
for _, d := range defs {
p.Progress.Steps = append(p.Progress.Steps, Step{Name: string(d.Name), Description: d.Description})
}
}
func (p *PullJob) Publish() (err error) {
if p.TestMode > 0 {
return nil
}
streamPath := p.StreamPath
if len(p.Connection.Args) > 0 {
streamPath += "?" + p.Connection.Args.Encode()
}
var publisher *Publisher
publisher, err = p.Plugin.PublishWithConfig(p.puller, streamPath, p.PublishConfig)
if err == nil {
p.Publisher = publisher
publisher.OnDispose(func() {
if publisher.StopReasonIs(pkg.ErrPublishDelayCloseTimeout, task.ErrStopByUser) || p.MaxRetry == 0 {
p.Stop(publisher.StopReason())
} else {
p.puller.Stop(publisher.StopReason())
}
})
}
return
}
func (p *PullJob) Start() (err error) {
p.AddTask(p.puller, p.Logger)
return
}
func (p *HTTPFilePuller) Start() (err error) {
p.PullJob.SetProgressStepsDefs(httpFilePullSteps)
if p.PullJob.PublishConfig.Speed == 0 {
p.PullJob.PublishConfig.Speed = 1 // 对于文件流需要控制速度
}
if err = p.PullJob.Publish(); err != nil {
p.PullJob.Fail(err.Error())
return
}
// move to url_parsing step
p.PullJob.GoToStepConst(pkg.StepURLParsing)
if p.ReadCloser != nil {
return
}
p.PullJob.GoToStepConst(pkg.StepConnection)
remoteURL := p.PullJob.RemoteURL
p.Info("pull", "remoteurl", 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 {
p.PullJob.Fail("HTTP status not OK")
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
}
if err != nil {
p.PullJob.Fail(err.Error())
return
}
if p.ReadCloser != nil {
p.OnStop(p.ReadCloser.Close)
}
return
}
func (p *HTTPFilePuller) GetPullJob() *PullJob {
return &p.PullJob
}
func (p *HTTPFilePuller) Dispose() {
p.ReadCloser = nil
}
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{
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.Connection.Args); err != nil {
return
}
p.seekChan = make(chan time.Time, 1)
loop := p.PullJob.Connection.Args.Get(util.LoopKey)
p.Loop, err = strconv.Atoi(loop)
if err != nil || p.Loop < 0 {
p.Loop = math.MaxInt32
}
publisher := p.PullJob.Publisher
if publisher != nil {
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
}
func NewAnnexBPuller(conf config.Pull) IPuller {
return &AnnexBPuller{}
}
type AnnexBPuller struct {
HTTPFilePuller
}
func (p *AnnexBPuller) Run() (err error) {
allocator := gomem.NewScalableMemoryAllocator(1 << gomem.MinPowerOf2)
defer allocator.Recycle()
writer := NewPublishVideoWriter[*format.AnnexB](p.PullJob.Publisher, allocator)
frame := writer.VideoFrame
// 移动到解析步骤 - 开始解析文件格式
p.PullJob.GoToStepConst(pkg.StepParsing)
// 创建 AnnexB 专用读取器
var annexbReader pkg.AnnexBReader
var hasFrame bool
// 移动到流数据读取步骤 - 开始读取和发布数据
p.PullJob.GoToStepConst(pkg.StepStreaming)
for !p.IsStopped() {
// 读取一块数据
chunkData := allocator.Malloc(8192)
n, readErr := p.ReadCloser.Read(chunkData)
if n != 8192 {
allocator.Free(chunkData[n:])
chunkData = chunkData[:n]
}
if readErr != nil && readErr != io.EOF {
p.PullJob.Fail(readErr.Error())
p.Error("读取数据失败", "error", readErr)
return readErr
}
// 将新数据追加到 AnnexB 读取器
annexbReader.AppendBuffer(chunkData)
hasFrame, err = frame.Parse(&annexbReader)
if err != nil {
p.PullJob.Fail(err.Error())
return
}
if hasFrame {
frame.SetTS32(uint32(time.Now().UnixMilli()))
if err = writer.NextVideo(); err != nil {
p.PullJob.Fail(err.Error())
return
}
frame = writer.VideoFrame
}
if readErr == io.EOF {
return
}
}
return
}