@@ -7,15 +7,35 @@ import (
77 "slices"
88 "strconv"
99 "strings"
10+ "time"
1011
1112 "golang.org/x/text/language"
1213 "google.golang.org/grpc/codes"
1314 "google.golang.org/grpc/status"
1415 "google.golang.org/protobuf/proto"
16+ "google.golang.org/protobuf/types/known/durationpb"
1517
1618 "github.com/livekit/protocol/utils/xtwirp"
1719)
1820
21+ // MaxSIPMediaTimeout is the maximum allowed trunk / API value for media_timeout
22+ // (no incoming RTP before the RTP path is torn down)
23+ const MaxSIPMediaTimeout = 10 * time .Minute
24+
25+ func validateDuration (name string , d * durationpb.Duration , min , max * time.Duration ) error {
26+ if d == nil {
27+ return nil
28+ }
29+ dur := d .AsDuration ()
30+ if min != nil && dur < * min {
31+ return fmt .Errorf ("%s must not be less than %v" , name , * min )
32+ }
33+ if max != nil && dur > * max {
34+ return fmt .Errorf ("%s must not be greater than %v" , name , * max )
35+ }
36+ return nil
37+ }
38+
1939var (
2040 _ xtwirp.ErrorMeta = (* SIPStatus )(nil )
2141 _ error = (* SIPStatus )(nil )
@@ -452,6 +472,10 @@ func (p *SIPInboundTrunkInfo) Validate() error {
452472 if err := validateHeaderToAttributes (p .HeadersToAttributes ); err != nil {
453473 return err
454474 }
475+ timeout := MaxSIPMediaTimeout
476+ if err := validateDuration ("media_timeout" , p .MediaTimeout , nil , & timeout ); err != nil {
477+ return err
478+ }
455479 return nil
456480}
457481
@@ -465,6 +489,10 @@ func (p *SIPInboundTrunkUpdate) Validate() error {
465489 if err := p .AllowedNumbers .Validate (); err != nil {
466490 return err
467491 }
492+ timeout := MaxSIPMediaTimeout
493+ if err := validateDuration ("media_timeout" , p .MediaTimeout , nil , & timeout ); err != nil {
494+ return err
495+ }
468496 return nil
469497}
470498
@@ -480,6 +508,7 @@ func (p *SIPInboundTrunkUpdate) Apply(info *SIPInboundTrunkInfo) error {
480508 applyUpdate (& info .Name , p .Name )
481509 applyUpdate (& info .Metadata , p .Metadata )
482510 applyUpdate (& info .MediaEncryption , p .MediaEncryption )
511+ applyUpdatePtr (& info .MediaTimeout , p .MediaTimeout )
483512 return info .Validate ()
484513}
485514
@@ -541,6 +570,10 @@ func (p *SIPOutboundTrunkInfo) Validate() error {
541570 if err := validateHeaderToAttributes (p .HeadersToAttributes ); err != nil {
542571 return err
543572 }
573+ timeout := MaxSIPMediaTimeout
574+ if err := validateDuration ("media_timeout" , p .MediaTimeout , nil , & timeout ); err != nil {
575+ return err
576+ }
544577 return nil
545578}
546579
@@ -560,13 +593,21 @@ func (p *SIPOutboundConfig) Validate() error {
560593 if err := validateHeaderToAttributes (p .HeadersToAttributes ); err != nil {
561594 return err
562595 }
596+ timeout := MaxSIPMediaTimeout
597+ if err := validateDuration ("media_timeout" , p .MediaTimeout , nil , & timeout ); err != nil {
598+ return err
599+ }
563600 return nil
564601}
565602
566603func (p * SIPOutboundTrunkUpdate ) Validate () error {
567604 if err := p .Numbers .Validate (); err != nil {
568605 return err
569606 }
607+ timeout := MaxSIPMediaTimeout
608+ if err := validateDuration ("media_timeout" , p .MediaTimeout , nil , & timeout ); err != nil {
609+ return err
610+ }
570611 return nil
571612}
572613
@@ -584,6 +625,7 @@ func (p *SIPOutboundTrunkUpdate) Apply(info *SIPOutboundTrunkInfo) error {
584625 applyUpdate (& info .Metadata , p .Metadata )
585626 applyUpdate (& info .MediaEncryption , p .MediaEncryption )
586627 applyUpdate (& info .FromHost , p .FromHost )
628+ applyUpdatePtr (& info .MediaTimeout , p .MediaTimeout )
587629 return info .Validate ()
588630}
589631
0 commit comments