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 #ifndef SDK_ANDROID_SRC_JNI_PC_OWNED_FACTORY_AND_THREADS_H_ 12 #define SDK_ANDROID_SRC_JNI_PC_OWNED_FACTORY_AND_THREADS_H_ 13 14 #include <jni.h> 15 #include <memory> 16 #include <utility> 17 18 #include "api/peer_connection_interface.h" 19 #include "rtc_base/thread.h" 20 21 namespace webrtc { 22 namespace jni { 23 24 // Helper struct for working around the fact that CreatePeerConnectionFactory() 25 // comes in two flavors: either entirely automagical (constructing its own 26 // threads and deleting them on teardown, but no external codec factory support) 27 // or entirely manual (requires caller to delete threads after factory 28 // teardown). This struct takes ownership of its ctor's arguments to present a 29 // single thing for Java to hold and eventually free. 30 class OwnedFactoryAndThreads { 31 public: 32 OwnedFactoryAndThreads( 33 std::unique_ptr<rtc::SocketFactory> socket_factory, 34 std::unique_ptr<rtc::Thread> network_thread, 35 std::unique_ptr<rtc::Thread> worker_thread, 36 std::unique_ptr<rtc::Thread> signaling_thread, 37 const rtc::scoped_refptr<PeerConnectionFactoryInterface>& factory); 38 39 ~OwnedFactoryAndThreads() = default; 40 factory()41 PeerConnectionFactoryInterface* factory() { return factory_.get(); } socket_factory()42 rtc::SocketFactory* socket_factory() { return socket_factory_.get(); } network_thread()43 rtc::Thread* network_thread() { return network_thread_.get(); } signaling_thread()44 rtc::Thread* signaling_thread() { return signaling_thread_.get(); } worker_thread()45 rtc::Thread* worker_thread() { return worker_thread_.get(); } 46 47 private: 48 // Usually implemented by the SocketServer associated with the network thread, 49 // so needs to outlive the network thread. 50 const std::unique_ptr<rtc::SocketFactory> socket_factory_; 51 const std::unique_ptr<rtc::Thread> network_thread_; 52 const std::unique_ptr<rtc::Thread> worker_thread_; 53 const std::unique_ptr<rtc::Thread> signaling_thread_; 54 const rtc::scoped_refptr<PeerConnectionFactoryInterface> factory_; 55 }; 56 57 } // namespace jni 58 } // namespace webrtc 59 60 #endif // SDK_ANDROID_SRC_JNI_PC_OWNED_FACTORY_AND_THREADS_H_ 61