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