I trying to stream my Web Cam from ComputerA to computerB, using VLCj 3.0.1, VLC 2.1.3 here is the code
Code: Select all
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import uk.co.caprica.vlcj.loadDll.LoadVlcJ;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
public class VideoStream1 {
private final MediaPlayerFactory mediaPlayerFactory;
private final EmbeddedMediaPlayer localMediaPlayer;
private final EmbeddedMediaPlayer remoteMediaPlayer;
private final JFrame frame;
private final JPanel contentPane;
private final JPanel videoPanel;
private final Canvas localCanvas;
private final Canvas remoteCanvas;
private final CanvasVideoSurface localVideoSurface;
private final CanvasVideoSurface remoteVideoSurface;
public static void main(String[] args) throws Exception {
LoadVlcJ loadVlcJ = LoadVlcJ.loadLib();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new VideoStream1().start();
}
});
}
public VideoStream1() {
mediaPlayerFactory = new MediaPlayerFactory();
localMediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
remoteMediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
localMediaPlayer.setEnableKeyInputHandling(false);
localMediaPlayer.setEnableMouseInputHandling(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(16, 16, 16, 16));
contentPane.setLayout(new BorderLayout(16, 16));
localCanvas = new Canvas();
localCanvas.setBackground(Color.green);
localCanvas.setSize(120, 90);
localVideoSurface = mediaPlayerFactory.newVideoSurface(localCanvas);
localMediaPlayer.setVideoSurface(localVideoSurface);
remoteCanvas = new Canvas();
remoteCanvas.setBackground(Color.black);
remoteCanvas.setSize(320, 180);
remoteVideoSurface = mediaPlayerFactory.newVideoSurface(remoteCanvas);
remoteMediaPlayer.setVideoSurface(remoteVideoSurface);
videoPanel = new GridBagLayoutTest(localCanvas, remoteCanvas);
frame = new JFrame("vlcj video chat");
frame.setContentPane(videoPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev){
localMediaPlayer.release();
remoteMediaPlayer.release();
mediaPlayerFactory.release();
System.out.println("Released !!!!!!!!!!!");
}
});
frame.pack();
send();
receive();
}
private void start() {
frame.setVisible(true);
}
private void send() {
String mrl ="dshow://";
if(mrl.length() > 0) {
String streamTo = "192.168.1.3:5555";
String[] parts = streamTo.split(":");
if(parts.length == 2) {
String host = parts[0];
int port = Integer.parseInt(parts[1]);
String[] localOptions = {formatRtpStream(host, port), ":no-sout-rtp-sap", ":no-sout-standard-sap", ":sout-all", ":sout-keep",};
localMediaPlayer.playMedia(mrl, localOptions);
}
else {
JOptionPane.showMessageDialog(frame, "You must specify host:port to stream to.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(frame, "You must specify source media, e.g. v4l2:///dev/video0 on Linux or dshow:// on Windows.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void receive() {
String mrl = "192.168.1.2:5555";
remoteMediaPlayer.playMedia("rtp://@" + mrl);
}
private static String formatRtpStream(String serverAddress, int serverPort) {
StringBuilder sb = new StringBuilder(60);
sb.append(":sout=#transcode{vcodec=mp2v,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=display,dst=rtp{dst=");
sb.append(serverAddress);
sb.append(",port=");
sb.append(serverPort);
sb.append(",mux=ts}}");
System.out.println(sb.toString());
return sb.toString();
}
}
So my question is:
How Can I detect that the remote streaming was stopped ? Does LibVLC emits any event when that happens.
and Thanks for your help.