Java – How can I send a live video stream from my phone to a remote server!

How can I send a live video stream from my phone to a remote server!… here is a solution to the problem.

How can I send a live video stream from my phone to a remote server!

I’m having trouble streaming videos from my phone to a server live.
That is, let my phone become a webcam and the server can watch live video from my phone

I googled many solutions,
But no one could solve my problem.
I use MediaRecorder to record .
It can properly save video files in SD card.
I then quote this page and some of the following methods

 skt = new Socket(InetAddress.getByName(hostname),port);
 pfd =ParcelFileDescriptor.fromSocket(skt);
 mediaRecorder.setOutputFile(pfd.getFileDescriptor());

Now it seems that I can send a video stream while recording

However, I wrote a receiver program to receive a video stream from Android,
But it doesn’t work. Is there something wrong?
I can receive files, but I can’t open video files.
I guess the problem might be caused by the file format?

There is an outline of my code.

On Android

    Socket skt = new Socket(hostIP,port);
ParcelFileDescriptor pfd =ParcelFileDescriptor.fromSocket(skt);
....
....
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(pfd.getFileDescriptor());
.....
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
.....
mediaRecorder.start();

On the receiver side (my ACER notebook).

// anyway , I don't think the file extentions will do any effect                
File video = new File (strDate+".3gpp");
FileOutputStream fos;
try {
fos = new FileOutputStream(video);

byte[] data = new byte[1024];

int count =-1;

while( (count = fin.read(data,0,1024) ) !=-1)
{
    fos.write(data,0,count);                                
    fos.flush();    
}                                       
fos.close();
fin.close();

Confused for a long time….
Thanks in advance

Solution

Poc,

Here’s how MediaRecorder writes files:
Make room for empty titles
Write the contents of the file when recording
When recording is complete, navigate to the beginning of the file
Write a title at the beginning of the file
Then (I believe) there is another search to the end of the file where the metadata is written.

Because there is no concept of “seeking” on sockets, you have to figure out when the title is coming, look for the beginning of the file, and then write the title in the appropriate place.

The best starting point is to use a hex editor to determine the format of a valid 3GPP file, and then analyze this hexadecimal based on the hexadecimal output of your receiver program. You also need to look at the 3GPP file format.

Related Problems and Solutions