Skip to content

Commit 14ef6bc

Browse files
committed
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.
1 parent fd8eb0e commit 14ef6bc

8 files changed

Lines changed: 110 additions & 14 deletions

File tree

‎webrtc-jni/src/main/cpp/src/JNI_CustomAudioSource.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_CustomAudioSource_init
3838
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_CustomAudioSource_initializeWithClock
3939
(JNIEnv * env, jobject caller, jobject javaClock)
4040
{
41-
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, javaClock);
41+
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, javaClock);
4242
CHECK_HANDLE(clock);
4343

44-
std::shared_ptr<jni::SyncClock> sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting
45-
webrtc::scoped_refptr<jni::CustomAudioSource> source = webrtc::make_ref_counted<jni::CustomAudioSource>(sync_clock);
44+
// The source shares the clock, so disposing the Java clock while the
45+
// source is still pushed to does not free it.
46+
webrtc::scoped_refptr<jni::CustomAudioSource> source = webrtc::make_ref_counted<jni::CustomAudioSource>(*clock);
4647

4748
SetHandle(env, caller, source.release());
4849
}

‎webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_init
3939
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_initializeWithClock
4040
(JNIEnv * env, jobject caller, jobject javaClock)
4141
{
42-
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, javaClock);
42+
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, javaClock);
4343
CHECK_HANDLE(clock);
4444

45-
std::shared_ptr<jni::SyncClock> sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting
46-
webrtc::scoped_refptr<jni::CustomVideoSource> source = webrtc::make_ref_counted<jni::CustomVideoSource>(sync_clock);
45+
// The source shares the clock, so disposing the Java clock while the
46+
// source is still pushed to does not free it.
47+
webrtc::scoped_refptr<jni::CustomVideoSource> source = webrtc::make_ref_counted<jni::CustomVideoSource>(*clock);
4748

4849
SetHandle(env, caller, source.release());
4950
}

‎webrtc-jni/src/main/cpp/src/JNI_SyncClock.cpp‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020

2121
#include "rtc_base/time_utils.h"
2222

23+
#include <memory>
24+
2325
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_initialize
2426
(JNIEnv * env, jobject caller)
2527
{
2628
try {
27-
jni::SyncClock * clock = new jni::SyncClock();
29+
// The handle holds a reference, not the clock itself: a custom source
30+
// constructed with this clock takes its own, so disposing the Java
31+
// clock leaves the native one alive for as long as a source uses it.
32+
auto clock = new std::shared_ptr<jni::SyncClock>(std::make_shared<jni::SyncClock>());
2833

2934
SetHandle(env, caller, clock);
3035
}
@@ -36,37 +41,39 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_initialize
3641
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getTimestampUs
3742
(JNIEnv * env, jobject caller)
3843
{
39-
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
44+
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, caller);
4045
CHECK_HANDLEV(clock, 0);
4146

42-
return clock->GetTimestampUs();
47+
return (*clock)->GetTimestampUs();
4348
}
4449

4550
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getTimestampMs
4651
(JNIEnv * env, jobject caller)
4752
{
48-
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
53+
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, caller);
4954
CHECK_HANDLEV(clock, 0);
5055

51-
return clock->GetTimestampMs();
56+
return (*clock)->GetTimestampMs();
5257
}
5358

5459
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getNtpTime
5560
(JNIEnv * env, jobject caller)
5661
{
57-
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
62+
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, caller);
5863
CHECK_HANDLEV(clock, 0);
5964

60-
webrtc::NtpTime ntpTime = clock->GetNtpTime();
65+
webrtc::NtpTime ntpTime = (*clock)->GetNtpTime();
6166
return ntpTime.ToMs();
6267
}
6368

6469
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_dispose
6570
(JNIEnv * env, jobject caller)
6671
{
67-
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
72+
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, caller);
6873
CHECK_HANDLE(clock);
6974

75+
// Drops this wrapper's reference only; sources still using the clock
76+
// keep theirs.
7077
delete clock;
7178

7279
SetHandle<std::nullptr_t>(env, caller, nullptr);

‎webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public SyncClock() {
9292
* Disposes of any native resources held by this clock.
9393
* This method should be called when the clock is no longer needed
9494
* to prevent memory leaks.
95+
* <p>
96+
* A custom audio or video source constructed with this clock shares it,
97+
* so the clock may be disposed while such a source is still in use: the
98+
* source keeps the native clock until it is disposed itself.
9599
*/
96100
public native void dispose();
97101

‎webrtc/src/main/java/dev/onvoid/webrtc/media/audio/CustomAudioSource.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public CustomAudioSource() {
5656

5757
/**
5858
* Constructs a new CustomAudioSource instance with a specified SyncClock.
59+
* The source shares the clock, which may therefore be disposed before
60+
* this source.
5961
*
6062
* @param clock The SyncClock to use for timing and synchronization.
6163
*/

‎webrtc/src/main/java/dev/onvoid/webrtc/media/video/CustomVideoSource.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public CustomVideoSource() {
3737

3838
/**
3939
* Constructs a new CustomVideoSource instance with a specified SyncClock.
40+
* The source shares the clock, which may therefore be disposed before
41+
* this source.
4042
*
4143
* @param clock The SyncClock to use for timing and synchronization.
4244
*/

‎webrtc/src/test/java/dev/onvoid/webrtc/media/audio/CustomAudioSourceTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,35 @@ void constructWithSyncClock() {
240240
clock.dispose();
241241
}
242242

243+
@Test
244+
void pushAudioAfterClockDisposed() {
245+
// The source shares its clock, so disposing the Java clock first must
246+
// leave the native one alive for as long as the source holds it.
247+
SyncClock clock = new SyncClock();
248+
CustomAudioSource sourceWithClock = new CustomAudioSource(clock);
249+
250+
clock.dispose();
251+
252+
AudioTrack audioTrack = factory.createAudioTrack("audioTrack", sourceWithClock);
253+
254+
final AtomicInteger chunksReceived = new AtomicInteger(0);
255+
AudioTrackSink testSink = (data, bits, rate, chans, frames) -> chunksReceived.incrementAndGet();
256+
257+
audioTrack.addSink(testSink);
258+
259+
byte[] audioData = new byte[480 * 2 * 2]; // 10ms of 48kHz stereo 16-bit audio
260+
261+
for (int i = 0; i < 3; i++) {
262+
sourceWithClock.pushAudio(audioData, 16, 48000, 2, 480);
263+
}
264+
265+
assertEquals(3, chunksReceived.get(), "Not every chunk reached the sink");
266+
267+
audioTrack.removeSink(testSink);
268+
audioTrack.dispose();
269+
sourceWithClock.dispose();
270+
}
271+
243272
private void testAudioFormat(int bitsPerSample, int sampleRate, int channels, int frameCount) {
244273
AudioTrack audioTrack = factory.createAudioTrack("audioTrack", customAudioSource);
245274

‎webrtc/src/test/java/dev/onvoid/webrtc/media/video/CustomVideoSourceTest.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,56 @@ public void onVideoFrame(VideoFrame frame) {
271271
clock.dispose();
272272
}
273273

274+
@Test
275+
void pushFrameAfterClockDisposed() {
276+
// The source shares its clock, so disposing the Java clock first must
277+
// leave the native one alive for the source, with and without a
278+
// capture time from the caller.
279+
SyncClock clock = new SyncClock();
280+
CustomVideoSource sourceWithClock = new CustomVideoSource(clock);
281+
282+
clock.dispose();
283+
284+
VideoTrack videoTrack = factory.createVideoTrack("videoTrack", sourceWithClock);
285+
286+
final List<Long> timestamps = new ArrayList<>();
287+
288+
VideoTrackSink sink = frame -> timestamps.add(frame.timestampNs);
289+
290+
videoTrack.addSink(sink);
291+
292+
long[] beforeUs = new long[2];
293+
long[] afterUs = new long[2];
294+
295+
for (int i = 0; i < 2; i++) {
296+
NativeI420Buffer buffer = NativeI420Buffer.allocate(320, 240);
297+
VideoFrame frame = new VideoFrame(buffer, 0);
298+
299+
beforeUs[i] = SyncClock.currentTimeUs();
300+
301+
if (i == 0) {
302+
sourceWithClock.pushFrame(frame);
303+
}
304+
else {
305+
sourceWithClock.pushFrame(frame, beforeUs[i]);
306+
}
307+
308+
afterUs[i] = SyncClock.currentTimeUs();
309+
310+
frame.release();
311+
}
312+
313+
assertEquals(2, timestamps.size(), "Not every frame reached the sink");
314+
315+
for (int i = 0; i < 2; i++) {
316+
assertPushedBetween(beforeUs[i], afterUs[i], timestamps.get(i));
317+
}
318+
319+
videoTrack.removeSink(sink);
320+
videoTrack.dispose();
321+
sourceWithClock.dispose();
322+
}
323+
274324
private static void assertPushedBetween(long beforeUs, long afterUs, long timestampNs) {
275325
long timestampUs = timestampNs / 1000;
276326

0 commit comments

Comments
 (0)