Commits

Liam Staskawicz committed 876c1ef

session: collapse memberTable into session for now

  • Participants
  • Parent commits d76a7d7

Comments (0)

Files changed (3)

 func (s *Session) rtcpInterval() int64 {
 	// as implemented in Appendix A.7
 
-	// XXX: locking around s.state access
-	rtcpBw := s.state.rtcpBw
-	senders := float64(s.state.senders)
-	initial := s.state.initial
+	// XXX: locking around state access
+	rtcpBw := s.rtcpBw
+	senders := float64(s.senders)
+	initial := s.initial
 	rtcpMinTime := rtcpIntervalMinTime
-	n := float64(len(s.members.m))
+	n := float64(len(s.members))
 
 	// Very first call at application start-up uses half the min
 	// delay for quicker notification while still allowing some time
 	// the number of senders is large enough that their share is
 	// more than that fraction.
 	if senders <= n*rtcpIntervalSenderBwFrac {
-		if s.state.weSent {
+		if s.weSent {
 			rtcpBw *= rtcpIntervalSenderBwFrac
 			n = senders
 		} else {
 	// meet the bandwidth target, with a minimum enforced.  In that
 	// time interval we send one report so this time is also our
 	// average time between reports.
-	interval := s.state.avgRtcpSize * n / rtcpBw
+	interval := s.avgRtcpSize * n / rtcpBw
 	if math.IsNaN(interval) {
 		panic("rtcp interval is NaN")
 	}
 
 import (
 	"log"
+	"sync"
 )
 
-type sessionState struct {
-	tp          int     // the last time an RTCP packet was transmitted
-	tc          int     // the current time
-	tn          int     // the next scheduled transmission time of an RTCP packet
-	pmembers    int     // the estimated number of session members at the time tn was last recomputed
-	members     int     // the most current estimate for the number of session members
-	senders     int     // the most current estimate for the number of senders in the session
-	rtcpBw      float64 // The target RTCP bandwidth
-	weSent      bool    // true if the application has sent data since the 2nd previous RTCP report was transmitted
-	avgRtcpSize float64 // average compound RTCP packet size, in octets, over all RTCP packets sent and received
-	initial     bool    // Flag that is true if the application has not yet sent an RTCP packet.
-}
-
 type Session struct {
 	RtpHandler       RtpHandler
 	RtcpHandler      RtcpHandler
 	CtrlEventHandler CtrlEventHandler
 	Transport        Transport
-	members          *memberTable
-	state            sessionState
+
+	sync.RWMutex // locks access to state below
+	members      map[uint32]*Source
+	tp           int     // the last time an RTCP packet was transmitted
+	tc           int     // the current time
+	tn           int     // the next scheduled transmission time of an RTCP packet
+	pmembers     int     // the estimated number of session members at the time tn was last recomputed
+	senders      int     // the most current estimate for the number of senders in the session
+	rtcpBw       float64 // The target RTCP bandwidth
+	weSent       bool    // true if the application has sent data since the 2nd previous RTCP report was transmitted
+	avgRtcpSize  float64 // average compound RTCP packet size, in octets, over all RTCP packets sent and received
+	initial      bool    // Flag that is true if the application has not yet sent an RTCP packet.
 }
 
 type CtrlEvent struct {
 func NewSession(t Transport) *Session {
 	s := &Session{
 		Transport: t,
-		members:   newMemberTable(),
+		members:   make(map[uint32]*Source),
 	}
 	t.SetHandler(s)
 	return s
 	// compute first transmission interval, guess if not yet specified.
 	// XXX: ultimately want to calculate this based on an out source that
 	//      the user has created, just arbitrary for now
-	if s.state.rtcpBw == 0.0 {
+	if s.rtcpBw == 0.0 {
 		format := PayloadTypes[0xa]
-		s.state.rtcpBw += float64(format.SampleRate) * float64(format.Channels*8) / 20.
+		s.rtcpBw += float64(format.SampleRate) * float64(format.Channels*8) / 20.
 	}
-	s.state.avgRtcpSize = 28 // for SDES
+	s.avgRtcpSize = 28 // for SDES
 
 	go s.transmissionIntervalService()
 
 		return
 	}
 
-	s.members.Lock() // ----------------------------
+	s.Lock() // ----------------------------
 
-	src, ok := s.members.m[hdr.SyncSrc]
+	src, ok := s.members[hdr.SyncSrc]
 	if !ok {
 		log.Println("new source:", hdr.SyncSrc)
-		src = s.members.newSource(hdr) // unknown stream, make a new one
+		src = newSource(hdr) // unknown stream, make a new one
+		s.members[hdr.SyncSrc] = src
 	}
 
 	for _, csrc := range hdr.CsrcList {
 	}
 
 	src.valid = src.updateSeq(hdr.SeqNum)
-	s.members.Unlock() // --------------------------
+	s.Unlock() // --------------------------
 
 	// forward packet to application
 	if s.RtpHandler != nil {
 func (s *Session) HandleRtcp(p *RtcpPacket) {
 
 	plen := len(p.buf)
-	s.state.updateAvgSize(plen)
+	s.updateAvgSize(plen)
 
 	for pos := 0; pos < plen; {
 		hdr := p.Header(pos)
 }
 
 // update avgRtcpSize per section 6.3.3
-func (ss *sessionState) updateAvgSize(sz int) {
-	ss.avgRtcpSize = (1./16.)*float64(sz) + (15./16.)*ss.avgRtcpSize
+func (s *Session) updateAvgSize(sz int) {
+	s.avgRtcpSize = (1./16.)*float64(sz) + (15./16.)*s.avgRtcpSize
 }
 
 func (s *Session) reportCtrlEvent(code int, ssrc, index uint32) {
 package rtp
 
-import (
-	"sync"
-)
+import ()
 
 const (
 	rtpSeqMod     = 1 << 16
 	minSequential = 2
 )
 
-type memberTable struct {
-	// memberTable can be accessed (at least) by rtp and rtcp receive
-	// goroutines, so we must lock access
-	sync.RWMutex
-	m map[uint32]*Source
-}
-
 type Source struct {
 	ssrc          uint32
 	valid         bool
 	jitter        uint32 // estimated jitter
 }
 
-func newMemberTable() *memberTable {
-	return &memberTable{
-		m: make(map[uint32]*Source),
-	}
-}
-
-func (t *memberTable) newSource(h *RtpHeader) *Source {
+func newSource(h *RtpHeader) *Source {
 	s := &Source{
-		ssrc: h.SyncSrc,
+		ssrc:      h.SyncSrc,
+		probation: minSequential,
 	}
 	s.initSeq(h.SeqNum)
 	s.maxSeq = h.SeqNum - 1
-	s.probation = minSequential
-
-	t.m[s.ssrc] = s
 	return s
 }