1. First we need a camera 2 object to record our video

-> Here is the code myvideo.java <-  (Its saved as a .txt just change the suffix if you want to cut and paste but I don’t recommend doing that)


Androids camera 2 api is a beast itself. Using the official example will take most of the work out. Here is the example I copied.

The above example is buggy (at time of writing) even though its provided by google.

Heres the thing, when you record a video the camera starts sending the data to a file right away. Only after the recording is complete does the api write the data needed to play that file in the form of file headers . We need to do 2 things with this api

  1. We need to record short videos and extract that header data before we even start streaming.
  2. When we are streaming we have to  direct androids media recorder to send the file instead of writing the file.

Here is my code all folded up


The top fold is boilerplate stuff not super important


The third fold are the normal camera methods called to operate the camera. Lets focus in on the ones that are tough and critical to you success

The android camera method setUpMediaRecorder needs to tell android where to send you actual video data. Below you will see that I have created a bool flag that either saves the data or calls a method.

if (collectSDP){
Log.d(TAG, "setUpMediaRecorder: collect");
Log.d(TAG, "setUpMediaRecorder: dontcollect");

In order to get some critical data we need later we need to save a small video file. In the method getSDP you will see I go through the motions of recording a short video.  The video is then parsed with the SDPMaker class which is the next part of this tutorial.

You will also see that option 2 of the above code is getFD. This returns a file descriptor which allows you to connect an outputstream -> inputstream. This tricks your camera api into writing into a buffer which is immediately read by another class which will package and send the data to wherever you are sending it. Notice all the abandon code.


private FileDescriptor getStreamFd() {

ParcelFileDescriptor[] pipe = null;

try {
pipe = ParcelFileDescriptor.createPipe();

new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]),
new Socket(), mRobotPoint).start();

transferH264 = new TransferH264(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]),
new Socket(), mRobotPoint, this, ssrc);


transH264 = new TransH264(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]),
new Socket(), mRobotPoint, this, ssrc);


new TransferH264(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]),
new Socket(), mRobotPoint, this).start();

} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Exception opening pipe", e);

return (pipe[1].getFileDescriptor());


This is a multipart post…keep going


Leave a comment

Your email address will not be published. Required fields are marked *