diff --git a/webrtc/examples/android/media_demo/jni/on_load.cc b/webrtc/examples/android/media_demo/jni/on_load.cc index 27a2394b3d..9f9a0c7ac1 100644 --- a/webrtc/examples/android/media_demo/jni/on_load.cc +++ b/webrtc/examples/android/media_demo/jni/on_load.cc @@ -47,6 +47,8 @@ JOWW(void, NativeWebRtcContextRegistry_register)( JOWW(void, NativeWebRtcContextRegistry_unRegister)( JNIEnv* jni, jclass) { + CHECK(webrtc::VideoEngine::SetAndroidObjects(NULL) == 0, + "Failed to unregister android objects from video engine"); CHECK(webrtc::VoiceEngine::SetAndroidObjects(NULL, NULL, NULL) == 0, "Failed to unregister android objects from voice engine"); webrtc_examples::ClearVieDeviceObjects(); diff --git a/webrtc/modules/video_capture/android/device_info_android.cc b/webrtc/modules/video_capture/android/device_info_android.cc index 144fcbd57e..4a80fe2722 100644 --- a/webrtc/modules/video_capture/android/device_info_android.cc +++ b/webrtc/modules/video_capture/android/device_info_android.cc @@ -142,6 +142,13 @@ void DeviceInfoAndroid::Initialize(JNIEnv* jni) { } } +void DeviceInfoAndroid::DeInitialize() { + if (g_camera_info) { + delete g_camera_info; + g_camera_info = NULL; + } +} + VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo( const int32_t id) { return new videocapturemodule::DeviceInfoAndroid(id); diff --git a/webrtc/modules/video_capture/android/device_info_android.h b/webrtc/modules/video_capture/android/device_info_android.h index b8d838c037..542cbba088 100644 --- a/webrtc/modules/video_capture/android/device_info_android.h +++ b/webrtc/modules/video_capture/android/device_info_android.h @@ -24,6 +24,7 @@ namespace videocapturemodule class DeviceInfoAndroid : public DeviceInfoImpl { public: static void Initialize(JNIEnv* env); + static void DeInitialize(); DeviceInfoAndroid(int32_t id); virtual ~DeviceInfoAndroid(); diff --git a/webrtc/modules/video_capture/android/video_capture_android.cc b/webrtc/modules/video_capture/android/video_capture_android.cc index 93c71922c5..f42329e300 100644 --- a/webrtc/modules/video_capture/android/video_capture_android.cc +++ b/webrtc/modules/video_capture/android/video_capture_android.cc @@ -39,25 +39,37 @@ void JNICALL ProvideCameraFrame( } int32_t SetCaptureAndroidVM(JavaVM* javaVM) { - assert(!g_jvm); - g_jvm = javaVM; - AttachThreadScoped ats(g_jvm); + if (javaVM) { + assert(!g_jvm); + g_jvm = javaVM; + AttachThreadScoped ats(g_jvm); - videocapturemodule::DeviceInfoAndroid::Initialize(ats.env()); + videocapturemodule::DeviceInfoAndroid::Initialize(ats.env()); - jclass j_capture_class = - ats.env()->FindClass("org/webrtc/videoengine/VideoCaptureAndroid"); - assert(j_capture_class); - g_java_capturer_class = - reinterpret_cast(ats.env()->NewGlobalRef(j_capture_class)); - assert(g_java_capturer_class); + jclass j_capture_class = + ats.env()->FindClass("org/webrtc/videoengine/VideoCaptureAndroid"); + assert(j_capture_class); + g_java_capturer_class = + reinterpret_cast(ats.env()->NewGlobalRef(j_capture_class)); + assert(g_java_capturer_class); - JNINativeMethod native_method = { - "ProvideCameraFrame", "([BIJ)V", - reinterpret_cast(&ProvideCameraFrame) - }; - if (ats.env()->RegisterNatives(g_java_capturer_class, &native_method, 1) != 0) - assert(false); + JNINativeMethod native_method = { + "ProvideCameraFrame", "([BIJ)V", + reinterpret_cast(&ProvideCameraFrame) + }; + if (ats.env()->RegisterNatives(g_java_capturer_class, + &native_method, 1) != 0) + assert(false); + } else { + if (g_jvm) { + AttachThreadScoped ats(g_jvm); + ats.env()->UnregisterNatives(g_java_capturer_class); + ats.env()->DeleteGlobalRef(g_java_capturer_class); + g_java_capturer_class = NULL; + videocapturemodule::DeviceInfoAndroid::DeInitialize(); + g_jvm = NULL; + } + } return 0; }