Send Camera Preview to PresentationFragment


#1

Hi mmurphy:

I have a question, base on cwac-presentation I use PresenationFragemenmt to created the second display and add a TextureView, but my Camera must be working in Activity. If I want show Camera Preview on TextureView which in second display, how should I do? Thank you so much.


#2

Sorry, but I do not know what you mean by this. A SurfaceView or TextureView can be displayed by a fragment, and the camera preview can be directed to that SurfaceView or TextureView.

If I want show Camera Preview on TextureView which in second display, how should I do?

I have never done that. But, I’d start by showing it the same way that I would show anything else on the second display: use a PresentationFragment.


#3

Hi mmurphy:

Thank you for your fast reply!

“but my Camera must be working in Activity” which mean I call Camera2 API in Activity.

I already use PresentationFragment to built my second display, the second display will show Mirror and Camera Preview, and there is a MirroringLayout working in Activity use to mirroring some UI contents.I knew If I create TextureView in MirroringLayout I just need call getTextureView() in Activity.

But because I want to show Camera preview and UI mirror in PresentationFragment, I do not have idea how to send preview from Activity to PresentationFragment :frowning:


#4

You cannot mirror a SurfaceView or TextureView with my mirroring code.

I do not have idea how to send preview from Activity to PresentationFragment

Do not have a preview in the activity. Have a preview in the PresentationFragment, by putting the SurfaceView or TextureView there, and having the Camera2 API send its preview frames there.

If you want the preview in both the activity and the PresentationFragment… it might be possible to have the Camera2 API send preview frames to two surfaces, though that may get tricky if the aspect ratios are not the same.

If you want the preview in either the activity or the PresentationFragment, but not both at once, then have a surface in each, and tell the Camera2 API which to use, when you want to switch from one surface to the other.


#5

There is textureView in my PresentationFragment and I also created textureview.getSurfaceTexture() to get SurfaceTexture. In my Activity I need use SurfaceTexture to get preview base on some other SDK issues. So how to get this SurfaceTexture in Activity, I got null object reference… :frowning:


#6
  1. Add a method to your fragment that supplies the SurfaceTexture, such as getSurfaceTexture()

  2. Call that method from your activity some time after that SurfaceTexture should exist, to retrieve the SurfaceTexture

Alternatively, you could:

  • Have the fragment push the SurfaceTexture to the activity, by calling a method on the activity once the SurfaceTexture is created
  • Use some reactive solution (LiveData, RxJava, etc.) to have the fragment supply the SurfaceTexture to the activity, which can then react when that SurfaceTexture is delivered

#7

Thank you mmurphy:

  1. In my PresentationFragment, I created a TextureView blinding TextureView UI after I inflate fragment layout in OnCreateView(). Then I created public TextureView getTextureView() { return textureView}

public class MyFragment extends PresentationFragment{

private Mirror mirror;
private TextureView textureView;
public static MyFragment newInstance(Context context, Display display) {
	MyFragment  myFragment  = new MyFragment();
	myFragment.setDisplay(context, display);
	return myFragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater layoutInflater, @Nullable ViewGroup contatiner, Bundle savedInstanceState) {
	View rootView = layoutInflater.inflate(R.layout.external_display, contatiner, false);
	mirror = rootView.findViewById(R.id.mirror_target);
	textureView = rootView.findViewById(R.id.camera_preview);
	MirroringLayout mirroringLayout = ((MyFragment) getActivity()).getMirroringLayout();
	mirroringLayout.setMirror(mirror);
	return rootView;
}

public TextureView getTextureView() { return textureView; }
}
  1. In Activity, I am trying make the call getTextureView() in OnResume().

     private TextureView textureView;
     private SurfaceTexture surfaceTexture;
     private MyFragment myFragment;
     @Override
     protected void onResume() {
         super.onResume();
         presentationhelper.onResume();
         textureView = myFragment.getTextureview();
         if (textureView.isAvailable()){ surfaceTexture = textureView.getSurfaceTexture(); }
     }
    

I will get error for “myFragment.getTextureview() null object reference”. I am stack…

Thank you!


#8

I found one thing such as you said I need use setTextureViewListener() to know SurfaceTextureAvailable in my PresentationFragment. I really want use this Listener in my Activity, but I found I can not call textureView.isAvailable() or TextureView$SurfaceTextureListener which will be show null object reference. So how to design this program make Activity can know PresentationFragment side TextureView is available then call getTextureView();


#9

Presumably onCreateView() on the fragment has not been called yet by the time onResume() is called. You need to wait to try to use the TextureView until after it exists.


#10

Thank you so much for your advise, which help me find solution.
I create function fragmentOnCreateViewFinish() in Activity which use to make the call getTextureView from fragment. In fragment onStart() I use((Activity)getActivity()).fragmentOnCreateViewFinish().

Question: why onCreateView has not been called in real-time?


#11

Sorry, but I have not looked at the source of FragmentActivity or Activity to try to determine exactly when it calls onCreateView() on fragments.


#12

I think the problem is PresentationHelper trigger PresentationFragment begin work on Activity onResume().


#13

Oh, right! Sorry, I had forgotten that this was a PresentationFragment. Yes, it will not create its views until you start to show the PresentationFragment.


#14

So do we need consider to add a listener to support Activity to know PresentationFragment lifecycle? Looks like FragmentManager.FragmentLifecycleCallbacks requires API 26 that will make cwac-presentation SDK not friendly.


#15

I have not needed that in the past, but I have had simpler PresentationFragment implementations.