aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java
blob: 037ab779cb364f6f3239e8b8314d24f2197f4ab5 (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
package jogamp.opengl.android.av;

import java.io.IOException;

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

import jogamp.common.os.android.StaticContext;
import jogamp.opengl.av.GLMediaPlayerImpl;

import android.graphics.SurfaceTexture;
import android.graphics.SurfaceTexture.OnFrameAvailableListener;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.Surface;

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

/***
 * Android API Level 14: {@link MediaPlayer#setSurface(Surface)}
 * Android API Level 14: {@link Surface#Surface(android.graphics.SurfaceTexture)}
 */
public class AndroidGLMediaPlayerAPI14 extends GLMediaPlayerImpl {
    MediaPlayer mp;
    boolean updateSurface = false;
    Object updateSurfaceLock = new Object();
    AndroidTextureFrame atex = null;
    
    public static class AndroidTextureFrame extends TextureFrame {
        public AndroidTextureFrame(Texture t, SurfaceTexture stex) {
            super(t);
            this.stex = stex;
        }
        
        public final SurfaceTexture getSurfaceTexture() { return stex; }
        
        public String toString() {
            return "AndroidTextureFrame[" + texture + ", "+ stex + "]";
        }
        protected SurfaceTexture stex;
    }
    
    public AndroidGLMediaPlayerAPI14() {
        super();
        this.setTextureTarget(GLES2.GL_TEXTURE_EXTERNAL_OES);
        this.setTextureCount(1);
        mp = new MediaPlayer();
    }

    @Override
    public void setPlaySpeed(float rate) {
        // n/a
    }

    @Override
    public float getPlaySpeed() {
        return 0;
    }
    
    @Override
    protected boolean startImpl() {
        if(null != mp) {        
            try {
                mp.start();
                return true;
            } catch (IllegalStateException ise) {
                if(DEBUG) {
                    ise.printStackTrace();
                }
            }
        }
        return false;
    }

    @Override
    protected boolean pauseImpl() {
        if(null != mp) {
            try {
                mp.pause();
                return true;
            } catch (IllegalStateException ise) {
                if(DEBUG) {
                    ise.printStackTrace();
                }
            }
        }
        return false;
    }

    @Override
    protected boolean stopImpl() {
        if(null != mp) {
            try {
                mp.stop();
                return true;
            } catch (IllegalStateException ise) {
                if(DEBUG) {
                    ise.printStackTrace();
                }
            }
        }
        return false;
    }
    
    @Override
    protected long seekImpl(long msec) {
        if(null != mp) {
            mp.seekTo((int)msec);
            return mp.getCurrentPosition();
        }
        return 0;
    }

    @Override
    public TextureFrame getLastTexture() {
        return atex;
    }

    @Override
    public TextureFrame getNextTexture() {
        if(null != atex && null != mp) {
            final boolean _updateSurface;
            synchronized(updateSurfaceLock) {
                _updateSurface = updateSurface;
                updateSurface = false;
            }
            if(_updateSurface) {
                atex.getSurfaceTexture().updateTexImage();
                // atex.getSurfaceTexture().getTransformMatrix(atex.getSTMatrix());
            }
            return atex;
        } else {
            return null;
        }
    }
    
    @Override
    public long getCurrentPosition() {
        return null != mp ? mp.getCurrentPosition() : 0;
    }

    @Override
    protected void destroyImpl(GL gl) {
        if(null != mp) {
            mp.release();
            mp = null;
        }
    }
    
    @Override
    protected void initStreamImplPreGL() throws IOException {
        if(null!=mp && null!=url) {
            try {
                final Uri uri = Uri.parse(url.toExternalForm());        
                mp.setDataSource(StaticContext.getContext(), uri);
            } catch (IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (SecurityException e) {
                throw new RuntimeException(e);
            } catch (IllegalStateException e) {
                throw new RuntimeException(e);
            }
            try {
                mp.prepare();
            } catch (IOException ioe) {
                throw new IOException("MediaPlayer failed to process stream <"+url.toExternalForm()+">: "+ioe.getMessage(), ioe);
            }
            
            width = mp.getVideoWidth();
            height = mp.getVideoHeight();
            fps = 0;
            bps = 0;
            totalFrames = 0;
            duration = mp.getDuration();
            acodec = "unknown";
            vcodec = "unknown";
        }
    }
    
    @Override
    protected void destroyTexImage(GLContext ctx, TextureFrame imgTex) {
        final AndroidTextureFrame atf = (AndroidTextureFrame) imgTex;
        atf.getSurfaceTexture().release();
        atex = null;
        super.destroyTexImage(ctx, imgTex);
    }
    
    @Override
    protected AndroidTextureFrame createTexImage(GLContext ctx, int idx, int[] tex) {
        final Texture texture = super.createTexImageImpl(ctx, idx, tex, true);
                
        final SurfaceTexture stex = new SurfaceTexture(tex[idx]);
        stex.setOnFrameAvailableListener(onFrameAvailableListener);
        final Surface surf = new Surface(stex);
        mp.setSurface(surf);
        surf.release();
        
        atex = new AndroidTextureFrame( texture, stex );
        return atex;
    }

    protected OnFrameAvailableListener onFrameAvailableListener = new OnFrameAvailableListener() {
        @Override
        public void onFrameAvailable(SurfaceTexture surfaceTexture) {
            synchronized(updateSurfaceLock) {
                updateSurface = true;
            }
            AndroidGLMediaPlayerAPI14.this.newFrameAvailable(atex);
        }        
    };        
}