| 1 | use super::*; |
| 2 | |
| 3 | #[derive(Clone)] |
| 4 | pub(super) struct RemoteCall { |
| 5 | pub(super) signaling: Arc<Signaling>, |
| 6 | pub(super) conversation: Conversation, |
| 7 | pub(super) participant: Participant, |
| 8 | pub(super) threadless: bool, |
| 9 | } |
| 10 | |
| 11 | #[derive(Default)] |
| 12 | pub(super) struct Live { |
| 13 | pub(super) peers: Vec<PeerConnection>, |
| 14 | pub(super) audio: Option<AudioSetup>, |
| 15 | pub(super) remote: Option<RemoteCall>, |
| 16 | pub(super) broker: Option<JoinHandle<()>>, |
| 17 | pub(super) broker_running: Arc<AtomicBool>, |
| 18 | pub(super) callbacks: Option<mpsc::UnboundedReceiver<TrouterCallback>>, |
| 19 | pub(super) route_id: Option<String>, |
| 20 | pub(super) call_legs: Vec<String>, |
| 21 | pub(super) skip_leave: bool, |
| 22 | pub(super) cancel_leave: bool, |
| 23 | pub(super) video_tasks: Vec<JoinHandle<()>>, |
| 24 | pub(super) video_counters: Arc<VideoCounters>, |
| 25 | } |
| 26 | |
| 27 | pub(super) async fn stop_local_publish( |
| 28 | local_video: &mut LocalVideo, |
| 29 | remote: &RemoteCall, |
| 30 | media_links: &BTreeMap<String, String>, |
| 31 | request_id: &mut u32, |
| 32 | control: &CallControl, |
| 33 | timeline: &Timeline, |
| 34 | ) { |
| 35 | let camera_mid = local_video.camera_mid(); |
| 36 | let share_mid = local_video.share_mid(); |
| 37 | let was_sharing = local_video.sharing(); |
| 38 | let was_camera = local_video.camera_on() || local_video.camera_previewing(); |
| 39 | if was_sharing { |
| 40 | local_video.stop_share(); |
| 41 | clear_share(control); |
| 42 | } |
| 43 | if was_camera { |
| 44 | local_video.stop_camera(); |
| 45 | control.send(CallUpdate::Camera(false)); |
| 46 | } |
| 47 | local_video.clear_lines(); |
| 48 | let Some(url) = media_links.get("updateMediaDescriptions").cloned() else { |
| 49 | return; |
| 50 | }; |
| 51 | let mut descriptions = Vec::new(); |
| 52 | if was_camera { |
| 53 | descriptions.push(MediaDescription::camera_line( |
| 54 | camera_mid.unwrap_or_else(|| CAMERA_MID.to_owned()), |
| 55 | false, |
| 56 | )); |
| 57 | } |
| 58 | if was_sharing { |
| 59 | descriptions.push(MediaDescription::share_line( |
| 60 | share_mid.unwrap_or_else(|| SHARE_MID.to_owned()), |
| 61 | false, |
| 62 | )); |
| 63 | } |
| 64 | if descriptions.is_empty() { |
| 65 | return; |
| 66 | } |
| 67 | *request_id += 1; |
| 68 | let id = *request_id; |
| 69 | match remote |
| 70 | .signaling |
| 71 | .update_media_descriptions(&url, &descriptions, id) |
| 72 | .await |
| 73 | { |
| 74 | Ok(()) => timeline.record( |
| 75 | "updateMediaDescriptions", |
| 76 | format!("publish off before leave, request {id}"), |
| 77 | ), |
| 78 | Err(error) => timeline.record( |
| 79 | "updateMediaDescriptions failed", |
| 80 | format!("publish off before leave: {error}"), |
| 81 | ), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | pub(super) async fn tear_down( |
| 86 | mut live: Live, |
| 87 | inner: &Inner, |
| 88 | timeline: &Timeline, |
| 89 | report: &mut CallReport, |
| 90 | cancelled: bool, |
| 91 | ) { |
| 92 | if crate::noise_stage::overloaded() { |
| 93 | timeline.record("noise suppression", "DeepFilterNet too slow, WebRTC only"); |
| 94 | } |
| 95 | crate::noise_stage::clear(); |
| 96 | // Stop keep-alive / broker first so Teams does not see us as still present. |
| 97 | live.broker_running.store(false, Ordering::SeqCst); |
| 98 | if let Some(broker) = live.broker.take() { |
| 99 | broker.abort(); |
| 100 | } |
| 101 | if let Some(audio) = live.audio.take() { |
| 102 | audio.stop().await; |
| 103 | } |
| 104 | // Leave and drop call legs while peers are still open, then close media. |
| 105 | if !live.skip_leave |
| 106 | && let Some(remote) = &live.remote |
| 107 | { |
| 108 | let reason = if cancelled { |
| 109 | LeaveReason::Cancel |
| 110 | } else { |
| 111 | LeaveReason::Hangup |
| 112 | }; |
| 113 | match remote |
| 114 | .signaling |
| 115 | .leave(&remote.conversation, &remote.participant, reason) |
| 116 | .await |
| 117 | { |
| 118 | Ok(()) => report.left_cleanly = true, |
| 119 | Err(error) => timeline.record("leave failed", error.to_string()), |
| 120 | } |
| 121 | for call_leg in &live.call_legs { |
| 122 | if let Err(error) = remote.signaling.end_call_leg(call_leg).await { |
| 123 | timeline.record("callLeg end failed", error.to_string()); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | if report.end.is_none() |
| 128 | && let Some(callbacks) = live.callbacks.as_mut() |
| 129 | && live.remote.is_some() |
| 130 | { |
| 131 | let waited = timeout(END_CALLBACK_WAIT, async { |
| 132 | while let Some(callback) = callbacks.recv().await { |
| 133 | let event = handle_callback(&callback, timeline); |
| 134 | if let Ok(CallEvent::End(end)) = event { |
| 135 | return Some(end); |
| 136 | } |
| 137 | } |
| 138 | None |
| 139 | }) |
| 140 | .await; |
| 141 | report.end = waited.ok().flatten(); |
| 142 | } |
| 143 | for peer in &live.peers { |
| 144 | peer.close(); |
| 145 | } |
| 146 | for task in live.video_tasks.drain(..) { |
| 147 | task.abort(); |
| 148 | } |
| 149 | if let Some(route_id) = &live.route_id { |
| 150 | inner.unregister_route(route_id); |
| 151 | } |
| 152 | timeline.record("closed", "peer closed, call route removed"); |
| 153 | } |
| 154 | |
| 155 | pub(super) fn setup_end_kind(end: &CallEnd) -> EndKind { |
| 156 | match end.kind() { |
| 157 | EndKind::Normal | EndKind::Other => EndKind::Unavailable, |
| 158 | kind => kind, |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | pub(super) fn handle_callback( |
| 163 | callback: &TrouterCallback, |
| 164 | timeline: &Timeline, |
| 165 | ) -> Result<CallEvent> { |
| 166 | let body = decode_body(callback)?; |
| 167 | let (_, event) = classify(&callback.path, body)?; |
| 168 | let detail = match &event { |
| 169 | CallEvent::Acceptance(acceptance) => format!( |
| 170 | "answer sdp {} bytes, fromMixer={}", |
| 171 | acceptance.sdp.len(), |
| 172 | acceptance.from_mixer |
| 173 | ), |
| 174 | CallEvent::End(end) => format!( |
| 175 | "code={} subCode={} phrase={}", |
| 176 | end.code, end.sub_code, end.phrase |
| 177 | ), |
| 178 | CallEvent::Progress(status) => format!("{status:?}"), |
| 179 | CallEvent::ConversationEnd(body) |
| 180 | | CallEvent::Rejection(body) |
| 181 | | CallEvent::AddParticipantFailure(body) |
| 182 | | CallEvent::RetargetCompletion(body) => outcome_summary(body), |
| 183 | CallEvent::ControlVideoStreaming(body) => parse_request(body) |
| 184 | .map_or_else(|| "body not understood".to_owned(), |request| request.shape), |
| 185 | CallEvent::MediaAcknowledgement(ack) => { |
| 186 | let names = ack.links.keys().cloned().collect::<Vec<_>>().join(","); |
| 187 | if names.is_empty() { |
| 188 | format!("links=none keys={}", ack.keys) |
| 189 | } else { |
| 190 | format!("links={names}") |
| 191 | } |
| 192 | } |
| 193 | _ => String::new(), |
| 194 | }; |
| 195 | timeline.record(format!("Trouter {}", event.name()), detail); |
| 196 | match &event { |
| 197 | CallEvent::Acceptance(acceptance) => timeline.record_sdp("remote answer", &acceptance.sdp), |
| 198 | CallEvent::MediaAnswer(answer) => timeline.record_sdp("remote answer", &answer.sdp), |
| 199 | CallEvent::MediaRenegotiation(renegotiation) => { |
| 200 | timeline.record_sdp("remote offer", &renegotiation.sdp); |
| 201 | let names = renegotiation |
| 202 | .links |
| 203 | .keys() |
| 204 | .cloned() |
| 205 | .collect::<Vec<_>>() |
| 206 | .join(","); |
| 207 | timeline.record( |
| 208 | "renegotiation links", |
| 209 | if names.is_empty() { |
| 210 | "none".to_owned() |
| 211 | } else { |
| 212 | names |
| 213 | }, |
| 214 | ); |
| 215 | } |
| 216 | _ => {} |
| 217 | } |
| 218 | Ok(event) |
| 219 | } |
| 220 | |
| 221 | pub(super) async fn poll_broker( |
| 222 | signaling: Arc<Signaling>, |
| 223 | conversation: Conversation, |
| 224 | running: Arc<AtomicBool>, |
| 225 | ) { |
| 226 | let Ok(first) = conversation.link("subscribe") else { |
| 227 | return; |
| 228 | }; |
| 229 | let mut url = first.to_owned(); |
| 230 | while running.load(Ordering::SeqCst) { |
| 231 | match signaling.poll_broker(&url).await { |
| 232 | Ok(Some(next)) => url = next, |
| 233 | Ok(None) => tokio::time::sleep(BROKER_IDLE_WAIT).await, |
| 234 | Err(_) => tokio::time::sleep(BROKER_RETRY_WAIT).await, |
| 235 | } |
| 236 | } |
| 237 | } |