Stream video from camera preview to wowza server in Android

Posted By : Chandan Wadhwa | 28-Jun-2016

Stream video from camera preview to Wowza server in Android

Wowza streaming server is a streaming media server developed by Wowza Media System. Wowza server is widely used for streaming on-demand video, audio, and rich Internet applications over IP networks to desktop, laptop, and tablet computers, mobile devices.

 

In this blog I am explaning you how to get camera preview and store raw data of preview frames to file sysem and send to wowza server uisng FFMPEG.

Getting camera preview frames and store in file.

 

Camera.PreviewCallback callback = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            //Do your processing here... Use the byte[] called "data"
                Log.d("Cam ","Cam preview callback");
            try {
                File f=new  File(Environment.getExternalStorageDirectory()+"/test.data");
                if(!f.exists()){
                    f.createNewFile();
                }
                OutputStream outStream = new FileOutputStream(f,true);

                Camera.Parameters parameters = camera.getParameters();
                imageFormat = parameters.getPreviewFormat();
                if (imageFormat == ImageFormat.NV21) {
                    Camera.Size previewSize = parameters.getPreviewSize();
                    frameWidth = previewSize.width;
                    frameHeight = previewSize.height;
                    Rect rect = new Rect(0, 0, frameWidth, frameHeight);
                    YuvImage img = new YuvImage(data, ImageFormat.NV21, frameWidth, frameHeight, null);
                    outStream.write(data);                    
                    outStream.flush();
                }
            }
            catch (FileNotFoundException iex){
                Log.d("File :-",iex.getMessage());

            }
            catch (IOException iexp){
                Log.d("File :-","IOExcp"+iexp.getMessage());
            }
        }
    };
 

FFMPEG command to send raw stream to wowza server

 

 

class ExecFFMPEGCommand extends AsyncTask{

        @Override
        protected Void doInBackground(Void... params) {
            ffmpegLoadBinary();
            String ffmpegCommand=" -f rawvideo -pix_fmt nv21 -s 640x480 -r 15 -i " + Environment.getExternalStorageDirectory()+"/test.data" +" rtmp://oodles:[email protected]:1935/live/mstream.flv ";
            Log.d("Cmd ",ffmpegCommand);
            executeFfmpegCommand(ffmpegCommand);
            return null;
        }
    }
 

 

Note :- Add 'com.github.hiteshsondhi88.libffmpeg:FFmpegAndroid:0.2.5' dependency for using FFMPEG.

Now call ExecFFMPEGCommand async task inside onPreviewFrame of Camera PreviewCallback  after writing daa to file.

 

Thanks.

About Author

Author Image
Chandan Wadhwa

Chandan is an Android Apps developer with good experience in building native Android applications.

Request for Proposal

Name is required

Comment is required

Sending message..