stream a MediaRecorder file while recording

ebaum picture ebaum · Jan 9, 2011 · Viewed 8.7k times · Source

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

  1. thread a FileInputStream that reads from the FileDescriptor of the video destination
  2. call MediaRecoder.setOutputFile on the FD of a 'sender' socket. this socket connects to a LocalSocketServer, whose destination is a 'receiver' socket.
  3. open a socket to my server and giving setOutputFile its FD

unfortunately both attempts have failed.

  1. Only registers 24 bytes whenever i call FileInputStream.available(), and the actual number of bytes only after i call Recorder.stop()
  2. 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(...) ...

  3. 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
  1. properly saves an uncorrupted video to sd
  2. works if I use setOutputFile(pathToFile) instead. the sockets also work when i run sender.getOutputStream().write(message.getBytes());

Answer

Peter Knego picture Peter Knego · Jan 9, 2011
  1. 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?

  2. & 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).