rlm@3
|
1 package com.aurellem.capture;
|
rlm@3
|
2
|
rlm@3
|
3 import java.awt.image.BufferedImage;
|
rlm@3
|
4 import java.io.File;
|
rlm@3
|
5 import java.io.IOException;
|
rlm@3
|
6 import java.nio.ByteBuffer;
|
rlm@3
|
7
|
rlm@3
|
8 import com.jme3.app.Application;
|
rlm@3
|
9 import com.jme3.app.state.AppState;
|
rlm@3
|
10 import com.jme3.app.state.AppStateManager;
|
rlm@3
|
11 import com.jme3.post.SceneProcessor;
|
rlm@3
|
12 import com.jme3.renderer.Camera;
|
rlm@3
|
13 import com.jme3.renderer.RenderManager;
|
rlm@3
|
14 import com.jme3.renderer.ViewPort;
|
rlm@3
|
15 import com.jme3.renderer.queue.RenderQueue;
|
rlm@3
|
16 import com.jme3.texture.FrameBuffer;
|
rlm@3
|
17 import com.jme3.util.BufferUtils;
|
rlm@3
|
18 import com.jme3.util.Screenshots;
|
rlm@3
|
19
|
rlm@3
|
20 /**
|
rlm@3
|
21 * <code>VideoProcessor</code> copies the frames it receives to video.
|
rlm@3
|
22 * To ensure smooth video at a constant framerate, you should set your
|
rlm@3
|
23 * application's timer to a new {@link IsoTimer}. This class will
|
rlm@3
|
24 * auto-determine the framerate of the video based on the time difference
|
rlm@3
|
25 * between the first two frames it receives, although you can manually set
|
rlm@3
|
26 * the framerate by calling <code>setFps(newFramerate)</code>. Be sure to
|
rlm@3
|
27 * place this processor *after* any other processors whose effects you want
|
rlm@3
|
28 * to be included in the output video. You can attach multiple
|
rlm@3
|
29 * <code>VideoProcessor</code>s to the same <code>ViewPort</code>.
|
rlm@3
|
30 *
|
rlm@3
|
31 * For example,
|
rlm@3
|
32 * <code>
|
rlm@3
|
33 * someViewPort.addProcessor(new VideoProcessor(file1));
|
rlm@3
|
34 * someViewPort.addProcessor(someShadowRenderer);
|
rlm@3
|
35 * someViewPort.addProcessor(new VideoProcessor(file2));
|
rlm@3
|
36 * </code>
|
rlm@3
|
37 *
|
rlm@3
|
38 * will output a video without shadows to <code>file1</code> and a video
|
rlm@3
|
39 * with shadows to <code>file2</code>
|
rlm@3
|
40 *
|
rlm@3
|
41 * @author Robert McIntyre
|
rlm@3
|
42 *
|
rlm@3
|
43 */
|
rlm@3
|
44
|
rlm@3
|
45 public abstract class AbstractVideoRecorder
|
rlm@3
|
46 implements SceneProcessor, IVideoRecorder, AppState{
|
rlm@3
|
47
|
rlm@3
|
48 final File output;
|
rlm@3
|
49 Camera camera;
|
rlm@3
|
50 int width;
|
rlm@3
|
51 int height;
|
rlm@3
|
52 String targetFileName;
|
rlm@3
|
53 FrameBuffer frameBuffer;
|
rlm@3
|
54 Double fps = null;
|
rlm@3
|
55 RenderManager renderManager;
|
rlm@3
|
56 ByteBuffer byteBuffer;
|
rlm@3
|
57 BufferedImage rawFrame;
|
rlm@3
|
58 boolean isInitilized = false;
|
rlm@3
|
59 boolean paused = false;
|
rlm@3
|
60
|
rlm@3
|
61 public AbstractVideoRecorder(File output) throws IOException {
|
rlm@3
|
62 this.output = output;
|
rlm@3
|
63 this.targetFileName = this.output.getCanonicalPath();
|
rlm@3
|
64 }
|
rlm@3
|
65
|
rlm@3
|
66
|
rlm@3
|
67 public double getFps() {return this.fps;}
|
rlm@3
|
68
|
rlm@3
|
69 public AbstractVideoRecorder setFps(double fps) {
|
rlm@3
|
70 this.fps = fps;
|
rlm@3
|
71 return this;
|
rlm@3
|
72 }
|
rlm@3
|
73
|
rlm@3
|
74 public void initialize(RenderManager rm, ViewPort viewPort) {
|
rlm@3
|
75 Camera camera = viewPort.getCamera();
|
rlm@3
|
76 this.width = camera.getWidth();
|
rlm@3
|
77 this.height = camera.getHeight();
|
rlm@3
|
78
|
rlm@3
|
79 rawFrame = new BufferedImage(width, height,
|
rlm@3
|
80 BufferedImage.TYPE_4BYTE_ABGR);
|
rlm@3
|
81 byteBuffer = BufferUtils.createByteBuffer(width * height * 4 );
|
rlm@3
|
82 this.renderManager = rm;
|
rlm@3
|
83 this.isInitilized = true;
|
rlm@3
|
84 }
|
rlm@3
|
85
|
rlm@3
|
86 public void reshape(ViewPort vp, int w, int h) {}
|
rlm@3
|
87
|
rlm@3
|
88 public boolean isInitialized() {return this.isInitilized;}
|
rlm@3
|
89
|
rlm@3
|
90 public void preFrame(float tpf) {
|
rlm@3
|
91 if (null == this.fps){
|
rlm@3
|
92 this.setFps(1.0 / tpf);}
|
rlm@3
|
93 }
|
rlm@3
|
94
|
rlm@3
|
95 public void postQueue(RenderQueue rq) {}
|
rlm@3
|
96
|
rlm@3
|
97 public void postFrame(FrameBuffer out) {
|
rlm@3
|
98 if (!this.paused){
|
rlm@3
|
99 byteBuffer.clear();
|
rlm@3
|
100 renderManager.getRenderer().readFrameBuffer(out, byteBuffer);
|
rlm@3
|
101 Screenshots.convertScreenShot(byteBuffer, rawFrame);
|
rlm@3
|
102 record(rawFrame);
|
rlm@3
|
103 }
|
rlm@3
|
104 }
|
rlm@3
|
105
|
rlm@3
|
106 public void cleanup(){
|
rlm@3
|
107 this.pause();
|
rlm@3
|
108 this.finish();
|
rlm@3
|
109 };
|
rlm@3
|
110
|
rlm@3
|
111 public void pause(){
|
rlm@3
|
112 this.paused = true;
|
rlm@3
|
113 }
|
rlm@3
|
114
|
rlm@3
|
115 public void start(){
|
rlm@3
|
116 this.paused = false;
|
rlm@3
|
117 }
|
rlm@3
|
118
|
rlm@3
|
119 // methods from AppState
|
rlm@3
|
120 public void initialize(AppStateManager stateManager, Application app) {}
|
rlm@3
|
121
|
rlm@3
|
122 public void setEnabled(boolean active) {
|
rlm@3
|
123 if (active) {this.start();}
|
rlm@3
|
124 else {this.pause();}
|
rlm@3
|
125 }
|
rlm@3
|
126
|
rlm@3
|
127 public boolean isEnabled() {
|
rlm@3
|
128 return this.paused;
|
rlm@3
|
129 }
|
rlm@3
|
130
|
rlm@3
|
131 public void stateAttached(AppStateManager stateManager) {}
|
rlm@3
|
132
|
rlm@3
|
133
|
rlm@3
|
134 public void stateDetached(AppStateManager stateManager) {
|
rlm@3
|
135 this.pause();
|
rlm@3
|
136 this.finish();
|
rlm@3
|
137 }
|
rlm@3
|
138
|
rlm@3
|
139 public void update(float tpf) {}
|
rlm@3
|
140 public void render(RenderManager rm) {}
|
rlm@3
|
141 public void postRender() {}
|
rlm@3
|
142
|
rlm@3
|
143 }
|