🐛 FIX: 自动录制没有进入当前录制列表中

This commit is contained in:
dexter
2022-10-11 19:15:02 +08:00
parent 55c1611d83
commit 2594e446df
7 changed files with 48 additions and 32 deletions

View File

@@ -76,7 +76,7 @@ func (r *Record) Tree(dstPath string, level int) (files []*VideoFileInfo, err er
return return
} }
if !fileInfo.IsDir() { //如果dstF是文件 if !fileInfo.IsDir() { //如果dstF是文件
if path.Ext(fileInfo.Name()) == r.Ext { if r.Ext == "." || path.Ext(fileInfo.Name()) == r.Ext {
p := strings.TrimPrefix(dstPath, r.Path) p := strings.TrimPrefix(dstPath, r.Path)
p = strings.ReplaceAll(p, "\\", "/") p = strings.ReplaceAll(p, "\\", "/")
var duration uint32 var duration uint32

11
flv.go
View File

@@ -13,7 +13,14 @@ type FLVRecorder struct {
Recorder Recorder
} }
func (r *FLVRecorder) Start() { func (r *FLVRecorder) Start(streamPath string) (err error){
r.Record = &recordConfig.Flv
r.ID = streamPath + "/flv"
return plugin.Subscribe(streamPath, r)
}
func (r *FLVRecorder) start() {
recordConfig.recordings.Store(r.ID, r)
r.PlayFLV() r.PlayFLV()
recordConfig.recordings.Delete(r.ID) recordConfig.recordings.Delete(r.ID)
r.Close() r.Close()
@@ -35,7 +42,7 @@ func (r *FLVRecorder) OnEvent(event any) {
if !r.append { if !r.append {
r.Write(codec.FLVHeader) r.Write(codec.FLVHeader)
} }
go r.Start() go r.start()
case FLVFrame: case FLVFrame:
if ts := r.Video.Frame.AbsTime - r.SkipTS; r.Video.Frame.IFrame && int64(ts-r.FirstAbsTS) >= int64(r.Fragment*1000) { if ts := r.Video.Frame.AbsTime - r.SkipTS; r.Video.Frame.IFrame && int64(ts-r.FirstAbsTS) >= int64(r.Fragment*1000) {
r.FirstAbsTS = ts r.FirstAbsTS = ts

8
hls.go
View File

@@ -22,6 +22,12 @@ type HLSRecorder struct {
tsWriter io.WriteCloser tsWriter io.WriteCloser
} }
func (h *HLSRecorder) Start(streamPath string) error {
h.Record = &recordConfig.Hls
h.ID = streamPath + "/hls"
return plugin.Subscribe(streamPath, h)
}
func (h *HLSRecorder) OnEvent(event any) { func (h *HLSRecorder) OnEvent(event any) {
var err error var err error
defer func() { defer func() {
@@ -45,7 +51,7 @@ func (h *HLSRecorder) OnEvent(event any) {
if err = h.createHlsTsSegmentFile(); err != nil { if err = h.createHlsTsSegmentFile(); err != nil {
return return
} }
go h.Start() go h.start()
case AudioDeConf: case AudioDeConf:
h.asc, err = hls.DecodeAudioSpecificConfig(v.AVCC[0]) h.asc, err = hls.DecodeAudioSpecificConfig(v.AVCC[0])
case *AudioFrame: case *AudioFrame:

40
main.go
View File

@@ -60,23 +60,18 @@ func (conf *RecordConfig) OnEvent(event any) {
case SEpublish: case SEpublish:
if conf.Flv.NeedRecord(v.Stream.Path) { if conf.Flv.NeedRecord(v.Stream.Path) {
var flv FLVRecorder var flv FLVRecorder
flv.Record = &conf.Flv flv.Start(v.Stream.Path)
plugin.Subscribe(v.Stream.Path, &flv)
} }
if conf.Mp4.NeedRecord(v.Stream.Path) { if conf.Mp4.NeedRecord(v.Stream.Path) {
mp4 := NewMP4Recorder() NewMP4Recorder().Start(v.Stream.Path)
mp4.Record = &conf.Mp4
plugin.Subscribe(v.Stream.Path, mp4)
} }
if conf.Hls.NeedRecord(v.Stream.Path) { if conf.Hls.NeedRecord(v.Stream.Path) {
var hls HLSRecorder var hls HLSRecorder
hls.Record = &conf.Hls hls.Start(v.Stream.Path)
plugin.Subscribe(v.Stream.Path, &hls)
} }
if conf.Raw.NeedRecord(v.Stream.Path) { if conf.Raw.NeedRecord(v.Stream.Path) {
var raw RawRecorder var raw RawRecorder
raw.Record = &conf.Raw raw.Start(v.Stream.Path)
plugin.Subscribe(v.Stream.Path, &raw)
} }
} }
} }
@@ -133,39 +128,33 @@ func (conf *RecordConfig) API_start(w http.ResponseWriter, r *http.Request) {
t := query.Get("type") t := query.Get("type")
var sub ISubscriber var sub ISubscriber
var filePath string var filePath string
var err error
switch t { switch t {
case "": case "":
t = "flv" t = "flv"
fallthrough fallthrough
case "flv": case "flv":
var flvRecoder FLVRecorder var flvRecoder FLVRecorder
flvRecoder.Record = &conf.Flv
sub = &flvRecoder
flvRecoder.append = query.Get("append") != "" && util.Exist(filePath) flvRecoder.append = query.Get("append") != "" && util.Exist(filePath)
err = flvRecoder.Start(streamPath)
case "mp4": case "mp4":
recorder := NewMP4Recorder() err = NewMP4Recorder().Start(streamPath)
recorder.Record = &conf.Mp4
sub = recorder
case "hls": case "hls":
recorder := &HLSRecorder{} var recorder HLSRecorder
recorder.Record = &conf.Hls err = recorder.Start(streamPath)
sub = recorder
case "raw": case "raw":
recorder := &RawRecorder{} var recorder RawRecorder
recorder.Record = &conf.Raw
recorder.append = query.Get("append") != "" && util.Exist(filePath) recorder.append = query.Get("append") != "" && util.Exist(filePath)
sub = recorder err = recorder.Start(streamPath)
default: default:
http.Error(w, "type not supported", http.StatusBadRequest) http.Error(w, "type not supported", http.StatusBadRequest)
return
} }
if err := plugin.Subscribe(streamPath, sub); err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
id := streamPath + "/" + t w.Write([]byte(sub.GetIO().ID))
sub.GetIO().ID = id
conf.recordings.Store(id, sub)
w.Write([]byte(id))
} }
func (conf *RecordConfig) API_list_recording(w http.ResponseWriter, r *http.Request) { func (conf *RecordConfig) API_list_recording(w http.ResponseWriter, r *http.Request) {
@@ -182,6 +171,7 @@ func (conf *RecordConfig) API_list_recording(w http.ResponseWriter, r *http.Requ
func (conf *RecordConfig) API_stop(w http.ResponseWriter, r *http.Request) { func (conf *RecordConfig) API_stop(w http.ResponseWriter, r *http.Request) {
if recorder, ok := conf.recordings.Load(r.URL.Query().Get("id")); ok { if recorder, ok := conf.recordings.Load(r.URL.Query().Get("id")); ok {
recorder.(ISubscriber).Stop() recorder.(ISubscriber).Stop()
w.Write([]byte("ok"))
return return
} }
http.Error(w, "no such recorder", http.StatusBadRequest) http.Error(w, "no such recorder", http.StatusBadRequest)

8
mp4.go
View File

@@ -67,6 +67,12 @@ func NewMP4Recorder() *MP4Recorder {
return r return r
} }
func (r *MP4Recorder) Start(streamPath string) (err error) {
r.Record = &recordConfig.Mp4
r.ID = streamPath + "/mp4"
return plugin.Subscribe(streamPath, r)
}
func (r *MP4Recorder) Close() error { func (r *MP4Recorder) Close() error {
if r.Writer != nil { if r.Writer != nil {
if r.video.fragment != nil { if r.video.fragment != nil {
@@ -135,7 +141,7 @@ func (r *MP4Recorder) OnEvent(event any) {
case ISubscriber: case ISubscriber:
defaultFtyp.Encode(r) defaultFtyp.Encode(r)
r.Moov.Encode(r) r.Moov.Encode(r)
go r.Start() go r.start()
case *AudioFrame: case *AudioFrame:
if r.audio.trackId != 0 { if r.audio.trackId != 0 {
r.audio.push(r, v.AbsTime, v.DeltaTime, util.ConcatBuffers(v.Raw), mp4.SyncSampleFlags) r.audio.push(r, v.AbsTime, v.DeltaTime, util.ConcatBuffers(v.Raw), mp4.SyncSampleFlags)

8
raw.go
View File

@@ -14,11 +14,17 @@ type RawRecorder struct {
Recorder Recorder
} }
func (r *RawRecorder) Start(streamPath string) error {
r.Record = &recordConfig.Raw
r.ID = streamPath + "/raw"
return plugin.Subscribe(streamPath, r)
}
func (r *RawRecorder) OnEvent(event any) { func (r *RawRecorder) OnEvent(event any) {
r.Recorder.OnEvent(event) r.Recorder.OnEvent(event)
switch v := event.(type) { switch v := event.(type) {
case *RawRecorder: case *RawRecorder:
go r.Start() go r.start()
case *track.Video: case *track.Video:
if r.Ext == "." { if r.Ext == "." {
if v.CodecID == codec.CodecID_H264 { if v.CodecID == codec.CodecID_H264 {

View File

@@ -15,7 +15,8 @@ type Recorder struct {
append bool // 是否追加模式 append bool // 是否追加模式
} }
func (r *Recorder) Start() { func (r *Recorder) start() {
recordConfig.recordings.Store(r.ID, r)
r.PlayRaw() r.PlayRaw()
recordConfig.recordings.Delete(r.ID) recordConfig.recordings.Delete(r.ID)
r.Close() r.Close()