view src/com/aurellem/capture/AbstractVideoRecorder.java @ 4:edaa7e7806e4

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