| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/pc/ortcfactory.h" | |
| 12 | |
| 13 #include <string> | |
| 14 #include <utility> // For std::move. | |
| 15 | |
| 16 #include "webrtc/base/bind.h" | |
| 17 #include "webrtc/base/asyncpacketsocket.h" | |
| 18 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | |
| 19 #include "webrtc/p2p/base/udptransport.h" | |
| 20 | |
| 21 namespace webrtc { | |
| 22 | |
| 23 // static | |
| 24 std::unique_ptr<OrtcFactoryInterface> OrtcFactoryInterface::Create( | |
| 25 rtc::Thread* network_thread, | |
| 26 rtc::Thread* signaling_thread, | |
| 27 rtc::NetworkManager* network_manager, | |
| 28 rtc::PacketSocketFactory* socket_factory) { | |
| 29 // Hop to signaling thread if needed. | |
| 30 if (signaling_thread && !signaling_thread->IsCurrent()) { | |
| 31 // The template parameters are necessary because there are two | |
| 32 // OrtcFactoryInterface::Create methods, so the types can't be derived from | |
| 33 // just the function pointer. | |
| 34 return signaling_thread->Invoke<std::unique_ptr<OrtcFactoryInterface>>( | |
| 35 RTC_FROM_HERE, | |
| 36 rtc::Bind<std::unique_ptr<OrtcFactoryInterface>, rtc::Thread*, | |
| 37 rtc::Thread*, rtc::NetworkManager*, | |
| 38 rtc::PacketSocketFactory*>(&OrtcFactoryInterface::Create, | |
| 39 network_thread, signaling_thread, | |
| 40 network_manager, socket_factory)); | |
| 41 } | |
| 42 OrtcFactory* new_factory = | |
| 43 new OrtcFactory(network_thread, signaling_thread, | |
| 44 network_manager, socket_factory); | |
| 45 // Return a proxy so that any calls on the returned object (including | |
| 46 // destructor) happen on the signaling thread. | |
| 47 return OrtcFactoryProxy::Create(new_factory->signaling_thread(), | |
| 48 new_factory->network_thread(), new_factory); | |
| 49 } | |
| 50 | |
| 51 OrtcFactory::OrtcFactory(rtc::Thread* network_thread, | |
| 52 rtc::Thread* signaling_thread, | |
| 53 rtc::NetworkManager* network_manager, | |
| 54 rtc::PacketSocketFactory* socket_factory) | |
| 55 : network_thread_(network_thread), | |
| 56 signaling_thread_(signaling_thread), | |
| 57 network_manager_(network_manager), | |
| 58 socket_factory_(socket_factory) { | |
| 59 if (!network_thread_) { | |
| 60 owned_network_thread_ = rtc::Thread::CreateWithSocketServer(); | |
| 61 owned_network_thread_->Start(); | |
| 62 network_thread_ = owned_network_thread_.get(); | |
| 63 } | |
| 64 | |
| 65 // The worker thread is created internally because it's an implementation | |
| 66 // detail, and consumers of the API don't need to really know about it. | |
| 67 owned_worker_thread_ = rtc::Thread::Create(); | |
| 68 owned_worker_thread_->Start(); | |
| 69 | |
| 70 if (signaling_thread_) { | |
| 71 RTC_DCHECK_RUN_ON(signaling_thread_); | |
| 72 } else { | |
| 73 signaling_thread_ = rtc::Thread::Current(); | |
| 74 if (!signaling_thread_) { | |
| 75 // If this thread isn't already wrapped by an rtc::Thread, create a | |
| 76 // wrapper and own it in this class. | |
| 77 signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); | |
| 78 wraps_signaling_thread_ = true; | |
| 79 } | |
| 80 } | |
| 81 if (!network_manager_) { | |
| 82 owned_network_manager_.reset(new rtc::BasicNetworkManager()); | |
| 83 network_manager_ = owned_network_manager_.get(); | |
| 84 } | |
| 85 if (!socket_factory_) { | |
| 86 owned_socket_factory_.reset( | |
| 87 new rtc::BasicPacketSocketFactory(network_thread_)); | |
| 88 socket_factory_ = owned_socket_factory_.get(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 OrtcFactory::~OrtcFactory() { | |
| 93 RTC_DCHECK_RUN_ON(signaling_thread_); | |
| 94 if (wraps_signaling_thread_) { | |
| 95 rtc::ThreadManager::Instance()->UnwrapCurrentThread(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 std::unique_ptr<UdpTransportInterface> OrtcFactory::CreateUdpTransport( | |
| 100 int family, | |
| 101 uint16_t min_port, | |
| 102 uint16_t max_port) { | |
| 103 if (!network_thread_->IsCurrent()) { | |
| 104 RTC_DCHECK_RUN_ON(signaling_thread_); | |
| 105 return network_thread_->Invoke<std::unique_ptr<UdpTransportInterface>>( | |
| 106 RTC_FROM_HERE, rtc::Bind(&OrtcFactory::CreateUdpTransport, this, family, | |
| 107 min_port, max_port)); | |
| 108 } | |
| 109 std::unique_ptr<rtc::AsyncPacketSocket> socket( | |
| 110 socket_factory_->CreateUdpSocket( | |
| 111 rtc::SocketAddress(rtc::GetAnyIP(family), 0), min_port, max_port)); | |
| 112 if (!socket) { | |
| 113 LOG(LS_WARNING) << "Local socket allocation failure."; | |
| 114 return nullptr; | |
| 115 } | |
| 116 LOG(LS_INFO) << "Created UDP socket with address " | |
| 117 << socket->GetLocalAddress().ToSensitiveString() << "."; | |
| 118 // Use proxy so that calls to the returned object are invoked on the network | |
| 119 // thread. | |
| 120 return UdpTransportProxy::Create( | |
| 121 signaling_thread_, network_thread_, | |
| 122 new cricket::UdpTransport(std::string(), std::move(socket))); | |
| 123 } | |
| 124 | |
| 125 } // namespace webrtc | |
| OLD | NEW |