Refactored and reformatted code to use less static variables
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package eu.crushedpixel.replaymod.video;
|
||||
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
||||
import org.monte.media.*;
|
||||
import org.monte.media.FormatKeys.MediaType;
|
||||
import org.monte.media.math.Rational;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -8,140 +14,127 @@ import java.util.Calendar;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.monte.media.Buffer;
|
||||
import org.monte.media.Format;
|
||||
import org.monte.media.FormatKeys;
|
||||
import org.monte.media.FormatKeys.MediaType;
|
||||
import org.monte.media.MovieWriter;
|
||||
import org.monte.media.Registry;
|
||||
import org.monte.media.VideoFormatKeys;
|
||||
import org.monte.media.math.Rational;
|
||||
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
||||
|
||||
public class VideoWriter {
|
||||
|
||||
private static final String DATE_FORMAT = "yyyy_MM_dd_HH_mm_ss";
|
||||
private static final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
|
||||
private static final String DATE_FORMAT = "yyyy_MM_dd_HH_mm_ss";
|
||||
private static final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
|
||||
|
||||
private static final String VIDEO_EXTENSION = ".avi";
|
||||
private static final String VIDEO_EXTENSION = ".avi";
|
||||
|
||||
private static MovieWriter out;
|
||||
private static File file;
|
||||
private static boolean isRecording = false;
|
||||
private static boolean requestFinish = false;
|
||||
private static boolean abort = false;
|
||||
private static MovieWriter out;
|
||||
private static File file;
|
||||
private static boolean isRecording = false;
|
||||
private static boolean requestFinish = false;
|
||||
private static boolean abort = false;
|
||||
|
||||
private static Buffer buf;
|
||||
private static int track;
|
||||
private static Buffer buf;
|
||||
private static int track;
|
||||
private static Queue<BufferedImage> toWrite = new LinkedBlockingQueue<BufferedImage>();
|
||||
|
||||
public static boolean isRecording() {
|
||||
return isRecording;
|
||||
}
|
||||
public static boolean isRecording() {
|
||||
return isRecording;
|
||||
}
|
||||
|
||||
public static void startRecording(int width, int height) {
|
||||
if(isRecording) {
|
||||
IllegalStateException up = new IllegalStateException("VideoWriter is already recording!");
|
||||
throw up; //lolololo
|
||||
}
|
||||
isRecording = true;
|
||||
public static void startRecording(int width, int height) {
|
||||
if(isRecording) {
|
||||
IllegalStateException up = new IllegalStateException("VideoWriter is already recording!");
|
||||
throw up; //lolololo
|
||||
}
|
||||
isRecording = true;
|
||||
|
||||
toWrite = new LinkedBlockingQueue<BufferedImage>();
|
||||
toWrite = new LinkedBlockingQueue<BufferedImage>();
|
||||
|
||||
try {
|
||||
File folder = ReplayFileIO.getRenderFolder();
|
||||
try {
|
||||
File folder = ReplayFileIO.getRenderFolder();
|
||||
|
||||
String fileName = sdf.format(Calendar.getInstance().getTime());
|
||||
String fileName = sdf.format(Calendar.getInstance().getTime());
|
||||
|
||||
file = new File(folder, fileName+VIDEO_EXTENSION);
|
||||
file.createNewFile();
|
||||
file = new File(folder, fileName + VIDEO_EXTENSION);
|
||||
file.createNewFile();
|
||||
|
||||
out = Registry.getInstance().getWriter(file);
|
||||
Format format = new Format(FormatKeys.MediaTypeKey, MediaType.VIDEO,
|
||||
FormatKeys.EncodingKey, VideoFormatKeys.ENCODING_AVI_MJPG,
|
||||
FormatKeys.FrameRateKey, new Rational(ReplayMod.replaySettings.getVideoFramerate(), 1),
|
||||
VideoFormatKeys.WidthKey, width,
|
||||
VideoFormatKeys.HeightKey, height,
|
||||
VideoFormatKeys.DepthKey, 24,
|
||||
VideoFormatKeys.QualityKey, (float)ReplayMod.replaySettings.getVideoQuality());
|
||||
out = Registry.getInstance().getWriter(file);
|
||||
Format format = new Format(FormatKeys.MediaTypeKey, MediaType.VIDEO,
|
||||
FormatKeys.EncodingKey, VideoFormatKeys.ENCODING_AVI_MJPG,
|
||||
FormatKeys.FrameRateKey, new Rational(ReplayMod.replaySettings.getVideoFramerate(), 1),
|
||||
VideoFormatKeys.WidthKey, width,
|
||||
VideoFormatKeys.HeightKey, height,
|
||||
VideoFormatKeys.DepthKey, 24,
|
||||
VideoFormatKeys.QualityKey, (float) ReplayMod.replaySettings.getVideoQuality());
|
||||
|
||||
|
||||
track = out.addTrack(format);
|
||||
track = out.addTrack(format);
|
||||
|
||||
buf = new Buffer();
|
||||
buf.format = new Format(VideoFormatKeys.DataClassKey, BufferedImage.class);
|
||||
buf.sampleDuration = out.getFormat(track).get(VideoFormatKeys.FrameRateKey).inverse();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
buf = new Buffer();
|
||||
buf.format = new Format(VideoFormatKeys.DataClassKey, BufferedImage.class);
|
||||
buf.sampleDuration = out.getFormat(track).get(VideoFormatKeys.FrameRateKey).inverse();
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
Thread t = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(true) {
|
||||
if(toWrite.isEmpty() || abort) {
|
||||
if(requestFinish) {
|
||||
requestFinish = false;
|
||||
isRecording = false;
|
||||
try {
|
||||
out.close();
|
||||
if(abort) {
|
||||
file.delete();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
abort = false;
|
||||
toWrite = new LinkedBlockingQueue<BufferedImage>();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
write();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
while(true) {
|
||||
if(toWrite.isEmpty() || abort) {
|
||||
if(requestFinish) {
|
||||
requestFinish = false;
|
||||
isRecording = false;
|
||||
try {
|
||||
out.close();
|
||||
if(abort) {
|
||||
file.delete();
|
||||
}
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
abort = false;
|
||||
toWrite = new LinkedBlockingQueue<BufferedImage>();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
write();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
private static Queue<BufferedImage> toWrite = new LinkedBlockingQueue<BufferedImage>();
|
||||
public static void writeImage(BufferedImage image) {
|
||||
if(requestFinish || !isRecording) {
|
||||
IllegalStateException up = new IllegalStateException(
|
||||
"The VideoWriter is currently not available. Please try again later.");
|
||||
throw up; //lolololo^2
|
||||
}
|
||||
|
||||
public static void writeImage(BufferedImage image) {
|
||||
if(requestFinish || !isRecording) {
|
||||
IllegalStateException up = new IllegalStateException(
|
||||
"The VideoWriter is currently not available. Please try again later.");
|
||||
throw up; //lolololo^2
|
||||
}
|
||||
toWrite.add(image);
|
||||
}
|
||||
|
||||
toWrite.add(image);
|
||||
}
|
||||
public static void endRecording() {
|
||||
if(!isRecording) return;
|
||||
requestFinish = true;
|
||||
}
|
||||
|
||||
public static void endRecording() {
|
||||
if(!isRecording) return;
|
||||
requestFinish = true;
|
||||
}
|
||||
private static void write() {
|
||||
try {
|
||||
BufferedImage img = toWrite.poll();
|
||||
if(img != null) {
|
||||
buf.data = img;
|
||||
out.write(track, buf);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void write() {
|
||||
try {
|
||||
BufferedImage img = toWrite.poll();
|
||||
if(img != null) {
|
||||
buf.data = img;
|
||||
out.write(track, buf);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void abortRecording() {
|
||||
requestFinish = true;
|
||||
abort = true;
|
||||
}
|
||||
public static void abortRecording() {
|
||||
requestFinish = true;
|
||||
abort = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user