Updated vlc video component use and fixed bugs

This commit is contained in:
Jeff Wallace 2013-07-31 15:59:02 -04:00
parent fce1446bbc
commit a18fdc33e7
2 changed files with 399 additions and 344 deletions

View File

@ -110,34 +110,37 @@ public class DataContentViewerMedia extends javax.swing.JPanel implements DataCo
@Override @Override
public void setNode(Node selectedNode) { public void setNode(Node selectedNode) {
try {
if (selectedNode == null) {
videoPanel.reset();
return;
}
AbstractFile file = selectedNode.getLookup().lookup(AbstractFile.class);
if (file == null) {
return;
}
if (file.equals(lastFile)) {
return; //prevent from loading twice if setNode() called mult. times
} else {
lastFile = file;
}
if (selectedNode == null) {
videoPanel.reset(); videoPanel.reset();
return;
}
AbstractFile file = selectedNode.getLookup().lookup(AbstractFile.class); final Dimension dims = DataContentViewerMedia.this.getSize();
if (file == null) {
return;
}
if (file.equals(lastFile)) { if (imagePanelInited && containsExt(file.getName(), IMAGES)) {
return; //prevent from loading twice if setNode() called mult. times imagePanel.showImageFx(file, dims);
} else { this.switchPanels(false);
lastFile = file; } else if (videoPanelInited
} && (containsExt(file.getName(), VIDEOS) || containsExt(file.getName(), AUDIOS))) {
videoPanel.setupVideo(file, dims);
videoPanel.reset(); switchPanels(true);
}
final Dimension dims = DataContentViewerMedia.this.getSize(); } catch (Exception e) {
logger.log(Level.SEVERE, "Exception while setting node", e);
if (imagePanelInited && containsExt(file.getName(), IMAGES)) {
imagePanel.showImageFx(file, dims);
this.switchPanels(false);
} else if (videoPanelInited
&& (containsExt(file.getName(), VIDEOS) || containsExt(file.getName(), AUDIOS))) {
videoPanel.setupVideo(file, dims);
switchPanels(true);
} }
} }

View File

@ -38,8 +38,6 @@ import javax.swing.SwingUtilities;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import org.gstreamer.elements.PlayBin2;
import org.gstreamer.swing.VideoComponent;
import org.netbeans.api.progress.ProgressHandle; import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory; import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.util.Cancellable; import org.openide.util.Cancellable;
@ -68,23 +66,21 @@ import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapture { public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapture {
private static final Logger logger = Logger.getLogger(MediaViewVideoPanel.class.getName()); private static final Logger logger = Logger.getLogger(MediaViewVideoPanel.class.getName());
private boolean gstInited;
private boolean vlcInited; private boolean vlcInited;
private static final long MIN_FRAME_INTERVAL_MILLIS = 500; private static final long MIN_FRAME_INTERVAL_MILLIS = 500;
private static final long FRAME_CAPTURE_TIMEOUT_MILLIS = 1000; private static final long FRAME_CAPTURE_TIMEOUT_MILLIS = 1000;
private static final String MEDIA_PLAYER_ERROR_STRING = "The media player cannot process this file."; private static final String MEDIA_PLAYER_ERROR_STRING = "The media player cannot process this file.";
private static final int POS_FACTOR = 10000;
//playback //playback
private long durationMillis = 0; private long durationMillis = 0;
private VideoProgressWorker videoProgressWorker; private VideoProgressWorker videoProgressWorker;
private int totalHours, totalMinutes, totalSeconds; private int totalHours, totalMinutes, totalSeconds;
private volatile PlayBin2 gstPlaybin2;
private MediaPlayer vlcMediaPlayer; private MediaPlayer vlcMediaPlayer;
private VideoComponent gstVideoComponent;
private EmbeddedMediaPlayerComponent vlcVideoComponent; private EmbeddedMediaPlayerComponent vlcVideoComponent;
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 of gstPlaybin2 player
private AbstractFile currentFile; private AbstractFile currentFile;
private boolean isFileLoaded; private java.io.File currentVideoFile;
private boolean replay;
private Set<String> badVideoFiles = Collections.synchronizedSet(new HashSet<String>()); private Set<String> badVideoFiles = Collections.synchronizedSet(new HashSet<String>());
/** /**
@ -112,12 +108,10 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
} }
public EmbeddedMediaPlayerComponent getVideoComponent() { public EmbeddedMediaPlayerComponent getVideoComponent() {
// return gstVideoComponent;
return vlcVideoComponent; return vlcVideoComponent;
} }
public boolean isInited() { public boolean isInited() {
// return gstInited;
return vlcInited; return vlcInited;
} }
@ -137,14 +131,15 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
*/ */
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
int time = progressSlider.getValue(); if (vlcMediaPlayer != null && !autoTracking) {
if (vlcMediaPlayer != null && !autoTracking) { float positionValue = progressSlider.getValue() / (float) POS_FACTOR;
boolean wasPlaying = !vlcMediaPlayer.isPlaying(); // Avoid end of file freeze-up
vlcMediaPlayer.setPause(true); if (positionValue > 0.99f) {
vlcMediaPlayer.setTime(time); positionValue = 0.99f;
vlcMediaPlayer.setPause(wasPlaying);
} }
vlcMediaPlayer.setPosition(positionValue);
} }
}
}); });
} }
@ -162,6 +157,27 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
} }
} }
/**
* Reset the video for replaying the current file.
*
* Called when finished Event is fired from MediaPlayer.
*/
private void finished() {
if (videoProgressWorker != null) {
videoProgressWorker.cancel(true);
videoProgressWorker = null;
}
logger.log(Level.INFO, "Resetting media");
replay = true;
}
/**
* Load the VLC library dll using JNA.
*
* @return <code>true</code>, if the library was loaded correctly.
* <code>false</code>, otherwise.
*/
private boolean initVlc() { private boolean initVlc() {
try { try {
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
@ -184,7 +200,7 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
void setupVideo(final AbstractFile file, final Dimension dims) { void setupVideo(final AbstractFile file, final Dimension dims) {
infoLabel.setText(""); infoLabel.setText("");
currentFile = file; currentFile = file;
isFileLoaded = false; replay = false;
final boolean deleted = file.isDirNameFlagSet(TskData.TSK_FS_NAME_FLAG_ENUM.UNALLOC); final boolean deleted = file.isDirNameFlagSet(TskData.TSK_FS_NAME_FLAG_ENUM.UNALLOC);
if (deleted) { if (deleted) {
infoLabel.setText("Playback of deleted videos is not supported, use an external player."); infoLabel.setText("Playback of deleted videos is not supported, use an external player.");
@ -205,8 +221,6 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
pauseButton.setEnabled(true); pauseButton.setEnabled(true);
progressSlider.setEnabled(true); progressSlider.setEnabled(true);
java.io.File ioFile = getJFile(file);
vlcVideoComponent = new EmbeddedMediaPlayerComponent(); vlcVideoComponent = new EmbeddedMediaPlayerComponent();
vlcMediaPlayer = vlcVideoComponent.getMediaPlayer(); vlcMediaPlayer = vlcVideoComponent.getMediaPlayer();
vlcMediaPlayer.setPlaySubItems(true); vlcMediaPlayer.setPlaySubItems(true);
@ -216,6 +230,9 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
videoPanel.add(vlcVideoComponent); videoPanel.add(vlcVideoComponent);
videoPanel.setVisible(true); videoPanel.setVisible(true);
logger.log(Level.INFO, "Created media player."); logger.log(Level.INFO, "Created media player.");
ExtractMedia em = new ExtractMedia(currentFile, getJFile(currentFile));
em.execute();
} }
void reset() { void reset() {
@ -231,6 +248,12 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
return; return;
} }
// get rid of any existing videoProgressWorker thread
if (videoProgressWorker != null) {
videoProgressWorker.cancel(true);
videoProgressWorker = null;
}
if (vlcMediaPlayer != null) { if (vlcMediaPlayer != null) {
if (vlcMediaPlayer.isPlaying()) { if (vlcMediaPlayer.isPlaying()) {
vlcMediaPlayer.stop(); vlcMediaPlayer.stop();
@ -245,13 +268,8 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
logger.log(Level.INFO, "Released video component"); logger.log(Level.INFO, "Released video component");
} }
// get rid of any existing videoProgressWorker thread
if (videoProgressWorker != null) {
videoProgressWorker.cancel(true);
videoProgressWorker = null;
}
currentFile = null; currentFile = null;
currentVideoFile = null;
} }
private java.io.File getJFile(AbstractFile file) { private java.io.File getJFile(AbstractFile file) {
@ -269,6 +287,45 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
return tempFile; return tempFile;
} }
void playMedia() {
logger.log(Level.INFO, "In play media");
if (currentVideoFile == null || !currentVideoFile.exists()) {
progressLabel.setText("Error buffering file");
return;
}
boolean mediaPrepared = vlcMediaPlayer.prepareMedia(currentVideoFile.getAbsolutePath());
if (mediaPrepared) {
vlcMediaPlayer.parseMedia();
durationMillis = vlcMediaPlayer.getMediaMeta().getLength();
logger.log(Level.INFO, "Media loaded correctly");
} else {
progressLabel.setText(MEDIA_PLAYER_ERROR_STRING);
}
// pick out the total hours, minutes, seconds
long durationSeconds = (int) durationMillis / 1000;
totalHours = (int) durationSeconds / 3600;
durationSeconds -= totalHours * 3600;
totalMinutes = (int) durationSeconds / 60;
durationSeconds -= totalMinutes * 60;
totalSeconds = (int) durationSeconds;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressSlider.setMaximum(POS_FACTOR);
progressSlider.setMinimum(0);
logger.log(Level.INFO, "Starting the media...");
vlcMediaPlayer.start();
pauseButton.setText("||");
videoProgressWorker = new VideoProgressWorker();
videoProgressWorker.execute();
}
});
}
/** /**
* @param file a video file from which to capture frames * @param file a video file from which to capture frames
* @param numFrames the number of frames to capture. These frames will be * @param numFrames the number of frames to capture. These frames will be
@ -359,6 +416,8 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
} }
}); });
progressSlider.setSnapToTicks(true);
org.openide.awt.Mnemonics.setLocalizedText(progressLabel, org.openide.util.NbBundle.getMessage(MediaViewVideoPanel.class, "MediaViewVideoPanel.progressLabel.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(progressLabel, org.openide.util.NbBundle.getMessage(MediaViewVideoPanel.class, "MediaViewVideoPanel.progressLabel.text")); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(infoLabel, org.openide.util.NbBundle.getMessage(MediaViewVideoPanel.class, "MediaViewVideoPanel.infoLabel.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(infoLabel, org.openide.util.NbBundle.getMessage(MediaViewVideoPanel.class, "MediaViewVideoPanel.infoLabel.text")); // NOI18N
@ -407,15 +466,16 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
}// </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
if (!isFileLoaded) { if (replay) {
// First time play button is pressed // File has completed playing. Play button now replays
logger.log(Level.INFO, "Loading media file"); logger.log(Level.INFO, "Replaying video.");
ExtractMedia em = new ExtractMedia(currentFile, getJFile(currentFile)); replay = false;
em.execute(); playMedia();
em.getExtractedBytes();
} else if (vlcMediaPlayer.isPlaying()) { } else if (vlcMediaPlayer.isPlaying()) {
logger.log(Level.INFO, "Pausing.");
this.pause(); this.pause();
} else { } else {
logger.log(Level.INFO, "Playing");
this.unPause(); this.unPause();
} }
}//GEN-LAST:event_pauseButtonActionPerformed }//GEN-LAST:event_pauseButtonActionPerformed
@ -428,10 +488,23 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
private javax.swing.JPanel videoPanel; private javax.swing.JPanel videoPanel;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
void releaseVlcComponents() {
if (vlcMediaPlayer != null) {
vlcMediaPlayer.release();
vlcMediaPlayer = null;
}
if (vlcVideoComponent != null) {
vlcVideoComponent.release();
vlcVideoComponent = null;
}
}
private class VideoProgressWorker extends SwingWorker<Object, Object> { private class VideoProgressWorker extends SwingWorker<Object, Object> {
private String durationFormat = "%02d:%02d:%02d/%02d:%02d:%02d "; private String durationFormat = "%02d:%02d:%02d/%02d:%02d:%02d ";
private long millisElapsed = 0; private long millisElapsed = 0;
private float currentPosition = 0.0f;
private final long INTER_FRAME_PERIOD_MS = 20; private final long INTER_FRAME_PERIOD_MS = 20;
private final long END_TIME_MARGIN_MS = 50; private final long END_TIME_MARGIN_MS = 50;
private boolean hadError = false; private boolean hadError = false;
@ -441,28 +514,23 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
} }
private void resetVideo() throws Exception { private void resetVideo() throws Exception {
logger.log(Level.INFO, "Resetting video.");
if (vlcMediaPlayer != null) {
vlcMediaPlayer.stop();
}
pauseButton.setText(""); pauseButton.setText("");
progressSlider.setValue(0);
String durationStr = String.format(durationFormat, 0, 0, 0, String durationStr = String.format(durationFormat, 0, 0, 0,
totalHours, totalMinutes, totalSeconds); totalHours, totalMinutes, totalSeconds);
progressLabel.setText(durationStr); progressLabel.setText(durationStr);
} }
/** // /**
* @return true while millisElapsed is greater than END_TIME_MARGIN_MS // * @return true while millisElapsed is greater than END_TIME_MARGIN_MS
* from durationMillis. This is used to indicate when the video has // * from durationMillis. This is used to indicate when the video has
* ended because for some videos the time elapsed never becomes equal to // * ended because for some videos the time elapsed never becomes equal to
* the reported duration of the video. // * the reported duration of the video.
*/ // */
private boolean hasNotEnded() { // private boolean hasNotEnded() {
boolean ended = (durationMillis - millisElapsed) > END_TIME_MARGIN_MS; // boolean ended = (durationMillis - millisElapsed) > END_TIME_MARGIN_MS;
return ended; // return ended;
} // }
@Override @Override
protected Object doInBackground() throws Exception { protected Object doInBackground() throws Exception {
@ -471,9 +539,10 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
progressSlider.setEnabled(true); progressSlider.setEnabled(true);
int elapsedHours = -1, elapsedMinutes = -1, elapsedSeconds = -1; int elapsedHours = -1, elapsedMinutes = -1, elapsedSeconds = -1;
while (hasNotEnded() && isMediaPlayerReady() && !isCancelled()) { while (isMediaPlayerReady() && !isCancelled()) {
millisElapsed = vlcMediaPlayer.getTime(); millisElapsed = vlcMediaPlayer.getTime();
currentPosition = vlcMediaPlayer.getPosition();
// pick out the elapsed hours, minutes, seconds // pick out the elapsed hours, minutes, seconds
long secondsElapsed = millisElapsed / 1000; long secondsElapsed = millisElapsed / 1000;
@ -489,7 +558,7 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
progressLabel.setText(durationStr); progressLabel.setText(durationStr);
autoTracking = true; autoTracking = true;
progressSlider.setValue((int) millisElapsed); progressSlider.setValue((int) (currentPosition * POS_FACTOR));
autoTracking = false; autoTracking = false;
try { try {
@ -562,58 +631,41 @@ public class MediaViewVideoPanel extends javax.swing.JPanel implements FrameCapt
logger.log(Level.SEVERE, "Fatal error during media buffering.", ex); logger.log(Level.SEVERE, "Fatal error during media buffering.", ex);
} finally { } finally {
progress.finish(); progress.finish();
isFileLoaded = true;
if (!this.isCancelled()) { if (!this.isCancelled()) {
logger.log(Level.INFO, "Loaded media file"); logger.log(Level.INFO, "Loaded media file");
currentVideoFile = jFile;
playMedia(); playMedia();
} }
} }
} }
void playMedia() {
if (jFile == null || !jFile.exists()) {
progressLabel.setText("Error buffering file");
return;
}
boolean mediaPrepared = vlcMediaPlayer.prepareMedia(jFile.getAbsolutePath());
if (mediaPrepared) {
vlcMediaPlayer.parseMedia();
durationMillis = vlcMediaPlayer.getMediaMeta().getLength();
logger.log(Level.INFO, "Media loaded correctly");
} else {
progressLabel.setText(MEDIA_PLAYER_ERROR_STRING);
}
// pick out the total hours, minutes, seconds
long durationSeconds = (int) durationMillis / 1000;
totalHours = (int) durationSeconds / 3600;
durationSeconds -= totalHours * 3600;
totalMinutes = (int) durationSeconds / 60;
durationSeconds -= totalMinutes * 60;
totalSeconds = (int) durationSeconds;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressSlider.setMaximum((int) durationMillis);
progressSlider.setMinimum(0);
logger.log(Level.INFO, "Starting the media...");
vlcMediaPlayer.start();
pauseButton.setText("||");
videoProgressWorker = new VideoProgressWorker();
videoProgressWorker.execute();
}
});
}
} }
private class VlcMediaPlayerEventListener extends MediaPlayerEventAdapter { private class VlcMediaPlayerEventListener extends MediaPlayerEventAdapter {
@Override
public void finished(MediaPlayer mediaPlayer) {
logger.log(Level.INFO, "Media Player finished playing the media");
finished();
}
@Override @Override
public void error(MediaPlayer mediaPlayer) { public void error(MediaPlayer mediaPlayer) {
logger.log(Level.INFO, "an Error occured."); logger.log(Level.INFO, "an Error occured.");
} }
@Override
public void mediaDurationChanged(MediaPlayer mediaPlayer, long newDuration) {
logger.log(Level.INFO, "DURATION CHANGED: " + newDuration);
}
@Override
public void mediaFreed(MediaPlayer mediaPlayer) {
logger.log(Level.INFO, "Media was freed");
}
@Override
public void lengthChanged(MediaPlayer mediaPlayer, long newLength) {
logger.log(Level.INFO, "LENGTH CHANGED: " + newLength);
}
} }
} }