diff options
Diffstat (limited to 'Source/WebKit2/NetworkProcess/webrtc')
11 files changed, 910 insertions, 0 deletions
diff --git a/Source/WebKit2/NetworkProcess/webrtc/LibWebRTCSocketClient.cpp b/Source/WebKit2/NetworkProcess/webrtc/LibWebRTCSocketClient.cpp new file mode 100644 index 000000000..33c6be505 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/LibWebRTCSocketClient.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "LibWebRTCSocketClient.h" + +#if USE(LIBWEBRTC) + +#include "Connection.h" +#include "DataReference.h" +#include "NetworkRTCProvider.h" +#include "WebRTCSocketMessages.h" +#include <WebCore/SharedBuffer.h> +#include <wtf/Function.h> + +namespace WebKit { + +LibWebRTCSocketClient::LibWebRTCSocketClient(uint64_t identifier, NetworkRTCProvider& rtcProvider, std::unique_ptr<rtc::AsyncPacketSocket>&& socket, Type type) + : m_identifier(identifier) + , m_rtcProvider(rtcProvider) + , m_socket(WTFMove(socket)) +{ + if (!m_socket) { + rtcProvider.sendFromMainThread([identifier](IPC::Connection& connection) { + connection.send(Messages::WebRTCSocket::SignalClose(1), identifier); + }); + return; + } + + m_socket->SignalReadPacket.connect(this, &LibWebRTCSocketClient::signalReadPacket); + m_socket->SignalSentPacket.connect(this, &LibWebRTCSocketClient::signalSentPacket); + m_socket->SignalConnect.connect(this, &LibWebRTCSocketClient::signalConnect); + m_socket->SignalClose.connect(this, &LibWebRTCSocketClient::signalClose); + + if (type == Type::ClientTCP) { + m_socket->SignalAddressReady.connect(this, &LibWebRTCSocketClient::signalAddressReady); + return; + } + signalAddressReady(); +} + +void LibWebRTCSocketClient::sendTo(const WebCore::SharedBuffer& buffer, const rtc::SocketAddress& socketAddress, const rtc::PacketOptions& options) +{ + m_socket->SendTo(reinterpret_cast<const uint8_t*>(buffer.data()), buffer.size(), socketAddress, options); +} + +void LibWebRTCSocketClient::close() +{ + ASSERT(m_socket); + m_socket->Close(); + m_rtcProvider.takeSocket(m_identifier); +} + +void LibWebRTCSocketClient::setOption(int option, int value) +{ + ASSERT(m_socket); + m_socket->SetOption(static_cast<rtc::Socket::Option>(option), value); +} + +void LibWebRTCSocketClient::signalReadPacket(rtc::AsyncPacketSocket*, const char* value, size_t length, const rtc::SocketAddress& address, const rtc::PacketTime& packetTime) +{ + 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::WebRTCSocket::SignalReadPacket(data, RTCNetwork::IPAddress(address.ipaddr()), address.port(), packetTime.timestamp), identifier); + }); +} + +void LibWebRTCSocketClient::signalSentPacket(rtc::AsyncPacketSocket*, const rtc::SentPacket& sentPacket) +{ + m_rtcProvider.sendFromMainThread([identifier = m_identifier, sentPacket](IPC::Connection& connection) { + connection.send(Messages::WebRTCSocket::SignalSentPacket(sentPacket.packet_id, sentPacket.send_time_ms), identifier); + }); +} + +void LibWebRTCSocketClient::signalAddressReady(rtc::AsyncPacketSocket*, const rtc::SocketAddress& address) +{ + m_rtcProvider.sendFromMainThread([identifier = m_identifier, address = RTCNetwork::isolatedCopy(address)](IPC::Connection& connection) { + connection.send(Messages::WebRTCSocket::SignalAddressReady(RTCNetwork::SocketAddress(address)), identifier); + }); +} + +void LibWebRTCSocketClient::signalAddressReady() +{ + signalAddressReady(m_socket.get(), m_socket->GetLocalAddress()); +} + +void LibWebRTCSocketClient::signalConnect(rtc::AsyncPacketSocket*) +{ + m_rtcProvider.sendFromMainThread([identifier = m_identifier](IPC::Connection& connection) { + connection.send(Messages::WebRTCSocket::SignalConnect(), identifier); + }); +} + +void LibWebRTCSocketClient::signalClose(rtc::AsyncPacketSocket*, int error) +{ + m_rtcProvider.sendFromMainThread([identifier = m_identifier, error](IPC::Connection& connection) { + connection.send(Messages::WebRTCSocket::SignalClose(error), identifier); + }); + m_rtcProvider.takeSocket(m_identifier); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/LibWebRTCSocketClient.h b/Source/WebKit2/NetworkProcess/webrtc/LibWebRTCSocketClient.h new file mode 100644 index 000000000..375cfdf24 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/LibWebRTCSocketClient.h @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include <WebCore/LibWebRTCMacros.h> +#include <webrtc/base/asyncpacketsocket.h> +#include <webrtc/base/sigslot.h> + +namespace rtc { +class AsyncPacketSocket; +class SocketAddress; +struct PacketOptions; +struct PacketTime; +struct SentPacket; +} + +namespace WebCore { +class SharedBuffer; +} + +namespace WebKit { + +class NetworkRTCProvider; + +class LibWebRTCSocketClient final : public sigslot::has_slots<> { +public: + enum class Type { UDP, ServerTCP, ClientTCP }; + + LibWebRTCSocketClient(uint64_t identifier, NetworkRTCProvider&, std::unique_ptr<rtc::AsyncPacketSocket>&&, Type); + +private: + friend class NetworkRTCSocket; + + void close(); + void setOption(int option, int value); + void sendTo(const WebCore::SharedBuffer&, const rtc::SocketAddress&, const rtc::PacketOptions&); + + void signalReadPacket(rtc::AsyncPacketSocket*, const char*, size_t, const rtc::SocketAddress&, const rtc::PacketTime&); + void signalSentPacket(rtc::AsyncPacketSocket*, const rtc::SentPacket&); + void signalAddressReady(rtc::AsyncPacketSocket*, const rtc::SocketAddress&); + void signalConnect(rtc::AsyncPacketSocket*); + void signalClose(rtc::AsyncPacketSocket*, int); + + void signalAddressReady(); + + uint64_t m_identifier; + NetworkRTCProvider& m_rtcProvider; + std::unique_ptr<rtc::AsyncPacketSocket> m_socket; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.cpp b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.cpp new file mode 100644 index 000000000..e30b5eb07 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "NetworkRTCMonitor.h" + +#if USE(LIBWEBRTC) + +#include "Connection.h" +#include "NetworkRTCProvider.h" +#include "WebRTCMonitorMessages.h" +#include <wtf/Function.h> + +namespace WebKit { + +void NetworkRTCMonitor::startUpdating() +{ + m_isStarted = true; + m_rtcProvider.callOnRTCNetworkThread([this]() { + m_manager = std::make_unique<rtc::BasicNetworkManager>(); + m_manager->SignalNetworksChanged.connect(this, &NetworkRTCMonitor::onNetworksChanged); + m_manager->StartUpdating(); + }); +} + +void NetworkRTCMonitor::stopUpdating() +{ + m_isStarted = false; + m_rtcProvider.callOnRTCNetworkThread([this]() { + m_manager->StopUpdating(); + m_manager = nullptr; + }); +} + +void NetworkRTCMonitor::onNetworksChanged() +{ + rtc::BasicNetworkManager::NetworkList networks; + m_manager->GetNetworks(&networks); + + RTCNetwork::IPAddress ipv4; + m_manager->GetDefaultLocalAddress(AF_INET, &ipv4.value); + RTCNetwork::IPAddress ipv6; + m_manager->GetDefaultLocalAddress(AF_INET6, &ipv6.value); + + Vector<RTCNetwork> networkList; + networkList.reserveInitialCapacity(networks.size()); + for (auto* network : networks) { + ASSERT(network); + networkList.uncheckedAppend(RTCNetwork { *network }); + } + + m_rtcProvider.sendFromMainThread([this, networkList = WTFMove(networkList), ipv4 = WTFMove(ipv4), ipv6 = WTFMove(ipv6)](IPC::Connection& connection) { + if (!m_isStarted) + return; + connection.send(Messages::WebRTCMonitor::NetworksChanged(networkList, ipv4, ipv6), 0); + }); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.h b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.h new file mode 100644 index 000000000..0e07d84b7 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "WebCore/LibWebRTCMacros.h" +#include <webrtc/base/network.h> +#include <webrtc/base/sigslot.h> +#include <webrtc/base/thread.h> + +namespace IPC { +class Connection; +class Decoder; +} + +namespace WebKit { + +class NetworkRTCProvider; + +class NetworkRTCMonitor final : public sigslot::has_slots<> { +public: + explicit NetworkRTCMonitor(NetworkRTCProvider& rtcProvider) : m_rtcProvider(rtcProvider) { } + + void didReceiveMessage(IPC::Connection&, IPC::Decoder&); + void stopUpdating(); + +private: + void startUpdating(); + + void onNetworksChanged(); + + NetworkRTCProvider& m_rtcProvider; + std::unique_ptr<rtc::BasicNetworkManager> m_manager; + bool m_isStarted { false }; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.messages.in b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.messages.in new file mode 100644 index 000000000..51b15dd28 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCMonitor.messages.in @@ -0,0 +1,30 @@ +# Copyright (C) 2017 Apple Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#if USE(LIBWEBRTC) + +messages -> NetworkRTCMonitor { + void StartUpdating() + void StopUpdating() +} + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.cpp b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.cpp new file mode 100644 index 000000000..a64f0f2cd --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.cpp @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "NetworkRTCProvider.h" + +#if USE(LIBWEBRTC) + +#include "NetworkConnectionToWebProcess.h" +#include "NetworkProcess.h" +#include "NetworkRTCSocket.h" +#include "WebRTCResolverMessages.h" +#include <WebCore/LibWebRTCMacros.h> +#include <webrtc/base/asyncpacketsocket.h> +#include <wtf/MainThread.h> +#include <wtf/text/WTFString.h> + +namespace WebKit { + +static inline std::unique_ptr<rtc::Thread> createThread() +{ + auto thread = rtc::Thread::CreateWithSocketServer(); + auto result = thread->Start(); + ASSERT_UNUSED(result, result); + // FIXME: Set thread name. + return thread; +} + +NetworkRTCProvider::NetworkRTCProvider(NetworkConnectionToWebProcess& connection) + : m_connection(&connection) + , m_rtcMonitor(*this) + , m_rtcNetworkThread(createThread()) + , m_packetSocketFactory(makeUniqueRef<rtc::BasicPacketSocketFactory>(m_rtcNetworkThread.get())) +{ +} + +void NetworkRTCProvider::close() +{ + m_connection = nullptr; + m_resolvers.clear(); + m_rtcMonitor.stopUpdating(); + + callOnRTCNetworkThread([this]() { + m_sockets.clear(); + }); +} + +void NetworkRTCProvider::createUDPSocket(uint64_t identifier, const RTCNetwork::SocketAddress& address, uint16_t minPort, uint16_t maxPort) +{ + callOnRTCNetworkThread([this, identifier, address = RTCNetwork::isolatedCopy(address.value), minPort, maxPort]() { + std::unique_ptr<rtc::AsyncPacketSocket> socket(m_packetSocketFactory->CreateUdpSocket(address, minPort, maxPort)); + addSocket(identifier, std::make_unique<LibWebRTCSocketClient>(identifier, *this, WTFMove(socket), LibWebRTCSocketClient::Type::UDP)); + }); +} + +void NetworkRTCProvider::createServerTCPSocket(uint64_t identifier, const RTCNetwork::SocketAddress& address, uint16_t minPort, uint16_t maxPort, int options) +{ + callOnRTCNetworkThread([this, identifier, address = RTCNetwork::isolatedCopy(address.value), minPort, maxPort, options]() { + std::unique_ptr<rtc::AsyncPacketSocket> socket(m_packetSocketFactory->CreateServerTcpSocket(address, minPort, maxPort, options)); + addSocket(identifier, std::make_unique<LibWebRTCSocketClient>(identifier, *this, WTFMove(socket), LibWebRTCSocketClient::Type::ServerTCP)); + }); +} + +void NetworkRTCProvider::createClientTCPSocket(uint64_t identifier, const RTCNetwork::SocketAddress& localAddress, const RTCNetwork::SocketAddress& remoteAddress, int options) +{ + callOnRTCNetworkThread([this, identifier, localAddress = RTCNetwork::isolatedCopy(localAddress.value), remoteAddress = RTCNetwork::isolatedCopy(remoteAddress.value), options]() { + std::unique_ptr<rtc::AsyncPacketSocket> socket(m_packetSocketFactory->CreateClientTcpSocket(localAddress, remoteAddress, { }, { }, options)); + addSocket(identifier, std::make_unique<LibWebRTCSocketClient>(identifier, *this, WTFMove(socket), LibWebRTCSocketClient::Type::ClientTCP)); + }); +} + +void NetworkRTCProvider::addSocket(uint64_t identifier, std::unique_ptr<LibWebRTCSocketClient>&& socket) +{ + m_sockets.add(identifier, WTFMove(socket)); +} + +std::unique_ptr<LibWebRTCSocketClient> NetworkRTCProvider::takeSocket(uint64_t identifier) +{ + return m_sockets.take(identifier); +} + +void NetworkRTCProvider::didReceiveNetworkRTCSocketMessage(IPC::Connection& connection, IPC::Decoder& decoder) +{ + NetworkRTCSocket(decoder.destinationID(), *this).didReceiveMessage(connection, decoder); +} + +void NetworkRTCProvider::createResolver(uint64_t identifier, const String& address) +{ + CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, address.createCFString().get()); + ASSERT(host); + + auto resolver = std::make_unique<Resolver>(identifier, *this, host); + + CFHostClientContext context = { 0, resolver.get(), nullptr, nullptr, nullptr }; + CFHostSetClient(host, NetworkRTCProvider::resolvedName, &context); + CFHostScheduleWithRunLoop(host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + Boolean result = CFHostStartInfoResolution(host, kCFHostAddresses, nullptr); + ASSERT_UNUSED(result, result); + + m_resolvers.add(identifier, WTFMove(resolver)); +} + +void NetworkRTCProvider::stopResolver(uint64_t identifier) +{ + auto resolver = m_resolvers.take(identifier); + if (resolver) + CFHostCancelInfoResolution(resolver->host, CFHostInfoType::kCFHostAddresses); +} + +void NetworkRTCProvider::resolvedName(CFHostRef hostRef, CFHostInfoType typeInfo, const CFStreamError *error, void *info) +{ + ASSERT_UNUSED(typeInfo, !typeInfo); + + if (error->domain) { + // FIXME: Need to handle failure, but info is not provided in the callback. + return; + } + + ASSERT(info); + auto* resolverInfo = static_cast<Resolver*>(info); + auto resolver = resolverInfo->rtcProvider.m_resolvers.take(resolverInfo->identifier); + if (!resolver) + return; + + Boolean result; + CFArrayRef resolvedAddresses = (CFArrayRef)CFHostGetAddressing(hostRef, &result); + ASSERT_UNUSED(result, result); + + size_t count = CFArrayGetCount(resolvedAddresses); + Vector<RTCNetwork::IPAddress> addresses; + addresses.reserveInitialCapacity(count); + + for (size_t index = 0; index < count; ++index) { + CFDataRef data = (CFDataRef)CFArrayGetValueAtIndex(resolvedAddresses, index); + auto* address = reinterpret_cast<const struct sockaddr_in*>(CFDataGetBytePtr(data)); + addresses.uncheckedAppend(RTCNetwork::IPAddress(rtc::IPAddress(address->sin_addr))); + } + ASSERT(resolver->rtcProvider.m_connection); + resolver->rtcProvider.m_connection->connection().send(Messages::WebRTCResolver::SetResolvedAddress(addresses), resolver->identifier); +} + +struct NetworkMessageData : public rtc::MessageData { + NetworkMessageData(Ref<NetworkRTCProvider>&& rtcProvider, Function<void()>&& callback) + : rtcProvider(WTFMove(rtcProvider)) + , callback(WTFMove(callback)) + { } + Ref<NetworkRTCProvider> rtcProvider; + Function<void()> callback; +}; + +void NetworkRTCProvider::OnMessage(rtc::Message* message) +{ + ASSERT(message->message_id == 1); + static_cast<NetworkMessageData*>(message->pdata)->callback(); +} + +void NetworkRTCProvider::callOnRTCNetworkThread(Function<void()>&& callback) +{ + m_rtcNetworkThread->Post(RTC_FROM_HERE, this, 1, new NetworkMessageData(*this, WTFMove(callback))); +} + +void NetworkRTCProvider::callSocket(uint64_t identifier, Function<void(LibWebRTCSocketClient&)>&& callback) +{ + callOnRTCNetworkThread([this, identifier, callback = WTFMove(callback)]() { + if (auto* socket = m_sockets.get(identifier)) + callback(*socket); + }); +} + +void NetworkRTCProvider::sendFromMainThread(Function<void(IPC::Connection&)>&& callback) +{ + callOnMainThread([provider = makeRef(*this), callback = WTFMove(callback)]() { + if (provider->m_connection) + callback(provider->m_connection->connection()); + }); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.h b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.h new file mode 100644 index 000000000..6a72182be --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.h @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "LibWebRTCSocketClient.h" +#include "NetworkRTCMonitor.h" +#include "RTCNetwork.h" +#include <CFNetwork/CFHost.h> +#include <WebCore/LibWebRTCMacros.h> +#include <webrtc/base/sigslot.h> +#include <webrtc/p2p/base/basicpacketsocketfactory.h> +#include <wtf/HashMap.h> +#include <wtf/ThreadSafeRefCounted.h> +#include <wtf/UniqueRef.h> +#include <wtf/text/WTFString.h> + +namespace IPC { +class Connection; +class Decoder; +} + +namespace WebKit { + +class NetworkConnectionToWebProcess; +class NetworkRTCSocket; + +class NetworkRTCProvider : public ThreadSafeRefCounted<NetworkRTCProvider>, public rtc::MessageHandler { +public: + static Ref<NetworkRTCProvider> create(NetworkConnectionToWebProcess& connection) { return adoptRef(*new NetworkRTCProvider(connection)); } + + void didReceiveMessage(IPC::Connection&, IPC::Decoder&); + void didReceiveNetworkRTCMonitorMessage(IPC::Connection& connection, IPC::Decoder& decoder) { m_rtcMonitor.didReceiveMessage(connection, decoder); } + void didReceiveNetworkRTCSocketMessage(IPC::Connection&, IPC::Decoder&); + + std::unique_ptr<LibWebRTCSocketClient> takeSocket(uint64_t); + void resolverDone(uint64_t); + + void close(); + + void callSocket(uint64_t, Function<void(LibWebRTCSocketClient&)>&&); + void callOnRTCNetworkThread(Function<void()>&&); + void sendFromMainThread(Function<void(IPC::Connection&)>&&); + +private: + explicit NetworkRTCProvider(NetworkConnectionToWebProcess&); + + void createUDPSocket(uint64_t, const RTCNetwork::SocketAddress&, uint16_t, uint16_t); + void createClientTCPSocket(uint64_t, const RTCNetwork::SocketAddress&, const RTCNetwork::SocketAddress&, int); + void createServerTCPSocket(uint64_t, const RTCNetwork::SocketAddress&, uint16_t minPort, uint16_t maxPort, int); + void createResolver(uint64_t, const String&); + void stopResolver(uint64_t); + + void addSocket(uint64_t, std::unique_ptr<LibWebRTCSocketClient>&&); + + void OnMessage(rtc::Message*); + + static void resolvedName(CFHostRef, CFHostInfoType, const CFStreamError*, void*); + + struct Resolver { + Resolver(uint64_t identifier, NetworkRTCProvider& rtcProvider, RetainPtr<CFHostRef>&& host) + : identifier(identifier) + , rtcProvider(rtcProvider) + , host(WTFMove(host)) { } + ~Resolver() { CFRelease(host); } + + uint64_t identifier; + NetworkRTCProvider& rtcProvider; + CFHostRef host; + }; + + HashMap<uint64_t, std::unique_ptr<Resolver>> m_resolvers; + HashMap<uint64_t, std::unique_ptr<LibWebRTCSocketClient>> m_sockets; + NetworkConnectionToWebProcess* m_connection; + bool m_isStarted { true }; + + NetworkRTCMonitor m_rtcMonitor; + + std::unique_ptr<rtc::Thread> m_rtcNetworkThread; + UniqueRef<rtc::BasicPacketSocketFactory> m_packetSocketFactory; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.messages.in b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.messages.in new file mode 100644 index 000000000..940952144 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCProvider.messages.in @@ -0,0 +1,33 @@ +# Copyright (C) 2017 Apple Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#if USE(LIBWEBRTC) + +messages -> NetworkRTCProvider { + CreateUDPSocket(int identifier, WebKit::RTCNetwork::SocketAddress localAddress, uint16_t minPort, uint16_t maxPort) + CreateServerTCPSocket(int identifier, WebKit::RTCNetwork::SocketAddress localAddress, uint16_t minPort, uint16_t maxPort, int options) + CreateClientTCPSocket(int identifier, WebKit::RTCNetwork::SocketAddress localAddress, WebKit::RTCNetwork::SocketAddress remoteAddress, int options) + CreateResolver(uint64_t identifier, String address) + StopResolver(uint64_t identifier) +} + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.cpp b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.cpp new file mode 100644 index 000000000..fcd12498f --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "NetworkRTCSocket.h" + +#if USE(LIBWEBRTC) + +#include "DataReference.h" +#include "LibWebRTCSocketClient.h" +#include "NetworkRTCProvider.h" +#include <WebCore/SharedBuffer.h> +#include <wtf/Function.h> + +namespace WebKit { + +NetworkRTCSocket::NetworkRTCSocket(uint64_t identifier, NetworkRTCProvider& rtcProvider) + : m_identifier(identifier) + , m_rtcProvider(rtcProvider) +{ +} + +void NetworkRTCSocket::sendTo(const IPC::DataReference& data, const RTCNetwork::SocketAddress& socketAddress, int packetID, int rtpSendtimeExtensionID, String srtpAuth, int64_t srtpPacketIndex, int dscp) +{ + auto buffer = WebCore::SharedBuffer::create(data.data(), data.size()); + + rtc::PacketOptions options; + options.packet_id = packetID; + options.packet_time_params.rtp_sendtime_extension_id = rtpSendtimeExtensionID; + options.packet_time_params.srtp_packet_index = srtpPacketIndex; + options.dscp = static_cast<rtc::DiffServCodePoint>(dscp); + if (srtpAuth.utf8().length()) { + options.packet_time_params.srtp_auth_key = std::vector<char>(srtpAuth.utf8().data(), srtpAuth.utf8().data() + srtpAuth.utf8().length()); + options.packet_time_params.srtp_auth_tag_len = srtpAuth.utf8().length(); + } else + options.packet_time_params.srtp_auth_tag_len = -1; + + m_rtcProvider.callSocket(m_identifier, [buffer = WTFMove(buffer), socketAddress, options](LibWebRTCSocketClient& client) { + client.sendTo(buffer.get(), socketAddress.value, options); + }); +} + +void NetworkRTCSocket::close() +{ + m_rtcProvider.callSocket(m_identifier, [](LibWebRTCSocketClient& socket) { + socket.close(); + }); +} + +void NetworkRTCSocket::setOption(int option, int value) +{ + m_rtcProvider.callSocket(m_identifier, [option, value](LibWebRTCSocketClient& socket) { + socket.setOption(option, value); + }); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.h b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.h new file mode 100644 index 000000000..ea99540e6 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "RTCNetwork.h" + +#include <WebCore/LibWebRTCMacros.h> +#include <webrtc/base/asyncpacketsocket.h> +#include <webrtc/base/sigslot.h> + +namespace IPC { +class Connection; +class DataReference; +} + +namespace rtc { +class AsyncPacketSocket; +class SocketAddress; +struct PacketOptions; +struct PacketTime; +struct SentPacket; +} + +namespace WebCore { +class SharedBuffer; +} + +namespace WebKit { + +class NetworkConnectionToWebProcess; +class NetworkRTCProvider; + +class NetworkRTCSocket { +public: + NetworkRTCSocket(uint64_t, NetworkRTCProvider&); + void didReceiveMessage(IPC::Connection&, IPC::Decoder&); +private: + void sendTo(const IPC::DataReference&, const RTCNetwork::SocketAddress&, int packetID, int rtpSendtimeExtensionID, String srtpAuth, int64_t srtpPacketIndex, int dscp); + void close(); + void setOption(int option, int value); + + uint64_t m_identifier; + NetworkRTCProvider& m_rtcProvider; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.messages.in b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.messages.in new file mode 100644 index 000000000..713d423e8 --- /dev/null +++ b/Source/WebKit2/NetworkProcess/webrtc/NetworkRTCSocket.messages.in @@ -0,0 +1,31 @@ +# Copyright (C) 2017 Apple Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#if USE(LIBWEBRTC) + +messages -> NetworkRTCSocket { + void SendTo(IPC::DataReference data, WebKit::RTCNetwork::SocketAddress address, int packetID, int rtpSendtimeExtensionID, String srtpAuth, int64_t srtpPacketIndex, int dscp) + void Close() + void SetOption(int option, int value) +} + +#endif // USE(LIBWEBRTC)s |