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