From 14ef6bca7ca5e0b54e0fa0010e1752cc84115a5c Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Tue, 27 Oct 2026 00:06:13 +0100 Subject: [PATCH] fix: share a SyncClock with the custom sources constructed with it The sources wrapped the clock in a shared_ptr with an empty deleter, so SyncClock.dispose() freed it while they still used it. The Java handle now holds a shared_ptr and the sources take their own reference. --- .../main/cpp/src/JNI_CustomAudioSource.cpp | 7 +-- .../main/cpp/src/JNI_CustomVideoSource.cpp | 7 +-- webrtc-jni/src/main/cpp/src/JNI_SyncClock.cpp | 23 ++++++--- .../dev/onvoid/webrtc/media/SyncClock.java | 4 ++ .../webrtc/media/audio/CustomAudioSource.java | 2 + .../webrtc/media/video/CustomVideoSource.java | 2 + .../media/audio/CustomAudioSourceTest.java | 29 +++++++++++ .../media/video/CustomVideoSourceTest.java | 50 +++++++++++++++++++ 8 files changed, 110 insertions(+), 14 deletions(-) diff --git a/webrtc-jni/src/main/cpp/src/JNI_CustomAudioSource.cpp b/webrtc-jni/src/main/cpp/src/JNI_CustomAudioSource.cpp index 595a18e2b..24cb00341 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_CustomAudioSource.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_CustomAudioSource.cpp @@ -38,11 +38,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_CustomAudioSource_init JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_CustomAudioSource_initializeWithClock (JNIEnv * env, jobject caller, jobject javaClock) { - jni::SyncClock * clock = GetHandle(env, javaClock); + auto clock = GetHandle>(env, javaClock); CHECK_HANDLE(clock); - std::shared_ptr sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting - webrtc::scoped_refptr source = webrtc::make_ref_counted(sync_clock); + // The source shares the clock, so disposing the Java clock while the + // source is still pushed to does not free it. + webrtc::scoped_refptr source = webrtc::make_ref_counted(*clock); SetHandle(env, caller, source.release()); } diff --git a/webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp b/webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp index a075fee29..f07b5a86c 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp @@ -39,11 +39,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_init JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_initializeWithClock (JNIEnv * env, jobject caller, jobject javaClock) { - jni::SyncClock * clock = GetHandle(env, javaClock); + auto clock = GetHandle>(env, javaClock); CHECK_HANDLE(clock); - std::shared_ptr sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting - webrtc::scoped_refptr source = webrtc::make_ref_counted(sync_clock); + // The source shares the clock, so disposing the Java clock while the + // source is still pushed to does not free it. + webrtc::scoped_refptr source = webrtc::make_ref_counted(*clock); SetHandle(env, caller, source.release()); } diff --git a/webrtc-jni/src/main/cpp/src/JNI_SyncClock.cpp b/webrtc-jni/src/main/cpp/src/JNI_SyncClock.cpp index 63ffcc021..53e735772 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_SyncClock.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_SyncClock.cpp @@ -20,11 +20,16 @@ #include "rtc_base/time_utils.h" +#include + JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_initialize (JNIEnv * env, jobject caller) { try { - jni::SyncClock * clock = new jni::SyncClock(); + // The handle holds a reference, not the clock itself: a custom source + // constructed with this clock takes its own, so disposing the Java + // clock leaves the native one alive for as long as a source uses it. + auto clock = new std::shared_ptr(std::make_shared()); SetHandle(env, caller, clock); } @@ -36,37 +41,39 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_initialize JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getTimestampUs (JNIEnv * env, jobject caller) { - jni::SyncClock * clock = GetHandle(env, caller); + auto clock = GetHandle>(env, caller); CHECK_HANDLEV(clock, 0); - return clock->GetTimestampUs(); + return (*clock)->GetTimestampUs(); } JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getTimestampMs (JNIEnv * env, jobject caller) { - jni::SyncClock * clock = GetHandle(env, caller); + auto clock = GetHandle>(env, caller); CHECK_HANDLEV(clock, 0); - return clock->GetTimestampMs(); + return (*clock)->GetTimestampMs(); } JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getNtpTime (JNIEnv * env, jobject caller) { - jni::SyncClock * clock = GetHandle(env, caller); + auto clock = GetHandle>(env, caller); CHECK_HANDLEV(clock, 0); - webrtc::NtpTime ntpTime = clock->GetNtpTime(); + webrtc::NtpTime ntpTime = (*clock)->GetNtpTime(); return ntpTime.ToMs(); } JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_dispose (JNIEnv * env, jobject caller) { - jni::SyncClock * clock = GetHandle(env, caller); + auto clock = GetHandle>(env, caller); CHECK_HANDLE(clock); + // Drops this wrapper's reference only; sources still using the clock + // keep theirs. delete clock; SetHandle(env, caller, nullptr); diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java index b16aee4fc..c0167644f 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java @@ -92,6 +92,10 @@ public SyncClock() { * Disposes of any native resources held by this clock. * This method should be called when the clock is no longer needed * to prevent memory leaks. + *

+ * A custom audio or video source constructed with this clock shares it, + * so the clock may be disposed while such a source is still in use: the + * source keeps the native clock until it is disposed itself. */ public native void dispose(); diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/CustomAudioSource.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/CustomAudioSource.java index 29671d08c..027c515b3 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/CustomAudioSource.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/CustomAudioSource.java @@ -56,6 +56,8 @@ public CustomAudioSource() { /** * Constructs a new CustomAudioSource instance with a specified SyncClock. + * The source shares the clock, which may therefore be disposed before + * this source. * * @param clock The SyncClock to use for timing and synchronization. */ diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/CustomVideoSource.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/CustomVideoSource.java index 586ba34e3..d35ec44ef 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/CustomVideoSource.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/CustomVideoSource.java @@ -37,6 +37,8 @@ public CustomVideoSource() { /** * Constructs a new CustomVideoSource instance with a specified SyncClock. + * The source shares the clock, which may therefore be disposed before + * this source. * * @param clock The SyncClock to use for timing and synchronization. */ diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/CustomAudioSourceTest.java b/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/CustomAudioSourceTest.java index 0aca70b63..8acfd931c 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/CustomAudioSourceTest.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/CustomAudioSourceTest.java @@ -240,6 +240,35 @@ void constructWithSyncClock() { clock.dispose(); } + @Test + void pushAudioAfterClockDisposed() { + // The source shares its clock, so disposing the Java clock first must + // leave the native one alive for as long as the source holds it. + SyncClock clock = new SyncClock(); + CustomAudioSource sourceWithClock = new CustomAudioSource(clock); + + clock.dispose(); + + AudioTrack audioTrack = factory.createAudioTrack("audioTrack", sourceWithClock); + + final AtomicInteger chunksReceived = new AtomicInteger(0); + AudioTrackSink testSink = (data, bits, rate, chans, frames) -> chunksReceived.incrementAndGet(); + + audioTrack.addSink(testSink); + + byte[] audioData = new byte[480 * 2 * 2]; // 10ms of 48kHz stereo 16-bit audio + + for (int i = 0; i < 3; i++) { + sourceWithClock.pushAudio(audioData, 16, 48000, 2, 480); + } + + assertEquals(3, chunksReceived.get(), "Not every chunk reached the sink"); + + audioTrack.removeSink(testSink); + audioTrack.dispose(); + sourceWithClock.dispose(); + } + private void testAudioFormat(int bitsPerSample, int sampleRate, int channels, int frameCount) { AudioTrack audioTrack = factory.createAudioTrack("audioTrack", customAudioSource); diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/media/video/CustomVideoSourceTest.java b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/CustomVideoSourceTest.java index bcd54e00e..f6216e6e0 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/media/video/CustomVideoSourceTest.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/CustomVideoSourceTest.java @@ -271,6 +271,56 @@ public void onVideoFrame(VideoFrame frame) { clock.dispose(); } + @Test + void pushFrameAfterClockDisposed() { + // The source shares its clock, so disposing the Java clock first must + // leave the native one alive for the source, with and without a + // capture time from the caller. + SyncClock clock = new SyncClock(); + CustomVideoSource sourceWithClock = new CustomVideoSource(clock); + + clock.dispose(); + + VideoTrack videoTrack = factory.createVideoTrack("videoTrack", sourceWithClock); + + final List timestamps = new ArrayList<>(); + + VideoTrackSink sink = frame -> timestamps.add(frame.timestampNs); + + videoTrack.addSink(sink); + + long[] beforeUs = new long[2]; + long[] afterUs = new long[2]; + + for (int i = 0; i < 2; i++) { + NativeI420Buffer buffer = NativeI420Buffer.allocate(320, 240); + VideoFrame frame = new VideoFrame(buffer, 0); + + beforeUs[i] = SyncClock.currentTimeUs(); + + if (i == 0) { + sourceWithClock.pushFrame(frame); + } + else { + sourceWithClock.pushFrame(frame, beforeUs[i]); + } + + afterUs[i] = SyncClock.currentTimeUs(); + + frame.release(); + } + + assertEquals(2, timestamps.size(), "Not every frame reached the sink"); + + for (int i = 0; i < 2; i++) { + assertPushedBetween(beforeUs[i], afterUs[i], timestamps.get(i)); + } + + videoTrack.removeSink(sink); + videoTrack.dispose(); + sourceWithClock.dispose(); + } + private static void assertPushedBetween(long beforeUs, long afterUs, long timestampNs) { long timestampUs = timestampNs / 1000;