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
unfortunately both attempts have failed.
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(1000);
}
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
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).