mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-22 08:09:22 +08:00
优化代码适配RC1
This commit is contained in:
123
subscriber.go
123
subscriber.go
@@ -7,31 +7,34 @@ import (
|
||||
. "github.com/Monibuca/engine/v4/common"
|
||||
"github.com/Monibuca/engine/v4/config"
|
||||
"github.com/Monibuca/engine/v4/track"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AudioFrame AVFrame[AudioSlice]
|
||||
type VideoFrame AVFrame[NALUSlice]
|
||||
type AudioFrame *AVFrame[AudioSlice]
|
||||
type VideoFrame *AVFrame[NALUSlice]
|
||||
type AudioDeConf DecoderConfiguration[AudioSlice]
|
||||
type VideoDeConf DecoderConfiguration[NALUSlice]
|
||||
type ISubscriber interface {
|
||||
IIO
|
||||
receive(string, any, *config.Subscribe) bool
|
||||
receive(string, ISubscriber, *config.Subscribe) bool
|
||||
config.SubscribeConfig
|
||||
GetSubscriber() *Subscriber
|
||||
Unsubscribe()
|
||||
IsPlaying() bool
|
||||
Play(ISubscriber)
|
||||
Stop()
|
||||
}
|
||||
type TrackPlayer struct {
|
||||
context.Context
|
||||
context.CancelFunc
|
||||
AudioTrack *track.Audio
|
||||
VideoTrack *track.Video
|
||||
vr *AVRing[NALUSlice]
|
||||
ar *AVRing[AudioSlice]
|
||||
startTime time.Time //读到第一个关键帧的时间
|
||||
firstIFrame *VideoFrame //起始关键帧
|
||||
AudioTrack *track.Audio
|
||||
VideoTrack *track.Video
|
||||
vr *AVRing[NALUSlice]
|
||||
ar *AVRing[AudioSlice]
|
||||
}
|
||||
|
||||
// Subscriber 订阅者实体定义
|
||||
type Subscriber struct {
|
||||
IO[config.Subscribe]
|
||||
IO[config.Subscribe, ISubscriber]
|
||||
TrackPlayer
|
||||
}
|
||||
|
||||
@@ -39,25 +42,23 @@ func (p *Subscriber) GetSubscriber() *Subscriber {
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Subscriber) Unsubscribe() {
|
||||
p.bye(p)
|
||||
}
|
||||
|
||||
func (s *Subscriber) GetSubscribeConfig() *config.Subscribe {
|
||||
return s.Config
|
||||
}
|
||||
|
||||
func (s *Subscriber) OnEvent(event any) any {
|
||||
s.IO.OnEvent(event)
|
||||
func (s *Subscriber) OnEvent(event any) {
|
||||
switch v := event.(type) {
|
||||
case TrackRemoved:
|
||||
if a, ok := v.(*track.Audio); ok && a == s.AudioTrack {
|
||||
if a, ok := v.Track.(*track.Audio); ok && a == s.AudioTrack {
|
||||
s.ar = nil
|
||||
} else if v, ok := v.(*track.Video); ok && v == s.VideoTrack {
|
||||
} else if v, ok := v.Track.(*track.Video); ok && v == s.VideoTrack {
|
||||
s.vr = nil
|
||||
}
|
||||
case Track: //默认接受所有track
|
||||
s.AddTrack(v)
|
||||
default:
|
||||
s.IO.OnEvent(event)
|
||||
}
|
||||
return event
|
||||
}
|
||||
|
||||
func (s *Subscriber) AddTrack(t Track) bool {
|
||||
@@ -68,7 +69,7 @@ func (s *Subscriber) AddTrack(t Track) bool {
|
||||
}
|
||||
s.VideoTrack = v
|
||||
s.vr = v.ReadRing()
|
||||
s.firstIFrame = (*VideoFrame)(s.vr.Read(s.TrackPlayer))
|
||||
s.Info("track+1", zap.String("name", v.Name))
|
||||
return true
|
||||
}
|
||||
} else if a, ok := t.(*track.Audio); ok {
|
||||
@@ -78,6 +79,7 @@ func (s *Subscriber) AddTrack(t Track) bool {
|
||||
}
|
||||
s.AudioTrack = a
|
||||
s.ar = a.ReadRing()
|
||||
s.Info("track+1", zap.String("name", a.Name))
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -86,47 +88,81 @@ func (s *Subscriber) AddTrack(t Track) bool {
|
||||
}
|
||||
|
||||
func (s *Subscriber) IsPlaying() bool {
|
||||
return s.TrackPlayer.Err() == nil && (s.AudioTrack != nil || s.VideoTrack != nil)
|
||||
return s.TrackPlayer.Context != nil && s.TrackPlayer.Err() == nil
|
||||
}
|
||||
|
||||
func (s *Subscriber) Stop() {
|
||||
if s.IsPlaying() {
|
||||
s.TrackPlayer.CancelFunc()
|
||||
}
|
||||
}
|
||||
|
||||
//Play 开始播放
|
||||
func (s *Subscriber) Play() {
|
||||
func (s *Subscriber) Play(spesic ISubscriber) {
|
||||
s.Info("play")
|
||||
var t time.Time
|
||||
for s.TrackPlayer.Err() == nil {
|
||||
var startTime time.Time //读到第一个关键帧的时间
|
||||
var firstIFrame VideoFrame //起始关键帧
|
||||
var audioSent bool //音频是否发送过
|
||||
s.TrackPlayer.Context, s.TrackPlayer.CancelFunc = context.WithCancel(s.IO)
|
||||
ctx := s.TrackPlayer.Context
|
||||
defer s.Info("stop")
|
||||
for ctx.Err() == nil {
|
||||
if s.vr != nil {
|
||||
if startTime.IsZero() {
|
||||
startTime = time.Now()
|
||||
firstIFrame = (VideoFrame)(s.vr.Read(ctx))
|
||||
s.Debug("firstIFrame", zap.Uint32("seq", firstIFrame.Sequence))
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
spesic.OnEvent(VideoDeConf(s.VideoTrack.DecoderConfiguration))
|
||||
}
|
||||
for {
|
||||
var vp VideoFrame
|
||||
// 如果进入正常模式
|
||||
if s.firstIFrame == nil {
|
||||
vp := s.vr.Read(s.TrackPlayer)
|
||||
s.OnEvent((*VideoFrame)(vp))
|
||||
if firstIFrame == nil {
|
||||
vp = VideoFrame(s.vr.Read(ctx))
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
spesic.OnEvent(vp)
|
||||
s.vr.MoveNext()
|
||||
if vp.Timestamp.After(t) {
|
||||
t = vp.Timestamp
|
||||
break
|
||||
}
|
||||
} else {
|
||||
if s.startTime.IsZero() {
|
||||
s.startTime = time.Now()
|
||||
}
|
||||
if &s.VideoTrack.IDRing.Value != (*AVFrame[NALUSlice])(s.firstIFrame) {
|
||||
s.firstIFrame = nil
|
||||
if s.VideoTrack.IDRing.Value.Sequence != firstIFrame.Sequence {
|
||||
firstIFrame = nil
|
||||
s.vr = s.VideoTrack.ReadRing()
|
||||
s.Debug("skip to latest key frame")
|
||||
continue
|
||||
} else {
|
||||
vp := s.vr.Read(s.TrackPlayer)
|
||||
s.OnEvent((*VideoFrame)(vp))
|
||||
fast := time.Duration(vp.AbsTime-s.firstIFrame.AbsTime)*time.Millisecond - time.Since(s.startTime)
|
||||
if fast > 0 {
|
||||
vp = VideoFrame(s.vr.Read(ctx))
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
spesic.OnEvent(vp)
|
||||
if fast := time.Duration(vp.AbsTime-firstIFrame.AbsTime)*time.Millisecond - time.Since(startTime); fast > 0 {
|
||||
time.Sleep(fast)
|
||||
}
|
||||
s.vr.MoveNext()
|
||||
}
|
||||
}
|
||||
if vp.Timestamp.After(t) {
|
||||
t = vp.Timestamp
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if s.ar != nil {
|
||||
if s.ar != nil && firstIFrame == nil {
|
||||
if !audioSent {
|
||||
spesic.OnEvent(AudioDeConf(s.AudioTrack.DecoderConfiguration))
|
||||
audioSent = true
|
||||
}
|
||||
for {
|
||||
ap := s.ar.Read(s.TrackPlayer)
|
||||
s.OnEvent((*AudioFrame)(ap))
|
||||
ap := AudioFrame(s.ar.Read(ctx))
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
spesic.OnEvent(ap)
|
||||
s.ar.MoveNext()
|
||||
if ap.Timestamp.After(t) {
|
||||
t = ap.Timestamp
|
||||
@@ -135,7 +171,6 @@ func (s *Subscriber) Play() {
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type PushEvent int
|
||||
|
||||
Reference in New Issue
Block a user