feat: add raw_audio recoder

This commit is contained in:
langhuihui
2023-05-25 14:17:50 +08:00
parent 61c5ae667b
commit 079d712e88
3 changed files with 88 additions and 16 deletions

64
raw.go
View File

@@ -12,11 +12,19 @@ import (
type RawRecorder struct {
Recorder
IsAudio bool
}
func (r *RawRecorder) Start(streamPath string) error {
r.Record = &RecordPluginConfig.Raw
if r.IsAudio {
r.Record = &RecordPluginConfig.RawAudio
} else {
r.Record = &RecordPluginConfig.Raw
}
r.ID = streamPath + "/raw"
if r.IsAudio {
r.ID += "_audio"
}
if _, ok := RecordPluginConfig.recordings.Load(r.ID); ok {
return ErrRecordExist
}
@@ -24,11 +32,22 @@ func (r *RawRecorder) Start(streamPath string) error {
}
func (r *RawRecorder) OnEvent(event any) {
r.Recorder.OnEvent(event)
switch v := event.(type) {
case *RawRecorder:
filename := strconv.FormatInt(time.Now().Unix(), 10) + r.Ext
if r.Fragment == 0 {
filename = r.Stream.Path + r.Ext
} else {
filename = filepath.Join(r.Stream.Path, filename)
}
if file, err := r.CreateFileFn(filename, r.append); err == nil {
r.SetIO(file)
}
go r.start()
case *track.Video:
if r.IsAudio {
break
}
if r.Ext == "." {
if v.CodecID == codec.CodecID_H264 {
r.Ext = ".h264"
@@ -36,14 +55,45 @@ func (r *RawRecorder) OnEvent(event any) {
r.Ext = ".h265"
}
}
r.AddTrack(v)
case *track.Audio:
if !r.IsAudio {
break
}
if r.Ext == "." {
switch v.CodecID {
case codec.CodecID_AAC:
r.Ext = ".aac"
case codec.CodecID_PCMA:
r.Ext = ".pcma"
case codec.CodecID_PCMU:
r.Ext = ".pcmu"
}
}
r.AddTrack(v)
case AudioFrame:
if r.Fragment > 0 {
if r.cut(v.AbsTime); r.newFile {
r.newFile = false
r.Close()
if file, err := r.CreateFileFn(filepath.Join(r.Stream.Path, strconv.FormatInt(time.Now().Unix(), 10)+r.Ext), false); err == nil {
r.SetIO(file)
}
}
}
v.WriteRawTo(r)
case VideoFrame:
if r.Fragment != 0 && r.newFile {
r.newFile = false
r.Close()
if file, err := r.CreateFileFn(filepath.Join(r.Stream.Path, strconv.FormatInt(time.Now().Unix(), 10)+r.Ext), false); err == nil {
r.SetIO(file)
if r.Fragment > 0 && v.IFrame {
if r.cut(v.AbsTime); r.newFile {
r.newFile = false
r.Close()
if file, err := r.CreateFileFn(filepath.Join(r.Stream.Path, strconv.FormatInt(time.Now().Unix(), 10)+r.Ext), false); err == nil {
r.SetIO(file)
}
}
}
v.WriteAnnexBTo(r)
default:
r.IO.OnEvent(v)
}
}