view src/com/aurellem/capture/video/AbstractVideoRecorder.java @ 56:afc437f637bd

improved formating
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Dec 2011 19:25:27 -0600
parents 3dc1f15e1e13
children
line wrap: on
line source
1 package com.aurellem.capture.video;
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>VideoRecorder</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 <code>IsoTimer</code>. 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 */
44 public abstract class AbstractVideoRecorder
45 implements SceneProcessor, VideoRecorder, AppState{
47 final File output;
48 Camera camera;
49 int width;
50 int height;
51 String targetFileName;
52 FrameBuffer frameBuffer;
53 Double fps = null;
54 RenderManager renderManager;
55 ByteBuffer byteBuffer;
56 BufferedImage rawFrame;
57 boolean isInitilized = false;
58 boolean paused = false;
60 public AbstractVideoRecorder(File output) throws IOException {
61 this.output = output;
62 this.targetFileName = this.output.getCanonicalPath();
63 }
66 public double getFps() {return this.fps;}
68 public AbstractVideoRecorder setFps(double fps) {
69 this.fps = fps;
70 return this;
71 }
73 public void initialize(RenderManager rm, ViewPort viewPort) {
74 Camera camera = viewPort.getCamera();
75 this.width = camera.getWidth();
76 this.height = camera.getHeight();
78 rawFrame = new BufferedImage(width, height,
79 BufferedImage.TYPE_4BYTE_ABGR);
80 byteBuffer = BufferUtils.createByteBuffer(width * height * 4 );
81 this.renderManager = rm;
82 this.isInitilized = true;
83 }
85 public void reshape(ViewPort vp, int w, int h) {}
87 public boolean isInitialized() {return this.isInitilized;}
89 public void preFrame(float tpf) {
90 if (null == this.fps){
91 this.setFps(1.0 / tpf);}
92 }
94 public void postQueue(RenderQueue rq) {}
96 public void postFrame(FrameBuffer out) {
97 if (!this.paused){
98 byteBuffer.clear();
99 renderManager.getRenderer().readFrameBuffer(out, byteBuffer);
100 Screenshots.convertScreenShot(byteBuffer, rawFrame);
101 record(rawFrame);
102 }
103 }
105 public void cleanup(){
106 this.pause();
107 this.finish();
108 };
110 public void pause(){
111 this.paused = true;
112 }
114 public void start(){
115 this.paused = false;
116 }
118 // methods from AppState
119 public void initialize(AppStateManager stateManager, Application app) {}
121 public void setEnabled(boolean active) {
122 if (active) {this.start();}
123 else {this.pause();}
124 }
126 public boolean isEnabled() {
127 return this.paused;
128 }
130 public void stateAttached(AppStateManager stateManager) {}
133 public void stateDetached(AppStateManager stateManager) {
134 this.pause();
135 this.finish();
136 }
138 public void update(float tpf) {}
139 public void render(RenderManager rm) {}
140 public void postRender() {}
142 }