aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/omx/OMXGLMediaPlayer.java
blob: 8f5f9c3abd2bc5052a5bb2ba9b21323a3006a737 (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

package jogamp.opengl.omx;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.media.opengl.GL;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLException;

import com.jogamp.opengl.av.GLMediaEventListener;
import com.jogamp.opengl.util.texture.Texture;

import jogamp.opengl.av.EGLMediaPlayerImpl;
import jogamp.opengl.egl.EGL;

public class OMXGLMediaPlayer extends EGLMediaPlayerImpl {
    protected long moviePtr = 0;
    
    /**
     * Old stream values, before the last attributesUpdated)
     */
    protected int o_width = 0;
    protected int o_height = 0;
    protected int o_fps = 0;
    protected long o_bps = 0;
    protected long o_totalFrames = 0;
        
    public OMXGLMediaPlayer() {
        super(TextureType.KHRImage, true);
        initOMX();
    }

    protected void initOMX() {
        moviePtr = _createInstance();
        if(0==moviePtr) {
            throw new GLException("Couldn't create OMXInstance");
        }        
    }
    
    @Override
    protected TextureFrame createTexImage(GLContext ctx, int idx, int[] tex) {
        final EGLTextureFrame eglTex = (EGLTextureFrame) super.createTexImage(ctx, idx, tex);
        _setStreamEGLImageTexture2D(moviePtr, idx, tex[idx], eglTex.getImage(), eglTex.getSync());
        return eglTex;
    }
    
    @Override
    protected void destroyTexImage(GLContext ctx, TextureFrame imgTex) {
        super.destroyTexImage(ctx, imgTex);
    }
    
    @Override
    protected void destroyImpl(GL gl) {
        _detachVideoRenderer(moviePtr);
        if (moviePtr != 0) {
            _destroyInstance(moviePtr);
            moviePtr = 0;
        }
    }
    
    @Override
    protected void setStreamImpl() throws IOException {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        String path=null;
        if (url.getProtocol() == null || "file".equals(url.getProtocol())) {
            // CV only accepts absolute paths
            try {
                File file = new File(url.getPath());
                if (!file.exists()) {
                    throw new FileNotFoundException(file.toString());
                }
                path = file.getCanonicalPath();
                System.out.println("setURL: path "+path);
            } catch (Exception e) {
                e.printStackTrace();
                throw new IOException(e);
            }
        }
        path = replaceAll(path, "\\", "/").trim();
        if(null==path) {
            throw new IOException("Couldn't parse stream URL: "+url);
        }
        System.out.println("setURL: clean path "+path);
    
        System.out.println("setURL: p1 "+this);
        _setStream(moviePtr, textureCount, path);
        System.out.println("setURL: p2 "+this);
    }
    
        
    @Override
    public synchronized int getCurrentPosition() {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        return _getCurrentPosition(moviePtr);
    }

    @Override
    public synchronized boolean isValid() {
        return (moviePtr != 0);
    }

    @Override
    public synchronized void setPlaySpeed(float rate) {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        _setPlaySpeed(moviePtr, rate);
        playSpeed = rate;
    }

    /** @return time position after issuing the command */
    @Override
    public synchronized void start() {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        _play(moviePtr);
    }

    /** @return time position after issuing the command */
    @Override
    public synchronized void pause() {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        _pause(moviePtr);
    }

    /** @return time position after issuing the command */
    @Override
    public synchronized void stop() {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        _stop(moviePtr);
    }

    /** @return time position after issuing the command */
    @Override
    public synchronized int seek(int msec) {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        return _seek(moviePtr, msec);
    }

    @Override
    public synchronized Texture getNextTextureID() {
        if(0==moviePtr) {
            throw new GLException("OMX native instance null");
        }
        texture=null;
        TextureFrame eglImgTex = texFrameMap.get(new Integer(_getNextTextureID(moviePtr)));
        if(null!=eglImgTex) {
            texture = eglImgTex.getTexture();
        }
        return texture;
    }

    protected void attributesUpdated() {
        int event_mask = 0;
        if( o_width != width || o_height != height ) {
            event_mask |= GLMediaEventListener.EVENT_CHANGE_SIZE;
        }   
        if( o_fps != fps ) {
            event_mask |= GLMediaEventListener.EVENT_CHANGE_FPS;
        }
        if( o_bps != bps ) {
            event_mask |= GLMediaEventListener.EVENT_CHANGE_BPS;
        }
        if( o_totalFrames != totalFrames ) {
            event_mask |= GLMediaEventListener.EVENT_CHANGE_LENGTH;
        }
        if(0==event_mask) {
            return;
        }
        super.attributesUpdated(event_mask);    
    }

    /**
     * Java callback method issued by the native OMX backend
     */
    private void saveAttributes() {
        o_width = width;
        o_height = height;
        o_fps = fps;
        o_bps = bps;
        o_totalFrames = totalFrames;
    }

    private String replaceAll(String orig, String search, String repl) {
        String dest=null;
        // In case replaceAll / java.util.regex.* is not supported (-> CVM)
        int i=0,j;
        dest = new String();
        while((j=orig.indexOf(search, i))>=0) {
            dest=dest.concat(orig.substring(i, j));
            dest=dest.concat(repl);
            i=j+1;
        }
        return dest.concat(orig.substring(i, orig.length()));
    }

    private void errorCheckEGL(String s) {
        int e;
        if( (e=EGL.eglGetError()) != EGL.EGL_SUCCESS ) {
            System.out.println("EGL Error: ("+s+"): 0x"+Integer.toHexString(e));
        }
    }

    //
    // OMXEventListener Support
    //

    native long _createInstance();    
    native void _destroyInstance(long moviePtr);
    
    native void _detachVideoRenderer(long moviePtr); // stop before
    native void _attachVideoRenderer(long moviePtr); // detach before
    native void _setStream(long moviePtr, int textureNum, String path);
    native void _activateStream(long moviePtr);
    
    native void _setStreamEGLImageTexture2D(long moviePtr, int i, int tex, long image, long sync);
    native int _seek(long moviePtr, int position);
    native void _setPlaySpeed(long moviePtr, float rate);
    native void _play(long moviePtr);
    native void _pause(long moviePtr);
    native void _stop(long moviePtr);
    native int  _getNextTextureID(long moviePtr);
    native int _getCurrentPosition(long moviePtr);
}