Skip to content

Commit

Permalink
fix random OPENGL error messages caused by WATERMeDIA
Browse files Browse the repository at this point in the history
good dammit this was broken for a long time
  • Loading branch information
SrRapero720 committed Jun 2, 2024
1 parent 24adf23 commit 543bc6c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class MediaPlayer {
*/
private RendererItem renderer;

private volatile boolean released;

/**
* Arbitrary object associated with this media list player.
*/
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 543bc6c

Please sign in to comment.