-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[camera_android] prevent startImageStream OOM error when main thread hangs (flutter#166533) #8998
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
Open
kwikwag
wants to merge
9
commits into
flutter:main
Choose a base branch
from
kwikwag:fix/issue_166533
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 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 dd9af3f
Merge branch 'main' into fix/issue_166533
kwikwag 9dfb418
fix: move getImage to ImageStreamReaderTestUtils
kwikwag 092c441
fix: separate exception handling from numImagesInTransit
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
55 changes: 55 additions & 0 deletions
55
...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,55 @@ | ||
// 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 { | ||
public static Image getImage(int imageWidth, int imageHeight, int padding, int imageFormat) { | ||
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.
Shouldnt this also have
--numImagesInTransit
otherwise if we had more than 3 images error then we would no longer update any frames.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.
@reidbaker Thanks for these questions. I'll address the issues one-by-one
As opposed to how I initially phrased the issue, this will not only happen when the debugger is paused, but any time the main loop is lagging. I see no benefit to the 'consecutive' method:
EventSink.success()
call can raise an exception. If it can, it would perhaps be wish to wrap the call with atry..finally
. However, I would not move the decrement before thesuccess()
call, as decrement should happen when the image memory can readily be released, otherwise a queue build-up might still occur.post()
and eventually crash the app with an out-of-memory error. Ensuring thatpost()
arrives and completes is exactly the queue capability you are talking about, as far as I understand.Regarding not updating frames anymore - the only scenario I see that you will no longer update frames in, is when the handler stops handling the
Runnable
s passed topost()
. If that is the case, it means the main thread is paused or hanging, in which case it should be OK to not update the frames until it releases and theRunnable
gets invoked. I see no risk of deadlock, as AFAIK there is no way for the main thread to wait on whatever callsonImageAvailable()
.How long will it be before @camsim99 comes back?
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.
My two cents:
numImagesInTransit
back to 0 when a frame is delivered. My understanding is that if we do, then we would no longer only be waiting on 3 images to accumulate to start closing images, but instead, would be waiting on 3 then 6 then potentially 9 and so on and so forth.IllegalStateException
is thrown streaming an image), we should also decrementnumImagesInTransit
because we have essentially "handled" that image.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.
My worry is that over time either changes to the behavior of android or the camera code we relyon will cause us to not decrement at the same rate we increment and cause the camera to break for users.
The weak reference suggestion would mitigate that I think.
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.
@kwikwag Thank you for the clear explanation on the weak reference solution! Considering the concern of this PR changing behavior, I am for pursuing that solution instead of this one. What do you think?
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.
@kwikwag We're leaning towards the weak reference solution based on this discussion. What do you think?