linphonecore_jni.cc 243 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*/

Ghislain MARY's avatar
Ghislain MARY committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#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
80
static JavaVM *jvm=0;
81
static const char* LogDomain = "Linphone";
82 83 84
static jclass handler_class;
static jmethodID loghandler_id;
static jobject handler_obj=NULL;
jehan's avatar
jehan committed
85

86
static jobject create_java_linphone_content(JNIEnv *env, const LinphoneContent *content);
87
static jobject create_java_linphone_buffer(JNIEnv *env, const LinphoneBuffer *buffer);
88
static LinphoneBuffer* create_c_linphone_buffer_from_java_linphone_buffer(JNIEnv *env, jobject jbuffer);
89

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

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

108
static void linphone_android_ortp_log_handler(OrtpLogLevel lev, const char *fmt, va_list args) {
109 110 111 112
	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
113

114 115
	int prio;
	switch(lev){
116 117 118 119 120 121
	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;
122
	}
123 124
	if (handler_obj){
		JNIEnv *env=ms_get_jni_env();
125 126 127 128 129 130 131
		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);
132 133
	}else
		linphone_android_log_handler(prio, str);
134 135
}

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

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


jehan's avatar
jehan committed
151
//LinphoneFactory
jehan's avatar
jehan committed
152
extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_setDebugMode(JNIEnv*  env
jehan's avatar
jehan committed
153
		,jobject  thiz
154 155
		,jboolean isDebug
		,jstring  jdebugTag) {
jehan's avatar
jehan committed
156
	if (isDebug) {
157
		LogDomain = env->GetStringUTFChars(jdebugTag, NULL);
158
		linphone_core_enable_logs_with_cb(linphone_android_ortp_log_handler);
jehan's avatar
jehan committed
159 160 161 162
	} else {
		linphone_core_disable_logs();
	}
}
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_enableLogCollection(JNIEnv* env
		,jobject  thiz
		,jboolean enable) {
	linphone_core_enable_log_collection(enable ? LinphoneLogCollectionEnabledWithoutPreviousLogHandler : LinphoneLogCollectionDisabled);
}

extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_setLogCollectionPath(JNIEnv* env
		,jobject  thiz
		,jstring jpath) {

	const char* path = env->GetStringUTFChars(jpath, NULL);
	linphone_core_set_log_collection_path(path);
	env->ReleaseStringUTFChars(jpath, path);
}
jehan's avatar
jehan committed
178 179
// LinphoneCore

180 181 182 183 184 185 186
/*
 * returns the java LinphoneProxyConfig associated with a C LinphoneProxyConfig.
**/
jobject getProxy(JNIEnv *env, LinphoneProxyConfig *proxy, jobject core){
	jobject jobj=0;

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

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		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));
			}
		}
205
		env->DeleteLocalRef(proxyClass);
206 207 208 209 210 211 212 213
	}
	return jobj;
}

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

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

217 218 219 220 221 222 223 224 225 226
		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;
		}
227
		env->DeleteLocalRef(callClass);
228 229 230 231 232 233 234 235
	}
	return jobj;
}

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

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

239 240 241 242 243 244 245 246 247
		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;
		}
248
		env->DeleteLocalRef(chatMessageClass);
249 250 251 252
	}
	return jobj;
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
jobject getFriend(JNIEnv *env, LinphoneFriend *lfriend, jobject core){
	jobject jobj=0;

	if (lfriend!=NULL){
		jclass friendClass = (jclass)env->FindClass("org/linphone/core/LinphoneFriendImpl");
		jmethodID friendCtrId = env->GetMethodID(friendClass,"<init>", "(J)V");

		void *up=linphone_friend_get_user_data(lfriend);

		if (up==NULL){
			jobj=env->NewObject(friendClass,friendCtrId,(jlong)lfriend);
			linphone_friend_set_user_data(lfriend,(void*)env->NewWeakGlobalRef(jobj));
			linphone_friend_ref(lfriend);
		}else{
			jobj=env->NewLocalRef((jobject)up);
			if (jobj == NULL){
				jobj=env->NewObject(friendClass,friendCtrId,(jlong)lfriend);
				linphone_friend_set_user_data(lfriend,(void*)env->NewWeakGlobalRef(jobj));
			}
		}
		env->DeleteLocalRef(friendClass);
	}
	return jobj;
}

278 279 280 281
jobject getEvent(JNIEnv *env, LinphoneEvent *lev){
	if (lev==NULL) return NULL;
	jobject jev=(jobject)linphone_event_get_user_data(lev);
	if (jev==NULL){
282
		jclass linphoneEventClass = (jclass)env->FindClass("org/linphone/core/LinphoneEventImpl");
283
		jmethodID linphoneEventCtrId = env->GetMethodID(linphoneEventClass,"<init>", "(J)V");
284

285 286 287
		jev=env->NewObject(linphoneEventClass,linphoneEventCtrId,(jlong)linphone_event_ref(lev));
		jev=env->NewGlobalRef(jev);
		linphone_event_set_user_data(lev,jev);
288

289
		env->DeleteLocalRef(linphoneEventClass);
290 291 292 293
	}
	return jev;
}

jehan's avatar
jehan committed
294 295
class LinphoneCoreData {
public:
296
	LinphoneCoreData(JNIEnv *env, jobject lc, LinphoneCoreVTable *vTable, jobject alistener) {
jehan's avatar
jehan committed
297 298
		core = env->NewGlobalRef(lc);
		listener = env->NewGlobalRef(alistener);
299

300
		memset(vTable, 0, sizeof(LinphoneCoreVTable));
301

302
		listenerClass = (jclass)env->NewGlobalRef(env->GetObjectClass(alistener));
303

jehan's avatar
jehan committed
304
		/*displayStatus(LinphoneCore lc,String message);*/
305
		displayStatusId = env->GetMethodID(listenerClass,"displayStatus","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;)V");
306 307 308
		if (displayStatusId) {
			vTable->display_status = displayStatusCb;
		}
309

jehan's avatar
jehan committed
310
		/*void generalState(LinphoneCore lc,int state); */
311 312
		globalStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$GlobalState"));
		globalStateFromIntId = env->GetStaticMethodID(globalStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$GlobalState;");
313
		globalStateId = env->GetMethodID(listenerClass,"globalState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$GlobalState;Ljava/lang/String;)V");
314 315 316
		if (globalStateId) {
			vTable->global_state_changed = globalStateChange;
		}
317

318 319 320
		/*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;");
321
		registrationStateId = env->GetMethodID(listenerClass,"registrationState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneProxyConfig;Lorg/linphone/core/LinphoneCore$RegistrationState;Ljava/lang/String;)V");
322 323 324
		if (registrationStateId) {
			vTable->registration_state_changed = registrationStateChange;
		}
325

326 327 328
		/*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;");
329
		callStateId = env->GetMethodID(listenerClass,"callState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;Ljava/lang/String;)V");
330 331 332
		if (callStateId) {
			vTable->call_state_changed = callStateChange;
		}
333

334
		transferStateId = env->GetMethodID(listenerClass,"transferState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;)V");
335 336 337
		if (transferStateId) {
			vTable->transfer_state_changed = transferStateChanged;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
338

339 340
		/*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");
341 342 343
		if (callStatsUpdatedId) {
			vTable->call_stats_updated = callStatsUpdated;
		}
344

345
		/*callEncryption(LinphoneCore lc, LinphoneCall call, boolean encrypted,String auth_token);*/
346 347 348 349
		callEncryptionChangedId = env->GetMethodID(listenerClass,"callEncryptionChanged","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;ZLjava/lang/String;)V");
		if (callEncryptionChangedId) {
			vTable->call_encryption_changed = callEncryptionChange;
		}
350

jehan's avatar
jehan committed
351 352 353
		/*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;");
354
		ecCalibrationStatusId = env->GetMethodID(listenerClass,"ecCalibrationStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;ILjava/lang/Object;)V");
355

356
		/*void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf, String url)*/
357
		newSubscriptionRequestId = env->GetMethodID(listenerClass,"newSubscriptionRequest","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;Ljava/lang/String;)V");
358 359 360
		if (newSubscriptionRequestId) {
			vTable->new_subscription_requested = new_subscription_requested;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
361

362
		authInfoRequestedId = env->GetMethodID(listenerClass,"authInfoRequested","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
363 364 365
		if (authInfoRequestedId) {
			vTable->auth_info_requested = authInfoRequested;
		}
366 367

		/*void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf);*/
368
		notifyPresenceReceivedId = env->GetMethodID(listenerClass,"notifyPresenceReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;)V");
369 370 371
		if (notifyPresenceReceivedId) {
			vTable->notify_presence_received = notify_presence_received;
		}
372 373

		/*void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from,String message);*/
374
		textReceivedId = env->GetMethodID(listenerClass,"textReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneAddress;Ljava/lang/String;)V");
375 376 377
		if (textReceivedId) {
			vTable->text_received = text_received;
		}
378

Simon Morlat's avatar
Simon Morlat committed
379
		messageReceivedId = env->GetMethodID(listenerClass,"messageReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneChatMessage;)V");
380 381 382
		if (messageReceivedId) {
			vTable->message_received = message_received;
		}
383

384
		isComposingReceivedId = env->GetMethodID(listenerClass,"isComposingReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;)V");
385 386 387
		if (isComposingReceivedId) {
			vTable->is_composing_received = is_composing_received;
		}
388

Sylvain Berfini's avatar
Sylvain Berfini committed
389
		dtmfReceivedId = env->GetMethodID(listenerClass,"dtmfReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;I)V");
390 391 392
		if (dtmfReceivedId) {
			vTable->dtmf_received = dtmf_received;
		}
393

394 395 396 397
		infoReceivedId = env->GetMethodID(listenerClass,"infoReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneInfoMessage;)V");
		if (infoReceivedId) {
			vTable->info_received = infoReceived;
		}
398

399 400
		subscriptionStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/SubscriptionState"));
		subscriptionStateFromIntId = env->GetStaticMethodID(subscriptionStateClass,"fromInt","(I)Lorg/linphone/core/SubscriptionState;");
401
		subscriptionStateId = env->GetMethodID(listenerClass,"subscriptionStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/SubscriptionState;)V");
402 403 404
		if (subscriptionStateId) {
			vTable->subscription_state_changed = subscriptionStateChanged;
		}
405

406 407
		publishStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/PublishState"));
		publishStateFromIntId = env->GetStaticMethodID(publishStateClass,"fromInt","(I)Lorg/linphone/core/PublishState;");
408
		publishStateId = env->GetMethodID(listenerClass,"publishStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/PublishState;)V");
409 410 411
		if (publishStateId) {
			vTable->publish_state_changed = publishStateChanged;
		}
412

413 414 415 416
		notifyRecvId = env->GetMethodID(listenerClass,"notifyReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Ljava/lang/String;Lorg/linphone/core/LinphoneContent;)V");
		if (notifyRecvId) {
			vTable->notify_received = notifyReceived;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
417

418 419
		configuringStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$RemoteProvisioningState"));
		configuringStateFromIntId = env->GetStaticMethodID(configuringStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;");
420
		configuringStateId = env->GetMethodID(listenerClass,"configuringStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;Ljava/lang/String;)V");
421 422 423
		if (configuringStateId) {
			vTable->configuring_status = configuringStatus;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
424

425 426 427 428
		fileTransferProgressIndicationId = env->GetMethodID(listenerClass, "fileTransferProgressIndication", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;I)V");
		if (fileTransferProgressIndicationId) {
			vTable->file_transfer_progress_indication = fileTransferProgressIndication;
		}
429

430 431 432 433 434 435 436 437 438
		fileTransferSendId = env->GetMethodID(listenerClass, "fileTransferSend", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;Ljava/nio/ByteBuffer;I)I");
		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");
		if (fileTransferRecvId) {
			vTable->file_transfer_recv = fileTransferRecv;
		}
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
439

440 441
		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
442
		logCollectionUploadProgressId = env->GetMethodID(listenerClass, "uploadProgressIndication", "(Lorg/linphone/core/LinphoneCore;II)V");
443 444 445 446 447 448 449
		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");
		if (logCollectionUploadStateId) {
			vTable->log_collection_upload_state_changed = logCollectionUploadStateChange;
		}
450 451 452

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

454
		proxyClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneProxyConfigImpl"));
455
		proxyCtrId = env->GetMethodID(proxyClass,"<init>", "(Lorg/linphone/core/LinphoneCoreImpl;J)V");
456 457 458

		callClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCallImpl"));
		callCtrId = env->GetMethodID(callClass,"<init>", "(J)V");
459 460
		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
461

462
		chatMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessageImpl"));
463 464 465
		if (chatMessageClass) {
			chatMessageCtrId = env->GetMethodID(chatMessageClass,"<init>", "(J)V");
		}
466

467 468 469 470
		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"));;
471
		friendCtrId = env->GetMethodID(friendClass,"<init>", "(J)V");
472

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

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

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

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

485 486
		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
487 488 489 490 491 492 493
	}

	~LinphoneCoreData() {
		JNIEnv *env = 0;
		jvm->AttachCurrentThread(&env,NULL);
		env->DeleteGlobalRef(core);
		env->DeleteGlobalRef(listener);
494
		env->DeleteGlobalRef(listenerClass);
jehan's avatar
jehan committed
495
		env->DeleteGlobalRef(globalStateClass);
496
		env->DeleteGlobalRef(configuringStateClass);
jehan's avatar
jehan committed
497 498
		env->DeleteGlobalRef(registrationStateClass);
		env->DeleteGlobalRef(callStateClass);
499
		env->DeleteGlobalRef(chatMessageStateClass);
jehan's avatar
jehan committed
500 501
		env->DeleteGlobalRef(proxyClass);
		env->DeleteGlobalRef(callClass);
502
		env->DeleteGlobalRef(chatMessageClass);
503 504
		env->DeleteGlobalRef(chatRoomClass);
		env->DeleteGlobalRef(friendClass);
505
		env->DeleteGlobalRef(infoMessageClass);
506 507 508
		env->DeleteGlobalRef(linphoneEventClass);
		env->DeleteGlobalRef(subscriptionStateClass);
		env->DeleteGlobalRef(subscriptionDirClass);
509
		env->DeleteGlobalRef(logCollectionUploadStateClass);
jehan's avatar
jehan committed
510 511 512 513
	}
	jobject core;
	jobject listener;

514
	jclass listenerClass;
jehan's avatar
jehan committed
515
	jmethodID displayStatusId;
516 517 518
	jmethodID newSubscriptionRequestId;
	jmethodID notifyPresenceReceivedId;
	jmethodID textReceivedId;
519
	jmethodID messageReceivedId;
520
	jmethodID isComposingReceivedId;
Sylvain Berfini's avatar
Sylvain Berfini committed
521
	jmethodID dtmfReceivedId;
522
	jmethodID callStatsUpdatedId;
523
	jmethodID transferStateId;
524
	jmethodID infoReceivedId;
525
	jmethodID subscriptionStateId;
526
	jmethodID authInfoRequestedId;
527
	jmethodID publishStateId;
528
	jmethodID notifyRecvId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
529

530 531 532
	jclass configuringStateClass;
	jmethodID configuringStateId;
	jmethodID configuringStateFromIntId;
533 534 535 536 537 538 539 540 541 542 543 544 545

	jclass globalStateClass;
	jmethodID globalStateId;
	jmethodID globalStateFromIntId;

	jclass registrationStateClass;
	jmethodID registrationStateId;
	jmethodID registrationStateFromIntId;

	jclass callStateClass;
	jmethodID callStateId;
	jmethodID callStateFromIntId;

546 547 548 549 550
	jclass callStatsClass;
	jmethodID callStatsId;
	jmethodID callSetAudioStatsId;
	jmethodID callSetVideoStatsId;

551 552 553
	jclass chatMessageStateClass;
	jmethodID chatMessageStateFromIntId;

554 555
	jmethodID callEncryptionChangedId;

jehan's avatar
jehan committed
556 557 558 559
	jclass ecCalibratorStatusClass;
	jmethodID ecCalibrationStatusId;
	jmethodID ecCalibratorStatusFromIntId;

560 561 562 563 564 565
	jclass proxyClass;
	jmethodID proxyCtrId;

	jclass callClass;
	jmethodID callCtrId;

566 567 568
	jclass chatMessageClass;
	jmethodID chatMessageCtrId;

569 570 571 572 573 574
	jclass chatRoomClass;
	jmethodID chatRoomCtrId;

	jclass friendClass;
	jmethodID friendCtrId;

jehan's avatar
jehan committed
575 576
	jclass addressClass;
	jmethodID addressCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
577

578 579
	jclass infoMessageClass;
	jmethodID infoMessageCtor;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
580

581 582
	jclass linphoneEventClass;
	jmethodID linphoneEventCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
583

584 585
	jclass subscriptionStateClass;
	jmethodID subscriptionStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
586

587 588
	jclass publishStateClass;
	jmethodID publishStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
589

590 591
	jclass subscriptionDirClass;
	jmethodID subscriptionDirFromIntId;
jehan's avatar
jehan committed
592

593
	jmethodID fileTransferProgressIndicationId;
594 595
	jmethodID fileTransferSendId;
	jmethodID fileTransferRecvId;
596

597 598 599 600 601
	jclass logCollectionUploadStateClass;
	jmethodID logCollectionUploadStateId;
	jmethodID logCollectionUploadStateFromIntId;
	jmethodID logCollectionUploadProgressId;

jehan's avatar
jehan committed
602
	LinphoneCoreVTable vTable;
603

jehan's avatar
jehan committed
604 605 606 607
	static void displayStatusCb(LinphoneCore *lc, const char *message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
608
			ms_error("cannot attach VM");
jehan's avatar
jehan committed
609 610
			return;
		}
611 612
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
613 614 615 616 617
		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
618
	}
619
	static void authInfoRequested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
620 621 622 623 624 625
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
		}
626 627
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
628 629 630
		jstring r = realm ? env->NewStringUTF(realm) : NULL;
		jstring u = username ? env->NewStringUTF(username) : NULL;
		jstring d = domain ? env->NewStringUTF(domain) : NULL;
631 632 633
		env->CallVoidMethod(lcData->listener,
							lcData->authInfoRequestedId,
							lcData->core,
634 635 636 637 638 639 640 641 642 643 644 645
							r,
							u,
							d);
		if (r) {
			env->DeleteLocalRef(r);
		}
		if (u) {
			env->DeleteLocalRef(u);
		}
		if (d) {
			env->DeleteLocalRef(d);
		}
jehan's avatar
jehan committed
646
	}
647 648 649 650
	static void globalStateChange(LinphoneCore *lc, LinphoneGlobalState gstate,const char* message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
651
			ms_error("cannot attach VM");
652 653
			return;
		}
654 655
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
656
		jstring msg = message ? env->NewStringUTF(message) : NULL;
657 658 659
		env->CallVoidMethod(lcData->listener
							,lcData->globalStateId
							,lcData->core
jehan's avatar
jehan committed
660
							,env->CallStaticObjectMethod(lcData->globalStateClass,lcData->globalStateFromIntId,(jint)gstate),
661
							msg);
662 663 664 665
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
666 667 668
		if (msg) {
			env->DeleteLocalRef(msg);
		}
669
	}
jehan's avatar
jehan committed
670
	static void registrationStateChange(LinphoneCore *lc, LinphoneProxyConfig* proxy,LinphoneRegistrationState state,const char* message) {
jehan's avatar
jehan committed
671 672
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
673
		jobject jproxy;
jehan's avatar
jehan committed
674
		if (result != 0) {
675
			ms_error("cannot attach VM");
jehan's avatar
jehan committed
676 677
			return;
		}
678 679
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
680
		jstring msg = message ? env->NewStringUTF(message) : NULL;
jehan's avatar
jehan committed
681
		env->CallVoidMethod(lcData->listener
682
							,lcData->registrationStateId
jehan's avatar
jehan committed
683
							,lcData->core
684
							,(jproxy=getProxy(env,proxy,lcData->core))
jehan's avatar
jehan committed
685
							,env->CallStaticObjectMethod(lcData->registrationStateClass,lcData->registrationStateFromIntId,(jint)state),
686
							msg);
687 688 689 690
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
691 692 693
		if (msg) {
			env->DeleteLocalRef(msg);
		}
jehan's avatar
jehan committed
694
	}
695

jehan's avatar
jehan committed
696
	static void callStateChange(LinphoneCore *lc, LinphoneCall* call,LinphoneCallState state,const char* message) {
697 698
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
699
		jobject jcall;
700
		if (result != 0) {
701
			ms_error("cannot attach VM");
702 703
			return;
		}
704 705
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
706
		jstring msg = message ? env->NewStringUTF(message) : NULL;
707 708 709
		env->CallVoidMethod(lcData->listener
							,lcData->callStateId
							,lcData->core
710
							,(jcall=getCall(env,call))
jehan's avatar
jehan committed
711
							,env->CallStaticObjectMethod(lcData->callStateClass,lcData->callStateFromIntId,(jint)state),
712
							msg);
713 714 715 716
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
717
		if (state==LinphoneCallReleased) {
718 719 720
			linphone_call_set_user_pointer(call,NULL);
			env->DeleteGlobalRef(jcall);
		}
721 722 723
		if (msg) {
			env->DeleteLocalRef(msg);
		}
724
	}
725 726 727 728
	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) {
729
			ms_error("cannot attach VM");
730 731
			return;
		}
732 733
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
734 735 736
		env->CallVoidMethod(lcData->listener
							,lcData->callEncryptionChangedId
							,lcData->core
737
							,getCall(env,call)
738 739
							,encrypted
							,authentication_token ? env->NewStringUTF(authentication_token) : NULL);
740 741 742 743
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
744
	}
745
	static void notify_presence_received(LinphoneCore *lc,  LinphoneFriend *my_friend) {
746 747 748
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
749
			ms_error("cannot attach VM");
750 751
			return;
		}
752 753
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
754 755 756 757
		env->CallVoidMethod(lcData->listener
							,lcData->notifyPresenceReceivedId
							,lcData->core
							,env->NewObject(lcData->friendClass,lcData->friendCtrId,(jlong)my_friend));
758 759 760 761
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
762
	}
763
	static void new_subscription_requested(LinphoneCore *lc,  LinphoneFriend *my_friend, const char* url) {
764 765 766
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
767
			ms_error("cannot attach VM");
768 769
			return;
		}
770 771
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
772 773 774 775 776
		env->CallVoidMethod(lcData->listener
							,lcData->newSubscriptionRequestId
							,lcData->core
							,env->NewObject(lcData->friendClass,lcData->friendCtrId,(jlong)my_friend)
							,url ? env->NewStringUTF(url) : NULL);
777 778 779 780
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
781
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
782 783 784 785
	static void dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
786
			ms_error("cannot attach VM");
Sylvain Berfini's avatar
Sylvain Berfini committed
787 788
			return;
		}
789 790
		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
791 792 793
		env->CallVoidMethod(lcData->listener
							,lcData->dtmfReceivedId
							,lcData->core
794
							,getCall(env,call)
Sylvain Berfini's avatar
Sylvain Berfini committed
795
							,dtmf);
796 797 798 799
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
Sylvain Berfini's avatar
Sylvain Berfini committed
800
	}
801 802 803 804
	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) {
805
			ms_error("cannot attach VM");
806 807
			return;
		}
808 809
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
810 811 812 813
		env->CallVoidMethod(lcData->listener
							,lcData->textReceivedId
							,lcData->core
							,env->NewObject(lcData->chatRoomClass,lcData->chatRoomCtrId,(jlong)room)
jehan's avatar
jehan committed
814
							,env->NewObject(lcData->addressClass,lcData->addressCtrId,(jlong)from)
815
							,message ? env->NewStringUTF(message) : NULL);
816 817 818 819
		if (env->ExceptionCheck()) {
			ms_error("Listener %p raised an exception",lcData->listener);
			env->ExceptionClear();
		}
820
	}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
821
	static void message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
822 823 824 825 826 827
		JNIEnv *env = 0;
		jobject jmsg;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
828
		}
829 830 831 832 833 834 835 836
		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