-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[camera_android] Use WeakReference to prevent startImageStream OOM error when main thread hangs (flutter#166533) #9571
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3a5ef0f
[camera_android] fix: prevent startImageStream OOM error when main th…
kwikwag 71e9414
update pubspec.yaml and CHANGELOG.md
kwikwag f9db089
Merge branch 'main' into fix/issue_166533
kwikwag ee845f4
add test
kwikwag 97e98f2
fix formatting
kwikwag bf15d0b
fix: remove log message on each frame
kwikwag d498fb7
[camera_android] fix: prevent startImageStream OOM error when main th…
kwikwag cc61ef8
fix: move getImage to ImageStreamReaderTestUtils
kwikwag 229ccd4
fix: separate exception handling from numImagesInTransit
kwikwag cad3010
fix: formatting
kwikwag 6ed20c9
fix: whoops, forgot to remove some constants
kwikwag 79deae1
fix: some comments
kwikwag 511e27f
Merge branch 'main' into fix/issue_166533_alt2
kwikwag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...oid/android/src/test/java/io/flutter/plugins/camera/media/ImageStreamReaderTestUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camera.media; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.media.Image; | ||
import java.nio.ByteBuffer; | ||
|
||
public class ImageStreamReaderTestUtils { | ||
/** | ||
* Creates a mock {@link android.media.Image} object for use in tests, simulating the specified | ||
* dimensions, padding, and image format. | ||
*/ | ||
public static Image getImage(int imageWidth, int imageHeight, int padding, int imageFormat) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add java doc for this method |
||
int rowStride = imageWidth + padding; | ||
|
||
int ySize = (rowStride * imageHeight) - padding; | ||
int uSize = (ySize / 2) - (padding / 2); | ||
int vSize = uSize; | ||
|
||
// Mock YUV image | ||
Image mockImage = mock(Image.class); | ||
when(mockImage.getWidth()).thenReturn(imageWidth); | ||
when(mockImage.getHeight()).thenReturn(imageHeight); | ||
when(mockImage.getFormat()).thenReturn(imageFormat); | ||
|
||
// Mock planes. YUV images have 3 planes (Y, U, V). | ||
Image.Plane planeY = mock(Image.Plane.class); | ||
Image.Plane planeU = mock(Image.Plane.class); | ||
Image.Plane planeV = mock(Image.Plane.class); | ||
|
||
// Y plane is width*height | ||
// Row stride is generally == width but when there is padding it will | ||
// be larger. | ||
// Here we are adding 256 padding. | ||
when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(ySize)); | ||
when(planeY.getRowStride()).thenReturn(rowStride); | ||
when(planeY.getPixelStride()).thenReturn(1); | ||
|
||
// U and V planes are always the same sizes/values. | ||
// https://developer.android.com/reference/android/graphics/ImageFormat#YUV_420_888 | ||
when(planeU.getBuffer()).thenReturn(ByteBuffer.allocate(uSize)); | ||
when(planeV.getBuffer()).thenReturn(ByteBuffer.allocate(vSize)); | ||
when(planeU.getRowStride()).thenReturn(rowStride); | ||
when(planeV.getRowStride()).thenReturn(rowStride); | ||
when(planeU.getPixelStride()).thenReturn(2); | ||
when(planeV.getPixelStride()).thenReturn(2); | ||
|
||
// Add planes to image | ||
Image.Plane[] planes = {planeY, planeU, planeV}; | ||
when(mockImage.getPlanes()).thenReturn(planes); | ||
|
||
return mockImage; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is correct but can you add a comment here about why garbage collected image buffer is neither and error or success?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope the comment I added is sufficient?