Stream video from camera preview to wowza server in Android
Posted By : Chandan Wadhwa | 28-Jun-2016
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Wadhwa
Chandan is an Android Apps developer with good experience in building native Android applications.