mirror of
https://github.com/langhuihui/monibuca.git
synced 2025-12-24 13:48:04 +08:00
91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package m7s
|
|
|
|
import (
|
|
"fmt"
|
|
"m7s.live/m7s/v5/pkg/util"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"m7s.live/m7s/v5/pkg"
|
|
)
|
|
|
|
type Recorder = func(*RecordContext) error
|
|
|
|
func createRecoder(p *Plugin, streamPath string, filePath string) (recorder *RecordContext) {
|
|
recorder = &RecordContext{
|
|
Plugin: p,
|
|
Fragment: p.config.Record.Fragment,
|
|
Append: p.config.Record.Append,
|
|
FilePath: filePath,
|
|
StreamPath: streamPath,
|
|
}
|
|
recorder.Name = "record"
|
|
recorder.Logger = p.Logger.With("filePath", filePath, "streamPath", streamPath)
|
|
return
|
|
}
|
|
|
|
type RecordContext struct {
|
|
util.MarcoTask
|
|
StreamPath string // 对应本地流
|
|
Plugin *Plugin
|
|
Subscriber *Subscriber
|
|
Fragment time.Duration
|
|
Append bool
|
|
FilePath string
|
|
}
|
|
|
|
func (p *RecordContext) GetKey() string {
|
|
return p.FilePath
|
|
}
|
|
|
|
type recordSubTask struct {
|
|
util.Task
|
|
ctx *RecordContext
|
|
Recorder
|
|
}
|
|
|
|
func (r *recordSubTask) Start() (err error) {
|
|
p := r.ctx
|
|
dir := p.FilePath
|
|
if p.Fragment == 0 || p.Append {
|
|
if filepath.Ext(p.FilePath) == "" {
|
|
p.FilePath += ".flv"
|
|
}
|
|
dir = filepath.Dir(p.FilePath)
|
|
}
|
|
r.Name = fmt.Sprintf("record:%s", p.FilePath)
|
|
if err = os.MkdirAll(dir, 0755); err != nil {
|
|
return
|
|
}
|
|
p.Subscriber, err = p.Plugin.Subscribe(r.Context, p.StreamPath)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Recorder(p)
|
|
return
|
|
}
|
|
|
|
func (p *RecordContext) Do(recorder Recorder) {
|
|
p.AddTask(&recordSubTask{
|
|
ctx: p,
|
|
Recorder: recorder,
|
|
})
|
|
}
|
|
|
|
func (p *RecordContext) Start() (err error) {
|
|
s := p.Plugin.Server
|
|
if _, ok := s.Records.Get(p.GetKey()); ok {
|
|
return pkg.ErrRecordSamePath
|
|
}
|
|
s.Records.Add(p)
|
|
if p.Plugin.Meta.Recorder != nil {
|
|
p.Do(p.Plugin.Meta.Recorder)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (p *RecordContext) Dispose() {
|
|
p.Plugin.Server.Records.Remove(p)
|
|
}
|