rlm@9: package com.aurellem.capture.video;
rlm@9:
rlm@9: import java.awt.image.BufferedImage;
rlm@9: import java.io.File;
rlm@9: import java.io.IOException;
rlm@9: import java.nio.ByteBuffer;
rlm@9:
rlm@9: import com.aurellem.capture.IsoTimer;
rlm@9: import com.jme3.app.Application;
rlm@9: import com.jme3.app.state.AppState;
rlm@9: import com.jme3.app.state.AppStateManager;
rlm@9: import com.jme3.post.SceneProcessor;
rlm@9: import com.jme3.renderer.Camera;
rlm@9: import com.jme3.renderer.RenderManager;
rlm@9: import com.jme3.renderer.ViewPort;
rlm@9: import com.jme3.renderer.queue.RenderQueue;
rlm@9: import com.jme3.texture.FrameBuffer;
rlm@9: import com.jme3.util.BufferUtils;
rlm@9: import com.jme3.util.Screenshots;
rlm@9:
rlm@9: /**
rlm@39: * VideoRecorder
copies the frames it receives to video.
rlm@9: * To ensure smooth video at a constant framerate, you should set your
rlm@39: * application's timer to a new IsoTimer
. This class will
rlm@9: * auto-determine the framerate of the video based on the time difference
rlm@9: * between the first two frames it receives, although you can manually set
rlm@9: * the framerate by calling setFps(newFramerate)
. Be sure to
rlm@9: * place this processor *after* any other processors whose effects you want
rlm@9: * to be included in the output video. You can attach multiple
rlm@9: * VideoProcessor
s to the same ViewPort
.
rlm@9: *
rlm@9: * For example,
rlm@9: *
rlm@9: * someViewPort.addProcessor(new VideoProcessor(file1));
rlm@9: * someViewPort.addProcessor(someShadowRenderer);
rlm@9: * someViewPort.addProcessor(new VideoProcessor(file2));
rlm@9: *
rlm@9: *
rlm@9: * will output a video without shadows to file1
and a video
rlm@9: * with shadows to file2
rlm@9: *
rlm@9: * @author Robert McIntyre
rlm@9: *
rlm@9: */
rlm@9:
rlm@9: public abstract class AbstractVideoRecorder
rlm@39: implements SceneProcessor, VideoRecorder, AppState{
rlm@9:
rlm@9: final File output;
rlm@9: Camera camera;
rlm@9: int width;
rlm@9: int height;
rlm@9: String targetFileName;
rlm@9: FrameBuffer frameBuffer;
rlm@9: Double fps = null;
rlm@9: RenderManager renderManager;
rlm@9: ByteBuffer byteBuffer;
rlm@9: BufferedImage rawFrame;
rlm@9: boolean isInitilized = false;
rlm@9: boolean paused = false;
rlm@9:
rlm@9: public AbstractVideoRecorder(File output) throws IOException {
rlm@9: this.output = output;
rlm@9: this.targetFileName = this.output.getCanonicalPath();
rlm@9: }
rlm@9:
rlm@9:
rlm@9: public double getFps() {return this.fps;}
rlm@9:
rlm@9: public AbstractVideoRecorder setFps(double fps) {
rlm@9: this.fps = fps;
rlm@9: return this;
rlm@9: }
rlm@9:
rlm@9: public void initialize(RenderManager rm, ViewPort viewPort) {
rlm@9: Camera camera = viewPort.getCamera();
rlm@9: this.width = camera.getWidth();
rlm@9: this.height = camera.getHeight();
rlm@9:
rlm@9: rawFrame = new BufferedImage(width, height,
rlm@9: BufferedImage.TYPE_4BYTE_ABGR);
rlm@9: byteBuffer = BufferUtils.createByteBuffer(width * height * 4 );
rlm@9: this.renderManager = rm;
rlm@9: this.isInitilized = true;
rlm@9: }
rlm@9:
rlm@9: public void reshape(ViewPort vp, int w, int h) {}
rlm@9:
rlm@9: public boolean isInitialized() {return this.isInitilized;}
rlm@9:
rlm@9: public void preFrame(float tpf) {
rlm@9: if (null == this.fps){
rlm@9: this.setFps(1.0 / tpf);}
rlm@9: }
rlm@9:
rlm@9: public void postQueue(RenderQueue rq) {}
rlm@9:
rlm@9: public void postFrame(FrameBuffer out) {
rlm@9: if (!this.paused){
rlm@9: byteBuffer.clear();
rlm@9: renderManager.getRenderer().readFrameBuffer(out, byteBuffer);
rlm@9: Screenshots.convertScreenShot(byteBuffer, rawFrame);
rlm@9: record(rawFrame);
rlm@9: }
rlm@9: }
rlm@9:
rlm@9: public void cleanup(){
rlm@9: this.pause();
rlm@9: this.finish();
rlm@9: };
rlm@9:
rlm@9: public void pause(){
rlm@9: this.paused = true;
rlm@9: }
rlm@9:
rlm@9: public void start(){
rlm@9: this.paused = false;
rlm@9: }
rlm@9:
rlm@9: // methods from AppState
rlm@9: public void initialize(AppStateManager stateManager, Application app) {}
rlm@9:
rlm@9: public void setEnabled(boolean active) {
rlm@9: if (active) {this.start();}
rlm@9: else {this.pause();}
rlm@9: }
rlm@9:
rlm@9: public boolean isEnabled() {
rlm@9: return this.paused;
rlm@9: }
rlm@9:
rlm@9: public void stateAttached(AppStateManager stateManager) {}
rlm@9:
rlm@9:
rlm@9: public void stateDetached(AppStateManager stateManager) {
rlm@9: this.pause();
rlm@9: this.finish();
rlm@9: }
rlm@9:
rlm@9: public void update(float tpf) {}
rlm@9: public void render(RenderManager rm) {}
rlm@9: public void postRender() {}
rlm@9:
rlm@9: }