| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "pc/rtp_transport.h" |
| 12 |
| 13 | #include <errno.h> |
| 14 |
| 15 | #include <cstdint> |
| 16 | #include <utility> |
| 17 |
| 18 | #include "absl/strings/string_view.h" |
| 19 | #include "api/array_view.h" |
| 20 | #include "api/units/timestamp.h" |
| 21 | #include "media/base/rtp_utils.h" |
| 22 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 23 | #include "rtc_base/checks.h" |
| 24 | #include "rtc_base/copy_on_write_buffer.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 | #include "rtc_base/trace_event.h" |
| 27 |
| 28 | namespace webrtc { |
| 29 |
| 30 | void RtpTransport::SetRtcpMuxEnabled(bool enable) { |
| 31 | rtcp_mux_enabled_ = enable; |
| 32 | MaybeSignalReadyToSend(); |
| 33 | } |
| 34 |
| 35 | const std::string& RtpTransport::transport_name() const { |
| 36 | return rtp_packet_transport_->transport_name(); |
| 37 | } |
| 38 |
| 39 | int RtpTransport::SetRtpOption(rtc::Socket::Option opt, int value) { |
| 40 | return rtp_packet_transport_->SetOption(opt, value); |
| 41 | } |
| 42 |
| 43 | int RtpTransport::SetRtcpOption(rtc::Socket::Option opt, int value) { |
| 44 | if (rtcp_packet_transport_) { |
| 45 | return rtcp_packet_transport_->SetOption(opt, value); |
| 46 | } |
| 47 | return -1; |
| 48 | } |
| 49 |
| 50 | void RtpTransport::SetRtpPacketTransport( |
| 51 | rtc::PacketTransportInternal* new_packet_transport) { |
| 52 | if (new_packet_transport == rtp_packet_transport_) { |
| 53 | return; |
| 54 | } |
| 55 | if (rtp_packet_transport_) { |
| 56 | rtp_packet_transport_->SignalReadyToSend.disconnect(this); |
| 57 | rtp_packet_transport_->SignalReadPacket.disconnect(this); |
| 58 | rtp_packet_transport_->SignalNetworkRouteChanged.disconnect(this); |
| 59 | rtp_packet_transport_->SignalWritableState.disconnect(this); |
| 60 | rtp_packet_transport_->SignalSentPacket.disconnect(this); |
| 61 | // Reset the network route of the old transport. |
| 62 | SendNetworkRouteChanged(absl::optional<rtc::NetworkRoute>()); |
| 63 | } |
| 64 | if (new_packet_transport) { |
| 65 | new_packet_transport->SignalReadyToSend.connect( |
| 66 | this, &RtpTransport::OnReadyToSend); |
| 67 | new_packet_transport->SignalReadPacket.connect(this, |
| 68 | &RtpTransport::OnReadPacket); |
| 69 | new_packet_transport->SignalNetworkRouteChanged.connect( |
| 70 | this, &RtpTransport::OnNetworkRouteChanged); |
| 71 | new_packet_transport->SignalWritableState.connect( |
| 72 | this, &RtpTransport::OnWritableState); |
| 73 | new_packet_transport->SignalSentPacket.connect(this, |
| 74 | &RtpTransport::OnSentPacket); |
| 75 | // Set the network route for the new transport. |
| 76 | SendNetworkRouteChanged(new_packet_transport->network_route()); |
| 77 | } |
| 78 |
| 79 | rtp_packet_transport_ = new_packet_transport; |
| 80 | // Assumes the transport is ready to send if it is writable. If we are wrong, |
| 81 | // ready to send will be updated the next time we try to send. |
| 82 | SetReadyToSend(false, |
| 83 | rtp_packet_transport_ && rtp_packet_transport_->writable()); |
| 84 | } |
| 85 |
| 86 | void RtpTransport::SetRtcpPacketTransport( |
| 87 | rtc::PacketTransportInternal* new_packet_transport) { |
| 88 | if (new_packet_transport == rtcp_packet_transport_) { |
| 89 | return; |
| 90 | } |
| 91 | if (rtcp_packet_transport_) { |
| 92 | rtcp_packet_transport_->SignalReadyToSend.disconnect(this); |
| 93 | rtcp_packet_transport_->SignalReadPacket.disconnect(this); |
| 94 | rtcp_packet_transport_->SignalNetworkRouteChanged.disconnect(this); |
| 95 | rtcp_packet_transport_->SignalWritableState.disconnect(this); |
| 96 | rtcp_packet_transport_->SignalSentPacket.disconnect(this); |
| 97 | // Reset the network route of the old transport. |
| 98 | SendNetworkRouteChanged(absl::optional<rtc::NetworkRoute>()); |
| 99 | } |
| 100 | if (new_packet_transport) { |
| 101 | new_packet_transport->SignalReadyToSend.connect( |
| 102 | this, &RtpTransport::OnReadyToSend); |
| 103 | new_packet_transport->SignalReadPacket.connect(this, |
| 104 | &RtpTransport::OnReadPacket); |
| 105 | new_packet_transport->SignalNetworkRouteChanged.connect( |
| 106 | this, &RtpTransport::OnNetworkRouteChanged); |
| 107 | new_packet_transport->SignalWritableState.connect( |
| 108 | this, &RtpTransport::OnWritableState); |
| 109 | new_packet_transport->SignalSentPacket.connect(this, |
| 110 | &RtpTransport::OnSentPacket); |
| 111 | // Set the network route for the new transport. |
| 112 | SendNetworkRouteChanged(new_packet_transport->network_route()); |
| 113 | } |
| 114 | rtcp_packet_transport_ = new_packet_transport; |
| 115 |
| 116 | // Assumes the transport is ready to send if it is writable. If we are wrong, |
| 117 | // ready to send will be updated the next time we try to send. |
| 118 | SetReadyToSend(true, |
| 119 | rtcp_packet_transport_ && rtcp_packet_transport_->writable()); |
| 120 | } |
| 121 |
| 122 | bool RtpTransport::IsWritable(bool rtcp) const { |
| 123 | rtc::PacketTransportInternal* transport = rtcp && !rtcp_mux_enabled_ |
| 124 | ? rtcp_packet_transport_ |
| 125 | : rtp_packet_transport_; |
| 126 | return transport && transport->writable(); |
| 127 | } |
| 128 |
| 129 | bool RtpTransport::SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 130 | const rtc::PacketOptions& options, |
| 131 | int flags) { |
| 132 | return SendPacket(false, packet, options, flags); |
| 133 | } |
| 134 |
| 135 | bool RtpTransport::SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 136 | const rtc::PacketOptions& options, |
| 137 | int flags) { |
| 138 | return SendPacket(true, packet, options, flags); |
| 139 | } |
| 140 |
| 141 | bool RtpTransport::SendPacket(bool rtcp, |
| 142 | rtc::CopyOnWriteBuffer* packet, |
| 143 | const rtc::PacketOptions& options, |
| 144 | int flags) { |
| 145 | rtc::PacketTransportInternal* transport = rtcp && !rtcp_mux_enabled_ |
| 146 | ? rtcp_packet_transport_ |
| 147 | : rtp_packet_transport_; |
| 148 | int ret = transport->SendPacket(packet->cdata<char>(), packet->size(), |
| 149 | options, flags); |
| 150 | if (ret != static_cast<int>(packet->size())) { |
| 151 | if (transport->GetError() == ENOTCONN) { |
| 152 | RTC_LOG(LS_WARNING) << "Got ENOTCONN from transport."; |
| 153 | SetReadyToSend(rtcp, false); |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 |
| 160 | void RtpTransport::UpdateRtpHeaderExtensionMap( |
| 161 | const cricket::RtpHeaderExtensions& header_extensions) { |
| 162 | header_extension_map_ = RtpHeaderExtensionMap(header_extensions); |
| 163 | } |
| 164 |
| 165 | bool RtpTransport::RegisterRtpDemuxerSink(const RtpDemuxerCriteria& criteria, |
| 166 | RtpPacketSinkInterface* sink) { |
| 167 | rtp_demuxer_.RemoveSink(sink); |
| 168 | if (!rtp_demuxer_.AddSink(criteria, sink)) { |
| 169 | RTC_LOG(LS_ERROR) << "Failed to register the sink for RTP demuxer."; |
| 170 | return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 |
| 175 | bool RtpTransport::UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) { |
| 176 | if (!rtp_demuxer_.RemoveSink(sink)) { |
| 177 | RTC_LOG(LS_ERROR) << "Failed to unregister the sink for RTP demuxer."; |
| 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 |
| 183 | flat_set<uint32_t> RtpTransport::GetSsrcsForSink(RtpPacketSinkInterface* sink) { |
| 184 | return rtp_demuxer_.GetSsrcsForSink(sink); |
| 185 | } |
| 186 |
| 187 | void RtpTransport::DemuxPacket(rtc::CopyOnWriteBuffer packet, |
| 188 | int64_t packet_time_us) { |
| 189 | RtpPacketReceived parsed_packet(&header_extension_map_, |
| 190 | packet_time_us == -1 |
| 191 | ? Timestamp::MinusInfinity() |
| 192 | : Timestamp::Micros(packet_time_us)); |
| 193 | if (!parsed_packet.Parse(std::move(packet))) { |
| 194 | RTC_LOG(LS_ERROR) |
| 195 | << "Failed to parse the incoming RTP packet before demuxing. Drop it."; |
| 196 | return; |
| 197 | } |
| 198 |
| 199 | bool isUnresolved = true; |
| 200 | if (!rtp_demuxer_.OnRtpPacket(parsed_packet)) { |
| 201 | isUnresolved = true; |
| 202 | RTC_LOG(LS_VERBOSE) << "Failed to demux RTP packet: " |
| 203 | << RtpDemuxer::DescribePacket(parsed_packet); |
| 204 | NotifyUnDemuxableRtpPacketReceived(parsed_packet); |
| 205 | } |
| 206 | ProcessRtpPacket(parsed_packet, isUnresolved); |
| 207 | } |
| 208 |
| 209 | bool RtpTransport::IsTransportWritable() { |
| 210 | auto rtcp_packet_transport = |
| 211 | rtcp_mux_enabled_ ? nullptr : rtcp_packet_transport_; |
| 212 | return rtp_packet_transport_ && rtp_packet_transport_->writable() && |
| 213 | (!rtcp_packet_transport || rtcp_packet_transport->writable()); |
| 214 | } |
| 215 |
| 216 | void RtpTransport::OnReadyToSend(rtc::PacketTransportInternal* transport) { |
| 217 | SetReadyToSend(transport == rtcp_packet_transport_, true); |
| 218 | } |
| 219 |
| 220 | void RtpTransport::OnNetworkRouteChanged( |
| 221 | absl::optional<rtc::NetworkRoute> network_route) { |
| 222 | SendNetworkRouteChanged(network_route); |
| 223 | } |
| 224 |
| 225 | void RtpTransport::OnWritableState( |
| 226 | rtc::PacketTransportInternal* packet_transport) { |
| 227 | RTC_DCHECK(packet_transport == rtp_packet_transport_ || |
| 228 | packet_transport == rtcp_packet_transport_); |
| 229 | SendWritableState(IsTransportWritable()); |
| 230 | } |
| 231 |
| 232 | void RtpTransport::OnSentPacket(rtc::PacketTransportInternal* packet_transport, |
| 233 | const rtc::SentPacket& sent_packet) { |
| 234 | RTC_DCHECK(packet_transport == rtp_packet_transport_ || |
| 235 | packet_transport == rtcp_packet_transport_); |
| 236 | if (processing_sent_packet_) { |
| 237 | TaskQueueBase::Current()->PostTask(SafeTask( |
| 238 | safety_.flag(), [this, sent_packet] { SendSentPacket(sent_packet); })); |
| 239 | return; |
| 240 | } |
| 241 | processing_sent_packet_ = true; |
| 242 | SendSentPacket(sent_packet); |
| 243 | processing_sent_packet_ = false; |
| 244 | } |
| 245 |
| 246 | void RtpTransport::OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet, |
| 247 | int64_t packet_time_us) { |
| 248 | DemuxPacket(packet, packet_time_us); |
| 249 | } |
| 250 |
| 251 | void RtpTransport::OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet, |
| 252 | int64_t packet_time_us) { |
| 253 | SendRtcpPacketReceived(&packet, packet_time_us); |
| 254 | } |
| 255 |
| 256 | void RtpTransport::OnReadPacket(rtc::PacketTransportInternal* transport, |
| 257 | const char* data, |
| 258 | size_t len, |
| 259 | const int64_t& packet_time_us, |
| 260 | int flags) { |
| 261 | TRACE_EVENT0("webrtc", "RtpTransport::OnReadPacket"); |
| 262 |
| 263 | // When using RTCP multiplexing we might get RTCP packets on the RTP |
| 264 | // transport. We check the RTP payload type to determine if it is RTCP. |
| 265 | auto array_view = rtc::MakeArrayView(data, len); |
| 266 | cricket::RtpPacketType packet_type = cricket::InferRtpPacketType(array_view); |
| 267 | // Filter out the packet that is neither RTP nor RTCP. |
| 268 | if (packet_type == cricket::RtpPacketType::kUnknown) { |
| 269 | return; |
| 270 | } |
| 271 |
| 272 | // Protect ourselves against crazy data. |
| 273 | if (!cricket::IsValidRtpPacketSize(packet_type, len)) { |
| 274 | RTC_LOG(LS_ERROR) << "Dropping incoming " |
| 275 | << cricket::RtpPacketTypeToString(packet_type) |
| 276 | << " packet: wrong size=" << len; |
| 277 | return; |
| 278 | } |
| 279 |
| 280 | rtc::CopyOnWriteBuffer packet(data, len); |
| 281 | if (packet_type == cricket::RtpPacketType::kRtcp) { |
| 282 | OnRtcpPacketReceived(std::move(packet), packet_time_us); |
| 283 | } else { |
| 284 | OnRtpPacketReceived(std::move(packet), packet_time_us); |
| 285 | } |
| 286 | } |
| 287 |
| 288 | void RtpTransport::SetReadyToSend(bool rtcp, bool ready) { |
| 289 | if (rtcp) { |
| 290 | rtcp_ready_to_send_ = ready; |
| 291 | } else { |
| 292 | rtp_ready_to_send_ = ready; |
| 293 | } |
| 294 |
| 295 | MaybeSignalReadyToSend(); |
| 296 | } |
| 297 |
| 298 | void RtpTransport::MaybeSignalReadyToSend() { |
| 299 | bool ready_to_send = |
| 300 | rtp_ready_to_send_ && (rtcp_ready_to_send_ || rtcp_mux_enabled_); |
| 301 | if (ready_to_send != ready_to_send_) { |
| 302 | if (processing_ready_to_send_) { |
| 303 | // Delay ReadyToSend processing until current operation is finished. |
| 304 | // Note that this may not cause a signal, since ready_to_send may |
| 305 | // have a new value by the time this executes. |
| 306 | TaskQueueBase::Current()->PostTask( |
| 307 | SafeTask(safety_.flag(), [this] { MaybeSignalReadyToSend(); })); |
| 308 | return; |
| 309 | } |
| 310 | ready_to_send_ = ready_to_send; |
| 311 | processing_ready_to_send_ = true; |
| 312 | SendReadyToSend(ready_to_send); |
| 313 | processing_ready_to_send_ = false; |
| 314 | } |
| 315 | } |
| 316 |
| 317 | } // namespace webrtc |
| 318 |