Skip to content

Commit 650c6ec

Browse files
authored
Move peer API logic into submodule in medea-flutter-webrtc-native crate (#231, #210)
1 parent b46f6f5 commit 650c6ec

35 files changed

+2182
-1799
lines changed

crates/native/src/api/mod.rs

Lines changed: 12 additions & 654 deletions
Large diffs are not rendered by default.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! [RTCIceConnectionState][1] definitions.
2+
//!
3+
//! [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate
4+
5+
use libwebrtc_sys as sys;
6+
7+
/// [RTCIceConnectionState][1] representation.
8+
///
9+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate
10+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11+
pub enum IceConnectionState {
12+
/// [RTCIceConnectionState.new][1] representation.
13+
///
14+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate-new
15+
New,
16+
17+
/// [RTCIceConnectionState.checking][1] representation.
18+
///
19+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate-checking
20+
Checking,
21+
22+
/// [RTCIceConnectionState.connected][1] representation.
23+
///
24+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate-connected
25+
Connected,
26+
27+
/// [RTCIceConnectionState.completed][1] representation.
28+
///
29+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate-completed
30+
Completed,
31+
32+
/// [RTCIceConnectionState.failed][1] representation.
33+
///
34+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate-failed
35+
Failed,
36+
37+
/// [RTCIceConnectionState.disconnected][1] representation.
38+
///
39+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate-disconnected
40+
Disconnected,
41+
42+
/// [RTCIceConnectionState.closed][1] representation.
43+
///
44+
/// [1]: https://w3.org/TR/webrtc#dom-rtciceconnectionstate-closed
45+
Closed,
46+
}
47+
48+
impl From<sys::IceConnectionState> for IceConnectionState {
49+
fn from(state: sys::IceConnectionState) -> Self {
50+
match state {
51+
sys::IceConnectionState::kIceConnectionNew => Self::New,
52+
sys::IceConnectionState::kIceConnectionChecking => Self::Checking,
53+
sys::IceConnectionState::kIceConnectionConnected => Self::Connected,
54+
sys::IceConnectionState::kIceConnectionCompleted => Self::Completed,
55+
sys::IceConnectionState::kIceConnectionFailed => Self::Failed,
56+
sys::IceConnectionState::kIceConnectionDisconnected => {
57+
Self::Disconnected
58+
}
59+
sys::IceConnectionState::kIceConnectionClosed => Self::Closed,
60+
_ => unreachable!(),
61+
}
62+
}
63+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! [RTCIceGatheringState][1] definitions.
2+
//!
3+
//! [1]: https://w3.org/TR/webrtc#dom-rtcicegatheringstate
4+
5+
use libwebrtc_sys as sys;
6+
7+
/// [RTCIceGatheringState][1] representation.
8+
///
9+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicegatheringstate
10+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11+
pub enum IceGatheringState {
12+
/// [RTCIceGatheringState.new][1] representation.
13+
///
14+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicegatheringstate-new
15+
New,
16+
17+
/// [RTCIceGatheringState.gathering][1] representation.
18+
///
19+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicegatheringstate-gathering
20+
Gathering,
21+
22+
/// [RTCIceGatheringState.complete][1] representation.
23+
///
24+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicegatheringstate-complete
25+
Complete,
26+
}
27+
28+
impl From<sys::IceGatheringState> for IceGatheringState {
29+
fn from(state: sys::IceGatheringState) -> Self {
30+
match state {
31+
sys::IceGatheringState::kIceGatheringNew => Self::New,
32+
sys::IceGatheringState::kIceGatheringGathering => Self::Gathering,
33+
sys::IceGatheringState::kIceGatheringComplete => Self::Complete,
34+
_ => unreachable!(),
35+
}
36+
}
37+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//! [`PeerConnection`]'s events.
2+
3+
pub mod ice_connection_state;
4+
pub mod ice_gathering_state;
5+
pub mod peer_connection_state;
6+
pub mod rtc_track_event;
7+
pub mod signaling_state;
8+
9+
use std::sync::Arc;
10+
11+
pub use self::{
12+
ice_connection_state::IceConnectionState,
13+
ice_gathering_state::IceGatheringState,
14+
peer_connection_state::PeerConnectionState, rtc_track_event::RtcTrackEvent,
15+
signaling_state::SignalingState,
16+
};
17+
use crate::{PeerConnection, frb_generated::RustOpaque};
18+
19+
/// Representation of [`PeerConnection`]'s events.
20+
#[derive(Clone)]
21+
pub enum PeerConnectionEvent {
22+
/// [`PeerConnection`] has been created.
23+
PeerCreated {
24+
/// Rust side [`PeerConnection`].
25+
peer: RustOpaque<Arc<PeerConnection>>,
26+
},
27+
28+
/// [RTCIceCandidate][1] has been discovered.
29+
///
30+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
31+
IceCandidate {
32+
/// Media stream "identification-tag" defined in [RFC 5888] for the
33+
/// media component the discovered [RTCIceCandidate][1] is associated
34+
/// with.
35+
///
36+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
37+
/// [RFC 5888]: https://tools.ietf.org/html/rfc5888
38+
sdp_mid: String,
39+
40+
/// Index (starting at zero) of the media description in the SDP this
41+
/// [RTCIceCandidate][1] is associated with.
42+
///
43+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
44+
sdp_mline_index: i32,
45+
46+
/// Candidate-attribute as defined in Section 15.1 of [RFC 5245].
47+
///
48+
/// If this [RTCIceCandidate][1] represents an end-of-candidates
49+
/// indication or a peer reflexive remote candidate, candidate is an
50+
/// empty string.
51+
///
52+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
53+
/// [RFC 5245]: https://tools.ietf.org/html/rfc5245
54+
candidate: String,
55+
},
56+
57+
/// [`PeerConnection`]'s ICE gathering state has changed.
58+
IceGatheringStateChange(IceGatheringState),
59+
60+
/// Failure occurred when gathering [RTCIceCandidate][1].
61+
///
62+
/// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
63+
IceCandidateError {
64+
/// Local IP address used to communicate with the STUN or TURN server.
65+
address: String,
66+
67+
/// Port used to communicate with the STUN or TURN server.
68+
port: i32,
69+
70+
/// STUN or TURN URL identifying the STUN or TURN server for which the
71+
/// failure occurred.
72+
url: String,
73+
74+
/// Numeric STUN error code returned by the STUN or TURN server
75+
/// [`STUN-PARAMETERS`][1].
76+
///
77+
/// If no host candidate can reach the server, it will be set to the
78+
/// value `701` which is outside the STUN error code range.
79+
///
80+
/// [1]: https://tinyurl.com/stun-parameters-6
81+
error_code: i32,
82+
83+
/// STUN reason text returned by the STUN or TURN server
84+
/// [`STUN-PARAMETERS`][1].
85+
///
86+
/// If the server could not be reached, it will be set to an
87+
/// implementation-specific value providing details about the error.
88+
///
89+
/// [1]: https://tinyurl.com/stun-parameters-6
90+
error_text: String,
91+
},
92+
93+
/// Negotiation or renegotiation of the [`PeerConnection`] needs to be
94+
/// performed.
95+
NegotiationNeeded,
96+
97+
/// [`PeerConnection`]'s [`SignalingState`] has been changed.
98+
SignallingChange(SignalingState),
99+
100+
/// [`PeerConnection`]'s [`IceConnectionState`] has been changed.
101+
IceConnectionStateChange(IceConnectionState),
102+
103+
/// [`PeerConnection`]'s [`PeerConnectionState`] has been changed.
104+
ConnectionStateChange(PeerConnectionState),
105+
106+
/// New incoming media has been negotiated.
107+
Track(RtcTrackEvent),
108+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Current state of a [`PeerConnection`].
2+
3+
use libwebrtc_sys as sys;
4+
5+
#[cfg(doc)]
6+
use crate::{PeerConnection, api::IceConnectionState};
7+
8+
/// Indicator of the current state of a [`PeerConnection`].
9+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10+
pub enum PeerConnectionState {
11+
/// At least one of the connection's ICE transports is in the new state,
12+
/// and none of them are in one of the following states: `connecting`,
13+
/// `checking`, `failed`, `disconnected`, or all of the connection's
14+
/// transports are in the `closed` state.
15+
New,
16+
17+
/// One or more of the ICE transports are currently in the process of
18+
/// establishing a connection. That is, their [`IceConnectionState`] is
19+
/// either [`IceConnectionState::Checking`] or
20+
/// [`IceConnectionState::Connected`], and no transports are in the
21+
/// `failed` state.
22+
Connecting,
23+
24+
/// Every ICE transport used by the connection is either in use (state
25+
/// `connected` or `completed`) or is closed (state `closed`). In addition,
26+
/// at least one transport is either `connected` or `completed`.
27+
Connected,
28+
29+
/// At least one of the ICE transports for the connection is in the
30+
/// `disconnected` state and none of the other transports are in the state
31+
/// `failed`, `connecting` or `checking`.
32+
Disconnected,
33+
34+
/// One or more of the ICE transports on the connection is in the `failed`
35+
/// state.
36+
Failed,
37+
38+
/// Peer connection is closed.
39+
Closed,
40+
}
41+
42+
impl From<sys::PeerConnectionState> for PeerConnectionState {
43+
fn from(state: sys::PeerConnectionState) -> Self {
44+
match state {
45+
sys::PeerConnectionState::kNew => Self::New,
46+
sys::PeerConnectionState::kConnecting => Self::Connecting,
47+
sys::PeerConnectionState::kConnected => Self::Connected,
48+
sys::PeerConnectionState::kDisconnected => Self::Disconnected,
49+
sys::PeerConnectionState::kFailed => Self::Failed,
50+
sys::PeerConnectionState::kClosed => Self::Closed,
51+
_ => unreachable!(),
52+
}
53+
}
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Representation of a [`MediaStreamTrack`] event.
2+
3+
#[cfg(doc)]
4+
use crate::PeerConnection;
5+
use crate::api::{MediaStreamTrack, RtcRtpTransceiver};
6+
7+
/// Representation of a track event, sent when a new [`MediaStreamTrack`] is
8+
/// added to an [`RtcRtpTransceiver`] as part of a [`PeerConnection`].
9+
#[derive(Clone)]
10+
pub struct RtcTrackEvent {
11+
/// [`MediaStreamTrack`] associated with the [RTCRtpReceiver] identified
12+
/// by the receiver.
13+
///
14+
/// [RTCRtpReceiver]: https://w3.org/TR/webrtc#dom-rtcrtpreceiver
15+
pub track: MediaStreamTrack,
16+
17+
/// [`RtcRtpTransceiver`] object associated with the event.
18+
pub transceiver: RtcRtpTransceiver,
19+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//! [RTCSignalingState] definitions.
2+
//!
3+
//! [RTCSignalingState]: https://w3.org/TR/webrtc#state-definitions
4+
5+
use libwebrtc_sys as sys;
6+
7+
/// [RTCSignalingState] representation.
8+
///
9+
/// [RTCSignalingState]: https://w3.org/TR/webrtc#state-definitions
10+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11+
pub enum SignalingState {
12+
/// [RTCSignalingState.stable][1] representation.
13+
///
14+
/// [1]: https://w3.org/TR/webrtc#dom-rtcsignalingstate-stable
15+
Stable,
16+
17+
/// [RTCSignalingState.have-local-offer][1] representation.
18+
///
19+
/// [1]: https://w3.org/TR/webrtc#dom-rtcsignalingstate-have-local-offer
20+
HaveLocalOffer,
21+
22+
/// [RTCSignalingState.have-local-pranswer][1] representation.
23+
///
24+
/// [1]: https://tinyurl.com/have-local-pranswer
25+
HaveLocalPrAnswer,
26+
27+
/// [RTCSignalingState.have-remote-offer][1] representation.
28+
///
29+
/// [1]: https://tinyurl.com/have-remote-offer
30+
HaveRemoteOffer,
31+
32+
/// [RTCSignalingState.have-remote-pranswer][1] representation.
33+
///
34+
/// [1]: https://tinyurl.com/have-remote-pranswer
35+
HaveRemotePrAnswer,
36+
37+
/// [RTCSignalingState.closed][1] representation.
38+
///
39+
/// [1]: https://w3.org/TR/webrtc#dom-rtcsignalingstate-closed
40+
Closed,
41+
}
42+
43+
impl From<sys::SignalingState> for SignalingState {
44+
fn from(state: sys::SignalingState) -> Self {
45+
match state {
46+
sys::SignalingState::kStable => Self::Stable,
47+
sys::SignalingState::kHaveLocalOffer => Self::HaveLocalOffer,
48+
sys::SignalingState::kHaveLocalPrAnswer => Self::HaveLocalPrAnswer,
49+
sys::SignalingState::kHaveRemoteOffer => Self::HaveRemoteOffer,
50+
sys::SignalingState::kHaveRemotePrAnswer => {
51+
Self::HaveRemotePrAnswer
52+
}
53+
sys::SignalingState::kClosed => Self::Closed,
54+
_ => unreachable!(),
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)