|
3 | 3 | pub mod rtp_receiver; |
4 | 4 | pub mod rtp_sender; |
5 | 5 |
|
| 6 | +use crate::error::Error; |
6 | 7 | use crate::media_stream::{TrackLocal, TrackRemote}; |
7 | 8 | use crate::peer_connection::{Interceptor, NoopInterceptor, PeerConnectionRef}; |
| 9 | +use crate::rtp_transceiver::rtp_receiver::RtpReceiverImpl; |
| 10 | +use crate::rtp_transceiver::rtp_sender::RtpSenderImpl; |
8 | 11 | use crate::runtime::Mutex; |
9 | 12 | use rtc::media_stream::MediaStreamId; |
10 | 13 | use rtc::rtp_transceiver::RTCRtpTransceiverId; |
11 | 14 | use rtc::rtp_transceiver::rtp_receiver::{RTCRtpContributingSource, RTCRtpSynchronizationSource}; |
12 | 15 | use rtc::rtp_transceiver::rtp_sender::{ |
13 | | - RTCRtpCapabilities, RTCRtpCodec, RTCRtpReceiveParameters, RTCRtpSendParameters, |
| 16 | + RTCRtpCapabilities, RTCRtpCodecParameters, RTCRtpReceiveParameters, RTCRtpSendParameters, |
14 | 17 | RTCSetParameterOptions, RtpCodecKind, |
15 | 18 | }; |
16 | 19 | pub use rtc::rtp_transceiver::{ |
@@ -48,13 +51,14 @@ pub trait RtpSender: Send + Sync + 'static { |
48 | 51 |
|
49 | 52 | #[async_trait::async_trait] |
50 | 53 | pub trait RtpTransceiver: Send + Sync + 'static { |
51 | | - async fn mid(&self) -> Option<String>; |
52 | | - async fn sender(&self) -> Option<Arc<dyn RtpSender>>; |
53 | | - async fn receiver(&self) -> Option<Arc<dyn RtpReceiver>>; |
54 | | - async fn direction(&self) -> RTCRtpTransceiverDirection; |
55 | | - async fn current_direction(&self) -> RTCRtpTransceiverDirection; |
| 54 | + async fn mid(&self) -> Result<Option<String>>; |
| 55 | + async fn sender(&self) -> Result<Option<Arc<dyn RtpSender>>>; |
| 56 | + async fn receiver(&self) -> Result<Option<Arc<dyn RtpReceiver>>>; |
| 57 | + async fn direction(&self) -> Result<RTCRtpTransceiverDirection>; |
| 58 | + async fn set_direction(&self, direction: RTCRtpTransceiverDirection) -> Result<()>; |
| 59 | + async fn current_direction(&self) -> Result<RTCRtpTransceiverDirection>; |
56 | 60 | async fn stop(&self) -> Result<()>; |
57 | | - async fn set_codec_preferences(&self, codecs: Vec<RTCRtpCodec>) -> Result<()>; |
| 61 | + async fn set_codec_preferences(&self, codecs: Vec<RTCRtpCodecParameters>) -> Result<()>; |
58 | 62 | } |
59 | 63 |
|
60 | 64 | /// Concrete async rtp transceiver implementation (generic over interceptor type). |
@@ -94,31 +98,103 @@ impl<I> RtpTransceiver for RtpTransceiverImpl<I> |
94 | 98 | where |
95 | 99 | I: Interceptor + 'static, |
96 | 100 | { |
97 | | - async fn mid(&self) -> Option<String> { |
98 | | - todo!() |
| 101 | + async fn mid(&self) -> Result<Option<String>> { |
| 102 | + let mut peer_connection = self.inner.core.lock().await; |
| 103 | + |
| 104 | + Ok(peer_connection |
| 105 | + .rtp_transceiver(self.id) |
| 106 | + .ok_or(Error::ErrTPTransceiverNotExisted)? |
| 107 | + .mid() |
| 108 | + .clone()) |
99 | 109 | } |
100 | 110 |
|
101 | | - async fn sender(&self) -> Option<Arc<dyn RtpSender>> { |
102 | | - todo!() |
| 111 | + async fn sender(&self) -> Result<Option<Arc<dyn RtpSender>>> { |
| 112 | + let mut peer_connection = self.inner.core.lock().await; |
| 113 | + |
| 114 | + let transceiver = peer_connection |
| 115 | + .rtp_transceiver(self.id) |
| 116 | + .ok_or(Error::ErrTPTransceiverNotExisted)?; |
| 117 | + |
| 118 | + let mut sender = self.sender.lock().await; |
| 119 | + if let Some(sender_id) = transceiver.sender() { |
| 120 | + if sender.is_none() { |
| 121 | + *sender = Some(Arc::new(RtpSenderImpl::new( |
| 122 | + sender_id, |
| 123 | + Arc::clone(&self.inner), |
| 124 | + ))); |
| 125 | + } |
| 126 | + } else { |
| 127 | + *sender = None; |
| 128 | + } |
| 129 | + |
| 130 | + Ok(sender.clone()) |
103 | 131 | } |
104 | 132 |
|
105 | | - async fn receiver(&self) -> Option<Arc<dyn RtpReceiver>> { |
106 | | - todo!() |
| 133 | + async fn receiver(&self) -> Result<Option<Arc<dyn RtpReceiver>>> { |
| 134 | + let mut peer_connection = self.inner.core.lock().await; |
| 135 | + |
| 136 | + let transceiver = peer_connection |
| 137 | + .rtp_transceiver(self.id) |
| 138 | + .ok_or(Error::ErrTPTransceiverNotExisted)?; |
| 139 | + |
| 140 | + let mut receiver = self.receiver.lock().await; |
| 141 | + if let Some(receiver_id) = transceiver.receiver() { |
| 142 | + if receiver.is_none() { |
| 143 | + *receiver = Some(Arc::new(RtpReceiverImpl::new( |
| 144 | + receiver_id, |
| 145 | + Arc::clone(&self.inner), |
| 146 | + ))); |
| 147 | + } |
| 148 | + } else { |
| 149 | + *receiver = None; |
| 150 | + } |
| 151 | + Ok(receiver.clone()) |
107 | 152 | } |
108 | 153 |
|
109 | | - async fn direction(&self) -> RTCRtpTransceiverDirection { |
110 | | - todo!() |
| 154 | + async fn direction(&self) -> Result<RTCRtpTransceiverDirection> { |
| 155 | + let mut peer_connection = self.inner.core.lock().await; |
| 156 | + |
| 157 | + Ok(peer_connection |
| 158 | + .rtp_transceiver(self.id) |
| 159 | + .ok_or(Error::ErrTPTransceiverNotExisted)? |
| 160 | + .direction()) |
111 | 161 | } |
112 | 162 |
|
113 | | - async fn current_direction(&self) -> RTCRtpTransceiverDirection { |
114 | | - todo!() |
| 163 | + async fn set_direction(&self, direction: RTCRtpTransceiverDirection) -> Result<()> { |
| 164 | + let mut peer_connection = self.inner.core.lock().await; |
| 165 | + |
| 166 | + peer_connection |
| 167 | + .rtp_transceiver(self.id) |
| 168 | + .ok_or(Error::ErrTPTransceiverNotExisted)? |
| 169 | + .set_direction(direction); |
| 170 | + |
| 171 | + Ok(()) |
| 172 | + } |
| 173 | + |
| 174 | + async fn current_direction(&self) -> Result<RTCRtpTransceiverDirection> { |
| 175 | + let mut peer_connection = self.inner.core.lock().await; |
| 176 | + |
| 177 | + Ok(peer_connection |
| 178 | + .rtp_transceiver(self.id) |
| 179 | + .ok_or(Error::ErrTPTransceiverNotExisted)? |
| 180 | + .current_direction()) |
115 | 181 | } |
116 | 182 |
|
117 | 183 | async fn stop(&self) -> Result<()> { |
118 | | - todo!() |
| 184 | + let mut peer_connection = self.inner.core.lock().await; |
| 185 | + |
| 186 | + peer_connection |
| 187 | + .rtp_transceiver(self.id) |
| 188 | + .ok_or(Error::ErrTPTransceiverNotExisted)? |
| 189 | + .stop() |
119 | 190 | } |
120 | 191 |
|
121 | | - async fn set_codec_preferences(&self, _codecs: Vec<RTCRtpCodec>) -> Result<()> { |
122 | | - todo!() |
| 192 | + async fn set_codec_preferences(&self, codecs: Vec<RTCRtpCodecParameters>) -> Result<()> { |
| 193 | + let mut peer_connection = self.inner.core.lock().await; |
| 194 | + |
| 195 | + peer_connection |
| 196 | + .rtp_transceiver(self.id) |
| 197 | + .ok_or(Error::ErrTPTransceiverNotExisted)? |
| 198 | + .set_codec_preferences(codecs) |
123 | 199 | } |
124 | 200 | } |
0 commit comments