rtci-host 0.3.0

The host I/O around the sans-I/O rtci WebRTC engine: sockets, timers, STUN/TURN clients, ICE-TCP, mDNS and media, behind a permission gate and a socket factory
Documentation
// Copyright 2026 Brainwires. MIT OR Apache-2.0.

//! [`Peer`]'s media calls (`RTCRtpTransceiver`, `RTCRtpSender`,
//! `RTCRtpReceiver`, encoded frames, fake devices, DTMF). Each runs on the
//! connection's state and then wakes the driver and the event queue.
//! Transceivers are named by the key [`Peer::add_transceiver`] returned.

use crate::error::Error;
use crate::media::CodecArg;
use crate::media::FakeFrame;
use crate::media::SenderFrameInfo;
use crate::media::TrackArg;
use crate::media::TransceiverArg;
use crate::media::TransceiverInfo;
use crate::peer::Peer;

impl Peer {
  /// `addTransceiver()` (and `addTrack()`'s new transceiver): its key.
  pub fn add_transceiver(&self, arg: TransceiverArg) -> Result<u32, Error> {
    self.media(|core| core.add_transceiver(arg))
  }

  /// A track for a transceiver's sender (`addTrack()` reusing one,
  /// `replaceTrack()`).
  pub fn attach_track(
    &self,
    key: u32,
    track: TrackArg,
    streams: Vec<String>,
  ) -> Result<(), Error> {
    self.media(|core| core.attach_track(key, track, streams))
  }

  /// `getTransceivers()`: empty once the connection is closed.
  pub fn transceivers(&self) -> Result<Vec<TransceiverInfo>, Error> {
    if self.is_closed() {
      return Ok(Vec::new());
    }
    self.media(|core| Ok(core.transceivers()))
  }

  /// `transceiver.direction = ...`.
  pub fn set_transceiver_direction(
    &self,
    key: u32,
    direction: &str,
  ) -> Result<(), Error> {
    self.media(|core| core.set_direction(key, direction))
  }

  /// `transceiver.stop()`.
  pub fn stop_transceiver(&self, key: u32) -> Result<(), Error> {
    self.media(|core| core.stop_transceiver(key))
  }

  /// `transceiver.setCodecPreferences()`.
  pub fn set_codec_preferences(
    &self,
    key: u32,
    codecs: Vec<CodecArg>,
  ) -> Result<(), Error> {
    self.media(|core| core.set_codec_preferences(key, codecs))
  }

  // wpt100:hdrext
  /// `transceiver.setHeaderExtensionsToNegotiate()`, by URI.
  pub fn set_header_extensions_to_negotiate(
    &self,
    key: u32,
    uris: Vec<String>,
  ) -> Result<(), Error> {
    self.media(|core| core.set_header_extensions_to_negotiate(key, uris))
  }

  // wpt100:hdrext
  /// `RTCConfiguration.rtpHeaderEncryptionPolicy` and
  /// `alwaysNegotiateDataChannels`.
  pub fn set_negotiation_options(
    &self,
    rtp_header_encryption_policy: &str,
    always_negotiate_data_channels: bool,
  ) -> Result<(), Error> {
    self.media(|core| {
      core.set_negotiation_options(
        rtp_header_encryption_policy,
        always_negotiate_data_channels,
      )
    })
  }

  /// The sender's new track is an encoded track of `codec` (what its frames
  /// are in), or `None`: another track or none.
  pub fn set_sender_track_codec(
    &self,
    key: u32,
    codec: Option<CodecArg>,
  ) -> Result<(), Error> {
    self.media(|core| {
      core.set_sender_track_codec(key, codec);
      Ok(())
    })
  }

  /// Whether a sender sends (a disabled or ended track pauses it).
  pub fn set_sender_paused(&self, key: u32, paused: bool) -> Result<(), Error> {
    self.media(|core| {
      core.set_sender_paused(key, paused);
      Ok(())
    })
  }

  /// `sender.setStreams()`.
  pub fn set_sender_streams(
    &self,
    key: u32,
    streams: Vec<String>,
  ) -> Result<(), Error> {
    self.media(|core| core.set_streams(key, streams))
  }

  /// `sender.getParameters()`.
  pub fn sender_parameters(
    &self,
    key: u32,
  ) -> Result<serde_json::Value, Error> {
    self.media(|core| core.sender_parameters(key))
  }

  /// `receiver.getParameters()`.
  pub fn receiver_parameters(
    &self,
    key: u32,
  ) -> Result<serde_json::Value, Error> {
    self.media(|core| core.receiver_parameters(key))
  }

  /// `receiver.getSynchronizationSources()` / `getContributingSources()`.
  pub fn receiver_sources(
    &self,
    key: u32,
    contributing: bool,
  ) -> Result<Vec<serde_json::Value>, Error> {
    self.media(|core| core.receiver_sources(key, contributing))
  }

  /// Writes an RTP packet (header included) for a sender.
  pub fn write_rtp(&self, key: u32, data: &[u8]) -> Result<bool, Error> {
    self.media(|core| core.write_rtp(key, data))
  }

  /// Writes an encoded frame for a sender.
  pub fn write_frame(
    &self,
    key: u32,
    data: &[u8],
    duration_ms: f64,
  ) -> Result<bool, Error> {
    self.media(|core| core.write_frame(key, data, duration_ms))
  }

  /// Writes a fake capture device's next frame for a sender.
  pub fn write_fake(&self, key: u32, stereo: bool) -> Result<bool, Error> {
    self.media(|core| core.write_fake(key, stereo))
  }

  /// A fake device's next frame for a sender with an encoded transform (it
  /// is written later with [`Peer::write_frame_at`]).
  pub fn fake_frame(
    &self,
    key: u32,
    stereo: bool,
  ) -> Result<Option<FakeFrame>, Error> {
    self.media(|core| core.fake_frame(key, stereo))
  }

  /// Takes the RTP timestamp of a sender's next frame, for an encoded
  /// transform's frame; `None` before negotiation.
  pub fn reserve_frame(
    &self,
    key: u32,
    duration_ms: f64,
  ) -> Result<Option<SenderFrameInfo>, Error> {
    self.media(|core| core.reserve_frame(key, duration_ms))
  }

  /// Writes an encoded transform's frame at the RTP timestamp it reserved.
  pub fn write_frame_at(
    &self,
    key: u32,
    data: &[u8],
    duration_ms: f64,
    rtp_timestamp: u32,
  ) -> Result<bool, Error> {
    self.media(|core| {
      core.write_frame_at(key, data, duration_ms, Some(rtp_timestamp))
    })
  }

  /// `receiver.transform` set or cleared: the receiver's frames are
  /// delivered as `encodedframe` events while it is set.
  pub fn set_receiver_transform(
    &self,
    key: u32,
    on: bool,
  ) -> Result<(), Error> {
    self.media(|core| core.receiver_transform(key, on))
  }

  /// Whether a receiver's RTP packets are delivered (`rtp` events).
  pub fn set_receiver_read_rtp(&self, key: u32, on: bool) -> Result<(), Error> {
    self.media(|core| {
      if on {
        core.media.reading.insert(key);
      } else {
        core.media.reading.remove(&key);
      }
      Ok(())
    })
  }

  // wpt100:mediasink
  /// Whether a receiver's frames are put together and delivered
  /// (`mediaframe` events).
  pub fn set_receiver_read_frames(
    &self,
    key: u32,
    on: bool,
  ) -> Result<(), Error> {
    self.media(|core| core.receiver_read_frames(key, on))
  }

  /// A receiver's track was muted (its remote side stopped sending): the
  /// next RTP packet unmutes it again (`trackunmute`).
  pub fn set_receiver_muted(
    &self,
    key: u32,
    generation: u32,
  ) -> Result<(), Error> {
    self.media(|core| {
      core.receiver_muted(key, generation);
      Ok(())
    })
  }

  /// `RTCDTMFSender`: one telephone event (`event` above 15 is 15).
  pub fn send_dtmf(
    &self,
    key: u32,
    event: u32,
    duration_ms: u32,
  ) -> Result<bool, Error> {
    self.media(|core| core.send_dtmf(key, event.min(15) as u8, duration_ms))
  }

  /// Asks a receiver's remote sender for a key frame (PLI).
  pub fn request_key_frame(&self, key: u32) -> Result<bool, Error> {
    self.media(|core| core.request_key_frame(key))
  }
}