pub async fn replace_track(
        &self,
        track: Option<Arc<dyn TrackLocal + Send + Sync>>,
    ) -> Result<()> {
        if let Some(t) = &track {
            let tr = self.rtp_transceiver.lock().await;
            if let Some(r) = &*tr {
                if let Some(r) = r.upgrade() {
                    if r.kind != t.kind() {
                        return Err(Error::ErrRTPSenderNewTrackHasIncorrectKind);
                    }
                } else {
                    //TODO: what about None arc?
                }
            } else {
                //TODO: what about None tr?
            }
        }

        if self.has_sent().await {
            let t = {
                let t = self.track.lock().await;
                t.clone()
            };
            if let Some(t) = t {
                let context = self.context.lock().await;
                t.unbind(&*context).await?;
            }
        }

        if !self.has_sent().await || track.is_none() {
            let mut t = self.track.lock().await;
            *t = track;
            return Ok(());
        }

        let context = {
            let context = self.context.lock().await;
            context.clone()
        };

        let result = if let Some(t) = &track {
            let new_context = TrackLocalContext {
                id: context.id.clone(),
                params: self
                    .media_engine
                    .get_rtp_parameters_by_kind(t.kind(), &[RTCRtpTransceiverDirection::Sendonly])
                    .await,
                ssrc: context.ssrc,
                write_stream: context.write_stream.clone(),
            };

            t.bind(&new_context).await
        } else {
            Err(Error::ErrRTPSenderTrackNil)
        };

        match result {
            Err(err) => {
                // Re-bind the original track
                let track = self.track.lock().await;
                if let Some(t) = &*track {
                    t.bind(&context).await?;
                }

                Err(err)
            }
            Ok(codec) => {
                // Codec has changed
                if self.payload_type != codec.payload_type {
                    let mut context = self.context.lock().await;
                    context.params.codecs = vec![codec];
                }

                {
                    let mut t = self.track.lock().await;
                    *t = track;
                }

                Ok(())
            }
        }
    }
/// replace_track replaces the track currently being used as the sender's source with a new TrackLocal.
/// The new track must be of the same media kind (audio, video, etc) and switching the track should not
/// require negotiation.
replace_track replaces the track currently being used as the sender's source with a new TrackLocal.
The new track must be of the same media kind (audio, video, etc) and switching the track should not
require negotiation.
replace_track replaces the track currently being used as the sender ' s source with a new TrackLocal . The new track must be of the same media kind ( audio video etc ) and switching the track should not require negotiation .
pub async fn replace_track(
        &self,
        track: Option<Arc<dyn TrackLocal + Send + Sync>>,
    ) -> Result<()> {
        if let Some(t) = &track {
            let tr = self.rtp_transceiver.lock().await;
            if let Some(r) = &*tr {
                if let Some(r) = r.upgrade() {
                    if r.kind != t.kind() {
                        return Err(Error::ErrRTPSenderNewTrackHasIncorrectKind);
                    }
                } else {
                    //TODO: what about None arc?
                }
            } else {
                //TODO: what about None tr?
            }
        }
        if self.has_sent().await {
            let t = {
                let t = self.track.lock().await;
                t.clone()
            };
            if let Some(t) = t {
                let context = self.context.lock().await;
                t.unbind(&*context).await?;
            }
        }
        if !self.has_sent().await || track.is_none() {
            let mut t = self.track.lock().await;
            *t = track;
            return Ok(());
        }
        let context = {
            let context = self.context.lock().await;
            context.clone()
        };
        let result = if let Some(t) = &track {
            let new_context = TrackLocalContext {
                id: context.id.clone(),
                params: self
                    .media_engine
                    .get_rtp_parameters_by_kind(t.kind(), &[RTCRtpTransceiverDirection::Sendonly])
                    .await,
                ssrc: context.ssrc,
                write_stream: context.write_stream.clone(),
            };
            t.bind(&new_context).await
        } else {
            Err(Error::ErrRTPSenderTrackNil)
        };
        match result {
            Err(err) => {
                // Re-bind the original track
                let track = self.track.lock().await;
                if let Some(t) = &*track {
                    t.bind(&context).await?;
                }
                Err(err)
            }
            Ok(codec) => {
                // Codec has changed
                if self.payload_type != codec.payload_type {
                    let mut context = self.context.lock().await;
                    context.params.codecs = vec![codec];
                }
                {
                    let mut t = self.track.lock().await;
                    *t = track;
                }
                Ok(())
            }
        }
    }
pub async fn replace_track ( & self , track : Option < Arc < dyn TrackLocal + Send + Sync > > , ) -> Result < ( ) > { if let Some ( t ) = & track { let tr = self . rtp_transceiver . lock ( ) . await ; if let Some ( r ) = & * tr { if let Some ( r ) = r . upgrade ( ) { if r . kind != t . kind ( ) { return Err ( Error :: ErrRTPSenderNewTrackHasIncorrectKind ) ; } } else { //TODO: what about None arc? } } else { //TODO: what about None tr? } } if self . has_sent ( ) . await { let t = { let t = self . track . lock ( ) . await ; t . clone ( ) } ; if let Some ( t ) = t { let context = self . context . lock ( ) . await ; t . unbind ( & * context ) . await ? ; } } if ! self . has_sent ( ) . await || track . is_none ( ) { let mut t = self . track . lock ( ) . await ; * t = track ; return Ok ( ( ) ) ; } let context = { let context = self . context . lock ( ) . await ; context . clone ( ) } ; let result = if let Some ( t ) = & track { let new_context = TrackLocalContext { id : context . id . clone ( ) , params : self . media_engine . get_rtp_parameters_by_kind ( t . kind ( ) , & [ RTCRtpTransceiverDirection :: Sendonly ] ) . await , ssrc : context . ssrc , write_stream : context . write_stream . clone ( ) , } ; t . bind ( & new_context ) . await } else { Err ( Error :: ErrRTPSenderTrackNil ) } ; match result { Err ( err ) => { // Re-bind the original track let track = self . track . lock ( ) . await ; if let Some ( t ) = & * track { t . bind ( & context ) . await ? ; } Err ( err ) } Ok ( codec ) => { // Codec has changed if self . payload_type != codec . payload_type { let mut context = self . context . lock ( ) . await ; context . params . codecs = vec ! [ codec ] ; } { let mut t = self . track . lock ( ) . await ; * t = track ; } Ok ( ( ) ) } } }
replace_track replaces the track currently being used as the sender's source with a new TrackLocal.
replace_track replaces the track currently being used as the sender ' s source with a new TrackLocal .
&self
track
Option<Arc<dyn TrackLocal + Send + Sync>>