nuclear@6: #include nuclear@6: #include nuclear@6: #include nuclear@6: #include "opengl.h" nuclear@6: #include "camera.h" nuclear@6: nuclear@6: nuclear@6: static JavaVM *jvm; nuclear@6: static JNIEnv *jni; nuclear@6: static jclass activity_class; nuclear@6: static unsigned int tex; nuclear@6: static int capturing; nuclear@6: nuclear@6: int cam_init(void *platform_data) nuclear@6: { nuclear@6: int glerr; nuclear@6: struct cam_android_platform_data *pdata = platform_data; nuclear@6: nuclear@6: jvm = pdata->vm; nuclear@6: jni = pdata->jni; nuclear@6: activity_class = pdata->activity_class; nuclear@6: nuclear@6: // create the camera texture nuclear@6: assert(glGetError() == GL_NO_ERROR); nuclear@6: glGenTextures(1, &tex); nuclear@6: glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex); nuclear@6: glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST); nuclear@6: glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); nuclear@6: nuclear@6: glerr = glGetError(); nuclear@6: assert(glerr == GL_NO_ERROR); nuclear@6: return glerr == GL_NO_ERROR ? 0 : -1; nuclear@6: } nuclear@6: nuclear@6: void cam_shutdown(void) nuclear@6: { nuclear@6: cam_stop_video(); nuclear@6: glDeleteTextures(1, &tex); nuclear@6: tex = 0; nuclear@6: } nuclear@6: nuclear@6: unsigned int cam_texture(void) nuclear@6: { nuclear@6: return tex; nuclear@6: } nuclear@6: nuclear@6: void cam_texture_size(int *w, int *h) nuclear@6: { nuclear@6: /* TODO */ nuclear@6: } nuclear@6: nuclear@6: int cam_start_video(void) nuclear@6: { nuclear@6: jmethodID method; nuclear@6: nuclear@6: if(!jvm) { nuclear@6: fprintf(stderr, "failed to start video, camera not initialized\n"); nuclear@6: return -1; nuclear@6: } nuclear@6: if(cam_is_capturing()) { nuclear@6: return 0; nuclear@6: } nuclear@6: nuclear@6: if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "start_video", "(I)I"))) { nuclear@6: fprintf(stderr, "failed to find static method: start_video\n"); nuclear@6: return -1; nuclear@6: } nuclear@6: if((*jni)->CallStaticIntMethod(jni, activity_class, method, tex) == -1) { nuclear@6: fprintf(stderr, "failed to start video capture\n"); nuclear@6: capturing = 0; nuclear@6: return -1; nuclear@6: } nuclear@6: capturing = 1; nuclear@6: nuclear@6: printf("video started\n"); nuclear@6: nuclear@6: return 1; nuclear@6: } nuclear@6: nuclear@6: int cam_stop_video(void) nuclear@6: { nuclear@6: jmethodID method; nuclear@6: nuclear@6: if(!jvm) { nuclear@6: fprintf(stderr, "failed to stop video, camera not initialized\n"); nuclear@6: return -1; nuclear@6: } nuclear@6: nuclear@6: if(!cam_is_capturing()) { nuclear@6: return 0; nuclear@6: } nuclear@6: nuclear@6: if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "stop_video", "()V"))) { nuclear@6: fprintf(stderr, "failed to find static method: stop_video\n"); nuclear@6: return -1; nuclear@6: } nuclear@6: (*jni)->CallStaticVoidMethod(jni, activity_class, method); nuclear@6: capturing = 0; nuclear@6: nuclear@6: printf("video stopped\n"); nuclear@6: nuclear@6: return 0; nuclear@6: } nuclear@6: nuclear@6: int cam_is_capturing(void) nuclear@6: { nuclear@6: return capturing; // XXX is it better to do this properly through JNI? nuclear@6: } nuclear@6: nuclear@6: int cam_take_picture(void) nuclear@6: { nuclear@6: return -1; // TODO nuclear@6: }