ChillingVan / AndroidInstantVideo

展现Android硬编码下的视频数据流动,可以对视频做处理,例如加滤镜,加水印等,做直播推流(用RTMP)。 Show the stream of Android video hardware encode, including video processing and video publishing by RTMP.
Apache License 2.0
718 stars 196 forks source link

Need help: Using surface created in SurfaceTextureCreatedListener to display screen using MediaProjection #25

Closed inventionsbyhamid closed 4 years ago

inventionsbyhamid commented 4 years ago

We can display screen using the following sample: https://github.com/android/media-samples/tree/master/ScreenCapture

MediaProjection requires to give a surface to draw screen frames. I am modifying the TestCameraPublisherActivity to also include the screen display on the textureview. When I pass the surface obtained in the SurfaceTextureCreatedListener to media projection, nothing is drawn when i use canvasGL.drawSurfaceTexture, I also set the onFrameAvailable on the SurfaceTexture and can see that it is being called for screen display.

Any help as to why drawing the screen display surface texture does not show on the preview?

ChillingVan commented 4 years ago

Could you show your code as an example?

ChillingVan commented 4 years ago

And is requestRender called?

inventionsbyhamid commented 4 years ago

Here is the link https://github.com/inventionsbyhamid/AndroidInstantVideo/tree/screen_display I have created a separate activity "ScreenPublisherActivity" to just draw the screen preview. Not able to figure out why nothing gets drawn on the texture view.

ChillingVan commented 4 years ago

I check and the onFrameAvailable is not called, I am wondering why

ChillingVan commented 4 years ago

It seems that the surface in ImageReader works fine. Need to look into the source code of it.

ChillingVan commented 4 years ago

I try to use

                surfaceTexture.setDefaultBufferSize(width, height);

And it works. Add this line before set onFrameAvailable listener:

        displayTextureView.setSurfaceTextureCreatedListener(new GLMultiTexProducerView.SurfaceTextureCreatedListener() {
            @Override
            public void onCreated(List<GLTexture> producedTextureList) {
                GLTexture texture = producedTextureList.get(0);
                surfaceTexture = texture.getSurfaceTexture();
                RawTexture outsideTexture = texture.getRawTexture();
                width = outsideTexture.getWidth();
                height = outsideTexture.getHeight();
                surfaceTexture.setDefaultBufferSize(width, height);
                surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
                    @Override
                    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
                        Log.i(TAG, "onFrameAvailable");
                        displayTextureView.requestRender();
                    }
                }, mHandler);
                mSurface = new Surface(surfaceTexture);
                if(mVirtualDisplay != null) {
                    mVirtualDisplay.setSurface(mSurface);
                }
            }
        });
inventionsbyhamid commented 4 years ago

It works fine. Thanks for the help!