|
| 1 | +// Copyright 2023 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package signalling |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/livekit/protocol/livekit" |
| 19 | +) |
| 20 | + |
| 21 | +func GetConnectRequest(wireMessage *livekit.Signalv2WireMessage) *livekit.ConnectRequest { |
| 22 | + if wireMessage != nil { |
| 23 | + switch msg := wireMessage.GetMessage().(type) { |
| 24 | + case *livekit.Signalv2WireMessage_Envelope: |
| 25 | + for _, innerMsg := range msg.Envelope.GetClientMessages() { |
| 26 | + switch clientMessage := innerMsg.GetMessage().(type) { |
| 27 | + case *livekit.Signalv2ClientMessage_ConnectRequest: |
| 28 | + return clientMessage.ConnectRequest |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +func WithoutConnectRequest(wireMessage *livekit.Signalv2WireMessage) *livekit.Signalv2WireMessage { |
| 38 | + var strippedWireMessage *livekit.Signalv2WireMessage |
| 39 | + if wireMessage != nil { |
| 40 | + switch msg := wireMessage.GetMessage().(type) { |
| 41 | + case *livekit.Signalv2WireMessage_Envelope: |
| 42 | + clientMessages := make( |
| 43 | + []*livekit.Signalv2ClientMessage, |
| 44 | + 0, |
| 45 | + len(msg.Envelope.GetClientMessages()), |
| 46 | + ) |
| 47 | + for _, clientMessage := range msg.Envelope.GetClientMessages() { |
| 48 | + switch clientMessage.GetMessage().(type) { |
| 49 | + case *livekit.Signalv2ClientMessage_ConnectRequest: |
| 50 | + default: |
| 51 | + clientMessages = append(clientMessages, clientMessage) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if len(clientMessages) != 0 { |
| 56 | + strippedWireMessage = &livekit.Signalv2WireMessage{ |
| 57 | + Message: &livekit.Signalv2WireMessage_Envelope{ |
| 58 | + Envelope: &livekit.Envelope{ |
| 59 | + ClientMessages: clientMessages, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return strippedWireMessage |
| 68 | +} |
| 69 | + |
| 70 | +func GetConnectResponse(wireMessage *livekit.Signalv2WireMessage) *livekit.ConnectResponse { |
| 71 | + if wireMessage != nil { |
| 72 | + switch msg := wireMessage.GetMessage().(type) { |
| 73 | + case *livekit.Signalv2WireMessage_Envelope: |
| 74 | + for _, innerMsg := range msg.Envelope.GetServerMessages() { |
| 75 | + switch serverMessage := innerMsg.GetMessage().(type) { |
| 76 | + case *livekit.Signalv2ServerMessage_ConnectResponse: |
| 77 | + return serverMessage.ConnectResponse |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
0 commit comments