rtc

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 22 Imported by: ? 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 rtc makes audio and video calls and data connections over WebRTC, on pion. It doesn't do signaling: the app carries the offer, the answer and the candidates between the peers, over its own server.

c, _ := rtc.New(ctx, app, rtc.Config{
	ICEServers: []rtc.ICEServer{{URLs: []string{"stun:stun.example.com:3478"}}},
	Microphone: true, Speaker: true,
	OnCandidate: func(c string) { signal.Send("candidate", c) },
})
offer, _ := c.Offer(ctx)   // send it; the other side calls Answer
...
c.Accept(answer)           // their answer
c.AddCandidate(candidate)  // each of theirs

Audio is G.711 (PCMU, PCMA) out of the box, which every WebRTC peer speaks; Config.AudioCodecs adds others, such as Opus. Video needs a VideoCodec: frames go out through SendVideo and arrive on RemoteVideo.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AudioCodec

type AudioCodec interface {
	// MimeType is "audio/opus", "audio/PCMU"...
	MimeType() string
	ClockRate() int
	Channels() int
	// Fmtp is the SDP format parameters line, or "".
	Fmtp() string
	PayloadType() uint8
	// SampleRate is the rate of the PCM it encodes and decodes.
	SampleRate() int
	NewEncoder() (AudioEncoder, error)
	NewDecoder() (AudioDecoder, error)
}

AudioCodec is an audio codec a call can negotiate.

var PCMA AudioCodec = g711{/* contains filtered or unexported fields */}

PCMA is G.711 A-law, built in.

var PCMU AudioCodec = g711{/* contains filtered or unexported fields */}

PCMU is G.711 μ-law, built in.

type AudioDecoder

type AudioDecoder interface {
	Decode(payload []byte) ([]int16, error)
	Close()
}

AudioDecoder decodes a packet's payload.

type AudioEncoder

type AudioEncoder interface {
	Encode(pcm []int16) ([]byte, error)
	Close()
}

AudioEncoder encodes 20 ms of 16-bit PCM at a time.

type Call

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

Call is one peer connection.

func New

func New(ctx context.Context, app *giode.App, cfg Config) (*Call, error)

New makes a call. Then one side calls Offer and the other Answer.

func (*Call) Accept

func (c *Call) Accept(answer string) error

Accept takes the other side's answer to this side's offer.

func (*Call) AddCandidate

func (c *Call) AddCandidate(candidate string) error

AddCandidate adds one of the other side's ICE candidates, as its OnCandidate gave it.

func (*Call) Answer

func (c *Call) Answer(ctx context.Context, offer string) (string, error)

Answer takes the other side's offer and makes the answer to send back.

func (*Call) AudioCodec

func (c *Call) AudioCodec() string

AudioCodec is the codec audio is sent with, once negotiated.

func (*Call) AudioSampleRate

func (c *Call) AudioSampleRate() int

AudioSampleRate is the rate WriteAudio takes, once negotiated.

func (*Call) Close

func (c *Call) Close()

Close hangs up.

func (*Call) Muted

func (c *Call) Muted() bool

Muted reports whether audio is muted.

func (*Call) Offer

func (c *Call) Offer(ctx context.Context) (string, error)

Offer makes the offer to send to the other side.

func (*Call) OpenDataChannel

func (c *Call) OpenDataChannel(label string, ordered bool) (*DataChannel, error)

OpenDataChannel opens a data channel; ordered delivers messages in order, retransmitting lost ones.

func (*Call) RemoteVideo

func (c *Call) RemoteVideo() *video.Surface

RemoteVideo is where the other side's video shows.

func (*Call) SendVideo

func (c *Call) SendVideo(frame *image.RGBA) error

SendVideo sends a frame, timed at Config.VideoFPS.

func (*Call) SetMuted

func (c *Call) SetMuted(m bool)

SetMuted stops or resumes sending audio; silence goes out instead.

func (*Call) State

func (c *Call) State() State

State is the connection's state.

func (*Call) Stats

func (c *Call) Stats() Stats

Stats are the call's counters.

func (*Call) WriteAudio

func (c *Call) WriteAudio(pcm []int16) error

WriteAudio sends PCM at the negotiated codec's sample rate (see AudioSampleRate), from a source other than the microphone.

type Config

type Config struct {
	ICEServers []ICEServer
	// RelayOnly sends everything through TURN, hiding the peers'
	// addresses from each other.
	RelayOnly bool

	// Microphone sends the microphone, with the platform's echo
	// cancellation, noise suppression and gain control.
	Microphone bool
	// MicrophoneDevice is an audio.Inputs ID; the default when empty.
	MicrophoneDevice string
	// Speaker plays the other side; SpeakerPhone uses the loudspeaker
	// on phones.
	Speaker, SpeakerPhone bool
	// SendAudio offers to send audio without the microphone: write it
	// with WriteAudio.
	SendAudio bool
	// OnRemoteAudio receives the other side's audio, decoded, as it
	// arrives (as well as playing it with Speaker).
	OnRemoteAudio func(pcm []int16, sampleRate int)
	// AudioCodecs are preferred over the built-in G.711, in order.
	AudioCodecs []AudioCodec

	// VideoCodecs are the video codecs to offer, in order; without
	// one, there's no video.
	VideoCodecs []VideoCodec
	// SendVideo offers to send video, written with SendVideo.
	SendVideo bool
	// VideoFPS is the rate SendVideo frames are timed at: 30 by
	// default.
	VideoFPS int

	// OnCandidate gets each local ICE candidate, to send to the other
	// side as it's found (trickle ICE). Without it, Offer and Answer
	// wait until all candidates are found and include them.
	OnCandidate func(candidate string)
	// OnState is called as the connection changes.
	OnState func(State)
	// OnDataChannel is called when the other side opens a data channel.
	OnDataChannel func(*DataChannel)
	// contains filtered or unexported fields
}

Config configures New.

type DataChannel

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

DataChannel carries messages between the peers.

func (*DataChannel) Close

func (d *DataChannel) Close() error

Close closes it.

func (*DataChannel) Label

func (d *DataChannel) Label() string

Label is the name it was opened with.

func (*DataChannel) OnClose

func (d *DataChannel) OnClose(fn func())

OnClose is called when it closes.

func (*DataChannel) OnMessage

func (d *DataChannel) OnMessage(fn func(data []byte, text bool))

OnMessage is called with each message; text tells text from binary.

func (*DataChannel) OnOpen

func (d *DataChannel) OnOpen(fn func())

OnOpen is called when it can send.

func (*DataChannel) Send

func (d *DataChannel) Send(b []byte) error

Send sends binary data.

func (*DataChannel) SendText

func (d *DataChannel) SendText(s string) error

SendText sends a text message.

type ICEServer

type ICEServer struct {
	URLs       []string
	Username   string
	Credential string
}

ICEServer is a STUN or TURN server.

type State

type State int

State is a call's connection state.

const (
	StateNew State = iota
	StateConnecting
	StateConnected
	// StateDisconnected is a lost connection that may come back.
	StateDisconnected
	StateFailed
	StateClosed
)

func (State) String

func (s State) String() string

type Stats

type Stats struct {
	AudioSent, AudioReceived int // packets
	VideoSent, VideoReceived int // frames
	// RemoteLevel is the other side's loudness, 0 to 1, for a meter.
	RemoteLevel float64
	// Since is when the call connected.
	Since time.Time
}

Stats are a call's counters.

type VideoCodec

type VideoCodec interface {
	// MimeType is "video/VP8", "video/H264"...
	MimeType() string
	Fmtp() string
	PayloadType() uint8
	NewEncoder(w, h, fps int) (VideoEncoder, error)
	NewDecoder() (VideoDecoder, error)
}

VideoCodec is a video codec a call can negotiate: VP8, VP9, H.264 or AV1, whose packets the call splits and joins.

var VP8 VideoCodec = vp8Codec{/* contains filtered or unexported fields */}

VP8 is a built-in video codec in pure Go: every frame is a key frame, so it needs more bandwidth than a full encoder (about 1 to 2 Mbit/s for 640×480 at 15 frames a second) but runs everywhere, and a lost packet costs only one frame. Its decoder takes key frames only: between Giode apps that's every frame; from a browser, which sends key frames only when asked, the picture updates about once a second.

func VP8Quality

func VP8Quality(q int) VideoCodec

VP8Quality is VP8 with a quantizer from 0 (best, biggest) to 127.

type VideoDecoder

type VideoDecoder interface {
	// Decode decodes a frame, or returns nil while it waits for a key
	// frame.
	Decode(frame []byte) (image.Image, error)
	Close()
}

VideoDecoder decodes a frame's bitstream.

type VideoEncoder

type VideoEncoder interface {
	// Encode encodes a frame; keyframe asks for a key frame, which a
	// peer that lost packets needs.
	Encode(frame *image.RGBA, keyframe bool) ([]byte, error)
	Close()
}

VideoEncoder encodes frames into the codec's bitstream (Annex B for H.264).

Jump to

Keyboard shortcuts

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