mirror of
https://github.com/aler9/gortsplib
synced 2025-11-02 11:24:05 +08:00
implement rtcp sender reports
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user