Monday, September 10, 2012

[android-developers] Save video stream from Socket to File (from Android app to Java server)

I use my Android application for streaming video from phone camera to my PC Server and need to save them into file on HDD. So, file created and stream successfully saved, but the resulting file can not play with any video player (GOM, KMP, Windows Media Player, VLC etc.) - no picture, no sound, only playback errors.

I tested my Android application into phone and may say that in this instance captured video successfully stored on phone SD card and after transfer it to PC played witout errors, so, my code is correct.

In the end, I realized that the problem in the video container: data streamed from phone in MP4 format and stored in *.mp4 files on PC, and in this case, file may be incorrect for playback with video players. Can anyone suggest how to correctly save streaming video to a file?

There is my code that process and store stream data (without errors handling to simplify):

// getOutputMediaFile() returns a new File object

DataInputStream in = new DataInputStream (server.getInputStream());
FileOutputStream videoFile = new FileOutputStream(getOutputMediaFile());
int len;
byte buffer[] = new byte[8192];

while((len = in.read(buffer)) != -1) {
videoFile.write(buffer, 0, len);
}

videoFile.close();
server.close();

For record video locally to phone storage I use:

//targetFile - new File object, represents a file on phone SD card

myMediaRecorder.setOutputFile(targetFile);

And for streaming it to PC (without errors handling to simplify)

ParcelFileDescriptor pfd = null;
Socket socket = null;
String hostname = "my IP";
int port = 8081;

socket = new Socket(InetAddress.getByName(hostname), port);
pfd = ParcelFileDescriptor.fromSocket(socket);

myMediaRecorder.setOutputFile(pfd.getFileDescriptor());

Also, I would appreciate if someone will talk about the possible "pitfalls" in dealing with the conservation of media streams.

Thank you, I hope for your help!

Alex.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment