-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathCommonVideoPlayerDesktop.java
337 lines (292 loc) · 8.19 KB
/
CommonVideoPlayerDesktop.java
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*******************************************************************************
* Copyright 2014 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.video;
import static com.badlogic.gdx.Application.LOG_DEBUG;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Null;
import com.badlogic.gdx.video.VideoDecoder.VideoDecoderBuffers;
/** Desktop implementation of the VideoPlayer
*
* @author Rob Bogie rob.bogie@codepoke.net */
abstract public class CommonVideoPlayerDesktop extends AbstractVideoPlayer implements VideoPlayer {
VideoDecoder decoder;
Texture texture;
Music audio;
boolean showAlreadyDecodedFrame = false;
boolean paused = false;
boolean looping = false;
boolean isFirstFrame = true;
int currentVideoWidth, currentVideoHeight;
float currentVideoTime;
int videoBufferWidth;
VideoSizeListener sizeListener;
CompletionListener completionListener;
FileHandle currentFile;
BufferedInputStream inputStream;
ReadableByteChannel fileChannel;
boolean playing = false;
public CommonVideoPlayerDesktop () {
}
abstract Music createMusic (VideoDecoder decoder, ByteBuffer audioBuffer, int audioChannels, int sampleRate);
private int getTextureWidth () {
return videoBufferWidth;
}
private int getTextureHeight () {
return currentVideoHeight;
}
@Override
public boolean play (FileHandle file) throws FileNotFoundException {
if (file == null) {
return false;
}
if (!file.exists()) {
throw new FileNotFoundException("Could not find file: " + file.path());
}
if (!FfMpeg.isLoaded()) {
FfMpeg.loadLibraries();
}
if (decoder != null) {
// Do all the cleanup
stop();
}
VideoDecoder.setDebug(Gdx.app.getLogLevel() >= LOG_DEBUG);
currentFile = file;
inputStream = file.read(256 * 1024);
fileChannel = Channels.newChannel(inputStream);
isFirstFrame = true;
decoder = new VideoDecoder();
VideoDecoderBuffers buffers;
try {
buffers = decoder.loadStream(new VideoDecoder.VideoFileReader() {
@Override
public int fillBuffer (ByteBuffer buffer) {
return readFileContents(buffer);
}
});
if (buffers != null) {
ByteBuffer audioBuffer = buffers.getAudioBuffer();
if (audioBuffer != null) {
if (audio != null) audio.dispose();
audio = createMusic(decoder, audioBuffer, buffers.getAudioChannels(), buffers.getAudioSampleRate());
}
currentVideoWidth = buffers.getVideoWidth();
currentVideoHeight = buffers.getVideoHeight();
videoBufferWidth = buffers.getVideoBufferWidth();
if (texture != null && (texture.getWidth() != getTextureWidth() || texture.getHeight() != getTextureHeight())) {
texture.dispose();
texture = null;
}
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
if (sizeListener != null) {
sizeListener.onVideoSize(currentVideoWidth, currentVideoHeight);
}
playing = true;
return true;
}
/** Called by jni to fill in the file buffer.
*
* @param buffer The buffer that needs to be filled
* @return The amount that has been filled into the buffer. */
private int readFileContents (ByteBuffer buffer) {
try {
buffer.rewind();
return fileChannel.read(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean update () {
if (decoder != null && (!paused || isFirstFrame) && playing) {
if (!paused && currentVideoTime == 0) {
// Since startTime is 0, this means that we should now display the first frame of the video, and set the
// time.
if (audio != null) {
audio.play();
}
}
boolean newFrame = false;
if (!showAlreadyDecodedFrame) {
ByteBuffer videoData = decoder.nextVideoFrame();
if (videoData != null) {
if (texture == null) {
texture = new Texture(getTextureWidth(), getTextureHeight(), Format.RGB888);
texture.setFilter(minFilter, magFilter);
}
texture.bind();
Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL20.GL_RGB, getTextureWidth(), getTextureHeight(), 0, GL20.GL_RGB,
GL20.GL_UNSIGNED_BYTE, videoData);
newFrame = true;
} else if (isFirstFrame) {
return false;
} else if (looping) {
try {
// NOTE: this just creates a new decoder instead of reusing the existing one.
float volume = getVolume();
play(currentFile);
setVolume(volume);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return false;
} else {
playing = false;
if (completionListener != null) {
completionListener.onCompletionListener(currentFile);
}
return false;
}
}
isFirstFrame = false;
double secondsAhead;
if (audio == null) {
float deltaTime = Gdx.graphics.getDeltaTime();
// Avoid huge delta time if game execution has been suspended
if (deltaTime >= 1f) deltaTime = 1f / Gdx.graphics.getDisplayMode().refreshRate;
currentVideoTime += deltaTime;
secondsAhead = decoder.getCurrentFrameTimestamp() - currentVideoTime;
} else {
secondsAhead = decoder.getCurrentFrameTimestamp() - audio.getPosition();
}
showAlreadyDecodedFrame = secondsAhead > .02f;
return newFrame;
}
return false;
}
@Override
@Null
public Texture getTexture () {
return texture;
}
/** Will return whether the buffer is filled. At the time of writing, the buffer used can store 10 frames of video. You can
* find the value in jni/VideoDecoder.h
*
* @return whether buffer is filled. */
@Override
public boolean isBuffered () {
if (decoder != null) {
return decoder.isBuffered();
}
return false;
}
@Override
public void stop () {
playing = false;
if (audio != null) {
audio.dispose();
audio = null;
}
if (decoder != null) {
decoder.dispose();
decoder = null;
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
inputStream = null;
}
currentVideoTime = 0;
showAlreadyDecodedFrame = false;
isFirstFrame = true;
}
@Override
public void pause () {
if (!paused) {
paused = true;
if (audio != null) {
audio.pause();
}
}
}
@Override
public void resume () {
if (paused) {
paused = false;
if (audio != null) {
audio.play();
}
}
}
@Override
public void dispose () {
stop();
if (texture != null) {
texture.dispose();
texture = null;
}
}
@Override
public void setOnVideoSizeListener (VideoSizeListener listener) {
sizeListener = listener;
}
@Override
public void setOnCompletionListener (CompletionListener listener) {
completionListener = listener;
}
@Override
public int getVideoWidth () {
return currentVideoWidth;
}
@Override
public int getVideoHeight () {
return currentVideoHeight;
}
@Override
public boolean isPlaying () {
return playing;
}
@Override
public void setVolume (float volume) {
if (audio != null) audio.setVolume(volume);
}
@Override
public float getVolume () {
if (audio == null) return 0;
return audio.getVolume();
}
@Override
public void setLooping (boolean looping) {
this.looping = looping;
}
@Override
public boolean isLooping () {
return looping;
}
@Override
public int getCurrentTimestamp () {
return (int)(decoder.getCurrentFrameTimestamp() * 1000);
}
}