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