calls

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package calls is the media server of the one-to-one calls (SPEC §7.18, D63, D64): a small WebRTC forwarding server made with pion. A call has two participants, each with one PeerConnection to the server, which is always the offerer; every RTP packet one participant sends is forwarded to the other. Nothing is decoded, mixed or recorded.

The package is the only one that imports pion. It never imports the store: every permission and every change of the state of a call goes through the Controller, which internal/app implements, and the end of a call is learnt from the live events (Events). The signalling of a participant is a WebSocket that Server.Serve runs, after the web or the API handler has authenticated the user and checked that the user is a participant of the call.

Nothing of the media, no SDP and no candidate is ever logged at info level.

Index

Constants

View Source
const (
	// TagCallClosed is the tag of a socket that joins a call whose media has
	// closed already: the call ended.
	TagCallClosed = "MediaCallClosed"
	// TagNotParticipant is the tag of a socket of a user who is neither the
	// caller nor the callee of the call.
	TagNotParticipant = "MediaNotParticipant"
)
View Source
const (
	// DefaultICETimeout is how long a participant has to connect, counted from
	// its answer to the offer of the server.
	DefaultICETimeout = 10 * time.Second
	// DefaultDisconnectTimeout is how long the transport of a participant may
	// stay disconnected or failed before it is closed and the call ends.
	DefaultDisconnectTimeout = 15 * time.Second
	// DefaultReconnectGrace is how long a participant of an active call has
	// to open a new signalling socket after its socket closed.
	DefaultReconnectGrace = 15 * time.Second
	// DefaultJoinTimeout is how long a participant has, from the moment the
	// call is answered, to answer the offer of the server.
	DefaultJoinTimeout = 30 * time.Second
	// DefaultMaxVideoBitrate is the most video, in bits per second, that a
	// sender may send (2.5 Mbit/s).
	DefaultMaxVideoBitrate = 2_500_000
	// DefaultPingInterval is how often the server pings a signalling socket,
	// and DefaultPingTimeout how long the client has to answer.
	DefaultPingInterval = 30 * time.Second
	DefaultPingTimeout  = 15 * time.Second
	// DefaultWriteTimeout is how long one write to a signalling socket may
	// take.
	DefaultWriteTimeout = 10 * time.Second
)

The defaults of the durations and limits of Config (SPEC D64).

View Source
const (
	// CodeInvalidMessage is the code of a message that is not a message of the
	// protocol.
	CodeInvalidMessage = "invalid_message"
	// CodeNegotiationFailed is the code of an offer, an answer or a candidate
	// that the media server could not use.
	CodeNegotiationFailed = "negotiation_failed"
	// CodeInternal is the code of a failure of the media server itself.
	CodeInternal = "internal"
	// CodeWrongState is the code of a request that the state of the call does
	// not allow.
	CodeWrongState = "wrong_state"
)

The codes of the error messages.

View Source
const (
	// EnvJoin tells the home that a socket of a participant opened on the
	// edge; the home answers with its offer as for a socket of its own.
	EnvJoin = "join"
	// EnvClient carries a message that the client of the socket sent.
	EnvClient = "client"
	// EnvLeave tells the home that the socket closed.
	EnvLeave = "leave"
	// EnvServer carries a message for the client of the socket.
	EnvServer = "server"
	// EnvClose tells the edge to close the socket with a code and a reason.
	EnvClose = "close"
)

The kinds of Envelope (SPEC D65). The first three go from the edge to the home of the call, the last two from the home to the edge.

View Source
const (
	// TagListenFailed is the tag of a media address whose ports cannot be
	// opened.
	TagListenFailed = "MediaListenFailed"
	// TagSetupFailed is the tag of the failure to make the WebRTC API of the
	// media server.
	TagSetupFailed = "MediaSetupFailed"
)
View Source
const (
	// CloseSessionEnded is the code of a socket whose session is gone.
	CloseSessionEnded websocket.StatusCode = 4001
	// CloseSessionChanged is the code of a socket whose session changed (the
	// role of the user); a client may join again.
	CloseSessionChanged websocket.StatusCode = 4002
)

The close codes of a signalling socket besides the standard ones. 4001 and 4002 are those of the WebSocket of the API (SPEC D44).

View Source
const TagNegotiationFailed = "MediaNegotiationFailed"

TagNegotiationFailed is the tag of a failure to make, send or apply an offer of the media server.

View Source
const TagNoRelay = "MediaNoRelay"

TagNoRelay is the tag of a socket whose call is held by another process, on a server that has no relay to reach it.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Addr is the address to listen on, as host:port, such as ":7882": one
	// UDP port and one TCP port with that number (SPEC D64). A port of 0
	// chooses one; Server.Addr tells which.
	Addr string
	// PublicIP is announced as the address of the server in the host
	// candidates, when the server runs behind NAT; empty announces the
	// addresses of the machine.
	PublicIP string
	// IncludeLoopback makes the server announce loopback addresses too, which
	// the tests and a local trial on 127.0.0.1 need. A loopback PublicIP does
	// it as well.
	IncludeLoopback bool
	// ProcessID is the ID of this process, the one the service of the calls
	// records as the home of the calls it starts (SPEC D65). A socket whose
	// call has another home is relayed to that process (Relay). Empty means a
	// process that is alone: every call is its own.
	ProcessID string
	// Relay carries the signalling of the participants whose socket reached
	// this process while their call is held by another one (SPEC D65); nil
	// when the process runs alone. It needs ProcessID.
	Relay Relay
	// Logger receives the state changes and the failures; nil logs nothing.
	// Nothing of the media, no SDP and no candidate is logged at info level.
	Logger *slog.Logger
	// ICETimeout, DisconnectTimeout, ReconnectGrace and JoinTimeout are the
	// deadlines of SPEC D64; zero takes the default. Tests make them short.
	ICETimeout        time.Duration
	DisconnectTimeout time.Duration
	ReconnectGrace    time.Duration
	JoinTimeout       time.Duration
	// MaxVideoBitrate is the limit of video per sender, in bits per second;
	// zero takes the default.
	MaxVideoBitrate uint64
	// SocketsOpen counts the signalling sockets that are open (the gauge of
	// kind "call" of clowder_websockets_open, SPEC D60). Nil counts nothing.
	SocketsOpen *metrics.Gauge
	// Outcomes counts the calls whose media this process held, when each
	// closes, by how it ended (clowder_calls_total); its one label is the
	// outcome. Nil counts nothing.
	Outcomes *metrics.CounterVec
	// PingInterval, PingTimeout and WriteTimeout bound a signalling socket;
	// zero takes the default.
	PingInterval time.Duration
	PingTimeout  time.Duration
	WriteTimeout time.Duration
}

Config holds the settings of the media server.

type Controller

type Controller interface {
	// Status reads the state of the call. A call that does not exist is not
	// open.
	Status(ctx context.Context, callID uint64) (Status, error)
	// Fail ends the call as failed: its media did not connect or was lost.
	Fail(ctx context.Context, callID uint64) error
	// SetMedia tells both users that a participant turned the microphone or the
	// camera on or off.
	SetMedia(ctx context.Context, callID, userID uint64, microphone, camera bool) error
	// Leave is a participant ending the call from the signalling socket: the
	// caller cancels a ringing call and either side hangs up an active one.
	Leave(ctx context.Context, callID, userID uint64) error
}

Controller is what the media server asks of the service of the calls (package app implements it): the state of a call, and the changes that the media server causes. It makes no permission check of its own; the user has been checked before the socket was attached.

type Envelope

type Envelope struct {
	// Kind is one of the Env constants.
	Kind string `json:"kind"`
	// From is the ID of the process that sent the envelope: for a join, the
	// edge, to which the home answers.
	From string `json:"from"`
	// CallID and UserID say whose socket it is, and Socket which one: every
	// socket has an ID of its own, made by the edge, so that the home can tell
	// a socket from the one that replaced it.
	CallID uint64 `json:"call_id"`
	UserID uint64 `json:"user_id"`
	Socket string `json:"socket"`
	// Join is set for EnvJoin: the call, as the edge's handler checked it.
	Join *EnvelopeJoin `json:"join,omitempty"`
	// Message is the signalling message of EnvClient and EnvServer.
	Message json.RawMessage `json:"message,omitempty"`
	// Code and Reason are the close code and reason of EnvClose.
	Code   int    `json:"code,omitempty"`
	Reason string `json:"reason,omitempty"`
}

Envelope is one message of the relay. The signalling message it carries is the JSON of the WebSocket, unchanged.

type EnvelopeJoin

type EnvelopeJoin struct {
	ConversationID uint64 `json:"conversation_id"`
	CallerID       uint64 `json:"caller_id"`
	CalleeID       uint64 `json:"callee_id"`
}

EnvelopeJoin is the part of a Join that the home needs to know the call.

type Events

type Events interface {
	// Subscribe returns a subscription to the topics that ends with ctx.
	Subscribe(ctx context.Context, topics ...string) *live.Subscription
}

Events delivers the live events of the topics (the hub of package live is one). The media server listens to the user topics of the two participants of each call for the end of the call and for the end of a session.

type Join

type Join struct {
	// CallID is the call and ConversationID its direct conversation.
	CallID         uint64
	ConversationID uint64
	// CallerID and CalleeID are the two participants; UserID, who joins, is
	// one of them.
	CallerID uint64
	CalleeID uint64
	UserID   uint64
	// Home is the ID of the process that holds the media of the call (SPEC
	// D65). When it is not this process (Config.ProcessID) the socket is
	// relayed to it; empty means this process.
	Home string
	// Session checks the session of the socket again, as D44 says. It returns
	// 0 while the session is good, else the close code that ends the socket
	// (4001 session ended, 4002 session changed) and its reason. Nil means
	// the session is never checked.
	Session func(ctx context.Context) (code websocket.StatusCode, reason string)
}

Join says which call a signalling socket joins and who joins it. The web and the API handlers fill it after they have authenticated the user and found, with the service of the calls, that the user is a participant of the call and may join it now.

type Relay

type Relay interface {
	// Send delivers the envelope to the process named to. It fails when the
	// transport fails or when no process listens there.
	Send(ctx context.Context, to string, env Envelope) error
	// Messages yields the envelopes addressed to this process, in the order
	// they arrived. It is closed when the relay is.
	Messages() <-chan Envelope
}

Relay carries the signalling between the processes of a cluster (SPEC D65, D41). A participant whose socket reached a process that does not hold the call is relayed to the home through it, one Envelope for each message in each direction. internal/cluster implements it with Redis, so that this package stays free of Redis; no media ever goes through it.

type Server

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

Server is the media server: it holds the UDP and TCP ports of the calls, the one webrtc.API that makes every PeerConnection, and the calls that are being forwarded. Make it with New and stop it with Close, which closes every PeerConnection and socket and waits for every goroutine it started.

func New

func New(ctx context.Context, cfg Config, ctl Controller, events Events) (*Server, error)

New opens the UDP and TCP ports of cfg.Addr, makes the one webrtc.API of the server and returns the server. ctx owns the server: when it is done the calls are closed, and Close must still be called to wait for them. It fails with TagListenFailed when the ports cannot be opened and with TagSetupFailed when the API cannot be made.

func (*Server) Addr

func (s *Server) Addr() (udp, tcp net.Addr)

Addr returns the UDP and TCP addresses the server listens on, which differ only when the configured port was 0 and the system chose.

func (*Server) Calls

func (s *Server) Calls() int

Calls returns the number of calls the server is forwarding.

func (*Server) Close

func (s *Server) Close() error

Close stops the server: every call is closed (its PeerConnections and sockets), the ports are released and every goroutine that the server started has ended when it returns. It is safe to call twice. The service of the calls ends the calls in the database (Calls.EndOpen); the media server does not.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, conn *websocket.Conn, j Join)

Serve runs the signalling socket of one participant until it is over: the call has ended, the client or the server closed the connection, or the session ended. The handler that accepted the WebSocket calls it and returns when it does; Serve closes the connection. Only a user who is a participant of the call, and whom the service allows to join it now, may be given to Serve: it checks nothing of that itself.

The server is the offerer: Serve joins the socket to the call, which makes the PeerConnection of the participant and sends the offer. The client answers, exchanges ICE candidates in both directions, sends its state (microphone and camera) and may hang up. A second socket of the same participant replaces the first, and with it the PeerConnection. A socket that closes while the call is active gives the participant the reconnect grace to open a new one before the call ends as failed.

When the call is held by another process (Join.Home, SPEC D65) this process is the participant's edge: it keeps the socket, its pings, its session checks and its close codes, and relays the messages to the home through the Relay; the media of the participant goes straight to the home's own port.

type Status

type Status struct {
	// Open is true while the call is ringing or active.
	Open bool
	// Active is true once the callee has answered and while the call is open.
	Active bool
	// Reason is how the call ended (hangup, declined, failed, ...); empty
	// while it is open.
	Reason string
}

Status is what the media server asks about a call.

Directories

Path Synopsis
Package callstest is the client of the tests of the calls: a pion peer that takes part in a call through the signalling socket of the server, sends synthetic Opus and VP8 packets and counts what it receives.
Package callstest is the client of the tests of the calls: a pion peer that takes part in a call through the signalling socket of the server, sends synthetic Opus and VP8 packets and counts what it receives.

Jump to

Keyboard shortcuts

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