From 543bc6cf8ba0e5fdd6ea656e21fa8db9464993c0 Mon Sep 17 00:00:00 2001 From: J-RAP Date: Sun, 2 Jun 2024 04:10:59 -0600 Subject: [PATCH] fix random OPENGL error messages caused by WATERMeDIA good dammit this was broken for a long time --- .../caprica/vlcj/player/base/MediaPlayer.java | 8 +++++++ .../videosurface/CallbackVideoSurface.java | 2 +- .../api/player/SyncVideoPlayer.java | 21 ++++++------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/base/MediaPlayer.java b/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/base/MediaPlayer.java index 2c76cab9..47ef8f1a 100644 --- a/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/base/MediaPlayer.java +++ b/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/base/MediaPlayer.java @@ -64,6 +64,8 @@ public class MediaPlayer { */ private RendererItem renderer; + private volatile boolean released; + /** * Arbitrary object associated with this media list player. */ @@ -275,10 +277,16 @@ public final void submit(Runnable r) { executor.submit(r); } + public boolean isReleased() { + return released; + } + /** * Release the media player, freeing all associated (including native) resources. */ public final void release() { + this.released = true; + executor.release(); onBeforeRelease(); diff --git a/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/embedded/videosurface/CallbackVideoSurface.java b/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/embedded/videosurface/CallbackVideoSurface.java index 336e6487..9f23a211 100644 --- a/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/embedded/videosurface/CallbackVideoSurface.java +++ b/lib-vlcj/src/main/java/me/lib720/caprica/vlcj/player/embedded/videosurface/CallbackVideoSurface.java @@ -104,7 +104,7 @@ public int format(PointerByReference opaque, PointerByReference chroma, IntByRef */ private void applyBufferFormat(BufferFormat bufferFormat, PointerByReference chroma, IntByReference width, IntByReference height, PointerByReference pitches, PointerByReference lines) { byte[] chromaBytes = bufferFormat.getChroma().getBytes(); - chroma.getPointer().write(0, chromaBytes, 0, Math.min(chromaBytes.length, 4)); + chroma.getPointer().write(0, chromaBytes, 0, chromaBytes.length < 4 ? chromaBytes.length : 4); width.setValue(bufferFormat.getWidth()); height.setValue(bufferFormat.getHeight()); int[] pitchValues = bufferFormat.getPitches(); diff --git a/src/main/java/me/srrapero720/watermedia/api/player/SyncVideoPlayer.java b/src/main/java/me/srrapero720/watermedia/api/player/SyncVideoPlayer.java index aa7679f9..5f1ea7af 100644 --- a/src/main/java/me/srrapero720/watermedia/api/player/SyncVideoPlayer.java +++ b/src/main/java/me/srrapero720/watermedia/api/player/SyncVideoPlayer.java @@ -8,8 +8,8 @@ import org.lwjgl.opengl.GL11; import java.awt.*; -import java.nio.Buffer; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; @@ -24,7 +24,6 @@ public class SyncVideoPlayer extends SyncBasePlayer { private volatile int height = 1; private volatile ByteBuffer buffer; private volatile Throwable exception; - private final BufferHelper bufferHelper; protected final Executor playerThreadEx; protected final ReentrantLock renderLock = new ReentrantLock(); @@ -72,15 +71,12 @@ public SyncVideoPlayer(MediaPlayerFactory factory, Executor playerThreadEx, Buff super(); this.playerThreadEx = playerThreadEx; this.texture = GL11.glGenTextures(); - this.bufferHelper = bufferHelper; this.init(factory, (mediaPlayer, nativeBuffers, bufferFormat) -> { renderLock.lock(); // we are running in a native thread!! careful try { // FIXME: this increases allocation rate as HELL. we need to find out the source of the buffer pointers and allocate it directly - if (buffer == null) return; - ((Buffer) nativeBuffers[0]).rewind(); - buffer.put(nativeBuffers[0]); - ((Buffer) buffer).rewind(); + if (mediaPlayer.isReleased()) return; + this.buffer = nativeBuffers[0]; updateFrame.set(true); } catch (Throwable t) { if (exception == null) { @@ -95,11 +91,9 @@ public SyncVideoPlayer(MediaPlayerFactory factory, Executor playerThreadEx, Buff try { width = sourceWidth; height = sourceHeight; - if (updateFirstFrame.compareAndSet(true, true)) { - if (bufferHelper == DEFAULT_BUFFER_HELPER) RenderAPI.freeByteBuffer(buffer); - } - buffer = bufferHelper.create(sourceWidth * sourceHeight * 4); + buffer = ByteBuffer.allocateDirect(sourceWidth * sourceHeight * 4).order(ByteOrder.nativeOrder()); updateFrame.set(true); + updateFirstFrame.set(true); } catch (Throwable t) { if (exception == null) { exception = t; @@ -254,10 +248,7 @@ public Dimension getMediaDimensions() { */ @Override public void release() { - playerThreadEx.execute(() -> { - GL11.glDeleteTextures(texture); - if (bufferHelper == DEFAULT_BUFFER_HELPER) RenderAPI.freeByteBuffer(buffer); - }); + playerThreadEx.execute(() -> GL11.glDeleteTextures(texture)); super.release(); buffer = null; }