9

I want to encrypt video on the fly that android camera captures. So I need to tell android MediaRecorder to write it video stream to my CipherOutputStream. The problem is MediaRecorder.setOutputFile() method accepts only FileDescriptor and there is no way to get encrypting file descriptor from CipherOutputStream.

So my question is how can I either emulate FileDescriptor to receive data writes and do encryption manually or somehow convince MediaRecorder to stream video into CipherOutputStream.

Alex Amiryan
  • 1,374
  • 1
  • 18
  • 30
  • 1
    I don't think this is possible: http://stackoverflow.com/questions/9257364/modifying-fileinputstream-for-mediaplayer-setdatasource – Matt Ball Jul 24 '12 at 20:06

1 Answers1

5

You can use LocalServerSocket and LocalSocket to implement what you want.

LocalServerSocket which provides the FileDescriptor via LocalServerSocket.getFileDescriptor()

  1. Initiate a LocalServerSocket.
  2. Initiate a LocalSocket object and connect to LocalServerSocket.
  3. Invoke LocalServerSocket.accept() to accept connection from LocalSocket.
  4. When the connection established, you can get FileDescriptor from LocalServerSocket.
  5. Every bytes the Camera writes to LocalServerSocket could be retrieved from LocalSocket.getInputStream(), you can use a for-loop to get byte stream and write to CipherOutputStream.

Remember to put all steps into a new Thread.

I used those APIs to create on-the-fly stream processor with the Camera as stream source.

I hope this helps.

yrulee
  • 66
  • 1
  • 2
  • Is there a media format this actually works with? MP4 for example, MediaRecorder expects random access to the FileDescriptor - it wants to write the moov atom at the beginning of the file, which 1) doesn't work with the socket derived FileDescriptor, and 2) wouldn't work with the CipherOutputStream either. – Mark Nov 17 '14 at 21:15
  • The MP4 is not working because of the random position of moov/mdat. I tested the streaming file format such as the M2TS is working, and I think other streaming file format should be working as well. – yrulee Apr 17 '15 at 14:57