rtc

package
v0.0.0-...-831fea5 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package rtc wraps Pion: one Engine per process (codec + ICE settings), one Peer per browser connection. It reproduces the SmallWebRTCConnection contract: non-trickle answers (ICE gathering completes before the answer is returned), an opaque pc_id, renegotiation on re-offer, and a "closed" event when the connection ends for any reason.

Index

Constants

View Source
const FrameSamples = 960

FrameSamples is a 20 ms Opus frame at 48 kHz.

View Source
const OpusClockRate = 48000

OpusClockRate is fixed by RFC 7587.

Variables

This section is empty.

Functions

func NewPeerID

func NewPeerID() string

NewPeerID generates an opaque pc_id. It only has to be unique and URL-safe (the frontend passes it back in the transcript websocket path).

Types

type Engine

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

Engine builds peers.

func NewEngine

func NewEngine(cfg EngineConfig) (*Engine, error)

NewEngine registers Opus (48 kHz, 2 ch as the SDP requires; we send mono packets, which Opus/browsers handle) and the default RTCP interceptors.

func (*Engine) NewPeer

func (e *Engine) NewPeer(id string, offerSDP, offerType string, h PeerHandler) (*Peer, *webrtc.SessionDescription, error)

NewPeer creates a peer connection, applies the browser's offer and returns the complete (non-trickle) answer.

type EngineConfig

type EngineConfig struct {
	ICEServers []config.ICEServer
	UDPPortMin int
	UDPPortMax int
	NAT1To1IPs []string
	Logger     *slog.Logger
}

EngineConfig configures the shared API.

type Peer

type Peer struct {
	ID string
	// contains filtered or unexported fields
}

Peer is one browser connection.

func (*Peer) Close

func (p *Peer) Close()

Close tears the connection down; OnClosed fires (once) as a result.

func (*Peer) Connected

func (p *Peer) Connected() bool

Connected reports whether the peer is currently connected.

func (*Peer) Done

func (p *Peer) Done() <-chan struct{}

Done is closed when the peer has ended.

func (*Peer) Renegotiate

func (p *Peer) Renegotiate(offerSDP, offerType string) (*webrtc.SessionDescription, error)

Renegotiate applies a new offer on the existing connection (ICE restart is handled by Pion when the remote ufrag/pwd change).

func (*Peer) Stats

func (p *Peer) Stats() (received, concealed, fec, dropped uint64)

Stats returns inbound counters (for logs/metrics).

func (*Peer) WriteOpus

func (p *Peer) WriteOpus(payload []byte, timestamp uint32, marker bool) error

WriteOpus sends one encoded frame with the given RTP timestamp. The caller owns the timestamp clock (advance by FrameSamples per 20 ms whether or not a packet is sent) so silence gaps stay aligned with wall time.

type PeerHandler

type PeerHandler interface {
	OnAudio(pcm []int16)
	OnConnected()
	OnClosed()
}

PeerHandler receives connection events. OnAudio runs on the peer's reader goroutine with 48 kHz mono PCM for each decoded (or concealed) frame; the slice is only valid for the duration of the call.

Jump to

Keyboard shortcuts

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