-
Notifications
You must be signed in to change notification settings - Fork 201
/
publisher.go
149 lines (134 loc) · 3.64 KB
/
publisher.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
package engine
import (
"go.uber.org/zap"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/common"
"m7s.live/engine/v4/config"
"m7s.live/engine/v4/track"
"m7s.live/engine/v4/util"
)
type IPublisher interface {
common.IPuber
GetPublisher() *Publisher
}
var _ IPublisher = (*Publisher)(nil)
type Publisher struct {
IO
Config *config.Publish
common.AudioTrack `json:"-" yaml:"-"`
common.VideoTrack `json:"-" yaml:"-"`
}
func (p *Publisher) Publish(streamPath string, pub common.IPuber) error {
return p.receive(streamPath, pub)
}
func (p *Publisher) GetPublisher() *Publisher {
return p
}
// func (p *Publisher) Stop(reason ...zapcore.Field) {
// p.IO.Stop(reason...)
// p.Stream.Receive(ACTION_PUBLISHCLOSE)
// }
func (p *Publisher) GetAudioTrack() common.AudioTrack {
return p.AudioTrack
}
func (p *Publisher) GetVideoTrack() common.VideoTrack {
return p.VideoTrack
}
func (p *Publisher) GetConfig() *config.Publish {
return p.Config
}
// func (p *Publisher) OnEvent(event any) {
// p.IO.OnEvent(event)
// switch event.(type) {
// case SEclose, SEKick:
// p.AudioTrack = nil
// p.VideoTrack = nil
// }
// }
func (p *Publisher) CreateAudioTrack(codecID codec.AudioCodecID, stuff ...any) common.AudioTrack {
switch codecID {
case codec.CodecID_AAC:
p.AudioTrack = track.NewAAC(p, stuff...)
case codec.CodecID_PCMA:
p.AudioTrack = track.NewG711(p, true, stuff...)
case codec.CodecID_PCMU:
p.AudioTrack = track.NewG711(p, false, stuff...)
case codec.CodecID_OPUS:
p.AudioTrack = track.NewOpus(p, stuff...)
}
return p.AudioTrack
}
func (p *Publisher) CreateVideoTrack(codecID codec.VideoCodecID, stuff ...any) common.VideoTrack {
switch codecID {
case codec.CodecID_H264:
p.VideoTrack = track.NewH264(p, stuff...)
case codec.CodecID_H265:
p.VideoTrack = track.NewH265(p, stuff...)
case codec.CodecID_AV1:
p.VideoTrack = track.NewAV1(p, stuff...)
}
return p.VideoTrack
}
func (p *Publisher) WriteAVCCVideo(ts uint32, frame *util.BLL, pool util.BytesPool) {
if frame.ByteLength < 6 {
return
}
if p.VideoTrack == nil {
b0 := frame.GetByte(0)
// https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp-v1.pdf
if isExtHeader := b0 & 0b1000_0000; isExtHeader != 0 {
fourCC := frame.GetUintN(1, 4)
switch fourCC {
case codec.FourCC_H265_32:
p.VideoTrack = track.NewH265(p, pool)
p.VideoTrack.WriteAVCC(ts, frame)
case codec.FourCC_AV1_32:
p.VideoTrack = track.NewAV1(p, pool)
p.VideoTrack.WriteAVCC(ts, frame)
}
} else {
if frame.GetByte(1) == 0 {
ts = 0
p.CreateVideoTrack(codec.VideoCodecID(b0&0x0F), pool)
if p.VideoTrack == nil {
p.Stream.Error("video codecID not support", zap.Uint8("codeId", uint8(codec.VideoCodecID(b0&0x0F))))
return
}
p.VideoTrack.WriteAVCC(ts, frame)
} else {
p.Stream.Warn("need sequence frame")
}
}
} else {
p.VideoTrack.WriteAVCC(ts, frame)
}
}
func (p *Publisher) WriteAVCCAudio(ts uint32, frame *util.BLL, pool util.BytesPool) {
if frame.ByteLength < 4 {
return
}
if p.AudioTrack == nil {
b0 := frame.GetByte(0)
t := p.CreateAudioTrack(codec.AudioCodecID(b0>>4), pool)
switch a := t.(type) {
case *track.AAC:
if frame.GetByte(1) != 0 {
return
}
a.AVCCHead = []byte{frame.GetByte(0), 1}
a.WriteAVCC(0, frame)
case *track.G711:
a.Audio.SampleRate = uint32(codec.SoundRate[(b0&0x0c)>>2])
if b0&0x02 == 0 {
a.Audio.SampleSize = 8
}
a.Channels = b0&0x01 + 1
a.AVCCHead = []byte{b0}
a.WriteAVCC(ts, frame)
default:
p.Stream.Error("audio codec not support yet", zap.Uint8("codecId", uint8(codec.AudioCodecID(b0>>4))))
}
} else {
p.AudioTrack.WriteAVCC(ts, frame)
}
}