aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/native/openmax/jogamp.opengl.omx.OMXGLMediaPlayer.c
blob: 00c0ca5623f5fe524c2e44ab6d9bf61a32c5690e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 *  media_video_Movie.c
 *  JFXFramework
 *
 *  Created by sun on 17/02/08.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */

// http://developer.apple.com/technotes/tn2005/tn2140.html
// http://developer.apple.com/qa/qa2005/qa1443.html
// http://developer.apple.com/documentation/QuickTime/Conceptual/QT7UpdateGuide/Chapter03/chapter_3_section_1.html#//apple_ref/doc/c_ref/NewMovieFromProperties
// http://developer.apple.com/qa/qa2001/qa1149.html
// http://developer.apple.com/qa/qa2001/qa1262.html

#include "jogamp_opengl_omx_OMXGLMediaPlayer.h"
#include "omx_tool.h"
#include <stdarg.h>

static const char * const ClazzNameRuntimeException =
                            "java/lang/RuntimeException";
static jclass    runtimeExceptionClz=NULL;
#ifdef _WIN32_WCE
    #define STDOUT_FILE "\\Storage Card\\demos\\stdout.txt"
    #define STDERR_FILE "\\Storage Card\\demos\\stderr.txt"
#endif

static void _initStatics(JNIEnv *env)
{
    jclass c;
#ifdef _WIN32_WCE
    _wfreopen(TEXT(STDOUT_FILE),L"w",stdout);
    _wfreopen(TEXT(STDERR_FILE),L"w",stderr);
#endif
    fprintf(stdout, "_initstatics ..\n"); fflush(stdout); // JAU
    if (runtimeExceptionClz != NULL) {
        return;
    }

    c = (*env)->FindClass(env, ClazzNameRuntimeException);
    if(NULL==c) {
        fprintf(stdout, "FatalError: can't find %s\n", ClazzNameRuntimeException);
        (*env)->FatalError(env, ClazzNameRuntimeException);
    }
    runtimeExceptionClz = (jclass)(*env)->NewGlobalRef(env, c);
    if(NULL==runtimeExceptionClz) {
        fprintf(stdout, "FatalError: can't use %s\n", ClazzNameRuntimeException);
        (*env)->FatalError(env, ClazzNameRuntimeException);
    }
}

void java_throwNewRuntimeException(intptr_t jni_env, const char* format, ...)
{
    va_list ap;
    char buffer[255];
    va_start(ap, format);
    #ifdef _WIN32
        _vsnprintf(buffer, sizeof(buffer)-1, format, ap);
    #else
        vsnprintf(buffer, sizeof(buffer)-1, format, ap);
    #endif
    va_end(ap);
    buffer[sizeof(buffer)-1]=0;
    fprintf(stderr, "RuntimeException: %s\n", buffer); fflush(stderr);
    if(jni_env!=0) {
        (*((JNIEnv *)jni_env))->ThrowNew((JNIEnv *)jni_env, runtimeExceptionClz, buffer);
    }
}

void OMXInstance_SaveJavaAttributes(OMXToolBasicAV_t *pOMXAV, KDboolean issueJavaCallback)
{
    if(NULL==pOMXAV || 0==pOMXAV->jni_env || 0==pOMXAV->jni_instance) {
        fprintf(stderr, "OMXInstance_SaveJavaAttributes failed");
        return;
    } else if(issueJavaCallback==KD_TRUE) {
        JNIEnv  * env = (JNIEnv *)pOMXAV->jni_env;
        jobject instance = (jobject)pOMXAV->jni_instance;
        (*env)->CallVoidMethod(env, instance, (jmethodID)pOMXAV->jni_mid_saveAttributes);
    }
}

void OMXInstance_UpdateJavaAttributes(OMXToolBasicAV_t *pOMXAV, KDboolean issueJavaCallback)
{
    if(NULL==pOMXAV || 0==pOMXAV->jni_env || 0==pOMXAV->jni_instance) {
        fprintf(stderr, "OMXInstance_UpdateJavaAttributes failed");
        return;
    } else {
        JNIEnv  * env = (JNIEnv *)pOMXAV->jni_env;
        jobject instance = (jobject)pOMXAV->jni_instance;
        (*env)->SetIntField(env, instance, (jfieldID)pOMXAV->jni_fid_width, (jint)pOMXAV->width);
        (*env)->SetIntField(env, instance, (jfieldID)pOMXAV->jni_fid_height, (jint)pOMXAV->height);
        (*env)->SetIntField(env, instance, (jfieldID)pOMXAV->jni_fid_fps, (jint)pOMXAV->framerate);
        (*env)->SetLongField(env, instance, (jfieldID)pOMXAV->jni_fid_bps, (jlong)pOMXAV->bitrate);
        (*env)->SetLongField(env, instance, (jfieldID)pOMXAV->jni_fid_totalFrames, (jlong)(pOMXAV->length*pOMXAV->framerate));
        if(issueJavaCallback==KD_TRUE) {
            (*env)->CallVoidMethod(env, instance, (jmethodID)pOMXAV->jni_mid_attributesUpdated);
        } else {
            if(strlen(pOMXAV->videoCodec)>0) {
                (*env)->SetObjectField(env, instance, (jfieldID)pOMXAV->jni_fid_vcodec, (*env)->NewStringUTF(env, pOMXAV->videoCodec));
            }
            if(strlen(pOMXAV->audioCodec)>0) {
                (*env)->SetObjectField(env, instance, (jfieldID)pOMXAV->jni_fid_acodec, (*env)->NewStringUTF(env, pOMXAV->audioCodec));
            }
        }
    }
}

JNIEXPORT jlong JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1createInstance
  (JNIEnv *env, jobject instance)
{
    OMXToolBasicAV_t * pOMXAV;

    _initStatics(env);

    pOMXAV->jni_env=(intptr_t)env;
    pOMXAV->jni_instance=(intptr_t)instance;

    pOMXAV = OMXToolBasicAV_CreateInstance((intptr_t)env, (intptr_t)instance);
    if(NULL!=pOMXAV) {
        jclass cls = (*env)->GetObjectClass(env, instance);
        pOMXAV->jni_mid_saveAttributes = (intptr_t) (*env)->GetMethodID(env, cls, "saveAttributes", "()V");
        pOMXAV->jni_mid_attributesUpdated = (intptr_t) (*env)->GetMethodID(env, cls, "attributesUpdated", "()V");
        pOMXAV->jni_fid_width = (intptr_t) (*env)->GetFieldID(env, cls, "width",  "I");
        pOMXAV->jni_fid_height = (intptr_t) (*env)->GetFieldID(env, cls, "height",  "I");
        pOMXAV->jni_fid_fps = (intptr_t) (*env)->GetFieldID(env, cls, "fps",  "I");
        pOMXAV->jni_fid_bps = (intptr_t) (*env)->GetFieldID(env, cls, "bps",  "J");
        pOMXAV->jni_fid_totalFrames = (intptr_t) (*env)->GetFieldID(env, cls, "totalFrames",  "J");
        pOMXAV->jni_fid_acodec = (intptr_t) (*env)->GetFieldID(env, cls, "acodec",  "Ljava/lang/String;");
        pOMXAV->jni_fid_vcodec = (intptr_t) (*env)->GetFieldID(env, cls, "vcodec",  "Ljava/lang/String;");
    }

    return (jlong) (intptr_t) (void *)pOMXAV;
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1setStream
  (JNIEnv *env, jobject instance, jlong ptr, jint vBufferNum, jstring jpath)
{
    jboolean iscopy;
    OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));

    fprintf(stdout, "setStream 1 ..\n"); fflush(stdout); // JAU
    if (pOMXAV != NULL) {
        const char *filePath = (*env)->GetStringUTFChars(env, jpath, &iscopy);
        fprintf(stdout, "setStream 2 %s..\n", filePath); fflush(stdout); // JAU
        pOMXAV->jni_env=(intptr_t)env;
        pOMXAV->jni_instance=(intptr_t)instance;
        OMXToolBasicAV_SetStream(pOMXAV, vBufferNum, filePath);
        (*env)->ReleaseStringChars(env, jpath, (const jchar *)filePath);
    }
    fprintf(stdout, "setStream 3 ..\n"); fflush(stdout); // JAU
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1setStreamEGLImageTexture2D
  (JNIEnv *env, jobject instance, jlong ptr, jint i, jint tex, jlong image, jlong sync)
{
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  if (pOMXAV != NULL) {
    OMXToolBasicAV_SetStreamEGLImageTexture2D( pOMXAV, i, (GLuint) tex, 
                                         (EGLImageKHR)(intptr_t)image,
                                         (EGLSyncKHR)(intptr_t)sync);
  }
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1activateStream
  (JNIEnv *env, jobject instance, jlong ptr)
{
    OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));

    if (pOMXAV != NULL) {
        OMXToolBasicAV_ActivateStream(pOMXAV);
    }
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1attachVideoRenderer
  (JNIEnv *env, jobject instance, jlong ptr)
{
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  OMXToolBasicAV_AttachVideoRenderer(pOMXAV);
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1detachVideoRenderer
  (JNIEnv *env, jobject instance, jlong ptr)
{
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  OMXToolBasicAV_DetachVideoRenderer(pOMXAV);
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1setPlaySpeed
  (JNIEnv *env, jobject instance, jlong ptr, jfloat scale)
{
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  OMXToolBasicAV_SetPlaySpeed(pOMXAV, scale);
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1play
  (JNIEnv *env, jobject instance, jlong ptr)
{
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  OMXToolBasicAV_PlayStart(pOMXAV);
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1pause
  (JNIEnv *env, jobject instance, jlong ptr)
{
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  OMXToolBasicAV_PlayPause(pOMXAV);
}

JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1stop
  (JNIEnv *env, jobject instance, jlong ptr)
{
    OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
    OMXToolBasicAV_PlayStop(pOMXAV);
}

JNIEXPORT jint JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1seek
  (JNIEnv *env, jobject instance, jlong ptr, jfloat pos)
{
    OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
    OMXToolBasicAV_PlaySeek(pOMXAV, pos);
    return OMXToolBasicAV_GetCurrentPosition(pOMXAV);
}

JNIEXPORT jint JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1getNextTextureID
  (JNIEnv *env, jobject instance, jlong ptr)
{
  jint textureID = 0xffffffff;
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  if (pOMXAV != NULL) {
      textureID = OMXToolBasicAV_GetNextTextureID(pOMXAV);
  }
  return textureID;
}

JNIEXPORT jint JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1getCurrentPosition
  (JNIEnv *env, jobject instance, jlong ptr)
{
    OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
    return OMXToolBasicAV_GetCurrentPosition(pOMXAV);
}


JNIEXPORT void JNICALL Java_jogamp_opengl_omx_OMXGLMediaPlayer__1destroyInstance
  (JNIEnv *env, jobject instance, jlong ptr)
{
  OMXToolBasicAV_t *pOMXAV = (OMXToolBasicAV_t *)((void *)((intptr_t)ptr));
  if (pOMXAV != NULL) {
    OMXToolBasicAV_DestroyInstance(pOMXAV);
  }
}