Files
livekit-3d/pkg/rtc/signalling/signalhandlerv2.go
T
Raja Subramanian 1c99b9ad5f Split signal segmenter and reassembler. (#3805)
* Split signal segmenter and reassembler.

As reassembler may need to run a goroutine for pruning, splitting it up
so that we do not have unnecessary goroutines. Keeping the UT unified
though.

Also, for now, trying to see if `Prune` can be called from external
place to avoid creating a goroutine and have extra state/functions to
close the reassembler and clean up goroutine etc. May still have to do
goroutine, but have not created one for now.

* fix: do not increase packetId for each fragment of a packet
2025-07-21 11:10:20 +05:30

106 lines
3.0 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 signalling
import (
"fmt"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/rtc/types"
"go.uber.org/atomic"
)
var _ ParticipantSignalHandler = (*signalhandlerv2)(nil)
type SignalHandlerv2Params struct {
Logger logger.Logger
Participant types.LocalParticipant
Signalling ParticipantSignalling
}
type signalhandlerv2 struct {
signalhandlerUnimplemented
params SignalHandlerv2Params
// SIGNALLING-V2-TODO: have to set this properly for `ConnectRequest` coming via sync HTTP path
lastProcessedRemoteMessageId atomic.Uint32
signalReassembler *SignalReassembler
}
func NewSignalHandlerv2(params SignalHandlerv2Params) ParticipantSignalHandler {
return &signalhandlerv2{
params: params,
signalReassembler: NewSignalReassembler(SignalReassemblerParams{
Logger: params.Logger,
}),
}
}
func (s *signalhandlerv2) HandleRequest(msg proto.Message) error {
req, ok := msg.(*livekit.Signalv2WireMessage)
if !ok {
s.params.Logger.Warnw(
"unknown message type", nil,
"messageType", fmt.Sprintf("%T", msg),
)
return ErrInvalidMessageType
}
// SIGNALLING-V2-TODO: check if this makes sense for data channel based signalling
s.params.Participant.UpdateLastSeenSignal()
switch msg := req.GetMessage().(type) {
case *livekit.Signalv2WireMessage_Envelope:
for _, clientMessage := range msg.Envelope.ClientMessages {
if clientMessage.Sequencer.MessageId != s.lastProcessedRemoteMessageId.Load()+1 {
s.params.Logger.Infow(
"gap in message stream",
"last", s.lastProcessedRemoteMessageId.Load(),
"current", clientMessage.Sequencer.MessageId,
)
}
// SIGNALLING-V2-TODO: process messages
s.params.Signalling.AckMessageId(clientMessage.Sequencer.LastProcessedRemoteMessageId)
s.params.Signalling.SetLastProcessedRemoteMessageId(clientMessage.Sequencer.MessageId)
}
case *livekit.Signalv2WireMessage_Fragment:
bytes := s.signalReassembler.Reassemble(msg.Fragment)
if len(bytes) != 0 {
wireMessage := &livekit.Signalv2WireMessage{}
err := proto.Unmarshal(bytes, wireMessage)
if err != nil {
s.params.Logger.Warnw("could not unmarshal re-assembled packet", err)
return err
}
s.HandleRequest(wireMessage)
}
}
return nil
}
func (s *signalhandlerv2) PruneStaleReassemblies() {
s.signalReassembler.Prune()
}