jni.mustache 6.22 KB
Newer Older
Sylvain Berfini's avatar
Sylvain Berfini committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
linphone_jni.cc
Copyright (C) 2017 Belledonne Communications SARL

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <jni.h>
Sylvain Berfini's avatar
Sylvain Berfini committed
21 22 23 24
#ifdef USE_JAVAH
#include "linphonecore_jni.h"
#endif
#include "linphone/core_utils.h"
Sylvain Berfini's avatar
Sylvain Berfini committed
25 26
#include <cpu-features.h>

Sylvain Berfini's avatar
Sylvain Berfini committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "mediastreamer2/mediastream.h"
#include "mediastreamer2/mscommon.h"
#include "mediastreamer2/msmediaplayer.h"
#include "mediastreamer2/msutils.h"
#include "mediastreamer2/devices.h"
#include "mediastreamer2/msjava.h"
#include "linphone/core.h"
#include "linphone/tunnel.h"
#include "linphone/account_creator.h"
#include "linphone/wrapper_utils.h"
#include "linphone/lpconfig.h"

#ifdef __ANDROID__
#include <android/log.h>
#include <belle-sip/wakelock.h>
#endif /* __ANDROID__ */

44
static JavaVM *jvm = NULL;
Sylvain Berfini's avatar
Sylvain Berfini committed
45

Sylvain Berfini's avatar
Sylvain Berfini committed
46 47 48 49
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *ajvm, void *reserved) {
#ifdef __ANDROID__
	ms_set_jvm(ajvm);
#endif /* __ANDROID__ */
50
	jvm = ajvm;
Sylvain Berfini's avatar
Sylvain Berfini committed
51 52 53 54 55 56 57 58 59 60
	return JNI_VERSION_1_2;
}

static const char* GetStringUTFChars(JNIEnv* env, jstring string) {
	const char *cstring = string ? env->GetStringUTFChars(string, NULL) : NULL;
	return cstring;
}

static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char *cstring) {
	if (string) env->ReleaseStringUTFChars(string, cstring);
61 62 63 64 65 66 67 68 69
}

static jlong GetObjectNativePtr(JNIEnv *env, jobject object) {
	jclass objClass = env->GetObjectClass(object);
	jfieldID nativePtrId = env->GetFieldID(objClass, "nativePtr", "J");
	jlong nativePtr = env->GetLongField(object, nativePtrId);
	return nativePtr;
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
class LinphoneJavaBindings {
public:
	LinphoneJavaBindings(JNIEnv *env) {
		{{#objects}}
		{{cPrefix}}_class = (jclass)env->NewGlobalRef(env->FindClass("{{jniPath}}{{classImplName}}"));
		{{cPrefix}}_class_constructor = env->GetMethodID({{cPrefix}}_class, "<init>", "(J)V");
		{{/objects}}
	}

	~LinphoneJavaBindings() {
		JNIEnv *env = 0;
		jvm->AttachCurrentThread(&env,NULL);
		{{#objects}}
		env->DeleteGlobalRef({{cPrefix}}_class);
		{{/objects}}
	}

	{{#objects}}
	jclass {{cPrefix}}_class;
	jmethodID {{cPrefix}}_class_constructor;
	{{/objects}}
};

Sylvain Berfini's avatar
Sylvain Berfini committed
93 94 95 96 97 98
{{#objects}}
jobject get{{className}}(JNIEnv *env, {{classCName}} *ptr) {
	jobject jobj = 0;

	if (ptr != NULL) {
		void *up = {{cPrefix}}_get_user_data(ptr);
99 100 101
		//TODO get ljb
		jclass {{cPrefix}}_class = ljb->{{cPrefix}}_class;
		jmethodID {{cPrefix}}_constructor = ljb->{{cPrefix}}_class_constructor;
Sylvain Berfini's avatar
Sylvain Berfini committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

		if (up == NULL) {
			jobj = env->NewObject({{cPrefix}}_class, {{cPrefix}}_constructor, (jlong)ptr);
			{{cPrefix}}_set_user_data(ptr, (void*)env->NewWeakGlobalRef(jobj));
			{{cPrefix}}_ref(ptr);
		} else {
			jobj = env->NewLocalRef((jobject)up);
			if (jobj == NULL) {
				// Delete weak ref ?
				env->DeleteWeakGlobalRef((jobject)up);
				// takes implicit local ref
				jobj = env->NewObject({{cPrefix}}_class, {{cPrefix}}_constructor, (jlong)ptr);
				{{cPrefix}}_set_user_data(ptr, (void*)env->NewWeakGlobalRef(jobj));
				{{cPrefix}}_ref(ptr);
			}
		}
	}
	return jobj;
}

void Java_{{jniPrefix}}{{classImplName}}_unref(JNIEnv* env, jobject thiz, jlong ptr) {
	{{classCName}} *cptr = ({{classCName}}*)ptr;
	jobject wref = (jobject){{cPrefix}}_get_user_data(cptr);
	{{cPrefix}}_set_user_data(cptr, NULL);
	if (wref) {
		env->DeleteWeakGlobalRef(wref);
	}
	{{cPrefix}}_unref(cptr);
}

{{/objects}}

{{#methods}}
{{return}} {{name}}({{params}}) {
	{{#notStatic}}{{classCName}} *cptr = ({{classCName}}*)ptr;{{/notStatic}}
	{{#strings}}
	const char* c_{{string}} = GetStringUTFChars(env, {{string}});
139
	{{/strings}}{{#objects}}
Sylvain Berfini's avatar
Sylvain Berfini committed
140 141
	{{objectClassCName}}* c_{{object}} = NULL;
	if ({{object}}) c_{{object}} = ({{objectClassCName}}*)GetObjectNativePtr(env, {{object}});
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	{{/objects}}{{#lists}}
	bctbx_list_t *bctbx_list_{{list}} = NULL;
	int count = env->GetArrayLength({{list}});
	for (int i=0; i < count; i++) {
		{{#isStringList}}
		jstring obj = (jstring) env->GetObjectArrayElement({{list}}, i);
		const char *str = GetStringUTFChars(env, obj);
		if (str) {
			bctbx_list_{{list}} = bctbx_list_append(bctbx_list_{{list}}, ms_strdup(str));
			ReleaseStringUTFChars(env, obj, str);
		}
		{{/isStringList}}
		{{#isObjList}}
		jobject obj = env->GetObjectArrayElement({{list}}, i);
		bctbx_list_{{list}} = bctbx_list_append(bctbx_list_{{list}}, GetObjectNativePtr(env, obj));
		{{/isObjList}}
	}
	{{/lists}}{{#hasListReturn}}
	bctbx_list_t *list = {{c_name}}({{#notStatic}}cptr{{/notStatic}}{{params_impl}});
	size_t count = bctbx_list_size(list);
162 163 164
	{{#isRealObjectArray}}
	//TODO get ljb
	jobjectArray jni_list_result = env->NewObjectArray(count, ljb->{{cPrefix}}_class, NULL);{{/isRealObjectArray}}
165 166 167 168 169 170 171 172 173 174 175 176 177 178
	{{#isStringObjectArray}}jobjectArray jni_list_result = env->NewObjectArray(count, env->FindClass("java/lang/String"), env->NewStringUTF(""));{{/isStringObjectArray}}
	for (size_t i = 0; i < count; i++) {
		{{#isRealObjectArray}}
		{{objectClassCName}}* c_object = ({{objectClassCName}}*)list->data;
		jobject object = get{{objectClassName}}(c_object);
		{{/isRealObjectArray}}
		{{#isStringObjectArray}}jstring object = list->data ? env->NewStringUTF(list->data) : 0;{{/isStringObjectArray}}
		if (object != 0) {
			env->SetObjectArrayElement(jni_list_result, i, object);
			{{#isRealObjectArray}}env->DeleteLocalRef(object);{{/isRealObjectArray}}
		}
		list = bctbx_list_next(list);
	}
	{{/hasListReturn}}{{#hasNormalReturn}}
Sylvain Berfini's avatar
Sylvain Berfini committed
179
	{{#hasReturn}}{{return}} jni_result = {{#returnObject}}get{{returnClassName}}({{/returnObject}}{{/hasReturn}}{{c_name}}({{#notStatic}}cptr{{/notStatic}}{{params_impl}}){{#returnObject}}){{/returnObject}};
180
	{{/hasNormalReturn}}{{#strings}}
Sylvain Berfini's avatar
Sylvain Berfini committed
181
	ReleaseStringUTFChars(env, {{string}}, c_{{string}});
182
	{{/strings}}{{#hasReturn}}return jni_result;{{/hasReturn}}{{#hasListReturn}}return jni_list_result;{{/hasListReturn}}
Sylvain Berfini's avatar
Sylvain Berfini committed
183 184 185
}

{{/methods}}