@@ -412,17 +412,21 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
412
412
pinPrompt = true
413
413
}
414
414
415
- // We need to start media first, otherwise we won't be able to send audio prompts to the caller, or receive DTMF.
416
- answerData , err := c .runMediaConn (req .Body (), conf , disp .EnabledFeatures )
417
- if err != nil {
418
- c .log .Errorw ("Cannot start media" , err )
419
- c .cc .RespondAndDrop (sip .StatusInternalServerError , "" )
420
- c .close (true , callDropped , "media-failed" )
421
- return err
415
+ runMedia := func (enc livekit.SIPMediaEncryption ) ([]byte , error ) {
416
+ answerData , err := c .runMediaConn (req .Body (), enc , conf , disp .EnabledFeatures )
417
+ if err != nil {
418
+ c .log .Errorw ("Cannot start media" , err )
419
+ c .cc .RespondAndDrop (sip .StatusInternalServerError , "" )
420
+ c .close (true , callDropped , "media-failed" )
421
+ return nil , err
422
+ }
423
+ return answerData , nil
422
424
}
423
- acceptCall := func () (bool , error ) {
425
+
426
+ // We need to start media first, otherwise we won't be able to send audio prompts to the caller, or receive DTMF.
427
+ acceptCall := func (answerData []byte ) (bool , error ) {
424
428
c .log .Infow ("Accepting the call" , "headers" , disp .Headers )
425
- if err = c .cc .Accept (ctx , answerData , disp .Headers ); err != nil {
429
+ if err : = c .cc .Accept (ctx , answerData , disp .Headers ); err != nil {
426
430
c .log .Errorw ("Cannot respond to INVITE" , err )
427
431
return false , err
428
432
}
@@ -435,15 +439,30 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
435
439
}
436
440
437
441
ok := false
442
+ var answerData []byte
438
443
if pinPrompt {
444
+ var err error
439
445
// Accept the call first on the SIP side, so that we can send audio prompts.
440
- if ok , err = acceptCall (); ! ok {
446
+ // This also means we have to pick encryption setting early, before room is selected.
447
+ // Backend must explicitly enable encryption for pin prompts.
448
+ answerData , err = runMedia (disp .MediaEncryption )
449
+ if err != nil {
450
+ return err // already sent a response
451
+ }
452
+ if ok , err = acceptCall (answerData ); ! ok {
441
453
return err // could be success if the caller hung up
442
454
}
443
455
disp , ok , err = c .pinPrompt (ctx , trunkID )
444
456
if ! ok {
445
457
return err // already sent a response. Could be success if user hung up
446
458
}
459
+ } else {
460
+ // Start media with given encryption settings.
461
+ var err error
462
+ answerData , err = runMedia (disp .MediaEncryption )
463
+ if err != nil {
464
+ return err // already sent a response
465
+ }
447
466
}
448
467
if len (disp .HeadersToAttributes ) != 0 {
449
468
p := & disp .Room .Participant
@@ -486,7 +505,7 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
486
505
if ok , err := c .waitSubscribe (ctx , disp .RingingTimeout ); ! ok {
487
506
return err // already sent a response. Could be success if caller hung up
488
507
}
489
- if ok , err := acceptCall (); ! ok {
508
+ if ok , err := acceptCall (answerData ); ! ok {
490
509
return err // already sent a response. Could be success if caller hung up
491
510
}
492
511
}
@@ -518,11 +537,16 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
518
537
}
519
538
}
520
539
521
- func (c * inboundCall ) runMediaConn (offerData []byte , conf * config.Config , features []livekit.SIPFeature ) (answerData []byte , _ error ) {
540
+ func (c * inboundCall ) runMediaConn (offerData []byte , enc livekit. SIPMediaEncryption , conf * config.Config , features []livekit.SIPFeature ) (answerData []byte , _ error ) {
522
541
c .mon .SDPSize (len (offerData ), true )
523
542
c .log .Debugw ("SDP offer" , "sdp" , string (offerData ))
543
+ e , err := sdpEncryption (enc )
544
+ if err != nil {
545
+ c .log .Errorw ("Cannot parse encryption" , err )
546
+ return nil , err
547
+ }
524
548
525
- mp , err := NewMediaPort (c .log , c .mon , & MediaConfig {
549
+ mp , err := NewMediaPort (c .log , c .mon , & MediaOptions {
526
550
IP : c .s .sconf .SignalingIP ,
527
551
Ports : conf .RTPPort ,
528
552
MediaTimeoutInitial : c .s .conf .MediaTimeoutInitial ,
@@ -535,7 +559,7 @@ func (c *inboundCall) runMediaConn(offerData []byte, conf *config.Config, featur
535
559
c .media .EnableTimeout (false ) // enabled once we accept the call
536
560
c .media .SetDTMFAudio (conf .AudioDTMF )
537
561
538
- answer , mconf , err := mp .SetOffer (offerData )
562
+ answer , mconf , err := mp .SetOffer (offerData , e )
539
563
if err != nil {
540
564
return nil , err
541
565
}
0 commit comments