Search code examples
androidopencvvideodecoding

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.


Solution

  • 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!