forked from Monibuca/plugin-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw.go
102 lines (95 loc) · 1.92 KB
/
raw.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
package record
import (
"go.uber.org/zap"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/track"
)
type RawRecorder struct {
Recorder
IsAudio bool
}
func NewRawRecorder() (r *RawRecorder) {
r = &RawRecorder{}
r.Record = RecordPluginConfig.Raw
r.Storage = RecordPluginConfig.Storage
return r
}
func NewRawAudioRecorder() (r *RawRecorder) {
r = &RawRecorder{IsAudio: true}
r.Record = RecordPluginConfig.RawAudio
return r
}
func (r *RawRecorder) Start(streamPath string) error {
r.ID = streamPath + "/raw"
if r.IsAudio {
r.ID += "_audio"
}
return r.start(r, streamPath, SUBTYPE_RAW)
}
func (r *RawRecorder) StartWithFileName(streamPath string, fileName string) error {
r.ID = streamPath + "/raw/" + fileName
if r.IsAudio {
r.ID += "_audio"
}
return r.start(r, streamPath, SUBTYPE_RAW)
}
func (r *RawRecorder) Close() (err error) {
if r.File != nil {
err = r.File.Close()
if err != nil {
r.Error("Raw File Close", zap.Error(err))
} else {
r.Info("Raw File Close", zap.Error(err))
go r.UploadFile(r.Path, r.filePath)
}
}
return
}
func (r *RawRecorder) OnEvent(event any) {
switch v := event.(type) {
case FileWr:
r.SetIO(v)
case *RawRecorder:
r.Recorder.OnEvent(event)
case *track.Video:
if r.IsAudio {
break
}
if r.Ext == "." {
if v.CodecID == codec.CodecID_H264 {
r.Ext = ".h264"
} else {
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:
r.Recorder.OnEvent(event)
if _, err := v.WriteRawTo(r); err != nil {
r.Stop(zap.Error(err))
}
case VideoFrame:
r.Recorder.OnEvent(event)
if _, err := v.WriteAnnexBTo(r); err != nil {
r.Stop(zap.Error(err))
}
default:
r.IO.OnEvent(v)
}
}