Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My device starts the camera in an inverted way #1576

Open
tiomurray opened this issue Sep 11, 2024 · 5 comments
Open

My device starts the camera in an inverted way #1576

tiomurray opened this issue Sep 11, 2024 · 5 comments

Comments

@tiomurray
Copy link

tiomurray commented Sep 11, 2024

I have a device which seems to start the camera in an inverted way: if I start the activity in portrait mode, this loads the camera with a landscape resolution, and if I start it in landscape mode, the camera starts with a portrait resolution, Is there any way to open the camera inverting the width and the height? I tried playing with rotation, width and height but didn't achieve what I want. Here is some pictures of the issue, my goal is to get the inverted result: the portrait preview in portrait mode and landscape preview in landscape mode. I could get this result but output video is stretched, as initial width and height of the signal of the camera is inverted as I said.

My current code is working fine with all devices except this one, so I guess is software issue or something related at the time of starting the camera, even before the surface is created.

WhatsApp Image 2024-09-11 at 20 34 20
WhatsApp Image 2024-09-11 at 20 34 38

@pedroSG94
Copy link
Owner

Hello,

You will need to handle the orientation manually to fix it. If you have autohandleOrientation, set it to false:
https://github.com/pedroSG94/RootEncoder/blob/master/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt#L77
Now, create a sensor class to handle the orientation manually:

  private val sensorRotationManager = SensorRotationManager(context, true, true, object: SensorRotationManager.RotationChangedListener {
    override fun onRotationChanged(rotation: Int, isPortrait: Boolean) {
      genericStream.getGlInterface().setCameraOrientation(rotation)
      genericStream.getGlInterface().setIsPortrait(!isPortrait)
    }
  })

In this case, I did !isPortrait in setIsPortrait because I think that the fail is related with that parameters because the orientation seems fine.
Remember start and stop the sensor in onResume and in onPause methods or similar depend of your lifecycle:

sensorRotationManager.start()
sensorRotationManager.stop()

@tiomurray
Copy link
Author

tiomurray commented Sep 11, 2024

Its not working with that code, I tried several options. What's happening is that the camera in portrait mode is opening like if it was recording with native camera in landscape mode, this is, 1920x1080 so at the time of converting that 1920x1080 in the portrait mode is getting stretched because portrait mode expects video data of 1080x1920.

I tried the setCameraOrientation and setIsPortrait with all the combinations but the result was the same in every case.

EDIT: setCameraOrientation() and setIsPortrait() work after call startPreview(mCameraView), anyway I can't find the way to open the camera in the right way, I think is an initial issue, before prepareVideo()

@pedroSG94
Copy link
Owner

pedroSG94 commented Sep 12, 2024

Hello,

prepareVideo could change orientation depend of the rotation value. Using 0 you have landscape and using 90 you have portrait but the preview should be correct in both cases. This only should affect to the stream result rotating the resolution depend of the value. For example:

  • Resolution 1920x1080 and rotation 0 -> stream resolution 1920x1080
  • Resolution 1920x1080 and rotation 90 -> stream resolution 1080x1920

Try add this:

genericStream.getGlInterface().forceOrientation(OrientationForced.LANDSCAPE)
genericStream.getGlInterface().forceOrientation(OrientationForced.PORTRAIT)
genericStream.getGlInterface().forceOrientation(OrientationForced.NONE) //this is used to disable this mode

Try using one of that for landscape or portrait. Test all combinations

@tiomurray
Copy link
Author

tiomurray commented Sep 12, 2024

Ok so I was able to draw in the surface the correct preview, but the issue is that the streaming output is inverted. a 1080x1920 preview is streaming in 1920x1080, and viceversa. I add the code and pictures of the output:

@Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        genericStream = new GenericStream(requireContext(), this);
        
        genericStream.getGlInterface().setAspectRatioMode(AspectRatioMode.Fill);

        Size cameraSize = getCameraSize();

        genericStream.prepareVideo(
                cameraSize.getWidth(),
                cameraSize.getHeight(),
                1200 * 1000,
                30,
                2,
                getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? 90 : 0
        );
        genericStream.prepareAudio(32000, true, 128 * 1000);

        genericStream.startPreview(mCameraView);

        boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
        genericStream.getGlInterface().setCameraOrientation(isPortrait ? 0 : 270);
        genericStream.getGlInterface().setIsPortrait(!isPortrait);
    }

Pictures:

Portrait preview:
1

Portrait streaming result:
2

Landscape preview:
3

Landscape streaming result:
4

All these results were obtained using width=1920, height=1080.

If I use width=1080,height=1920, all is the same except portrait streaming result, which is this:

2

@pedroSG94
Copy link
Owner

pedroSG94 commented Sep 12, 2024

Was the streaming result working fine before that changes?

Can you tell me the values of SensorManager in all orientations?
I want know your values to create an enviroment to test it and try a fix for it. Use the code of my previous comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants