starting signaller interface (#3802)
* starting signaller interface * WIP * WIP * WIP * typo * connect response check * WIP * clean up * move signal handling to participant fully * service methods * type assertions for interfaces
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
// 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 (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type ParticipantSignaller interface {
|
||||
SetResponseSink(sink routing.MessageSink)
|
||||
GetResponseSink() routing.MessageSink
|
||||
CloseSignalConnection(reason types.SignallingCloseReason)
|
||||
|
||||
WriteMessage(msg proto.Message) error
|
||||
}
|
||||
|
||||
type ParticipantSignalling interface {
|
||||
SignalJoinResponse(join *livekit.JoinResponse) proto.Message
|
||||
SignalParticipantUpdate(participants []*livekit.ParticipantInfo) proto.Message
|
||||
SignalSpeakerUpdate(speakers []*livekit.SpeakerInfo) proto.Message
|
||||
SignalRoomUpdate(room *livekit.Room) proto.Message
|
||||
SignalConnectionQualityUpdate(connectionQuality *livekit.ConnectionQualityUpdate) proto.Message
|
||||
SignalRefreshToken(token string) proto.Message
|
||||
SignalRequestResponse(requestResponse *livekit.RequestResponse) proto.Message
|
||||
SignalRoomMovedResponse(roomMoved *livekit.RoomMovedResponse) proto.Message
|
||||
SignalReconnectResponse(reconnect *livekit.ReconnectResponse) proto.Message
|
||||
SignalICECandidate(trickle *livekit.TrickleRequest) proto.Message
|
||||
SignalTrackMuted(mute *livekit.MuteTrackRequest) proto.Message
|
||||
SignalTrackPublished(trackPublished *livekit.TrackPublishedResponse) proto.Message
|
||||
SignalTrackUnpublished(trackUnpublished *livekit.TrackUnpublishedResponse) proto.Message
|
||||
SignalTrackSubscribed(trackSubscribed *livekit.TrackSubscribed) proto.Message
|
||||
SignalLeaveRequest(leave *livekit.LeaveRequest) proto.Message
|
||||
SignalSdpAnswer(answer *livekit.SessionDescription) proto.Message
|
||||
SignalSdpOffer(offer *livekit.SessionDescription) proto.Message
|
||||
SignalStreamStateUpdate(streamStateUpdate *livekit.StreamStateUpdate) proto.Message
|
||||
SignalSubscribedQualityUpdate(subscribedQualityUpdate *livekit.SubscribedQualityUpdate) proto.Message
|
||||
SignalSubscriptionResponse(subscriptionResponse *livekit.SubscriptionResponse) proto.Message
|
||||
SignalSubscriptionPermissionUpdate(subscriptionPermissionUpdate *livekit.SubscriptionPermissionUpdate) proto.Message
|
||||
|
||||
AckMessageId(ackMessageId uint32)
|
||||
SetLastProcessedRemoteMessageId(lastProcessedRemoteMessageId uint32)
|
||||
|
||||
PendingMessages() proto.Message
|
||||
|
||||
SignalConnectResponse(connectResponse *livekit.ConnectResponse) proto.Message
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// 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 (
|
||||
"math/rand"
|
||||
"sync"
|
||||
|
||||
"github.com/gammazero/deque"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
type SignalCacheParams struct {
|
||||
Logger logger.Logger
|
||||
FirstMessageId uint32 // should be used for testing only
|
||||
}
|
||||
|
||||
type SignalCache struct {
|
||||
params SignalCacheParams
|
||||
|
||||
lock sync.Mutex
|
||||
messageId uint32
|
||||
lastProcessedRemoteMessageId uint32
|
||||
messages deque.Deque[*livekit.Signalv2ServerMessage]
|
||||
}
|
||||
|
||||
func NewSignalCache(params SignalCacheParams) *SignalCache {
|
||||
s := &SignalCache{
|
||||
params: params,
|
||||
messageId: params.FirstMessageId,
|
||||
}
|
||||
if s.messageId == 0 {
|
||||
s.messageId = uint32(rand.Intn(1<<8) + 1)
|
||||
}
|
||||
s.messages.SetBaseCap(16)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SignalCache) SetLastProcessedRemoteMessageId(lastProcessedRemoteMessageId uint32) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
s.lastProcessedRemoteMessageId = lastProcessedRemoteMessageId
|
||||
}
|
||||
|
||||
func (s *SignalCache) Add(msg *livekit.Signalv2ServerMessage) {
|
||||
if msg != nil {
|
||||
s.AddBatch([]*livekit.Signalv2ServerMessage{msg})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalCache) AddBatch(msgs []*livekit.Signalv2ServerMessage) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
for _, msg := range msgs {
|
||||
msg.Sequencer = &livekit.Sequencer{
|
||||
MessageId: s.messageId,
|
||||
}
|
||||
s.messageId++
|
||||
|
||||
s.messages.PushBack(msg)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalCache) Clear(till uint32) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
s.clearLocked(till)
|
||||
}
|
||||
|
||||
func (s *SignalCache) clearLocked(till uint32) {
|
||||
for s.messages.Len() != 0 {
|
||||
front := s.messages.Front()
|
||||
if front.Sequencer.GetMessageId() > till {
|
||||
break
|
||||
}
|
||||
s.messages.PopFront()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalCache) GetFromFront() []*livekit.Signalv2ServerMessage {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
return s.getFromFrontLocked()
|
||||
}
|
||||
|
||||
func (s *SignalCache) getFromFrontLocked() []*livekit.Signalv2ServerMessage {
|
||||
var msgs []*livekit.Signalv2ServerMessage
|
||||
for msg := range s.messages.Iter() {
|
||||
clone := utils.CloneProto(msg)
|
||||
clone.Sequencer.LastProcessedRemoteMessageId = s.lastProcessedRemoteMessageId
|
||||
msgs = append(msgs, clone)
|
||||
}
|
||||
|
||||
return msgs
|
||||
}
|
||||
|
||||
func (s *SignalCache) ClearAndGetFrom(from uint32) []*livekit.Signalv2ServerMessage {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
s.clearLocked(from - 1)
|
||||
return s.getFromFrontLocked()
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
// 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 (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
func TestSignalCache(t *testing.T) {
|
||||
firstMessageId := uint32(10)
|
||||
lastProcessedRemoteMessageId := uint32(2345)
|
||||
cache := NewSignalCache(SignalCacheParams{
|
||||
FirstMessageId: firstMessageId,
|
||||
})
|
||||
|
||||
inputMessages := []*livekit.Signalv2ServerMessage{
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
// SIGNALLING-V2-TODO: replace with other kinds of messages when more types are added
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
}
|
||||
|
||||
expectedOutputMessages := []*livekit.Signalv2ServerMessage{
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
// SIGNALLING-V2-TODO: replace with other kinds of messages when more types are added
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId + 1,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId + 2,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId + 3,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
}
|
||||
|
||||
cache.SetLastProcessedRemoteMessageId(lastProcessedRemoteMessageId)
|
||||
|
||||
// Add() - add one message at a time
|
||||
for _, inputMessage := range inputMessages {
|
||||
cache.Add(inputMessage)
|
||||
}
|
||||
|
||||
// get all messages in cache
|
||||
outputMessages := cache.GetFromFront()
|
||||
require.True(t, compareProtoSlices(expectedOutputMessages, outputMessages))
|
||||
|
||||
// clear one and get again
|
||||
cache.Clear(firstMessageId)
|
||||
|
||||
outputMessages = cache.GetFromFront()
|
||||
require.True(t, compareProtoSlices(expectedOutputMessages[1:], outputMessages))
|
||||
|
||||
// clearing some evicted messages should not clear anything
|
||||
cache.Clear(firstMessageId) // firstMessageId has been cleared already at this point
|
||||
|
||||
outputMessages = cache.GetFromFront()
|
||||
require.True(t, compareProtoSlices(expectedOutputMessages[1:], outputMessages))
|
||||
|
||||
// clear some and get rest in one go
|
||||
outputMessages = cache.ClearAndGetFrom(firstMessageId + 3)
|
||||
require.Equal(t, 1, len(outputMessages))
|
||||
require.True(t, compareProtoSlices(expectedOutputMessages[3:], outputMessages))
|
||||
|
||||
// getting again should get the same messages again as they sill should in cache
|
||||
outputMessages = cache.GetFromFront()
|
||||
require.True(t, compareProtoSlices(expectedOutputMessages[3:], outputMessages))
|
||||
|
||||
// clearing all and getting should return nil
|
||||
require.Nil(t, cache.ClearAndGetFrom(firstMessageId+uint32(len(inputMessages))))
|
||||
|
||||
// getting again should return nil as the cache is fully cleared above
|
||||
require.Nil(t, cache.GetFromFront())
|
||||
|
||||
lastProcessedRemoteMessageId = 4567
|
||||
cache.SetLastProcessedRemoteMessageId(lastProcessedRemoteMessageId)
|
||||
|
||||
expectedOutputMessages = []*livekit.Signalv2ServerMessage{
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId + 4,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
// SIGNALLING-V2-TODO: replace with other kinds of messages when more types are added
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId + 1 + 4,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId + 2 + 4,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
&livekit.Signalv2ServerMessage{
|
||||
Sequencer: &livekit.Sequencer{
|
||||
MessageId: firstMessageId + 3 + 4,
|
||||
LastProcessedRemoteMessageId: lastProcessedRemoteMessageId,
|
||||
},
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{},
|
||||
},
|
||||
}
|
||||
|
||||
// AddBatch() - add all messages at once
|
||||
cache.AddBatch(inputMessages)
|
||||
|
||||
// get all messages in cache
|
||||
outputMessages = cache.GetFromFront()
|
||||
require.True(t, compareProtoSlices(expectedOutputMessages, outputMessages))
|
||||
}
|
||||
|
||||
func compareProtoSlices(a []*livekit.Signalv2ServerMessage, b []*livekit.Signalv2ServerMessage) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 0; i < len(a); i++ {
|
||||
if !proto.Equal(a[i], b[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
// 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 (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultMaxFragmentSize = 8192
|
||||
)
|
||||
|
||||
type reassembly struct {
|
||||
startedAt time.Time
|
||||
fragments []*livekit.Fragment
|
||||
isCorrupted bool
|
||||
}
|
||||
|
||||
type SignalFragmentParams struct {
|
||||
Logger logger.Logger
|
||||
MaxFragmentSize int
|
||||
FirstPacketId uint32 // should be used for testing only
|
||||
}
|
||||
|
||||
type SignalFragment struct {
|
||||
params SignalFragmentParams
|
||||
|
||||
lock sync.Mutex
|
||||
packetId uint32
|
||||
reassemblies map[uint32]*reassembly
|
||||
}
|
||||
|
||||
func NewSignalFragment(params SignalFragmentParams) *SignalFragment {
|
||||
s := &SignalFragment{
|
||||
params: params,
|
||||
packetId: params.FirstPacketId,
|
||||
reassemblies: make(map[uint32]*reassembly),
|
||||
}
|
||||
if s.params.MaxFragmentSize == 0 {
|
||||
s.params.MaxFragmentSize = defaultMaxFragmentSize
|
||||
}
|
||||
if s.packetId == 0 {
|
||||
s.packetId = uint32(rand.Intn(1<<8) + 1)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SignalFragment) Segment(data []byte) []*livekit.Fragment {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if len(data) <= s.params.MaxFragmentSize {
|
||||
return nil
|
||||
}
|
||||
|
||||
var fragments []*livekit.Fragment
|
||||
numFragments := uint32((len(data) + s.params.MaxFragmentSize - 1) / s.params.MaxFragmentSize)
|
||||
fragmentNumber := uint32(1)
|
||||
consumed := 0
|
||||
for len(data[consumed:]) != 0 {
|
||||
fragmentSize := min(len(data[consumed:]), s.params.MaxFragmentSize)
|
||||
fragment := &livekit.Fragment{
|
||||
PacketId: s.packetId,
|
||||
FragmentNumber: fragmentNumber,
|
||||
NumFragments: numFragments,
|
||||
FragmentSize: uint32(fragmentSize),
|
||||
TotalSize: uint32(len(data)),
|
||||
Data: data[consumed : consumed+fragmentSize],
|
||||
}
|
||||
fragments = append(fragments, fragment)
|
||||
fragmentNumber++
|
||||
consumed += fragmentSize
|
||||
}
|
||||
|
||||
return fragments
|
||||
}
|
||||
|
||||
func (s *SignalFragment) Reassemble(fragment *livekit.Fragment) []byte {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
re, ok := s.reassemblies[fragment.PacketId]
|
||||
if !ok {
|
||||
re = &reassembly{
|
||||
startedAt: time.Now(),
|
||||
fragments: make([]*livekit.Fragment, fragment.NumFragments),
|
||||
}
|
||||
s.reassemblies[fragment.PacketId] = re
|
||||
}
|
||||
if int(fragment.FragmentNumber) <= len(re.fragments) {
|
||||
if int(fragment.FragmentSize) != len(fragment.Data) {
|
||||
re.isCorrupted = true // runt packet, data size of blob does not match fragment size
|
||||
} else {
|
||||
re.fragments[fragment.FragmentNumber-1] = fragment
|
||||
}
|
||||
} else {
|
||||
re.isCorrupted = true
|
||||
}
|
||||
|
||||
if re.isCorrupted {
|
||||
return nil
|
||||
}
|
||||
|
||||
// try to reassemble
|
||||
expectedTotalSize := uint32(0)
|
||||
totalSize := 0
|
||||
for _, fr := range re.fragments {
|
||||
if fr == nil {
|
||||
return nil // not received all fragments of packet yet
|
||||
}
|
||||
|
||||
expectedTotalSize = fr.TotalSize // can read this from any fragment of packet
|
||||
totalSize += len(fr.Data)
|
||||
}
|
||||
if expectedTotalSize != 0 && uint32(totalSize) != expectedTotalSize {
|
||||
re.isCorrupted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
data := make([]byte, 0, expectedTotalSize)
|
||||
for _, fr := range re.fragments {
|
||||
data = append(data, fr.Data...)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// SIGNALLING-V2-TODO: need a prune worker to handle stale re-assemblies
|
||||
@@ -0,0 +1,139 @@
|
||||
// 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 (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
func TestSignalFragment(t *testing.T) {
|
||||
inputMessage := &livekit.Envelope{
|
||||
ServerMessages: []*livekit.Signalv2ServerMessage{
|
||||
{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{
|
||||
ConnectResponse: &livekit.ConnectResponse{
|
||||
SifTrailer: []byte("abcdefghijklmnopqrstuvwxyz0123456789"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{
|
||||
ConnectResponse: &livekit.ConnectResponse{
|
||||
SifTrailer: []byte("0123456789abcdefghijklmnopqrstuvwxyz0123456789"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{
|
||||
ConnectResponse: &livekit.ConnectResponse{
|
||||
SifTrailer: []byte("ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyz0123456789"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("no segmentation needed", func(t *testing.T) {
|
||||
sr := NewSignalFragment(SignalFragmentParams{
|
||||
MaxFragmentSize: 5_000_000,
|
||||
})
|
||||
|
||||
marshalled, err := proto.Marshal(inputMessage)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, sr.Segment(marshalled))
|
||||
})
|
||||
|
||||
t.Run("segmentation + reassmebly", func(t *testing.T) {
|
||||
maxFragmentSize := 5
|
||||
sr := NewSignalFragment(SignalFragmentParams{
|
||||
MaxFragmentSize: maxFragmentSize,
|
||||
})
|
||||
|
||||
marshalled, err := proto.Marshal(inputMessage)
|
||||
require.NoError(t, err)
|
||||
|
||||
expectedNumFragments := (len(marshalled) + maxFragmentSize - 1) / maxFragmentSize
|
||||
|
||||
fragments := sr.Segment(marshalled)
|
||||
require.NotZero(t, len(fragments))
|
||||
require.Equal(t, uint32(len(marshalled)), fragments[0].TotalSize)
|
||||
|
||||
var reassembled []byte
|
||||
for idx, fragment := range fragments {
|
||||
require.Equal(t, uint32(idx+1), fragment.FragmentNumber)
|
||||
require.NotZero(t, fragment.FragmentSize)
|
||||
require.Equal(t, uint32(expectedNumFragments), fragment.NumFragments)
|
||||
require.Equal(t, fragment.FragmentSize, uint32(len(fragment.Data)))
|
||||
|
||||
reassembled = sr.Reassemble(fragment)
|
||||
}
|
||||
require.Equal(t, marshalled, reassembled)
|
||||
})
|
||||
|
||||
t.Run("runt", func(t *testing.T) {
|
||||
maxFragmentSize := 5
|
||||
sr := NewSignalFragment(SignalFragmentParams{
|
||||
MaxFragmentSize: maxFragmentSize,
|
||||
})
|
||||
|
||||
marshalled, err := proto.Marshal(inputMessage)
|
||||
require.NoError(t, err)
|
||||
|
||||
fragments := sr.Segment(marshalled)
|
||||
|
||||
var reassembled []byte
|
||||
for idx, fragment := range fragments {
|
||||
// do not send one packet into re-assembly initially, re-assembly should not succeed
|
||||
if idx == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
reassembled = sr.Reassemble(fragment)
|
||||
}
|
||||
require.Zero(t, len(reassembled))
|
||||
|
||||
// submit 1st fragment and ensure reassembly completes
|
||||
reassembled = sr.Reassemble(fragments[0])
|
||||
require.Equal(t, marshalled, reassembled)
|
||||
})
|
||||
|
||||
t.Run("corrupted", func(t *testing.T) {
|
||||
maxFragmentSize := 5
|
||||
sr := NewSignalFragment(SignalFragmentParams{
|
||||
MaxFragmentSize: maxFragmentSize,
|
||||
})
|
||||
|
||||
marshalled, err := proto.Marshal(inputMessage)
|
||||
require.NoError(t, err)
|
||||
|
||||
fragments := sr.Segment(marshalled)
|
||||
|
||||
var reassembled []byte
|
||||
for idx, fragment := range fragments {
|
||||
// corrupt a fragment, re-assembly should fail
|
||||
if idx == 0 {
|
||||
fragment.FragmentSize += 1
|
||||
}
|
||||
|
||||
reassembled = sr.Reassemble(fragment)
|
||||
}
|
||||
require.Zero(t, len(reassembled))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// 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"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/psrpc"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var _ ParticipantSignaller = (*signallerAsync)(nil)
|
||||
|
||||
type SignallerAsyncParams struct {
|
||||
Logger logger.Logger
|
||||
Participant types.LocalParticipant
|
||||
}
|
||||
|
||||
type signallerAsync struct {
|
||||
params SignallerAsyncParams
|
||||
|
||||
*signallerAsyncBase
|
||||
}
|
||||
|
||||
func NewSignallerAsync(params SignallerAsyncParams) ParticipantSignaller {
|
||||
return &signallerAsync{
|
||||
params: params,
|
||||
signallerAsyncBase: newSignallerAsyncBase(signallerAsyncBaseParams{Logger: params.Logger}),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signallerAsync) WriteMessage(msg proto.Message) error {
|
||||
if msg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.params.Participant.IsDisconnected() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !s.params.Participant.IsReady() {
|
||||
if typed, ok := msg.(*livekit.SignalResponse); !ok {
|
||||
s.params.Logger.Warnw(
|
||||
"unknown message type", nil,
|
||||
"messageType", fmt.Sprintf("%T", msg),
|
||||
)
|
||||
} else {
|
||||
if typed.GetJoin() == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sink := s.GetResponseSink()
|
||||
if sink == nil {
|
||||
if typed, ok := msg.(*livekit.SignalResponse); ok {
|
||||
s.params.Logger.Debugw(
|
||||
"could not send message to participant",
|
||||
"messageType", fmt.Sprintf("%T", typed.Message),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := sink.WriteMessage(msg)
|
||||
if err != nil {
|
||||
// SIGNALLING-V2-TODO: check for data channel errors to treat as non-error
|
||||
if utils.ErrorIsOneOf(err, psrpc.Canceled, routing.ErrChannelClosed) {
|
||||
if typed, ok := msg.(*livekit.SignalResponse); ok {
|
||||
s.params.Logger.Debugw(
|
||||
"could not send message to participant",
|
||||
"error", err,
|
||||
"messageType", fmt.Sprintf("%T", typed.Message),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
if typed, ok := msg.(*livekit.SignalResponse); ok {
|
||||
s.params.Logger.Warnw(
|
||||
"could not send message to participant", err,
|
||||
"messageType", fmt.Sprintf("%T", typed.Message),
|
||||
)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// 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 (
|
||||
"sync"
|
||||
|
||||
"github.com/livekit/protocol/logger"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
)
|
||||
|
||||
type signallerAsyncBaseParams struct {
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type signallerAsyncBase struct {
|
||||
signallerUnimplemented
|
||||
|
||||
params signallerAsyncBaseParams
|
||||
|
||||
resSinkMu sync.Mutex
|
||||
resSink routing.MessageSink
|
||||
}
|
||||
|
||||
func newSignallerAsyncBase(params signallerAsyncBaseParams) *signallerAsyncBase {
|
||||
return &signallerAsyncBase{
|
||||
params: params,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signallerAsyncBase) SetResponseSink(sink routing.MessageSink) {
|
||||
s.resSinkMu.Lock()
|
||||
defer s.resSinkMu.Unlock()
|
||||
s.resSink = sink
|
||||
}
|
||||
|
||||
func (s *signallerAsyncBase) GetResponseSink() routing.MessageSink {
|
||||
s.resSinkMu.Lock()
|
||||
defer s.resSinkMu.Unlock()
|
||||
return s.resSink
|
||||
}
|
||||
|
||||
// closes signal connection to notify client to resume/reconnect
|
||||
func (s *signallerAsyncBase) CloseSignalConnection(reason types.SignallingCloseReason) {
|
||||
sink := s.GetResponseSink()
|
||||
if sink == nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.params.Logger.Debugw("closing signal connection", "reason", reason, "connID", sink.ConnectionID())
|
||||
sink.Close()
|
||||
s.SetResponseSink(nil)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// 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 (
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var _ ParticipantSignaller = (*signallerUnimplemented)(nil)
|
||||
|
||||
type signallerUnimplemented struct{}
|
||||
|
||||
func (u *signallerUnimplemented) SetResponseSink(sink routing.MessageSink) {}
|
||||
|
||||
func (u *signallerUnimplemented) GetResponseSink() routing.MessageSink {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallerUnimplemented) CloseSignalConnection(reason types.SignallingCloseReason) {}
|
||||
|
||||
func (u *signallerUnimplemented) WriteMessage(msg proto.Message) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
// 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"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/psrpc"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var _ ParticipantSignaller = (*signallerv2Async)(nil)
|
||||
|
||||
type Signallerv2AsyncParams struct {
|
||||
Logger logger.Logger
|
||||
Participant types.LocalParticipant
|
||||
}
|
||||
|
||||
type signallerv2Async struct {
|
||||
params Signallerv2AsyncParams
|
||||
|
||||
*signallerAsyncBase
|
||||
|
||||
signalFragment *SignalFragment
|
||||
}
|
||||
|
||||
func NewSignallerv2Async(params Signallerv2AsyncParams) ParticipantSignaller {
|
||||
return &signallerv2Async{
|
||||
params: params,
|
||||
signallerAsyncBase: newSignallerAsyncBase(signallerAsyncBaseParams{Logger: params.Logger}),
|
||||
signalFragment: NewSignalFragment(SignalFragmentParams{
|
||||
Logger: params.Logger,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// SIGNALLING-V2-TODO: need to lock write so that fragments do not get interrupted
|
||||
func (s *signallerv2Async) WriteMessage(msg proto.Message) error {
|
||||
if msg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.params.Participant.IsDisconnected() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !s.params.Participant.IsReady() {
|
||||
if typed, ok := msg.(*livekit.Signalv2WireMessage); !ok {
|
||||
s.params.Logger.Warnw(
|
||||
"unknown message type", nil,
|
||||
"messageType", fmt.Sprintf("%T", msg),
|
||||
)
|
||||
} else {
|
||||
if !hasConnectResponse(typed) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sink := s.GetResponseSink()
|
||||
if sink == nil {
|
||||
if typed, ok := msg.(*livekit.Signalv2WireMessage); ok {
|
||||
s.params.Logger.Debugw(
|
||||
"could not send message to participant",
|
||||
"messageType", fmt.Sprintf("%T", typed.Message),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SIGNALLING-V2-TODO: avoid double marshalling,
|
||||
// have to marshal once to get size of serialised packet and decide if it needs fragmentation,
|
||||
// should used the marshaled bytes if fragmentation is not needed
|
||||
var fragments []*livekit.Fragment
|
||||
marshaled, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
if typed, ok := msg.(*livekit.Signalv2WireMessage); ok {
|
||||
s.params.Logger.Warnw(
|
||||
"could not send message to participant", err,
|
||||
"messageType", fmt.Sprintf("%T", typed.Message),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
fragments = s.signalFragment.Segment(marshaled)
|
||||
}
|
||||
|
||||
sendMsg := func(m proto.Message) error {
|
||||
if err := sink.WriteMessage(m); err != nil {
|
||||
// SIGNALLING-V2-TODO: check for data channel errors to treat as debug too
|
||||
if utils.ErrorIsOneOf(err, psrpc.Canceled, routing.ErrChannelClosed) {
|
||||
if typed, ok := m.(*livekit.Signalv2WireMessage); ok {
|
||||
s.params.Logger.Debugw(
|
||||
"could not send message to participant",
|
||||
"error", err,
|
||||
"messageType", fmt.Sprintf("%T", typed.Message),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
if typed, ok := m.(*livekit.Signalv2WireMessage); ok {
|
||||
s.params.Logger.Warnw(
|
||||
"could not send message to participant", err,
|
||||
"messageType", fmt.Sprintf("%T", typed.Message),
|
||||
)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(fragments) != 0 {
|
||||
for _, fragment := range fragments {
|
||||
wireMessage := &livekit.Signalv2WireMessage{
|
||||
Message: &livekit.Signalv2WireMessage_Fragment{
|
||||
Fragment: fragment,
|
||||
},
|
||||
}
|
||||
if err := sendMsg(wireMessage); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := sendMsg(msg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
|
||||
func hasConnectResponse(wireMessage *livekit.Signalv2WireMessage) bool {
|
||||
switch msg := wireMessage.GetMessage().(type) {
|
||||
case *livekit.Signalv2WireMessage_Envelope:
|
||||
for _, innerMsg := range msg.Envelope.GetServerMessages() {
|
||||
switch innerMsg.GetMessage().(type) {
|
||||
case *livekit.Signalv2ServerMessage_ConnectResponse:
|
||||
return true
|
||||
|
||||
default:
|
||||
return false // first message should be `ConnectResponse`
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
// SIGNALLING-V2-TODO: handle ConnectResponse getting fragmented.
|
||||
return false
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// 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 (
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
var _ ParticipantSignaller = (*signallerv2Hybrid)(nil)
|
||||
|
||||
type Signallerv2HybridParams struct {
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type signallerv2Hybrid struct {
|
||||
params Signallerv2HybridParams
|
||||
|
||||
*signallerv2Async
|
||||
}
|
||||
|
||||
func NewSignallerv2Hybrid(params Signallerv2HybridParams) ParticipantSignaller {
|
||||
return &signallerv2Hybrid{
|
||||
params: params,
|
||||
signallerv2Async: NewSignallerv2Async(Signallerv2AsyncParams{Logger: params.Logger}).(*signallerv2Async),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
// 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 (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var _ ParticipantSignalling = (*signalling)(nil)
|
||||
|
||||
type SignallingParams struct {
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type signalling struct {
|
||||
signallingUnimplemented
|
||||
|
||||
params SignallingParams
|
||||
}
|
||||
|
||||
func NewSignalling(params SignallingParams) ParticipantSignalling {
|
||||
return &signalling{
|
||||
params: params,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalJoinResponse(join *livekit.JoinResponse) proto.Message {
|
||||
if join == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Join{
|
||||
Join: join,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalParticipantUpdate(participants []*livekit.ParticipantInfo) proto.Message {
|
||||
if len(participants) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Update{
|
||||
Update: &livekit.ParticipantUpdate{
|
||||
Participants: participants,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalSpeakerUpdate(speakers []*livekit.SpeakerInfo) proto.Message {
|
||||
if len(speakers) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_SpeakersChanged{
|
||||
SpeakersChanged: &livekit.SpeakersChanged{
|
||||
Speakers: speakers,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalRoomUpdate(room *livekit.Room) proto.Message {
|
||||
if room == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_RoomUpdate{
|
||||
RoomUpdate: &livekit.RoomUpdate{
|
||||
Room: room,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalConnectionQualityUpdate(connectionQuality *livekit.ConnectionQualityUpdate) proto.Message {
|
||||
if connectionQuality == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_ConnectionQuality{
|
||||
ConnectionQuality: connectionQuality,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalRefreshToken(token string) proto.Message {
|
||||
if token == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_RefreshToken{
|
||||
RefreshToken: token,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalRequestResponse(requestResponse *livekit.RequestResponse) proto.Message {
|
||||
if requestResponse == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_RequestResponse{
|
||||
RequestResponse: requestResponse,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalRoomMovedResponse(roomMoved *livekit.RoomMovedResponse) proto.Message {
|
||||
if roomMoved == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_RoomMoved{
|
||||
RoomMoved: roomMoved,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalReconnectResponse(reconnect *livekit.ReconnectResponse) proto.Message {
|
||||
if reconnect == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Reconnect{
|
||||
Reconnect: reconnect,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalICECandidate(trickle *livekit.TrickleRequest) proto.Message {
|
||||
if trickle == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Trickle{
|
||||
Trickle: trickle,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalTrackMuted(mute *livekit.MuteTrackRequest) proto.Message {
|
||||
if mute == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Mute{
|
||||
Mute: mute,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalTrackPublished(trackPublished *livekit.TrackPublishedResponse) proto.Message {
|
||||
if trackPublished == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_TrackPublished{
|
||||
TrackPublished: trackPublished,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalTrackUnpublished(trackUnpublished *livekit.TrackUnpublishedResponse) proto.Message {
|
||||
if trackUnpublished == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_TrackUnpublished{
|
||||
TrackUnpublished: trackUnpublished,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalTrackSubscribed(trackSubscribed *livekit.TrackSubscribed) proto.Message {
|
||||
if trackSubscribed == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_TrackSubscribed{
|
||||
TrackSubscribed: trackSubscribed,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalLeaveRequest(leave *livekit.LeaveRequest) proto.Message {
|
||||
if leave == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Leave{
|
||||
Leave: leave,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalSdpAnswer(answer *livekit.SessionDescription) proto.Message {
|
||||
if answer == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Answer{
|
||||
Answer: answer,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalSdpOffer(offer *livekit.SessionDescription) proto.Message {
|
||||
if offer == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Offer{
|
||||
Offer: offer,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalStreamStateUpdate(streamStateUpdate *livekit.StreamStateUpdate) proto.Message {
|
||||
if streamStateUpdate == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_StreamStateUpdate{
|
||||
StreamStateUpdate: streamStateUpdate,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalSubscribedQualityUpdate(subscribedQualityUpdate *livekit.SubscribedQualityUpdate) proto.Message {
|
||||
if subscribedQualityUpdate == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_SubscribedQualityUpdate{
|
||||
SubscribedQualityUpdate: subscribedQualityUpdate,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalSubscriptionResponse(subscriptionResponse *livekit.SubscriptionResponse) proto.Message {
|
||||
if subscriptionResponse == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_SubscriptionResponse{
|
||||
SubscriptionResponse: subscriptionResponse,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalling) SignalSubscriptionPermissionUpdate(subscriptionPermissionUpdate *livekit.SubscriptionPermissionUpdate) proto.Message {
|
||||
if subscriptionPermissionUpdate == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_SubscriptionPermissionUpdate{
|
||||
SubscriptionPermissionUpdate: subscriptionPermissionUpdate,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// 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 (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var _ ParticipantSignalling = (*signallingUnimplemented)(nil)
|
||||
|
||||
type signallingUnimplemented struct{}
|
||||
|
||||
func (u *signallingUnimplemented) SignalJoinResponse(join *livekit.JoinResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalParticipantUpdate(participants []*livekit.ParticipantInfo) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalSpeakerUpdate(speakers []*livekit.SpeakerInfo) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalRoomUpdate(room *livekit.Room) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalConnectionQualityUpdate(connectionQuality *livekit.ConnectionQualityUpdate) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalRefreshToken(token string) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalRequestResponse(requestResponse *livekit.RequestResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalRoomMovedResponse(roomMoved *livekit.RoomMovedResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalReconnectResponse(reconnect *livekit.ReconnectResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalICECandidate(trickle *livekit.TrickleRequest) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalTrackMuted(mute *livekit.MuteTrackRequest) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalTrackPublished(trackPublished *livekit.TrackPublishedResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalTrackUnpublished(trackUnpublished *livekit.TrackUnpublishedResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalTrackSubscribed(trackSubscribed *livekit.TrackSubscribed) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalLeaveRequest(leave *livekit.LeaveRequest) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalSdpAnswer(answer *livekit.SessionDescription) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalSdpOffer(offer *livekit.SessionDescription) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalStreamStateUpdate(streamStateUpdate *livekit.StreamStateUpdate) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalSubscribedQualityUpdate(subscribedQualityUpdate *livekit.SubscribedQualityUpdate) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalSubscriptionResponse(subscriptionResponse *livekit.SubscriptionResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalSubscriptionPermissionUpdate(subscriptionPermissionUpdate *livekit.SubscriptionPermissionUpdate) proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) AckMessageId(ackMessageId uint32) {}
|
||||
|
||||
func (u *signallingUnimplemented) SetLastProcessedRemoteMessageId(lastProcessedRemoteMessageId uint32) {
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) PendingMessages() proto.Message {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *signallingUnimplemented) SignalConnectResponse(connectResponse *livekit.ConnectResponse) proto.Message {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// 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 (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var _ ParticipantSignalling = (*signallingv2)(nil)
|
||||
|
||||
type Signallingv2Params struct {
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type signallingv2 struct {
|
||||
signallingUnimplemented
|
||||
|
||||
params Signallingv2Params
|
||||
|
||||
signalCache *SignalCache
|
||||
signalFragment *SignalFragment
|
||||
}
|
||||
|
||||
func NewSignallingv2(params Signallingv2Params) ParticipantSignalling {
|
||||
return &signallingv2{
|
||||
params: params,
|
||||
signalCache: NewSignalCache(SignalCacheParams{
|
||||
Logger: params.Logger,
|
||||
}),
|
||||
signalFragment: NewSignalFragment(SignalFragmentParams{
|
||||
Logger: params.Logger,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signallingv2) AckMessageId(ackMessageId uint32) {
|
||||
s.signalCache.Clear(ackMessageId)
|
||||
}
|
||||
|
||||
func (s *signallingv2) SetLastProcessedRemoteMessageId(lastProcessedRemoteMessageId uint32) {
|
||||
s.signalCache.SetLastProcessedRemoteMessageId(lastProcessedRemoteMessageId)
|
||||
}
|
||||
|
||||
func (s *signallingv2) PendingMessages() proto.Message {
|
||||
serverMessages := s.signalCache.GetFromFront()
|
||||
if len(serverMessages) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &livekit.Signalv2WireMessage{
|
||||
Message: &livekit.Signalv2WireMessage_Envelope{
|
||||
Envelope: &livekit.Envelope{
|
||||
ServerMessages: serverMessages,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signallingv2) SignalConnectResponse(connectResponse *livekit.ConnectResponse) proto.Message {
|
||||
if connectResponse == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
serverMessage := &livekit.Signalv2ServerMessage{
|
||||
Message: &livekit.Signalv2ServerMessage_ConnectResponse{
|
||||
ConnectResponse: connectResponse,
|
||||
},
|
||||
}
|
||||
s.signalCache.Add(serverMessage)
|
||||
return &livekit.Signalv2WireMessage{
|
||||
Message: &livekit.Signalv2WireMessage_Envelope{
|
||||
Envelope: &livekit.Envelope{
|
||||
ServerMessages: []*livekit.Signalv2ServerMessage{serverMessage},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user