diff --git a/webrtc/modules/audio_device/android/audio_device_template.h b/webrtc/modules/audio_device/android/audio_device_template.h index 2811269825..83fdea9a79 100644 --- a/webrtc/modules/audio_device/android/audio_device_template.h +++ b/webrtc/modules/audio_device/android/audio_device_template.h @@ -30,6 +30,12 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } return InputType::SetAndroidAudioDeviceObjects(javaVM, env, context); } + + static void ClearAndroidAudioDeviceObjects() { + OutputType::ClearAndroidAudioDeviceObjects(); + InputType::ClearAndroidAudioDeviceObjects(); + } + explicit AudioDeviceTemplate(const int32_t id) : output_(id), input_(id, &output_) { diff --git a/webrtc/modules/audio_device/android/audio_record_jni.cc b/webrtc/modules/audio_device/android/audio_record_jni.cc index 198ac8ca97..f86910ea39 100644 --- a/webrtc/modules/audio_device/android/audio_record_jni.cc +++ b/webrtc/modules/audio_device/android/audio_record_jni.cc @@ -37,62 +37,62 @@ jclass AudioRecordJni::globalScClass = NULL; int32_t AudioRecordJni::SetAndroidAudioDeviceObjects(void* javaVM, void* env, void* context) { + assert(env); globalJvm = reinterpret_cast(javaVM); - - if (env) { - globalJNIEnv = reinterpret_cast(env); - // Get java class type (note path to class packet). - jclass javaScClassLocal = globalJNIEnv->FindClass( - "org/webrtc/voiceengine/WebRtcAudioRecord"); - if (!javaScClassLocal) { - WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, - "%s: could not find java class", __FUNCTION__); - return -1; // exception thrown - } - - // Create a global reference to the class (to tell JNI that we are - // referencing it after this function has returned). - globalScClass = reinterpret_cast ( - globalJNIEnv->NewGlobalRef(javaScClassLocal)); - if (!globalScClass) { - WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, - "%s: could not create reference", __FUNCTION__); - return -1; - } - - globalContext = globalJNIEnv->NewGlobalRef( - reinterpret_cast(context)); - if (!globalContext) { - WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, - "%s: could not create context reference", __FUNCTION__); - return -1; - } - - // Delete local class ref, we only use the global ref - globalJNIEnv->DeleteLocalRef(javaScClassLocal); + globalJNIEnv = reinterpret_cast(env); + // Get java class type (note path to class packet). + jclass javaScClassLocal = globalJNIEnv->FindClass( + "org/webrtc/voiceengine/WebRtcAudioRecord"); + if (!javaScClassLocal) { + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, + "%s: could not find java class", __FUNCTION__); + return -1; // exception thrown } - else { // User is resetting the env variable - WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, -1, - "%s: env is NULL, assuming deinit", __FUNCTION__); - if (!globalJNIEnv) { - WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, -1, - "%s: saved env already NULL", __FUNCTION__); - return 0; - } - - globalJNIEnv->DeleteGlobalRef(globalScClass); - globalScClass = reinterpret_cast(NULL); - - globalJNIEnv->DeleteGlobalRef(globalContext); - globalContext = reinterpret_cast(NULL); - - globalJNIEnv = reinterpret_cast(NULL); + // Create a global reference to the class (to tell JNI that we are + // referencing it after this function has returned). + globalScClass = reinterpret_cast ( + globalJNIEnv->NewGlobalRef(javaScClassLocal)); + if (!globalScClass) { + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, + "%s: could not create reference", __FUNCTION__); + return -1; } + globalContext = globalJNIEnv->NewGlobalRef( + reinterpret_cast(context)); + if (!globalContext) { + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, + "%s: could not create context reference", __FUNCTION__); + return -1; + } + + // Delete local class ref, we only use the global ref + globalJNIEnv->DeleteLocalRef(javaScClassLocal); + return 0; } +void AudioRecordJni::ClearAndroidAudioDeviceObjects() { + WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, -1, + "%s: env is NULL, assuming deinit", __FUNCTION__); + + globalJvm = NULL;; + if (!globalJNIEnv) { + WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, -1, + "%s: saved env already NULL", __FUNCTION__); + return; + } + + globalJNIEnv->DeleteGlobalRef(globalContext); + globalContext = reinterpret_cast(NULL); + + globalJNIEnv->DeleteGlobalRef(globalScClass); + globalScClass = reinterpret_cast(NULL); + + globalJNIEnv = reinterpret_cast(NULL); +} + AudioRecordJni::AudioRecordJni( const int32_t id, PlayoutDelayProvider* delay_provider) : _javaVM(NULL), diff --git a/webrtc/modules/audio_device/android/audio_record_jni.h b/webrtc/modules/audio_device/android/audio_record_jni.h index 1c525ff569..6cf80d8867 100644 --- a/webrtc/modules/audio_device/android/audio_record_jni.h +++ b/webrtc/modules/audio_device/android/audio_record_jni.h @@ -31,6 +31,7 @@ class AudioRecordJni { public: static int32_t SetAndroidAudioDeviceObjects(void* javaVM, void* env, void* context); + static void ClearAndroidAudioDeviceObjects(); AudioRecordJni(const int32_t id, PlayoutDelayProvider* delay_provider); ~AudioRecordJni(); diff --git a/webrtc/modules/audio_device/android/audio_track_jni.cc b/webrtc/modules/audio_device/android/audio_track_jni.cc index ac4ccee42f..023b911a12 100644 --- a/webrtc/modules/audio_device/android/audio_track_jni.cc +++ b/webrtc/modules/audio_device/android/audio_track_jni.cc @@ -36,62 +36,61 @@ jclass AudioTrackJni::globalScClass = NULL; int32_t AudioTrackJni::SetAndroidAudioDeviceObjects(void* javaVM, void* env, void* context) { + assert(env); globalJvm = reinterpret_cast(javaVM); - - if (env) { - globalJNIEnv = reinterpret_cast(env); - // Get java class type (note path to class packet). - jclass javaScClassLocal = globalJNIEnv->FindClass( - "org/webrtc/voiceengine/WebRtcAudioTrack"); - if (!javaScClassLocal) { - WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, - "%s: could not find java class", __FUNCTION__); - return -1; // exception thrown - } - - // Create a global reference to the class (to tell JNI that we are - // referencing it after this function has returned). - globalScClass = reinterpret_cast ( - globalJNIEnv->NewGlobalRef(javaScClassLocal)); - if (!globalScClass) { - WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, - "%s: could not create reference", __FUNCTION__); - return -1; - } - - globalContext = globalJNIEnv->NewGlobalRef( - reinterpret_cast(context)); - if (!globalContext) { - WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, - "%s: could not create context reference", __FUNCTION__); - return -1; - } - - // Delete local class ref, we only use the global ref - globalJNIEnv->DeleteLocalRef(javaScClassLocal); - } - else { // User is resetting the env variable - WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, -1, - "%s: env is NULL, assuming deinit", __FUNCTION__); - - if (!globalJNIEnv) { - WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, -1, - "%s: saved env already NULL", __FUNCTION__); - return 0; - } - - globalJNIEnv->DeleteGlobalRef(globalScClass); - globalScClass = reinterpret_cast(NULL); - - globalJNIEnv->DeleteGlobalRef(globalContext); - globalContext = reinterpret_cast(NULL); - - globalJNIEnv = reinterpret_cast(NULL); + globalJNIEnv = reinterpret_cast(env); + // Get java class type (note path to class packet). + jclass javaScClassLocal = globalJNIEnv->FindClass( + "org/webrtc/voiceengine/WebRtcAudioTrack"); + if (!javaScClassLocal) { + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, + "%s: could not find java class", __FUNCTION__); + return -1; // exception thrown } + // Create a global reference to the class (to tell JNI that we are + // referencing it after this function has returned). + globalScClass = reinterpret_cast ( + globalJNIEnv->NewGlobalRef(javaScClassLocal)); + if (!globalScClass) { + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, + "%s: could not create reference", __FUNCTION__); + return -1; + } + + globalContext = globalJNIEnv->NewGlobalRef( + reinterpret_cast(context)); + if (!globalContext) { + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1, + "%s: could not create context reference", __FUNCTION__); + return -1; + } + + // Delete local class ref, we only use the global ref + globalJNIEnv->DeleteLocalRef(javaScClassLocal); return 0; } +void AudioTrackJni::ClearAndroidAudioDeviceObjects() { + WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, -1, + "%s: env is NULL, assuming deinit", __FUNCTION__); + + globalJvm = NULL; + if (!globalJNIEnv) { + WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, -1, + "%s: saved env already NULL", __FUNCTION__); + return; + } + + globalJNIEnv->DeleteGlobalRef(globalContext); + globalContext = reinterpret_cast(NULL); + + globalJNIEnv->DeleteGlobalRef(globalScClass); + globalScClass = reinterpret_cast(NULL); + + globalJNIEnv = reinterpret_cast(NULL); +} + AudioTrackJni::AudioTrackJni(const int32_t id) : _javaVM(NULL), _jniEnvPlay(NULL), diff --git a/webrtc/modules/audio_device/android/audio_track_jni.h b/webrtc/modules/audio_device/android/audio_track_jni.h index 1c1721025b..a597fd7f26 100644 --- a/webrtc/modules/audio_device/android/audio_track_jni.h +++ b/webrtc/modules/audio_device/android/audio_track_jni.h @@ -30,7 +30,7 @@ class AudioTrackJni : public PlayoutDelayProvider { public: static int32_t SetAndroidAudioDeviceObjects(void* javaVM, void* env, void* context); - + static void ClearAndroidAudioDeviceObjects(); explicit AudioTrackJni(const int32_t id); virtual ~AudioTrackJni(); diff --git a/webrtc/modules/audio_device/android/opensles_input.cc b/webrtc/modules/audio_device/android/opensles_input.cc index e276fcc523..5a6919a062 100644 --- a/webrtc/modules/audio_device/android/opensles_input.cc +++ b/webrtc/modules/audio_device/android/opensles_input.cc @@ -75,6 +75,9 @@ int32_t OpenSlesInput::SetAndroidAudioDeviceObjects(void* javaVM, return 0; } +void OpenSlesInput::ClearAndroidAudioDeviceObjects() { +} + int32_t OpenSlesInput::Init() { assert(!initialized_); diff --git a/webrtc/modules/audio_device/android/opensles_input.h b/webrtc/modules/audio_device/android/opensles_input.h index ea5144b70f..1ce02e5086 100644 --- a/webrtc/modules/audio_device/android/opensles_input.h +++ b/webrtc/modules/audio_device/android/opensles_input.h @@ -41,6 +41,7 @@ class OpenSlesInput { static int32_t SetAndroidAudioDeviceObjects(void* javaVM, void* env, void* context); + static void ClearAndroidAudioDeviceObjects(); // Main initializaton and termination int32_t Init(); diff --git a/webrtc/modules/audio_device/android/opensles_output.cc b/webrtc/modules/audio_device/android/opensles_output.cc index 3823305532..12f5f3814a 100644 --- a/webrtc/modules/audio_device/android/opensles_output.cc +++ b/webrtc/modules/audio_device/android/opensles_output.cc @@ -76,6 +76,10 @@ int32_t OpenSlesOutput::SetAndroidAudioDeviceObjects(void* javaVM, return 0; } +void OpenSlesOutput::ClearAndroidAudioDeviceObjects() { + AudioManagerJni::ClearAndroidAudioDeviceObjects(); +} + int32_t OpenSlesOutput::Init() { assert(!initialized_); diff --git a/webrtc/modules/audio_device/android/opensles_output.h b/webrtc/modules/audio_device/android/opensles_output.h index 38ca969a01..13acd8a713 100644 --- a/webrtc/modules/audio_device/android/opensles_output.h +++ b/webrtc/modules/audio_device/android/opensles_output.h @@ -41,6 +41,7 @@ class OpenSlesOutput : public PlayoutDelayProvider { static int32_t SetAndroidAudioDeviceObjects(void* javaVM, void* env, void* context); + static void ClearAndroidAudioDeviceObjects(); // Main initializaton and termination int32_t Init(); diff --git a/webrtc/voice_engine/voice_engine_impl.cc b/webrtc/voice_engine/voice_engine_impl.cc index 038609cab0..396a6c1491 100644 --- a/webrtc/voice_engine/voice_engine_impl.cc +++ b/webrtc/voice_engine/voice_engine_impl.cc @@ -149,12 +149,18 @@ int VoiceEngine::SetAndroidObjects(void* javaVM, void* env, void* context) { #ifdef WEBRTC_ANDROID #ifdef WEBRTC_ANDROID_OPENSLES - return AudioDeviceTemplate:: - SetAndroidAudioDeviceObjects(javaVM, env, context); + typedef AudioDeviceTemplate + AudioDeviceInstance; #else - return AudioDeviceTemplate:: - SetAndroidAudioDeviceObjects(javaVM, env, context); + typedef AudioDeviceTemplate + AudioDeviceInstance; #endif + if (javaVM && env && context) { + AudioDeviceInstance::SetAndroidAudioDeviceObjects(javaVM, env, context); + } else { + AudioDeviceInstance::ClearAndroidAudioDeviceObjects(); + } + return 0; #else return -1; #endif