diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:35:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:35:49 +0000 |
commit | d8bbc7858622b6d9c278469aab701ca0b609cddf (patch) | |
tree | eff41dc61d9f714852212739e6b3738b82a2af87 /third_party/libwebrtc/rtc_base | |
parent | Releasing progress-linux version 125.0.3-1~progress7.99u1. (diff) | |
download | firefox-d8bbc7858622b6d9c278469aab701ca0b609cddf.tar.xz firefox-d8bbc7858622b6d9c278469aab701ca0b609cddf.zip |
Merging upstream version 126.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
32 files changed, 347 insertions, 320 deletions
diff --git a/third_party/libwebrtc/rtc_base/BUILD.gn b/third_party/libwebrtc/rtc_base/BUILD.gn index 5392e5f472..57a9c11f01 100644 --- a/third_party/libwebrtc/rtc_base/BUILD.gn +++ b/third_party/libwebrtc/rtc_base/BUILD.gn @@ -1119,13 +1119,17 @@ rtc_library("socket") { "socket.h", ] deps = [ + ":buffer", ":macromagic", ":socket_address", + "../api/units:timestamp", + "system:rtc_export", "third_party/sigslot", ] if (is_win) { deps += [ ":win32" ] } + absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ] } rtc_source_set("network_constants") { @@ -1367,7 +1371,9 @@ if (!build_with_mozilla) { ":socket_factory", ":timeutils", "../api:sequence_checker", + "../api/units:time_delta", "../system_wrappers:field_trial", + "network:received_packet", "network:sent_packet", "system:no_unique_address", ] @@ -1664,10 +1670,7 @@ rtc_source_set("gtest_prod") { rtc_library("gunit_helpers") { testonly = true - sources = [ - "gunit.cc", - "gunit.h", - ] + sources = [ "gunit.h" ] deps = [ ":logging", ":rtc_base_tests_utils", @@ -1806,9 +1809,7 @@ rtc_library("task_queue_for_test") { ] deps = [ ":checks", - ":macromagic", ":rtc_event", - ":rtc_task_queue", "../api:function_view", "../api/task_queue", "../api/task_queue:default_task_queue_factory", @@ -2020,6 +2021,7 @@ if (rtc_include_tests) { "containers:flat_map", "containers:unittests", "memory:unittests", + "network:received_packet", "synchronization:mutex", "task_utils:repeating_task", "third_party/base64", @@ -2046,7 +2048,6 @@ if (rtc_include_tests) { ":gunit_helpers", ":rtc_base_tests_utils", ":rtc_event", - ":rtc_task_queue", ":task_queue_for_test", ":timeutils", "../api/units:time_delta", @@ -2181,6 +2182,7 @@ if (rtc_include_tests) { "../test:test_main", "../test:test_support", "memory:fifo_buffer", + "network:received_packet", "synchronization:mutex", "third_party/sigslot", ] diff --git a/third_party/libwebrtc/rtc_base/async_packet_socket.cc b/third_party/libwebrtc/rtc_base/async_packet_socket.cc index 3721366099..e00bd03e3b 100644 --- a/third_party/libwebrtc/rtc_base/async_packet_socket.cc +++ b/third_party/libwebrtc/rtc_base/async_packet_socket.cc @@ -45,13 +45,11 @@ void AsyncPacketSocket::RegisterReceivedPacketCallback( received_packet_callback) { RTC_DCHECK_RUN_ON(&network_checker_); RTC_CHECK(!received_packet_callback_); - SignalReadPacket.connect(this, &AsyncPacketSocket::NotifyPacketReceived); received_packet_callback_ = std::move(received_packet_callback); } void AsyncPacketSocket::DeregisterReceivedPacketCallback() { RTC_DCHECK_RUN_ON(&network_checker_); - SignalReadPacket.disconnect(this); received_packet_callback_ = nullptr; } @@ -62,17 +60,6 @@ void AsyncPacketSocket::NotifyPacketReceived( received_packet_callback_(this, packet); return; } - if (SignalReadPacket.is_empty()) { - RTC_DCHECK_NOTREACHED() << " No listener registered"; - return; - } - // TODO(bugs.webrtc.org:15368): Remove. This code path is only used if - // SignalReadyPacket is used by clients to get notification of received - // packets but actual socket implementation use NotifyPacketReceived to - // trigger the notification. - SignalReadPacket(this, reinterpret_cast<const char*>(packet.payload().data()), - packet.payload().size(), packet.source_address(), - packet.arrival_time() ? packet.arrival_time()->us() : -1); } void CopySocketInformationToPacketInfo(size_t packet_size_bytes, diff --git a/third_party/libwebrtc/rtc_base/async_packet_socket.h b/third_party/libwebrtc/rtc_base/async_packet_socket.h index 768fcd446b..740c0bb61f 100644 --- a/third_party/libwebrtc/rtc_base/async_packet_socket.h +++ b/third_party/libwebrtc/rtc_base/async_packet_socket.h @@ -122,18 +122,6 @@ class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> { received_packet_callback); void DeregisterReceivedPacketCallback(); - // Emitted each time a packet is read. Used only for UDP and - // connected TCP sockets. - // TODO(bugs.webrtc.org:15368): Deprecate and remove. - sigslot::signal5<AsyncPacketSocket*, - const char*, - size_t, - const SocketAddress&, - // TODO(bugs.webrtc.org/9584): Change to passing the int64_t - // timestamp by value. - const int64_t&> - SignalReadPacket; - // Emitted each time a packet is sent. sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket; diff --git a/third_party/libwebrtc/rtc_base/async_packet_socket_unittest.cc b/third_party/libwebrtc/rtc_base/async_packet_socket_unittest.cc index 6cd4f09459..1d66821958 100644 --- a/third_party/libwebrtc/rtc_base/async_packet_socket_unittest.cc +++ b/third_party/libwebrtc/rtc_base/async_packet_socket_unittest.cc @@ -63,48 +63,5 @@ TEST(AsyncPacketSocket, RegisteredCallbackReceivePacketsFromNotify) { mock_socket.NotifyPacketReceived(); } -TEST(AsyncPacketSocket, RegisteredCallbackReceivePacketsFromSignalReadPacket) { - MockAsyncPacketSocket mock_socket; - MockFunction<void(AsyncPacketSocket*, const rtc::ReceivedPacket&)> - received_packet; - - EXPECT_CALL(received_packet, Call); - mock_socket.RegisterReceivedPacketCallback(received_packet.AsStdFunction()); - char data[1] = {'a'}; - mock_socket.SignalReadPacket(&mock_socket, data, 1, SocketAddress(), -1); -} - -TEST(AsyncPacketSocket, SignalReadPacketTriggeredByNotifyPacketReceived) { - class SigslotPacketReceiver : public sigslot::has_slots<> { - public: - explicit SigslotPacketReceiver(rtc::AsyncPacketSocket& socket) { - socket.SignalReadPacket.connect(this, - &SigslotPacketReceiver::OnPacketReceived); - } - - bool packet_received() const { return packet_received_; } - - private: - void OnPacketReceived(AsyncPacketSocket*, - const char*, - size_t, - const SocketAddress&, - // TODO(bugs.webrtc.org/9584): Change to passing the - // int64_t timestamp by value. - const int64_t&) { - packet_received_ = true; - } - - bool packet_received_ = false; - }; - - MockAsyncPacketSocket mock_socket; - SigslotPacketReceiver receiver(mock_socket); - ASSERT_FALSE(receiver.packet_received()); - - mock_socket.NotifyPacketReceived(); - EXPECT_TRUE(receiver.packet_received()); -} - } // namespace } // namespace rtc diff --git a/third_party/libwebrtc/rtc_base/async_udp_socket.cc b/third_party/libwebrtc/rtc_base/async_udp_socket.cc index 358420a5de..3d258bcb26 100644 --- a/third_party/libwebrtc/rtc_base/async_udp_socket.cc +++ b/third_party/libwebrtc/rtc_base/async_udp_socket.cc @@ -10,9 +10,11 @@ #include "rtc_base/async_udp_socket.h" - +#include "absl/types/optional.h" +#include "api/units/time_delta.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/network/sent_packet.h" #include "rtc_base/time_utils.h" #include "system_wrappers/include/field_trial.h" @@ -109,10 +111,8 @@ void AsyncUDPSocket::OnReadEvent(Socket* socket) { RTC_DCHECK(socket_.get() == socket); RTC_DCHECK_RUN_ON(&sequence_checker_); - SocketAddress remote_addr; - int64_t timestamp = -1; - int len = socket_->RecvFrom(buf_, BUF_SIZE, &remote_addr, ×tamp); - + Socket::ReceiveBuffer receive_buffer(buffer_); + int len = socket_->RecvFrom(receive_buffer); if (len < 0) { // An error here typically means we got an ICMP error in response to our // send datagram, indicating the remote address was unreachable. @@ -123,21 +123,31 @@ void AsyncUDPSocket::OnReadEvent(Socket* socket) { << "] receive failed with error " << socket_->GetError(); return; } - if (timestamp == -1) { + if (len == 0) { + // Spurios wakeup. + return; + } + + if (!receive_buffer.arrival_time) { // Timestamp from socket is not available. - timestamp = TimeMicros(); + receive_buffer.arrival_time = webrtc::Timestamp::Micros(rtc::TimeMicros()); } else { if (!socket_time_offset_) { - socket_time_offset_ = - !IsScmTimeStampExperimentDisabled() ? TimeMicros() - timestamp : 0; + // Estimate timestamp offset from first packet arrival time unless + // disabled + bool estimate_time_offset = !IsScmTimeStampExperimentDisabled(); + if (estimate_time_offset) { + socket_time_offset_ = webrtc::Timestamp::Micros(rtc::TimeMicros()) - + *receive_buffer.arrival_time; + } else { + socket_time_offset_ = webrtc::TimeDelta::Micros(0); + } } - timestamp += *socket_time_offset_; + *receive_buffer.arrival_time += *socket_time_offset_; } - - // TODO: Make sure that we got all of the packet. - // If we did not, then we should resize our buffer to be large enough. - NotifyPacketReceived( - rtc::ReceivedPacket::CreateFromLegacy(buf_, len, timestamp, remote_addr)); + NotifyPacketReceived(ReceivedPacket(receive_buffer.payload, + receive_buffer.source_address, + receive_buffer.arrival_time)); } void AsyncUDPSocket::OnWriteEvent(Socket* socket) { diff --git a/third_party/libwebrtc/rtc_base/async_udp_socket.h b/third_party/libwebrtc/rtc_base/async_udp_socket.h index 4198b25c4d..af361b98ea 100644 --- a/third_party/libwebrtc/rtc_base/async_udp_socket.h +++ b/third_party/libwebrtc/rtc_base/async_udp_socket.h @@ -18,6 +18,7 @@ #include "absl/types/optional.h" #include "api/sequence_checker.h" +#include "api/units/time_delta.h" #include "rtc_base/async_packet_socket.h" #include "rtc_base/socket.h" #include "rtc_base/socket_address.h" @@ -68,9 +69,9 @@ class AsyncUDPSocket : public AsyncPacketSocket { RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_; std::unique_ptr<Socket> socket_; - static constexpr int BUF_SIZE = 64 * 1024; - char buf_[BUF_SIZE] RTC_GUARDED_BY(sequence_checker_); - absl::optional<int64_t> socket_time_offset_ RTC_GUARDED_BY(sequence_checker_); + rtc::Buffer buffer_ RTC_GUARDED_BY(sequence_checker_); + absl::optional<webrtc::TimeDelta> socket_time_offset_ + RTC_GUARDED_BY(sequence_checker_); }; } // namespace rtc diff --git a/third_party/libwebrtc/rtc_base/bitstream_reader.h b/third_party/libwebrtc/rtc_base/bitstream_reader.h index c367b9dc9f..62b0ba5ebf 100644 --- a/third_party/libwebrtc/rtc_base/bitstream_reader.h +++ b/third_party/libwebrtc/rtc_base/bitstream_reader.h @@ -124,11 +124,12 @@ class BitstreamReader { }; inline BitstreamReader::BitstreamReader(rtc::ArrayView<const uint8_t> bytes) - : bytes_(bytes.data()), remaining_bits_(bytes.size() * 8) {} + : bytes_(bytes.data()), + remaining_bits_(rtc::checked_cast<int>(bytes.size() * 8)) {} inline BitstreamReader::BitstreamReader(absl::string_view bytes) : bytes_(reinterpret_cast<const uint8_t*>(bytes.data())), - remaining_bits_(bytes.size() * 8) {} + remaining_bits_(rtc::checked_cast<int>(bytes.size() * 8)) {} inline BitstreamReader::~BitstreamReader() { RTC_DCHECK(last_read_is_verified_) << "Latest calls to Read or ConsumeBit " diff --git a/third_party/libwebrtc/rtc_base/byte_buffer.cc b/third_party/libwebrtc/rtc_base/byte_buffer.cc index a076f46ecb..5674d54e21 100644 --- a/third_party/libwebrtc/rtc_base/byte_buffer.cc +++ b/third_party/libwebrtc/rtc_base/byte_buffer.cc @@ -16,21 +16,13 @@ namespace rtc { ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {} -ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len) +ByteBufferWriter::ByteBufferWriter(const uint8_t* bytes, size_t len) : ByteBufferWriterT(bytes, len) {} ByteBufferReader::ByteBufferReader(rtc::ArrayView<const uint8_t> bytes) { Construct(bytes.data(), bytes.size()); } -ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) { - Construct(reinterpret_cast<const uint8_t*>(bytes), len); -} - -ByteBufferReader::ByteBufferReader(const char* bytes) { - Construct(reinterpret_cast<const uint8_t*>(bytes), strlen(bytes)); -} - ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) { Construct(reinterpret_cast<const uint8_t*>(buf.Data()), buf.Length()); } @@ -140,6 +132,14 @@ bool ByteBufferReader::ReadString(std::string* val, size_t len) { } } +bool ByteBufferReader::ReadStringView(absl::string_view* val, size_t len) { + if (!val || len > Length()) + return false; + *val = absl::string_view(reinterpret_cast<const char*>(bytes_ + start_), len); + start_ += len; + return true; +} + bool ByteBufferReader::ReadBytes(rtc::ArrayView<uint8_t> val) { if (val.size() == 0) { return true; @@ -147,10 +147,6 @@ bool ByteBufferReader::ReadBytes(rtc::ArrayView<uint8_t> val) { return ReadBytes(val.data(), val.size()); } -bool ByteBufferReader::ReadBytes(char* val, size_t len) { - return ReadBytes(reinterpret_cast<uint8_t*>(val), len); -} - // Private function supporting the other Read* functions. bool ByteBufferReader::ReadBytes(uint8_t* val, size_t len) { if (len > Length()) { diff --git a/third_party/libwebrtc/rtc_base/byte_buffer.h b/third_party/libwebrtc/rtc_base/byte_buffer.h index c15773779e..1508bd6ead 100644 --- a/third_party/libwebrtc/rtc_base/byte_buffer.h +++ b/third_party/libwebrtc/rtc_base/byte_buffer.h @@ -51,30 +51,32 @@ class ByteBufferWriterT { absl::string_view DataAsStringView() const { return absl::string_view(reinterpret_cast<const char*>(Data()), Length()); } - char* DataAsCharPointer() const { return reinterpret_cast<char*>(Data()); } + const char* DataAsCharPointer() const { + return reinterpret_cast<const char*>(Data()); + } // Write value to the buffer. Resizes the buffer when it is // neccessary. void WriteUInt8(uint8_t val) { - WriteBytes(reinterpret_cast<const value_type*>(&val), 1); + WriteBytesInternal(reinterpret_cast<const value_type*>(&val), 1); } void WriteUInt16(uint16_t val) { uint16_t v = HostToNetwork16(val); - WriteBytes(reinterpret_cast<const value_type*>(&v), 2); + WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 2); } void WriteUInt24(uint32_t val) { uint32_t v = HostToNetwork32(val); value_type* start = reinterpret_cast<value_type*>(&v); ++start; - WriteBytes(start, 3); + WriteBytesInternal(start, 3); } void WriteUInt32(uint32_t val) { uint32_t v = HostToNetwork32(val); - WriteBytes(reinterpret_cast<const value_type*>(&v), 4); + WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 4); } void WriteUInt64(uint64_t val) { uint64_t v = HostToNetwork64(val); - WriteBytes(reinterpret_cast<const value_type*>(&v), 8); + WriteBytesInternal(reinterpret_cast<const value_type*>(&v), 8); } // Serializes an unsigned varint in the format described by // https://developers.google.com/protocol-buffers/docs/encoding#varints @@ -84,17 +86,19 @@ class ByteBufferWriterT { // Write 7 bits at a time, then set the msb to a continuation byte // (msb=1). value_type byte = static_cast<value_type>(val) | 0x80; - WriteBytes(&byte, 1); + WriteBytesInternal(&byte, 1); val >>= 7; } value_type last_byte = static_cast<value_type>(val); - WriteBytes(&last_byte, 1); + WriteBytesInternal(&last_byte, 1); } void WriteString(absl::string_view val) { - WriteBytes(val.data(), val.size()); + WriteBytesInternal(reinterpret_cast<const value_type*>(val.data()), + val.size()); } - void WriteBytes(const value_type* val, size_t len) { - buffer_.AppendData(val, len); + // Write an array of bytes (uint8_t) + void WriteBytes(const uint8_t* val, size_t len) { + WriteBytesInternal(reinterpret_cast<const value_type*>(val), len); } // Reserves the given number of bytes and returns a value_type* that can be @@ -122,16 +126,20 @@ class ByteBufferWriterT { } } + void WriteBytesInternal(const value_type* val, size_t len) { + buffer_.AppendData(val, len); + } + BufferClassT buffer_; // There are sensible ways to define these, but they aren't needed in our code // base. }; -class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> { +class ByteBufferWriter : public ByteBufferWriterT<BufferT<uint8_t>> { public: ByteBufferWriter(); - ByteBufferWriter(const char* bytes, size_t len); + ByteBufferWriter(const uint8_t* bytes, size_t len); ByteBufferWriter(const ByteBufferWriter&) = delete; ByteBufferWriter& operator=(const ByteBufferWriter&) = delete; @@ -141,28 +149,18 @@ class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> { // valid during the lifetime of the reader. class ByteBufferReader { public: - [[deprecated("Use ArrayView<uint8_t>")]] ByteBufferReader(const char* bytes, - size_t len); - explicit ByteBufferReader( rtc::ArrayView<const uint8_t> bytes ABSL_ATTRIBUTE_LIFETIME_BOUND); - // Initializes buffer from a zero-terminated string. - explicit ByteBufferReader(const char* bytes); - explicit ByteBufferReader(const ByteBufferWriter& buf); ByteBufferReader(const ByteBufferReader&) = delete; ByteBufferReader& operator=(const ByteBufferReader&) = delete; - // Returns start of unprocessed data. - // TODO(bugs.webrtc.org/15661): Deprecate and remove. - const char* Data() const { - return reinterpret_cast<const char*>(bytes_ + start_); - } + const uint8_t* Data() const { return bytes_ + start_; } // Returns number of unprocessed bytes. size_t Length() const { return end_ - start_; } - // Returns a view of the unprocessed data. + // Returns a view of the unprocessed data. Does not move current position. rtc::ArrayView<const uint8_t> DataView() const { return rtc::ArrayView<const uint8_t>(bytes_ + start_, end_ - start_); } @@ -175,14 +173,14 @@ class ByteBufferReader { bool ReadUInt32(uint32_t* val); bool ReadUInt64(uint64_t* val); bool ReadUVarint(uint64_t* val); + // Copies the val.size() next bytes into val.data(). bool ReadBytes(rtc::ArrayView<uint8_t> val); - // For backwards compatibility. - // TODO(bugs.webrtc.org/15661): Deprecate and remove. - [[deprecated("Read using ArrayView")]] bool ReadBytes(char* val, size_t len); - // Appends next `len` bytes from the buffer to `val`. Returns false // if there is less than `len` bytes left. bool ReadString(std::string* val, size_t len); + // Same as `ReadString` except that the returned string_view will point into + // the internal buffer (no additional buffer allocation). + bool ReadStringView(absl::string_view* val, size_t len); // Moves current position `size` bytes forward. Returns false if // there is less than `size` bytes left in the buffer. Consume doesn't diff --git a/third_party/libwebrtc/rtc_base/byte_buffer_unittest.cc b/third_party/libwebrtc/rtc_base/byte_buffer_unittest.cc index f65299e639..520845d40b 100644 --- a/third_party/libwebrtc/rtc_base/byte_buffer_unittest.cc +++ b/third_party/libwebrtc/rtc_base/byte_buffer_unittest.cc @@ -16,10 +16,26 @@ #include "rtc_base/arraysize.h" #include "rtc_base/byte_order.h" +#include "test/gmock.h" #include "test/gtest.h" namespace rtc { +using ::testing::ElementsAre; + +TEST(ByteBufferTest, WriterAccessors) { + // To be changed into ByteBufferWriter when base type is converted. + ByteBufferWriterT<BufferT<uint8_t>> buffer; + buffer.WriteString("abc"); + EXPECT_EQ(buffer.Length(), 3U); + EXPECT_THAT(buffer.DataView(), ElementsAre('a', 'b', 'c')); + EXPECT_EQ(absl::string_view("abc"), buffer.DataAsStringView()); + + buffer.WriteUInt8(0); + EXPECT_STREQ(buffer.DataAsCharPointer(), "abc"); + EXPECT_STREQ(reinterpret_cast<const char*>(buffer.Data()), "abc"); +} + TEST(ByteBufferTest, TestByteOrder) { uint16_t n16 = 1; uint32_t n32 = 1; @@ -150,7 +166,7 @@ TEST(ByteBufferTest, TestReadWriteBuffer) { buffer.Clear(); // Write and read bytes - char write_bytes[] = "foo"; + uint8_t write_bytes[] = "foo"; buffer.WriteBytes(write_bytes, 3); ByteBufferReader read_buf7(buffer); uint8_t read_bytes[3]; @@ -162,7 +178,7 @@ TEST(ByteBufferTest, TestReadWriteBuffer) { buffer.Clear(); // Write and read reserved buffer space - char* write_dst = buffer.ReserveWriteBuffer(3); + uint8_t* write_dst = buffer.ReserveWriteBuffer(3); memcpy(write_dst, write_bytes, 3); ByteBufferReader read_buf8(buffer); memset(read_bytes, 0, 3); @@ -194,6 +210,27 @@ TEST(ByteBufferTest, TestReadWriteBuffer) { buffer.Clear(); } +TEST(ByteBufferTest, TestReadStringView) { + const absl::string_view tests[] = {"hello", " ", "string_view"}; + std::string buffer; + for (const auto& test : tests) + buffer += test; + + rtc::ArrayView<const uint8_t> bytes( + reinterpret_cast<const uint8_t*>(&buffer[0]), buffer.size()); + + ByteBufferReader read_buf(bytes); + size_t consumed = 0; + for (const auto& test : tests) { + absl::string_view sv; + EXPECT_TRUE(read_buf.ReadStringView(&sv, test.length())); + EXPECT_EQ(sv.compare(test), 0); + // The returned string view should point directly into the original string. + EXPECT_EQ(&sv[0], &buffer[0 + consumed]); + consumed += sv.size(); + } +} + TEST(ByteBufferTest, TestReadWriteUVarint) { ByteBufferWriter write_buffer; size_t size = 0; diff --git a/third_party/libwebrtc/rtc_base/experiments/BUILD.gn b/third_party/libwebrtc/rtc_base/experiments/BUILD.gn index 185d5931f7..d44eefd4fc 100644 --- a/third_party/libwebrtc/rtc_base/experiments/BUILD.gn +++ b/third_party/libwebrtc/rtc_base/experiments/BUILD.gn @@ -16,10 +16,9 @@ rtc_library("alr_experiment") { deps = [ "..:logging", "../../api:field_trials_view", - "../../api/transport:field_trial_based_config", ] absl_deps = [ - "//third_party/abseil-cpp/absl/strings:strings", + "//third_party/abseil-cpp/absl/strings:string_view", "//third_party/abseil-cpp/absl/types:optional", ] } diff --git a/third_party/libwebrtc/rtc_base/experiments/alr_experiment.cc b/third_party/libwebrtc/rtc_base/experiments/alr_experiment.cc index f5d36f6867..5370de5452 100644 --- a/third_party/libwebrtc/rtc_base/experiments/alr_experiment.cc +++ b/third_party/libwebrtc/rtc_base/experiments/alr_experiment.cc @@ -16,21 +16,16 @@ #include <string> #include "absl/strings/string_view.h" -#include "api/transport/field_trial_based_config.h" +#include "api/field_trials_view.h" #include "rtc_base/logging.h" namespace webrtc { +namespace { -const char AlrExperimentSettings::kScreenshareProbingBweExperimentName[] = - "WebRTC-ProbingScreenshareBwe"; -const char AlrExperimentSettings::kStrictPacingAndProbingExperimentName[] = - "WebRTC-StrictPacingAndProbing"; -const char kDefaultProbingScreenshareBweSettings[] = "1.0,2875,80,40,-60,3"; +constexpr absl::string_view kDefaultProbingScreenshareBweSettings = + "1.0,2875,80,40,-60,3"; -bool AlrExperimentSettings::MaxOneFieldTrialEnabled() { - return AlrExperimentSettings::MaxOneFieldTrialEnabled( - FieldTrialBasedConfig()); -} +} // namespace bool AlrExperimentSettings::MaxOneFieldTrialEnabled( const FieldTrialsView& key_value_config) { @@ -40,12 +35,6 @@ bool AlrExperimentSettings::MaxOneFieldTrialEnabled( } absl::optional<AlrExperimentSettings> -AlrExperimentSettings::CreateFromFieldTrial(absl::string_view experiment_name) { - return AlrExperimentSettings::CreateFromFieldTrial(FieldTrialBasedConfig(), - experiment_name); -} - -absl::optional<AlrExperimentSettings> AlrExperimentSettings::CreateFromFieldTrial( const FieldTrialsView& key_value_config, absl::string_view experiment_name) { diff --git a/third_party/libwebrtc/rtc_base/experiments/alr_experiment.h b/third_party/libwebrtc/rtc_base/experiments/alr_experiment.h index 048fd90cab..9914828827 100644 --- a/third_party/libwebrtc/rtc_base/experiments/alr_experiment.h +++ b/third_party/libwebrtc/rtc_base/experiments/alr_experiment.h @@ -30,14 +30,14 @@ struct AlrExperimentSettings { // reserved value to indicate absence of experiment. int group_id; - static const char kScreenshareProbingBweExperimentName[]; - static const char kStrictPacingAndProbingExperimentName[]; - static absl::optional<AlrExperimentSettings> CreateFromFieldTrial( - absl::string_view experiment_name); + static constexpr absl::string_view kScreenshareProbingBweExperimentName = + "WebRTC-ProbingScreenshareBwe"; + static constexpr absl::string_view kStrictPacingAndProbingExperimentName = + "WebRTC-StrictPacingAndProbing"; + static absl::optional<AlrExperimentSettings> CreateFromFieldTrial( const FieldTrialsView& key_value_config, absl::string_view experiment_name); - static bool MaxOneFieldTrialEnabled(); static bool MaxOneFieldTrialEnabled(const FieldTrialsView& key_value_config); private: diff --git a/third_party/libwebrtc/rtc_base/gunit.cc b/third_party/libwebrtc/rtc_base/gunit.cc deleted file mode 100644 index 7cd60fe9ee..0000000000 --- a/third_party/libwebrtc/rtc_base/gunit.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2018 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "rtc_base/gunit.h" - -#include <string> - -#include "absl/strings/match.h" -#include "absl/strings/string_view.h" - -::testing::AssertionResult AssertStartsWith(const char* text_expr, - const char* prefix_expr, - absl::string_view text, - absl::string_view prefix) { - if (absl::StartsWith(text, prefix)) { - return ::testing::AssertionSuccess(); - } else { - return ::testing::AssertionFailure() - << text_expr << "\nwhich is\n\"" << text - << "\"\ndoes not start with\n" - << prefix_expr << "\nwhich is\n\"" << prefix << "\""; - } -} - -::testing::AssertionResult AssertStringContains(const char* str_expr, - const char* substr_expr, - absl::string_view str, - absl::string_view substr) { - if (str.find(substr) != absl::string_view::npos) { - return ::testing::AssertionSuccess(); - } else { - return ::testing::AssertionFailure() - << str_expr << "\nwhich is\n\"" << str << "\"\ndoes not contain\n" - << substr_expr << "\nwhich is\n\"" << substr << "\""; - } -} diff --git a/third_party/libwebrtc/rtc_base/gunit.h b/third_party/libwebrtc/rtc_base/gunit.h index 6bc1419729..759b377aa2 100644 --- a/third_party/libwebrtc/rtc_base/gunit.h +++ b/third_party/libwebrtc/rtc_base/gunit.h @@ -154,16 +154,4 @@ } else \ GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : ASSERT_EQ(v1, v2) -// Usage: EXPECT_PRED_FORMAT2(AssertStartsWith, text, "prefix"); -testing::AssertionResult AssertStartsWith(const char* text_expr, - const char* prefix_expr, - absl::string_view text, - absl::string_view prefix); - -// Usage: EXPECT_PRED_FORMAT2(AssertStringContains, str, "substring"); -testing::AssertionResult AssertStringContains(const char* str_expr, - const char* substr_expr, - absl::string_view str, - absl::string_view substr); - #endif // RTC_BASE_GUNIT_H_ diff --git a/third_party/libwebrtc/rtc_base/nat_server.cc b/third_party/libwebrtc/rtc_base/nat_server.cc index b818685efb..c274cedf18 100644 --- a/third_party/libwebrtc/rtc_base/nat_server.cc +++ b/third_party/libwebrtc/rtc_base/nat_server.cc @@ -10,12 +10,15 @@ #include "rtc_base/nat_server.h" +#include <cstddef> #include <memory> #include "rtc_base/checks.h" #include "rtc_base/logging.h" #include "rtc_base/nat_socket_factory.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/socket_adapters.h" +#include "rtc_base/socket_address.h" namespace rtc { @@ -125,17 +128,27 @@ class NATProxyServer : public ProxyServer { }; NATServer::NATServer(NATType type, + rtc::Thread& internal_socket_thread, SocketFactory* internal, const SocketAddress& internal_udp_addr, const SocketAddress& internal_tcp_addr, + rtc::Thread& external_socket_thread, SocketFactory* external, const SocketAddress& external_ip) - : external_(external), external_ip_(external_ip.ipaddr(), 0) { + : internal_socket_thread_(internal_socket_thread), + external_socket_thread_(external_socket_thread), + external_(external), + external_ip_(external_ip.ipaddr(), 0) { nat_ = NAT::Create(type); - udp_server_socket_ = AsyncUDPSocket::Create(internal, internal_udp_addr); - udp_server_socket_->SignalReadPacket.connect(this, - &NATServer::OnInternalUDPPacket); + internal_socket_thread_.BlockingCall([&] { + udp_server_socket_ = AsyncUDPSocket::Create(internal, internal_udp_addr); + udp_server_socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnInternalUDPPacket(socket, packet); + }); + }); + tcp_proxy_server_ = new NATProxyServer(internal, internal_tcp_addr, external, external_ip); @@ -156,10 +169,11 @@ NATServer::~NATServer() { } void NATServer::OnInternalUDPPacket(AsyncPacketSocket* socket, - const char* buf, - size_t size, - const SocketAddress& addr, - const int64_t& /* packet_time_us */) { + const rtc::ReceivedPacket& packet) { + RTC_DCHECK(internal_socket_thread_.IsCurrent()); + const char* buf = reinterpret_cast<const char*>(packet.payload().data()); + size_t size = packet.payload().size(); + const SocketAddress& addr = packet.source_address(); // Read the intended destination from the wire. SocketAddress dest_addr; size_t length = UnpackAddressFromNAT(buf, size, &dest_addr); @@ -182,10 +196,8 @@ void NATServer::OnInternalUDPPacket(AsyncPacketSocket* socket, } void NATServer::OnExternalUDPPacket(AsyncPacketSocket* socket, - const char* buf, - size_t size, - const SocketAddress& remote_addr, - const int64_t& /* packet_time_us */) { + const rtc::ReceivedPacket& packet) { + RTC_DCHECK(external_socket_thread_.IsCurrent()); SocketAddress local_addr = socket->GetLocalAddress(); // Find the translation for this addresses. @@ -193,36 +205,46 @@ void NATServer::OnExternalUDPPacket(AsyncPacketSocket* socket, RTC_DCHECK(iter != ext_map_->end()); // Allow the NAT to reject this packet. - if (ShouldFilterOut(iter->second, remote_addr)) { - RTC_LOG(LS_INFO) << "Packet from " << remote_addr.ToSensitiveString() + if (ShouldFilterOut(iter->second, packet.source_address())) { + RTC_LOG(LS_INFO) << "Packet from " + << packet.source_address().ToSensitiveString() << " was filtered out by the NAT."; return; } // Forward this packet to the internal address. // First prepend the address in a quasi-STUN format. - std::unique_ptr<char[]> real_buf(new char[size + kNATEncodedIPv6AddressSize]); + std::unique_ptr<char[]> real_buf( + new char[packet.payload().size() + kNATEncodedIPv6AddressSize]); size_t addrlength = PackAddressForNAT( - real_buf.get(), size + kNATEncodedIPv6AddressSize, remote_addr); + real_buf.get(), packet.payload().size() + kNATEncodedIPv6AddressSize, + packet.source_address()); // Copy the data part after the address. rtc::PacketOptions options; - memcpy(real_buf.get() + addrlength, buf, size); - udp_server_socket_->SendTo(real_buf.get(), size + addrlength, + memcpy(real_buf.get() + addrlength, packet.payload().data(), + packet.payload().size()); + udp_server_socket_->SendTo(real_buf.get(), + packet.payload().size() + addrlength, iter->second->route.source(), options); } void NATServer::Translate(const SocketAddressPair& route) { - AsyncUDPSocket* socket = AsyncUDPSocket::Create(external_, external_ip_); + external_socket_thread_.BlockingCall([&] { + AsyncUDPSocket* socket = AsyncUDPSocket::Create(external_, external_ip_); - if (!socket) { - RTC_LOG(LS_ERROR) << "Couldn't find a free port!"; - return; - } + if (!socket) { + RTC_LOG(LS_ERROR) << "Couldn't find a free port!"; + return; + } - TransEntry* entry = new TransEntry(route, socket, nat_); - (*int_map_)[route] = entry; - (*ext_map_)[socket->GetLocalAddress()] = entry; - socket->SignalReadPacket.connect(this, &NATServer::OnExternalUDPPacket); + TransEntry* entry = new TransEntry(route, socket, nat_); + (*int_map_)[route] = entry; + (*ext_map_)[socket->GetLocalAddress()] = entry; + socket->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnExternalUDPPacket(socket, packet); + }); + }); } bool NATServer::ShouldFilterOut(TransEntry* entry, diff --git a/third_party/libwebrtc/rtc_base/nat_server.h b/third_party/libwebrtc/rtc_base/nat_server.h index acbd62a092..d179efa567 100644 --- a/third_party/libwebrtc/rtc_base/nat_server.h +++ b/third_party/libwebrtc/rtc_base/nat_server.h @@ -58,15 +58,17 @@ struct AddrCmp { const int NAT_SERVER_UDP_PORT = 4237; const int NAT_SERVER_TCP_PORT = 4238; -class NATServer : public sigslot::has_slots<> { +class NATServer { public: NATServer(NATType type, + rtc::Thread& internal_socket_thread, SocketFactory* internal, const SocketAddress& internal_udp_addr, const SocketAddress& internal_tcp_addr, + rtc::Thread& external_socket_thread, SocketFactory* external, const SocketAddress& external_ip); - ~NATServer() override; + ~NATServer(); NATServer(const NATServer&) = delete; NATServer& operator=(const NATServer&) = delete; @@ -81,15 +83,9 @@ class NATServer : public sigslot::has_slots<> { // Packets received on one of the networks. void OnInternalUDPPacket(AsyncPacketSocket* socket, - const char* buf, - size_t size, - const SocketAddress& addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); void OnExternalUDPPacket(AsyncPacketSocket* socket, - const char* buf, - size_t size, - const SocketAddress& remote_addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); private: typedef std::set<SocketAddress, AddrCmp> AddressSet; @@ -118,6 +114,8 @@ class NATServer : public sigslot::has_slots<> { bool ShouldFilterOut(TransEntry* entry, const SocketAddress& ext_addr); NAT* nat_; + rtc::Thread& internal_socket_thread_; + rtc::Thread& external_socket_thread_; SocketFactory* external_; SocketAddress external_ip_; AsyncUDPSocket* udp_server_socket_; diff --git a/third_party/libwebrtc/rtc_base/nat_socket_factory.cc b/third_party/libwebrtc/rtc_base/nat_socket_factory.cc index fe021b95ff..83ec2bc327 100644 --- a/third_party/libwebrtc/rtc_base/nat_socket_factory.cc +++ b/third_party/libwebrtc/rtc_base/nat_socket_factory.cc @@ -368,7 +368,8 @@ NATSocketServer::Translator* NATSocketServer::AddTranslator( if (nats_.Get(ext_ip)) return nullptr; - return nats_.Add(ext_ip, new Translator(this, type, int_ip, server_, ext_ip)); + return nats_.Add( + ext_ip, new Translator(this, type, int_ip, *msg_queue_, server_, ext_ip)); } void NATSocketServer::RemoveTranslator(const SocketAddress& ext_ip) { @@ -413,6 +414,7 @@ Socket* NATSocketServer::CreateInternalSocket(int family, NATSocketServer::Translator::Translator(NATSocketServer* server, NATType type, const SocketAddress& int_ip, + Thread& external_socket_thread, SocketFactory* ext_factory, const SocketAddress& ext_ip) : server_(server) { @@ -422,7 +424,8 @@ NATSocketServer::Translator::Translator(NATSocketServer* server, internal_server_ = std::make_unique<VirtualSocketServer>(); internal_server_->SetMessageQueue(server_->queue()); nat_server_ = std::make_unique<NATServer>( - type, internal_server_.get(), int_ip, int_ip, ext_factory, ext_ip); + type, *server->queue(), internal_server_.get(), int_ip, int_ip, + external_socket_thread, ext_factory, ext_ip); } NATSocketServer::Translator::~Translator() { @@ -443,8 +446,8 @@ NATSocketServer::Translator* NATSocketServer::Translator::AddTranslator( return nullptr; AddClient(ext_ip); - return nats_.Add(ext_ip, - new Translator(server_, type, int_ip, server_, ext_ip)); + return nats_.Add(ext_ip, new Translator(server_, type, int_ip, + *server_->queue(), server_, ext_ip)); } void NATSocketServer::Translator::RemoveTranslator( const SocketAddress& ext_ip) { diff --git a/third_party/libwebrtc/rtc_base/nat_socket_factory.h b/third_party/libwebrtc/rtc_base/nat_socket_factory.h index 0b301b5844..f803496b05 100644 --- a/third_party/libwebrtc/rtc_base/nat_socket_factory.h +++ b/third_party/libwebrtc/rtc_base/nat_socket_factory.h @@ -102,6 +102,7 @@ class NATSocketServer : public SocketServer, public NATInternalSocketFactory { Translator(NATSocketServer* server, NATType type, const SocketAddress& int_addr, + Thread& external_socket_thread, SocketFactory* ext_factory, const SocketAddress& ext_addr); ~Translator(); diff --git a/third_party/libwebrtc/rtc_base/nat_unittest.cc b/third_party/libwebrtc/rtc_base/nat_unittest.cc index 432985d283..742e0d6ee7 100644 --- a/third_party/libwebrtc/rtc_base/nat_unittest.cc +++ b/third_party/libwebrtc/rtc_base/nat_unittest.cc @@ -76,16 +76,17 @@ void TestSend(SocketServer* internal, Thread th_int(internal); Thread th_ext(external); + th_int.Start(); + th_ext.Start(); + SocketAddress server_addr = internal_addr; server_addr.SetPort(0); // Auto-select a port - NATServer* nat = new NATServer(nat_type, internal, server_addr, server_addr, - external, external_addrs[0]); + NATServer* nat = + new NATServer(nat_type, th_int, internal, server_addr, server_addr, + th_ext, external, external_addrs[0]); NATSocketFactory* natsf = new NATSocketFactory( internal, nat->internal_udp_address(), nat->internal_tcp_address()); - th_int.Start(); - th_ext.Start(); - TestClient* in; th_int.BlockingCall([&] { in = CreateTestClient(natsf, internal_addr); }); @@ -139,13 +140,13 @@ void TestRecv(SocketServer* internal, SocketAddress server_addr = internal_addr; server_addr.SetPort(0); // Auto-select a port - NATServer* nat = new NATServer(nat_type, internal, server_addr, server_addr, - external, external_addrs[0]); - NATSocketFactory* natsf = new NATSocketFactory( - internal, nat->internal_udp_address(), nat->internal_tcp_address()); - th_int.Start(); th_ext.Start(); + NATServer* nat = + new NATServer(nat_type, th_int, internal, server_addr, server_addr, + th_ext, external, external_addrs[0]); + NATSocketFactory* natsf = new NATSocketFactory( + internal, nat->internal_udp_address(), nat->internal_tcp_address()); TestClient* in = nullptr; th_int.BlockingCall([&] { in = CreateTestClient(natsf, internal_addr); }); @@ -355,9 +356,11 @@ class NatTcpTest : public ::testing::Test, public sigslot::has_slots<> { int_thread_(new Thread(int_vss_.get())), ext_thread_(new Thread(ext_vss_.get())), nat_(new NATServer(NAT_OPEN_CONE, + *int_thread_, int_vss_.get(), int_addr_, int_addr_, + *ext_thread_, ext_vss_.get(), ext_addr_)), natsf_(new NATSocketFactory(int_vss_.get(), diff --git a/third_party/libwebrtc/rtc_base/network/BUILD.gn b/third_party/libwebrtc/rtc_base/network/BUILD.gn index 7e9cf7ab68..2be484e1e0 100644 --- a/third_party/libwebrtc/rtc_base/network/BUILD.gn +++ b/third_party/libwebrtc/rtc_base/network/BUILD.gn @@ -18,6 +18,7 @@ rtc_library("sent_packet") { } rtc_library("received_packet") { + visibility = [ "*" ] sources = [ "received_packet.cc", "received_packet.h", diff --git a/third_party/libwebrtc/rtc_base/network/received_packet.cc b/third_party/libwebrtc/rtc_base/network/received_packet.cc index 40d6e1142c..95f5e22d3b 100644 --- a/third_party/libwebrtc/rtc_base/network/received_packet.cc +++ b/third_party/libwebrtc/rtc_base/network/received_packet.cc @@ -25,14 +25,12 @@ ReceivedPacket::ReceivedPacket(rtc::ArrayView<const uint8_t> payload, // static ReceivedPacket ReceivedPacket::CreateFromLegacy( - const char* data, + const uint8_t* data, size_t size, int64_t packet_time_us, const rtc::SocketAddress& source_address) { RTC_DCHECK(packet_time_us == -1 || packet_time_us >= 0); - return ReceivedPacket(rtc::reinterpret_array_view<const uint8_t>( - rtc::MakeArrayView(data, size)), - source_address, + return ReceivedPacket(rtc::MakeArrayView(data, size), source_address, (packet_time_us >= 0) ? absl::optional<webrtc::Timestamp>( webrtc::Timestamp::Micros(packet_time_us)) diff --git a/third_party/libwebrtc/rtc_base/network/received_packet.h b/third_party/libwebrtc/rtc_base/network/received_packet.h index e33361ca29..d898ccb2e9 100644 --- a/third_party/libwebrtc/rtc_base/network/received_packet.h +++ b/third_party/libwebrtc/rtc_base/network/received_packet.h @@ -47,6 +47,15 @@ class RTC_EXPORT ReceivedPacket { const char* data, size_t size, int64_t packet_time_us, + const rtc::SocketAddress& addr = rtc::SocketAddress()) { + return CreateFromLegacy(reinterpret_cast<const uint8_t*>(data), size, + packet_time_us, addr); + } + + static ReceivedPacket CreateFromLegacy( + const uint8_t* data, + size_t size, + int64_t packet_time_us, const rtc::SocketAddress& = rtc::SocketAddress()); private: diff --git a/third_party/libwebrtc/rtc_base/server_socket_adapters.cc b/third_party/libwebrtc/rtc_base/server_socket_adapters.cc index 47c19cbed9..0bef752f1e 100644 --- a/third_party/libwebrtc/rtc_base/server_socket_adapters.cc +++ b/third_party/libwebrtc/rtc_base/server_socket_adapters.cc @@ -75,7 +75,9 @@ void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) { // Consume parsed data *len = response.Length(); - memmove(data, response.Data(), *len); + if (response.Length() > 0) { + memmove(data, response.DataView().data(), *len); + } } void AsyncSocksProxyServerSocket::DirectSend(const ByteBufferWriter& buf) { diff --git a/third_party/libwebrtc/rtc_base/socket.cc b/third_party/libwebrtc/rtc_base/socket.cc index bcd62ad2a4..0908c2991f 100644 --- a/third_party/libwebrtc/rtc_base/socket.cc +++ b/third_party/libwebrtc/rtc_base/socket.cc @@ -10,4 +10,24 @@ #include "rtc_base/socket.h" -namespace rtc {} // namespace rtc +#include <cstdint> + +#include "rtc_base/buffer.h" + +namespace rtc { + +int Socket::RecvFrom(ReceiveBuffer& buffer) { + static constexpr int BUF_SIZE = 64 * 1024; + int64_t timestamp = -1; + buffer.payload.EnsureCapacity(BUF_SIZE); + int len = RecvFrom(buffer.payload.data(), buffer.payload.capacity(), + &buffer.source_address, ×tamp); + buffer.payload.SetSize(len > 0 ? len : 0); + if (len > 0 && timestamp != -1) { + buffer.arrival_time = webrtc::Timestamp::Micros(timestamp); + } + + return len; +} + +} // namespace rtc diff --git a/third_party/libwebrtc/rtc_base/socket.h b/third_party/libwebrtc/rtc_base/socket.h index 0ed3a7fa6a..fac75aca94 100644 --- a/third_party/libwebrtc/rtc_base/socket.h +++ b/third_party/libwebrtc/rtc_base/socket.h @@ -13,6 +13,8 @@ #include <errno.h> +#include "absl/types/optional.h" + #if defined(WEBRTC_POSIX) #include <arpa/inet.h> #include <netinet/in.h> @@ -25,7 +27,10 @@ #include "rtc_base/win32.h" #endif +#include "api/units/timestamp.h" +#include "rtc_base/buffer.h" #include "rtc_base/socket_address.h" +#include "rtc_base/system/rtc_export.h" #include "rtc_base/third_party/sigslot/sigslot.h" // Rather than converting errors into a private namespace, @@ -78,8 +83,15 @@ inline bool IsBlockingError(int e) { // General interface for the socket implementations of various networks. The // methods match those of normal UNIX sockets very closely. -class Socket { +class RTC_EXPORT Socket { public: + struct ReceiveBuffer { + ReceiveBuffer(rtc::Buffer& payload) : payload(payload) {} + + absl::optional<webrtc::Timestamp> arrival_time; + SocketAddress source_address; + rtc::Buffer& payload; + }; virtual ~Socket() {} Socket(const Socket&) = delete; @@ -103,6 +115,10 @@ class Socket { size_t cb, SocketAddress* paddr, int64_t* timestamp) = 0; + // Intended to replace RecvFrom(void* ...). + // Default implementation calls RecvFrom(void* ...) with 64Kbyte buffer. + // Returns number of bytes received or a negative value on error. + virtual int RecvFrom(ReceiveBuffer& buffer); virtual int Listen(int backlog) = 0; virtual Socket* Accept(SocketAddress* paddr) = 0; virtual int Close() = 0; diff --git a/third_party/libwebrtc/rtc_base/socket_adapters.cc b/third_party/libwebrtc/rtc_base/socket_adapters.cc index f628929a46..a1eee5bd67 100644 --- a/third_party/libwebrtc/rtc_base/socket_adapters.cc +++ b/third_party/libwebrtc/rtc_base/socket_adapters.cc @@ -629,7 +629,7 @@ void AsyncSocksProxySocket::SendAuth() { size_t len = pass_.GetLength() + 1; char* sensitive = new char[len]; pass_.CopyTo(sensitive, true); - request.WriteBytes(sensitive, pass_.GetLength()); // Password + request.WriteString(std::string(sensitive, pass_.GetLength())); // Password ExplicitZeroMemory(sensitive, len); delete[] sensitive; DirectSend(request.Data(), request.Length()); diff --git a/third_party/libwebrtc/rtc_base/task_queue_for_test.cc b/third_party/libwebrtc/rtc_base/task_queue_for_test.cc index cb6b23ceae..e8993edcd1 100644 --- a/third_party/libwebrtc/rtc_base/task_queue_for_test.cc +++ b/third_party/libwebrtc/rtc_base/task_queue_for_test.cc @@ -10,12 +10,28 @@ #include "rtc_base/task_queue_for_test.h" +#include <memory> +#include <utility> + #include "api/task_queue/default_task_queue_factory.h" +#include "api/task_queue/task_queue_base.h" namespace webrtc { -TaskQueueForTest::TaskQueueForTest(absl::string_view name, Priority priority) - : TaskQueue( - CreateDefaultTaskQueueFactory()->CreateTaskQueue(name, priority)) {} +TaskQueueForTest::TaskQueueForTest( + std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue) + : impl_(std::move(task_queue)) {} + +TaskQueueForTest::TaskQueueForTest(absl::string_view name, + TaskQueueFactory::Priority priority) + : impl_(CreateDefaultTaskQueueFactory()->CreateTaskQueue(name, priority)) {} + +TaskQueueForTest::~TaskQueueForTest() { + // Stop the TaskQueue before invalidating impl_ pointer so that tasks that + // race with the TaskQueueForTest destructor could still use TaskQueueForTest + // functions like 'IsCurrent'. + impl_.get_deleter()(impl_.get()); + impl_.release(); +} } // namespace webrtc diff --git a/third_party/libwebrtc/rtc_base/task_queue_for_test.h b/third_party/libwebrtc/rtc_base/task_queue_for_test.h index 4c7f842abe..b54b1daefa 100644 --- a/third_party/libwebrtc/rtc_base/task_queue_for_test.h +++ b/third_party/libwebrtc/rtc_base/task_queue_for_test.h @@ -17,10 +17,9 @@ #include "absl/strings/string_view.h" #include "api/function_view.h" #include "api/task_queue/task_queue_base.h" +#include "api/task_queue/task_queue_factory.h" #include "rtc_base/checks.h" #include "rtc_base/event.h" -#include "rtc_base/task_queue.h" -#include "rtc_base/thread_annotations.h" namespace webrtc { @@ -38,14 +37,39 @@ inline void SendTask(TaskQueueBase* task_queue, /*warn_after=*/TimeDelta::Seconds(10))); } -class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue { +class TaskQueueForTest { public: - using rtc::TaskQueue::TaskQueue; - explicit TaskQueueForTest(absl::string_view name = "TestQueue", - Priority priority = Priority::NORMAL); + explicit TaskQueueForTest( + std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue); + explicit TaskQueueForTest( + absl::string_view name = "TestQueue", + TaskQueueFactory::Priority priority = TaskQueueFactory::Priority::NORMAL); TaskQueueForTest(const TaskQueueForTest&) = delete; TaskQueueForTest& operator=(const TaskQueueForTest&) = delete; - ~TaskQueueForTest() = default; + ~TaskQueueForTest(); + + bool IsCurrent() const { return impl_->IsCurrent(); } + + // Returns non-owning pointer to the task queue implementation. + TaskQueueBase* Get() { return impl_.get(); } + + void PostTask( + absl::AnyInvocable<void() &&> task, + const webrtc::Location& location = webrtc::Location::Current()) { + impl_->PostTask(std::move(task), location); + } + void PostDelayedTask( + absl::AnyInvocable<void() &&> task, + webrtc::TimeDelta delay, + const webrtc::Location& location = webrtc::Location::Current()) { + impl_->PostDelayedTask(std::move(task), delay, location); + } + void PostDelayedHighPrecisionTask( + absl::AnyInvocable<void() &&> task, + webrtc::TimeDelta delay, + const webrtc::Location& location = webrtc::Location::Current()) { + impl_->PostDelayedHighPrecisionTask(std::move(task), delay, location); + } // A convenience, test-only method that blocks the current thread while // a task executes on the task queue. @@ -61,6 +85,9 @@ class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue { // that all already posted tasks on the queue get executed. SendTask([]() {}); } + + private: + std::unique_ptr<TaskQueueBase, TaskQueueDeleter> impl_; }; } // namespace webrtc diff --git a/third_party/libwebrtc/rtc_base/task_queue_unittest.cc b/third_party/libwebrtc/rtc_base/task_queue_unittest.cc index 579dc3cced..eb5c5b16fb 100644 --- a/third_party/libwebrtc/rtc_base/task_queue_unittest.cc +++ b/third_party/libwebrtc/rtc_base/task_queue_unittest.cc @@ -28,10 +28,10 @@ #include "rtc_base/time_utils.h" #include "test/gtest.h" -namespace rtc { +namespace webrtc { namespace { -using ::webrtc::TimeDelta; + // Noop on all platforms except Windows, where it turns on high precision // multimedia timers which increases the precision of TimeMillis() while in // scope. @@ -51,12 +51,6 @@ class EnableHighResTimers { #endif }; -void CheckCurrent(Event* signal, TaskQueue* queue) { - EXPECT_TRUE(queue->IsCurrent()); - if (signal) - signal->Set(); -} - } // namespace // This task needs to be run manually due to the slowness of some of our bots. @@ -65,14 +59,18 @@ TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) { EnableHighResTimers high_res_scope; static const char kQueueName[] = "PostDelayedHighRes"; - Event event; - webrtc::TaskQueueForTest queue(kQueueName, TaskQueue::Priority::HIGH); + rtc::Event event; + TaskQueueForTest queue(kQueueName, TaskQueueFactory::Priority::HIGH); - uint32_t start = Time(); - queue.PostDelayedTask([&event, &queue] { CheckCurrent(&event, &queue); }, - TimeDelta::Millis(3)); - EXPECT_TRUE(event.Wait(webrtc::TimeDelta::Seconds(1))); - uint32_t end = TimeMillis(); + uint32_t start = rtc::TimeMillis(); + queue.PostDelayedTask( + [&event, &queue] { + EXPECT_TRUE(queue.IsCurrent()); + event.Set(); + }, + TimeDelta::Millis(3)); + EXPECT_TRUE(event.Wait(TimeDelta::Seconds(1))); + uint32_t end = rtc::TimeMillis(); // These tests are a little relaxed due to how "powerful" our test bots can // be. Most recently we've seen windows bots fire the callback after 94-99ms, // which is why we have a little bit of leeway backwards as well. @@ -80,4 +78,4 @@ TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) { EXPECT_NEAR(end - start, 3, 3u); } -} // namespace rtc +} // namespace webrtc diff --git a/third_party/libwebrtc/rtc_base/thread_unittest.cc b/third_party/libwebrtc/rtc_base/thread_unittest.cc index cd733db2cd..11ee2abc9f 100644 --- a/third_party/libwebrtc/rtc_base/thread_unittest.cc +++ b/third_party/libwebrtc/rtc_base/thread_unittest.cc @@ -22,6 +22,7 @@ #include "rtc_base/fake_clock.h" #include "rtc_base/gunit.h" #include "rtc_base/internal/default_socket_server.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/null_socket_server.h" #include "rtc_base/physical_socket_server.h" #include "rtc_base/ref_counted_object.h" @@ -84,20 +85,20 @@ class SocketClient : public TestGenerator, public sigslot::has_slots<> { : socket_(AsyncUDPSocket::Create(socket, addr)), post_thread_(post_thread), post_handler_(phandler) { - socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket); + socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnPacket(socket, packet); + }); } ~SocketClient() override { delete socket_; } SocketAddress address() const { return socket_->GetLocalAddress(); } - void OnPacket(AsyncPacketSocket* socket, - const char* buf, - size_t size, - const SocketAddress& remote_addr, - const int64_t& packet_time_us) { - EXPECT_EQ(size, sizeof(uint32_t)); - uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0]; + void OnPacket(AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + EXPECT_EQ(packet.payload().size(), sizeof(uint32_t)); + uint32_t prev = + reinterpret_cast<const uint32_t*>(packet.payload().data())[0]; uint32_t result = Next(prev); post_thread_->PostDelayedTask([post_handler_ = post_handler_, diff --git a/third_party/libwebrtc/rtc_base/virtual_socket_unittest.cc b/third_party/libwebrtc/rtc_base/virtual_socket_unittest.cc index 67585b1fcd..8efc9d8223 100644 --- a/third_party/libwebrtc/rtc_base/virtual_socket_unittest.cc +++ b/third_party/libwebrtc/rtc_base/virtual_socket_unittest.cc @@ -13,6 +13,8 @@ #include <stdlib.h> #include <string.h> #include <time.h> + +#include "rtc_base/network/received_packet.h" #if defined(WEBRTC_POSIX) #include <netinet/in.h> #endif @@ -101,7 +103,10 @@ struct Receiver : public sigslot::has_slots<> { sum(0), sum_sq(0), samples(0) { - socket->SignalReadPacket.connect(this, &Receiver::OnReadPacket); + socket->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); periodic = RepeatingTaskHandle::DelayedStart( thread, TimeDelta::Seconds(1), [this] { // It is always possible for us to receive more than expected because @@ -116,18 +121,15 @@ struct Receiver : public sigslot::has_slots<> { ~Receiver() override { periodic.Stop(); } - void OnReadPacket(AsyncPacketSocket* s, - const char* data, - size_t size, - const SocketAddress& remote_addr, - const int64_t& /* packet_time_us */) { + void OnReadPacket(AsyncPacketSocket* s, const rtc::ReceivedPacket& packet) { ASSERT_EQ(socket.get(), s); - ASSERT_GE(size, 4U); + ASSERT_GE(packet.payload().size(), 4U); - count += size; - sec_count += size; + count += packet.payload().size(); + sec_count += packet.payload().size(); - uint32_t send_time = *reinterpret_cast<const uint32_t*>(data); + uint32_t send_time = + *reinterpret_cast<const uint32_t*>(packet.payload().data()); uint32_t recv_time = rtc::TimeMillis(); uint32_t delay = recv_time - send_time; sum += delay; |