From 7cb4752184f93437ff17093d149b6103704aff4e Mon Sep 17 00:00:00 2001 From: "braveyao@webrtc.org" Date: Thu, 15 May 2014 03:18:15 +0000 Subject: [PATCH] WebRTCDemo: couldn't run a second time. The reason is voe could register/unregister for each run, but vie would expect initialization only once per process. This cl is to teach videocapture android how to deinitialize and allow it to be re-initializable. BUG=3284 TEST=ManualTest R=fischman@webrtc.org Review URL: https://webrtc-codereview.appspot.com/15429004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@6167 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../android/media_demo/jni/on_load.cc | 2 + .../android/device_info_android.cc | 7 +++ .../android/device_info_android.h | 1 + .../android/video_capture_android.cc | 44 ++++++++++++------- 4 files changed, 38 insertions(+), 16 deletions(-) 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; }