1use crate::media::cache;
2use crate::synthesis::{SynthesisEvent, SynthesisOption};
3use crate::{
4 app::AppState,
5 call::{
6 ActiveCall, ActiveCallType, Command,
7 active_call::{ActiveCallGuard, CallParams, CallSpec},
8 },
9 handler::playbook,
10 playbook::{Playbook, PlaybookRunner},
11};
12use crate::{event::SessionEvent, media::track::TrackConfig};
13use axum::{
14 Json, Router,
15 extract::{Path, Query, State, WebSocketUpgrade, ws::Message},
16 response::sse::{Event, KeepAlive, Sse},
17 response::{IntoResponse, Response},
18 routing::{get, post},
19};
20use bytes::Bytes;
21use chrono::Utc;
22use futures::{SinkExt, StreamExt};
23use rustrtc::IceServer;
24use serde_json::json;
25use std::collections::HashMap;
26use std::{path::PathBuf, sync::Arc, time::Duration};
27use tokio::{join, select};
28use tokio_util::sync::CancellationToken;
29use tracing::{debug, info, trace, warn};
30use uuid::Uuid;
31
32fn filter_headers(
33 extras: &mut std::collections::HashMap<String, serde_json::Value>,
34 allowed_headers: &[String],
35) {
36 extras.retain(|k, _| allowed_headers.iter().any(|h| h.eq_ignore_ascii_case(k)));
37}
38
39pub fn call_router() -> Router<AppState> {
40 let r = Router::new()
41 .route("/call", get(ws_handler))
42 .route("/call/webrtc", get(webrtc_handler))
43 .route("/call/sip", get(sip_handler))
44 .route("/list", get(list_active_calls))
45 .route("/kill/{id}", get(kill_active_call))
46 .route("/events/{id}", get(stream_events))
47 .route("/command/{id}", post(send_command))
48 .route("/precache", post(precache));
49 r
50}
51
52pub fn iceservers_router() -> Router<AppState> {
53 let r = Router::new();
54 r.route("/iceservers", get(get_iceservers))
55}
56
57pub fn playbook_router() -> Router<AppState> {
58 Router::new()
59 .route("/api/playbooks", get(playbook::list_playbooks))
60 .route(
61 "/api/playbooks/{name}",
62 get(playbook::get_playbook).post(playbook::save_playbook),
63 )
64 .route(
65 "/api/playbook/run",
66 axum::routing::post(playbook::run_playbook),
67 )
68 .route("/api/records", get(playbook::list_records))
69}
70
71pub async fn ws_handler(
72 ws: WebSocketUpgrade,
73 State(state): State<AppState>,
74 Query(params): Query<CallParams>,
75) -> Response {
76 call_handler(ActiveCallType::WebSocket, ws, state, params).await
77}
78
79pub async fn sip_handler(
80 ws: WebSocketUpgrade,
81 State(state): State<AppState>,
82 Query(params): Query<CallParams>,
83) -> Response {
84 call_handler(ActiveCallType::Sip, ws, state, params).await
85}
86
87pub async fn webrtc_handler(
88 ws: WebSocketUpgrade,
89 State(state): State<AppState>,
90 Query(params): Query<CallParams>,
91) -> Response {
92 call_handler(ActiveCallType::Webrtc, ws, state, params).await
93}
94
95pub async fn call_handler_core(
102 call_type: ActiveCallType,
103 session_id: String,
104 app_state: AppState,
105 cancel_token: CancellationToken,
106 audio_receiver: tokio::sync::mpsc::UnboundedReceiver<Bytes>,
107 server_side_track: Option<String>,
108 dump_events: bool,
109 ping_interval: u64,
110 mut command_receiver: tokio::sync::mpsc::UnboundedReceiver<Command>,
111 event_sender_to_client: tokio::sync::mpsc::UnboundedSender<crate::event::SessionEvent>,
112 extras: Option<HashMap<String, serde_json::Value>>,
113 playbook_name: Option<String>,
114) -> Option<HashMap<String, serde_json::Value>> {
115 let Some(_admission) =
122 crate::call::active_call::AdmissionTicket::acquire(&app_state.config(), &session_id).await
123 else {
124 warn!(session_id, "admission timeout; rejecting call");
125 cancel_token.cancel();
126 return None;
127 };
128 let _cancel_guard = cancel_token.clone().drop_guard();
129 let track_config = TrackConfig::default();
130
131 let active_call = Arc::new(ActiveCall::new(CallSpec {
132 call_type: call_type.clone(),
133 cancel_token: cancel_token.clone(),
134 session_id: session_id.clone(),
135 invitation: app_state.invitation.clone(),
136 app_state: app_state.clone(),
137 track_config,
138 audio_receiver: Some(audio_receiver),
139 dump_events,
140 server_side_track_id: server_side_track.clone(),
141 extras,
142 }));
143
144 {
147 let name_or_content = playbook_name.or_else(|| {
148 app_state
149 .pending_playbooks
150 .try_lock()
151 .ok()
152 .and_then(|mut pending| pending.remove(&session_id).map(|(val, _)| val))
153 });
154 if let Some(name_or_content) = name_or_content {
155 let playbook_result = if name_or_content.trim().starts_with("---") {
156 Playbook::parse(&name_or_content)
157 } else {
158 let path = if name_or_content.starts_with("config/playbook/") {
160 PathBuf::from(&name_or_content)
161 } else {
162 PathBuf::from("config/playbook").join(&name_or_content)
163 };
164 Playbook::load(path).await
165 };
166
167 match playbook_result {
168 Ok(mut playbook) => {
169 if call_type == ActiveCallType::Sip {
171 if let Some(sip_config) = &playbook.config.sip {
172 if let Some(allowed_headers) = &sip_config.extract_headers {
173 let extras = active_call.extras.rcu(|e| {
174 let mut e = HashMap::clone(e);
175 filter_headers(&mut e, allowed_headers);
176 e
177 });
178 let header_keys: Vec<String> = extras
180 .keys()
181 .filter(|k| !k.starts_with('_'))
182 .cloned()
183 .collect();
184 active_call.set_extra(
185 "_sip_header_keys",
186 serde_json::to_value(&header_keys).unwrap_or_default(),
187 );
188 if let Ok(result) = playbook.render(&active_call.extras.load_full())
189 {
190 playbook = result;
191 }
192 }
193 }
194 }
195
196 match PlaybookRunner::new(playbook, active_call.clone()) {
197 Ok(runner) => {
198 crate::spawn(async move {
199 runner.run().await;
200 });
201 let display_name = if name_or_content.trim().starts_with("---") {
202 "custom content"
203 } else {
204 &name_or_content
205 };
206 info!(session_id, "Playbook runner started for {}", display_name);
207 }
208 Err(e) => {
209 let display_name = if name_or_content.trim().starts_with("---") {
210 "custom content"
211 } else {
212 &name_or_content
213 };
214 warn!(
215 session_id,
216 "Failed to create runner {}: {}", display_name, e
217 )
218 }
219 }
220 }
221 Err(e) => {
222 let display_name = if name_or_content.trim().starts_with("---") {
223 "custom content"
224 } else {
225 &name_or_content
226 };
227 warn!(
228 session_id,
229 "Failed to load playbook {}: {}", display_name, e
230 );
231 let event = SessionEvent::Error {
232 timestamp: crate::media::get_timestamp(),
233 track_id: session_id.clone(),
234 sender: "playbook".to_string(),
235 error: format!("{}", e),
236 code: None,
237 };
238 event_sender_to_client.send(event).ok();
239 return None;
240 }
241 }
242 }
243 }
244
245 let recv_commands_loop = async {
246 while let Some(command) = command_receiver.recv().await {
247 if let Err(_) = active_call.enqueue_command(command).await {
248 break;
249 }
250 }
251 };
252
253 let mut event_receiver = active_call.event_sender.subscribe();
254 let send_events_loop = async {
255 loop {
256 match event_receiver.recv().await {
257 Ok(event) => {
258 if let Err(_) = event_sender_to_client.send(event) {
259 break;
260 }
261 }
262 Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
263 Err(_) => break,
264 }
265 }
266 };
267
268 let send_ping_loop = async {
269 if ping_interval == 0 {
270 active_call.cancel_token.cancelled().await;
271 return;
272 }
273 let mut ticker = tokio::time::interval(Duration::from_secs(ping_interval));
274 loop {
275 ticker.tick().await;
276 let payload = Utc::now().to_rfc3339();
277 let event = SessionEvent::Ping {
278 timestamp: crate::media::get_timestamp(),
279 payload: Some(payload),
280 };
281 if let Err(_) = active_call.event_sender.send(event) {
282 break;
283 }
284 }
285 };
286
287 let guard = ActiveCallGuard::new(active_call.clone());
288 info!(
289 session_id,
290 active_calls = guard.active_calls,
291 ?call_type,
292 "new call started"
293 );
294 let receiver = active_call.new_receiver();
295
296 let (r, _) = join! {
297 active_call.clone().serve(receiver),
298 async {
299 select!{
300 _ = send_ping_loop => {},
301 _ = cancel_token.cancelled() => {},
302 _ = send_events_loop => { },
303 _ = recv_commands_loop => {
304 info!(session_id, "Command receiver closed");
305 },
306 }
307 cancel_token.cancel();
308 }
309 };
310 while let Ok(event) = event_receiver.try_recv() {
312 if let Err(_) = event_sender_to_client.send(event) {
313 break;
314 }
315 }
316 match r {
317 Ok(_) => info!(session_id, "call ended successfully"),
318 Err(e) => warn!(session_id, "call ended with error: {}", e),
319 }
320
321 let final_extras = Some(active_call.extras.load_full().as_ref().clone());
323
324 drop(event_sender_to_client);
327 active_call.cleanup().await.ok();
328 debug!(session_id, "Call handler core completed");
329
330 final_extras
331}
332
333pub async fn call_handler(
334 call_type: ActiveCallType,
335 ws: WebSocketUpgrade,
336 app_state: AppState,
337 params: CallParams,
338) -> Response {
339 let has_explicit_id = params.id.is_some();
340 let session_id = params
341 .id
342 .clone()
343 .unwrap_or_else(|| format!("s.{}", Uuid::new_v4().to_string()));
344 let server_side_track = params.server_side_track.clone();
345 let dump_events = params.dump_events.unwrap_or(true);
346 let ping_interval = params.ping_interval.unwrap_or(20);
347
348 if has_explicit_id {
358 let found_locally = {
359 let active_calls = app_state.active_calls.lock().unwrap();
360 active_calls.contains_key(&session_id)
361 } || app_state
362 .invitation
363 .find_dialog_id_by_session_id(&session_id)
364 .is_some();
365 if !found_locally {
366 if params.forward.is_none() {
367 if let Some(peer_ws) =
368 crate::handler::peer::try_forward(&app_state, &session_id, ¶ms).await
369 {
370 info!(session_id, "forwarding websocket to peer hosting the call");
371 return ws.on_upgrade(move |socket| async move {
372 crate::handler::peer::tunnel(socket, peer_ws).await;
373 });
374 }
375 }
376 if params.forward == Some(true) {
377 warn!(
378 session_id,
379 "call not found on this node, rejecting forwarded request"
380 );
381 return (
382 axum::http::StatusCode::NOT_FOUND,
383 "call not found on this node",
384 )
385 .into_response();
386 }
387 }
388 }
389
390 let resp = ws.on_upgrade(move |socket| async move {
391 let (mut ws_sender, mut ws_receiver) = socket.split();
392 let (audio_sender, audio_receiver) = tokio::sync::mpsc::unbounded_channel::<Bytes>();
393 let (command_sender, command_receiver) = tokio::sync::mpsc::unbounded_channel::<Command>();
394 let (event_sender_to_client, mut event_receiver_from_core) =
395 tokio::sync::mpsc::unbounded_channel::<crate::event::SessionEvent>();
396 let cancel_token = CancellationToken::new();
397
398 let session_id_clone = session_id.clone();
400 let app_state_clone = app_state.clone();
401 let cancel_token_clone = cancel_token.clone();
402 crate::spawn(async move {
403 call_handler_core(
404 call_type,
405 session_id_clone,
406 app_state_clone,
407 cancel_token_clone,
408 audio_receiver,
409 server_side_track,
410 dump_events,
411 ping_interval.into(),
412 command_receiver,
413 event_sender_to_client,
414 None, None, )
417 .await;
418 });
419
420 let recv_from_ws_loop = async {
422 while let Some(Ok(message)) = ws_receiver.next().await {
423 match message {
424 Message::Text(text) => {
425 let command = match serde_json::from_str::<Command>(&text) {
426 Ok(cmd) => cmd,
427 Err(e) => {
428 warn!(session_id, %text, "Failed to parse command {}", e);
429 continue;
430 }
431 };
432 if let Err(_) = command_sender.send(command) {
433 break;
434 }
435 }
436 Message::Binary(bin) => {
437 audio_sender.send(bin.into()).ok();
438 }
439 Message::Close(_) => {
440 info!(session_id, "WebSocket closed by client");
441 break;
442 }
443 _ => {}
444 }
445 }
446 };
447
448 let send_to_ws_loop = async {
449 while let Some(event) = event_receiver_from_core.recv().await {
450 trace!(session_id, %event, "Sending WS message");
451 let message = match event.into_ws_message() {
452 Ok(msg) => msg,
453 Err(e) => {
454 warn!(session_id, error=%e, "Failed to serialize event to WS message");
455 continue;
456 }
457 };
458 if let Err(_) = ws_sender.send(message).await {
459 info!(session_id, "WebSocket send failed, closing");
460 break;
461 }
462 }
463 };
464
465 select! {
466 _ = recv_from_ws_loop => {
467 info!(session_id, "WebSocket receive loop ended");
468 },
469 _ = send_to_ws_loop => {
470 info!(session_id, "WebSocket send loop ended");
471 },
472 }
473
474 while let Ok(event) = event_receiver_from_core.try_recv() {
476 if let Ok(message) = event.into_ws_message() {
477 if ws_sender.send(message).await.is_err() {
478 break;
479 }
480 }
481 }
482
483 cancel_token.cancel();
484 ws_sender.flush().await.ok();
485 ws_sender.close().await.ok();
486 debug!(session_id, "WebSocket connection closed");
487 });
488 resp
489}
490
491pub(crate) async fn get_iceservers(State(state): State<AppState>) -> Response {
492 if let Some(ice_servers) = state.config().ice_servers.as_ref() {
493 return Json(ice_servers).into_response();
494 }
495 Json(vec![IceServer {
496 urls: vec!["stun:stun.l.google.com:19302".to_string()],
497 ..Default::default()
498 }])
499 .into_response()
500}
501
502pub(crate) async fn list_active_calls(State(state): State<AppState>) -> Response {
503 let calls = state
506 .active_calls
507 .lock()
508 .unwrap()
509 .iter()
510 .map(|(_, c)| c.clone())
511 .collect::<Vec<_>>();
512 let list = calls
513 .iter()
514 .map(|c| {
515 let progress = c.progress.load_full();
516 json!({
517 "id": c.session_id,
518 "callType": c.call_type,
519 "cs.option": progress.option,
520 "ringTime": progress.ring_time,
521 "startTime": progress.answer_time,
522 })
523 })
524 .collect::<Vec<_>>();
525 Json(serde_json::json!({ "active_calls": list })).into_response()
526}
527
528pub(crate) async fn kill_active_call(
529 Path(id): Path<String>,
530 State(state): State<AppState>,
531) -> Response {
532 let active_calls = state.active_calls.lock().unwrap();
533 if let Some(call) = active_calls.get(&id) {
534 call.cancel_token.cancel();
535 Json(serde_json::json!({ "status": "killed", "id": id })).into_response()
536 } else {
537 (
538 axum::http::StatusCode::NOT_FOUND,
539 Json(serde_json::json!({ "status": "not_found", "id": id })),
540 )
541 .into_response()
542 }
543}
544
545pub(crate) async fn stream_events(
546 Path(id): Path<String>,
547 State(state): State<AppState>,
548) -> Response {
549 let mut rx_events;
550 let mut rx_commands;
551 {
552 let active_calls = state.active_calls.lock().unwrap();
553 if let Some(call) = active_calls.get(&id) {
554 rx_events = call.event_sender.subscribe();
555 rx_commands = call.cmd_sender.subscribe();
556 } else {
557 return (axum::http::StatusCode::NOT_FOUND, "track not active").into_response();
558 }
559 }
560
561 let stream = async_stream::stream! {
562 loop {
563 let result = tokio::select! {
564 r = rx_events.recv() => r.map(|e| serde_json::to_string(&e).map(|json| Event::default().event("event").data(json))),
565 r = rx_commands.recv() => r.map(|c| serde_json::to_string(&c).map(|json| Event::default().event("command").data(json))),
566 };
567 match result {
568 Ok(Ok(sse_event)) => yield Ok::<Event, serde_json::Error>(sse_event),
569 Ok(Err(e)) => yield Err(e.into()),
570 Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
571 Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
572 }
573 }
574 };
575
576 let mut response = Sse::new(stream)
577 .keep_alive(KeepAlive::default())
578 .into_response();
579 response.headers_mut().insert(
580 axum::http::header::CONTENT_TYPE,
581 "text/event-stream;charset=utf-8".parse().unwrap(),
582 );
583 response
584}
585
586pub(crate) async fn send_command(
587 Path(id): Path<String>,
588 State(state): State<AppState>,
589 Json(command): Json<Command>,
590) -> Response {
591 let active_calls = state.active_calls.lock().unwrap();
592 if let Some(call) = active_calls.get(&id) {
593 if let Ok(_) = call.cmd_sender.send(command) {
594 return Json(serde_json::json!({ "status": "sent", "id": id })).into_response();
595 }
596 }
597
598 (
599 axum::http::StatusCode::NOT_FOUND,
600 Json(serde_json::json!({ "status": "not_found", "id": id })),
601 )
602 .into_response()
603}
604
605pub(crate) async fn precache(
616 State(state): State<AppState>,
617 Json(command): Json<Command>,
618) -> Response {
619 let result = match command {
620 Command::Tts {
621 text,
622 speaker,
623 option,
624 cache_key,
625 ..
626 } => precache_tts(&state, text, speaker, option, cache_key).await,
627 _ => {
628 return (
629 axum::http::StatusCode::BAD_REQUEST,
630 Json(json!({ "status": "error", "error": "precache only accepts tts commands" })),
631 )
632 .into_response();
633 }
634 };
635
636 match result {
637 Ok((cache_key, bytes, existed)) => Json(json!({
638 "status": if existed { "exists" } else { "cached" },
639 "cacheKey": cache_key,
640 "bytes": bytes,
641 }))
642 .into_response(),
643 Err(e) => (
644 axum::http::StatusCode::INTERNAL_SERVER_ERROR,
645 Json(json!({ "status": "error", "error": e.to_string() })),
646 )
647 .into_response(),
648 }
649}
650
651async fn precache_tts(
654 state: &AppState,
655 text: String,
656 speaker: Option<String>,
657 option: Option<SynthesisOption>,
658 cache_key: Option<String>,
659) -> anyhow::Result<(String, usize, bool)> {
660 let mut opt = option.ok_or_else(|| anyhow::anyhow!("tts precache requires an option"))?;
661 opt.speaker = speaker.or(opt.speaker);
663 opt.check_default();
664
665 let mut client = state.stream_engine.create_tts_client(false, &opt).await?;
666 let sample_rate = opt.samplerate.unwrap_or(16000) as u32;
668 let cache_key = cache_key.unwrap_or_else(|| {
669 cache::generate_cache_key(
670 &format!("tts:{}{}", client.provider(), text),
671 sample_rate,
672 opt.speaker.as_ref(),
673 opt.speed,
674 )
675 });
676 if cache::is_cached(&cache_key).await.unwrap_or(false) {
677 return Ok((cache_key, 0, true));
678 }
679
680 let mut stream = client.start().await?;
681 client.synthesize(&text, Some(0), Some(opt.clone())).await?;
682 client.stop().await?;
683
684 let mut chunks: Vec<Bytes> = Vec::new();
685 let mut first_chunk = true;
686 while let Some((_seq, res)) = stream.next().await {
687 match res? {
688 SynthesisEvent::AudioChunk(mut chunk) => {
689 if first_chunk {
691 if chunk.len() > 44 && chunk[..4] == [0x52, 0x49, 0x46, 0x46] {
692 let _ = chunk.split_to(44);
693 }
694 first_chunk = false;
695 }
696 chunks.push(chunk);
697 }
698 SynthesisEvent::Finished => break,
699 _ => {}
700 }
701 }
702 if chunks.is_empty() {
703 return Err(anyhow::anyhow!("tts produced no audio"));
704 }
705 let bytes: usize = chunks.iter().map(|c| c.len()).sum();
706 cache::store_in_cache_vectored(&cache_key, &chunks).await?;
707 info!(cache_key = %cache_key, bytes, "precache: stored tts audio");
708 Ok((cache_key, bytes, false))
709}
710
711trait IntoWsMessage {
712 fn into_ws_message(self) -> Result<Message, serde_json::Error>;
713}
714
715impl IntoWsMessage for crate::event::SessionEvent {
716 fn into_ws_message(self) -> Result<Message, serde_json::Error> {
717 match self {
718 SessionEvent::Binary { data, .. } => Ok(Message::Binary(data.into())),
719 SessionEvent::Ping { timestamp, payload } => {
720 let payload = payload.unwrap_or_else(|| timestamp.to_string());
721 Ok(Message::Ping(payload.into()))
722 }
723 event => serde_json::to_string(&event).map(|payload| Message::Text(payload.into())),
724 }
725 }
726}
727
728#[cfg(test)]
729mod tests {
730 use super::*;
731 use serde_json::json;
732 use std::collections::HashMap;
733
734 #[test]
735 fn test_filter_headers() {
736 let mut extras = HashMap::new();
737 extras.insert("X-Tenant-ID".to_string(), json!("123"));
738 extras.insert("X-User-ID".to_string(), json!("456"));
739 extras.insert("Custom-Header".to_string(), json!("abc"));
740 extras.insert("Irrelevant-Header".to_string(), json!("ignore"));
741
742 let allowed = vec!["x-tenant-id".to_string(), "Custom-Header".to_string()];
744
745 filter_headers(&mut extras, &allowed);
746
747 assert!(extras.contains_key("X-Tenant-ID"));
748 assert!(extras.contains_key("Custom-Header"));
749 assert!(!extras.contains_key("X-User-ID"));
750 assert!(!extras.contains_key("Irrelevant-Header"));
751
752 assert_eq!(extras.get("X-Tenant-ID").unwrap(), &json!("123"));
754 assert_eq!(extras.get("Custom-Header").unwrap(), &json!("abc"));
755 }
756
757 #[tokio::test]
758 async fn test_call_handler_core_extras_are_session_scoped() {
759 use crate::app::AppStateBuilder;
760 use crate::call::{ActiveCallType, Command};
761 use crate::config::Config;
762
763 let mut config = Config::default();
764 config.udp_port = 0;
765 let app_state = AppStateBuilder::new()
766 .with_config(config)
767 .build()
768 .await
769 .expect("Failed to build app state");
770
771 let session_id = "test-session-scoped".to_string();
772 let cancel_token = CancellationToken::new();
773
774 let mut extras = HashMap::new();
776 extras.insert("X-Custom".to_string(), json!("value"));
777
778 let (_audio_sender, audio_receiver) = tokio::sync::mpsc::unbounded_channel::<Bytes>();
779 let (command_sender, command_receiver) = tokio::sync::mpsc::unbounded_channel::<Command>();
780 let (event_sender, _event_receiver) =
781 tokio::sync::mpsc::unbounded_channel::<crate::event::SessionEvent>();
782
783 command_sender
785 .send(Command::Hangup {
786 reason: None,
787 initiator: None,
788 headers: None,
789 refer: None,
790 })
791 .ok();
792 drop(command_sender);
793
794 let final_extras = call_handler_core(
796 ActiveCallType::Sip,
797 session_id.clone(),
798 app_state.clone(),
799 cancel_token,
800 audio_receiver,
801 None,
802 false,
803 0,
804 command_receiver,
805 event_sender,
806 Some(extras), None, )
809 .await;
810
811 assert!(final_extras.is_some(), "final extras should be returned");
813 let extras = final_extras.unwrap();
814 assert_eq!(
815 extras.get("X-Custom"),
816 Some(&json!("value")),
817 "session-scoped extras should be preserved"
818 );
819 }
820}