From 265538ac6eb780d10d159da9f40af7d2a48847f7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Sat, 28 Mar 2015 15:44:43 +0100 Subject: [PATCH] amc: Use new JNI utils almost everywhere --- sys/androidmedia/gstamc.c | 686 ++++++++++++---------------------------------- sys/androidmedia/gstamc.h | 17 +- 2 files changed, 177 insertions(+), 526 deletions(-) diff --git a/sys/androidmedia/gstamc.c b/sys/androidmedia/gstamc.c index 1f45f31..dd41ad5 100644 --- a/sys/androidmedia/gstamc.c +++ b/sys/androidmedia/gstamc.c @@ -1,6 +1,7 @@ /* * Copyright (C) 2012, Collabora Ltd. * Author: Sebastian Dröge + * Copyright (C) 2015, Sebastian Dröge * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -117,36 +118,29 @@ gst_amc_codec_new (const gchar * name, GError ** err) env = gst_amc_jni_get_env (); - name_str = (*env)->NewStringUTF (env, name); - if (name_str == NULL) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create Java String"); + name_str = gst_amc_jni_string_from_gchar (env, err, FALSE, name); + if (!name_str) { goto error; } codec = g_slice_new0 (GstAmcCodec); - object = - (*env)->CallStaticObjectMethod (env, media_codec.klass, - media_codec.create_by_codec_name, name_str); - if ((*env)->ExceptionCheck (env) || !object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create codec '%s'", name); + if (!gst_amc_jni_call_static_object_method (env, err, media_codec.klass, + media_codec.create_by_codec_name, &object, name_str)) goto error; - } - codec->object = (*env)->NewGlobalRef (env, object); + codec->object = gst_amc_jni_object_make_global (env, object); + object = NULL; + if (!codec->object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create global codec reference"); + gst_amc_jni_set_error (env, err, GST_LIBRARY_ERROR, + GST_LIBRARY_ERROR_SETTINGS, "Failed to create global codec reference"); goto error; } done: - if (object) - (*env)->DeleteLocalRef (env, object); if (name_str) - (*env)->DeleteLocalRef (env, name_str); + gst_amc_jni_object_local_unref (env, name_str); name_str = NULL; return codec; @@ -166,7 +160,7 @@ gst_amc_codec_free (GstAmcCodec * codec) g_return_if_fail (codec != NULL); env = gst_amc_jni_get_env (); - (*env)->DeleteGlobalRef (env, codec->object); + gst_amc_jni_object_unref (env, codec->object); g_slice_free (GstAmcCodec, codec); } @@ -175,25 +169,13 @@ gst_amc_codec_configure (GstAmcCodec * codec, GstAmcFormat * format, gint flags, GError ** err) { JNIEnv *env; - gboolean ret = TRUE; g_return_val_if_fail (codec != NULL, FALSE); g_return_val_if_fail (format != NULL, FALSE); env = gst_amc_jni_get_env (); - - (*env)->CallVoidMethod (env, codec->object, media_codec.configure, - format->object, NULL, NULL, flags); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_SETTINGS, - err, "Failed to configure codec"); - ret = FALSE; - goto done; - } - -done: - - return ret; + return gst_amc_jni_call_void_method (env, err, codec->object, + media_codec.configure, format->object, NULL, NULL, flags); } GstAmcFormat * @@ -207,27 +189,20 @@ gst_amc_codec_get_output_format (GstAmcCodec * codec, GError ** err) env = gst_amc_jni_get_env (); - object = - (*env)->CallObjectMethod (env, codec->object, - media_codec.get_output_format); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_SETTINGS, - err, "Failed to get output format"); + if (!gst_amc_jni_call_object_method (env, err, codec->object, + media_codec.get_output_format, &object)) goto done; - } ret = g_slice_new0 (GstAmcFormat); - ret->object = (*env)->NewGlobalRef (env, object); + ret->object = gst_amc_jni_object_make_global (env, object); if (!ret->object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_SETTINGS, - err, "Failed to create global format reference"); + gst_amc_jni_set_error (env, err, GST_LIBRARY_ERROR, + GST_LIBRARY_ERROR_SETTINGS, "Failed to create global format reference"); g_slice_free (GstAmcFormat, ret); ret = NULL; } - (*env)->DeleteLocalRef (env, object); - done: return ret; @@ -237,109 +212,48 @@ gboolean gst_amc_codec_start (GstAmcCodec * codec, GError ** err) { JNIEnv *env; - gboolean ret = TRUE; g_return_val_if_fail (codec != NULL, FALSE); env = gst_amc_jni_get_env (); - - (*env)->CallVoidMethod (env, codec->object, media_codec.start); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to start codec"); - ret = FALSE; - goto done; - } - -done: - - return ret; + return gst_amc_jni_call_void_method (env, err, codec->object, + media_codec.start); } gboolean gst_amc_codec_stop (GstAmcCodec * codec, GError ** err) { JNIEnv *env; - gboolean ret = TRUE; g_return_val_if_fail (codec != NULL, FALSE); env = gst_amc_jni_get_env (); - - (*env)->CallVoidMethod (env, codec->object, media_codec.stop); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to stop codec"); - ret = FALSE; - goto done; - } - -done: - - return ret; + return gst_amc_jni_call_void_method (env, err, codec->object, + media_codec.stop); } gboolean gst_amc_codec_flush (GstAmcCodec * codec, GError ** err) { JNIEnv *env; - gboolean ret = TRUE; g_return_val_if_fail (codec != NULL, FALSE); env = gst_amc_jni_get_env (); - - (*env)->CallVoidMethod (env, codec->object, media_codec.flush); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to flush codec"); - ret = FALSE; - goto done; - } - -done: - - return ret; + return gst_amc_jni_call_void_method (env, err, codec->object, + media_codec.flush); } gboolean gst_amc_codec_release (GstAmcCodec * codec, GError ** err) { JNIEnv *env; - gboolean ret = TRUE; g_return_val_if_fail (codec != NULL, FALSE); env = gst_amc_jni_get_env (); - - (*env)->CallVoidMethod (env, codec->object, media_codec.release); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to release codec"); - ret = FALSE; - goto done; - } - -done: - - return ret; -} - -void -gst_amc_codec_free_buffers (GstAmcBuffer * buffers, gsize n_buffers) -{ - JNIEnv *env; - jsize i; - - g_return_if_fail (buffers != NULL); - - env = gst_amc_jni_get_env (); - - for (i = 0; i < n_buffers; i++) { - if (buffers[i].object) - (*env)->DeleteGlobalRef (env, buffers[i].object); - } - g_free (buffers); + return gst_amc_jni_call_void_method (env, err, codec->object, + media_codec.release); } GstAmcBuffer * @@ -348,9 +262,7 @@ gst_amc_codec_get_output_buffers (GstAmcCodec * codec, gsize * n_buffers, { JNIEnv *env; jobject output_buffers = NULL; - jsize n_output_buffers; GstAmcBuffer *ret = NULL; - jsize i; g_return_val_if_fail (codec != NULL, NULL); g_return_val_if_fail (n_buffers != NULL, NULL); @@ -358,64 +270,17 @@ gst_amc_codec_get_output_buffers (GstAmcCodec * codec, gsize * n_buffers, *n_buffers = 0; env = gst_amc_jni_get_env (); - output_buffers = - (*env)->CallObjectMethod (env, codec->object, - media_codec.get_output_buffers); - if ((*env)->ExceptionCheck (env) || !output_buffers) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get output buffers"); - goto done; - } - - n_output_buffers = (*env)->GetArrayLength (env, output_buffers); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get output buffers array length"); + if (!gst_amc_jni_call_object_method (env, err, codec->object, + media_codec.get_output_buffers, &output_buffers)) goto done; - } - - *n_buffers = n_output_buffers; - ret = g_new0 (GstAmcBuffer, n_output_buffers); - - for (i = 0; i < n_output_buffers; i++) { - jobject buffer = NULL; - buffer = (*env)->GetObjectArrayElement (env, output_buffers, i); - if ((*env)->ExceptionCheck (env) || !buffer) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get output buffer %d", i); - goto error; - } - - ret[i].object = (*env)->NewGlobalRef (env, buffer); - (*env)->DeleteLocalRef (env, buffer); - if (!ret[i].object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create global output buffer reference %d", i); - goto error; - } - - ret[i].data = (*env)->GetDirectBufferAddress (env, ret[i].object); - if (!ret[i].data) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get output buffer address %d", i); - goto error; - } - ret[i].size = (*env)->GetDirectBufferCapacity (env, ret[i].object); - } + gst_amc_jni_get_buffer_array (env, err, output_buffers, &ret, n_buffers); done: if (output_buffers) - (*env)->DeleteLocalRef (env, output_buffers); - output_buffers = NULL; + gst_amc_jni_object_local_unref (env, output_buffers); return ret; -error: - if (ret) - gst_amc_codec_free_buffers (ret, n_output_buffers); - ret = NULL; - *n_buffers = 0; - goto done; } GstAmcBuffer * @@ -424,9 +289,7 @@ gst_amc_codec_get_input_buffers (GstAmcCodec * codec, gsize * n_buffers, { JNIEnv *env; jobject input_buffers = NULL; - jsize n_input_buffers; GstAmcBuffer *ret = NULL; - jsize i; g_return_val_if_fail (codec != NULL, NULL); g_return_val_if_fail (n_buffers != NULL, NULL); @@ -434,64 +297,26 @@ gst_amc_codec_get_input_buffers (GstAmcCodec * codec, gsize * n_buffers, *n_buffers = 0; env = gst_amc_jni_get_env (); - input_buffers = - (*env)->CallObjectMethod (env, codec->object, - media_codec.get_input_buffers); - if ((*env)->ExceptionCheck (env) || !input_buffers) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get input buffers"); + if (!gst_amc_jni_call_object_method (env, err, codec->object, + media_codec.get_input_buffers, &input_buffers)) goto done; - } - n_input_buffers = (*env)->GetArrayLength (env, input_buffers); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get input buffers array length"); - goto done; - } - - *n_buffers = n_input_buffers; - ret = g_new0 (GstAmcBuffer, n_input_buffers); - - for (i = 0; i < n_input_buffers; i++) { - jobject buffer = NULL; - - buffer = (*env)->GetObjectArrayElement (env, input_buffers, i); - if ((*env)->ExceptionCheck (env) || !buffer) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get input buffer %d", i); - goto error; - } - - ret[i].object = (*env)->NewGlobalRef (env, buffer); - (*env)->DeleteLocalRef (env, buffer); - if (!ret[i].object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create global input buffer reference %d", i); - goto error; - } - - ret[i].data = (*env)->GetDirectBufferAddress (env, ret[i].object); - if (!ret[i].data) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get input buffer address %d", i); - goto error; - } - ret[i].size = (*env)->GetDirectBufferCapacity (env, ret[i].object); - } + gst_amc_jni_get_buffer_array (env, err, input_buffers, &ret, n_buffers); done: if (input_buffers) - (*env)->DeleteLocalRef (env, input_buffers); - input_buffers = NULL; + gst_amc_jni_object_local_unref (env, input_buffers); return ret; -error: - if (ret) - gst_amc_codec_free_buffers (ret, n_input_buffers); - ret = NULL; - *n_buffers = 0; - goto done; +} + +void +gst_amc_codec_free_buffers (GstAmcBuffer * buffers, gsize n_buffers) +{ + JNIEnv *env; + + env = gst_amc_jni_get_env (); + gst_amc_jni_free_buffer_array (env, buffers, n_buffers); } gint @@ -504,18 +329,9 @@ gst_amc_codec_dequeue_input_buffer (GstAmcCodec * codec, gint64 timeoutUs, g_return_val_if_fail (codec != NULL, G_MININT); env = gst_amc_jni_get_env (); - - ret = - (*env)->CallIntMethod (env, codec->object, - media_codec.dequeue_input_buffer, timeoutUs); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to dequeue input buffer"); - ret = G_MININT; - goto done; - } - -done: + if (!gst_amc_jni_call_int_method (env, err, codec->object, + media_codec.dequeue_input_buffer, &ret, timeoutUs)) + return G_MININT; return ret; } @@ -526,39 +342,22 @@ gst_amc_codec_fill_buffer_info (JNIEnv * env, jobject buffer_info, { g_return_val_if_fail (buffer_info != NULL, FALSE); - info->flags = - (*env)->GetIntField (env, buffer_info, media_codec_buffer_info.flags); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get buffer info flags"); + if (!gst_amc_jni_get_int_field (env, err, buffer_info, + media_codec_buffer_info.flags, &info->flags)) return FALSE; - } - info->offset = - (*env)->GetIntField (env, buffer_info, media_codec_buffer_info.offset); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get buffer info offset"); + if (!gst_amc_jni_get_int_field (env, err, buffer_info, + media_codec_buffer_info.offset, &info->offset)) return FALSE; - } - info->presentation_time_us = - (*env)->GetLongField (env, buffer_info, - media_codec_buffer_info.presentation_time_us); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get buffer info pts"); + if (!gst_amc_jni_get_long_field (env, err, buffer_info, + media_codec_buffer_info.presentation_time_us, + &info->presentation_time_us)) return FALSE; - } - info->size = - (*env)->GetIntField (env, buffer_info, media_codec_buffer_info.size); - if ((*env)->ExceptionCheck (env)) { - (*env)->ExceptionClear (env); - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get buffer info size"); + if (!gst_amc_jni_get_int_field (env, err, buffer_info, + media_codec_buffer_info.size, &info->size)) return FALSE; - } return TRUE; } @@ -576,20 +375,13 @@ gst_amc_codec_dequeue_output_buffer (GstAmcCodec * codec, env = gst_amc_jni_get_env (); info_o = - (*env)->NewObject (env, media_codec_buffer_info.klass, + gst_amc_jni_new_object (env, err, FALSE, media_codec_buffer_info.klass, media_codec_buffer_info.constructor); - if (!info_o) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create buffer info instance"); + if (!info_o) goto done; - } - ret = - (*env)->CallIntMethod (env, codec->object, - media_codec.dequeue_output_buffer, info_o, timeoutUs); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to dequeue output buffer"); + if (!gst_amc_jni_call_int_method (env, err, codec->object, + media_codec.dequeue_output_buffer, &ret, info_o, timeoutUs)) { ret = G_MININT; goto done; } @@ -601,7 +393,7 @@ gst_amc_codec_dequeue_output_buffer (GstAmcCodec * codec, done: if (info_o) - (*env)->DeleteLocalRef (env, info_o); + gst_amc_jni_object_local_unref (env, info_o); info_o = NULL; return ret; @@ -612,25 +404,14 @@ gst_amc_codec_queue_input_buffer (GstAmcCodec * codec, gint index, const GstAmcBufferInfo * info, GError ** err) { JNIEnv *env; - gboolean ret = TRUE; g_return_val_if_fail (codec != NULL, FALSE); g_return_val_if_fail (info != NULL, FALSE); env = gst_amc_jni_get_env (); - - (*env)->CallVoidMethod (env, codec->object, media_codec.queue_input_buffer, - index, info->offset, info->size, info->presentation_time_us, info->flags); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to queue input buffer"); - ret = FALSE; - goto done; - } - -done: - - return ret; + return gst_amc_jni_call_void_method (env, err, codec->object, + media_codec.queue_input_buffer, index, info->offset, info->size, + info->presentation_time_us, info->flags); } gboolean @@ -638,24 +419,12 @@ gst_amc_codec_release_output_buffer (GstAmcCodec * codec, gint index, GError ** err) { JNIEnv *env; - gboolean ret = TRUE; g_return_val_if_fail (codec != NULL, FALSE); env = gst_amc_jni_get_env (); - - (*env)->CallVoidMethod (env, codec->object, media_codec.release_output_buffer, - index, JNI_FALSE); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to release output buffer"); - ret = FALSE; - goto done; - } - -done: - - return ret; + return gst_amc_jni_call_void_method (env, err, codec->object, + media_codec.release_output_buffer, index, JNI_FALSE); } GstAmcFormat * @@ -665,42 +434,25 @@ gst_amc_format_new_audio (const gchar * mime, gint sample_rate, gint channels, JNIEnv *env; GstAmcFormat *format = NULL; jstring mime_str; - jobject object = NULL; g_return_val_if_fail (mime != NULL, NULL); env = gst_amc_jni_get_env (); - mime_str = (*env)->NewStringUTF (env, mime); - if (mime_str == NULL) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create Java string"); + mime_str = gst_amc_jni_string_from_gchar (env, err, FALSE, mime); + if (!mime_str) goto error; - } format = g_slice_new0 (GstAmcFormat); - - object = - (*env)->CallStaticObjectMethod (env, media_format.klass, + format->object = + gst_amc_jni_new_object_from_static (env, err, TRUE, media_format.klass, media_format.create_audio_format, mime_str, sample_rate, channels); - if ((*env)->ExceptionCheck (env) || !object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create format instance '%s'", mime); + if (!format->object) goto error; - } - - format->object = (*env)->NewGlobalRef (env, object); - if (!format->object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create global format reference"); - goto error; - } done: - if (object) - (*env)->DeleteLocalRef (env, object); if (mime_str) - (*env)->DeleteLocalRef (env, mime_str); + gst_amc_jni_object_local_unref (env, mime_str); mime_str = NULL; return format; @@ -719,42 +471,25 @@ gst_amc_format_new_video (const gchar * mime, gint width, gint height, JNIEnv *env; GstAmcFormat *format = NULL; jstring mime_str; - jobject object = NULL; g_return_val_if_fail (mime != NULL, NULL); env = gst_amc_jni_get_env (); - mime_str = (*env)->NewStringUTF (env, mime); - if (mime_str == NULL) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create Java string"); + mime_str = gst_amc_jni_string_from_gchar (env, err, FALSE, mime); + if (!mime_str) goto error; - } format = g_slice_new0 (GstAmcFormat); - - object = - (*env)->CallStaticObjectMethod (env, media_format.klass, + format->object = + gst_amc_jni_new_object_from_static (env, err, TRUE, media_format.klass, media_format.create_video_format, mime_str, width, height); - if ((*env)->ExceptionCheck (env) || !object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create format instance '%s'", mime); - goto error; - } - - format->object = (*env)->NewGlobalRef (env, object); - if (!format->object) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_INIT, err, - "Failed to create global format reference"); + if (!format->object) goto error; - } done: - if (object) - (*env)->DeleteLocalRef (env, object); if (mime_str) - (*env)->DeleteLocalRef (env, mime_str); + gst_amc_jni_object_local_unref (env, mime_str); mime_str = NULL; return format; @@ -774,7 +509,7 @@ gst_amc_format_free (GstAmcFormat * format) g_return_if_fail (format != NULL); env = gst_amc_jni_get_env (); - (*env)->DeleteGlobalRef (env, format->object); + gst_amc_jni_object_unref (env, format->object); g_slice_free (GstAmcFormat, format); } @@ -783,35 +518,18 @@ gst_amc_format_to_string (GstAmcFormat * format, GError ** err) { JNIEnv *env; jstring v_str = NULL; - const gchar *v = NULL; gchar *ret = NULL; g_return_val_if_fail (format != NULL, FALSE); env = gst_amc_jni_get_env (); - v_str = - (*env)->CallObjectMethod (env, format->object, media_format.to_string); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to convert format to string"); + if (!gst_amc_jni_call_object_method (env, err, format->object, + media_format.to_string, &v_str)) goto done; - } - - v = (*env)->GetStringUTFChars (env, v_str, NULL); - if (!v) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to get UTF8 string"); - goto done; - } - - ret = g_strdup (v); + ret = gst_amc_jni_string_to_gchar (env, v_str, TRUE); done: - if (v) - (*env)->ReleaseStringUTFChars (env, v_str, v); - if (v_str) - (*env)->DeleteLocalRef (env, v_str); return ret; } @@ -829,25 +547,17 @@ gst_amc_format_contains_key (GstAmcFormat * format, const gchar * key, env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - ret = - (*env)->CallBooleanMethod (env, format->object, media_format.contains_key, - key_str); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to check if format contains key '%s'", key); + if (!gst_amc_jni_call_boolean_method (env, err, format->object, + media_format.contains_key, &ret, key_str)) goto done; - } done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); return ret; } @@ -867,60 +577,50 @@ gst_amc_format_get_float (GstAmcFormat * format, const gchar * key, *value = 0; env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - *value = - (*env)->CallFloatMethod (env, format->object, media_format.get_float, - key_str); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed get float key '%s'", key); + if (!gst_amc_jni_call_float_method (env, err, format->object, + media_format.get_float, value, key_str)) goto done; - } ret = TRUE; done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); return ret; } -void +gboolean gst_amc_format_set_float (GstAmcFormat * format, const gchar * key, gfloat value, GError ** err) { JNIEnv *env; jstring key_str = NULL; + gboolean ret = FALSE; - g_return_if_fail (format != NULL); - g_return_if_fail (key != NULL); + g_return_val_if_fail (format != NULL, FALSE); + g_return_val_if_fail (key != NULL, FALSE); env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - (*env)->CallVoidMethod (env, format->object, media_format.set_float, key_str, - value); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed set float key '%s'", key); + if (!gst_amc_jni_call_void_method (env, err, format->object, + media_format.set_float, key_str, value)) goto done; - } + + ret = TRUE; done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); + + return ret; } gboolean @@ -938,61 +638,51 @@ gst_amc_format_get_int (GstAmcFormat * format, const gchar * key, gint * value, *value = 0; env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - *value = - (*env)->CallIntMethod (env, format->object, media_format.get_integer, - key_str); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed get integer key '%s'", key); + if (!gst_amc_jni_call_int_method (env, err, format->object, + media_format.get_integer, value, key_str)) goto done; - } ret = TRUE; done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); return ret; } -void +gboolean gst_amc_format_set_int (GstAmcFormat * format, const gchar * key, gint value, GError ** err) { JNIEnv *env; jstring key_str = NULL; + gboolean ret = FALSE; - g_return_if_fail (format != NULL); - g_return_if_fail (key != NULL); + g_return_val_if_fail (format != NULL, FALSE); + g_return_val_if_fail (key != NULL, FALSE); env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - (*env)->CallVoidMethod (env, format->object, media_format.set_integer, - key_str, value); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed set integer key '%s'", key); + if (!gst_amc_jni_call_void_method (env, err, format->object, + media_format.set_integer, key_str, value)) goto done; - } + + ret = TRUE; done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); + + return ret; } gboolean @@ -1003,7 +693,6 @@ gst_amc_format_get_string (GstAmcFormat * format, const gchar * key, gboolean ret = FALSE; jstring key_str = NULL; jstring v_str = NULL; - const gchar *v = NULL; g_return_val_if_fail (format != NULL, FALSE); g_return_val_if_fail (key != NULL, FALSE); @@ -1012,85 +701,61 @@ gst_amc_format_get_string (GstAmcFormat * format, const gchar * key, *value = 0; env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - v_str = - (*env)->CallObjectMethod (env, format->object, media_format.get_string, - key_str); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed get string key '%s'", key); + if (!gst_amc_jni_call_object_method (env, err, format->object, + media_format.get_string, &v_str, key_str)) goto done; - } - v = (*env)->GetStringUTFChars (env, v_str, NULL); - if (!v) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed get string UTF8 characters"); - goto done; - } - - *value = g_strdup (v); + *value = gst_amc_jni_string_to_gchar (env, v_str, TRUE); ret = TRUE; done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); - if (v) - (*env)->ReleaseStringUTFChars (env, v_str, v); - if (v_str) - (*env)->DeleteLocalRef (env, v_str); + gst_amc_jni_object_local_unref (env, key_str); return ret; } -void +gboolean gst_amc_format_set_string (GstAmcFormat * format, const gchar * key, const gchar * value, GError ** err) { JNIEnv *env; jstring key_str = NULL; jstring v_str = NULL; + gboolean ret = FALSE; - g_return_if_fail (format != NULL); - g_return_if_fail (key != NULL); - g_return_if_fail (value != NULL); + g_return_val_if_fail (format != NULL, FALSE); + g_return_val_if_fail (key != NULL, FALSE); + g_return_val_if_fail (value != NULL, FALSE); env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - v_str = (*env)->NewStringUTF (env, value); - if (!v_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + v_str = gst_amc_jni_string_from_gchar (env, err, FALSE, value); + if (!v_str) goto done; - } - (*env)->CallVoidMethod (env, format->object, media_format.set_string, key_str, - v_str); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed set string key '%s'", key); + if (!gst_amc_jni_call_void_method (env, err, format->object, + media_format.set_string, key_str, v_str)) goto done; - } + + ret = TRUE; done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); if (v_str) - (*env)->DeleteLocalRef (env, v_str); + gst_amc_jni_object_local_unref (env, v_str); + + return ret; } gboolean @@ -1111,25 +776,18 @@ gst_amc_format_get_buffer (GstAmcFormat * format, const gchar * key, *size = 0; env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } - v = (*env)->CallObjectMethod (env, format->object, - media_format.get_byte_buffer, key_str); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed get buffer key '%s'", key); + if (!gst_amc_jni_call_object_method (env, err, format->object, + media_format.get_byte_buffer, &v, key_str)) goto done; - } *data = (*env)->GetDirectBufferAddress (env, v); if (!data) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed get buffer address"); + gst_amc_jni_set_error (env, err, GST_LIBRARY_ERROR, + GST_LIBRARY_ERROR_FAILED, "Failed get buffer address"); goto done; } *size = (*env)->GetDirectBufferCapacity (env, v); @@ -1139,55 +797,53 @@ gst_amc_format_get_buffer (GstAmcFormat * format, const gchar * key, done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); if (v) - (*env)->DeleteLocalRef (env, v); + gst_amc_jni_object_local_unref (env, v); return ret; } -void +gboolean gst_amc_format_set_buffer (GstAmcFormat * format, const gchar * key, guint8 * data, gsize size, GError ** err) { JNIEnv *env; jstring key_str = NULL; jobject v = NULL; + gboolean ret = FALSE; - g_return_if_fail (format != NULL); - g_return_if_fail (key != NULL); - g_return_if_fail (data != NULL); + g_return_val_if_fail (format != NULL, FALSE); + g_return_val_if_fail (key != NULL, FALSE); + g_return_val_if_fail (data != NULL, FALSE); env = gst_amc_jni_get_env (); - key_str = (*env)->NewStringUTF (env, key); - if (!key_str) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed to create Java string"); + key_str = gst_amc_jni_string_from_gchar (env, err, FALSE, key); + if (!key_str) goto done; - } /* FIXME: The memory must remain valid until the codec is stopped */ v = (*env)->NewDirectByteBuffer (env, data, size); if (!v) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed create Java byte buffer"); + gst_amc_jni_set_error (env, err, GST_LIBRARY_ERROR, + GST_LIBRARY_ERROR_FAILED, "Failed create Java byte buffer"); goto done; } - (*env)->CallVoidMethod (env, format->object, media_format.set_byte_buffer, - key_str, v); - if ((*env)->ExceptionCheck (env)) { - gst_amc_jni_set_error (env, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, - err, "Failed set buffer key '%s'", key); + if (!gst_amc_jni_call_void_method (env, err, format->object, + media_format.set_byte_buffer, key_str, v)) goto done; - } + + ret = TRUE; done: if (key_str) - (*env)->DeleteLocalRef (env, key_str); + gst_amc_jni_object_local_unref (env, key_str); if (v) - (*env)->DeleteLocalRef (env, v); + gst_amc_jni_object_local_unref (env, v); + + return ret; } static gboolean diff --git a/sys/androidmedia/gstamc.h b/sys/androidmedia/gstamc.h index 7fcae1e..e216039 100644 --- a/sys/androidmedia/gstamc.h +++ b/sys/androidmedia/gstamc.h @@ -26,6 +26,8 @@ #include #include +#include "gstjniutils.h" + G_BEGIN_DECLS typedef struct _GstAmcCodecInfo GstAmcCodecInfo; @@ -33,7 +35,6 @@ typedef struct _GstAmcCodecType GstAmcCodecType; typedef struct _GstAmcCodec GstAmcCodec; typedef struct _GstAmcBufferInfo GstAmcBufferInfo; typedef struct _GstAmcFormat GstAmcFormat; -typedef struct _GstAmcBuffer GstAmcBuffer; typedef struct _GstAmcColorFormatInfo GstAmcColorFormatInfo; struct _GstAmcCodecType { @@ -56,12 +57,6 @@ struct _GstAmcCodecInfo { gint n_supported_types; }; -struct _GstAmcBuffer { - jobject object; /* global reference */ - guint8 *data; - gsize size; -}; - struct _GstAmcFormat { /* < private > */ jobject object; /* global reference */ @@ -112,13 +107,13 @@ gchar * gst_amc_format_to_string (GstAmcFormat * format, GError **err); gboolean gst_amc_format_contains_key (GstAmcFormat *format, const gchar *key, GError **err); gboolean gst_amc_format_get_float (GstAmcFormat *format, const gchar *key, gfloat *value, GError **err); -void gst_amc_format_set_float (GstAmcFormat *format, const gchar *key, gfloat value, GError **err); +gboolean gst_amc_format_set_float (GstAmcFormat *format, const gchar *key, gfloat value, GError **err); gboolean gst_amc_format_get_int (GstAmcFormat *format, const gchar *key, gint *value, GError **err); -void gst_amc_format_set_int (GstAmcFormat *format, const gchar *key, gint value, GError **err); +gboolean gst_amc_format_set_int (GstAmcFormat *format, const gchar *key, gint value, GError **err); gboolean gst_amc_format_get_string (GstAmcFormat *format, const gchar *key, gchar **value, GError **err); -void gst_amc_format_set_string (GstAmcFormat *format, const gchar *key, const gchar *value, GError **err); +gboolean gst_amc_format_set_string (GstAmcFormat *format, const gchar *key, const gchar *value, GError **err); gboolean gst_amc_format_get_buffer (GstAmcFormat *format, const gchar *key, guint8 **data, gsize *size, GError **err); -void gst_amc_format_set_buffer (GstAmcFormat *format, const gchar *key, guint8 *data, gsize size, GError **err); +gboolean gst_amc_format_set_buffer (GstAmcFormat *format, const gchar *key, guint8 *data, gsize size, GError **err); GstVideoFormat gst_amc_color_format_to_video_format (const GstAmcCodecInfo * codec_info, const gchar * mime, gint color_format); gint gst_amc_video_format_to_color_format (const GstAmcCodecInfo * codec_info, const gchar * mime, GstVideoFormat video_format); -- 2.7.4