ffjni.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * JNI utility functions
  3. *
  4. * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_FFJNI_H
  23. #define AVCODEC_FFJNI_H
  24. #include <jni.h>
  25. /*
  26. * Attach permanently a JNI environment to the current thread and retrieve it.
  27. *
  28. * If successfully attached, the JNI environment will automatically be detached
  29. * at thread destruction.
  30. *
  31. * @param attached pointer to an integer that will be set to 1 if the
  32. * environment has been attached to the current thread or 0 if it is
  33. * already attached.
  34. * @param log_ctx context used for logging, can be NULL
  35. * @return the JNI environment on success, NULL otherwise
  36. */
  37. JNIEnv *ff_jni_get_env(void *log_ctx);
  38. /*
  39. * Convert a jstring to its utf characters equivalent.
  40. *
  41. * @param env JNI environment
  42. * @param string Java string to convert
  43. * @param log_ctx context used for logging, can be NULL
  44. * @return a pointer to an array of unicode characters on success, NULL
  45. * otherwise
  46. */
  47. char *ff_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, void *log_ctx);
  48. /*
  49. * Convert utf chars to its jstring equivalent.
  50. *
  51. * @param env JNI environment
  52. * @param utf_chars a pointer to an array of unicode characters
  53. * @param log_ctx context used for logging, can be NULL
  54. * @return a Java string object on success, NULL otherwise
  55. */
  56. jstring ff_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, void *log_ctx);
  57. /*
  58. * Extract the error summary from a jthrowable in the form of "className: errorMessage"
  59. *
  60. * @param env JNI environment
  61. * @param exception exception to get the summary from
  62. * @param error address pointing to the error, the value is updated if a
  63. * summary can be extracted
  64. * @param log_ctx context used for logging, can be NULL
  65. * @return 0 on success, < 0 otherwise
  66. */
  67. int ff_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error, void *log_ctx);
  68. /*
  69. * Check if an exception has occurred,log it using av_log and clear it.
  70. *
  71. * @param env JNI environment
  72. * @param log value used to enable logging if an exception has occurred,
  73. * 0 disables logging, != 0 enables logging
  74. * @param log_ctx context used for logging, can be NULL
  75. */
  76. int ff_jni_exception_check(JNIEnv *env, int log, void *log_ctx);
  77. /*
  78. * Jni field type.
  79. */
  80. enum FFJniFieldType {
  81. FF_JNI_CLASS,
  82. FF_JNI_FIELD,
  83. FF_JNI_STATIC_FIELD,
  84. FF_JNI_METHOD,
  85. FF_JNI_STATIC_METHOD
  86. };
  87. /*
  88. * Jni field describing a class, a field or a method to be retrieved using
  89. * the ff_jni_init_jfields method.
  90. */
  91. struct FFJniField {
  92. const char *name;
  93. const char *method;
  94. const char *signature;
  95. enum FFJniFieldType type;
  96. int offset;
  97. int mandatory;
  98. };
  99. /*
  100. * Retrieve class references, field ids and method ids to an arbitrary structure.
  101. *
  102. * @param env JNI environment
  103. * @param jfields a pointer to an arbitrary structure where the different
  104. * fields are declared and where the FFJNIField mapping table offsets are
  105. * pointing to
  106. * @param jfields_mapping null terminated array of FFJNIFields describing
  107. * the class/field/method to be retrieved
  108. * @param global make the classes references global. It is the caller
  109. * responsibility to properly release global references.
  110. * @param log_ctx context used for logging, can be NULL
  111. * @return 0 on success, < 0 otherwise
  112. */
  113. int ff_jni_init_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx);
  114. /*
  115. * Delete class references, field ids and method ids of an arbitrary structure.
  116. *
  117. * @param env JNI environment
  118. * @param jfields a pointer to an arbitrary structure where the different
  119. * fields are declared and where the FFJNIField mapping table offsets are
  120. * pointing to
  121. * @param jfields_mapping null terminated array of FFJNIFields describing
  122. * the class/field/method to be deleted
  123. * @param global threat the classes references as global and delete them
  124. * accordingly
  125. * @param log_ctx context used for logging, can be NULL
  126. * @return 0 on success, < 0 otherwise
  127. */
  128. int ff_jni_reset_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx);
  129. #endif /* AVCODEC_FFJNI_H */