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
7 changes: 4 additions & 3 deletions webrtc-jni/src/main/cpp/src/JNI_CustomAudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<jni::SyncClock>(env, javaClock);
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, javaClock);
CHECK_HANDLE(clock);

std::shared_ptr<jni::SyncClock> sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting
webrtc::scoped_refptr<jni::CustomAudioSource> source = webrtc::make_ref_counted<jni::CustomAudioSource>(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<jni::CustomAudioSource> source = webrtc::make_ref_counted<jni::CustomAudioSource>(*clock);

SetHandle(env, caller, source.release());
}
Expand Down
7 changes: 4 additions & 3 deletions webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<jni::SyncClock>(env, javaClock);
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, javaClock);
CHECK_HANDLE(clock);

std::shared_ptr<jni::SyncClock> sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting
webrtc::scoped_refptr<jni::CustomVideoSource> source = webrtc::make_ref_counted<jni::CustomVideoSource>(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<jni::CustomVideoSource> source = webrtc::make_ref_counted<jni::CustomVideoSource>(*clock);

SetHandle(env, caller, source.release());
}
Expand Down
23 changes: 15 additions & 8 deletions webrtc-jni/src/main/cpp/src/JNI_SyncClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@

#include "rtc_base/time_utils.h"

#include <memory>

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<jni::SyncClock>(std::make_shared<jni::SyncClock>());

SetHandle(env, caller, clock);
}
Expand All @@ -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<jni::SyncClock>(env, caller);
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(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<jni::SyncClock>(env, caller);
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(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<jni::SyncClock>(env, caller);
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(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<jni::SyncClock>(env, caller);
auto clock = GetHandle<std::shared_ptr<jni::SyncClock>>(env, caller);
CHECK_HANDLE(clock);

// Drops this wrapper's reference only; sources still using the clock
// keep theirs.
delete clock;

SetHandle<std::nullptr_t>(env, caller, nullptr);
Expand Down
4 changes: 4 additions & 0 deletions webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> 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;

Expand Down
Loading