henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/async_udp_socket.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 12 | |
Per K | f81af2f | 2023-12-22 12:24:40 | [diff] [blame] | 13 | #include "absl/types/optional.h" |
| 14 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/logging.h" |
Per K | f81af2f | 2023-12-22 12:24:40 | [diff] [blame] | 17 | #include "rtc_base/network/received_packet.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 18 | #include "rtc_base/network/sent_packet.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 19 | #include "rtc_base/time_utils.h" |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 20 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
Per K | 7efd372 | 2023-03-21 13:10:44 | [diff] [blame] | 24 | // Returns true if the experiement "WebRTC-SCM-Timestamp" is explicitly |
| 25 | // disabled. |
| 26 | static bool IsScmTimeStampExperimentDisabled() { |
| 27 | return webrtc::field_trial::IsDisabled("WebRTC-SCM-Timestamp"); |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 28 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 29 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 30 | AsyncUDPSocket* AsyncUDPSocket::Create(Socket* socket, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 31 | const SocketAddress& bind_address) { |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 32 | std::unique_ptr<Socket> owned_socket(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 33 | if (socket->Bind(bind_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 34 | RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 35 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 36 | } |
| 37 | return new AsyncUDPSocket(owned_socket.release()); |
| 38 | } |
| 39 | |
| 40 | AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory, |
| 41 | const SocketAddress& bind_address) { |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 42 | Socket* socket = factory->CreateSocket(bind_address.family(), SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 43 | if (!socket) |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 44 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 45 | return Create(socket, bind_address); |
| 46 | } |
| 47 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 48 | AsyncUDPSocket::AsyncUDPSocket(Socket* socket) : socket_(socket) { |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 49 | sequence_checker_.Detach(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 50 | // The socket should start out readable but not writable. |
| 51 | socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent); |
| 52 | socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent); |
| 53 | } |
| 54 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 55 | SocketAddress AsyncUDPSocket::GetLocalAddress() const { |
| 56 | return socket_->GetLocalAddress(); |
| 57 | } |
| 58 | |
| 59 | SocketAddress AsyncUDPSocket::GetRemoteAddress() const { |
| 60 | return socket_->GetRemoteAddress(); |
| 61 | } |
| 62 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 63 | int AsyncUDPSocket::Send(const void* pv, |
| 64 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 65 | const rtc::PacketOptions& options) { |
Qingsi Wang | 6e641e6 | 2018-04-12 03:14:17 | [diff] [blame] | 66 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 67 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-17 01:22:31 | [diff] [blame] | 68 | CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 14:26:07 | [diff] [blame] | 69 | int ret = socket_->Send(pv, cb); |
| 70 | SignalSentPacket(this, sent_packet); |
| 71 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 72 | } |
| 73 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 74 | int AsyncUDPSocket::SendTo(const void* pv, |
| 75 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 76 | const SocketAddress& addr, |
| 77 | const rtc::PacketOptions& options) { |
Qingsi Wang | 6e641e6 | 2018-04-12 03:14:17 | [diff] [blame] | 78 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 79 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-17 01:22:31 | [diff] [blame] | 80 | CopySocketInformationToPacketInfo(cb, *this, true, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 14:26:07 | [diff] [blame] | 81 | int ret = socket_->SendTo(pv, cb, addr); |
| 82 | SignalSentPacket(this, sent_packet); |
| 83 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int AsyncUDPSocket::Close() { |
| 87 | return socket_->Close(); |
| 88 | } |
| 89 | |
| 90 | AsyncUDPSocket::State AsyncUDPSocket::GetState() const { |
| 91 | return STATE_BOUND; |
| 92 | } |
| 93 | |
| 94 | int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) { |
| 95 | return socket_->GetOption(opt, value); |
| 96 | } |
| 97 | |
| 98 | int AsyncUDPSocket::SetOption(Socket::Option opt, int value) { |
| 99 | return socket_->SetOption(opt, value); |
| 100 | } |
| 101 | |
| 102 | int AsyncUDPSocket::GetError() const { |
| 103 | return socket_->GetError(); |
| 104 | } |
| 105 | |
| 106 | void AsyncUDPSocket::SetError(int error) { |
| 107 | return socket_->SetError(error); |
| 108 | } |
| 109 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 110 | void AsyncUDPSocket::OnReadEvent(Socket* socket) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 111 | RTC_DCHECK(socket_.get() == socket); |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 112 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 113 | |
Per K | f81af2f | 2023-12-22 12:24:40 | [diff] [blame] | 114 | Socket::ReceiveBuffer receive_buffer(buffer_); |
| 115 | int len = socket_->RecvFrom(receive_buffer); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 116 | if (len < 0) { |
| 117 | // An error here typically means we got an ICMP error in response to our |
| 118 | // send datagram, indicating the remote address was unreachable. |
| 119 | // When doing ICE, this kind of thing will often happen. |
| 120 | // TODO: Do something better like forwarding the error to the user. |
| 121 | SocketAddress local_addr = socket_->GetLocalAddress(); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 122 | RTC_LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString() |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 123 | << "] receive failed with error " << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 124 | return; |
| 125 | } |
Per K | f81af2f | 2023-12-22 12:24:40 | [diff] [blame] | 126 | if (len == 0) { |
| 127 | // Spurios wakeup. |
| 128 | return; |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 129 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 130 | |
Per K | f81af2f | 2023-12-22 12:24:40 | [diff] [blame] | 131 | if (!receive_buffer.arrival_time) { |
| 132 | // Timestamp from socket is not available. |
| 133 | receive_buffer.arrival_time = webrtc::Timestamp::Micros(rtc::TimeMicros()); |
| 134 | } else { |
| 135 | if (!socket_time_offset_) { |
| 136 | // Estimate timestamp offset from first packet arrival time unless |
| 137 | // disabled |
| 138 | bool estimate_time_offset = !IsScmTimeStampExperimentDisabled(); |
| 139 | if (estimate_time_offset) { |
| 140 | socket_time_offset_ = webrtc::Timestamp::Micros(rtc::TimeMicros()) - |
| 141 | *receive_buffer.arrival_time; |
| 142 | } else { |
| 143 | socket_time_offset_ = webrtc::TimeDelta::Micros(0); |
| 144 | } |
| 145 | } |
| 146 | *receive_buffer.arrival_time += *socket_time_offset_; |
| 147 | } |
| 148 | NotifyPacketReceived(ReceivedPacket(receive_buffer.payload, |
| 149 | receive_buffer.source_address, |
| 150 | receive_buffer.arrival_time)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 151 | } |
| 152 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 153 | void AsyncUDPSocket::OnWriteEvent(Socket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 154 | SignalReadyToSend(this); |
| 155 | } |
| 156 | |
| 157 | } // namespace rtc |