feat: WIP s6-overlay and friends

This commit is contained in:
DatCaptainHorse
2026-02-19 18:02:10 +02:00
parent b743dab332
commit 34afd371ad
96 changed files with 2340 additions and 1063 deletions

View File

@@ -3,6 +3,7 @@ package shared
import (
"errors"
"fmt"
"github.com/pion/rtp"
"io"
"log/slog"
"relay/internal/common"
@@ -104,6 +105,17 @@ func (p *Participant) Close() {
}
func (p *Participant) packetWriter() {
flags := common.GetFlags()
playoutExt := &rtp.PlayoutDelayExtension{
MinDelay: uint16(flags.PlayoutDelayMin),
MaxDelay: uint16(flags.PlayoutDelayMax),
}
playoutPayload, err := playoutExt.Marshal()
if err != nil {
slog.Error("Failed to marshal PlayoutDelayExtension for participant", "participant", p.ID, "err", err)
return
}
for pkt := range p.packetQueue {
var track *webrtc.TrackLocalStaticRTP
@@ -114,6 +126,14 @@ func (p *Participant) packetWriter() {
track = p.VideoTrack
}
// Use PlayoutDelayExtension for low latency, if set for this track kind
if extID, ok := common.GetExtension(track.Kind(), common.ExtensionPlayoutDelay); ok {
if err = pkt.packet.SetExtension(extID, playoutPayload); err != nil {
slog.Error("Failed to set PlayoutDelayExtension for participant", "participant", p.ID, "err", err)
continue
}
}
if track != nil {
if err := track.WriteRTP(pkt.packet); err != nil && !errors.Is(err, io.ErrClosedPipe) {
slog.Error("WriteRTP failed", "participant", p.ID, "kind", pkt.kind, "err", err)

View File

@@ -19,8 +19,8 @@ var participantPacketPool = sync.Pool{
}
type participantPacket struct {
kind webrtc.RTPCodecType
packet *rtp.Packet
kind webrtc.RTPCodecType
packet *rtp.Packet
}
type RoomInfo struct {
@@ -31,11 +31,13 @@ type RoomInfo struct {
type Room struct {
RoomInfo
AudioCodec webrtc.RTPCodecCapability
VideoCodec webrtc.RTPCodecCapability
PeerConnection *webrtc.PeerConnection
DataChannel *connections.NestriDataChannel
codecMu sync.RWMutex
audioCodec webrtc.RTPCodecCapability
videoCodec webrtc.RTPCodecCapability
// Atomic pointer to slice of participant channels
participantChannels atomic.Pointer[[]chan<- *participantPacket]
participantsMtx sync.Mutex // Use only for add/remove
@@ -90,7 +92,6 @@ func (r *Room) Close() {
}
}
// AddParticipant adds a Participant to a Room
func (r *Room) AddParticipant(participant *Participant) {
r.participantsMtx.Lock()
defer r.participantsMtx.Unlock()
@@ -108,7 +109,6 @@ func (r *Room) AddParticipant(participant *Participant) {
slog.Debug("Added participant", "participant", participant.ID, "room", r.Name)
}
// RemoveParticipantByID removes a Participant from a Room by participant's ID
func (r *Room) RemoveParticipantByID(pID ulid.ULID) {
r.participantsMtx.Lock()
defer r.participantsMtx.Unlock()
@@ -134,7 +134,6 @@ func (r *Room) RemoveParticipantByID(pID ulid.ULID) {
slog.Debug("Removed participant", "participant", pID, "room", r.Name)
}
// IsOnline checks if the room is online
func (r *Room) IsOnline() bool {
return r.PeerConnection != nil
}
@@ -153,7 +152,7 @@ func (r *Room) BroadcastPacket(kind webrtc.RTPCodecType, pkt *rtp.Packet) {
// Get packet struct from pool
pp := participantPacketPool.Get().(*participantPacket)
pp.kind = kind
pp.packet = pkt
pp.packet = pkt.Clone()
select {
case ch <- pp:
@@ -165,3 +164,27 @@ func (r *Room) BroadcastPacket(kind webrtc.RTPCodecType, pkt *rtp.Packet) {
}
}
}
func (r *Room) SetAudioCodec(c webrtc.RTPCodecCapability) {
r.codecMu.Lock()
defer r.codecMu.Unlock()
r.audioCodec = c
}
func (r *Room) GetAudioCodec() webrtc.RTPCodecCapability {
r.codecMu.RLock()
defer r.codecMu.RUnlock()
return r.audioCodec
}
func (r *Room) SetVideoCodec(c webrtc.RTPCodecCapability) {
r.codecMu.Lock()
defer r.codecMu.Unlock()
r.videoCodec = c
}
func (r *Room) GetVideoCodec() webrtc.RTPCodecCapability {
r.codecMu.RLock()
defer r.codecMu.RUnlock()
return r.videoCodec
}