OLD | NEW |
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 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 } | 1028 } |
1029 | 1029 |
1030 // Helper struct for working around the fact that CreatePeerConnectionFactory() | 1030 // Helper struct for working around the fact that CreatePeerConnectionFactory() |
1031 // comes in two flavors: either entirely automagical (constructing its own | 1031 // comes in two flavors: either entirely automagical (constructing its own |
1032 // threads and deleting them on teardown, but no external codec factory support) | 1032 // threads and deleting them on teardown, but no external codec factory support) |
1033 // or entirely manual (requires caller to delete threads after factory | 1033 // or entirely manual (requires caller to delete threads after factory |
1034 // teardown). This struct takes ownership of its ctor's arguments to present a | 1034 // teardown). This struct takes ownership of its ctor's arguments to present a |
1035 // single thing for Java to hold and eventually free. | 1035 // single thing for Java to hold and eventually free. |
1036 class OwnedFactoryAndThreads { | 1036 class OwnedFactoryAndThreads { |
1037 public: | 1037 public: |
1038 OwnedFactoryAndThreads(Thread* worker_thread, | 1038 OwnedFactoryAndThreads(std::unique_ptr<Thread> network_thread, |
1039 Thread* signaling_thread, | 1039 std::unique_ptr<Thread> worker_thread, |
| 1040 std::unique_ptr<Thread> signaling_thread, |
1040 WebRtcVideoEncoderFactory* encoder_factory, | 1041 WebRtcVideoEncoderFactory* encoder_factory, |
1041 WebRtcVideoDecoderFactory* decoder_factory, | 1042 WebRtcVideoDecoderFactory* decoder_factory, |
1042 rtc::NetworkMonitorFactory* network_monitor_factory, | 1043 rtc::NetworkMonitorFactory* network_monitor_factory, |
1043 PeerConnectionFactoryInterface* factory) | 1044 PeerConnectionFactoryInterface* factory) |
1044 : worker_thread_(worker_thread), | 1045 : network_thread_(std::move(network_thread)), |
1045 signaling_thread_(signaling_thread), | 1046 worker_thread_(std::move(worker_thread)), |
| 1047 signaling_thread_(std::move(signaling_thread)), |
1046 encoder_factory_(encoder_factory), | 1048 encoder_factory_(encoder_factory), |
1047 decoder_factory_(decoder_factory), | 1049 decoder_factory_(decoder_factory), |
1048 network_monitor_factory_(network_monitor_factory), | 1050 network_monitor_factory_(network_monitor_factory), |
1049 factory_(factory) {} | 1051 factory_(factory) {} |
1050 | 1052 |
1051 ~OwnedFactoryAndThreads() { | 1053 ~OwnedFactoryAndThreads() { |
1052 CHECK_RELEASE(factory_); | 1054 CHECK_RELEASE(factory_); |
1053 if (network_monitor_factory_ != nullptr) { | 1055 if (network_monitor_factory_ != nullptr) { |
1054 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_); | 1056 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_); |
1055 } | 1057 } |
1056 } | 1058 } |
1057 | 1059 |
1058 PeerConnectionFactoryInterface* factory() { return factory_; } | 1060 PeerConnectionFactoryInterface* factory() { return factory_; } |
1059 WebRtcVideoEncoderFactory* encoder_factory() { return encoder_factory_; } | 1061 WebRtcVideoEncoderFactory* encoder_factory() { return encoder_factory_; } |
1060 WebRtcVideoDecoderFactory* decoder_factory() { return decoder_factory_; } | 1062 WebRtcVideoDecoderFactory* decoder_factory() { return decoder_factory_; } |
1061 rtc::NetworkMonitorFactory* network_monitor_factory() { | 1063 rtc::NetworkMonitorFactory* network_monitor_factory() { |
1062 return network_monitor_factory_; | 1064 return network_monitor_factory_; |
1063 } | 1065 } |
1064 void clear_network_monitor_factory() { network_monitor_factory_ = nullptr; } | 1066 void clear_network_monitor_factory() { network_monitor_factory_ = nullptr; } |
1065 void InvokeJavaCallbacksOnFactoryThreads(); | 1067 void InvokeJavaCallbacksOnFactoryThreads(); |
1066 | 1068 |
1067 private: | 1069 private: |
1068 void JavaCallbackOnFactoryThreads(); | 1070 void JavaCallbackOnFactoryThreads(); |
1069 | 1071 |
| 1072 const std::unique_ptr<Thread> network_thread_; |
1070 const std::unique_ptr<Thread> worker_thread_; | 1073 const std::unique_ptr<Thread> worker_thread_; |
1071 const std::unique_ptr<Thread> signaling_thread_; | 1074 const std::unique_ptr<Thread> signaling_thread_; |
1072 WebRtcVideoEncoderFactory* encoder_factory_; | 1075 WebRtcVideoEncoderFactory* encoder_factory_; |
1073 WebRtcVideoDecoderFactory* decoder_factory_; | 1076 WebRtcVideoDecoderFactory* decoder_factory_; |
1074 rtc::NetworkMonitorFactory* network_monitor_factory_; | 1077 rtc::NetworkMonitorFactory* network_monitor_factory_; |
1075 PeerConnectionFactoryInterface* factory_; // Const after ctor except dtor. | 1078 PeerConnectionFactoryInterface* factory_; // Const after ctor except dtor. |
1076 }; | 1079 }; |
1077 | 1080 |
1078 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() { | 1081 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() { |
1079 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 1082 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
1080 ScopedLocalRefFrame local_ref_frame(jni); | 1083 ScopedLocalRefFrame local_ref_frame(jni); |
1081 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory"); | 1084 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory"); |
1082 jmethodID m = nullptr; | 1085 jmethodID m = nullptr; |
| 1086 if (Thread::Current() == network_thread_.get()) { |
| 1087 LOG(LS_INFO) << "Network thread JavaCallback"; |
| 1088 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); |
| 1089 } |
1083 if (Thread::Current() == worker_thread_.get()) { | 1090 if (Thread::Current() == worker_thread_.get()) { |
1084 LOG(LS_INFO) << "Worker thread JavaCallback"; | 1091 LOG(LS_INFO) << "Worker thread JavaCallback"; |
1085 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); | 1092 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); |
1086 } | 1093 } |
1087 if (Thread::Current() == signaling_thread_.get()) { | 1094 if (Thread::Current() == signaling_thread_.get()) { |
1088 LOG(LS_INFO) << "Signaling thread JavaCallback"; | 1095 LOG(LS_INFO) << "Signaling thread JavaCallback"; |
1089 m = GetStaticMethodID( | 1096 m = GetStaticMethodID( |
1090 jni, j_factory_class, "onSignalingThreadReady", "()V"); | 1097 jni, j_factory_class, "onSignalingThreadReady", "()V"); |
1091 } | 1098 } |
1092 if (m != nullptr) { | 1099 if (m != nullptr) { |
1093 jni->CallStaticVoidMethod(j_factory_class, m); | 1100 jni->CallStaticVoidMethod(j_factory_class, m); |
1094 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod"; | 1101 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod"; |
1095 } | 1102 } |
1096 } | 1103 } |
1097 | 1104 |
1098 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() { | 1105 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() { |
1099 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads."; | 1106 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads."; |
1100 worker_thread_->Invoke<void>( | 1107 network_thread_->Invoke<void>( |
1101 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); | 1108 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); |
| 1109 if (worker_thread_) { |
| 1110 worker_thread_->Invoke<void>( |
| 1111 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); |
| 1112 } |
1102 signaling_thread_->Invoke<void>( | 1113 signaling_thread_->Invoke<void>( |
1103 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); | 1114 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); |
1104 } | 1115 } |
1105 | 1116 |
1106 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni, | 1117 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni, |
1107 jobject options) { | 1118 jobject options) { |
1108 jclass options_class = jni->GetObjectClass(options); | 1119 jclass options_class = jni->GetObjectClass(options); |
1109 jfieldID network_ignore_mask_field = | 1120 jfieldID network_ignore_mask_field = |
1110 jni->GetFieldID(options_class, "networkIgnoreMask", "I"); | 1121 jni->GetFieldID(options_class, "networkIgnoreMask", "I"); |
1111 int network_ignore_mask = | 1122 int network_ignore_mask = |
(...skipping 19 matching lines...) Expand all Loading... |
1131 return native_options; | 1142 return native_options; |
1132 } | 1143 } |
1133 | 1144 |
1134 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnectionFactory)( | 1145 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnectionFactory)( |
1135 JNIEnv* jni, jclass, jobject joptions) { | 1146 JNIEnv* jni, jclass, jobject joptions) { |
1136 // talk/ assumes pretty widely that the current Thread is ThreadManager'd, but | 1147 // talk/ assumes pretty widely that the current Thread is ThreadManager'd, but |
1137 // ThreadManager only WrapCurrentThread()s the thread where it is first | 1148 // ThreadManager only WrapCurrentThread()s the thread where it is first |
1138 // created. Since the semantics around when auto-wrapping happens in | 1149 // created. Since the semantics around when auto-wrapping happens in |
1139 // webrtc/base/ are convoluted, we simply wrap here to avoid having to think | 1150 // webrtc/base/ are convoluted, we simply wrap here to avoid having to think |
1140 // about ramifications of auto-wrapping there. | 1151 // about ramifications of auto-wrapping there. |
| 1152 // TODO(danilchap): Make choice or move flag below to easier settable. |
| 1153 const bool worker_is_network = false; |
1141 rtc::ThreadManager::Instance()->WrapCurrentThread(); | 1154 rtc::ThreadManager::Instance()->WrapCurrentThread(); |
1142 webrtc::Trace::CreateTrace(); | 1155 webrtc::Trace::CreateTrace(); |
1143 Thread* worker_thread = new Thread(); | 1156 std::unique_ptr<Thread> network_thread = |
1144 worker_thread->SetName("worker_thread", NULL); | 1157 rtc::Thread::CreateWithSocketServer(); |
1145 Thread* signaling_thread = new Thread(); | 1158 network_thread->SetName("network_thread", NULL); |
| 1159 RTC_CHECK(network_thread->Start()) << "Failed to start thread"; |
| 1160 Thread* worker_thread = nullptr; |
| 1161 std::unique_ptr<Thread> worker_thread_own; |
| 1162 if (worker_is_network) { |
| 1163 worker_thread = network_thread.get(); |
| 1164 } else { |
| 1165 worker_thread_own = rtc::Thread::Create(); |
| 1166 worker_thread = worker_thread_own.get(); |
| 1167 worker_thread->SetName("worker_thread", NULL); |
| 1168 RTC_CHECK(worker_thread->Start()) << "Failed to start thread"; |
| 1169 } |
| 1170 std::unique_ptr<Thread> signaling_thread = rtc::Thread::Create(); |
1146 signaling_thread->SetName("signaling_thread", NULL); | 1171 signaling_thread->SetName("signaling_thread", NULL); |
1147 RTC_CHECK(worker_thread->Start() && signaling_thread->Start()) | 1172 RTC_CHECK(signaling_thread->Start()) << "Failed to start thread"; |
1148 << "Failed to start threads"; | |
1149 WebRtcVideoEncoderFactory* encoder_factory = nullptr; | 1173 WebRtcVideoEncoderFactory* encoder_factory = nullptr; |
1150 WebRtcVideoDecoderFactory* decoder_factory = nullptr; | 1174 WebRtcVideoDecoderFactory* decoder_factory = nullptr; |
1151 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr; | 1175 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr; |
1152 | 1176 |
1153 PeerConnectionFactoryInterface::Options options; | 1177 PeerConnectionFactoryInterface::Options options; |
1154 bool has_options = joptions != NULL; | 1178 bool has_options = joptions != NULL; |
1155 if (has_options) { | 1179 if (has_options) { |
1156 options = ParseOptionsFromJava(jni, joptions); | 1180 options = ParseOptionsFromJava(jni, joptions); |
1157 } | 1181 } |
1158 | 1182 |
1159 if (video_hw_acceleration_enabled) { | 1183 if (video_hw_acceleration_enabled) { |
1160 encoder_factory = new MediaCodecVideoEncoderFactory(); | 1184 encoder_factory = new MediaCodecVideoEncoderFactory(); |
1161 decoder_factory = new MediaCodecVideoDecoderFactory(); | 1185 decoder_factory = new MediaCodecVideoDecoderFactory(); |
1162 } | 1186 } |
1163 // Do not create network_monitor_factory only if the options are | 1187 // Do not create network_monitor_factory only if the options are |
1164 // provided and disable_network_monitor therein is set to true. | 1188 // provided and disable_network_monitor therein is set to true. |
1165 if (!(has_options && options.disable_network_monitor)) { | 1189 if (!(has_options && options.disable_network_monitor)) { |
1166 network_monitor_factory = new AndroidNetworkMonitorFactory(); | 1190 network_monitor_factory = new AndroidNetworkMonitorFactory(); |
1167 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory); | 1191 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory); |
1168 } | 1192 } |
1169 | 1193 |
1170 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | 1194 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( |
1171 webrtc::CreatePeerConnectionFactory(worker_thread, | 1195 webrtc::CreatePeerConnectionFactory(network_thread.get(), worker_thread, |
1172 signaling_thread, | 1196 signaling_thread.get(), NULL, |
1173 NULL, | 1197 encoder_factory, decoder_factory)); |
1174 encoder_factory, | |
1175 decoder_factory)); | |
1176 RTC_CHECK(factory) << "Failed to create the peer connection factory; " | 1198 RTC_CHECK(factory) << "Failed to create the peer connection factory; " |
1177 << "WebRTC/libjingle init likely failed on this device"; | 1199 << "WebRTC/libjingle init likely failed on this device"; |
1178 // TODO(honghaiz): Maybe put the options as the argument of | 1200 // TODO(honghaiz): Maybe put the options as the argument of |
1179 // CreatePeerConnectionFactory. | 1201 // CreatePeerConnectionFactory. |
1180 if (has_options) { | 1202 if (has_options) { |
1181 factory->SetOptions(options); | 1203 factory->SetOptions(options); |
1182 } | 1204 } |
1183 OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads( | 1205 OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads( |
1184 worker_thread, signaling_thread, | 1206 std::move(network_thread), std::move(worker_thread_own), |
1185 encoder_factory, decoder_factory, | 1207 std::move(signaling_thread), encoder_factory, decoder_factory, |
1186 network_monitor_factory, factory.release()); | 1208 network_monitor_factory, factory.release()); |
1187 owned_factory->InvokeJavaCallbacksOnFactoryThreads(); | 1209 owned_factory->InvokeJavaCallbacksOnFactoryThreads(); |
1188 return jlongFromPointer(owned_factory); | 1210 return jlongFromPointer(owned_factory); |
1189 } | 1211 } |
1190 | 1212 |
1191 JOW(void, PeerConnectionFactory_nativeFreeFactory)(JNIEnv*, jclass, jlong j_p) { | 1213 JOW(void, PeerConnectionFactory_nativeFreeFactory)(JNIEnv*, jclass, jlong j_p) { |
1192 delete reinterpret_cast<OwnedFactoryAndThreads*>(j_p); | 1214 delete reinterpret_cast<OwnedFactoryAndThreads*>(j_p); |
1193 if (field_trials_init_string) { | 1215 if (field_trials_init_string) { |
1194 webrtc::field_trial::InitFieldTrialsFromString(NULL); | 1216 webrtc::field_trial::InitFieldTrialsFromString(NULL); |
1195 delete field_trials_init_string; | 1217 delete field_trials_init_string; |
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2207 return JavaStringFromStdString( | 2229 return JavaStringFromStdString( |
2208 jni, | 2230 jni, |
2209 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id()); | 2231 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id()); |
2210 } | 2232 } |
2211 | 2233 |
2212 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { | 2234 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { |
2213 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release(); | 2235 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release(); |
2214 } | 2236 } |
2215 | 2237 |
2216 } // namespace webrtc_jni | 2238 } // namespace webrtc_jni |
OLD | NEW |