Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding video stream on Android from DJI drone

Hi I would like do some image processing with use OpenCv on video stream from DJI phantom 3 pro. Unfortunately for this thing is necessary making own decoding video. I know that it should be work with use Media Codec Android class but I dont know how to do. I saw some examples for decoding video from video file, but I wasn't able modify this code for my aim. Could somebody show some example or tutorial how to do? Thanks for help

mReceivedVideoDataCallBack = new DJIReceivedVideoDataCallBack(){
        @Override
        public void onResult(byte[] videoBuffer, int size){
            //recvData = true;
            //DJI methods for decoding              
            //mDjiGLSurfaceView.setDataToDecoder(videoBuffer, size);
        }
    };

This is method which is sending encoding stream from drone, and I need to send for decode the videoBuffer and then modify to Mat for OpenCV.

like image 898
Bubo Igonda Avatar asked Nov 04 '15 13:11

Bubo Igonda


1 Answers

Initialize the video callback like this

mReceivedVideoDataCallBack = new DJICamera.CameraReceivedVideoDataCallback() {
            @Override
            public void onResult(byte[] videoBuffer, int size) {
                if(mCodecManager != null){
                    // Send the raw H264 video data to codec manager for decoding
                    mCodecManager.sendDataToDecoder(videoBuffer, size);
                }else {
                    Log.e(TAG, "mCodecManager is null");
                 }
            }        
}

Make your activity implement TextureView.SurfaceTextureListener and for the TextureView mVideoSurface call this line after it is initialized:

mVideoSurface.setSurfaceTextureListener(this);

and then implement:

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Log.v(TAG, "onSurfaceTextureAvailable");
        DJICamera camera = FPVDemoApplication.getCameraInstance();
        if (mCodecManager == null && surface != null && camera != null) {
            //Normal init for the surface
            mCodecManager = new DJICodecManager(this, surface, width, height);
            Log.v(TAG, "Initialized CodecManager");
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        Log.v(TAG, "onSurfaceTextureSizeChanged");
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        Log.v(TAG, "onSurfaceTextureDestroyed");
        if (mCodecManager != null) {
            mCodecManager.cleanSurface();
            mCodecManager = null;
        }

        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        final Bitmap image = mVideoSurface.getBitmap();
        //Do whatever you want with the bitmap image here on every frame
    }

Hope this helps!

like image 130
Shazambom Avatar answered Oct 13 '22 15:10

Shazambom