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
2 changes: 2 additions & 0 deletions docs/guide/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ peerConnection.close();
factory.dispose();
```

`dispose()` and `close()` may be called from any thread and more than once: only the first call frees anything, later ones do nothing. Once an object is disposed, its methods throw an `IllegalStateException` (a closed connection still reports its state). A method that another thread is running at that moment keeps the native object until it returns, and what the dispose frees is freed then.

## Next Steps

Now that you have a basic understanding of webrtc-java, you can explore more advanced features:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@
#include <vector>


// Return from a native method whose native object is gone, with an
// IllegalStateException thrown unless an exception is pending already. The
// handle is a raw pointer or a lease, which names its object's class.
#define CHECK_HANDLE(handle) \
do { \
if (handle == NULL) { \
JNIEnv * env = AttachCurrentThread(); \
env->Throw(jni::JavaNullPointerException(env, "Object handle is null")); \
if (!(handle)) { \
ThrowNullHandle(handle); \
return; \
} \
} while(0)

#define CHECK_HANDLEV(handle, retValue) \
do { \
if (handle == NULL) { \
JNIEnv * env = AttachCurrentThread(); \
env->Throw(jni::JavaNullPointerException(env, "Object handle is null")); \
if (!(handle)) { \
ThrowNullHandle(handle); \
return retValue; \
} \
} while(0)

#define CHECK_HANDLE_DEFAULT(handle, retValue) \
do { \
if (handle == NULL) { \
if (!(handle)) { \
return retValue; \
} \
} while(0)
Expand All @@ -53,6 +54,16 @@ void ThrowCxxJavaException(JNIEnv * env);
void SetJavaVM(JavaVM * vm);
JavaVM * GetJavaVM();
JNIEnv * AttachCurrentThread();

// Throws the IllegalStateException of a missing native object for
// CHECK_HANDLE on a raw pointer. A lease has an overload of its own, found by
// argument-dependent lookup, that names the object's class.
template<typename T>
void ThrowNullHandle(T *)
{
ThrowJavaException(AttachCurrentThread(), "java/lang/IllegalStateException", "Native object is disposed");
}

bool ExceptionCheck(JNIEnv * env);
jclass FindClass(JNIEnv * env, const char * name);
jmethodID GetMethod(JNIEnv * env, jclass cls, const char * name, const char * sig);
Expand Down
144 changes: 144 additions & 0 deletions webrtc-jni/src/main/cpp/include/NativeHandle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright 2026 Alex Andres
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef JNI_WEBRTC_NATIVE_HANDLE_H_
#define JNI_WEBRTC_NATIVE_HANDLE_H_

#include <jni.h>

#include <functional>

namespace jni
{
// The life of the native object behind NativeObject.nativeHandle, shared
// by every thread that calls into it.
//
// A native method uses the object through a NativeLease, which marks the
// handle in use for as long as the call runs. dispose() and close() take
// the handle with Take(), which unbinds it in one step: of several threads
// disposing the same object, one gets the handle, the others nothing, and
// no call starts using it afterwards. The object's owning reference is then
// given up with Release(): at once if no call uses the handle, otherwise by
// the last of those calls when it returns. Nothing waits for another
// thread, so a dispose from within a callback cannot deadlock with a call
// that waits for that callback's thread.
namespace NativeHandle
{
// Returns the bound handle of a NativeObject and marks it in use, or 0
// when the object is null or no longer bound. A non-zero handle must be
// given back with Unuse().
jlong Use(JNIEnv * env, jobject object);

// Ends a use begun with Use(), and runs the releases that waited for it
// if it was the last.
void Unuse(JNIEnv * env, jlong handle) noexcept;

// Unbinds the handle of a NativeObject and returns it. Returns 0 when
// the object is null or another thread has taken the handle before.
jlong Take(JNIEnv * env, jobject object);

// Gives up what a taken handle owns. The release runs right away, with
// deferred false, unless a call still uses the handle; then it runs on
// the thread of the last such call, when that call returns, with
// deferred true. A deferred release must not use the JNIEnv or the Java
// object of the call that took the handle.
void Release(JNIEnv * env, jlong handle, std::function<void(bool deferred)> release);

// Throws the IllegalStateException of a disposed object, unless an
// exception is pending already.
void ThrowDisposed(JNIEnv * env, jobject object) noexcept;

// Reports that a release left references to the native object behind:
// as a java.lang.Error when it ran right away, as a warning in the log
// when it was deferred, since the call it ran in is not the one to fail.
void ReportNotDeleted(bool deferred) noexcept;
}

// The native object of a NativeObject, kept from being released while the
// lease lives. Converts to T *, which is null when the object has been
// disposed: check it with CHECK_HANDLE, which then throws the object's
// IllegalStateException.
template <typename T>
class NativeLease
{
public:
NativeLease(JNIEnv * env, jobject object) :
env(env),
object(object),
handle(NativeHandle::Use(env, object))
{
}

~NativeLease()
{
if (handle != 0) {
NativeHandle::Unuse(env, handle);
}
}

NativeLease(const NativeLease &) = delete;
NativeLease & operator=(const NativeLease &) = delete;

T * get() const
{
return reinterpret_cast<T *>(handle);
}

operator T * () const
{
return get();
}

T * operator->() const
{
return get();
}

void throwDisposed() const
{
NativeHandle::ThrowDisposed(env, object);
}

private:
JNIEnv * env;
jobject object;
jlong handle;
};

// Found by argument-dependent lookup from CHECK_HANDLE.
template <typename T>
void ThrowNullHandle(const NativeLease<T> & lease)
{
lease.throwDisposed();
}

// NativeHandle::Take() for the native type the handle stands for. Null
// when the object has been disposed already.
template <typename T>
T * TakeHandle(JNIEnv * env, jobject object)
{
return reinterpret_cast<T *>(NativeHandle::Take(env, object));
}

// NativeHandle::Release() for a handle taken with TakeHandle().
template <typename T>
void ReleaseHandle(JNIEnv * env, T * object, std::function<void(bool deferred)> release)
{
NativeHandle::Release(env, reinterpret_cast<jlong>(object), std::move(release));
}
}

#endif
14 changes: 9 additions & 5 deletions webrtc-jni/src/main/cpp/src/JNI_AudioConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "JNI_AudioConverter.h"
#include "NativeHandle.h"
#include "api/audio/audio_processing.h"
#include "media/audio/AudioConverter.h"
#include "JavaUtils.h"
Expand All @@ -25,7 +26,7 @@
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_convertInternal
(JNIEnv * env, jobject caller, jbyteArray src, jint nSrcSamples, jbyteArray dst, jint nDstSamples)
{
jni::AudioConverter * converter = GetHandle<jni::AudioConverter>(env, caller);
jni::NativeLease<jni::AudioConverter> converter(env, caller);
CHECK_HANDLE(converter);

if (src == nullptr || dst == nullptr) {
Expand Down Expand Up @@ -73,12 +74,15 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_convert
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_dispose
(JNIEnv * env, jobject caller)
{
jni::AudioConverter * converter = GetHandle<jni::AudioConverter>(env, caller);
CHECK_HANDLE(converter);
jni::AudioConverter * converter = jni::TakeHandle<jni::AudioConverter>(env, caller);

SetHandle<std::nullptr_t>(env, caller, nullptr);
if (converter == nullptr) {
return;
}

delete converter;
jni::ReleaseHandle(env, converter, [converter](bool) {
delete converter;
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_initialize
Expand Down
Loading
Loading