| 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 | SignalNetworkRouteChanged(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 | SignalNetworkRouteChanged(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 | SignalNetworkRouteChanged(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 | SignalNetworkRouteChanged(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 | void RtpTransport::DemuxPacket(rtc::CopyOnWriteBuffer packet, |
| 184 | int64_t packet_time_us) { |
| 185 | webrtc::RtpPacketReceived parsed_packet( |
| 186 | &header_extension_map_, packet_time_us == -1 |
| 187 | ? Timestamp::MinusInfinity() |
| 188 | : Timestamp::Micros(packet_time_us)); |
| 189 | if (!parsed_packet.Parse(std::move(packet))) { |
| 190 | RTC_LOG(LS_ERROR) |
| 191 | << "Failed to parse the incoming RTP packet before demuxing. Drop it."; |
| 192 | return; |
| 193 | } |
| 194 |
| 195 | bool isUnresolved = true; |
| 196 | if (!rtp_demuxer_.OnRtpPacket(parsed_packet)) { |
| 197 | isUnresolved = true; |
| 198 | RTC_LOG(LS_WARNING) << "Failed to demux RTP packet: " |
| 199 | << RtpDemuxer::DescribePacket(parsed_packet); |
| 200 | } |
| 201 |
| 202 | ProcessRtpPacket(parsed_packet, isUnresolved); |
| 203 | } |
| 204 |
| 205 | bool RtpTransport::IsTransportWritable() { |
| 206 | auto rtcp_packet_transport = |
| 207 | rtcp_mux_enabled_ ? nullptr : rtcp_packet_transport_; |
| 208 | return rtp_packet_transport_ && rtp_packet_transport_->writable() && |
| 209 | (!rtcp_packet_transport || rtcp_packet_transport->writable()); |
| 210 | } |
| 211 |
| 212 | void RtpTransport::OnReadyToSend(rtc::PacketTransportInternal* transport) { |
| 213 | SetReadyToSend(transport == rtcp_packet_transport_, true); |
| 214 | } |
| 215 |
| 216 | void RtpTransport::OnNetworkRouteChanged( |
| 217 | absl::optional<rtc::NetworkRoute> network_route) { |
| 218 | SignalNetworkRouteChanged(network_route); |
| 219 | } |
| 220 |
| 221 | void RtpTransport::OnWritableState( |
| 222 | rtc::PacketTransportInternal* packet_transport) { |
| 223 | RTC_DCHECK(packet_transport == rtp_packet_transport_ || |
| 224 | packet_transport == rtcp_packet_transport_); |
| 225 | SignalWritableState(IsTransportWritable()); |
| 226 | } |
| 227 |
| 228 | void RtpTransport::OnSentPacket(rtc::PacketTransportInternal* packet_transport, |
| 229 | const rtc::SentPacket& sent_packet) { |
| 230 | RTC_DCHECK(packet_transport == rtp_packet_transport_ || |
| 231 | packet_transport == rtcp_packet_transport_); |
| 232 | SignalSentPacket(sent_packet); |
| 233 | } |
| 234 |
| 235 | void RtpTransport::OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet, |
| 236 | int64_t packet_time_us) { |
| 237 | DemuxPacket(packet, packet_time_us); |
| 238 | } |
| 239 |
| 240 | void RtpTransport::OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet, |
| 241 | int64_t packet_time_us) { |
| 242 | SignalRtcpPacketReceived(&packet, packet_time_us); |
| 243 | } |
| 244 |
| 245 | void RtpTransport::OnReadPacket(rtc::PacketTransportInternal* transport, |
| 246 | const char* data, |
| 247 | size_t len, |
| 248 | const int64_t& packet_time_us, |
| 249 | int flags) { |
| 250 | TRACE_EVENT0("webrtc", "RtpTransport::OnReadPacket"); |
| 251 |
| 252 | // When using RTCP multiplexing we might get RTCP packets on the RTP |
| 253 | // transport. We check the RTP payload type to determine if it is RTCP. |
| 254 | auto array_view = rtc::MakeArrayView(data, len); |
| 255 | cricket::RtpPacketType packet_type = cricket::InferRtpPacketType(array_view); |
| 256 | // Filter out the packet that is neither RTP nor RTCP. |
| 257 | if (packet_type == cricket::RtpPacketType::kUnknown) { |
| 258 | return; |
| 259 | } |
| 260 |
| 261 | // Protect ourselves against crazy data. |
| 262 | if (!cricket::IsValidRtpPacketSize(packet_type, len)) { |
| 263 | RTC_LOG(LS_ERROR) << "Dropping incoming " |
| 264 | << cricket::RtpPacketTypeToString(packet_type) |
| 265 | << " packet: wrong size=" << len; |
| 266 | return; |
| 267 | } |
| 268 |
| 269 | rtc::CopyOnWriteBuffer packet(data, len); |
| 270 | if (packet_type == cricket::RtpPacketType::kRtcp) { |
| 271 | OnRtcpPacketReceived(std::move(packet), packet_time_us); |
| 272 | } else { |
| 273 | OnRtpPacketReceived(std::move(packet), packet_time_us); |
| 274 | } |
| 275 | } |
| 276 |
| 277 | void RtpTransport::SetReadyToSend(bool rtcp, bool ready) { |
| 278 | if (rtcp) { |
| 279 | rtcp_ready_to_send_ = ready; |
| 280 | } else { |
| 281 | rtp_ready_to_send_ = ready; |
| 282 | } |
| 283 |
| 284 | MaybeSignalReadyToSend(); |
| 285 | } |
| 286 |
| 287 | void RtpTransport::MaybeSignalReadyToSend() { |
| 288 | bool ready_to_send = |
| 289 | rtp_ready_to_send_ && (rtcp_ready_to_send_ || rtcp_mux_enabled_); |
| 290 | if (ready_to_send != ready_to_send_) { |
| 291 | ready_to_send_ = ready_to_send; |
| 292 | SignalReadyToSend(ready_to_send); |
| 293 | } |
| 294 | } |
| 295 |
| 296 | } // namespace webrtc |
| 297 |