Passing an array of bytes to Java without copying?
843829Jul 13 2001 — edited Jun 13 2003I have implemented a video capture device for the Java Media Framework that connects Java to Microsoft's DirectShow. I use the JNI to pass the video stream buffer to the JMF every time a new frame is available.
The C++ JNI function that passes the buffer on to Java looks something like this (note: I put "..." in places of tabs/spaces since this site seems to remove leading spaces from lines):
IMediaSample *gVideoFrame;
JNIEXPORT void JNICALL Java_com_teraglobal_GetDSFrame
(JNIEnv *inEnv, jobject inObj, jbyteArray theData)
{
....// Get the pointer to the image buffer
....jbyte *pSource = NULL;
....HRESULT hr = gVideoFrame->GetPointer((BYTE **)(&pSource));
....if (SUCCEEDED(hr))
....{
........// Copy the bytes from our array into Java's array (EEECH!!!)
........inEnv->SetByteArrayRegion(theData, 0, inEnv->GetArrayLength(theData), pSource);
....}
}
The problem: For each video frame, I need to do a memory copy to move the image from the native C buffer into a Java byte array. For a 320x240 frame that amounts to 150k+ memory move each time--this slows things down! I would like to pass this buffer pointer directly on to Java without the memory copy, but the JMF requires that the frame be in a "byte[]" Java Object.
My question: is there any way I can tell Java to use my pointer from the native C++ code when it allocates the "byte[]" array object?
Thanks.