| 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/api/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 return signaling_thread->Invoke<std::unique_ptr<OrtcFactoryInterface>>( | |
| 32 RTC_FROM_HERE, | |
| 33 rtc::Bind(&OrtcFactoryInterface::Create, network_thread, | |
| 34 signaling_thread, network_manager, socket_factory)); | |
| 35 } | |
| 36 OrtcFactory* new_factory = | |
| 37 new OrtcFactory(network_thread, signaling_thread, | |
| 38 network_manager, socket_factory); | |
| 39 // Return a proxy so that any calls on the returned object (including | |
| 40 // destructor) happen on the signaling thread. | |
| 41 return OrtcFactoryProxy::Create(new_factory->signaling_thread(), | |
| 42 new_factory->network_thread(), new_factory); | |
| 43 } | |
| 44 | |
| 45 OrtcFactory::OrtcFactory(rtc::Thread* network_thread, | |
| 46 rtc::Thread* signaling_thread, | |
| 47 rtc::NetworkManager* network_manager, | |
| 48 rtc::PacketSocketFactory* socket_factory) | |
| 49 : network_thread_(network_thread), | |
| 50 signaling_thread_(signaling_thread), | |
| 51 network_manager_(network_manager), | |
| 52 socket_factory_(socket_factory) { | |
| 53 if (!network_thread_) { | |
| 54 owned_network_thread_ = rtc::Thread::CreateWithSocketServer(); | |
| 55 owned_network_thread_->Start(); | |
| 56 network_thread_ = owned_network_thread_.get(); | |
| 57 } | |
| 58 | |
| 59 // The worker thread is created internally because it's an implementation | |
| 60 // detail, and consumers of the API don't need to really know about it. | |
| 61 owned_worker_thread_ = rtc::Thread::Create(); | |
| 62 owned_worker_thread_->Start(); | |
| 63 | |
| 64 if (signaling_thread_) { | |
| 65 RTC_DCHECK_RUN_ON(signaling_thread_); | |
| 66 } else { | |
| 67 signaling_thread_ = rtc::Thread::Current(); | |
| 68 if (!signaling_thread_) { | |
| 69 // If this thread isn't already wrapped by an rtc::Thread, create a | |
| 70 // wrapper and own it in this class. | |
| 71 signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); | |
| 72 wraps_signaling_thread_ = true; | |
| 73 } | |
| 74 } | |
| 75 if (!network_manager_) { | |
| 76 owned_network_manager_.reset(new rtc::BasicNetworkManager()); | |
| 77 network_manager_ = owned_network_manager_.get(); | |
| 78 } | |
| 79 if (!socket_factory_) { | |
| 80 owned_socket_factory_.reset( | |
| 81 new rtc::BasicPacketSocketFactory(network_thread_)); | |
| 82 socket_factory_ = owned_socket_factory_.get(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 OrtcFactory::~OrtcFactory() { | |
| 87 RTC_DCHECK_RUN_ON(signaling_thread_); | |
| 88 if (wraps_signaling_thread_) { | |
| 89 rtc::ThreadManager::Instance()->UnwrapCurrentThread(); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 std::unique_ptr<UdpTransportInterface> OrtcFactory::CreateUdpTransport( | |
| 94 int family, | |
| 95 uint16_t min_port, | |
| 96 uint16_t max_port) { | |
| 97 if (!network_thread_->IsCurrent()) { | |
| 98 RTC_DCHECK_RUN_ON(signaling_thread_); | |
| 99 return network_thread_->Invoke<std::unique_ptr<UdpTransportInterface>>( | |
| 100 RTC_FROM_HERE, rtc::Bind(&OrtcFactory::CreateUdpTransport, this, family, | |
| 101 min_port, max_port)); | |
| 102 } | |
| 103 std::unique_ptr<rtc::AsyncPacketSocket> socket( | |
| 104 socket_factory_->CreateUdpSocket( | |
| 105 rtc::SocketAddress(rtc::GetAnyIP(family), 0), min_port, max_port)); | |
| 106 if (!socket) { | |
| 107 LOG(LS_WARNING) << "Local socket allocation failure."; | |
| 108 return nullptr; | |
| 109 } | |
| 110 LOG(LS_INFO) << "Created UDP socket with address " | |
| 111 << socket->GetLocalAddress().ToSensitiveString() << "."; | |
| 112 // Use proxy so that calls to the returned object are invoked on the network | |
| 113 // thread. | |
| 114 return UdpTransportProxy::Create( | |
| 115 signaling_thread_, network_thread_, | |
| 116 new cricket::UdpTransport(std::string(), std::move(socket))); | |
| 117 } | |
| 118 | |
| 119 } // namespace webrtc | |
| OLD | NEW |