-
Notifications
You must be signed in to change notification settings - Fork 37
/
hls.go
146 lines (137 loc) · 3.29 KB
/
hls.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
package record
import (
"fmt"
"math"
"path/filepath"
"time"
"go.uber.org/zap"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/codec/mpegts"
"m7s.live/engine/v4/util"
"m7s.live/plugin/hls/v4"
)
type HLSRecorder struct {
playlist hls.Playlist
tsStartTime uint32
tsLastTime uint32
tsTitle string
video_cc, audio_cc byte
Recorder
MemoryTs
}
func NewHLSRecorder() (r *HLSRecorder) {
r = &HLSRecorder{}
r.Record = RecordPluginConfig.Hls
return r
}
func (h *HLSRecorder) Start(streamPath string) error {
h.ID = streamPath + "/hls"
return h.start(h, streamPath, SUBTYPE_RAW)
}
func (h *HLSRecorder) StartWithFileName(streamPath string, fileName string) error {
h.ID = streamPath + "/hls/" + fileName
return h.start(h, streamPath, SUBTYPE_RAW)
}
func (r *HLSRecorder) Close() (err error) {
if r.File != nil {
inf := hls.PlaylistInf{
Duration: float64(r.tsLastTime-r.tsStartTime) / 1000,
Title: r.tsTitle,
}
r.playlist.WriteInf(inf)
r.tsStartTime = 0
err = r.File.Close()
}
return
}
func (h *HLSRecorder) OnEvent(event any) {
var err error
defer func() {
if err != nil {
h.Stop(zap.Error(err))
}
}()
switch v := event.(type) {
case *HLSRecorder:
h.BytesPool = make(util.BytesPool, 17)
if h.Writer, err = h.Recorder.CreateFile(); err != nil {
return
}
h.SetIO(h.Writer)
h.playlist = hls.Playlist{
Writer: h.Writer,
Version: 3,
Sequence: 0,
Targetduration: int(math.Ceil(h.Fragment.Seconds())),
}
if err = h.playlist.Init(); err != nil {
return
}
if h.File, err = h.CreateFile(); err != nil {
return
}
case AudioFrame:
if h.tsStartTime == 0 {
h.tsStartTime = v.AbsTime
}
h.tsLastTime = v.AbsTime
h.Recorder.OnEvent(event)
pes := &mpegts.MpegtsPESFrame{
Pid: mpegts.PID_AUDIO,
IsKeyFrame: false,
ContinuityCounter: h.audio_cc,
ProgramClockReferenceBase: uint64(v.DTS),
}
h.WriteAudioFrame(v, pes)
_, err = h.BLL.WriteTo(h.File)
h.Recycle()
h.Clear()
h.audio_cc = pes.ContinuityCounter
case VideoFrame:
if h.tsStartTime == 0 {
h.tsStartTime = v.AbsTime
}
h.tsLastTime = v.AbsTime
h.Recorder.OnEvent(event)
pes := &mpegts.MpegtsPESFrame{
Pid: mpegts.PID_VIDEO,
IsKeyFrame: v.IFrame,
ContinuityCounter: h.video_cc,
ProgramClockReferenceBase: uint64(v.DTS),
}
if err = h.WriteVideoFrame(v, pes); err != nil {
return
}
_, err = h.BLL.WriteTo(h.File)
h.Recycle()
h.Clear()
h.video_cc = pes.ContinuityCounter
default:
h.Recorder.OnEvent(v)
}
}
// 创建一个新的ts文件
func (h *HLSRecorder) CreateFile() (fw FileWr, err error) {
h.tsTitle = fmt.Sprintf("%d.ts", time.Now().Unix())
filePath := filepath.Join(h.Stream.Path, h.tsTitle)
fw, err = h.CreateFileFn(filePath, false)
if err != nil {
h.Error("create file", zap.String("path", filePath), zap.Error(err))
return
}
h.Info("create file", zap.String("path", filePath))
if err = mpegts.WriteDefaultPATPacket(fw); err != nil {
return
}
var vcodec codec.VideoCodecID = 0
var acodec codec.AudioCodecID = 0
if h.Video != nil {
vcodec = h.Video.CodecID
}
if h.Audio != nil {
acodec = h.Audio.CodecID
}
mpegts.WritePMTPacket(fw, vcodec, acodec)
return
}