1 /*
2  *  Copyright 2018 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 #include "sdk/android/native_api/peerconnection/peer_connection_factory.h"
11 
12 #include <memory>
13 
14 #include "api/rtc_event_log/rtc_event_log_factory.h"
15 #include "api/task_queue/default_task_queue_factory.h"
16 #include "media/base/media_engine.h"
17 #include "media/engine/internal_decoder_factory.h"
18 #include "media/engine/internal_encoder_factory.h"
19 #include "media/engine/webrtc_media_engine.h"
20 #include "media/engine/webrtc_media_engine_defaults.h"
21 #include "rtc_base/logging.h"
22 #include "rtc_base/physical_socket_server.h"
23 #include "rtc_base/thread.h"
24 #include "sdk/android/generated_native_unittests_jni/PeerConnectionFactoryInitializationHelper_jni.h"
25 #include "sdk/android/native_api/audio_device_module/audio_device_android.h"
26 #include "sdk/android/native_api/jni/jvm.h"
27 #include "sdk/android/native_unittests/application_context_provider.h"
28 #include "sdk/android/src/jni/jni_helpers.h"
29 #include "test/gtest.h"
30 
31 namespace webrtc {
32 namespace test {
33 namespace {
34 
35 // Create native peer connection factory, that will be wrapped by java one
CreateTestPCF(JNIEnv * jni,rtc::Thread * network_thread,rtc::Thread * worker_thread,rtc::Thread * signaling_thread)36 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> CreateTestPCF(
37     JNIEnv* jni,
38     rtc::Thread* network_thread,
39     rtc::Thread* worker_thread,
40     rtc::Thread* signaling_thread) {
41   // talk/ assumes pretty widely that the current Thread is ThreadManager'd, but
42   // ThreadManager only WrapCurrentThread()s the thread where it is first
43   // created.  Since the semantics around when auto-wrapping happens in
44   // webrtc/rtc_base/ are convoluted, we simply wrap here to avoid having to
45   // think about ramifications of auto-wrapping there.
46   rtc::ThreadManager::Instance()->WrapCurrentThread();
47 
48   PeerConnectionFactoryDependencies pcf_deps;
49   pcf_deps.network_thread = network_thread;
50   pcf_deps.worker_thread = worker_thread;
51   pcf_deps.signaling_thread = signaling_thread;
52   pcf_deps.task_queue_factory = CreateDefaultTaskQueueFactory();
53   pcf_deps.call_factory = CreateCallFactory();
54   pcf_deps.event_log_factory =
55       std::make_unique<RtcEventLogFactory>(pcf_deps.task_queue_factory.get());
56 
57   cricket::MediaEngineDependencies media_deps;
58   media_deps.task_queue_factory = pcf_deps.task_queue_factory.get();
59   media_deps.adm =
60       CreateJavaAudioDeviceModule(jni, GetAppContextForTest(jni).obj());
61   media_deps.video_encoder_factory =
62       std::make_unique<webrtc::InternalEncoderFactory>();
63   media_deps.video_decoder_factory =
64       std::make_unique<webrtc::InternalDecoderFactory>();
65   SetMediaEngineDefaults(&media_deps);
66   pcf_deps.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
67   RTC_LOG(LS_INFO) << "Media engine created: " << pcf_deps.media_engine.get();
68 
69   auto factory = CreateModularPeerConnectionFactory(std::move(pcf_deps));
70   RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << factory.get();
71   RTC_CHECK(factory) << "Failed to create the peer connection factory; "
72                         "WebRTC/libjingle init likely failed on this device";
73 
74   return factory;
75 }
76 
TEST(PeerConnectionFactoryTest,NativeToJavaPeerConnectionFactory)77 TEST(PeerConnectionFactoryTest, NativeToJavaPeerConnectionFactory) {
78   JNIEnv* jni = AttachCurrentThreadIfNeeded();
79 
80   RTC_LOG(LS_INFO) << "Initializing java peer connection factory.";
81   jni::Java_PeerConnectionFactoryInitializationHelper_initializeFactoryForTests(
82       jni);
83   RTC_LOG(LS_INFO) << "Java peer connection factory initialized.";
84 
85   auto socket_server = std::make_unique<rtc::PhysicalSocketServer>();
86 
87   // Create threads.
88   auto network_thread = std::make_unique<rtc::Thread>(socket_server.get());
89   network_thread->SetName("network_thread", nullptr);
90   RTC_CHECK(network_thread->Start()) << "Failed to start thread";
91 
92   std::unique_ptr<rtc::Thread> worker_thread = rtc::Thread::Create();
93   worker_thread->SetName("worker_thread", nullptr);
94   RTC_CHECK(worker_thread->Start()) << "Failed to start thread";
95 
96   std::unique_ptr<rtc::Thread> signaling_thread = rtc::Thread::Create();
97   signaling_thread->SetName("signaling_thread", NULL);
98   RTC_CHECK(signaling_thread->Start()) << "Failed to start thread";
99 
100   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> factory =
101       CreateTestPCF(jni, network_thread.get(), worker_thread.get(),
102                     signaling_thread.get());
103 
104   jobject java_factory = NativeToJavaPeerConnectionFactory(
105       jni, factory, std::move(socket_server), std::move(network_thread),
106       std::move(worker_thread), std::move(signaling_thread));
107 
108   RTC_LOG(LS_INFO) << java_factory;
109 
110   EXPECT_NE(java_factory, nullptr);
111 }
112 
113 }  // namespace
114 }  // namespace test
115 }  // namespace webrtc
116