videocall-client 4.0.6

High-performance WebAssembly video conferencing client for videocall.rs, supporting WebTransport and WebSocket.
/*
 * Copyright 2025 Security Union LLC
 *
 * Licensed under either of
 *
 * * Apache License, Version 2.0
 *   (http://www.apache.org/licenses/LICENSE-2.0)
 * * MIT license
 *   (http://opensource.org/licenses/MIT)
 *
 * at your option.
 *
 * Unless you explicitly state otherwise, any contribution intentionally
 * submitted for inclusion in the work by you, as defined in the Apache-2.0
 * license, shall be dual licensed as above, without any additional terms or
 * conditions.
 */

//
// Generic Task that can be a WebSocketTask or WebTransportTask.
//
// Handles rollover of connection from WebTransport to WebSocket
//
use log::debug;
use videocall_transport::websocket::WebSocketTask;
use videocall_transport::webtransport::WebTransportTask;
use videocall_types::protos::packet_wrapper::PacketWrapper;

use super::webmedia::{ConnectOptions, WebMedia};

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub(super) enum Task {
    WebSocket(WebSocketTask),
    WebTransport(WebTransportTask),
}

impl Task {
    pub fn connect(webtransport: bool, options: ConnectOptions) -> anyhow::Result<Self> {
        if webtransport {
            debug!("Task::connect trying WebTransport");
            WebTransportTask::connect(options).map(Task::WebTransport)
        } else {
            debug!("Task::connect trying WebSocket");
            WebSocketTask::connect(options).map(Task::WebSocket)
        }
    }

    pub fn send_packet(&self, packet: PacketWrapper) {
        match self {
            Task::WebSocket(ws) => ws.send_packet(packet),
            Task::WebTransport(wt) => wt.send_packet(packet),
        }
    }

    /// Send a packet via datagram (unreliable, low-latency) when supported.
    ///
    /// For WebTransport, this uses datagrams for small packets and falls back
    /// to streams for oversized packets. For WebSocket, this is equivalent to
    /// `send_packet()` since WebSocket has no datagram concept.
    pub fn send_packet_datagram(&self, packet: PacketWrapper) {
        match self {
            Task::WebSocket(ws) => ws.send_packet(packet),
            Task::WebTransport(wt) => wt.send_packet_datagram(packet),
        }
    }
}