8

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());
Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
  • I am trying to implement the same function. But I do not get your method. You set recorder.setOutputFile two times: one time to a file, the other to a socket. I think the recorder will work with the new fileID whenever you change, rather than with multiple fileIDs. – user1914692 May 29 '13 at 16:57
  • 1) and 2) are two separate examples –  May 29 '13 at 17:57
  • If they are separate, then how do you implement the two tasks at the same time? Actually if you do the two tasks separately, it should be a difficulty issue. For the streaming task, see Spydroid-ipcamera in code.google – user1914692 May 29 '13 at 21:14
  • @ebaum Can you give any updates about the task? I have the same issue. Maybe in '19 we have some fresher approach? Is it approach still the best or maybe you can suggest something else? – Ponomarenko Oleh Aug 07 '19 at 09:36

1 Answers1

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

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • I am using the... galaxy S. I didn't see this coming, thanks for the catch! If i were to set the receiver socket to output to a file, would 2 or 3 work? –  Jan 09 '11 at 10:24
  • Hello, I'm also trying to do the same thing... Did you by chance tried on an Huawei IDEOS phone or HTC G1? If no, can I have access to the APK and test it? Thanks. – Rob May 03 '11 at 06:18
  • It did work on G1 at that time (about 1.5 years ago), don't know about the other phone. The apk is on www.livecliq.net. I don't know about current status of the project. – Peter Knego May 03 '11 at 09:07
  • I had something similar that worked. We created a ParcelFileDescriptor that is wrapped around a sockets file descriptor; using a method ParcelFileDescriptor.fromSocket(socket). Unfortunately; this is no longer possible on lollipop. http://stackoverflow.com/questions/26990816/mediarecorder-issue-on-android-lollipop. I've been looking for alternative schemes for transfering information with FileStreams but I've running into a lot of difficulties. Does anybody have any solution/example for piping a MediaRecorder to java.net.Socket? – user1743524 Dec 23 '14 at 17:09
  • @user1743524 have you found a solution to this? – Roman Samoilenko Sep 12 '17 at 11:10