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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 #include "webrtc/base/checks.h" | 54 #include "webrtc/base/checks.h" |
55 #include "webrtc/base/event_tracer.h" | 55 #include "webrtc/base/event_tracer.h" |
56 #include "webrtc/base/logging.h" | 56 #include "webrtc/base/logging.h" |
57 #include "webrtc/base/logsinks.h" | 57 #include "webrtc/base/logsinks.h" |
58 #include "webrtc/base/messagequeue.h" | 58 #include "webrtc/base/messagequeue.h" |
59 #include "webrtc/base/networkmonitor.h" | 59 #include "webrtc/base/networkmonitor.h" |
60 #include "webrtc/base/rtccertificategenerator.h" | 60 #include "webrtc/base/rtccertificategenerator.h" |
61 #include "webrtc/base/ssladapter.h" | 61 #include "webrtc/base/ssladapter.h" |
62 #include "webrtc/base/stringutils.h" | 62 #include "webrtc/base/stringutils.h" |
63 #include "webrtc/media/base/videocapturer.h" | 63 #include "webrtc/media/base/videocapturer.h" |
64 #include "webrtc/media/engine/webrtcvideodecoderfactory.h" | |
65 #include "webrtc/media/engine/webrtcvideoencoderfactory.h" | |
66 #include "webrtc/modules/utility/include/jvm_android.h" | 64 #include "webrtc/modules/utility/include/jvm_android.h" |
67 #include "webrtc/system_wrappers/include/field_trial.h" | |
68 #include "webrtc/pc/webrtcsdp.h" | 65 #include "webrtc/pc/webrtcsdp.h" |
69 #include "webrtc/sdk/android/src/jni/androidmediadecoder_jni.h" | |
70 #include "webrtc/sdk/android/src/jni/androidmediaencoder_jni.h" | |
71 #include "webrtc/sdk/android/src/jni/androidnetworkmonitor_jni.h" | 66 #include "webrtc/sdk/android/src/jni/androidnetworkmonitor_jni.h" |
72 #include "webrtc/sdk/android/src/jni/androidvideotracksource.h" | 67 // Adding 'nogncheck' to disable the gn include headers check. |
| 68 // We don't want to always depend on audio and video related targets. |
| 69 #include "webrtc/sdk/android/src/jni/androidvideotracksource.h" // nogncheck |
| 70 #include "webrtc/sdk/android/src/jni/audio_jni.h" |
73 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | 71 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" |
74 #include "webrtc/sdk/android/src/jni/jni_helpers.h" | 72 #include "webrtc/sdk/android/src/jni/jni_helpers.h" |
75 #include "webrtc/sdk/android/src/jni/native_handle_impl.h" | 73 #include "webrtc/sdk/android/src/jni/media_jni.h" |
| 74 #include "webrtc/sdk/android/src/jni/ownedfactoryandthreads.h" |
76 #include "webrtc/sdk/android/src/jni/rtcstatscollectorcallbackwrapper.h" | 75 #include "webrtc/sdk/android/src/jni/rtcstatscollectorcallbackwrapper.h" |
| 76 #include "webrtc/sdk/android/src/jni/video_jni.h" |
| 77 #include "webrtc/system_wrappers/include/field_trial.h" |
77 // Adding 'nogncheck' to disable the gn include headers check. | 78 // Adding 'nogncheck' to disable the gn include headers check. |
78 // We don't want to depend on 'system_wrappers:field_trial_default' because | 79 // We don't want to depend on 'system_wrappers:field_trial_default' because |
79 // clients should be able to provide their own implementation. | 80 // clients should be able to provide their own implementation. |
80 #include "webrtc/system_wrappers/include/field_trial_default.h" // nogncheck | 81 #include "webrtc/system_wrappers/include/field_trial_default.h" // nogncheck |
81 #include "webrtc/system_wrappers/include/logcat_trace_context.h" | 82 #include "webrtc/system_wrappers/include/logcat_trace_context.h" |
82 #include "webrtc/system_wrappers/include/trace.h" | 83 #include "webrtc/system_wrappers/include/trace.h" |
83 #include "webrtc/voice_engine/include/voe_base.h" | 84 #include "webrtc/voice_engine/include/voe_base.h" // nogncheck |
84 | 85 |
85 using cricket::WebRtcVideoDecoderFactory; | 86 using cricket::WebRtcVideoDecoderFactory; |
86 using cricket::WebRtcVideoEncoderFactory; | 87 using cricket::WebRtcVideoEncoderFactory; |
87 using rtc::Bind; | 88 using rtc::Bind; |
88 using rtc::Thread; | 89 using rtc::Thread; |
89 using rtc::ThreadManager; | 90 using rtc::ThreadManager; |
90 using webrtc::AudioSourceInterface; | 91 using webrtc::AudioSourceInterface; |
91 using webrtc::AudioTrackInterface; | 92 using webrtc::AudioTrackInterface; |
92 using webrtc::AudioTrackVector; | 93 using webrtc::AudioTrackVector; |
93 using webrtc::CreateSessionDescriptionObserver; | 94 using webrtc::CreateSessionDescriptionObserver; |
(...skipping 12 matching lines...) Expand all Loading... |
106 using webrtc::PeerConnectionInterface; | 107 using webrtc::PeerConnectionInterface; |
107 using webrtc::PeerConnectionObserver; | 108 using webrtc::PeerConnectionObserver; |
108 using webrtc::RtpReceiverInterface; | 109 using webrtc::RtpReceiverInterface; |
109 using webrtc::RtpReceiverObserverInterface; | 110 using webrtc::RtpReceiverObserverInterface; |
110 using webrtc::RtpSenderInterface; | 111 using webrtc::RtpSenderInterface; |
111 using webrtc::SessionDescriptionInterface; | 112 using webrtc::SessionDescriptionInterface; |
112 using webrtc::SetSessionDescriptionObserver; | 113 using webrtc::SetSessionDescriptionObserver; |
113 using webrtc::StatsObserver; | 114 using webrtc::StatsObserver; |
114 using webrtc::StatsReport; | 115 using webrtc::StatsReport; |
115 using webrtc::StatsReports; | 116 using webrtc::StatsReports; |
116 using webrtc::VideoTrackSourceInterface; | |
117 using webrtc::VideoTrackInterface; | 117 using webrtc::VideoTrackInterface; |
118 using webrtc::VideoTrackVector; | |
119 using webrtc::kVideoCodecVP8; | |
120 | 118 |
121 namespace webrtc_jni { | 119 namespace webrtc_jni { |
122 | 120 |
123 // Field trials initialization string | 121 // Field trials initialization string |
124 static char *field_trials_init_string = NULL; | 122 static char *field_trials_init_string = NULL; |
125 | 123 |
126 // Set in PeerConnectionFactory_initializeAndroidGlobals(). | 124 // Set in PeerConnectionFactory_initializeAndroidGlobals(). |
127 static bool factory_static_initialized = false; | 125 static bool factory_static_initialized = false; |
128 static bool video_hw_acceleration_enabled = true; | 126 static bool video_hw_acceleration_enabled = true; |
129 | 127 |
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 } | 837 } |
840 | 838 |
841 const ScopedGlobalRef<jobject> j_observer_global_; | 839 const ScopedGlobalRef<jobject> j_observer_global_; |
842 const ScopedGlobalRef<jclass> j_observer_class_; | 840 const ScopedGlobalRef<jclass> j_observer_class_; |
843 const ScopedGlobalRef<jclass> j_stats_report_class_; | 841 const ScopedGlobalRef<jclass> j_stats_report_class_; |
844 const jmethodID j_stats_report_ctor_; | 842 const jmethodID j_stats_report_ctor_; |
845 const ScopedGlobalRef<jclass> j_value_class_; | 843 const ScopedGlobalRef<jclass> j_value_class_; |
846 const jmethodID j_value_ctor_; | 844 const jmethodID j_value_ctor_; |
847 }; | 845 }; |
848 | 846 |
849 // Wrapper dispatching rtc::VideoSinkInterface to a Java VideoRenderer | |
850 // instance. | |
851 class JavaVideoRendererWrapper | |
852 : public rtc::VideoSinkInterface<webrtc::VideoFrame> { | |
853 public: | |
854 JavaVideoRendererWrapper(JNIEnv* jni, jobject j_callbacks) | |
855 : j_callbacks_(jni, j_callbacks), | |
856 j_render_frame_id_(GetMethodID( | |
857 jni, GetObjectClass(jni, j_callbacks), "renderFrame", | |
858 "(Lorg/webrtc/VideoRenderer$I420Frame;)V")), | |
859 j_frame_class_(jni, | |
860 FindClass(jni, "org/webrtc/VideoRenderer$I420Frame")), | |
861 j_i420_frame_ctor_id_(GetMethodID( | |
862 jni, *j_frame_class_, "<init>", "(III[I[Ljava/nio/ByteBuffer;J)V")), | |
863 j_texture_frame_ctor_id_(GetMethodID( | |
864 jni, *j_frame_class_, "<init>", | |
865 "(IIII[FJ)V")), | |
866 j_byte_buffer_class_(jni, FindClass(jni, "java/nio/ByteBuffer")) { | |
867 CHECK_EXCEPTION(jni); | |
868 } | |
869 | |
870 virtual ~JavaVideoRendererWrapper() {} | |
871 | |
872 void OnFrame(const webrtc::VideoFrame& video_frame) override { | |
873 ScopedLocalRefFrame local_ref_frame(jni()); | |
874 | |
875 jobject j_frame; | |
876 if (video_frame.video_frame_buffer()->type() == | |
877 webrtc::VideoFrameBuffer::Type::kNative) { | |
878 AndroidVideoFrameBuffer* android_buffer = | |
879 static_cast<AndroidVideoFrameBuffer*>( | |
880 video_frame.video_frame_buffer().get()); | |
881 switch (android_buffer->android_type()) { | |
882 case AndroidVideoFrameBuffer::AndroidType::kTextureBuffer: | |
883 j_frame = ToJavaTextureFrame(&video_frame); | |
884 break; | |
885 case AndroidVideoFrameBuffer::AndroidType::kJavaBuffer: | |
886 j_frame = static_cast<AndroidVideoBuffer*>(android_buffer) | |
887 ->ToJavaI420Frame(jni(), video_frame.width(), | |
888 video_frame.height(), | |
889 video_frame.rotation()); | |
890 break; | |
891 default: | |
892 RTC_NOTREACHED(); | |
893 } | |
894 } else { | |
895 j_frame = ToJavaI420Frame(&video_frame); | |
896 } | |
897 // |j_callbacks_| is responsible for releasing |j_frame| with | |
898 // VideoRenderer.renderFrameDone(). | |
899 jni()->CallVoidMethod(*j_callbacks_, j_render_frame_id_, j_frame); | |
900 CHECK_EXCEPTION(jni()); | |
901 } | |
902 | |
903 private: | |
904 // Make a shallow copy of |frame| to be used with Java. The callee has | |
905 // ownership of the frame, and the frame should be released with | |
906 // VideoRenderer.releaseNativeFrame(). | |
907 static jlong javaShallowCopy(const webrtc::VideoFrame* frame) { | |
908 return jlongFromPointer(new webrtc::VideoFrame(*frame)); | |
909 } | |
910 | |
911 // Return a VideoRenderer.I420Frame referring to the data in |frame|. | |
912 jobject ToJavaI420Frame(const webrtc::VideoFrame* frame) { | |
913 jintArray strides = jni()->NewIntArray(3); | |
914 jint* strides_array = jni()->GetIntArrayElements(strides, NULL); | |
915 rtc::scoped_refptr<webrtc::I420BufferInterface> i420_buffer = | |
916 frame->video_frame_buffer()->ToI420(); | |
917 strides_array[0] = i420_buffer->StrideY(); | |
918 strides_array[1] = i420_buffer->StrideU(); | |
919 strides_array[2] = i420_buffer->StrideV(); | |
920 jni()->ReleaseIntArrayElements(strides, strides_array, 0); | |
921 jobjectArray planes = jni()->NewObjectArray(3, *j_byte_buffer_class_, NULL); | |
922 jobject y_buffer = jni()->NewDirectByteBuffer( | |
923 const_cast<uint8_t*>(i420_buffer->DataY()), | |
924 i420_buffer->StrideY() * i420_buffer->height()); | |
925 size_t chroma_height = i420_buffer->ChromaHeight(); | |
926 jobject u_buffer = | |
927 jni()->NewDirectByteBuffer(const_cast<uint8_t*>(i420_buffer->DataU()), | |
928 i420_buffer->StrideU() * chroma_height); | |
929 jobject v_buffer = | |
930 jni()->NewDirectByteBuffer(const_cast<uint8_t*>(i420_buffer->DataV()), | |
931 i420_buffer->StrideV() * chroma_height); | |
932 | |
933 jni()->SetObjectArrayElement(planes, 0, y_buffer); | |
934 jni()->SetObjectArrayElement(planes, 1, u_buffer); | |
935 jni()->SetObjectArrayElement(planes, 2, v_buffer); | |
936 return jni()->NewObject( | |
937 *j_frame_class_, j_i420_frame_ctor_id_, | |
938 frame->width(), frame->height(), | |
939 static_cast<int>(frame->rotation()), | |
940 strides, planes, javaShallowCopy(frame)); | |
941 } | |
942 | |
943 // Return a VideoRenderer.I420Frame referring texture object in |frame|. | |
944 jobject ToJavaTextureFrame(const webrtc::VideoFrame* frame) { | |
945 NativeHandleImpl handle = | |
946 static_cast<AndroidTextureBuffer*>(frame->video_frame_buffer().get()) | |
947 ->native_handle_impl(); | |
948 jfloatArray sampling_matrix = handle.sampling_matrix.ToJava(jni()); | |
949 | |
950 return jni()->NewObject( | |
951 *j_frame_class_, j_texture_frame_ctor_id_, frame->width(), | |
952 frame->height(), static_cast<int>(frame->rotation()), | |
953 handle.oes_texture_id, sampling_matrix, javaShallowCopy(frame)); | |
954 } | |
955 | |
956 JNIEnv* jni() { | |
957 return AttachCurrentThreadIfNeeded(); | |
958 } | |
959 | |
960 ScopedGlobalRef<jobject> j_callbacks_; | |
961 jmethodID j_render_frame_id_; | |
962 ScopedGlobalRef<jclass> j_frame_class_; | |
963 jmethodID j_i420_frame_ctor_id_; | |
964 jmethodID j_texture_frame_ctor_id_; | |
965 ScopedGlobalRef<jclass> j_byte_buffer_class_; | |
966 }; | |
967 | |
968 // Adapter between the C++ RtpReceiverObserverInterface and the Java | 847 // Adapter between the C++ RtpReceiverObserverInterface and the Java |
969 // RtpReceiver.Observer interface. Wraps an instance of the Java interface and | 848 // RtpReceiver.Observer interface. Wraps an instance of the Java interface and |
970 // dispatches C++ callbacks to Java. | 849 // dispatches C++ callbacks to Java. |
971 class RtpReceiverObserver : public RtpReceiverObserverInterface { | 850 class RtpReceiverObserver : public RtpReceiverObserverInterface { |
972 public: | 851 public: |
973 RtpReceiverObserver(JNIEnv* jni, jobject j_observer) | 852 RtpReceiverObserver(JNIEnv* jni, jobject j_observer) |
974 : j_observer_global_(jni, j_observer) {} | 853 : j_observer_global_(jni, j_observer) {} |
975 | 854 |
976 ~RtpReceiverObserver() override {} | 855 ~RtpReceiverObserver() override {} |
977 | 856 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1100 | 979 |
1101 JOW(void, PeerConnection_freeObserver)(JNIEnv*, jclass, jlong j_p) { | 980 JOW(void, PeerConnection_freeObserver)(JNIEnv*, jclass, jlong j_p) { |
1102 PCOJava* p = reinterpret_cast<PCOJava*>(j_p); | 981 PCOJava* p = reinterpret_cast<PCOJava*>(j_p); |
1103 delete p; | 982 delete p; |
1104 } | 983 } |
1105 | 984 |
1106 JOW(void, MediaSource_free)(JNIEnv*, jclass, jlong j_p) { | 985 JOW(void, MediaSource_free)(JNIEnv*, jclass, jlong j_p) { |
1107 reinterpret_cast<rtc::RefCountInterface*>(j_p)->Release(); | 986 reinterpret_cast<rtc::RefCountInterface*>(j_p)->Release(); |
1108 } | 987 } |
1109 | 988 |
1110 JOW(void, VideoRenderer_freeWrappedVideoRenderer)(JNIEnv*, jclass, jlong j_p) { | |
1111 delete reinterpret_cast<JavaVideoRendererWrapper*>(j_p); | |
1112 } | |
1113 | |
1114 JOW(void, VideoRenderer_releaseNativeFrame)( | |
1115 JNIEnv* jni, jclass, jlong j_frame_ptr) { | |
1116 delete reinterpret_cast<const webrtc::VideoFrame*>(j_frame_ptr); | |
1117 } | |
1118 | |
1119 JOW(void, MediaStreamTrack_free)(JNIEnv*, jclass, jlong j_p) { | 989 JOW(void, MediaStreamTrack_free)(JNIEnv*, jclass, jlong j_p) { |
1120 reinterpret_cast<MediaStreamTrackInterface*>(j_p)->Release(); | 990 reinterpret_cast<MediaStreamTrackInterface*>(j_p)->Release(); |
1121 } | 991 } |
1122 | 992 |
1123 JOW(jboolean, MediaStream_nativeAddAudioTrack)( | 993 JOW(jboolean, MediaStream_nativeAddAudioTrack)( |
1124 JNIEnv* jni, jclass, jlong pointer, jlong j_audio_track_pointer) { | 994 JNIEnv* jni, jclass, jlong pointer, jlong j_audio_track_pointer) { |
1125 return reinterpret_cast<MediaStreamInterface*>(pointer)->AddTrack( | 995 return reinterpret_cast<MediaStreamInterface*>(pointer)->AddTrack( |
1126 reinterpret_cast<AudioTrackInterface*>(j_audio_track_pointer)); | 996 reinterpret_cast<AudioTrackInterface*>(j_audio_track_pointer)); |
1127 } | 997 } |
1128 | 998 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1217 rtc::tracing::ShutdownInternalTracer(); | 1087 rtc::tracing::ShutdownInternalTracer(); |
1218 } | 1088 } |
1219 | 1089 |
1220 JOW(void, AudioTrack_nativeSetVolume) | 1090 JOW(void, AudioTrack_nativeSetVolume) |
1221 (JNIEnv*, jclass, jlong j_p, jdouble volume) { | 1091 (JNIEnv*, jclass, jlong j_p, jdouble volume) { |
1222 rtc::scoped_refptr<AudioSourceInterface> source( | 1092 rtc::scoped_refptr<AudioSourceInterface> source( |
1223 reinterpret_cast<AudioTrackInterface*>(j_p)->GetSource()); | 1093 reinterpret_cast<AudioTrackInterface*>(j_p)->GetSource()); |
1224 source->SetVolume(volume); | 1094 source->SetVolume(volume); |
1225 } | 1095 } |
1226 | 1096 |
1227 // Helper struct for working around the fact that CreatePeerConnectionFactory() | |
1228 // comes in two flavors: either entirely automagical (constructing its own | |
1229 // threads and deleting them on teardown, but no external codec factory support) | |
1230 // or entirely manual (requires caller to delete threads after factory | |
1231 // teardown). This struct takes ownership of its ctor's arguments to present a | |
1232 // single thing for Java to hold and eventually free. | |
1233 class OwnedFactoryAndThreads { | |
1234 public: | |
1235 OwnedFactoryAndThreads(std::unique_ptr<Thread> network_thread, | |
1236 std::unique_ptr<Thread> worker_thread, | |
1237 std::unique_ptr<Thread> signaling_thread, | |
1238 WebRtcVideoEncoderFactory* encoder_factory, | |
1239 WebRtcVideoDecoderFactory* decoder_factory, | |
1240 rtc::NetworkMonitorFactory* network_monitor_factory, | |
1241 PeerConnectionFactoryInterface* factory) | |
1242 : network_thread_(std::move(network_thread)), | |
1243 worker_thread_(std::move(worker_thread)), | |
1244 signaling_thread_(std::move(signaling_thread)), | |
1245 encoder_factory_(encoder_factory), | |
1246 decoder_factory_(decoder_factory), | |
1247 network_monitor_factory_(network_monitor_factory), | |
1248 factory_(factory) {} | |
1249 | |
1250 ~OwnedFactoryAndThreads() { | |
1251 CHECK_RELEASE(factory_); | |
1252 if (network_monitor_factory_ != nullptr) { | |
1253 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_); | |
1254 } | |
1255 } | |
1256 | |
1257 PeerConnectionFactoryInterface* factory() { return factory_; } | |
1258 Thread* signaling_thread() { return signaling_thread_.get(); } | |
1259 Thread* worker_thread() { return worker_thread_.get(); } | |
1260 WebRtcVideoEncoderFactory* encoder_factory() { return encoder_factory_; } | |
1261 WebRtcVideoDecoderFactory* decoder_factory() { return decoder_factory_; } | |
1262 rtc::NetworkMonitorFactory* network_monitor_factory() { | |
1263 return network_monitor_factory_; | |
1264 } | |
1265 void clear_network_monitor_factory() { network_monitor_factory_ = nullptr; } | |
1266 void InvokeJavaCallbacksOnFactoryThreads(); | |
1267 | |
1268 private: | |
1269 void JavaCallbackOnFactoryThreads(); | |
1270 | |
1271 const std::unique_ptr<Thread> network_thread_; | |
1272 const std::unique_ptr<Thread> worker_thread_; | |
1273 const std::unique_ptr<Thread> signaling_thread_; | |
1274 WebRtcVideoEncoderFactory* encoder_factory_; | |
1275 WebRtcVideoDecoderFactory* decoder_factory_; | |
1276 rtc::NetworkMonitorFactory* network_monitor_factory_; | |
1277 PeerConnectionFactoryInterface* factory_; // Const after ctor except dtor. | |
1278 }; | |
1279 | |
1280 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() { | |
1281 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | |
1282 ScopedLocalRefFrame local_ref_frame(jni); | |
1283 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory"); | |
1284 jmethodID m = nullptr; | |
1285 if (network_thread_->IsCurrent()) { | |
1286 LOG(LS_INFO) << "Network thread JavaCallback"; | |
1287 m = GetStaticMethodID(jni, j_factory_class, "onNetworkThreadReady", "()V"); | |
1288 } | |
1289 if (worker_thread_->IsCurrent()) { | |
1290 LOG(LS_INFO) << "Worker thread JavaCallback"; | |
1291 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); | |
1292 } | |
1293 if (signaling_thread_->IsCurrent()) { | |
1294 LOG(LS_INFO) << "Signaling thread JavaCallback"; | |
1295 m = GetStaticMethodID( | |
1296 jni, j_factory_class, "onSignalingThreadReady", "()V"); | |
1297 } | |
1298 if (m != nullptr) { | |
1299 jni->CallStaticVoidMethod(j_factory_class, m); | |
1300 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod"; | |
1301 } | |
1302 } | |
1303 | |
1304 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() { | |
1305 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads."; | |
1306 network_thread_->Invoke<void>(RTC_FROM_HERE, | |
1307 [this] { JavaCallbackOnFactoryThreads(); }); | |
1308 worker_thread_->Invoke<void>(RTC_FROM_HERE, | |
1309 [this] { JavaCallbackOnFactoryThreads(); }); | |
1310 signaling_thread_->Invoke<void>(RTC_FROM_HERE, | |
1311 [this] { JavaCallbackOnFactoryThreads(); }); | |
1312 } | |
1313 | |
1314 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni, | 1097 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni, |
1315 jobject options) { | 1098 jobject options) { |
1316 jclass options_class = jni->GetObjectClass(options); | 1099 jclass options_class = jni->GetObjectClass(options); |
1317 jfieldID network_ignore_mask_field = | 1100 jfieldID network_ignore_mask_field = |
1318 jni->GetFieldID(options_class, "networkIgnoreMask", "I"); | 1101 jni->GetFieldID(options_class, "networkIgnoreMask", "I"); |
1319 int network_ignore_mask = | 1102 int network_ignore_mask = |
1320 jni->GetIntField(options, network_ignore_mask_field); | 1103 jni->GetIntField(options, network_ignore_mask_field); |
1321 | 1104 |
1322 jfieldID disable_encryption_field = | 1105 jfieldID disable_encryption_field = |
1323 jni->GetFieldID(options_class, "disableEncryption", "Z"); | 1106 jni->GetFieldID(options_class, "disableEncryption", "Z"); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1355 RTC_CHECK(network_thread->Start()) << "Failed to start thread"; | 1138 RTC_CHECK(network_thread->Start()) << "Failed to start thread"; |
1356 | 1139 |
1357 std::unique_ptr<Thread> worker_thread = rtc::Thread::Create(); | 1140 std::unique_ptr<Thread> worker_thread = rtc::Thread::Create(); |
1358 worker_thread->SetName("worker_thread", nullptr); | 1141 worker_thread->SetName("worker_thread", nullptr); |
1359 RTC_CHECK(worker_thread->Start()) << "Failed to start thread"; | 1142 RTC_CHECK(worker_thread->Start()) << "Failed to start thread"; |
1360 | 1143 |
1361 std::unique_ptr<Thread> signaling_thread = rtc::Thread::Create(); | 1144 std::unique_ptr<Thread> signaling_thread = rtc::Thread::Create(); |
1362 signaling_thread->SetName("signaling_thread", NULL); | 1145 signaling_thread->SetName("signaling_thread", NULL); |
1363 RTC_CHECK(signaling_thread->Start()) << "Failed to start thread"; | 1146 RTC_CHECK(signaling_thread->Start()) << "Failed to start thread"; |
1364 | 1147 |
1365 WebRtcVideoEncoderFactory* encoder_factory = nullptr; | 1148 WebRtcVideoEncoderFactory* video_encoder_factory = nullptr; |
1366 WebRtcVideoDecoderFactory* decoder_factory = nullptr; | 1149 WebRtcVideoDecoderFactory* video_decoder_factory = nullptr; |
1367 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr; | 1150 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr; |
| 1151 auto audio_encoder_factory = CreateAudioEncoderFactory(); |
| 1152 auto audio_decoder_factory = CreateAudioDecoderFactory(); |
1368 | 1153 |
1369 PeerConnectionFactoryInterface::Options options; | 1154 PeerConnectionFactoryInterface::Options options; |
1370 bool has_options = joptions != NULL; | 1155 bool has_options = joptions != NULL; |
1371 if (has_options) { | 1156 if (has_options) { |
1372 options = ParseOptionsFromJava(jni, joptions); | 1157 options = ParseOptionsFromJava(jni, joptions); |
1373 } | 1158 } |
1374 | 1159 |
1375 if (video_hw_acceleration_enabled) { | 1160 if (video_hw_acceleration_enabled) { |
1376 encoder_factory = new MediaCodecVideoEncoderFactory(); | 1161 video_encoder_factory = CreateVideoEncoderFactory(); |
1377 decoder_factory = new MediaCodecVideoDecoderFactory(); | 1162 video_decoder_factory = CreateVideoDecoderFactory(); |
1378 } | 1163 } |
1379 // Do not create network_monitor_factory only if the options are | 1164 // Do not create network_monitor_factory only if the options are |
1380 // provided and disable_network_monitor therein is set to true. | 1165 // provided and disable_network_monitor therein is set to true. |
1381 if (!(has_options && options.disable_network_monitor)) { | 1166 if (!(has_options && options.disable_network_monitor)) { |
1382 network_monitor_factory = new AndroidNetworkMonitorFactory(); | 1167 network_monitor_factory = new AndroidNetworkMonitorFactory(); |
1383 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory); | 1168 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory); |
1384 } | 1169 } |
1385 | 1170 |
1386 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | 1171 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( |
1387 webrtc::CreatePeerConnectionFactory( | 1172 CreateNativePeerConnectionFactory( |
1388 network_thread.get(), worker_thread.get(), signaling_thread.get(), | 1173 network_thread.get(), worker_thread.get(), signaling_thread.get(), |
1389 nullptr, encoder_factory, decoder_factory)); | 1174 nullptr, audio_encoder_factory, audio_decoder_factory, |
| 1175 video_encoder_factory, video_decoder_factory)); |
1390 RTC_CHECK(factory) << "Failed to create the peer connection factory; " | 1176 RTC_CHECK(factory) << "Failed to create the peer connection factory; " |
1391 << "WebRTC/libjingle init likely failed on this device"; | 1177 << "WebRTC/libjingle init likely failed on this device"; |
1392 // TODO(honghaiz): Maybe put the options as the argument of | 1178 // TODO(honghaiz): Maybe put the options as the argument of |
1393 // CreatePeerConnectionFactory. | 1179 // CreatePeerConnectionFactory. |
1394 if (has_options) { | 1180 if (has_options) { |
1395 factory->SetOptions(options); | 1181 factory->SetOptions(options); |
1396 } | 1182 } |
1397 OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads( | 1183 OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads( |
1398 std::move(network_thread), std::move(worker_thread), | 1184 std::move(network_thread), std::move(worker_thread), |
1399 std::move(signaling_thread), encoder_factory, decoder_factory, | 1185 std::move(signaling_thread), video_encoder_factory, video_decoder_factory, |
1400 network_monitor_factory, factory.release()); | 1186 network_monitor_factory, factory.release()); |
1401 owned_factory->InvokeJavaCallbacksOnFactoryThreads(); | 1187 owned_factory->InvokeJavaCallbacksOnFactoryThreads(); |
1402 return jlongFromPointer(owned_factory); | 1188 return jlongFromPointer(owned_factory); |
1403 } | 1189 } |
1404 | 1190 |
1405 JOW(void, PeerConnectionFactory_nativeFreeFactory)(JNIEnv*, jclass, jlong j_p) { | 1191 JOW(void, PeerConnectionFactory_nativeFreeFactory)(JNIEnv*, jclass, jlong j_p) { |
1406 delete reinterpret_cast<OwnedFactoryAndThreads*>(j_p); | 1192 delete reinterpret_cast<OwnedFactoryAndThreads*>(j_p); |
1407 if (field_trials_init_string) { | 1193 if (field_trials_init_string) { |
1408 webrtc::field_trial::InitFieldTrialsFromString(NULL); | 1194 webrtc::field_trial::InitFieldTrialsFromString(NULL); |
1409 delete field_trials_init_string; | 1195 delete field_trials_init_string; |
1410 field_trials_init_string = NULL; | 1196 field_trials_init_string = NULL; |
1411 } | 1197 } |
1412 webrtc::Trace::ReturnTrace(); | 1198 webrtc::Trace::ReturnTrace(); |
1413 } | 1199 } |
1414 | 1200 |
1415 static PeerConnectionFactoryInterface* factoryFromJava(jlong j_p) { | |
1416 return reinterpret_cast<OwnedFactoryAndThreads*>(j_p)->factory(); | |
1417 } | |
1418 | |
1419 JOW(void, PeerConnectionFactory_nativeThreadsCallbacks)( | 1201 JOW(void, PeerConnectionFactory_nativeThreadsCallbacks)( |
1420 JNIEnv*, jclass, jlong j_p) { | 1202 JNIEnv*, jclass, jlong j_p) { |
1421 OwnedFactoryAndThreads *factory = | 1203 OwnedFactoryAndThreads *factory = |
1422 reinterpret_cast<OwnedFactoryAndThreads*>(j_p); | 1204 reinterpret_cast<OwnedFactoryAndThreads*>(j_p); |
1423 factory->InvokeJavaCallbacksOnFactoryThreads(); | 1205 factory->InvokeJavaCallbacksOnFactoryThreads(); |
1424 } | 1206 } |
1425 | 1207 |
1426 JOW(jlong, PeerConnectionFactory_nativeCreateLocalMediaStream)( | 1208 JOW(jlong, PeerConnectionFactory_nativeCreateLocalMediaStream)( |
1427 JNIEnv* jni, jclass, jlong native_factory, jstring label) { | 1209 JNIEnv* jni, jclass, jlong native_factory, jstring label) { |
1428 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | 1210 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( |
1429 factoryFromJava(native_factory)); | 1211 factoryFromJava(native_factory)); |
1430 rtc::scoped_refptr<MediaStreamInterface> stream( | 1212 rtc::scoped_refptr<MediaStreamInterface> stream( |
1431 factory->CreateLocalMediaStream(JavaToStdString(jni, label))); | 1213 factory->CreateLocalMediaStream(JavaToStdString(jni, label))); |
1432 return (jlong)stream.release(); | 1214 return (jlong)stream.release(); |
1433 } | 1215 } |
1434 | 1216 |
1435 JOW(jlong, PeerConnectionFactory_nativeCreateVideoSource) | |
1436 (JNIEnv* jni, | |
1437 jclass, | |
1438 jlong native_factory, | |
1439 jobject j_surface_texture_helper, | |
1440 jboolean is_screencast) { | |
1441 OwnedFactoryAndThreads* factory = | |
1442 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); | |
1443 | |
1444 rtc::scoped_refptr<webrtc::AndroidVideoTrackSource> source( | |
1445 new rtc::RefCountedObject<webrtc::AndroidVideoTrackSource>( | |
1446 factory->signaling_thread(), jni, j_surface_texture_helper, | |
1447 is_screencast)); | |
1448 rtc::scoped_refptr<webrtc::VideoTrackSourceProxy> proxy_source = | |
1449 webrtc::VideoTrackSourceProxy::Create(factory->signaling_thread(), | |
1450 factory->worker_thread(), source); | |
1451 | |
1452 return (jlong)proxy_source.release(); | |
1453 } | |
1454 | |
1455 JOW(jlong, PeerConnectionFactory_nativeCreateVideoTrack)( | |
1456 JNIEnv* jni, jclass, jlong native_factory, jstring id, | |
1457 jlong native_source) { | |
1458 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1459 factoryFromJava(native_factory)); | |
1460 rtc::scoped_refptr<VideoTrackInterface> track(factory->CreateVideoTrack( | |
1461 JavaToStdString(jni, id), | |
1462 reinterpret_cast<VideoTrackSourceInterface*>(native_source))); | |
1463 return (jlong)track.release(); | |
1464 } | |
1465 | |
1466 JOW(jlong, PeerConnectionFactory_nativeCreateAudioSource)( | 1217 JOW(jlong, PeerConnectionFactory_nativeCreateAudioSource)( |
1467 JNIEnv* jni, jclass, jlong native_factory, jobject j_constraints) { | 1218 JNIEnv* jni, jclass, jlong native_factory, jobject j_constraints) { |
1468 std::unique_ptr<ConstraintsWrapper> constraints( | 1219 std::unique_ptr<ConstraintsWrapper> constraints( |
1469 new ConstraintsWrapper(jni, j_constraints)); | 1220 new ConstraintsWrapper(jni, j_constraints)); |
1470 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | 1221 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( |
1471 factoryFromJava(native_factory)); | 1222 factoryFromJava(native_factory)); |
1472 cricket::AudioOptions options; | 1223 cricket::AudioOptions options; |
1473 CopyConstraintsIntoAudioOptions(constraints.get(), &options); | 1224 CopyConstraintsIntoAudioOptions(constraints.get(), &options); |
1474 rtc::scoped_refptr<AudioSourceInterface> source( | 1225 rtc::scoped_refptr<AudioSourceInterface> source( |
1475 factory->CreateAudioSource(options)); | 1226 factory->CreateAudioSource(options)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1514 OwnedFactoryAndThreads* owner = | 1265 OwnedFactoryAndThreads* owner = |
1515 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); | 1266 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); |
1516 if (owner->network_monitor_factory()) { | 1267 if (owner->network_monitor_factory()) { |
1517 rtc::NetworkMonitorFactory::ReleaseFactory( | 1268 rtc::NetworkMonitorFactory::ReleaseFactory( |
1518 owner->network_monitor_factory()); | 1269 owner->network_monitor_factory()); |
1519 owner->clear_network_monitor_factory(); | 1270 owner->clear_network_monitor_factory(); |
1520 } | 1271 } |
1521 } | 1272 } |
1522 } | 1273 } |
1523 | 1274 |
1524 JOW(void, PeerConnectionFactory_nativeSetVideoHwAccelerationOptions)( | |
1525 JNIEnv* jni, jclass, jlong native_factory, jobject local_egl_context, | |
1526 jobject remote_egl_context) { | |
1527 OwnedFactoryAndThreads* owned_factory = | |
1528 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); | |
1529 | |
1530 jclass j_eglbase14_context_class = | |
1531 FindClass(jni, "org/webrtc/EglBase14$Context"); | |
1532 | |
1533 MediaCodecVideoEncoderFactory* encoder_factory = | |
1534 static_cast<MediaCodecVideoEncoderFactory*> | |
1535 (owned_factory->encoder_factory()); | |
1536 if (encoder_factory && | |
1537 jni->IsInstanceOf(local_egl_context, j_eglbase14_context_class)) { | |
1538 LOG(LS_INFO) << "Set EGL context for HW encoding."; | |
1539 encoder_factory->SetEGLContext(jni, local_egl_context); | |
1540 } | |
1541 | |
1542 MediaCodecVideoDecoderFactory* decoder_factory = | |
1543 static_cast<MediaCodecVideoDecoderFactory*> | |
1544 (owned_factory->decoder_factory()); | |
1545 if (decoder_factory) { | |
1546 LOG(LS_INFO) << "Set EGL context for HW decoding."; | |
1547 decoder_factory->SetEGLContext(jni, remote_egl_context); | |
1548 } | |
1549 } | |
1550 | |
1551 static PeerConnectionInterface::IceTransportsType | 1275 static PeerConnectionInterface::IceTransportsType |
1552 JavaIceTransportsTypeToNativeType(JNIEnv* jni, jobject j_ice_transports_type) { | 1276 JavaIceTransportsTypeToNativeType(JNIEnv* jni, jobject j_ice_transports_type) { |
1553 std::string enum_name = GetJavaEnumName( | 1277 std::string enum_name = GetJavaEnumName( |
1554 jni, "org/webrtc/PeerConnection$IceTransportsType", | 1278 jni, "org/webrtc/PeerConnection$IceTransportsType", |
1555 j_ice_transports_type); | 1279 j_ice_transports_type); |
1556 | 1280 |
1557 if (enum_name == "ALL") | 1281 if (enum_name == "ALL") |
1558 return PeerConnectionInterface::kAll; | 1282 return PeerConnectionInterface::kAll; |
1559 | 1283 |
1560 if (enum_name == "RELAY") | 1284 if (enum_name == "RELAY") |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2172 ExtractNativePC(jni, j_pc)->Close(); | 1896 ExtractNativePC(jni, j_pc)->Close(); |
2173 return; | 1897 return; |
2174 } | 1898 } |
2175 | 1899 |
2176 JOW(jobject, MediaSource_nativeState)(JNIEnv* jni, jclass, jlong j_p) { | 1900 JOW(jobject, MediaSource_nativeState)(JNIEnv* jni, jclass, jlong j_p) { |
2177 rtc::scoped_refptr<MediaSourceInterface> p( | 1901 rtc::scoped_refptr<MediaSourceInterface> p( |
2178 reinterpret_cast<MediaSourceInterface*>(j_p)); | 1902 reinterpret_cast<MediaSourceInterface*>(j_p)); |
2179 return JavaEnumFromIndex(jni, "MediaSource$State", p->state()); | 1903 return JavaEnumFromIndex(jni, "MediaSource$State", p->state()); |
2180 } | 1904 } |
2181 | 1905 |
2182 JOW(jlong, VideoRenderer_nativeWrapVideoRenderer)( | |
2183 JNIEnv* jni, jclass, jobject j_callbacks) { | |
2184 std::unique_ptr<JavaVideoRendererWrapper> renderer( | |
2185 new JavaVideoRendererWrapper(jni, j_callbacks)); | |
2186 return (jlong)renderer.release(); | |
2187 } | |
2188 | |
2189 JOW(void, VideoRenderer_nativeCopyPlane)( | |
2190 JNIEnv *jni, jclass, jobject j_src_buffer, jint width, jint height, | |
2191 jint src_stride, jobject j_dst_buffer, jint dst_stride) { | |
2192 size_t src_size = jni->GetDirectBufferCapacity(j_src_buffer); | |
2193 size_t dst_size = jni->GetDirectBufferCapacity(j_dst_buffer); | |
2194 RTC_CHECK(src_stride >= width) << "Wrong source stride " << src_stride; | |
2195 RTC_CHECK(dst_stride >= width) << "Wrong destination stride " << dst_stride; | |
2196 RTC_CHECK(src_size >= src_stride * height) | |
2197 << "Insufficient source buffer capacity " << src_size; | |
2198 RTC_CHECK(dst_size >= dst_stride * height) | |
2199 << "Insufficient destination buffer capacity " << dst_size; | |
2200 uint8_t *src = | |
2201 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_src_buffer)); | |
2202 uint8_t *dst = | |
2203 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_buffer)); | |
2204 if (src_stride == dst_stride) { | |
2205 memcpy(dst, src, src_stride * height); | |
2206 } else { | |
2207 for (int i = 0; i < height; i++) { | |
2208 memcpy(dst, src, width); | |
2209 src += src_stride; | |
2210 dst += dst_stride; | |
2211 } | |
2212 } | |
2213 } | |
2214 | |
2215 JOW(void, FileVideoCapturer_nativeI420ToNV21)( | 1906 JOW(void, FileVideoCapturer_nativeI420ToNV21)( |
2216 JNIEnv *jni, jclass, jbyteArray j_src_buffer, jint width, jint height, | 1907 JNIEnv *jni, jclass, jbyteArray j_src_buffer, jint width, jint height, |
2217 jbyteArray j_dst_buffer) { | 1908 jbyteArray j_dst_buffer) { |
2218 size_t src_size = jni->GetArrayLength(j_src_buffer); | 1909 size_t src_size = jni->GetArrayLength(j_src_buffer); |
2219 size_t dst_size = jni->GetArrayLength(j_dst_buffer); | 1910 size_t dst_size = jni->GetArrayLength(j_dst_buffer); |
2220 int src_stride = width; | 1911 int src_stride = width; |
2221 int dst_stride = width; | 1912 int dst_stride = width; |
2222 RTC_CHECK_GE(src_size, src_stride * height * 3 / 2); | 1913 RTC_CHECK_GE(src_size, src_stride * height * 3 / 2); |
2223 RTC_CHECK_GE(dst_size, dst_stride * height * 3 / 2); | 1914 RTC_CHECK_GE(dst_size, dst_stride * height * 3 / 2); |
2224 | 1915 |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2736 return reinterpret_cast<DtmfSenderInterface*>(j_dtmf_sender_pointer) | 2427 return reinterpret_cast<DtmfSenderInterface*>(j_dtmf_sender_pointer) |
2737 ->inter_tone_gap(); | 2428 ->inter_tone_gap(); |
2738 } | 2429 } |
2739 | 2430 |
2740 JOW(void, DtmfSender_free) | 2431 JOW(void, DtmfSender_free) |
2741 (JNIEnv* jni, jclass, jlong j_dtmf_sender_pointer) { | 2432 (JNIEnv* jni, jclass, jlong j_dtmf_sender_pointer) { |
2742 reinterpret_cast<DtmfSenderInterface*>(j_dtmf_sender_pointer)->Release(); | 2433 reinterpret_cast<DtmfSenderInterface*>(j_dtmf_sender_pointer)->Release(); |
2743 } | 2434 } |
2744 | 2435 |
2745 } // namespace webrtc_jni | 2436 } // namespace webrtc_jni |
OLD | NEW |