-
Notifications
You must be signed in to change notification settings - Fork 37
/
mp4.go
136 lines (125 loc) · 3.59 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
package record
import (
"net"
"os"
"path/filepath"
"time"
"github.com/yapingcat/gomedia/go-mp4"
"go.uber.org/zap"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/util"
)
type MP4Recorder struct {
Recorder
*mp4.Movmuxer `json:"-" yaml:"-"`
videoId uint32
audioId uint32
}
func (r *MP4Recorder) SetId(string) {
//TODO implement me
panic("implement me")
}
func (r *MP4Recorder) GetRecordModeString(mode RecordMode) string {
//TODO implement me
panic("implement me")
}
func (r *MP4Recorder) StartWithDynamicTimeout(streamPath, fileName string, timeout time.Duration) error {
//TODO implement me
panic("implement me")
}
func (r *MP4Recorder) UpdateTimeout(timeout time.Duration) {
//TODO implement me
panic("implement me")
}
func NewMP4Recorder() *MP4Recorder {
r := &MP4Recorder{}
r.Record = RecordPluginConfig.Mp4
return r
}
func (r *MP4Recorder) Start(streamPath string) (err error) {
r.ID = streamPath + "/mp4"
return r.start(r, streamPath, SUBTYPE_RAW)
}
func (r *MP4Recorder) StartWithFileName(streamPath string, fileName string) error {
r.ID = streamPath + "/mp4/" + fileName
return r.start(r, streamPath, SUBTYPE_RAW)
}
func (r *MP4Recorder) Close() (err error) {
if r.File != nil {
if !isWrifeFrame {
fullPath := filepath.Join(r.Path, "/", r.filePath)
go func(f FileWr) {
err = r.File.Close()
err = os.Remove(fullPath)
if err != nil {
r.Info("未写入帧,文件为空,直接删除,删除结果为=======" + err.Error())
}
}(r.File)
} else {
go func(f FileWr) {
err = r.Movmuxer.WriteTrailer()
if err != nil {
r.Error("mp4 write trailer", zap.Error(err))
} else {
// _, err = r.file.Write(r.cache.buf)
r.Info("mp4 write trailer", zap.Error(err))
}
err = f.Close()
}(r.File)
}
}
isWrifeFrame = false
return
}
func (r *MP4Recorder) setTracks() {
if r.Audio != nil {
switch r.Audio.CodecID {
case codec.CodecID_AAC:
r.audioId = r.AddAudioTrack(mp4.MP4_CODEC_AAC, mp4.WithExtraData(r.Audio.SequenceHead[2:]))
case codec.CodecID_PCMA:
r.audioId = r.AddAudioTrack(mp4.MP4_CODEC_G711A, mp4.WithAudioSampleRate(r.Audio.SampleRate), mp4.WithAudioChannelCount(r.Audio.Channels), mp4.WithAudioSampleBits(r.Audio.SampleSize))
case codec.CodecID_PCMU:
r.audioId = r.AddAudioTrack(mp4.MP4_CODEC_G711U, mp4.WithAudioSampleRate(r.Audio.SampleRate), mp4.WithAudioChannelCount(r.Audio.Channels), mp4.WithAudioSampleBits(r.Audio.SampleSize))
}
}
if r.Video != nil {
switch r.Video.CodecID {
case codec.CodecID_H264:
r.videoId = r.AddVideoTrack(mp4.MP4_CODEC_H264, mp4.WithExtraData(r.Video.SequenceHead[5:]))
case codec.CodecID_H265:
r.videoId = r.AddVideoTrack(mp4.MP4_CODEC_H265, mp4.WithExtraData(r.Video.SequenceHead[5:]))
}
}
}
func (r *MP4Recorder) OnEvent(event any) {
var err error
r.Recorder.OnEvent(event)
switch v := event.(type) {
case FileWr:
r.Movmuxer, err = mp4.CreateMp4Muxer(v)
if err != nil {
r.Error("mp4 create muxer", zap.Error(err))
} else {
r.setTracks()
}
case AudioFrame:
if r.audioId != 0 {
var audioData []byte
if v.ADTS == nil {
audioData = v.AUList.ToBytes()
} else {
audioData = util.ConcatBuffers(append(net.Buffers{v.ADTS.Value}, v.AUList.ToBuffers()...))
}
if err = r.Write(r.audioId, audioData, uint64(v.AbsTime+(v.PTS-v.DTS)/90), uint64(v.AbsTime)); err != nil {
r.Stop(zap.Error(err))
}
}
case VideoFrame:
if r.videoId != 0 {
if err = r.Write(r.videoId, util.ConcatBuffers(v.GetAnnexB()), uint64(v.AbsTime+(v.PTS-v.DTS)/90), uint64(v.AbsTime)); err != nil {
r.Stop(zap.Error(err))
}
}
}
}