Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace jni
private:
JavaVM * vm;
JNIEnv * env;
bool attached;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,30 @@

#include "JavaThreadEnv.h"

#include <iostream>
#include <thread>

namespace jni
{
JavaThreadEnv::JavaThreadEnv(JavaVM * vm) :
vm(vm),
env(nullptr)
env(nullptr),
attached(false)
{
int status = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);

if (status == JNI_EDETACHED) {
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), NULL) != 0) {
std::cout << "VM attach current thread failed" << std::endl;
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), NULL) == JNI_OK) {
attached = true;
}
else {
env = nullptr;
}
}

if (env == nullptr) {
std::cout << "Failed to attach thread " << std::this_thread::get_id() << std::endl;
}
}

JavaThreadEnv::~JavaThreadEnv()
{
vm->DetachCurrentThread();

//std::cout << "Dettached thread " << std::this_thread::get_id() << std::endl;
if (attached) {
vm->DetachCurrentThread();
}
}

JNIEnv * JavaThreadEnv::getEnv() const
Expand Down
4 changes: 4 additions & 0 deletions webrtc-jni/src/main/cpp/src/rtc/LogSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace jni
{
JNIEnv * env = AttachCurrentThread();

if (env == nullptr) {
return;
}

JavaLocalRef<jobject> jSeverity = JavaEnums::toJava(env, severity);
JavaLocalRef<jstring> jMessage = JavaString::toJava(env, message);

Expand Down
Loading