@@ -29,6 +29,8 @@ type Subscriber struct {
2929 Cancel context.CancelFunc
3030 Sign string
3131 OffsetTime uint32
32+ startTime uint32
33+ avformat.SendPacket
3234}
3335
3436// IsClosed 检查订阅者是否已经关闭
@@ -52,55 +54,45 @@ func (s *Subscriber) Subscribe(streamPath string) (err error) {
5254 }
5355 GetStream (streamPath ).Subscribe (s )
5456 defer s .UnSubscribe (s )
55- //加锁解锁的目的是等待发布者首屏数据,如果发布者尚为发布,则会等待,否则就会往下执行
56- s .WaitingMutex .RLock ()
57- s .WaitingMutex .RUnlock ()
58- sendPacket := avformat .NewSendPacket (s .VideoTag , 0 )
59- defer sendPacket .Recycle ()
60- s .OnData (sendPacket )
57+ //等待发布者首屏数据,如果发布者尚为发布,则会等待,否则就会往下执行
58+ s .WaitPub .Wait ()
59+ s .sendAv (s .VideoTag , 0 )
6160 packet := s .FirstScreen .Clone ()
62- startTime := packet .Timestamp
63- packet .RLock ()
64- sendPacket .AVPacket = & packet .AVPacket
65- s .OnData (sendPacket )
66- packet .NextR ()
67- atsent := false
68- dropping := false
69- droped := 0
70- for {
71- select {
72- case <- s .Done ():
73- return s .Err ()
74- default :
75- s .TotalPacket ++
76- packet .RLock ()
77- if ! dropping {
78- if packet .Type == avformat .FLV_TAG_TYPE_AUDIO && ! atsent {
79- sendPacket .AVPacket = s .AudioTag
80- sendPacket .Timestamp = 0
81- s .OnData (sendPacket )
82- atsent = true
83- }
84- sendPacket .AVPacket = & packet .AVPacket
85- sendPacket .Timestamp = packet .Timestamp - startTime
86- s .OnData (sendPacket )
87- if s .checkDrop (packet ) {
88- dropping = true
89- droped = 0
90- }
91- packet .NextR ()
92- } else if packet .IsKeyFrame {
93- //遇到关键帧则退出丢帧
94- dropping = false
95- //fmt.Println("drop package ", droped)
96- s .TotalDrop += droped
97- packet .RUnlock ()
98- } else {
99- droped ++
100- packet .NextR ()
61+ s .startTime = packet .Timestamp
62+ s .send (packet )
63+ packet .GoNext ()
64+ for atsent , dropping , droped := false , false , 0 ; s .Err () == nil ; packet .GoNext () {
65+ s .TotalPacket ++
66+ if ! dropping {
67+ if packet .Type == avformat .FLV_TAG_TYPE_AUDIO && ! atsent {
68+ s .sendAv (s .AudioTag , 0 )
69+ atsent = true
10170 }
71+ s .send (packet )
72+ if s .checkDrop (packet ) {
73+ dropping = true
74+ droped = 0
75+ }
76+ } else if packet .IsKeyFrame {
77+ //遇到关键帧则退出丢帧
78+ dropping = false
79+ //fmt.Println("drop package ", droped)
80+ s .TotalDrop += droped
81+ s .send (packet )
82+ } else {
83+ droped ++
10284 }
10385 }
86+ return s .Err ()
87+ }
88+ func (s * Subscriber ) sendAv (packet * avformat.AVPacket , t uint32 ) {
89+ s .AVPacket = packet
90+ s .Timestamp = t
91+ s .OnData (& s .SendPacket )
92+ }
93+ func (s * Subscriber ) send (packet * Ring ) {
94+ packet .Wait ()
95+ s .sendAv (& packet .AVPacket , packet .Timestamp - s .startTime )
10496}
10597func (s * Subscriber ) checkDrop (packet * Ring ) bool {
10698 pIndex := s .AVRing .Index
0 commit comments