Skip to content

Commit 270b769

Browse files
authored
Set extra headers earlier for outbound calls. (#324)
1 parent 8df6af1 commit 270b769

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

pkg/sip/inbound.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (s *Server) newInboundCall(
356356
extra map[string]string,
357357
) *inboundCall {
358358
// Map known headers immediately on join. The rest of the mapping will be available later.
359-
extra = HeadersToAttrs(extra, nil, 0, cc)
359+
extra = HeadersToAttrs(extra, nil, 0, cc, nil)
360360
c := &inboundCall{
361361
s: s,
362362
log: log,
@@ -472,7 +472,7 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
472472
}
473473
}
474474
p := &disp.Room.Participant
475-
p.Attributes = HeadersToAttrs(p.Attributes, disp.HeadersToAttributes, disp.IncludeHeaders, c.cc)
475+
p.Attributes = HeadersToAttrs(p.Attributes, disp.HeadersToAttributes, disp.IncludeHeaders, c.cc, nil)
476476
if disp.MaxCallDuration <= 0 || disp.MaxCallDuration > maxCallDuration {
477477
disp.MaxCallDuration = maxCallDuration
478478
}

pkg/sip/outbound.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ func (c *outboundCall) connectMedia() {
397397
c.media.HandleDTMF(c.handleDTMF)
398398
}
399399

400-
func sipResponse(ctx context.Context, tx sip.ClientTransaction, stop <-chan struct{}, setState func(code sip.StatusCode)) (*sip.Response, error) {
400+
type sipRespFunc func(code sip.StatusCode, hdrs Headers)
401+
402+
func sipResponse(ctx context.Context, tx sip.ClientTransaction, stop <-chan struct{}, setState sipRespFunc) (*sip.Response, error) {
401403
cnt := 0
402404
for {
403405
select {
@@ -411,12 +413,12 @@ func sipResponse(ctx context.Context, tx sip.ClientTransaction, stop <-chan stru
411413
return nil, psrpc.NewErrorf(psrpc.Canceled, "transaction failed to complete (%d intermediate responses)", cnt)
412414
case res := <-tx.Responses():
413415
status := res.StatusCode
416+
if setState != nil {
417+
setState(res.StatusCode, res.Headers())
418+
}
414419
if status/100 != 1 { // != 1xx
415420
return res, nil
416421
}
417-
if setState != nil {
418-
setState(res.StatusCode)
419-
}
420422
// continue
421423
cnt++
422424
}
@@ -442,6 +444,18 @@ func (c *outboundCall) setStatus(v CallStatus) {
442444
})
443445
}
444446

447+
func (c *outboundCall) setExtraAttrs(hdrToAttr map[string]string, opts livekit.SIPHeaderOptions, cc Signaling, hdrs Headers) {
448+
extra := HeadersToAttrs(nil, hdrToAttr, opts, cc, hdrs)
449+
if c.lkRoom != nil && len(extra) != 0 {
450+
room := c.lkRoom.Room()
451+
if room != nil {
452+
room.LocalParticipant.SetAttributes(extra)
453+
} else {
454+
c.log.Warnw("could not set attributes on nil room", nil, "attrs", extra)
455+
}
456+
}
457+
}
458+
445459
func (c *outboundCall) sipSignal(ctx context.Context) error {
446460
ctx, span := tracer.Start(ctx, "outboundCall.sipSignal")
447461
defer span.End()
@@ -479,11 +493,15 @@ func (c *outboundCall) sipSignal(ctx context.Context) error {
479493
toUri := CreateURIFromUserAndAddress(c.sipConf.to, c.sipConf.address, TransportFrom(c.sipConf.transport))
480494

481495
ringing := false
482-
sdpResp, err := c.cc.Invite(ctx, toUri, c.sipConf.user, c.sipConf.pass, c.sipConf.headers, sdpOfferData, func(code sip.StatusCode) {
483-
if !ringing && code >= sip.StatusRinging {
496+
sdpResp, err := c.cc.Invite(ctx, toUri, c.sipConf.user, c.sipConf.pass, c.sipConf.headers, sdpOfferData, func(code sip.StatusCode, hdrs Headers) {
497+
if code == sip.StatusOK {
498+
return // is set separately
499+
}
500+
if !ringing && code >= sip.StatusRinging && code < sip.StatusOK {
484501
ringing = true
485502
c.setStatus(CallRinging)
486503
}
504+
c.setExtraAttrs(nil, 0, nil, hdrs)
487505
})
488506
if err != nil {
489507
// TODO: should we retry? maybe new offer will work
@@ -526,15 +544,7 @@ func (c *outboundCall) sipSignal(ctx context.Context) error {
526544
}
527545
joinDur()
528546

529-
extra := HeadersToAttrs(nil, c.sipConf.headersToAttrs, c.sipConf.includeHeaders, c.cc)
530-
if c.lkRoom != nil && len(extra) != 0 {
531-
room := c.lkRoom.Room()
532-
if room != nil {
533-
room.LocalParticipant.SetAttributes(extra)
534-
} else {
535-
c.log.Warnw("could not set attributes on nil room", nil, "attrs", extra)
536-
}
537-
}
547+
c.setExtraAttrs(c.sipConf.headersToAttrs, c.sipConf.includeHeaders, c.cc, nil)
538548
c.state.DeferUpdate(func(info *livekit.SIPCallInfo) {
539549
info.AudioCodec = mc.Audio.Codec.Info().SDPName
540550
if r := c.lkRoom.Room(); r != nil {
@@ -684,7 +694,7 @@ func (c *sipOutbound) RemoteHeaders() Headers {
684694
return c.inviteOk.Headers()
685695
}
686696

687-
func (c *sipOutbound) Invite(ctx context.Context, to URI, user, pass string, headers map[string]string, sdpOffer []byte, setState func(code sip.StatusCode)) ([]byte, error) {
697+
func (c *sipOutbound) Invite(ctx context.Context, to URI, user, pass string, headers map[string]string, sdpOffer []byte, setState sipRespFunc) ([]byte, error) {
688698
ctx, span := tracer.Start(ctx, "sipOutbound.Invite")
689699
defer span.End()
690700
c.mu.Lock()
@@ -814,7 +824,7 @@ func (c *sipOutbound) AckInviteOK(ctx context.Context) error {
814824
return c.c.sipCli.WriteRequest(sip.NewAckRequest(c.invite, c.inviteOk, nil))
815825
}
816826

817-
func (c *sipOutbound) attemptInvite(ctx context.Context, callID sip.CallIDHeader, dest string, to *sip.ToHeader, offer []byte, authHeaderName, authHeader string, headers Headers, setState func(code sip.StatusCode)) (*sip.Request, *sip.Response, error) {
827+
func (c *sipOutbound) attemptInvite(ctx context.Context, callID sip.CallIDHeader, dest string, to *sip.ToHeader, offer []byte, authHeaderName, authHeader string, headers Headers, setState sipRespFunc) (*sip.Request, *sip.Response, error) {
818828
ctx, span := tracer.Start(ctx, "sipOutbound.attemptInvite")
819829
defer span.End()
820830
req := sip.NewRequest(sip.INVITE, to.Address)

pkg/sip/types.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,13 @@ func LoggerWithHeaders(log logger.Logger, c Signaling) logger.Logger {
241241
return log
242242
}
243243

244-
func HeadersToAttrs(attrs, hdrToAttr map[string]string, opts livekit.SIPHeaderOptions, c Signaling) map[string]string {
244+
func HeadersToAttrs(attrs, hdrToAttr map[string]string, opts livekit.SIPHeaderOptions, c Signaling, headers Headers) map[string]string {
245245
if attrs == nil {
246246
attrs = make(map[string]string)
247247
}
248-
headers := c.RemoteHeaders()
248+
if c != nil {
249+
headers = c.RemoteHeaders()
250+
}
249251
// Map all headers, if requested
250252
if opts != livekit.SIPHeaderOptions_SIP_NO_HEADERS {
251253
for _, h := range headers {
@@ -277,12 +279,14 @@ func HeadersToAttrs(attrs, hdrToAttr map[string]string, opts livekit.SIPHeaderOp
277279
attrs[name] = h.Value()
278280
}
279281
}
280-
// Other metadata
281-
if tag := c.Tag(); tag != "" {
282-
attrs[AttrSIPCallTag] = string(tag)
283-
}
284-
if cid := c.CallID(); cid != "" {
285-
attrs[AttrSIPCallIDFull] = cid
282+
if c != nil {
283+
// Other metadata
284+
if tag := c.Tag(); tag != "" {
285+
attrs[AttrSIPCallTag] = string(tag)
286+
}
287+
if cid := c.CallID(); cid != "" {
288+
attrs[AttrSIPCallIDFull] = cid
289+
}
286290
}
287291
return attrs
288292
}

0 commit comments

Comments
 (0)