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