]> sjero.net Git - linphone/blob - coreapi/linphonecore_jni.cc
Few changes in JNI interface for linphone friends + fix issue in linphone_friend_destroy
[linphone] / coreapi / linphonecore_jni.cc
1 /*
2 linphonecore_jni.cc
3 Copyright (C) 2010  Belledonne Communications, Grenoble, France
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 #include <jni.h>
20 #include "linphonecore_utils.h"
21 #include <ortp/zrtp.h>
22
23 #ifdef TUNNEL_ENABLED
24 #include "linphone_tunnel.h"
25 #endif
26
27 extern "C" {
28 #include "mediastreamer2/mediastream.h"
29 }
30 #include "mediastreamer2/msjava.h"
31 #include "private.h"
32 #include <cpu-features.h>
33
34 #ifdef ANDROID
35 #include <android/log.h>
36 extern "C" void libmsilbc_init();
37 #ifdef HAVE_X264
38 extern "C" void libmsx264_init();
39 #endif
40 #ifdef HAVE_AMR
41 extern "C" void libmsamr_init();
42 #endif
43 #ifdef HAVE_SILK
44 extern "C" void libmssilk_init();
45 #endif
46 #ifdef HAVE_G729
47 extern "C" void libmsbcg729_init();
48 #endif
49 #endif /*ANDROID*/
50
51 static JavaVM *jvm=0;
52
53 #ifdef ANDROID
54 static void linphone_android_log_handler(OrtpLogLevel lev, const char *fmt, va_list args){
55         int prio;
56         char str[4096];
57         char *current;
58         char *next;
59         switch(lev){
60         case ORTP_DEBUG:        prio = ANDROID_LOG_DEBUG;       break;
61         case ORTP_MESSAGE:      prio = ANDROID_LOG_INFO;        break;
62         case ORTP_WARNING:      prio = ANDROID_LOG_WARN;        break;
63         case ORTP_ERROR:        prio = ANDROID_LOG_ERROR;       break;
64         case ORTP_FATAL:        prio = ANDROID_LOG_FATAL;       break;
65         default:                prio = ANDROID_LOG_DEFAULT;     break;
66         }
67         vsnprintf(str, sizeof(str) - 1, fmt, args);
68         str[sizeof(str) - 1] = '\0';
69         if (strlen(str) < 512) {
70                 __android_log_write(prio, LOG_DOMAIN, str);
71         } else {
72                 current = str;
73                 while ((next = strchr(current, '\n')) != NULL) {
74                         *next = '\0';
75                         __android_log_write(prio, LOG_DOMAIN, current);
76                         current = next + 1;
77                 }
78                 __android_log_write(prio, LOG_DOMAIN, current);
79         }
80 }
81
82 int dumbMethodForAllowingUsageOfCpuFeaturesFromStaticLibMediastream() {
83         return (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM && (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0);
84 }
85 #endif /*ANDROID*/
86
87 JNIEXPORT jint JNICALL  JNI_OnLoad(JavaVM *ajvm, void *reserved)
88 {
89 #ifdef ANDROID
90         ms_set_jvm(ajvm);
91 #endif /*ANDROID*/
92         jvm=ajvm;
93         return JNI_VERSION_1_2;
94 }
95
96
97 //LinphoneFactory
98 extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_setDebugMode(JNIEnv*  env
99                 ,jobject  thiz
100                 ,jboolean isDebug) {
101         if (isDebug) {
102                 linphone_core_enable_logs_with_cb(linphone_android_log_handler);
103         } else {
104                 linphone_core_disable_logs();
105         }
106 }
107 // LinphoneCore
108
109 class LinphoneCoreData {
110 public:
111         LinphoneCoreData(JNIEnv*  env, jobject lc,jobject alistener, jobject auserdata) {
112
113                 core = env->NewGlobalRef(lc);
114                 listener = env->NewGlobalRef(alistener);
115                 userdata = auserdata?env->NewGlobalRef(auserdata):0;
116                 memset(&vTable,0,sizeof(vTable));
117                 vTable.show = showInterfaceCb;
118                 vTable.auth_info_requested = authInfoRequested;
119                 vTable.display_status = displayStatusCb;
120                 vTable.display_message = displayMessageCb;
121                 vTable.display_warning = displayMessageCb;
122                 vTable.global_state_changed = globalStateChange;
123                 vTable.registration_state_changed = registrationStateChange;
124                 vTable.call_state_changed = callStateChange;
125                 vTable.call_encryption_changed = callEncryptionChange;
126                 vTable.text_received = text_received;
127                 vTable.message_received = message_received;
128                 vTable.dtmf_received = dtmf_received;
129                 vTable.new_subscription_request = new_subscription_request;
130                 vTable.notify_presence_recv = notify_presence_recv;
131                 vTable.call_stats_updated = callStatsUpdated;
132
133                 listenerClass = (jclass)env->NewGlobalRef(env->GetObjectClass( alistener));
134
135                 /*displayStatus(LinphoneCore lc,String message);*/
136                 displayStatusId = env->GetMethodID(listenerClass,"displayStatus","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;)V");
137
138                 /*void generalState(LinphoneCore lc,int state); */
139                 globalStateId = env->GetMethodID(listenerClass,"globalState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$GlobalState;Ljava/lang/String;)V");
140                 globalStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$GlobalState"));
141                 globalStateFromIntId = env->GetStaticMethodID(globalStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$GlobalState;");
142
143                 /*registrationState(LinphoneCore lc, LinphoneProxyConfig cfg, LinphoneCore.RegistrationState cstate, String smessage);*/
144                 registrationStateId = env->GetMethodID(listenerClass,"registrationState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneProxyConfig;Lorg/linphone/core/LinphoneCore$RegistrationState;Ljava/lang/String;)V");
145                 registrationStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$RegistrationState"));
146                 registrationStateFromIntId = env->GetStaticMethodID(registrationStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$RegistrationState;");
147
148                 /*callState(LinphoneCore lc, LinphoneCall call, LinphoneCall.State cstate,String message);*/
149                 callStateId = env->GetMethodID(listenerClass,"callState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;Ljava/lang/String;)V");
150                 callStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCall$State"));
151                 callStateFromIntId = env->GetStaticMethodID(callStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCall$State;");
152
153                 /*callStatsUpdated(LinphoneCore lc, LinphoneCall call, LinphoneCallStats stats);*/
154                 callStatsUpdatedId = env->GetMethodID(listenerClass, "callStatsUpdated", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCallStats;)V");
155
156                 chatMessageStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessage$State"));
157                 chatMessageStateFromIntId = env->GetStaticMethodID(chatMessageStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneChatMessage$State;");
158
159                 /*callEncryption(LinphoneCore lc, LinphoneCall call, boolean encrypted,String auth_token);*/
160                 callEncryptionChangedId=env->GetMethodID(listenerClass,"callEncryptionChanged","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;ZLjava/lang/String;)V");
161
162                 /*void ecCalibrationStatus(LinphoneCore.EcCalibratorStatus status, int delay_ms, Object data);*/
163                 ecCalibrationStatusId = env->GetMethodID(listenerClass,"ecCalibrationStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;ILjava/lang/Object;)V");
164                 ecCalibratorStatusClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$EcCalibratorStatus"));
165                 ecCalibratorStatusFromIntId = env->GetStaticMethodID(ecCalibratorStatusClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;");
166
167                 /*void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf, String url)*/
168                 newSubscriptionRequestId = env->GetMethodID(listenerClass,"newSubscriptionRequest","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;Ljava/lang/String;)V");
169
170                 /*void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf);*/
171                 notifyPresenceReceivedId = env->GetMethodID(listenerClass,"notifyPresenceReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;)V");
172
173                 /*void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from,String message);*/
174                 textReceivedId = env->GetMethodID(listenerClass,"textReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneAddress;Ljava/lang/String;)V");
175                 messageReceivedId = env->GetMethodID(listenerClass,"messageReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneChatMessage;)V");
176                 dtmfReceivedId = env->GetMethodID(listenerClass,"dtmfReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;I)V");
177
178                 proxyClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneProxyConfigImpl"));
179                 proxyCtrId = env->GetMethodID(proxyClass,"<init>", "(J)V");
180
181                 callClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCallImpl"));
182                 callCtrId = env->GetMethodID(callClass,"<init>", "(J)V");
183
184                 chatMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessageImpl"));
185                 if (chatMessageClass) chatMessageCtrId = env->GetMethodID(chatMessageClass,"<init>", "(J)V");
186
187                 chatRoomClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatRoomImpl"));
188                 chatRoomCtrId = env->GetMethodID(chatRoomClass,"<init>", "(J)V");
189
190                 friendClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneFriendImpl"));;
191                 friendCtrId =env->GetMethodID(friendClass,"<init>", "(J)V");
192
193                 addressClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAddressImpl"));
194                 addressCtrId =env->GetMethodID(addressClass,"<init>", "(J)V");
195
196                 callStatsClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCallStatsImpl"));
197                 callStatsId = env->GetMethodID(callStatsClass, "<init>", "(JJ)V");
198                 callSetAudioStatsId = env->GetMethodID(callClass, "setAudioStats", "(Lorg/linphone/core/LinphoneCallStats;)V");
199                 callSetVideoStatsId = env->GetMethodID(callClass, "setVideoStats", "(Lorg/linphone/core/LinphoneCallStats;)V");
200         }
201
202         ~LinphoneCoreData() {
203                 JNIEnv *env = 0;
204                 jvm->AttachCurrentThread(&env,NULL);
205                 env->DeleteGlobalRef(core);
206                 env->DeleteGlobalRef(listener);
207                 if (userdata) env->DeleteGlobalRef(userdata);
208                 env->DeleteGlobalRef(listenerClass);
209                 env->DeleteGlobalRef(globalStateClass);
210                 env->DeleteGlobalRef(registrationStateClass);
211                 env->DeleteGlobalRef(callStateClass);
212                 env->DeleteGlobalRef(callStatsClass);
213                 env->DeleteGlobalRef(chatMessageStateClass);
214                 env->DeleteGlobalRef(proxyClass);
215                 env->DeleteGlobalRef(callClass);
216                 env->DeleteGlobalRef(chatMessageClass);
217                 env->DeleteGlobalRef(chatRoomClass);
218                 env->DeleteGlobalRef(friendClass);
219
220         }
221         jobject core;
222         jobject listener;
223         jobject userdata;
224
225         jclass listenerClass;
226         jmethodID displayStatusId;
227         jmethodID newSubscriptionRequestId;
228         jmethodID notifyPresenceReceivedId;
229         jmethodID textReceivedId;
230         jmethodID messageReceivedId;
231         jmethodID dtmfReceivedId;
232         jmethodID callStatsUpdatedId;
233
234         jclass globalStateClass;
235         jmethodID globalStateId;
236         jmethodID globalStateFromIntId;
237
238         jclass registrationStateClass;
239         jmethodID registrationStateId;
240         jmethodID registrationStateFromIntId;
241
242         jclass callStateClass;
243         jmethodID callStateId;
244         jmethodID callStateFromIntId;
245
246         jclass callStatsClass;
247         jmethodID callStatsId;
248         jmethodID callSetAudioStatsId;
249         jmethodID callSetVideoStatsId;
250
251         jclass chatMessageStateClass;
252         jmethodID chatMessageStateFromIntId;
253
254         jmethodID callEncryptionChangedId;
255
256         jclass ecCalibratorStatusClass;
257         jmethodID ecCalibrationStatusId;
258         jmethodID ecCalibratorStatusFromIntId;
259
260         jclass proxyClass;
261         jmethodID proxyCtrId;
262
263         jclass callClass;
264         jmethodID callCtrId;
265
266         jclass chatMessageClass;
267         jmethodID chatMessageCtrId;
268
269         jclass chatRoomClass;
270         jmethodID chatRoomCtrId;
271
272         jclass friendClass;
273         jmethodID friendCtrId;
274
275         jclass addressClass;
276         jmethodID addressCtrId;
277
278         LinphoneCoreVTable vTable;
279
280         static void showInterfaceCb(LinphoneCore *lc) {
281
282         }
283         static void byeReceivedCb(LinphoneCore *lc, const char *from) {
284
285         }
286         static void displayStatusCb(LinphoneCore *lc, const char *message) {
287                 JNIEnv *env = 0;
288                 jint result = jvm->AttachCurrentThread(&env,NULL);
289                 if (result != 0) {
290                         ms_error("cannot attach VM\n");
291                         return;
292                 }
293                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
294                 env->CallVoidMethod(lcData->listener,lcData->displayStatusId,lcData->core,message ? env->NewStringUTF(message) : NULL);
295         }
296         static void displayMessageCb(LinphoneCore *lc, const char *message) {
297
298         }
299         static void authInfoRequested(LinphoneCore *lc, const char *realm, const char *username) {
300
301         }
302         static void globalStateChange(LinphoneCore *lc, LinphoneGlobalState gstate,const char* message) {
303                 JNIEnv *env = 0;
304                 jint result = jvm->AttachCurrentThread(&env,NULL);
305                 if (result != 0) {
306                         ms_error("cannot attach VM\n");
307                         return;
308                 }
309                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
310                 env->CallVoidMethod(lcData->listener
311                                                         ,lcData->globalStateId
312                                                         ,lcData->core
313                                                         ,env->CallStaticObjectMethod(lcData->globalStateClass,lcData->globalStateFromIntId,(jint)gstate),
314                                                         message ? env->NewStringUTF(message) : NULL);
315         }
316         static void registrationStateChange(LinphoneCore *lc, LinphoneProxyConfig* proxy,LinphoneRegistrationState state,const char* message) {
317                 JNIEnv *env = 0;
318                 jint result = jvm->AttachCurrentThread(&env,NULL);
319                 if (result != 0) {
320                         ms_error("cannot attach VM\n");
321                         return;
322                 }
323                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
324                 env->CallVoidMethod(lcData->listener
325                                                         ,lcData->registrationStateId
326                                                         ,lcData->core
327                                                         ,env->NewObject(lcData->proxyClass,lcData->proxyCtrId,(jlong)proxy)
328                                                         ,env->CallStaticObjectMethod(lcData->registrationStateClass,lcData->registrationStateFromIntId,(jint)state),
329                                                         message ? env->NewStringUTF(message) : NULL);
330         }
331         jobject getCall(JNIEnv *env , LinphoneCall *call){
332                 jobject jobj=0;
333
334                 if (call!=NULL){
335                         void *up=linphone_call_get_user_pointer(call);
336                         
337                         if (up==NULL){
338                                 jobj=env->NewObject(callClass,callCtrId,(jlong)call);
339                                 jobj=env->NewGlobalRef(jobj);
340                                 linphone_call_set_user_pointer(call,(void*)jobj);
341                                 linphone_call_ref(call);
342                         }else{
343                                 jobj=(jobject)up;
344                         }
345                 }
346                 return jobj;
347         }
348
349         static void callStateChange(LinphoneCore *lc, LinphoneCall* call,LinphoneCallState state,const char* message) {
350                 JNIEnv *env = 0;
351                 jint result = jvm->AttachCurrentThread(&env,NULL);
352                 jobject jcall;
353                 if (result != 0) {
354                         ms_error("cannot attach VM\n");
355                         return;
356                 }
357                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
358                 env->CallVoidMethod(lcData->listener
359                                                         ,lcData->callStateId
360                                                         ,lcData->core
361                                                         ,(jcall=lcData->getCall(env,call))
362                                                         ,env->CallStaticObjectMethod(lcData->callStateClass,lcData->callStateFromIntId,(jint)state),
363                                                         message ? env->NewStringUTF(message) : NULL);
364                 if (state==LinphoneCallReleased){
365                         linphone_call_set_user_pointer(call,NULL);
366                         env->DeleteGlobalRef(jcall);
367                 }
368         }
369         static void callEncryptionChange(LinphoneCore *lc, LinphoneCall* call, bool_t encrypted,const char* authentication_token) {
370                 JNIEnv *env = 0;
371                 jint result = jvm->AttachCurrentThread(&env,NULL);
372                 if (result != 0) {
373                         ms_error("cannot attach VM\n");
374                         return;
375                 }
376                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
377                 env->CallVoidMethod(lcData->listener
378                                                         ,lcData->callEncryptionChangedId
379                                                         ,lcData->core
380                                                         ,lcData->getCall(env,call)
381                                                         ,encrypted
382                                                         ,authentication_token ? env->NewStringUTF(authentication_token) : NULL);
383         }
384         static void notify_presence_recv (LinphoneCore *lc,  LinphoneFriend *my_friend) {
385                 JNIEnv *env = 0;
386                 jint result = jvm->AttachCurrentThread(&env,NULL);
387                 if (result != 0) {
388                         ms_error("cannot attach VM\n");
389                         return;
390                 }
391                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
392                 env->CallVoidMethod(lcData->listener
393                                                         ,lcData->notifyPresenceReceivedId
394                                                         ,lcData->core
395                                                         ,env->NewObject(lcData->friendClass,lcData->friendCtrId,(jlong)my_friend));
396         }
397         static void new_subscription_request (LinphoneCore *lc,  LinphoneFriend *my_friend, const char* url) {
398                 JNIEnv *env = 0;
399                 jint result = jvm->AttachCurrentThread(&env,NULL);
400                 if (result != 0) {
401                         ms_error("cannot attach VM\n");
402                         return;
403                 }
404                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
405                 env->CallVoidMethod(lcData->listener
406                                                         ,lcData->newSubscriptionRequestId
407                                                         ,lcData->core
408                                                         ,env->NewObject(lcData->friendClass,lcData->friendCtrId,(jlong)my_friend)
409                                                         ,url ? env->NewStringUTF(url) : NULL);
410         }
411         static void dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf) {
412                 JNIEnv *env = 0;
413                 jint result = jvm->AttachCurrentThread(&env,NULL);
414                 if (result != 0) {
415                         ms_error("cannot attach VM\n");
416                         return;
417                 }
418                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
419                 env->CallVoidMethod(lcData->listener
420                                                         ,lcData->dtmfReceivedId
421                                                         ,lcData->core
422                                                         ,lcData->getCall(env,call)
423                                                         ,dtmf);
424         }
425         static void text_received(LinphoneCore *lc, LinphoneChatRoom *room, const LinphoneAddress *from, const char *message) {
426                 JNIEnv *env = 0;
427                 jint result = jvm->AttachCurrentThread(&env,NULL);
428                 if (result != 0) {
429                         ms_error("cannot attach VM\n");
430                         return;
431                 }
432                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
433                 env->CallVoidMethod(lcData->listener
434                                                         ,lcData->textReceivedId
435                                                         ,lcData->core
436                                                         ,env->NewObject(lcData->chatRoomClass,lcData->chatRoomCtrId,(jlong)room)
437                                                         ,env->NewObject(lcData->addressClass,lcData->addressCtrId,(jlong)from)
438                                                         ,message ? env->NewStringUTF(message) : NULL);
439         }
440         static void message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
441                         JNIEnv *env = 0;
442                         jint result = jvm->AttachCurrentThread(&env,NULL);
443                         if (result != 0) {
444                                 ms_error("cannot attach VM\n");
445                                 return;
446                         }
447                         LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
448                         env->CallVoidMethod(lcData->listener
449                                                                 ,lcData->messageReceivedId
450                                                                 ,lcData->core
451                                                                 ,env->NewObject(lcData->chatRoomClass,lcData->chatRoomCtrId,(jlong)room)
452                                                                 ,env->NewObject(lcData->chatMessageClass,lcData->chatMessageCtrId,(jlong)msg));
453                 }
454         static void ecCalibrationStatus(LinphoneCore *lc, LinphoneEcCalibratorStatus status, int delay_ms, void *data) {
455                 JNIEnv *env = 0;
456                 jint result = jvm->AttachCurrentThread(&env,NULL);
457                 if (result != 0) {
458                         ms_error("cannot attach VM\n");
459                         return;
460                 }
461                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
462                 env->CallVoidMethod(lcData->listener
463                                                         ,lcData->ecCalibrationStatusId
464                                                         ,lcData->core
465                                                         ,env->CallStaticObjectMethod(lcData->ecCalibratorStatusClass,lcData->ecCalibratorStatusFromIntId,(jint)status)
466                                                         ,delay_ms
467                                                         ,data ? data : NULL);
468                 if (data != NULL &&status !=LinphoneEcCalibratorInProgress ) {
469                         //final state, releasing global ref
470                         env->DeleteGlobalRef((jobject)data);
471                 }
472
473         }
474         static void callStatsUpdated(LinphoneCore *lc, LinphoneCall* call, const LinphoneCallStats *stats) {
475                 JNIEnv *env = 0;
476                 jobject statsobj;
477                 jobject callobj;
478                 jint result = jvm->AttachCurrentThread(&env,NULL);
479                 if (result != 0) {
480                         ms_error("cannot attach VM\n");
481                         return;
482                 }
483                 LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
484                 statsobj = env->NewObject(lcData->callStatsClass, lcData->callStatsId, (jlong)call, (jlong)stats);
485                 callobj = lcData->getCall(env, call);
486                 if (stats->type == LINPHONE_CALL_STATS_AUDIO)
487                         env->CallVoidMethod(callobj, lcData->callSetAudioStatsId, statsobj);
488                 else
489                         env->CallVoidMethod(callobj, lcData->callSetVideoStatsId, statsobj);
490                 env->CallVoidMethod(lcData->listener, lcData->callStatsUpdatedId, lcData->core, callobj, statsobj);
491         }
492
493
494 };
495 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_newLinphoneCore(JNIEnv*  env
496                 ,jobject  thiz
497                 ,jobject jlistener
498                 ,jstring juserConfig
499                 ,jstring jfactoryConfig
500                 ,jobject  juserdata){
501
502         const char* userConfig = juserConfig?env->GetStringUTFChars(juserConfig, NULL):NULL;
503         const char* factoryConfig = jfactoryConfig?env->GetStringUTFChars(jfactoryConfig, NULL):NULL;
504         LinphoneCoreData* ldata = new LinphoneCoreData(env,thiz,jlistener,juserdata);
505
506 #ifdef HAVE_ILBC
507         libmsilbc_init(); // requires an fpu
508 #endif
509 #ifdef HAVE_X264
510         libmsx264_init();
511 #endif
512 #ifdef HAVE_AMR
513         libmsamr_init();
514 #endif
515 #ifdef HAVE_SILK
516         libmssilk_init();
517 #endif
518 #ifdef HAVE_G729
519         libmsbcg729_init();
520 #endif
521         jlong nativePtr = (jlong)linphone_core_new(     &ldata->vTable
522                         ,userConfig
523                         ,factoryConfig
524                         ,ldata);
525         //clear auth info list
526         linphone_core_clear_all_auth_info((LinphoneCore*) nativePtr);
527         //clear existing proxy config
528         linphone_core_clear_proxy_config((LinphoneCore*) nativePtr);
529
530         if (userConfig) env->ReleaseStringUTFChars(juserConfig, userConfig);
531         if (factoryConfig) env->ReleaseStringUTFChars(jfactoryConfig, factoryConfig);
532         return nativePtr;
533 }
534 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_delete(JNIEnv*  env
535                 ,jobject  thiz
536                 ,jlong lc) {
537         LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data((LinphoneCore*)lc);
538         linphone_core_destroy((LinphoneCore*)lc);
539         delete lcData;
540 }
541
542 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_clearProxyConfigs(JNIEnv* env, jobject thiz,jlong lc) {
543         linphone_core_clear_proxy_config((LinphoneCore*)lc);
544 }
545
546 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setDefaultProxyConfig(  JNIEnv*  env
547                 ,jobject  thiz
548                 ,jlong lc
549                 ,jlong pc) {
550         linphone_core_set_default_proxy((LinphoneCore*)lc,(LinphoneProxyConfig*)pc);
551 }
552 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_getDefaultProxyConfig( JNIEnv*  env
553                 ,jobject  thiz
554                 ,jlong lc) {
555         LinphoneProxyConfig *config=0;
556         linphone_core_get_default_proxy((LinphoneCore*)lc,&config);
557         return (jlong)config;
558 }
559
560 extern "C" jlongArray Java_org_linphone_core_LinphoneCoreImpl_getProxyConfigList(JNIEnv* env, jobject thiz, jlong lc) {
561         const MSList* proxies = linphone_core_get_proxy_config_list((LinphoneCore*)lc);
562         int proxyCount = ms_list_size(proxies);
563         jlongArray jProxies = env->NewLongArray(proxyCount);
564         jlong *jInternalArray = env->GetLongArrayElements(jProxies, NULL);
565
566         for (int i = 0; i < proxyCount; i++ ) {
567                 jInternalArray[i] = (unsigned long) (proxies->data);
568                 proxies = proxies->next;
569         }
570
571         env->ReleaseLongArrayElements(jProxies, jInternalArray, 0);
572
573         return jProxies;
574 }
575
576 extern "C" int Java_org_linphone_core_LinphoneCoreImpl_addProxyConfig(  JNIEnv*  env
577                 ,jobject  thiz
578                 ,jobject jproxyCfg
579                 ,jlong lc
580                 ,jlong pc) {
581         LinphoneProxyConfig* proxy = (LinphoneProxyConfig*)pc;
582         linphone_proxy_config_set_user_data(proxy, env->NewGlobalRef(jproxyCfg));
583
584         return linphone_core_add_proxy_config((LinphoneCore*)lc,(LinphoneProxyConfig*)pc);
585 }
586
587 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_clearAuthInfos(JNIEnv* env, jobject thiz,jlong lc) {
588         linphone_core_clear_all_auth_info((LinphoneCore*)lc);
589 }
590
591 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_refreshRegisters(JNIEnv* env, jobject thiz,jlong lc) {
592         linphone_core_refresh_registers((LinphoneCore*)lc);
593 }
594
595 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_addAuthInfo(    JNIEnv*  env
596                 ,jobject  thiz
597                 ,jlong lc
598                 ,jlong pc) {
599         linphone_core_add_auth_info((LinphoneCore*)lc,(LinphoneAuthInfo*)pc);
600 }
601 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_iterate(        JNIEnv*  env
602                 ,jobject  thiz
603                 ,jlong lc) {
604         linphone_core_iterate((LinphoneCore*)lc);
605 }
606 extern "C" jobject Java_org_linphone_core_LinphoneCoreImpl_invite(      JNIEnv*  env
607                 ,jobject  thiz
608                 ,jlong lc
609                 ,jstring juri) {
610         const char* uri = env->GetStringUTFChars(juri, NULL);
611         LinphoneCoreData *lcd=(LinphoneCoreData*)linphone_core_get_user_data((LinphoneCore*)lc);
612         LinphoneCall* lCall = linphone_core_invite((LinphoneCore*)lc,uri);
613         env->ReleaseStringUTFChars(juri, uri);
614         return lcd->getCall(env,lCall);
615 }
616 extern "C" jobject Java_org_linphone_core_LinphoneCoreImpl_inviteAddress(       JNIEnv*  env
617                 ,jobject  thiz
618                 ,jlong lc
619                 ,jlong to) {
620         LinphoneCoreData *lcd=(LinphoneCoreData*)linphone_core_get_user_data((LinphoneCore*)lc);
621         return lcd->getCall(env, linphone_core_invite_address((LinphoneCore*)lc,(LinphoneAddress*)to));
622 }
623
624 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_terminateCall(  JNIEnv*  env
625                 ,jobject  thiz
626                 ,jlong lc
627                 ,jlong call) {
628         linphone_core_terminate_call((LinphoneCore*)lc,(LinphoneCall*)call);
629 }
630
631 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_getRemoteAddress(      JNIEnv*  env
632                 ,jobject  thiz
633                 ,jlong lc) {
634         return (jlong)linphone_core_get_current_call_remote_address((LinphoneCore*)lc);
635 }
636 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isInCall(   JNIEnv*  env
637                 ,jobject  thiz
638                 ,jlong lc) {
639
640         return linphone_core_in_call((LinphoneCore*)lc);
641 }
642 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isInComingInvitePending(    JNIEnv*  env
643                 ,jobject  thiz
644                 ,jlong lc) {
645
646         return linphone_core_inc_invite_pending((LinphoneCore*)lc);
647 }
648 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_acceptCall(     JNIEnv*  env
649                 ,jobject  thiz
650                 ,jlong lc
651                 ,jlong call) {
652
653         linphone_core_accept_call((LinphoneCore*)lc,(LinphoneCall*)call);
654 }
655
656 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_acceptCallWithParams(JNIEnv *env,
657                 jobject thiz,
658                 jlong lc,
659                 jlong call,
660                 jlong params){
661         linphone_core_accept_call_with_params((LinphoneCore*)lc,(LinphoneCall*)call, (LinphoneCallParams*)params);
662 }
663
664 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_acceptCallUpdate(JNIEnv *env,
665                 jobject thiz,
666                 jlong lc,
667                 jlong call,
668                 jlong params){
669         linphone_core_accept_call_update((LinphoneCore*)lc,(LinphoneCall*)call, (LinphoneCallParams*)params);
670 }
671
672 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_deferCallUpdate(JNIEnv *env,
673                 jobject thiz,
674                 jlong lc,
675                 jlong call){
676         linphone_core_defer_call_update((LinphoneCore*)lc,(LinphoneCall*)call);
677 }
678
679 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_getCallLog(    JNIEnv*  env
680                 ,jobject  thiz
681                 ,jlong lc
682                 ,jint position) {
683                 return (jlong)ms_list_nth_data(linphone_core_get_call_logs((LinphoneCore*)lc),position);
684 }
685 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_getNumberOfCallLogs(    JNIEnv*  env
686                 ,jobject  thiz
687                 ,jlong lc) {
688                 return ms_list_size(linphone_core_get_call_logs((LinphoneCore*)lc));
689 }
690 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setNetworkStateReachable(       JNIEnv*  env
691                 ,jobject  thiz
692                 ,jlong lc
693                 ,jboolean isReachable) {
694                 linphone_core_set_network_reachable((LinphoneCore*)lc,isReachable);
695 }
696
697 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isNetworkStateReachable(    JNIEnv*  env
698                 ,jobject  thiz
699                 ,jlong lc) {
700                 return linphone_core_is_network_reachable((LinphoneCore*)lc);
701 }
702
703 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setPlaybackGain(        JNIEnv*  env
704                 ,jobject  thiz
705                 ,jlong lc
706                 ,jfloat gain) {
707                 linphone_core_set_playback_gain_db((LinphoneCore*)lc,gain);
708 }
709
710 extern "C" float Java_org_linphone_core_LinphoneCoreImpl_getPlaybackGain(       JNIEnv*  env
711                 ,jobject  thiz
712                 ,jlong lc) {
713                 return linphone_core_get_playback_gain_db((LinphoneCore*)lc);
714 }
715
716 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_muteMic(        JNIEnv*  env
717                 ,jobject  thiz
718                 ,jlong lc
719                 ,jboolean isMuted) {
720                 linphone_core_mute_mic((LinphoneCore*)lc,isMuted);
721 }
722
723 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_interpretUrl(  JNIEnv*  env
724                 ,jobject  thiz
725                 ,jlong lc
726                 ,jstring jurl) {
727         const char* url = env->GetStringUTFChars(jurl, NULL);
728         jlong result = (jlong)linphone_core_interpret_url((LinphoneCore*)lc,url);
729         env->ReleaseStringUTFChars(jurl, url);
730         return result;
731 }
732 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_sendDtmf(       JNIEnv*  env
733                 ,jobject  thiz
734                 ,jlong lc
735                 ,jchar dtmf) {
736         linphone_core_send_dtmf((LinphoneCore*)lc,dtmf);
737 }
738 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_playDtmf(       JNIEnv*  env
739                 ,jobject  thiz
740                 ,jlong lc
741                 ,jchar dtmf
742                 ,jint duration) {
743         linphone_core_play_dtmf((LinphoneCore*)lc,dtmf,duration);
744 }
745 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_stopDtmf(       JNIEnv*  env
746                 ,jobject  thiz
747                 ,jlong lc) {
748         linphone_core_stop_dtmf((LinphoneCore*)lc);
749 }
750
751 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_getMissedCallsCount(JNIEnv*  env
752                                                                                                                                                 ,jobject  thiz
753                                                                                                                                                 ,jlong lc) {
754         linphone_core_get_missed_calls_count((LinphoneCore*)lc);
755 }
756
757 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_resetMissedCallsCount(JNIEnv*  env
758                                                                                                                                                 ,jobject  thiz
759                                                                                                                                                 ,jlong lc) {
760         linphone_core_reset_missed_calls_count((LinphoneCore*)lc);
761 }
762
763 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_removeCallLog(JNIEnv*  env
764                                                                                                                                                 ,jobject  thiz
765                                                                                                                                                 ,jlong lc, jlong log) {
766         linphone_core_remove_call_log((LinphoneCore*)lc, (LinphoneCallLog*) log);
767 }
768
769 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_clearCallLogs(JNIEnv*  env
770                                                                                                                                                 ,jobject  thiz
771                                                                                                                                                 ,jlong lc) {
772         linphone_core_clear_call_logs((LinphoneCore*)lc);
773 }
774 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isMicMuted( JNIEnv*  env
775                 ,jobject  thiz
776                 ,jlong lc) {
777         return linphone_core_is_mic_muted((LinphoneCore*)lc);
778 }
779 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_findPayloadType(JNIEnv*  env
780                                                                                                                                                         ,jobject  thiz
781                                                                                                                                                         ,jlong lc
782                                                                                                                                                         ,jstring jmime
783                                                                                                                                                         ,jint rate
784                                                                                                                                                         ,jint channels) {
785         const char* mime = env->GetStringUTFChars(jmime, NULL);
786         jlong result = (jlong)linphone_core_find_payload_type((LinphoneCore*)lc,mime,rate,channels);
787         env->ReleaseStringUTFChars(jmime, mime);
788         return result;
789 }
790 extern "C" jlongArray Java_org_linphone_core_LinphoneCoreImpl_listVideoPayloadTypes(JNIEnv*  env
791                                                                                                                                                         ,jobject  thiz
792                                                                                                                                                         ,jlong lc) {
793         const MSList* codecs = linphone_core_get_video_codecs((LinphoneCore*)lc);
794         int codecsCount = ms_list_size(codecs);
795         jlongArray jCodecs = env->NewLongArray(codecsCount);
796         jlong *jInternalArray = env->GetLongArrayElements(jCodecs, NULL);
797
798         for (int i = 0; i < codecsCount; i++ ) {
799                 jInternalArray[i] = (unsigned long) (codecs->data);
800                 codecs = codecs->next;
801         }
802
803         env->ReleaseLongArrayElements(jCodecs, jInternalArray, 0);
804
805         return jCodecs;
806 }
807
808 extern "C" jlongArray Java_org_linphone_core_LinphoneCoreImpl_listAudioPayloadTypes(JNIEnv*  env
809                                                                                                                                                         ,jobject  thiz
810                                                                                                                                                         ,jlong lc) {
811         const MSList* codecs = linphone_core_get_audio_codecs((LinphoneCore*)lc);
812         int codecsCount = ms_list_size(codecs);
813         jlongArray jCodecs = env->NewLongArray(codecsCount);
814         jlong *jInternalArray = env->GetLongArrayElements(jCodecs, NULL);
815
816         for (int i = 0; i < codecsCount; i++ ) {
817                 jInternalArray[i] = (unsigned long) (codecs->data);
818                 codecs = codecs->next;
819         }
820
821         env->ReleaseLongArrayElements(jCodecs, jInternalArray, 0);
822
823         return jCodecs;
824 }
825
826 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_enablePayloadType(JNIEnv*  env
827                                                                                                                                                         ,jobject  thiz
828                                                                                                                                                         ,jlong lc
829                                                                                                                                                         ,jlong pt
830                                                                                                                                                         ,jboolean enable) {
831         return linphone_core_enable_payload_type((LinphoneCore*)lc,(PayloadType*)pt,enable);
832 }
833 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_enableEchoCancellation(JNIEnv*  env
834                                                                                                                                                         ,jobject  thiz
835                                                                                                                                                         ,jlong lc
836                                                                                                                                                         ,jboolean enable) {
837         linphone_core_enable_echo_cancellation((LinphoneCore*)lc,enable);
838 }
839 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_enableEchoLimiter(JNIEnv*  env
840                                                                                                                                                         ,jobject  thiz
841                                                                                                                                                         ,jlong lc
842                                                                                                                                                         ,jboolean enable) {
843         linphone_core_enable_echo_limiter((LinphoneCore*)lc,enable);
844 }
845 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isEchoCancellationEnabled(JNIEnv*  env
846                                                                                                                                                         ,jobject  thiz
847                                                                                                                                                         ,jlong lc
848                                                                                                                                                         ) {
849         return linphone_core_echo_cancellation_enabled((LinphoneCore*)lc);
850 }
851
852 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isEchoLimiterEnabled(JNIEnv*  env
853                                                                                                                                                         ,jobject  thiz
854                                                                                                                                                         ,jlong lc
855                                                                                                                                                         ) {
856         return linphone_core_echo_limiter_enabled((LinphoneCore*)lc);
857 }
858
859 extern "C" jobject Java_org_linphone_core_LinphoneCoreImpl_getCurrentCall(JNIEnv*  env
860                                                                                                                                                         ,jobject  thiz
861                                                                                                                                                         ,jlong lc
862                                                                                                                                                         ) {
863         LinphoneCoreData *lcdata=(LinphoneCoreData*)linphone_core_get_user_data((LinphoneCore*)lc);
864         
865         return lcdata->getCall(env,linphone_core_get_current_call((LinphoneCore*)lc));
866 }
867 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_addFriend(JNIEnv*  env
868                                                                                                                                                         ,jobject  thiz
869                                                                                                                                                         ,jlong lc
870                                                                                                                                                         ,jlong aFriend
871                                                                                                                                                         ) {
872         linphone_core_add_friend((LinphoneCore*)lc,(LinphoneFriend*)aFriend);
873 }
874 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setPresenceInfo(JNIEnv*  env
875                                                                                                                                                         ,jobject  thiz
876                                                                                                                                                         ,jlong lc
877                                                                                                                                                         ,jint minute_away
878                                                                                                                                                         ,jstring jalternative_contact
879                                                                                                                                                         ,jint status) {
880         const char* alternative_contact = jalternative_contact?env->GetStringUTFChars(jalternative_contact, NULL):NULL;
881         linphone_core_set_presence_info((LinphoneCore*)lc,minute_away,alternative_contact,(LinphoneOnlineStatus)status);
882         if (alternative_contact) env->ReleaseStringUTFChars(jalternative_contact, alternative_contact);
883 }
884
885 extern "C" long Java_org_linphone_core_LinphoneCoreImpl_createChatRoom(JNIEnv*  env
886                                                                                                                                                         ,jobject  thiz
887                                                                                                                                                         ,jlong lc
888                                                                                                                                                         ,jstring jto) {
889
890         const char* to = env->GetStringUTFChars(jto, NULL);
891         LinphoneChatRoom* lResult = linphone_core_create_chat_room((LinphoneCore*)lc,to);
892         env->ReleaseStringUTFChars(jto, to);
893         return (long)lResult;
894 }
895
896 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_enableVideo(JNIEnv*  env
897                                                                                                                                                         ,jobject  thiz
898                                                                                                                                                         ,jlong lc
899                                                                                                                                                         ,jboolean vcap_enabled
900                                                                                                                                                         ,jboolean display_enabled) {
901         linphone_core_enable_video((LinphoneCore*)lc, vcap_enabled,display_enabled);
902
903 }
904 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isVideoEnabled(JNIEnv*  env
905                                                                                                                                                         ,jobject  thiz
906                                                                                                                                                         ,jlong lc) {
907         return linphone_core_video_enabled((LinphoneCore*)lc);
908 }
909 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setPlayFile(JNIEnv*  env
910                                                                                                                                                         ,jobject  thiz
911                                                                                                                                                         ,jlong lc
912                                                                                                                                                         ,jstring jpath) {
913         const char* path = jpath?env->GetStringUTFChars(jpath, NULL):NULL;
914         linphone_core_set_play_file((LinphoneCore*)lc,path);
915         if (path) env->ReleaseStringUTFChars(jpath, path);
916 }
917 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setRing(JNIEnv*  env
918                                                                                                                                                         ,jobject  thiz
919                                                                                                                                                         ,jlong lc
920                                                                                                                                                         ,jstring jpath) {
921         const char* path = jpath?env->GetStringUTFChars(jpath, NULL):NULL;
922         linphone_core_set_ring((LinphoneCore*)lc,path);
923         if (path) env->ReleaseStringUTFChars(jpath, path);
924 }
925 extern "C" jstring Java_org_linphone_core_LinphoneCoreImpl_getRing(JNIEnv*  env
926                                                                                                                                                         ,jobject  thiz
927                                                                                                                                                         ,jlong lc
928                                                                                                                                                         ) {
929         const char* path = linphone_core_get_ring((LinphoneCore*)lc);
930         if (path) {
931                 return env->NewStringUTF(path);
932         } else {
933                 return NULL;
934         }
935 }
936 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setRootCA(JNIEnv*  env
937                                                                                                                                                         ,jobject  thiz
938                                                                                                                                                         ,jlong lc
939                                                                                                                                                         ,jstring jpath) {
940         const char* path = jpath?env->GetStringUTFChars(jpath, NULL):NULL;
941         linphone_core_set_root_ca((LinphoneCore*)lc,path);
942         if (path) env->ReleaseStringUTFChars(jpath, path);
943 }
944 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_enableKeepAlive(JNIEnv*  env
945                                                                                                                                 ,jobject  thiz
946                                                                                                                                 ,jlong lc
947                                                                                                                                 ,jboolean enable) {
948         linphone_core_enable_keep_alive((LinphoneCore*)lc,enable);
949
950 }
951 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isKeepAliveEnabled(JNIEnv*  env
952                                                                                                                                 ,jobject  thiz
953                                                                                                                                 ,jlong lc) {
954         return linphone_core_keep_alive_enabled((LinphoneCore*)lc);
955
956 }
957 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_startEchoCalibration(JNIEnv*  env
958                                                                                                                                                                 ,jobject  thiz
959                                                                                                                                                                 ,jlong lc
960                                                                                                                                                                 ,jobject data) {
961         return linphone_core_start_echo_calibration((LinphoneCore*)lc
962                                                                                                         , LinphoneCoreData::ecCalibrationStatus
963                                                                                                         , data?env->NewGlobalRef(data):NULL);
964
965 }
966
967 extern "C" int Java_org_linphone_core_LinphoneCoreImpl_getMediaEncryption(JNIEnv*  env
968                                                                                                                                                         ,jobject  thiz
969                                                                                                                                                         ,jlong lc
970                                                                                                                                                         ) {
971         return (int)linphone_core_get_media_encryption((LinphoneCore*)lc);
972 }
973
974 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setMediaEncryption(JNIEnv*  env
975                                                                                                                                                         ,jobject  thiz
976                                                                                                                                                         ,jlong lc
977                                                                                                                                                         ,int menc) {
978         linphone_core_set_media_encryption((LinphoneCore*)lc,(LinphoneMediaEncryption)menc);
979 }
980
981 extern "C" long Java_org_linphone_core_LinphoneCallParamsImpl_getUsedAudioCodec(JNIEnv *env, jobject thiz, jlong cp) {
982         return (long)linphone_call_params_get_used_audio_codec((LinphoneCallParams *)cp);
983 }
984
985 extern "C" long Java_org_linphone_core_LinphoneCallParamsImpl_getUsedVideoCodec(JNIEnv *env, jobject thiz, jlong cp) {
986         return (long)linphone_call_params_get_used_video_codec((LinphoneCallParams *)cp);
987 }
988
989 extern "C" int Java_org_linphone_core_LinphoneCallParamsImpl_getMediaEncryption(JNIEnv*  env
990                                                                                                                                                         ,jobject  thiz
991                                                                                                                                                         ,jlong cp
992                                                                                                                                                         ) {
993         return (int)linphone_call_params_get_media_encryption((LinphoneCallParams*)cp);
994 }
995
996 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_mediaEncryptionSupported(JNIEnv*  env
997                                                                                                                                                         ,jobject  thiz
998                                                                                                                                                         ,jlong lc, jint menc
999                                                                                                                                                         ) {
1000         return linphone_core_media_encryption_supported((LinphoneCore*)lc,(LinphoneMediaEncryption)menc);
1001 }
1002
1003 extern "C" void Java_org_linphone_core_LinphoneCallParamsImpl_setMediaEncryption(JNIEnv*  env
1004                                                                                                                                                         ,jobject  thiz
1005                                                                                                                                                         ,jlong cp
1006                                                                                                                                                         ,int jmenc) {
1007         linphone_call_params_set_media_encryption((LinphoneCallParams*)cp,(LinphoneMediaEncryption)jmenc);
1008 }
1009
1010 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_getMediaEncryptionMandatory(JNIEnv*  env
1011                                                                                                                                                         ,jobject  thiz
1012                                                                                                                                                         ,jlong lc
1013                                                                                                                                                         ) {
1014         return linphone_core_is_media_encryption_mandatory((LinphoneCore*)lc);
1015 }
1016
1017 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setMediaEncryptionMandatory(JNIEnv*  env
1018                                                                                                                                                         ,jobject  thiz
1019                                                                                                                                                         ,jlong lc
1020                                                                                                                                                         , jboolean yesno
1021                                                                                                                                                         ) {
1022         linphone_core_set_media_encryption_mandatory((LinphoneCore*)lc, yesno);
1023 }
1024
1025 //ProxyConfig
1026
1027 extern "C" jlong Java_org_linphone_core_LinphoneProxyConfigImpl_newLinphoneProxyConfig(JNIEnv*  env,jobject  thiz) {
1028         LinphoneProxyConfig* proxy = linphone_proxy_config_new();
1029         return  (jlong) proxy;
1030 }
1031
1032 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_delete(JNIEnv*  env,jobject  thiz,jlong ptr) {
1033         linphone_proxy_config_destroy((LinphoneProxyConfig*)ptr);
1034 }
1035 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_setIdentity(JNIEnv* env,jobject thiz,jlong proxyCfg,jstring jidentity) {
1036         const char* identity = env->GetStringUTFChars(jidentity, NULL);
1037         linphone_proxy_config_set_identity((LinphoneProxyConfig*)proxyCfg,identity);
1038         env->ReleaseStringUTFChars(jidentity, identity);
1039 }
1040 extern "C" jstring Java_org_linphone_core_LinphoneProxyConfigImpl_getIdentity(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1041         const char* identity = linphone_proxy_config_get_identity((LinphoneProxyConfig*)proxyCfg);
1042         if (identity) {
1043                 return env->NewStringUTF(identity);
1044         } else {
1045                 return NULL;
1046         }
1047 }
1048 extern "C" int Java_org_linphone_core_LinphoneProxyConfigImpl_setProxy(JNIEnv* env,jobject thiz,jlong proxyCfg,jstring jproxy) {
1049         const char* proxy = env->GetStringUTFChars(jproxy, NULL);
1050         int err=linphone_proxy_config_set_server_addr((LinphoneProxyConfig*)proxyCfg,proxy);
1051         env->ReleaseStringUTFChars(jproxy, proxy);
1052         return err;
1053 }
1054 extern "C" jstring Java_org_linphone_core_LinphoneProxyConfigImpl_getProxy(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1055         const char* proxy = linphone_proxy_config_get_addr((LinphoneProxyConfig*)proxyCfg);
1056         if (proxy) {
1057                 return env->NewStringUTF(proxy);
1058         } else {
1059                 return NULL;
1060         }
1061 }
1062 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_setContactParameters(JNIEnv* env,jobject thiz,jlong proxyCfg,jstring jparams) {
1063         const char* params = env->GetStringUTFChars(jparams, NULL);
1064         linphone_proxy_config_set_contact_parameters((LinphoneProxyConfig*)proxyCfg, params);
1065         env->ReleaseStringUTFChars(jparams, params);
1066 }
1067 extern "C" int Java_org_linphone_core_LinphoneProxyConfigImpl_setRoute(JNIEnv* env,jobject thiz,jlong proxyCfg,jstring jroute) {
1068         if (jroute != NULL) {
1069                 const char* route = env->GetStringUTFChars(jroute, NULL);
1070                 int err=linphone_proxy_config_set_route((LinphoneProxyConfig*)proxyCfg,route);
1071                 env->ReleaseStringUTFChars(jroute, route);
1072                 return err;
1073         } else {
1074                 return linphone_proxy_config_set_route((LinphoneProxyConfig*)proxyCfg,NULL);
1075         }
1076 }
1077 extern "C" jstring Java_org_linphone_core_LinphoneProxyConfigImpl_getRoute(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1078         const char* route = linphone_proxy_config_get_route((LinphoneProxyConfig*)proxyCfg);
1079         if (route) {
1080                 return env->NewStringUTF(route);
1081         } else {
1082                 return NULL;
1083         }
1084 }
1085
1086 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_enableRegister(JNIEnv* env,jobject thiz,jlong proxyCfg,jboolean enableRegister) {
1087         linphone_proxy_config_enable_register((LinphoneProxyConfig*)proxyCfg,enableRegister);
1088 }
1089 extern "C" jboolean Java_org_linphone_core_LinphoneProxyConfigImpl_isRegistered(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1090         return linphone_proxy_config_is_registered((LinphoneProxyConfig*)proxyCfg);
1091 }
1092 extern "C" jboolean Java_org_linphone_core_LinphoneProxyConfigImpl_isRegisterEnabled(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1093         return linphone_proxy_config_register_enabled((LinphoneProxyConfig*)proxyCfg);
1094 }
1095 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_edit(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1096         linphone_proxy_config_edit((LinphoneProxyConfig*)proxyCfg);
1097 }
1098 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_done(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1099         linphone_proxy_config_done((LinphoneProxyConfig*)proxyCfg);
1100 }
1101 extern "C" jstring Java_org_linphone_core_LinphoneProxyConfigImpl_normalizePhoneNumber(JNIEnv* env,jobject thiz,jlong proxyCfg,jstring jnumber) {
1102         if (jnumber == 0) {
1103                 ms_error("cannot normalized null number");
1104         }
1105         const char* number = env->GetStringUTFChars(jnumber, NULL);
1106         int len = env->GetStringLength(jnumber);
1107         if (len == 0) {
1108                 ms_warning("cannot normalize empty number");
1109                 return jnumber;
1110         }
1111         char targetBuff[2*len];// returned number can be greater than origin (specially in case of prefix insertion
1112         linphone_proxy_config_normalize_number((LinphoneProxyConfig*)proxyCfg,number,targetBuff,sizeof(targetBuff));
1113         jstring normalizedNumber = env->NewStringUTF(targetBuff);
1114         env->ReleaseStringUTFChars(jnumber, number);
1115         return normalizedNumber;
1116 }
1117 extern "C" jint Java_org_linphone_core_LinphoneProxyConfigImpl_lookupCCCFromIso(JNIEnv* env, jobject thiz, jlong proxyCfg, jstring jiso) {
1118         const char* iso = env->GetStringUTFChars(jiso, NULL);
1119         int prefix = linphone_dial_plan_lookup_ccc_from_iso(iso);
1120         env->ReleaseStringUTFChars(jiso, iso);
1121         return (jint) prefix;
1122 }
1123 extern "C" jstring Java_org_linphone_core_LinphoneProxyConfigImpl_getDomain(JNIEnv* env
1124                                                                                                                                                         ,jobject thiz
1125                                                                                                                                                         ,jlong proxyCfg) {
1126         const char* domain = linphone_proxy_config_get_domain((LinphoneProxyConfig*)proxyCfg);
1127         if (domain) {
1128                 return env->NewStringUTF(domain);
1129         } else {
1130                 return NULL;
1131         }
1132 }
1133 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_setDialEscapePlus(JNIEnv* env,jobject thiz,jlong proxyCfg,jboolean value) {
1134         linphone_proxy_config_set_dial_escape_plus((LinphoneProxyConfig*)proxyCfg,value);
1135 }
1136
1137 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_setDialPrefix(JNIEnv* env
1138                                                                                                                                         ,jobject thiz
1139                                                                                                                                         ,jlong proxyCfg
1140                                                                                                                                         ,jstring jprefix) {
1141         const char* prefix = env->GetStringUTFChars(jprefix, NULL);
1142         linphone_proxy_config_set_dial_prefix((LinphoneProxyConfig*)proxyCfg,prefix);
1143         env->ReleaseStringUTFChars(jprefix, prefix);
1144 }
1145 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_enablePublish(JNIEnv* env
1146                                                                                                                                                                 ,jobject thiz
1147                                                                                                                                                                 ,jlong proxyCfg
1148                                                                                                                                                                 ,jboolean val) {
1149         linphone_proxy_config_enable_publish((LinphoneProxyConfig*)proxyCfg,val);
1150 }
1151 extern "C" jboolean Java_org_linphone_core_LinphoneProxyConfigImpl_publishEnabled(JNIEnv* env,jobject thiz,jlong proxyCfg) {
1152         return linphone_proxy_config_publish_enabled((LinphoneProxyConfig*)proxyCfg);
1153 }
1154
1155 //Auth Info
1156
1157 extern "C" jlong Java_org_linphone_core_LinphoneAuthInfoImpl_newLinphoneAuthInfo(JNIEnv* env
1158                 , jobject thiz
1159                 , jstring jusername
1160                 , jstring juserid
1161                 , jstring jpassword
1162                 , jstring jha1
1163                 , jstring jrealm) {
1164
1165         const char* username = env->GetStringUTFChars(jusername, NULL);
1166         const char* userid = env->GetStringUTFChars(juserid, NULL);
1167         const char* password = env->GetStringUTFChars(jpassword, NULL);
1168         const char* ha1 = env->GetStringUTFChars(jha1, NULL);
1169         const char* realm = env->GetStringUTFChars(jrealm, NULL);
1170         jlong auth = (jlong)linphone_auth_info_new(username,userid,password,ha1,realm);
1171
1172         env->ReleaseStringUTFChars(jusername, username);
1173         env->ReleaseStringUTFChars(juserid, userid);
1174         env->ReleaseStringUTFChars(jpassword, password);
1175         env->ReleaseStringUTFChars(jha1, ha1);
1176         env->ReleaseStringUTFChars(jrealm, realm);
1177         return auth;
1178
1179 }
1180 extern "C" void Java_org_linphone_core_LinphoneAuthInfoImpl_delete(JNIEnv* env
1181                 , jobject thiz
1182                 , jlong ptr) {
1183         linphone_auth_info_destroy((LinphoneAuthInfo*)ptr);
1184 }
1185
1186 //LinphoneAddress
1187
1188 extern "C" jlong Java_org_linphone_core_LinphoneAddressImpl_newLinphoneAddressImpl(JNIEnv*  env
1189                                                                                                                                                                         ,jobject  thiz
1190                                                                                                                                                                         ,jstring juri
1191                                                                                                                                                                         ,jstring jdisplayName) {
1192         const char* uri = env->GetStringUTFChars(juri, NULL);
1193         LinphoneAddress* address = linphone_address_new(uri);
1194         if (jdisplayName && address) {
1195                 const char* displayName = env->GetStringUTFChars(jdisplayName, NULL);
1196                 linphone_address_set_display_name(address,displayName);
1197                 env->ReleaseStringUTFChars(jdisplayName, displayName);
1198         }
1199         env->ReleaseStringUTFChars(juri, uri);
1200
1201         return  (jlong) address;
1202 }
1203 extern "C" void Java_org_linphone_core_LinphoneAddressImpl_delete(JNIEnv*  env
1204                                                                                                                                                 ,jobject  thiz
1205                                                                                                                                                 ,jlong ptr) {
1206         linphone_address_destroy((LinphoneAddress*)ptr);
1207 }
1208
1209 extern "C" jstring Java_org_linphone_core_LinphoneAddressImpl_getDisplayName(JNIEnv*  env
1210                                                                                                                                                 ,jobject  thiz
1211                                                                                                                                                 ,jlong ptr) {
1212         const char* displayName = linphone_address_get_display_name((LinphoneAddress*)ptr);
1213         if (displayName) {
1214                 return env->NewStringUTF(displayName);
1215         } else {
1216                 return 0;
1217         }
1218 }
1219 extern "C" jstring Java_org_linphone_core_LinphoneAddressImpl_getUserName(JNIEnv*  env
1220                                                                                                                                                 ,jobject  thiz
1221                                                                                                                                                 ,jlong ptr) {
1222         const char* userName = linphone_address_get_username((LinphoneAddress*)ptr);
1223         if (userName) {
1224                 return env->NewStringUTF(userName);
1225         } else {
1226                 return 0;
1227         }
1228 }
1229 extern "C" jstring Java_org_linphone_core_LinphoneAddressImpl_getDomain(JNIEnv*  env
1230                                                                                                                                                 ,jobject  thiz
1231                                                                                                                                                 ,jlong ptr) {
1232         const char* domain = linphone_address_get_domain((LinphoneAddress*)ptr);
1233         if (domain) {
1234                 return env->NewStringUTF(domain);
1235         } else {
1236                 return 0;
1237         }
1238 }
1239
1240 extern "C" jstring Java_org_linphone_core_LinphoneAddressImpl_toString(JNIEnv*  env
1241                                                                                                                                                 ,jobject  thiz
1242                                                                                                                                                 ,jlong ptr) {
1243         char* uri = linphone_address_as_string((LinphoneAddress*)ptr);
1244         jstring juri =env->NewStringUTF(uri);
1245         ms_free(uri);
1246         return juri;
1247 }
1248 extern "C" jstring Java_org_linphone_core_LinphoneAddressImpl_toUri(JNIEnv*  env
1249                                                                                                                                                 ,jobject  thiz
1250                                                                                                                                                 ,jlong ptr) {
1251         char* uri = linphone_address_as_string_uri_only((LinphoneAddress*)ptr);
1252         jstring juri =env->NewStringUTF(uri);
1253         ms_free(uri);
1254         return juri;
1255 }
1256 extern "C" void Java_org_linphone_core_LinphoneAddressImpl_setDisplayName(JNIEnv*  env
1257                                                                                                                                                 ,jobject  thiz
1258                                                                                                                                                 ,jlong address
1259                                                                                                                                                 ,jstring jdisplayName) {
1260         const char* displayName = jdisplayName!= NULL?env->GetStringUTFChars(jdisplayName, NULL):NULL;
1261         linphone_address_set_display_name((LinphoneAddress*)address,displayName);
1262         if (displayName != NULL) env->ReleaseStringUTFChars(jdisplayName, displayName);
1263 }
1264
1265
1266 //CallLog
1267 extern "C" jlong Java_org_linphone_core_LinphoneCallLogImpl_getFrom(JNIEnv*  env
1268                                                                                                                                                 ,jobject  thiz
1269                                                                                                                                                 ,jlong ptr) {
1270         return (jlong)((LinphoneCallLog*)ptr)->from;
1271 }
1272 extern "C" int Java_org_linphone_core_LinphoneCallLogImpl_getStatus(JNIEnv*  env
1273                                                                                                                                                 ,jobject  thiz
1274                                                                                                                                                 ,jlong ptr) {
1275         return (jlong)((LinphoneCallLog*)ptr)->status;
1276 }
1277 extern "C" jlong Java_org_linphone_core_LinphoneCallLogImpl_getTo(JNIEnv*  env
1278                                                                                                                                                 ,jobject  thiz
1279                                                                                                                                                 ,jlong ptr) {
1280         return (jlong)((LinphoneCallLog*)ptr)->to;
1281 }
1282 extern "C" jboolean Java_org_linphone_core_LinphoneCallLogImpl_isIncoming(JNIEnv*  env
1283                                                                                                                                                 ,jobject  thiz
1284                                                                                                                                                 ,jlong ptr) {
1285         return ((LinphoneCallLog*)ptr)->dir==LinphoneCallIncoming?JNI_TRUE:JNI_FALSE;
1286 }
1287 extern "C" jstring Java_org_linphone_core_LinphoneCallLogImpl_getStartDate(JNIEnv*  env
1288                                                                                                                                                 ,jobject  thiz
1289                                                                                                                                                 ,jlong ptr) {
1290         jstring jvalue =env->NewStringUTF(((LinphoneCallLog*)ptr)->start_date);
1291         return jvalue;
1292 }
1293 extern "C" jint Java_org_linphone_core_LinphoneCallLogImpl_getCallDuration(JNIEnv*  env
1294                                                                                                                                                 ,jobject  thiz
1295                                                                                                                                                 ,jlong ptr) {
1296         return ((LinphoneCallLog*)ptr)->duration;
1297 }
1298
1299 /* CallStats */
1300 extern "C" int Java_org_linphone_core_LinphoneCallStatsImpl_getMediaType(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1301         return (int)((LinphoneCallStats *)stats_ptr)->type;
1302 }
1303 extern "C" int Java_org_linphone_core_LinphoneCallStatsImpl_getIceState(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1304         return (int)((LinphoneCallStats *)stats_ptr)->ice_state;
1305 }
1306 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getDownloadBandwidth(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1307         return (jfloat)((LinphoneCallStats *)stats_ptr)->download_bandwidth;
1308 }
1309 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getUploadBandwidth(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1310         return (jfloat)((LinphoneCallStats *)stats_ptr)->upload_bandwidth;
1311 }
1312 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getSenderLossRate(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1313         const LinphoneCallStats *stats = (LinphoneCallStats *)stats_ptr;
1314         const report_block_t *srb = NULL;
1315
1316         if (!stats || !stats->sent_rtcp)
1317                 return (jfloat)0.0;
1318         /* Perform msgpullup() to prevent crashes in rtcp_is_SR() or rtcp_is_RR() if the RTCP packet is composed of several mblk_t structure */
1319         if (stats->sent_rtcp->b_cont != NULL)
1320                 msgpullup(stats->sent_rtcp, -1);
1321         if (rtcp_is_SR(stats->sent_rtcp))
1322                 srb = rtcp_SR_get_report_block(stats->sent_rtcp, 0);
1323         else if (rtcp_is_RR(stats->sent_rtcp))
1324                 srb = rtcp_RR_get_report_block(stats->sent_rtcp, 0);
1325         if (!srb)
1326                 return (jfloat)0.0;
1327         return (jfloat)(100.0 * report_block_get_fraction_lost(srb) / 256.0);
1328 }
1329 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getReceiverLossRate(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1330         const LinphoneCallStats *stats = (LinphoneCallStats *)stats_ptr;
1331         const report_block_t *rrb = NULL;
1332
1333         if (!stats || !stats->received_rtcp)
1334                 return (jfloat)0.0;
1335         /* Perform msgpullup() to prevent crashes in rtcp_is_SR() or rtcp_is_RR() if the RTCP packet is composed of several mblk_t structure */
1336         if (stats->received_rtcp->b_cont != NULL)
1337                 msgpullup(stats->received_rtcp, -1);
1338         if (rtcp_is_RR(stats->received_rtcp))
1339                 rrb = rtcp_RR_get_report_block(stats->received_rtcp, 0);
1340         else if (rtcp_is_SR(stats->received_rtcp))
1341                 rrb = rtcp_SR_get_report_block(stats->received_rtcp, 0);
1342         if (!rrb)
1343                 return (jfloat)0.0;
1344         return (jfloat)(100.0 * report_block_get_fraction_lost(rrb) / 256.0);
1345 }
1346 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getSenderInterarrivalJitter(JNIEnv *env, jobject thiz, jlong stats_ptr, jlong call_ptr) {
1347         LinphoneCallStats *stats = (LinphoneCallStats *)stats_ptr;
1348         const LinphoneCall *call = (LinphoneCall *)call_ptr;
1349         const LinphoneCallParams *params;
1350         const PayloadType *pt;
1351         const report_block_t *srb = NULL;
1352
1353         if (!stats || !call || !stats->sent_rtcp)
1354                 return (jfloat)0.0;
1355         params = linphone_call_get_current_params(call);
1356         if (!params)
1357                 return (jfloat)0.0;
1358         /* Perform msgpullup() to prevent crashes in rtcp_is_SR() or rtcp_is_RR() if the RTCP packet is composed of several mblk_t structure */
1359         if (stats->sent_rtcp->b_cont != NULL)
1360                 msgpullup(stats->sent_rtcp, -1);
1361         if (rtcp_is_SR(stats->sent_rtcp))
1362                 srb = rtcp_SR_get_report_block(stats->sent_rtcp, 0);
1363         else if (rtcp_is_RR(stats->sent_rtcp))
1364                 srb = rtcp_RR_get_report_block(stats->sent_rtcp, 0);
1365         if (!srb)
1366                 return (jfloat)0.0;
1367         if (stats->type == LINPHONE_CALL_STATS_AUDIO)
1368                 pt = linphone_call_params_get_used_audio_codec(params);
1369         else
1370                 pt = linphone_call_params_get_used_video_codec(params);
1371         return (jfloat)((float)report_block_get_interarrival_jitter(srb) / (float)pt->clock_rate);
1372 }
1373 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getReceiverInterarrivalJitter(JNIEnv *env, jobject thiz, jlong stats_ptr, jlong call_ptr) {
1374         LinphoneCallStats *stats = (LinphoneCallStats *)stats_ptr;
1375         const LinphoneCall *call = (LinphoneCall *)call_ptr;
1376         const LinphoneCallParams *params;
1377         const PayloadType *pt;
1378         const report_block_t *rrb = NULL;
1379
1380         if (!stats || !call || !stats->received_rtcp)
1381                 return (jfloat)0.0;
1382         params = linphone_call_get_current_params(call);
1383         if (!params)
1384                 return (jfloat)0.0;
1385         /* Perform msgpullup() to prevent crashes in rtcp_is_SR() or rtcp_is_RR() if the RTCP packet is composed of several mblk_t structure */
1386         if (stats->received_rtcp->b_cont != NULL)
1387                 msgpullup(stats->received_rtcp, -1);
1388         if (rtcp_is_SR(stats->received_rtcp))
1389                 rrb = rtcp_SR_get_report_block(stats->received_rtcp, 0);
1390         else if (rtcp_is_RR(stats->received_rtcp))
1391                 rrb = rtcp_RR_get_report_block(stats->received_rtcp, 0);
1392         if (!rrb)
1393                 return (jfloat)0.0;
1394         if (stats->type == LINPHONE_CALL_STATS_AUDIO)
1395                 pt = linphone_call_params_get_used_audio_codec(params);
1396         else
1397                 pt = linphone_call_params_get_used_video_codec(params);
1398         return (jfloat)((float)report_block_get_interarrival_jitter(rrb) / (float)pt->clock_rate);
1399 }
1400 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getRoundTripDelay(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1401         return (jfloat)((LinphoneCallStats *)stats_ptr)->round_trip_delay;
1402 }
1403 extern "C" jlong Java_org_linphone_core_LinphoneCallStatsImpl_getLatePacketsCumulativeNumber(JNIEnv *env, jobject thiz, jlong stats_ptr, jlong call_ptr) {
1404         LinphoneCallStats *stats = (LinphoneCallStats *)stats_ptr;
1405         LinphoneCall *call = (LinphoneCall *)call_ptr;
1406         rtp_stats_t rtp_stats;
1407
1408         if (!stats || !call)
1409                 return (jlong)0;
1410         memset(&rtp_stats, 0, sizeof(rtp_stats));
1411         if (stats->type == LINPHONE_CALL_STATS_AUDIO)
1412                 audio_stream_get_local_rtp_stats(call->audiostream, &rtp_stats);
1413 #ifdef VIDEO_ENABLED
1414         else
1415                 video_stream_get_local_rtp_stats(call->videostream, &rtp_stats);
1416 #endif
1417         return (jlong)rtp_stats.outoftime;
1418 }
1419 extern "C" jfloat Java_org_linphone_core_LinphoneCallStatsImpl_getJitterBufferSize(JNIEnv *env, jobject thiz, jlong stats_ptr) {
1420         return (jfloat)((LinphoneCallStats *)stats_ptr)->jitter_stats.jitter_buffer_size_ms;
1421 }
1422
1423 /*payloadType*/
1424 extern "C" jstring Java_org_linphone_core_PayloadTypeImpl_toString(JNIEnv*  env,jobject  thiz,jlong ptr) {
1425         PayloadType* pt = (PayloadType*)ptr;
1426         char* value = ms_strdup_printf("[%s] clock [%i], bitrate [%i]"
1427                                                                         ,payload_type_get_mime(pt)
1428                                                                         ,payload_type_get_rate(pt)
1429                                                                         ,payload_type_get_bitrate(pt));
1430         jstring jvalue =env->NewStringUTF(value);
1431         ms_free(value);
1432         return jvalue;
1433 }
1434 extern "C" jstring Java_org_linphone_core_PayloadTypeImpl_getMime(JNIEnv*  env,jobject  thiz,jlong ptr) {
1435         PayloadType* pt = (PayloadType*)ptr;
1436         jstring jvalue =env->NewStringUTF(payload_type_get_mime(pt));
1437         return jvalue;
1438 }
1439 extern "C" jint Java_org_linphone_core_PayloadTypeImpl_getRate(JNIEnv*  env,jobject  thiz, jlong ptr) {
1440         PayloadType* pt = (PayloadType*)ptr;
1441         return payload_type_get_rate(pt);
1442 }
1443
1444 //LinphoneCall
1445 extern "C" void Java_org_linphone_core_LinphoneCallImpl_finalize(JNIEnv*  env
1446                                                                                                                                                 ,jobject  thiz
1447                                                                                                                                                 ,jlong ptr) {
1448         LinphoneCall *call=(LinphoneCall*)ptr;
1449         linphone_call_unref(call);
1450 }
1451
1452 extern "C" jlong Java_org_linphone_core_LinphoneCallImpl_getCallLog(    JNIEnv*  env
1453                                                                                                                                                 ,jobject  thiz
1454                                                                                                                                                 ,jlong ptr) {
1455         return (jlong)linphone_call_get_call_log((LinphoneCall*)ptr);
1456 }
1457
1458 extern "C" void Java_org_linphone_core_LinphoneCallImpl_takeSnapshot(   JNIEnv*  env
1459                                                                                                                                                 ,jobject  thiz
1460                                                                                                                                                 ,jlong ptr, jstring path) {
1461         const char* filePath = path != NULL ? env->GetStringUTFChars(path, NULL) : NULL;
1462         linphone_call_take_video_snapshot((LinphoneCall*)ptr, filePath);
1463 }
1464
1465 extern "C" void Java_org_linphone_core_LinphoneCallImpl_zoomVideo(              JNIEnv*  env
1466                                                                                                                                                 ,jobject  thiz
1467                                                                                                                                                 ,jlong ptr, jfloat zoomFactor, jfloat cx, jfloat cy) {
1468         linphone_call_zoom_video((LinphoneCall*)ptr, zoomFactor, &cx, &cy);
1469 }
1470
1471 extern "C" jboolean Java_org_linphone_core_LinphoneCallImpl_isIncoming( JNIEnv*  env
1472                                                                                                                                                 ,jobject  thiz
1473                                                                                                                                                 ,jlong ptr) {
1474         return linphone_call_get_dir((LinphoneCall*)ptr)==LinphoneCallIncoming?JNI_TRUE:JNI_FALSE;
1475 }
1476
1477 extern "C" jlong Java_org_linphone_core_LinphoneCallImpl_getRemoteAddress(      JNIEnv*  env
1478                                                                                                                                                 ,jobject  thiz
1479                                                                                                                                                 ,jlong ptr) {
1480         return (jlong)linphone_call_get_remote_address((LinphoneCall*)ptr);
1481 }
1482
1483 extern "C" jint Java_org_linphone_core_LinphoneCallImpl_getState(       JNIEnv*  env
1484                                                                                                                                                 ,jobject  thiz
1485                                                                                                                                                 ,jlong ptr) {
1486         return (jint)linphone_call_get_state((LinphoneCall*)ptr);
1487 }
1488 extern "C" void Java_org_linphone_core_LinphoneCallImpl_enableEchoCancellation( JNIEnv*  env
1489                                                                                                                                                 ,jobject  thiz
1490                                                                                                                                                 ,jlong ptr
1491                                                                                                                                                 ,jboolean value) {
1492         linphone_call_enable_echo_cancellation((LinphoneCall*)ptr,value);
1493 }
1494 extern "C" jboolean Java_org_linphone_core_LinphoneCallImpl_isEchoCancellationEnabled(  JNIEnv*  env
1495                                                                                                                                                 ,jobject  thiz
1496                                                                                                                                                 ,jlong ptr) {
1497         return linphone_call_echo_cancellation_enabled((LinphoneCall*)ptr);
1498 }
1499
1500 extern "C" void Java_org_linphone_core_LinphoneCallImpl_enableEchoLimiter(      JNIEnv*  env
1501                                                                                                                                                 ,jobject  thiz
1502                                                                                                                                                 ,jlong ptr
1503                                                                                                                                                 ,jboolean value) {
1504         linphone_call_enable_echo_limiter((LinphoneCall*)ptr,value);
1505 }
1506 extern "C" jboolean Java_org_linphone_core_LinphoneCallImpl_isEchoLimiterEnabled(       JNIEnv*  env
1507                                                                                                                                                 ,jobject  thiz
1508                                                                                                                                                 ,jlong ptr) {
1509         return linphone_call_echo_limiter_enabled((LinphoneCall*)ptr);
1510 }
1511
1512 extern "C" jobject Java_org_linphone_core_LinphoneCallImpl_getReplacedCall(     JNIEnv*  env
1513                                                                                                                                                 ,jobject  thiz
1514                                                                                                                                                 ,jlong ptr) {
1515         LinphoneCoreData *lcd=(LinphoneCoreData*)linphone_core_get_user_data(linphone_call_get_core((LinphoneCall*)ptr));       
1516         return lcd->getCall(env,linphone_call_get_replaced_call((LinphoneCall*)ptr));
1517 }
1518
1519 extern "C" jfloat Java_org_linphone_core_LinphoneCallImpl_getCurrentQuality(    JNIEnv*  env
1520                                                                                                                                                 ,jobject  thiz
1521                                                                                                                                                 ,jlong ptr) {
1522         return (jfloat)linphone_call_get_current_quality((LinphoneCall*)ptr);
1523 }
1524
1525 extern "C" jfloat Java_org_linphone_core_LinphoneCallImpl_getAverageQuality(    JNIEnv*  env
1526                                                                                                                                                 ,jobject  thiz
1527                                                                                                                                                 ,jlong ptr) {
1528         return (jfloat)linphone_call_get_average_quality((LinphoneCall*)ptr);
1529 }
1530
1531
1532 //LinphoneFriend
1533 extern "C" long Java_org_linphone_core_LinphoneFriendImpl_newLinphoneFriend(JNIEnv*  env
1534                                                                                                                                                 ,jobject  thiz
1535                                                                                                                                                 ,jstring jFriendUri) {
1536         LinphoneFriend* lResult;
1537
1538         if (jFriendUri) {
1539                 const char* friendUri = env->GetStringUTFChars(jFriendUri, NULL);
1540                 lResult= linphone_friend_new_with_addr(friendUri);
1541                 env->ReleaseStringUTFChars(jFriendUri, friendUri);
1542         } else {
1543                 lResult = linphone_friend_new();
1544         }
1545         return (long)lResult;
1546 }
1547 extern "C" void Java_org_linphone_core_LinphoneFriendImpl_setAddress(JNIEnv*  env
1548                                                                                                                                                 ,jobject  thiz
1549                                                                                                                                                 ,jlong ptr
1550                                                                                                                                                 ,jlong linphoneAddress) {
1551         linphone_friend_set_addr((LinphoneFriend*)ptr,(LinphoneAddress*)linphoneAddress);
1552 }
1553 extern "C" long Java_org_linphone_core_LinphoneFriendImpl_getAddress(JNIEnv*  env
1554                                                                                                                                                 ,jobject  thiz
1555                                                                                                                                                 ,jlong ptr) {
1556         return (long)linphone_friend_get_address((LinphoneFriend*)ptr);
1557 }
1558 extern "C" void Java_org_linphone_core_LinphoneFriendImpl_setIncSubscribePolicy(JNIEnv*  env
1559                                                                                                                                                 ,jobject  thiz
1560                                                                                                                                                 ,jlong ptr
1561                                                                                                                                                 ,jint policy) {
1562         linphone_friend_set_inc_subscribe_policy((LinphoneFriend*)ptr,(LinphoneSubscribePolicy)policy);
1563 }
1564 extern "C" jint Java_org_linphone_core_LinphoneFriendImpl_getIncSubscribePolicy(JNIEnv*  env
1565                                                                                                                                                 ,jobject  thiz
1566                                                                                                                                                 ,jlong ptr) {
1567         return linphone_friend_get_inc_subscribe_policy((LinphoneFriend*)ptr);
1568 }
1569 extern "C" void Java_org_linphone_core_LinphoneFriendImpl_enableSubscribes(JNIEnv*  env
1570                                                                                                                                                 ,jobject  thiz
1571                                                                                                                                                 ,jlong ptr
1572                                                                                                                                                 ,jboolean value) {
1573         linphone_friend_enable_subscribes((LinphoneFriend*)ptr,value);
1574 }
1575 extern "C" jboolean Java_org_linphone_core_LinphoneFriendImpl_isSubscribesEnabled(JNIEnv*  env
1576                                                                                                                                                 ,jobject  thiz
1577                                                                                                                                                 ,jlong ptr) {
1578         return linphone_friend_subscribes_enabled((LinphoneFriend*)ptr);
1579 }
1580 extern "C" jboolean Java_org_linphone_core_LinphoneFriendImpl_getStatus(JNIEnv*  env
1581                                                                                                                                                 ,jobject  thiz
1582                                                                                                                                                 ,jlong ptr) {
1583         return linphone_friend_get_status((LinphoneFriend*)ptr);
1584 }
1585 extern "C" void Java_org_linphone_core_LinphoneFriendImpl_edit(JNIEnv*  env
1586                                                                                                                                                 ,jobject  thiz
1587                                                                                                                                                 ,jlong ptr) {
1588         return linphone_friend_edit((LinphoneFriend*)ptr);
1589 }
1590 extern "C" void Java_org_linphone_core_LinphoneFriendImpl_done(JNIEnv*  env
1591                                                                                                                                                 ,jobject  thiz
1592                                                                                                                                                 ,jlong ptr) {
1593          linphone_friend_done((LinphoneFriend*)ptr);
1594 }
1595 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_removeFriend(JNIEnv*  env
1596                                                                                                                                                 ,jobject  thiz
1597                                                                                                                                                 ,jlong ptr
1598                                                                                                                                                 ,jlong lf) {
1599         linphone_core_remove_friend((LinphoneCore*)ptr, (LinphoneFriend*)lf);
1600 }
1601 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_getFriendByAddress(JNIEnv*  env
1602                                                                                                                                                 ,jobject  thiz
1603                                                                                                                                                 ,jlong ptr
1604                                                                                                                                                 ,jstring jaddress) {
1605         const char* address = env->GetStringUTFChars(jaddress, NULL);
1606         LinphoneFriend *lf = linphone_core_get_friend_by_address((LinphoneCore*)ptr, address);
1607         env->ReleaseStringUTFChars(jaddress, address);
1608         return (jlong) lf;
1609 }
1610 //LinphoneChatRoom
1611 extern "C" long Java_org_linphone_core_LinphoneChatRoomImpl_getPeerAddress(JNIEnv*  env
1612                                                                                                                                                 ,jobject  thiz
1613                                                                                                                                                 ,jlong ptr) {
1614         return (long) linphone_chat_room_get_peer_address((LinphoneChatRoom*)ptr);
1615 }
1616 extern "C" jlong Java_org_linphone_core_LinphoneChatRoomImpl_createLinphoneChatMessage(JNIEnv*  env
1617                                                                                                                                                 ,jobject  thiz
1618                                                                                                                                                 ,jlong ptr
1619                                                                                                                                                 ,jstring jmessage) {
1620         const char* message = env->GetStringUTFChars(jmessage, NULL);
1621         LinphoneChatMessage *chatMessage = linphone_chat_room_create_message((LinphoneChatRoom *)ptr, message);
1622         env->ReleaseStringUTFChars(jmessage, message);
1623
1624         return (jlong) chatMessage;
1625 }
1626 extern "C" void Java_org_linphone_core_LinphoneChatMessageImpl_setUserData(JNIEnv*  env
1627                                                                                                                                                 ,jobject  thiz
1628                                                                                                                                                 ,jlong ptr) {
1629         jobject ud = env->NewGlobalRef(thiz);
1630         linphone_chat_message_set_user_data((LinphoneChatMessage*)ptr,(void*) ud);
1631 }
1632 extern "C" jstring Java_org_linphone_core_LinphoneChatMessageImpl_getMessage(JNIEnv*  env
1633                                                                                                                                                 ,jobject  thiz
1634                                                                                                                                                 ,jlong ptr) {
1635         jstring jvalue =env->NewStringUTF(linphone_chat_message_get_message((LinphoneChatMessage*)ptr));
1636         return jvalue;
1637 }
1638 extern "C" jstring Java_org_linphone_core_LinphoneChatMessageImpl_getExternalBodyUrl(JNIEnv*  env
1639                                                                                                                                                 ,jobject  thiz
1640                                                                                                                                                 ,jlong ptr) {
1641         jstring jvalue =env->NewStringUTF(linphone_chat_message_get_external_body_url((LinphoneChatMessage*)ptr));
1642         return jvalue;
1643 }
1644 extern "C" void Java_org_linphone_core_LinphoneChatMessageImpl_setExternalBodyUrl(JNIEnv*  env
1645                                                                                                                                                 ,jobject  thiz
1646                                                                                                                                                 ,jlong ptr
1647                                                                                                                                                 ,jstring jurl) {
1648         const char* url = env->GetStringUTFChars(jurl, NULL);
1649         linphone_chat_message_set_external_body_url((LinphoneChatMessage *)ptr, url);
1650         env->ReleaseStringUTFChars(jurl, url);
1651 }
1652 extern "C" jlong Java_org_linphone_core_LinphoneChatMessageImpl_getFrom(JNIEnv*  env
1653                                                                                                                                                 ,jobject  thiz
1654                                                                                                                                                 ,jlong ptr) {
1655         return (jlong) linphone_chat_message_get_from((LinphoneChatMessage*)ptr);
1656 }
1657 extern "C" jlong Java_org_linphone_core_LinphoneChatMessageImpl_getPeerAddress(JNIEnv*  env
1658                                                                                                                                                 ,jobject  thiz
1659                                                                                                                                                 ,jlong ptr) {
1660         return (jlong) linphone_chat_message_get_peer_address((LinphoneChatMessage*)ptr);
1661 }
1662 extern "C" void Java_org_linphone_core_LinphoneChatRoomImpl_sendMessage(JNIEnv*  env
1663                                                                                                                                                 ,jobject  thiz
1664                                                                                                                                                 ,jlong ptr
1665                                                                                                                                                 ,jstring jmessage) {
1666         const char* message = env->GetStringUTFChars(jmessage, NULL);
1667         linphone_chat_room_send_message((LinphoneChatRoom*)ptr, message);
1668         env->ReleaseStringUTFChars(jmessage, message);
1669 }
1670
1671 static void chat_room_impl_callback(LinphoneChatMessage* msg, LinphoneChatMessageState state, void* ud) {
1672         JNIEnv *env = 0;
1673         jint result = jvm->AttachCurrentThread(&env,NULL);
1674         if (result != 0) {
1675                 ms_error("cannot attach VM\n");
1676                 return;
1677         }
1678
1679         jobject listener = (jobject) ud;
1680         jclass clazz = (jclass) env->GetObjectClass(listener);
1681         jmethodID method = env->GetMethodID(clazz, "onLinphoneChatMessageStateChanged","(Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneChatMessage$State;)V");
1682
1683         LinphoneCore *lc = linphone_chat_room_get_lc(linphone_chat_message_get_chat_room(msg));
1684         LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_get_user_data(lc);
1685         env->CallVoidMethod(
1686                         listener,
1687                         method,
1688                         (jobject)linphone_chat_message_get_user_data(msg),
1689                         env->CallStaticObjectMethod(lcData->chatMessageStateClass,lcData->chatMessageStateFromIntId,(jint)state));
1690
1691         if (state == LinphoneChatMessageStateDelivered || state == LinphoneChatMessageStateNotDelivered) {
1692                 env->DeleteGlobalRef(listener);
1693         }
1694 }
1695 extern "C" void Java_org_linphone_core_LinphoneChatRoomImpl_sendMessage2(JNIEnv*  env
1696                                                                                                                                                 ,jobject  thiz
1697                                                                                                                                                 ,jlong ptr
1698                                                                                                                                                 ,jlong jmessage
1699                                                                                                                                                 ,jobject jlistener) {
1700         jobject listener = env->NewGlobalRef(jlistener);
1701         linphone_chat_room_send_message2((LinphoneChatRoom*)ptr, (LinphoneChatMessage*)jmessage, chat_room_impl_callback, (void*)listener);
1702 }
1703 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setVideoWindowId(JNIEnv* env
1704                                                                                                                                                 ,jobject thiz
1705                                                                                                                                                 ,jlong lc
1706                                                                                                                                                 ,jobject obj) {
1707         jobject oldWindow = (jobject) linphone_core_get_native_video_window_id((LinphoneCore*)lc);
1708         if (oldWindow != NULL) {
1709                 env->DeleteGlobalRef(oldWindow);
1710         }
1711         if (obj != NULL) {
1712                 obj = env->NewGlobalRef(obj);
1713         }
1714         linphone_core_set_native_video_window_id((LinphoneCore*)lc,(unsigned long)obj);
1715 }
1716
1717 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setPreviewWindowId(JNIEnv* env
1718                                                                                                                                                 ,jobject thiz
1719                                                                                                                                                 ,jlong lc
1720                                                                                                                                                 ,jobject obj) {
1721         jobject oldWindow = (jobject) linphone_core_get_native_preview_window_id((LinphoneCore*)lc);
1722         if (oldWindow != NULL) {
1723                 env->DeleteGlobalRef(oldWindow);
1724         }
1725         if (obj != NULL) {
1726                 obj = env->NewGlobalRef(obj);
1727         }
1728         linphone_core_set_native_preview_window_id((LinphoneCore*)lc,(unsigned long)obj);
1729 }
1730
1731 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setDeviceRotation(JNIEnv* env
1732                                                                                                                                                 ,jobject thiz
1733                                                                                                                                                 ,jlong lc
1734                                                                                                                                                 ,jint rotation) {
1735         linphone_core_set_device_rotation((LinphoneCore*)lc,rotation);
1736 }
1737
1738
1739 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setFirewallPolicy(JNIEnv *env, jobject thiz, jlong lc, int enum_value){
1740         linphone_core_set_firewall_policy((LinphoneCore*)lc,(LinphoneFirewallPolicy)enum_value);
1741 }
1742
1743 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_getFirewallPolicy(JNIEnv *env, jobject thiz, jlong lc){
1744         return linphone_core_get_firewall_policy((LinphoneCore*)lc);
1745 }
1746
1747 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setStunServer(JNIEnv *env, jobject thiz, jlong lc, jstring jserver){
1748         const char* server = NULL;
1749         if (jserver) server=env->GetStringUTFChars(jserver, NULL);
1750         linphone_core_set_stun_server((LinphoneCore*)lc,server);
1751         if (server) env->ReleaseStringUTFChars(jserver,server);
1752 }
1753
1754 extern "C" jstring Java_org_linphone_core_LinphoneCoreImpl_getStunServer(JNIEnv *env, jobject thiz, jlong lc){
1755         const char *ret= linphone_core_get_stun_server((LinphoneCore*)lc);
1756         if (ret==NULL) return NULL;
1757         jstring jvalue =env->NewStringUTF(ret);
1758         return jvalue;
1759 }
1760
1761 extern "C" void Java_org_linphone_core_LinphoneCallParamsImpl_audioBandwidth(JNIEnv *env, jobject thiz, jlong lcp, jint bw){
1762         linphone_call_params_set_audio_bandwidth_limit((LinphoneCallParams*)lcp, bw);
1763 }
1764
1765 extern "C" void Java_org_linphone_core_LinphoneCallParamsImpl_enableVideo(JNIEnv *env, jobject thiz, jlong lcp, jboolean b){
1766         linphone_call_params_enable_video((LinphoneCallParams*)lcp, b);
1767 }
1768
1769 extern "C" jboolean Java_org_linphone_core_LinphoneCallParamsImpl_getVideoEnabled(JNIEnv *env, jobject thiz, jlong lcp){
1770         return linphone_call_params_video_enabled((LinphoneCallParams*)lcp);
1771 }
1772
1773 extern "C" jboolean Java_org_linphone_core_LinphoneCallParamsImpl_localConferenceMode(JNIEnv *env, jobject thiz, jlong lcp){
1774         return linphone_call_params_local_conference_mode((LinphoneCallParams*)lcp);
1775 }
1776
1777 extern "C" void Java_org_linphone_core_LinphoneCallParamsImpl_destroy(JNIEnv *env, jobject thiz, jlong lc){
1778         return linphone_call_params_destroy((LinphoneCallParams*)lc);
1779 }
1780 extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_createDefaultCallParams(JNIEnv *env, jobject thiz, jlong lc){
1781         return (jlong) linphone_core_create_default_call_parameters((LinphoneCore*)lc);
1782 }
1783
1784 extern "C" jlong Java_org_linphone_core_LinphoneCallImpl_getRemoteParams(JNIEnv *env, jobject thiz, jlong lc){
1785         if (linphone_call_get_remote_params((LinphoneCall*)lc) == NULL) {
1786                         return (jlong) 0;
1787         }
1788         return (jlong) linphone_call_params_copy(linphone_call_get_remote_params((LinphoneCall*)lc));
1789 }
1790
1791 extern "C" jlong Java_org_linphone_core_LinphoneCallImpl_getCurrentParamsCopy(JNIEnv *env, jobject thiz, jlong lc){
1792         return (jlong) linphone_call_params_copy(linphone_call_get_current_params((LinphoneCall*)lc));
1793 }
1794
1795 extern "C" jlong Java_org_linphone_core_LinphoneCallImpl_enableCamera(JNIEnv *env, jobject thiz, jlong lc, jboolean b){
1796         linphone_call_enable_camera((LinphoneCall *)lc, (bool_t) b);
1797 }
1798
1799 extern "C" jboolean Java_org_linphone_core_LinphoneCallImpl_cameraEnabled(JNIEnv *env, jobject thiz, jlong lc){
1800         linphone_call_camera_enabled((LinphoneCall *)lc);
1801 }
1802
1803 extern "C" jobject Java_org_linphone_core_LinphoneCoreImpl_inviteAddressWithParams(JNIEnv *env, jobject thiz, jlong lc, jlong addr, jlong params){
1804         LinphoneCoreData *lcd=(LinphoneCoreData*)linphone_core_get_user_data((LinphoneCore*)lc);
1805         return  lcd->getCall(env,linphone_core_invite_address_with_params((LinphoneCore *)lc, (const LinphoneAddress *)addr, (const LinphoneCallParams *)params));
1806 }
1807
1808 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_updateAddressWithParams(JNIEnv *env, jobject thiz, jlong lc, jlong call, jlong params){
1809         return (jint) linphone_core_update_call((LinphoneCore *)lc, (LinphoneCall *)call, (LinphoneCallParams *)params);
1810 }
1811
1812 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_updateCall(JNIEnv *env, jobject thiz, jlong lc, jlong call, jlong params){
1813         return (jint) linphone_core_update_call((LinphoneCore *)lc, (LinphoneCall *)call, (const LinphoneCallParams *)params);
1814 }
1815
1816
1817 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setPreferredVideoSize(JNIEnv *env, jobject thiz, jlong lc, jint width, jint height){
1818         MSVideoSize vsize;
1819         vsize.width = (int)width;
1820         vsize.height = (int)height;
1821         linphone_core_set_preferred_video_size((LinphoneCore *)lc, vsize);
1822 }
1823
1824 extern "C" jintArray Java_org_linphone_core_LinphoneCoreImpl_getPreferredVideoSize(JNIEnv *env, jobject thiz, jlong lc){
1825         MSVideoSize vsize = linphone_core_get_preferred_video_size((LinphoneCore *)lc);
1826     jintArray arr = env->NewIntArray(2);
1827         int tVsize [2]= {vsize.width,vsize.height};
1828     env->SetIntArrayRegion(arr, 0, 2, tVsize);
1829     return arr;
1830 }
1831
1832 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setDownloadBandwidth(JNIEnv *env, jobject thiz, jlong lc, jint bw){
1833         linphone_core_set_download_bandwidth((LinphoneCore *)lc, (int) bw);
1834 }
1835
1836 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setUploadBandwidth(JNIEnv *env, jobject thiz, jlong lc, jint bw){
1837         linphone_core_set_upload_bandwidth((LinphoneCore *)lc, (int) bw);
1838 }
1839
1840 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setDownloadPtime(JNIEnv *env, jobject thiz, jlong lc, jint ptime){
1841         linphone_core_set_download_ptime((LinphoneCore *)lc, (int) ptime);
1842 }
1843
1844 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setUploadPtime(JNIEnv *env, jobject thiz, jlong lc, jint ptime){
1845         linphone_core_set_upload_ptime((LinphoneCore *)lc, (int) ptime);
1846 }
1847
1848 extern "C" int Java_org_linphone_core_LinphoneProxyConfigImpl_getState(JNIEnv*  env,jobject thiz,jlong ptr) {
1849         return (int) linphone_proxy_config_get_state((const LinphoneProxyConfig *) ptr);
1850 }
1851 extern "C" void Java_org_linphone_core_LinphoneProxyConfigImpl_setExpires(JNIEnv*  env,jobject thiz,jlong ptr,jint delay) {
1852         linphone_proxy_config_expires((LinphoneProxyConfig *) ptr, (int) delay);
1853 }
1854
1855 extern "C" jint Java_org_linphone_core_LinphoneCallImpl_getDuration(JNIEnv*  env,jobject thiz,jlong ptr) {
1856         linphone_call_get_duration((LinphoneCall *) ptr);
1857 }
1858
1859 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_getSignalingTransportPort(JNIEnv* env,jobject thiz,jlong ptr, jint code) {
1860         LCSipTransports tr;
1861         linphone_core_get_sip_transports((LinphoneCore *) ptr, &tr);
1862                 switch (code) {
1863         case 0:
1864                 return tr.udp_port;
1865         case 1:
1866                 return tr.tcp_port;
1867         case 3:
1868                 return tr.tls_port;
1869         default:
1870                 return -2;
1871         }
1872 }
1873
1874 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setSignalingTransportPorts(JNIEnv*  env,jobject thiz,jlong ptr,jint udp, jint tcp, jint tls) {
1875         LinphoneCore *lc = (LinphoneCore *) ptr;
1876         LCSipTransports tr;
1877         tr.udp_port = udp;
1878         tr.tcp_port = tcp;
1879         tr.tls_port = tls;
1880
1881         linphone_core_set_sip_transports(lc, &tr); // tr will be copied
1882 }
1883
1884 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_enableIpv6(JNIEnv* env,jobject  thiz
1885               ,jlong lc, jboolean enable) {
1886               linphone_core_enable_ipv6((LinphoneCore*)lc,enable);
1887 }
1888
1889 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_adjustSoftwareVolume(JNIEnv* env,jobject  thiz
1890               ,jlong ptr, jint db) {
1891         linphone_core_set_playback_gain_db((LinphoneCore *) ptr, db);
1892 }
1893
1894 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_pauseCall(JNIEnv *env,jobject thiz,jlong pCore, jlong pCall) {
1895         return linphone_core_pause_call((LinphoneCore *) pCore, (LinphoneCall *) pCall);
1896 }
1897 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_pauseAllCalls(JNIEnv *env,jobject thiz,jlong pCore) {
1898         return linphone_core_pause_all_calls((LinphoneCore *) pCore);
1899 }
1900 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_resumeCall(JNIEnv *env,jobject thiz,jlong pCore, jlong pCall) {
1901         return linphone_core_resume_call((LinphoneCore *) pCore, (LinphoneCall *) pCall);
1902 }
1903 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isInConference(JNIEnv *env,jobject thiz,jlong pCore) {
1904         return linphone_core_is_in_conference((LinphoneCore *) pCore);
1905 }
1906 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_enterConference(JNIEnv *env,jobject thiz,jlong pCore) {
1907         return 0 == linphone_core_enter_conference((LinphoneCore *) pCore);
1908 }
1909 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_leaveConference(JNIEnv *env,jobject thiz,jlong pCore) {
1910         linphone_core_leave_conference((LinphoneCore *) pCore);
1911 }
1912 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_addAllToConference(JNIEnv *env,jobject thiz,jlong pCore) {
1913         linphone_core_add_all_to_conference((LinphoneCore *) pCore);
1914 }
1915 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_addToConference(JNIEnv *env,jobject thiz,jlong pCore, jlong pCall) {
1916         linphone_core_add_to_conference((LinphoneCore *) pCore, (LinphoneCall *) pCall);
1917 }
1918 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_removeFromConference(JNIEnv *env,jobject thiz,jlong pCore, jlong pCall) {
1919         linphone_core_remove_from_conference((LinphoneCore *) pCore, (LinphoneCall *) pCall);
1920 }
1921
1922 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_terminateConference(JNIEnv *env,jobject thiz,jlong pCore) {
1923         linphone_core_terminate_conference((LinphoneCore *) pCore);
1924 }
1925 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_getConferenceSize(JNIEnv *env,jobject thiz,jlong pCore) {
1926         return linphone_core_get_conference_size((LinphoneCore *) pCore);
1927 }
1928 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_terminateAllCalls(JNIEnv *env,jobject thiz,jlong pCore) {
1929         linphone_core_terminate_all_calls((LinphoneCore *) pCore);
1930 }
1931 extern "C" jobject Java_org_linphone_core_LinphoneCoreImpl_getCall(JNIEnv *env,jobject thiz,jlong pCore,jint position) {
1932         LinphoneCoreData *lcd=(LinphoneCoreData*)linphone_core_get_user_data((LinphoneCore*)pCore);
1933         LinphoneCall* lCall = (LinphoneCall*) ms_list_nth_data(linphone_core_get_calls((LinphoneCore *) pCore),position);
1934         return lcd->getCall(env,lCall);
1935 }
1936 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_getCallsNb(JNIEnv *env,jobject thiz,jlong pCore) {
1937         return ms_list_size(linphone_core_get_calls((LinphoneCore *) pCore));
1938 }
1939
1940 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_transferCall(JNIEnv *env,jobject thiz,jlong pCore, jlong pCall, jstring jReferTo) {
1941         const char* cReferTo=env->GetStringUTFChars(jReferTo, NULL);
1942         linphone_core_transfer_call((LinphoneCore *) pCore, (LinphoneCall *) pCall, cReferTo);
1943         env->ReleaseStringUTFChars(jReferTo, cReferTo);
1944 }
1945 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_transferCallToAnother(JNIEnv *env,jobject thiz,jlong pCore, jlong pCall, jlong pDestCall) {
1946         linphone_core_transfer_call_to_another((LinphoneCore *) pCore, (LinphoneCall *) pCall, (LinphoneCall *) pDestCall);
1947 }
1948
1949 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setZrtpSecretsCache(JNIEnv *env,jobject thiz,jlong pCore, jstring jFile) {
1950         if (jFile) {
1951                 const char* cFile=env->GetStringUTFChars(jFile, NULL);
1952                 linphone_core_set_zrtp_secrets_file((LinphoneCore *) pCore,cFile);
1953                 env->ReleaseStringUTFChars(jFile, cFile);
1954         } else {
1955                 linphone_core_set_zrtp_secrets_file((LinphoneCore *) pCore,NULL);
1956         }
1957 }
1958
1959 extern "C" jobject Java_org_linphone_core_LinphoneCoreImpl_findCallFromUri(JNIEnv *env,jobject thiz,jlong pCore, jstring jUri) {
1960         const char* cUri=env->GetStringUTFChars(jUri, NULL);
1961         LinphoneCall *call= (LinphoneCall *) linphone_core_find_call_from_uri((LinphoneCore *) pCore,cUri);
1962         env->ReleaseStringUTFChars(jUri, cUri);
1963         LinphoneCoreData *lcdata=(LinphoneCoreData*)linphone_core_get_user_data((LinphoneCore*)pCore);
1964         return (jobject) lcdata->getCall(env,call);
1965 }
1966
1967
1968 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_setVideoDevice(JNIEnv *env,jobject thiz,jlong pCore,jint id) {
1969         LinphoneCore* lc = (LinphoneCore *) pCore;
1970         const char** devices = linphone_core_get_video_devices(lc);
1971         if (devices == NULL) {
1972                 ms_error("No existing video devices\n");
1973                 return -1;
1974         }
1975         int i;
1976         for(i=0; i<=id; i++) {
1977                 if (devices[i] == NULL)
1978                         break;
1979                 ms_message("Existing device %d : %s\n", i, devices[i]);
1980                 if (i==id) {
1981                         return linphone_core_set_video_device(lc, devices[i]);
1982                 }
1983         }
1984         return -1;
1985 }
1986
1987 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_getVideoDevice(JNIEnv *env,jobject thiz,jlong pCore) {
1988         LinphoneCore* lc = (LinphoneCore *) pCore;
1989         const char** devices = linphone_core_get_video_devices(lc);
1990         if (devices == NULL) {
1991                 ms_error("No existing video devices\n");
1992                 return -1;
1993         }
1994         const char* cam = linphone_core_get_video_device(lc);
1995         if (cam == NULL) {
1996                 ms_error("No current video device\n");
1997         } else {
1998                 int i=0;
1999                 while(devices[i] != NULL) {
2000                         if (strcmp(devices[i], cam) == 0)
2001                                 return i;
2002                         i++;
2003                 }
2004         }
2005         ms_warning("Returning default (0) device\n");
2006         return 0;
2007 }
2008
2009 extern "C" jstring Java_org_linphone_core_LinphoneCallImpl_getAuthenticationToken(JNIEnv*  env,jobject thiz,jlong ptr) {
2010         LinphoneCall *call = (LinphoneCall *) ptr;
2011         const char* token = linphone_call_get_authentication_token(call);
2012         if (token == NULL) return NULL;
2013         return env->NewStringUTF(token);
2014 }
2015 extern "C" jboolean Java_org_linphone_core_LinphoneCallImpl_isAuthenticationTokenVerified(JNIEnv*  env,jobject thiz,jlong ptr) {
2016         LinphoneCall *call = (LinphoneCall *) ptr;
2017         return linphone_call_get_authentication_token_verified(call);
2018 }
2019 extern "C" void Java_org_linphone_core_LinphoneCallImpl_setAuthenticationTokenVerified(JNIEnv*  env,jobject thiz,jlong ptr,jboolean verified) {
2020         LinphoneCall *call = (LinphoneCall *) ptr;
2021         linphone_call_set_authentication_token_verified(call, verified);
2022 }
2023
2024 extern "C" jfloat Java_org_linphone_core_LinphoneCallImpl_getPlayVolume(JNIEnv* env, jobject thiz, jlong ptr) {
2025         LinphoneCall *call = (LinphoneCall *) ptr;
2026         return linphone_call_get_play_volume(call);
2027 }
2028
2029 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_soundResourcesLocked(JNIEnv* env,jobject thiz,jlong ptr){
2030         return linphone_core_sound_resources_locked((LinphoneCore *) ptr);
2031 }
2032
2033 // Needed by Galaxy S (can't switch to/from speaker while playing and still keep mic working)
2034 // Implemented directly in msandroid.cpp (sound filters for Android).
2035 extern "C" void msandroid_hack_speaker_state(bool speakerOn);
2036
2037 extern "C" void Java_org_linphone_LinphoneManager_hackSpeakerState(JNIEnv*  env,jobject thiz,jboolean speakerOn){
2038         msandroid_hack_speaker_state(speakerOn);
2039 }
2040 // End Galaxy S hack functions
2041
2042
2043 extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_getMaxCalls(JNIEnv *env,jobject thiz,jlong pCore) {
2044         return (jint) linphone_core_get_max_calls((LinphoneCore *) pCore);
2045 }
2046 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setMaxCalls(JNIEnv *env,jobject thiz,jlong pCore, jint max) {
2047         linphone_core_set_max_calls((LinphoneCore *) pCore, (int) max);
2048 }
2049
2050 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_tunnelAddServerAndMirror(JNIEnv *env,jobject thiz,jlong pCore,
2051                 jstring jHost, jint port, jint mirror, jint delay) {
2052         LinphoneTunnel *tunnel=((LinphoneCore *) pCore)->tunnel;
2053         if (!tunnel) return;
2054         const char* cHost=env->GetStringUTFChars(jHost, NULL);
2055         linphone_tunnel_add_server_and_mirror(tunnel, cHost, port, mirror, delay);
2056         env->ReleaseStringUTFChars(jHost, cHost);
2057 }
2058
2059 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_tunnelSetHttpProxy(JNIEnv *env,jobject thiz,jlong pCore,
2060                 jstring jHost, jint port, jstring username, jstring password) {
2061
2062         LinphoneTunnel *tunnel=((LinphoneCore *) pCore)->tunnel;
2063         if (!tunnel) return;
2064         const char* cHost=(jHost!=NULL) ? env->GetStringUTFChars(jHost, NULL) : NULL;
2065         const char* cUsername= (username!=NULL) ? env->GetStringUTFChars(username, NULL) : NULL;
2066         const char* cPassword= (password!=NULL) ? env->GetStringUTFChars(password, NULL) : NULL;
2067         linphone_tunnel_set_http_proxy(tunnel,cHost, port,cUsername,cPassword);
2068         if (cHost) env->ReleaseStringUTFChars(jHost, cHost);
2069         if (cUsername) env->ReleaseStringUTFChars(username, cUsername);
2070         if (cPassword) env->ReleaseStringUTFChars(password, cPassword);
2071 }
2072
2073
2074 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_tunnelAutoDetect(JNIEnv *env,jobject thiz,jlong pCore) {
2075         LinphoneTunnel *tunnel=((LinphoneCore *) pCore)->tunnel; if (!tunnel) return;
2076         linphone_tunnel_auto_detect(tunnel);
2077
2078 }
2079
2080 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_tunnelCleanServers(JNIEnv *env,jobject thiz,jlong pCore) {
2081         LinphoneTunnel *tunnel=((LinphoneCore *) pCore)->tunnel; if (!tunnel) return;
2082         linphone_tunnel_clean_servers(tunnel);
2083 }
2084
2085 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_tunnelEnable(JNIEnv *env,jobject thiz,jlong pCore, jboolean enable) {
2086         LinphoneTunnel *tunnel=((LinphoneCore *) pCore)->tunnel; if (!tunnel) return;
2087         linphone_tunnel_enable(tunnel, enable);
2088 }
2089
2090
2091 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setUserAgent(JNIEnv *env,jobject thiz,jlong pCore, jstring name, jstring version){
2092         const char* cname=env->GetStringUTFChars(name, NULL);
2093         const char* cversion=env->GetStringUTFChars(version, NULL);
2094         linphone_core_set_user_agent(cname,cversion);
2095         env->ReleaseStringUTFChars(name, cname);
2096         env->ReleaseStringUTFChars(version, cversion);
2097 }
2098
2099 extern "C" jboolean Java_org_linphone_core_LinphoneCoreImpl_isTunnelAvailable(JNIEnv *env,jobject thiz){
2100         return linphone_core_tunnel_available();
2101 }
2102
2103 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setVideoPolicy(JNIEnv *env, jobject thiz, jlong lc, jboolean autoInitiate, jboolean autoAccept){
2104         LinphoneVideoPolicy vpol;
2105         vpol.automatically_initiate = autoInitiate;
2106         vpol.automatically_accept = autoAccept;
2107         linphone_core_set_video_policy((LinphoneCore *)lc, &vpol);
2108 }
2109
2110 extern "C" void Java_org_linphone_core_LinphoneCoreImpl_setCpuCountNative(JNIEnv *env, jobject thiz, jint count) {
2111         ms_set_cpu_count(count);
2112 }
2113
2114 extern "C" jstring Java_org_linphone_core_LinphoneCoreImpl_getVersion(JNIEnv*  env,jobject  thiz,jlong ptr) {
2115         jstring jvalue =env->NewStringUTF(linphone_core_get_version());
2116         return jvalue;
2117 }