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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
//! [`Component`] for `MediaTrack` with a `Recv` direction.
use std::rc::Rc;
use futures::StreamExt as _;
use medea_client_api_proto as proto;
use medea_client_api_proto::{
MediaSourceKind, MediaType, MemberId, TrackId, TrackPatchEvent,
};
use medea_macro::watchers;
use medea_reactive::{
when_all_processed, AllProcessed, Guarded, ObservableCell, Processed,
ProgressableCell,
};
use proto::ConnectionMode;
use crate::{
media::{LocalTracksConstraints, MediaDirection, MediaKind},
peer::{
component::SyncState,
media::{transitable_state::media_exchange_state, InTransition},
MediaExchangeState, MediaExchangeStateController,
MediaStateControllable, MuteStateController, TransceiverSide,
},
utils::{component, AsProtoState, SynchronizableState, Updatable},
};
use super::Receiver;
/// Component responsible for the [`Receiver`] enabling/disabling and
/// muting/unmuting.
pub type Component = component::Component<State, Receiver>;
/// State of the [`Component`].
#[derive(Debug)]
pub struct State {
/// ID of the [`Receiver`]'s [`remote::Track`].
///
/// [`remote::Track`]: crate::media::track::remote::Track
id: TrackId,
/// [MID] of the [`Receiver`]'s [`Transceiver`].
///
/// [`Transceiver`]: crate::platform::Transceiver
/// [MID]: https://w3.org/TR/webrtc#dom-rtptransceiver-mid
mid: Option<String>,
/// [`MediaType`] of the [`Receiver`]'s [`remote::Track`].
///
/// [`remote::Track`]: crate::media::track::remote::Track
media_type: MediaType,
/// ID of the member sending the [`Receiver`]'s [`remote::Track`].
///
/// [`remote::Track`]: crate::media::track::remote::Track
sender_id: MemberId,
/// Indicator whether the [`Receiver`]'s [`remote::Track`] is enabled
/// individually.
///
/// [`remote::Track`]: crate::media::track::remote::Track
enabled_individual: Rc<MediaExchangeStateController>,
/// Indicator whether the [`Receiver`]'s [`remote::Track`] is enabled
/// generally.
///
/// [`remote::Track`]: crate::media::track::remote::Track
enabled_general: ProgressableCell<media_exchange_state::Stable>,
/// Current general [`MediaDirection`] of this [`Receiver`].
media_direction: ObservableCell<MediaDirection>,
/// Indicator whether the [`Receiver`]'s [`remote::Track`] is muted.
///
/// [`remote::Track`]: crate::media::track::remote::Track
muted: ObservableCell<bool>,
/// Indicator whether this [`Receiver`] is working in a [P2P mesh] or [SFU]
/// mode.
///
/// [P2P mesh]: https://webrtcglossary.com/mesh
/// [SFU]: https://webrtcglossary.com/sfu
connection_mode: ConnectionMode,
/// Synchronization state of the [`Component`].
sync_state: ObservableCell<SyncState>,
}
impl AsProtoState for State {
type Output = proto::state::Receiver;
fn as_proto(&self) -> Self::Output {
Self::Output {
id: self.id,
connection_mode: self.connection_mode,
mid: self.mid.clone(),
media_type: self.media_type,
sender_id: self.sender_id.clone(),
muted: false,
media_direction: self.media_direction().into(),
}
}
}
impl SynchronizableState for State {
type Input = proto::state::Receiver;
fn from_proto(input: Self::Input, _: &LocalTracksConstraints) -> Self {
Self {
id: input.id,
mid: input.mid,
media_type: input.media_type,
sender_id: input.sender_id,
enabled_individual: MediaExchangeStateController::new(
media_exchange_state::Stable::from(
input.media_direction.is_recv_enabled(),
),
),
enabled_general: ProgressableCell::new(
media_exchange_state::Stable::from(
input.media_direction.is_enabled_general(),
),
),
muted: ObservableCell::new(input.muted),
media_direction: ObservableCell::new(input.media_direction.into()),
connection_mode: input.connection_mode,
sync_state: ObservableCell::new(SyncState::Synced),
}
}
fn apply(&self, input: Self::Input, _: &LocalTracksConstraints) {
let new_media_exchange_state = media_exchange_state::Stable::from(
input.media_direction.is_recv_enabled(),
);
let current_media_exchange_state = match self.enabled_individual.state()
{
MediaExchangeState::Transition(transition) => {
transition.into_inner()
}
MediaExchangeState::Stable(stable) => stable,
};
if current_media_exchange_state != new_media_exchange_state {
self.enabled_individual.update(new_media_exchange_state);
}
self.enabled_general.set(media_exchange_state::Stable::from(
input.media_direction.is_enabled_general(),
));
self.media_direction.set(input.media_direction.into());
self.sync_state.set(SyncState::Synced);
}
}
impl Updatable for State {
/// Returns [`Future`] resolving once [`media_exchange_state`] is
/// stabilized.
///
/// [`Future`]: std::future::Future
fn when_stabilized(&self) -> AllProcessed<'static> {
let controller = Rc::clone(&self.enabled_individual);
when_all_processed(std::iter::once(
Processed::new(Box::new(move || {
let controller = Rc::clone(&controller);
Box::pin(async move {
controller.when_stabilized().await;
})
}))
.into(),
))
}
/// Returns [`Future`] resolving once [`State`] update will be applied onto
/// the [`Receiver`].
///
/// [`Future`]: std::future::Future
fn when_updated(&self) -> AllProcessed<'static> {
when_all_processed(vec![
self.enabled_individual.when_processed().into(),
self.enabled_general.when_all_processed().into(),
])
}
/// Notifies [`State`] about a RPC connection loss.
fn connection_lost(&self) {
self.sync_state.set(SyncState::Desynced);
}
/// Notifies [`State`] about a RPC connection restore.
fn connection_recovered(&self) {
self.sync_state.set(SyncState::Syncing);
}
}
impl From<&State> for proto::state::Receiver {
fn from(from: &State) -> Self {
Self {
id: from.id,
connection_mode: from.connection_mode,
mid: from.mid.clone(),
media_type: from.media_type,
sender_id: from.sender_id.clone(),
media_direction: from.media_direction().into(),
muted: false,
}
}
}
impl State {
/// Returns [`State`] with a provided data.
#[must_use]
pub fn new(
id: TrackId,
mid: Option<String>,
media_type: MediaType,
media_direction: medea_client_api_proto::MediaDirection,
muted: bool,
sender: MemberId,
connection_mode: ConnectionMode,
) -> Self {
Self {
id,
mid,
media_type,
sender_id: sender,
enabled_individual: MediaExchangeStateController::new(
media_direction.is_recv_enabled().into(),
),
enabled_general: ProgressableCell::new(
media_direction.is_enabled_general().into(),
),
muted: ObservableCell::new(muted),
sync_state: ObservableCell::new(SyncState::Synced),
connection_mode,
media_direction: ObservableCell::new(media_direction.into()),
}
}
/// Returns [`TrackId`] of this [`State`].
#[must_use]
pub const fn id(&self) -> TrackId {
self.id
}
/// Returns current `mid` of this [`State`].
#[must_use]
pub fn mid(&self) -> Option<&str> {
self.mid.as_deref()
}
/// Returns current [`MediaType`] of this [`State`].
#[must_use]
pub const fn media_type(&self) -> MediaType {
self.media_type
}
/// Returns current [`MemberId`] of the `Member` from which this
/// [`State`] should receive media data.
#[must_use]
pub const fn sender_id(&self) -> &MemberId {
&self.sender_id
}
/// Returns current individual media exchange state of this [`State`].
#[must_use]
pub fn enabled_individual(&self) -> bool {
self.enabled_individual.enabled()
}
/// Returns current general media exchange state of this [`State`].
#[must_use]
pub fn enabled_general(&self) -> bool {
self.enabled_general.get() == media_exchange_state::Stable::Enabled
}
/// Returns current mute state of this [`State`].
#[must_use]
pub fn muted(&self) -> bool {
self.muted.get()
}
/// Returns the current general [`MediaDirection`] of this [`State`].
#[must_use]
pub fn media_direction(&self) -> MediaDirection {
self.media_direction.get()
}
/// Updates this [`State`] with the provided [`TrackPatchEvent`].
pub fn update(&self, track_patch: &TrackPatchEvent) {
if self.id != track_patch.id {
return;
}
if let Some(direction) = track_patch.media_direction {
self.enabled_general
.set(direction.is_enabled_general().into());
self.enabled_individual
.update(direction.is_recv_enabled().into());
}
if let Some(muted) = track_patch.muted {
self.muted.set(muted);
}
if let Some(direction) = track_patch.media_direction {
self.media_direction.set(direction.into());
}
}
}
#[watchers]
impl Component {
/// Watcher for the [`State::enabled_general`] updates.
///
/// Updates [`Receiver`]'s general media exchange state. Adds or removes
/// [`RECV`] direction from the [`Transceiver`] of the [`Receiver`].
///
/// [`RECV`]: crate::platform::TransceiverDirection::RECV
/// [`Transceiver`]: crate::platform::Transceiver
#[watch(self.enabled_general.subscribe())]
async fn general_media_exchange_state_changed(
receiver: Rc<Receiver>,
st: Rc<State>,
state: Guarded<media_exchange_state::Stable>,
) {
let (state, _guard) = state.into_parts();
receiver
.enabled_general
.set(state == media_exchange_state::Stable::Enabled);
if (st.connection_mode, state)
== (ConnectionMode::Mesh, media_exchange_state::Stable::Disabled)
{
let sub_recv = {
receiver
.transceiver
.borrow()
.as_ref()
.map(|trnscvr| trnscvr.set_recv(false))
};
if let Some(fut) = sub_recv {
fut.await;
}
} else {
let add_recv = receiver
.transceiver
.borrow()
.as_ref()
.map(|trnscvr| trnscvr.set_recv(true));
if let Some(fut) = add_recv {
fut.await;
}
}
receiver.maybe_notify_track().await;
}
/// Watcher for [`media_exchange_state::Stable`] media exchange state
/// updates.
///
/// Updates [`Receiver::enabled_individual`] to the new state.
#[watch(self.enabled_individual.subscribe_stable())]
fn enabled_individual_stable_state_changed(
receiver: &Receiver,
_: &State,
state: media_exchange_state::Stable,
) {
receiver
.enabled_individual
.set(state == media_exchange_state::Stable::Enabled);
}
/// Watcher for media exchange state [`media_exchange_state::Transition`]
/// updates.
///
/// Sends [`TrackEvent::MediaExchangeIntention`][1] with the provided
/// [`media_exchange_state`].
///
/// [1]: crate::peer::TrackEvent::MediaExchangeIntention
#[watch(self.enabled_individual.subscribe_transition())]
fn enabled_individual_transition_started(
receiver: &Receiver,
_: &State,
state: media_exchange_state::Transition,
) {
receiver.send_media_exchange_state_intention(state);
}
/// Watcher for the mute state updates.
///
/// Propagates command to the associated [`Receiver`] and updates its media
/// track (if any).
#[watch(self.muted.subscribe())]
fn mute_state_changed(receiver: &Receiver, _: &State, muted: bool) {
receiver.muted.set(muted);
if let Some(track) = receiver.track.borrow().as_ref() {
track.set_muted(muted);
}
}
/// Stops transition timeouts on [`SyncState::Desynced`].
///
/// Sends media state intentions and resets transition timeouts on
/// [`SyncState::Synced`].
#[watch(self.sync_state.subscribe().skip(1))]
fn sync_state_watcher(
receiver: &Receiver,
state: &State,
sync_state: SyncState,
) {
match sync_state {
SyncState::Synced => {
if let MediaExchangeState::Transition(transition) =
state.enabled_individual.state()
{
receiver.send_media_exchange_state_intention(transition);
}
state.enabled_individual.reset_transition_timeout();
}
SyncState::Desynced => {
state.enabled_individual.stop_transition_timeout();
}
SyncState::Syncing => (),
}
}
/// Updates [`MediaDirection`] of the provided [`Receiver`].
#[watch(self.media_direction.subscribe())]
fn direction_watcher(
receiver: &Receiver,
_: &State,
direction: MediaDirection,
) {
receiver.set_media_direction(direction);
}
}
impl MediaStateControllable for State {
fn media_exchange_state_controller(
&self,
) -> Rc<MediaExchangeStateController> {
Rc::clone(&self.enabled_individual)
}
fn mute_state_controller(&self) -> Rc<MuteStateController> {
// Receivers can be muted, but currently they are muted directly by
// server events.
//
// There is no point to provide an external API for muting receivers,
// since the muting is pipelined after demuxing and decoding, so it
// won't reduce incoming traffic or CPU usage. Therefore receivers
// muting don't require `MuteStateController`'s state management.
//
// Removing this `unreachable!()` would require abstracting
// `MuteStateController` to some trait and creating some dummy
// implementation. Not worth it atm.
unreachable!("Receivers muting is not implemented");
}
}
impl TransceiverSide for State {
fn track_id(&self) -> TrackId {
self.id
}
fn kind(&self) -> MediaKind {
match &self.media_type {
MediaType::Audio(_) => MediaKind::Audio,
MediaType::Video(_) => MediaKind::Video,
}
}
fn source_kind(&self) -> MediaSourceKind {
match &self.media_type {
MediaType::Audio(_) => MediaSourceKind::Device,
MediaType::Video(video) => video.source_kind,
}
}
fn is_transitable(&self) -> bool {
true
}
}
#[cfg(feature = "mockable")]
#[allow(clippy::multiple_inherent_impl)]
impl State {
/// Stabilizes [`MediaExchangeState`] of this [`State`].
pub fn stabilize(&self) {
if let crate::peer::MediaExchangeState::Transition(transition) =
self.enabled_individual.state()
{
self.enabled_individual.update(transition.intended());
self.enabled_general.set(transition.intended());
}
}
/// Sets the [`State::sync_state`] to a [`SyncState::Synced`].
pub fn synced(&self) {
self.sync_state.set(SyncState::Synced);
}
}