Version
menu_open
Wwise SDK 2023.1.5
AkJNIThread.h
Go to the documentation of this file.
1 /*******************************************************************************
2 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
3 released in source code form as part of the SDK installer package.
4 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Copyright (c) 2024 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 #pragma once
28 
29 #include <jni.h>
30 
31 namespace AKPLATFORM
32 {
33  /// Utility class for acquiring a pointer to a valid JNI environment attached to the current thread (with proper cleanup in destructor)
35  {
36  public:
37 
38  CAkJNIThread(JavaVM* jvm) : m_jvm(jvm), m_jEnv(NULL), m_mustDetachExplicitely(false), m_mustPopLocalFrame(false)
39  {
40  AcquireEnv(jvm, 0);
41  }
42 
43  CAkJNIThread(JavaVM* jvm, int numLocalRefs) : m_jvm(jvm), m_jEnv(NULL), m_mustDetachExplicitely(false), m_mustPopLocalFrame(false)
44  {
45  AcquireEnv(jvm, numLocalRefs);
46  }
47 
49  {
50  if (m_mustPopLocalFrame)
51  {
52  m_jEnv->PopLocalFrame(NULL);
53  m_mustPopLocalFrame = false;
54  }
55  if (m_jvm != NULL && m_jEnv != NULL && m_mustDetachExplicitely)
56  {
57  m_jvm->DetachCurrentThread();
58  m_mustDetachExplicitely = false;
59  }
60  }
61 
62  JNIEnv *getEnv()
63  {
64  return m_jEnv;
65  }
66 
67  bool isAttached()
68  {
69  return m_jEnv != NULL;
70  }
71 
72  private:
73  void AcquireEnv(JavaVM* jvm, int numLocalRefs)
74  {
75  if (!jvm)
76  return;
77 
78  jvm->GetEnv((void**)&m_jEnv, JNI_VERSION_1_6);
79  if (m_jEnv == NULL)
80  {
81  JavaVMAttachArgs lJavaVMAttachArgs;
82  lJavaVMAttachArgs.version = JNI_VERSION_1_6;
83  lJavaVMAttachArgs.name = "NativeThread";
84  lJavaVMAttachArgs.group = NULL;
85 
86  jvm->AttachCurrentThread(&m_jEnv, &lJavaVMAttachArgs);
87  m_mustDetachExplicitely = true;
88  }
89  if (m_jEnv && numLocalRefs > 0)
90  {
91  m_mustPopLocalFrame = 0 == m_jEnv->PushLocalFrame(numLocalRefs);
92  }
93  }
94 
95  JavaVM * m_jvm;
96  JNIEnv * m_jEnv;
97  bool m_mustDetachExplicitely: 1;
98  bool m_mustPopLocalFrame: 1;
99  };
100 }
Platform-dependent helpers.
Definition: AkBitFuncs.h:43
CAkJNIThread(JavaVM *jvm, int numLocalRefs)
Definition: AkJNIThread.h:43
#define NULL
Definition: AkTypes.h:46
Utility class for acquiring a pointer to a valid JNI environment attached to the current thread (with...
Definition: AkJNIThread.h:35
CAkJNIThread(JavaVM *jvm)
Definition: AkJNIThread.h:38

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise