use std::sync::Arc;
use std::time::Duration;
use adk_core::egress::ToolEgress;
use async_trait::async_trait;
use livekit_api::access_token::TokenVerifier;
use livekit_api::services::ServiceError;
use livekit_api::services::room::RoomClient;
use livekit_api::services::sip::{
CreateSIPDispatchRuleOptions, CreateSIPInboundTrunkOptions, CreateSIPOutboundTrunkOptions,
CreateSIPParticipantOptions, SIPClient, TransferSIPParticipantOptions,
};
use livekit_api::webhooks::WebhookReceiver;
use livekit_protocol as proto;
use super::{
CallAnswerer, CallCapabilities, CallControl, CallTarget, DialRequest, IncomingCall,
OutboundDialer,
};
use crate::config::RealtimeConfig;
use crate::error::{RealtimeError, Result};
const PROVIDER: &str = "livekit";
pub const ROOM_ATTRIBUTE: &str = "room";
pub const PARTICIPANT_IDENTITY_ATTRIBUTE: &str = "participant_identity";
pub const DTMF_ATTRIBUTE: &str = "dtmf";
const SIP_ATTRIBUTES: [&str; 5] =
["sip.callID", "sip.phoneNumber", "sip.trunkPhoneNumber", "sip.trunkID", "sip.ruleID"];
fn service_error(action: &str, error: ServiceError) -> RealtimeError {
match error {
ServiceError::Twirp(livekit_api::services::ServerError::Twirp(server)) => {
let sip_status = server.meta.get("sip_status_code").map(String::as_str);
let message = match sip_status {
Some(status) => format!("LiveKit {action}: {} (SIP {status})", server.msg),
None => format!("LiveKit {action}: {}", server.msg),
};
RealtimeError::ServerError { code: server.code, message }
}
ServiceError::Twirp(other) => {
RealtimeError::connection(format!("LiveKit {action} request failed: {other}"))
}
other => RealtimeError::config(format!("LiveKit {action}: {other}")),
}
}
struct Inner {
host: String,
sip: SIPClient,
rooms: RoomClient,
}
#[derive(Clone)]
pub struct LiveKitSip {
inner: Arc<Inner>,
ringing_timeout: Option<Duration>,
}
impl std::fmt::Debug for LiveKitSip {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LiveKitSip").field("host", &self.inner.host).finish_non_exhaustive()
}
}
impl LiveKitSip {
pub fn new(host: &str, api_key: &str, api_secret: &str) -> Self {
Self {
inner: Arc::new(Inner {
host: host.to_string(),
sip: SIPClient::with_api_key(host, api_key, api_secret),
rooms: RoomClient::with_api_key(host, api_key, api_secret),
}),
ringing_timeout: None,
}
}
pub fn with_ringing_timeout(mut self, timeout: Duration) -> Self {
self.ringing_timeout = Some(timeout);
self
}
pub fn sip(&self) -> &SIPClient {
&self.inner.sip
}
pub async fn create_inbound_trunk(
&self,
name: &str,
numbers: Vec<String>,
options: CreateSIPInboundTrunkOptions,
) -> Result<proto::SipInboundTrunkInfo> {
self.inner
.sip
.create_sip_inbound_trunk(name.to_string(), numbers, options)
.await
.map_err(|error| service_error("CreateSIPInboundTrunk", error))
}
pub async fn create_outbound_trunk(
&self,
name: &str,
address: &str,
numbers: Vec<String>,
options: CreateSIPOutboundTrunkOptions,
) -> Result<proto::SipOutboundTrunkInfo> {
self.inner
.sip
.create_sip_outbound_trunk(name.to_string(), address.to_string(), numbers, options)
.await
.map_err(|error| service_error("CreateSIPOutboundTrunk", error))
}
pub async fn create_dispatch_rule(
&self,
room_prefix: &str,
options: CreateSIPDispatchRuleOptions,
) -> Result<proto::SipDispatchRuleInfo> {
let rule = proto::sip_dispatch_rule::Rule::DispatchRuleIndividual(
proto::SipDispatchRuleIndividual {
room_prefix: room_prefix.to_string(),
..Default::default()
},
);
self.inner
.sip
.create_sip_dispatch_rule(rule, options)
.await
.map_err(|error| service_error("CreateSIPDispatchRule", error))
}
pub fn call(&self, room: &str, participant_identity: &str) -> LiveKitSipCall {
LiveKitSipCall {
livekit: self.clone(),
room: room.to_string(),
identity: participant_identity.to_string(),
call_id: participant_identity.to_string(),
}
}
pub fn dialer(&self, trunk_id: &str) -> LiveKitSipDialer {
LiveKitSipDialer { livekit: self.clone(), trunk_id: trunk_id.to_string(), room: None }
}
fn egress(&self) -> ToolEgress {
let host = self.inner.host.as_str();
let host = host
.strip_prefix("wss://")
.map(|rest| format!("https://{rest}"))
.or_else(|| host.strip_prefix("ws://").map(|rest| format!("http://{rest}")))
.unwrap_or_else(|| host.to_string());
ToolEgress::for_urls([host])
}
}
#[async_trait]
impl CallAnswerer for LiveKitSip {
async fn answer(
&self,
call: &IncomingCall,
_config: &RealtimeConfig,
) -> Result<Arc<dyn CallControl>> {
Ok(Arc::new(LiveKitSipCall::for_incoming(self, call)?))
}
async fn decline(&self, call: &IncomingCall, _status: u16) -> Result<()> {
LiveKitSipCall::for_incoming(self, call)?.hang_up().await
}
}
#[derive(Clone)]
pub struct LiveKitSipCall {
livekit: LiveKitSip,
room: String,
identity: String,
call_id: String,
}
impl std::fmt::Debug for LiveKitSipCall {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LiveKitSipCall")
.field("room", &self.room)
.field("participant_identity", &self.identity)
.field("call_id", &self.call_id)
.finish()
}
}
impl LiveKitSipCall {
fn for_incoming(livekit: &LiveKitSip, call: &IncomingCall) -> Result<Self> {
let attribute = |key: &str| {
call.attributes.get(key).cloned().ok_or_else(|| {
RealtimeError::config(format!(
"a LiveKit call needs the `{key}` attribute; build it from a verified webhook"
))
})
};
Ok(Self {
livekit: livekit.clone(),
room: attribute(ROOM_ATTRIBUTE)?,
identity: attribute(PARTICIPANT_IDENTITY_ATTRIBUTE)?,
call_id: call.call_id.clone(),
})
}
pub fn room(&self) -> &str {
&self.room
}
pub fn participant_identity(&self) -> &str {
&self.identity
}
}
#[async_trait]
impl CallControl for LiveKitSipCall {
fn call_id(&self) -> &str {
&self.call_id
}
fn provider(&self) -> &str {
PROVIDER
}
fn capabilities(&self) -> CallCapabilities {
CallCapabilities::new(true, false)
}
fn egress(&self) -> ToolEgress {
self.livekit.egress()
}
async fn transfer(&self, target: &CallTarget) -> Result<()> {
let options = TransferSIPParticipantOptions {
play_dialtone: Some(false),
ringing_timeout: self.livekit.ringing_timeout,
..Default::default()
};
self.livekit
.inner
.sip
.transfer_sip_participant(
self.room.clone(),
self.identity.clone(),
target.as_uri().to_string(),
options,
)
.await
.map_err(|error| service_error("TransferSIPParticipant", error))
}
async fn hang_up(&self) -> Result<()> {
self.livekit
.inner
.rooms
.remove_participant(&self.room, &self.identity)
.await
.map_err(|error| service_error("RemoveParticipant", error))
}
}
#[derive(Debug, Clone)]
pub struct LiveKitSipDialer {
livekit: LiveKitSip,
trunk_id: String,
room: Option<String>,
}
impl LiveKitSipDialer {
pub fn with_room(mut self, room: impl Into<String>) -> Self {
self.room = Some(room.into());
self
}
}
#[async_trait]
impl OutboundDialer for LiveKitSipDialer {
fn provider(&self) -> &str {
PROVIDER
}
fn egress(&self) -> ToolEgress {
self.livekit.egress()
}
async fn dial(&self, request: &DialRequest) -> Result<Arc<dyn CallControl>> {
let number = request.to.phone_number().ok_or_else(|| {
RealtimeError::config(format!(
"LiveKit dials phone numbers; '{}' is not a tel: target",
request.to
))
})?;
let room =
request.attributes.get(ROOM_ATTRIBUTE).or(self.room.as_ref()).cloned().ok_or_else(
|| {
RealtimeError::config(
"a LiveKit call needs a room: set the dialler's or the request's `room`",
)
},
)?;
let identity = request
.attributes
.get(PARTICIPANT_IDENTITY_ATTRIBUTE)
.cloned()
.unwrap_or_else(|| format!("sip_{number}"));
let dtmf = request.attributes.get(DTMF_ATTRIBUTE).cloned();
if let Some(tones) = &dtmf
&& !tones.chars().all(|c| c.is_ascii_digit() || matches!(c, '*' | '#' | 'w'))
{
return Err(RealtimeError::config("dial DTMF may hold only 0-9, *, # and w"));
}
let options = CreateSIPParticipantOptions {
participant_identity: identity.clone(),
dtmf,
wait_until_answered: Some(true),
ringing_timeout: self.livekit.ringing_timeout,
..Default::default()
};
let info = self
.livekit
.inner
.sip
.create_sip_participant(
self.trunk_id.clone(),
number.to_string(),
room.clone(),
options,
None,
)
.await
.map_err(|error| service_error("CreateSIPParticipant", error))?;
tracing::info!(
call.provider = PROVIDER,
call.id = %info.sip_call_id,
call.room = %info.room_name,
"outbound SIP call answered"
);
Ok(Arc::new(LiveKitSipCall {
livekit: self.livekit.clone(),
room: if info.room_name.is_empty() { room } else { info.room_name },
identity: if info.participant_identity.is_empty() {
identity
} else {
info.participant_identity
},
call_id: info.sip_call_id,
}))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum LiveKitWebhookEvent {
IncomingCall(IncomingCall),
Other {
event: String,
},
}
#[derive(Debug, Clone)]
pub struct LiveKitWebhookVerifier {
receiver: WebhookReceiver,
}
impl LiveKitWebhookVerifier {
pub fn new(api_key: &str, api_secret: &str) -> Self {
Self { receiver: WebhookReceiver::new(TokenVerifier::with_api_key(api_key, api_secret)) }
}
pub fn verify(&self, body: &str, authorization: &str) -> Result<LiveKitWebhookEvent> {
let token = authorization.strip_prefix("Bearer ").unwrap_or(authorization);
let event = self.receiver.receive(body, token).map_err(|error| {
RealtimeError::AuthError(format!("LiveKit webhook rejected: {error}"))
})?;
let inbound_sip = event.event == "participant_joined"
&& event.participant.as_ref().is_some_and(|participant| {
participant.kind == proto::participant_info::Kind::Sip as i32
&& participant.attributes.get("sip.ruleID").is_some_and(|rule| !rule.is_empty())
});
let (true, Some(room), Some(participant)) = (inbound_sip, event.room, event.participant)
else {
return Ok(LiveKitWebhookEvent::Other { event: event.event });
};
let call_id = participant
.attributes
.get("sip.callID")
.cloned()
.unwrap_or_else(|| participant.identity.clone());
let mut call = IncomingCall::new(PROVIDER, call_id)
.with_attribute(ROOM_ATTRIBUTE, room.name)
.with_attribute(PARTICIPANT_IDENTITY_ATTRIBUTE, participant.identity.clone());
for key in SIP_ATTRIBUTES {
if let Some(value) = participant.attributes.get(key) {
call = call.with_attribute(key, value.clone());
}
}
tracing::info!(call.provider = PROVIDER, call.id = %call.call_id, "incoming SIP call");
Ok(LiveKitWebhookEvent::IncomingCall(call))
}
}