houxiao
2017-07-13 b022b91c0c6fa807424b6c12cc92ac5946838083
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2010  Belledonne Communications SARL
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
 
#include "mediastreamer2/msjava.h"
//#include "mediastreamer2/mscommon.h"
#include "../../logger.h"
 
static JavaVM *ms2_vm=NULL;
 
#ifndef _WIN32
#include <pthread.h>
 
static pthread_key_t jnienv_key;
 
/*
 * Do not forget that any log within this routine may cause re-attach of the thread to the jvm because the logs can callback the java application
 * (see LinphoneCoreFactory.setLogHandler() ).
**/
void _android_key_cleanup(void *data){
    JNIEnv *env = (JNIEnv*) data;
 
    if (env != NULL) {
        LOGP(INFO, "Thread end, detaching jvm from current thread");
        ms2_vm->DetachCurrentThread();
        pthread_setspecific(jnienv_key,NULL);
    }
}
#endif
 
void ms_set_jvm_from_env(JNIEnv *env){
    env->GetJavaVM(&ms2_vm);
#ifndef _WIN32
    pthread_key_create(&jnienv_key,_android_key_cleanup);
#endif
}
 
void ms_set_jvm(JavaVM *vm){
    ms2_vm=vm;
#ifndef _WIN32
    pthread_key_create(&jnienv_key,_android_key_cleanup);
#endif
}
 
JavaVM *ms_get_jvm(void){
    return ms2_vm;
}
 
JNIEnv *ms_get_jni_env(void){
    JNIEnv *env=NULL;
    if (ms2_vm==NULL){
        LOGP(ERROR, "Calling ms_get_jni_env() while no jvm has been set using ms_set_jvm().");
    }else{
#ifndef _WIN32
        env=(JNIEnv*)pthread_getspecific(jnienv_key);
        if (env==NULL){
            if (ms2_vm->AttachCurrentThread(&env,NULL)!=0){
                LOGP(ERROR, "AttachCurrentThread() failed !");
                return NULL;
            }
            pthread_setspecific(jnienv_key,env);
        }
#else
        LOGP(ERROR, "ms_get_jni_env() not implemented on windows.");
#endif
    }
    return env;
}
 
#ifdef __ANDROID__
 
int ms_get_android_sdk_version(void) {
    static int sdk_version = 0;
    if (sdk_version==0){
        /* Get Android SDK version. */
        JNIEnv *env = ms_get_jni_env();
        jclass version_class = env->FindClass("android/os/Build$VERSION");
        jfieldID fid = env->GetStaticFieldID(version_class, "SDK_INT", "I");
        sdk_version = env->GetStaticIntField(version_class, fid);
        LOGP(INFO, "SDK version [%i] detected", sdk_version);
        env->DeleteLocalRef(version_class);
    }
    return sdk_version;
}
 
JNIEXPORT void JNICALL Java_org_linphone_mediastream_Log_d(JNIEnv* env, jobject thiz, jstring jmsg) {
    const char* msg = jmsg ? env->GetStringUTFChars(jmsg, NULL) : NULL;
    LOGP(DEBUG, "%s", msg);
    if (msg) env->ReleaseStringUTFChars(jmsg, msg);
}
 
JNIEXPORT void JNICALL Java_org_linphone_mediastream_Log_i(JNIEnv* env, jobject thiz, jstring jmsg) {
    const char* msg = jmsg ? env->GetStringUTFChars(jmsg, NULL) : NULL;
    LOGP(INFO, "%s", msg);
    if (msg) env->ReleaseStringUTFChars(jmsg, msg);
}
 
JNIEXPORT void JNICALL Java_org_linphone_mediastream_Log_w(JNIEnv* env, jobject thiz, jstring jmsg) {
    const char* msg = jmsg ? env->GetStringUTFChars(jmsg, NULL) : NULL;
    LOGP(WARNING, "%s", msg);
    if (msg) env->ReleaseStringUTFChars(jmsg, msg);
}
 
JNIEXPORT void JNICALL Java_org_linphone_mediastream_Log_e(JNIEnv* env, jobject thiz, jstring jmsg) {
    const char* msg = jmsg ? env->GetStringUTFChars(jmsg, NULL) : NULL;
    LOGP(ERROR, "%s", msg);
    if (msg) env->ReleaseStringUTFChars(jmsg, msg);
}
 
#endif