Files
ee75008bea Generate downstream FlexFEC with per-track protection modes (#5195)
* Do not recover FlexFEC from RTX repaired packets

A packet repaired from RTX takes its header from the RTX packet: libwebrtc
adds a MID to retransmissions until the RTX SSRC is acknowledged, and sets
abs-send-time and transport-cc at send time. The stored packet therefore
differs from the media the publisher protected, so using it as a protected
packet recovered the wrong length, failing with "invalid recovered packet
size", or silently corrupted the recovered packet's header extensions when
the length happened to match.

Like libwebrtc's FlexfecReceiver, which skips packets recovered by RTX, mark
RTX repaired sequence numbers and treat them as missing for FEC recovery.
They are still forwarded and update NACK state as before.

With a libwebrtc publisher at 10% upstream loss, every recovery attempt in a
block containing a retransmitted packet failed; with retransmissions excluded
recoveries succeed.

* Move RTX repair tracking into BufferBase

Keep the RTX repaired history, its lookup and the publisher sequence number
extension in BufferBase, so every buffer built on it, such as the relay, can
exclude RTX repaired packets from FlexFEC recovery the same way. The history
is now cleared with the rest of the sequence number state when the stream
restarts.

Buffer only marks repaired packets from writeRTX. Packets recovered by FEC
also enter with isRTX set, but they are exact reconstructions and stay
usable, so marking is explicit rather than tied to that flag.

* Keep RTX-repaired history safe with large packet caches

The RTX repair history had 512 fixed slots. With a larger video packet
cache, e.g. packet_buffer_size_video: 1500, two cached repairs could share
a slot, the earlier mark was lost, and FlexFEC could recover from the
altered retransmission.

- Size the history from the video packet cache, at least 512 slots, and
  allocate it on the first repair.
- When a later repair reuses a slot, the earlier packet counts as repaired:
  losing a recovery is better than corrupting one.
- Do not mark a retransmission of a packet that is already cached. The
  bucket rejects it as a duplicate and keeps the exact packet, which stays
  usable for recovery.
- Mark a repair before ingesting it and do not feed RTX repaired packets to
  the FEC decoder, as BufferBase now feeds ingested media to the decoder.

* Generate downstream FlexFEC with per-track protection presets

Generate FlexFEC-03 repairs for subscribed video tracks. Subscribers select
none (default), low, medium or high through UpdateTrackSettings.fec; repairs
require rtc.flexfec.downstream_enabled and negotiated flexfec-03 support.

Generate repairs at each frame marker, splitting large frames into blocks of
at most 48 packets, with upstream Q8 repair-count rounding and at least one
repair per block. Reserve measured repair overhead during layer allocation
and charge repair traffic to pacing, bandwidth estimation and metrics.

Carry repair sequence state through the DownTrack GetState/SeedState handoff,
apply subscriber settings from immutable snapshots, and preserve upstream FEC
on shared sendrecv codec lists when downstream generation is disabled.

This squashes the three commits of dc/feature/fec_down (38a67502, d269ac59,
7b02897c) and rebases them onto master. Conflict resolution:
- pacer.Packet keeps master's held-extension scratch next to the FEC field,
  and the DownTrack sets FEC on the pacer packet master now creates before
  adding extensions
- DownTrackParams and subscriber config keep master's RTXBudget
- configureSenderParams uses master's enableFlexFEC name
- tests follow master: pion/transport v5 packetio, mid-less receivers skipped
  when restricting codecs, and settings compared by value since they are
  stored as merged snapshots

Requires the UpdateTrackSettings.fec field from livekit/protocol#1794, which
is not in the pinned protocol version yet.

* Add end-to-end tests for downstream FlexFEC

Publish a VP8 track through the SFU to a FlexFEC-capable subscriber,
simulate downstream loss before the subscriber's receive buffer, and recover
the dropped packets from the repairs actually sent, using the same FlexFEC-03
decoder as upstream recovery. RTX is dropped meanwhile so retransmissions
cannot fill the gaps.

The test checks that no repairs are sent before a preset is selected, that
every preset recovers isolated losses bit-exactly, that presets change
without renegotiation, and that a subscriber which declines flexfec-03
receives media but no repairs. Frames alternate between one and five
packets, so repair counts match the Q8 rounding for both block sizes.

The test client gains a subscriber RTP filter to simulate loss and accessors
for the subscriber descriptions. The tests use their own ports so they can
run next to a local server on the default ports.

* Support downstream FEC protection modes

Adopt the FECProtection message from livekit/protocol#1987: subscribers
select a mode (disabled, adaptive, fixed, minimum) and a level (low,
medium, high; unspecified is low) per track. As before, an update that
omits fec keeps the previous setting. Unknown modes disable FEC and
unknown levels are treated as unspecified.

The modes follow libwebrtc's RtpParameters::fec_protection
(webrtc-sdk/webrtc#329):
- adaptive ports VCMNackFecMethod: the kFecRateTable lookup with a 10
  second max-window loss filter, key frame boost, ConvertFECRate, NACK
  only delta frames below 20ms RTT, and BitRateTooLowForFec. Inputs are
  the subscriber's receiver reports, the sent bit and frame rates, and
  the forwarded layer's dimensions.
- fixed uses the level for delta and key frames.
- minimum uses the larger of the adaptive rate and the level.
- the adaptive state is updated in every mode, so switching to adaptive
  or minimum starts from the current loss history, and a protection
  change keeps the group that is accumulating and protects it at the
  new rate.

Key frames get their own protection factor, so pacer packets carry a
key frame flag. Repairs are still generated per frame; multi-frame
blocks (max_fec_frames) are not supported.

The end-to-end tests report the simulated loss in receiver reports,
since the test client sends none, and check that adaptive protection
sends no repairs without loss and protects with loss, and that minimum
protection matches its level without loss and rises with loss.

Requires livekit/protocol#1987, which is not in the pinned protocol
version yet.

* Point protocol at livekit/protocol#1987

* Report loss before repair in receiver reports to publishers

Receiver reports counted packets restored by RTX or recovered by FlexFEC
as received, so as long as repairs kept up the reported fraction lost
stayed at zero, even with 20% upstream loss. libwebrtc receivers exclude
recovered packets from their receive statistics, and a publisher's
loss-based protection depends on that: with no reported loss, FlexFEC
backs off to almost no repairs.

Packets that fill a loss through a repair are now counted separately, and
receiver reports include them in the fraction and cumulative loss.
Reordered packets that arrive on their own still cancel the loss. The
fraction is also capped at 255, so a full loss no longer wraps to zero.

With 20% simulated upstream loss from an Android publisher, the reported
fraction lost goes from 0 to about 0.15, the FEC packets received over 30
seconds go from 64 to 204, and the packets recovered by FEC from 11 to 61.

* Protect zeroed abs-send-time and TWCC in downstream FlexFEC

The pacer encoded FEC after it had filled in abs-send-time and the
transport-wide sequence number. libwebrtc generates FEC before its sender
fills them in, and its FlexFEC receiver zeroes both in media packets before
recovery. Packets recovered from SFU repairs therefore got wrong bytes in
those extensions, and in the payload when the header lengths within a group
differed, for example when only the first packet carried abs-capture-time.

Zero both extensions after the media packet is written and before it is
fed to the encoder.

* Aggregate receiver report loss for adaptive FEC protection

Adaptive and minimum protection fed the fraction lost of every reception
report into the max-window loss filter. libwebrtc receivers can send a
report with each NACK, so a report often covers only a few packets and its
fraction lost is very noisy. With 10% random loss, single reports read up
to 70%, and the filter kept the maximum.

Derive loss from the cumulative counters instead and produce a sample only
once it covers at least 20 expected packets, like libwebrtc's
SendSideBandwidthEstimation::UpdatePacketsLost. The end-to-end tests now
report cumulative loss and the extended highest sequence number in their
receiver reports.

* Report loss before repair only to FlexFEC publishers

Counting repaired packets as lost in receiver reports applied to every
publisher, so any publisher whose loss RTX repaired started to see that loss
and its bandwidth estimate could back off sooner. Only FlexFEC protection
needs loss before repair to choose its rate.

Count repairs only for streams that negotiated flexfec-03, which requires
FlexFEC to be enabled on the server. Other publishers keep receiving loss
after repair, as before.

* Keep the newest RTX repair mark of a history slot

A late retransmission of an older packet that has aged out of the cache
overwrote the slot of a newer repair sharing it. The newer repair, which
carries the RTX packet's header, could then be used for FlexFEC recovery and
corrupt the recovered packet.

Keep the highest extended sequence number written to a slot until the stream
restarts. isRTXRepairedLocked already treats an older packet in a slot with a
newer mark as repaired.

* Measure FEC overhead against all media

The allocator scales the whole media bitrate by the measured repair
overhead, but the overhead was measured against protected media only. With
adaptive protection below 20 ms RTT only key frames are protected, so the
ratio of key frame repairs to key frame media was applied to every frame,
and the reservation could make the allocator lower the layer or pause the
track although the bandwidth was sufficient.

Count all non-padding media while any protection is active.

* Keep the loss sample that closes a filter window

The max-window loss filter, ported from libwebrtc, dropped the report that
closes a one-second window when the window already had a maximum, so a loss
spike arriving on the boundary never reached adaptive protection. Store the
larger of the window's maximum and that sample.

* Skip the encoder allocation check under the race detector

CI runs the tests with -race and swaps sync.Mutex for go-deadlock, whose
Lock allocates, so Encode allocated about once per call there. Check
allocations only without the race detector, like the other allocation tests.

* Bump protocol to v1.55.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: reserve measured FEC overhead only on the forwarded layer

The measured repair overhead comes from the spatial layer being
forwarded. On a low layer with one or two packets per frame it can be
60-100%, and applying it to every layer reserved the high layer at
nearly twice its cost, so the allocator could stay on the low layer.

Attribute measurements to the forwarded spatial layer, restarting them
when forwarding starts, resumes or switches layers, and reserve the
configured rate for the other layers.

Also reallocate only when a reserved overhead moves by 5 points or
more, or turns on or off, so that per-second measurements and adaptive
protection do not re-run allocation on every receiver report.

* buffer: gate upstream FlexFEC on an explicit flag

A publisher PC registers flexfec-03 when only downstream FEC is enabled,
because it can also carry subscriptions. Its buffers then got a FEC
payload type, which reported loss before repair to the publisher and
let the decoder start from the observed repair SSRC, although upstream
FEC was disabled.

Buffers now ignore the negotiated FlexFEC payload type unless the buffer
factory enables upstream FlexFEC.

* pacer: protect interceptor-written transport-cc as zeros in FEC

With UseSendSideBWEInterceptor, Pion's TWCC header extension
interceptor writes transport-cc during WriteRTP while the DownTrack
leaves TransportWideExtID at zero. The extension was then protected
with its non-zero value, which the receiver zeroes before recovery, so
recovered packets got wrong bytes there.

Pass the negotiated transport-cc ID to the pacer for FEC and zero it
before encoding as well.

* sfu: skip the FEC encoder while protection is disabled

The encoder was attached whenever flexfec-03 was negotiated, including
in the default disabled mode, so every forwarded packet took the
encoder lock and updated the adaptive state without generating repairs.

Pass the encoder to the pacer only while protection is enabled. Receiver
reports still keep the loss history; adaptive protection measures media
rates again once enabled.

* sfu: account sent FEC repairs in the down track RTP stats

Repairs were counted only in the aggregate Prometheus counter, so the
down track's stats under-counted what the subscriber receives. Record
them in the sender's FEC packet and byte counters, separate from media,
and export the FEC counters in RTPStats for both directions.

* rtc: merge subscriber FEC settings in one place

The subscription merged and normalized settings, and the subscribed
track merged and normalized them again, while the DownTrack mapped
unknown modes and levels a third time.

Merge only in the subscription, store the merged settings in the
subscribed track as is, and leave unknown values to the DownTrack.

* prometheus: resolve fixed-label FEC counters once

RecordFECDownstream runs on the pacer path for every repair batch and
looked up both counter vectors by label each time.

* rtpstats: seed a sender without holding both locks

Seed took the receiving sender's lock and then the source's, so two
senders seeding from each other at the same time could deadlock. Copy
the source's state under its own lock first, then apply the copy under
the receiving sender's lock.

* telemetry: count FlexFEC repairs in traffic totals

Retransmissions and padding count towards node and participant
traffic, but FEC repairs, which are also on the wire, did not. Add
repairs sent and received to the node packet and byte totals, and to
the packets and bytes of TrafficStats.

Analytics streams are unchanged: AnalyticsStream has no FEC field.

* sfu: read the FEC transport-cc ID atomically

setRTPHeaderExtensions runs again when the stream allocator listener is
set, which can happen while media is already forwarded, and WriteRTP
read the ID without synchronization. Store it atomically once the
extensions are processed.

* telemetry: count only FEC repairs sent in node traffic

Media recovered from FEC repairs is counted as received media, so
adding the repairs received to node ingress counted recovered packets
twice. Node traffic feeds room admission against limit.bytes_per_sec,
so ingress must not be inflated. Keep repairs sent in node egress,
where nothing is synthesized, and leave ingress as before.

TrafficStats is derived from RTPStats without a direction, so it cannot
tell repairs sent from repairs received; leave it without FEC.

* rtc: add WebRTCConfig.SetFlexFEC for per-session FlexFEC settings

* rtc: apply a session's upstream FlexFEC to its buffers

SetFlexFEC changed what a session negotiates with its publisher, but the
publisher's buffers still followed the node config: the room's buffer
factory read it once. A session with upstream on and a node with it off
negotiated flexfec-03, so the publisher sent repairs that its buffers
ignored; the reverse turned on upstream recovery and loss reported before
repair for a session that asked for neither.

The session's buffer factory now follows the session's upstream setting,
whichever of SetFlexFEC and SetBufferFactory comes first. Without a
SetFlexFEC call it follows the node config, as before. The setting applies
to buffers the factory creates afterwards, so SetFlexFEC is meant to be
called while the session is set up, before any track is published.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Jules Testard <jules.testard@livekit.io>
Co-authored-by: David Chen <chenosaurus@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 14:55:27 +01:00

1515 lines
43 KiB
Go

// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"math/rand/v2"
"net/http"
"net/url"
"path/filepath"
"runtime"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/transport/v5/packetio"
"github.com/pion/webrtc/v4"
"github.com/thoas/go-funk"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/livekit/mediatransportutil/pkg/rtcconfig"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/codecs/mime"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/signalling"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/transport/transportfakes"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/protocol/datatrack"
)
type SignalRequestHandler func(msg *livekit.SignalRequest) error
type SignalRequestInterceptor func(msg *livekit.SignalRequest, next SignalRequestHandler) error
type SignalResponseHandler func(msg *livekit.SignalResponse) error
type SignalResponseInterceptor func(msg *livekit.SignalResponse, next SignalResponseHandler) error
type RTCClient struct {
useSinglePeerConnection bool
id livekit.ParticipantID
conn *websocket.Conn
publisher *rtc.PCTransport
subscriber *rtc.PCTransport
enabledCodecs []*livekit.Codec
forceRelay bool
enableFlexFEC bool
subscriberRTPFilter func(pkt *rtp.Packet) bool
transportReady chan struct{}
// sid => track
localTracks map[string]webrtc.TrackLocal
trackSenders map[string]*webrtc.RTPSender
lock sync.Mutex
wsLock sync.Mutex
ctx context.Context
cancel context.CancelFunc
me *webrtc.MediaEngine // optional, populated only when receiving tracks
subscribedTracks map[livekit.ParticipantID][]*webrtc.TrackRemote
localParticipant *livekit.ParticipantInfo
remoteParticipants map[livekit.ParticipantID]*livekit.ParticipantInfo
signalRequestInterceptor SignalRequestInterceptor
signalResponseInterceptor SignalResponseInterceptor
icQueue [2]atomic.Pointer[webrtc.ICECandidate]
subscriberAsPrimary atomic.Bool
publisherFullyEstablished atomic.Bool
subscriberFullyEstablished atomic.Bool
pongReceivedAt atomic.Int64
// tracks waiting to be acked, cid => trackInfo
pendingPublishedTracks map[string]*livekit.TrackInfo
// remote tracks waiting to be processed
pendingRemoteTracks []*webrtc.TrackRemote
pendingTrackWriters []TrackWriter
// callbacks are set by tests while the client is already running, and read
// from the signal and data channel goroutines
onConnected atomic.Pointer[func()]
onDataReceived atomic.Pointer[func(data []byte, sid string)]
onDataUnlabeledReceived atomic.Pointer[func(data []byte)]
refreshToken string
// map of livekit.ParticipantID and last packet
lastPackets map[livekit.ParticipantID]*rtp.Packet
bytesReceived map[livekit.ParticipantID]uint64
subscriptionResponse atomic.Pointer[livekit.SubscriptionResponse]
nextDataTrackHandle atomic.Uint32
pendingPublishedDataTracks map[uint16]*livekit.DataTrackInfo
pendingDataTrackWriters []TrackWriter
subscribedDataTracks map[livekit.ParticipantID]map[uint16]*DataTrackRemote
}
var (
// minimal settings only with stun server
rtcConf = webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
extMimeMapping = map[string]string{
".ivf": mime.MimeTypeVP8.String(),
".h264": mime.MimeTypeH264.String(),
".ogg": mime.MimeTypeOpus.String(),
}
)
type Options struct {
AutoSubscribe bool
AutoSubscribeDataTrack bool
Publish string
Attributes map[string]string
ClientInfo *livekit.ClientInfo
DisabledCodecs []webrtc.RTPCodecCapability
TokenCustomizer func(token *auth.AccessToken, grants *auth.VideoGrant)
SignalRequestInterceptor SignalRequestInterceptor
SignalResponseInterceptor SignalResponseInterceptor
UseJoinRequestQueryParam bool
RTCServicePath string
ForceRelay bool
EnableFlexFEC bool
// SubscriberRTPFilter is called with every RTP packet received from the
// SFU before it reaches the receive buffer; returning false drops it.
// Used to simulate loss. The packet aliases the receive buffer, so clone
// anything that is kept.
SubscriberRTPFilter func(pkt *rtp.Packet) bool
}
func NewWebSocketConn(host, token string, opts *Options) (*websocket.Conn, error) {
rtcServicePath := "/rtc"
if opts != nil && opts.RTCServicePath != "" {
rtcServicePath = opts.RTCServicePath
}
parsedURL, err := url.Parse(host + rtcServicePath)
if err != nil {
return nil, err
}
requestHeader := make(http.Header)
SetAuthorizationToken(requestHeader, token)
connectUrl := parsedURL.String()
if opts != nil && opts.UseJoinRequestQueryParam {
clientInfo := &livekit.ClientInfo{
Os: runtime.GOOS,
Sdk: livekit.ClientInfo_GO,
Protocol: int32(types.CurrentProtocol),
}
if opts.ClientInfo != nil {
clientInfo = opts.ClientInfo
}
connectionSettings := &livekit.ConnectionSettings{
AutoSubscribe: opts.AutoSubscribe,
AutoSubscribeDataTrack: &opts.AutoSubscribeDataTrack,
}
joinRequest := &livekit.JoinRequest{
ClientInfo: clientInfo,
ConnectionSettings: connectionSettings,
ParticipantAttributes: opts.Attributes,
}
if marshalled, err := proto.Marshal(joinRequest); err == nil {
wrapped := &livekit.WrappedJoinRequest{
JoinRequest: marshalled,
}
if marshalled, err := proto.Marshal(wrapped); err == nil {
connectUrl += fmt.Sprintf("?join_request=%s", base64.URLEncoding.EncodeToString(marshalled))
}
}
} else {
connectUrl += fmt.Sprintf("?protocol=%d", types.CurrentProtocol)
sdk := "go"
if opts != nil {
connectUrl += fmt.Sprintf("&auto_subscribe=%t", opts.AutoSubscribe)
connectUrl += fmt.Sprintf("&auto_subscribe_data_track=%t", opts.AutoSubscribeDataTrack)
if opts.Publish != "" {
connectUrl += encodeQueryParam("publish", opts.Publish)
}
if len(opts.Attributes) != 0 {
data, err := json.Marshal(opts.Attributes)
if err != nil {
return nil, err
}
connectUrl += encodeQueryParam("attributes", base64.URLEncoding.EncodeToString(data))
}
if opts.ClientInfo != nil {
if opts.ClientInfo.DeviceModel != "" {
connectUrl += encodeQueryParam("device_model", opts.ClientInfo.DeviceModel)
}
if opts.ClientInfo.Os != "" {
connectUrl += encodeQueryParam("os", opts.ClientInfo.Os)
}
if opts.ClientInfo.Sdk != livekit.ClientInfo_UNKNOWN {
sdk = opts.ClientInfo.Sdk.String()
}
}
}
connectUrl += encodeQueryParam("sdk", sdk)
}
logger.Infow("connecting to", "url", parsedURL.String())
conn, _, err := websocket.DefaultDialer.Dial(connectUrl, requestHeader)
return conn, err
}
func SetAuthorizationToken(header http.Header, token string) {
header.Set("Authorization", "Bearer "+token)
}
func NewRTCClient(conn *websocket.Conn, useSinglePeerConnection bool, opts *Options) (*RTCClient, error) {
c := &RTCClient{
useSinglePeerConnection: useSinglePeerConnection,
conn: conn,
localTracks: make(map[string]webrtc.TrackLocal),
trackSenders: make(map[string]*webrtc.RTPSender),
pendingPublishedTracks: make(map[string]*livekit.TrackInfo),
subscribedTracks: make(map[livekit.ParticipantID][]*webrtc.TrackRemote),
remoteParticipants: make(map[livekit.ParticipantID]*livekit.ParticipantInfo),
me: &webrtc.MediaEngine{},
lastPackets: make(map[livekit.ParticipantID]*rtp.Packet),
bytesReceived: make(map[livekit.ParticipantID]uint64),
pendingPublishedDataTracks: make(map[uint16]*livekit.DataTrackInfo),
subscribedDataTracks: make(map[livekit.ParticipantID]map[uint16]*DataTrackRemote),
transportReady: make(chan struct{}),
}
c.nextDataTrackHandle.Store(uint32(rand.IntN(8192)))
c.ctx, c.cancel = context.WithCancel(context.Background())
var codecs []*livekit.Codec
for _, codec := range []*livekit.Codec{
{
Mime: "audio/opus",
},
{
Mime: "video/vp8",
},
{
Mime: "video/h264",
},
} {
var disabled bool
if opts != nil {
for _, dc := range opts.DisabledCodecs {
if mime.IsMimeTypeStringEqual(dc.MimeType, codec.Mime) && (dc.SDPFmtpLine == "" || dc.SDPFmtpLine == codec.FmtpLine) {
disabled = true
break
}
}
}
if !disabled {
codecs = append(codecs, codec)
}
}
c.enabledCodecs = codecs
if opts != nil {
c.signalRequestInterceptor = opts.SignalRequestInterceptor
c.signalResponseInterceptor = opts.SignalResponseInterceptor
c.forceRelay = opts.ForceRelay
c.enableFlexFEC = opts.EnableFlexFEC
c.subscriberRTPFilter = opts.SubscriberRTPFilter
}
return c, nil
}
func (c *RTCClient) createTransport(rtcconf webrtc.Configuration) error {
var err error
conf := rtc.WebRTCConfig{
WebRTCConfig: rtcconfig.WebRTCConfig{
Configuration: rtcconf,
},
}
conf.SettingEngine.SetLite(false)
conf.SettingEngine.SetAnsweringDTLSRole(webrtc.DTLSRoleClient)
ff := buffer.NewFactoryOfBufferFactory(500, 200)
conf.SetBufferFactory(ff.CreateBufferFactory())
if filter := c.subscriberRTPFilter; filter != nil {
getOrNew := conf.SettingEngine.BufferFactory
conf.SettingEngine.BufferFactory = func(kind packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser {
rw := getOrNew(kind, ssrc)
if kind != packetio.RTPBufferPacket {
return rw
}
return &filteredRTPWriter{ReadWriteCloser: rw, filter: filter}
}
}
if c.enableFlexFEC {
fecConfig := rtc.FlexFECDirectionConfig{
Enabled: true,
}
conf.Publisher.FlexFEC = fecConfig
conf.Subscriber.FlexFEC = fecConfig
}
//
// The signal targets are from point of view of server.
// From client side, they are flipped,
// i. e. the publisher transport on client side has SUBSCRIBER signal target (i. e. publisher is offerer).
// Same applies for subscriber transport also
//
publisherHandler := &transportfakes.FakeHandler{}
c.publisher, err = rtc.NewPCTransport(rtc.TransportParams{
Config: &conf,
DirectionConfig: conf.Subscriber,
EnabledPublishCodecs: c.enabledCodecs,
EnabledSubscribeCodecs: c.enabledCodecs,
IsOfferer: true,
IsSendSide: true,
Handler: publisherHandler,
DatachannelMaxReceiverBufferSize: 1500,
DatachannelSlowThreshold: 1024 * 1024 * 1024,
FireOnTrackBySdp: true,
EnableDataTracks: true,
})
if err != nil {
return err
}
publisherHandler.OnICECandidateCalls(func(ic *webrtc.ICECandidate, t livekit.SignalTarget) error {
return c.SendIceCandidate(ic, livekit.SignalTarget_PUBLISHER)
})
publisherHandler.OnTrackCalls(func(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
go c.processRemoteTrack(track)
})
publisherHandler.OnDataMessageCalls(c.handleDataMessage)
publisherHandler.OnDataMessageUnlabeledCalls(c.handleDataMessageUnlabeled)
publisherHandler.OnDataTrackMessageCalls(c.handleDataTrackMessage)
publisherHandler.OnInitialConnectedCalls(func() {
logger.Debugw("publisher initial connected", "participant", c.localParticipant.Identity)
c.lock.Lock()
defer c.lock.Unlock()
for _, tw := range c.pendingTrackWriters {
if err := tw.Start(); err != nil {
logger.Errorw("track writer error", err)
}
}
c.pendingTrackWriters = nil
for _, dtw := range c.pendingDataTrackWriters {
if err := dtw.Start(); err != nil {
logger.Errorw("data track writer error", err)
}
}
c.pendingDataTrackWriters = nil
if f := c.onConnected.Load(); f != nil {
go (*f)()
}
})
publisherHandler.OnOfferCalls(c.onOffer)
publisherHandler.OnFullyEstablishedCalls(func() {
logger.Debugw("publisher fully established", "participant", c.localParticipant.Identity, "participantID", c.localParticipant.Sid)
c.publisherFullyEstablished.Store(true)
})
ordered := true
if c.useSinglePeerConnection {
if err := createNegotiatedDataChannels(c.publisher); err != nil {
return err
}
} else {
if err := c.publisher.CreateDataChannel(rtc.ReliableDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
}); err != nil {
return err
}
}
if err := c.publisher.CreateDataChannel("pubraw", &webrtc.DataChannelInit{
Ordered: &ordered,
}); err != nil {
return err
}
if !c.useSinglePeerConnection {
maxRetransmits := uint16(0)
ordered = false
if err := c.publisher.CreateDataChannel(rtc.LossyDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
MaxRetransmits: &maxRetransmits,
}); err != nil {
return err
}
if err := c.publisher.CreateDataChannel(rtc.DataTrackDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
MaxRetransmits: &maxRetransmits,
}); err != nil {
return err
}
subscriberHandler := &transportfakes.FakeHandler{}
c.subscriber, err = rtc.NewPCTransport(rtc.TransportParams{
Config: &conf,
DirectionConfig: conf.Publisher,
EnabledPublishCodecs: c.enabledCodecs,
EnabledSubscribeCodecs: c.enabledCodecs,
Handler: subscriberHandler,
DatachannelMaxReceiverBufferSize: 1500,
DatachannelSlowThreshold: 1024 * 1024 * 1024,
FireOnTrackBySdp: true,
EnableDataTracks: true,
})
if err != nil {
return err
}
ordered := true
if err := c.subscriber.CreateReadableDataChannel("subraw", &webrtc.DataChannelInit{
Ordered: &ordered,
}); err != nil {
return err
}
subscriberHandler.OnICECandidateCalls(func(ic *webrtc.ICECandidate, t livekit.SignalTarget) error {
if ic == nil {
return nil
}
return c.SendIceCandidate(ic, livekit.SignalTarget_SUBSCRIBER)
})
subscriberHandler.OnTrackCalls(func(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
go c.processRemoteTrack(track)
})
subscriberHandler.OnDataMessageCalls(c.handleDataMessage)
subscriberHandler.OnDataMessageUnlabeledCalls(c.handleDataMessageUnlabeled)
subscriberHandler.OnDataTrackMessageCalls(c.handleDataTrackMessage)
subscriberHandler.OnInitialConnectedCalls(func() {
logger.Debugw("subscriber initial connected", "participant", c.localParticipant.Identity)
c.lock.Lock()
defer c.lock.Unlock()
for _, tw := range c.pendingTrackWriters {
if err := tw.Start(); err != nil {
logger.Errorw("track writer error", err)
}
}
c.pendingTrackWriters = nil
for _, dtw := range c.pendingDataTrackWriters {
if err := dtw.Start(); err != nil {
logger.Errorw("data track writer error", err)
}
}
c.pendingDataTrackWriters = nil
if f := c.onConnected.Load(); f != nil {
go (*f)()
}
})
subscriberHandler.OnFullyEstablishedCalls(func() {
logger.Debugw("subscriber fully established", "participant", c.localParticipant.Identity, "participantID", c.localParticipant.Sid)
c.subscriberFullyEstablished.Store(true)
})
subscriberHandler.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error {
// send remote an answer
logger.Infow(
"sending subscriber answer",
"participant", c.localParticipant.Identity,
"sdp", answer,
)
return c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_Answer{
Answer: signalling.ToProtoSessionDescription(answer, answerId, nil),
},
})
})
} else {
go c.ensurePublisherConnected()
}
return nil
}
func (c *RTCClient) ID() livekit.ParticipantID {
return c.id
}
// SetOnConnected is safe to call after the client is running.
func (c *RTCClient) SetOnConnected(f func()) {
c.onConnected.Store(&f)
}
// SetOnDataReceived is safe to call after the client is running.
func (c *RTCClient) SetOnDataReceived(f func(data []byte, sid string)) {
c.onDataReceived.Store(&f)
}
// SetOnDataUnlabeledReceived is safe to call after the client is running.
func (c *RTCClient) SetOnDataUnlabeledReceived(f func(data []byte)) {
c.onDataUnlabeledReceived.Store(&f)
}
// create an offer for the server
func (c *RTCClient) Run() error {
c.conn.SetCloseHandler(func(code int, text string) error {
// when closed, stop connection
logger.Infow("connection closed", "code", code, "text", text)
c.Stop()
return nil
})
// run the session
for {
res, err := c.ReadResponse()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
logger.Errorw("error while reading", err)
return err
}
if c.signalResponseInterceptor != nil {
err = c.signalResponseInterceptor(res, c.handleSignalResponse)
} else {
err = c.handleSignalResponse(res)
}
if err != nil {
return err
}
}
}
func (c *RTCClient) handleSignalResponse(res *livekit.SignalResponse) error {
switch msg := res.Message.(type) {
case *livekit.SignalResponse_Join:
c.localParticipant = msg.Join.Participant
c.id = livekit.ParticipantID(msg.Join.Participant.Sid)
c.lock.Lock()
for _, p := range msg.Join.OtherParticipants {
c.remoteParticipants[livekit.ParticipantID(p.Sid)] = p
}
c.lock.Unlock()
var iceServers []webrtc.ICEServer
for _, is := range msg.Join.IceServers {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: is.Urls,
Username: is.Username,
Credential: is.Credential,
})
}
if len(iceServers) == 0 {
iceServers = rtcConf.ICEServers
}
rtcconf := rtcConf
rtcconf.ICEServers = iceServers
if c.forceRelay {
rtcconf.ICETransportPolicy = webrtc.ICETransportPolicyRelay
}
if err := c.createTransport(rtcconf); err != nil {
return err
}
close(c.transportReady)
// if publish only, negotiate
if !msg.Join.SubscriberPrimary {
c.subscriberAsPrimary.Store(false)
c.publisher.Negotiate(false)
} else {
c.subscriberAsPrimary.Store(true)
}
if c.subscriber != nil {
logger.Infow("join accepted, awaiting offer", "participant", msg.Join.Participant.Identity)
} else {
logger.Infow("join accepted", "participant", msg.Join.Participant.Identity)
}
case *livekit.SignalResponse_Answer:
logger.Infow(
"received server answer",
"participant", c.localParticipant.Identity,
"answer", msg.Answer.Sdp,
)
c.handleAnswer(signalling.FromProtoSessionDescription(msg.Answer))
case *livekit.SignalResponse_Offer:
desc, offerId, midToTrackID := signalling.FromProtoSessionDescription(msg.Offer)
logger.Infow(
"received server offer",
"participant", c.localParticipant.Identity,
"sdp", desc,
"offerId", offerId,
"midToTrackID", midToTrackID,
)
c.handleOffer(desc, offerId, midToTrackID)
case *livekit.SignalResponse_Trickle:
candidateInit, err := signalling.FromProtoTrickle(msg.Trickle)
if err != nil {
return err
}
if msg.Trickle.Target == livekit.SignalTarget_PUBLISHER {
c.publisher.AddICECandidate(candidateInit)
} else {
c.subscriber.AddICECandidate(candidateInit)
}
case *livekit.SignalResponse_Update:
c.lock.Lock()
for _, p := range msg.Update.Participants {
if livekit.ParticipantID(p.Sid) != c.id {
if p.State != livekit.ParticipantInfo_DISCONNECTED {
c.remoteParticipants[livekit.ParticipantID(p.Sid)] = p
} else {
delete(c.remoteParticipants, livekit.ParticipantID(p.Sid))
}
}
}
c.lock.Unlock()
case *livekit.SignalResponse_TrackPublished:
logger.Debugw(
"track published",
"participant", c.localParticipant.Identity,
"cid", msg.TrackPublished.Cid,
"trackID", msg.TrackPublished.Track.Sid,
"trackName", msg.TrackPublished.Track.Name,
)
c.lock.Lock()
c.pendingPublishedTracks[msg.TrackPublished.Cid] = msg.TrackPublished.Track
c.lock.Unlock()
case *livekit.SignalResponse_RefreshToken:
c.lock.Lock()
c.refreshToken = msg.RefreshToken
c.lock.Unlock()
case *livekit.SignalResponse_TrackUnpublished:
sid := msg.TrackUnpublished.TrackSid
c.lock.Lock()
if sender := c.trackSenders[sid]; sender != nil {
if err := c.publisher.RemoveTrack(sender); err != nil {
logger.Errorw("Could not unpublish track", err)
}
c.publisher.Negotiate(false)
}
delete(c.trackSenders, sid)
delete(c.localTracks, sid)
c.lock.Unlock()
case *livekit.SignalResponse_Pong:
c.pongReceivedAt.Store(msg.Pong)
case *livekit.SignalResponse_SubscriptionResponse:
c.subscriptionResponse.Store(msg.SubscriptionResponse)
case *livekit.SignalResponse_MediaSectionsRequirement:
logger.Infow(
"received media sections requirement",
"participant", c.localParticipant.Identity,
"numAudios", msg.MediaSectionsRequirement.NumAudios,
"numVideos", msg.MediaSectionsRequirement.NumVideos,
)
c.handleMediaSectionsRequirement(msg.MediaSectionsRequirement)
case *livekit.SignalResponse_PublishDataTrackResponse:
logger.Debugw(
"data track published",
"participant", c.localParticipant.Identity,
"trackID", msg.PublishDataTrackResponse.Info.Sid,
"trackHandle", msg.PublishDataTrackResponse.Info.PubHandle,
"trackName", msg.PublishDataTrackResponse.Info.Name,
)
c.lock.Lock()
c.pendingPublishedDataTracks[uint16(msg.PublishDataTrackResponse.Info.PubHandle)] = msg.PublishDataTrackResponse.Info
c.lock.Unlock()
case *livekit.SignalResponse_DataTrackSubscriberHandles:
logger.Infow(
"received data track subscriber handles",
"participant", c.localParticipant.Identity,
"handles", msg.DataTrackSubscriberHandles.SubHandles,
)
c.lock.Lock()
// create new remote data tracks if one does not exist for a handle
for handle, publishedDataTrack := range msg.DataTrackSubscriberHandles.SubHandles {
publisherID := livekit.ParticipantID(publishedDataTrack.PublisherSid)
tracks := c.subscribedDataTracks[publisherID]
if tracks == nil {
c.subscribedDataTracks[publisherID] = make(map[uint16]*DataTrackRemote)
tracks = c.subscribedDataTracks[publisherID]
}
if tracks[uint16(handle)] == nil {
tracks[uint16(handle)] = NewDataTrackRemote(
livekit.ParticipantIdentity(publishedDataTrack.PublisherIdentity),
livekit.ParticipantID(publishedDataTrack.PublisherSid),
uint16(handle),
livekit.TrackID(publishedDataTrack.TrackSid),
logger.GetLogger().WithValues("participant", c.localParticipant.Identity, "participantID", c.localParticipant.Sid),
)
}
}
// delete remote data tracks that have gone away
for publisherID, tracks := range c.subscribedDataTracks {
for handle, dataTrackRemote := range tracks {
if msg.DataTrackSubscriberHandles.SubHandles[uint32(handle)] == nil {
dataTrackRemote.Close()
delete(tracks, handle)
if len(tracks) == 0 {
delete(c.subscribedDataTracks, publisherID)
}
}
}
}
c.lock.Unlock()
}
return nil
}
func (c *RTCClient) WaitUntilConnected(timeout time.Duration) error {
if timeout == 0 {
timeout = 20 * time.Second
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
select {
case <-ctx.Done():
id := string(c.ID())
if c.localParticipant != nil {
id = c.localParticipant.Identity
}
return fmt.Errorf("%s could not connect after timeout", id)
case <-time.After(10 * time.Millisecond):
if c.subscriberAsPrimary.Load() {
if c.subscriberFullyEstablished.Load() {
return nil
}
} else {
if c.publisherFullyEstablished.Load() {
return nil
}
}
}
}
}
func (c *RTCClient) ReadResponse() (*livekit.SignalResponse, error) {
for {
// handle special messages and pass on the rest
messageType, payload, err := c.conn.ReadMessage()
if err != nil {
return nil, err
}
if c.ctx.Err() != nil {
return nil, c.ctx.Err()
}
msg := &livekit.SignalResponse{}
switch messageType {
case websocket.PingMessage:
_ = c.conn.WriteMessage(websocket.PongMessage, nil)
continue
case websocket.BinaryMessage:
// protobuf encoded
err := proto.Unmarshal(payload, msg)
return msg, err
default:
return nil, fmt.Errorf("unexpected message received: %v", messageType)
}
}
}
func (c *RTCClient) SubscribedTracks() map[livekit.ParticipantID][]*webrtc.TrackRemote {
// create a copy of this
c.lock.Lock()
defer c.lock.Unlock()
tracks := make(map[livekit.ParticipantID][]*webrtc.TrackRemote, len(c.subscribedTracks))
maps.Copy(tracks, c.subscribedTracks)
return tracks
}
func (c *RTCClient) SubscribedDataTracks() map[livekit.ParticipantID]map[uint16]*DataTrackRemote {
// create a copy of this
c.lock.Lock()
defer c.lock.Unlock()
tracks := make(map[livekit.ParticipantID]map[uint16]*DataTrackRemote, len(c.subscribedDataTracks))
for publisherID, sts := range c.subscribedDataTracks {
tracks[publisherID] = make(map[uint16]*DataTrackRemote)
maps.Copy(tracks[publisherID], sts)
}
return tracks
}
func (c *RTCClient) RemoteParticipants() []*livekit.ParticipantInfo {
c.lock.Lock()
defer c.lock.Unlock()
return funk.Values(c.remoteParticipants).([]*livekit.ParticipantInfo)
}
func (c *RTCClient) GetRemoteParticipant(sid livekit.ParticipantID) *livekit.ParticipantInfo {
c.lock.Lock()
defer c.lock.Unlock()
return c.remoteParticipants[sid]
}
func (c *RTCClient) Stop() {
logger.Infow("stopping client", "ID", c.ID())
_ = c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_Leave{
Leave: &livekit.LeaveRequest{
Reason: livekit.DisconnectReason_CLIENT_INITIATED,
Action: livekit.LeaveRequest_DISCONNECT,
},
},
})
c.publisherFullyEstablished.Store(false)
c.subscriberFullyEstablished.Store(false)
_ = c.conn.Close()
if c.publisher != nil {
c.publisher.Close()
}
if c.subscriber != nil {
c.subscriber.Close()
}
c.cancel()
}
func (c *RTCClient) RefreshToken() string {
c.lock.Lock()
defer c.lock.Unlock()
return c.refreshToken
}
func (c *RTCClient) PongReceivedAt() int64 {
return c.pongReceivedAt.Load()
}
func (c *RTCClient) GetSubscriptionResponseAndClear() *livekit.SubscriptionResponse {
return c.subscriptionResponse.Swap(nil)
}
func (c *RTCClient) SendPing() error {
return c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_Ping{
Ping: time.Now().UnixNano(),
},
})
}
func (c *RTCClient) SendRequest(msg *livekit.SignalRequest) error {
if c.signalRequestInterceptor != nil {
return c.signalRequestInterceptor(msg, c.sendRequest)
} else {
return c.sendRequest(msg)
}
}
func (c *RTCClient) sendRequest(msg *livekit.SignalRequest) error {
payload, err := proto.Marshal(msg)
if err != nil {
return err
}
c.wsLock.Lock()
defer c.wsLock.Unlock()
return c.conn.WriteMessage(websocket.BinaryMessage, payload)
}
func (c *RTCClient) SendIceCandidate(ic *webrtc.ICECandidate, target livekit.SignalTarget) error {
prevIC := c.icQueue[target].Swap(ic)
if prevIC == nil {
return nil
}
return c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_Trickle{
Trickle: signalling.ToProtoTrickle(prevIC.ToJSON(), target, ic == nil),
},
})
}
func (c *RTCClient) SetAttributes(attrs map[string]string) error {
return c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_UpdateMetadata{
UpdateMetadata: &livekit.UpdateParticipantMetadata{
Attributes: attrs,
},
},
})
}
func (c *RTCClient) hasPrimaryEverConnected() bool {
if c.subscriberAsPrimary.Load() {
return c.subscriber.PeerConnectionHasEverConnected()
} else {
return c.publisher.PeerConnectionHasEverConnected()
}
}
type AddTrackParams struct {
NoWriter bool
}
type AddTrackOption func(params *AddTrackParams)
func AddTrackNoWriter() AddTrackOption {
return func(params *AddTrackParams) {
params.NoWriter = true
}
}
func (c *RTCClient) AddTrack(track *webrtc.TrackLocalStaticSample, path string, opts ...AddTrackOption) (writer TrackWriter, err error) {
select {
case <-c.transportReady:
case <-c.ctx.Done():
return nil, c.ctx.Err()
}
var params AddTrackParams
for _, opt := range opts {
opt(&params)
}
trackType := livekit.TrackType_AUDIO
if track.Kind() == webrtc.RTPCodecTypeVideo {
trackType = livekit.TrackType_VIDEO
}
sender, _, err := c.publisher.AddTrack(track, types.AddTrackParams{}, nil, rtc.RTCPFeedbackConfig{})
if err != nil {
logger.Errorw(
"add track failed", err,
"participant", c.localParticipant.Identity,
"participantID", c.localParticipant.Sid,
"trackID", track.ID(),
)
return
}
if err = c.SendAddTrack(track.ID(), track.Codec().MimeType, track.StreamID(), trackType); err != nil {
return
}
// wait till track published message is received
timeout := time.After(5 * time.Second)
var ti *livekit.TrackInfo
for {
select {
case <-timeout:
return nil, errors.New("could not publish track after timeout")
default:
c.lock.Lock()
ti = c.pendingPublishedTracks[track.ID()]
if ti != nil {
delete(c.pendingPublishedTracks, track.ID())
c.lock.Unlock()
break
}
c.lock.Unlock()
time.Sleep(50 * time.Millisecond)
}
if ti != nil {
break
}
}
c.lock.Lock()
defer c.lock.Unlock()
c.localTracks[ti.Sid] = track
c.trackSenders[ti.Sid] = sender
c.publisher.Negotiate(false)
if !params.NoWriter {
writer = NewTrackWriter(c.ctx, track, path)
// write tracks only after connection established
if c.hasPrimaryEverConnected() {
err = writer.Start()
} else {
c.pendingTrackWriters = append(c.pendingTrackWriters, writer)
}
}
return
}
func (c *RTCClient) AddStaticTrack(mime string, id string, label string, opts ...AddTrackOption) (writer TrackWriter, err error) {
return c.AddStaticTrackWithCodec(webrtc.RTPCodecCapability{MimeType: mime}, id, label, opts...)
}
func (c *RTCClient) AddStaticTrackWithCodec(codec webrtc.RTPCodecCapability, id string, label string, opts ...AddTrackOption) (writer TrackWriter, err error) {
track, err := webrtc.NewTrackLocalStaticSample(codec, id, label)
if err != nil {
return
}
return c.AddTrack(track, "", opts...)
}
func (c *RTCClient) AddFileTrack(path string, id string, label string) (writer TrackWriter, err error) {
// determine file mime
mime, ok := extMimeMapping[filepath.Ext(path)]
if !ok {
return nil, fmt.Errorf("%s has an unsupported extension", filepath.Base(path))
}
logger.Debugw("adding file track", "mime", mime)
track, err := webrtc.NewTrackLocalStaticSample(
webrtc.RTPCodecCapability{MimeType: mime},
id,
label,
)
if err != nil {
return
}
return c.AddTrack(track, path)
}
// send AddTrack command to server to initiate server-side negotiation
func (c *RTCClient) SendAddTrack(cid string, mimeType string, name string, trackType livekit.TrackType) error {
return c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_AddTrack{
AddTrack: &livekit.AddTrackRequest{
Cid: cid,
Name: name,
Type: trackType,
SimulcastCodecs: []*livekit.SimulcastCodec{
{
Cid: cid,
Codec: mimeType,
},
},
},
},
})
}
func (c *RTCClient) PublishData(data []byte, kind livekit.DataPacket_Kind) error {
if err := c.ensurePublisherConnected(); err != nil {
return err
}
dpData, err := proto.Marshal(&livekit.DataPacket{
Value: &livekit.DataPacket_User{
User: &livekit.UserPacket{Payload: data},
},
})
if err != nil {
return err
}
return c.publisher.SendDataMessage(kind, dpData)
}
func (c *RTCClient) PublishDataUnlabeled(data []byte) error {
if err := c.ensurePublisherConnected(); err != nil {
return err
}
return c.publisher.SendDataMessageUnlabeled(data)
}
func (c *RTCClient) PublishDataTrack() (writer TrackWriter, err error) {
if err = c.ensurePublisherConnected(); err != nil {
return
}
dataTrackHandle := uint16(c.nextDataTrackHandle.Inc())
if err = c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_PublishDataTrackRequest{
PublishDataTrackRequest: &livekit.PublishDataTrackRequest{
PubHandle: uint32(dataTrackHandle),
Name: fmt.Sprintf("data_track_%d", dataTrackHandle),
},
},
}); err != nil {
return
}
// wait till data track published message is received
timeout := time.After(5 * time.Second)
var dti *livekit.DataTrackInfo
for {
select {
case <-timeout:
return nil, errors.New("could not publish data track after timeout")
default:
c.lock.Lock()
dti = c.pendingPublishedDataTracks[dataTrackHandle]
if dti != nil {
delete(c.pendingPublishedDataTracks, dataTrackHandle)
c.lock.Unlock()
break
}
c.lock.Unlock()
time.Sleep(50 * time.Millisecond)
}
if dti != nil {
break
}
}
c.lock.Lock()
defer c.lock.Unlock()
writer = NewDataTrackWriter(c.ctx, dataTrackHandle, c.publisher)
// write data tracks only after connection established
if c.hasPrimaryEverConnected() {
err = writer.Start()
} else {
c.pendingDataTrackWriters = append(c.pendingDataTrackWriters, writer)
}
return
}
func (c *RTCClient) GetPublishedTrackIDs() []string {
c.lock.Lock()
defer c.lock.Unlock()
var trackIDs []string
for key := range c.localTracks {
trackIDs = append(trackIDs, key)
}
return trackIDs
}
// SubscriberRemoteDescription returns the SDP the SFU last sent for the
// subscriber connection.
func (c *RTCClient) SubscriberRemoteDescription() *webrtc.SessionDescription {
if c.subscriber == nil {
return nil
}
return c.subscriber.CurrentRemoteDescription()
}
// SubscriberLocalDescription returns the SDP this client last sent for the
// subscriber connection, i.e. what it negotiated.
func (c *RTCClient) SubscriberLocalDescription() *webrtc.SessionDescription {
if c.subscriber == nil {
return nil
}
return c.subscriber.CurrentLocalDescription()
}
// LastAnswer return SDP of the last answer for the publisher connection
func (c *RTCClient) LastAnswer() *webrtc.SessionDescription {
return c.publisher.CurrentRemoteDescription()
}
func (c *RTCClient) ensurePublisherConnected() error {
select {
case <-c.transportReady:
case <-c.ctx.Done():
return c.ctx.Err()
}
if c.publisher.PeerConnectionHasEverConnected() {
return nil
}
// start negotiating
c.publisher.Negotiate(false)
// wait until connected, increase wait time since it takes more than 10s sometimes on GH
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for {
select {
case <-ctx.Done():
return fmt.Errorf("could not connect publisher after timeout")
case <-time.After(10 * time.Millisecond):
if c.publisherFullyEstablished.Load() {
return nil
}
}
}
}
func (c *RTCClient) handleDataMessage(kind livekit.DataPacket_Kind, data []byte) {
dp := &livekit.DataPacket{}
err := proto.Unmarshal(data, dp)
if err != nil {
return
}
dp.Kind = kind
if val, ok := dp.Value.(*livekit.DataPacket_User); ok {
if f := c.onDataReceived.Load(); f != nil {
(*f)(val.User.Payload, val.User.ParticipantSid)
}
}
}
func (c *RTCClient) handleDataMessageUnlabeled(data []byte) {
if f := c.onDataUnlabeledReceived.Load(); f != nil {
(*f)(data)
}
}
func (c *RTCClient) handleDataTrackMessage(data []byte, _arrivalTime int64) {
var packet datatrack.Packet
if err := packet.Unmarshal(data); err != nil {
return
}
var dataTrackRemote *DataTrackRemote
c.lock.Lock()
for _, tracks := range c.subscribedDataTracks {
if tracks[packet.Handle] != nil {
dataTrackRemote = tracks[packet.Handle]
break
}
}
c.lock.Unlock()
if dataTrackRemote != nil {
dataTrackRemote.PacketReceived(&packet)
}
}
// handles a server initiated offer, handle on subscriber PC
func (c *RTCClient) handleOffer(desc webrtc.SessionDescription, offerId uint32, _midToTrackID map[string]string) {
logger.Infow("handling server offer", "participant", c.localParticipant.Identity)
c.subscriber.HandleRemoteDescription(desc, offerId)
c.processPendingRemoteTracks()
}
// the client handles answer on the publisher PC
func (c *RTCClient) handleAnswer(desc webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) {
logger.Infow("handling server answer", "participant", c.localParticipant.Identity)
// remote answered the offer, establish connection
c.publisher.HandleRemoteDescription(desc, answerId)
c.processPendingRemoteTracks()
}
// the client handles media sections requirement on the publisher PC
func (c *RTCClient) handleMediaSectionsRequirement(mediaSectionsRequirement *livekit.MediaSectionsRequirement) {
addTransceivers := func(kind webrtc.RTPCodecType, count uint32) {
for range count {
if _, err := c.publisher.AddTransceiverFromKind(
kind,
webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionRecvonly,
},
); err != nil {
logger.Warnw(
"could not add transceiver", err,
"participant", c.localParticipant.Identity,
"kind", kind,
)
} else {
logger.Infow(
"added transceiver of kind",
"participant", c.localParticipant.Identity,
"kind", kind,
)
}
}
}
addTransceivers(webrtc.RTPCodecTypeAudio, mediaSectionsRequirement.NumAudios)
addTransceivers(webrtc.RTPCodecTypeVideo, mediaSectionsRequirement.NumVideos)
c.publisher.Negotiate(false)
}
func (c *RTCClient) onOffer(offer webrtc.SessionDescription, offerId uint32, midToTrackID map[string]string) error {
if c.localParticipant != nil {
logger.Infow("starting negotiation", "participant", c.localParticipant.Identity)
logger.Infow(
"sending publisher offer",
"participant", c.localParticipant.Identity,
"offer", offer,
"midToTrackID", midToTrackID,
)
}
return c.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_Offer{
Offer: signalling.ToProtoSessionDescription(offer, offerId, nil),
},
})
}
func (c *RTCClient) processPendingRemoteTracks() {
c.lock.Lock()
pendingRemoteTracks := c.pendingRemoteTracks
c.pendingRemoteTracks = nil
c.lock.Unlock()
for _, pendingRemoteTrack := range pendingRemoteTracks {
go c.processRemoteTrack(pendingRemoteTrack)
}
}
func (c *RTCClient) processRemoteTrack(track *webrtc.TrackRemote) {
lastUpdate := time.Time{}
// because of FireOnTrackBySdp, it is possible get an empty streamID
// if media comes before SDP, cache and try later
streamID := track.StreamID()
if streamID == "" {
logger.Infow(
"client caching track",
"participant", c.localParticipant.Identity,
"participantID", c.ID(),
"codec", track.Codec(),
"ssrc", track.SSRC(),
)
c.lock.Lock()
c.pendingRemoteTracks = append(c.pendingRemoteTracks, track)
c.lock.Unlock()
return
}
publisherID, trackID := rtc.UnpackStreamID(streamID)
if trackID == "" {
trackID = livekit.TrackID(track.ID())
}
c.lock.Lock()
c.subscribedTracks[publisherID] = append(c.subscribedTracks[publisherID], track)
c.lock.Unlock()
logger.Infow(
"client added track",
"participant", c.localParticipant.Identity,
"participantID", c.ID(),
"publisherID", publisherID,
"trackID", trackID,
"codec", track.Codec(),
"ssrc", track.SSRC(),
)
defer func() {
c.lock.Lock()
c.subscribedTracks[publisherID] = funk.Without(c.subscribedTracks[publisherID], track).([]*webrtc.TrackRemote)
c.lock.Unlock()
}()
numBytes := 0
for {
pkt, _, err := track.ReadRTP()
if c.ctx.Err() != nil {
break
}
if rtc.IsEOF(err) {
logger.Infow(
"client track removed",
"participant", c.localParticipant.Identity,
"participantID", c.ID(),
"publisherID", publisherID,
"trackID", trackID,
"codec", track.Codec(),
"ssrc", track.SSRC(),
)
break
}
if err != nil {
logger.Warnw("error reading RTP", err)
continue
}
c.lock.Lock()
c.lastPackets[publisherID] = pkt
c.bytesReceived[publisherID] += uint64(pkt.MarshalSize())
c.lock.Unlock()
numBytes += pkt.MarshalSize()
if time.Since(lastUpdate) > 30*time.Second {
logger.Infow(
"consumed from participant",
"participant", c.localParticipant.Identity,
"participantID", c.ID(),
"publisherID", publisherID,
"trackID", trackID,
"size", numBytes,
)
lastUpdate = time.Now()
}
}
}
func (c *RTCClient) BytesReceived() uint64 {
var total uint64
c.lock.Lock()
for _, size := range c.bytesReceived {
total += size
}
c.lock.Unlock()
return total
}
func (c *RTCClient) IsLocalCandidateRelaySelected() bool {
var info *types.ICEConnectionInfo
if c.subscriberAsPrimary.Load() {
if c.subscriber != nil {
info = c.subscriber.GetICEConnectionInfo()
}
} else {
if c.publisher != nil {
info = c.publisher.GetICEConnectionInfo()
}
}
if info == nil {
return false
}
for _, local := range info.Local {
if local.SelectedOrder > 0 && local.Candidate != nil && local.Candidate.Typ == webrtc.ICECandidateTypeRelay {
return true
}
}
return false
}
// WriteSubscriberRTCP sends RTCP on the subscriber connection, for tests that
// report reception statistics themselves.
func (c *RTCClient) WriteSubscriberRTCP(packets []rtcp.Packet) error {
return c.subscriber.WriteRTCP(packets)
}
func (c *RTCClient) SendNacks(count int) {
var packets []rtcp.Packet
c.lock.Lock()
for _, pkt := range c.lastPackets {
seqs := make([]uint16, 0, count)
for i := range count {
seqs = append(seqs, pkt.SequenceNumber-uint16(i))
}
packets = append(packets, &rtcp.TransportLayerNack{
MediaSSRC: pkt.SSRC,
Nacks: rtcp.NackPairsFromSequenceNumbers(seqs),
})
}
c.lock.Unlock()
_ = c.subscriber.WriteRTCP(packets)
}
func encodeQueryParam(key, value string) string {
return fmt.Sprintf("&%s=%s", url.QueryEscape(key), url.QueryEscape(value))
}
// createNegotiatedDataChannels creates the well-known data channels the way protocol >= 18 clients do:
// out-of-band with fixed IDs, matching what the server creates in single peer connection mode.
func createNegotiatedDataChannels(t *rtc.PCTransport) error {
negotiated := true
ordered := true
reliableID := uint16(livekit.NegotiatedDataChannelID_NDCI_RELIABLE)
if err := t.CreateDataChannel(rtc.ReliableDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
Negotiated: &negotiated,
ID: &reliableID,
}); err != nil {
return err
}
ordered = false
maxRetransmits := uint16(0)
lossyID := uint16(livekit.NegotiatedDataChannelID_NDCI_LOSSY)
if err := t.CreateDataChannel(rtc.LossyDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
MaxRetransmits: &maxRetransmits,
Negotiated: &negotiated,
ID: &lossyID,
}); err != nil {
return err
}
dataTrackID := uint16(livekit.NegotiatedDataChannelID_NDCI_DATA_TRACK)
return t.CreateDataChannel(rtc.DataTrackDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
MaxRetransmits: &maxRetransmits,
Negotiated: &negotiated,
ID: &dataTrackID,
})
}
// filteredRTPWriter drops RTP packets rejected by filter before they reach
// the wrapped receive buffer.
type filteredRTPWriter struct {
io.ReadWriteCloser
filter func(pkt *rtp.Packet) bool
}
func (w *filteredRTPWriter) Write(raw []byte) (int, error) {
var pkt rtp.Packet
if err := pkt.Unmarshal(raw); err == nil && !w.filter(&pkt) {
return len(raw), nil
}
return w.ReadWriteCloser.Write(raw)
}