minor gstreamer tweaks

This commit is contained in:
adam-m 2012-09-11 13:25:42 -04:00
parent 373b256058
commit 8f80324fa3

View File

@ -46,19 +46,19 @@ import org.sleuthkit.datamodel.TskData;
*
* @author dfickling
*/
@ServiceProvider(service = DataContentViewer.class, position=5)
@ServiceProvider(service = DataContentViewer.class, position = 5)
public class DataContentViewerMedia extends javax.swing.JPanel implements DataContentViewer {
private static final String[] IMAGES = new String[]{ ".jpg", ".jpeg", ".png", ".gif", ".jpe", ".bmp"};
private static final String[] VIDEOS = new String[]{ ".mov", ".m4v", ".flv", ".mp4", ".3gp", ".avi", ".mpg", ".mpeg"};
private static final String[] AUDIOS = new String[]{ ".mp3", ".wav", ".wma"};
private static final String[] IMAGES = new String[]{".jpg", ".jpeg", ".png", ".gif", ".jpe", ".bmp"};
private static final String[] VIDEOS = new String[]{".mov", ".m4v", ".flv", ".mp4", ".3gp", ".avi", ".mpg", ".mpeg"};
private static final String[] AUDIOS = new String[]{".mp3", ".wav", ".wma"};
private static final Logger logger = Logger.getLogger(DataContentViewerMedia.class.getName());
private VideoComponent videoComponent;
private PlayBin2 playbin2;
private File currentFile;
private long durationMillis = 0;
private boolean autoTracking = false; // true if the slider is moving automatically
private final Object playbinLock = new Object(); // lock for synchronization
private final Object playbinLock = new Object(); // lock for synchronization of playbin2 player
/**
* Creates new form DataContentViewerVideo
@ -71,17 +71,16 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
private void customizeComponents() {
Gst.init();
progressSlider.addChangeListener(new ChangeListener() {
/**
* Should always try to synchronize any call to
* progressSlider.setValue() to avoid a different thread
* changing playbin while stateChanged() is processing
* progressSlider.setValue() to avoid a different thread changing
* playbin while stateChanged() is processing
*/
@Override
public void stateChanged(ChangeEvent e) {
int time = progressSlider.getValue();
synchronized(playbinLock) {
if(playbin2 != null && !autoTracking) {
synchronized (playbinLock) {
if (playbin2 != null && !autoTracking) {
State orig = playbin2.getState();
playbin2.pause();
playbin2.seek(ClockTime.fromMillis(time));
@ -89,7 +88,6 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
}
}
}
});
}
@ -158,22 +156,22 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
}// </editor-fold>//GEN-END:initComponents
private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed
synchronized(playbinLock) {
if(playbin2.getState().equals(State.PLAYING)){
synchronized (playbinLock) {
State state = playbin2.getState();
if (state.equals(State.PLAYING)) {
playbin2.pause();
pauseButton.setText("");
playbin2.setState(State.PAUSED);
} else if(playbin2.getState().equals(State.PAUSED)) {
} else if (state.equals(State.PAUSED)) {
playbin2.play();
pauseButton.setText("||");
playbin2.setState(State.PLAYING);
} else if(playbin2.getState().equals(State.READY)) {
} else if (state.equals(State.READY)) {
ExtractMedia em = new ExtractMedia(currentFile, getJFile(currentFile));
em.execute();
}
}
}//GEN-LAST:event_pauseButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton pauseButton;
private javax.swing.JLabel progressLabel;
@ -185,21 +183,26 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
public void setNode(Node selectedNode) {
reset();
setComponentsVisibility(false);
if(selectedNode == null) { return; }
if (selectedNode == null) {
return;
}
File file = selectedNode.getLookup().lookup(File.class);
if(file == null) { return; }
if (file == null) {
return;
}
currentFile = file;
if(containsExt(file.getName(), IMAGES)) {
if (containsExt(file.getName(), IMAGES)) {
showImage(file);
} else if(containsExt(file.getName(), VIDEOS) || containsExt(file.getName(), AUDIOS)) {
} else if (containsExt(file.getName(), VIDEOS) || containsExt(file.getName(), AUDIOS)) {
setupVideo(file);
}
}
/**
* Initialize vars and display the image on the panel.
*
* @param file
*/
private void showImage(File file) {
@ -213,7 +216,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
}
videoComponent = new VideoComponent();
synchronized(playbinLock) {
synchronized (playbinLock) {
playbin2 = new PlayBin2("ImageViewer");
playbin2.setVideoSink(videoComponent.getElement());
}
@ -224,7 +227,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
videoPanel.revalidate();
videoPanel.repaint();
synchronized(playbinLock) {
synchronized (playbinLock) {
playbin2.setInputFile(ioFile);
playbin2.play();
}
@ -233,6 +236,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
/**
* Initialize all the necessary vars to play a video/audio file.
*
* @param file the File to play
*/
private void setupVideo(File file) {
@ -242,7 +246,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
progressSlider.setValue(0);
videoComponent = new VideoComponent();
synchronized(playbinLock) {
synchronized (playbinLock) {
playbin2 = new PlayBin2("VideoPlayer");
playbin2.setVideoSink(videoComponent.getElement());
}
@ -253,7 +257,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
videoPanel.revalidate();
videoPanel.repaint();
synchronized(playbinLock) {
synchronized (playbinLock) {
playbin2.setInputFile(ioFile);
playbin2.setState(State.READY);
}
@ -263,7 +267,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
/**
* To set the visibility of specific components in this class.
*
* @param isVisible whether to show or hide the specific components
* @param isVisible whether to show or hide the specific components
*/
private void setComponentsVisibility(boolean isVisible) {
pauseButton.setVisible(isVisible);
@ -299,16 +303,16 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
}
private void reset() {
synchronized(playbinLock) {
if(playbin2 != null) {
if(playbin2.isPlaying()) {
synchronized (playbinLock) {
if (playbin2 != null) {
if (playbin2.isPlaying()) {
playbin2.stop();
}
playbin2.setState(State.NULL);
// try {
// Thread.sleep(20); // gstreamer needs to catch up
// } catch (InterruptedException ex) { }
if(playbin2.getState().equals(State.NULL)) {
if (playbin2.getState().equals(State.NULL)) {
playbin2.dispose();
}
playbin2 = null;
@ -332,13 +336,13 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
return false;
}
if(file.getSize() == 0) {
if (file.getSize() == 0) {
return false;
}
String name = file.getName().toLowerCase();
if(containsExt(name, IMAGES) || containsExt(name, AUDIOS) || containsExt(name, VIDEOS)) {
if (containsExt(name, IMAGES) || containsExt(name, AUDIOS) || containsExt(name, VIDEOS)) {
return true;
}
@ -347,7 +351,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
@Override
public int isPreferred(Node node, boolean isSupported) {
if(isSupported) {
if (isSupported) {
return 7;
} else {
return 0;
@ -379,7 +383,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
}
/* Thread that extracts and plays a file */
private class ExtractMedia extends SwingWorker<Object,Void> {
private class ExtractMedia extends SwingWorker<Object, Void> {
private ProgressHandle progress;
boolean success = false;
@ -388,10 +392,12 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
String duration;
String position;
ExtractMedia(org.sleuthkit.datamodel.File sFile, java.io.File jFile){
ExtractMedia(org.sleuthkit.datamodel.File sFile, java.io.File jFile) {
this.sFile = sFile;
this.jFile = jFile;
};
}
;
@Override
protected Object doInBackground() throws Exception {
@ -438,39 +444,49 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
progressLabel.setText("Error buffering file");
return;
}
synchronized(playbinLock) {
ClockTime dur = null;
synchronized (playbinLock) {
playbin2.play(); // must play, then pause and get state to get duration.
playbin2.pause();
playbin2.getState();
duration = playbin2.queryDuration().toString();
durationMillis = playbin2.queryDuration().toMillis();
dur = playbin2.queryDuration();
}
progressSlider.setMaximum((int)durationMillis);
duration = dur.toString();
durationMillis = dur.toMillis();
progressSlider.setMaximum((int) durationMillis);
progressSlider.setMinimum(0);
final String finalDuration;
if(duration.length() == 8 && duration.substring(0,3).equals("00:")) {
if (duration.length() == 8 && duration.substring(0, 3).equals("00:")) {
finalDuration = duration.substring(3);
progressLabel.setText("00:00/" + duration);
} else {
finalDuration = duration;
progressLabel.setText("00:00:00/" + duration);
}
synchronized(playbinLock) {
synchronized (playbinLock) {
playbin2.play();
}
pauseButton.setText("||");
new Thread(new Runnable() {
private boolean isPlayBinReady() {
synchronized (playbinLock) {
return playbin2 != null && playbin2.getState().equals(State.NULL);
}
}
@Override
public void run() {
long positionMillis = 0;
while (positionMillis < durationMillis
&& playbin2 != null
&& !playbin2.getState().equals(State.NULL)) {
synchronized(playbinLock) {
position = playbin2.queryPosition().toString();
positionMillis = playbin2.queryPosition().toMillis();
&& isPlayBinReady() ) {
ClockTime pos = null;
synchronized (playbinLock) {
pos = playbin2.queryPosition();
}
position = pos.toString();
positionMillis = pos.toMillis();
if (position.length() == 8) {
position = position.substring(3);
}
@ -480,7 +496,8 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
autoTracking = false;
try {
Thread.sleep(20);
} catch (InterruptedException ex) { }
} catch (InterruptedException ex) {
}
}
if (finalDuration.length() == 5) {
progressLabel.setText("00:00/" + finalDuration);
@ -488,13 +505,13 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
progressLabel.setText("00:00:00/" + finalDuration);
}
// If it reached the end
if(progressSlider.getValue() == progressSlider.getMaximum()) {
if (progressSlider.getValue() == progressSlider.getMaximum()) {
restartVideo();
}
}
public void restartVideo() {
synchronized(playbinLock) {
synchronized (playbinLock) {
if (playbin2 != null) {
playbin2.stop();
playbin2.setState(State.READY); // ready to be played again