Stream video over a socket and play from a client

I have two simple programs, a server and a client, both on localhost. What I want to do is stream video from the server to the client via a socket, and the client can play it using the filediscriptor socket. First I try to send a message and the client can receive it. After that, I send the client a few bytes of video from the server card. The client can receive these bytes, but cannot reproduce it. Does anyone know how to solve a problem?

Here are my snippets of server and client code:

Server:

//Receive request from client. Socket client=serversocket.accept(); System.out.println("accept"); //Receive client message. BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); String str=in.readLine(); System.out.println("read:"+str); //Send message to client. //PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true); //out.println("server message"); FileInputStream fis=new FileInputStream("/sdcard/toystory3.3gp"); byte buffer[]=new byte[2000]; fis.read(buffer,0,20); DataOutputStream out=new DataOutputStream(client.getOutputStream()); out.write(buffer,0,20); in.close(); out.close(); client.close(); System.out.println("close"); 

Client:

  Socket socket=new Socket("127.0.0.1",4444); String message="Initial"+"\r\n"; //Send message to server. PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); out.println(message); //Receive message from server. BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream())); String msg=br.readLine(); //ParcelFileDescriptor pfd=ParcelFileDescriptor.fromSocket(socket); //MediaPlayer m=new MediaPlayer(); //m.setDataSource(pfd.getFileDescriptor()); //m.prepare(); //m.start(); if(msg!=null) { System.out.println("Data received."); System.out.println(msg); } else { System.out.println("Data not received."); } out.close(); br.close(); socket.close(); 
+6
android
source share
2 answers

This will not work because 3gp (and other avi derived files like mp4 etc.) have a (sic) header at the end of the file. Thus, any player must have access to the entire file.

RTSP/RTP is the only way to stream video at the moment. HTTP adaptive streaming works.

Also, if you are trying to make p2p video (device to device), you should be aware that all devices in carrier networks are behind the NAT firewall. They can only open outgoing connections. You will need to use some kind of NAT piercing.

+5
source share

I tried porting ffmpeg to android to solve the problem of video and streaming formats for Android. But I found out that creating ffmpeg for android with all the network stuff is pretty complicated.

RTSP has a lot of settings for streaming, I managed to get something like what you want with MediaPlayer, etc., you should try to find out which formats you are switching from (vlc in this case) is a little better .

Also, take a lot on the darwin streaming server, it is easy to configure, I was able to transfer it to the Android device using it.

0
source share

All Articles