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