I am doing a project that is based on VLCJ streaming. I have looked up the the VLCJ tutorial on how to create the server side from the below code. How would the client get the video content from the server? I have written the client code as well but it's not displaying anything.
Any help would be appreciated. Thank you.
Server code:
package com.khalid.VideoStreaming;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;
public class PublicServer extends VlcjTest{
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Specify a single MRL to stream");
System.exit(1);
}
String media = args[0];
//String publicIP = "192.168.0.255";
String publicIP = "192.168.1.104";
short publicPort = 5555;
String options = formatRtpStream(publicIP, publicPort);
System.out.println("Streaming '" + media + "' to '" + options + "'");
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
mediaPlayer.playMedia(media, options, ":no-sout-rtp-sap", ":no-sout-standard-sap", ":sout-all", ":sout-keep");
Thread.currentThread().join(); // Don't exit
}
private static String formatRtpStream(String serverAddress, short serverPort) {
StringBuilder sb = new StringBuilder(200);
//sb.append(":sout=#transcode{acodec=mp4a,samplerate=12000,width=400,height=300}:rtp{dst=");
//sb.append("::sout=#transcode{vcodec=mp4v,vb=4096,scale=1,fps=30,acodec=mpga,ab=128,channels=2,samplerate=44100,width=800,height=600}:rtp:duplicate{dst=file{dst=");
sb.append("::sout=#transcode{vcodec=mp4v,vb=3000,fps=30,scale=1,acodec=mp4a,ab=128,channels=2,samplerate=48000,width=800,height=600}:rtp{dst=");
sb.append(serverAddress);
sb.append(",port=");
sb.append(serverPort);
sb.append(",mux=ts}");
return sb.toString();
}
}
Client side:
String mediatorIP = "192.168.1.104"; short mediatorPort = 6001;
String publicIP, publicServer, localIP, localServer, clientIP;
short publicPort, localPort;
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
Canvas canvas = new Canvas();
canvas.setBackground(Color.black);
CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
mediaPlayer.setVideoSurface(videoSurface);
JFrame f = new JFrame();
//f.setIconImage(new ImageIcon(Client.class.getResource("icons/vlcj-logo.png")).getImage());
f.add(canvas);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
publicIP = inFromServer.readLine(); // Recv public server's ip from mediator
publicPort = Short.parseShort(inFromServer.readLine()); // Recv public server's port from mediator
publicServer = formatRtpStream(publicIP, publicPort);
System.out.println("Capturing from '" + publicServer + "'");
f.setTitle("Capturing from Public Server 'rtp://" + publicIP + ":" + publicPort + "'");
mediaPlayer.playMedia(publicServer);
As you can see above, mediaPlayer.playMedia(publicServer) should be displaying the video contents but nothing is displayed. The JFrame is just empty.
The problem I had with the code shown above was the IP address of the server. The client would connect to the server but VLC wouldn't play the MRL. So I did a bit of research on caprica's examples online and found that I should use a multicast IP address to stream with RTP. (e.g. 230.0.0.1) and the problem got resolved. Streaming from the client side occurred without problem.