Skip to content

Change the call flow. Defer track subscription. - #165

Merged
dennwc merged 1 commit into
mainfrom
call-flow
Sep 5, 2024
Merged

dennwc merged 1 commit into
mainfrom
call-flow

Conversation

@dennwc

@dennwc dennwc commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

Refactor SIP signaling an change the call flow for both inbound and outbound.

For inbound with PIN (almost the same as before):

  • Keep sending Ringing status for the whole time until the call is accepted.
  • Signal to SIP peer as soon as we can and run the audio for the pin prompts.
  • Given the PIN, join selected room on LK server.
  • Immediate publish SIP track and subscribe to the rest.

For inbound without PIN:

  • Keep sending Ringing status for the whole time until the call is accepted.
  • First connect to the LK room according to dispatch rules.
  • Subscribe and wait for at least one audio track. This is important! SIP now keeps ringing until there's one other participant with enabled audio track.
  • Accept the call on SIP side. Wait for the first media packet.
  • Publish SIP track to LiveKit.

For outbound:

  • Flow as before, but delay track subscription until media pipeline is initialized.

Should resolve #102, #136, #114

@dennwc dennwc self-assigned this Sep 2, 2024
@dennwc
dennwc requested a review from a team as a code owner September 2, 2024 13:14

@biglittlebigben biglittlebigben left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Couple of questions inline.

Comment thread pkg/sip/inbound.go Resolved
Comment thread pkg/sip/inbound.go
}

func (c *sipInbound) StartRinging() {
c.attachTag()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will StartRinging and StopRinging always be called from the same goroutine, since c.ringing is not thread safe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's not explicitly mentioned, they should run under the same call mutex. Most signaling is done by a single thread, except the calls to Close, but they would still acquire the same mutex.

Having said that, I'll check it again and will probably add mutex specifically for the signaling part of the connection. We will likely reuse it for our test client later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, everything is protected now. I should actually move it to a separate package later.

Comment thread pkg/sip/outbound.go
tag, err := getToTag(inviteResp)
if err != nil {
return err
c.invite, c.inviteOk = req, resp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are c.invite and c.inviteOk always be accessed from the same goroutine as the one Invite is called on?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be roughly the same as in the other comment thread. So it should hold the call mutex while doing so. But will double-check.

@davidzhao

Copy link
Copy Markdown
Member

awesome improvement!

For the flow without PIN, I'm wondering if we can move publishing earlier:

  • Publish SIP track to LiveKit.
  • Subscribe and wait for at least one audio track. This is important! SIP now keeps ringing until there's one other participant with enabled audio track.
  • Accept the call on SIP side. Wait for the first media packet.

If we accepted the SIP call, but the track isn't published yet, it means if the caller speaks immediately, their first words won't be heard by the other side.

@dennwc

dennwc commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

@davidzhao We can do that, but I'm a bit worried that there will be no explicit signal when finally accepting the call in that case.

In the flow that you propose, as soon as SIP subscribes to at least one track, it will confirm the call via SIP signaling. There will be some delay until we actually receive audio packets. SIP won't emit silence automatically in this case. But maybe it's acceptable?

@dennwc
dennwc force-pushed the call-flow branch 4 times, most recently from ccb7871 to 55e88a4 Compare September 5, 2024 12:42
@dennwc
dennwc merged commit 63e5c2e into main Sep 5, 2024
@dennwc
dennwc deleted the call-flow branch September 5, 2024 14:17
@MFaiqKhan

Copy link
Copy Markdown

Hi @dennwc currently participants.attribute.get(sip.callstatus) shows ringing and then after ringing even if the user hasn't picked up the call and shows active in the call status , user hasn't picked up the call and call has been turned off but the logs shows that user has picked up and active status on sip participant.

@dennwc

dennwc commented Jan 22, 2025

Copy link
Copy Markdown
Contributor Author

@MFaiqKhan I assume you mean outbound calls in this case. It could be that the server you are dialing is proxying the call to another server and indicates that the call is dialed even before the actual user picks up. From LiveKit's perspective the call is dialed and receives audio from the remote server. We'll have to add dial tone detection to know if the dial tone is still going in the audio stream.

@faiqkhan-aiconnect

faiqkhan-aiconnect commented Jan 22, 2025 •

Copy link
Copy Markdown

@dennwc
these are my logs currently that is from voice cloud livekit :

here the logs shows that agent left because the phone after ringing turned off but exactly the ringing stops the agent left and the particpant is autoamtically joined when they even havent picked up the call .

and are you saying that twilio (sip provider), is proxying the call to the different server ???
Because the server that showed in the terminal logs were of singapore but the user server livekit shows of UAE .
what could be the issue here ?

phone_user_+61 | (none) | 1/22/2025, 7:57 AM | UAE |   | 0 | 23s


Events

—	ROOM_ENDED	{"event":"ROOM_ENDED","time_stamp":"2025-01-22T02:58:23.754058Z"}	
1/22/2025, 7:58 AM

phone_user_+61	PARTICIPANT_LEFT	{"participant_id":"PA_MdKpEkJDdpP9","participant_identity":"phone_user_+61","event":"PARTICIPANT_LEFT","time_stamp":"2025-01-22T02:57:43.185201Z"}	
1/22/2025, 7:57 AM

phone_user_+	TRACK_UNPUBLISHED	{"track_source":"MICROPHONE","track_id":"TR_AMadZe7hhsnZke","participant_id":"PA_MdKpEkJDdpP9","event":"TRACK_UNPUBLISHED","time_stamp":"2025-01-22T02:57:43.185138Z"}	
1/22/2025, 7:57 AM

phone_user_+61	TRACK_PUBLISHED	{"track_source":"MICROPHONE","track_id":"TR_AMadZe7hhsnZke","participant_id":"PA_MdKpEkJDdpP9","participant_identity":"phone_user_+61","event":"TRACK_PUBLISHED","time_stamp":"2025-01-22T02:57:22.321434Z"}	
1/22/2025, 7:57 AM

agent-AJ_K5vLsMqKEhmZ	PARTICIPANT_LEFT	{"participant_id":"PA_FEd9M5EJNjiY","participant_identity":"agent-AJ_K5vLsMqKEhmZ","event":"PARTICIPANT_LEFT","time_stamp":"2025-01-22T02:57:21.482699Z"}	
1/22/2025, 7:57 AM

phone_user_+61	PARTICIPANT_ACTIVE	{"participant_id":"PA_MdKpEkJDdpP9","participant_identity":"phone_user_+61","event":"PARTICIPANT_ACTIVE","time_stamp":"2025-01-22T02:57:20.707548Z"}	
1/22/2025, 7:57 AM

phone_user_+61	PARTICIPANT_JOINED	{"participant_id":"PA_MdKpEkJDdpP9","participant_identity":"phone_user_+61","event":"PARTICIPANT_JOINED","time_stamp":"2025-01-22T02:57:20.549481Z"}	
1/22/2025, 7:57 AM

agent-AJ_K5vLsMqKEhmZ	PARTICIPANT_ACTIVE	{"participant_id":"PA_FEd9M5EJNjiY","participant_identity":"agent-AJ_K5vLsMqKEhmZ","event":"PARTICIPANT_ACTIVE","time_stamp":"2025-01-22T02:57:19.252151Z"}	
1/22/2025, 7:57 AM

agent-AJ_K5vLsMqKEhmZ	PARTICIPANT_JOINED	{"participant_id":"PA_FEd9M5EJNjiY","participant_identity":"agent-AJ_K5vLsMqKEhmZ","event":"PARTICIPANT_JOINED","time_stamp":"2025-01-22T02:57:18.433368Z"}	
1/22/2025, 7:57 AM

—	ROOM_CREATED	{"event":"ROOM_CREATED","time_stamp":"2025-01-22T02:57:16Z"}	
1/22/2025, 7:57 AM

@dennwc

dennwc commented Jan 22, 2025

Copy link
Copy Markdown
Contributor Author

@faiqkhan-aiconnect could you please file a new issue instead with all these details? Please also specify either the callID or the sequence of events as you observed it.

@serhiynovos

Copy link
Copy Markdown

It would be nice in dispatch rules to have it as strategy to accept immediately or wait first track publication or even have ability to answer the call programmatically .

In my case I have to broadcast incoming call to other devices which means I have only one way communication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add early SIP dialog state

6 participants