summaryrefslogtreecommitdiffstats
path: root/vendor/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs
blob: 38b30dd0beeaf4b942541f2f3497d9de2801414d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;

use web_sys::{
    RtcPeerConnection, RtcRtpTransceiver, RtcRtpTransceiverDirection, RtcRtpTransceiverInit,
    RtcSessionDescriptionInit,
};

#[wasm_bindgen(
    inline_js = "export function is_unified_avail() { return Object.keys(RTCRtpTransceiver.prototype).indexOf('currentDirection')>-1; }"
)]
extern "C" {
    /// Available in FF since forever, in Chrome since 72, in Safari since 12.1
    fn is_unified_avail() -> bool;
}

#[wasm_bindgen_test]
async fn rtc_rtp_transceiver_direction() {
    if !is_unified_avail() {
        return;
    }

    let mut tr_init: RtcRtpTransceiverInit = RtcRtpTransceiverInit::new();

    let pc1: RtcPeerConnection = RtcPeerConnection::new().unwrap();

    let tr1: RtcRtpTransceiver = pc1.add_transceiver_with_str_and_init(
        "audio",
        tr_init.direction(RtcRtpTransceiverDirection::Sendonly),
    );
    assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly);
    assert_eq!(tr1.current_direction(), None);

    let pc2: RtcPeerConnection = RtcPeerConnection::new().unwrap();

    let (_, p2) = exchange_sdps(pc1, pc2).await;
    assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly);
    assert_eq!(
        tr1.current_direction(),
        Some(RtcRtpTransceiverDirection::Sendonly)
    );

    let tr2: RtcRtpTransceiver = js_sys::try_iter(&p2.get_transceivers())
        .unwrap()
        .unwrap()
        .next()
        .unwrap()
        .unwrap()
        .unchecked_into();

    assert_eq!(tr2.direction(), RtcRtpTransceiverDirection::Recvonly);
    assert_eq!(
        tr2.current_direction(),
        Some(RtcRtpTransceiverDirection::Recvonly)
    );
}

async fn exchange_sdps(
    p1: RtcPeerConnection,
    p2: RtcPeerConnection,
) -> (RtcPeerConnection, RtcPeerConnection) {
    let offer = JsFuture::from(p1.create_offer()).await.unwrap();
    let offer = offer.unchecked_into::<RtcSessionDescriptionInit>();
    JsFuture::from(p1.set_local_description(&offer))
        .await
        .unwrap();
    JsFuture::from(p2.set_remote_description(&offer))
        .await
        .unwrap();
    let answer = JsFuture::from(p2.create_answer()).await.unwrap();
    let answer = answer.unchecked_into::<RtcSessionDescriptionInit>();
    JsFuture::from(p2.set_local_description(&answer))
        .await
        .unwrap();
    JsFuture::from(p1.set_remote_description(&answer))
        .await
        .unwrap();
    (p1, p2)
}