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
41 changes: 31 additions & 10 deletions webrtc-jni/src/main/cpp/src/api/VideoFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ namespace jni
{
namespace
{
// A direct buffer over a plane, or throws: the allocation may fail, with or without an exception pending.
JavaLocalRef<jobject> NewPlane(JNIEnv * env, const char * name, const uint8_t * data, jlong size)
{
JavaLocalRef<jobject> plane(env, env->NewDirectByteBuffer(const_cast<uint8_t *>(data), size));

if (plane.get() == nullptr) {
ExceptionCheck(env);

throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "The %s plane could not be wrapped in a direct buffer", name)));
}

return plane;
}

// Throws unless the plane holds every byte a copy reads: the last row ends at the width.
void CheckPlane(JNIEnv * env, const char * name, const JavaRef<jobject> & data, jint stride, jint width, jint height)
{
Expand Down Expand Up @@ -187,20 +202,26 @@ namespace jni
return static_cast<jlong>(stride) * (height - 1) + width;
};

jobject yBuffer = env->NewDirectByteBuffer(const_cast<uint8_t *>(buffer->DataY()), planeSize(buffer->StrideY(), buffer->width(), buffer->height()));
jobject uBuffer = env->NewDirectByteBuffer(const_cast<uint8_t *>(buffer->DataU()), planeSize(buffer->StrideU(), buffer->ChromaWidth(), buffer->ChromaHeight()));
jobject vBuffer = env->NewDirectByteBuffer(const_cast<uint8_t *>(buffer->DataV()), planeSize(buffer->StrideV(), buffer->ChromaWidth(), buffer->ChromaHeight()));
JavaLocalRef<jobject> yBuffer = NewPlane(env, "Y", buffer->DataY(), planeSize(buffer->StrideY(), buffer->width(), buffer->height()));
JavaLocalRef<jobject> uBuffer = NewPlane(env, "U", buffer->DataU(), planeSize(buffer->StrideU(), buffer->ChromaWidth(), buffer->ChromaHeight()));
JavaLocalRef<jobject> vBuffer = NewPlane(env, "V", buffer->DataV(), planeSize(buffer->StrideV(), buffer->ChromaWidth(), buffer->ChromaHeight()));

jobject jBuffer = env->NewObject(javaClass->cls, javaClass->ctor, buffer->width(), buffer->height(),
yBuffer, buffer->StrideY(), uBuffer, buffer->StrideU(), vBuffer, buffer->StrideV());
JavaLocalRef<jobject> jBuffer(env, env->NewObject(javaClass->cls, javaClass->ctor, buffer->width(), buffer->height(),
yBuffer.get(), buffer->StrideY(), uBuffer.get(), buffer->StrideU(), vBuffer.get(), buffer->StrideV()));

SetHandle(env, jBuffer, buffer.get());
if (jBuffer.get() == nullptr) {
ExceptionCheck(env);

env->DeleteLocalRef(yBuffer);
env->DeleteLocalRef(uBuffer);
env->DeleteLocalRef(vBuffer);
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "The I420 buffer could not be created")));
}

// Last, once nothing can fail: the caller hands the wrapper its
// reference only when it gets the wrapper back.
SetHandle(env, jBuffer.get(), buffer.get());
ExceptionCheck(env);

return JavaLocalRef<jobject>(env, jBuffer);
return jBuffer;
}
}

Expand Down
12 changes: 10 additions & 2 deletions webrtc-jni/src/main/cpp/src/api/VideoTrackSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,24 @@ namespace jni
}

webrtc::scoped_refptr<webrtc::I420Buffer> i420BufferCopy = webrtc::I420Buffer::Copy(*i420Buffer);
i420BufferCopy->AddRef();

jint rotation = static_cast<jint>(frame.rotation());
jlong timestamp = frame.timestamp_us() * webrtc::kNumNanosecsPerMicrosec;

try {
JavaLocalRef<jobject> jBuffer = I420Buffer::toJava(env, i420BufferCopy);

// The reference the Java frame gives up when it is released.
i420BufferCopy->AddRef();

JavaLocalRef<jobject> jFrame(env, env->NewObject(javaFrameClass->cls, javaFrameClass->ctor, jBuffer.get(), rotation, timestamp));

if (jFrame.get() != nullptr) {
if (jFrame.get() == nullptr) {
// No frame got the buffer that would release it.
i420BufferCopy->Release();
ExceptionCheck(env);
}
else {
env->CallVoidMethod(sink, javaClass->onFrame, jFrame.get());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,22 @@ namespace jni
jlong timestamp = webrtc::TimeMicros() * webrtc::kNumNanosecsPerMicrosec;

webrtc::scoped_refptr<webrtc::I420Buffer> i420BufferCopy = webrtc::I420Buffer::Copy(*i420Buffer);
i420BufferCopy->AddRef();

try {
auto jresult = JavaEnums::toJava(env, result);
JavaLocalRef<jobject> jBuffer = I420Buffer::toJava(env, i420BufferCopy);

// The reference the Java frame gives up when it is released.
i420BufferCopy->AddRef();

JavaLocalRef<jobject> jFrame(env, env->NewObject(javaFrameClass->cls, javaFrameClass->ctor, jBuffer.get(), rotation, timestamp));

if (jFrame.get() != nullptr) {
if (jFrame.get() == nullptr) {
// No frame got the buffer that would release it.
i420BufferCopy->Release();
ExceptionCheck(env);
}
else {
env->CallVoidMethod(callback, javaClass->onCaptureResult, jresult.get(), jFrame.get());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Path;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import dev.onvoid.webrtc.media.audio.CustomAudioSource;
import dev.onvoid.webrtc.media.video.CustomVideoSource;
Expand Down Expand Up @@ -158,26 +159,46 @@ public MediaFileSource(String source, Duration timeout) throws IOException {
* @throws IOException if the source cannot be opened in time, or decoded.
*/
public MediaFileSource(String source, Duration timeout, boolean hardwareDecoding) throws IOException {
MediaReader reader = new MediaReader(source, timeout);

// Read while the reader is still ours: the player takes it over.
info = reader.getInfo();
this(new MediaReader(source, timeout), CustomVideoSource::new, CustomAudioSource::new,
hardwareDecoding);
}

videoSource = info.hasVideo() ? new CustomVideoSource() : null;
audioSource = info.hasAudio() ? new CustomAudioSource() : null;
/**
* Builds on a reader that is open, which this source takes over whether
* or not it is made. The suppliers make the media sources, which tests
* fail on purpose.
*/
MediaFileSource(MediaReader reader, Supplier<CustomVideoSource> videoSources,
Supplier<CustomAudioSource> audioSources, boolean hardwareDecoding) throws IOException {
MediaInfo readerInfo;
CustomVideoSource video = null;
CustomAudioSource audio = null;
MediaPlayer created;

try {
player = new MediaPlayer(reader, videoSource, audioSource, hardwareDecoding);
}
catch (IOException | RuntimeException | Error e) {
// The player did not take charge, so what was made here has to be
// unwound rather than left to leak.
disposeSources();
// Read while the reader is still ours: the player takes it over.
readerInfo = reader.getInfo();

reader.close();
video = readerInfo.hasVideo() ? videoSources.get() : null;
audio = readerInfo.hasAudio() ? audioSources.get() : null;

created = new MediaPlayer(reader, video, audio, hardwareDecoding);
}
catch (Throwable e) {
// Nothing was taken over by a player, so whatever was made here,
// the reader with its open file included, is released rather than
// left to leak. Closing a reader a player took over does nothing.
release(e, video != null ? video::dispose : null);
release(e, audio != null ? audio::dispose : null);
release(e, reader::close);

throw e;
}

info = readerInfo;
videoSource = video;
audioSource = audio;
player = created;
}

/**
Expand Down Expand Up @@ -314,4 +335,21 @@ private void disposeSources() {
}
}

/**
* Releases something made before the error, which a failing release only
* adds to.
*/
private static void release(Throwable error, Runnable release) {
if (release == null) {
return;
}

try {
release.run();
}
catch (Throwable e) {
error.addSuppressed(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public MediaPlayer(MediaReader reader, CustomVideoSource videoSource,
throw new IllegalArgumentException("A player needs at least one media source");
}

// Before the reader is taken over: should these fail, such as when the
// library of the sources cannot be loaded, the caller still has the
// reader to close.
long tableAddress = NativeApi.tableAddress();
long videoSourceHandle = videoSource != null ? NativeApi.handleOf(videoSource) : 0;
long audioSourceHandle = audioSource != null ? NativeApi.handleOf(audioSource) : 0;

// Taking the reader over rather than sharing it: the native player
// owns it from here, and a Java reader that still held the same
// pointer would release it a second time.
Expand All @@ -148,9 +155,7 @@ public MediaPlayer(MediaReader reader, CustomVideoSource videoSource,
throw new IOException("The reader is closed");
}

handle = create(readerHandle, NativeApi.tableAddress(),
videoSource != null ? NativeApi.handleOf(videoSource) : 0,
audioSource != null ? NativeApi.handleOf(audioSource) : 0,
handle = create(readerHandle, tableAddress, videoSourceHandle, audioSourceHandle,
hardwareDecoding);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import dev.onvoid.webrtc.media.audio.AudioLayer;
import dev.onvoid.webrtc.media.audio.AudioTrack;
import dev.onvoid.webrtc.media.audio.AudioTrackSink;
import dev.onvoid.webrtc.media.video.CustomVideoSource;
import dev.onvoid.webrtc.media.video.VideoTrack;
import dev.onvoid.webrtc.media.video.VideoTrackSink;

Expand Down Expand Up @@ -133,6 +134,32 @@ void missingFileFails() {
assertThrows(IOException.class, () -> new MediaFileSource(Paths.get("no-such-file.webm")));
}

@Test
void failedSetupReleasesReader() throws Exception {
// As when the library of the sources cannot be loaded: the video
// source was made, the audio source fails, and no player was made.
MediaReader reader = new MediaReader(asset());
AtomicInteger disposed = new AtomicInteger();

assertThrows(UnsatisfiedLinkError.class, () -> new MediaFileSource(reader,
() -> new CustomVideoSource() {

@Override
public void dispose() {
disposed.incrementAndGet();
super.dispose();
}
},
() -> {
throw new UnsatisfiedLinkError("no library");
},
false));

// Closed, which releases the file it had open.
assertThrows(IllegalStateException.class, reader::getInfo);
assertEquals(1, disposed.get());
}

@Test
void closesTwice() throws Exception {
MediaFileSource source = new MediaFileSource(asset());
Expand Down
4 changes: 1 addition & 3 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCDtlsTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
long handle = getNativeHandle();

return handle == 0 ? System.identityHashCode(this) : Long.hashCode(handle);
return nativeHashCode();
}

}
4 changes: 1 addition & 3 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCDtmfSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
long handle = getNativeHandle();

return handle == 0 ? System.identityHashCode(this) : Long.hashCode(handle);
return nativeHashCode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
long handle = getNativeHandle();

return handle == 0 ? System.identityHashCode(this) : Long.hashCode(handle);
return nativeHashCode();
}

private native RTCRtpSender addTrackInternal(MediaStreamTrack track,
Expand Down
4 changes: 1 addition & 3 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
long handle = getNativeHandle();

return handle == 0 ? System.identityHashCode(this) : Long.hashCode(handle);
return nativeHashCode();
}

}
4 changes: 1 addition & 3 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
long handle = getNativeHandle();

return handle == 0 ? System.identityHashCode(this) : Long.hashCode(handle);
return nativeHashCode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
long handle = getNativeHandle();

return handle == 0 ? System.identityHashCode(this) : Long.hashCode(handle);
return nativeHashCode();
}

}
Loading
Loading