Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Fixed camera preview for Xiaomi devices #1780

Merged
merged 1 commit into from
Jan 4, 2022

Conversation

Axemasta
Copy link
Contributor

@Axemasta Axemasta commented Jan 2, 2022

Description of Bug

CameraView preview does not work correctly for front facing camera on Xiaomi Note 7/8 devices.

This appears to be a niche issue on Xiaomi devices of a suitable age (the newer ones don't appear to show the issue) where the front camera cannot be used to preview / capture media. Instead of displaying a preview, the media capture failed event is raised & the camera cannot be used.

Below is the XCT sample running on a Xiaomi Redmi Note 7 & a Samsung A21S. The Samsung has no issues displaying the front camera and the Xiaomi cannot display it.

Xiaomi with issue

Redmi.Note_7.2022-01-02.14-50-26.mp4

Samsung without issue

SM.A217F.2022-01-02.14-52-33.mp4

Upon debugging the source code with the affected device, I found that the ChooseOptimalSize method was responsible for returning a resolution that could not be displayed.

The following line on the affected device returns a size of 4224x3136 and when the camera is initialized the following errors are logged to the debug console:

[CameraDevice-JV-1] Stream configuration failed due to: endConfigure:520: Camera 1: Unsupported set of inputs/outputs provided
[CameraCaptureSession] Session 0: Failed to create capture session; configuration failed

After some reading up on the Camera2 api it looks like this lib is following the example set by Google. Stepping through the issue comes with the aspect ratio check which always seems to fail on this particular device. Maybe it is an Android flavour conflict?

To sort of the issue I've added a check to see if the fallback value used is a valid size (smaller than the max preview), which will be used instead of the invalid value. This code change shouldn't affect working devices since it only alters the fallback logic and my Xiaomi is now happily giving me both cameras!

Redmi.Note_7.2022-01-02.15-08-08.mp4

Issues Fixed

Behavioral Changes

CameraView should work on Xiaomi Redmi Note 7/8 devices 🥳

I removed the else clause on this line & made the failed to retrieve camera state return early, sorry guys I love to reduce nesting in methods 😉

PR Checklist

  • Has a linked Issue, and the Issue has been approved
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

Further investigation required but the xiaomi has no "optimal sizes" for preview and is attempting to set the preview size to 4224x3136, which as a guess I would say is a raw resolution not accounting the dpi
@bijington
Copy link
Contributor

@Axemasta thank you for this submission! The changes look good to me especially given they don't effect the existing checks for a matching size (I do wonder if we could improve upon that when this shifts to the Maui Community Toolkit but that is outside of the scope of this change).

It will be nice to see if the 2 people that you tagged on the issue are able to confirm the fix, I will aim to test on some other devices I have here tomorrow to double check all is fine, I don't possess a Xiaomi device though.

@Axemasta
Copy link
Contributor Author

Axemasta commented Jan 4, 2022

@bijington

To address your comments... in reverse order 😊:

It will be nice to see if the 2 people that you tagged on the issue are able to confirm the fix, I will aim to test on some other devices I have here tomorrow to double check all is fine, I don't possess a Xiaomi device though.

I'm fortunate to have access to an affected device, mine is a Xiaomi Note 7 running Android 9. I potentially have access to more affected devices through a fork which I will add the change & observe if devices can access the camera correctly.

I've tested on 3 devices:

  • Xiaomi Note 7
  • Samsung A21
  • Samsung A7 Tablet

Both the samsungs return valid surface mappings:
map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture)))

The Xiaomi does return mappings but the aspect ratios are off, causing the checking code to completely fallthrough. I can't confirm whether its a Xiaomi or an Android flavour issue but it seems the fallback at least works for affected devices.


@Axemasta thank you for this submission! The changes look good to me especially given they don't effect the existing checks for a matching size (I do wonder if we could improve upon that when this shifts to the Maui Community Toolkit but that is outside of the scope of this change).

When looking into this issue I did some research into how the Camera2 api should be implemented and it looks like the lastest google sample implements it in a different way. The current implementation matches the following:

Both of these use the ChooseOptimalSize method which appears to be a method google cooked up to select the smallest preview size possible.

The current google samples repo does not contain this method and whilst I've not completely wrapped my head around the differences between old & new, it appears to be using a method called getPreviewOutputSize which you can find implemented here.

Since changing the current implementation would affect more devices, perhaps it would be something that could be revisited in another pr to update the android renderers to use the latest & greatest from the google sample?

@bijington
Copy link
Contributor

@Axemasta thanks for the guidance on the differences between this implementation and googles docs. I will add a note on the MAUI Community Toolkit proposal so we certainly don't miss this in the new implementation (CommunityToolkit/Maui#106)

Given the rest appears to work and testing my devices here (Goole Pixel 3a, One Plus Nord N100 and some cheap Umidigi device) has been fine I suspect we might not worry about changing things in this repo.

@bijington bijington merged commit 000acd0 into xamarin:main Jan 4, 2022
@bijington
Copy link
Contributor

@Axemasta thanks again for this effort. It really is appreciated!

@Axemasta
Copy link
Contributor Author

Here is a Camera2 preview control that uses the preview builder to set the preview window. This is possibly the implementation we'd want to use for the MAUI version of the control ref.

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

Successfully merging this pull request may close these issues.

[Bug] Error: Failed to create captire sesstion
2 participants