forked from Monibuca/plugin-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flv.go
223 lines (212 loc) · 5.78 KB
/
flv.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
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
package record
import (
"fmt"
"io"
"net"
"os"
"time"
"go.uber.org/zap"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/util"
)
type FLVRecorder struct {
Recorder
filepositions []uint64
times []float64
Offset int64
duration int64
}
func NewFLVRecorder() (r *FLVRecorder) {
r = &FLVRecorder{}
r.Record = RecordPluginConfig.Flv
return r
}
func (r *FLVRecorder) Start(streamPath string) (err error) {
r.ID = streamPath + "/flv"
return r.start(r, streamPath, SUBTYPE_FLV)
}
func (r *FLVRecorder) StartWithFileName(streamPath string, fileName string) error {
r.ID = fmt.Sprintf("%s/flv/%s", streamPath, fileName)
return r.start(r, streamPath, SUBTYPE_FLV)
}
func (r *FLVRecorder) writeMetaData(file FileWr, duration int64) {
defer file.Close()
at, vt := r.Audio, r.Video
hasAudio, hasVideo := at != nil, vt != nil
var amf util.AMF
metaData := util.EcmaArray{
"MetaDataCreator": "m7s " + Engine.Version,
"hasVideo": hasVideo,
"hasAudio": hasAudio,
"hasMatadata": true,
"canSeekToEnd": false,
"duration": float64(duration) / 1000,
"hasKeyFrames": len(r.filepositions) > 0,
"filesize": 0,
}
var flags byte
if hasAudio {
flags |= (1 << 2)
metaData["audiocodecid"] = int(at.CodecID)
metaData["audiosamplerate"] = at.SampleRate
metaData["audiosamplesize"] = at.SampleSize
metaData["stereo"] = at.Channels == 2
}
if hasVideo {
flags |= 1
metaData["videocodecid"] = int(vt.CodecID)
metaData["width"] = vt.SPSInfo.Width
metaData["height"] = vt.SPSInfo.Height
metaData["framerate"] = vt.FPS
metaData["videodatarate"] = vt.BPS
metaData["keyframes"] = map[string]any{
"filepositions": r.filepositions,
"times": r.times,
}
defer func() {
r.filepositions = []uint64{0}
r.times = []float64{0}
}()
}
amf.Marshals("onMetaData", metaData)
offset := amf.Len() + len(codec.FLVHeader) + 15
if keyframesCount := len(r.filepositions); keyframesCount > 0 {
metaData["filesize"] = uint64(offset) + r.filepositions[keyframesCount-1]
for i := range r.filepositions {
r.filepositions[i] += uint64(offset)
}
metaData["keyframes"] = map[string]any{
"filepositions": r.filepositions,
"times": r.times,
}
}
if tempFile, err := os.CreateTemp("", "*.flv"); err != nil {
r.Error("create temp file failed: ", zap.Error(err))
return
} else {
defer func() {
tempFile.Close()
os.Remove(tempFile.Name())
r.Info("writeMetaData success")
}()
_, err := tempFile.Write([]byte{'F', 'L', 'V', 0x01, flags, 0, 0, 0, 9, 0, 0, 0, 0})
if err != nil {
r.Error("", zap.Error(err))
return
}
amf.Reset()
marshals := amf.Marshals("onMetaData", metaData)
codec.WriteFLVTag(tempFile, codec.FLV_TAG_TYPE_SCRIPT, 0, marshals)
_, err = file.Seek(int64(len(codec.FLVHeader)), io.SeekStart)
if err != nil {
r.Error("writeMetaData Seek failed: ", zap.Error(err))
return
}
_, err = io.Copy(tempFile, file)
if err != nil {
r.Error("writeMetaData Copy failed: ", zap.Error(err))
return
}
tempFile.Seek(0, io.SeekStart)
file.Seek(0, io.SeekStart)
_, err = io.Copy(file, tempFile)
if err != nil {
r.Error("writeMetaData Copy failed: ", zap.Error(err))
return
}
}
}
func (r *FLVRecorder) OnEvent(event any) {
r.Recorder.OnEvent(event)
switch v := event.(type) {
case FileWr:
// 写入文件头
if !r.append {
v.Write(codec.FLVHeader)
} else {
if _, err := v.Seek(-4, io.SeekEnd); err != nil {
r.Error("seek file failed", zap.Error(err))
v.Write(codec.FLVHeader)
} else {
tmp := make(util.Buffer, 4)
tmp2 := tmp
v.Read(tmp)
tagSize := tmp.ReadUint32()
tmp = tmp2
v.Seek(int64(tagSize), io.SeekEnd)
v.Read(tmp2)
ts := tmp2.ReadUint24() | (uint32(tmp[3]) << 24)
r.Info("append flv", zap.Uint32("last tagSize", tagSize), zap.Uint32("last ts", ts))
if r.VideoReader != nil {
r.VideoReader.StartTs = time.Duration(ts) * time.Millisecond
}
if r.AudioReader != nil {
r.AudioReader.StartTs = time.Duration(ts) * time.Millisecond
}
v.Seek(0, io.SeekEnd)
}
}
case FLVFrame:
check := false
var absTime uint32
if r.VideoReader == nil {
check = true
absTime = r.AudioReader.AbsTime
} else if v.IsVideo() {
check = r.VideoReader.Value.IFrame
absTime = r.VideoReader.AbsTime
if check {
r.filepositions = append(r.filepositions, uint64(r.Offset))
r.times = append(r.times, float64(absTime)/1000)
}
}
if r.duration = int64(absTime); r.Fragment > 0 && check && time.Duration(r.duration)*time.Millisecond >= r.Fragment {
r.Close()
r.Offset = 0
if file, err := r.CreateFile(); err == nil {
r.File = file
file.Write(codec.FLVHeader)
var dcflv net.Buffers
if r.VideoReader != nil {
r.VideoReader.ResetAbsTime()
dcflv = codec.VideoAVCC2FLV(0, r.VideoReader.Track.SequenceHead)
flv := append(dcflv, codec.VideoAVCC2FLV(0, r.VideoReader.Value.AVCC.ToBuffers()...)...)
flv.WriteTo(file)
}
if r.AudioReader != nil {
r.AudioReader.ResetAbsTime()
if r.Audio.CodecID == codec.CodecID_AAC {
dcflv = codec.AudioAVCC2FLV(0, r.AudioReader.Track.SequenceHead)
}
flv := append(dcflv, codec.AudioAVCC2FLV(0, r.AudioReader.Value.AVCC.ToBuffers()...)...)
flv.WriteTo(file)
}
return
}
}
if n, err := v.WriteTo(r.File); err != nil {
r.Error("write file failed", zap.Error(err))
r.Stop(zap.Error(err))
} else {
r.Offset += n
}
}
}
func (r *FLVRecorder) Close() (err error) {
if r.File != nil {
if !r.append {
go r.writeMetaData(r.File, r.duration)
} else {
err = r.File.Close()
if err != nil {
r.Error("FLV File Close", zap.Error(err))
} else {
r.Info("FLV File Close", zap.Error(err))
go r.UploadFile(r.Path, r.filePath)
}
return err
}
}
return nil
}