@@ -656,6 +656,20 @@ func (c *inboundCall) appendLogValues(kvs ...any) {
656656 c .setLog (c .log ().WithValues (kvs ... ))
657657}
658658
659+ func (c * inboundCall ) mediaTimeout () error {
660+ if c .cc == nil {
661+ c .closeWithTimeout (true )
662+ return psrpc .NewErrorf (psrpc .DeadlineExceeded , "media timeout" )
663+ }
664+ if ! c .cc .GotACK () {
665+ c .log ().Warnw ("Media timeout after missing ACK" , errNoACK )
666+ c .closeWithNoACK ()
667+ return psrpc .NewError (psrpc .DeadlineExceeded , errNoACK )
668+ }
669+ c .closeWithTimeout (false )
670+ return nil // logged as a warning in close
671+ }
672+
659673func (c * inboundCall ) handleInvite (ctx context.Context , tid traceid.ID , req * sip.Request , trunkID string , conf * config.Config ) error {
660674 c .mon .InviteAccept ()
661675 c .mon .CallStart ()
@@ -876,7 +890,6 @@ func (c *inboundCall) handleInvite(ctx context.Context, tid traceid.ID, req *sip
876890
877891 c .started .Break ()
878892
879- var noAck = false
880893 // Wait for the caller to terminate the call. Send regular keep alives
881894 ticker := time .NewTicker (stateUpdateTick )
882895 defer ticker .Stop ()
@@ -896,13 +909,7 @@ func (c *inboundCall) handleInvite(ctx context.Context, tid traceid.ID, req *sip
896909 c .close (false , callDropped , "removed" )
897910 return nil
898911 case <- c .media .Timeout ():
899- if noAck {
900- c .log ().Warnw ("Media timeout after missing ACK" , errNoACK )
901- c .closeWithNoACK ()
902- return psrpc .NewError (psrpc .DeadlineExceeded , errNoACK )
903- }
904- c .closeWithTimeout ()
905- return psrpc .NewErrorf (psrpc .DeadlineExceeded , "media timeout" )
912+ return c .mediaTimeout ()
906913 case <- ackReceived :
907914 ackTimeout = nil // all good, disable timeout
908915 ackReceived = nil
@@ -911,7 +918,6 @@ func (c *inboundCall) handleInvite(ctx context.Context, tid traceid.ID, req *sip
911918 c .log ().Warnw ("Call accepted, but no ACK received" , errNoACK )
912919 // We don't need to wait for a full media timeout initially, we already know something is not quite right.
913920 c .media .SetTimeout (min (inviteOkAckLateTimeout , c .s .conf .MediaTimeoutInitial ), c .s .conf .MediaTimeout )
914- noAck = true
915921 }
916922 }
917923}
@@ -997,8 +1003,7 @@ func (c *inboundCall) waitMedia(ctx context.Context) (bool, error) {
9971003 c .closeWithHangup ()
9981004 return false , psrpc .NewErrorf (psrpc .Canceled , "room closed" )
9991005 case <- c .media .Timeout ():
1000- c .closeWithTimeout ()
1001- return false , psrpc .NewErrorf (psrpc .DeadlineExceeded , "media timed out" )
1006+ return false , c .mediaTimeout ()
10021007 case <- c .media .Received ():
10031008 case <- delay .C :
10041009 }
@@ -1021,8 +1026,7 @@ func (c *inboundCall) waitSubscribe(ctx context.Context, timeout time.Duration)
10211026 c .closeWithHangup ()
10221027 return false , psrpc .NewErrorf (psrpc .Canceled , "room closed" )
10231028 case <- c .media .Timeout ():
1024- c .closeWithTimeout ()
1025- return false , psrpc .NewErrorf (psrpc .DeadlineExceeded , "media timed out" )
1029+ return false , c .mediaTimeout ()
10261030 case <- timer .C :
10271031 c .close (false , callDropped , "cannot-subscribe" )
10281032 return false , psrpc .NewErrorf (psrpc .DeadlineExceeded , "room subscription timed out" )
@@ -1048,8 +1052,7 @@ func (c *inboundCall) pinPrompt(ctx context.Context, trunkID string) (disp CallD
10481052 c .closeWithHangup ()
10491053 return disp , false , nil
10501054 case <- c .media .Timeout ():
1051- c .closeWithTimeout ()
1052- return disp , false , psrpc .NewErrorf (psrpc .DeadlineExceeded , "media timeout" )
1055+ return disp , false , c .mediaTimeout ()
10531056 case b , ok := <- c .dtmf :
10541057 if ! ok {
10551058 c .Close ()
@@ -1114,7 +1117,8 @@ func (c *inboundCall) close(error bool, status CallStatus, reason string) {
11141117 defer c .printStats (log )
11151118 c .setStatus (status )
11161119 c .mon .CallTerminate (reason )
1117- if error {
1120+ isWarn := error || status == callHangupMedia
1121+ if isWarn {
11181122 log .Warnw ("Closing inbound call with error" , nil )
11191123 } else {
11201124 log .Infow ("Closing inbound call" )
@@ -1150,8 +1154,12 @@ func (c *inboundCall) close(error bool, status CallStatus, reason string) {
11501154 c .cancel ()
11511155}
11521156
1153- func (c * inboundCall ) closeWithTimeout () {
1154- c .close (true , callDropped , "media-timeout" )
1157+ func (c * inboundCall ) closeWithTimeout (isError bool ) {
1158+ status := callDropped
1159+ if ! isError {
1160+ status = callHangupMedia
1161+ }
1162+ c .close (isError , status , "media-timeout" )
11551163}
11561164
11571165func (c * inboundCall ) closeWithNoACK () {
@@ -1583,6 +1591,10 @@ func (c *sipInbound) stopRinging() {
15831591 }
15841592}
15851593
1594+ func (c * sipInbound ) GotACK () bool {
1595+ return c .acked .IsBroken ()
1596+ }
1597+
15861598func (c * sipInbound ) InviteACK () <- chan struct {} {
15871599 return c .acked .Watch ()
15881600}
0 commit comments