Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1026)

Side by Side Diff: webrtc/api/java/jni/peerconnection_jni.cc

Issue 1968393002: Propogate network-worker thread split to api (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: rebase including nits Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after
1031 } 1031 }
1032 1032
1033 // Helper struct for working around the fact that CreatePeerConnectionFactory() 1033 // Helper struct for working around the fact that CreatePeerConnectionFactory()
1034 // comes in two flavors: either entirely automagical (constructing its own 1034 // comes in two flavors: either entirely automagical (constructing its own
1035 // threads and deleting them on teardown, but no external codec factory support) 1035 // threads and deleting them on teardown, but no external codec factory support)
1036 // or entirely manual (requires caller to delete threads after factory 1036 // or entirely manual (requires caller to delete threads after factory
1037 // teardown). This struct takes ownership of its ctor's arguments to present a 1037 // teardown). This struct takes ownership of its ctor's arguments to present a
1038 // single thing for Java to hold and eventually free. 1038 // single thing for Java to hold and eventually free.
1039 class OwnedFactoryAndThreads { 1039 class OwnedFactoryAndThreads {
1040 public: 1040 public:
1041 OwnedFactoryAndThreads(Thread* worker_thread, 1041 OwnedFactoryAndThreads(std::unique_ptr<Thread> network_thread,
1042 Thread* signaling_thread, 1042 std::unique_ptr<Thread> worker_thread,
1043 std::unique_ptr<Thread> signaling_thread,
1043 WebRtcVideoEncoderFactory* encoder_factory, 1044 WebRtcVideoEncoderFactory* encoder_factory,
1044 WebRtcVideoDecoderFactory* decoder_factory, 1045 WebRtcVideoDecoderFactory* decoder_factory,
1045 rtc::NetworkMonitorFactory* network_monitor_factory, 1046 rtc::NetworkMonitorFactory* network_monitor_factory,
1046 PeerConnectionFactoryInterface* factory) 1047 PeerConnectionFactoryInterface* factory)
1047 : worker_thread_(worker_thread), 1048 : network_thread_(std::move(network_thread)),
1048 signaling_thread_(signaling_thread), 1049 worker_thread_(std::move(worker_thread)),
1050 signaling_thread_(std::move(signaling_thread)),
1049 encoder_factory_(encoder_factory), 1051 encoder_factory_(encoder_factory),
1050 decoder_factory_(decoder_factory), 1052 decoder_factory_(decoder_factory),
1051 network_monitor_factory_(network_monitor_factory), 1053 network_monitor_factory_(network_monitor_factory),
1052 factory_(factory) {} 1054 factory_(factory) {}
1053 1055
1054 ~OwnedFactoryAndThreads() { 1056 ~OwnedFactoryAndThreads() {
1055 CHECK_RELEASE(factory_); 1057 CHECK_RELEASE(factory_);
1056 if (network_monitor_factory_ != nullptr) { 1058 if (network_monitor_factory_ != nullptr) {
1057 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_); 1059 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_);
1058 } 1060 }
1059 } 1061 }
1060 1062
1061 PeerConnectionFactoryInterface* factory() { return factory_; } 1063 PeerConnectionFactoryInterface* factory() { return factory_; }
1062 WebRtcVideoEncoderFactory* encoder_factory() { return encoder_factory_; } 1064 WebRtcVideoEncoderFactory* encoder_factory() { return encoder_factory_; }
1063 WebRtcVideoDecoderFactory* decoder_factory() { return decoder_factory_; } 1065 WebRtcVideoDecoderFactory* decoder_factory() { return decoder_factory_; }
1064 rtc::NetworkMonitorFactory* network_monitor_factory() { 1066 rtc::NetworkMonitorFactory* network_monitor_factory() {
1065 return network_monitor_factory_; 1067 return network_monitor_factory_;
1066 } 1068 }
1067 void clear_network_monitor_factory() { network_monitor_factory_ = nullptr; } 1069 void clear_network_monitor_factory() { network_monitor_factory_ = nullptr; }
1068 void InvokeJavaCallbacksOnFactoryThreads(); 1070 void InvokeJavaCallbacksOnFactoryThreads();
1069 1071
1070 private: 1072 private:
1071 void JavaCallbackOnFactoryThreads(); 1073 void JavaCallbackOnFactoryThreads();
1072 1074
1075 const std::unique_ptr<Thread> network_thread_;
1073 const std::unique_ptr<Thread> worker_thread_; 1076 const std::unique_ptr<Thread> worker_thread_;
1074 const std::unique_ptr<Thread> signaling_thread_; 1077 const std::unique_ptr<Thread> signaling_thread_;
1075 WebRtcVideoEncoderFactory* encoder_factory_; 1078 WebRtcVideoEncoderFactory* encoder_factory_;
1076 WebRtcVideoDecoderFactory* decoder_factory_; 1079 WebRtcVideoDecoderFactory* decoder_factory_;
1077 rtc::NetworkMonitorFactory* network_monitor_factory_; 1080 rtc::NetworkMonitorFactory* network_monitor_factory_;
1078 PeerConnectionFactoryInterface* factory_; // Const after ctor except dtor. 1081 PeerConnectionFactoryInterface* factory_; // Const after ctor except dtor.
1079 }; 1082 };
1080 1083
1081 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() { 1084 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() {
1082 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 1085 JNIEnv* jni = AttachCurrentThreadIfNeeded();
1083 ScopedLocalRefFrame local_ref_frame(jni); 1086 ScopedLocalRefFrame local_ref_frame(jni);
1084 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory"); 1087 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory");
1085 jmethodID m = nullptr; 1088 jmethodID m = nullptr;
1086 if (Thread::Current() == worker_thread_.get()) { 1089 if (network_thread_->IsCurrent()) {
1090 LOG(LS_INFO) << "Network thread JavaCallback";
1091 m = GetStaticMethodID(jni, j_factory_class, "onNetworkThreadReady", "()V");
1092 }
1093 if (worker_thread_->IsCurrent()) {
1087 LOG(LS_INFO) << "Worker thread JavaCallback"; 1094 LOG(LS_INFO) << "Worker thread JavaCallback";
1088 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); 1095 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V");
1089 } 1096 }
1090 if (Thread::Current() == signaling_thread_.get()) { 1097 if (signaling_thread_->IsCurrent()) {
1091 LOG(LS_INFO) << "Signaling thread JavaCallback"; 1098 LOG(LS_INFO) << "Signaling thread JavaCallback";
1092 m = GetStaticMethodID( 1099 m = GetStaticMethodID(
1093 jni, j_factory_class, "onSignalingThreadReady", "()V"); 1100 jni, j_factory_class, "onSignalingThreadReady", "()V");
1094 } 1101 }
1095 if (m != nullptr) { 1102 if (m != nullptr) {
1096 jni->CallStaticVoidMethod(j_factory_class, m); 1103 jni->CallStaticVoidMethod(j_factory_class, m);
1097 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod"; 1104 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod";
1098 } 1105 }
1099 } 1106 }
1100 1107
1101 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() { 1108 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() {
1102 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads."; 1109 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads.";
1103 worker_thread_->Invoke<void>( 1110 network_thread_->Invoke<void>([this] { JavaCallbackOnFactoryThreads(); });
1104 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); 1111 worker_thread_->Invoke<void>([this] { JavaCallbackOnFactoryThreads(); });
1105 signaling_thread_->Invoke<void>( 1112 signaling_thread_->Invoke<void>([this] { JavaCallbackOnFactoryThreads(); });
1106 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this));
1107 } 1113 }
1108 1114
1109 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni, 1115 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni,
1110 jobject options) { 1116 jobject options) {
1111 jclass options_class = jni->GetObjectClass(options); 1117 jclass options_class = jni->GetObjectClass(options);
1112 jfieldID network_ignore_mask_field = 1118 jfieldID network_ignore_mask_field =
1113 jni->GetFieldID(options_class, "networkIgnoreMask", "I"); 1119 jni->GetFieldID(options_class, "networkIgnoreMask", "I");
1114 int network_ignore_mask = 1120 int network_ignore_mask =
1115 jni->GetIntField(options, network_ignore_mask_field); 1121 jni->GetIntField(options, network_ignore_mask_field);
1116 1122
(...skipping 19 matching lines...) Expand all
1136 1142
1137 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnectionFactory)( 1143 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnectionFactory)(
1138 JNIEnv* jni, jclass, jobject joptions) { 1144 JNIEnv* jni, jclass, jobject joptions) {
1139 // talk/ assumes pretty widely that the current Thread is ThreadManager'd, but 1145 // talk/ assumes pretty widely that the current Thread is ThreadManager'd, but
1140 // ThreadManager only WrapCurrentThread()s the thread where it is first 1146 // ThreadManager only WrapCurrentThread()s the thread where it is first
1141 // created. Since the semantics around when auto-wrapping happens in 1147 // created. Since the semantics around when auto-wrapping happens in
1142 // webrtc/base/ are convoluted, we simply wrap here to avoid having to think 1148 // webrtc/base/ are convoluted, we simply wrap here to avoid having to think
1143 // about ramifications of auto-wrapping there. 1149 // about ramifications of auto-wrapping there.
1144 rtc::ThreadManager::Instance()->WrapCurrentThread(); 1150 rtc::ThreadManager::Instance()->WrapCurrentThread();
1145 webrtc::Trace::CreateTrace(); 1151 webrtc::Trace::CreateTrace();
1146 Thread* worker_thread = new Thread(); 1152
1147 worker_thread->SetName("worker_thread", NULL); 1153 std::unique_ptr<Thread> network_thread =
1148 Thread* signaling_thread = new Thread(); 1154 rtc::Thread::CreateWithSocketServer();
1155 network_thread->SetName("network_thread", nullptr);
1156 RTC_CHECK(network_thread->Start()) << "Failed to start thread";
1157
1158 std::unique_ptr<Thread> worker_thread = rtc::Thread::Create();
1159 worker_thread->SetName("worker_thread", nullptr);
1160 RTC_CHECK(worker_thread->Start()) << "Failed to start thread";
1161
1162 std::unique_ptr<Thread> signaling_thread = rtc::Thread::Create();
1149 signaling_thread->SetName("signaling_thread", NULL); 1163 signaling_thread->SetName("signaling_thread", NULL);
1150 RTC_CHECK(worker_thread->Start() && signaling_thread->Start()) 1164 RTC_CHECK(signaling_thread->Start()) << "Failed to start thread";
1151 << "Failed to start threads"; 1165
1152 WebRtcVideoEncoderFactory* encoder_factory = nullptr; 1166 WebRtcVideoEncoderFactory* encoder_factory = nullptr;
1153 WebRtcVideoDecoderFactory* decoder_factory = nullptr; 1167 WebRtcVideoDecoderFactory* decoder_factory = nullptr;
1154 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr; 1168 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr;
1155 1169
1156 PeerConnectionFactoryInterface::Options options; 1170 PeerConnectionFactoryInterface::Options options;
1157 bool has_options = joptions != NULL; 1171 bool has_options = joptions != NULL;
1158 if (has_options) { 1172 if (has_options) {
1159 options = ParseOptionsFromJava(jni, joptions); 1173 options = ParseOptionsFromJava(jni, joptions);
1160 } 1174 }
1161 1175
1162 if (video_hw_acceleration_enabled) { 1176 if (video_hw_acceleration_enabled) {
1163 encoder_factory = new MediaCodecVideoEncoderFactory(); 1177 encoder_factory = new MediaCodecVideoEncoderFactory();
1164 decoder_factory = new MediaCodecVideoDecoderFactory(); 1178 decoder_factory = new MediaCodecVideoDecoderFactory();
1165 } 1179 }
1166 // Do not create network_monitor_factory only if the options are 1180 // Do not create network_monitor_factory only if the options are
1167 // provided and disable_network_monitor therein is set to true. 1181 // provided and disable_network_monitor therein is set to true.
1168 if (!(has_options && options.disable_network_monitor)) { 1182 if (!(has_options && options.disable_network_monitor)) {
1169 network_monitor_factory = new AndroidNetworkMonitorFactory(); 1183 network_monitor_factory = new AndroidNetworkMonitorFactory();
1170 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory); 1184 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory);
1171 } 1185 }
1172 1186
1173 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( 1187 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory(
1174 webrtc::CreatePeerConnectionFactory(worker_thread, 1188 webrtc::CreatePeerConnectionFactory(
1175 signaling_thread, 1189 network_thread.get(), worker_thread.get(), signaling_thread.get(),
1176 NULL, 1190 nullptr, encoder_factory, decoder_factory));
1177 encoder_factory,
1178 decoder_factory));
1179 RTC_CHECK(factory) << "Failed to create the peer connection factory; " 1191 RTC_CHECK(factory) << "Failed to create the peer connection factory; "
1180 << "WebRTC/libjingle init likely failed on this device"; 1192 << "WebRTC/libjingle init likely failed on this device";
1181 // TODO(honghaiz): Maybe put the options as the argument of 1193 // TODO(honghaiz): Maybe put the options as the argument of
1182 // CreatePeerConnectionFactory. 1194 // CreatePeerConnectionFactory.
1183 if (has_options) { 1195 if (has_options) {
1184 factory->SetOptions(options); 1196 factory->SetOptions(options);
1185 } 1197 }
1186 OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads( 1198 OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads(
1187 worker_thread, signaling_thread, 1199 std::move(network_thread), std::move(worker_thread),
1188 encoder_factory, decoder_factory, 1200 std::move(signaling_thread), encoder_factory, decoder_factory,
1189 network_monitor_factory, factory.release()); 1201 network_monitor_factory, factory.release());
1190 owned_factory->InvokeJavaCallbacksOnFactoryThreads(); 1202 owned_factory->InvokeJavaCallbacksOnFactoryThreads();
1191 return jlongFromPointer(owned_factory); 1203 return jlongFromPointer(owned_factory);
1192 } 1204 }
1193 1205
1194 JOW(void, PeerConnectionFactory_nativeFreeFactory)(JNIEnv*, jclass, jlong j_p) { 1206 JOW(void, PeerConnectionFactory_nativeFreeFactory)(JNIEnv*, jclass, jlong j_p) {
1195 delete reinterpret_cast<OwnedFactoryAndThreads*>(j_p); 1207 delete reinterpret_cast<OwnedFactoryAndThreads*>(j_p);
1196 if (field_trials_init_string) { 1208 if (field_trials_init_string) {
1197 webrtc::field_trial::InitFieldTrialsFromString(NULL); 1209 webrtc::field_trial::InitFieldTrialsFromString(NULL);
1198 delete field_trials_init_string; 1210 delete field_trials_init_string;
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after
2211 return JavaStringFromStdString( 2223 return JavaStringFromStdString(
2212 jni, 2224 jni,
2213 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id()); 2225 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id());
2214 } 2226 }
2215 2227
2216 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { 2228 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) {
2217 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release(); 2229 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release();
2218 } 2230 }
2219 2231
2220 } // namespace webrtc_jni 2232 } // namespace webrtc_jni
OLDNEW
« no previous file with comments | « talk/app/webrtc/objc/RTCPeerConnectionFactory.mm ('k') | webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698