Skip to content

Commit

Permalink
Allow passing all SIP trunk options in CreateSIPParticipant.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Feb 3, 2025
1 parent c909c95 commit e8d19b4
Show file tree
Hide file tree
Showing 7 changed files with 677 additions and 634 deletions.
6 changes: 6 additions & 0 deletions .changeset/metal-sheep-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/protocol": minor
"github.com/livekit/protocol": minor
---

Allow passing all SIP trunk options in CreateSIPParticipant.
820 changes: 416 additions & 404 deletions livekit/livekit_sip.pb.go

Large diffs are not rendered by default.

439 changes: 220 additions & 219 deletions livekit/livekit_sip.twirp.go

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions livekit/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ func (p *SIPInboundTrunkInfo) Validate() error {
return nil
}

func (p *SIPOutboundTrunkInfo) Validate() error {
if len(p.Numbers) == 0 {
return errors.New("no trunk numbers specified")
}
func (p *SIPOutboundTrunkInfo) validateWithoutNumbers() error {
if p.Address == "" {
return errors.New("no outbound address specified")
} else if strings.Contains(p.Address, "transport=") {
Expand All @@ -249,6 +246,13 @@ func (p *SIPOutboundTrunkInfo) Validate() error {
return nil
}

func (p *SIPOutboundTrunkInfo) Validate() error {
if len(p.Numbers) == 0 {
return errors.New("no trunk numbers specified")
}
return p.validateWithoutNumbers()
}

func (p *CreateSIPDispatchRuleRequest) Validate() error {
if p.Rule == nil {
return errors.New("missing rule")
Expand All @@ -257,9 +261,14 @@ func (p *CreateSIPDispatchRuleRequest) Validate() error {
}

func (p *CreateSIPParticipantRequest) Validate() error {
if p.SipTrunkId == "" {
if p.SipTrunkId == "" && p.SipTrunk == nil {
return errors.New("missing sip trunk id")
}
if p.SipTrunk != nil {
if err := p.SipTrunk.validateWithoutNumbers(); err != nil {
return err
}
}
if p.SipCallTo == "" {
return errors.New("missing sip callee number")
} else if strings.Contains(p.SipCallTo, "@") {
Expand Down
3 changes: 2 additions & 1 deletion protobufs/livekit_sip.proto
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ message DeleteSIPDispatchRuleRequest {
message CreateSIPParticipantRequest {
// What SIP Trunk should be used to dial the user
string sip_trunk_id = 1;
SIPOutboundTrunkInfo sip_trunk = 20;

// What number should be dialed via SIP
string sip_call_to = 2;
Expand Down Expand Up @@ -576,7 +577,7 @@ message CreateSIPParticipantRequest {

// Wait for the answer for the call before returning.
bool wait_until_answered = 19;
// NEXT ID: 20
// NEXT ID: 21
}

message SIPParticipantInfo {
Expand Down
5 changes: 4 additions & 1 deletion rpc/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func NewCreateSIPParticipantRequest(
req *livekit.CreateSIPParticipantRequest,
trunk *livekit.SIPOutboundTrunkInfo,
) (*InternalCreateSIPParticipantRequest, error) {
if req.SipTrunk != nil {
trunk = req.SipTrunk
}
outboundNumber := req.SipNumber
if outboundNumber == "" {
if len(trunk.Numbers) == 0 {
Expand All @@ -40,7 +43,7 @@ func NewCreateSIPParticipantRequest(
}
attrs[livekit.AttrSIPCallID] = callID
trunkID := req.SipTrunkId
if trunkID == "" {
if trunkID == "" && req.SipTrunk == nil {
trunkID = trunk.SipTrunkId
}
attrs[livekit.AttrSIPTrunkID] = trunkID
Expand Down
19 changes: 15 additions & 4 deletions rpc/sip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func TestNewCreateSIPParticipantRequest(t *testing.T) {
"X-B": "B1",
},
}
res, err := NewCreateSIPParticipantRequest("p_123", "call-id", "xyz.sip.livekit.cloud", "url", "token", r, tr)
require.NoError(t, err)
require.Equal(t, &InternalCreateSIPParticipantRequest{
exp := &InternalCreateSIPParticipantRequest{
ProjectId: "p_123",
SipCallId: "call-id",
SipTrunkId: "trunk",
Expand Down Expand Up @@ -68,7 +66,10 @@ func TestNewCreateSIPParticipantRequest(t *testing.T) {
"X-C": "C",
},
WaitUntilAnswered: true,
}, res)
}
res, err := NewCreateSIPParticipantRequest("p_123", "call-id", "xyz.sip.livekit.cloud", "url", "token", r, tr)
require.NoError(t, err)
require.Equal(t, exp, res)

r.HidePhoneNumber = true
res, err = NewCreateSIPParticipantRequest("p_123", "call-id", "xyz.sip.livekit.cloud", "url", "token", r, tr)
Expand Down Expand Up @@ -101,4 +102,14 @@ func TestNewCreateSIPParticipantRequest(t *testing.T) {
},
WaitUntilAnswered: true,
}, res)

r.HidePhoneNumber = false
r.SipTrunk = tr
r.SipTrunkId = ""
tr.SipTrunkId = ""
exp.SipTrunkId = ""
exp.ParticipantAttributes[livekit.AttrSIPTrunkID] = ""
res, err = NewCreateSIPParticipantRequest("p_123", "call-id", "xyz.sip.livekit.cloud", "url", "token", r, nil)
require.NoError(t, err)
require.Equal(t, exp, res)
}

0 comments on commit e8d19b4

Please sign in to comment.