feat: add CreateAudioTrack and CreateVideoTrack to Publisher

This commit is contained in:
langhuihui
2024-02-27 20:08:47 +08:00
parent b58e926819
commit 348f52afb5
2 changed files with 53 additions and 33 deletions

View File

@@ -47,6 +47,8 @@ reamins: 剩余
"http handle added to engine": http处理器已添加到引擎
"plugin disabled": 插件已禁用
"audio codec not support yet": 音频编码暂不支持
"attach video track": 附加视频轨道
"attach audio track": 附加音频轨道
"video track attached": 视频轨道已附加
"audio track attached": 音频轨道已附加
"data track attached": 数据轨道已附加
@@ -56,7 +58,8 @@ reamins: 剩余
"disabled by env": 被环境变量禁用
"event cost too much time": 事件处理耗时过长
"run timeout": 流事件处理耗时过长
"cost": 耗时
cost: 耗时
jump: 跳过
firstTs: 第一帧时间戳
firstSeq: 第一帧序列号
skipSeq: 跳过序列号

View File

@@ -31,28 +31,57 @@ 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) 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) 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 {
@@ -74,13 +103,9 @@ func (p *Publisher) WriteAVCCVideo(ts uint32, frame *util.BLL, pool util.BytesPo
} else {
if frame.GetByte(1) == 0 {
ts = 0
switch codecID := codec.VideoCodecID(b0 & 0x0F); codecID {
case codec.CodecID_H264:
p.VideoTrack = track.NewH264(p, pool)
case codec.CodecID_H265:
p.VideoTrack = track.NewH265(p, pool)
default:
p.Stream.Error("video codecID not support", zap.Uint8("codeId", uint8(codecID)))
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)
@@ -99,23 +124,15 @@ func (p *Publisher) WriteAVCCAudio(ts uint32, frame *util.BLL, pool util.BytesPo
}
if p.AudioTrack == nil {
b0 := frame.GetByte(0)
switch codecID := codec.AudioCodecID(b0 >> 4); codecID {
case codec.CodecID_AAC:
t := p.CreateAudioTrack(codec.AudioCodecID(b0>>4), pool)
switch a := t.(type) {
case *track.AAC:
if frame.GetByte(1) != 0 {
return
}
a := track.NewAAC(p, pool)
p.AudioTrack = a
a.AVCCHead = []byte{frame.GetByte(0), 1}
a.WriteAVCC(0, frame)
case codec.CodecID_PCMA,
codec.CodecID_PCMU:
alaw := true
if codecID == codec.CodecID_PCMU {
alaw = false
}
a := track.NewG711(p, alaw, pool)
p.AudioTrack = a
case *track.G711:
a.Audio.SampleRate = uint32(codec.SoundRate[(b0&0x0c)>>2])
if b0&0x02 == 0 {
a.Audio.SampleSize = 8
@@ -124,7 +141,7 @@ func (p *Publisher) WriteAVCCAudio(ts uint32, frame *util.BLL, pool util.BytesPo
a.AVCCHead = []byte{b0}
a.WriteAVCC(ts, frame)
default:
p.Stream.Error("audio codec not support yet", zap.Uint8("codecId", uint8(codecID)))
p.Stream.Error("audio codec not support yet", zap.Uint8("codecId", uint8(codec.AudioCodecID(b0>>4))))
}
} else {
p.AudioTrack.WriteAVCC(ts, frame)