(
channel: Arc<RTCDataChannel>,
session: crate::simulators::session::SimulatorSession,
state: AppState,
udid: String,
stream_control_tx: mpsc::UnboundedSender<WebRtcStreamCommand>,
| 492 | } |
| 493 | |
| 494 | fn attach_control_data_channel( |
| 495 | channel: Arc<RTCDataChannel>, |
| 496 | session: crate::simulators::session::SimulatorSession, |
| 497 | state: AppState, |
| 498 | udid: String, |
| 499 | stream_control_tx: mpsc::UnboundedSender<WebRtcStreamCommand>, |
| 500 | ) { |
| 501 | let (control_tx, control_rx) = mpsc::unbounded_channel::<ControlMessage>(); |
| 502 | task::spawn(run_webrtc_control_queue( |
| 503 | session.clone(), |
| 504 | state.clone(), |
| 505 | udid.clone(), |
| 506 | stream_control_tx.clone(), |
| 507 | control_rx, |
| 508 | )); |
| 509 | channel.on_message(Box::new(move |message: DataChannelMessage| { |
| 510 | let session = session.clone(); |
| 511 | let state = state.clone(); |
| 512 | let udid = udid.clone(); |
| 513 | let stream_control_tx = stream_control_tx.clone(); |
| 514 | let control_tx = control_tx.clone(); |
| 515 | Box::pin(async move { |
| 516 | let Ok(text) = std::str::from_utf8(&message.data) else { |
| 517 | warn!("Invalid WebRTC control message bytes for {udid}"); |
| 518 | return; |
| 519 | }; |
| 520 | if let Ok(message) = serde_json::from_str::<WebRtcDataChannelMessage>(text) { |
| 521 | match message { |
| 522 | WebRtcDataChannelMessage::ClientStats { stats } => { |
| 523 | if !stats.client_id.trim().is_empty() && !stats.kind.trim().is_empty() { |
| 524 | apply_stream_client_foreground_from_stats(&state, &stats); |
| 525 | state.metrics.record_client_stream_stats(*stats); |
| 526 | } |
| 527 | } |
| 528 | WebRtcDataChannelMessage::StreamControl { |
| 529 | client_id, |
| 530 | force_keyframe, |
| 531 | foreground, |
| 532 | snapshot, |
| 533 | } => { |
| 534 | apply_stream_client_foreground(&state, &session, &client_id, foreground); |
| 535 | let command = WebRtcStreamCommand { |
| 536 | force_keyframe: force_keyframe.unwrap_or(false), |
| 537 | snapshot: snapshot.unwrap_or(false), |
| 538 | }; |
| 539 | if command.force_keyframe || command.snapshot { |
| 540 | session.request_keyframe(); |
| 541 | } |
| 542 | let _ = stream_control_tx.send(command); |
| 543 | } |
| 544 | WebRtcDataChannelMessage::StreamQuality { config } => { |
| 545 | if let Err(error) = apply_stream_quality_payload(&state, &config) { |
| 546 | warn!("WebRTC stream quality update failed for {udid}: {error}"); |
| 547 | } else { |
| 548 | session.request_keyframe(); |
| 549 | } |
| 550 | } |
| 551 | } |
no test coverage detected