Skip to content

Commit 95a3247

Browse files
Send regular keep alive while call is active (#390)
1 parent 9b9c61a commit 95a3247

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

pkg/sip/analytics.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,9 @@ func (s *CallState) Flush(ctx context.Context) {
145145
}
146146
s.flush(ctx)
147147
}
148+
149+
func (s *CallState) ForceFlush(ctx context.Context) {
150+
s.mu.Lock()
151+
defer s.mu.Unlock()
152+
s.flush(ctx)
153+
}

pkg/sip/inbound.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -566,20 +566,27 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
566566

567567
c.started.Break()
568568

569-
// Wait for the caller to terminate the call.
570-
select {
571-
case <-ctx.Done():
572-
c.closeWithHangup()
573-
return nil
574-
case <-c.lkRoom.Closed():
575-
c.state.DeferUpdate(func(info *livekit.SIPCallInfo) {
576-
info.DisconnectReason = livekit.DisconnectReason_CLIENT_INITIATED
577-
})
578-
c.close(false, callDropped, "removed")
579-
return nil
580-
case <-c.media.Timeout():
581-
c.closeWithTimeout()
582-
return psrpc.NewErrorf(psrpc.DeadlineExceeded, "media timeout")
569+
ticker := time.NewTicker(10 * time.Minute)
570+
defer ticker.Stop()
571+
// Wait for the caller to terminate the call. Send regular keep alives
572+
for {
573+
select {
574+
case <-ticker.C:
575+
c.log.Debugw("sending keep-alive")
576+
c.state.ForceFlush(ctx)
577+
case <-ctx.Done():
578+
c.closeWithHangup()
579+
return nil
580+
case <-c.lkRoom.Closed():
581+
c.state.DeferUpdate(func(info *livekit.SIPCallInfo) {
582+
info.DisconnectReason = livekit.DisconnectReason_CLIENT_INITIATED
583+
})
584+
c.close(false, callDropped, "removed")
585+
return nil
586+
case <-c.media.Timeout():
587+
c.closeWithTimeout()
588+
return psrpc.NewErrorf(psrpc.DeadlineExceeded, "media timeout")
589+
}
583590
}
584591
}
585592

pkg/sip/outbound.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,25 @@ func (c *outboundCall) Dial(ctx context.Context) error {
205205
func (c *outboundCall) WaitClose(ctx context.Context) error {
206206
ctx = context.WithoutCancel(ctx)
207207
defer c.ensureClosed(ctx)
208-
select {
209-
case <-c.Disconnected():
210-
c.CloseWithReason(callDropped, "removed", livekit.DisconnectReason_CLIENT_INITIATED)
211-
return nil
212-
case <-c.media.Timeout():
213-
c.closeWithTimeout()
214-
err := psrpc.NewErrorf(psrpc.DeadlineExceeded, "media timeout")
215-
c.setErrStatus(ctx, err)
216-
return err
217-
case <-c.Closed():
218-
return nil
208+
209+
ticker := time.NewTicker(10 * time.Minute)
210+
defer ticker.Stop()
211+
for {
212+
select {
213+
case <-ticker.C:
214+
c.log.Debugw("sending keep-alive")
215+
c.state.ForceFlush(ctx)
216+
case <-c.Disconnected():
217+
c.CloseWithReason(callDropped, "removed", livekit.DisconnectReason_CLIENT_INITIATED)
218+
return nil
219+
case <-c.media.Timeout():
220+
c.closeWithTimeout()
221+
err := psrpc.NewErrorf(psrpc.DeadlineExceeded, "media timeout")
222+
c.setErrStatus(ctx, err)
223+
return err
224+
case <-c.Closed():
225+
return nil
226+
}
219227
}
220228
}
221229

0 commit comments

Comments
 (0)