How can I register an event to detect when a SIP c...
# ask-ai
d
How can I register an event to detect when a SIP call has ended?
t
Thanks for reaching out, Martin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To detect when a SIP call has ended, you can use the
Ended
event on the Track object. When a SIP participant's call ends, the track associated with that participant will trigger the
Ended
event. You can subscribe to this event using:
Copy code
track.Ended += OnTrackEnded;
Here,
OnTrackEnded
is your event handler method, which you can define as:
Copy code
void OnTrackEnded(Track track)
{
    // Your code to handle the ended call
}
Additionally, you can monitor the SIP participant's attributes, specifically the
sip.callStatus
attribute, which will change to
hangup
when a call has been ended by a participant. Sources: SIP participant | LiveKit Docs | Class Track | LiveKit Unity SDK | Delegate ITrack.EndedDelegate | LiveKit Unity SDK
d
How can I monitor sip.callStatus from a Python agent?
How can I monitor sip.callStatus from a Python agent?
t
To monitor
sip.callStatus
from a Python agent, you can use the following approach:
Copy code
start_time = perf_counter()
while perf_counter() - start_time < 30:
    call_status = participant.attributes.get("sip.callStatus")
    if call_status == "active":
        <http://logger.info|logger.info>("User has picked up")
        return
    elif call_status == "automation":
        # If DTMF is used in the `sip_call_to` number, typically used to dial
        # an extension or enter a PIN.
        # During DTMF dialing, the participant will be in the "automation" state
        pass
    elif call_status == "hangup":
        # User hung up, we'll exit the job
        <http://logger.info|logger.info>("User hung up, exiting job")
        break
    await asyncio.sleep(0.1)
<http://logger.info|logger.info>("Session timed out, exiting job")
ctx.shutdown()
The
sip.callStatus
attribute can have the following values: • `active`: Indicates the user has answered the call • `automation`: Indicates DTMF dialing is in progress (for entering extensions or PINs) • `hangup`: Indicates the user has hung up This loop checks the status every 0.1 seconds for up to 30 seconds before timing out. Sources: Making calls using SIP | LiveKit Docs
To monitor
sip.callStatus
from a Python agent, you can use the following approach:
Copy code
start_time = perf_counter()
while perf_counter() - start_time < 30:
    call_status = participant.attributes.get("sip.callStatus")
    if call_status == "active":
        <http://logger.info|logger.info>("User has picked up")
        return
    elif call_status == "automation":
        # If DTMF is used in the `sip_call_to` number, typically used to dial
        # an extension or enter a PIN.
        # During DTMF dialing, the participant will be in the "automation" state
        pass
    elif call_status == "hangup":
        # User hung up, we'll exit the job
        <http://logger.info|logger.info>("User hung up, exiting job")
        break
    await asyncio.sleep(0.1)
<http://logger.info|logger.info>("Session timed out, exiting job")
ctx.shutdown()
The
sip.callStatus
attribute can have the following values: • `active`: Indicates the user has answered the call • `automation`: Indicates DTMF dialing is in progress (for entering extensions or PINs) • `hangup`: Indicates the user has hung up This loop checks the status every 0.1 seconds for up to 30 seconds before timing out.