sip

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: 20 Imported by: 0

Documentation

Overview

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. A call is a net.Conn of its RTP media, so any packet stream runs over it: mediactl.Stream sends, mediactl.Echo answers.

Index

Constants

This section is empty.

Variables

View Source
var CloseTimeout = 5 * time.Second

CloseTimeout bounds the BYE of a call closed as a conn.

Functions

This section is empty.

Types

type Call

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

Call is a call of a phone, and a net.Conn of its media: its writes leave as the payloads of RTP packets to the peer's media address, its reads return the payloads of the RTP packets received, both on a single socket. Closing it hangs up.

func (*Call) Close

func (c *Call) Close() error

Close hangs up the call, bounded by CloseTimeout, unless it has ended already.

func (*Call) Done

func (c *Call) Done() <-chan struct{}

Done is closed once the call ends.

func (*Call) EndReason

func (c *Call) EndReason() string

EndReason returns why the call ended.

func (*Call) Hangup

func (c *Call) Hangup(ctx context.Context) error

Hangup ends the call with a BYE, unless the call has ended. A hangup while another is in flight waits for that one and returns its result.

func (*Call) ID

func (c *Call) ID() string

ID returns the Call-ID of the call.

func (*Call) LocalAddr

func (c *Call) LocalAddr() net.Addr

LocalAddr returns the address of the call's media socket.

func (*Call) Read

func (c *Call) Read(p []byte) (int, error)

Read returns the payload of the next RTP packet of the call.

func (*Call) RemoteAddr

func (c *Call) RemoteAddr() net.Addr

RemoteAddr returns the peer's RTP address.

func (*Call) RemoteMedia

func (c *Call) RemoteMedia() netip.AddrPort

RemoteMedia returns the address the call sends RTP to.

func (*Call) SetDeadline

func (c *Call) SetDeadline(t time.Time) error

SetDeadline sets the deadlines of the media socket.

func (*Call) SetReadDeadline

func (c *Call) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline of the media socket.

func (*Call) SetWriteDeadline

func (c *Call) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline of the media socket.

func (*Call) Stats

func (c *Call) Stats() mediartp.Stats

Stats returns the RTP and RTCP counters of the call.

func (*Call) String

func (c *Call) String() string

String describes the call.

func (*Call) Write

func (c *Call) Write(p []byte) (int, error)

Write sends p as the payload of the next RTP packet of the call.

type Config

type Config struct {
	// Net is the network the phone runs over; nil is the host network.
	Net transport.Net
	// Addr is the ip:port the phone answers calls at over UDP; empty picks a port on every
	// interface.
	Addr string
	// Username and Password answer the digest challenges of the INVITEs the phone sends.
	Username, Password string
	// AnswerDelay is how long the phone rings before it answers.
	AnswerDelay time.Duration
	// Registrar makes the phone a registrar too: it takes REGISTERs and dials an AOR registered
	// with it over the flow the AOR registered on (RFC 5626).
	Registrar bool
	// LoggerFactory makes the phone's logger; nil logs nothing.
	LoggerFactory logging.LoggerFactory
}

Config is the configuration of a phone.

type Phone

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

Phone is a SIP phone over UDP. Every peer has a conn of its own: a dialed call its dialed conn, the callers the conns the listener accepts. The local address of a conn is the phone's address on it.

func New

func New(cfg Config) (*Phone, error)

New creates a phone answering calls at its address.

func (*Phone) Accept

func (p *Phone) Accept() (net.Conn, error)

Accept returns the next call the phone answered, once acknowledged: the phone is a net.Listener of calls. It fails once the phone is closed.

func (*Phone) Addr

func (p *Phone) Addr() net.Addr

Addr returns the address the phone answers calls at.

func (*Phone) Close

func (p *Phone) Close() error

Close ends every call locally, without a BYE, and stops the phone.

func (*Phone) Dial

func (p *Phone) Dial(ctx context.Context, uri string) (*Call, error)

Dial calls a SIP URI and returns the call once it is answered. An AOR registered with a registrar phone is called at its Contact over the flow it registered on. Cancelling the context before the answer cancels the call.

func (*Phone) DialContext

func (p *Phone) DialContext(ctx context.Context, _, address string) (net.Conn, error)

DialContext calls the SIP URI of address, over UDP whatever the network: the phone is a dialer of calls.

func (*Phone) Incoming

func (p *Phone) Incoming() <-chan *Call

Incoming returns the calls the phone answered, once acknowledged.

func (*Phone) Ping

func (p *Phone) Ping(ctx context.Context, uri string) error

Ping sends an OPTIONS to the URI on a flow of its own and returns nil on a 2xx: the SIP peer at the URI's address takes requests. A URI with no user part addresses a proxy itself.

func (*Phone) Registered

func (p *Phone) Registered() <-chan Registration

Registered returns the REGISTERs a registrar phone took.

type Registration

type Registration struct {
	// AOR is the address of record, Contact the address it is reached at.
	AOR, Contact sip.Uri
	// Expires is the expiry the phone granted, zero for a removed binding.
	Expires time.Duration
}

Registration is a REGISTER a registrar phone took.

type StatusError

type StatusError struct {
	Code   int
	Reason string
}

StatusError is a final non-2xx response to a request.

func (*StatusError) Error

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

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