mediactl

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package mediactl holds the media sources and sinks of the mediactl tool and the tests: packet streams that measure the bitrate, the loss and the round trip, and echo servers, over any net.Conn. Every protocol is a Dialer of the conns of a sender and a net.Listener of the conns of an answerer: net.Dialer and pkg/udp or net.Listen for UDP and TCP, and the subpackages for the rest, sip (SIP calls, their RTP media framed by rtp), srt (SRT callers and listeners) and turn (conns relayed through a TURN server).

Index

Constants

View Source
const (
	DefaultLength = 160
	DefaultRate   = 50
)

The stream of a sender by default: a packet of 160 bytes 50 times a second, the size and the rate of 20 ms G.711 frames.

View Source
const HeaderLen = 12

HeaderLen is the header a stream writes at the start of every packet long enough: a sequence number and the send time, so the receiver can count the loss and the sender the round trip of the echo.

Variables

View Source
var (
	// ReportInterval is how often a stream logs its rates.
	ReportInterval = time.Second
	// EchoDrain is how long an echoing stream waits for the echoes still on the way back once it
	// stopped sending.
	EchoDrain = 500 * time.Millisecond
	// FlowIdle is how long a datagram echo waits for the next packet before it ends the flow
	// and reports it.
	FlowIdle = 3 * time.Second
)

The timers of the streams and the echo servers, variables so the tests can shorten them.

View Source
var ErrNothingReceived = errors.New("nothing received")

ErrNothingReceived fails a stream that expected packets and got none.

Functions

func Check

func Check(r Result, c Config, maxLoss float64, maxRTT time.Duration) error

Check judges the result of a sender streaming by c: a publisher fails when it sent nothing, a stream expecting packets when it got nothing, or lost more than maxLoss percent of its packets (a negative maxLoss never fails), or its average round trip is longer than maxRTT (0 never fails).

func IsClosed

func IsClosed(err error) bool

IsClosed tells whether an error is that of a closed conn or listener, the error a server returns when its run ends.

func Rate

func Rate(bytes int64, d time.Duration) string

Rate formats bytes over a duration as a bitrate with an SI prefix: 75.64 kbit/s, 7.56 Mbit/s.

func Serve

func Serve(l net.Listener, datagram bool, log logging.LeveledLogger) error

Serve echoes every conn a listener accepts until the listener fails: UDP clients of pkg/udp, TCP conns, SIP calls of a sip.Phone, SRT callers. Every conn is reported to the logger (unless nil) when its echo ends.

Types

type Config

type Config struct {
	// Length is the bytes per packet the stream sends, Rate the packets per second; a zero
	// Rate sends nothing.
	Length int
	Rate   float64
	// Echo tells that what comes back is the echo of what the stream sent: the loss is what did
	// not come back, else the gaps in the sequence numbers received.
	Echo bool
	// ByteStream tells that the conn is a byte stream (TCP): a packet is a read or a write, no
	// header is read and nothing is lost.
	ByteStream bool
	// VoIP estimates the call quality of an echoing stream as the ITU-T G.107 E-model does for a
	// G.711 voice call: the R-factor and the MOS, from the loss, the round trip and the jitter.
	// They mean nothing for other media, such as video.
	VoIP bool
	// Source, if set, fills every packet sent instead of the header and the padding: real media,
	// such as an MPEG-TS file. The receiver measures no loss and no round trip then; a short read
	// sends a short packet, an error ends the stream.
	Source io.Reader
	// Logger logs the rates of every ReportInterval and the summary; nil logs nothing.
	Logger logging.LeveledLogger
}

Config is the configuration of a stream.

type Dialer

type Dialer interface {
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer makes the conns of a sender: net.Dialer for UDP and TCP, and the dialers of the subpackages for SIP calls, SRT streams and TURN relays.

type Result

type Result struct {
	// Duration is up to the last packet sent, for a receiver alone up to the last received.
	Duration                                 time.Duration
	Sent, SentBytes, Received, ReceivedBytes int64
	Lost, Expected                           int64
	RTTMin, RTTAvg, RTTMax                   time.Duration
	Jitter                                   time.Duration
	// RFactor and MOS estimate the call quality of a VoIP stream, zero for other streams.
	RFactor, MOS float64
}

Result is what a stream measured. Lost and Expected are zero when the stream cannot tell (nothing to compare against, or a byte stream), the round trip when nothing echoed came back, the jitter when fewer than two packets with a header arrived. It marshals to JSON with the durations as strings.

func Total

func Total(rs []Result) Result

Total adds up the results of parallel streams: the counters summed, the longest duration, the round trip over all of them, its average and the jitter weighted by the packets received, and the call quality of VoIP streams from these.

func (Result) MarshalJSON

func (r Result) MarshalJSON() ([]byte, error)

MarshalJSON renders the result with snake case names and the durations as strings.

func (Result) String

func (r Result) String() string

String reports a result: what was sent, what was received, the loss and the round trip, each where it means something.

type Stats

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

Stats count a stream: the packets and bytes sent and received, and over the received packets that carry a header, the highest sequence number, the round trip and the jitter.

func Echo

func Echo(conn net.Conn, datagram bool) *Stats

Echo sends back everything it reads on a conn until the conn fails or, for a datagram conn, stays silent for FlowIdle; then it closes the conn and returns what it received. It counts the packets and the bytes only: the sender measures the loss and the round trip.

func NewStats

func NewStats() *Stats

NewStats returns the stats of a receiver alone, such as an echo server, starting now.

func Stream

func Stream(ctx context.Context, conn io.ReadWriteCloser, c Config) *Stats

Stream sends a packet every 1/Rate seconds on conn and counts what it reads, until the context ends or the conn fails, then closes the conn. An echoing stream waits EchoDrain for the echoes on the way back before it closes.

func (*Stats) Received

func (s *Stats) Received(p []byte, parse bool)

Received counts a received packet, reading its header if parse is set.

func (*Stats) Result

func (s *Stats) Result() Result

Result returns what the stream measured so far.

func (*Stats) String

func (s *Stats) String() string

String reports what the stream measured.

Directories

Path Synopsis
Package k8s resolves the k8s:// addresses of mediactl and the stunnerd tunnel from a Kubernetes cluster, through the kubeconfig of the caller:
Package k8s resolves the k8s:// addresses of mediactl and the stunnerd tunnel from a Kubernetes cluster, through the kubeconfig of the caller:
Package rtp frames a packet conn as an RTP session: a net.Conn whose writes leave as the payloads of RTP packets and whose reads return the payloads of the RTP packets received.
Package rtp frames a packet conn as an RTP session: a net.Conn whose writes leave as the payloads of RTP packets and whose reads return the payloads of the RTP packets received.
Package sip is a minimal SIP phone, a dialer and a listener of calls: Dial and DialContext return a call once it is answered, Accept the next call the phone answered.
Package sip is a minimal SIP phone, a dialer and a listener of calls: Dial and DialContext return a call once it is answered, Accept the next call the phone answered.
Package srt holds the SRT dialer and listener of mediactl: a dialer calls for a stream in a mode, named in the access control syntax of the stream ID, a listener takes the callers of a passphrase, passes every published stream to the readers of the same stream itself and returns the other callers, for mediactl.Serve to echo.
Package srt holds the SRT dialer and listener of mediactl: a dialer calls for a stream in a mode, named in the access control syntax of the stream ID, a listener takes the callers of a passphrase, passes every published stream to the readers of the same stream itself and returns the other callers, for mediactl.Serve to echo.
Package turn holds the TURN dialer of mediactl: conns to peers relayed through a TURN server, over a UDP allocation or an RFC 6062 TCP connection.
Package turn holds the TURN dialer of mediactl: conns to peers relayed through a TURN server, over a UDP allocation or an RFC 6062 TCP connection.

Jump to

Keyboard shortcuts

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