krut.KRUT_Recording
Class ScreenGrabber

java.lang.Object
  extended by java.lang.Thread
      extended by krut.KRUT_Recording.ScreenGrabber
All Implemented Interfaces:
java.lang.Runnable

public class ScreenGrabber
extends java.lang.Thread

This class is used both to record a movie and to take separate screen shots. The movie is recorded into a temporary data file containing a series of screen shots. This temporary data file is an OutputStream, and the type of this stream can be changed relatively easy. Originally the stream did not point to a file but to memory, and because of this some methods that have no logical place in the present class still remain here.

After recording is finished, a DataList object is used to feed a JpegImagesToMoviesMod object with the images, creating a movie file. After a movie file has been created, a Merge object is used to add audio to the movie.

There are several methods in this class involving speed and memory tests, which have become deprecated, but still remain to make sure that the program doesn't break.

The present also handles the updating of the preview window, and the movements of the capture area when the mouse is being followed.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 int acceleration
          This value represents the acceleration of the capRect when we are tracking the mouse pointer.
 boolean audioRecording
          This flag can be used to get the run() method to hold at the end, in order to increase sync between audio and video.
 java.awt.Rectangle capRect
          The capture area for video recording.
 int cntMovies
          cntMovies handles the numbering of the movie files, and is updated everytime a movie is finished recording.
 float encQuality
          The video encoding quality.
 boolean error
          error is just used to signal that something went wrong with video recording. error is set true if an exception was fired in the run() method. error is set false in the init() method.
 boolean followMouse
          Should we follow the mouse pointer around the screen or not?
 boolean getMouse
          getMouse selects if the mouse pointer should be in the film.
protected  DataList images
          This object is used to reload the images from the file they are saved in, and then supply them to the JpegImagesToMovieMod class.
 boolean initing
          A flag used to prevent the recording from starting by mistake while the init() method is running.
 int maxNOPics
          maxNOPics should only be read.
 double moveLimit
          This value represents the limit for when the capRect should move to be re-centered around the mouse pointer, if we are tracking the mouse pointer.
 EncodingProgressBar myProgressBar
          This object is used if the user wants to interrupt the encoding.
 SaveFileChooser mySaveQuery
          It is necessary for the ScreenGrabber to have access to a SaveFileChooser, because the ScreenGrabber needs to use the getNextFile() method both in init() and in finished().
 SnapShot mySnapShot
          It is necessary for the ScreenGrabber to have access to a SnapShot, in case a preview film is to be displayed during recording.
 boolean notFinished
          notFinished is set to false by user to stop recording.
 boolean preview
          if preview is true, an image is sent to the SnapShot object at each frame, where a preview film of the recording is running.
 boolean recording
          recording is set to false at the exact time when capping is finished.
 boolean running
          running is set to false when the ScreenGrabber is ready for another recording.
 java.io.File screenshotFile
          The name and path of the screenshotFile.
protected  java.awt.Dimension screenSize
          This is the screensize of the default screen.
 java.lang.String tempFile
          The name of the tempFiles.
 double time
          time should only be read.
 boolean unRecoverableError
          A flag to indicate that something has gone terribly wrong with the recording of the movie.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ScreenGrabber()
          Constructor for ScreenGrabber.
ScreenGrabber(java.awt.Rectangle capSize, int fps)
          Constructor for ScreenGrabber.
 
Method Summary
 void encode()
          This is the method where the encoding of the mov-file takes place.
 void hold()
          Used to sync video.
 void init()
          Init or reinit the encoder.
 void run()
          Main working method.
 void setFps(int fps, int playbackFps)
          Set the fps values.
 void setSyncTime(long syncTime)
          Set the syncTime for the run() method This MUST be done before wakeUp() is called to start the recording.
 void snapshot()
          Take a snapshot of the selected screencap area and save to a new screenshot file.
 double testCapTime()
          Test the Robot cap time per frame.
 double[] testEnc()
          Test the average encoder encoding size and time.
 void wakeUp()
          Called by the user to start capturing images.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

capRect

public java.awt.Rectangle capRect
The capture area for video recording.


encQuality

public float encQuality
The video encoding quality. The value of this parameter will be overwritten from Run_KRUT.checkInited(), so changing the initial value of this parameter will have no effect.


moveLimit

public double moveLimit
This value represents the limit for when the capRect should move to be re-centered around the mouse pointer, if we are tracking the mouse pointer. A comparing value is calculated, and if this comparing value is larger than the moveLimit value, the capRect is re-centered. The comparing value is calculated as follows:

d1 = The distance from the capRect center to the mousepointer.
d2 = The distance from the capRect center to the bounding edge of the capRect, measured along the line through the mousepointer.

moveLimit = d1 / d2


acceleration

public int acceleration
This value represents the acceleration of the capRect when we are tracking the mouse pointer. The value is used both for acceleration and retardation. Currently, this value is updated in the run() method, at the start of the main loop, everytime a recording is started, so changing it here will have no effect.


time

public double time
time should only be read. time is the time in ms between each frame


maxNOPics

public int maxNOPics
maxNOPics should only be read. maxNOPics is the maximum number of frames that can be recorded into memory.


cntMovies

public int cntMovies
cntMovies handles the numbering of the movie files, and is updated everytime a movie is finished recording. cntMovies should only be read.


notFinished

public boolean notFinished
notFinished is set to false by user to stop recording.


recording

public boolean recording
recording is set to false at the exact time when capping is finished. This is used to synchronize shound.


running

public boolean running
running is set to false when the ScreenGrabber is ready for another recording. It is set to true in the run() method and changed back to false in the encode() method.


unRecoverableError

public boolean unRecoverableError
A flag to indicate that something has gone terribly wrong with the recording of the movie. Old. Only used in old methods.


getMouse

public boolean getMouse
getMouse selects if the mouse pointer should be in the film. getMouse can be changed at any time, including during recording, to start/stop recording mouse positions.


followMouse

public boolean followMouse
Should we follow the mouse pointer around the screen or not? This parameter can be changed at any time, including during recording.


preview

public boolean preview
if preview is true, an image is sent to the SnapShot object at each frame, where a preview film of the recording is running.


error

public boolean error
error is just used to signal that something went wrong with video recording. error is set true if an exception was fired in the run() method. error is set false in the init() method.


initing

public boolean initing
A flag used to prevent the recording from starting by mistake while the init() method is running. Changed in the init() method, and checked at the beginning of run(). Also changed before and after the call to init() in encode(). The initing parameter could be changed by the user before and after calling init() for increased reliability on the flag controls, but in reality reliability is pretty good the way it is, with changes at the start and end of the init() method.


tempFile

public java.lang.String tempFile
The name of the tempFiles. This is changed in Run_KRUT.setAllSaveFiles(), and changing it here has no effect.


screenshotFile

public java.io.File screenshotFile
The name and path of the screenshotFile. It is changed from the setAllSaveFiles() method in the Run_KRUT class.


myProgressBar

public EncodingProgressBar myProgressBar
This object is used if the user wants to interrupt the encoding. In the EncodeThread constuctor, the DataList is also given to myProgressBar, so that the progressBar can stop the dumper.


audioRecording

public boolean audioRecording
This flag can be used to get the run() method to hold at the end, in order to increase sync between audio and video. If so, it should be set true when audio recording is started, and then set false once recording is finished. The practical result will be that the run() method will take a pause before performing it's call to init(). After this parameter is set false when audio recording is finished, the run() method MUST be woken with a call to wakeUp().


mySaveQuery

public SaveFileChooser mySaveQuery
It is necessary for the ScreenGrabber to have access to a SaveFileChooser, because the ScreenGrabber needs to use the getNextFile() method both in init() and in finished(). This parameter will be updated by Run_KRUT as soon as the global SaveFileChooser is initiated.


mySnapShot

public SnapShot mySnapShot
It is necessary for the ScreenGrabber to have access to a SnapShot, in case a preview film is to be displayed during recording. This parameter will be updated by Run_KRUT immideatly after after initializing.


images

protected DataList images
This object is used to reload the images from the file they are saved in, and then supply them to the JpegImagesToMovieMod class. A new images object is created and set up at the end of the main recording thread (in the run() method). When the encode() method is called by the user, it takes this object and passes it on to the startDumper() method.


screenSize

protected java.awt.Dimension screenSize
This is the screensize of the default screen. If another screen would be used, this parameter would need to be updated.

Constructor Detail

ScreenGrabber

public ScreenGrabber(java.awt.Rectangle capSize,
                     int fps)
Constructor for ScreenGrabber. Test encoding to get a good value for avgCapSize set up. Then setup outfiles.

Parameters:
capSize - The initial capture area of the ScreenGrabber. This can later be changed by changing the public parameter capRect, and then calling the init method of this class.
fps - The initial fps of the ScreenGrabber. This can later be changed by calling the setFps method of this class.

ScreenGrabber

public ScreenGrabber()
Constructor for ScreenGrabber. Setup the capture Rectangle and the fps to use. Then test encoding to get a good value for avgCapSize set up. Then setup outfiles.

Method Detail

init

public void init()
          throws java.io.IOException
Init or reinit the encoder. This needs to be done everytime the amount of memory needed has changed, the sizes of the frames have changed, or just between every recording.

Throws:
java.io.IOException

setFps

public void setFps(int fps,
                   int playbackFps)
Set the fps values.

Parameters:
fps - The recording fps.
playbackFps - The playback fps.

setSyncTime

public void setSyncTime(long syncTime)
Set the syncTime for the run() method This MUST be done before wakeUp() is called to start the recording.

Parameters:
syncTime - A parameter determining how many millis behind the fps the capturing can fall, before a frame is doubled to compensate.

testCapTime

public double testCapTime()
                   throws java.io.IOException
Test the Robot cap time per frame. Capture iterations frames in the loop, and calculate and return an average. This method is called by the speedTest() method in Run_KRUT.

Returns:
The average capture time in milliseconds.
Throws:
java.io.IOException

testEnc

public double[] testEnc()
                 throws java.io.IOException
Test the average encoder encoding size and time. Encode iterations frames in the loop, and calculate and return average values of speed and size. avgSize is also set as a class variable, for simplicity, since other methods in the ScreenGrabber have use for it. For this method to deliver reasonable values, there should be an 'average' picture in the capture area.

Throws:
java.io.IOException

snapshot

public void snapshot()
Take a snapshot of the selected screencap area and save to a new screenshot file. Overwrites any prior screenshot file with the same name. (In a normal case, the Run_KRUT class that calls this method from Run_KRUT.snapaction() will be in control of whether files should be overwritten or not, and make sure that the global parameter screenshotFile has a correct filename.)


hold

public void hold()
Used to sync video. Users may call this method, and are then woken once when the last frame is captured, and once more when the recording is completely finished. The recording and running flags must be checked in order to safely determine if recording is running upon return from this method. The ScreenGrabber itself calls this method from run(), and then waits here for capture to start.


wakeUp

public void wakeUp()
Called by the user to start capturing images. Also wakes up users waiting for the grabber to finish. This method is called once when the last frame is captured, and once more when all video data are written to the temp file.


run

public void run()
Main working method. It captures a frame, and sleeps for the amount of time left until the next frame should be captured. If capturing the frame took longer than fps would allow, the frame is marked as missed, and then copied the number of times required to get back in sync. The method ends with a call to startDumper(), where a temp mov file is made, then this method finally changes the running parameter, and makes one last call to wakeUp(). The recording is started when the user sets the notFinished flag to true, and then calls ScreenGrabber.wakeUp(). It is stopped when the user sets the notFinished flag to false.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

encode

public void encode()
This is the method where the encoding of the mov-file takes place. In essence, this method picks up exactly where the run() method left off. The reason for keeping this code outside the run() method, is that the user may want to record at one thread priority (typically the highest), and encode at another. Before calling the encode() method, the user should assign an EncoderProgressBar to this ScreenGrabber object by setting the myProgressBar parameter.