rtc_rtp_transceiver_direction.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. use wasm_bindgen::{prelude::*, JsCast};
  2. use wasm_bindgen_futures::JsFuture;
  3. use wasm_bindgen_test::*;
  4. use web_sys::{
  5. RtcPeerConnection, RtcRtpTransceiver, RtcRtpTransceiverDirection, RtcRtpTransceiverInit,
  6. RtcSessionDescriptionInit,
  7. };
  8. #[wasm_bindgen(
  9. inline_js = "export function is_unified_avail() { return Object.keys(RTCRtpTransceiver.prototype).indexOf('currentDirection')>-1; }"
  10. )]
  11. extern "C" {
  12. /// Available in FF since forever, in Chrome since 72, in Safari since 12.1
  13. fn is_unified_avail() -> bool;
  14. }
  15. #[wasm_bindgen_test]
  16. async fn rtc_rtp_transceiver_direction() {
  17. if !is_unified_avail() {
  18. return;
  19. }
  20. let mut tr_init: RtcRtpTransceiverInit = RtcRtpTransceiverInit::new();
  21. let pc1: RtcPeerConnection = RtcPeerConnection::new().unwrap();
  22. let tr1: RtcRtpTransceiver = pc1.add_transceiver_with_str_and_init(
  23. "audio",
  24. tr_init.direction(RtcRtpTransceiverDirection::Sendonly),
  25. );
  26. assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly);
  27. assert_eq!(tr1.current_direction(), None);
  28. let pc2: RtcPeerConnection = RtcPeerConnection::new().unwrap();
  29. let (_, p2) = exchange_sdps(pc1, pc2).await;
  30. assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly);
  31. assert_eq!(
  32. tr1.current_direction(),
  33. Some(RtcRtpTransceiverDirection::Sendonly)
  34. );
  35. let tr2: RtcRtpTransceiver = js_sys::try_iter(&p2.get_transceivers())
  36. .unwrap()
  37. .unwrap()
  38. .next()
  39. .unwrap()
  40. .unwrap()
  41. .unchecked_into();
  42. assert_eq!(tr2.direction(), RtcRtpTransceiverDirection::Recvonly);
  43. assert_eq!(
  44. tr2.current_direction(),
  45. Some(RtcRtpTransceiverDirection::Recvonly)
  46. );
  47. }
  48. async fn exchange_sdps(
  49. p1: RtcPeerConnection,
  50. p2: RtcPeerConnection,
  51. ) -> (RtcPeerConnection, RtcPeerConnection) {
  52. let offer = JsFuture::from(p1.create_offer()).await.unwrap();
  53. let offer = offer.unchecked_into::<RtcSessionDescriptionInit>();
  54. JsFuture::from(p1.set_local_description(&offer))
  55. .await
  56. .unwrap();
  57. JsFuture::from(p2.set_remote_description(&offer))
  58. .await
  59. .unwrap();
  60. let answer = JsFuture::from(p2.create_answer()).await.unwrap();
  61. let answer = answer.unchecked_into::<RtcSessionDescriptionInit>();
  62. JsFuture::from(p2.set_local_description(&answer))
  63. .await
  64. .unwrap();
  65. JsFuture::from(p1.set_remote_description(&answer))
  66. .await
  67. .unwrap();
  68. (p1, p2)
  69. }