package com.dji.sdk.sample.internal.view;
import android.app.Service;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.widget.FrameLayout;
import com.dji.sdk.sample.R;
import com.dji.sdk.sample.internal.controller.DJISampleApplication;
import dji.sdk.base.BaseProduct;
import dji.sdk.camera.VideoFeeder;
import dji.sdk.codec.DJICodecManager;
/**
* This class is designed for showing the fpv video feed from the camera or Lightbridge 2.
*/
public class BaseFpvView extends FrameLayout implements TextureView.SurfaceTextureListener {
private VideoFeeder.VideoDataCallback receivedVideoDataCallback = null;
private DJICodecManager codecManager = null;
public BaseFpvView(Context context, AttributeSet attrs) {
super(context, attrs);
initUI();
}
private void initUI() {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Service.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.view_fpv_display, this, true);
Log.v("TAG", "Start to test");
TextureView mVideoSurface = (TextureView) findViewById(R.id.texture_video_previewer_surface);
if (null != mVideoSurface) {
mVideoSurface.setSurfaceTextureListener(this);
// This callback is for
receivedVideoDataCallback = new VideoFeeder.VideoDataCallback() {
@Override
public void onReceive(byte[] bytes, int size) {
if (null != codecManager) {
codecManager.sendDataToDecoder(bytes, size);
}
}
};
}
initSDKCallback();
}
private void initSDKCallback() {
try {
BaseProduct mProduct = DJISampleApplication.getProductInstance();
if (VideoFeeder.getInstance().getVideoFeeds() != null
&& VideoFeeder.getInstance().getVideoFeeds().size() > 0) {
VideoFeeder.getInstance().getVideoFeeds().get(0).setCallback(receivedVideoDataCallback);
}
} catch (Exception exception) {
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (codecManager == null) {
codecManager = new DJICodecManager(getContext(), surface, width, height);
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (codecManager != null) {
codecManager.cleanSurface();
codecManager = null;
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}