linphonecore_jni.cc 245 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"
Margaux Clerc's avatar
Margaux Clerc committed
32
#include "devices.h"
33
}
Simon Morlat's avatar
Simon Morlat committed
34
#include "mediastreamer2/msjava.h"
35
#include "private.h"
36 37
#include <cpu-features.h>

38 39
#include "lpconfig.h"

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

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

90
static jobject create_java_linphone_content(JNIEnv *env, const LinphoneContent *content);
91
static jobject create_java_linphone_buffer(JNIEnv *env, const LinphoneBuffer *buffer);
92
static LinphoneBuffer* create_c_linphone_buffer_from_java_linphone_buffer(JNIEnv *env, jobject jbuffer);
93

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

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

112
static void linphone_android_ortp_log_handler(OrtpLogLevel lev, const char *fmt, va_list args) {
113 114 115 116
	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
117

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

140 141 142
int dumbMethodForAllowingUsageOfCpuFeaturesFromStaticLibMediastream() {
	return (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM && (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0);
}
143 144 145 146

int dumbMethodForAllowingUsageOfMsAudioDiffFromStaticLibMediastream() {
	return ms_audio_diff(NULL, NULL, NULL, 0, NULL, NULL);
}
jehan's avatar
jehan committed
147 148
#endif /*ANDROID*/

149

jehan's avatar
jehan committed
150 151
JNIEXPORT jint JNICALL  JNI_OnLoad(JavaVM *ajvm, void *reserved)
{
jehan's avatar
jehan committed
152
#ifdef ANDROID
Simon Morlat's avatar
Simon Morlat committed
153
	ms_set_jvm(ajvm);
154
	
jehan's avatar
jehan committed
155 156 157 158 159 160
#endif /*ANDROID*/
	jvm=ajvm;
	return JNI_VERSION_1_2;
}


jehan's avatar
jehan committed
161
//LinphoneFactory
jehan's avatar
jehan committed
162
extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_setDebugMode(JNIEnv*  env
jehan's avatar
jehan committed
163
		,jobject  thiz
164 165
		,jboolean isDebug
		,jstring  jdebugTag) {
jehan's avatar
jehan committed
166
	if (isDebug) {
167
		LogDomain = env->GetStringUTFChars(jdebugTag, NULL);
168
		linphone_core_enable_logs_with_cb(linphone_android_ortp_log_handler);
jehan's avatar
jehan committed
169 170 171 172
	} else {
		linphone_core_disable_logs();
	}
}
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

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
188 189
// LinphoneCore

190 191 192 193 194 195 196
/*
 * returns the java LinphoneProxyConfig associated with a C LinphoneProxyConfig.
**/
jobject getProxy(JNIEnv *env, LinphoneProxyConfig *proxy, jobject core){
	jobject jobj=0;

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

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		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));
			}
		}
215
		env->DeleteLocalRef(proxyClass);
216 217 218 219 220 221 222 223
	}
	return jobj;
}

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

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

227 228 229 230 231 232 233 234 235 236
		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;
		}
237
		env->DeleteLocalRef(callClass);
238 239 240 241 242 243 244 245
	}
	return jobj;
}

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

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

249 250 251 252 253 254 255 256 257
		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;
		}
258
		env->DeleteLocalRef(chatMessageClass);
259 260 261 262
	}
	return jobj;
}

Margaux Clerc's avatar
Margaux Clerc committed
263
jobject getFriend(JNIEnv *env, LinphoneFriend *lfriend){
264 265
	jobject jobj=0;

Margaux Clerc's avatar
Margaux Clerc committed
266
	if (lfriend != NULL){
267 268 269 270 271
		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);

Margaux Clerc's avatar
Margaux Clerc committed
272
		if (up == NULL){
273 274 275 276
			jobj=env->NewObject(friendClass,friendCtrId,(jlong)lfriend);
			linphone_friend_set_user_data(lfriend,(void*)env->NewWeakGlobalRef(jobj));
			linphone_friend_ref(lfriend);
		}else{
Margaux Clerc's avatar
Margaux Clerc committed
277

278 279 280 281 282 283 284 285 286 287 288
			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;
}

289 290 291 292
jobject getEvent(JNIEnv *env, LinphoneEvent *lev){
	if (lev==NULL) return NULL;
	jobject jev=(jobject)linphone_event_get_user_data(lev);
	if (jev==NULL){
293
		jclass linphoneEventClass = (jclass)env->FindClass("org/linphone/core/LinphoneEventImpl");
294
		jmethodID linphoneEventCtrId = env->GetMethodID(linphoneEventClass,"<init>", "(J)V");
295

296 297 298
		jev=env->NewObject(linphoneEventClass,linphoneEventCtrId,(jlong)linphone_event_ref(lev));
		jev=env->NewGlobalRef(jev);
		linphone_event_set_user_data(lev,jev);
299

300
		env->DeleteLocalRef(linphoneEventClass);
301 302 303 304
	}
	return jev;
}

jehan's avatar
jehan committed
305 306
class LinphoneCoreData {
public:
307
	LinphoneCoreData(JNIEnv *env, jobject lc, LinphoneCoreVTable *vTable, jobject alistener) {
jehan's avatar
jehan committed
308 309
		core = env->NewGlobalRef(lc);
		listener = env->NewGlobalRef(alistener);
310

311
		memset(vTable, 0, sizeof(LinphoneCoreVTable));
312

313
		listenerClass = (jclass)env->NewGlobalRef(env->GetObjectClass(alistener));
314

jehan's avatar
jehan committed
315
		/*displayStatus(LinphoneCore lc,String message);*/
316
		displayStatusId = env->GetMethodID(listenerClass,"displayStatus","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;)V");
317 318 319
		if (displayStatusId) {
			vTable->display_status = displayStatusCb;
		}
320

jehan's avatar
jehan committed
321
		/*void generalState(LinphoneCore lc,int state); */
322 323
		globalStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$GlobalState"));
		globalStateFromIntId = env->GetStaticMethodID(globalStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$GlobalState;");
324
		globalStateId = env->GetMethodID(listenerClass,"globalState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$GlobalState;Ljava/lang/String;)V");
325 326 327
		if (globalStateId) {
			vTable->global_state_changed = globalStateChange;
		}
328

329 330 331
		/*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;");
332
		registrationStateId = env->GetMethodID(listenerClass,"registrationState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneProxyConfig;Lorg/linphone/core/LinphoneCore$RegistrationState;Ljava/lang/String;)V");
333 334 335
		if (registrationStateId) {
			vTable->registration_state_changed = registrationStateChange;
		}
336

337 338 339
		/*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;");
340
		callStateId = env->GetMethodID(listenerClass,"callState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;Ljava/lang/String;)V");
341 342 343
		if (callStateId) {
			vTable->call_state_changed = callStateChange;
		}
344

345
		transferStateId = env->GetMethodID(listenerClass,"transferState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;)V");
346 347 348
		if (transferStateId) {
			vTable->transfer_state_changed = transferStateChanged;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
349

350 351
		/*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");
352 353 354
		if (callStatsUpdatedId) {
			vTable->call_stats_updated = callStatsUpdated;
		}
355

356
		/*callEncryption(LinphoneCore lc, LinphoneCall call, boolean encrypted,String auth_token);*/
357 358 359 360
		callEncryptionChangedId = env->GetMethodID(listenerClass,"callEncryptionChanged","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;ZLjava/lang/String;)V");
		if (callEncryptionChangedId) {
			vTable->call_encryption_changed = callEncryptionChange;
		}
361

jehan's avatar
jehan committed
362 363 364
		/*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;");
365
		ecCalibrationStatusId = env->GetMethodID(listenerClass,"ecCalibrationStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;ILjava/lang/Object;)V");
366

367
		/*void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf, String url)*/
368
		newSubscriptionRequestId = env->GetMethodID(listenerClass,"newSubscriptionRequest","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;Ljava/lang/String;)V");
369 370 371
		if (newSubscriptionRequestId) {
			vTable->new_subscription_requested = new_subscription_requested;
		}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
372

373
		authInfoRequestedId = env->GetMethodID(listenerClass,"authInfoRequested","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
374 375 376
		if (authInfoRequestedId) {
			vTable->auth_info_requested = authInfoRequested;
		}
377 378

		/*void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf);*/
379
		notifyPresenceReceivedId = env->GetMethodID(listenerClass,"notifyPresenceReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;)V");
380 381 382
		if (notifyPresenceReceivedId) {
			vTable->notify_presence_received = notify_presence_received;
		}
383

Simon Morlat's avatar
Simon Morlat committed
384
		messageReceivedId = env->GetMethodID(listenerClass,"messageReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneChatMessage;)V");
385 386 387
		if (messageReceivedId) {
			vTable->message_received = message_received;
		}
388

389
		isComposingReceivedId = env->GetMethodID(listenerClass,"isComposingReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;)V");
390 391 392
		if (isComposingReceivedId) {
			vTable->is_composing_received = is_composing_received;
		}
393

Sylvain Berfini's avatar
Sylvain Berfini committed
394
		dtmfReceivedId = env->GetMethodID(listenerClass,"dtmfReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;I)V");
395 396 397
		if (dtmfReceivedId) {
			vTable->dtmf_received = dtmf_received;
		}
398

399 400 401 402
		infoReceivedId = env->GetMethodID(listenerClass,"infoReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneInfoMessage;)V");
		if (infoReceivedId) {
			vTable->info_received = infoReceived;
		}
403

404 405
		subscriptionStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/SubscriptionState"));
		subscriptionStateFromIntId = env->GetStaticMethodID(subscriptionStateClass,"fromInt","(I)Lorg/linphone/core/SubscriptionState;");
406
		subscriptionStateId = env->GetMethodID(listenerClass,"subscriptionStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/SubscriptionState;)V");
407 408 409
		if (subscriptionStateId) {
			vTable->subscription_state_changed = subscriptionStateChanged;
		}
410

411 412
		publishStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/PublishState"));
		publishStateFromIntId = env->GetStaticMethodID(publishStateClass,"fromInt","(I)Lorg/linphone/core/PublishState;");
413
		publishStateId = env->GetMethodID(listenerClass,"publishStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/PublishState;)V");
414 415 416
		if (publishStateId) {
			vTable->publish_state_changed = publishStateChanged;
		}
417

418 419 420 421
		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
422

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

430 431 432 433
		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;
		}
434

435 436 437 438 439 440 441 442 443
		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
444

445 446
		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
447
		logCollectionUploadProgressId = env->GetMethodID(listenerClass, "uploadProgressIndication", "(Lorg/linphone/core/LinphoneCore;II)V");
448 449 450 451 452 453 454
		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;
		}
455 456 457

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

459
		proxyClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneProxyConfigImpl"));
460
		proxyCtrId = env->GetMethodID(proxyClass,"<init>", "(Lorg/linphone/core/LinphoneCoreImpl;J)V");
461 462 463

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

467
		chatMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessageImpl"));
468 469 470
		if (chatMessageClass) {
			chatMessageCtrId = env->GetMethodID(chatMessageClass,"<init>", "(J)V");
		}
471

472 473 474 475
		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"));;
476
		friendCtrId = env->GetMethodID(friendClass,"<init>", "(J)V");
477

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

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

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

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

490 491
		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
492 493 494 495 496 497 498
	}

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

519
	jclass listenerClass;
jehan's avatar
jehan committed
520
	jmethodID displayStatusId;
521 522
	jmethodID newSubscriptionRequestId;
	jmethodID notifyPresenceReceivedId;
523
	jmethodID messageReceivedId;
524
	jmethodID isComposingReceivedId;
Sylvain Berfini's avatar
Sylvain Berfini committed
525
	jmethodID dtmfReceivedId;
526
	jmethodID callStatsUpdatedId;
527
	jmethodID transferStateId;
528
	jmethodID infoReceivedId;
529
	jmethodID subscriptionStateId;
530
	jmethodID authInfoRequestedId;
531
	jmethodID publishStateId;
532
	jmethodID notifyRecvId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
533

534 535 536
	jclass configuringStateClass;
	jmethodID configuringStateId;
	jmethodID configuringStateFromIntId;
537 538 539 540 541 542 543 544 545 546 547 548 549

	jclass globalStateClass;
	jmethodID globalStateId;
	jmethodID globalStateFromIntId;

	jclass registrationStateClass;
	jmethodID registrationStateId;
	jmethodID registrationStateFromIntId;

	jclass callStateClass;
	jmethodID callStateId;
	jmethodID callStateFromIntId;

550 551 552 553 554
	jclass callStatsClass;
	jmethodID callStatsId;
	jmethodID callSetAudioStatsId;
	jmethodID callSetVideoStatsId;

555 556 557
	jclass chatMessageStateClass;
	jmethodID chatMessageStateFromIntId;

558 559
	jmethodID callEncryptionChangedId;

jehan's avatar
jehan committed
560 561 562 563
	jclass ecCalibratorStatusClass;
	jmethodID ecCalibrationStatusId;
	jmethodID ecCalibratorStatusFromIntId;

564 565 566 567 568 569
	jclass proxyClass;
	jmethodID proxyCtrId;

	jclass callClass;
	jmethodID callCtrId;

570 571 572
	jclass chatMessageClass;
	jmethodID chatMessageCtrId;

573 574 575 576 577 578
	jclass chatRoomClass;
	jmethodID chatRoomCtrId;

	jclass friendClass;
	jmethodID friendCtrId;

jehan's avatar
jehan committed
579 580
	jclass addressClass;
	jmethodID addressCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
581

582 583
	jclass infoMessageClass;
	jmethodID infoMessageCtor;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
584

585 586
	jclass linphoneEventClass;
	jmethodID linphoneEventCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
587

588 589
	jclass subscriptionStateClass;
	jmethodID subscriptionStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
590

591 592
	jclass publishStateClass;
	jmethodID publishStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
593

594 595
	jclass subscriptionDirClass;
	jmethodID subscriptionDirFromIntId;
jehan's avatar
jehan committed
596

597
	jmethodID fileTransferProgressIndicationId;
598 599
	jmethodID fileTransferSendId;
	jmethodID fileTransferRecvId;
600

601 602 603 604 605
	jclass logCollectionUploadStateClass;
	jmethodID logCollectionUploadStateId;
	jmethodID logCollectionUploadStateFromIntId;
	jmethodID logCollectionUploadProgressId;

jehan's avatar
jehan committed
606
	LinphoneCoreVTable vTable;
607

jehan's avatar
jehan committed
608 609 610 611
	static void displayStatusCb(LinphoneCore *lc, const char *message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
612
			ms_error("cannot attach VM");
jehan's avatar
jehan committed
613 614
			return;
		}
615 616
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
617 618
		jstring msg = message ? env->NewStringUTF(message) : NULL;
		env->CallVoidMethod(lcData->listener,lcData->displayStatusId,lcData->core,msg);
619
		handle_possible_java_exception(env, lcData->listener);
620 621 622
		if (msg) {
			env->DeleteLocalRef(msg);
		}
jehan's avatar
jehan committed
623
	}
624
	static void authInfoRequested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
625 626 627 628 629 630
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
		}
631 632
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
633 634 635
		jstring r = realm ? env->NewStringUTF(realm) : NULL;
		jstring u = username ? env->NewStringUTF(username) : NULL;
		jstring d = domain ? env->NewStringUTF(domain) : NULL;
636 637 638
		env->CallVoidMethod(lcData->listener,
							lcData->authInfoRequestedId,
							lcData->core,
639 640 641
							r,
							u,
							d);
642
		handle_possible_java_exception(env, lcData->listener);
643 644 645 646 647 648 649 650 651
		if (r) {
			env->DeleteLocalRef(r);
		}
		if (u) {
			env->DeleteLocalRef(u);
		}
		if (d) {
			env->DeleteLocalRef(d);
		}
jehan's avatar
jehan committed
652
	}
653 654 655 656
	static void globalStateChange(LinphoneCore *lc, LinphoneGlobalState gstate,const char* message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
657
			ms_error("cannot attach VM");
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;
663 664 665
		env->CallVoidMethod(lcData->listener
							,lcData->globalStateId
							,lcData->core
jehan's avatar
jehan committed
666
							,env->CallStaticObjectMethod(lcData->globalStateClass,lcData->globalStateFromIntId,(jint)gstate),
667
							msg);
668
		handle_possible_java_exception(env, lcData->listener);
669 670 671
		if (msg) {
			env->DeleteLocalRef(msg);
		}
672
	}
jehan's avatar
jehan committed
673
	static void registrationStateChange(LinphoneCore *lc, LinphoneProxyConfig* proxy,LinphoneRegistrationState state,const char* message) {
jehan's avatar
jehan committed
674 675
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
676
		jobject jproxy;
jehan's avatar
jehan committed
677
		if (result != 0) {
678
			ms_error("cannot attach VM");
jehan's avatar
jehan committed
679 680
			return;
		}
681 682
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
683
		jstring msg = message ? env->NewStringUTF(message) : NULL;
jehan's avatar
jehan committed
684
		env->CallVoidMethod(lcData->listener
685
							,lcData->registrationStateId
jehan's avatar
jehan committed
686
							,lcData->core
687
							,(jproxy=getProxy(env,proxy,lcData->core))
jehan's avatar
jehan committed
688
							,env->CallStaticObjectMethod(lcData->registrationStateClass,lcData->registrationStateFromIntId,(jint)state),
689
							msg);
690
		handle_possible_java_exception(env, lcData->listener);
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
		handle_possible_java_exception(env, lcData->listener);
714
		if (state==LinphoneCallReleased) {
715 716 717
			linphone_call_set_user_pointer(call,NULL);
			env->DeleteGlobalRef(jcall);
		}
718 719 720
		if (msg) {
			env->DeleteLocalRef(msg);
		}
721
	}
722 723 724 725
	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) {
726
			ms_error("cannot attach VM");
727 728
			return;
		}
729 730
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
731 732 733
		env->CallVoidMethod(lcData->listener
							,lcData->callEncryptionChangedId
							,lcData->core
734
							,getCall(env,call)
735 736
							,encrypted
							,authentication_token ? env->NewStringUTF(authentication_token) : NULL);
737
		handle_possible_java_exception(env, lcData->listener);
738
	}
739
	static void notify_presence_received(LinphoneCore *lc,  LinphoneFriend *my_friend) {
740 741 742
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
743
			ms_error("cannot attach VM");
744 745
			return;
		}
746 747
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
748 749 750
		env->CallVoidMethod(lcData->listener
							,lcData->notifyPresenceReceivedId
							,lcData->core
Margaux Clerc's avatar
Margaux Clerc committed
751
							,getFriend(env,my_friend));
752
		handle_possible_java_exception(env, lcData->listener);
753
	}
754
	static void new_subscription_requested(LinphoneCore *lc,  LinphoneFriend *my_friend, const char* url) {
755 756 757
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
758
			ms_error("cannot attach VM");
759 760
			return;
		}
761 762
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
763 764 765
		env->CallVoidMethod(lcData->listener
							,lcData->newSubscriptionRequestId
							,lcData->core
Margaux Clerc's avatar
Margaux Clerc committed
766
							,getFriend(env,my_friend)
767
							,url ? env->NewStringUTF(url) : NULL);
768
		handle_possible_java_exception(env, lcData->listener);
769
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
770 771 772 773
	static void dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
774
			ms_error("cannot attach VM");
Sylvain Berfini's avatar
Sylvain Berfini committed
775 776
			return;
		}
777 778
		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
779 780 781
		env->CallVoidMethod(lcData->listener
							,lcData->dtmfReceivedId
							,lcData->core
782
							,getCall(env,call)
Sylvain Berfini's avatar
Sylvain Berfini committed
783
							,dtmf);
784
		handle_possible_java_exception(env, lcData->listener);
Sylvain Berfini's avatar
Sylvain Berfini committed
785
	}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
786
	static void message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
787 788 789 790 791 792
		JNIEnv *env = 0;
		jobject jmsg;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error(