Beispiel #1
0
func NewRtpClient(listenIP string, listenPort int) (*RtpClient, error) {
	localAddr, err := net.ResolveIPAddr("ip", listenIP)
	if err != nil {
		return nil, err
	}
	rtpTransport, err := rtp.NewTransportUDP(localAddr, listenPort)
	if err != nil {
		return nil, err
	}
	session := rtp.NewSession(rtpTransport, rtpTransport)
	err = session.StartSession()
	if err != nil {
		return nil, err
	}

	client := &RtpClient{
		listenPort:   listenPort,
		ReceiveStats: stats.NewStats("RTP received"),
		MissedStats:  stats.NewStats("RTP missed"),
		CtrlStats:    stats.NewStats("RTCP events"),
		RtpSession:   session,
		ctrlChan:     session.CreateCtrlEventChan(rtpCtrlBuffer),
		dataChan:     session.CreateDataReceiveChan(rtpDataBuffer),
		stopped:      golib.NewOneshotCondition(),
	}

	client.wg.Add(1)
	go client.handleCtrlEvents()
	client.wg.Add(1)
	go client.handleDataPackets()

	return client, nil
}
Beispiel #2
0
// NewSession creates a new RTP session.
//
// A RTP session requires two transports:
//   tpw - a transport that implements the RtpTransportWrite interface
//   tpr - a transport that implements the RtpTransportRecv interface
//
func NewSession(tpw TransportWrite, tpr TransportRecv) *Session {
	rs := new(Session)

	// Maps grow dynamically, set size to avoid resizing in normal cases.
	rs.streamsOut = make(streamOutMap, maxNumberOutStreams)
	rs.streamsIn = make(streamInMap, maxNumberInStreams)
	rs.remotes = make(remoteMap, 2)
	rs.conflicts = make(conflictMap, 2)

	rs.MaxNumberOutStreams = maxNumberOutStreams
	rs.MaxNumberInStreams = maxNumberInStreams

	rs.transportWrite = tpw
	rs.transportRecv = tpr

	rs.transportEnd = make(TransportEnd, 2)
	rs.rtcpCtrlChan = make(rtcpCtrlChan, 1)

	tpr.SetCallUpper(rs)
	tpr.SetEndChannel(rs.transportEnd)

	rs.DroppedDataPackets = stats.NewStats("Dropped RTP data packets")
	rs.DroppedCtrlPackets = stats.NewStats("Dropped RTP ctrl packets")

	return rs
}
Beispiel #3
0
func NewUdpProxy(listenAddr, targetAddr string) (*UdpProxy, error) {
	var listenUDP, targetUDP *net.UDPAddr
	var err error
	if listenUDP, err = net.ResolveUDPAddr("udp", listenAddr); err != nil {
		return nil, err
	}
	if targetUDP, err = net.ResolveUDPAddr("udp", targetAddr); err != nil {
		return nil, err
	}

	listenConn, err := net.ListenUDP("udp", listenUDP)
	if err != nil {
		return nil, err
	}
	// TODO http://play.golang.org/p/ygGFr9oLpW
	// for per-UDP-packet addressing in case one proxy handles multiple connections
	targetConn, err := net.DialUDP("udp", nil, targetUDP)
	if err != nil {
		listenConn.Close()
		return nil, err
	}

	return &UdpProxy{
		listenConn:      listenConn,
		listenAddr:      listenUDP,
		targetConn:      targetConn,
		targetAddr:      targetUDP,
		packets:         make(chan []byte, BufferedPackets),
		proxyClosed:     golib.NewOneshotCondition(),
		writeErrors:     make(chan error, buf_write_errors),
		Stats:           stats.NewStats("UDP Proxy " + listenAddr),
		OnError:         OnErrorClose,
		writePausedCond: sync.Cond{L: new(sync.Mutex)},
	}, nil
}
Beispiel #4
0
func RegisterServer(server *protocols.Server) (*LoadStats, error) {
	if err := server.Protocol().CheckIncludesFragment(Protocol.Name()); err != nil {
		return nil, err
	}
	stats := &LoadStats{
		server:   server,
		Received: stats.NewStats("Received"),
		Missed:   stats.NewStats("Missed"),
	}
	err := server.RegisterHandlers(protocols.ServerHandlerMap{
		codeLoad: stats.handleLoad,
	})
	if err != nil {
		return nil, err
	}
	return stats, nil
}