Commits

Liam Staskawicz committed 35ba939

Transport: make a more generic interface, such that other Transports could be plugged in if desired

Comments (0)

Files changed (4)

 func main() {
 	log.Println("running...")
 
-	s := rtp.NewSession(rtpAddr, rtcpAddr)
+    t := rtp.NewTransportUDP(rtpAddr, rtcpAddr)
+	s := rtp.NewSession(t)
     s.Start()
 	select {}
 
 	RtpHandler       RtpHandler
 	RtcpHandler      RtcpHandler
 	CtrlEventHandler CtrlEventHandler
-	Transport        *TransportUDP // XXX: this should likely be a Transport interface
+	Transport        Transport
 	state            sessionState
 }
 
 	Reason    string // Resaon string if it was available, empty otherwise
 }
 
-func NewSession(rtp, rtcp string) *Session {
-	s := &Session{}
-	s.Transport = &TransportUDP{
-		Handler:  s,
-		RtpAddr:  rtp,
-		RtcpAddr: rtcp,
+func NewSession(t Transport) *Session {
+	s := &Session{
+		Transport: t,
 	}
+	t.SetHandler(s)
 	return s
 }
 
+package rtp
+
+// Transport sends and receives data on the wire
+type Transport interface {
+	SetHandler(TransportHandler)
+	ListenAndServe() error
+}
+
+// Transport reports new data to its TransportHandler
+type TransportHandler interface {
+	HandleRtp(*RtpPacket)
+	HandleRtcp(*RtcpPacket)
+}

rtp/transportUdp.go

 	"net"
 )
 
-type TransportHandler interface {
-	HandleRtp(*RtpPacket)
-	HandleRtcp(*RtcpPacket)
-}
-
 type TransportUDP struct {
 	Handler  TransportHandler
 	RtpAddr  string
 	maxPacket = 1500 // reasonable MTU size
 )
 
+func NewTransportUDP(rtp, rtcp string) *TransportUDP {
+	return &TransportUDP{
+		RtpAddr:  rtp,
+		RtcpAddr: rtcp,
+	}
+}
+
+func (t *TransportUDP) SetHandler(h TransportHandler) {
+	t.Handler = h
+}
+
 // listen for both incoming rtp and rtcp data
 func (t *TransportUDP) ListenAndServe() error {
 
 	return nil
 }
 
-func ListenAndServeUDP(rtp, rtcp string, h TransportHandler) error {
-	t := &TransportUDP{
-		Handler:  h,
-		RtpAddr:  rtp,
-		RtcpAddr: rtcp,
-	}
-	return t.ListenAndServe()
-}
-
 func (t *TransportUDP) readRtpPacket(c *net.UDPConn) {
 
 	for {