Skip to content

Add support for native to web cross-play #47

Closed

Description

@johanhelsing

There are some small incompatibilities between the native and wasm implementation that makes connections fail.

The native version implements ICE candidate trickling, while the wasm version does not. (#16)

Activity

  1. rozgo commented on Nov 26, 2022

    @rozgo
    Contributor

    One of our projects needs this to work. I'll look into this and report back.

  2. johanhelsing commented on Dec 9, 2022

    @johanhelsing
    OwnerAuthor

    @rozgo How did it go?

  3. rozgo commented on Dec 11, 2022

    @rozgo
    Contributor

    I've got it in a state where it should be working, but its not. So far, I'm getting ICE candidates, sending them to connected peers, with ICE trickling enabled. But still works among native or WASM, but not both. I had to pause this last week, but will look into it again on Monday.

    Minor changes:

        let ice_server_config_list = [ice_server_config];
        peer_config.ice_servers(&serde_wasm_bindgen::to_value(&ice_server_config_list).unwrap());
        peer_config.ice_transport_policy(RtcIceTransportPolicy::Relay);
        RtcPeerConnection::new_with_configuration(&peer_config).unwrap();

    Listening for OnIceCandidates:

        let signal_peer_ice = signal_peer.clone();
        let onicecandidate: Box<dyn FnMut(JsValue)> = Box::new(move |event| {
            let event = Reflect::get(&event, &JsValue::from_str("candidate")).efix();
            if let Ok(event) = event {
                if let Ok(candidate) = event.dyn_into::<RtcIceCandidate>() {
                    info!("ICE candidate: {:?}", candidate);
                    signal_peer_ice.send(PeerSignal::IceCandidate(candidate.candidate()));
                }
            }
        });
        let onicecandidate = Closure::wrap(onicecandidate);
        conn.set_onicecandidate(Some(onicecandidate.as_ref().unchecked_ref()));

    Handling the new candidate with existing message:

                PeerSignal::IceCandidate(candidate) => {
                    // Add ice candidate
                    info!("ICE CANDIDATE ADDING: {:?}", candidate);
                    candidates.push(candidate);
                }

    image

    NOTE: I noticed right before I had to move on, that the signaling server was not properly handling Native and WASM, at least it the minimal demo Im using. So it might be something simple, and things might work after that.

  4. rozgo commented on Dec 13, 2022

    @rozgo
    Contributor

    Ok, I got back into this. Made some progress and got WASM communicating to Native but breaks in the other direction. I think I understand the ICE back and forth chatter requirements for trickling now. Should have more info by EOD.

    Basically, I'm selecting for receiving ICE candidates and checking if data channel is ready, all while sending ICE candidates to remote peers.

        loop {
            select! {
                _ = channel_ready_rx.next() => {
                    debug!("channel ready");
                    break;
                }
                msg = signal_receiver.next() => {
                    if let Some(PeerSignal::IceCandidate(candidate)) = msg {
                        debug!("got an IceCandidate signal! {}", candidate);
                        let mut ice_candidate: RtcIceCandidateInit = RtcIceCandidateInit::new(&candidate);
                        ice_candidate.sdp_m_line_index(Some(0));
                        JsFuture::from(
                            conn.add_ice_candidate_with_opt_rtc_ice_candidate_init(Some(&ice_candidate)),
                        )
                        .await
                        .efix()?;
                    }
                }
            };
        }
        let signal_peer_ice = signal_peer.clone();
        let onicecandidate: Box<dyn FnMut(JsValue)> = Box::new(move |event| {
            let event = Reflect::get(&event, &JsValue::from_str("candidate")).efix();
            if let Ok(event) = event {
                if let Ok(candidate) = event.dyn_into::<RtcIceCandidate>() {
                    debug!("sending IceCandidate signal {}", candidate.candidate());
                    signal_peer_ice.send(PeerSignal::IceCandidate(candidate.candidate()));
                }
            }
        });
        let onicecandidate = Closure::wrap(onicecandidate);
        conn.set_onicecandidate(Some(onicecandidate.as_ref().unchecked_ref()));
  5. rozgo commented on Dec 13, 2022

    @rozgo
    Contributor

    Ok, it works. will clean up the code and submit a PR tonight.

    image

  6. johanhelsing commented on Dec 15, 2022

    @johanhelsing
    OwnerAuthor

    Awesome! I'll have some time today and tomorrow to review it

  7. johanhelsing commented on Dec 17, 2022

    @johanhelsing
    OwnerAuthor

    Done in #54 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions