This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera] Move camera streaming to platform interface #5783
Merged
fluttergithubbot
merged 10 commits into
flutter:main
from
stuartmorgan-g:camera-stream-platform-interface
May 25, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6fb3c24
Add platform interface versions of the image data classes
stuartmorgan-g d86e912
Platform interface package parts
stuartmorgan-g ea8c032
Rewire app-facing package
stuartmorgan-g 7a84131
Test update
stuartmorgan-g 3b6865c
Temporary pubspec overrides
stuartmorgan-g 7414e48
Version bumps
stuartmorgan-g b191e34
Future-proof the new API with an options dictionary
stuartmorgan-g 014f1c0
Review comments
stuartmorgan-g 701eefe
Revert app-facing portion
stuartmorgan-g 881786c
Merge branch 'main' into camera-stream-platform-interface
stuartmorgan-g 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
## NEXT | ||
## 2.2.0 | ||
|
||
* Adds image streaming to the platform interface. | ||
* Removes unnecessary imports. | ||
|
||
## 2.1.6 | ||
|
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
61 changes: 61 additions & 0 deletions
61
packages/camera/camera_platform_interface/lib/src/method_channel/type_conversion.dart
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,61 @@ | ||
// 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. | ||
|
||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
|
||
import '../types/types.dart'; | ||
|
||
/// Converts method channel call [data] for `receivedImageStreamData` to a | ||
/// [CameraImageData]. | ||
CameraImageData cameraImageFromPlatformData(Map<dynamic, dynamic> data) { | ||
return CameraImageData( | ||
format: _cameraImageFormatFromPlatformData(data['format']), | ||
height: data['height'] as int, | ||
width: data['width'] as int, | ||
lensAperture: data['lensAperture'] as double?, | ||
sensorExposureTime: data['sensorExposureTime'] as int?, | ||
sensorSensitivity: data['sensorSensitivity'] as double?, | ||
planes: List<CameraImagePlane>.unmodifiable( | ||
(data['planes'] as List<dynamic>).map<CameraImagePlane>( | ||
(dynamic planeData) => _cameraImagePlaneFromPlatformData( | ||
planeData as Map<dynamic, dynamic>)))); | ||
} | ||
|
||
CameraImageFormat _cameraImageFormatFromPlatformData(dynamic data) { | ||
return CameraImageFormat(_imageFormatGroupFromPlatformData(data), raw: data); | ||
} | ||
|
||
ImageFormatGroup _imageFormatGroupFromPlatformData(dynamic data) { | ||
if (defaultTargetPlatform == TargetPlatform.android) { | ||
switch (data) { | ||
case 35: // android.graphics.ImageFormat.YUV_420_888 | ||
return ImageFormatGroup.yuv420; | ||
case 256: // android.graphics.ImageFormat.JPEG | ||
return ImageFormatGroup.jpeg; | ||
} | ||
} | ||
|
||
if (defaultTargetPlatform == TargetPlatform.iOS) { | ||
switch (data) { | ||
case 875704438: // kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange | ||
return ImageFormatGroup.yuv420; | ||
|
||
case 1111970369: // kCVPixelFormatType_32BGRA | ||
return ImageFormatGroup.bgra8888; | ||
} | ||
} | ||
|
||
return ImageFormatGroup.unknown; | ||
} | ||
|
||
CameraImagePlane _cameraImagePlaneFromPlatformData(Map<dynamic, dynamic> data) { | ||
return CameraImagePlane( | ||
bytes: data['bytes'] as Uint8List, | ||
bytesPerPixel: data['bytesPerPixel'] as int?, | ||
bytesPerRow: data['bytesPerRow'] as int, | ||
height: data['height'] as int?, | ||
width: data['width'] as int?); | ||
} |
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
126 changes: 126 additions & 0 deletions
126
packages/camera/camera_platform_interface/lib/src/types/camera_image_data.dart
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,126 @@ | ||
// 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. | ||
|
||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
|
||
import '../../camera_platform_interface.dart'; | ||
|
||
/// Options for configuring camera streaming. | ||
/// | ||
/// Currently unused; this exists for future-proofing of the platform interface | ||
/// API. | ||
@immutable | ||
class CameraImageStreamOptions {} | ||
|
||
/// A single color plane of image data. | ||
/// | ||
/// The number and meaning of the planes in an image are determined by its | ||
/// format. | ||
@immutable | ||
class CameraImagePlane { | ||
/// Creates a new instance with the given bytes and optional metadata. | ||
const CameraImagePlane({ | ||
required this.bytes, | ||
required this.bytesPerRow, | ||
this.bytesPerPixel, | ||
this.height, | ||
this.width, | ||
}); | ||
|
||
/// Bytes representing this plane. | ||
final Uint8List bytes; | ||
|
||
/// The row stride for this color plane, in bytes. | ||
final int bytesPerRow; | ||
|
||
/// The distance between adjacent pixel samples in bytes, when available. | ||
final int? bytesPerPixel; | ||
|
||
/// Height of the pixel buffer, when available. | ||
final int? height; | ||
|
||
/// Width of the pixel buffer, when available. | ||
final int? width; | ||
} | ||
|
||
/// Describes how pixels are represented in an image. | ||
@immutable | ||
class CameraImageFormat { | ||
/// Create a new format with the given cross-platform group and raw underyling | ||
/// platform identifier. | ||
const CameraImageFormat(this.group, {required this.raw}); | ||
|
||
/// Describes the format group the raw image format falls into. | ||
final ImageFormatGroup group; | ||
|
||
/// Raw version of the format from the underlying platform. | ||
/// | ||
/// On Android, this should be an `int` from class | ||
/// `android.graphics.ImageFormat`. See | ||
/// https://developer.android.com/reference/android/graphics/ImageFormat | ||
/// | ||
/// On iOS, this should be a `FourCharCode` constant from Pixel Format | ||
/// Identifiers. See | ||
/// https://developer.apple.com/documentation/corevideo/1563591-pixel_format_identifiers | ||
final dynamic raw; | ||
} | ||
|
||
/// A single complete image buffer from the platform camera. | ||
/// | ||
/// This class allows for direct application access to the pixel data of an | ||
/// Image through one or more [Uint8List]. Each buffer is encapsulated in a | ||
/// [CameraImagePlane] that describes the layout of the pixel data in that | ||
/// plane. [CameraImageData] is not directly usable as a UI resource. | ||
/// | ||
/// Although not all image formats are planar on all platforms, this class | ||
/// treats 1-dimensional images as single planar images. | ||
@immutable | ||
class CameraImageData { | ||
/// Creates a new instance with the given format, planes, and metadata. | ||
const CameraImageData({ | ||
required this.format, | ||
required this.planes, | ||
required this.height, | ||
required this.width, | ||
this.lensAperture, | ||
this.sensorExposureTime, | ||
this.sensorSensitivity, | ||
}); | ||
|
||
/// Format of the image provided. | ||
/// | ||
/// Determines the number of planes needed to represent the image, and | ||
/// the general layout of the pixel data in each [Uint8List]. | ||
final CameraImageFormat format; | ||
|
||
/// Height of the image in pixels. | ||
/// | ||
/// For formats where some color channels are subsampled, this is the height | ||
/// of the largest-resolution plane. | ||
final int height; | ||
|
||
/// Width of the image in pixels. | ||
/// | ||
/// For formats where some color channels are subsampled, this is the width | ||
/// of the largest-resolution plane. | ||
final int width; | ||
|
||
/// The pixels planes for this image. | ||
/// | ||
/// The number of planes is determined by the format of the image. | ||
final List<CameraImagePlane> planes; | ||
|
||
/// The aperture settings for this image. | ||
/// | ||
/// Represented as an f-stop value. | ||
final double? lensAperture; | ||
|
||
/// The sensor exposure time for this image in nanoseconds. | ||
final int? sensorExposureTime; | ||
|
||
/// The sensor sensitivity in standard ISO arithmetic units. | ||
final double? sensorSensitivity; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1038,6 +1038,52 @@ void main() { | |
arguments: <String, Object?>{'cameraId': cameraId}), | ||
]); | ||
}); | ||
|
||
test('Should start streaming', () async { | ||
// Arrange | ||
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. I see that this is done throughout the entire file. I don't have any comments on it, it just seems unnecessary to do for each test. 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. I'm not a big fan of the commenting myself, although I can see the value in reminding people to always think this way in test structure (since it is a good structure, and I've seen plenty of tests that turn into soup when people don't follow it). I was just going for file-level consistency. |
||
final MethodChannelMock channel = MethodChannelMock( | ||
channelName: 'plugins.flutter.io/camera', | ||
methods: <String, dynamic>{ | ||
'startImageStream': null, | ||
'stopImageStream': null, | ||
}, | ||
); | ||
|
||
// Act | ||
final StreamSubscription<CameraImageData> subscription = camera | ||
.onStreamedFrameAvailable(cameraId) | ||
.listen((CameraImageData imageData) {}); | ||
|
||
// Assert | ||
expect(channel.log, <Matcher>[ | ||
isMethodCall('startImageStream', arguments: null), | ||
]); | ||
|
||
subscription.cancel(); | ||
}); | ||
|
||
test('Should stop streaming', () async { | ||
// Arrange | ||
final MethodChannelMock channel = MethodChannelMock( | ||
channelName: 'plugins.flutter.io/camera', | ||
methods: <String, dynamic>{ | ||
'startImageStream': null, | ||
'stopImageStream': null, | ||
}, | ||
); | ||
|
||
// Act | ||
final StreamSubscription<CameraImageData> subscription = camera | ||
.onStreamedFrameAvailable(cameraId) | ||
.listen((CameraImageData imageData) {}); | ||
subscription.cancel(); | ||
|
||
// Assert | ||
expect(channel.log, <Matcher>[ | ||
isMethodCall('startImageStream', arguments: null), | ||
isMethodCall('stopImageStream', arguments: null), | ||
]); | ||
}); | ||
}); | ||
}); | ||
} |
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'm confused by this name. Does the app-facing package notify the platform implementation package that a frame is available? Based on the
MethodChannel
implementation, it looks like this method creates the stream that the app-facing package listens to. Would a better name be something likecreateCameraImageDataStream
?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 don't like this name either; I used this name because it's the pattern used by all of the existing Streams in the interface
Do you want to pick a new naming scheme for platform interface streams in this plugin as part of this PR? If so I could make alternate named versions of all of the existing ones, doing passthroughs to the old names (which would be soft-deprecated with a comment), so that we'd get consistency.
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.
Ah, I see. I think I was confused by the call to startImageStream. I understand now that the app-facing package is adding a listener to the stream which is created by the platform implementation. I think the current name is appropriate for how it is being used then. Streams and callbacks in Flutter tend to use the
on<event-name>
pattern like the one here.