-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4.go
198 lines (188 loc) · 4.92 KB
/
mp4.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
package record
import (
"path/filepath"
"strconv"
"time"
"github.com/edgeware/mp4ff/aac"
"github.com/edgeware/mp4ff/mp4"
m7sdb "github.com/zzs89117920/m7s-db"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/track"
)
type mediaContext struct {
trackId uint32
fragment *mp4.Fragment
ts uint32 // 每个小片段起始时间戳
}
func (m *mediaContext) push(recoder *MP4Recorder, dt uint32, dur uint32, data []byte, flags uint32) {
if m.fragment != nil && dt-m.ts > 1000 {
m.fragment.Encode(recoder)
m.fragment = nil
}
if m.fragment == nil {
recoder.seqNumber++
m.fragment, _ = mp4.CreateFragment(recoder.seqNumber, m.trackId)
m.ts = dt
}
m.fragment.AddFullSample(mp4.FullSample{
Data: data,
DecodeTime: uint64(dt),
Sample: mp4.Sample{
Flags: flags,
Dur: dur,
Size: uint32(len(data)),
},
})
}
type MP4Recorder struct {
Recorder
*mp4.InitSegment `json:"-" yaml:"-"`
video mediaContext
audio mediaContext
seqNumber uint32
ftyp *mp4.FtypBox
}
func NewMP4Recorder() *MP4Recorder {
r := &MP4Recorder{
InitSegment: mp4.CreateEmptyInit(),
}
r.Moov.Mvhd.NextTrackID = 1
return r
}
func (r *MP4Recorder) Start(streamPath string) (err error) {
r.Record = &RecordPluginConfig.Mp4
r.ID = streamPath + "/mp4"
if _, ok := RecordPluginConfig.recordings.Load(r.ID); ok {
return ErrRecordExist
}
return plugin.Subscribe(streamPath, r)
}
func (r *MP4Recorder) Close() error {
if r.Writer != nil {
if r.video.fragment != nil {
r.video.fragment.Encode(r.Writer)
r.video.fragment = nil
}
if r.audio.fragment != nil {
r.audio.fragment.Encode(r.Writer)
r.audio.fragment = nil
}
r.Closer.Close()
}
return nil
}
func (r *MP4Recorder) OnEvent(event any) {
r.Recorder.OnEvent(event)
if r.newFile {
r.newFile = false
r.Close()
filename := time.Now().Format("2006010215")
var count int64
db := m7sdb.MysqlDB()
db.Model(&MediaRecord{}).Where("file_name = ? and stream_path= ?", filename , r.Stream.Path).Count(&count)
if(count>0){
filename += "_" + strconv.FormatInt(count, 10)
}
fileFullname := filename + r.Ext
if file, err := r.CreateFileFn(filepath.Join(r.Stream.Path, fileFullname), false); err == nil {
r.fileName = fileFullname
r.SetIO(file)
mr := &MediaRecord{
CreateTime: time.Now(),
Status: 1,
StreamPath: r.Stream.Path,
FileName: filename,
Type: 2,
RecordId: r.ID,
}
db.Create(&mr)
r.InitSegment = mp4.CreateEmptyInit()
r.Moov.Mvhd.NextTrackID = 1
if r.VideoReader != nil {
r.OnEvent(r.Video)
}
if r.AudioReader != nil {
r.OnEvent(r.Audio)
}
r.ftyp.Encode(r)
r.Moov.Encode(r)
r.seqNumber = 0
}
}
switch v := event.(type) {
case *track.Video:
moov := r.Moov
trackID := moov.Mvhd.NextTrackID
moov.Mvhd.NextTrackID++
newTrak := mp4.CreateEmptyTrak(trackID, 1000, "video", "chi")
moov.AddChild(newTrak)
moov.Mvex.AddChild(mp4.CreateTrex(trackID))
r.video.trackId = trackID
switch v.CodecID {
case codec.CodecID_H264:
r.ftyp = mp4.NewFtyp("isom", 0x200, []string{
"isom", "iso2", "avc1", "mp41",
})
newTrak.SetAVCDescriptor("avc1", v.ParamaterSets[0:1], v.ParamaterSets[1:2], true)
case codec.CodecID_H265:
r.ftyp = mp4.NewFtyp("isom", 0x200, []string{
"isom", "iso2", "hvc1", "mp41",
})
newTrak.SetHEVCDescriptor("hvc1", v.ParamaterSets[0:1], v.ParamaterSets[1:2], v.ParamaterSets[2:3], true)
}
r.AddTrack(v)
case *track.Audio:
moov := r.Moov
trackID := moov.Mvhd.NextTrackID
moov.Mvhd.NextTrackID++
newTrak := mp4.CreateEmptyTrak(trackID, 1000, "audio", "chi")
moov.AddChild(newTrak)
moov.Mvex.AddChild(mp4.CreateTrex(trackID))
r.audio.trackId = trackID
switch v.CodecID {
case codec.CodecID_AAC:
switch v.AudioObjectType {
case 1:
newTrak.SetAACDescriptor(aac.HEAACv1, int(v.SampleRate))
case 2:
newTrak.SetAACDescriptor(aac.AAClc, int(v.SampleRate))
case 3:
newTrak.SetAACDescriptor(aac.HEAACv2, int(v.SampleRate))
}
case codec.CodecID_PCMA:
stsd := newTrak.Mdia.Minf.Stbl.Stsd
pcma := mp4.CreateAudioSampleEntryBox("pcma",
uint16(v.Channels),
uint16(v.SampleSize), uint16(v.SampleRate), nil)
stsd.AddChild(pcma)
case codec.CodecID_PCMU:
stsd := newTrak.Mdia.Minf.Stbl.Stsd
pcmu := mp4.CreateAudioSampleEntryBox("pcmu",
uint16(v.Channels),
uint16(v.SampleSize), uint16(v.SampleRate), nil)
stsd.AddChild(pcmu)
}
r.AddTrack(v)
case ISubscriber:
if r.ftyp != nil && r.Writer != nil {
r.ftyp.Encode(r)
r.Moov.Encode(r)
go r.start()
}
case AudioFrame:
if r.audio.trackId != 0 {
r.audio.push(r, v.AbsTime, v.DeltaTime, v.AUList.ToBytes(), mp4.SyncSampleFlags)
}
case VideoFrame:
if r.video.trackId != 0 {
flag := mp4.NonSyncSampleFlags
if v.IFrame {
flag = mp4.SyncSampleFlags
}
if data := v.AVCC.ToBytes(); len(data) > 5 {
r.video.push(r, v.AbsTime, v.DeltaTime, data[5:], flag)
}
}
}
}