Skip to content

Instantly share code, notes, and snippets.

@androuino
Created October 11, 2019 05:16
Show Gist options
  • Save androuino/b573c497784b922a340c4fe716c9de89 to your computer and use it in GitHub Desktop.
Save androuino/b573c497784b922a340c4fe716c9de89 to your computer and use it in GitHub Desktop.
Direct Media Player Component
private inner class CanvasPlayerComponent : DirectMediaPlayerComponent(CanvasBufferFormatCallback()) {
internal var pixelWriter: PixelWriter? = null
private val pw: PixelWriter?
get() {
if (pixelWriter == null) {
pixelWriter = writableImage?.getPixelWriter()
}
return pixelWriter
}
override fun display(
mediaPlayer: DirectMediaPlayer?,
nativeBuffers: Array<Memory>?,
bufferFormat: BufferFormat?
) {
if (writableImage == null) {
return
}
Platform.runLater {
try {
val nativeBuffer = mediaPlayer!!.lock()[0]
val byteBuffer = nativeBuffer.getByteBuffer(0, nativeBuffer.size())
val y = stackPaneMain.height.minus(bufferFormat?.height!!).div(2).toInt()
pw!!.setPixels(
0,
y,
bufferFormat.width,
bufferFormat.height,
writablePixelFormat,
byteBuffer,
bufferFormat.pitches[0]
)
} catch (e: Exception) {
log.warn("nativeBuffer is null:", e.message)
} finally {
mediaPlayer?.unlock()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment