7

I have two simple programmes, server and client, both at localhost. What I want to do is to streaming the video from the server to the client through socket and the client can play it by using filediscriptor of the socket. First I try to send some message and the client can receive it. After that I send a few bytes of the video from the server'sd card to the client. The client can receive those bytes but cannot play it. Anyone know how to solve the problem?

Here is my server and client code snippets:

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();
charlie
  • 95
  • 1
  • 1
  • 5
  • Have a look my- http://stackoverflow.com/questions/6116880/stream-live-video-from-phone-to-phone-using-socket-fd/10260068#10260068 – Suvam Roy Apr 21 '12 at 15:00

2 Answers2

5

This won't work because 3gp (and other avi derived files like mp4, etc) have header (sic) at the end of the file. So any player must have access to the whole file.

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

Also if you're trying to do p2p video (device to device) you should know that all devices on operator networks are behind the NAT firewall. They can only open connections outbound. You'll need to use some kind of NAT-piercing.

Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Peter, thanks for your reply. The reason I am using http streaming is because I want to build a simple httpserver in my phone to trick the mediaplayer that it is receiving http packets from outside. This httpserver needs to buffer the video packets from outside. Is that possible? Actually I have using vlc to stream rstp video packets to my phone. But I found those rtsp packets could not be buffered/saved. Do you have any suggestion/advice on this? – charlie Nov 21 '10 at 14:35
  • At the moment android does not yet support (adaptive) http streaming. It uses HTTP but just downloads the file. But if you use VLC that might work - I have no experience with VLC so I could not give you any advice here. – Peter Knego Nov 21 '10 at 15:21
0

I tried to port ffmpeg to android to solve the problem of video and streaming formats for android. But I found out that building ffmpeg for android with all the network stuff is pretty hard.

RTSP has a lot of configuration for streaming, I managed to get something like you want with the MediaPlayer and stuff, you should try to get to know the formats that you are streaming from (vlc in that case) a little better.

Also, take a lot at darwin streaming server its easy to configure, I was able to stream to an android device using it.

renam.antunes
  • 761
  • 1
  • 5
  • 11
  • Thanks a lot. I have managed to streaming rtsp packets by vlc, but the problem is android side seems not to support much functions of rtsp, for example, buffering streaming data. That's why I am trying to build a http local server to feed data as what I can control. Actually vlc also support http streaming, but it is quite unstable. I havn't tried darwin. – charlie Dec 01 '10 at 04:57