JNI : java - C // Using Global variables - java

I would like to know whether JNI accepts the use of global variables declared in the .cpp (and of type created by me).
It would be for a speech recognition program. In fact, I would like to use C to declare all the settings of the decoder and Java to manage the recording thing on Android. I don't want to create a Decoder class on Java because it should be use both on Android and iOS (I would like to have 3 separate parts :
1) set up the decoder (only in C)
2) do the recording and manage the threads (only in Android-java)
3) decode the audio (in java but calling C functions that will use the settings of the decoder)
Example :
function.cpp :
struct decoder {
char *model;
};
In the function_2.ccp file :
decoder *ps;
void set_decoder(char *modelToUse){
ps->model = modelToUse;
return;
}
void process(int *buf ) {
process_raw(ps, buf); // modify buf accordingly to the parameters of ps
return;
}
char *getHyp (){
return ps->hyp;
}
void main(){
ps = set_decoder(var1);
jclass = android_class = (*env)->FindClass(env,"android/app/nameofactivity");
// find method_ID
jmethodID android_mid = (*env)->GetStaticMethodID(env, android_class, "java_method",String_signature);
// call method
jString hyp_java = (*env)->CallIntMethod(env, android_class, android_mid, int samplerate);
EDIT (add these lines) :
const char *hyp_tmp = (*env)->GetStringUTFChars(env, hyp_java, 0);
const char *hyp = hyp_tmp;
printf(hyp_java);
(*env)->ReleaseStringUTFChars(env, hyp_java, hyp_tmp);
//END of EDIT
}
In the java file (the methods process and getHyp are declared as native methods ; the read method is a method from Android):
file.java
String java_method(int samplerate){
int buf[samplerate];
recorder.read(buf);
this.process(buf);
return this.getHyp();
}
Is it possible to do that?
EDIT : In brief, I wonder whether I can declare a global variable "ps" in C and then call a java function that will call other C functions that will use the value of the declared variable "ps".

Related

Unable to set Java int array field from JNI

I'm developing an Android App and I'm receiving camera data from a lib in C++. I need to send this data from C++ to the Java code. For this, I'm using JNI. I'm able to set different fields in Java from the JNI and the C++ data (like the name or the type of the camera), but I'm unable to set the ID Field because it's an uint8_t array.
How can I do this?
I already tried several ways to do this but each time I've got an SIGSEGV error with an invalid address. For others fields I'm using
env->Set<Primitives>Field(jobject, jfieldID, value)
method but there are no methods like that for int array, are there?
So, I've tried to set this field by calling a method from my class and provide the int array as parameter but this function failed and returned the SIGSEGV error.
Then, I searched on the web and I tried to set the field through
env->GetObjectField(jobject, jfieldID)
and
env->SetIntArrayRegion(jintArray, start, end, myIntArray)
but here the first method returns always null.
JavaVM * mJVM; //My Java Virtual Machine
jobject mCameraObject, mThreadObject; //Previously initialize to call functions in the right thread
void onReceiveCameraList(void *ptr, uint32_t /*id*/, my::lib::Camera *arrayCamera, uint32_t nbCameras) {
JNIEnv *env;
mJVM->AttachCurrentThread(&env, nullptr);
if (env->ExceptionCheck())
return;
//Get Field, Method ID, Object and Class
jclass cameraClass = env->GetObjectClass(mCameraObject);
jfieldID camIDField = env->GetFieldID(cameraClass, "idCam", "[I");
jfieldID camNameField = env->GetFieldID(cameraClass, "label", "Ljava/lang/String;");
jfieldID camConnectedField = env->GetFieldID(cameraClass, "connected", "Z");
jfieldID camTypeField = env->GetFieldID(cameraClass, "typeProduit", "B");
jmethodID camReceptionMID = env->GetMethodID(env->GetObjectClass(mThreadObject), "onCamerasReception", "([Lcom/my/path/models/Camera;)V"); //Java function
jobjectArray cameraArray = env->NewObjectArray(nbCameras, cameraClass, mCameraObject); //Object return in the functions
//Put the cameras into the vector
std::vector<my::lib::Camera> vectorCameras;
if(!vectorCameras.empty())
vectorCameras.clear();
if ((arrayCamera != nullptr) && (nbCameras > 0)) {
for (uint32_t i = 0; i < nbCameras; ++i) {
vectorCameras.push_back(arrayCamera[i]);
}
}
//Set the my::lib::Camera field into Java::Camera
int c= 0;
for (auto & cam : vectorCameras)
{
jobject camera = env->AllocObject(cameraClass); //Object Camera to add in cameraArray object
// MY DATA TO SET ID FIELD ///
jint idArray[16];
for (int i = 0; i < 16 ; ++i) {
idArray[i] = cam.idCamera.data[i]; // uint8_t cam.idCamera.data[16]
}
///////// FIRST WAY /////////
jmethodID setIDCamMID = env->GetMethodID(env->GetObjectClass(camera), "setIDCam", "([I)V");
env->CallVoidMethod(camera, setIDCamMID, idArray);
///////// SECOND WAY /////////
jintArray jintArray1 = (jintArray)env->GetObjectField(camera, camIDField);
env->SetIntArrayRegion(jintArray1, 0, 16, idArray);
//Set<Primitives>Field : WORKING
env->SetObjectField(camera, camNameField, env->NewStringUTF((const char *) cam.labelCamera));
env->SetBooleanField(camera, camConnectedField, cam.isCameraConnected);
jbyte type;
if (cam.typeCamera == my::lib::TYPE_1 || cam.typeCamera == my::lib::TYPE_2 || cam.typeCamera == my::lib::TYPE_3) //type not known in JAVA
type = 0;
else
type = cam.typeCamera;
env->SetByteField(camera, camTypeField, type);
//Put camera object into cameraArray object
env->SetObjectArrayElement(cameraArray, c++, camera);
}//for
//Call Java method with cameraArray
env->CallVoidMethod(mThreadObject, camReceptionMID, dpCameraArray);
}//onreceiveCamera
Can someone tell me if I made a mistake or am using it the wrong way?
Is there any other way to set this data?
This yields a C++ array with elements of type jint:
// MY DATA TO SET ID FIELD ///
jint idArray[16];
for (int i = 0; i < 16 ; ++i) {
idArray[i] = cam.idCamera.data[i]; // uint8_t cam.idCamera.data[16]
}
It is important to understand that that is not a Java array. Therefore, this ...
///////// FIRST WAY /////////
jmethodID setIDCamMID = env->GetMethodID(env->GetObjectClass(camera), "setIDCam", "([I)V");
env->CallVoidMethod(camera, setIDCamMID, idArray);
... is incorrect. idArray is not the right type (and does not decay to a pointer to the right type) for the corresponding parameter to the Java method you are trying to invoke.
On the other hand, this ...
///////// SECOND WAY /////////
jintArray jintArray1 = (jintArray)env->GetObjectField(camera, camIDField);
env->SetIntArrayRegion(jintArray1, 0, 16, idArray);
... is ok, provided that the field already contains a reference to an int[] of length at least 16. Since it didn't work for you, I take it that the field's initial value does not satisfy that criterion.
If you need to create a new Java int[], then
use JNI's NewIntArray() function, which returns a jintArray with the length you specify. Then
use the appropriate JNI methods for setting the elements (see below), and finally
either assign the array directly to the target object's field with SetObjectField() or use the object's setter method to do so, as in your first attempt.
As for setting elements of the Java array, SetIntArrayRegion() will work fine for that (given an actual Java array of sufficient length), but that does require you to allocate a separate native array of primitives (your idArray) from which to copy the values. A slightly more efficient approach would be to use GetPrimitiveArrayCritical() to let Java provide the buffer -- possibly a direct pointer to the internal data -- and then ReleasePrimitiveArrayCritical() when you're done. Something like this:
// It is assumed here that the length of the array is sufficient, perhaps because
// we just created this (Java) array.
jint *idArray = (jint *) env->GetPrimitiveArrayCritical(jintArray1, NULL);
for (uint32_t i = 0; i < nbCameras; ++i) {
idArray[i] = cam.idCamera.data[i];
}
env->ReleasePrimitiveArrayCritical(jintArray1, idArray, 0);
For the first approach, you need to create a Java int[] first, fill it from idArray, and then call your method:
jintArray j_arr = env->NewIntArray(16);
env->SetIntArrayRegion(j_arr, 0, 16, idArray);
env->CallVoidMethod(camera, setIDCamMID, j_arr);
Your second approach does not work because you never called a constructor that filled the idCam field.
You can do that from JNI, however:
env->SetObjectField(camera, camIDField, j_arr);

JNA : Correct mapping for COPYDATASTRUCT?

I have struggled a lot to make it work (a call to win32 API : SendMessage with WM_COPYDATA and COPYDATASTRUCT to hold the data) and as it works on my windows 7 computer now, I am wondering if my mapping is the good one and if there is no side effect of my solution ?
Here is my code :
/**
* For usage with WM_COPYDATA
* cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms649010(v=vs.85).aspx
*/
long SendMessage(HWND hWnd, int msg, WPARAM wParam, COPYDATASTRUCT.ByReference lParam);
int WM_COPYDATA = 0x004A;
//cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms649010(v=vs.85).aspx
class COPYDATASTRUCT extends Structure {
public static class ByReference extends COPYDATASTRUCT implements Structure.ByReference {
}
public COPYDATASTRUCT() {
super();
}
public int dwData;
public long cbData;
public Pointer lpData;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "dwData", "cbData", "lpData" });
}
}
And the calling code with 2 examples :
User32Extension.COPYDATASTRUCT.ByReference dataStruct = new User32Extension.COPYDATASTRUCT.ByReference();
String message = "Hello ! :-) !";
Memory m = new Memory(message.length() + 1);
m.setString(0, message);
dataStruct.dwData = 10;
dataStruct.cbData = message.length() + 1;
dataStruct.lpData = m;
dataStruct.write(); // writes to native memory the structure.
result = user32.SendMessage(hwndTarget, // target hwnd.
User32Extension.WM_COPYDATA, // copy data message.
wparam, // current hwnd
dataStruct // data by reference here
);
User32Extension.COPYDATASTRUCT.ByReference myDataStruct = new User32Extension.COPYDATASTRUCT.ByReference();
User32Extension.TEST_STRUCT myStruct = new User32Extension.TEST_STRUCT();
//simple C structure here with 4 fields of C types int, char, char and long.
myStruct.iNumber = 677;
myStruct.cCode = 'E';
myStruct.cCode2 = 'T';
myStruct.lLong1 = new NativeLong(123456789L);
myStruct.write();
LOGGER.trace("myStruct (size=" + myStruct.size() + ")=" + myStruct.toString(true));
myDataStruct.dwData = 11;
myDataStruct.cbData = myStruct.size();
myDataStruct.lpData = myStruct.getPointer();
myDataStruct.write(); // writes to native memory the structure.
result = user32.SendMessage(hwndTarget, // target hwnd.
User32Extension.WM_COPYDATA, // copy data message.
wparam, // current hwnd
myDataStruct // data
);
The key thing is this code compared to everything I have found on the net, is that COPYDATASTRUCT attribute cbData is of type long.
If I set to int, it does not work (data is not correctly received in WndProc of the legacy C application). Is it correct to map a DWORD to a long java type ? Would it be better with a NativeLong ?
Another thing, that is to be noted, is the explicit call to Structure.write() for all the instantiated Structures (myStruct and myDataStruct). It is necessary in order to not have an empty memory before calling the SendMessage api.
Do you think it is normal ? Or jna should call it automatically before invoking the SendMessage ?
Thanks in advance.
This native code:
typedef struct tagCOPYDATASTRUCT {
ULONG_PTR dwData;
DWORD cbData;
PVOID lpData;
} COPYDATASTRUCT, *PCOPYDATASTRUCT;
maps to this JNA Structure definition:
public class COPYDATASTRUCT extends Structure {
ULONG_PTR dwData;
DWORD cbData; // or use "int"
Pointer lpData;
}
The first and last fields will be of different size (and the structure will have different alignment/padding) depending on whether you're running 32- or 64-bit.

matlab calling Findclass JNI crashes

I´m trying to call java methods from simulink Matlab using JNI. I developed a small code in C++ that calls a main method which only prints helloworld on screen as a first test but at the line that it calls to find the class, matlab crashes.
The C++ code is this :
#include <stdio.h>
#include <jni.h>
#include "mex.h"
class MatlabAmbassador {
public:
MatlabAmbassador ();
//Destructor
~MatlabAmbassador ();
void run();
JNIEnv* create_vm() ;
void invoke_class(JNIEnv* env);
private:
}; // end class
MatlabAmbassador::MatlabAmbassador() {
}
MatlabAmbassador::~MatlabAmbassador() {
}
// -------------------------------------------------------------------------
JNIEnv* MatlabAmbassador::create_vm() {
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];
long status;
/* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=C:\\Apps\\Projetos em java\\Portico\\Portico_Agent\\bin";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
status=JNI_CreateJavaVM(&jvm, (void **)&env, &args);
return env;
}
// -------------------------------------------------------------------------
void MatlabAmbassador::invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;
mexPrintf("First\n");
helloWorldClass = env->FindClass("Teste"); <--- MATLAB CRASHES HERE
mexPrintf("second\n");
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
env->ExceptionClear() ;
}
if ( helloWorldClass == NULL ) {
mexPrintf( "%s%s\n", "Unable to obtain class reference for ",
helloWorldClass );
return;
} else {
mexPrintf( "%s%s\n", "Sucessfully created class reference for ",
helloWorldClass );
}
mainMethod = env->GetStaticMethodID( helloWorldClass, "main", "([Ljava/lang/String;)V");
applicationArgs = env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
applicationArg0 = env->NewStringUTF( "From-C-program");
env->SetObjectArrayElement( applicationArgs, 0, applicationArg0);
env->CallStaticVoidMethod( helloWorldClass, mainMethod, applicationArgs);
}
// -------------------------------------------------------------------------
void MatlabAmbassador::run() {
char str [80];
mexPrintf(" INITIALIZING....\n" );
JNIEnv* env = create_vm();
invoke_class( env );
}
// -------------------------------------------------------------------------
Simulink Matlab has some methods to be used. One of this method is used to call the method run() described above. Here follows a piece of code:
static void mdlStart(SimStruct *S)
{
char *buf;
size_t buflen;
int status;
buflen = mxGetN((ssGetSFcnParam(S, 2)))*sizeof(mxChar)+1 ; // le o 3o parametro passado pela funcao (nome do arq)
buf = (char *)mxMalloc(buflen); // aloca memoria
status = mxGetString((ssGetSFcnParam(S, 2)), buf,(mwSize)buflen);
ssGetPWork(S)[0] = (void *) new MatlabAmbassador; // store new C++ object in the
MatlabAmbassador *c = (MatlabAmbassador *) ssGetPWork(S)[0];
c->run();
and the simple java code is:
public class Teste
{
public static void main(String[] args)
{
System.out.println(" INITALIZING...");
}
}
So, can anyone explain what am I missing or explain if there is a real problem calling JNI inside Matlab. Matlab is version 2011b and java version installed is JDK 1.0.6_45.
I´d appreciate any help as soon as possible.
Best regards
André Nudel
andre.nudel#gmail.com
Are you sure JNI_CreateJavaVM succeeds? Your code is not checking the returned status code or resulting env value (which is not getting initialized, so it may contain garbage), and the crash is apparently occurring at the first attempt to use env.
If you're running this inside Matlab, the JVM creation may be failing because there's already a JVM running inside the process as part of Matlab's normal environment. The JNI documents say that "creation of multiple VMs in a single process is not supported" (http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html#wp636). If this is what's happening, JNI_CreateJavaVM will return JNI_EEXIST (-5). Even if that's not what's going on here, it's good practice to check the status codes returned from functions that can fail. The examples in the JNI documentation omit error checking "for clarity", but you should include it in code you're actually going to run.
Check the status returned by JNI_CreateJavaVM and print it out to make sure it's succeeding. And maybe initialize env to 0 so it's clear whether you got a pointer from JNI or just random data.
I am not very familiar with Simulink, so I'm showing an example of a regular MEX-function instead.
As suggested by #AndrewJanke, I retrieve the existing JVM instance running inside the MATLAB process instead of creating a new one.
jni.cpp
#include "mex.h"
#include "jni.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// get MATLAB Java virtual machine instance
JavaVM *vm = NULL;
int n; jint res;
res = JNI_GetCreatedJavaVMs(&vm, 1, (jsize*) &n);
if (res != JNI_OK || n != 1) {
mexErrMsgIdAndTxt("mex:jni", "Couldn't find existing Java VM");
}
// get JNI interface instance
JNIEnv *env = NULL;
res = vm->GetEnv((void**) &env, JNI_VERSION_1_6);
if (res != JNI_OK || env == NULL) {
mexErrMsgIdAndTxt("mex:jni", "Couldn't get Java JNI environment");
}
mexPrintf("using Java %d.%d\n",
env->GetVersion() >> 16, env->GetVersion() & 0xFFFF);
}
I compiled the file using:
mex -I"C:\Program Files\Java\jdk1.6.0_45\include"
-I"C:\Program Files\Java\jdk1.6.0_45\include\win32" jni.cpp
"C:\Program Files\Java\jdk1.6.0_45\lib\jvm.lib"
Run in MATLAB, I get:
>> jni
using Java 1.6
You should be able to extend the example above to call an external Java class. Note that I have not been able to pass data between MATLAB MEX-API and Java JNI API (by that I mean passing Java objects as they are to a MEX-function, or the other way around, passing some kind of jobject back directly as is to MATLAB, without conversion that is), as in:
>> x = java.lang.Double(1)
>> my_mex_jni_fcn(x)
The problem is that mxArray is an opaque type, so I do not know how to interpret the pointer returned by mxGetData

Memory leak in SWIG polymorphism across C++ and Java using directors

I have a C++ program to receive binary data from network. After data is received, it will callback the data as a byte array from C++ to a Java client. I use the director feature in SWIG to easily achieve cross language polymorphism across C++ and Java for callback. Here's a link.
But I found there is no typemaps in SWIG from char* in C++ to byte[] in Java. So, I added a patch to the virous.i. Here's a link.
/*Director specific typemaps*/
%typemap(directorin, descriptor="[B") char *BYTE {
jbyteArray jb = (jenv)->NewByteArray(strlen(BYTE));
(jenv)->SetByteArrayRegion(jb, 0, strlen(BYTE), (jbyte*)BYTE);
$input = jb;
}
%typemap(directorout) char *BYTE {
$1 = 0;
if($input){
$result = (char *) jenv->GetByteArrayElements($input, 0);
if(!$1)
return $null;
jenv->ReleaseByteArrayElements($input, $result, 0);
}
}
%typemap(javadirectorin) char *BYTE "$jniinput"
%typemap(javadirectorout) char *BYTE "$javacall"
For example. I have a class in C++ called A:
class A{
public:
virtual void onDataReceived(const char* BYTE, const size_t len) {}
};
Then in Java code I have another class B to extend A:
class B extends A {
#Override
public void onDataReceived(byte[] data, long len) {
//handling the data.
}
}
I can receive the byte array in Java code, but it seems it is never garbage collected by JVM. The onDataReceived method in SWIG generated wrapper file is like this:
void SwigDirector_A::onDataReceived(char const *BYTE, size_t const len) {
JNIEnvWrapper swigjnienv(this);
JNIEnv * jenv = swigjnienv.getJNIEnv();
jobject swigjobj = (jobject) NULL;
jbyteArray jBYTE = 0;
jlong jlen;
if (!swig_override[3]) {
A::onDataReceived(BYTE,len);
return;
}
swigjobj = swig_get_self(jenv);
if (swigjobj && jenv->IsSameObject(swigjobj, NULL) == JNI_FALSE) {
{
jbyteArray jb = (jenv)->NewByteArray(strlen(BYTE));
(jenv)->SetByteArrayRegion(jb, 0, strlen(BYTE), (jbyte*)BYTE);
jBYTE = jb;
}
jlen = (jlong) len;
jenv->CallStaticVoidMethod(Swig::jclass_DataTransferJNI, Swig::director_methids[3], swigjobj, jBYTE, jlen);
if (jenv->ExceptionCheck() == JNI_TRUE) return;
} else {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null upcall object");
}
if (swigjobj) jenv->DeleteLocalRef(swigjobj);
}
In my C++ code, I will delete the data in the receive buffer after the callback is done. But I checked from Java VisualVM that the memory used by java client process is not GC-ed after a long time. Thanks advance for any help!
PS. The data is quite large, it is around 32KB.
I got it solved by added deletion of reference to jb in Director typemaps: (jenv)->DeleteLocalRef(jb);
Here's the updated patch.

What is the best way to pass a jbyteArray from (objective-c) JNI to Java?

I'm currently retrieving image data from an iSight camera and I'd like to hand it over to Java for processing. I originally tried to put the data in a jbyteArray and return the jbyteArray. This works once per process. Calling the native function a second time will result in an invalid memory access.
Since I'm working with objective-c and Cocoa, I must use the JNF_COCOA_ENTER(...) and JNF_COCOA_EXIT(...) functions. Sadly, if I can't return the jbyteArray because doing so would result in JNF_COCOA_EXIT(...) not getting called. It was suggested to use a direct ByteBuffer to pass the data from JNI land to java land. Unfortunately, all the resources and references I've been using don't outline this straightforwardly enough for my brain to comprehend. I deeply apologize if this is a "duh" moment or has already been asked (I've searched with no luck), but...
1) What is the most efficient way to bring this image data to Java?
2) How should I use the ByteBuffer class to accomplish this? (if relevant)
Thanks!
Code might be helpful here. It's not as difficult as you might imagine once you get an idea of what those macros do, and how to separate the java and cocoa concerns from each other.
What you need to remember is that JNF_COCOA_ENTER and JNF_COCOA_EXIT do two main things for you:
They establish a local scope (so variables defined inside are not available outside - this is to help you not do "dumb" things with variables you shouldn't be touching) and also to set up an auto-release pool, so cocoa objects which are "autoreleased" inside that scope will be gone when the scope disappears. (this is part of why the local scope is useful/helpful) They'll also do some exception parsing so that you can catch Cocoa exceptions in Java.
That said, the following code IS LEGAL, you just need to be quite careful about managing memory access and ownership of data. Avoid mixing Java ownership and Objective-C ownership if possible, or else have your object manage the ownership, and clean up when the java object is GCed.
jbyteArray bytes;
JNF_COCOA_ENTER(env);
// Assign and full the bytes array here, doing any requisite transformations.
// Remember to COPY any data out of COCOA objects, as references will be dead soon!
JNF_COCOA_EXIT(env);
return bytes;
Using Java Objects from C is complex, but not unworkable. Still, the number of method calls is non-trivial, and the jump back and fourth is time consuming, so if you'll be calling this method often, or it's time critical, stick with the basic types as much as possible.
If you need to push data from Cocoa to Java from a delegate, things are a little bit more complicated, but not unworkable. Here's a segment from a project I manage called QTCubed, which does exactly that. didOutputVideoFrame is the delegate method, and this object MUST be initialized with the target Java Object Reference, and Java Environment from a JNI called method. Once initialized, it's then set as a delegate object, and receives updates from the camera.
#implementation QTKitCaptureDecompressedVideoOutput
- (QTKitCaptureDecompressedVideoOutput *)initWithEnv:(JNIEnv *) env javaObject:(jobject) javaObjectRef {
[super init];
// Save a reference to the VM
(*env)->GetJavaVM(env,&g_vm);
// Create a global reference to this object so we can access it later
objectRef = (*env)->NewGlobalRef(env,javaObjectRef);
return self;
}
- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection {
// Move into Java to deliver the data
JNIEnv *env;
(*g_vm)->AttachCurrentThread (g_vm, (void **) &env, NULL);
void * rawData = [sampleBuffer bytesForAllSamples];
int length = [sampleBuffer lengthForAllSamples];
QTFormatDescription * formatDescription = [sampleBuffer formatDescription];
QTTime duration = [sampleBuffer duration];
float frameDuration = duration.timeValue/duration.timeScale;
float fps = 1/frameDuration;
jint format = [formatDescription formatType];
NSValue * pixelSize = [formatDescription attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute];
NSSize size = [pixelSize sizeValue];
jint width = size.width;
jint height = size.height;
//NSLog(#"Outputting frame sized %d x %d of length %d with format: %#x",width,height,length,format);
switch (format) {
// 8 bit codecs
case kCVPixelFormatType_1Monochrome:
case kCVPixelFormatType_2Indexed:
case kCVPixelFormatType_4Indexed:
case kCVPixelFormatType_8Indexed:
case kCVPixelFormatType_1IndexedGray_WhiteIsZero:
case kCVPixelFormatType_2IndexedGray_WhiteIsZero:
case kCVPixelFormatType_4IndexedGray_WhiteIsZero:
case kCVPixelFormatType_8IndexedGray_WhiteIsZero:
case kCVPixelFormatType_422YpCbCr8:
case kCVPixelFormatType_4444YpCbCrA8:
case kCVPixelFormatType_4444YpCbCrA8R:
case kCVPixelFormatType_444YpCbCr8:
case kCVPixelFormatType_420YpCbCr8Planar:
case kCVPixelFormatType_422YpCbCr_4A_8BiPlanar:
case kCVPixelFormatType_24RGB:
case kCVPixelFormatType_24BGR:
default:
{
// Re-use the existing array if possible
if (byteFrameData == nil || (*env)->GetArrayLength(env,byteFrameData) < length) {
// Clean up the previously allocated global reference
if (byteFrameData != nil) {
(*env)->DeleteGlobalRef(env,byteFrameData);
byteFrameData = nil;
}
// Create an appropriately sized byte array to hold the data
byteFrameData = (*env)->NewGlobalRef(env,(*env)->NewByteArray(env,length));
}
if (byteFrameData) {
// Copy the raw data into the byteArray
(*env)->SetByteArrayRegion(env,byteFrameData,0,length,rawData);
// Get the class reference for our object
jclass classRef = (*env)->GetObjectClass(env,objectRef);
// Get the pushFrame methodId
jmethodID methodId = (*env)->GetMethodID(env,classRef,"pushFrame","([BIIIF)V");
// Call pushFrame with the byte array
(*env)->CallVoidMethod(env,objectRef,methodId,byteFrameData,format,width,height,fps);
}
break;
}
// 16 bit (short) storage of values
case kCVPixelFormatType_16BE555:
case kCVPixelFormatType_16LE555:
case kCVPixelFormatType_16LE5551:
case kCVPixelFormatType_16BE565:
case kCVPixelFormatType_16LE565:
case kCVPixelFormatType_16Gray:
case kCVPixelFormatType_422YpCbCr16:
case kCVPixelFormatType_422YpCbCr10:
case kCVPixelFormatType_444YpCbCr10:
{
// Re-use the existing array if possible
if (shortFrameData == nil || (*env)->GetArrayLength(env,shortFrameData) < length/2) {
// Clean up the previously allocated global reference
if (shortFrameData != nil) {
(*env)->DeleteGlobalRef(env,shortFrameData);
shortFrameData = nil;
}
// Create an appropriately sized byte array to hold the data
shortFrameData = (*env)->NewGlobalRef(env,(*env)->NewShortArray(env,length/2));
}
if (shortFrameData) {
// Copy the raw data into the byteArray
(*env)->SetShortArrayRegion(env,shortFrameData,0,length/2,rawData);
// Get the class reference for our object
jclass classRef = (*env)->GetObjectClass(env,objectRef);
// Get the pushFrame methodId
jmethodID methodId = (*env)->GetMethodID(env,classRef,"pushFrame","([SIIIF)V");
// Call pushFrame with the short array
(*env)->CallVoidMethod(env,objectRef,methodId,shortFrameData,format,width,height,fps);
}
break;
}
// 32 bit (int) storage of values
case kCVPixelFormatType_32ABGR:
case kCVPixelFormatType_32AlphaGray:
case kCVPixelFormatType_32ARGB:
case kCVPixelFormatType_32BGRA:
case kCVPixelFormatType_32RGBA:
{
// Re-use the existing array if possible
if (intFrameData == nil || (*env)->GetArrayLength(env,intFrameData) < length/4) {
// Clean up the previously allocated global reference
if (intFrameData != nil) {
(*env)->DeleteGlobalRef(env,intFrameData);
intFrameData = nil;
}
// Create an appropriately sized byte array to hold the data
intFrameData = (*env)->NewGlobalRef(env,(*env)->NewIntArray(env,length/4));
}
if (intFrameData) {
// Copy the raw data into the byteArray
(*env)->SetByteArrayRegion(env,intFrameData,0,length/4,rawData);
// Get the class reference for our object
jclass classRef = (*env)->GetObjectClass(env,objectRef);
// Get the pushFrame methodId
jmethodID methodId = (*env)->GetMethodID(env,classRef,"pushFrame","([IIIIF)V");
// Call pushFrame with the int array
(*env)->CallVoidMethod(env,objectRef,methodId,intFrameData,format,width,height,fps);
}
break;
}
}
// Detatch from Java
(*g_vm)->DetachCurrentThread (g_vm);
}
/* Clean up java references so they can be properly GCed in java */
- (void) dealloc {
// Attach to java so we can release references
JNIEnv *env;
(*g_vm)->AttachCurrentThread (g_vm, (void **) &env, NULL);
// release the references we hold
if (objectRef != nil) {
(*env)->DeleteGlobalRef(env,objectRef);
objectRef = nil;
}
if (byteFrameData != nil) {
(*env)->DeleteGlobalRef(env,byteFrameData);
byteFrameData = nil;
}
if (shortFrameData != nil) {
(*env)->DeleteGlobalRef(env,shortFrameData);
shortFrameData = nil;
}
if (intFrameData != nil) {
(*env)->DeleteGlobalRef(env,intFrameData);
intFrameData = nil;
}
// Detatch from Java
(*g_vm)->DetachCurrentThread (g_vm);
g_vm = nil;
[super dealloc];
}
#end
Here's the method signature on the java side for the java object reference that is passed in:
protected void pushFrame(byte[] frameData, int formatInt, int width, int height, float frameRate);
protected void pushFrame(short[] frameData, int formatInt, int width, int height, float frameRate);
protected void pushFrame(int[] frameData, int formatInt, int width, int height, float frameRate);
Once you have a running application, USE INSTRUMENTS TO MAKE SURE YOU'RE DISPOSING OF REFERENCES PROPERLY!

Categories