Skip to content

Commit

Permalink
fix rtcp read keep allocating buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Totting committed Sep 17, 2024
1 parent b85c85d commit 294f613
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type internalDataMessage struct {
Data interface{} `json:"data"`
}

type internalDataVAD struct {
type InternalDataVAD struct {
Type string `json:"type"`
Data voiceactivedetector.VoiceActivity `json:"data"`
}
Expand Down Expand Up @@ -849,11 +849,14 @@ func (c *Client) renegotiate() {
c.log.Debug("client: renegotiate")
c.negotiationNeeded.Store(true)

c.muCallback.Lock()
if c.onRenegotiation == nil {
c.muCallback.Unlock()
c.log.Errorf("client: onRenegotiation is not set, can't do renegotiation")

return
}
c.muCallback.Unlock()

if c.isInRemoteNegotiation.Load() {
c.log.Infof("sfu: renegotiation is delayed because the remote client %s is doing negotiation ", c.ID)
Expand Down Expand Up @@ -1035,6 +1038,20 @@ func (c *Client) ClientTracks() map[string]iClientTrack {
return clientTracks
}

func readRTCP(r *webrtc.RTPSender, b []byte) ([]rtcp.Packet, interceptor.Attributes, error) {
i, attributes, err := r.Read(b)
if err != nil {
return nil, nil, err
}

pkts, err := rtcp.Unmarshal(b[:i])
if err != nil {
return nil, nil, err
}

return pkts, attributes, nil
}

// TODO: need to improve and reduce goroutine usage
func (c *Client) enableReportAndStats(rtpSender *webrtc.RTPSender, track iClientTrack) {
ssrc := rtpSender.GetParameters().Encodings[0].SSRC
Expand All @@ -1045,14 +1062,16 @@ func (c *Client) enableReportAndStats(rtpSender *webrtc.RTPSender, track iClient
clientCtx, cancelClientCtx := context.WithCancel(c.context)
defer cancelClientCtx()

b := make([]byte, 1500)

for {
select {
case <-clientCtx.Done():
return
case <-localCtx.Done():
return
default:
rtcpPackets, _, err := rtpSender.ReadRTCP()
rtcpPackets, _, err := readRTCP(rtpSender, b)
if err != nil && (err == io.EOF || err == io.ErrClosedPipe) {
return
}
Expand Down Expand Up @@ -1764,7 +1783,7 @@ func (c *Client) enableSendVADToInternalDataChannel() {
}

func (c *Client) sendVad(dataType string, activity voiceactivedetector.VoiceActivity) {
dataMessage := internalDataVAD{
dataMessage := InternalDataVAD{
Type: dataType,
Data: activity,
}
Expand Down

0 comments on commit 294f613

Please sign in to comment.