Skip to content

Commit

Permalink
refactoring Candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Jan 16, 2024
1 parent fab3fcd commit 5117761
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
24 changes: 7 additions & 17 deletions src/server/endpoint/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,25 @@ pub struct Candidate {
remote_conn_cred: ConnectionCredentials,
local_conn_cred: ConnectionCredentials,
remote_description: RTCSessionDescription,
local_description: Option<RTCSessionDescription>,
local_description: RTCSessionDescription,
}

impl Candidate {
pub(crate) fn new(
session_id: SessionId,
endpoint_id: EndpointId,
certificates: &[RTCCertificate],
remote_conn_cred: ConnectionCredentials,
local_conn_cred: ConnectionCredentials,
remote_description: RTCSessionDescription,
local_description: RTCSessionDescription,
) -> Self {
Self {
session_id,
endpoint_id,
local_conn_cred: ConnectionCredentials::new(
certificates,
remote_conn_cred.dtls_params.role,
),
local_conn_cred,
remote_conn_cred,
remote_description,
local_description: None,
local_description,
}
}

Expand Down Expand Up @@ -246,19 +244,11 @@ impl Candidate {
)
}

pub(crate) fn set_remote_description(&mut self, remote_description: &RTCSessionDescription) {
self.remote_description = remote_description.clone();
}

pub(crate) fn remote_description(&self) -> &RTCSessionDescription {
&self.remote_description
}

pub(crate) fn set_local_description(&mut self, local_description: &RTCSessionDescription) {
self.local_description = Some(local_description.clone());
}

pub(crate) fn local_description(&mut self) -> Option<&RTCSessionDescription> {
self.local_description.as_ref()
pub(crate) fn local_description(&self) -> &RTCSessionDescription {
&self.local_description
}
}
42 changes: 22 additions & 20 deletions src/server/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::rc::Rc;
pub mod description;

use crate::server::certificate::RTCCertificate;
use crate::server::endpoint::candidate::{DTLSRole, DEFAULT_DTLS_ROLE_OFFER};
use crate::server::endpoint::candidate::{DTLSRole, RTCIceParameters, DEFAULT_DTLS_ROLE_OFFER};
use crate::server::endpoint::{
candidate::{Candidate, ConnectionCredentials},
Endpoint,
Expand Down Expand Up @@ -82,27 +82,33 @@ impl Session {
let remote_conn_cred = ConnectionCredentials::from_sdp(&parsed)?;
offer.parsed = Some(parsed);

let mut candidate = Candidate::new(
let local_conn_cred =
ConnectionCredentials::new(&self.certificates, remote_conn_cred.dtls_params.role);

let answer = self.create_pending_answer(&offer, &local_conn_cred.ice_params)?;

self.add_candidate(Rc::new(Candidate::new(
self.session_id,
endpoint_id,
&self.certificates,
remote_conn_cred,
local_conn_cred,
offer,
);

let answer = self.create_pending_answer(&candidate)?;
candidate.set_local_description(&answer);

self.add_candidate(Rc::new(candidate));
answer.clone(),
)));

Ok(answer)
}

pub fn create_pending_answer(&self, candidate: &Candidate) -> Result<RTCSessionDescription> {
pub fn create_pending_answer(
&self,
remote_description: &RTCSessionDescription,
local_ice_params: &RTCIceParameters,
) -> Result<RTCSessionDescription> {
let use_identity = false; //TODO: self.config.idp_login_url.is_some();
let local_transceivers = vec![]; //TODO: self.get_transceivers();
let mut d = self.generate_matched_sdp(
candidate,
remote_description,
local_ice_params,
&local_transceivers,
use_identity,
false, /*includeUnmatched */
Expand All @@ -127,14 +133,12 @@ impl Session {
/// This is used for the initial call for CreateOffer
pub(crate) fn generate_unmatched_sdp(
&self,
candidate: &Candidate,
local_ice_params: &RTCIceParameters,
local_transceivers: &[RTCRtpTransceiver],
use_identity: bool,
) -> Result<SessionDescription> {
let d = SessionDescription::new_jsep_session_description(use_identity);

let ice_params = candidate.get_local_parameters();

let mut media_sections = vec![];

for t in local_transceivers.iter() {
Expand Down Expand Up @@ -172,7 +176,7 @@ impl Session {
d,
&dtls_fingerprints,
&self.local_addr,
ice_params,
local_ice_params,
DEFAULT_DTLS_ROLE_OFFER.to_connection_role(),
&media_sections,
true,
Expand All @@ -183,17 +187,15 @@ impl Session {
/// this is used everytime we have a remote_description
pub(crate) fn generate_matched_sdp(
&self,
candidate: &Candidate,
remote_description: &RTCSessionDescription,
local_ice_params: &RTCIceParameters,
local_transceivers: &[RTCRtpTransceiver],
use_identity: bool,
include_unmatched: bool,
connection_role: ConnectionRole,
) -> Result<SessionDescription> {
let d = SessionDescription::new_jsep_session_description(use_identity);

let ice_params = candidate.get_local_parameters();

let remote_description = candidate.remote_description();
let mut media_sections = vec![];
let mut already_have_application_media_section = false;
if let Some(parsed) = remote_description.parsed.as_ref() {
Expand Down Expand Up @@ -271,7 +273,7 @@ impl Session {
d,
&dtls_fingerprints,
&self.local_addr,
ice_params,
local_ice_params,
connection_role,
&media_sections,
true,
Expand Down

0 comments on commit 5117761

Please sign in to comment.