Skip to content

Adding a custom struct to use for call end hooks #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ func (s *Service) DeregisterTransferSIPParticipantTopic(sipCallId string) {
}
}

func (s *Service) OnCallEnd(ctx context.Context, callInfo *livekit.SIPCallInfo, reason string) {
s.log.Infow("SIP call ended", "callID", callInfo.CallId, "reason", reason)
func (s *Service) OnCallEnd(ctx context.Context, callIdentifier *sip.CallIdentifier, callInfo *livekit.SIPCallInfo, reason string) {
s.log.Infow("Post SIP call end processing", "callID", callInfo.CallId, "reason", reason)
}
6 changes: 5 additions & 1 deletion pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,11 @@ func (c *inboundCall) close(error bool, status CallStatus, reason string) {

// Call the handler asynchronously to avoid blocking
if c.s.handler != nil {
go c.s.handler.OnCallEnd(context.Background(), c.state.callInfo, reason)
go c.s.handler.OnCallEnd(context.Background(), &CallIdentifier{
ProjectID: c.state.callInfo.ParticipantAttributes["projectID"],
CallID: c.state.callInfo.ParticipantAttributes["sip.callID"],
SipCallID: c.state.callInfo.CallId,
}, c.state.callInfo, reason)
}

c.cancel()
Expand Down
6 changes: 5 additions & 1 deletion pkg/sip/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ func (c *outboundCall) close(err error, status CallStatus, description string, r

// Call the handler asynchronously to avoid blocking
if c.c.handler != nil {
go c.c.handler.OnCallEnd(context.Background(), c.state.callInfo, description)
go c.c.handler.OnCallEnd(context.Background(), &CallIdentifier{
ProjectID: c.state.callInfo.ParticipantAttributes["projectID"],
CallID: c.state.callInfo.ParticipantAttributes["sip.callID"],
SipCallID: c.state.callInfo.CallId,
}, c.state.callInfo, description)
}
})
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/sip/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ type CallDispatch struct {
MediaEncryption livekit.SIPMediaEncryption
}

type CallIdentifier struct {
ProjectID string
CallID string
SipCallID string
}

type Handler interface {
GetAuthCredentials(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error)
DispatchCall(ctx context.Context, info *CallInfo) CallDispatch
Expand All @@ -107,7 +113,7 @@ type Handler interface {
RegisterTransferSIPParticipantTopic(sipCallId string) error
DeregisterTransferSIPParticipantTopic(sipCallId string)

OnCallEnd(ctx context.Context, callInfo *livekit.SIPCallInfo, reason string)
OnCallEnd(ctx context.Context, callIdentifier *CallIdentifier, callInfo *livekit.SIPCallInfo, reason string)
}

type Server struct {
Expand Down
20 changes: 14 additions & 6 deletions pkg/sip/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func expectNoResponse(t *testing.T, tx sip.ClientTransaction) {
type TestHandler struct {
GetAuthCredentialsFunc func(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error)
DispatchCallFunc func(ctx context.Context, info *CallInfo) CallDispatch
OnCallEndFunc func(ctx context.Context, callInfo *livekit.SIPCallInfo, reason string)
OnCallEndFunc func(ctx context.Context, callIdentifier *CallIdentifier, callInfo *livekit.SIPCallInfo, reason string)
}

func (h TestHandler) GetAuthCredentials(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error) {
Expand All @@ -81,9 +81,9 @@ func (h TestHandler) DeregisterTransferSIPParticipantTopic(sipCallId string) {
// no-op
}

func (h TestHandler) OnCallEnd(ctx context.Context, callInfo *livekit.SIPCallInfo, reason string) {
func (h TestHandler) OnCallEnd(ctx context.Context, callIdentifier *CallIdentifier, callInfo *livekit.SIPCallInfo, reason string) {
if h.OnCallEndFunc != nil {
h.OnCallEndFunc(ctx, callInfo, reason)
h.OnCallEndFunc(ctx, callIdentifier, callInfo, reason)
}
}

Expand Down Expand Up @@ -202,7 +202,7 @@ func TestService_OnCallEnd(t *testing.T) {
},
}
},
OnCallEndFunc: func(ctx context.Context, callInfo *livekit.SIPCallInfo, reason string) {
OnCallEndFunc: func(ctx context.Context, callIdentifier *CallIdentifier, callInfo *livekit.SIPCallInfo, reason string) {
receivedCallInfo = callInfo
receivedReason = reason
close(callEnded)
Expand All @@ -229,8 +229,15 @@ func TestService_OnCallEnd(t *testing.T) {
require.NoError(t, s.Start())

// Call OnCallEnd directly
h.OnCallEnd(context.Background(), &livekit.SIPCallInfo{
CallId: expectedCallID,
h.OnCallEnd(context.Background(), &CallIdentifier{
ProjectID: "test-project",
CallID: "test-call-id",
SipCallID: "test-sip-call-id",
}, &livekit.SIPCallInfo{
CallId: "test-call-id",
ParticipantAttributes: map[string]string{
"projectID": "test-project",
},
}, expectedReason)

// Wait for OnCallEnd to be called
Expand All @@ -243,6 +250,7 @@ func TestService_OnCallEnd(t *testing.T) {

// Verify the parameters passed to OnCallEnd
require.NotNil(t, receivedCallInfo, "CallInfo should not be nil")
require.Equal(t, "test-project", receivedCallInfo.ParticipantAttributes["projectID"], "CallInfo.ParticipantAttributes[projectID] should match")
require.Equal(t, expectedCallID, receivedCallInfo.CallId, "CallInfo.CallId should match")
require.Equal(t, expectedReason, receivedReason, "Reason should match")
}
Loading