Files
protocol/rpc/sip.go
David Zhao e0c49d3c9d Support for key/value attributes on Participant (#733)
* Support for key/value attributes on Participant

Key/value attributes lets us:
* allow various components to provide more information about the participant (i.e. SIP including the phone number)
* allow Agents to communicate their own status to the end user via RTC
* enable applications to update specific fields without overriding others

* generated protobuf

* Attribute support for SIP.

* generated protobuf

* generated protobuf

* allow specifying attributes during token generation

* Remove sip prefix and rename phone number attrs.

* regenerate protos

* update tests

* add kv attrs to sip token

* add attribute test

* changeset

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Denys Smirnov <dennwc@pm.me>
2024-06-19 22:27:09 -07:00

67 lines
2.0 KiB
Go

package rpc
import (
"errors"
"maps"
"math/rand/v2"
"strings"
"github.com/livekit/protocol/livekit"
)
// NewCreateSIPParticipantRequest fills InternalCreateSIPParticipantRequest from
// livekit.CreateSIPParticipantRequest and livekit.SIPTrunkInfo.
func NewCreateSIPParticipantRequest(
callID, wsUrl, token string,
req *livekit.CreateSIPParticipantRequest,
trunk *livekit.SIPOutboundTrunkInfo,
) (*InternalCreateSIPParticipantRequest, error) {
if len(trunk.Numbers) == 0 {
return nil, errors.New("no numbers on outbound trunk")
}
outboundNumber := trunk.Numbers[rand.IntN(len(trunk.Numbers))]
// A sanity check for the number format for well-known providers.
switch {
case strings.HasSuffix(trunk.Address, "telnyx.com"):
// Telnyx omits leading '+' by default.
outboundNumber = strings.TrimPrefix(outboundNumber, "+")
case strings.HasSuffix(trunk.Address, "twilio.com"):
// Twilio requires leading '+'.
if !strings.HasPrefix(outboundNumber, "+") {
outboundNumber = "+" + outboundNumber
}
}
attrs := maps.Clone(req.ParticipantAttributes)
if attrs == nil {
attrs = make(map[string]string)
}
attrs[livekit.AttrSIPCallID] = callID
trunkID := req.SipTrunkId
if trunkID == "" {
trunkID = trunk.SipTrunkId
}
attrs[livekit.AttrSIPTrunkID] = trunkID
if !req.HidePhoneNumber {
attrs[livekit.AttrSIPPhoneNumber] = req.SipCallTo
attrs[livekit.AttrSIPTrunkNumber] = outboundNumber
}
return &InternalCreateSIPParticipantRequest{
SipCallId: callID,
Address: trunk.Address,
Transport: trunk.Transport,
Number: outboundNumber,
Username: trunk.AuthUsername,
Password: trunk.AuthPassword,
CallTo: req.SipCallTo,
WsUrl: wsUrl,
Token: token,
RoomName: req.RoomName,
ParticipantIdentity: req.ParticipantIdentity,
ParticipantName: req.ParticipantName,
ParticipantMetadata: req.ParticipantMetadata,
ParticipantAttributes: attrs,
Dtmf: req.Dtmf,
PlayRingtone: req.PlayRingtone,
}, nil
}