stream a MediaRecorder file while recording
I am trying to get a video stream from point A (2.1 android phone) to point B (my server) in real time. How would I do it? Detailed below are my attempts (a little long, but concise!)
The goal is to get an hour long video recorded with the phone to the server without pausing/stopping the stream. a delay of up to several minutes isn't a problem. I've tried three approaches
- thread a FileInputStream that reads from the FileDescriptor of the video destination
- call MediaRecoder.setOutputFile on the FD of a 'sender' socket. this socket connects to a LocalSocketServer, whose destination is a 'receiver' socket.
- open a socket to my server and giving setOutputFile its FD
unfortunately both attempts have failed.
- Only registers 24 bytes whenever i call FileInputStream.available(), and the actual number of bytes only after i call Recorder.stop()
gives me this beautifully useless stacktrace
ERROR/AndroidRuntime(18532): Caused by: java.lang.RuntimeException: start failed. ERROR/AndroidRuntime(18532): at android.media.MediaRecorder.start(Native Method) ERROR/AndroidRuntime(18532): at com.example.demovideo.DemoVideo.initializeCamera(...) ...
same error 2
code snippets (parts omitted)
1)
fileOut = new FileOutputStream(pathToFile);
...
recorder.setOutputFile(fileOut.getFD());
recorder.prepare()
recorder.start()
// in an Async Thread
fileIn = FileInputStream(fileOut.getFD);
while (recording) {
fos.flush();
Log.w("---", "bytesAvailable: " + fileIn.available()); //always returns 24
Thread.sleep(1开发者_开发问答000);
}
2)
// in a Thread
server = new LocalServerSocket(SOCKET_ADDRESS);
while (true){
receiver = server.accept();
if (receiver != null){
InputStream input = receiver.getInputStream();
... // processing would go here
} }
sender = new LocalSocket();
sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
recorder.setOutputFile(sender.getFileDescriptor());
...
recorder.prepare();
recorder.start(); // <- error
- properly saves an uncorrupted video to sd
- works if I use
setOutputFile(pathToFile) instead.
the sockets also work when i run
sender.getOutputStream().write(message.getBytes());
I created a mobile-to-server video streaming app with this approach and it worked. So this should be the right approach. Later when I was not part of the project anymore I got reports that this approach did not work with some newer phones - most notably Samsung Galaxy S. The problem was that this phones flushed video data sparingly, just once a minute maybe. What phone are you using to test this?
& 3. MediaRecorder is a wrapper around a native library. I assume that this library wants a concrete file to write to not a pipe. On a file-system level files & pipes look the same, but one can not have random access to a pipe (seeking).
精彩评论