Skip to content

Commit

Permalink
Optimize getOrCreateReadStream
Browse files Browse the repository at this point in the history
The allocation of a readStream every time was causing a ton of
allocations for no reason. Passing in a function makes it faster.
  • Loading branch information
kixelated committed Mar 5, 2019
1 parent c22c5d8 commit 678a09e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
20 changes: 12 additions & 8 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type SessionKeys struct {
RemoteMasterSalt []byte
}

func (s *session) getOrCreateReadStream(ssrc uint32, child streamSession, proto readStream) (readStream, bool) {
func (s *session) getOrCreateReadStream(ssrc uint32, child streamSession, proto func() readStream) (readStream, bool) {
s.readStreamsLock.Lock()
defer s.readStreamsLock.Unlock()

Expand All @@ -54,15 +54,19 @@ func (s *session) getOrCreateReadStream(ssrc uint32, child streamSession, proto
}

r, ok := s.readStreams[ssrc]
if !ok {
if err := proto.init(child, ssrc); err != nil {
return nil, false
}
if ok {
return r, false
}

// Create the readStream.
r = proto()

s.readStreams[ssrc] = proto
return proto, true
if err := r.init(child, ssrc); err != nil {
return nil, false
}
return r, false

s.readStreams[ssrc] = r
return r, true
}

func (s *session) removeReadStream(ssrc uint32) {
Expand Down
4 changes: 2 additions & 2 deletions session_srtcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *SessionSRTCP) OpenWriteStream() (*WriteStreamSRTCP, error) {
// OpenReadStream opens a read stream for the given SSRC, it can be used
// if you want a certain SSRC, but don't want to wait for AcceptStream
func (s *SessionSRTCP) OpenReadStream(SSRC uint32) (*ReadStreamSRTCP, error) {
r, _ := s.session.getOrCreateReadStream(SSRC, s, &ReadStreamSRTCP{})
r, _ := s.session.getOrCreateReadStream(SSRC, s, newReadStreamSRTCP)

if readStream, ok := r.(*ReadStreamSRTCP); ok {
return readStream, nil
Expand Down Expand Up @@ -119,7 +119,7 @@ func (s *SessionSRTCP) decrypt(buf []byte) error {
}

for _, ssrc := range report.DestinationSSRC() {
r, isNew := s.session.getOrCreateReadStream(ssrc, s, &ReadStreamSRTCP{})
r, isNew := s.session.getOrCreateReadStream(ssrc, s, newReadStreamSRTCP)
if r == nil {
return nil // Session has been closed
} else if isNew {
Expand Down
4 changes: 2 additions & 2 deletions session_srtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *SessionSRTP) OpenWriteStream() (*WriteStreamSRTP, error) {
// OpenReadStream opens a read stream for the given SSRC, it can be used
// if you want a certain SSRC, but don't want to wait for AcceptStream
func (s *SessionSRTP) OpenReadStream(SSRC uint32) (*ReadStreamSRTP, error) {
r, _ := s.session.getOrCreateReadStream(SSRC, s, &ReadStreamSRTP{})
r, _ := s.session.getOrCreateReadStream(SSRC, s, newReadStreamSRTP)

if readStream, ok := r.(*ReadStreamSRTP); ok {
return readStream, nil
Expand Down Expand Up @@ -104,7 +104,7 @@ func (s *SessionSRTP) decrypt(buf []byte) error {
return err
}

r, isNew := s.session.getOrCreateReadStream(h.SSRC, s, &ReadStreamSRTP{})
r, isNew := s.session.getOrCreateReadStream(h.SSRC, s, newReadStreamSRTP)
if r == nil {
return nil // Session has been closed
} else if isNew {
Expand Down
5 changes: 5 additions & 0 deletions stream_srtcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type ReadStreamSRTCP struct {
readRetCh chan readResultSRTCP
}

// Used by getOrCreateReadStream
func newReadStreamSRTCP() readStream {
return &ReadStreamSRTCP{}
}

// ReadRTCP reads and decrypts full RTCP packet and its header from the nextConn
func (r *ReadStreamSRTCP) ReadRTCP(payload []byte) (int, *rtcp.Header, error) {
select {
Expand Down
5 changes: 5 additions & 0 deletions stream_srtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type ReadStreamSRTP struct {
readRetCh chan readResultSRTP
}

// Used by getOrCreateReadStream
func newReadStreamSRTP() readStream {
return &ReadStreamSRTP{}
}

// ReadRTP reads and decrypts full RTP packet and its header from the nextConn
func (r *ReadStreamSRTP) ReadRTP(payload []byte) (int, *rtp.Header, error) {
select {
Expand Down

0 comments on commit 678a09e

Please sign in to comment.