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 cache_key: Option<String>,
61 },
62 Play {
63 url: String,
64 play_id: Option<String>,
65 auto_hangup: Option<bool>,
66 wait_input_timeout: Option<u32>,
67 offset_ms: Option<u32>,
68 },
69 Interrupt {
70 graceful: Option<bool>,
71 fade_out_ms: Option<u32>,
72 },
73 Pause {},
74 Resume {},
75 Hangup {
76 reason: Option<String>,
77 initiator: Option<String>,
78 headers: Option<HashMap<String, String>>,
79 refer: Option<bool>,
80 },
81 Refer {
82 caller: String,
83 callee: String,
85 options: Option<ReferOption>,
86 },
87 Bridge {
92 target_session_id: String,
94 },
95 Unbridge {
97 target_session_id: String,
99 },
100 Mute {
101 track_id: Option<String>,
102 },
103 Unmute {
104 track_id: Option<String>,
105 },
106 History {
107 speaker: String,
108 text: String,
109 },
110 Custom {
111 sender: Option<String>,
112 data: serde_json::Value,
113 },
114}
115
116#[derive(Debug)]
118pub struct RoutingState {
119 round_robin_counters: Arc<Mutex<HashMap<String, usize>>>,
121}
122
123impl Default for RoutingState {
124 fn default() -> Self {
125 Self::new()
126 }
127}
128
129impl RoutingState {
130 pub fn new() -> Self {
131 Self {
132 round_robin_counters: Arc::new(Mutex::new(HashMap::new())),
133 }
134 }
135
136 pub fn next_round_robin_index(&self, destination_key: &str, trunk_count: usize) -> usize {
138 if trunk_count == 0 {
139 return 0;
140 }
141
142 let mut counters = self.round_robin_counters.lock().unwrap();
143 let counter = counters
144 .entry(destination_key.to_string())
145 .or_insert_with(|| 0);
146 let r = *counter % trunk_count;
147 *counter += 1;
148 return r;
149 }
150}