Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
37be5c5
Fix padding issue
acoutts Jan 12, 2023
45d2c58
New approach that is more efficient
acoutts Jan 13, 2023
d2492c8
Refactored image reader to enable testing
acoutts Jan 13, 2023
bd55965
Added tests for image stream reader
acoutts Jan 17, 2023
88079a9
Added tests for buffer trimming
acoutts Jan 17, 2023
fee7979
Formatter
acoutts Jan 17, 2023
f0f0640
Bump pubspec version
acoutts Jan 17, 2023
df99b02
Optimize imports
acoutts Jan 17, 2023
90b2993
Cleanup
acoutts Jan 17, 2023
f5c7f8f
Imports cleanup
acoutts Jan 17, 2023
fd858eb
Imports
acoutts Jan 17, 2023
b1f292e
Imports
acoutts Jan 17, 2023
e6e1f58
Update Camera.java
acoutts Jan 17, 2023
800878f
Update CameraRegionUtils.java
acoutts Jan 17, 2023
56995bb
Update ImageStreamReader.java
acoutts Jan 17, 2023
5efd3e6
Format imports on tests
acoutts Jan 17, 2023
6667183
Formatters
acoutts Jan 18, 2023
b7dec7f
Licenses
acoutts Jan 18, 2023
0b8d812
Merge remote-tracking branch 'upstream/main' into fix-handle-padding
acoutts Jan 18, 2023
ce2a3b9
Added support for NV21 streaming on android as a new image format
acoutts Jan 20, 2023
404a954
Remove unused parameter
acoutts Jan 20, 2023
3ec602e
Add NV21 type conversion
acoutts Jan 20, 2023
a010585
Add legacy code path for nv21 to camera package
acoutts Jan 22, 2023
8bd8710
Clean up log messages
acoutts Feb 17, 2023
7b7708b
Formatters
acoutts Feb 17, 2023
d18a997
Dependency overrides as per contributing guidelines
acoutts Feb 17, 2023
37fa30d
Fix tests for image stream reader
acoutts Feb 17, 2023
c4c1719
Test cleanup
acoutts Feb 17, 2023
bf18e06
Added tests for removing padding
acoutts Feb 17, 2023
628a16a
Formatter
acoutts Feb 17, 2023
0ddc5d4
Merge remote-tracking branch 'upstream/main' into fix-handle-padding
acoutts Feb 17, 2023
651d202
Update CHANGELOG.md
acoutts Feb 17, 2023
cbfb447
Changelogs
acoutts Feb 17, 2023
1d1ecbf
Update CHANGELOG.md
acoutts Feb 17, 2023
804ddc7
Update ImageStreamReader.java
acoutts Feb 17, 2023
5a10847
Comments
acoutts Feb 17, 2023
198c55d
comments
acoutts Feb 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests for image stream reader
  • Loading branch information
acoutts committed Feb 17, 2023
commit 37fa30daca4aa79cf09e1eea77e122af827378f0
7 changes: 7 additions & 0 deletions packages/camera/camera/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ dev_dependencies:

flutter:
uses-material-design: true

# FOR TESTING ONLY. DO NOT MERGE.
dependency_overrides:
camera_android:
path: ../../../camera/camera_android
camera_platform_interface:
path: ../../../camera/camera_platform_interface
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,16 @@ public class ImageStreamReader {
private final ImageReader imageReader;
private final ImageStreamReaderUtils imageStreamReaderUtils;

// For NV21, we will be streaming YUV420 but converting the frames
// before sending back to dart.
public static ImageReader createImageReader(
int width, int height, int imageFormat, int maxImages) {
final int _imageFormat =
imageFormat == ImageFormat.NV21 ? ImageFormat.YUV_420_888 : imageFormat;

return ImageReader.newInstance(width, height, _imageFormat, maxImages);
}

/**
* Creates a new instance of the {@link ImageStreamReader}.
*
* @param imageReader is the image reader that will receive frames
* @param imageStreamReaderUtils is an instance of {@link ImageStreamReaderUtils}
*/
@VisibleForTesting
public ImageStreamReader(ImageReader imageReader, ImageStreamReaderUtils imageStreamReaderUtils) {
public ImageStreamReader(ImageReader imageReader, int dartImageFormat, ImageStreamReaderUtils imageStreamReaderUtils) {
this.imageReader = imageReader;
this.dartImageFormat = imageReader.getImageFormat();
this.dartImageFormat = dartImageFormat;
this.imageStreamReaderUtils = imageStreamReaderUtils;
}

Expand All @@ -76,10 +66,11 @@ public ImageStreamReader(int width, int height, int imageFormat, int maxImages)
* except when dart is requesting NV21. In that case we stream YUV420 and process it into NV21
* before sending the frames over.
*
* @param dartImageFormat
* @return
* @param dartImageFormat is the image format dart is requesting.
* @return the image format that should be streamed from the camera.
*/
private static int computeStreamImageFormat(int dartImageFormat) {
@VisibleForTesting
public static int computeStreamImageFormat(int dartImageFormat) {
if (dartImageFormat == ImageFormat.NV21) {
return ImageFormat.YUV_420_888;
} else {
Expand All @@ -92,8 +83,6 @@ private static int computeStreamImageFormat(int dartImageFormat) {
* frame to Dart.
*
* @param image is the image which needs processed as an {@link Image}
* @param imageFormat is the image format from the image reader as an int, a valid {@link
* ImageFormat}
* @param captureProps is the capture props from the camera class as {@link
* CameraCaptureProperties}
* @param imageStreamSink is the image stream sink from dart as a dart {@link
Expand Down Expand Up @@ -140,7 +129,7 @@ public void onImageAvailable(
}
}

public List<Map<String, Object>> parsePlanesForYuvOrJpeg(Image image) {
public List<Map<String, Object>> parsePlanesForYuvOrJpeg(@NonNull Image image) {
List<Map<String, Object>> planes = new ArrayList<>();

// For YUV420 and JPEG, just send the data as-is for each plane.
Expand All @@ -160,7 +149,7 @@ public List<Map<String, Object>> parsePlanesForYuvOrJpeg(Image image) {
return planes;
}

public List<Map<String, Object>> parsePlanesForNv21(Image image) {
public List<Map<String, Object>> parsePlanesForNv21(@NonNull Image image) {
List<Map<String, Object>> planes = new ArrayList<>();

// We will convert the YUV data to NV21 which is a single-plane image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.flutter.plugins.camera.media;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
Expand All @@ -24,43 +25,105 @@

@RunWith(RobolectricTestRunner.class)
public class ImageStreamReaderTest {
private ImageStreamReader imageStreamReader;
private ImageStreamReaderUtils mockImageStreamReaderUtils;
/**
* If we request YUV42 we should stream in YUV420.
*/
@Test
public void computeStreamImageFormat_computesCorrectStreamFormatYuv() {
int requestedStreamFormat = ImageFormat.YUV_420_888;
int result = ImageStreamReader.computeStreamImageFormat(requestedStreamFormat);
assertEquals(result, ImageFormat.YUV_420_888);
}

/**
* When we want to stream in NV21, we should still request YUV420 from the
* camera because we will convert it to NV21 before sending it to dart.
*/
@Test
public void computeStreamImageFormat_computesCorrectStreamFormatNv21() {
int requestedStreamFormat = ImageFormat.NV21;
int result = ImageStreamReader.computeStreamImageFormat(requestedStreamFormat);
assertEquals(result, ImageFormat.YUV_420_888);
}

/**
* If we are requesting NV21, then the planes should be processed and
* converted to NV21 before being sent to dart. We make sure yuv420ThreePlanesToNV21
* is called when we are requesting
*/
@Test
public void onImageAvailable_parsesPlanesForNv21() {
// Dart wants NV21 frames
int dartImageFormat = ImageFormat.NV21;

@Before
public void setUp() {
ImageReader mockImageReader = mock(ImageReader.class);
ImageStreamReaderUtils mockImageStreamReaderUtils = mock(ImageStreamReaderUtils.class);
ImageStreamReader imageStreamReader =
new ImageStreamReader(mockImageReader, dartImageFormat, mockImageStreamReaderUtils);

this.mockImageStreamReaderUtils = mockImageStreamReaderUtils;
this.imageStreamReader =
new ImageStreamReader(mockImageReader, this.mockImageStreamReaderUtils);
}
ByteBuffer mockBytes = ByteBuffer.allocate(0);
when(mockImageStreamReaderUtils.yuv420ThreePlanesToNV21(any(), anyInt(), anyInt())).thenReturn(mockBytes);

@Test
public void onImageAvailable_doesNotTryToFixPaddingOnNonYuvImage() {
// Mock JPEG image
int imageFormat = ImageFormat.JPEG;
// The image format as streamed from the camera
int imageFormat = ImageFormat.YUV_420_888;

// Mock YUV image
Image mockImage = mock(Image.class);
when(mockImage.getWidth()).thenReturn(1280);
when(mockImage.getHeight()).thenReturn(720);
when(mockImage.getFormat()).thenReturn(imageFormat);

// Mock plane. JPEG images have only one plane
Image.Plane plane0 = mock(Image.Plane.class);
when(plane0.getBuffer()).thenReturn(ByteBuffer.allocate(497950));
when(plane0.getRowStride()).thenReturn(0);
when(plane0.getPixelStride()).thenReturn(0);
Image.Plane[] planes = {plane0};
// 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. The numbers in this example are from a Vivo V2135 on 'high'
// setting (1280x720).
when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(1105664));
when(planeY.getRowStride()).thenReturn(1536);
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(552703));
when(planeV.getBuffer()).thenReturn(ByteBuffer.allocate(552703));
when(planeU.getRowStride()).thenReturn(1536);
when(planeV.getRowStride()).thenReturn(1536);
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);

CameraCaptureProperties mockCaptureProps = mock(CameraCaptureProperties.class);
EventChannel.EventSink mockEventSink = mock(EventChannel.EventSink.class);
imageStreamReader.onImageAvailable(mockImage, imageFormat, mockCaptureProps, mockEventSink);
imageStreamReader.onImageAvailable(mockImage, mockCaptureProps, mockEventSink);

verify(mockImageStreamReaderUtils, never()).removePlaneBufferPadding(any(), anyInt(), anyInt());
// Make sure we processed the frame with parsePlanesForNv21
verify(mockImageStreamReaderUtils).yuv420ThreePlanesToNV21(any(), anyInt(), anyInt());
}

/**
* If we are requesting YUV420, then we should send the 3-plane image as it is.
*/
@Test
public void onImageAvailable_shouldTryToFixPaddingOnYuvImageWithExtraPadding() {
public void onImageAvailable_parsesPlanesForYuv420() {
// Dart wants NV21 frames
int dartImageFormat = ImageFormat.YUV_420_888;

ImageReader mockImageReader = mock(ImageReader.class);
ImageStreamReaderUtils mockImageStreamReaderUtils = mock(ImageStreamReaderUtils.class);
ImageStreamReader imageStreamReader =
new ImageStreamReader(mockImageReader, dartImageFormat, mockImageStreamReaderUtils);

ByteBuffer mockBytes = ByteBuffer.allocate(0);
when(mockImageStreamReaderUtils.yuv420ThreePlanesToNV21(any(), anyInt(), anyInt())).thenReturn(mockBytes);

// The image format as streamed from the camera
int imageFormat = ImageFormat.YUV_420_888;

// Mock YUV image
Expand Down Expand Up @@ -97,8 +160,9 @@ public void onImageAvailable_shouldTryToFixPaddingOnYuvImageWithExtraPadding() {

CameraCaptureProperties mockCaptureProps = mock(CameraCaptureProperties.class);
EventChannel.EventSink mockEventSink = mock(EventChannel.EventSink.class);
imageStreamReader.onImageAvailable(mockImage, imageFormat, mockCaptureProps, mockEventSink);
imageStreamReader.onImageAvailable(mockImage, mockCaptureProps, mockEventSink);

verify(mockImageStreamReaderUtils).removePlaneBufferPadding(any(), anyInt(), anyInt());
// Make sure we processed the frame with parsePlanesForYuvOrJpeg
verify(mockImageStreamReaderUtils, never()).yuv420ThreePlanesToNV21(any(), anyInt(), anyInt());
}
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
// 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;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ImageStreamReaderUtilsTest {
private ImageStreamReaderUtils imageStreamReaderUtils;

@Before
public void setUp() {
this.imageStreamReaderUtils = new ImageStreamReaderUtils();
}

@Test(expected = IllegalArgumentException.class)
public void removePlaneBufferPadding_throwsIfPixelStrideInvalid() {
Image.Plane planeU = mock(Image.Plane.class);
when(planeU.getBuffer()).thenReturn(ByteBuffer.allocate(552703));
when(planeU.getRowStride()).thenReturn(1536);
when(planeU.getPixelStride()).thenReturn(2);

imageStreamReaderUtils.removePlaneBufferPadding(planeU, 1280 / 2, 720 / 2);
}

// Values here are taken from a Vivo V2135 which adds padding in 1280x720 mode
@Test
public void removePlaneBufferPadding_removesPaddingCorrectly() {
Image.Plane planeY = mock(Image.Plane.class);
when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(1105664));
when(planeY.getRowStride()).thenReturn(1536);
when(planeY.getPixelStride()).thenReturn(1);

byte[] fixed = imageStreamReaderUtils.removePlaneBufferPadding(planeY, 1280, 720);

// After trimming the padding, the buffer size should match the image size
Assert.assertEquals(fixed.length, 1280 * 720);
Assert.assertNotEquals(fixed.length, planeY.getBuffer().limit());
Assert.assertFalse(Arrays.equals(fixed, planeY.getBuffer().array()));
}

// Values here are taken from a Pixel 6 which does not add any padding.
// In the event we pass a buffer with no padding, the returned buffer
// should be identical to the source one because nothing is trimmed.
@Test
public void removePlaneBufferPadding_doesNothingIfThereIsNoPadding() {
Image.Plane planeY = mock(Image.Plane.class);
when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(921600));
when(planeY.getRowStride()).thenReturn(1280);
when(planeY.getPixelStride()).thenReturn(1);

byte[] fixed = imageStreamReaderUtils.removePlaneBufferPadding(planeY, 1280, 720);

// After trimming the padding, the buffer size should match the image size
Assert.assertEquals(fixed.length, 1280 * 720);
Assert.assertEquals(fixed.length, planeY.getBuffer().limit());
Assert.assertArrayEquals(fixed, planeY.getBuffer().array());
}
}
//// 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;
//import java.util.Arrays;
//import org.junit.Assert;
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.robolectric.RobolectricTestRunner;
//
//@RunWith(RobolectricTestRunner.class)
//public class ImageStreamReaderUtilsTest {
// private ImageStreamReaderUtils imageStreamReaderUtils;
//
// @Before
// public void setUp() {
// this.imageStreamReaderUtils = new ImageStreamReaderUtils();
// }
//
// @Test(expected = IllegalArgumentException.class)
// public void removePlaneBufferPadding_throwsIfPixelStrideInvalid() {
// Image.Plane planeU = mock(Image.Plane.class);
// when(planeU.getBuffer()).thenReturn(ByteBuffer.allocate(552703));
// when(planeU.getRowStride()).thenReturn(1536);
// when(planeU.getPixelStride()).thenReturn(2);
//
// imageStreamReaderUtils.removePlaneBufferPadding(planeU, 1280 / 2, 720 / 2);
// }
//
// // Values here are taken from a Vivo V2135 which adds padding in 1280x720 mode
// @Test
// public void removePlaneBufferPadding_removesPaddingCorrectly() {
// Image.Plane planeY = mock(Image.Plane.class);
// when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(1105664));
// when(planeY.getRowStride()).thenReturn(1536);
// when(planeY.getPixelStride()).thenReturn(1);
//
// byte[] fixed = imageStreamReaderUtils.removePlaneBufferPadding(planeY, 1280, 720);
//
// // After trimming the padding, the buffer size should match the image size
// Assert.assertEquals(fixed.length, 1280 * 720);
// Assert.assertNotEquals(fixed.length, planeY.getBuffer().limit());
// Assert.assertFalse(Arrays.equals(fixed, planeY.getBuffer().array()));
// }
//
// // Values here are taken from a Pixel 6 which does not add any padding.
// // In the event we pass a buffer with no padding, the returned buffer
// // should be identical to the source one because nothing is trimmed.
// @Test
// public void removePlaneBufferPadding_doesNothingIfThereIsNoPadding() {
// Image.Plane planeY = mock(Image.Plane.class);
// when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(921600));
// when(planeY.getRowStride()).thenReturn(1280);
// when(planeY.getPixelStride()).thenReturn(1);
//
// byte[] fixed = imageStreamReaderUtils.removePlaneBufferPadding(planeY, 1280, 720);
//
// // After trimming the padding, the buffer size should match the image size
// Assert.assertEquals(fixed.length, 1280 * 720);
// Assert.assertEquals(fixed.length, planeY.getBuffer().limit());
// Assert.assertArrayEquals(fixed, planeY.getBuffer().array());
// }
//}