Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into benjamin/analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
biglittlebigben committed Nov 12, 2024
2 parents d4446bd + 3950d6c commit ec9ec4d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ require (
github.com/nats-io/nuid v1.0.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
github.com/opencontainers/runc v1.1.14 // indirect
github.com/pion/datachannel v1.5.9 // indirect
github.com/pion/dtls/v2 v2.2.12 // indirect
github.com/pion/ice/v2 v2.3.36 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/opencontainers/runc v1.1.12 h1:BOIssBaW1La0/qbNZHXOOa71dZfZEQOzW7dqQf3phss=
github.com/opencontainers/runc v1.1.12/go.mod h1:S+lQwSfncpBha7XTy/5lBwWgm5+y5Ma/O44Ekby9FK8=
github.com/opencontainers/runc v1.1.14 h1:rgSuzbmgz5DUJjeSnw337TxDbRuqjs6iqQck/2weR6w=
github.com/opencontainers/runc v1.1.14/go.mod h1:E4C2z+7BxR7GHXp0hAY53mek+x49X1LjPNeMTfRGvOA=
github.com/ory/dockertest/v3 v3.10.0 h1:4K3z2VMe8Woe++invjaTB7VRyQXQy5UY+loujO4aNE4=
github.com/ory/dockertest/v3 v3.10.0/go.mod h1:nr57ZbRWMqfsdGdFNLHz5jjNdDb7VVFnzAeW1n5N1Lg=
github.com/pion/datachannel v1.5.9 h1:LpIWAOYPyDrXtU+BW7X0Yt/vGtYxtXQ8ql7dFfYUVZA=
Expand Down
9 changes: 8 additions & 1 deletion pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,14 +910,17 @@ func (s *Server) newInbound(id LocalTag, contact URI, invite *sip.Request, invit
if h != nil {
c.nextRequestCSeq = h.SeqNo + 1
}

if callID, _ := invite.CallID(); callID != nil {
c.callID = callID.Value()
}
return c
}

type sipInbound struct {
s *Server
id LocalTag
tag RemoteTag
callID string
invite *sip.Request
inviteTx sip.ServerTransaction
contact *sip.ContactHeader
Expand Down Expand Up @@ -1004,6 +1007,10 @@ func (c *sipInbound) Tag() RemoteTag {
return c.tag
}

func (c *sipInbound) CallID() string {
return c.callID
}

func (c *sipInbound) RemoteHeaders() Headers {
c.mu.RLock()
defer c.mu.RUnlock()
Expand Down
26 changes: 17 additions & 9 deletions pkg/sip/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,13 @@ func (c *outboundCall) sipSignal(ctx context.Context) error {
}
joinDur()

if len(c.cc.RemoteHeaders()) != 0 {
extra := HeadersToAttrs(nil, c.sipConf.headersToAttrs, c.cc)
if c.lkRoom != nil && len(extra) != 0 {
room := c.lkRoom.Room()
if room != nil {
room.LocalParticipant.SetAttributes(extra)
} else {
c.log.Warnw("could not set attributes on nil room", nil, "attrs", extra)
}
extra := HeadersToAttrs(nil, c.sipConf.headersToAttrs, c.cc)
if c.lkRoom != nil && len(extra) != 0 {
room := c.lkRoom.Room()
if room != nil {
room.LocalParticipant.SetAttributes(extra)
} else {
c.log.Warnw("could not set attributes on nil room", nil, "attrs", extra)
}
}
return nil
Expand Down Expand Up @@ -531,6 +529,7 @@ type sipOutbound struct {

mu sync.RWMutex
tag RemoteTag
callID string
invite *sip.Request
inviteOk *sip.Response
to *sip.ToHeader
Expand Down Expand Up @@ -563,6 +562,12 @@ func (c *sipOutbound) Tag() RemoteTag {
return c.tag
}

func (c *sipOutbound) CallID() string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.callID
}

func (c *sipOutbound) RemoteHeaders() Headers {
c.mu.RLock()
defer c.mu.RUnlock()
Expand Down Expand Up @@ -665,6 +670,9 @@ authLoop:
if !ok {
return nil, errors.New("no tag in To header in INVITE response")
}
if callID, _ := c.invite.CallID(); callID != nil {
c.callID = callID.Value()
}
h, _ := c.invite.CSeq()
if h != nil {
c.nextRequestCSeq = h.SeqNo + 1
Expand Down
5 changes: 5 additions & 0 deletions pkg/sip/participant.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const (
defaultRingingTimeout = 3 * time.Minute
)

const (
AttrSIPCallIDFull = livekit.AttrSIPPrefix + "callIDFull"
AttrSIPCallTag = livekit.AttrSIPPrefix + "callTag"
)

var headerToLog = map[string]string{
"X-Twilio-AccountSid": "twilioAccSID",
"X-Twilio-CallSid": "twilioCallSID",
Expand Down
1 change: 1 addition & 0 deletions pkg/sip/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Signaling interface {
To() sip.Uri
ID() LocalTag
Tag() RemoteTag
CallID() string
RemoteHeaders() Headers

WriteRequest(req *sip.Request) error
Expand Down
6 changes: 6 additions & 0 deletions pkg/sip/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,11 @@ func HeadersToAttrs(attrs, hdrToAttr map[string]string, c Signaling) map[string]
attrs[name] = h.Value()
}
}
if tag := c.Tag(); tag != "" {
attrs[AttrSIPCallTag] = string(tag)
}
if cid := c.CallID(); cid != "" {
attrs[AttrSIPCallIDFull] = cid
}
return attrs
}
36 changes: 32 additions & 4 deletions test/lktest/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package lktest
import (
"context"
"maps"
"slices"
"strings"

"github.com/livekit/protocol/livekit"
Expand All @@ -27,13 +28,36 @@ import (

func checkSIPAttrs(t TB, exp, got map[string]string) (_, _ map[string]string) {
exp, got = maps.Clone(exp), maps.Clone(got)
if _, ok := exp[livekit.AttrSIPCallID]; ok {
require.True(t, strings.HasPrefix(got[livekit.AttrSIPCallID], guid.SIPCallPrefix))
delete(exp, livekit.AttrSIPCallID)
delete(got, livekit.AttrSIPCallID)

var keepKeys []string
for _, a := range []string{
livekit.AttrSIPCallID,
livekit.AttrSIPPrefix + "callIDFull",
livekit.AttrSIPPrefix + "callTag",
} {
if _, ok := exp[a]; !ok {
continue
}
v, ok := got[a]
if !ok {
// let the caller fail
keepKeys = append(keepKeys, a)
continue
}
require.True(t, ok, "missing attribute %q", a)
require.NotEmpty(t, v, "empty attribute %q", a)
switch a {
case livekit.AttrSIPCallID:
require.True(t, strings.HasPrefix(v, guid.SIPCallPrefix))
}
delete(exp, a)
delete(got, a)
}
// remove extra attributes from comparison
for key := range got {
if slices.Contains(keepKeys, key) {
continue
}
if _, ok := exp[key]; !ok {
delete(got, key)
}
Expand Down Expand Up @@ -129,6 +153,8 @@ func TestSIPOutbound(t TB, ctx context.Context, lkOut, lkIn *LiveKit, params SIP
t.Log("checking rooms (outbound)")
expAttrsOut := map[string]string{
"sip.callID": "<test>", // special case
"sip.callTag": "<test>", // special case
"sip.callIDFull": "<test>", // special case
"sip.callStatus": "active",
"sip.trunkPhoneNumber": params.NumberOut,
"sip.phoneNumber": params.NumberIn,
Expand All @@ -150,6 +176,8 @@ func TestSIPOutbound(t TB, ctx context.Context, lkOut, lkIn *LiveKit, params SIP
t.Log("checking rooms (inbound)")
expAttrsIn := map[string]string{
"sip.callID": "<test>", // special case
"sip.callTag": "<test>", // special case
"sip.callIDFull": "<test>", // special case
"sip.callStatus": "active",
"sip.trunkPhoneNumber": params.NumberIn,
"sip.phoneNumber": params.NumberOut,
Expand Down

0 comments on commit ec9ec4d

Please sign in to comment.