implement rtcp sender reports

This commit is contained in:
aler9
2020-11-21 23:54:32 +01:00
parent a21bced1dd
commit 2eebf48fca
8 changed files with 233 additions and 42 deletions

View File

@@ -78,8 +78,8 @@ func (c *ConnClient) backgroundRecordUDP() {
defer close(c.backgroundDone)
defer func() {
c.publishMutex.Lock()
defer c.publishMutex.Unlock()
c.publishWriteMutex.Lock()
defer c.publishWriteMutex.Unlock()
c.publishOpen = false
}()
@@ -98,6 +98,9 @@ func (c *ConnClient) backgroundRecordUDP() {
}
}()
reportTicker := time.NewTicker(clientSenderReportPeriod)
defer reportTicker.Stop()
select {
case <-c.backgroundTerminate:
c.nconn.SetReadDeadline(time.Now())
@@ -105,6 +108,17 @@ func (c *ConnClient) backgroundRecordUDP() {
c.publishError = fmt.Errorf("terminated")
return
case <-reportTicker.C:
c.publishWriteMutex.Lock()
now := time.Now()
for trackId := range c.rtcpSenders {
report := c.rtcpSenders[trackId].Report(now)
if report != nil {
c.udpRtcpListeners[trackId].write(report)
}
}
c.publishWriteMutex.Unlock()
case err := <-readerDone:
c.publishError = err
return
@@ -115,24 +129,53 @@ func (c *ConnClient) backgroundRecordTCP() {
defer close(c.backgroundDone)
defer func() {
c.publishMutex.Lock()
defer c.publishMutex.Unlock()
c.publishWriteMutex.Lock()
defer c.publishWriteMutex.Unlock()
c.publishOpen = false
}()
<-c.backgroundTerminate
reportTicker := time.NewTicker(clientSenderReportPeriod)
defer reportTicker.Stop()
for {
select {
case <-c.backgroundTerminate:
return
case <-reportTicker.C:
c.publishWriteMutex.Lock()
now := time.Now()
for trackId := range c.rtcpSenders {
report := c.rtcpSenders[trackId].Report(now)
if report != nil {
c.nconn.SetWriteDeadline(time.Now().Add(c.d.WriteTimeout))
frame := base.InterleavedFrame{
TrackId: trackId,
StreamType: StreamTypeRtcp,
Content: report,
}
frame.Write(c.bw)
}
}
c.publishWriteMutex.Unlock()
}
}
}
// WriteFrame writes a frame.
// This can be called only after Record().
func (c *ConnClient) WriteFrame(trackId int, streamType StreamType, content []byte) error {
c.publishMutex.RLock()
defer c.publishMutex.RUnlock()
c.publishWriteMutex.RLock()
defer c.publishWriteMutex.RUnlock()
if !c.publishOpen {
return c.publishError
}
now := time.Now()
c.rtcpSenders[trackId].OnFrame(now, streamType, content)
if *c.streamProtocol == StreamProtocolUDP {
if streamType == StreamTypeRtp {
return c.udpRtpListeners[trackId].write(content)
@@ -140,7 +183,7 @@ func (c *ConnClient) WriteFrame(trackId int, streamType StreamType, content []by
return c.udpRtcpListeners[trackId].write(content)
}
c.nconn.SetWriteDeadline(time.Now().Add(c.d.WriteTimeout))
c.nconn.SetWriteDeadline(now.Add(c.d.WriteTimeout))
frame := base.InterleavedFrame{
TrackId: trackId,
StreamType: streamType,