Send WebRTC packets received from network process to web process from a background...
authoryouenn@apple.com <youenn@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 29 Jun 2020 18:27:54 +0000 (18:27 +0000)
committeryouenn@apple.com <youenn@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 29 Jun 2020 18:27:54 +0000 (18:27 +0000)
https://bugs.webkit.org/show_bug.cgi?id=213548

Reviewed by Eric Carlson.

Make LibWebRTCSocketClient take a ref to an IPC connection at creation time.
Use this connection from the RTC network thread to send received packets to improve performances.
Covered by existing tests.

* NetworkProcess/webrtc/LibWebRTCSocketClient.cpp:
(WebKit::LibWebRTCSocketClient::LibWebRTCSocketClient):
(WebKit::LibWebRTCSocketClient::signalReadPacket):
(WebKit::LibWebRTCSocketClient::signalSentPacket):
(WebKit::LibWebRTCSocketClient::signalAddressReady):
(WebKit::LibWebRTCSocketClient::signalConnect):
(WebKit::LibWebRTCSocketClient::signalClose):
* NetworkProcess/webrtc/LibWebRTCSocketClient.h:
* NetworkProcess/webrtc/NetworkRTCProvider.cpp:
(WebKit::NetworkRTCProvider::createSocket):
(WebKit::NetworkRTCProvider::createUDPSocket):
(WebKit::NetworkRTCProvider::createServerTCPSocket):
(WebKit::NetworkRTCProvider::createClientTCPSocket):
(WebKit::NetworkRTCProvider::wrapNewTCPConnection):
* NetworkProcess/webrtc/NetworkRTCProvider.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@263670 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/NetworkProcess/webrtc/LibWebRTCSocketClient.cpp
Source/WebKit/NetworkProcess/webrtc/LibWebRTCSocketClient.h
Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.cpp
Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.h

index 2dbaee2..eca4091 100644 (file)
@@ -1,3 +1,30 @@
+2020-06-29  Youenn Fablet  <youenn@apple.com>
+
+        Send WebRTC packets received from network process to web process from a background thread
+        https://bugs.webkit.org/show_bug.cgi?id=213548
+
+        Reviewed by Eric Carlson.
+
+        Make LibWebRTCSocketClient take a ref to an IPC connection at creation time.
+        Use this connection from the RTC network thread to send received packets to improve performances.
+        Covered by existing tests.
+
+        * NetworkProcess/webrtc/LibWebRTCSocketClient.cpp:
+        (WebKit::LibWebRTCSocketClient::LibWebRTCSocketClient):
+        (WebKit::LibWebRTCSocketClient::signalReadPacket):
+        (WebKit::LibWebRTCSocketClient::signalSentPacket):
+        (WebKit::LibWebRTCSocketClient::signalAddressReady):
+        (WebKit::LibWebRTCSocketClient::signalConnect):
+        (WebKit::LibWebRTCSocketClient::signalClose):
+        * NetworkProcess/webrtc/LibWebRTCSocketClient.h:
+        * NetworkProcess/webrtc/NetworkRTCProvider.cpp:
+        (WebKit::NetworkRTCProvider::createSocket):
+        (WebKit::NetworkRTCProvider::createUDPSocket):
+        (WebKit::NetworkRTCProvider::createServerTCPSocket):
+        (WebKit::NetworkRTCProvider::createClientTCPSocket):
+        (WebKit::NetworkRTCProvider::wrapNewTCPConnection):
+        * NetworkProcess/webrtc/NetworkRTCProvider.h:
+
 2020-06-29  Elliot  <CheeseEBoi@mailo.com>
 
         [GTK] Dark mode for GTK themes that end with -Dark
index 515e87b..4e64c11 100644 (file)
 
 namespace WebKit {
 
-LibWebRTCSocketClient::LibWebRTCSocketClient(WebCore::LibWebRTCSocketIdentifier identifier, NetworkRTCProvider& rtcProvider, std::unique_ptr<rtc::AsyncPacketSocket>&& socket, Type type)
+LibWebRTCSocketClient::LibWebRTCSocketClient(WebCore::LibWebRTCSocketIdentifier identifier, NetworkRTCProvider& rtcProvider, std::unique_ptr<rtc::AsyncPacketSocket>&& socket, Type type, Ref<IPC::Connection>&& connection)
     : m_identifier(identifier)
     , m_type(type)
     , m_rtcProvider(rtcProvider)
     , m_socket(WTFMove(socket))
+    , m_connection(WTFMove(connection))
 {
     ASSERT(m_socket);
 
@@ -97,19 +98,14 @@ void LibWebRTCSocketClient::setOption(int option, int value)
 void LibWebRTCSocketClient::signalReadPacket(rtc::AsyncPacketSocket* socket, const char* value, size_t length, const rtc::SocketAddress& address, const rtc::PacketTime& packetTime)
 {
     ASSERT_UNUSED(socket, m_socket.get() == socket);
-    auto buffer = WebCore::SharedBuffer::create(value, length);
-    m_rtcProvider.sendFromMainThread([identifier = m_identifier, buffer = WTFMove(buffer), address = RTCNetwork::isolatedCopy(address), packetTime](IPC::Connection& connection) {
-        IPC::DataReference data(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size());
-        connection.send(Messages::LibWebRTCNetwork::SignalReadPacket(identifier, data, RTCNetwork::IPAddress(address.ipaddr()), address.port(), packetTime), 0);
-    });
+    IPC::DataReference data(reinterpret_cast<const uint8_t*>(value), length);
+    m_connection->send(Messages::LibWebRTCNetwork::SignalReadPacket(m_identifier, data, RTCNetwork::IPAddress(address.ipaddr()), address.port(), packetTime), 0);
 }
 
 void LibWebRTCSocketClient::signalSentPacket(rtc::AsyncPacketSocket* socket, const rtc::SentPacket& sentPacket)
 {
     ASSERT_UNUSED(socket, m_socket.get() == socket);
-    m_rtcProvider.sendFromMainThread([identifier = m_identifier, sentPacket](IPC::Connection& connection) {
-        connection.send(Messages::LibWebRTCNetwork::SignalSentPacket(identifier, sentPacket.packet_id, sentPacket.send_time_ms), 0);
-    });
+    m_connection->send(Messages::LibWebRTCNetwork::SignalSentPacket(m_identifier, sentPacket.packet_id, sentPacket.send_time_ms), 0);
 }
 
 void LibWebRTCSocketClient::signalNewConnection(rtc::AsyncPacketSocket* socket, rtc::AsyncPacketSocket* newSocket)
@@ -121,9 +117,7 @@ void LibWebRTCSocketClient::signalNewConnection(rtc::AsyncPacketSocket* socket,
 void LibWebRTCSocketClient::signalAddressReady(rtc::AsyncPacketSocket* socket, const rtc::SocketAddress& address)
 {
     ASSERT_UNUSED(socket, m_socket.get() == socket);
-    m_rtcProvider.sendFromMainThread([identifier = m_identifier, address = RTCNetwork::isolatedCopy(address)](IPC::Connection& connection) {
-        connection.send(Messages::LibWebRTCNetwork::SignalAddressReady(identifier, RTCNetwork::SocketAddress(address)), 0);
-    });
+    m_connection->send(Messages::LibWebRTCNetwork::SignalAddressReady(m_identifier, RTCNetwork::SocketAddress(address)), 0);
 }
 
 void LibWebRTCSocketClient::signalAddressReady()
@@ -134,17 +128,14 @@ void LibWebRTCSocketClient::signalAddressReady()
 void LibWebRTCSocketClient::signalConnect(rtc::AsyncPacketSocket* socket)
 {
     ASSERT_UNUSED(socket, m_socket.get() == socket);
-    m_rtcProvider.sendFromMainThread([identifier = m_identifier](IPC::Connection& connection) {
-        connection.send(Messages::LibWebRTCNetwork::SignalConnect(identifier), 0);
-    });
+    m_connection->send(Messages::LibWebRTCNetwork::SignalConnect(m_identifier), 0);
 }
 
 void LibWebRTCSocketClient::signalClose(rtc::AsyncPacketSocket* socket, int error)
 {
     ASSERT_UNUSED(socket, m_socket.get() == socket);
-    m_rtcProvider.sendFromMainThread([identifier = m_identifier, error](IPC::Connection& connection) {
-        connection.send(Messages::LibWebRTCNetwork::SignalClose(identifier, error), 0);
-    });
+    m_connection->send(Messages::LibWebRTCNetwork::SignalClose(m_identifier, error), 0);
+
     // We want to remove 'this' from the socket map now but we will destroy it asynchronously
     // so that the socket parameter of signalClose remains alive as the caller of signalClose may actually being using it afterwards.
     m_rtcProvider.callOnRTCNetworkThread([socket = m_rtcProvider.takeSocket(m_identifier)] { });
index 5b7cc50..8e3f480 100644 (file)
@@ -41,7 +41,7 @@ namespace WebKit {
 class LibWebRTCSocketClient final : public NetworkRTCProvider::Socket, public sigslot::has_slots<> {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    LibWebRTCSocketClient(WebCore::LibWebRTCSocketIdentifier, NetworkRTCProvider&, std::unique_ptr<rtc::AsyncPacketSocket>&&, Type);
+    LibWebRTCSocketClient(WebCore::LibWebRTCSocketIdentifier, NetworkRTCProvider&, std::unique_ptr<rtc::AsyncPacketSocket>&&, Type, Ref<IPC::Connection>&&);
 
 private:
     WebCore::LibWebRTCSocketIdentifier identifier() const final { return m_identifier; }
@@ -64,6 +64,7 @@ private:
     Type m_type;
     NetworkRTCProvider& m_rtcProvider;
     std::unique_ptr<rtc::AsyncPacketSocket> m_socket;
+    Ref<IPC::Connection> m_connection;
     int m_sendError { 0 };
 };
 
index 2a005af..979cc53 100644 (file)
@@ -113,23 +113,21 @@ void NetworkRTCProvider::close()
     });
 }
 
-void NetworkRTCProvider::createSocket(LibWebRTCSocketIdentifier identifier, std::unique_ptr<rtc::AsyncPacketSocket>&& socket, Socket::Type type)
+void NetworkRTCProvider::createSocket(LibWebRTCSocketIdentifier identifier, std::unique_ptr<rtc::AsyncPacketSocket>&& socket, Socket::Type type, Ref<IPC::Connection>&& connection)
 {
     if (!socket) {
-        sendFromMainThread([this, identifier, size = m_sockets.size()](IPC::Connection& connection) {
-            RELEASE_LOG_ERROR_IF_ALLOWED("createSocket with %u sockets is unable to create a new socket", size);
-            connection.send(Messages::LibWebRTCNetwork::SignalClose(identifier, 1), 0);
-        });
+        RELEASE_LOG_ERROR_IF_ALLOWED("createSocket with %u sockets is unable to create a new socket", m_sockets.size());
+        connection->send(Messages::LibWebRTCNetwork::SignalClose(identifier, 1), 0);
         return;
     }
-    addSocket(identifier, makeUnique<LibWebRTCSocketClient>(identifier, *this, WTFMove(socket), type));
+    addSocket(identifier, makeUnique<LibWebRTCSocketClient>(identifier, *this, WTFMove(socket), type, WTFMove(connection)));
 }
 
 void NetworkRTCProvider::createUDPSocket(LibWebRTCSocketIdentifier identifier, const RTCNetwork::SocketAddress& address, uint16_t minPort, uint16_t maxPort)
 {
-    callOnRTCNetworkThread([this, identifier, address = RTCNetwork::isolatedCopy(address.value), minPort, maxPort]() {
+    callOnRTCNetworkThread([this, identifier, address = RTCNetwork::isolatedCopy(address.value), minPort, maxPort, connection = makeRef(m_connection->connection())]() mutable {
         std::unique_ptr<rtc::AsyncPacketSocket> socket(m_packetSocketFactory->CreateUdpSocket(address, minPort, maxPort));
-        createSocket(identifier, WTFMove(socket), Socket::Type::UDP);
+        createSocket(identifier, WTFMove(socket), Socket::Type::UDP, WTFMove(connection));
     });
 }
 
@@ -141,9 +139,9 @@ void NetworkRTCProvider::createServerTCPSocket(LibWebRTCSocketIdentifier identif
         return;
     }
 
-    callOnRTCNetworkThread([this, identifier, address = RTCNetwork::isolatedCopy(address.value), minPort, maxPort, options]() {
+    callOnRTCNetworkThread([this, identifier, address = RTCNetwork::isolatedCopy(address.value), minPort, maxPort, options, connection = makeRef(m_connection->connection())]() mutable {
         std::unique_ptr<rtc::AsyncPacketSocket> socket(m_packetSocketFactory->CreateServerTcpSocket(address, minPort, maxPort, options));
-        createSocket(identifier, WTFMove(socket), Socket::Type::ServerTCP);
+        createSocket(identifier, WTFMove(socket), Socket::Type::ServerTCP, WTFMove(connection));
     });
 }
 
@@ -161,21 +159,21 @@ void NetworkRTCProvider::createClientTCPSocket(LibWebRTCSocketIdentifier identif
         m_connection->connection().send(Messages::LibWebRTCNetwork::SignalClose(identifier, 1), 0);
         return;
     }
-    callOnRTCNetworkThread([this, identifier, localAddress = RTCNetwork::isolatedCopy(localAddress.value), remoteAddress = RTCNetwork::isolatedCopy(remoteAddress.value), proxyInfo = proxyInfoFromSession(remoteAddress, *session), userAgent = WTFMove(userAgent).isolatedCopy(), options]() {
+    callOnRTCNetworkThread([this, identifier, localAddress = RTCNetwork::isolatedCopy(localAddress.value), remoteAddress = RTCNetwork::isolatedCopy(remoteAddress.value), proxyInfo = proxyInfoFromSession(remoteAddress, *session), userAgent = WTFMove(userAgent).isolatedCopy(), options, connection = makeRef(m_connection->connection())]() mutable {
         rtc::PacketSocketTcpOptions tcpOptions;
         tcpOptions.opts = options;
         std::unique_ptr<rtc::AsyncPacketSocket> socket(m_packetSocketFactory->CreateClientTcpSocket(localAddress, remoteAddress, proxyInfo, userAgent.utf8().data(), tcpOptions));
-        createSocket(identifier, WTFMove(socket), Socket::Type::ClientTCP);
+        createSocket(identifier, WTFMove(socket), Socket::Type::ClientTCP, WTFMove(connection));
     });
 }
 
 void NetworkRTCProvider::wrapNewTCPConnection(LibWebRTCSocketIdentifier identifier, LibWebRTCSocketIdentifier newConnectionSocketIdentifier)
 {
-    callOnRTCNetworkThread([this, identifier, newConnectionSocketIdentifier]() {
+    callOnRTCNetworkThread([this, identifier, newConnectionSocketIdentifier, connection = makeRef(m_connection->connection())]() mutable {
         auto socket = m_pendingIncomingSockets.take(newConnectionSocketIdentifier);
         RELEASE_LOG_IF(!socket, WebRTC, "NetworkRTCProvider::wrapNewTCPConnection received an invalid socket identifier");
         if (socket)
-            addSocket(identifier, makeUnique<LibWebRTCSocketClient>(identifier, *this, WTFMove(socket), Socket::Type::ServerConnectionTCP));
+            addSocket(identifier, makeUnique<LibWebRTCSocketClient>(identifier, *this, WTFMove(socket), Socket::Type::ServerConnectionTCP, WTFMove(connection)));
     });
 }
 
index c8d7cf4..2ec20d1 100644 (file)
@@ -110,7 +110,7 @@ private:
 
     void addSocket(WebCore::LibWebRTCSocketIdentifier, std::unique_ptr<Socket>&&);
 
-    void createSocket(WebCore::LibWebRTCSocketIdentifier, std::unique_ptr<rtc::AsyncPacketSocket>&&, Socket::Type);
+    void createSocket(WebCore::LibWebRTCSocketIdentifier, std::unique_ptr<rtc::AsyncPacketSocket>&&, Socket::Type, Ref<IPC::Connection>&&);
 
     void OnMessage(rtc::Message*);