linphonecore_jni.cc 222 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
linphonecore_jni.cc
Copyright (C) 2010  Belledonne Communications, Grenoble, France

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#include <jni.h>
20
#ifdef USE_JAVAH
21
#include "linphonecore_jni.h"
22
#endif
jehan's avatar
jehan committed
23
#include "linphonecore_utils.h"
24
#include <mediastreamer2/zrtp.h>
jehan's avatar
jehan committed
25

Guillaume Beraudo's avatar
Guillaume Beraudo committed
26

27 28
extern "C" {
#include "mediastreamer2/mediastream.h"
29
#include "mediastreamer2/mscommon.h"
François Grisez's avatar
François Grisez committed
30
#include "mediastreamer2/msmediaplayer.h"
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
31
#include "mediastreamer2/msutils.h"
32
}
Simon Morlat's avatar
Simon Morlat committed
33
#include "mediastreamer2/msjava.h"
34
#include "private.h"
35 36
#include <cpu-features.h>

37 38
#include "lpconfig.h"

jehan's avatar
jehan committed
39 40
#ifdef ANDROID
#include <android/log.h>
Sylvain Berfini's avatar
Sylvain Berfini committed
41
extern "C" void libmsilbc_init();
42
#ifdef HAVE_X264
Simon Morlat's avatar
Simon Morlat committed
43
extern "C" void libmsx264_init();
44
#endif
45 46 47
#ifdef HAVE_OPENH264
extern "C" void libmsopenh264_init();
#endif
Simon Morlat's avatar
Simon Morlat committed
48 49 50
#ifdef HAVE_AMR
extern "C" void libmsamr_init();
#endif
51 52 53
#ifdef HAVE_SILK
extern "C" void libmssilk_init();
#endif
jehan's avatar
jehan committed
54 55 56
#ifdef HAVE_G729
extern "C" void libmsbcg729_init();
#endif
57 58
#ifdef HAVE_WEBRTC
extern "C" void libmswebrtc_init();
59
#endif
60
#include <belle-sip/wakelock.h>
jehan's avatar
jehan committed
61 62
#endif /*ANDROID*/

63 64
/*force linking of ms_audio_diff symbol because the tester requires it.*/
static void *audiodiff=(void*)&ms_audio_diff;
Ghislain MARY's avatar
Ghislain MARY committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

#define RETURN_USER_DATA_OBJECT(javaclass, funcprefix, cobj) \
	{ \
		jclass jUserDataObjectClass; \
		jmethodID jUserDataObjectCtor; \
		jobject jUserDataObj; \
		jUserDataObj = (jobject)funcprefix ## _get_user_data(cobj); \
		if (jUserDataObj == NULL) { \
			jUserDataObjectClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/" javaclass)); \
			jUserDataObjectCtor = env->GetMethodID(jUserDataObjectClass,"<init>", "(J)V"); \
			jUserDataObj = env->NewObject(jUserDataObjectClass, jUserDataObjectCtor, (jlong)funcprefix ## _ref(cobj)); \
			jUserDataObj = env->NewGlobalRef(jUserDataObj); \
			funcprefix ## _set_user_data(cobj, jUserDataObj); \
			env->DeleteGlobalRef(jUserDataObjectClass); \
		} \
		return jUserDataObj; \
	}

jehan's avatar
jehan committed
83
static JavaVM *jvm=0;
84
static const char* LogDomain = "Linphone";
85 86 87
static jclass handler_class;
static jmethodID loghandler_id;
static jobject handler_obj=NULL;
jehan's avatar
jehan committed
88

89 90
static jobject create_java_linphone_content(JNIEnv *env, const LinphoneContent *content);

jehan's avatar
jehan committed
91
#ifdef ANDROID
92
void linphone_android_log_handler(int prio, char *str) {
93 94
	char *current;
	char *next;
95 96

	if (strlen(str) < 512) {
97
		__android_log_write(prio, LogDomain, str);
98 99 100 101
	} else {
		current = str;
		while ((next = strchr(current, '\n')) != NULL) {
			*next = '\0';
102
			__android_log_write(prio, LogDomain, current);
103 104
			current = next + 1;
		}
105
		__android_log_write(prio, LogDomain, current);
106
	}
jehan's avatar
jehan committed
107
}
108

109
static void linphone_android_ortp_log_handler(OrtpLogLevel lev, const char *fmt, va_list args) {
110 111 112 113
	char str[4096];
	const char *levname="undef";
	vsnprintf(str, sizeof(str) - 1, fmt, args);
	str[sizeof(str) - 1] = '\0';
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
114

115 116
	int prio;
	switch(lev){
117 118 119 120 121 122
	case ORTP_DEBUG:	prio = ANDROID_LOG_DEBUG;	levname="debug"; break;
	case ORTP_MESSAGE:	prio = ANDROID_LOG_INFO;	levname="message"; break;
	case ORTP_WARNING:	prio = ANDROID_LOG_WARN;	levname="warning"; break;
	case ORTP_ERROR:	prio = ANDROID_LOG_ERROR;	levname="error"; break;
	case ORTP_FATAL:	prio = ANDROID_LOG_FATAL;	levname="fatal"; break;
	default:			prio = ANDROID_LOG_DEFAULT;	break;
123
	}
124 125
	if (handler_obj){
		JNIEnv *env=ms_get_jni_env();
126 127 128 129 130 131 132
		jstring jdomain=env->NewStringUTF(LogDomain);
		jstring jlevname=env->NewStringUTF(levname);
		jstring jstr=env->NewStringUTF(str);
		env->CallVoidMethod(handler_obj,loghandler_id,jdomain,(jint)lev,jlevname,jstr,NULL);
		if (jdomain) env->DeleteLocalRef(jdomain);
		if (jlevname) env->DeleteLocalRef(jlevname);
		if (jstr) env->DeleteLocalRef(jstr);
133 134
	}else
		linphone_android_log_handler(prio, str);
135 136
}

137 138 139
int dumbMethodForAllowingUsageOfCpuFeaturesFromStaticLibMediastream() {
	return (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM && (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0);
}
jehan's avatar
jehan committed
140 141 142 143
#endif /*ANDROID*/

JNIEXPORT jint JNICALL  JNI_OnLoad(JavaVM *ajvm, void *reserved)
{
jehan's avatar
jehan committed
144
#ifdef ANDROID
Simon Morlat's avatar
Simon Morlat committed
145
	ms_set_jvm(ajvm);
jehan's avatar
jehan committed
146 147 148 149 150 151
#endif /*ANDROID*/
	jvm=ajvm;
	return JNI_VERSION_1_2;
}


jehan's avatar
jehan committed
152
//LinphoneFactory
jehan's avatar
jehan committed
153
extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_setDebugMode(JNIEnv*  env
jehan's avatar
jehan committed
154
		,jobject  thiz
155 156
		,jboolean isDebug
		,jstring  jdebugTag) {
jehan's avatar
jehan committed
157
	if (isDebug) {
158
		LogDomain = env->GetStringUTFChars(jdebugTag, NULL);
159
		linphone_core_enable_logs_with_cb(linphone_android_ortp_log_handler);
jehan's avatar
jehan committed
160 161 162 163
	} else {
		linphone_core_disable_logs();
	}
}
jehan's avatar
jehan committed
164 165
// LinphoneCore

166 167 168 169 170 171 172
/*
 * returns the java LinphoneProxyConfig associated with a C LinphoneProxyConfig.
**/
jobject getProxy(JNIEnv *env, LinphoneProxyConfig *proxy, jobject core){
	jobject jobj=0;

	if (proxy!=NULL){
173
		jclass proxyClass = (jclass)env->FindClass("org/linphone/core/LinphoneProxyConfigImpl");
174
		jmethodID proxyCtrId = env->GetMethodID(proxyClass,"<init>", "(Lorg/linphone/core/LinphoneCoreImpl;J)V");
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		void *up=linphone_proxy_config_get_user_data(proxy);

		if (up==NULL){
			jobj=env->NewObject(proxyClass,proxyCtrId,core,(jlong)proxy);
			linphone_proxy_config_set_user_data(proxy,(void*)env->NewWeakGlobalRef(jobj));
			linphone_proxy_config_ref(proxy);
		}else{
			//promote the weak ref to local ref
			jobj=env->NewLocalRef((jobject)up);
			if (jobj == NULL){
				//the weak ref was dead
				jobj=env->NewObject(proxyClass,proxyCtrId,core,(jlong)proxy);
				linphone_proxy_config_set_user_data(proxy,(void*)env->NewWeakGlobalRef(jobj));
			}
		}
191
		env->DeleteLocalRef(proxyClass);
192 193 194 195 196 197 198 199
	}
	return jobj;
}

jobject getCall(JNIEnv *env, LinphoneCall *call){
	jobject jobj=0;

	if (call!=NULL){
200
		jclass callClass = (jclass)env->FindClass("org/linphone/core/LinphoneCallImpl");
201
		jmethodID callCtrId = env->GetMethodID(callClass,"<init>", "(J)V");
202

203 204 205 206 207 208 209 210 211 212
		void *up=linphone_call_get_user_pointer(call);

		if (up==NULL){
			jobj=env->NewObject(callClass,callCtrId,(jlong)call);
			jobj=env->NewGlobalRef(jobj);
			linphone_call_set_user_pointer(call,(void*)jobj);
			linphone_call_ref(call);
		}else{
			jobj=(jobject)up;
		}
213
		env->DeleteLocalRef(callClass);
214 215 216 217 218 219 220 221
	}
	return jobj;
}

jobject getChatMessage(JNIEnv *env, LinphoneChatMessage *msg){
	jobject jobj = 0;

	if (msg != NULL){
222
		jclass chatMessageClass = (jclass)env->FindClass("org/linphone/core/LinphoneChatMessageImpl");
223
		jmethodID chatMessageCtrId = env->GetMethodID(chatMessageClass,"<init>", "(J)V");
224

225 226 227 228 229 230 231 232 233
		void *up = linphone_chat_message_get_user_data(msg);

		if (up == NULL) {
			jobj = env->NewObject(chatMessageClass,chatMessageCtrId,(jlong)linphone_chat_message_ref(msg));
			jobj = env->NewGlobalRef(jobj);
			linphone_chat_message_set_user_data(msg,(void*)jobj);
		} else {
			jobj = (jobject)up;
		}
234
		env->DeleteLocalRef(chatMessageClass);
235 236 237 238 239 240 241 242
	}
	return jobj;
}

jobject getEvent(JNIEnv *env, LinphoneEvent *lev){
	if (lev==NULL) return NULL;
	jobject jev=(jobject)linphone_event_get_user_data(lev);
	if (jev==NULL){
243
		jclass linphoneEventClass = (jclass)env->FindClass("org/linphone/core/LinphoneEventImpl");
244
		jmethodID linphoneEventCtrId = env->GetMethodID(linphoneEventClass,"<init>", "(J)V");
245

246 247 248
		jev=env->NewObject(linphoneEventClass,linphoneEventCtrId,(jlong)linphone_event_ref(lev));
		jev=env->NewGlobalRef(jev);
		linphone_event_set_user_data(lev,jev);
249

250
		env->DeleteLocalRef(linphoneEventClass);
251 252 253 254
	}
	return jev;
}

jehan's avatar
jehan committed
255 256
class LinphoneCoreData {
public:
257
	LinphoneCoreData(JNIEnv *env, jobject lc, LinphoneCoreVTable *vTable, jobject alistener) {
jehan's avatar
jehan committed
258 259
		core = env->NewGlobalRef(lc);
		listener = env->NewGlobalRef(alistener);
260

261
		memset(vTable, 0, sizeof(LinphoneCoreVTable));
262

263
		listenerClass = (jclass)env->NewGlobalRef(env->GetObjectClass(alistener));
264

jehan's avatar
jehan committed
265
		/*displayStatus(LinphoneCore lc,String message);*/
266
		displayStatusId = env->GetMethodID(listenerClass,"displayStatus","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;)V");
267
		env->ExceptionClear();
268 269 270
		if (displayStatusId) {
			vTable->display_status = displayStatusCb;
		}
271

jehan's avatar
jehan committed
272
		/*void generalState(LinphoneCore lc,int state); */
273 274
		globalStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$GlobalState"));
		globalStateFromIntId = env->GetStaticMethodID(globalStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$GlobalState;");
275 276
		globalStateId = env->GetMethodID(listenerClass,"globalState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$GlobalState;Ljava/lang/String;)V");
		env->ExceptionClear();
277 278 279
		if (globalStateId) {
			vTable->global_state_changed = globalStateChange;
		}
280

281 282 283
		/*registrationState(LinphoneCore lc, LinphoneProxyConfig cfg, LinphoneCore.RegistrationState cstate, String smessage);*/
		registrationStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$RegistrationState"));
		registrationStateFromIntId = env->GetStaticMethodID(registrationStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$RegistrationState;");
284 285
		registrationStateId = env->GetMethodID(listenerClass,"registrationState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneProxyConfig;Lorg/linphone/core/LinphoneCore$RegistrationState;Ljava/lang/String;)V");
		env->ExceptionClear();
286 287 288
		if (registrationStateId) {
			vTable->registration_state_changed = registrationStateChange;
		}
289

290 291 292
		/*callState(LinphoneCore lc, LinphoneCall call, LinphoneCall.State cstate,String message);*/
		callStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCall$State"));
		callStateFromIntId = env->GetStaticMethodID(callStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCall$State;");
293 294
		callStateId = env->GetMethodID(listenerClass,"callState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;Ljava/lang/String;)V");
		env->ExceptionClear();
295 296 297
		if (callStateId) {
			vTable->call_state_changed = callStateChange;
		}
298

299
		transferStateId = env->GetMethodID(listenerClass,"transferState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;)V");
300
		env->ExceptionClear();
301 302 303
		if (transferStateId) {
			vTable->transfer_state_changed = transferStateChanged;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
304

305 306
		/*callStatsUpdated(LinphoneCore lc, LinphoneCall call, LinphoneCallStats stats);*/
		callStatsUpdatedId = env->GetMethodID(listenerClass, "callStatsUpdated", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCallStats;)V");
307
		env->ExceptionClear();
308 309 310
		if (callStatsUpdatedId) {
			vTable->call_stats_updated = callStatsUpdated;
		}
311

312
		/*callEncryption(LinphoneCore lc, LinphoneCall call, boolean encrypted,String auth_token);*/
313
		callEncryptionChangedId = env->GetMethodID(listenerClass,"callEncryptionChanged","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;ZLjava/lang/String;)V");
314
		env->ExceptionClear();
315 316 317
		if (callEncryptionChangedId) {
			vTable->call_encryption_changed = callEncryptionChange;
		}
318

jehan's avatar
jehan committed
319 320 321
		/*void ecCalibrationStatus(LinphoneCore.EcCalibratorStatus status, int delay_ms, Object data);*/
		ecCalibratorStatusClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$EcCalibratorStatus"));
		ecCalibratorStatusFromIntId = env->GetStaticMethodID(ecCalibratorStatusClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;");
322 323
		ecCalibrationStatusId = env->GetMethodID(listenerClass,"ecCalibrationStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;ILjava/lang/Object;)V");
		env->ExceptionClear();
324

325
		/*void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf, String url)*/
326
		newSubscriptionRequestId = env->GetMethodID(listenerClass,"newSubscriptionRequest","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;Ljava/lang/String;)V");
327
		env->ExceptionClear();
328 329 330
		if (newSubscriptionRequestId) {
			vTable->new_subscription_requested = new_subscription_requested;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
331

332
		authInfoRequestedId = env->GetMethodID(listenerClass,"authInfoRequested","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
333
		env->ExceptionClear();
334 335 336
		if (authInfoRequestedId) {
			vTable->auth_info_requested = authInfoRequested;
		}
337 338

		/*void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf);*/
339
		notifyPresenceReceivedId = env->GetMethodID(listenerClass,"notifyPresenceReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;)V");
340
		env->ExceptionClear();
341 342 343
		if (notifyPresenceReceivedId) {
			vTable->notify_presence_received = notify_presence_received;
		}
344 345

		/*void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from,String message);*/
346
		textReceivedId = env->GetMethodID(listenerClass,"textReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneAddress;Ljava/lang/String;)V");
347
		env->ExceptionClear();
348 349 350
		if (textReceivedId) {
			vTable->text_received = text_received;
		}
351

Simon Morlat's avatar
Simon Morlat committed
352
		messageReceivedId = env->GetMethodID(listenerClass,"messageReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneChatMessage;)V");
353
		env->ExceptionClear();
354 355 356
		if (messageReceivedId) {
			vTable->message_received = message_received;
		}
357

358
		isComposingReceivedId = env->GetMethodID(listenerClass,"isComposingReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;)V");
359
		env->ExceptionClear();
360 361 362
		if (isComposingReceivedId) {
			vTable->is_composing_received = is_composing_received;
		}
363

Sylvain Berfini's avatar
Sylvain Berfini committed
364
		dtmfReceivedId = env->GetMethodID(listenerClass,"dtmfReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;I)V");
365
		env->ExceptionClear();
366 367 368
		if (dtmfReceivedId) {
			vTable->dtmf_received = dtmf_received;
		}
369

370
		infoReceivedId = env->GetMethodID(listenerClass,"infoReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneInfoMessage;)V");
371
		env->ExceptionClear();
372 373 374
		if (infoReceivedId) {
			vTable->info_received = infoReceived;
		}
375

376 377
		subscriptionStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/SubscriptionState"));
		subscriptionStateFromIntId = env->GetStaticMethodID(subscriptionStateClass,"fromInt","(I)Lorg/linphone/core/SubscriptionState;");
378 379
		subscriptionStateId = env->GetMethodID(listenerClass,"subscriptionStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/SubscriptionState;)V");
		env->ExceptionClear();
380 381 382
		if (subscriptionStateId) {
			vTable->subscription_state_changed = subscriptionStateChanged;
		}
383

384 385
		publishStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/PublishState"));
		publishStateFromIntId = env->GetStaticMethodID(publishStateClass,"fromInt","(I)Lorg/linphone/core/PublishState;");
386 387
		publishStateId = env->GetMethodID(listenerClass,"publishStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/PublishState;)V");
		env->ExceptionClear();
388 389 390
		if (publishStateId) {
			vTable->publish_state_changed = publishStateChanged;
		}
391

392
		notifyRecvId = env->GetMethodID(listenerClass,"notifyReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Ljava/lang/String;Lorg/linphone/core/LinphoneContent;)V");
393
		env->ExceptionClear();
394 395 396
		if (notifyRecvId) {
			vTable->notify_received = notifyReceived;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
397

398 399
		configuringStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$RemoteProvisioningState"));
		configuringStateFromIntId = env->GetStaticMethodID(configuringStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;");
400 401
		configuringStateId = env->GetMethodID(listenerClass,"configuringStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;Ljava/lang/String;)V");
		env->ExceptionClear();
402 403 404
		if (configuringStateId) {
			vTable->configuring_status = configuringStatus;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
405

406
		fileTransferProgressIndicationId = env->GetMethodID(listenerClass, "fileTransferProgressIndication", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;I)V");
407
		env->ExceptionClear();
408 409 410
		if (fileTransferProgressIndicationId) {
			vTable->file_transfer_progress_indication = fileTransferProgressIndication;
		}
411

412
		fileTransferSendId = env->GetMethodID(listenerClass, "fileTransferSend", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;Ljava/nio/ByteBuffer;I)I");
413
		env->ExceptionClear();
414 415 416 417 418
		if (fileTransferSendId) {
			vTable->file_transfer_send = fileTransferSend;
		}

		fileTransferRecvId = env->GetMethodID(listenerClass, "fileTransferRecv", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;[BI)V");
419
		env->ExceptionClear();
420 421 422
		if (fileTransferRecvId) {
			vTable->file_transfer_recv = fileTransferRecv;
		}
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
423

424 425
		logCollectionUploadStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$LogCollectionUploadState"));
		logCollectionUploadStateFromIntId = env->GetStaticMethodID(logCollectionUploadStateClass, "fromInt", "(I)Lorg/linphone/core/LinphoneCore$LogCollectionUploadState;");
Sylvain Berfini's avatar
Sylvain Berfini committed
426
		logCollectionUploadProgressId = env->GetMethodID(listenerClass, "uploadProgressIndication", "(Lorg/linphone/core/LinphoneCore;II)V");
427 428 429 430 431 432 433 434 435
		env->ExceptionClear();
		if (logCollectionUploadProgressId) {
			vTable->log_collection_upload_progress_indication = logCollectionUploadProgressIndication;
		}
		logCollectionUploadStateId = env->GetMethodID(listenerClass, "uploadStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$LogCollectionUploadState;Ljava/lang/String;)V");
		env->ExceptionClear();
		if (logCollectionUploadStateId) {
			vTable->log_collection_upload_state_changed = logCollectionUploadStateChange;
		}
436 437 438

		chatMessageStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessage$State"));
		chatMessageStateFromIntId = env->GetStaticMethodID(chatMessageStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneChatMessage$State;");
439

440
		proxyClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneProxyConfigImpl"));
441
		proxyCtrId = env->GetMethodID(proxyClass,"<init>", "(Lorg/linphone/core/LinphoneCoreImpl;J)V");
442 443 444

		callClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCallImpl"));
		callCtrId = env->GetMethodID(callClass,"<init>", "(J)V");
445 446
		callSetAudioStatsId = env->GetMethodID(callClass, "setAudioStats", "(Lorg/linphone/core/LinphoneCallStats;)V");
		callSetVideoStatsId = env->GetMethodID(callClass, "setVideoStats", "(Lorg/linphone/core/LinphoneCallStats;)V");
jehan's avatar
jehan committed
447

448
		chatMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessageImpl"));
449 450 451
		if (chatMessageClass) {
			chatMessageCtrId = env->GetMethodID(chatMessageClass,"<init>", "(J)V");
		}
452

453 454 455 456
		chatRoomClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatRoomImpl"));
		chatRoomCtrId = env->GetMethodID(chatRoomClass,"<init>", "(J)V");

		friendClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneFriendImpl"));;
457
		friendCtrId = env->GetMethodID(friendClass,"<init>", "(J)V");
458

jehan's avatar
jehan committed
459
		addressClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAddressImpl"));
460
		addressCtrId = env->GetMethodID(addressClass,"<init>", "(J)V");
jehan's avatar
jehan committed
461

462 463
		callStatsClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCallStatsImpl"));
		callStatsId = env->GetMethodID(callStatsClass, "<init>", "(JJ)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
464

465 466
		infoMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneInfoMessageImpl"));
		infoMessageCtor = env->GetMethodID(infoMessageClass,"<init>", "(J)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
467

468 469
		linphoneEventClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneEventImpl"));
		linphoneEventCtrId = env->GetMethodID(linphoneEventClass,"<init>", "(J)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
470

471 472
		subscriptionDirClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/SubscriptionDir"));
		subscriptionDirFromIntId = env->GetStaticMethodID(subscriptionDirClass,"fromInt","(I)Lorg/linphone/core/SubscriptionDir;");
jehan's avatar
jehan committed
473 474 475 476 477 478 479
	}

	~LinphoneCoreData() {
		JNIEnv *env = 0;
		jvm->AttachCurrentThread(&env,NULL);
		env->DeleteGlobalRef(core);
		env->DeleteGlobalRef(listener);
480
		env->DeleteGlobalRef(listenerClass);
jehan's avatar
jehan committed
481
		env->DeleteGlobalRef(globalStateClass);
482
		env->DeleteGlobalRef(configuringStateClass);
jehan's avatar
jehan committed
483 484
		env->DeleteGlobalRef(registrationStateClass);
		env->DeleteGlobalRef(callStateClass);
485
		env->DeleteGlobalRef(chatMessageStateClass);
jehan's avatar
jehan committed
486 487
		env->DeleteGlobalRef(proxyClass);
		env->DeleteGlobalRef(callClass);
488
		env->DeleteGlobalRef(chatMessageClass);
489 490
		env->DeleteGlobalRef(chatRoomClass);
		env->DeleteGlobalRef(friendClass);
491
		env->DeleteGlobalRef(infoMessageClass);
492 493 494
		env->DeleteGlobalRef(linphoneEventClass);
		env->DeleteGlobalRef(subscriptionStateClass);
		env->DeleteGlobalRef(subscriptionDirClass);
495
		env->DeleteGlobalRef(logCollectionUploadStateClass);
jehan's avatar
jehan committed
496 497 498 499
	}
	jobject core;
	jobject listener;

500
	jclass listenerClass;
jehan's avatar
jehan committed
501
	jmethodID displayStatusId;
502 503 504
	jmethodID newSubscriptionRequestId;
	jmethodID notifyPresenceReceivedId;
	jmethodID textReceivedId;
505
	jmethodID messageReceivedId;
506
	jmethodID isComposingReceivedId;
Sylvain Berfini's avatar
Sylvain Berfini committed
507
	jmethodID dtmfReceivedId;
508
	jmethodID callStatsUpdatedId;
509
	jmethodID transferStateId;
510
	jmethodID infoReceivedId;
511
	jmethodID subscriptionStateId;
512
	jmethodID authInfoRequestedId;
513
	jmethodID publishStateId;
514
	jmethodID notifyRecvId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
515

516 517 518
	jclass configuringStateClass;
	jmethodID configuringStateId;
	jmethodID configuringStateFromIntId;
519 520 521 522 523 524 525 526 527 528 529 530 531

	jclass globalStateClass;
	jmethodID globalStateId;
	jmethodID globalStateFromIntId;

	jclass registrationStateClass;
	jmethodID registrationStateId;
	jmethodID registrationStateFromIntId;

	jclass callStateClass;
	jmethodID callStateId;
	jmethodID callStateFromIntId;

532 533 534 535 536
	jclass callStatsClass;
	jmethodID callStatsId;
	jmethodID callSetAudioStatsId;
	jmethodID callSetVideoStatsId;

537 538 539
	jclass chatMessageStateClass;
	jmethodID chatMessageStateFromIntId;

540 541
	jmethodID callEncryptionChangedId;

jehan's avatar
jehan committed
542 543 544 545
	jclass ecCalibratorStatusClass;
	jmethodID ecCalibrationStatusId;
	jmethodID ecCalibratorStatusFromIntId;

546 547 548 549 550 551
	jclass proxyClass;
	jmethodID proxyCtrId;

	jclass callClass;
	jmethodID callCtrId;

552 553 554
	jclass chatMessageClass;
	jmethodID chatMessageCtrId;

555 556 557 558 559 560
	jclass chatRoomClass;
	jmethodID chatRoomCtrId;

	jclass friendClass;
	jmethodID friendCtrId;

jehan's avatar
jehan committed
561 562
	jclass addressClass;
	jmethodID addressCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
563

564 565
	jclass infoMessageClass;
	jmethodID infoMessageCtor;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
566

567 568
	jclass linphoneEventClass;
	jmethodID linphoneEventCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
569

570 571
	jclass subscriptionStateClass;
	jmethodID subscriptionStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
572

573 574
	jclass publishStateClass;
	jmethodID publishStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
575

576 577
	jclass subscriptionDirClass;
	jmethodID subscriptionDirFromIntId;
jehan's avatar
jehan committed
578

579
	jmethodID fileTransferProgressIndicationId;
580 581
	jmethodID fileTransferSendId;
	jmethodID fileTransferRecvId;
582

583 584 585 586 587
	jclass logCollectionUploadStateClass;
	jmethodID logCollectionUploadStateId;
	jmethodID logCollectionUploadStateFromIntId;
	jmethodID logCollectionUploadProgressId;

jehan's avatar
jehan committed
588
	LinphoneCoreVTable vTable;
589

jehan's avatar
jehan committed
590 591 592 593
	static void displayStatusCb(LinphoneCore *lc, const char *message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
594
			ms_error("cannot attach VM");
jehan's avatar
jehan committed
595 596
			return;
		}
597 598
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
599 600 601 602 603
		jstring msg = message ? env->NewStringUTF(message) : NULL;
		env->CallVoidMethod(lcData->listener,lcData->displayStatusId,lcData->core,msg);
		if (msg) {
			env->DeleteLocalRef(msg);
		}
jehan's avatar
jehan committed
604
	}
605
	static void authInfoRequested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
606 607 608 609 610 611
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
		}
612 613
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
614 615 616
		jstring r = realm ? env->NewStringUTF(realm) : NULL;
		jstring u = username ? env->NewStringUTF(username) : NULL;
		jstring d = domain ? env->NewStringUTF(domain) : NULL;
617 618 619
		env->CallVoidMethod(lcData->listener,
							lcData->authInfoRequestedId,
							lcData->core,
620 621 622 623 624 625 626 627 628 629 630 631
							r,
							u,
							d);
		if (r) {
			env->DeleteLocalRef(r);
		}
		if (u) {
			env->DeleteLocalRef(u);
		}
		if (d) {
			env->DeleteLocalRef(d);
		}
jehan's avatar
jehan committed
632
	}
633 634 635 636
	static void globalStateChange(LinphoneCore *lc, LinphoneGlobalState gstate,const char* message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
637
			ms_error("cannot attach VM");
638 639
			return;
		}
640 641
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
642
		jstring msg = message ? env->NewStringUTF(message) : NULL;
643 644 645
		env->CallVoidMethod(lcData->listener
							,lcData->globalStateId
							,lcData->core
jehan's avatar
jehan committed
646
							,env->CallStaticObjectMethod(lcData->globalStateClass,lcData->globalStateFromIntId,(jint)gstate),
647 648 649 650
							msg);
		if (msg) {
			env->DeleteLocalRef(msg);
		}
651
	}
jehan's avatar
jehan committed
652
	static void registrationStateChange(LinphoneCore *lc, LinphoneProxyConfig* proxy,LinphoneRegistrationState state,const char* message) {
jehan's avatar
jehan committed
653 654
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
655
		jobject jproxy;
jehan's avatar
jehan committed
656
		if (result != 0) {
657
			ms_error("cannot attach VM");
jehan's avatar
jehan committed
658 659
			return;
		}
660 661
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
662
		jstring msg = message ? env->NewStringUTF(message) : NULL;
jehan's avatar
jehan committed
663
		env->CallVoidMethod(lcData->listener
664
							,lcData->registrationStateId
jehan's avatar
jehan committed
665
							,lcData->core
666
							,(jproxy=getProxy(env,proxy,lcData->core))
jehan's avatar
jehan committed
667
							,env->CallStaticObjectMethod(lcData->registrationStateClass,lcData->registrationStateFromIntId,(jint)state),
668 669 670 671
							msg);
		if (msg) {
			env->DeleteLocalRef(msg);
		}
jehan's avatar
jehan committed
672
	}
673

jehan's avatar
jehan committed
674
	static void callStateChange(LinphoneCore *lc, LinphoneCall* call,LinphoneCallState state,const char* message) {
675 676
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
677
		jobject jcall;
678
		if (result != 0) {
679
			ms_error("cannot attach VM");
680 681
			return;
		}
682 683
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
684
		jstring msg = message ? env->NewStringUTF(message) : NULL;
685 686 687
		env->CallVoidMethod(lcData->listener
							,lcData->callStateId
							,lcData->core
688
							,(jcall=getCall(env,call))
jehan's avatar
jehan committed
689
							,env->CallStaticObjectMethod(lcData->callStateClass,lcData->callStateFromIntId,(jint)state),
690 691
							msg);
		if (state==LinphoneCallReleased) {
692 693 694
			linphone_call_set_user_pointer(call,NULL);
			env->DeleteGlobalRef(jcall);
		}
695 696 697
		if (msg) {
			env->DeleteLocalRef(msg);
		}
698
	}
699 700 701 702
	static void callEncryptionChange(LinphoneCore *lc, LinphoneCall* call, bool_t encrypted,const char* authentication_token) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
703
			ms_error("cannot attach VM");
704 705
			return;
		}
706 707
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
708 709 710
		env->CallVoidMethod(lcData->listener
							,lcData->callEncryptionChangedId
							,lcData->core
711
							,getCall(env,call)
712 713 714
							,encrypted
							,authentication_token ? env->NewStringUTF(authentication_token) : NULL);
	}
715
	static void notify_presence_received(LinphoneCore *lc,  LinphoneFriend *my_friend) {
716 717 718
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
719
			ms_error("cannot attach VM");
720 721
			return;
		}
722 723
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
724 725 726 727 728
		env->CallVoidMethod(lcData->listener
							,lcData->notifyPresenceReceivedId
							,lcData->core
							,env->NewObject(lcData->friendClass,lcData->friendCtrId,(jlong)my_friend));
	}
729
	static void new_subscription_requested(LinphoneCore *lc,  LinphoneFriend *my_friend, const char* url) {
730 731 732
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
733
			ms_error("cannot attach VM");
734 735
			return;
		}
736 737
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
738 739 740 741 742 743
		env->CallVoidMethod(lcData->listener
							,lcData->newSubscriptionRequestId
							,lcData->core
							,env->NewObject(lcData->friendClass,lcData->friendCtrId,(jlong)my_friend)
							,url ? env->NewStringUTF(url) : NULL);
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
744 745 746 747
	static void dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
748
			ms_error("cannot attach VM");
Sylvain Berfini's avatar
Sylvain Berfini committed
749 750
			return;
		}
751 752
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
Sylvain Berfini's avatar
Sylvain Berfini committed
753 754 755
		env->CallVoidMethod(lcData->listener
							,lcData->dtmfReceivedId
							,lcData->core
756
							,getCall(env,call)
Sylvain Berfini's avatar
Sylvain Berfini committed
757 758
							,dtmf);
	}
759 760 761 762
	static void text_received(LinphoneCore *lc, LinphoneChatRoom *room, const LinphoneAddress *from, const char *message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
763
			ms_error("cannot attach VM");
764 765
			return;
		}
766 767
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
768 769 770 771
		env->CallVoidMethod(lcData->listener
							,lcData->textReceivedId
							,lcData->core
							,env->NewObject(lcData->chatRoomClass,lcData->chatRoomCtrId,(jlong)room)
jehan's avatar
jehan committed
772
							,env->NewObject(lcData->addressClass,lcData->addressCtrId,(jlong)from)
773 774
							,message ? env->NewStringUTF(message) : NULL);
	}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
775
	static void message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
776 777 778 779 780 781
		JNIEnv *env = 0;
		jobject jmsg;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
782
		}
783 784 785 786 787 788 789 790 791
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
		/*note: we call linphone_chat_message_ref() because the application does not acquire the object when invoked from a callback*/
		env->CallVoidMethod(lcData->listener
							,lcData->messageReceivedId
							,lcData->core
							,env->NewObject(lcData->chatRoomClass,lcData->chatRoomCtrId,(jlong)room)
							,(jmsg = getChatMessage(env, msg)));
	}
792 793 794 795 796 797 798
	static void is_composing_received(LinphoneCore *lc, LinphoneChatRoom *room) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
		}