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,20 +46,20 @@ import org.sleuthkit.datamodel.TskData;
* *
* @author dfickling * @author dfickling
*/ */
@ServiceProvider(service = DataContentViewer.class, position=5) @ServiceProvider(service = DataContentViewer.class, position = 5)
public class DataContentViewerMedia extends javax.swing.JPanel implements DataContentViewer { 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[] 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[] 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[] AUDIOS = new String[]{".mp3", ".wav", ".wma"};
private static final Logger logger = Logger.getLogger(DataContentViewerMedia.class.getName()); private static final Logger logger = Logger.getLogger(DataContentViewerMedia.class.getName());
private VideoComponent videoComponent; private VideoComponent videoComponent;
private PlayBin2 playbin2; private PlayBin2 playbin2;
private File currentFile; private File currentFile;
private long durationMillis = 0; private long durationMillis = 0;
private boolean autoTracking = false; // true if the slider is moving automatically 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 * Creates new form DataContentViewerVideo
*/ */
@ -67,21 +67,20 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
initComponents(); initComponents();
customizeComponents(); customizeComponents();
} }
private void customizeComponents() { private void customizeComponents() {
Gst.init(); Gst.init();
progressSlider.addChangeListener(new ChangeListener() { progressSlider.addChangeListener(new ChangeListener() {
/** /**
* Should always try to synchronize any call to * Should always try to synchronize any call to
* progressSlider.setValue() to avoid a different thread * progressSlider.setValue() to avoid a different thread changing
* changing playbin while stateChanged() is processing * playbin while stateChanged() is processing
*/ */
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
int time = progressSlider.getValue(); int time = progressSlider.getValue();
synchronized(playbinLock) { synchronized (playbinLock) {
if(playbin2 != null && !autoTracking) { if (playbin2 != null && !autoTracking) {
State orig = playbin2.getState(); State orig = playbin2.getState();
playbin2.pause(); playbin2.pause();
playbin2.seek(ClockTime.fromMillis(time)); 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 }// </editor-fold>//GEN-END:initComponents
private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed
synchronized(playbinLock) { synchronized (playbinLock) {
if(playbin2.getState().equals(State.PLAYING)){ State state = playbin2.getState();
if (state.equals(State.PLAYING)) {
playbin2.pause(); playbin2.pause();
pauseButton.setText(""); pauseButton.setText("");
playbin2.setState(State.PAUSED); playbin2.setState(State.PAUSED);
} else if(playbin2.getState().equals(State.PAUSED)) { } else if (state.equals(State.PAUSED)) {
playbin2.play(); playbin2.play();
pauseButton.setText("||"); pauseButton.setText("||");
playbin2.setState(State.PLAYING); playbin2.setState(State.PLAYING);
} else if(playbin2.getState().equals(State.READY)) { } else if (state.equals(State.READY)) {
ExtractMedia em = new ExtractMedia(currentFile, getJFile(currentFile)); ExtractMedia em = new ExtractMedia(currentFile, getJFile(currentFile));
em.execute(); em.execute();
} }
} }
}//GEN-LAST:event_pauseButtonActionPerformed }//GEN-LAST:event_pauseButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton pauseButton; private javax.swing.JButton pauseButton;
private javax.swing.JLabel progressLabel; private javax.swing.JLabel progressLabel;
@ -185,22 +183,27 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
public void setNode(Node selectedNode) { public void setNode(Node selectedNode) {
reset(); reset();
setComponentsVisibility(false); setComponentsVisibility(false);
if(selectedNode == null) { return; } if (selectedNode == null) {
return;
}
File file = selectedNode.getLookup().lookup(File.class); File file = selectedNode.getLookup().lookup(File.class);
if(file == null) { return; } if (file == null) {
return;
}
currentFile = file; currentFile = file;
if(containsExt(file.getName(), IMAGES)) { if (containsExt(file.getName(), IMAGES)) {
showImage(file); showImage(file);
} else if(containsExt(file.getName(), VIDEOS) || containsExt(file.getName(), AUDIOS)) { } else if (containsExt(file.getName(), VIDEOS) || containsExt(file.getName(), AUDIOS)) {
setupVideo(file); setupVideo(file);
} }
} }
/** /**
* Initialize vars and display the image on the panel. * Initialize vars and display the image on the panel.
* @param file *
* @param file
*/ */
private void showImage(File file) { private void showImage(File file) {
java.io.File ioFile = getJFile(file); java.io.File ioFile = getJFile(file);
@ -211,59 +214,60 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
logger.log(Level.WARNING, "Error buffering file", ex); logger.log(Level.WARNING, "Error buffering file", ex);
} }
} }
videoComponent = new VideoComponent(); videoComponent = new VideoComponent();
synchronized(playbinLock) { synchronized (playbinLock) {
playbin2 = new PlayBin2("ImageViewer"); playbin2 = new PlayBin2("ImageViewer");
playbin2.setVideoSink(videoComponent.getElement()); playbin2.setVideoSink(videoComponent.getElement());
} }
videoPanel.removeAll(); videoPanel.removeAll();
videoPanel.setLayout(new BoxLayout(videoPanel, BoxLayout.Y_AXIS)); videoPanel.setLayout(new BoxLayout(videoPanel, BoxLayout.Y_AXIS));
videoPanel.add(videoComponent); videoPanel.add(videoComponent);
videoPanel.revalidate(); videoPanel.revalidate();
videoPanel.repaint(); videoPanel.repaint();
synchronized(playbinLock) { synchronized (playbinLock) {
playbin2.setInputFile(ioFile); playbin2.setInputFile(ioFile);
playbin2.play(); playbin2.play();
} }
videoPanel.setVisible(true); videoPanel.setVisible(true);
} }
/** /**
* Initialize all the necessary vars to play a video/audio file. * Initialize all the necessary vars to play a video/audio file.
*
* @param file the File to play * @param file the File to play
*/ */
private void setupVideo(File file) { private void setupVideo(File file) {
java.io.File ioFile = getJFile(file); java.io.File ioFile = getJFile(file);
pauseButton.setText(""); pauseButton.setText("");
progressSlider.setValue(0); progressSlider.setValue(0);
videoComponent = new VideoComponent(); videoComponent = new VideoComponent();
synchronized(playbinLock) { synchronized (playbinLock) {
playbin2 = new PlayBin2("VideoPlayer"); playbin2 = new PlayBin2("VideoPlayer");
playbin2.setVideoSink(videoComponent.getElement()); playbin2.setVideoSink(videoComponent.getElement());
} }
videoPanel.removeAll(); videoPanel.removeAll();
videoPanel.setLayout(new BoxLayout(videoPanel, BoxLayout.Y_AXIS)); videoPanel.setLayout(new BoxLayout(videoPanel, BoxLayout.Y_AXIS));
videoPanel.add(videoComponent); videoPanel.add(videoComponent);
videoPanel.revalidate(); videoPanel.revalidate();
videoPanel.repaint(); videoPanel.repaint();
synchronized(playbinLock) { synchronized (playbinLock) {
playbin2.setInputFile(ioFile); playbin2.setInputFile(ioFile);
playbin2.setState(State.READY); playbin2.setState(State.READY);
} }
setComponentsVisibility(true); setComponentsVisibility(true);
} }
/** /**
* To set the visibility of specific components in this class. * 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) { private void setComponentsVisibility(boolean isVisible) {
pauseButton.setVisible(isVisible); pauseButton.setVisible(isVisible);
@ -297,18 +301,18 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
// we don't want this to do anything // we don't want this to do anything
// because we already reset on each selected node // because we already reset on each selected node
} }
private void reset() { private void reset() {
synchronized(playbinLock) { synchronized (playbinLock) {
if(playbin2 != null) { if (playbin2 != null) {
if(playbin2.isPlaying()) { if (playbin2.isPlaying()) {
playbin2.stop(); playbin2.stop();
} }
playbin2.setState(State.NULL); playbin2.setState(State.NULL);
// try { // try {
// Thread.sleep(20); // gstreamer needs to catch up // Thread.sleep(20); // gstreamer needs to catch up
// } catch (InterruptedException ex) { } // } catch (InterruptedException ex) { }
if(playbin2.getState().equals(State.NULL)) { if (playbin2.getState().equals(State.NULL)) {
playbin2.dispose(); playbin2.dispose();
} }
playbin2 = null; playbin2 = null;
@ -331,29 +335,29 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
if (File.dirFlagToValue(file.getDir_flags()).equals(TskData.TSK_FS_NAME_FLAG_ENUM.TSK_FS_NAME_FLAG_UNALLOC.toString())) { if (File.dirFlagToValue(file.getDir_flags()).equals(TskData.TSK_FS_NAME_FLAG_ENUM.TSK_FS_NAME_FLAG_UNALLOC.toString())) {
return false; return false;
} }
if(file.getSize() == 0) { if (file.getSize() == 0) {
return false; return false;
} }
String name = file.getName().toLowerCase(); 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; return true;
} }
return false; return false;
} }
@Override @Override
public int isPreferred(Node node, boolean isSupported) { public int isPreferred(Node node, boolean isSupported) {
if(isSupported) { if (isSupported) {
return 7; return 7;
} else { } else {
return 0; return 0;
} }
} }
private static boolean containsExt(String name, String[] exts) { private static boolean containsExt(String name, String[] exts) {
int extStart = name.lastIndexOf("."); int extStart = name.lastIndexOf(".");
String ext = ""; String ext = "";
@ -362,7 +366,7 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
} }
return Arrays.asList(exts).contains(ext); return Arrays.asList(exts).contains(ext);
} }
private java.io.File getJFile(File file) { private java.io.File getJFile(File file) {
// Get the temp folder path of the case // Get the temp folder path of the case
String tempPath = Case.getCurrentCase().getTempDirectory(); String tempPath = Case.getCurrentCase().getTempDirectory();
@ -377,9 +381,9 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
java.io.File tempFile = new java.io.File(tempPath); java.io.File tempFile = new java.io.File(tempPath);
return tempFile; return tempFile;
} }
/* Thread that extracts and plays a file */ /* Thread that extracts and plays a file */
private class ExtractMedia extends SwingWorker<Object,Void> { private class ExtractMedia extends SwingWorker<Object, Void> {
private ProgressHandle progress; private ProgressHandle progress;
boolean success = false; boolean success = false;
@ -387,11 +391,13 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
private java.io.File jFile; private java.io.File jFile;
String duration; String duration;
String position; 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.sFile = sFile;
this.jFile = jFile; this.jFile = jFile;
}; }
;
@Override @Override
protected Object doInBackground() throws Exception { protected Object doInBackground() throws Exception {
@ -438,39 +444,49 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
progressLabel.setText("Error buffering file"); progressLabel.setText("Error buffering file");
return; return;
} }
synchronized(playbinLock) { ClockTime dur = null;
synchronized (playbinLock) {
playbin2.play(); // must play, then pause and get state to get duration. playbin2.play(); // must play, then pause and get state to get duration.
playbin2.pause(); playbin2.pause();
playbin2.getState(); playbin2.getState();
duration = playbin2.queryDuration().toString(); dur = playbin2.queryDuration();
durationMillis = playbin2.queryDuration().toMillis();
} }
progressSlider.setMaximum((int)durationMillis); duration = dur.toString();
durationMillis = dur.toMillis();
progressSlider.setMaximum((int) durationMillis);
progressSlider.setMinimum(0); progressSlider.setMinimum(0);
final String finalDuration; 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); finalDuration = duration.substring(3);
progressLabel.setText("00:00/" + duration); progressLabel.setText("00:00/" + duration);
} else { } else {
finalDuration = duration; finalDuration = duration;
progressLabel.setText("00:00:00/" + duration); progressLabel.setText("00:00:00/" + duration);
} }
synchronized(playbinLock) { synchronized (playbinLock) {
playbin2.play(); playbin2.play();
} }
pauseButton.setText("||"); pauseButton.setText("||");
new Thread(new Runnable() { new Thread(new Runnable() {
private boolean isPlayBinReady() {
synchronized (playbinLock) {
return playbin2 != null && playbin2.getState().equals(State.NULL);
}
}
@Override @Override
public void run() { public void run() {
long positionMillis = 0; long positionMillis = 0;
while (positionMillis < durationMillis while (positionMillis < durationMillis
&& playbin2 != null && isPlayBinReady() ) {
&& !playbin2.getState().equals(State.NULL)) { ClockTime pos = null;
synchronized(playbinLock) { synchronized (playbinLock) {
position = playbin2.queryPosition().toString(); pos = playbin2.queryPosition();
positionMillis = playbin2.queryPosition().toMillis(); }
} position = pos.toString();
positionMillis = pos.toMillis();
if (position.length() == 8) { if (position.length() == 8) {
position = position.substring(3); position = position.substring(3);
} }
@ -480,7 +496,8 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
autoTracking = false; autoTracking = false;
try { try {
Thread.sleep(20); Thread.sleep(20);
} catch (InterruptedException ex) { } } catch (InterruptedException ex) {
}
} }
if (finalDuration.length() == 5) { if (finalDuration.length() == 5) {
progressLabel.setText("00:00/" + finalDuration); progressLabel.setText("00:00/" + finalDuration);
@ -488,13 +505,13 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
progressLabel.setText("00:00:00/" + finalDuration); progressLabel.setText("00:00:00/" + finalDuration);
} }
// If it reached the end // If it reached the end
if(progressSlider.getValue() == progressSlider.getMaximum()) { if (progressSlider.getValue() == progressSlider.getMaximum()) {
restartVideo(); restartVideo();
} }
} }
public void restartVideo() { public void restartVideo() {
synchronized(playbinLock) { synchronized (playbinLock) {
if (playbin2 != null) { if (playbin2 != null) {
playbin2.stop(); playbin2.stop();
playbin2.setState(State.READY); // ready to be played again playbin2.setState(State.READY); // ready to be played again