1use crate::{CallOption, ReferOption, media::recorder::RecorderOption, synthesis::SynthesisOption};
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4use std::{
5 collections::HashMap,
6 sync::{Arc, Mutex},
7};
8
9pub mod active_call;
10pub mod sip;
11pub use active_call::ActiveCall;
12pub use active_call::ActiveCallRef;
13pub use active_call::ActiveCallType;
14
15pub type CommandSender = tokio::sync::broadcast::Sender<Command>;
16pub type CommandReceiver = tokio::sync::broadcast::Receiver<Command>;
17
18#[skip_serializing_none]
20#[derive(Debug, Deserialize, Serialize, Clone)]
21#[serde(
22 tag = "command",
23 rename_all = "camelCase",
24 rename_all_fields = "camelCase"
25)]
26pub enum Command {
27 Invite {
28 option: CallOption,
29 },
30 Accept {
31 option: CallOption,
32 },
33 Reject {
34 reason: String,
35 code: Option<u32>,
36 },
37 Ringing {
38 recorder: Option<RecorderOption>,
39 early_media: Option<bool>,
40 ringtone: Option<String>,
41 },
42 Tts {
43 text: String,
44 speaker: Option<String>,
45 play_id: Option<String>,
47 auto_hangup: Option<bool>,
49 streaming: Option<bool>,
53 end_of_stream: Option<bool>,
55 option: Option<SynthesisOption>,
56 wait_input_timeout: Option<u32>,
57 base64: Option<bool>,
59 },
60 Play {
61 url: String,
62 play_id: Option<String>,
63 auto_hangup: Option<bool>,
64 wait_input_timeout: Option<u32>,
65 },
66 Interrupt {
67 graceful: Option<bool>,
68 fade_out_ms: Option<u32>,
69 },
70 Pause {},
71 Resume {},
72 Hangup {
73 reason: Option<String>,
74 initiator: Option<String>,
75 },
76 Refer {
77 caller: String,
78 callee: String,
80 options: Option<ReferOption>,
81 },
82 Mute {
83 track_id: Option<String>,
84 },
85 Unmute {
86 track_id: Option<String>,
87 },
88 History {
89 speaker: String,
90 text: String,
91 },
92}
93
94#[derive(Debug)]
96pub struct RoutingState {
97 round_robin_counters: Arc<Mutex<HashMap<String, usize>>>,
99}
100
101impl Default for RoutingState {
102 fn default() -> Self {
103 Self::new()
104 }
105}
106
107impl RoutingState {
108 pub fn new() -> Self {
109 Self {
110 round_robin_counters: Arc::new(Mutex::new(HashMap::new())),
111 }
112 }
113
114 pub fn next_round_robin_index(&self, destination_key: &str, trunk_count: usize) -> usize {
116 if trunk_count == 0 {
117 return 0;
118 }
119
120 let mut counters = self.round_robin_counters.lock().unwrap();
121 let counter = counters
122 .entry(destination_key.to_string())
123 .or_insert_with(|| 0);
124 let r = *counter % trunk_count;
125 *counter += 1;
126 return r;
127 }
128}