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 ¶
- type AudioCodec
- type AudioDecoder
- type AudioEncoder
- type Call
- func (c *Call) Accept(answer string) error
- func (c *Call) AddCandidate(candidate string) error
- func (c *Call) Answer(ctx context.Context, offer string) (string, error)
- func (c *Call) AudioCodec() string
- func (c *Call) AudioSampleRate() int
- func (c *Call) Close()
- func (c *Call) Muted() bool
- func (c *Call) Offer(ctx context.Context) (string, error)
- func (c *Call) OpenDataChannel(label string, ordered bool) (*DataChannel, error)
- func (c *Call) RemoteVideo() *video.Surface
- func (c *Call) SendVideo(frame *image.RGBA) error
- func (c *Call) SetMuted(m bool)
- func (c *Call) State() State
- func (c *Call) Stats() Stats
- func (c *Call) WriteAudio(pcm []int16) error
- type Config
- type DataChannel
- func (d *DataChannel) Close() error
- func (d *DataChannel) Label() string
- func (d *DataChannel) OnClose(fn func())
- func (d *DataChannel) OnMessage(fn func(data []byte, text bool))
- func (d *DataChannel) OnOpen(fn func())
- func (d *DataChannel) Send(b []byte) error
- func (d *DataChannel) SendText(s string) error
- type ICEServer
- type State
- type Stats
- type VideoCodec
- type VideoDecoder
- type VideoEncoder
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 ¶
AudioDecoder decodes a packet's payload.
type AudioEncoder ¶
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 (*Call) AddCandidate ¶
AddCandidate adds one of the other side's ICE candidates, as its OnCandidate gave it.
func (*Call) AudioCodec ¶
AudioCodec is the codec audio is sent with, once negotiated.
func (*Call) AudioSampleRate ¶
AudioSampleRate is the rate WriteAudio takes, once negotiated.
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 ¶
RemoteVideo is where the other side's video shows.
func (*Call) WriteAudio ¶
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) 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) SendText ¶
func (d *DataChannel) SendText(s string) error
SendText sends a text message.
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.