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