zstein | d48dbda | 2017-04-04 19:45:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/rtptransport.h" |
zstein | d48dbda | 2017-04-04 19:45:57 -0700 | [diff] [blame] | 12 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "media/base/rtputils.h" |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 16 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 17 | #include "p2p/base/p2pconstants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "p2p/base/packettransportinterface.h" |
| 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/copyonwritebuffer.h" |
| 21 | #include "rtc_base/trace_event.h" |
zstein | d48dbda | 2017-04-04 19:45:57 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 25 | void RtpTransport::SetRtcpMuxEnabled(bool enable) { |
| 26 | rtcp_mux_enabled_ = enable; |
| 27 | MaybeSignalReadyToSend(); |
zstein | d9ce764 | 2017-04-10 16:17:57 -0700 | [diff] [blame] | 28 | } |
| 29 | |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 30 | void RtpTransport::SetRtpPacketTransport( |
| 31 | rtc::PacketTransportInternal* new_packet_transport) { |
| 32 | if (new_packet_transport == rtp_packet_transport_) { |
| 33 | return; |
| 34 | } |
| 35 | if (rtp_packet_transport_) { |
| 36 | rtp_packet_transport_->SignalReadyToSend.disconnect(this); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 37 | rtp_packet_transport_->SignalReadPacket.disconnect(this); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 38 | rtp_packet_transport_->SignalNetworkRouteChanged.disconnect(this); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 39 | rtp_packet_transport_->SignalWritableState.disconnect(this); |
| 40 | rtp_packet_transport_->SignalSentPacket.disconnect(this); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 41 | // Reset the network route of the old transport. |
| 42 | SignalNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute>()); |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 43 | } |
| 44 | if (new_packet_transport) { |
| 45 | new_packet_transport->SignalReadyToSend.connect( |
| 46 | this, &RtpTransport::OnReadyToSend); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 47 | new_packet_transport->SignalReadPacket.connect(this, |
| 48 | &RtpTransport::OnReadPacket); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 49 | new_packet_transport->SignalNetworkRouteChanged.connect( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 50 | this, &RtpTransport::OnNetworkRouteChanged); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 51 | new_packet_transport->SignalWritableState.connect( |
| 52 | this, &RtpTransport::OnWritableState); |
| 53 | new_packet_transport->SignalSentPacket.connect(this, |
| 54 | &RtpTransport::OnSentPacket); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 55 | // Set the network route for the new transport. |
| 56 | SignalNetworkRouteChanged(new_packet_transport->network_route()); |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 57 | } |
Zhi Huang | 8c316c1 | 2017-11-13 21:13:45 +0000 | [diff] [blame] | 58 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 59 | rtp_packet_transport_ = new_packet_transport; |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 60 | // Assumes the transport is ready to send if it is writable. If we are wrong, |
| 61 | // ready to send will be updated the next time we try to send. |
| 62 | SetReadyToSend(false, |
| 63 | rtp_packet_transport_ && rtp_packet_transport_->writable()); |
| 64 | } |
| 65 | |
| 66 | void RtpTransport::SetRtcpPacketTransport( |
| 67 | rtc::PacketTransportInternal* new_packet_transport) { |
| 68 | if (new_packet_transport == rtcp_packet_transport_) { |
| 69 | return; |
| 70 | } |
| 71 | if (rtcp_packet_transport_) { |
| 72 | rtcp_packet_transport_->SignalReadyToSend.disconnect(this); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 73 | rtcp_packet_transport_->SignalReadPacket.disconnect(this); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 74 | rtcp_packet_transport_->SignalNetworkRouteChanged.disconnect(this); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 75 | rtcp_packet_transport_->SignalWritableState.disconnect(this); |
| 76 | rtcp_packet_transport_->SignalSentPacket.disconnect(this); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 77 | // Reset the network route of the old transport. |
| 78 | SignalNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute>()); |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 79 | } |
| 80 | if (new_packet_transport) { |
| 81 | new_packet_transport->SignalReadyToSend.connect( |
| 82 | this, &RtpTransport::OnReadyToSend); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 83 | new_packet_transport->SignalReadPacket.connect(this, |
| 84 | &RtpTransport::OnReadPacket); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 85 | new_packet_transport->SignalNetworkRouteChanged.connect( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 86 | this, &RtpTransport::OnNetworkRouteChanged); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 87 | new_packet_transport->SignalWritableState.connect( |
| 88 | this, &RtpTransport::OnWritableState); |
| 89 | new_packet_transport->SignalSentPacket.connect(this, |
| 90 | &RtpTransport::OnSentPacket); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 91 | // Set the network route for the new transport. |
| 92 | SignalNetworkRouteChanged(new_packet_transport->network_route()); |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 93 | } |
| 94 | rtcp_packet_transport_ = new_packet_transport; |
| 95 | |
| 96 | // Assumes the transport is ready to send if it is writable. If we are wrong, |
| 97 | // ready to send will be updated the next time we try to send. |
| 98 | SetReadyToSend(true, |
| 99 | rtcp_packet_transport_ && rtcp_packet_transport_->writable()); |
| 100 | } |
| 101 | |
| 102 | bool RtpTransport::IsWritable(bool rtcp) const { |
| 103 | rtc::PacketTransportInternal* transport = rtcp && !rtcp_mux_enabled_ |
| 104 | ? rtcp_packet_transport_ |
| 105 | : rtp_packet_transport_; |
| 106 | return transport && transport->writable(); |
| 107 | } |
| 108 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 109 | bool RtpTransport::SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 110 | const rtc::PacketOptions& options, |
| 111 | int flags) { |
| 112 | return SendPacket(false, packet, options, flags); |
| 113 | } |
| 114 | |
| 115 | bool RtpTransport::SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 116 | const rtc::PacketOptions& options, |
| 117 | int flags) { |
| 118 | return SendPacket(true, packet, options, flags); |
| 119 | } |
| 120 | |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 121 | bool RtpTransport::SendPacket(bool rtcp, |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 122 | rtc::CopyOnWriteBuffer* packet, |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 123 | const rtc::PacketOptions& options, |
| 124 | int flags) { |
| 125 | rtc::PacketTransportInternal* transport = rtcp && !rtcp_mux_enabled_ |
| 126 | ? rtcp_packet_transport_ |
| 127 | : rtp_packet_transport_; |
| 128 | int ret = transport->SendPacket(packet->data<char>(), packet->size(), options, |
| 129 | flags); |
| 130 | if (ret != static_cast<int>(packet->size())) { |
| 131 | if (transport->GetError() == ENOTCONN) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 132 | RTC_LOG(LS_WARNING) << "Got ENOTCONN from transport."; |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 133 | SetReadyToSend(rtcp, false); |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
zstein | d48dbda | 2017-04-04 19:45:57 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 140 | void RtpTransport::UpdateRtpHeaderExtensionMap( |
| 141 | const cricket::RtpHeaderExtensions& header_extensions) { |
| 142 | header_extension_map_ = RtpHeaderExtensionMap(header_extensions); |
| 143 | } |
| 144 | |
| 145 | bool RtpTransport::RegisterRtpDemuxerSink(const RtpDemuxerCriteria& criteria, |
| 146 | RtpPacketSinkInterface* sink) { |
| 147 | rtp_demuxer_.RemoveSink(sink); |
| 148 | if (!rtp_demuxer_.AddSink(criteria, sink)) { |
| 149 | RTC_LOG(LS_ERROR) << "Failed to register the sink for RTP demuxer."; |
| 150 | return false; |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | bool RtpTransport::UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) { |
| 156 | if (!rtp_demuxer_.RemoveSink(sink)) { |
| 157 | RTC_LOG(LS_ERROR) << "Failed to unregister the sink for RTP demuxer."; |
| 158 | return false; |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 163 | RTCError RtpTransport::SetParameters(const RtpTransportParameters& parameters) { |
| 164 | if (parameters_.rtcp.mux && !parameters.rtcp.mux) { |
zstein | d9ce764 | 2017-04-10 16:17:57 -0700 | [diff] [blame] | 165 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 166 | "Disabling RTCP muxing is not allowed."); |
| 167 | } |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 168 | if (parameters.keepalive != parameters_.keepalive) { |
| 169 | // TODO(sprang): Wire up support for keep-alive (only ORTC support for now). |
| 170 | LOG_AND_RETURN_ERROR( |
| 171 | RTCErrorType::INVALID_MODIFICATION, |
| 172 | "RTP keep-alive parameters not supported by this channel."); |
zstein | d9ce764 | 2017-04-10 16:17:57 -0700 | [diff] [blame] | 173 | } |
| 174 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 175 | RtpTransportParameters new_parameters = parameters; |
| 176 | |
| 177 | if (new_parameters.rtcp.cname.empty()) { |
| 178 | new_parameters.rtcp.cname = parameters_.rtcp.cname; |
| 179 | } |
| 180 | |
| 181 | parameters_ = new_parameters; |
zstein | d9ce764 | 2017-04-10 16:17:57 -0700 | [diff] [blame] | 182 | return RTCError::OK(); |
| 183 | } |
| 184 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 185 | RtpTransportParameters RtpTransport::GetParameters() const { |
| 186 | return parameters_; |
zstein | d9ce764 | 2017-04-10 16:17:57 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 189 | void RtpTransport::DemuxPacket(rtc::CopyOnWriteBuffer* packet, |
| 190 | const rtc::PacketTime& time) { |
| 191 | webrtc::RtpPacketReceived parsed_packet(&header_extension_map_); |
| 192 | if (!parsed_packet.Parse(std::move(*packet))) { |
| 193 | RTC_LOG(LS_ERROR) |
| 194 | << "Failed to parse the incoming RTP packet before demuxing. Drop it."; |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | if (time.timestamp != -1) { |
| 199 | parsed_packet.set_arrival_time_ms((time.timestamp + 500) / 1000); |
| 200 | } |
| 201 | rtp_demuxer_.OnRtpPacket(parsed_packet); |
| 202 | } |
| 203 | |
zstein | d9ce764 | 2017-04-10 16:17:57 -0700 | [diff] [blame] | 204 | RtpTransportAdapter* RtpTransport::GetInternal() { |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 208 | bool RtpTransport::IsTransportWritable() { |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 209 | auto rtcp_packet_transport = |
| 210 | rtcp_mux_enabled_ ? nullptr : rtcp_packet_transport_; |
| 211 | return rtp_packet_transport_ && rtp_packet_transport_->writable() && |
| 212 | (!rtcp_packet_transport || rtcp_packet_transport->writable()); |
| 213 | } |
| 214 | |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 215 | void RtpTransport::OnReadyToSend(rtc::PacketTransportInternal* transport) { |
| 216 | SetReadyToSend(transport == rtcp_packet_transport_, true); |
| 217 | } |
| 218 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 219 | void RtpTransport::OnNetworkRouteChanged( |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 220 | rtc::Optional<rtc::NetworkRoute> network_route) { |
| 221 | SignalNetworkRouteChanged(network_route); |
| 222 | } |
| 223 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 224 | void RtpTransport::OnWritableState( |
| 225 | rtc::PacketTransportInternal* packet_transport) { |
| 226 | RTC_DCHECK(packet_transport == rtp_packet_transport_ || |
| 227 | packet_transport == rtcp_packet_transport_); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 228 | SignalWritableState(IsTransportWritable()); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void RtpTransport::OnSentPacket(rtc::PacketTransportInternal* packet_transport, |
| 232 | const rtc::SentPacket& sent_packet) { |
| 233 | RTC_DCHECK(packet_transport == rtp_packet_transport_ || |
| 234 | packet_transport == rtcp_packet_transport_); |
| 235 | SignalSentPacket(sent_packet); |
| 236 | } |
| 237 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 238 | void RtpTransport::OnRtpPacketReceived(rtc::CopyOnWriteBuffer* packet, |
| 239 | const rtc::PacketTime& packet_time) { |
| 240 | DemuxPacket(packet, packet_time); |
| 241 | } |
| 242 | |
| 243 | void RtpTransport::OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet, |
| 244 | const rtc::PacketTime& packet_time) { |
| 245 | SignalRtcpPacketReceived(packet, packet_time); |
| 246 | } |
| 247 | |
| 248 | void RtpTransport::OnReadPacket(rtc::PacketTransportInternal* transport, |
| 249 | const char* data, |
| 250 | size_t len, |
| 251 | const rtc::PacketTime& packet_time, |
| 252 | int flags) { |
| 253 | TRACE_EVENT0("webrtc", "RtpTransport::OnReadPacket"); |
| 254 | |
| 255 | // When using RTCP multiplexing we might get RTCP packets on the RTP |
| 256 | // transport. We check the RTP payload type to determine if it is RTCP. |
| 257 | bool rtcp = |
| 258 | transport == rtcp_packet_transport() || cricket::IsRtcpPacket(data, len); |
| 259 | |
| 260 | // Filter out the packet that is neither RTP nor RTCP. |
| 261 | if (!rtcp && !cricket::IsRtpPacket(data, len)) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | rtc::CopyOnWriteBuffer packet(data, len); |
| 266 | // Protect ourselves against crazy data. |
| 267 | if (!cricket::IsValidRtpRtcpPacketSize(rtcp, packet.size())) { |
| 268 | RTC_LOG(LS_ERROR) << "Dropping incoming " |
| 269 | << cricket::RtpRtcpStringLiteral(rtcp) |
| 270 | << " packet: wrong size=" << packet.size(); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | if (rtcp) { |
| 275 | OnRtcpPacketReceived(&packet, packet_time); |
| 276 | } else { |
| 277 | OnRtpPacketReceived(&packet, packet_time); |
| 278 | } |
| 279 | } |
| 280 | |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 281 | void RtpTransport::SetReadyToSend(bool rtcp, bool ready) { |
| 282 | if (rtcp) { |
| 283 | rtcp_ready_to_send_ = ready; |
| 284 | } else { |
| 285 | rtp_ready_to_send_ = ready; |
| 286 | } |
| 287 | |
| 288 | MaybeSignalReadyToSend(); |
| 289 | } |
| 290 | |
| 291 | void RtpTransport::MaybeSignalReadyToSend() { |
| 292 | bool ready_to_send = |
| 293 | rtp_ready_to_send_ && (rtcp_ready_to_send_ || rtcp_mux_enabled_); |
| 294 | if (ready_to_send != ready_to_send_) { |
| 295 | ready_to_send_ = ready_to_send; |
| 296 | SignalReadyToSend(ready_to_send); |
| 297 | } |
| 298 | } |
| 299 | |
zstein | d48dbda | 2017-04-04 19:45:57 -0700 | [diff] [blame] | 300 | } // namespace webrtc |