修改命名

This commit is contained in:
unknown
2020-05-01 11:41:16 +08:00
parent 3f1ce8466e
commit 6487eaddbf
8 changed files with 123 additions and 148 deletions

View File

@@ -9,11 +9,6 @@ import (
"github.com/pkg/errors"
)
// Subscriber 订阅者
// type Subscriber interface {
// Send(*avformat.SendPacket) error
// }
// SubscriberInfo 订阅者可序列化信息,用于控制台输出
type SubscriberInfo struct {
ID string
@@ -25,51 +20,50 @@ type SubscriberInfo struct {
SubscribeTime time.Time
}
// OutputStream 订阅者实体定义
type OutputStream struct {
// Subscriber 订阅者实体定义
type Subscriber struct {
context.Context
*Room
*Stream
SubscriberInfo
SendHandler func(*avformat.SendPacket) error
Cancel context.CancelFunc
Sign string
OffsetTime uint32
OnData func(*avformat.SendPacket) error
Cancel context.CancelFunc
Sign string
OffsetTime uint32
}
// IsClosed 检查订阅者是否已经关闭
func (s *OutputStream) IsClosed() bool {
func (s *Subscriber) IsClosed() bool {
return s.Context != nil && s.Err() != nil
}
// Close 关闭订阅者
func (s *OutputStream) Close() {
func (s *Subscriber) Close() {
if s.Cancel != nil {
s.Cancel()
}
}
//Play 开始订阅
func (s *OutputStream) Play(streamPath string) (err error) {
if !config.EnableWaitRoom {
if _, ok := AllRoom.Load(streamPath); !ok {
//Subscribe 开始订阅
func (s *Subscriber) Subscribe(streamPath string) (err error) {
if !config.EnableWaitStream {
if _, ok := streamCollection.Load(streamPath); !ok {
return errors.New(fmt.Sprintf("Stream not found:%s", streamPath))
}
}
AllRoom.Get(streamPath).Subscribe(s)
GetStream(streamPath).Subscribe(s)
defer s.UnSubscribe(s)
//加锁解锁的目的是等待发布者首屏数据,如果发布者尚为发布,则会等待,否则就会往下执行
s.WaitingMutex.RLock()
s.WaitingMutex.RUnlock()
sendPacket := avformat.NewSendPacket(s.VideoTag, 0)
defer sendPacket.Recycle()
s.SendHandler(sendPacket)
s.OnData(sendPacket)
packet := s.FirstScreen.Clone()
startTime := packet.Timestamp
packet.RLock()
sendPacket.AVPacket = packet.AVPacket
s.SendHandler(sendPacket)
packet.RUnlock()
packet.GoNext()
s.OnData(sendPacket)
packet.NextR()
atsent := false
dropping := false
droped := 0
@@ -84,19 +78,18 @@ func (s *OutputStream) Play(streamPath string) (err error) {
if packet.Type == avformat.FLV_TAG_TYPE_AUDIO && !atsent {
sendPacket.AVPacket = s.AudioTag
sendPacket.Timestamp = 0
s.SendHandler(sendPacket)
s.OnData(sendPacket)
atsent = true
}
sendPacket.AVPacket = packet.AVPacket
sendPacket.Timestamp = packet.Timestamp - startTime
s.SendHandler(sendPacket)
s.OnData(sendPacket)
if s.checkDrop(packet) {
dropping = true
droped = 0
}
packet.RUnlock()
packet.GoNext()
} else if packet.AVPacket.IsKeyFrame() {
packet.NextR()
} else if packet.IsKeyFrame {
//遇到关键帧则退出丢帧
dropping = false
//fmt.Println("drop package ", droped)
@@ -104,8 +97,7 @@ func (s *OutputStream) Play(streamPath string) (err error) {
packet.RUnlock()
} else {
droped++
packet.RUnlock()
packet.GoNext()
packet.NextR()
}
}
}