peer

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RTCP    = "rtcp"
	RTP     = "rtp"
	DTLS    = "dtls"
	Unknown = "unknown"
)
View Source
const (
	RTPTypeSVC       = "svc"
	RTPTypeSimple    = "simple"
	RTPTypeSimulcast = "simulcast"
	RTPTypeNone      = "none"
)
View Source
const (
	MaxExtraOffsetMs = 75
	MsOffset         = 33
)
View Source
const (
	// AutoSwitchLayer we will auto switch layer based on bandwidth, from lowest to preferred
	AutoSwitchLayer = SwitchLayerMode(1) // TODO buggy, can't switch for now.
	// ManualSwitchLayer we won't change the layer unless UpdateLayer() has been called, default highest layer.
	ManualSwitchLayer = SwitchLayerMode(0)
)
View Source
const (
	ReceiveRTPPacketMedia          = "media"
	ReceiveRTPPacketDiscarded      = "discarded"
	ReceiveRTPPacketRetransmission = "retransmission"
)
View Source
const (
	RTPSeqMod   = uint32(1) << 16
	MaxDropout  = uint16(3000)
	MaxMisorder = uint16(1500)
)
View Source
const (
	RTPProbationSsrc = 1234
)

Variables

View Source
var (
	ErrNoMoreData = errors.New("no more data")
	ErrTooOld     = errors.New("seq too old")
)
View Source
var (
	ErrInvalidPacket    = errors.New("invalid rtpPacket")
	ErrInvalidRtxPacket = errors.New("invalid rtx rtpPacket")
	ErrNoNack           = errors.New("nack not support")
	ErrBadSeq           = errors.New("bad sequence number")
	ErrInvalidSimulcast = errors.New("invalid simulcast params")

	ErrReceiverExist      = errors.New("receiver already exist")
	ErrReceiverNotExist   = errors.New("receiver not exist")
	ErrPayloadNotMatch    = errors.New("payload type not match")
	ErrCodecNotMatch      = errors.New("stream and codec payload type not match")
	ErrRTXPayloadNotMatch = errors.New("rtx payload type not match")
	ErrHeaderIDNotMatch   = errors.New("header id not match in one connection")
	ErrCodecCantBeNil     = errors.New("codec cant be nil")
	ErrStreamCantBeEmpty  = errors.New("streams cant be empty")
	ErrConnExist          = errors.New("connection already exists")
)
View Source
var (
	ErrMidExist  = errors.New("mid already in use")
	ErrRidExist  = errors.New("rid already in use")
	ErrSsrcExist = errors.New("ssrc already in use")
)
View Source
var ErrInvalidRtx = errors.New("invalid rtx")

Functions

func IsDtls

func IsDtls(data []byte) bool

func IsRtcp

func IsRtcp(buf []byte) bool

IsRtcp is a MatchFunc that only matches SRTCP and not SRTP.

func MatchSRTP

func MatchSRTP(buf []byte) bool

MatchSRTP is a MatchFunc that only matches SRTP and not SRTCP.

func MatchSRTPOrSRTCP

func MatchSRTPOrSRTCP(b []byte) bool

MatchSRTPOrSRTCP is a MatchFunc that accepts packets with the first byte in [128..191] as defied in RFC7983.

func RandomString

func RandomString(size int) string

Types

type Broker

type Broker struct {
	// contains filtered or unexported fields
}

Broker is a sfu node, with global setting in it.

func NewBroker

func NewBroker(option BrokerOption) (*Broker, error)

func (*Broker) Close

func (b *Broker) Close()

Close clean before stop, if someone still call new connection, it's their fault, we don't care.

func (*Broker) Connection

func (b *Broker) Connection(id string) *Connection

func (*Broker) Connections

func (b *Broker) Connections() []*Connection

func (*Broker) NewConnection

func (b *Broker) NewConnection(id string, bweType string, transport Transport) (*Connection, error)

func (*Broker) NewWebRTCConnection

func (b *Broker) NewWebRTCConnection(options *WebRTCOption) (*Connection, error)

type BrokerOption

type BrokerOption struct {
	ICE ice.Option
}

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is lock-free but not gc friendly

func NewBuffer

func NewBuffer(cap int) *Buffer

NewBuffer create a single buffer with given cap. How Much Cap should it be? cap = 4*pkts/s

func (*Buffer) Latest

func (b *Buffer) Latest() uint32

func (*Buffer) Put

func (b *Buffer) Put(packet rtc.Packet)

func (*Buffer) SetIdx

func (b *Buffer) SetIdx(idx uint32)

SetIdx should be called only once.

func (*Buffer) Snap

func (b *Buffer) Snap() *BufferSnap

func (*Buffer) Wait

func (b *Buffer) Wait()

type BufferSnap

type BufferSnap struct {
	// contains filtered or unexported fields
}

func (*BufferSnap) Get

func (s *BufferSnap) Get(seq uint32) ([]rtc.Packet, uint32, error)

type Codec

type Codec struct {
	PayloadType    rtc.PayloadType
	EncoderName    string
	ClockRate      int
	Channels       int
	Parameters     map[string]string
	FeedbackParams []RtcpFeedback
	RTX            rtc.PayloadType
}

func (Codec) Equal

func (c Codec) Equal(c2 *Codec) bool

Equal if two codec has name encoder name and encoder parameters, we consider they are equal.

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

func (*Connection) Close

func (c *Connection) Close()

func (*Connection) Connected

func (c *Connection) Connected()

func (*Connection) Disconnected

func (c *Connection) Disconnected()

Disconnected We could restart ice and keep the connection, that's the disconnected do, but we do not support it yet.

func (*Connection) ID

func (c *Connection) ID() string

func (*Connection) NewReceiver

func (c *Connection) NewReceiver(req *ReceiverOption) (*Receiver, error)

func (*Connection) NewSender

func (c *Connection) NewSender(req *SenderOption) (Sender, error)

func (*Connection) OnConsumerNeedBitrateChange

func (c *Connection) OnConsumerNeedBitrateChange(s Sender)

func (*Connection) OnStateChange

func (c *Connection) OnStateChange(callback func(state int))

this is a tbd way to do it.

func (*Connection) Receivers

func (c *Connection) Receivers() []*Receiver

func (*Connection) Senders

func (c *Connection) Senders() []Sender

func (*Connection) Stats

func (c *Connection) Stats() *Stats

func (*Connection) Transport

func (c *Connection) Transport() Transport

type ConsumerListener

type ConsumerListener interface {
	OnConsumerNeedBitrateChange(s Sender)
	// contains filtered or unexported methods
}

type MatchFunc

type MatchFunc func([]byte) bool

func MatchRange

func MatchRange(lower, upper byte) MatchFunc

type PacketType

type PacketType string

func CheckPacket

func CheckPacket(data []byte) PacketType

type RTPSeqManager

type RTPSeqManager interface {
	Sync(seq uint16)
	Input(seq uint16) uint16
}

func NewSeqManager

func NewSeqManager() RTPSeqManager

type Receiver

type Receiver struct {
	// contains filtered or unexported fields
}

func (*Receiver) AddSender

func (r *Receiver) AddSender(s Sender)

func (*Receiver) Close

func (r *Receiver) Close()

func (*Receiver) Codec

func (r *Receiver) Codec() *Codec

func (*Receiver) GetRTPHeaderExtensionIds

func (r *Receiver) GetRTPHeaderExtensionIds() rtc.HeaderExtensionIDs

func (*Receiver) GetRTPStream

func (r *Receiver) GetRTPStream(packet rtc.Packet) ReceiverStream

func (*Receiver) GetRTPStreams

func (r *Receiver) GetRTPStreams() []ReceiverStream

func (*Receiver) GetRtcp

func (r *Receiver) GetRtcp(ms int64) rtcp.Packet

func (*Receiver) HeaderExtensions

func (r *Receiver) HeaderExtensions() []rtc.HeaderExtension

func (*Receiver) ID

func (r *Receiver) ID() string

func (*Receiver) Kind

func (r *Receiver) Kind() string

func (*Receiver) MID

func (r *Receiver) MID() string

func (*Receiver) MediaType

func (r *Receiver) MediaType() string

func (*Receiver) OnRTPStreamNeedWorstRemoteFractionLost

func (r *Receiver) OnRTPStreamNeedWorstRemoteFractionLost(ssrc uint32) uint8

func (*Receiver) ReceiveRTPPacket

func (r *Receiver) ReceiveRTPPacket(packet rtc.Packet) string

func (*Receiver) ReceiveRtcpSenderReport

func (r *Receiver) ReceiveRtcpSenderReport(report *rtcp.SenderReport)

func (*Receiver) RequestKeyFrame

func (r *Receiver) RequestKeyFrame(ssrc uint32)

type ReceiverListener

type ReceiverListener interface {
	// contains filtered or unexported methods
}

type ReceiverOption

type ReceiverOption struct {
	ID                   string
	MID                  string
	MediaType            string // video/audio
	Codec                *Codec
	HeaderExtensions     []rtc.HeaderExtension // required
	Streams              []StreamOption
	KeyFrameRequestDelay int
}

func (ReceiverOption) Validate

func (o ReceiverOption) Validate() error

type ReceiverStream

type ReceiverStream interface {
	Stream
	RequestKeyFrame()
	ReceivePacket(packet rtc.Packet) error

	GetSenderReportNtpMs() uint64
	GetSenderReportTS() int64

	ReceiveRtcpSenderReport(report *rtcp.SenderReport)
	GetRtcpReceiverReport() *rtcp.ReceptionReport
	GetRtxReceiverReport() *rtcp.ReceptionReport

	UpdateSSRC(uint32)
	UpdateRtxSSRC(uint32)
	SetRtx(payloadType rtc.PayloadType, ssrc uint32)
	Close()
}

func NewReceiverStream

func NewReceiverStream(lis ReceiverStreamListener, mediaType string, stream StreamOption, codec Codec) ReceiverStream

type ReceiverStreamListener

type ReceiverStreamListener interface {
	OnRTPStreamNeedWorstRemoteFractionLost(ssrc uint32) uint8
	// contains filtered or unexported methods
}

type RtcpFeedback

type RtcpFeedback struct {
	Type      string
	Parameter string
}

type RtxStream

type RtxStream struct {
	// contains filtered or unexported fields
}

RtxStream only used for cal rr report, huge waste.

func NewRtxStream

func NewRtxStream(params RtxStreamParams) *RtxStream

func (*RtxStream) GetExpectedPackets

func (r *RtxStream) GetExpectedPackets() int

func (*RtxStream) GetRtcpReceiverReport

func (r *RtxStream) GetRtcpReceiverReport() *rtcp.ReceptionReport

func (*RtxStream) InitSeq

func (r *RtxStream) InitSeq(seq uint16)

func (*RtxStream) ReceivePacket

func (r *RtxStream) ReceivePacket(packet rtc.Packet) error

func (*RtxStream) UpdateSeq

func (r *RtxStream) UpdateSeq(packet rtc.Packet) bool

type RtxStreamParams

type RtxStreamParams struct {
	EncodingIdx int
	Ssrc        uint32
	PayloadType rtc.PayloadType
	Codec       *codec.Codec
	ClockRate   int
	Rrid        string
	Cname       string
}

type Sender

type Sender interface {
	ID() string
	SendRTPPacket(packet rtc.Packet)
	SetExternallyManagedBitrate()
	TransportConnected()
	MediaType() string
	TransportDisconnected()
	ProducerRtcpSenderReport(stream ReceiverStream, first bool)
	ReceiveRtcpReceiverReport(report rtcp.ReceptionReport)
	GetRtcp(ms int64) rtcp.Packet
	ReceiveNack(report *rtcp.TransportLayerNack)
	RequestKeyframe()
	OnRTPStreamRetransmitRTPPacket(packet rtc.Packet)
	FractionLost() uint8
	UpdateLayer(layer int)
	HeaderExtensions() []rtc.HeaderExtension
	Codec() *Codec
	MID() string
	Stream() *StreamOption
	ReceiverID() string
	Close()
	Kind() string
	GetBitrate(layer int) int64
	// contains filtered or unexported methods
}

func NewSender

func NewSender(options *SenderOption, listener ConsumerListener, receiver *Receiver, stats *Stats) (Sender, error)

type SenderOption

type SenderOption struct {
	ID               string
	MID              string
	ConnectionID     string
	ReceiverID       string
	Codec            *Codec                // Optional
	HeaderExtensions []rtc.HeaderExtension // Optional
	// only worked in simulcast
	SwitchMode SwitchLayerMode
}

type SenderStream

type SenderStream interface {
	Stream
	ReceiveNack(report *rtcp.TransportLayerNack)
	GetRtcpSenderReport(ms int64) rtcp.Packet
	GetRtcpSdesChunk() rtcp.Packet
	ReceiveRtcpReceiverReport(report rtcp.ReceptionReport)
	ReceivePacket(packet rtc.Packet) error
}

func NewSenderStream

func NewSenderStream(lis SenderStreamListener, mediaType string, stream StreamOption, codec Codec) SenderStream

type SenderStreamListener

type SenderStreamListener interface {
	OnRTPStreamRetransmitRTPPacket(packet rtc.Packet)
}

type SimulcastConsumer

type SimulcastConsumer struct {
	// contains filtered or unexported fields
}

func (SimulcastConsumer) Close

func (s SimulcastConsumer) Close()

func (SimulcastConsumer) Codec

func (s SimulcastConsumer) Codec() *Codec

func (SimulcastConsumer) CreateRTPStream

func (s SimulcastConsumer) CreateRTPStream() error

func (SimulcastConsumer) FractionLost

func (s SimulcastConsumer) FractionLost() uint8

func (*SimulcastConsumer) GetBitrate

func (s *SimulcastConsumer) GetBitrate(layer int) int64

func (SimulcastConsumer) GetRtcp

func (s SimulcastConsumer) GetRtcp(ms int64) rtcp.Packet

func (SimulcastConsumer) HeaderExtensions

func (s SimulcastConsumer) HeaderExtensions() []rtc.HeaderExtension

func (SimulcastConsumer) ID

func (s SimulcastConsumer) ID() string

func (SimulcastConsumer) IsActive

func (s SimulcastConsumer) IsActive() bool

func (*SimulcastConsumer) Kind

func (s *SimulcastConsumer) Kind() string

func (SimulcastConsumer) MID

func (s SimulcastConsumer) MID() string

func (SimulcastConsumer) MediaType

func (s SimulcastConsumer) MediaType() string

func (SimulcastConsumer) OnRTPStreamRetransmitRTPPacket

func (s SimulcastConsumer) OnRTPStreamRetransmitRTPPacket(packet rtc.Packet)

func (*SimulcastConsumer) ProducerRtcpSenderReport

func (s *SimulcastConsumer) ProducerRtcpSenderReport(stream ReceiverStream, first bool)

func (SimulcastConsumer) ReceiveNack

func (s SimulcastConsumer) ReceiveNack(report *rtcp.TransportLayerNack)

func (SimulcastConsumer) ReceiveRtcpReceiverReport

func (s SimulcastConsumer) ReceiveRtcpReceiverReport(report rtcp.ReceptionReport)

func (SimulcastConsumer) ReceiverID

func (s SimulcastConsumer) ReceiverID() string

func (*SimulcastConsumer) RequestKeyframe

func (s *SimulcastConsumer) RequestKeyframe()

func (*SimulcastConsumer) SendRTPPacket

func (s *SimulcastConsumer) SendRTPPacket(packet rtc.Packet)

func (SimulcastConsumer) SetExternallyManagedBitrate

func (s SimulcastConsumer) SetExternallyManagedBitrate()

func (SimulcastConsumer) Stream

func (s SimulcastConsumer) Stream() *StreamOption

func (*SimulcastConsumer) TransportConnected

func (s *SimulcastConsumer) TransportConnected()

func (*SimulcastConsumer) TransportDisconnected

func (s *SimulcastConsumer) TransportDisconnected()

func (*SimulcastConsumer) UpdateLayer

func (s *SimulcastConsumer) UpdateLayer(layer int)

func (*SimulcastConsumer) UserOnTransportConnected

func (s *SimulcastConsumer) UserOnTransportConnected()

type Stats

type Stats struct {
	// contains filtered or unexported fields
}

Stats connection.GetStats() it design to be access from everywhere.

func (*Stats) BytesReceived

func (s *Stats) BytesReceived() int64

func (*Stats) BytesSend

func (s *Stats) BytesSend() int64

func (*Stats) IncomingRTP

func (s *Stats) IncomingRTP(packet rtc.Packet)

func (*Stats) OutcomePacket

func (s *Stats) OutcomePacket(packet rtc.Packet)

func (*Stats) PacketsReceived

func (s *Stats) PacketsReceived() int64

func (*Stats) ReceiveBPS

func (s *Stats) ReceiveBPS(nowMs int64) int64

func (*Stats) SentBPS

func (s *Stats) SentBPS(nowMs int64) int64

type Stream

type Stream interface {
	// SSRC return the ssrc of the stream. It could be zero if no ssrc set yet.
	SSRC() uint32
	// RtxSSRC return the rtx ssrc of the stream. It could be zero if no rtx ssrc set yet.
	RtxSSRC() uint32
	// PayloadType return the payload type of the stream.
	PayloadType() rtc.PayloadType
	// RtxPayloadType return the rtx payload type of the stream.
	RtxPayloadType() rtc.PayloadType
	// RID return the rid of the stream.
	RID() string
	// GetActiveMs return how long since last rtp rtpPacket.
	GetActiveMs() int64
	// GetClockRate return the clock rate of the stream Ccodec.
	GetClockRate() int

	Cname() string
	// GetMaxPacketTS return the max rtpPacket rtp timestamp of the stream.
	GetMaxPacketTS() uint32
	// FractionLost return the fractionLost.
	FractionLost() uint8

	Stats() *StreamStats
}

Stream is a internal wrap for parameter.StreamOption

type StreamOption

type StreamOption struct {
	SSRC            uint32
	Cname           string
	RID             string
	RTX             uint32
	Dtx             bool
	PayloadType     rtc.PayloadType
	ScalabilityMode string
	MaxBitrate      int
	MaxFramerate    float64
}

StreamOption basically one-ssrc map to one stream. but the rtx is different, they consider to be same stream if there is only one stream, ssrc and rid could both be empty, but mid can't otherwise it must exist at least 1

type StreamStats

type StreamStats struct {
	// contains filtered or unexported fields
}

func (*StreamStats) BytesReceived

func (s *StreamStats) BytesReceived() int64

func (*StreamStats) BytesSent

func (s *StreamStats) BytesSent() int64

func (*StreamStats) PacketsReceived

func (s *StreamStats) PacketsReceived() int64

func (*StreamStats) PacketsSent

func (s *StreamStats) PacketsSent() int64

func (*StreamStats) ReceiveBPS

func (s *StreamStats) ReceiveBPS(nowMs int64) int64

func (*StreamStats) SentBPS

func (s *StreamStats) SentBPS(nowMs int64) int64

type SwitchLayerMode

type SwitchLayerMode int

type Transport

type Transport interface {
	SetConnection(connection *Connection)
	IsConnected() bool
	SendRTPPacket(packet rtc.Packet)
	SendRtcpPacket(packet rtcp.Packet)
	Info() TransportInfo
	Close()
}

Transport should respond for read and write pkt.

func NewWebRTCTransport

func NewWebRTCTransport(options *WebRTCOption, iceServer *ice.Server, cm dtls.CertificateGenerator) (Transport, error)

NewWebRTCTransport is a webrtc implementation of peer.Transport, support ice, dtls, srtp.

type TransportInfo

type TransportInfo struct {
	ID      string
	IceInfo struct {
		Role       string
		Candidates []ice.Candidate
		Ufrag      string
		Pwd        string
		Lite       bool
	}
	DtlsInfo struct {
		Fingerprints []dtls.Fingerprint
		Role         string
	}
}

type WebRTCOption

type WebRTCOption struct {
	ID         string
	ListenIPs  []string
	DtlsOption dtls.Option
	BweType    string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL