linphonecore_jni.cc 234 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 87
static jobject create_java_linphone_content(JNIEnv *env, const LinphoneContent *content);

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

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

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

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

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

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


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

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
176 177
// LinphoneCore

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

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

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

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

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

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

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

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

237 238 239 240 241 242 243 244 245
		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;
		}
246
		env->DeleteLocalRef(chatMessageClass);
247 248 249 250 251 252 253 254
	}
	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){
255
		jclass linphoneEventClass = (jclass)env->FindClass("org/linphone/core/LinphoneEventImpl");
256
		jmethodID linphoneEventCtrId = env->GetMethodID(linphoneEventClass,"<init>", "(J)V");
257

258 259 260
		jev=env->NewObject(linphoneEventClass,linphoneEventCtrId,(jlong)linphone_event_ref(lev));
		jev=env->NewGlobalRef(jev);
		linphone_event_set_user_data(lev,jev);
261

262
		env->DeleteLocalRef(linphoneEventClass);
263 264 265 266
	}
	return jev;
}

jehan's avatar
jehan committed
267 268
class LinphoneCoreData {
public:
269
	LinphoneCoreData(JNIEnv *env, jobject lc, LinphoneCoreVTable *vTable, jobject alistener) {
jehan's avatar
jehan committed
270 271
		core = env->NewGlobalRef(lc);
		listener = env->NewGlobalRef(alistener);
272

273
		memset(vTable, 0, sizeof(LinphoneCoreVTable));
274

275
		listenerClass = (jclass)env->NewGlobalRef(env->GetObjectClass(alistener));
276

jehan's avatar
jehan committed
277
		/*displayStatus(LinphoneCore lc,String message);*/
278
		displayStatusId = env->GetMethodID(listenerClass,"displayStatus","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;)V");
279 280 281
		if (displayStatusId) {
			vTable->display_status = displayStatusCb;
		}
282

jehan's avatar
jehan committed
283
		/*void generalState(LinphoneCore lc,int state); */
284 285
		globalStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$GlobalState"));
		globalStateFromIntId = env->GetStaticMethodID(globalStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$GlobalState;");
286
		globalStateId = env->GetMethodID(listenerClass,"globalState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$GlobalState;Ljava/lang/String;)V");
287 288 289
		if (globalStateId) {
			vTable->global_state_changed = globalStateChange;
		}
290

291 292 293
		/*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;");
294
		registrationStateId = env->GetMethodID(listenerClass,"registrationState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneProxyConfig;Lorg/linphone/core/LinphoneCore$RegistrationState;Ljava/lang/String;)V");
295 296 297
		if (registrationStateId) {
			vTable->registration_state_changed = registrationStateChange;
		}
298

299 300 301
		/*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;");
302
		callStateId = env->GetMethodID(listenerClass,"callState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;Ljava/lang/String;)V");
303 304 305
		if (callStateId) {
			vTable->call_state_changed = callStateChange;
		}
306

307
		transferStateId = env->GetMethodID(listenerClass,"transferState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;)V");
308 309 310
		if (transferStateId) {
			vTable->transfer_state_changed = transferStateChanged;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
311

312 313
		/*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");
314 315 316
		if (callStatsUpdatedId) {
			vTable->call_stats_updated = callStatsUpdated;
		}
317

318
		/*callEncryption(LinphoneCore lc, LinphoneCall call, boolean encrypted,String auth_token);*/
319 320 321 322
		callEncryptionChangedId = env->GetMethodID(listenerClass,"callEncryptionChanged","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;ZLjava/lang/String;)V");
		if (callEncryptionChangedId) {
			vTable->call_encryption_changed = callEncryptionChange;
		}
323

jehan's avatar
jehan committed
324 325 326
		/*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;");
327
		ecCalibrationStatusId = env->GetMethodID(listenerClass,"ecCalibrationStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;ILjava/lang/Object;)V");
328

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

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

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

		/*void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from,String message);*/
347
		textReceivedId = env->GetMethodID(listenerClass,"textReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneAddress;Ljava/lang/String;)V");
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 354 355
		if (messageReceivedId) {
			vTable->message_received = message_received;
		}
356

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

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

367 368 369 370
		infoReceivedId = env->GetMethodID(listenerClass,"infoReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneInfoMessage;)V");
		if (infoReceivedId) {
			vTable->info_received = infoReceived;
		}
371

372 373
		subscriptionStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/SubscriptionState"));
		subscriptionStateFromIntId = env->GetStaticMethodID(subscriptionStateClass,"fromInt","(I)Lorg/linphone/core/SubscriptionState;");
374
		subscriptionStateId = env->GetMethodID(listenerClass,"subscriptionStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/SubscriptionState;)V");
375 376 377
		if (subscriptionStateId) {
			vTable->subscription_state_changed = subscriptionStateChanged;
		}
378

379 380
		publishStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/PublishState"));
		publishStateFromIntId = env->GetStaticMethodID(publishStateClass,"fromInt","(I)Lorg/linphone/core/PublishState;");
381
		publishStateId = env->GetMethodID(listenerClass,"publishStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/PublishState;)V");
382 383 384
		if (publishStateId) {
			vTable->publish_state_changed = publishStateChanged;
		}
385

386 387 388 389
		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
390

391 392
		configuringStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$RemoteProvisioningState"));
		configuringStateFromIntId = env->GetStaticMethodID(configuringStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;");
393
		configuringStateId = env->GetMethodID(listenerClass,"configuringStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;Ljava/lang/String;)V");
394 395 396
		if (configuringStateId) {
			vTable->configuring_status = configuringStatus;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
397

398 399 400 401
		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;
		}
402

403 404 405 406 407 408 409 410 411
		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
412

413 414
		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
415
		logCollectionUploadProgressId = env->GetMethodID(listenerClass, "uploadProgressIndication", "(Lorg/linphone/core/LinphoneCore;II)V");
416 417 418 419 420 421 422
		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;
		}
423 424 425

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

427
		proxyClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneProxyConfigImpl"));
428
		proxyCtrId = env->GetMethodID(proxyClass,"<init>", "(Lorg/linphone/core/LinphoneCoreImpl;J)V");
429 430 431

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

435
		chatMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessageImpl"));
436 437 438
		if (chatMessageClass) {
			chatMessageCtrId = env->GetMethodID(chatMessageClass,"<init>", "(J)V");
		}
439

440 441 442 443
		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"));;
444
		friendCtrId = env->GetMethodID(friendClass,"<init>", "(J)V");
445

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

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

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

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

458 459
		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
460 461 462 463 464 465 466
	}

	~LinphoneCoreData() {
		JNIEnv *env = 0;
		jvm->AttachCurrentThread(&env,NULL);
		env->DeleteGlobalRef(core);
		env->DeleteGlobalRef(listener);
467
		env->DeleteGlobalRef(listenerClass);
jehan's avatar
jehan committed
468
		env->DeleteGlobalRef(globalStateClass);
469
		env->DeleteGlobalRef(configuringStateClass);
jehan's avatar
jehan committed
470 471
		env->DeleteGlobalRef(registrationStateClass);
		env->DeleteGlobalRef(callStateClass);
472
		env->DeleteGlobalRef(chatMessageStateClass);
jehan's avatar
jehan committed
473 474
		env->DeleteGlobalRef(proxyClass);
		env->DeleteGlobalRef(callClass);
475
		env->DeleteGlobalRef(chatMessageClass);
476 477
		env->DeleteGlobalRef(chatRoomClass);
		env->DeleteGlobalRef(friendClass);
478
		env->DeleteGlobalRef(infoMessageClass);
479 480 481
		env->DeleteGlobalRef(linphoneEventClass);
		env->DeleteGlobalRef(subscriptionStateClass);
		env->DeleteGlobalRef(subscriptionDirClass);
482
		env->DeleteGlobalRef(logCollectionUploadStateClass);
jehan's avatar
jehan committed
483 484 485 486
	}
	jobject core;
	jobject listener;

487
	jclass listenerClass;
jehan's avatar
jehan committed
488
	jmethodID displayStatusId;
489 490 491
	jmethodID newSubscriptionRequestId;
	jmethodID notifyPresenceReceivedId;
	jmethodID textReceivedId;
492
	jmethodID messageReceivedId;
493
	jmethodID isComposingReceivedId;
Sylvain Berfini's avatar
Sylvain Berfini committed
494
	jmethodID dtmfReceivedId;
495
	jmethodID callStatsUpdatedId;
496
	jmethodID transferStateId;
497
	jmethodID infoReceivedId;
498
	jmethodID subscriptionStateId;
499
	jmethodID authInfoRequestedId;
500
	jmethodID publishStateId;
501
	jmethodID notifyRecvId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
502

503 504 505
	jclass configuringStateClass;
	jmethodID configuringStateId;
	jmethodID configuringStateFromIntId;
506 507 508 509 510 511 512 513 514 515 516 517 518

	jclass globalStateClass;
	jmethodID globalStateId;
	jmethodID globalStateFromIntId;

	jclass registrationStateClass;
	jmethodID registrationStateId;
	jmethodID registrationStateFromIntId;

	jclass callStateClass;
	jmethodID callStateId;
	jmethodID callStateFromIntId;

519 520 521 522 523
	jclass callStatsClass;
	jmethodID callStatsId;
	jmethodID callSetAudioStatsId;
	jmethodID callSetVideoStatsId;

524 525 526
	jclass chatMessageStateClass;
	jmethodID chatMessageStateFromIntId;

527 528
	jmethodID callEncryptionChangedId;

jehan's avatar
jehan committed
529 530 531 532
	jclass ecCalibratorStatusClass;
	jmethodID ecCalibrationStatusId;
	jmethodID ecCalibratorStatusFromIntId;

533 534 535 536 537 538
	jclass proxyClass;
	jmethodID proxyCtrId;

	jclass callClass;
	jmethodID callCtrId;

539 540 541
	jclass chatMessageClass;
	jmethodID chatMessageCtrId;

542 543 544 545 546 547
	jclass chatRoomClass;
	jmethodID chatRoomCtrId;

	jclass friendClass;
	jmethodID friendCtrId;

jehan's avatar
jehan committed
548 549
	jclass addressClass;
	jmethodID addressCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
550

551 552
	jclass infoMessageClass;
	jmethodID infoMessageCtor;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
553

554 555
	jclass linphoneEventClass;
	jmethodID linphoneEventCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
556

557 558
	jclass subscriptionStateClass;
	jmethodID subscriptionStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
559

560 561
	jclass publishStateClass;
	jmethodID publishStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
562

563 564
	jclass subscriptionDirClass;
	jmethodID subscriptionDirFromIntId;
jehan's avatar
jehan committed
565

566
	jmethodID fileTransferProgressIndicationId;
567 568
	jmethodID fileTransferSendId;
	jmethodID fileTransferRecvId;
569

570 571 572 573 574
	jclass logCollectionUploadStateClass;
	jmethodID logCollectionUploadStateId;
	jmethodID logCollectionUploadStateFromIntId;
	jmethodID logCollectionUploadProgressId;

jehan's avatar
jehan committed
575
	LinphoneCoreVTable vTable;
576

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

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