Add a deinit function and remove jclass references in case of error

This commit is contained in:
Youness Alaoui 2012-10-17 19:42:59 -04:00 committed by Nicolas Dufresne
parent 37685f09e0
commit 7562cc2e26
7 changed files with 121 additions and 22 deletions

View file

@ -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

View file

@ -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);

View file

@ -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 *

View file

@ -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);

View file

@ -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)

View file

@ -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,

View file

@ -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);
}