From 7562cc2e26dee7db146002d91c5933b6a35095e7 Mon Sep 17 00:00:00 2001 From: Youness Alaoui Date: Wed, 17 Oct 2012 19:42:59 -0400 Subject: [PATCH] Add a deinit function and remove jclass references in case of error --- .../gst-android-graphics-imageformat.c | 26 ++++++- .../gst-android-graphics-imageformat.h | 1 + .../gst-android-graphics-surfacetexture.c | 26 ++++++- .../gst-android-graphics-surfacetexture.h | 1 + .../gst-android-hardware-camera.c | 76 +++++++++++++++---- .../gst-android-hardware-camera.h | 1 + sys/androidcamera/gst-androidmedia.c | 12 ++- 7 files changed, 121 insertions(+), 22 deletions(-) diff --git a/sys/androidcamera/gst-android-graphics-imageformat.c b/sys/androidcamera/gst-android-graphics-imageformat.c index a79f6f0b86..d0766b4da7 100644 --- a/sys/androidcamera/gst-android-graphics-imageformat.c +++ b/sys/androidcamera/gst-android-graphics-imageformat.c @@ -38,7 +38,7 @@ static struct jint UNKNOWN; jint YUY2; jint YV12; -} android_graphics_imageformat; +} android_graphics_imageformat = {0}; gint ImageFormat_JPEG; gint ImageFormat_NV16; @@ -48,8 +48,8 @@ gint ImageFormat_UNKNOWN; gint ImageFormat_YUY2; gint ImageFormat_YV12; -gboolean -gst_android_graphics_imageformat_init () +static gboolean +_init_classes () { JNIEnv *env = gst_dvm_get_env (); @@ -77,6 +77,26 @@ gst_android_graphics_imageformat_init () return TRUE; } +gboolean +gst_android_graphics_imageformat_init () +{ + if (!_init_classes ()) { + gst_android_graphics_imageformat_deinit (); + return FALSE; + } + + return TRUE; +} + +void +gst_android_graphics_imageformat_deinit () +{ + JNIEnv *env = gst_dvm_get_env (); + + if (android_graphics_imageformat.klass) + (*env)->DeleteGlobalRef (env, android_graphics_imageformat.klass); + android_graphics_imageformat.klass = NULL; +} /* android.graphics.ImageFormat */ gint diff --git a/sys/androidcamera/gst-android-graphics-imageformat.h b/sys/androidcamera/gst-android-graphics-imageformat.h index 590e828620..84b48f8af6 100644 --- a/sys/androidcamera/gst-android-graphics-imageformat.h +++ b/sys/androidcamera/gst-android-graphics-imageformat.h @@ -37,6 +37,7 @@ extern gint ImageFormat_YUY2; extern gint ImageFormat_YV12; gboolean gst_android_graphics_imageformat_init (void); +void gst_android_graphics_imageformat_deinit (void); gint gst_ag_imageformat_get_bits_per_pixel (gint format); diff --git a/sys/androidcamera/gst-android-graphics-surfacetexture.c b/sys/androidcamera/gst-android-graphics-surfacetexture.c index 432f12034a..27ef352771 100644 --- a/sys/androidcamera/gst-android-graphics-surfacetexture.c +++ b/sys/androidcamera/gst-android-graphics-surfacetexture.c @@ -32,10 +32,10 @@ static struct jclass klass; jmethodID constructor; jmethodID release; -} android_graphics_surfacetexture; +} android_graphics_surfacetexture = {0}; -gboolean -gst_android_graphics_surfacetexture_init () +static gboolean +_init_classes () { JNIEnv *env = gst_dvm_get_env (); @@ -49,6 +49,26 @@ gst_android_graphics_surfacetexture_init () return TRUE; } +gboolean +gst_android_graphics_surfacetexture_init () +{ + if (!_init_classes ()) { + gst_android_graphics_surfacetexture_deinit (); + return FALSE; + } + + return TRUE; +} + +void +gst_android_graphics_surfacetexture_deinit () +{ + JNIEnv *env = gst_dvm_get_env (); + + if (android_graphics_surfacetexture.klass) + (*env)->DeleteGlobalRef (env, android_graphics_surfacetexture.klass); + android_graphics_surfacetexture.klass = NULL; +} /* android.graphics.SurfaceTexture */ GstAGSurfaceTexture * diff --git a/sys/androidcamera/gst-android-graphics-surfacetexture.h b/sys/androidcamera/gst-android-graphics-surfacetexture.h index 4d77e874b2..7938e3e8fe 100644 --- a/sys/androidcamera/gst-android-graphics-surfacetexture.h +++ b/sys/androidcamera/gst-android-graphics-surfacetexture.h @@ -36,6 +36,7 @@ struct _GstAGSurfaceTexture { gboolean gst_android_graphics_surfacetexture_init (void); +void gst_android_graphics_surfacetexture_deinit (void); /* android.graphics.SurfaceTexture */ GstAGSurfaceTexture *gst_ag_surfacetexture_new (gint texture_id); diff --git a/sys/androidcamera/gst-android-hardware-camera.c b/sys/androidcamera/gst-android-hardware-camera.c index 170fafbc81..963c72e871 100644 --- a/sys/androidcamera/gst-android-hardware-camera.c +++ b/sys/androidcamera/gst-android-hardware-camera.c @@ -46,7 +46,7 @@ static struct jmethodID stopPreview; jmethodID stopSmoothZoom; jmethodID unlock; -} android_hardware_camera; +} android_hardware_camera = {0}; static struct { @@ -56,7 +56,7 @@ static struct jfieldID orientation; jint CAMERA_FACING_BACK; jint CAMERA_FACING_FRONT; -} android_hardware_camera_camerainfo; +} android_hardware_camera_camerainfo = {0}; gint CameraInfo_CAMERA_FACING_BACK; gint CameraInfo_CAMERA_FACING_FRONT; @@ -65,7 +65,7 @@ static struct jclass klass; jfieldID width; jfieldID height; -} android_hardware_camera_size; +} android_hardware_camera_size = {0}; /* TODO: Add other parameters */ static struct @@ -82,32 +82,32 @@ static struct jmethodID setPreviewFpsRange; jmethodID setPreviewSize; jmethodID unflatten; -} android_hardware_camera_parameters; +} android_hardware_camera_parameters = {0}; static struct { jclass klass; jmethodID iterator; -} java_util_list; +} java_util_list = {0}; static struct { jclass klass; jmethodID hasNext; jmethodID next; -} java_util_iterator; +} java_util_iterator = {0}; static struct { jclass klass; jmethodID intValue; -} java_lang_integer; +} java_lang_integer = {0}; static struct { jclass klass; jmethodID constructor; -} com_gstreamer_gstahccallback; +} com_gstreamer_gstahccallback = {0}; static void gst_ah_camera_on_preview_frame (JNIEnv * env, jclass klass, jbyteArray data, @@ -134,16 +134,11 @@ static JNINativeMethod native_methods[] = { (void *) gst_ah_camera_on_error} }; -gboolean -gst_android_hardware_camera_init () +static gboolean +_init_classes () { JNIEnv *env = gst_dvm_get_env (); - if (!gst_android_graphics_surfacetexture_init ()) - return FALSE; - if (!gst_android_graphics_imageformat_init ()) - return FALSE; - /* android.hardware.Camera */ GST_DVM_GET_CLASS (android_hardware_camera, "android/hardware/Camera"); GST_DVM_GET_METHOD (android_hardware_camera, addCallbackBuffer, "([B)V"); @@ -248,6 +243,57 @@ gst_android_hardware_camera_init () } +gboolean +gst_android_hardware_camera_init () +{ + if (!_init_classes ()) { + gst_android_hardware_camera_deinit (); + return FALSE; + } + + return TRUE; +} + +void +gst_android_hardware_camera_deinit () +{ + JNIEnv *env = gst_dvm_get_env (); + + if (android_hardware_camera.klass) + (*env)->DeleteGlobalRef (env, android_hardware_camera.klass); + android_hardware_camera.klass = NULL; + + if (android_hardware_camera_camerainfo.klass) + (*env)->DeleteGlobalRef (env, android_hardware_camera_camerainfo.klass); + android_hardware_camera_camerainfo.klass = NULL; + + if (android_hardware_camera_size.klass) + (*env)->DeleteGlobalRef (env, android_hardware_camera_size.klass); + android_hardware_camera_size.klass = NULL; + + if (android_hardware_camera_parameters.klass) + (*env)->DeleteGlobalRef (env, android_hardware_camera_parameters.klass); + android_hardware_camera_parameters.klass = NULL; + + if (java_util_list.klass) + (*env)->DeleteGlobalRef (env, java_util_list.klass); + java_util_list.klass = NULL; + + if (java_util_iterator.klass) + (*env)->DeleteGlobalRef (env, java_util_iterator.klass); + java_util_iterator.klass = NULL; + + if (java_lang_integer.klass) + (*env)->DeleteGlobalRef (env, java_lang_integer.klass); + java_lang_integer.klass = NULL; + + if (com_gstreamer_gstahccallback.klass) { + (*env)->UnregisterNatives (env, com_gstreamer_gstahccallback.klass); + (*env)->DeleteGlobalRef (env, com_gstreamer_gstahccallback.klass); + } + com_gstreamer_gstahccallback.klass = NULL; +} + /* android.hardware.Camera */ void gst_ah_camera_add_callback_buffer (GstAHCamera * self, jbyteArray buffer) diff --git a/sys/androidcamera/gst-android-hardware-camera.h b/sys/androidcamera/gst-android-hardware-camera.h index a83811880b..2ff03f8d2e 100644 --- a/sys/androidcamera/gst-android-hardware-camera.h +++ b/sys/androidcamera/gst-android-hardware-camera.h @@ -66,6 +66,7 @@ typedef void (*GstAHCErrorCallback) (gint error, gpointer user_data); typedef void (*GstAHCPreviewCallback) (jbyteArray data, gpointer user_data); gboolean gst_android_hardware_camera_init (void); +void gst_android_hardware_camera_deinit (void); /* android.hardware.Camera */ void gst_ah_camera_add_callback_buffer (GstAHCamera *self, diff --git a/sys/androidcamera/gst-androidmedia.c b/sys/androidcamera/gst-androidmedia.c index ee42f2b838..ad44149a38 100644 --- a/sys/androidcamera/gst-androidmedia.c +++ b/sys/androidcamera/gst-androidmedia.c @@ -38,9 +38,19 @@ plugin_init (GstPlugin * plugin) if (!gst_dvm_init ()) return FALSE; - if (!gst_android_hardware_camera_init ()) + if (!gst_android_graphics_surfacetexture_init ()) return FALSE; + if (!gst_android_graphics_imageformat_init ()) { + gst_android_graphics_surfacetexture_deinit (); + return FALSE; + } + if (!gst_android_hardware_camera_init ()) { + gst_android_graphics_surfacetexture_deinit (); + gst_android_graphics_imageformat_deinit (); + return FALSE; + } + return gst_element_register (plugin, "ahcsrc", GST_RANK_NONE, GST_TYPE_AHC_SRC); }