Buffer creation from native memory
I want to construct a new org.freedesktop.gstreamer.Buffer object with data from a piece of native memory. For example, with the direct API, I could do something like this:
GstBuffer * gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
gsize maxsize, gsize offset, gsize size, gpointer user_data,
GDestroyNotify notify)
Is there any way I could do this using the java library? Neither Buffer nor GSTBufferApi seems to have this functionality?
The intention is to avoid having to allocate a new identical piece of memory and perform a copy into that when sending the buffer to GStreamer.
Where is your native memory coming from? If you can allocate via the bindings you could potentially use the Buffer constructor and map that / pass it around that way?
You could also map that one method via a JNA interface yourself and pass the pointer into Natives.objectFor(...).
We can consider PRs to extend the mapping with that functionality, but not until a stable v1 release is complete.
The memory is allocated by an external entity already. Importing the missing function with JNA seems like a simple solution for now, thanks!
Just for reference this was the temporary solution as suggested (many ways to handle enums, we just needed something that works). Natives.objectFor(...) is handled by JNA since Buffer already exists and can be defined as return type.
public interface NativeBufferWrapper extends Library {
public enum GstMemoryFlags {
GST_MEMORY_FLAGS_READONLY(2),
GST_MEMORY_FLAGS_NO_SHARE(16),
GST_MEMORY_FLAGS_ZERO_PREFIXED(32),
GST_MEMORY_FLAGS_ZERO_PADDED(64),
GST_MEMORY_FLAGS_PHYSICALLY_CONTIGUOUS(128),
GST_MEMORY_FLAGS_NOT_MAPPABLE(256),
GST_MEMORY_FLAGS_LAST(1048576);
public final int value;
GstMemoryFlags(int value) {
this.value = value;
}
}
NativeBufferWrapper INSTANCE = GstNative.load(NativeBufferWrapper.class);
@CallerOwnsReturn Buffer gst_buffer_new_wrapped_full (int gstMemoryFlags, Pointer data, int maxSize, int offset, int size, Pointer userData, GDestroyNotify destroyCallback);
}
Used in AppSrc like this:
Buffer buffer = NativeBufferWrapper.INSTANCE.gst_buffer_new_wrapped_full(NativeBufferWrapper.GstMemoryFlags.GST_MEMORY_FLAGS_READONLY.value, videoData.nativePointer(),
bufferSize, 0, bufferSize, Pointer.NULL, (Pointer pntr) -> {
// Deallocate it or decrease refcount if you are sharing it
});
. . .
source.pushBuffer(...);
While it's good that works, note that relying on internal mapping directly to Buffer is unsupported. It will break in the near future as it relies on behaviour in lowlevel and that is not intended to be API stable. I suggested using Natives.objectFor for a reason!
You may also want to look at NativeFlags under glib for that enum. https://github.com/gstreamer-java/gst1-java-core/blob/master/src/org/freedesktop/gstreamer/glib/NativeFlags.java
Ah, that's a good point - thanks for the heads-up!
Adding enhancement and post 1.0 labels - would be good to get this and other additional Buffer / Memory behaviour in once we have a stable release out.
Started looking at this, but punting forward again to 1.4. Need to consider API. Possibly direct ByteBuffer so as not to rely on JNA in public API.